Skip to content

Commit

Permalink
node: Drop support of node below 16 and support for node 20 #30
Browse files Browse the repository at this point in the history
  • Loading branch information
JunkyDeLuxe committed Oct 13, 2023
1 parent 23c665b commit 0e6f807
Show file tree
Hide file tree
Showing 8 changed files with 3,890 additions and 2,144 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extends:
- './node_modules/@neo9/n9-coding-style/.eslintrc.yaml'
10 changes: 8 additions & 2 deletions .github/workflows/node.js.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
node-version: [16.x, 18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand All @@ -28,12 +28,18 @@ jobs:
- name: Setup Nodejs with yarn caching
uses: actions/setup-node@v2
with:
node-version: '14'
node-version: ${{ matrix.node-version }}
cache: yarn

- name: Install dependencies
run: yarn

- name: Lint
run: yarn lint

- name: Build
run: yarn build

- name: Test
run: yarn test

Expand Down
48 changes: 21 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
"dist/src"
],
"engines": {
"node": ">=12.0.0"
"node": ">=16.0.0"
},
"scripts": {
"build": "rimraf dist/ && tsc",
"dev": "rimraf dist/ && tsc --watch",
"format": "prettier --write '{,@(src|test)/**/}*.*'",
"lint": "tslint --project tsconfig.json --format verbose '{src,test}/**/*.ts'",
"lint:apply": "npm run format && tslint --fix --project tsconfig.json '{src,test}/**/*.ts'",
"lint:verbose": "tslint --fix --project tsconfig.json --format verbose '{src,test}/**/*.ts'",
"lint": "eslint --config .eslintrc.yaml '{src,test}/**/*.ts'",
"lint:apply": "npm run format && npx eslint --fix '{src,test}/**/*.ts'",
"test:dev": "export NODE_ENV=test && TS_NODE_FILES=true ava --verbose --color --serial --watch",
"test": "npm run lint && npm run build && export NODE_ENV=test && TS_NODE_FILES=true nyc ava --verbose --color --serial && nyc report --reporter=html",
"test:watch": "node test/run-tests.js watch",
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov -f coverage.lcov",
"test": "export NODE_ENV=test && TS_NODE_FILES=true nyc ava --verbose --color --serial && nyc report --reporter=html",
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"preversion": "npm test",
"prepublishOnly": "npm run build",
"release": "export BABEL_DISABLE_CACHE=1 && release-it --ci",
Expand All @@ -32,27 +30,24 @@
"type": "git",
"url": "git+https://github.com/neo9/n9-node-utils"
},
"author": "Sebastien Chopin <sebastien.chopin@neo9.fr>",
"author": "Neo9 team",
"license": "MIT",
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@commitlint/cli": "^12.1.4",
"@neo9/n9-coding-style": "^2.0.0",
"@release-it/conventional-changelog": "^2.0.1",
"@tsconfig/node10": "^1.0.7",
"@ava/typescript": "^4.1.0",
"@commitlint/cli": "^17.7.1",
"@neo9/n9-coding-style": "^5.1.2",
"@release-it/conventional-changelog": "^7.0.1",
"@types/express": "^4.17.11",
"@types/node": "^15.3.0",
"ava": "^3.15.0",
"codecov": "^3.8.2",
"husky": "^4.3.0",
"@types/node": "^20.5.9",
"ava": "^5.3.1",
"codecov": "^3.8.3",
"husky": "4.3.8",
"nyc": "^15.1.0",
"prettier": "^2.3.0",
"release-it": "^14.6.2",
"rimraf": "^3.0.2",
"ts-node": "^9.1.1",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.3.0",
"typescript": "^4.2.4"
"prettier": "^3.0.3",
"release-it": "^16.1.5",
"rimraf": "^5.0.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
"keywords": [
"node utils",
Expand All @@ -77,7 +72,6 @@
],
"require": [
"ts-node/register"
],
"all": true
]
}
}
27 changes: 18 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { EventEmitter } from 'events';
import { NextFunction, Response } from 'express';
import * as os from 'os';
import { Transform } from 'stream';
import { promisify } from 'util';

const sleep = promisify(setTimeout);

/**
* N9Error(message [, status] [, context])
Expand Down Expand Up @@ -86,7 +89,9 @@ export async function cb<T = any>(fn: (...args: any[]) => any, ...args: any[]):
** waitFor(ms)
*/
export async function waitFor(ms?: number): Promise<any> {
return new Promise((resolve) => setTimeout(resolve, ms || 0));
await sleep(ms || 0);

return;
}

/*
Expand All @@ -109,7 +114,7 @@ export async function asyncObject(
);
const promises = keys.map((key) => obj[key]);
const results = await Promise.all(promises);
const container = Object.assign({}, obj);
const container = { ...obj };
results.forEach((result, index) => {
const key = keys[index];
container[key] = result;
Expand Down Expand Up @@ -154,15 +159,16 @@ export class N9JSONStream<T = object, M = any> extends Transform {
/**
* Usage example :
* ```
* const cursor = await this.service.find(filter, page, size);
* return cursor.pipe(
* new N9JSONStream({
* res, // express response
* total: await cursor.count(),
* }),
* );
* const cursor = await this.service.find(filter, page, size);
* return cursor.pipe(
* new N9JSONStream({
* res, // express response
* total: await cursor.count(),
* }),
* );
*
* ```
*
* @param options should give at least Express Response to stream in, and the total number of elements
*/
constructor(options: N9JSONStreamOptions<M>) {
Expand All @@ -174,9 +180,11 @@ export class N9JSONStream<T = object, M = any> extends Transform {
if (typeof options.total === 'undefined') {
throw new N9Error(`'total' property is required to N9JSONStream class`);
}

if (options.res) {
options.res.setHeader('Content-Type', 'application/json; charset=utf-8');
}

this.first = true;
this.base = {
total: options.total,
Expand Down Expand Up @@ -216,6 +224,7 @@ export class N9JSONStream<T = object, M = any> extends Transform {
if (typeof value === 'object') {
this.push(`"${key}": ${JSON.stringify(value)}`);
} else {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
this.push(`"${key}": ${value}`);
}
if (i < keysWithValue.length - 1) this.push(',');
Expand Down
2 changes: 1 addition & 1 deletion test/cb.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ava from 'ava';
import ava from 'ava';

import { cb } from '../src';

Expand Down
6 changes: 4 additions & 2 deletions test/n9-json-stream.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ava from 'ava';
import * as stream from 'stream';

import { N9JSONStream, N9JSONStreamResponse, waitFor } from '../src';

ava.serial('Return array ', async (t) => {
Expand All @@ -20,7 +21,8 @@ ava.serial('Return array ', async (t) => {
});

const items = [{ _id: 'a' }, { _id: 'b' }, { _id: 'c' }, { _id: 'd' }];
items.forEach((item) => readable.push(item.toString()));
items.forEach((item) => readable.push(JSON.stringify(item)));

// no more data
readable.push(null);

Expand All @@ -33,5 +35,5 @@ ava.serial('Return array ', async (t) => {
});

ava.serial('Throw error with wrong params ', async (t) => {
await t.throws(() => new N9JSONStream({ total: undefined as any }));
await Promise.resolve(t.throws(() => new N9JSONStream({ total: undefined as any })));
});
9 changes: 0 additions & 9 deletions tslint.yaml

This file was deleted.

Loading

0 comments on commit 0e6f807

Please sign in to comment.