@caido-utils/backend 0.6.0 → 1.0.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/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/pool/index.d.ts +2 -2
- package/dist/pool/index.js +1 -1
- package/dist/pool/pool.d.ts +26 -21
- package/dist/pool/pool.js +96 -69
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { sha256 } from "./crypto";
|
|
2
|
-
export {
|
|
3
|
-
export type { PoolCallbacks, PoolConfig
|
|
2
|
+
export { Queue } from "./pool";
|
|
3
|
+
export type { PoolCallbacks, PoolConfig } from "./pool";
|
|
4
4
|
export { spawnCommand } from "./process";
|
|
5
5
|
export { getProjectId } from "./project";
|
|
6
6
|
export { Fetch, queryRequests, type FetchOptions, type QueryRequestsOptions, } from "./requests";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { sha256 } from "./crypto/index.js";
|
|
2
|
-
export {
|
|
2
|
+
export { Queue } from "./pool/index.js";
|
|
3
3
|
export { spawnCommand } from "./process/index.js";
|
|
4
4
|
export { getProjectId } from "./project/index.js";
|
|
5
5
|
export {
|
package/dist/pool/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export type { PoolCallbacks, PoolConfig
|
|
1
|
+
export { Queue } from "./pool";
|
|
2
|
+
export type { PoolCallbacks, PoolConfig } from "./pool";
|
package/dist/pool/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { Queue } from "./pool.js";
|
package/dist/pool/pool.d.ts
CHANGED
|
@@ -1,28 +1,33 @@
|
|
|
1
|
-
export type PoolSignal = {
|
|
2
|
-
stopped: boolean;
|
|
3
|
-
paused: boolean;
|
|
4
|
-
};
|
|
5
1
|
export type PoolConfig = {
|
|
6
2
|
concurrency: number;
|
|
7
3
|
delayMs?: number;
|
|
8
4
|
};
|
|
9
5
|
export type PoolCallbacks<TItem> = {
|
|
10
|
-
onProgress?: (progress: {
|
|
11
|
-
completed: number;
|
|
12
|
-
total: number;
|
|
13
|
-
}) => void;
|
|
14
6
|
onError?: (item: TItem, error: string) => void;
|
|
15
7
|
};
|
|
16
|
-
export declare
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
8
|
+
export declare class Queue<TItem> {
|
|
9
|
+
private items;
|
|
10
|
+
private config;
|
|
11
|
+
private run;
|
|
12
|
+
private callbacks?;
|
|
13
|
+
private workers;
|
|
14
|
+
private stopped;
|
|
15
|
+
private paused;
|
|
16
|
+
private waiters;
|
|
17
|
+
private _completed;
|
|
18
|
+
constructor(config: PoolConfig, run: (item: TItem) => Promise<void>, callbacks?: PoolCallbacks<TItem>);
|
|
19
|
+
enqueue(items: TItem | TItem[]): void;
|
|
20
|
+
pause(): void;
|
|
21
|
+
resume(): void;
|
|
22
|
+
stop(): void;
|
|
23
|
+
isRunning(): boolean;
|
|
24
|
+
isPaused(): boolean;
|
|
25
|
+
setConcurrency(concurrency: number): void;
|
|
26
|
+
get pending(): number;
|
|
27
|
+
get completed(): number;
|
|
28
|
+
private notify;
|
|
29
|
+
private waitForItem;
|
|
30
|
+
private waitForUnpause;
|
|
31
|
+
private ensureWorkers;
|
|
32
|
+
private worker;
|
|
33
|
+
}
|
package/dist/pool/pool.js
CHANGED
|
@@ -1,85 +1,112 @@
|
|
|
1
|
-
export function createSignal() {
|
|
2
|
-
return { stopped: false, paused: false };
|
|
3
|
-
}
|
|
4
|
-
export function stopPool(signal) {
|
|
5
|
-
signal.stopped = true;
|
|
6
|
-
}
|
|
7
|
-
export function pausePool(signal) {
|
|
8
|
-
signal.paused = true;
|
|
9
|
-
}
|
|
10
|
-
export function resumePool(signal) {
|
|
11
|
-
signal.paused = false;
|
|
12
|
-
}
|
|
13
|
-
const PAUSE_POLL_MS = 200;
|
|
14
|
-
const YIELD_EVERY = 10;
|
|
15
|
-
function yieldTick() {
|
|
16
|
-
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
17
|
-
}
|
|
18
1
|
function delay(ms) {
|
|
19
2
|
if (ms <= 0) return Promise.resolve();
|
|
20
3
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
21
4
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const items = [];
|
|
38
|
-
for await (const item of source) {
|
|
39
|
-
items.push(item);
|
|
5
|
+
const PAUSE_POLL_MS = 200;
|
|
6
|
+
export class Queue {
|
|
7
|
+
items = [];
|
|
8
|
+
config;
|
|
9
|
+
run;
|
|
10
|
+
callbacks;
|
|
11
|
+
workers = [];
|
|
12
|
+
stopped = false;
|
|
13
|
+
paused = false;
|
|
14
|
+
waiters = [];
|
|
15
|
+
_completed = 0;
|
|
16
|
+
constructor(config, run, callbacks) {
|
|
17
|
+
this.config = config;
|
|
18
|
+
this.run = run;
|
|
19
|
+
this.callbacks = callbacks;
|
|
40
20
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
21
|
+
enqueue(items) {
|
|
22
|
+
const toAdd = Array.isArray(items) ? items : [items];
|
|
23
|
+
this.items.push(...toAdd);
|
|
24
|
+
this.notify();
|
|
25
|
+
this.ensureWorkers();
|
|
26
|
+
}
|
|
27
|
+
pause() {
|
|
28
|
+
this.paused = true;
|
|
29
|
+
}
|
|
30
|
+
resume() {
|
|
31
|
+
this.paused = false;
|
|
32
|
+
}
|
|
33
|
+
stop() {
|
|
34
|
+
this.stopped = true;
|
|
35
|
+
this.items = [];
|
|
36
|
+
this.notify();
|
|
37
|
+
}
|
|
38
|
+
isRunning() {
|
|
39
|
+
return this.workers.length > 0 && !this.stopped;
|
|
40
|
+
}
|
|
41
|
+
isPaused() {
|
|
42
|
+
return this.paused;
|
|
43
|
+
}
|
|
44
|
+
setConcurrency(concurrency) {
|
|
45
|
+
this.config.concurrency = concurrency;
|
|
46
|
+
this.notify();
|
|
47
|
+
this.ensureWorkers();
|
|
48
|
+
}
|
|
49
|
+
get pending() {
|
|
50
|
+
return this.items.length;
|
|
51
|
+
}
|
|
52
|
+
get completed() {
|
|
53
|
+
return this._completed;
|
|
54
|
+
}
|
|
55
|
+
notify() {
|
|
56
|
+
const waiters = this.waiters.splice(0);
|
|
57
|
+
for (const waiter of waiters) {
|
|
58
|
+
waiter.resolve();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
waitForItem() {
|
|
62
|
+
return new Promise((resolve) => {
|
|
63
|
+
this.waiters.push({ resolve });
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
async waitForUnpause() {
|
|
67
|
+
while (this.paused && !this.stopped) {
|
|
68
|
+
await delay(PAUSE_POLL_MS);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
ensureWorkers() {
|
|
72
|
+
if (this.stopped) return;
|
|
73
|
+
const needed = Math.min(
|
|
74
|
+
this.config.concurrency - this.workers.length,
|
|
75
|
+
this.items.length
|
|
76
|
+
);
|
|
77
|
+
for (let i = 0; i < needed; i++) {
|
|
78
|
+
const worker = this.worker();
|
|
79
|
+
this.workers.push(worker);
|
|
80
|
+
worker.then(() => {
|
|
81
|
+
const idx = this.workers.indexOf(worker);
|
|
82
|
+
if (idx !== -1) this.workers.splice(idx, 1);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async worker() {
|
|
87
|
+
const delayMs = this.config.delayMs ?? 0;
|
|
88
|
+
while (!this.stopped) {
|
|
89
|
+
await this.waitForUnpause();
|
|
90
|
+
if (this.stopped) return;
|
|
91
|
+
if (this.workers.length > this.config.concurrency) return;
|
|
92
|
+
const item = this.items.shift();
|
|
93
|
+
if (item === void 0) {
|
|
94
|
+
await this.waitForItem();
|
|
95
|
+
if (this.stopped) return;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
59
98
|
try {
|
|
60
|
-
await run(item);
|
|
61
|
-
completed++;
|
|
99
|
+
await this.run(item);
|
|
62
100
|
} catch (error) {
|
|
63
|
-
|
|
64
|
-
if (callbacks?.onError) {
|
|
101
|
+
if (this.callbacks?.onError) {
|
|
65
102
|
const message = error instanceof Error ? error.message : "Unknown error";
|
|
66
|
-
callbacks.onError(item, message);
|
|
103
|
+
this.callbacks.onError(item, message);
|
|
67
104
|
}
|
|
68
105
|
}
|
|
69
|
-
|
|
70
|
-
callbacks.onProgress({ completed, total });
|
|
71
|
-
}
|
|
72
|
-
localCount++;
|
|
73
|
-
if (localCount % YIELD_EVERY === 0) {
|
|
74
|
-
await yieldTick();
|
|
75
|
-
}
|
|
106
|
+
this._completed++;
|
|
76
107
|
if (delayMs > 0) {
|
|
77
108
|
await delay(delayMs);
|
|
78
109
|
}
|
|
79
110
|
}
|
|
80
111
|
}
|
|
81
|
-
const workerCount = Math.min(config.concurrency, items.length);
|
|
82
|
-
const workers = Array.from({ length: workerCount }, () => worker());
|
|
83
|
-
await Promise.all(workers);
|
|
84
|
-
return { completed };
|
|
85
112
|
}
|