@openclaw/qqbot 2026.7.1 → 2026.7.2-beta.2
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/api.js +8 -7
- package/dist/{channel-entry-C5YdhX3Y.js → channel-entry-CA2T2sf8.js} +4 -4
- package/dist/channel-entry-api.js +1 -1
- package/dist/{channel-D1UztsnG.js → channel-nz3Mkify.js} +99 -54
- package/dist/channel-plugin-api.js +1 -1
- package/dist/{channel.setup-2ItDYKhz.js → channel.setup-CNoBtKXQ.js} +1 -1
- package/dist/{config-C1qZbh0K.js → config-CpOXnoEc.js} +2 -2
- package/dist/{config-schema-JZEf1dvB.js → config-schema-D7MaH5X5.js} +61 -76
- package/dist/doctor-contract-api.js +4 -10
- package/dist/{gateway-pJQppxe4.js → gateway-D8uYPtoy.js} +286 -174
- package/dist/{group-o0GmovSf.js → group-BVHG8qUZ.js} +40 -23
- package/dist/{handler-runtime-zQvT6SrI.js → handler-runtime-S1_XF8on.js} +8 -12
- package/dist/{log-DEtcoDWe.js → log-Da4jz75I.js} +4 -8
- package/dist/{outbound-BIrfvvFJ.js → outbound-FOG4zNLY.js} +8 -142
- package/dist/{runtime-DodcT_fQ.js → runtime-CyjBiGD2.js} +2 -2
- package/dist/runtime-api.js +1 -1
- package/dist/secret-contract-api.js +6 -22
- package/dist/{sender-CjDuU-uz.js → sender-BAUHZqDW.js} +225 -66
- package/dist/setup-plugin-api.js +1 -1
- package/dist/state-keys-jLJ2SmJA.js +225 -0
- package/dist/{tools-UJJ-tLHP.js → tools-CC5CKQig.js} +71 -26
- package/dist/tools-api.js +1 -1
- package/node_modules/p-map/index.d.ts +155 -0
- package/node_modules/p-map/index.js +284 -0
- package/node_modules/p-map/license +9 -0
- package/node_modules/p-map/package.json +57 -0
- package/node_modules/p-map/readme.md +190 -0
- package/node_modules/parse-ms/index.d.ts +30 -0
- package/node_modules/parse-ms/index.js +45 -0
- package/node_modules/parse-ms/license +9 -0
- package/node_modules/parse-ms/package.json +47 -0
- package/node_modules/parse-ms/readme.md +46 -0
- package/node_modules/pretty-ms/index.d.ts +157 -0
- package/node_modules/pretty-ms/index.js +149 -0
- package/node_modules/pretty-ms/license +9 -0
- package/node_modules/pretty-ms/package.json +55 -0
- package/node_modules/pretty-ms/readme.md +179 -0
- package/npm-shrinkwrap.json +44 -3
- package/openclaw.plugin.json +105 -133
- package/package.json +8 -4
- package/dist/state-keys-CQKlAFyo.js +0 -141
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { a as resolveQQBotAccount, r as listQQBotAccountIds } from "./config-
|
|
2
|
-
import { a as formatErrorMessage, n as debugLog, t as debugError } from "./log-
|
|
1
|
+
import { a as resolveQQBotAccount, r as listQQBotAccountIds } from "./config-CpOXnoEc.js";
|
|
2
|
+
import { a as formatErrorMessage, n as debugLog, t as debugError } from "./log-Da4jz75I.js";
|
|
3
3
|
import { t as getRequestContext } from "./request-context-Bm7PTBD1.js";
|
|
4
4
|
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
|
|
5
5
|
import { readProviderTextResponse, readResponseTextLimited } from "openclaw/plugin-sdk/provider-http";
|
|
6
6
|
import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime";
|
|
7
7
|
import { resolveExpiresAtMsFromDurationMs } from "openclaw/plugin-sdk/number-runtime";
|
|
8
|
+
import { resolveChannelGroupPolicy } from "openclaw/plugin-sdk/channel-policy";
|
|
8
9
|
import { jsonResult } from "openclaw/plugin-sdk/tool-results";
|
|
9
10
|
import { callGatewayTool } from "openclaw/plugin-sdk/agent-harness-runtime";
|
|
10
11
|
//#region extensions/qqbot/src/engine/tools/channel-api.ts
|
|
@@ -109,6 +110,35 @@ function decodePathSegments(path) {
|
|
|
109
110
|
return null;
|
|
110
111
|
}
|
|
111
112
|
}
|
|
113
|
+
function resolvePathTarget(path) {
|
|
114
|
+
const segments = decodePathSegments(path);
|
|
115
|
+
if (!segments || segments.length === 0) return { kind: "unverified" };
|
|
116
|
+
const [scope, firstId, second] = segments;
|
|
117
|
+
if (scope?.toLowerCase() === "users" && firstId?.toLowerCase() === "@me" && second?.toLowerCase() === "guilds") return { kind: "guild-list" };
|
|
118
|
+
if (scope?.toLowerCase() === "guilds" && firstId) return {
|
|
119
|
+
kind: "guild",
|
|
120
|
+
id: firstId
|
|
121
|
+
};
|
|
122
|
+
if (scope?.toLowerCase() === "channels" && firstId) return {
|
|
123
|
+
kind: "channel",
|
|
124
|
+
id: firstId
|
|
125
|
+
};
|
|
126
|
+
return { kind: "unverified" };
|
|
127
|
+
}
|
|
128
|
+
function validateConfiguredTargetScope(path, options) {
|
|
129
|
+
if (!options.cfg) return null;
|
|
130
|
+
const basePolicy = resolveChannelGroupPolicy({
|
|
131
|
+
cfg: options.cfg,
|
|
132
|
+
channel: "qqbot",
|
|
133
|
+
accountId: options.accountId,
|
|
134
|
+
groupIdCaseInsensitive: true
|
|
135
|
+
});
|
|
136
|
+
if (!basePolicy.allowlistEnabled && basePolicy.allowed) return null;
|
|
137
|
+
const target = resolvePathTarget(path);
|
|
138
|
+
if (target.kind === "guild-list") return basePolicy.allowed ? null : "QQ channel API guild listing is unavailable while qqbot groups are scoped.";
|
|
139
|
+
if (target.kind === "unverified") return basePolicy.allowed ? null : "QQ channel API path target cannot be verified against configured qqbot groups.";
|
|
140
|
+
return basePolicy.allowed ? null : `QQ channel API ${target.kind} paths are unavailable while qqbot groups are scoped.`;
|
|
141
|
+
}
|
|
112
142
|
function isBulkAnnouncementDeletePath(path) {
|
|
113
143
|
const segments = decodePathSegments(path);
|
|
114
144
|
return Boolean(segments && segments.length === 4 && segments[0]?.toLowerCase() === "guilds" && segments[2]?.toLowerCase() === "announces" && segments[3]?.toLowerCase() === "all");
|
|
@@ -140,6 +170,11 @@ async function executeChannelApi(params, options) {
|
|
|
140
170
|
].includes(method)) return jsonResult({ error: `Unsupported HTTP method: ${method}. Allowed values: GET, POST, PUT, PATCH, DELETE` });
|
|
141
171
|
const pathError = validatePath(params.path);
|
|
142
172
|
if (pathError) return jsonResult({ error: pathError });
|
|
173
|
+
const scopeError = validateConfiguredTargetScope(params.path, options);
|
|
174
|
+
if (scopeError) return jsonResult({
|
|
175
|
+
error: scopeError,
|
|
176
|
+
path: params.path
|
|
177
|
+
});
|
|
143
178
|
const confirmationError = validateDeleteConfirmation({
|
|
144
179
|
...params,
|
|
145
180
|
method
|
|
@@ -168,8 +203,8 @@ async function executeChannelApi(params, options) {
|
|
|
168
203
|
"PATCH"
|
|
169
204
|
].includes(method)) fetchOptions.body = JSON.stringify(params.body);
|
|
170
205
|
debugLog(`[qqbot-channel-api] >>> ${method} ${url} (timeout: ${DEFAULT_TIMEOUT_MS}ms)`);
|
|
171
|
-
let res;
|
|
172
206
|
let release;
|
|
207
|
+
let receivedResponse = false;
|
|
173
208
|
try {
|
|
174
209
|
const guarded = await fetchWithSsrFGuard({
|
|
175
210
|
url,
|
|
@@ -177,26 +212,9 @@ async function executeChannelApi(params, options) {
|
|
|
177
212
|
auditContext: "qqbot-channel-api",
|
|
178
213
|
policy: resolveChannelApiSsrfPolicy(url)
|
|
179
214
|
});
|
|
180
|
-
res = guarded.response;
|
|
181
215
|
release = guarded.release;
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
if (err instanceof Error && err.name === "AbortError") {
|
|
185
|
-
debugError(`[qqbot-channel-api] <<< Request timeout after ${DEFAULT_TIMEOUT_MS}ms`);
|
|
186
|
-
return jsonResult({
|
|
187
|
-
error: `Request timed out after ${DEFAULT_TIMEOUT_MS}ms`,
|
|
188
|
-
path: params.path
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
debugError("[qqbot-channel-api] <<< Network error:", err);
|
|
192
|
-
return jsonResult({
|
|
193
|
-
error: `Network error: ${formatErrorMessage(err)}`,
|
|
194
|
-
path: params.path
|
|
195
|
-
});
|
|
196
|
-
} finally {
|
|
197
|
-
clearTimeout(timeoutId);
|
|
198
|
-
}
|
|
199
|
-
try {
|
|
216
|
+
receivedResponse = true;
|
|
217
|
+
const res = guarded.response;
|
|
200
218
|
debugLog(`[qqbot-channel-api] <<< Status: ${res.status} ${res.statusText}`);
|
|
201
219
|
const rawBody = res.ok ? await readProviderTextResponse(res, "QQ channel API response") : await readResponseTextLimited(res, CHANNEL_API_ERROR_BODY_LIMIT_BYTES);
|
|
202
220
|
if (!rawBody || rawBody.trim() === "") {
|
|
@@ -233,7 +251,27 @@ async function executeChannelApi(params, options) {
|
|
|
233
251
|
path: params.path,
|
|
234
252
|
data: parsed
|
|
235
253
|
});
|
|
254
|
+
} catch (err) {
|
|
255
|
+
if (controller.signal.aborted && err instanceof Error && err.name === "AbortError") {
|
|
256
|
+
debugError(`[qqbot-channel-api] <<< Request timeout after ${DEFAULT_TIMEOUT_MS}ms`);
|
|
257
|
+
return jsonResult({
|
|
258
|
+
error: `Request timed out after ${DEFAULT_TIMEOUT_MS}ms`,
|
|
259
|
+
path: params.path
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
if (!receivedResponse) {
|
|
263
|
+
debugError("[qqbot-channel-api] <<< Network error:", err);
|
|
264
|
+
return jsonResult({
|
|
265
|
+
error: `Network error: ${formatErrorMessage(err)}`,
|
|
266
|
+
path: params.path
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
return jsonResult({
|
|
270
|
+
error: formatErrorMessage(err),
|
|
271
|
+
path: params.path
|
|
272
|
+
});
|
|
236
273
|
} finally {
|
|
274
|
+
clearTimeout(timeoutId);
|
|
237
275
|
await release?.();
|
|
238
276
|
}
|
|
239
277
|
} catch (err) {
|
|
@@ -266,8 +304,12 @@ function registerChannelTool(api) {
|
|
|
266
304
|
description: "Authenticated HTTP proxy for QQ Open Platform channel APIs. Use write and delete endpoints only after explicit user intent; DELETE requires confirmed=true, and bulk deletes require bulkConfirmed=true after confirming the exact target. Common endpoints: list guilds GET /users/@me/guilds | list channels GET /guilds/{guild_id}/channels | get channel GET /channels/{channel_id} | create channel POST /guilds/{guild_id}/channels | list members GET /guilds/{guild_id}/members?after=0&limit=100 | get member GET /guilds/{guild_id}/members/{user_id} | list threads GET /channels/{channel_id}/threads | create thread PUT /channels/{channel_id}/threads | create announce POST /guilds/{guild_id}/announces | create schedule POST /channels/{channel_id}/schedules. See the qqbot-channel skill for full endpoint details.",
|
|
267
305
|
parameters: ChannelApiSchema,
|
|
268
306
|
async execute(_toolCallId, params) {
|
|
269
|
-
const { getAccessToken } = await import("./sender-
|
|
270
|
-
return executeChannelApi(params, {
|
|
307
|
+
const { getAccessToken } = await import("./sender-BAUHZqDW.js").then((n) => n._);
|
|
308
|
+
return executeChannelApi(params, {
|
|
309
|
+
accessToken: await getAccessToken(account.appId, account.clientSecret),
|
|
310
|
+
cfg,
|
|
311
|
+
accountId: firstAccountId
|
|
312
|
+
});
|
|
271
313
|
}
|
|
272
314
|
}, { name: "qqbot_channel_api" });
|
|
273
315
|
}
|
|
@@ -337,8 +379,11 @@ function parseRelativeTime(timeStr) {
|
|
|
337
379
|
if (match.index !== consumed) return null;
|
|
338
380
|
matched = true;
|
|
339
381
|
consumed = regex.lastIndex;
|
|
340
|
-
const
|
|
341
|
-
|
|
382
|
+
const valueText = match[1];
|
|
383
|
+
const unit = match[2];
|
|
384
|
+
if (valueText === void 0 || unit === void 0) return null;
|
|
385
|
+
const value = Number.parseFloat(valueText);
|
|
386
|
+
switch (unit) {
|
|
342
387
|
case "d":
|
|
343
388
|
totalMs += value * 864e5;
|
|
344
389
|
break;
|
package/dist/tools-api.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as registerQQBotTools } from "./tools-
|
|
1
|
+
import { t as registerQQBotTools } from "./tools-CC5CKQig.js";
|
|
2
2
|
export { registerQQBotTools };
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
type BaseOptions = {
|
|
2
|
+
/**
|
|
3
|
+
Number of concurrently pending promises returned by `mapper`.
|
|
4
|
+
|
|
5
|
+
Must be an integer from 1 and up or `Infinity`.
|
|
6
|
+
|
|
7
|
+
@default Infinity
|
|
8
|
+
*/
|
|
9
|
+
readonly concurrency?: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type Options = BaseOptions & {
|
|
13
|
+
/**
|
|
14
|
+
When `true`, the first mapper rejection will be rejected back to the consumer.
|
|
15
|
+
|
|
16
|
+
When `false`, instead of stopping when a promise rejects, it will wait for all the promises to settle and then reject with an [`AggregateError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) containing all the errors from the rejected promises.
|
|
17
|
+
|
|
18
|
+
Caveat: When `true`, any already-started async mappers will continue to run until they resolve or reject. In the case of infinite concurrency with sync iterables, *all* mappers are invoked on startup and will continue after the first rejection. Use the `signal` option for abort control.
|
|
19
|
+
|
|
20
|
+
@default true
|
|
21
|
+
*/
|
|
22
|
+
readonly stopOnError?: boolean;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
You can abort the promises using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
|
|
26
|
+
|
|
27
|
+
@example
|
|
28
|
+
```
|
|
29
|
+
import pMap from 'p-map';
|
|
30
|
+
import delay from 'delay';
|
|
31
|
+
|
|
32
|
+
const abortController = new AbortController();
|
|
33
|
+
|
|
34
|
+
setTimeout(() => {
|
|
35
|
+
abortController.abort();
|
|
36
|
+
}, 500);
|
|
37
|
+
|
|
38
|
+
const mapper = async value => value;
|
|
39
|
+
|
|
40
|
+
await pMap([delay(1000), delay(1000)], mapper, {signal: abortController.signal});
|
|
41
|
+
// Throws AbortError (DOMException) after 500 ms.
|
|
42
|
+
```
|
|
43
|
+
*/
|
|
44
|
+
readonly signal?: AbortSignal | undefined;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type IterableOptions = BaseOptions & {
|
|
48
|
+
/**
|
|
49
|
+
Maximum number of promises returned by `mapper` that have resolved but not yet collected by the consumer of the async iterable. Calls to `mapper` will be limited so that there is never too much backpressure.
|
|
50
|
+
|
|
51
|
+
Useful whenever you are consuming the iterable slower than what the mapper function can produce concurrently. For example, to avoid making an overwhelming number of HTTP requests if you are saving each of the results to a database.
|
|
52
|
+
|
|
53
|
+
Default: `options.concurrency`
|
|
54
|
+
*/
|
|
55
|
+
readonly backpressure?: number;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
Function which is called for every item in `input`. Expected to return a `Promise` or value.
|
|
62
|
+
|
|
63
|
+
@param element - Iterated element.
|
|
64
|
+
@param index - Index of the element in the source array.
|
|
65
|
+
*/
|
|
66
|
+
export type Mapper<Element = any, NewElement = unknown> = (
|
|
67
|
+
element: Element,
|
|
68
|
+
index: number
|
|
69
|
+
) => MaybePromise<NewElement | typeof pMapSkip>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
@param input - Synchronous or asynchronous iterable that is iterated over concurrently, calling the `mapper` function for each element. Each iterated item is `await`'d before the `mapper` is invoked so the iterable may return a `Promise` that resolves to an item. Asynchronous iterables (different from synchronous iterables that return `Promise` that resolves to an item) can be used when the next item may not be ready without waiting for an asynchronous process to complete and/or the end of the iterable may be reached after the asynchronous process completes. For example, reading from a remote queue when the queue has reached empty, or reading lines from a stream.
|
|
73
|
+
@param mapper - Function which is called for every item in `input`. Expected to return a `Promise` or value.
|
|
74
|
+
@returns A `Promise` that is fulfilled when all promises in `input` and the ones returned from `mapper` are fulfilled, or rejects if any of the promises reject. The fulfilled value is an `Array` of the fulfilled values returned from `mapper` in `input` order.
|
|
75
|
+
|
|
76
|
+
@example
|
|
77
|
+
```
|
|
78
|
+
import pMap from 'p-map';
|
|
79
|
+
import got from 'got';
|
|
80
|
+
|
|
81
|
+
const sites = [
|
|
82
|
+
getWebsiteFromUsername('sindresorhus'), //=> Promise
|
|
83
|
+
'https://avajs.dev',
|
|
84
|
+
'https://github.com'
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
const mapper = async site => {
|
|
88
|
+
const {requestUrl} = await got.head(site);
|
|
89
|
+
return requestUrl;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const result = await pMap(sites, mapper, {concurrency: 2});
|
|
93
|
+
|
|
94
|
+
console.log(result);
|
|
95
|
+
//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/']
|
|
96
|
+
```
|
|
97
|
+
*/
|
|
98
|
+
export default function pMap<Element, NewElement>(
|
|
99
|
+
input: AsyncIterable<Element | Promise<Element>> | Iterable<Element | Promise<Element>>,
|
|
100
|
+
mapper: Mapper<Element, NewElement>,
|
|
101
|
+
options?: Options
|
|
102
|
+
): Promise<Array<Exclude<NewElement, typeof pMapSkip>>>;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
@param input - Synchronous or asynchronous iterable that is iterated over concurrently, calling the `mapper` function for each element. Each iterated item is `await`'d before the `mapper` is invoked so the iterable may return a `Promise` that resolves to an item. Asynchronous iterables (different from synchronous iterables that return `Promise` that resolves to an item) can be used when the next item may not be ready without waiting for an asynchronous process to complete and/or the end of the iterable may be reached after the asynchronous process completes. For example, reading from a remote queue when the queue has reached empty, or reading lines from a stream.
|
|
106
|
+
@param mapper - Function which is called for every item in `input`. Expected to return a `Promise` or value.
|
|
107
|
+
@returns An async iterable that streams each return value from `mapper` in order.
|
|
108
|
+
|
|
109
|
+
@example
|
|
110
|
+
```
|
|
111
|
+
import {pMapIterable} from 'p-map';
|
|
112
|
+
|
|
113
|
+
// Multiple posts are fetched concurrently, with limited concurrency and backpressure
|
|
114
|
+
for await (const post of pMapIterable(postIds, getPostMetadata, {concurrency: 8})) {
|
|
115
|
+
console.log(post);
|
|
116
|
+
};
|
|
117
|
+
```
|
|
118
|
+
*/
|
|
119
|
+
export function pMapIterable<Element, NewElement>(
|
|
120
|
+
input: AsyncIterable<Element | Promise<Element>> | Iterable<Element | Promise<Element>>,
|
|
121
|
+
mapper: Mapper<Element, NewElement>,
|
|
122
|
+
options?: IterableOptions
|
|
123
|
+
): AsyncIterable<Exclude<NewElement, typeof pMapSkip>>;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
Return this value from a `mapper` function to skip including the value in the returned array.
|
|
127
|
+
|
|
128
|
+
@example
|
|
129
|
+
```
|
|
130
|
+
import pMap, {pMapSkip} from 'p-map';
|
|
131
|
+
import got from 'got';
|
|
132
|
+
|
|
133
|
+
const sites = [
|
|
134
|
+
getWebsiteFromUsername('sindresorhus'), //=> Promise
|
|
135
|
+
'https://avajs.dev',
|
|
136
|
+
'https://example.invalid',
|
|
137
|
+
'https://github.com'
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
const mapper = async site => {
|
|
141
|
+
try {
|
|
142
|
+
const {requestUrl} = await got.head(site);
|
|
143
|
+
return requestUrl;
|
|
144
|
+
} catch {
|
|
145
|
+
return pMapSkip;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const result = await pMap(sites, mapper, {concurrency: 2});
|
|
150
|
+
|
|
151
|
+
console.log(result);
|
|
152
|
+
//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/']
|
|
153
|
+
```
|
|
154
|
+
*/
|
|
155
|
+
export const pMapSkip: unique symbol;
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
export default async function pMap(
|
|
2
|
+
iterable,
|
|
3
|
+
mapper,
|
|
4
|
+
{
|
|
5
|
+
concurrency = Number.POSITIVE_INFINITY,
|
|
6
|
+
stopOnError = true,
|
|
7
|
+
signal,
|
|
8
|
+
} = {},
|
|
9
|
+
) {
|
|
10
|
+
return new Promise((resolve_, reject_) => {
|
|
11
|
+
if (iterable[Symbol.iterator] === undefined && iterable[Symbol.asyncIterator] === undefined) {
|
|
12
|
+
throw new TypeError(`Expected \`input\` to be either an \`Iterable\` or \`AsyncIterable\`, got (${typeof iterable})`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (typeof mapper !== 'function') {
|
|
16
|
+
throw new TypeError('Mapper function is required');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!((Number.isSafeInteger(concurrency) && concurrency >= 1) || concurrency === Number.POSITIVE_INFINITY)) {
|
|
20
|
+
throw new TypeError(`Expected \`concurrency\` to be an integer from 1 and up or \`Infinity\`, got \`${concurrency}\` (${typeof concurrency})`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const result = [];
|
|
24
|
+
const errors = [];
|
|
25
|
+
const skippedIndexesMap = new Map();
|
|
26
|
+
let isRejected = false;
|
|
27
|
+
let isResolved = false;
|
|
28
|
+
let isIterableDone = false;
|
|
29
|
+
let resolvingCount = 0;
|
|
30
|
+
let currentIndex = 0;
|
|
31
|
+
const iterator = iterable[Symbol.iterator] === undefined ? iterable[Symbol.asyncIterator]() : iterable[Symbol.iterator]();
|
|
32
|
+
|
|
33
|
+
const signalListener = () => {
|
|
34
|
+
reject(signal.reason);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const cleanup = () => {
|
|
38
|
+
signal?.removeEventListener('abort', signalListener);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const resolve = value => {
|
|
42
|
+
resolve_(value);
|
|
43
|
+
cleanup();
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const reject = reason => {
|
|
47
|
+
isRejected = true;
|
|
48
|
+
isResolved = true;
|
|
49
|
+
reject_(reason);
|
|
50
|
+
cleanup();
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
if (signal) {
|
|
54
|
+
if (signal.aborted) {
|
|
55
|
+
reject(signal.reason);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
signal.addEventListener('abort', signalListener, {once: true});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const next = async () => {
|
|
62
|
+
if (isResolved) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const nextItem = await iterator.next();
|
|
67
|
+
|
|
68
|
+
const index = currentIndex;
|
|
69
|
+
currentIndex++;
|
|
70
|
+
|
|
71
|
+
// Note: `iterator.next()` can be called many times in parallel.
|
|
72
|
+
// This can cause multiple calls to this `next()` function to
|
|
73
|
+
// receive a `nextItem` with `done === true`.
|
|
74
|
+
// The shutdown logic that rejects/resolves must be protected
|
|
75
|
+
// so it runs only one time as the `skippedIndex` logic is
|
|
76
|
+
// non-idempotent.
|
|
77
|
+
if (nextItem.done) {
|
|
78
|
+
isIterableDone = true;
|
|
79
|
+
|
|
80
|
+
if (resolvingCount === 0 && !isResolved) {
|
|
81
|
+
if (!stopOnError && errors.length > 0) {
|
|
82
|
+
reject(new AggregateError(errors)); // eslint-disable-line unicorn/error-message
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
isResolved = true;
|
|
87
|
+
|
|
88
|
+
if (skippedIndexesMap.size === 0) {
|
|
89
|
+
resolve(result);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const pureResult = [];
|
|
94
|
+
|
|
95
|
+
// Support multiple `pMapSkip`'s.
|
|
96
|
+
for (const [index, value] of result.entries()) {
|
|
97
|
+
if (skippedIndexesMap.get(index) === pMapSkip) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
pureResult.push(value);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
resolve(pureResult);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
resolvingCount++;
|
|
111
|
+
|
|
112
|
+
// Intentionally detached
|
|
113
|
+
(async () => {
|
|
114
|
+
try {
|
|
115
|
+
const element = await nextItem.value;
|
|
116
|
+
|
|
117
|
+
if (isResolved) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const value = await mapper(element, index);
|
|
122
|
+
|
|
123
|
+
// Use Map to stage the index of the element.
|
|
124
|
+
if (value === pMapSkip) {
|
|
125
|
+
skippedIndexesMap.set(index, value);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
result[index] = value;
|
|
129
|
+
|
|
130
|
+
resolvingCount--;
|
|
131
|
+
await next();
|
|
132
|
+
} catch (error) {
|
|
133
|
+
if (stopOnError) {
|
|
134
|
+
reject(error);
|
|
135
|
+
} else {
|
|
136
|
+
errors.push(error);
|
|
137
|
+
resolvingCount--;
|
|
138
|
+
|
|
139
|
+
// In that case we can't really continue regardless of `stopOnError` state
|
|
140
|
+
// since an iterable is likely to continue throwing after it throws once.
|
|
141
|
+
// If we continue calling `next()` indefinitely we will likely end up
|
|
142
|
+
// in an infinite loop of failed iteration.
|
|
143
|
+
try {
|
|
144
|
+
await next();
|
|
145
|
+
} catch (error) {
|
|
146
|
+
reject(error);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
})();
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// Create the concurrent runners in a detached (non-awaited)
|
|
154
|
+
// promise. We need this so we can await the `next()` calls
|
|
155
|
+
// to stop creating runners before hitting the concurrency limit
|
|
156
|
+
// if the iterable has already been marked as done.
|
|
157
|
+
// NOTE: We *must* do this for async iterators otherwise we'll spin up
|
|
158
|
+
// infinite `next()` calls by default and never start the event loop.
|
|
159
|
+
(async () => {
|
|
160
|
+
for (let index = 0; index < concurrency; index++) {
|
|
161
|
+
try {
|
|
162
|
+
// eslint-disable-next-line no-await-in-loop
|
|
163
|
+
await next();
|
|
164
|
+
} catch (error) {
|
|
165
|
+
reject(error);
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (isIterableDone || isRejected) {
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
})();
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function pMapIterable(
|
|
178
|
+
iterable,
|
|
179
|
+
mapper,
|
|
180
|
+
{
|
|
181
|
+
concurrency = Number.POSITIVE_INFINITY,
|
|
182
|
+
backpressure = concurrency,
|
|
183
|
+
} = {},
|
|
184
|
+
) {
|
|
185
|
+
if (iterable[Symbol.iterator] === undefined && iterable[Symbol.asyncIterator] === undefined) {
|
|
186
|
+
throw new TypeError(`Expected \`input\` to be either an \`Iterable\` or \`AsyncIterable\`, got (${typeof iterable})`);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (typeof mapper !== 'function') {
|
|
190
|
+
throw new TypeError('Mapper function is required');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (!((Number.isSafeInteger(concurrency) && concurrency >= 1) || concurrency === Number.POSITIVE_INFINITY)) {
|
|
194
|
+
throw new TypeError(`Expected \`concurrency\` to be an integer from 1 and up or \`Infinity\`, got \`${concurrency}\` (${typeof concurrency})`);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (!((Number.isSafeInteger(backpressure) && backpressure >= concurrency) || backpressure === Number.POSITIVE_INFINITY)) {
|
|
198
|
+
throw new TypeError(`Expected \`backpressure\` to be an integer from \`concurrency\` (${concurrency}) and up or \`Infinity\`, got \`${backpressure}\` (${typeof backpressure})`);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
async * [Symbol.asyncIterator]() {
|
|
203
|
+
const iterator = iterable[Symbol.asyncIterator] === undefined ? iterable[Symbol.iterator]() : iterable[Symbol.asyncIterator]();
|
|
204
|
+
|
|
205
|
+
const promises = [];
|
|
206
|
+
let pendingPromisesCount = 0;
|
|
207
|
+
let isDone = false;
|
|
208
|
+
let index = 0;
|
|
209
|
+
|
|
210
|
+
function trySpawn() {
|
|
211
|
+
if (isDone || !(pendingPromisesCount < concurrency && promises.length < backpressure)) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
pendingPromisesCount++;
|
|
216
|
+
|
|
217
|
+
const promise = (async () => {
|
|
218
|
+
const {done, value} = await iterator.next();
|
|
219
|
+
|
|
220
|
+
if (done) {
|
|
221
|
+
pendingPromisesCount--;
|
|
222
|
+
return {done: true};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Spawn if still below concurrency and backpressure limit
|
|
226
|
+
trySpawn();
|
|
227
|
+
|
|
228
|
+
try {
|
|
229
|
+
const currentIndex = index++;
|
|
230
|
+
const returnValue = await mapper(await value, currentIndex);
|
|
231
|
+
|
|
232
|
+
pendingPromisesCount--;
|
|
233
|
+
|
|
234
|
+
if (returnValue === pMapSkip) {
|
|
235
|
+
const index = promises.indexOf(promise);
|
|
236
|
+
|
|
237
|
+
if (index > 0) {
|
|
238
|
+
promises.splice(index, 1);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Spawn if still below backpressure limit and just dropped below concurrency limit
|
|
243
|
+
trySpawn();
|
|
244
|
+
|
|
245
|
+
return {done: false, value: returnValue};
|
|
246
|
+
} catch (error) {
|
|
247
|
+
pendingPromisesCount--;
|
|
248
|
+
isDone = true;
|
|
249
|
+
return {error};
|
|
250
|
+
}
|
|
251
|
+
})();
|
|
252
|
+
|
|
253
|
+
promises.push(promise);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
trySpawn();
|
|
257
|
+
|
|
258
|
+
while (promises.length > 0) {
|
|
259
|
+
const {error, done, value} = await promises[0]; // eslint-disable-line no-await-in-loop
|
|
260
|
+
|
|
261
|
+
promises.shift();
|
|
262
|
+
|
|
263
|
+
if (error) {
|
|
264
|
+
throw error;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (done) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Spawn if just dropped below backpressure limit and below the concurrency limit
|
|
272
|
+
trySpawn();
|
|
273
|
+
|
|
274
|
+
if (value === pMapSkip) {
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
yield value;
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export const pMapSkip = Symbol('skip');
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "p-map",
|
|
3
|
+
"version": "7.0.5",
|
|
4
|
+
"description": "Map over promises concurrently",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "sindresorhus/p-map",
|
|
7
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "Sindre Sorhus",
|
|
10
|
+
"email": "sindresorhus@gmail.com",
|
|
11
|
+
"url": "https://sindresorhus.com"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"default": "./index.js"
|
|
17
|
+
},
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=18"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "xo && ava && tsd"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"index.js",
|
|
27
|
+
"index.d.ts"
|
|
28
|
+
],
|
|
29
|
+
"keywords": [
|
|
30
|
+
"promise",
|
|
31
|
+
"map",
|
|
32
|
+
"resolved",
|
|
33
|
+
"wait",
|
|
34
|
+
"collection",
|
|
35
|
+
"iterable",
|
|
36
|
+
"iterator",
|
|
37
|
+
"race",
|
|
38
|
+
"fulfilled",
|
|
39
|
+
"async",
|
|
40
|
+
"await",
|
|
41
|
+
"promises",
|
|
42
|
+
"concurrently",
|
|
43
|
+
"concurrency",
|
|
44
|
+
"parallel",
|
|
45
|
+
"bluebird"
|
|
46
|
+
],
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"ava": "^5.2.0",
|
|
49
|
+
"chalk": "^5.3.0",
|
|
50
|
+
"delay": "^6.0.0",
|
|
51
|
+
"in-range": "^3.0.0",
|
|
52
|
+
"random-int": "^3.0.0",
|
|
53
|
+
"time-span": "^5.1.0",
|
|
54
|
+
"tsd": "^0.29.0",
|
|
55
|
+
"xo": "^0.56.0"
|
|
56
|
+
}
|
|
57
|
+
}
|