@caido-utils/backend 1.8.0 → 1.8.1

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.
@@ -1,2 +1,2 @@
1
- export { HttpClient, RawHttpClient, applyCloseConnection, applyContentLength, checkSignal, resolveRedirectUrl, sendRawRequest, waitForUnpause, type BatchCallbacks, type HttpClientOptions, type HttpClientRequest, type HttpClientResponse, type HttpMethod, type RawHttpConfig, type RawHttpSettings, type RawRequest, type RawSendResult, type RedirectPolicy, type Result, type Signal, } from "./client";
2
- export { HttpRequest, filterAuthHeaders, getStatusText, parseUrl, queryRequests, type HttpRequestOptions, type QueryRequestsOptions, } from "./helpers";
1
+ export { applyCloseConnection, applyContentLength, checkSignal, HttpClient, RawHttpClient, resolveRedirectUrl, sendRawRequest, waitForUnpause, type BatchCallbacks, type HttpClientOptions, type HttpClientRequest, type HttpClientResponse, type HttpMethod, type RawHttpConfig, type RawHttpSettings, type RawRequest, type RawSendResult, type RedirectPolicy, type Result, type Signal, } from "./client";
2
+ export { filterAuthHeaders, getStatusText, HttpRequest, parseUrl, queryRequests, type HttpRequestOptions, type QueryRequestsOptions, } from "./helpers";
@@ -1,17 +1,17 @@
1
1
  export {
2
- HttpClient,
3
- RawHttpClient,
4
2
  applyCloseConnection,
5
3
  applyContentLength,
6
4
  checkSignal,
5
+ HttpClient,
6
+ RawHttpClient,
7
7
  resolveRedirectUrl,
8
8
  sendRawRequest,
9
9
  waitForUnpause
10
10
  } from "./client/index.js";
11
11
  export {
12
- HttpRequest,
13
12
  filterAuthHeaders,
14
13
  getStatusText,
14
+ HttpRequest,
15
15
  parseUrl,
16
16
  queryRequests
17
17
  } from "./helpers/index.js";
package/dist/index.d.ts CHANGED
@@ -2,6 +2,8 @@ export { spawnCommand } from "./process";
2
2
  export { sha256 } from "./crypto";
3
3
  export { resolveFilterQuery } from "./filter";
4
4
  export { HttpClient, HttpRequest, RawHttpClient, applyCloseConnection, applyContentLength, checkSignal, filterAuthHeaders, getStatusText, parseUrl, queryRequests, resolveRedirectUrl, sendRawRequest, waitForUnpause, type BatchCallbacks, type HttpClientOptions, type HttpClientRequest, type HttpClientResponse, type HttpMethod, type HttpRequestOptions, type QueryRequestsOptions, type RawHttpConfig, type RawHttpSettings, type RawRequest, type RawSendResult, type RedirectPolicy, type Result, type Signal, } from "./http";
5
+ export { JobManager, WorkerPool } from "./jobs";
6
+ export type { Job, JobSignal, JobStatus } from "./jobs";
5
7
  export { Queue } from "./pool";
6
8
  export type { PoolCallbacks, PoolConfig } from "./pool";
7
9
  export { getProjectId } from "./project";
package/dist/index.js CHANGED
@@ -16,6 +16,7 @@ export {
16
16
  sendRawRequest,
17
17
  waitForUnpause
18
18
  } from "./http/index.js";
19
+ export { JobManager, WorkerPool } from "./jobs/index.js";
19
20
  export { Queue } from "./pool/index.js";
20
21
  export { getProjectId } from "./project/index.js";
21
22
  export { deleteFile, fileExists, readFile, storeFile } from "./storage/index.js";
@@ -0,0 +1,3 @@
1
+ export { JobManager } from "./manager";
2
+ export { WorkerPool } from "./worker-pool";
3
+ export type { Job, JobSignal, JobStatus } from "./types";
@@ -0,0 +1,2 @@
1
+ export { JobManager } from "./manager.js";
2
+ export { WorkerPool } from "./worker-pool.js";
@@ -0,0 +1,18 @@
1
+ import type { Job } from "./types";
2
+ export declare class JobManager<TType extends string = string> {
3
+ private jobs;
4
+ private completionListeners;
5
+ createJob(type: TType): Job<TType>;
6
+ getJob(id: string): Job<TType> | undefined;
7
+ getAllJobs(): Job<TType>[];
8
+ getJobsByType(type: TType): Job<TType>[];
9
+ completeJob(id: string): void;
10
+ failJob(id: string): void;
11
+ stopJob(id: string): void;
12
+ pauseJob(id: string): void;
13
+ resumeJob(id: string): void;
14
+ removeJob(id: string): void;
15
+ onJobDone(id: string, callback: () => void): void;
16
+ clearCompleted(): void;
17
+ private notifyDone;
18
+ }
@@ -0,0 +1,98 @@
1
+ let nextId = 1;
2
+ function generateId() {
3
+ return String(nextId++);
4
+ }
5
+ export class JobManager {
6
+ jobs = /* @__PURE__ */ new Map();
7
+ completionListeners = /* @__PURE__ */ new Map();
8
+ createJob(type) {
9
+ const id = generateId();
10
+ const job = {
11
+ id,
12
+ type,
13
+ status: "running",
14
+ signal: { stopped: false, paused: false },
15
+ createdAt: Date.now()
16
+ };
17
+ this.jobs.set(id, job);
18
+ return job;
19
+ }
20
+ getJob(id) {
21
+ return this.jobs.get(id);
22
+ }
23
+ getAllJobs() {
24
+ return [...this.jobs.values()];
25
+ }
26
+ getJobsByType(type) {
27
+ return [...this.jobs.values()].filter((j) => j.type === type);
28
+ }
29
+ completeJob(id) {
30
+ const job = this.jobs.get(id);
31
+ if (job !== void 0 && job.status === "running") {
32
+ job.status = "completed";
33
+ this.notifyDone(id);
34
+ }
35
+ }
36
+ failJob(id) {
37
+ const job = this.jobs.get(id);
38
+ if (job !== void 0) {
39
+ job.status = "error";
40
+ this.notifyDone(id);
41
+ }
42
+ }
43
+ stopJob(id) {
44
+ const job = this.jobs.get(id);
45
+ if (job === void 0) return;
46
+ if (job.status !== "running" && job.status !== "paused") return;
47
+ job.signal.stopped = true;
48
+ job.status = "stopped";
49
+ this.notifyDone(id);
50
+ }
51
+ pauseJob(id) {
52
+ const job = this.jobs.get(id);
53
+ if (job === void 0) return;
54
+ if (job.status !== "running") return;
55
+ job.signal.paused = true;
56
+ job.status = "paused";
57
+ }
58
+ resumeJob(id) {
59
+ const job = this.jobs.get(id);
60
+ if (job === void 0) return;
61
+ if (job.status !== "paused") return;
62
+ job.signal.paused = false;
63
+ job.status = "running";
64
+ }
65
+ removeJob(id) {
66
+ const job = this.jobs.get(id);
67
+ if (job === void 0) return;
68
+ if (job.status === "running" || job.status === "paused") {
69
+ job.signal.stopped = true;
70
+ job.status = "stopped";
71
+ }
72
+ this.jobs.delete(id);
73
+ }
74
+ onJobDone(id, callback) {
75
+ const existing = this.completionListeners.get(id);
76
+ if (existing !== void 0) {
77
+ existing.push(callback);
78
+ } else {
79
+ this.completionListeners.set(id, [callback]);
80
+ }
81
+ }
82
+ clearCompleted() {
83
+ for (const [id, job] of this.jobs) {
84
+ if (job.status === "completed" || job.status === "error" || job.status === "stopped") {
85
+ this.jobs.delete(id);
86
+ }
87
+ }
88
+ }
89
+ notifyDone(id) {
90
+ const listeners = this.completionListeners.get(id);
91
+ if (listeners !== void 0) {
92
+ for (const cb of listeners) {
93
+ cb();
94
+ }
95
+ this.completionListeners.delete(id);
96
+ }
97
+ }
98
+ }
@@ -0,0 +1,10 @@
1
+ import type { Signal } from "../http/client/types";
2
+ export type JobStatus = "running" | "paused" | "completed" | "error" | "stopped";
3
+ export type { Signal as JobSignal };
4
+ export type Job<TType extends string = string> = {
5
+ id: string;
6
+ type: TType;
7
+ status: JobStatus;
8
+ signal: Signal;
9
+ createdAt: number;
10
+ };
File without changes
@@ -0,0 +1,14 @@
1
+ import type { JobManager } from "./manager";
2
+ export declare class WorkerPool<TRequest> {
3
+ private readonly maxConcurrency;
4
+ private active;
5
+ private queue;
6
+ private dispatcher;
7
+ private jobManager;
8
+ constructor(maxConcurrency: number, dispatcher: (request: TRequest) => string, jobManager: JobManager);
9
+ submit(request: TRequest): void;
10
+ get pending(): number;
11
+ get running(): number;
12
+ private dispatch;
13
+ private drain;
14
+ }
@@ -0,0 +1,41 @@
1
+ export class WorkerPool {
2
+ maxConcurrency;
3
+ active = 0;
4
+ queue = [];
5
+ dispatcher;
6
+ jobManager;
7
+ constructor(maxConcurrency, dispatcher, jobManager) {
8
+ this.maxConcurrency = maxConcurrency;
9
+ this.dispatcher = dispatcher;
10
+ this.jobManager = jobManager;
11
+ }
12
+ submit(request) {
13
+ if (this.active < this.maxConcurrency) {
14
+ this.dispatch({ request });
15
+ } else {
16
+ this.queue.push({ request });
17
+ }
18
+ }
19
+ get pending() {
20
+ return this.queue.length;
21
+ }
22
+ get running() {
23
+ return this.active;
24
+ }
25
+ dispatch(task) {
26
+ this.active++;
27
+ const jobId = this.dispatcher(task.request);
28
+ this.jobManager.onJobDone(jobId, () => {
29
+ this.active--;
30
+ this.drain();
31
+ });
32
+ }
33
+ drain() {
34
+ while (this.active < this.maxConcurrency && this.queue.length > 0) {
35
+ const next = this.queue.shift();
36
+ if (next !== void 0) {
37
+ this.dispatch(next);
38
+ }
39
+ }
40
+ }
41
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caido-utils/backend",
3
- "version": "1.8.0",
3
+ "version": "1.8.1",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"