@caido-utils/backend 0.5.5 → 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.
@@ -0,0 +1 @@
1
+ export declare function sha256(content: string): string;
@@ -0,0 +1,4 @@
1
+ import crypto from "crypto";
2
+ export function sha256(content) {
3
+ return crypto.createHash("sha256").update(content).digest("hex");
4
+ }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- export { createSignal, pausePool, resumePool, runPool, stopPool } from "./pool";
2
- export type { PoolCallbacks, PoolConfig, PoolSignal } from "./pool";
1
+ export { sha256 } from "./crypto";
2
+ export { Queue } from "./pool";
3
+ export type { PoolCallbacks, PoolConfig } from "./pool";
3
4
  export { spawnCommand } from "./process";
4
5
  export { getProjectId } from "./project";
5
6
  export { Fetch, queryRequests, type FetchOptions, type QueryRequestsOptions, } from "./requests";
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
- export { createSignal, pausePool, resumePool, runPool, stopPool } from "./pool/index.js";
1
+ export { sha256 } from "./crypto/index.js";
2
+ export { Queue } from "./pool/index.js";
2
3
  export { spawnCommand } from "./process/index.js";
3
4
  export { getProjectId } from "./project/index.js";
4
5
  export {
@@ -1,2 +1,2 @@
1
- export { createSignal, pausePool, resumePool, runPool, stopPool } from "./pool";
2
- export type { PoolCallbacks, PoolConfig, PoolSignal } from "./pool";
1
+ export { Queue } from "./pool";
2
+ export type { PoolCallbacks, PoolConfig } from "./pool";
@@ -1 +1 @@
1
- export { createSignal, pausePool, resumePool, runPool, stopPool } from "./pool.js";
1
+ export { Queue } from "./pool.js";
@@ -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 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
- }>;
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
- 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);
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
- 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;
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
- completed++;
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
- if (callbacks?.onProgress) {
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caido-utils/backend",
3
- "version": "0.5.5",
3
+ "version": "1.0.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"