@depup/p-queue 9.1.1-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -0
- package/changes.json +14 -0
- package/dist/index.d.ts +259 -0
- package/dist/index.js +790 -0
- package/dist/lower-bound.d.ts +1 -0
- package/dist/lower-bound.js +18 -0
- package/dist/options.d.ts +137 -0
- package/dist/options.js +1 -0
- package/dist/priority-queue.d.ts +15 -0
- package/dist/priority-queue.js +47 -0
- package/dist/queue.d.ts +9 -0
- package/dist/queue.js +1 -0
- package/license +9 -0
- package/package.json +105 -0
- package/readme.md +1077 -0
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @depup/p-queue
|
|
2
|
+
|
|
3
|
+
> Dependency-bumped version of [p-queue](https://www.npmjs.com/package/p-queue)
|
|
4
|
+
|
|
5
|
+
Generated by [DepUp](https://github.com/depup/npm) -- all production
|
|
6
|
+
dependencies bumped to latest versions.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @depup/p-queue
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
| Field | Value |
|
|
15
|
+
|-------|-------|
|
|
16
|
+
| Original | [p-queue](https://www.npmjs.com/package/p-queue) @ 9.1.1 |
|
|
17
|
+
| Processed | 2026-04-01 |
|
|
18
|
+
| Smoke test | passed |
|
|
19
|
+
| Deps updated | 2 |
|
|
20
|
+
|
|
21
|
+
## Dependency Changes
|
|
22
|
+
|
|
23
|
+
| Dependency | From | To |
|
|
24
|
+
|------------|------|-----|
|
|
25
|
+
| eventemitter3 | ^5.0.1 | ^5.0.4 |
|
|
26
|
+
| p-timeout | ^7.0.0 | ^7.0.1 |
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
Source: https://github.com/depup/npm | Original: https://www.npmjs.com/package/p-queue
|
|
31
|
+
|
|
32
|
+
License inherited from the original package.
|
package/changes.json
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { EventEmitter } from 'eventemitter3';
|
|
2
|
+
import { type Queue, type RunFunction } from './queue.js';
|
|
3
|
+
import PriorityQueue from './priority-queue.js';
|
|
4
|
+
import { type QueueAddOptions, type Options, type TaskOptions } from './options.js';
|
|
5
|
+
type Task<TaskResultType> = ((options: TaskOptions) => PromiseLike<TaskResultType>) | ((options: TaskOptions) => TaskResultType);
|
|
6
|
+
type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error' | 'pendingZero' | 'rateLimit' | 'rateLimitCleared';
|
|
7
|
+
/**
|
|
8
|
+
Promise queue with concurrency control.
|
|
9
|
+
*/
|
|
10
|
+
export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue, EnqueueOptionsType extends QueueAddOptions = QueueAddOptions> extends EventEmitter<EventName> {
|
|
11
|
+
#private;
|
|
12
|
+
/**
|
|
13
|
+
Get or set the default timeout for all tasks. Can be changed at runtime.
|
|
14
|
+
|
|
15
|
+
Operations will throw a `TimeoutError` if they don't complete within the specified time.
|
|
16
|
+
|
|
17
|
+
The timeout begins when the operation is dequeued and starts execution, not while it's waiting in the queue.
|
|
18
|
+
|
|
19
|
+
@example
|
|
20
|
+
```
|
|
21
|
+
const queue = new PQueue({timeout: 5000});
|
|
22
|
+
|
|
23
|
+
// Change timeout for all future tasks
|
|
24
|
+
queue.timeout = 10000;
|
|
25
|
+
```
|
|
26
|
+
*/
|
|
27
|
+
timeout?: number;
|
|
28
|
+
constructor(options?: Options<QueueType, EnqueueOptionsType>);
|
|
29
|
+
get concurrency(): number;
|
|
30
|
+
set concurrency(newConcurrency: number);
|
|
31
|
+
/**
|
|
32
|
+
Updates the priority of a promise function by its id, affecting its execution order. Requires a defined concurrency limit to take effect.
|
|
33
|
+
|
|
34
|
+
For example, this can be used to prioritize a promise function to run earlier.
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import PQueue from 'p-queue';
|
|
38
|
+
|
|
39
|
+
const queue = new PQueue({concurrency: 1});
|
|
40
|
+
|
|
41
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
42
|
+
queue.add(async () => '🦀', {priority: 0, id: '🦀'});
|
|
43
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
44
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
45
|
+
|
|
46
|
+
queue.setPriority('🦀', 2);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
In this case, the promise function with `id: '🦀'` runs second.
|
|
50
|
+
|
|
51
|
+
You can also deprioritize a promise function to delay its execution:
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import PQueue from 'p-queue';
|
|
55
|
+
|
|
56
|
+
const queue = new PQueue({concurrency: 1});
|
|
57
|
+
|
|
58
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
59
|
+
queue.add(async () => '🦀', {priority: 1, id: '🦀'});
|
|
60
|
+
queue.add(async () => '🦄');
|
|
61
|
+
queue.add(async () => '🦄', {priority: 0});
|
|
62
|
+
|
|
63
|
+
queue.setPriority('🦀', -1);
|
|
64
|
+
```
|
|
65
|
+
Here, the promise function with `id: '🦀'` executes last.
|
|
66
|
+
*/
|
|
67
|
+
setPriority(id: string, priority: number): void;
|
|
68
|
+
/**
|
|
69
|
+
Adds a sync or async task to the queue. Always returns a promise.
|
|
70
|
+
*/
|
|
71
|
+
add<TaskResultType>(function_: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType>;
|
|
72
|
+
/**
|
|
73
|
+
Same as `.add()`, but accepts an array of sync or async functions.
|
|
74
|
+
|
|
75
|
+
@returns A promise that resolves when all functions are resolved.
|
|
76
|
+
*/
|
|
77
|
+
addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultsType[]>;
|
|
78
|
+
/**
|
|
79
|
+
Start (or resume) executing enqueued tasks within concurrency limit. No need to call this if queue is not paused (via `options.autoStart = false` or by `.pause()` method.)
|
|
80
|
+
*/
|
|
81
|
+
start(): this;
|
|
82
|
+
/**
|
|
83
|
+
Put queue execution on hold.
|
|
84
|
+
*/
|
|
85
|
+
pause(): void;
|
|
86
|
+
/**
|
|
87
|
+
Clear the queue.
|
|
88
|
+
*/
|
|
89
|
+
clear(): void;
|
|
90
|
+
/**
|
|
91
|
+
Can be called multiple times. Useful if you for example add additional items at a later time.
|
|
92
|
+
|
|
93
|
+
@returns A promise that settles when the queue becomes empty.
|
|
94
|
+
*/
|
|
95
|
+
onEmpty(): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
@returns A promise that settles when the queue size is less than the given limit: `queue.size < limit`.
|
|
98
|
+
|
|
99
|
+
If you want to avoid having the queue grow beyond a certain size you can `await queue.onSizeLessThan()` before adding a new item.
|
|
100
|
+
|
|
101
|
+
Note that this only limits the number of items waiting to start. There could still be up to `concurrency` jobs already running that this call does not include in its calculation.
|
|
102
|
+
*/
|
|
103
|
+
onSizeLessThan(limit: number): Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
The difference with `.onEmpty` is that `.onIdle` guarantees that all work from the queue has finished. `.onEmpty` merely signals that the queue is empty, but it could mean that some promises haven't completed yet.
|
|
106
|
+
|
|
107
|
+
@returns A promise that settles when the queue becomes empty, and all promises have completed; `queue.size === 0 && queue.pending === 0`.
|
|
108
|
+
*/
|
|
109
|
+
onIdle(): Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
The difference with `.onIdle` is that `.onPendingZero` only waits for currently running tasks to finish, ignoring queued tasks.
|
|
112
|
+
|
|
113
|
+
@returns A promise that settles when all currently running tasks have completed; `queue.pending === 0`.
|
|
114
|
+
*/
|
|
115
|
+
onPendingZero(): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
@returns A promise that settles when the queue becomes rate-limited due to intervalCap.
|
|
118
|
+
*/
|
|
119
|
+
onRateLimit(): Promise<void>;
|
|
120
|
+
/**
|
|
121
|
+
@returns A promise that settles when the queue is no longer rate-limited.
|
|
122
|
+
*/
|
|
123
|
+
onRateLimitCleared(): Promise<void>;
|
|
124
|
+
/**
|
|
125
|
+
@returns A promise that rejects when any task in the queue errors.
|
|
126
|
+
|
|
127
|
+
Use with `Promise.race([queue.onError(), queue.onIdle()])` to fail fast on the first error while still resolving normally when the queue goes idle.
|
|
128
|
+
|
|
129
|
+
Important: The promise returned by `add()` still rejects. You must handle each `add()` promise (for example, `.catch(() => {})`) to avoid unhandled rejections.
|
|
130
|
+
|
|
131
|
+
@example
|
|
132
|
+
```
|
|
133
|
+
import PQueue from 'p-queue';
|
|
134
|
+
|
|
135
|
+
const queue = new PQueue({concurrency: 2});
|
|
136
|
+
|
|
137
|
+
queue.add(() => fetchData(1)).catch(() => {});
|
|
138
|
+
queue.add(() => fetchData(2)).catch(() => {});
|
|
139
|
+
queue.add(() => fetchData(3)).catch(() => {});
|
|
140
|
+
|
|
141
|
+
// Stop processing on first error
|
|
142
|
+
try {
|
|
143
|
+
await Promise.race([
|
|
144
|
+
queue.onError(),
|
|
145
|
+
queue.onIdle()
|
|
146
|
+
]);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
queue.pause(); // Stop processing remaining tasks
|
|
149
|
+
console.error('Queue failed:', error);
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
*/
|
|
153
|
+
onError(): Promise<never>;
|
|
154
|
+
/**
|
|
155
|
+
Size of the queue, the number of queued items waiting to run.
|
|
156
|
+
*/
|
|
157
|
+
get size(): number;
|
|
158
|
+
/**
|
|
159
|
+
Size of the queue, filtered by the given options.
|
|
160
|
+
|
|
161
|
+
For example, this can be used to find the number of items remaining in the queue with a specific priority level.
|
|
162
|
+
*/
|
|
163
|
+
sizeBy(options: Readonly<Partial<EnqueueOptionsType>>): number;
|
|
164
|
+
/**
|
|
165
|
+
Number of running items (no longer in the queue).
|
|
166
|
+
*/
|
|
167
|
+
get pending(): number;
|
|
168
|
+
/**
|
|
169
|
+
Whether the queue is currently paused.
|
|
170
|
+
*/
|
|
171
|
+
get isPaused(): boolean;
|
|
172
|
+
/**
|
|
173
|
+
Whether the queue is currently rate-limited due to intervalCap.
|
|
174
|
+
*/
|
|
175
|
+
get isRateLimited(): boolean;
|
|
176
|
+
/**
|
|
177
|
+
Whether the queue is saturated. Returns `true` when:
|
|
178
|
+
- All concurrency slots are occupied and tasks are waiting, OR
|
|
179
|
+
- The queue is rate-limited and tasks are waiting
|
|
180
|
+
|
|
181
|
+
Useful for detecting backpressure and potential hanging tasks.
|
|
182
|
+
|
|
183
|
+
```js
|
|
184
|
+
import PQueue from 'p-queue';
|
|
185
|
+
|
|
186
|
+
const queue = new PQueue({concurrency: 2});
|
|
187
|
+
|
|
188
|
+
// Backpressure handling
|
|
189
|
+
if (queue.isSaturated) {
|
|
190
|
+
console.log('Queue is saturated, waiting for capacity...');
|
|
191
|
+
await queue.onSizeLessThan(queue.concurrency);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Monitoring for stuck tasks
|
|
195
|
+
setInterval(() => {
|
|
196
|
+
if (queue.isSaturated) {
|
|
197
|
+
console.warn(`Queue saturated: ${queue.pending} running, ${queue.size} waiting`);
|
|
198
|
+
}
|
|
199
|
+
}, 60000);
|
|
200
|
+
```
|
|
201
|
+
*/
|
|
202
|
+
get isSaturated(): boolean;
|
|
203
|
+
/**
|
|
204
|
+
The tasks currently being executed. Each task includes its `id`, `priority`, `startTime`, and `timeout` (if set).
|
|
205
|
+
|
|
206
|
+
Returns an array of task info objects.
|
|
207
|
+
|
|
208
|
+
```js
|
|
209
|
+
import PQueue from 'p-queue';
|
|
210
|
+
|
|
211
|
+
const queue = new PQueue({concurrency: 2});
|
|
212
|
+
|
|
213
|
+
// Add tasks with IDs for better debugging
|
|
214
|
+
queue.add(() => fetchUser(123), {id: 'user-123'});
|
|
215
|
+
queue.add(() => fetchPosts(456), {id: 'posts-456', priority: 1});
|
|
216
|
+
|
|
217
|
+
// Check what's running
|
|
218
|
+
console.log(queue.runningTasks);
|
|
219
|
+
// => [{
|
|
220
|
+
// id: 'user-123',
|
|
221
|
+
// priority: 0,
|
|
222
|
+
// startTime: 1759253001716,
|
|
223
|
+
// timeout: undefined
|
|
224
|
+
// }, {
|
|
225
|
+
// id: 'posts-456',
|
|
226
|
+
// priority: 1,
|
|
227
|
+
// startTime: 1759253001916,
|
|
228
|
+
// timeout: undefined
|
|
229
|
+
// }]
|
|
230
|
+
```
|
|
231
|
+
*/
|
|
232
|
+
get runningTasks(): ReadonlyArray<{
|
|
233
|
+
readonly id?: string;
|
|
234
|
+
readonly priority: number;
|
|
235
|
+
readonly startTime: number;
|
|
236
|
+
readonly timeout?: number;
|
|
237
|
+
}>;
|
|
238
|
+
}
|
|
239
|
+
export type { Queue } from './queue.js';
|
|
240
|
+
export { type QueueAddOptions, type Options } from './options.js';
|
|
241
|
+
/**
|
|
242
|
+
Error thrown when a task times out.
|
|
243
|
+
|
|
244
|
+
@example
|
|
245
|
+
```
|
|
246
|
+
import PQueue, {TimeoutError} from 'p-queue';
|
|
247
|
+
|
|
248
|
+
const queue = new PQueue({timeout: 1000});
|
|
249
|
+
|
|
250
|
+
try {
|
|
251
|
+
await queue.add(() => someTask());
|
|
252
|
+
} catch (error) {
|
|
253
|
+
if (error instanceof TimeoutError) {
|
|
254
|
+
console.log('Task timed out');
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
*/
|
|
259
|
+
export { TimeoutError } from 'p-timeout';
|