@caido-utils/backend 0.1.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,17 @@
1
+ import type { SDK } from "caido:plugin";
2
+ import type { RequestResponse, RequestSpec, RequestSpecRaw } from "caido:utils";
3
+ export type FetchOptions = {
4
+ threads?: number;
5
+ delay?: number;
6
+ };
7
+ export declare class Fetch {
8
+ private sdk;
9
+ private threads;
10
+ private delay;
11
+ private active;
12
+ private queue;
13
+ constructor(sdk: SDK, options?: FetchOptions);
14
+ send(request: RequestSpec | RequestSpecRaw): Promise<RequestResponse>;
15
+ private drain;
16
+ private execute;
17
+ }
@@ -0,0 +1,43 @@
1
+ function sleep(ms) {
2
+ if (ms <= 0) return Promise.resolve();
3
+ return new Promise((resolve) => setTimeout(resolve, ms));
4
+ }
5
+ export class Fetch {
6
+ sdk;
7
+ threads;
8
+ delay;
9
+ active = 0;
10
+ queue = [];
11
+ constructor(sdk, options = {}) {
12
+ this.sdk = sdk;
13
+ this.threads = options.threads ?? 10;
14
+ this.delay = options.delay ?? 0;
15
+ }
16
+ send(request) {
17
+ return new Promise((resolve, reject) => {
18
+ this.queue.push({ request, resolve, reject });
19
+ this.drain();
20
+ });
21
+ }
22
+ drain() {
23
+ while (this.active < this.threads && this.queue.length > 0) {
24
+ const item = this.queue.shift();
25
+ this.active++;
26
+ this.execute(item);
27
+ }
28
+ }
29
+ async execute(item) {
30
+ try {
31
+ const result = await this.sdk.requests.send(item.request);
32
+ item.resolve(result);
33
+ } catch (err) {
34
+ item.reject(err);
35
+ } finally {
36
+ if (this.delay > 0) {
37
+ await sleep(this.delay);
38
+ }
39
+ this.active--;
40
+ this.drain();
41
+ }
42
+ }
43
+ }
@@ -0,0 +1 @@
1
+ export { Fetch, type FetchOptions } from "./fetcher";
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { Fetch } from "./fetcher.js";
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@caido-utils/backend",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "peerDependencies": {
15
+ "@caido/sdk-backend": ">=0.46.0"
16
+ },
17
+ "devDependencies": {
18
+ "@caido/sdk-backend": "^0.46.0",
19
+ "typescript": "^5.5.4",
20
+ "unbuild": "^3.6.1"
21
+ },
22
+ "scripts": {
23
+ "build": "unbuild"
24
+ }
25
+ }