@caido-utils/backend 0.1.0 → 0.2.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 -0
- package/dist/index.js +1 -0
- package/dist/pool.d.ts +28 -0
- package/dist/pool.js +85 -0
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/pool.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export type PoolSignal = {
|
|
2
|
+
stopped: boolean;
|
|
3
|
+
paused: boolean;
|
|
4
|
+
};
|
|
5
|
+
export type PoolConfig = {
|
|
6
|
+
concurrency: number;
|
|
7
|
+
delayMs?: number;
|
|
8
|
+
};
|
|
9
|
+
export type PoolCallbacks<TItem> = {
|
|
10
|
+
onProgress?: (progress: {
|
|
11
|
+
completed: number;
|
|
12
|
+
total: number;
|
|
13
|
+
}) => void;
|
|
14
|
+
onError?: (item: TItem, error: string) => void;
|
|
15
|
+
};
|
|
16
|
+
export declare function createSignal(): PoolSignal;
|
|
17
|
+
export declare function stopPool(signal: PoolSignal): void;
|
|
18
|
+
export declare function pausePool(signal: PoolSignal): void;
|
|
19
|
+
export declare function resumePool(signal: PoolSignal): void;
|
|
20
|
+
export declare function runPool<TItem>(opts: {
|
|
21
|
+
items: TItem[] | AsyncIterable<TItem>;
|
|
22
|
+
config: PoolConfig;
|
|
23
|
+
signal: PoolSignal;
|
|
24
|
+
run: (item: TItem) => Promise<void>;
|
|
25
|
+
callbacks?: PoolCallbacks<TItem>;
|
|
26
|
+
}): Promise<{
|
|
27
|
+
completed: number;
|
|
28
|
+
}>;
|
package/dist/pool.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
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
|
+
function delay(ms) {
|
|
19
|
+
if (ms <= 0) return Promise.resolve();
|
|
20
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
21
|
+
}
|
|
22
|
+
function waitForUnpause(signal) {
|
|
23
|
+
if (!signal.paused || signal.stopped) return Promise.resolve();
|
|
24
|
+
return new Promise((resolve) => {
|
|
25
|
+
const poll = () => {
|
|
26
|
+
if (!signal.paused || signal.stopped) {
|
|
27
|
+
resolve();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
setTimeout(poll, PAUSE_POLL_MS);
|
|
31
|
+
};
|
|
32
|
+
setTimeout(poll, PAUSE_POLL_MS);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async function materializeItems(source) {
|
|
36
|
+
if (Array.isArray(source)) return source;
|
|
37
|
+
const items = [];
|
|
38
|
+
for await (const item of source) {
|
|
39
|
+
items.push(item);
|
|
40
|
+
}
|
|
41
|
+
return items;
|
|
42
|
+
}
|
|
43
|
+
export async function runPool(opts) {
|
|
44
|
+
const { config, signal, run, callbacks } = opts;
|
|
45
|
+
const items = await materializeItems(opts.items);
|
|
46
|
+
const total = items.length;
|
|
47
|
+
const delayMs = config.delayMs ?? 0;
|
|
48
|
+
let nextIndex = 0;
|
|
49
|
+
let completed = 0;
|
|
50
|
+
async function worker() {
|
|
51
|
+
let localCount = 0;
|
|
52
|
+
while (nextIndex < items.length) {
|
|
53
|
+
if (signal.stopped) return;
|
|
54
|
+
await waitForUnpause(signal);
|
|
55
|
+
if (signal.stopped) return;
|
|
56
|
+
const idx = nextIndex++;
|
|
57
|
+
const item = items[idx];
|
|
58
|
+
if (item === void 0) return;
|
|
59
|
+
try {
|
|
60
|
+
await run(item);
|
|
61
|
+
completed++;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
completed++;
|
|
64
|
+
if (callbacks?.onError) {
|
|
65
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
66
|
+
callbacks.onError(item, message);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (callbacks?.onProgress) {
|
|
70
|
+
callbacks.onProgress({ completed, total });
|
|
71
|
+
}
|
|
72
|
+
localCount++;
|
|
73
|
+
if (localCount % YIELD_EVERY === 0) {
|
|
74
|
+
await yieldTick();
|
|
75
|
+
}
|
|
76
|
+
if (delayMs > 0) {
|
|
77
|
+
await delay(delayMs);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@caido-utils/backend",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
"import": "./dist/index.js"
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "unbuild"
|
|
16
|
+
},
|
|
14
17
|
"peerDependencies": {
|
|
15
18
|
"@caido/sdk-backend": ">=0.46.0"
|
|
16
19
|
},
|
|
@@ -18,8 +21,5 @@
|
|
|
18
21
|
"@caido/sdk-backend": "^0.46.0",
|
|
19
22
|
"typescript": "^5.5.4",
|
|
20
23
|
"unbuild": "^3.6.1"
|
|
21
|
-
},
|
|
22
|
-
"scripts": {
|
|
23
|
-
"build": "unbuild"
|
|
24
24
|
}
|
|
25
|
-
}
|
|
25
|
+
}
|