qpass 1.1.0 → 1.2.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.
- package/README.md +207 -186
- package/dist/index.cjs +28 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +28 -5
- package/dist/index.js.map +1 -1
- package/dist/qpass.js +28 -5
- package/dist/qpass.js.map +1 -1
- package/package.json +36 -36
package/README.md
CHANGED
|
@@ -1,187 +1,208 @@
|
|
|
1
|
-
# Qpass
|
|
2
|
-
|
|
3
|
-
Qpass is a lightweight promise-job queue for JavaScript/TypeScript.
|
|
4
|
-
It runs jobs in cycles with configurable parallelism (`batchSize`) and reports progress after each cycle.
|
|
5
|
-
|
|
6
|
-
## Platform support
|
|
7
|
-
|
|
8
|
-
- Node.js ESM (`import`) via `qpass`
|
|
9
|
-
- Node.js CommonJS (`require`) via `qpass`
|
|
10
|
-
- Browser bundlers (Vite/Webpack/Rollup) via `qpass`
|
|
11
|
-
- Browser script tag/global build via `dist/qpass.js`
|
|
12
|
-
|
|
13
|
-
Version is sourced from `package.json` at build time, so you only update it in one place.
|
|
14
|
-
|
|
15
|
-
## Install
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
npm install qpass
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## Quick start
|
|
22
|
-
|
|
23
|
-
```js
|
|
24
|
-
import Qpass from "qpass";
|
|
25
|
-
|
|
26
|
-
const queue = new Qpass({
|
|
27
|
-
batchSize: 3,
|
|
28
|
-
onProgress: ({ batchToProcess, itemsToProcess, completed }) => {
|
|
29
|
-
console.log({ batchToProcess, itemsToProcess, completed });
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
const jobs = [
|
|
34
|
-
() => Promise.resolve("A"),
|
|
35
|
-
() => Promise.resolve("B"),
|
|
36
|
-
() => Promise.resolve("C"),
|
|
37
|
-
() => Promise.resolve("D"),
|
|
38
|
-
];
|
|
39
|
-
|
|
40
|
-
queue.add(jobs);
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
## Import by platform
|
|
44
|
-
|
|
45
|
-
### Node.js ESM
|
|
46
|
-
|
|
47
|
-
```js
|
|
48
|
-
import Qpass from "qpass";
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
### Node.js CommonJS
|
|
52
|
-
|
|
53
|
-
```js
|
|
54
|
-
const Qpass = require("qpass").default;
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### Browser bundler
|
|
58
|
-
|
|
59
|
-
```js
|
|
60
|
-
import Qpass from "qpass";
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
### Browser script tag (global)
|
|
64
|
-
|
|
65
|
-
Use the global build from your package output or CDN:
|
|
66
|
-
|
|
67
|
-
```html
|
|
68
|
-
<script src="https://cdn.jsdelivr.net/npm/qpass/dist/qpass.js"></script>
|
|
69
|
-
<script>
|
|
70
|
-
const queue = new Qpass();
|
|
71
|
-
</script>
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
## API
|
|
75
|
-
|
|
76
|
-
### `new Qpass(options?)`
|
|
77
|
-
|
|
78
|
-
```ts
|
|
79
|
-
new Qpass({
|
|
80
|
-
breakWhenError?: boolean;
|
|
81
|
-
batchSize?: number; // >= 1, default: 1
|
|
82
|
-
onProgress?: (progress: {
|
|
83
|
-
batchToProcess: number;
|
|
84
|
-
itemsToProcess: number;
|
|
85
|
-
completed: any[];
|
|
86
|
-
}) => void;
|
|
87
|
-
})
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
#### Options
|
|
91
|
-
|
|
92
|
-
- `batchSize` (default `1`): number of jobs to run in parallel per cycle.
|
|
93
|
-
- `breakWhenError` (default `false`): when `true`, queued jobs are cleared after an error is seen.
|
|
94
|
-
- `onProgress`: called after each cycle finishes.
|
|
95
|
-
|
|
96
|
-
### `add(jobs)`
|
|
97
|
-
|
|
98
|
-
```ts
|
|
99
|
-
add(jobs: (() => Promise<any>)[] | (() => Promise<any>)):
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
- Adds one or many jobs to the queue.
|
|
103
|
-
- Starts processing automatically.
|
|
104
|
-
-
|
|
105
|
-
|
|
106
|
-
### `
|
|
107
|
-
|
|
108
|
-
```ts
|
|
109
|
-
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
-
|
|
113
|
-
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
})
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
() => Promise.resolve("
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
queue.add(
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
1
|
+
# Qpass
|
|
2
|
+
|
|
3
|
+
Qpass is a lightweight promise-job queue for JavaScript/TypeScript.
|
|
4
|
+
It runs jobs in cycles with configurable parallelism (`batchSize`) and reports progress after each cycle.
|
|
5
|
+
|
|
6
|
+
## Platform support
|
|
7
|
+
|
|
8
|
+
- Node.js ESM (`import`) via `qpass`
|
|
9
|
+
- Node.js CommonJS (`require`) via `qpass`
|
|
10
|
+
- Browser bundlers (Vite/Webpack/Rollup) via `qpass`
|
|
11
|
+
- Browser script tag/global build via `dist/qpass.js`
|
|
12
|
+
|
|
13
|
+
Version is sourced from `package.json` at build time, so you only update it in one place.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install qpass
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import Qpass from "qpass";
|
|
25
|
+
|
|
26
|
+
const queue = new Qpass({
|
|
27
|
+
batchSize: 3,
|
|
28
|
+
onProgress: ({ batchToProcess, itemsToProcess, completed }) => {
|
|
29
|
+
console.log({ batchToProcess, itemsToProcess, completed });
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const jobs = [
|
|
34
|
+
() => Promise.resolve("A"),
|
|
35
|
+
() => Promise.resolve("B"),
|
|
36
|
+
() => Promise.resolve("C"),
|
|
37
|
+
() => Promise.resolve("D"),
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
queue.add(jobs);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Import by platform
|
|
44
|
+
|
|
45
|
+
### Node.js ESM
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import Qpass from "qpass";
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Node.js CommonJS
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
const Qpass = require("qpass").default;
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Browser bundler
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
import Qpass from "qpass";
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Browser script tag (global)
|
|
64
|
+
|
|
65
|
+
Use the global build from your package output or CDN:
|
|
66
|
+
|
|
67
|
+
```html
|
|
68
|
+
<script src="https://cdn.jsdelivr.net/npm/qpass/dist/qpass.js"></script>
|
|
69
|
+
<script>
|
|
70
|
+
const queue = new Qpass();
|
|
71
|
+
</script>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## API
|
|
75
|
+
|
|
76
|
+
### `new Qpass(options?)`
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
new Qpass({
|
|
80
|
+
breakWhenError?: boolean;
|
|
81
|
+
batchSize?: number; // >= 1, default: 1
|
|
82
|
+
onProgress?: (progress: {
|
|
83
|
+
batchToProcess: number;
|
|
84
|
+
itemsToProcess: number;
|
|
85
|
+
completed: any[];
|
|
86
|
+
}) => void;
|
|
87
|
+
})
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### Options
|
|
91
|
+
|
|
92
|
+
- `batchSize` (default `1`): number of jobs to run in parallel per cycle.
|
|
93
|
+
- `breakWhenError` (default `false`): when `true`, queued jobs are cleared after an error is seen.
|
|
94
|
+
- `onProgress`: called after each cycle finishes.
|
|
95
|
+
|
|
96
|
+
### `add(jobs)`
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
add(jobs: (() => Promise<any>)[] | (() => Promise<any>)): string[]
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- Adds one or many jobs to the queue.
|
|
103
|
+
- Starts processing automatically.
|
|
104
|
+
- Returns the array of ids for multiple jobs.
|
|
105
|
+
|
|
106
|
+
### `remove(id)`
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
remove(id: string | number): boolean
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
- Removes a pending queued job by its id.
|
|
113
|
+
- Returns `true` when a queued job is removed, or `false` if the id was not found.
|
|
114
|
+
- Does not stop jobs already in progress.
|
|
115
|
+
|
|
116
|
+
### `setBatchSize(size)`
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
setBatchSize(size: number): void
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
- Changes the number of jobs processed per batch.
|
|
123
|
+
- Takes effect on the next batch; any batch already in progress completes at its original size.
|
|
124
|
+
- Throws if `size` is not a number or is less than `1`.
|
|
125
|
+
- Safe to call at any point: mid-processing, while adding, or while removing jobs.
|
|
126
|
+
|
|
127
|
+
### `terminate()`
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
terminate(): void
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
- Clears all queued (not-yet-started) jobs.
|
|
134
|
+
- Already running jobs continue until they settle.
|
|
135
|
+
|
|
136
|
+
## Progress callback behavior
|
|
137
|
+
|
|
138
|
+
`onProgress` receives:
|
|
139
|
+
|
|
140
|
+
- `completed`: results (or errors) from the cycle that just finished.
|
|
141
|
+
- `itemsToProcess`: queued jobs still waiting to start.
|
|
142
|
+
- `batchToProcess`: `Math.ceil(itemsToProcess / batchSize)`.
|
|
143
|
+
|
|
144
|
+
Notes:
|
|
145
|
+
|
|
146
|
+
- Progress is reported per cycle, not per individual job completion.
|
|
147
|
+
- If you call `add` repeatedly (for example, inside a loop), the first cycle may be smaller than `batchSize` because processing starts immediately.
|
|
148
|
+
|
|
149
|
+
## Error handling
|
|
150
|
+
|
|
151
|
+
### Continue on error (`breakWhenError: false`)
|
|
152
|
+
|
|
153
|
+
```js
|
|
154
|
+
const queue = new Qpass({
|
|
155
|
+
breakWhenError: false,
|
|
156
|
+
batchSize: 2,
|
|
157
|
+
onProgress: ({ completed }) => {
|
|
158
|
+
console.log(
|
|
159
|
+
completed.map((x) => (x instanceof Error ? `Error: ${x.message}` : x))
|
|
160
|
+
);
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
queue.add([
|
|
165
|
+
() => Promise.resolve("ok-1"),
|
|
166
|
+
() => Promise.reject(new Error("failed-2")),
|
|
167
|
+
() => Promise.resolve("ok-3"),
|
|
168
|
+
]);
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Stop queueing after error (`breakWhenError: true`)
|
|
172
|
+
|
|
173
|
+
```js
|
|
174
|
+
const queue = new Qpass({
|
|
175
|
+
breakWhenError: true,
|
|
176
|
+
batchSize: 3,
|
|
177
|
+
onProgress: ({ itemsToProcess, completed }) => {
|
|
178
|
+
console.log({ itemsToProcess, completed });
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
queue.add([
|
|
183
|
+
() => Promise.resolve("ok-1"),
|
|
184
|
+
() => Promise.reject(new Error("failed-2")),
|
|
185
|
+
() => Promise.resolve("ok-3"),
|
|
186
|
+
() => Promise.resolve("queued-but-may-be-cleared"),
|
|
187
|
+
]);
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
With `breakWhenError: true`, Qpass clears jobs still waiting in the queue after an error is observed. Jobs already running in the same cycle still settle.
|
|
191
|
+
|
|
192
|
+
## Practical pattern: queue all jobs at once
|
|
193
|
+
|
|
194
|
+
If you want predictable `itemsToProcess` values in `onProgress`, build the job array first and call `add` once:
|
|
195
|
+
|
|
196
|
+
```js
|
|
197
|
+
const jobs = [];
|
|
198
|
+
|
|
199
|
+
for (let i = 1; i <= 14; i++) {
|
|
200
|
+
jobs.push(() => Promise.resolve(`Job ${i}`));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
queue.add(jobs);
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## License
|
|
207
|
+
|
|
187
208
|
ISC
|
package/dist/index.cjs
CHANGED
|
@@ -23,12 +23,14 @@ __export(index_exports, {
|
|
|
23
23
|
default: () => index_default
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(index_exports);
|
|
26
|
-
var QPASS_VERSION = true ? "1.1
|
|
26
|
+
var QPASS_VERSION = true ? "1.2.1" : "0.0.0";
|
|
27
27
|
var Qpass = class {
|
|
28
28
|
version = QPASS_VERSION;
|
|
29
29
|
// version info
|
|
30
30
|
items = [];
|
|
31
31
|
// job queue
|
|
32
|
+
nextJobId = 1;
|
|
33
|
+
// auto-generated job id
|
|
32
34
|
breakWhenError = false;
|
|
33
35
|
// whether to stop on error
|
|
34
36
|
batchSize = 1;
|
|
@@ -58,18 +60,39 @@ var Qpass = class {
|
|
|
58
60
|
}
|
|
59
61
|
}
|
|
60
62
|
}
|
|
61
|
-
// add jobs to the queue at once
|
|
63
|
+
// add jobs to the queue at once. now returns job id.
|
|
62
64
|
add(jobs) {
|
|
63
65
|
const jobsArray = Array.isArray(jobs) ? jobs : [jobs];
|
|
66
|
+
const addedIds = [];
|
|
64
67
|
for (const job of jobsArray) {
|
|
65
68
|
if (typeof job !== "function") {
|
|
66
69
|
throw new Error(
|
|
67
70
|
"Each job must be a function that returns a Promise"
|
|
68
71
|
);
|
|
69
72
|
}
|
|
70
|
-
this.
|
|
73
|
+
const id = String(this.nextJobId++);
|
|
74
|
+
this.items.push({ id, job });
|
|
75
|
+
addedIds.push(id);
|
|
71
76
|
}
|
|
72
77
|
this.processNext();
|
|
78
|
+
return addedIds;
|
|
79
|
+
}
|
|
80
|
+
// change the number of jobs processed per batch
|
|
81
|
+
setBatchSize(size) {
|
|
82
|
+
if (typeof size !== "number" || size < 1) {
|
|
83
|
+
throw new Error("batchSize must be at least 1");
|
|
84
|
+
}
|
|
85
|
+
this.batchSize = size;
|
|
86
|
+
}
|
|
87
|
+
// remove a queued job by id
|
|
88
|
+
remove(id) {
|
|
89
|
+
const jobId = String(id);
|
|
90
|
+
const index = this.items.findIndex((item) => item.id === jobId);
|
|
91
|
+
if (index === -1) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
this.items.splice(index, 1);
|
|
95
|
+
return true;
|
|
73
96
|
}
|
|
74
97
|
// process next batch of jobs
|
|
75
98
|
processNext() {
|
|
@@ -79,8 +102,8 @@ var Qpass = class {
|
|
|
79
102
|
const jobsToRun = this.items.splice(0, this.batchSize);
|
|
80
103
|
this.activeCount = jobsToRun.length;
|
|
81
104
|
this.currentCycleCompleted = new Array(jobsToRun.length);
|
|
82
|
-
jobsToRun.forEach((
|
|
83
|
-
Promise.resolve().then(() => job()).then((result) => {
|
|
105
|
+
jobsToRun.forEach((item, index) => {
|
|
106
|
+
Promise.resolve().then(() => item.job()).then((result) => {
|
|
84
107
|
this.finalizeJob(index, result, false);
|
|
85
108
|
return result;
|
|
86
109
|
}).catch((err) => {
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../index.ts"],"sourcesContent":["declare const __QPASS_VERSION__: string;\
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["declare const __QPASS_VERSION__: string;\n\nconst QPASS_VERSION =\n typeof __QPASS_VERSION__ === \"string\" ? __QPASS_VERSION__ : \"0.0.0\";\n\nclass Qpass {\n version = QPASS_VERSION; // version info\n\n private items: { id: string; job: () => Promise<any> }[] = []; // job queue\n private nextJobId: number = 1; // auto-generated job id\n private breakWhenError: boolean = false; // whether to stop on error\n private batchSize: number = 1; // number of jobs to process at once\n private onProgress?: (progress: {\n batchToProcess: number; // remaining batch count\n itemsToProcess: number; // remaining job count\n completed: any[]; // completed job results array\n }) => void; // progress callback\n\n private activeCount: number = 0; // number of jobs currently running\n private currentCycleCompleted: any[] = []; // completed results for current cycle\n private stopProcessing: boolean = false; // stop scheduling new jobs when true\n\n private get batchToProcess() {\n // calculate total batch count\n return Math.ceil(this.items.length / this.batchSize);\n }\n\n constructor(option?: {\n breakWhenError?: boolean;\n onProgress?: (progress: {\n batchToProcess: number;\n itemsToProcess: number;\n completed: any[];\n }) => void;\n batchSize?: number; // must be at least 1, default is 1\n }) {\n this.breakWhenError = !!option?.breakWhenError;\n this.batchSize = option?.batchSize ?? 1;\n\n if (typeof this.batchSize !== \"number\" || this.batchSize < 1) {\n throw new Error(\"batchSize must be at least 1\");\n }\n\n if (option?.onProgress) {\n if (typeof option.onProgress === \"function\") {\n this.onProgress = option.onProgress;\n } else {\n throw new Error(\"onProgress must be a function\");\n }\n }\n }\n\n // add jobs to the queue at once. now returns job id.\n add(jobs: (() => Promise<any>)[] | (() => Promise<any>)): string[] {\n // check if jobs is an array, if not convert to array\n const jobsArray = Array.isArray(jobs) ? jobs : [jobs];\n const addedIds: string[] = [];\n\n // check if each job is a function, if so add to queue\n for (const job of jobsArray) {\n if (typeof job !== \"function\") {\n throw new Error(\n \"Each job must be a function that returns a Promise\"\n );\n }\n\n const id = String(this.nextJobId++);\n this.items.push({ id, job });\n addedIds.push(id);\n }\n\n this.processNext();\n return addedIds;\n }\n\n // change the number of jobs processed per batch\n setBatchSize(size: number) {\n if (typeof size !== \"number\" || size < 1) {\n throw new Error(\"batchSize must be at least 1\");\n }\n this.batchSize = size;\n }\n\n // remove a queued job by id\n remove(id: string | number) {\n const jobId = String(id);\n const index = this.items.findIndex((item) => item.id === jobId);\n\n if (index === -1) {\n return false;\n }\n\n this.items.splice(index, 1);\n return true;\n }\n\n // process next batch of jobs\n private processNext() {\n // exit if queue is empty, processing is stopped, or a cycle is still running\n if (\n this.stopProcessing ||\n this.items.length === 0 ||\n this.activeCount > 0\n ) {\n return;\n }\n\n const jobsToRun = this.items.splice(0, this.batchSize);\n this.activeCount = jobsToRun.length;\n this.currentCycleCompleted = new Array(jobsToRun.length);\n\n jobsToRun.forEach((item, index) => {\n Promise.resolve()\n .then(() => item.job())\n .then((result) => {\n this.finalizeJob(index, result, false);\n return result;\n })\n .catch((err) => {\n this.finalizeJob(index, err, true);\n return err;\n });\n });\n }\n\n private finalizeJob(index: number, value: any, isError: boolean) {\n this.currentCycleCompleted[index] = value;\n this.activeCount -= 1;\n\n if (isError && this.breakWhenError) {\n this.stopProcessing = true;\n this.items = [];\n }\n\n if (this.activeCount !== 0) {\n return;\n }\n\n const completed = this.currentCycleCompleted.slice();\n this.currentCycleCompleted = [];\n\n if (this.onProgress) {\n this.onProgress({\n batchToProcess: this.batchToProcess,\n itemsToProcess: this.items.length,\n completed: completed,\n });\n }\n\n this.processNext();\n }\n\n terminate() {\n // remove all remaining jobs from queue\n this.items = [];\n }\n}\n\nexport default Qpass;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,IAAM,gBACF,OAAwC,UAAoB;AAEhE,IAAM,QAAN,MAAY;AAAA,EACR,UAAU;AAAA;AAAA,EAEF,QAAmD,CAAC;AAAA;AAAA,EACpD,YAAoB;AAAA;AAAA,EACpB,iBAA0B;AAAA;AAAA,EAC1B,YAAoB;AAAA;AAAA,EACpB;AAAA;AAAA,EAMA,cAAsB;AAAA;AAAA,EACtB,wBAA+B,CAAC;AAAA;AAAA,EAChC,iBAA0B;AAAA;AAAA,EAElC,IAAY,iBAAiB;AAEzB,WAAO,KAAK,KAAK,KAAK,MAAM,SAAS,KAAK,SAAS;AAAA,EACvD;AAAA,EAEA,YAAY,QAQT;AACC,SAAK,iBAAiB,CAAC,CAAC,QAAQ;AAChC,SAAK,YAAY,QAAQ,aAAa;AAEtC,QAAI,OAAO,KAAK,cAAc,YAAY,KAAK,YAAY,GAAG;AAC1D,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAClD;AAEA,QAAI,QAAQ,YAAY;AACpB,UAAI,OAAO,OAAO,eAAe,YAAY;AACzC,aAAK,aAAa,OAAO;AAAA,MAC7B,OAAO;AACH,cAAM,IAAI,MAAM,+BAA+B;AAAA,MACnD;AAAA,IACJ;AAAA,EACJ;AAAA;AAAA,EAGA,IAAI,MAA+D;AAE/D,UAAM,YAAY,MAAM,QAAQ,IAAI,IAAI,OAAO,CAAC,IAAI;AACpD,UAAM,WAAqB,CAAC;AAG5B,eAAW,OAAO,WAAW;AACzB,UAAI,OAAO,QAAQ,YAAY;AAC3B,cAAM,IAAI;AAAA,UACN;AAAA,QACJ;AAAA,MACJ;AAEA,YAAM,KAAK,OAAO,KAAK,WAAW;AAClC,WAAK,MAAM,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AAG;AACxB;AAAA,IACJ;AAEA,UAAM,YAAY,KAAK,sBAAsB,MAAM;AACnD,SAAK,wBAAwB,CAAC;AAE9B,QAAI,KAAK,YAAY;AACjB,WAAK,WAAW;AAAA,QACZ,gBAAgB,KAAK;AAAA,QACrB,gBAAgB,KAAK,MAAM;AAAA,QAC3B;AAAA,MACJ,CAAC;AAAA,IACL;AAEA,SAAK,YAAY;AAAA,EACrB;AAAA,EAEA,YAAY;AAER,SAAK,QAAQ,CAAC;AAAA,EAClB;AACJ;AAEA,IAAO,gBAAQ;","names":[]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
declare class Qpass {
|
|
2
2
|
version: string;
|
|
3
3
|
private items;
|
|
4
|
+
private nextJobId;
|
|
4
5
|
private breakWhenError;
|
|
5
6
|
private batchSize;
|
|
6
7
|
private onProgress?;
|
|
@@ -17,11 +18,12 @@ declare class Qpass {
|
|
|
17
18
|
}) => void;
|
|
18
19
|
batchSize?: number;
|
|
19
20
|
});
|
|
20
|
-
add(jobs: (() => Promise<any>)[] | (() => Promise<any>)):
|
|
21
|
+
add(jobs: (() => Promise<any>)[] | (() => Promise<any>)): string[];
|
|
22
|
+
setBatchSize(size: number): void;
|
|
23
|
+
remove(id: string | number): boolean;
|
|
21
24
|
private processNext;
|
|
22
25
|
private finalizeJob;
|
|
23
26
|
terminate(): void;
|
|
24
27
|
}
|
|
25
|
-
//# sourceMappingURL=index.d.ts.map
|
|
26
28
|
|
|
27
29
|
export { Qpass as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
declare class Qpass {
|
|
2
2
|
version: string;
|
|
3
3
|
private items;
|
|
4
|
+
private nextJobId;
|
|
4
5
|
private breakWhenError;
|
|
5
6
|
private batchSize;
|
|
6
7
|
private onProgress?;
|
|
@@ -17,11 +18,12 @@ declare class Qpass {
|
|
|
17
18
|
}) => void;
|
|
18
19
|
batchSize?: number;
|
|
19
20
|
});
|
|
20
|
-
add(jobs: (() => Promise<any>)[] | (() => Promise<any>)):
|
|
21
|
+
add(jobs: (() => Promise<any>)[] | (() => Promise<any>)): string[];
|
|
22
|
+
setBatchSize(size: number): void;
|
|
23
|
+
remove(id: string | number): boolean;
|
|
21
24
|
private processNext;
|
|
22
25
|
private finalizeJob;
|
|
23
26
|
terminate(): void;
|
|
24
27
|
}
|
|
25
|
-
//# sourceMappingURL=index.d.ts.map
|
|
26
28
|
|
|
27
29
|
export { Qpass as default };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// index.ts
|
|
2
|
-
var QPASS_VERSION = true ? "1.1
|
|
2
|
+
var QPASS_VERSION = true ? "1.2.1" : "0.0.0";
|
|
3
3
|
var Qpass = class {
|
|
4
4
|
version = QPASS_VERSION;
|
|
5
5
|
// version info
|
|
6
6
|
items = [];
|
|
7
7
|
// job queue
|
|
8
|
+
nextJobId = 1;
|
|
9
|
+
// auto-generated job id
|
|
8
10
|
breakWhenError = false;
|
|
9
11
|
// whether to stop on error
|
|
10
12
|
batchSize = 1;
|
|
@@ -34,18 +36,39 @@ var Qpass = class {
|
|
|
34
36
|
}
|
|
35
37
|
}
|
|
36
38
|
}
|
|
37
|
-
// add jobs to the queue at once
|
|
39
|
+
// add jobs to the queue at once. now returns job id.
|
|
38
40
|
add(jobs) {
|
|
39
41
|
const jobsArray = Array.isArray(jobs) ? jobs : [jobs];
|
|
42
|
+
const addedIds = [];
|
|
40
43
|
for (const job of jobsArray) {
|
|
41
44
|
if (typeof job !== "function") {
|
|
42
45
|
throw new Error(
|
|
43
46
|
"Each job must be a function that returns a Promise"
|
|
44
47
|
);
|
|
45
48
|
}
|
|
46
|
-
this.
|
|
49
|
+
const id = String(this.nextJobId++);
|
|
50
|
+
this.items.push({ id, job });
|
|
51
|
+
addedIds.push(id);
|
|
47
52
|
}
|
|
48
53
|
this.processNext();
|
|
54
|
+
return addedIds;
|
|
55
|
+
}
|
|
56
|
+
// change the number of jobs processed per batch
|
|
57
|
+
setBatchSize(size) {
|
|
58
|
+
if (typeof size !== "number" || size < 1) {
|
|
59
|
+
throw new Error("batchSize must be at least 1");
|
|
60
|
+
}
|
|
61
|
+
this.batchSize = size;
|
|
62
|
+
}
|
|
63
|
+
// remove a queued job by id
|
|
64
|
+
remove(id) {
|
|
65
|
+
const jobId = String(id);
|
|
66
|
+
const index = this.items.findIndex((item) => item.id === jobId);
|
|
67
|
+
if (index === -1) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
this.items.splice(index, 1);
|
|
71
|
+
return true;
|
|
49
72
|
}
|
|
50
73
|
// process next batch of jobs
|
|
51
74
|
processNext() {
|
|
@@ -55,8 +78,8 @@ var Qpass = class {
|
|
|
55
78
|
const jobsToRun = this.items.splice(0, this.batchSize);
|
|
56
79
|
this.activeCount = jobsToRun.length;
|
|
57
80
|
this.currentCycleCompleted = new Array(jobsToRun.length);
|
|
58
|
-
jobsToRun.forEach((
|
|
59
|
-
Promise.resolve().then(() => job()).then((result) => {
|
|
81
|
+
jobsToRun.forEach((item, index) => {
|
|
82
|
+
Promise.resolve().then(() => item.job()).then((result) => {
|
|
60
83
|
this.finalizeJob(index, result, false);
|
|
61
84
|
return result;
|
|
62
85
|
}).catch((err) => {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../index.ts"],"sourcesContent":["declare const __QPASS_VERSION__: string;\
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["declare const __QPASS_VERSION__: string;\n\nconst QPASS_VERSION =\n typeof __QPASS_VERSION__ === \"string\" ? __QPASS_VERSION__ : \"0.0.0\";\n\nclass Qpass {\n version = QPASS_VERSION; // version info\n\n private items: { id: string; job: () => Promise<any> }[] = []; // job queue\n private nextJobId: number = 1; // auto-generated job id\n private breakWhenError: boolean = false; // whether to stop on error\n private batchSize: number = 1; // number of jobs to process at once\n private onProgress?: (progress: {\n batchToProcess: number; // remaining batch count\n itemsToProcess: number; // remaining job count\n completed: any[]; // completed job results array\n }) => void; // progress callback\n\n private activeCount: number = 0; // number of jobs currently running\n private currentCycleCompleted: any[] = []; // completed results for current cycle\n private stopProcessing: boolean = false; // stop scheduling new jobs when true\n\n private get batchToProcess() {\n // calculate total batch count\n return Math.ceil(this.items.length / this.batchSize);\n }\n\n constructor(option?: {\n breakWhenError?: boolean;\n onProgress?: (progress: {\n batchToProcess: number;\n itemsToProcess: number;\n completed: any[];\n }) => void;\n batchSize?: number; // must be at least 1, default is 1\n }) {\n this.breakWhenError = !!option?.breakWhenError;\n this.batchSize = option?.batchSize ?? 1;\n\n if (typeof this.batchSize !== \"number\" || this.batchSize < 1) {\n throw new Error(\"batchSize must be at least 1\");\n }\n\n if (option?.onProgress) {\n if (typeof option.onProgress === \"function\") {\n this.onProgress = option.onProgress;\n } else {\n throw new Error(\"onProgress must be a function\");\n }\n }\n }\n\n // add jobs to the queue at once. now returns job id.\n add(jobs: (() => Promise<any>)[] | (() => Promise<any>)): string[] {\n // check if jobs is an array, if not convert to array\n const jobsArray = Array.isArray(jobs) ? jobs : [jobs];\n const addedIds: string[] = [];\n\n // check if each job is a function, if so add to queue\n for (const job of jobsArray) {\n if (typeof job !== \"function\") {\n throw new Error(\n \"Each job must be a function that returns a Promise\"\n );\n }\n\n const id = String(this.nextJobId++);\n this.items.push({ id, job });\n addedIds.push(id);\n }\n\n this.processNext();\n return addedIds;\n }\n\n // change the number of jobs processed per batch\n setBatchSize(size: number) {\n if (typeof size !== \"number\" || size < 1) {\n throw new Error(\"batchSize must be at least 1\");\n }\n this.batchSize = size;\n }\n\n // remove a queued job by id\n remove(id: string | number) {\n const jobId = String(id);\n const index = this.items.findIndex((item) => item.id === jobId);\n\n if (index === -1) {\n return false;\n }\n\n this.items.splice(index, 1);\n return true;\n }\n\n // process next batch of jobs\n private processNext() {\n // exit if queue is empty, processing is stopped, or a cycle is still running\n if (\n this.stopProcessing ||\n this.items.length === 0 ||\n this.activeCount > 0\n ) {\n return;\n }\n\n const jobsToRun = this.items.splice(0, this.batchSize);\n this.activeCount = jobsToRun.length;\n this.currentCycleCompleted = new Array(jobsToRun.length);\n\n jobsToRun.forEach((item, index) => {\n Promise.resolve()\n .then(() => item.job())\n .then((result) => {\n this.finalizeJob(index, result, false);\n return result;\n })\n .catch((err) => {\n this.finalizeJob(index, err, true);\n return err;\n });\n });\n }\n\n private finalizeJob(index: number, value: any, isError: boolean) {\n this.currentCycleCompleted[index] = value;\n this.activeCount -= 1;\n\n if (isError && this.breakWhenError) {\n this.stopProcessing = true;\n this.items = [];\n }\n\n if (this.activeCount !== 0) {\n return;\n }\n\n const completed = this.currentCycleCompleted.slice();\n this.currentCycleCompleted = [];\n\n if (this.onProgress) {\n this.onProgress({\n batchToProcess: this.batchToProcess,\n itemsToProcess: this.items.length,\n completed: completed,\n });\n }\n\n this.processNext();\n }\n\n terminate() {\n // remove all remaining jobs from queue\n this.items = [];\n }\n}\n\nexport default Qpass;\n"],"mappings":";AAEA,IAAM,gBACF,OAAwC,UAAoB;AAEhE,IAAM,QAAN,MAAY;AAAA,EACR,UAAU;AAAA;AAAA,EAEF,QAAmD,CAAC;AAAA;AAAA,EACpD,YAAoB;AAAA;AAAA,EACpB,iBAA0B;AAAA;AAAA,EAC1B,YAAoB;AAAA;AAAA,EACpB;AAAA;AAAA,EAMA,cAAsB;AAAA;AAAA,EACtB,wBAA+B,CAAC;AAAA;AAAA,EAChC,iBAA0B;AAAA;AAAA,EAElC,IAAY,iBAAiB;AAEzB,WAAO,KAAK,KAAK,KAAK,MAAM,SAAS,KAAK,SAAS;AAAA,EACvD;AAAA,EAEA,YAAY,QAQT;AACC,SAAK,iBAAiB,CAAC,CAAC,QAAQ;AAChC,SAAK,YAAY,QAAQ,aAAa;AAEtC,QAAI,OAAO,KAAK,cAAc,YAAY,KAAK,YAAY,GAAG;AAC1D,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAClD;AAEA,QAAI,QAAQ,YAAY;AAC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,IAAI,CAAC,EACrB,KAAK,CAAC,WAAW;AACd,aAAK,YAAY,OAAO,QAAQ,KAAK;AACrC,eAAO;AAAA,MACX,CAAC,EACA,MAAM,CAAC,QAAQ;AACZ,aAAK,YAAY,OAAO,KAAK,IAAI;AACjC,eAAO;AAAA,MACX,CAAC;AAAA,IACT,CAAC;AAAA,EACL;AAAA,EAEQ,YAAY,OAAe,OAAY,SAAkB;AAC7D,SAAK,sBAAsB,KAAK,IAAI;AACpC,SAAK,eAAe;AAEpB,QAAI,WAAW,KAAK,gBAAgB;AAChC,WAAK,iBAAiB;AACtB,WAAK,QAAQ,CAAC;AAAA,IAClB;AAEA,QAAI,KAAK,gBAAgB,GAAG;AACxB;AAAA,IACJ;AAEA,UAAM,YAAY,KAAK,sBAAsB,MAAM;AACnD,SAAK,wBAAwB,CAAC;AAE9B,QAAI,KAAK,YAAY;AACjB,WAAK,WAAW;AAAA,QACZ,gBAAgB,KAAK;AAAA,QACrB,gBAAgB,KAAK,MAAM;AAAA,QAC3B;AAAA,MACJ,CAAC;AAAA,IACL;AAEA,SAAK,YAAY;AAAA,EACrB;AAAA,EAEA,YAAY;AAER,SAAK,QAAQ,CAAC;AAAA,EAClB;AACJ;AAEA,IAAO,gBAAQ;","names":[]}
|
package/dist/qpass.js
CHANGED
|
@@ -23,12 +23,14 @@ var Qpass = (() => {
|
|
|
23
23
|
__export(index_exports, {
|
|
24
24
|
default: () => index_default
|
|
25
25
|
});
|
|
26
|
-
var QPASS_VERSION = true ? "1.1
|
|
26
|
+
var QPASS_VERSION = true ? "1.2.1" : "0.0.0";
|
|
27
27
|
var Qpass = class {
|
|
28
28
|
version = QPASS_VERSION;
|
|
29
29
|
// version info
|
|
30
30
|
items = [];
|
|
31
31
|
// job queue
|
|
32
|
+
nextJobId = 1;
|
|
33
|
+
// auto-generated job id
|
|
32
34
|
breakWhenError = false;
|
|
33
35
|
// whether to stop on error
|
|
34
36
|
batchSize = 1;
|
|
@@ -58,18 +60,39 @@ var Qpass = (() => {
|
|
|
58
60
|
}
|
|
59
61
|
}
|
|
60
62
|
}
|
|
61
|
-
// add jobs to the queue at once
|
|
63
|
+
// add jobs to the queue at once. now returns job id.
|
|
62
64
|
add(jobs) {
|
|
63
65
|
const jobsArray = Array.isArray(jobs) ? jobs : [jobs];
|
|
66
|
+
const addedIds = [];
|
|
64
67
|
for (const job of jobsArray) {
|
|
65
68
|
if (typeof job !== "function") {
|
|
66
69
|
throw new Error(
|
|
67
70
|
"Each job must be a function that returns a Promise"
|
|
68
71
|
);
|
|
69
72
|
}
|
|
70
|
-
this.
|
|
73
|
+
const id = String(this.nextJobId++);
|
|
74
|
+
this.items.push({ id, job });
|
|
75
|
+
addedIds.push(id);
|
|
71
76
|
}
|
|
72
77
|
this.processNext();
|
|
78
|
+
return addedIds;
|
|
79
|
+
}
|
|
80
|
+
// change the number of jobs processed per batch
|
|
81
|
+
setBatchSize(size) {
|
|
82
|
+
if (typeof size !== "number" || size < 1) {
|
|
83
|
+
throw new Error("batchSize must be at least 1");
|
|
84
|
+
}
|
|
85
|
+
this.batchSize = size;
|
|
86
|
+
}
|
|
87
|
+
// remove a queued job by id
|
|
88
|
+
remove(id) {
|
|
89
|
+
const jobId = String(id);
|
|
90
|
+
const index = this.items.findIndex((item) => item.id === jobId);
|
|
91
|
+
if (index === -1) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
this.items.splice(index, 1);
|
|
95
|
+
return true;
|
|
73
96
|
}
|
|
74
97
|
// process next batch of jobs
|
|
75
98
|
processNext() {
|
|
@@ -79,8 +102,8 @@ var Qpass = (() => {
|
|
|
79
102
|
const jobsToRun = this.items.splice(0, this.batchSize);
|
|
80
103
|
this.activeCount = jobsToRun.length;
|
|
81
104
|
this.currentCycleCompleted = new Array(jobsToRun.length);
|
|
82
|
-
jobsToRun.forEach((
|
|
83
|
-
Promise.resolve().then(() => job()).then((result) => {
|
|
105
|
+
jobsToRun.forEach((item, index) => {
|
|
106
|
+
Promise.resolve().then(() => item.job()).then((result) => {
|
|
84
107
|
this.finalizeJob(index, result, false);
|
|
85
108
|
return result;
|
|
86
109
|
}).catch((err) => {
|
package/dist/qpass.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../index.ts"],"sourcesContent":["declare const __QPASS_VERSION__: string;\
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["declare const __QPASS_VERSION__: string;\n\nconst QPASS_VERSION =\n typeof __QPASS_VERSION__ === \"string\" ? __QPASS_VERSION__ : \"0.0.0\";\n\nclass Qpass {\n version = QPASS_VERSION; // version info\n\n private items: { id: string; job: () => Promise<any> }[] = []; // job queue\n private nextJobId: number = 1; // auto-generated job id\n private breakWhenError: boolean = false; // whether to stop on error\n private batchSize: number = 1; // number of jobs to process at once\n private onProgress?: (progress: {\n batchToProcess: number; // remaining batch count\n itemsToProcess: number; // remaining job count\n completed: any[]; // completed job results array\n }) => void; // progress callback\n\n private activeCount: number = 0; // number of jobs currently running\n private currentCycleCompleted: any[] = []; // completed results for current cycle\n private stopProcessing: boolean = false; // stop scheduling new jobs when true\n\n private get batchToProcess() {\n // calculate total batch count\n return Math.ceil(this.items.length / this.batchSize);\n }\n\n constructor(option?: {\n breakWhenError?: boolean;\n onProgress?: (progress: {\n batchToProcess: number;\n itemsToProcess: number;\n completed: any[];\n }) => void;\n batchSize?: number; // must be at least 1, default is 1\n }) {\n this.breakWhenError = !!option?.breakWhenError;\n this.batchSize = option?.batchSize ?? 1;\n\n if (typeof this.batchSize !== \"number\" || this.batchSize < 1) {\n throw new Error(\"batchSize must be at least 1\");\n }\n\n if (option?.onProgress) {\n if (typeof option.onProgress === \"function\") {\n this.onProgress = option.onProgress;\n } else {\n throw new Error(\"onProgress must be a function\");\n }\n }\n }\n\n // add jobs to the queue at once. now returns job id.\n add(jobs: (() => Promise<any>)[] | (() => Promise<any>)): string[] {\n // check if jobs is an array, if not convert to array\n const jobsArray = Array.isArray(jobs) ? jobs : [jobs];\n const addedIds: string[] = [];\n\n // check if each job is a function, if so add to queue\n for (const job of jobsArray) {\n if (typeof job !== \"function\") {\n throw new Error(\n \"Each job must be a function that returns a Promise\"\n );\n }\n\n const id = String(this.nextJobId++);\n this.items.push({ id, job });\n addedIds.push(id);\n }\n\n this.processNext();\n return addedIds;\n }\n\n // change the number of jobs processed per batch\n setBatchSize(size: number) {\n if (typeof size !== \"number\" || size < 1) {\n throw new Error(\"batchSize must be at least 1\");\n }\n this.batchSize = size;\n }\n\n // remove a queued job by id\n remove(id: string | number) {\n const jobId = String(id);\n const index = this.items.findIndex((item) => item.id === jobId);\n\n if (index === -1) {\n return false;\n }\n\n this.items.splice(index, 1);\n return true;\n }\n\n // process next batch of jobs\n private processNext() {\n // exit if queue is empty, processing is stopped, or a cycle is still running\n if (\n this.stopProcessing ||\n this.items.length === 0 ||\n this.activeCount > 0\n ) {\n return;\n }\n\n const jobsToRun = this.items.splice(0, this.batchSize);\n this.activeCount = jobsToRun.length;\n this.currentCycleCompleted = new Array(jobsToRun.length);\n\n jobsToRun.forEach((item, index) => {\n Promise.resolve()\n .then(() => item.job())\n .then((result) => {\n this.finalizeJob(index, result, false);\n return result;\n })\n .catch((err) => {\n this.finalizeJob(index, err, true);\n return err;\n });\n });\n }\n\n private finalizeJob(index: number, value: any, isError: boolean) {\n this.currentCycleCompleted[index] = value;\n this.activeCount -= 1;\n\n if (isError && this.breakWhenError) {\n this.stopProcessing = true;\n this.items = [];\n }\n\n if (this.activeCount !== 0) {\n return;\n }\n\n const completed = this.currentCycleCompleted.slice();\n this.currentCycleCompleted = [];\n\n if (this.onProgress) {\n this.onProgress({\n batchToProcess: this.batchToProcess,\n itemsToProcess: this.items.length,\n completed: completed,\n });\n }\n\n this.processNext();\n }\n\n terminate() {\n // remove all remaining jobs from queue\n this.items = [];\n }\n}\n\nexport default Qpass;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAEA,MAAM,gBACF,OAAwC,UAAo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CAAC;AAAA,MACL;AAEA,WAAK,YAAY;AAAA,IACrB;AAAA,IAEA,YAAY;AAER,WAAK,QAAQ,CAAC;AAAA,IAClB;AAAA,EACJ;AAEA,MAAO,gBAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "qpass",
|
|
3
|
-
"version": "1.1
|
|
4
|
-
"description": "Asynchronous function handler with adjustable concurrency",
|
|
5
|
-
"main": "./dist/index.cjs",
|
|
6
|
-
"module": "./dist/index.js",
|
|
7
|
-
"types": "./dist/index.d.ts",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"types": "./dist/index.d.ts",
|
|
11
|
-
"import": "./dist/index.js",
|
|
12
|
-
"require": "./dist/index.cjs",
|
|
13
|
-
"default": "./dist/index.js"
|
|
14
|
-
},
|
|
15
|
-
"./package.json": "./package.json"
|
|
16
|
-
},
|
|
17
|
-
"browser": "./dist/index.js",
|
|
18
|
-
"unpkg": "./dist/qpass.js",
|
|
19
|
-
"jsdelivr": "./dist/qpass.js",
|
|
20
|
-
"files": [
|
|
21
|
-
"dist",
|
|
22
|
-
"README.md"
|
|
23
|
-
],
|
|
24
|
-
"type": "module",
|
|
25
|
-
"scripts": {
|
|
26
|
-
"test": "npm run build && node ./test.js",
|
|
27
|
-
"typecheck": "tsc --noEmit --project tsconfig.json",
|
|
28
|
-
"build": "tsup --config tsup.config.mjs"
|
|
29
|
-
},
|
|
30
|
-
"author": "",
|
|
31
|
-
"license": "ISC",
|
|
32
|
-
"devDependencies": {
|
|
33
|
-
"tsup": "^8.5.0",
|
|
34
|
-
"typescript": "^5.9.3"
|
|
35
|
-
}
|
|
36
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "qpass",
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"description": "Asynchronous function handler with adjustable concurrency",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"browser": "./dist/index.js",
|
|
18
|
+
"unpkg": "./dist/qpass.js",
|
|
19
|
+
"jsdelivr": "./dist/qpass.js",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "npm run build && node ./test.js",
|
|
27
|
+
"typecheck": "tsc --noEmit --project tsconfig.json",
|
|
28
|
+
"build": "tsup --config tsup.config.mjs"
|
|
29
|
+
},
|
|
30
|
+
"author": "",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"tsup": "^8.5.0",
|
|
34
|
+
"typescript": "^5.9.3"
|
|
35
|
+
}
|
|
36
|
+
}
|