@orpc/shared 2.0.0-beta.5 → 2.0.0-beta.51
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 +139 -102
- package/dist/args.d.ts +2 -0
- package/dist/args.js +5 -0
- package/dist/array.d.ts +5 -0
- package/dist/array.js +13 -0
- package/dist/bigint.d.ts +9 -0
- package/dist/bigint.js +15 -0
- package/dist/buffer.d.ts +18 -0
- package/dist/buffer.js +54 -0
- package/dist/compare.d.ts +1 -0
- package/dist/compare.js +26 -0
- package/dist/consts.d.ts +1 -0
- package/dist/consts.js +3 -0
- package/dist/error.d.ts +1 -0
- package/dist/error.js +5 -0
- package/dist/function.d.ts +12 -0
- package/dist/function.js +33 -0
- package/dist/http.d.ts +58 -0
- package/dist/http.js +147 -0
- package/dist/id.d.ts +8 -0
- package/dist/id.js +13 -0
- package/dist/index.d.ts +26 -306
- package/dist/index.js +23 -0
- package/dist/interceptor.d.ts +66 -0
- package/dist/interceptor.js +119 -0
- package/dist/iterator.d.ts +55 -0
- package/dist/iterator.js +141 -0
- package/dist/object.d.ts +62 -0
- package/dist/object.js +194 -0
- package/dist/plugin.d.ts +12 -0
- package/dist/plugin.js +45 -0
- package/dist/promise.d.ts +17 -0
- package/dist/promise.js +32 -0
- package/dist/proxy.d.ts +12 -0
- package/dist/proxy.js +25 -0
- package/dist/queue.d.ts +16 -0
- package/dist/queue.js +64 -0
- package/dist/regex.d.ts +9 -0
- package/dist/regex.js +11 -0
- package/dist/signal.d.ts +23 -0
- package/dist/signal.js +88 -0
- package/dist/stream.d.ts +47 -0
- package/dist/stream.js +129 -0
- package/dist/tracing.d.ts +73 -0
- package/dist/tracing.js +76 -0
- package/dist/types.d.ts +27 -0
- package/dist/value.d.ts +2 -0
- package/dist/value.js +6 -0
- package/package.json +22 -30
- package/dist/index.d.mts +0 -306
- package/dist/index.mjs +0 -892
package/dist/index.mjs
DELETED
|
@@ -1,892 +0,0 @@
|
|
|
1
|
-
import { AbortError, AsyncIteratorClass, isTypescriptObject, getOrBind } from '@standardserver/shared';
|
|
2
|
-
export { AbortError, AsyncIteratorClass, SequentialIdGenerator, getOrBind, isAsyncIteratorObject, isTypescriptObject, parseEmptyableJSON, sleep, stringifyJSON, toArray } from '@standardserver/shared';
|
|
3
|
-
|
|
4
|
-
function resolveMaybeOptionalOptions(rest) {
|
|
5
|
-
return rest[0] ?? {};
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
function splitInHalf(arr) {
|
|
9
|
-
const half = Math.ceil(arr.length / 2);
|
|
10
|
-
return [arr.slice(0, half), arr.slice(half)];
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
async function loadBytes(source) {
|
|
14
|
-
if (typeof source.bytes === "function") {
|
|
15
|
-
return source.bytes();
|
|
16
|
-
}
|
|
17
|
-
return new Uint8Array(await source.arrayBuffer());
|
|
18
|
-
}
|
|
19
|
-
async function toStringOrBytes(source) {
|
|
20
|
-
if (typeof source === "string") {
|
|
21
|
-
return source;
|
|
22
|
-
}
|
|
23
|
-
if (source instanceof ArrayBuffer) {
|
|
24
|
-
return new Uint8Array(source);
|
|
25
|
-
}
|
|
26
|
-
if (source instanceof Blob) {
|
|
27
|
-
return loadBytes(source);
|
|
28
|
-
}
|
|
29
|
-
if (Array.isArray(source)) {
|
|
30
|
-
return loadBytes(new Blob(source));
|
|
31
|
-
}
|
|
32
|
-
if (source instanceof Uint8Array) {
|
|
33
|
-
return source;
|
|
34
|
-
}
|
|
35
|
-
return new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function isDeepEqual(a, b) {
|
|
39
|
-
return isDeepEqualInternal(a, b, /* @__PURE__ */ new WeakMap());
|
|
40
|
-
}
|
|
41
|
-
function isDeepEqualInternal(a, b, visited) {
|
|
42
|
-
if (Object.is(a, b)) {
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
if (typeof a !== typeof b) {
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
if (a === null || typeof a !== "object") {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
if (b === null || typeof b !== "object") {
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
const isArray = Array.isArray(a);
|
|
55
|
-
if (isArray !== Array.isArray(b)) {
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
if (isArray && a.length !== b.length) {
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
const aRecord = a;
|
|
62
|
-
const bRecord = b;
|
|
63
|
-
const visitedMatches = visited.get(a);
|
|
64
|
-
if (visitedMatches?.has(b)) {
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
if (visitedMatches) {
|
|
68
|
-
visitedMatches.add(b);
|
|
69
|
-
} else {
|
|
70
|
-
visited.set(a, new WeakSet([b]));
|
|
71
|
-
}
|
|
72
|
-
const aKeys = Object.keys(aRecord).filter((k) => aRecord[k] !== void 0);
|
|
73
|
-
const bKeys = Object.keys(bRecord).filter((k) => bRecord[k] !== void 0);
|
|
74
|
-
if (aKeys.length !== bKeys.length) {
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
for (const key of aKeys) {
|
|
78
|
-
if (!Object.hasOwn(bRecord, key)) {
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
if (!isDeepEqualInternal(aRecord[key], bRecord[key], visited)) {
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return true;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const ORPC_NAME = "orpc";
|
|
89
|
-
|
|
90
|
-
function isAbortError(error) {
|
|
91
|
-
return error instanceof Error && error.name.includes("Abort");
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function once(fn) {
|
|
95
|
-
let cached;
|
|
96
|
-
return () => {
|
|
97
|
-
if (cached) {
|
|
98
|
-
return cached.result;
|
|
99
|
-
}
|
|
100
|
-
const result = fn();
|
|
101
|
-
cached = { result };
|
|
102
|
-
return result;
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
function defer(callback) {
|
|
106
|
-
if (typeof setTimeout === "function") {
|
|
107
|
-
setTimeout(callback, 0);
|
|
108
|
-
} else {
|
|
109
|
-
Promise.resolve().then(() => Promise.resolve().then(() => Promise.resolve().then(callback)));
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
function tryOrUndefined(fn) {
|
|
113
|
-
try {
|
|
114
|
-
return fn();
|
|
115
|
-
} catch {
|
|
116
|
-
return void 0;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function tryDecodeURIComponent(value) {
|
|
121
|
-
try {
|
|
122
|
-
return decodeURIComponent(value);
|
|
123
|
-
} catch {
|
|
124
|
-
return value;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function pathToHttpPath(path) {
|
|
129
|
-
return `/${path.map(encodeURIComponent).join("/")}`;
|
|
130
|
-
}
|
|
131
|
-
function normalizeHttpPath(path) {
|
|
132
|
-
const paths = path.split("/");
|
|
133
|
-
if (paths.at(0) === "") {
|
|
134
|
-
paths.shift();
|
|
135
|
-
}
|
|
136
|
-
return pathToHttpPath(paths.map(tryDecodeURIComponent));
|
|
137
|
-
}
|
|
138
|
-
function mergeHttpPath(a, b) {
|
|
139
|
-
return `${a.endsWith("/") ? a.slice(0, -1) : a}${b}`;
|
|
140
|
-
}
|
|
141
|
-
function matchesHttpPathPrefix(url, prefix) {
|
|
142
|
-
if (!url.startsWith(prefix)) {
|
|
143
|
-
return false;
|
|
144
|
-
}
|
|
145
|
-
const charAfterPrefix = url[prefix.length];
|
|
146
|
-
return charAfterPrefix === "/" || charAfterPrefix === "?" || charAfterPrefix === "#" || charAfterPrefix === void 0 || prefix[prefix.length - 1] === "/";
|
|
147
|
-
}
|
|
148
|
-
function matchesHttpPath(url, path) {
|
|
149
|
-
const pathWithoutEndSlash = path.endsWith("/") ? path.slice(0, path.length - 1) : path;
|
|
150
|
-
if (!url.startsWith(pathWithoutEndSlash)) {
|
|
151
|
-
return false;
|
|
152
|
-
}
|
|
153
|
-
let charAfterPrefix = url[pathWithoutEndSlash.length];
|
|
154
|
-
if (charAfterPrefix === "/") {
|
|
155
|
-
charAfterPrefix = url[pathWithoutEndSlash.length + 1];
|
|
156
|
-
}
|
|
157
|
-
return charAfterPrefix === void 0 || charAfterPrefix === "?" || charAfterPrefix === "#";
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function compareSequentialIds(a, b) {
|
|
161
|
-
if (a.length !== b.length) {
|
|
162
|
-
return a.length - b.length;
|
|
163
|
-
}
|
|
164
|
-
return a < b ? -1 : a > b ? 1 : 0;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function onStart(callback) {
|
|
168
|
-
return async (options, ...rest) => {
|
|
169
|
-
await callback(options, ...rest);
|
|
170
|
-
return await options.next();
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
function onSuccess(callback) {
|
|
174
|
-
return async (options, ...rest) => {
|
|
175
|
-
const result = await options.next();
|
|
176
|
-
await callback(result, options, ...rest);
|
|
177
|
-
return result;
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
function onError(callback) {
|
|
181
|
-
return async (options, ...rest) => {
|
|
182
|
-
try {
|
|
183
|
-
return await options.next();
|
|
184
|
-
} catch (error) {
|
|
185
|
-
await callback(error, options, ...rest);
|
|
186
|
-
throw error;
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
function onFinish(callback) {
|
|
191
|
-
let state;
|
|
192
|
-
return async (options, ...rest) => {
|
|
193
|
-
try {
|
|
194
|
-
const result = await options.next();
|
|
195
|
-
state = [null, result, true];
|
|
196
|
-
return result;
|
|
197
|
-
} catch (error) {
|
|
198
|
-
state = [error, void 0, false];
|
|
199
|
-
throw error;
|
|
200
|
-
} finally {
|
|
201
|
-
await callback(state, options, ...rest);
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
function intercept(interceptors, options, main) {
|
|
206
|
-
if (!interceptors?.length) {
|
|
207
|
-
return main(options);
|
|
208
|
-
}
|
|
209
|
-
const next = (options2, index) => {
|
|
210
|
-
const interceptor = interceptors[index];
|
|
211
|
-
if (!interceptor) {
|
|
212
|
-
return main(options2);
|
|
213
|
-
}
|
|
214
|
-
return interceptor({
|
|
215
|
-
...options2,
|
|
216
|
-
next: (newOptions = options2) => next(newOptions, index + 1)
|
|
217
|
-
});
|
|
218
|
-
};
|
|
219
|
-
return next(options, 0);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const SPAN_ERROR_STATUS = 2;
|
|
223
|
-
const OPENTELEMETRY_CONFIG_SYMBOL = Symbol.for("ORPC_OPENTELEMETRY_CONFIG");
|
|
224
|
-
function setOpenTelemetryConfig(config) {
|
|
225
|
-
globalThis[OPENTELEMETRY_CONFIG_SYMBOL] = config;
|
|
226
|
-
}
|
|
227
|
-
function getOpenTelemetryConfig() {
|
|
228
|
-
return globalThis[OPENTELEMETRY_CONFIG_SYMBOL];
|
|
229
|
-
}
|
|
230
|
-
function startSpan(options) {
|
|
231
|
-
const tracer = getOpenTelemetryConfig()?.tracer;
|
|
232
|
-
if (!tracer) {
|
|
233
|
-
return void 0;
|
|
234
|
-
}
|
|
235
|
-
const { name, context, ...spanOptions } = typeof options === "string" ? { name: options } : options;
|
|
236
|
-
return tracer.startSpan(name, spanOptions, context);
|
|
237
|
-
}
|
|
238
|
-
function recordSpanError(span, error) {
|
|
239
|
-
if (!span) {
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
const exception = toOtelException(error);
|
|
243
|
-
span.recordException(exception);
|
|
244
|
-
if (!isAbortError(error)) {
|
|
245
|
-
span.setStatus({
|
|
246
|
-
code: SPAN_ERROR_STATUS,
|
|
247
|
-
message: exception.message
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
function setSpanAttributeIfDefined(span, key, value) {
|
|
252
|
-
if (!span || value === void 0) {
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
span.setAttribute(key, value);
|
|
256
|
-
}
|
|
257
|
-
function toOtelException(error) {
|
|
258
|
-
if (error instanceof Error) {
|
|
259
|
-
const exception = {
|
|
260
|
-
message: error.message,
|
|
261
|
-
name: error.name,
|
|
262
|
-
stack: error.stack
|
|
263
|
-
};
|
|
264
|
-
if ("code" in error && (typeof error.code === "string" || typeof error.code === "number")) {
|
|
265
|
-
exception.code = error.code;
|
|
266
|
-
}
|
|
267
|
-
return exception;
|
|
268
|
-
}
|
|
269
|
-
return { message: String(error) };
|
|
270
|
-
}
|
|
271
|
-
function toSpanAttributeValue(data) {
|
|
272
|
-
if (data === void 0) {
|
|
273
|
-
return "undefined";
|
|
274
|
-
}
|
|
275
|
-
try {
|
|
276
|
-
return JSON.stringify(data, (_, value) => {
|
|
277
|
-
if (typeof value === "bigint") {
|
|
278
|
-
return value.toString();
|
|
279
|
-
}
|
|
280
|
-
if (value instanceof Map || value instanceof Set) {
|
|
281
|
-
return Array.from(value);
|
|
282
|
-
}
|
|
283
|
-
return value;
|
|
284
|
-
});
|
|
285
|
-
} catch {
|
|
286
|
-
return String(data);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
async function runWithSpan(options, fn) {
|
|
290
|
-
const tracer = getOpenTelemetryConfig()?.tracer;
|
|
291
|
-
if (!tracer) {
|
|
292
|
-
return fn();
|
|
293
|
-
}
|
|
294
|
-
if (typeof options === "string") {
|
|
295
|
-
options = { name: options };
|
|
296
|
-
}
|
|
297
|
-
const callback = async (span) => {
|
|
298
|
-
try {
|
|
299
|
-
return await fn(span);
|
|
300
|
-
} catch (e) {
|
|
301
|
-
recordSpanError(span, e);
|
|
302
|
-
throw e;
|
|
303
|
-
} finally {
|
|
304
|
-
span.end();
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
|
-
if (options.context) {
|
|
308
|
-
return tracer.startActiveSpan(options.name, options, options.context, callback);
|
|
309
|
-
} else {
|
|
310
|
-
return tracer.startActiveSpan(options.name, options, callback);
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
async function runInSpanContext(span, fn) {
|
|
314
|
-
const otelConfig = getOpenTelemetryConfig();
|
|
315
|
-
if (!span || !otelConfig) {
|
|
316
|
-
return fn();
|
|
317
|
-
}
|
|
318
|
-
const ctx = otelConfig.trace.setSpan(otelConfig.context.active(), span);
|
|
319
|
-
return otelConfig.context.with(ctx, fn);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
class AsyncIdQueue {
|
|
323
|
-
openIds = /* @__PURE__ */ new Set();
|
|
324
|
-
queues = /* @__PURE__ */ new Map();
|
|
325
|
-
waiters = /* @__PURE__ */ new Map();
|
|
326
|
-
get size() {
|
|
327
|
-
return this.openIds.size;
|
|
328
|
-
}
|
|
329
|
-
open(id) {
|
|
330
|
-
this.openIds.add(id);
|
|
331
|
-
}
|
|
332
|
-
isOpen(id) {
|
|
333
|
-
return this.openIds.has(id);
|
|
334
|
-
}
|
|
335
|
-
push(id, item) {
|
|
336
|
-
this.assertOpen(id);
|
|
337
|
-
const pending = this.waiters.get(id);
|
|
338
|
-
if (pending?.length) {
|
|
339
|
-
pending.shift()[0](item);
|
|
340
|
-
if (pending.length === 0) {
|
|
341
|
-
this.waiters.delete(id);
|
|
342
|
-
}
|
|
343
|
-
} else {
|
|
344
|
-
const items = this.queues.get(id);
|
|
345
|
-
if (items) {
|
|
346
|
-
items.push(item);
|
|
347
|
-
} else {
|
|
348
|
-
this.queues.set(id, [item]);
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
async pull(id) {
|
|
353
|
-
this.assertOpen(id);
|
|
354
|
-
const items = this.queues.get(id);
|
|
355
|
-
if (items?.length) {
|
|
356
|
-
const item = items.shift();
|
|
357
|
-
if (items.length === 0) {
|
|
358
|
-
this.queues.delete(id);
|
|
359
|
-
}
|
|
360
|
-
return item;
|
|
361
|
-
}
|
|
362
|
-
return new Promise((resolve, reject) => {
|
|
363
|
-
const waitingPulls = this.waiters.get(id);
|
|
364
|
-
const pending = [resolve, reject];
|
|
365
|
-
if (waitingPulls) {
|
|
366
|
-
waitingPulls.push(pending);
|
|
367
|
-
} else {
|
|
368
|
-
this.waiters.set(id, [pending]);
|
|
369
|
-
}
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
close({ id, reason } = {}) {
|
|
373
|
-
if (id === void 0) {
|
|
374
|
-
this.waiters.forEach((pendingPulls, id2) => {
|
|
375
|
-
const error2 = reason ?? new AbortError(`[AsyncIdQueue] Queue[${id2}] was closed or aborted while waiting for pulling.`);
|
|
376
|
-
pendingPulls.forEach(([, reject]) => reject(error2));
|
|
377
|
-
});
|
|
378
|
-
this.waiters.clear();
|
|
379
|
-
this.openIds.clear();
|
|
380
|
-
this.queues.clear();
|
|
381
|
-
return;
|
|
382
|
-
}
|
|
383
|
-
const error = reason ?? new AbortError(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`);
|
|
384
|
-
this.waiters.get(id)?.forEach(([, reject]) => reject(error));
|
|
385
|
-
this.waiters.delete(id);
|
|
386
|
-
this.openIds.delete(id);
|
|
387
|
-
this.queues.delete(id);
|
|
388
|
-
}
|
|
389
|
-
assertOpen(id) {
|
|
390
|
-
if (!this.isOpen(id)) {
|
|
391
|
-
throw new Error(`[AsyncIdQueue] Cannot access queue[${id}] because it is not open or aborted.`);
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
function wrapAsyncIterator(iterator, { runWith, mapResult, mapError, onError, onFinish }) {
|
|
397
|
-
runWith ??= (run) => run();
|
|
398
|
-
let isDone;
|
|
399
|
-
return new AsyncIteratorClass(async () => {
|
|
400
|
-
try {
|
|
401
|
-
let result;
|
|
402
|
-
try {
|
|
403
|
-
result = await runWith(() => iterator.next());
|
|
404
|
-
isDone = result.done;
|
|
405
|
-
} catch (error) {
|
|
406
|
-
isDone = true;
|
|
407
|
-
throw error;
|
|
408
|
-
}
|
|
409
|
-
return mapResult ? await mapResult(result) : result;
|
|
410
|
-
} catch (error) {
|
|
411
|
-
await onError?.(error);
|
|
412
|
-
throw mapError ? await mapError(error) : error;
|
|
413
|
-
}
|
|
414
|
-
}, async (_state) => {
|
|
415
|
-
try {
|
|
416
|
-
if (!isDone) {
|
|
417
|
-
try {
|
|
418
|
-
await runWith(async () => iterator.return?.());
|
|
419
|
-
} catch (error) {
|
|
420
|
-
await onError?.(error);
|
|
421
|
-
throw error;
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
} finally {
|
|
425
|
-
await onFinish?.();
|
|
426
|
-
}
|
|
427
|
-
});
|
|
428
|
-
}
|
|
429
|
-
function traceAsyncIterator(options, iterator) {
|
|
430
|
-
const getSpan = once(() => startSpan(options));
|
|
431
|
-
return wrapAsyncIterator(iterator, {
|
|
432
|
-
runWith: (run) => runInSpanContext(getSpan(), run),
|
|
433
|
-
mapResult(result) {
|
|
434
|
-
getSpan()?.addEvent(result.done ? "completed" : "yielded");
|
|
435
|
-
return result;
|
|
436
|
-
},
|
|
437
|
-
onError(error) {
|
|
438
|
-
recordSpanError(getSpan(), error);
|
|
439
|
-
},
|
|
440
|
-
onFinish() {
|
|
441
|
-
getSpan()?.end();
|
|
442
|
-
}
|
|
443
|
-
});
|
|
444
|
-
}
|
|
445
|
-
function replicateAsyncIterator(source, count) {
|
|
446
|
-
const queue = new AsyncIdQueue();
|
|
447
|
-
const ids = Array.from({ length: count }, (_, i) => i.toString());
|
|
448
|
-
let isSourceFinished = false;
|
|
449
|
-
const start = once(async () => {
|
|
450
|
-
try {
|
|
451
|
-
while (true) {
|
|
452
|
-
const item = await source.next();
|
|
453
|
-
ids.forEach((id) => {
|
|
454
|
-
if (queue.isOpen(id)) {
|
|
455
|
-
queue.push(id, { next: item });
|
|
456
|
-
}
|
|
457
|
-
});
|
|
458
|
-
if (item.done) {
|
|
459
|
-
break;
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
} catch (error) {
|
|
463
|
-
ids.forEach((id) => {
|
|
464
|
-
if (queue.isOpen(id)) {
|
|
465
|
-
queue.push(id, { error });
|
|
466
|
-
}
|
|
467
|
-
});
|
|
468
|
-
} finally {
|
|
469
|
-
isSourceFinished = true;
|
|
470
|
-
}
|
|
471
|
-
});
|
|
472
|
-
const replicated = ids.map((id) => {
|
|
473
|
-
queue.open(id);
|
|
474
|
-
return new AsyncIteratorClass(
|
|
475
|
-
async () => {
|
|
476
|
-
start();
|
|
477
|
-
const item = await queue.pull(id);
|
|
478
|
-
if (item.next) {
|
|
479
|
-
return item.next;
|
|
480
|
-
}
|
|
481
|
-
throw item.error;
|
|
482
|
-
},
|
|
483
|
-
async ({ kind, error }) => {
|
|
484
|
-
queue.close({ id, reason: error });
|
|
485
|
-
if (kind === "cancelled" && !queue.size && !isSourceFinished) {
|
|
486
|
-
isSourceFinished = true;
|
|
487
|
-
await source?.return?.();
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
);
|
|
491
|
-
});
|
|
492
|
-
return replicated;
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
496
|
-
if (check(payload)) {
|
|
497
|
-
maps.push(segments);
|
|
498
|
-
values.push(payload);
|
|
499
|
-
} else if (Array.isArray(payload)) {
|
|
500
|
-
payload.forEach((v, i) => {
|
|
501
|
-
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
502
|
-
});
|
|
503
|
-
} else if (isPlainObject(payload)) {
|
|
504
|
-
for (const key in payload) {
|
|
505
|
-
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
return { maps, values };
|
|
509
|
-
}
|
|
510
|
-
function getConstructor(value) {
|
|
511
|
-
if (!isTypescriptObject(value)) {
|
|
512
|
-
return null;
|
|
513
|
-
}
|
|
514
|
-
return Object.getPrototypeOf(value)?.constructor;
|
|
515
|
-
}
|
|
516
|
-
function isPlainObject(value) {
|
|
517
|
-
if (!value || typeof value !== "object") {
|
|
518
|
-
return false;
|
|
519
|
-
}
|
|
520
|
-
const proto = Object.getPrototypeOf(value);
|
|
521
|
-
return proto === Object.prototype || !proto || !proto.constructor;
|
|
522
|
-
}
|
|
523
|
-
function get(object, path) {
|
|
524
|
-
let current = object;
|
|
525
|
-
for (const key of path) {
|
|
526
|
-
if (!isTypescriptObject(current)) {
|
|
527
|
-
return void 0;
|
|
528
|
-
}
|
|
529
|
-
current = current[key];
|
|
530
|
-
}
|
|
531
|
-
return current;
|
|
532
|
-
}
|
|
533
|
-
function set(root, path, value) {
|
|
534
|
-
let current = root;
|
|
535
|
-
for (let i = 0; i < path.length - 1; i++) {
|
|
536
|
-
const key = path[i];
|
|
537
|
-
const next = current[key];
|
|
538
|
-
if (!isTypescriptObject(next)) {
|
|
539
|
-
current[key] = {};
|
|
540
|
-
}
|
|
541
|
-
current = current[key];
|
|
542
|
-
}
|
|
543
|
-
current[path.at(-1)] = value;
|
|
544
|
-
}
|
|
545
|
-
function omit(obj, keys) {
|
|
546
|
-
const result = { ...obj };
|
|
547
|
-
for (const key of keys) {
|
|
548
|
-
delete result[key];
|
|
549
|
-
}
|
|
550
|
-
return result;
|
|
551
|
-
}
|
|
552
|
-
function clone(value) {
|
|
553
|
-
if (Array.isArray(value)) {
|
|
554
|
-
return value.map(clone);
|
|
555
|
-
}
|
|
556
|
-
if (isPlainObject(value)) {
|
|
557
|
-
const result = {};
|
|
558
|
-
for (const key in value) {
|
|
559
|
-
result[key] = clone(value[key]);
|
|
560
|
-
}
|
|
561
|
-
for (const sym of Object.getOwnPropertySymbols(value)) {
|
|
562
|
-
result[sym] = clone(value[sym]);
|
|
563
|
-
}
|
|
564
|
-
return result;
|
|
565
|
-
}
|
|
566
|
-
return value;
|
|
567
|
-
}
|
|
568
|
-
function isPropertyKey(value) {
|
|
569
|
-
const type = typeof value;
|
|
570
|
-
return type === "string" || type === "number" || type === "symbol";
|
|
571
|
-
}
|
|
572
|
-
const NullProtoObj = /* @__PURE__ */ (() => {
|
|
573
|
-
const e = function() {
|
|
574
|
-
};
|
|
575
|
-
e.prototype = /* @__PURE__ */ Object.create(null);
|
|
576
|
-
Object.freeze(e.prototype);
|
|
577
|
-
return e;
|
|
578
|
-
})();
|
|
579
|
-
function bindMethods(obj) {
|
|
580
|
-
const methods = new NullProtoObj();
|
|
581
|
-
let current = obj;
|
|
582
|
-
while (current && current !== Object.prototype) {
|
|
583
|
-
for (const key of Object.getOwnPropertyNames(current)) {
|
|
584
|
-
if (key === "constructor" || key in methods) {
|
|
585
|
-
continue;
|
|
586
|
-
}
|
|
587
|
-
const val = obj[key];
|
|
588
|
-
if (typeof val === "function") {
|
|
589
|
-
methods[key] = val.bind(obj);
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
for (const sym of Object.getOwnPropertySymbols(current)) {
|
|
593
|
-
if (sym in methods) {
|
|
594
|
-
continue;
|
|
595
|
-
}
|
|
596
|
-
const val = obj[sym];
|
|
597
|
-
if (typeof val === "function") {
|
|
598
|
-
methods[sym] = val.bind(obj);
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
current = Object.getPrototypeOf(current);
|
|
602
|
-
}
|
|
603
|
-
return methods;
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
function sortPlugins(plugins) {
|
|
607
|
-
const pluginCount = plugins.length;
|
|
608
|
-
const pluginIdToIndices = /* @__PURE__ */ new Map();
|
|
609
|
-
for (let i = 0; i < pluginCount; i++) {
|
|
610
|
-
const plugin = plugins[i];
|
|
611
|
-
const indices = pluginIdToIndices.get(plugin.name);
|
|
612
|
-
if (indices === void 0) {
|
|
613
|
-
pluginIdToIndices.set(plugin.name, [i]);
|
|
614
|
-
} else {
|
|
615
|
-
indices.push(i);
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
const graph = Array.from(
|
|
619
|
-
{ length: pluginCount },
|
|
620
|
-
() => /* @__PURE__ */ new Set()
|
|
621
|
-
);
|
|
622
|
-
for (let i = 0; i < pluginCount; i++) {
|
|
623
|
-
const plugin = plugins[i];
|
|
624
|
-
const beforeList = plugin.before;
|
|
625
|
-
if (beforeList !== void 0) {
|
|
626
|
-
for (const beforeId of beforeList) {
|
|
627
|
-
const beforeIndices = pluginIdToIndices.get(beforeId);
|
|
628
|
-
if (beforeIndices === void 0)
|
|
629
|
-
continue;
|
|
630
|
-
for (const beforeIndex of beforeIndices) {
|
|
631
|
-
const beforeGraph = graph[beforeIndex];
|
|
632
|
-
if (beforeGraph !== void 0) {
|
|
633
|
-
beforeGraph.add(i);
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
const afterList = plugin.after;
|
|
639
|
-
if (afterList !== void 0) {
|
|
640
|
-
const currentGraph = graph[i];
|
|
641
|
-
if (currentGraph !== void 0) {
|
|
642
|
-
for (const afterId of afterList) {
|
|
643
|
-
const afterIndices = pluginIdToIndices.get(afterId);
|
|
644
|
-
if (afterIndices === void 0)
|
|
645
|
-
continue;
|
|
646
|
-
for (const afterIndex of afterIndices) {
|
|
647
|
-
currentGraph.add(afterIndex);
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
const sorted = [];
|
|
654
|
-
const visiting = /* @__PURE__ */ new Set();
|
|
655
|
-
const visited = /* @__PURE__ */ new Set();
|
|
656
|
-
function visit(index) {
|
|
657
|
-
if (visited.has(index))
|
|
658
|
-
return;
|
|
659
|
-
if (visiting.has(index)) {
|
|
660
|
-
const plugin2 = plugins[index];
|
|
661
|
-
const pluginId = plugin2 !== void 0 ? plugin2.name : "unknown";
|
|
662
|
-
throw new Error(`Circular dependency detected involving plugin "${pluginId}"`);
|
|
663
|
-
}
|
|
664
|
-
visiting.add(index);
|
|
665
|
-
const deps = graph[index];
|
|
666
|
-
if (deps !== void 0) {
|
|
667
|
-
for (const depIndex of deps) {
|
|
668
|
-
visit(depIndex);
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
visiting.delete(index);
|
|
672
|
-
visited.add(index);
|
|
673
|
-
const plugin = plugins[index];
|
|
674
|
-
if (plugin !== void 0) {
|
|
675
|
-
sorted.push(plugin);
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
for (let i = 0; i < pluginCount; i++) {
|
|
679
|
-
visit(i);
|
|
680
|
-
}
|
|
681
|
-
return sorted;
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
function promiseWithResolvers() {
|
|
685
|
-
const result = {};
|
|
686
|
-
result.promise = new Promise((resolve, reject) => {
|
|
687
|
-
result.resolve = resolve;
|
|
688
|
-
result.reject = reject;
|
|
689
|
-
});
|
|
690
|
-
return result;
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
function value(value2, ...args) {
|
|
694
|
-
if (typeof value2 === "function") {
|
|
695
|
-
return value2(...args);
|
|
696
|
-
}
|
|
697
|
-
return value2;
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
function override(target, partial) {
|
|
701
|
-
const proxy = new Proxy(typeof target === "function" ? partial : target, {
|
|
702
|
-
get(_, prop) {
|
|
703
|
-
const targetValue = prop in partial ? partial : value(target);
|
|
704
|
-
return getOrBind(targetValue, prop);
|
|
705
|
-
},
|
|
706
|
-
has(_, prop) {
|
|
707
|
-
return Reflect.has(partial, prop) || Reflect.has(value(target), prop);
|
|
708
|
-
}
|
|
709
|
-
});
|
|
710
|
-
return proxy;
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
function allAbortSignal(signals) {
|
|
714
|
-
const realSignals = signals.filter((signal) => signal !== void 0);
|
|
715
|
-
if (realSignals.length === 0 || realSignals.length !== signals.length) {
|
|
716
|
-
return void 0;
|
|
717
|
-
}
|
|
718
|
-
const controller = new AbortController();
|
|
719
|
-
const abortIfAllAborted = () => {
|
|
720
|
-
if (realSignals.every((signal) => signal.aborted)) {
|
|
721
|
-
controller.abort();
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
abortIfAllAborted();
|
|
725
|
-
for (const signal of realSignals) {
|
|
726
|
-
signal.addEventListener("abort", () => {
|
|
727
|
-
abortIfAllAborted();
|
|
728
|
-
}, {
|
|
729
|
-
once: true,
|
|
730
|
-
signal: controller.signal
|
|
731
|
-
});
|
|
732
|
-
}
|
|
733
|
-
return controller.signal;
|
|
734
|
-
}
|
|
735
|
-
async function runWithSignal(signal, fn) {
|
|
736
|
-
if (!signal) {
|
|
737
|
-
return fn();
|
|
738
|
-
}
|
|
739
|
-
signal.throwIfAborted();
|
|
740
|
-
const { promise, reject, resolve } = promiseWithResolvers();
|
|
741
|
-
let abortListener;
|
|
742
|
-
signal.addEventListener("abort", abortListener = () => {
|
|
743
|
-
reject(signal.reason);
|
|
744
|
-
abortListener = void 0;
|
|
745
|
-
});
|
|
746
|
-
try {
|
|
747
|
-
fn().then(resolve, reject);
|
|
748
|
-
return await promise;
|
|
749
|
-
} finally {
|
|
750
|
-
if (abortListener) {
|
|
751
|
-
signal.removeEventListener("abort", abortListener);
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
|
|
756
|
-
function replicateReadableStream(stream, count) {
|
|
757
|
-
if (count <= 0) {
|
|
758
|
-
return [];
|
|
759
|
-
}
|
|
760
|
-
const replicated = [];
|
|
761
|
-
let pending = stream;
|
|
762
|
-
for (let index = 0; index < count - 1; index++) {
|
|
763
|
-
const [replica, remainder] = pending.tee();
|
|
764
|
-
replicated.push(replica);
|
|
765
|
-
pending = remainder;
|
|
766
|
-
}
|
|
767
|
-
replicated.push(pending);
|
|
768
|
-
return replicated;
|
|
769
|
-
}
|
|
770
|
-
function wrapReadableStream(stream, { runWith, mapResult, mapError, onError, onFinish }) {
|
|
771
|
-
runWith ??= (run) => run();
|
|
772
|
-
const reader = once(() => stream.getReader());
|
|
773
|
-
const finish = once(async () => onFinish?.());
|
|
774
|
-
return new ReadableStream({
|
|
775
|
-
async pull(controller) {
|
|
776
|
-
let result;
|
|
777
|
-
try {
|
|
778
|
-
const readResult = await runWith(() => reader().read());
|
|
779
|
-
result = mapResult ? await mapResult(readResult) : readResult;
|
|
780
|
-
} catch (error) {
|
|
781
|
-
try {
|
|
782
|
-
await onError?.(error);
|
|
783
|
-
controller.error(mapError ? await mapError(error) : error);
|
|
784
|
-
} finally {
|
|
785
|
-
await finish();
|
|
786
|
-
}
|
|
787
|
-
return;
|
|
788
|
-
}
|
|
789
|
-
if (result.done) {
|
|
790
|
-
controller.close();
|
|
791
|
-
await finish();
|
|
792
|
-
} else {
|
|
793
|
-
controller.enqueue(result.value);
|
|
794
|
-
}
|
|
795
|
-
},
|
|
796
|
-
async cancel(reason) {
|
|
797
|
-
try {
|
|
798
|
-
try {
|
|
799
|
-
await runWith(() => reader().cancel(reason));
|
|
800
|
-
} catch (error) {
|
|
801
|
-
await onError?.(error);
|
|
802
|
-
throw error;
|
|
803
|
-
}
|
|
804
|
-
} finally {
|
|
805
|
-
await finish();
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
});
|
|
809
|
-
}
|
|
810
|
-
function traceReadableStream(options, stream) {
|
|
811
|
-
const getSpan = once(() => startSpan(options));
|
|
812
|
-
return wrapReadableStream(stream, {
|
|
813
|
-
runWith: (run) => runInSpanContext(getSpan(), run),
|
|
814
|
-
mapResult(result) {
|
|
815
|
-
getSpan()?.addEvent(result.done ? "closed" : "enqueued");
|
|
816
|
-
return result;
|
|
817
|
-
},
|
|
818
|
-
onError(error) {
|
|
819
|
-
recordSpanError(getSpan(), error);
|
|
820
|
-
},
|
|
821
|
-
onFinish() {
|
|
822
|
-
getSpan()?.end();
|
|
823
|
-
}
|
|
824
|
-
});
|
|
825
|
-
}
|
|
826
|
-
function streamToAsyncIteratorClass(stream, { signal } = {}) {
|
|
827
|
-
const reader = stream.getReader();
|
|
828
|
-
let cancelledBySignal = false;
|
|
829
|
-
return new AsyncIteratorClass(
|
|
830
|
-
async () => {
|
|
831
|
-
if (signal?.aborted) {
|
|
832
|
-
cancelledBySignal = true;
|
|
833
|
-
throw signal.reason;
|
|
834
|
-
}
|
|
835
|
-
if (!signal) {
|
|
836
|
-
return reader.read();
|
|
837
|
-
}
|
|
838
|
-
const { promise, reject } = promiseWithResolvers();
|
|
839
|
-
const onAbort = () => reject(signal.reason);
|
|
840
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
841
|
-
try {
|
|
842
|
-
return await Promise.race([
|
|
843
|
-
reader.read(),
|
|
844
|
-
promise.catch(async (reason) => {
|
|
845
|
-
cancelledBySignal = true;
|
|
846
|
-
throw reason;
|
|
847
|
-
})
|
|
848
|
-
]);
|
|
849
|
-
} finally {
|
|
850
|
-
signal.removeEventListener("abort", onAbort);
|
|
851
|
-
}
|
|
852
|
-
},
|
|
853
|
-
async ({ kind, error }) => {
|
|
854
|
-
if (kind === "cancelled" || kind === "error" && cancelledBySignal) {
|
|
855
|
-
await reader.cancel(error);
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
);
|
|
859
|
-
}
|
|
860
|
-
function asyncIteratorToStream(iterator) {
|
|
861
|
-
return new ReadableStream({
|
|
862
|
-
async pull(controller) {
|
|
863
|
-
const { done, value } = await iterator.next();
|
|
864
|
-
if (done) {
|
|
865
|
-
controller.close();
|
|
866
|
-
} else {
|
|
867
|
-
controller.enqueue(value);
|
|
868
|
-
}
|
|
869
|
-
},
|
|
870
|
-
async cancel() {
|
|
871
|
-
await iterator.return?.();
|
|
872
|
-
}
|
|
873
|
-
});
|
|
874
|
-
}
|
|
875
|
-
function asyncIteratorToUnproxiedDataStream(iterator) {
|
|
876
|
-
return new ReadableStream({
|
|
877
|
-
async pull(controller) {
|
|
878
|
-
const { done, value } = await iterator.next();
|
|
879
|
-
if (done) {
|
|
880
|
-
controller.close();
|
|
881
|
-
} else {
|
|
882
|
-
const unproxied = isPlainObject(value) ? { ...value } : Array.isArray(value) ? value.map((i) => i) : value;
|
|
883
|
-
controller.enqueue(unproxied);
|
|
884
|
-
}
|
|
885
|
-
},
|
|
886
|
-
async cancel() {
|
|
887
|
-
await iterator.return?.();
|
|
888
|
-
}
|
|
889
|
-
});
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
export { AsyncIdQueue, NullProtoObj, ORPC_NAME, allAbortSignal, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, bindMethods, clone, compareSequentialIds, defer, findDeepMatches, get, getConstructor, getOpenTelemetryConfig, intercept, isAbortError, isDeepEqual, isPlainObject, isPropertyKey, loadBytes, matchesHttpPath, matchesHttpPathPrefix, mergeHttpPath, normalizeHttpPath, omit, onError, onFinish, onStart, onSuccess, once, override, pathToHttpPath, promiseWithResolvers, recordSpanError, replicateAsyncIterator, replicateReadableStream, resolveMaybeOptionalOptions, runInSpanContext, runWithSignal, runWithSpan, set, setOpenTelemetryConfig, setSpanAttributeIfDefined, sortPlugins, splitInHalf, startSpan, streamToAsyncIteratorClass, toOtelException, toSpanAttributeValue, toStringOrBytes, traceAsyncIterator, traceReadableStream, tryDecodeURIComponent, tryOrUndefined, value, wrapAsyncIterator, wrapReadableStream };
|