From 918fe055624c144e8fade2280ee9e5544a17270f Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 14 Jun 2023 14:08:00 +0300 Subject: [PATCH] Require Node.js 16 --- .github/workflows/main.yml | 5 +++-- index.d.ts | 37 ++++++++++++++++--------------------- index.js | 12 ++++++++---- index.test-d.ts | 6 +++--- package.json | 24 ++++++++++++++++-------- 5 files changed, 46 insertions(+), 38 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 441975c..6a82b18 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,10 +10,11 @@ jobs: fail-fast: false matrix: node-version: + - 18 - 16 steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.d.ts b/index.d.ts index 9d13ba0..efbaa6c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,24 +3,24 @@ export type AddRemoveListener void ) => void; -export interface Emitter { +export type Emitter = { on?: AddRemoveListener; addListener?: AddRemoveListener; addEventListener?: AddRemoveListener; off?: AddRemoveListener; removeListener?: AddRemoveListener; removeEventListener?: AddRemoveListener; -} +}; export type FilterFunction = ( value: ElementType ) => boolean; -export interface CancelablePromise extends Promise { +export type CancelablePromise = { cancel(): void; -} +} & Promise; -export interface Options { +export type Options = { /** Events that will reject the promise. @@ -68,15 +68,13 @@ export interface Options { An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to abort waiting for the event. */ readonly signal?: AbortSignal; -} +}; -export interface MultiArgumentsOptions - extends Options { +export type MultiArgumentsOptions = { readonly multiArgs: true; -} +} & Options; -export interface MultipleOptions - extends Options { +export type MultipleOptions = { /** The number of times the event needs to be emitted before the promise resolves. */ @@ -119,15 +117,13 @@ export interface MultipleOptions ``` */ readonly resolveImmediately?: boolean; -} +} & Options; -export interface MultipleMultiArgumentsOptions - extends MultipleOptions { +export type MultipleMultiArgumentsOptions = { readonly multiArgs: true; -} +} & MultipleOptions; -export interface IteratorOptions - extends Options { +export type IteratorOptions = { /** The maximum number of events for the iterator before it ends. When the limit is reached, the iterator will be marked as `done`. This option is useful to paginate events, for example, fetching 10 events per page. @@ -141,12 +137,11 @@ export interface IteratorOptions @default [] */ readonly resolutionEvents?: ReadonlyArray; -} +} & Options; -export interface IteratorMultiArgumentsOptions - extends IteratorOptions { +export type IteratorMultiArgumentsOptions = { multiArgs: true; -} +} & IteratorOptions; /** Promisify an event by waiting for it to be emitted. diff --git a/index.js b/index.js index 056c2a3..8d29fe4 100644 --- a/index.js +++ b/index.js @@ -76,7 +76,9 @@ export function pEventMultiple(emitter, event, options) { } if (options.signal) { - options.signal.addEventListener('abort', () => rejectHandler(options.signal.reason), {once: true}); + options.signal.addEventListener('abort', () => { + rejectHandler(options.signal.reason); + }, {once: true}); } if (options.resolveImmediately) { @@ -87,7 +89,7 @@ export function pEventMultiple(emitter, event, options) { returnValue.cancel = cancel; if (typeof options.timeout === 'number') { - const timeout = pTimeout(returnValue, options.timeout); + const timeout = pTimeout(returnValue, {milliseconds: options.timeout}); timeout.cancel = cancel; return timeout; } @@ -107,7 +109,7 @@ export function pEvent(emitter, event, options) { }; const arrayPromise = pEventMultiple(emitter, event, options); - const promise = arrayPromise.then(array => array[0]); // eslint-disable-line promise/prefer-await-to-then + const promise = arrayPromise.then(array => array[0]); promise.cancel = arrayPromise.cancel; return promise; @@ -253,7 +255,9 @@ export function pEventIterator(emitter, event, options) { } if (options.signal) { - options.signal.addEventListener('abort', () => rejectHandler(options.signal.reason), {once: true}); + options.signal.addEventListener('abort', () => { + rejectHandler(options.signal.reason); + }, {once: true}); } return { diff --git a/index.test-d.ts b/index.test-d.ts index e569bbe..17ed447 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -2,7 +2,7 @@ import process from 'node:process'; import {EventEmitter} from 'node:events'; import fs from 'node:fs'; import {expectType} from 'tsd'; -import {pEvent, pEventMultiple, pEventIterator, CancelablePromise} from './index.js'; +import {pEvent, pEventMultiple, pEventIterator, type CancelablePromise} from './index.js'; class NodeEmitter extends EventEmitter { on(_event: 'finish', _listener: (number: number, string: string) => void) { @@ -129,9 +129,9 @@ void pEventIterator(new NodeEmitter(), 'finish', { }); async function getOpenReadStream(file: string): Promise { - const stream = fs.createReadStream(file); // eslint-disable-line @typescript-eslint/no-unsafe-assignment + const stream = fs.createReadStream(file) as NodeJS.ReadableStream; await pEvent(stream, 'open'); - return stream; // eslint-disable-line @typescript-eslint/no-unsafe-return + return stream; } const stream = await getOpenReadStream('unicorn.txt'); diff --git a/package.json b/package.json index da50177..7e5ae91 100644 --- a/package.json +++ b/package.json @@ -11,9 +11,12 @@ "url": "https://sindresorhus.com" }, "type": "module", - "exports": "./index.js", + "exports": { + "types": "./index.d.ts", + "default": "./index.js" + }, "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + "node": ">=16.17" }, "scripts": { "test": "xo && ava && tsd" @@ -46,13 +49,18 @@ "bluebird" ], "dependencies": { - "p-timeout": "^5.0.2" + "p-timeout": "^6.1.2" }, "devDependencies": { - "@types/node": "^16.11.6", - "ava": "^3.15.0", - "delay": "^5.0.0", - "tsd": "^0.18.0", - "xo": "^0.45.0" + "@types/node": "^20.3.1", + "ava": "^5.3.0", + "delay": "^6.0.0", + "tsd": "^0.28.1", + "xo": "^0.54.2" + }, + "xo": { + "rules": { + "@typescript-eslint/no-redundant-type-constituents": "off" + } } }