Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 68dfe95

Browse filesBrowse files
committed
Require Node.js 18
1 parent ce0c364 commit 68dfe95
Copy full SHA for 68dfe95

File tree

Expand file treeCollapse file tree

10 files changed

+58
-73
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

10 files changed

+58
-73
lines changed
Open diff view settings
Collapse file

‎.github/funding.yml‎

Copy file name to clipboardExpand all lines: .github/funding.yml
-2Lines changed: 0 additions & 2 deletions
This file was deleted.
Collapse file

‎.github/workflows/main.yml‎

Copy file name to clipboardExpand all lines: .github/workflows/main.yml
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ jobs:
1212
node-version:
1313
- 16
1414
steps:
15-
- uses: actions/checkout@v2
16-
- uses: actions/setup-node@v2
15+
- uses: actions/checkout@v4
16+
- uses: actions/setup-node@v4
1717
with:
1818
node-version: ${{ matrix.node-version }}
1919
- run: npm install
Collapse file

‎bench.ts‎

Copy file name to clipboardExpand all lines: bench.ts
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Benchmark, {Deferred, Event} from 'benchmark';
1+
import Benchmark, {type Deferred, type Event} from 'benchmark';
22
import PQueue from './source/index.js';
33

44
const suite = new Benchmark.Suite();
@@ -10,7 +10,7 @@ suite
1010
.add('baseline', {
1111
defer: true,
1212

13-
fn: async (deferred: Resolvable) => {
13+
async fn(deferred: Resolvable) {
1414
const queue = new PQueue();
1515

1616
for (let i = 0; i < 100; i++) {
@@ -25,7 +25,7 @@ suite
2525
.add('operation with random priority', {
2626
defer: true,
2727

28-
fn: async (deferred: Resolvable) => {
28+
async fn(deferred: Resolvable) {
2929
const queue = new PQueue();
3030

3131
for (let i = 0; i < 100; i++) {
@@ -42,7 +42,7 @@ suite
4242
.add('operation with increasing priority', {
4343
defer: true,
4444

45-
fn: async (deferred: Resolvable) => {
45+
async fn(deferred: Resolvable) {
4646
const queue = new PQueue();
4747

4848
for (let i = 0; i < 100; i++) {
Collapse file

‎package.json‎

Copy file name to clipboardExpand all lines: package.json
+21-17Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
"repository": "sindresorhus/p-queue",
77
"funding": "https://github.com/sponsors/sindresorhus",
88
"type": "module",
9-
"exports": "./dist/index.js",
9+
"exports": {
10+
"types": "./dist/index.d.ts",
11+
"default": "./dist/index.js"
12+
},
13+
"sideEffects": false,
1014
"engines": {
11-
"node": ">=12"
15+
"node": ">=18"
1216
},
1317
"scripts": {
1418
"build": "del-cli dist && tsc",
15-
"//test": "xo && ava && del-cli dist && tsc && tsd",
16-
"test": "ava && del-cli dist && tsc && tsd",
17-
"bench": "node --loader=ts-node/esm bench.ts",
19+
"test": "xo && ava && del-cli dist && tsc && tsd",
20+
"bench": "node --import=tsx/esm bench.ts",
1821
"prepublishOnly": "del-cli dist && tsc"
1922
},
2023
"files": [
@@ -45,34 +48,35 @@
4548
],
4649
"dependencies": {
4750
"eventemitter3": "^5.0.1",
48-
"p-timeout": "^5.0.2"
51+
"p-timeout": "^6.1.2"
4952
},
5053
"devDependencies": {
51-
"@sindresorhus/tsconfig": "^2.0.0",
52-
"@types/benchmark": "^2.1.1",
53-
"@types/node": "^17.0.13",
54+
"@sindresorhus/tsconfig": "^5.0.0",
55+
"@types/benchmark": "^2.1.5",
56+
"@types/node": "^20.10.4",
5457
"ava": "^5.3.1",
5558
"benchmark": "^2.1.4",
56-
"del-cli": "^5.0.0",
57-
"delay": "^5.0.0",
59+
"del-cli": "^5.1.0",
60+
"delay": "^6.0.0",
5861
"in-range": "^3.0.0",
5962
"p-defer": "^4.0.0",
6063
"random-int": "^3.0.0",
61-
"time-span": "^5.0.0",
62-
"ts-node": "^10.9.1",
63-
"tsd": "^0.25.0",
64-
"typescript": "^5.2.2",
65-
"xo": "^0.52.0"
64+
"time-span": "^5.1.0",
65+
"tsd": "^0.29.0",
66+
"tsx": "^4.6.2",
67+
"typescript": "^5.3.3",
68+
"xo": "^0.56.0"
6669
},
6770
"ava": {
71+
"workerThreads": false,
6872
"files": [
6973
"test/**"
7074
],
7175
"extensions": {
7276
"ts": "module"
7377
},
7478
"nodeArguments": [
75-
"--loader=ts-node/esm"
79+
"--import=tsx/esm"
7680
]
7781
},
7882
"xo": {
Collapse file

‎readme.md‎

Copy file name to clipboardExpand all lines: readme.md
+4-10Lines changed: 4 additions & 10 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Note that the project is feature complete. We are happy to review pull requests,
1414
npm install p-queue
1515
```
1616

17-
**Warning:** This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and no longer provides a CommonJS export. If your project uses CommonJS, you'll have to [convert to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) or use the [dynamic `import()`](https://v8.dev/features/dynamic-import) function. Please don't open issues for questions regarding CommonJS / ESM.
17+
**Warning:** This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and no longer provides a CommonJS export. If your project uses CommonJS, you'll have to [convert to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). Please don't open issues for questions regarding CommonJS / ESM.
1818

1919
## Usage
2020

@@ -139,12 +139,10 @@ Priority of operation. Operations with greater priority will be scheduled first.
139139

140140
##### signal
141141

142-
*Requires Node.js 16 or later.*
143-
144-
[`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for cancellation of the operation. When aborted, it will be removed from the queue and the `queue.add()` call will reject with an `AbortError`. If the operation is already running, the signal will need to be handled by the operation itself.
142+
[`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for cancellation of the operation. When aborted, it will be removed from the queue and the `queue.add()` call will reject with an [error](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/reason). If the operation is already running, the signal will need to be handled by the operation itself.
145143

146144
```js
147-
import PQueue, {AbortError} from 'p-queue';
145+
import PQueue from 'p-queue';
148146
import got, {CancelError} from 'got';
149147

150148
const queue = new PQueue();
@@ -168,7 +166,7 @@ try {
168166
}
169167
}, {signal: controller.signal});
170168
} catch (error) {
171-
if (!(error instanceof AbortError)) {
169+
if (!(error instanceof DOMException)) {
172170
throw error;
173171
}
174172
}
@@ -378,10 +376,6 @@ await queue.add(() => delay(600));
378376
//=> 'Task is completed. Size: 0 Pending: 0'
379377
```
380378

381-
### AbortError
382-
383-
The error thrown by `queue.add()` when a job is aborted before it is run. See [`signal`](#signal).
384-
385379
## Advanced example
386380

387381
A more advanced example to help you understand the flow.
Collapse file

‎source/index.ts‎

Copy file name to clipboardExpand all lines: source/index.ts
+9-20Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
import {EventEmitter} from 'eventemitter3';
22
import pTimeout, {TimeoutError} from 'p-timeout';
3-
import {Queue, RunFunction} from './queue.js';
3+
import {type Queue, type RunFunction} from './queue.js';
44
import PriorityQueue from './priority-queue.js';
5-
import {QueueAddOptions, Options, TaskOptions} from './options.js';
5+
import {type QueueAddOptions, type Options, type TaskOptions} from './options.js';
66

77
type Task<TaskResultType> =
88
| ((options: TaskOptions) => PromiseLike<TaskResultType>)
99
| ((options: TaskOptions) => TaskResultType);
1010

11-
/**
12-
The error thrown by `queue.add()` when a job is aborted before it is run. See `signal`.
13-
*/
14-
export class AbortError extends Error {}
15-
1611
type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error';
1712

1813
/**
1914
Promise queue with concurrency control.
2015
*/
21-
export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue, EnqueueOptionsType extends QueueAddOptions = QueueAddOptions> extends EventEmitter<EventName> {
16+
export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue, EnqueueOptionsType extends QueueAddOptions = QueueAddOptions> extends EventEmitter<EventName> { // eslint-disable-line @typescript-eslint/naming-convention, unicorn/prefer-event-target
2217
readonly #carryoverConcurrencyCount: boolean;
2318

2419
readonly #isIntervalIgnored: boolean;
@@ -228,9 +223,7 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
228223
async #throwOnAbort(signal: AbortSignal): Promise<never> {
229224
return new Promise((_resolve, reject) => {
230225
signal.addEventListener('abort', () => {
231-
// TODO: Reject with signal.throwIfAborted() when targeting Node.js 18
232-
// TODO: Use ABORT_ERR code when targeting Node.js 16 (https://nodejs.org/docs/latest-v16.x/api/errors.html#abort_err)
233-
reject(new AbortError('The task was aborted.'));
226+
reject(signal.reason);
234227
}, {once: true});
235228
});
236229
}
@@ -253,16 +246,12 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
253246
this.#intervalCount++;
254247

255248
try {
256-
// TODO: Use options.signal?.throwIfAborted() when targeting Node.js 18
257-
if (options.signal?.aborted) {
258-
// TODO: Use ABORT_ERR code when targeting Node.js 16 (https://nodejs.org/docs/latest-v16.x/api/errors.html#abort_err)
259-
throw new AbortError('The task was aborted.');
260-
}
249+
options.signal?.throwIfAborted();
261250

262251
let operation = function_({signal: options.signal});
263252

264253
if (options.timeout) {
265-
operation = pTimeout(Promise.resolve(operation), options.timeout);
254+
operation = pTimeout(Promise.resolve(operation), {milliseconds: options.timeout});
266255
}
267256

268257
if (options.signal) {
@@ -299,7 +288,7 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
299288
async addAll<TaskResultsType>(
300289
functions: ReadonlyArray<Task<TaskResultsType>>,
301290
options?: {throwOnTimeout: true} & Partial<Exclude<EnqueueOptionsType, 'throwOnTimeout'>>,
302-
): Promise<TaskResultsType[]>
291+
): Promise<TaskResultsType[]>;
303292
async addAll<TaskResultsType>(
304293
functions: ReadonlyArray<Task<TaskResultsType>>,
305294
options?: Partial<EnqueueOptionsType>,
@@ -430,5 +419,5 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
430419
}
431420
}
432421

433-
// TODO: Rename `DefaultAddOptions` to `QueueAddOptions` in next major version
434-
export {Queue, QueueAddOptions, QueueAddOptions as DefaultAddOptions, Options};
422+
export type {Queue} from './queue.js';
423+
export {type QueueAddOptions, type Options} from './options.js';
Collapse file

‎source/options.ts‎

Copy file name to clipboardExpand all lines: source/options.ts
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {Queue, RunFunction} from './queue.js';
1+
import {type Queue, type RunFunction} from './queue.js';
22

3-
interface TimeoutOptions {
3+
type TimeoutOptions = {
44
/**
55
Per-operation timeout in milliseconds. Operations fulfill once `timeout` elapses if they haven't already.
66
*/
@@ -12,9 +12,9 @@ interface TimeoutOptions {
1212
@default false
1313
*/
1414
throwOnTimeout?: boolean;
15-
}
15+
};
1616

17-
export interface Options<QueueType extends Queue<RunFunction, QueueOptions>, QueueOptions extends QueueAddOptions> extends TimeoutOptions {
17+
export type Options<QueueType extends Queue<RunFunction, QueueOptions>, QueueOptions extends QueueAddOptions> = {
1818
/**
1919
Concurrency limit.
2020
@@ -60,18 +60,18 @@ export interface Options<QueueType extends Queue<RunFunction, QueueOptions>, Que
6060
@default false
6161
*/
6262
readonly carryoverConcurrencyCount?: boolean;
63-
}
63+
} & TimeoutOptions;
6464

65-
export interface QueueAddOptions extends TaskOptions, TimeoutOptions {
65+
export type QueueAddOptions = {
6666
/**
6767
Priority of operation. Operations with greater priority will be scheduled first.
6868
6969
@default 0
7070
*/
7171
readonly priority?: number;
72-
}
72+
} & TaskOptions & TimeoutOptions;
7373

74-
export interface TaskOptions {
74+
export type TaskOptions = {
7575
/**
7676
[`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for cancellation of the operation. When aborted, it will be removed from the queue and the `queue.add()` call will reject with an `AbortError`. If the operation is already running, the signal will need to be handled by the operation itself.
7777
@@ -108,4 +108,4 @@ export interface TaskOptions {
108108
```
109109
*/
110110
readonly signal?: AbortSignal;
111-
}
111+
};
Collapse file

‎source/priority-queue.ts‎

Copy file name to clipboardExpand all lines: source/priority-queue.ts
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {Queue, RunFunction} from './queue.js';
1+
import {type Queue, type RunFunction} from './queue.js';
22
import lowerBound from './lower-bound.js';
3-
import {QueueAddOptions} from './options.js';
3+
import {type QueueAddOptions} from './options.js';
44

5-
export interface PriorityQueueOptions extends QueueAddOptions {
5+
export type PriorityQueueOptions = {
66
priority?: number;
7-
}
7+
} & QueueAddOptions;
88

99
export default class PriorityQueue implements Queue<RunFunction, PriorityQueueOptions> {
1010
readonly #queue: Array<PriorityQueueOptions & {run: RunFunction}> = [];
Collapse file

‎source/queue.ts‎

Copy file name to clipboard
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export type RunFunction = () => Promise<unknown>;
22

3-
export interface Queue<Element, Options> {
3+
export type Queue<Element, Options> = {
44
size: number;
55
filter: (options: Partial<Options>) => Element[];
66
dequeue: () => Element | undefined;
77
enqueue: (run: Element, options?: Partial<Options>) => void;
8-
}
8+
};
Collapse file

‎test/test.ts‎

Copy file name to clipboardExpand all lines: test/test.ts
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ test('should skip an aborted job', async t => {
11031103
controller.abort();
11041104
// eslint-disable-next-line @typescript-eslint/no-empty-function
11051105
await t.throwsAsync(queue.add(() => {}, {signal: controller.signal}), {
1106-
instanceOf: AbortError,
1106+
instanceOf: DOMException,
11071107
});
11081108
});
11091109

@@ -1130,7 +1130,7 @@ test('aborting multiple jobs at the same time', async t => {
11301130
controller2.abort();
11311131
}, 0);
11321132

1133-
await t.throwsAsync(task1, {instanceOf: AbortError});
1134-
await t.throwsAsync(task2, {instanceOf: AbortError});
1133+
await t.throwsAsync(task1, {instanceOf: DOMException});
1134+
await t.throwsAsync(task2, {instanceOf: DOMException});
11351135
t.like(queue, {size: 0, pending: 0});
11361136
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.