Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps): bump got and xo #13

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Node.js CI

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run lint
- run: npm test -- --coverage --maxWorkers 2
- name: Upload results to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ temp/
# Node.js #
###################
node_modules
package-lock.json


# NYC #
Expand Down
12 changes: 0 additions & 12 deletions .nycrc

This file was deleted.

10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

106 changes: 106 additions & 0 deletions format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const util = require('util');

Check failure on line 1 in format.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Prefer `node:util` over `util`.

Check failure on line 1 in format.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Prefer `node:util` over `util`.

Check failure on line 1 in format.js

View workflow job for this annotation

GitHub Actions / build (22.x)

Prefer `node:util` over `util`.

/**
* Pads the provided number into a string of 2 digits; adding leading
* zeros if needed.
* Values resulting in more than 2 digits are left untouched.
*
* @param {string} number
* @returns A two-digit number
* @throws {TypeError} if the provided value is not a valid integer
*/
function toTwoDigits(number) {
if (!Number.isInteger(number)) {
throw new TypeError(`Not a valid integer: ${number}`);
}

return number.toString().padStart(2, '0');
}

/**
* Look-up map of month number into month short name (in english).
* A month number is the value returned by Date#getMonth() and a short month name
* is a 3 letter representation of a month of the year.
*/
const shortMonthByMonthNumber = {
0: 'Jan',
1: 'Feb',
2: 'Mar',
3: 'Apr',
4: 'May',
5: 'Jun',
6: 'Jul',
7: 'Aug',
8: 'Sep',
9: 'Oct',
10: 'Nov',
11: 'Dec'
};

/**
* Returns a [short](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#month)
* (3-letters) representation of the given month index **in English**.
*
* @param {number} month
* @returns {string}
* @throws {TypeError} if the provided value is not a valid month number
*/
function toShortMonth(month) {
if (!(month in shortMonthByMonthNumber)) {
throw new TypeError(`Not a valid month value: ${month}`);
}

return shortMonthByMonthNumber[month];
}

/**
* Returns a [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) compliant
* offset string.
*
* @returns {string}
* @throws {TypeError} if the provided value is not a valid integer
*/
function toOffset(offsetMinutes) {
if (!Number.isInteger(offsetMinutes)) {
throw new TypeError(`Not a valid integer: ${offsetMinutes}`);
}

const absoluteOffset = Math.abs(offsetMinutes);
const hours = toTwoDigits(Math.floor(absoluteOffset / 60));
const minutes = toTwoDigits(absoluteOffset % 60);
const sign = offsetMinutes >= 0 ? '-' : '+';

return `${sign}${hours}${minutes}`;
}

/**
* Formats the provided date into a [Common Log Format](https://en.wikipedia.org/wiki/Common_Log_Format)
* compliant string.
*
* @param {Date} date
* @returns {string}
*/
function toCommonAccessLogDateFormat(date) {
if (!(date instanceof Date)) {
throw new TypeError('Not a valid date');
}

// e.g: 10/Oct/2000:13:55:36 -0700
return util.format(
'%s/%s/%s:%s:%s:%s %s',
toTwoDigits(date.getDate()),
toShortMonth(date.getMonth()),
date.getFullYear(),
toTwoDigits(date.getHours()),
toTwoDigits(date.getMinutes()),
toTwoDigits(date.getSeconds()),
toOffset(date.getTimezoneOffset())
);
}

module.exports = {
toCommonAccessLogDateFormat,
toOffset,
toShortMonth,
toTwoDigits
};
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const util = require('util');

Check failure on line 1 in index.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Prefer `node:util` over `util`.

Check failure on line 1 in index.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Prefer `node:util` over `util`.

Check failure on line 1 in index.js

View workflow job for this annotation

GitHub Actions / build (22.x)

Prefer `node:util` over `util`.
const moment = require('moment');
const { toCommonAccessLogDateFormat } = require('./format');

module.exports = function (stream) {

Check failure on line 4 in index.js

View workflow job for this annotation

GitHub Actions / build (18.x)

The function should be named.

Check failure on line 4 in index.js

View workflow job for this annotation

GitHub Actions / build (20.x)

The function should be named.

Check failure on line 4 in index.js

View workflow job for this annotation

GitHub Actions / build (22.x)

The function should be named.
if (!stream) stream = process.stdout;

Check failure on line 5 in index.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'if' statement can be replaced with a logical operator assignment with operator ||=.

Check failure on line 5 in index.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected use of the global variable 'process'. Use 'require("process")' instead.

Check failure on line 5 in index.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'if' statement can be replaced with a logical operator assignment with operator ||=.

Check failure on line 5 in index.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected use of the global variable 'process'. Use 'require("process")' instead.

Check failure on line 5 in index.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'if' statement can be replaced with a logical operator assignment with operator ||=.

Check failure on line 5 in index.js

View workflow job for this annotation

GitHub Actions / build (22.x)

Unexpected use of the global variable 'process'. Use 'require("process")' instead.

// eslint-disable-next-line func-names
return async function accesslog(ctx, next) {
Expand All @@ -13,7 +13,7 @@

// eslint-disable-next-line unicorn/explicit-length-check
const length = ctx.length ? ctx.length.toString() : '-';
const date = moment().format('D/MMM/YYYY:HH:mm:ss ZZ');
const date = toCommonAccessLogDateFormat(new Date());

stream.write(
util.format(
Expand Down
Loading
Loading