@orpc/shared 0.0.0-next.a9d0401 → 0.0.0-next.a9dc7dc
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 +18 -20
- package/dist/index.d.mts +246 -21
- package/dist/index.d.ts +246 -21
- package/dist/index.mjs +484 -142
- package/package.json +17 -9
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { group, guard, mapEntries, mapValues, omit } from 'radash';
|
|
1
|
+
export { group, guard, mapEntries, mapValues, omit, retry, sleep } from 'radash';
|
|
2
2
|
|
|
3
3
|
function resolveMaybeOptionalOptions(rest) {
|
|
4
4
|
return rest[0] ?? {};
|
|
@@ -12,6 +12,24 @@ function splitInHalf(arr) {
|
|
|
12
12
|
return [arr.slice(0, half), arr.slice(half)];
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
function readAsBuffer(source) {
|
|
16
|
+
if (typeof source.bytes === "function") {
|
|
17
|
+
return source.bytes();
|
|
18
|
+
}
|
|
19
|
+
return source.arrayBuffer();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const ORPC_NAME = "orpc";
|
|
23
|
+
const ORPC_SHARED_PACKAGE_NAME = "@orpc/shared";
|
|
24
|
+
const ORPC_SHARED_PACKAGE_VERSION = "0.0.0-next.a9dc7dc";
|
|
25
|
+
|
|
26
|
+
class AbortError extends Error {
|
|
27
|
+
constructor(...rest) {
|
|
28
|
+
super(...rest);
|
|
29
|
+
this.name = "AbortError";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
15
33
|
function once(fn) {
|
|
16
34
|
let cached;
|
|
17
35
|
return () => {
|
|
@@ -33,83 +51,119 @@ function sequential(fn) {
|
|
|
33
51
|
};
|
|
34
52
|
}
|
|
35
53
|
function defer(callback) {
|
|
36
|
-
if (
|
|
37
|
-
|
|
54
|
+
if (typeof setTimeout === "function") {
|
|
55
|
+
setTimeout(callback, 0);
|
|
38
56
|
} else {
|
|
39
57
|
Promise.resolve().then(() => Promise.resolve().then(() => Promise.resolve().then(callback)));
|
|
40
58
|
}
|
|
41
59
|
}
|
|
42
60
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
this.nextId = 0;
|
|
48
|
-
return Number.MAX_SAFE_INTEGER;
|
|
49
|
-
}
|
|
50
|
-
return this.nextId++;
|
|
51
|
-
}
|
|
61
|
+
const SPAN_ERROR_STATUS = 2;
|
|
62
|
+
const GLOBAL_OTEL_CONFIG_KEY = `__${ORPC_SHARED_PACKAGE_NAME}@${ORPC_SHARED_PACKAGE_VERSION}/otel/config__`;
|
|
63
|
+
function setGlobalOtelConfig(config) {
|
|
64
|
+
globalThis[GLOBAL_OTEL_CONFIG_KEY] = config;
|
|
52
65
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return async (options, ...rest) => {
|
|
56
|
-
await callback(options, ...rest);
|
|
57
|
-
return await options.next();
|
|
58
|
-
};
|
|
66
|
+
function getGlobalOtelConfig() {
|
|
67
|
+
return globalThis[GLOBAL_OTEL_CONFIG_KEY];
|
|
59
68
|
}
|
|
60
|
-
function
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
await callback(result, options, ...rest);
|
|
64
|
-
return result;
|
|
65
|
-
};
|
|
69
|
+
function startSpan(name, options = {}, context) {
|
|
70
|
+
const tracer = getGlobalOtelConfig()?.tracer;
|
|
71
|
+
return tracer?.startSpan(name, options, context);
|
|
66
72
|
}
|
|
67
|
-
function
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
73
|
+
function setSpanError(span, error, options = {}) {
|
|
74
|
+
if (!span) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const exception = toOtelException(error);
|
|
78
|
+
span.recordException(exception);
|
|
79
|
+
if (!options.signal?.aborted || options.signal.reason !== error) {
|
|
80
|
+
span.setStatus({
|
|
81
|
+
code: SPAN_ERROR_STATUS,
|
|
82
|
+
message: exception.message
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function setSpanAttribute(span, key, value) {
|
|
87
|
+
if (!span || value === void 0) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
span.setAttribute(key, value);
|
|
91
|
+
}
|
|
92
|
+
function toOtelException(error) {
|
|
93
|
+
if (error instanceof Error) {
|
|
94
|
+
const exception = {
|
|
95
|
+
message: error.message,
|
|
96
|
+
name: error.name,
|
|
97
|
+
stack: error.stack
|
|
98
|
+
};
|
|
99
|
+
if ("code" in error && (typeof error.code === "string" || typeof error.code === "number")) {
|
|
100
|
+
exception.code = error.code;
|
|
74
101
|
}
|
|
75
|
-
|
|
102
|
+
return exception;
|
|
103
|
+
}
|
|
104
|
+
return { message: String(error) };
|
|
76
105
|
}
|
|
77
|
-
function
|
|
78
|
-
|
|
79
|
-
|
|
106
|
+
function toSpanAttributeValue(data) {
|
|
107
|
+
if (data === void 0) {
|
|
108
|
+
return "undefined";
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
return JSON.stringify(data, (_, value) => {
|
|
112
|
+
if (typeof value === "bigint") {
|
|
113
|
+
return value.toString();
|
|
114
|
+
}
|
|
115
|
+
if (value instanceof Map || value instanceof Set) {
|
|
116
|
+
return Array.from(value);
|
|
117
|
+
}
|
|
118
|
+
return value;
|
|
119
|
+
});
|
|
120
|
+
} catch {
|
|
121
|
+
return String(data);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async function runWithSpan({ name, context, ...options }, fn) {
|
|
125
|
+
const tracer = getGlobalOtelConfig()?.tracer;
|
|
126
|
+
if (!tracer) {
|
|
127
|
+
return fn();
|
|
128
|
+
}
|
|
129
|
+
const callback = async (span) => {
|
|
80
130
|
try {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
state = [error, void 0, false];
|
|
86
|
-
throw error;
|
|
131
|
+
return await fn(span);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
setSpanError(span, e, options);
|
|
134
|
+
throw e;
|
|
87
135
|
} finally {
|
|
88
|
-
|
|
136
|
+
span.end();
|
|
89
137
|
}
|
|
90
138
|
};
|
|
139
|
+
if (context) {
|
|
140
|
+
return tracer.startActiveSpan(name, options, context, callback);
|
|
141
|
+
} else {
|
|
142
|
+
return tracer.startActiveSpan(name, options, callback);
|
|
143
|
+
}
|
|
91
144
|
}
|
|
92
|
-
function
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
...options2,
|
|
100
|
-
next: (newOptions = options2) => next(newOptions, index + 1)
|
|
101
|
-
});
|
|
102
|
-
};
|
|
103
|
-
return next(options, 0);
|
|
145
|
+
async function runInSpanContext(span, fn) {
|
|
146
|
+
const otelConfig = getGlobalOtelConfig();
|
|
147
|
+
if (!span || !otelConfig) {
|
|
148
|
+
return fn();
|
|
149
|
+
}
|
|
150
|
+
const ctx = otelConfig.trace.setSpan(otelConfig.context.active(), span);
|
|
151
|
+
return otelConfig.context.with(ctx, fn);
|
|
104
152
|
}
|
|
105
153
|
|
|
106
154
|
class AsyncIdQueue {
|
|
107
155
|
openIds = /* @__PURE__ */ new Set();
|
|
108
|
-
|
|
109
|
-
|
|
156
|
+
queues = /* @__PURE__ */ new Map();
|
|
157
|
+
waiters = /* @__PURE__ */ new Map();
|
|
110
158
|
get length() {
|
|
111
159
|
return this.openIds.size;
|
|
112
160
|
}
|
|
161
|
+
get waiterIds() {
|
|
162
|
+
return Array.from(this.waiters.keys());
|
|
163
|
+
}
|
|
164
|
+
hasBufferedItems(id) {
|
|
165
|
+
return Boolean(this.queues.get(id)?.length);
|
|
166
|
+
}
|
|
113
167
|
open(id) {
|
|
114
168
|
this.openIds.add(id);
|
|
115
169
|
}
|
|
@@ -118,59 +172,57 @@ class AsyncIdQueue {
|
|
|
118
172
|
}
|
|
119
173
|
push(id, item) {
|
|
120
174
|
this.assertOpen(id);
|
|
121
|
-
const pending = this.
|
|
175
|
+
const pending = this.waiters.get(id);
|
|
122
176
|
if (pending?.length) {
|
|
123
177
|
pending.shift()[0](item);
|
|
124
178
|
if (pending.length === 0) {
|
|
125
|
-
this.
|
|
179
|
+
this.waiters.delete(id);
|
|
126
180
|
}
|
|
127
181
|
} else {
|
|
128
|
-
const items = this.
|
|
182
|
+
const items = this.queues.get(id);
|
|
129
183
|
if (items) {
|
|
130
184
|
items.push(item);
|
|
131
185
|
} else {
|
|
132
|
-
this.
|
|
186
|
+
this.queues.set(id, [item]);
|
|
133
187
|
}
|
|
134
188
|
}
|
|
135
189
|
}
|
|
136
190
|
async pull(id) {
|
|
137
191
|
this.assertOpen(id);
|
|
138
|
-
const items = this.
|
|
192
|
+
const items = this.queues.get(id);
|
|
139
193
|
if (items?.length) {
|
|
140
194
|
const item = items.shift();
|
|
141
195
|
if (items.length === 0) {
|
|
142
|
-
this.
|
|
196
|
+
this.queues.delete(id);
|
|
143
197
|
}
|
|
144
198
|
return item;
|
|
145
199
|
}
|
|
146
200
|
return new Promise((resolve, reject) => {
|
|
147
|
-
const waitingPulls = this.
|
|
201
|
+
const waitingPulls = this.waiters.get(id);
|
|
148
202
|
const pending = [resolve, reject];
|
|
149
203
|
if (waitingPulls) {
|
|
150
204
|
waitingPulls.push(pending);
|
|
151
205
|
} else {
|
|
152
|
-
this.
|
|
206
|
+
this.waiters.set(id, [pending]);
|
|
153
207
|
}
|
|
154
208
|
});
|
|
155
209
|
}
|
|
156
210
|
close({ id, reason } = {}) {
|
|
157
211
|
if (id === void 0) {
|
|
158
|
-
this.
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
});
|
|
212
|
+
this.waiters.forEach((pendingPulls, id2) => {
|
|
213
|
+
const error2 = reason ?? new AbortError(`[AsyncIdQueue] Queue[${id2}] was closed or aborted while waiting for pulling.`);
|
|
214
|
+
pendingPulls.forEach(([, reject]) => reject(error2));
|
|
162
215
|
});
|
|
163
|
-
this.
|
|
216
|
+
this.waiters.clear();
|
|
164
217
|
this.openIds.clear();
|
|
165
|
-
this.
|
|
218
|
+
this.queues.clear();
|
|
166
219
|
return;
|
|
167
220
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
this.pendingPulls.delete(id);
|
|
221
|
+
const error = reason ?? new AbortError(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`);
|
|
222
|
+
this.waiters.get(id)?.forEach(([, reject]) => reject(error));
|
|
223
|
+
this.waiters.delete(id);
|
|
172
224
|
this.openIds.delete(id);
|
|
173
|
-
this.
|
|
225
|
+
this.queues.delete(id);
|
|
174
226
|
}
|
|
175
227
|
assertOpen(id) {
|
|
176
228
|
if (!this.isOpen(id)) {
|
|
@@ -183,110 +235,292 @@ function isAsyncIteratorObject(maybe) {
|
|
|
183
235
|
if (!maybe || typeof maybe !== "object") {
|
|
184
236
|
return false;
|
|
185
237
|
}
|
|
186
|
-
return Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
|
238
|
+
return "next" in maybe && typeof maybe.next === "function" && Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
|
187
239
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
240
|
+
const fallbackAsyncDisposeSymbol = Symbol.for("asyncDispose");
|
|
241
|
+
const asyncDisposeSymbol = Symbol.asyncDispose ?? fallbackAsyncDisposeSymbol;
|
|
242
|
+
class AsyncIteratorClass {
|
|
243
|
+
#isDone = false;
|
|
244
|
+
#isExecuteComplete = false;
|
|
245
|
+
#cleanup;
|
|
246
|
+
#next;
|
|
247
|
+
constructor(next, cleanup) {
|
|
248
|
+
this.#cleanup = cleanup;
|
|
249
|
+
this.#next = sequential(async () => {
|
|
250
|
+
if (this.#isDone) {
|
|
194
251
|
return { done: true, value: void 0 };
|
|
195
252
|
}
|
|
196
253
|
try {
|
|
197
254
|
const result = await next();
|
|
198
255
|
if (result.done) {
|
|
199
|
-
isDone = true;
|
|
256
|
+
this.#isDone = true;
|
|
200
257
|
}
|
|
201
258
|
return result;
|
|
202
259
|
} catch (err) {
|
|
203
|
-
isDone = true;
|
|
260
|
+
this.#isDone = true;
|
|
204
261
|
throw err;
|
|
205
262
|
} finally {
|
|
206
|
-
if (isDone && !isExecuteComplete) {
|
|
207
|
-
isExecuteComplete = true;
|
|
208
|
-
await cleanup("next");
|
|
263
|
+
if (this.#isDone && !this.#isExecuteComplete) {
|
|
264
|
+
this.#isExecuteComplete = true;
|
|
265
|
+
await this.#cleanup("next");
|
|
209
266
|
}
|
|
210
267
|
}
|
|
211
|
-
})
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
isDone = true;
|
|
222
|
-
if (!isExecuteComplete) {
|
|
223
|
-
isExecuteComplete = true;
|
|
224
|
-
await cleanup("throw");
|
|
225
|
-
}
|
|
226
|
-
throw err;
|
|
227
|
-
},
|
|
228
|
-
/**
|
|
229
|
-
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
230
|
-
*/
|
|
231
|
-
async [Symbol.asyncDispose ?? Symbol.for("asyncDispose")]() {
|
|
232
|
-
isDone = true;
|
|
233
|
-
if (!isExecuteComplete) {
|
|
234
|
-
isExecuteComplete = true;
|
|
235
|
-
await cleanup("dispose");
|
|
236
|
-
}
|
|
237
|
-
},
|
|
238
|
-
[Symbol.asyncIterator]() {
|
|
239
|
-
return iterator;
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
next() {
|
|
271
|
+
return this.#next();
|
|
272
|
+
}
|
|
273
|
+
async return(value) {
|
|
274
|
+
this.#isDone = true;
|
|
275
|
+
if (!this.#isExecuteComplete) {
|
|
276
|
+
this.#isExecuteComplete = true;
|
|
277
|
+
await this.#cleanup("return");
|
|
240
278
|
}
|
|
241
|
-
|
|
242
|
-
|
|
279
|
+
return { done: true, value };
|
|
280
|
+
}
|
|
281
|
+
async throw(err) {
|
|
282
|
+
this.#isDone = true;
|
|
283
|
+
if (!this.#isExecuteComplete) {
|
|
284
|
+
this.#isExecuteComplete = true;
|
|
285
|
+
await this.#cleanup("throw");
|
|
286
|
+
}
|
|
287
|
+
throw err;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
291
|
+
*/
|
|
292
|
+
async [asyncDisposeSymbol]() {
|
|
293
|
+
this.#isDone = true;
|
|
294
|
+
if (!this.#isExecuteComplete) {
|
|
295
|
+
this.#isExecuteComplete = true;
|
|
296
|
+
await this.#cleanup("dispose");
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
[Symbol.asyncIterator]() {
|
|
300
|
+
return this;
|
|
301
|
+
}
|
|
243
302
|
}
|
|
244
303
|
function replicateAsyncIterator(source, count) {
|
|
245
304
|
const queue = new AsyncIdQueue();
|
|
246
|
-
const
|
|
247
|
-
let
|
|
305
|
+
const ids = Array.from({ length: count }, (_, i) => i.toString());
|
|
306
|
+
let isSourceFinished = false;
|
|
248
307
|
const start = once(async () => {
|
|
249
308
|
try {
|
|
250
309
|
while (true) {
|
|
251
310
|
const item = await source.next();
|
|
252
|
-
|
|
311
|
+
ids.forEach((id) => {
|
|
253
312
|
if (queue.isOpen(id)) {
|
|
254
|
-
queue.push(id, item);
|
|
313
|
+
queue.push(id, { next: item });
|
|
255
314
|
}
|
|
256
|
-
}
|
|
315
|
+
});
|
|
257
316
|
if (item.done) {
|
|
258
317
|
break;
|
|
259
318
|
}
|
|
260
319
|
}
|
|
261
|
-
} catch (
|
|
262
|
-
|
|
320
|
+
} catch (error) {
|
|
321
|
+
ids.forEach((id) => {
|
|
322
|
+
if (queue.isOpen(id)) {
|
|
323
|
+
queue.push(id, { error });
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
} finally {
|
|
327
|
+
isSourceFinished = true;
|
|
263
328
|
}
|
|
264
329
|
});
|
|
265
|
-
|
|
330
|
+
const replicated = ids.map((id) => {
|
|
266
331
|
queue.open(id);
|
|
267
|
-
|
|
268
|
-
() => {
|
|
332
|
+
return new AsyncIteratorClass(
|
|
333
|
+
async () => {
|
|
269
334
|
start();
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
});
|
|
335
|
+
const item = await queue.pull(id);
|
|
336
|
+
if (item.next) {
|
|
337
|
+
return item.next;
|
|
338
|
+
}
|
|
339
|
+
throw item.error;
|
|
278
340
|
},
|
|
279
341
|
async (reason) => {
|
|
280
342
|
queue.close({ id });
|
|
343
|
+
if (reason !== "next" && !queue.length && !isSourceFinished) {
|
|
344
|
+
isSourceFinished = true;
|
|
345
|
+
await source?.return?.();
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
);
|
|
349
|
+
});
|
|
350
|
+
return replicated;
|
|
351
|
+
}
|
|
352
|
+
function asyncIteratorWithSpan({ name, ...options }, iterator) {
|
|
353
|
+
let span;
|
|
354
|
+
return new AsyncIteratorClass(
|
|
355
|
+
async () => {
|
|
356
|
+
span ??= startSpan(name);
|
|
357
|
+
try {
|
|
358
|
+
const result = await runInSpanContext(span, () => iterator.next());
|
|
359
|
+
span?.addEvent(result.done ? "completed" : "yielded");
|
|
360
|
+
return result;
|
|
361
|
+
} catch (err) {
|
|
362
|
+
setSpanError(span, err, options);
|
|
363
|
+
throw err;
|
|
364
|
+
}
|
|
365
|
+
},
|
|
366
|
+
async (reason) => {
|
|
367
|
+
try {
|
|
281
368
|
if (reason !== "next") {
|
|
282
|
-
|
|
283
|
-
await source?.return?.();
|
|
284
|
-
}
|
|
369
|
+
await runInSpanContext(span, () => iterator.return?.());
|
|
285
370
|
}
|
|
371
|
+
} catch (err) {
|
|
372
|
+
setSpanError(span, err, options);
|
|
373
|
+
throw err;
|
|
374
|
+
} finally {
|
|
375
|
+
span?.end();
|
|
286
376
|
}
|
|
287
|
-
|
|
377
|
+
}
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
class EventPublisher {
|
|
382
|
+
#listenersMap = /* @__PURE__ */ new Map();
|
|
383
|
+
#maxBufferedEvents;
|
|
384
|
+
constructor(options = {}) {
|
|
385
|
+
this.#maxBufferedEvents = options.maxBufferedEvents ?? 100;
|
|
386
|
+
}
|
|
387
|
+
get size() {
|
|
388
|
+
return this.#listenersMap.size;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Emits an event and delivers the payload to all subscribed listeners.
|
|
392
|
+
*/
|
|
393
|
+
publish(event, payload) {
|
|
394
|
+
const listeners = this.#listenersMap.get(event);
|
|
395
|
+
if (!listeners) {
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
for (const listener of listeners) {
|
|
399
|
+
listener(payload);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
subscribe(event, listenerOrOptions) {
|
|
403
|
+
if (typeof listenerOrOptions === "function") {
|
|
404
|
+
let listeners = this.#listenersMap.get(event);
|
|
405
|
+
if (!listeners) {
|
|
406
|
+
this.#listenersMap.set(event, listeners = []);
|
|
407
|
+
}
|
|
408
|
+
listeners.push(listenerOrOptions);
|
|
409
|
+
return once(() => {
|
|
410
|
+
listeners.splice(listeners.indexOf(listenerOrOptions), 1);
|
|
411
|
+
if (listeners.length === 0) {
|
|
412
|
+
this.#listenersMap.delete(event);
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
const signal = listenerOrOptions?.signal;
|
|
417
|
+
const maxBufferedEvents = listenerOrOptions?.maxBufferedEvents ?? this.#maxBufferedEvents;
|
|
418
|
+
signal?.throwIfAborted();
|
|
419
|
+
const bufferedEvents = [];
|
|
420
|
+
const pullResolvers = [];
|
|
421
|
+
const unsubscribe = this.subscribe(event, (payload) => {
|
|
422
|
+
const resolver = pullResolvers.shift();
|
|
423
|
+
if (resolver) {
|
|
424
|
+
resolver[0]({ done: false, value: payload });
|
|
425
|
+
} else {
|
|
426
|
+
bufferedEvents.push(payload);
|
|
427
|
+
if (bufferedEvents.length > maxBufferedEvents) {
|
|
428
|
+
bufferedEvents.shift();
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
const abortListener = (event2) => {
|
|
433
|
+
unsubscribe();
|
|
434
|
+
pullResolvers.forEach((resolver) => resolver[1](event2.target.reason));
|
|
435
|
+
pullResolvers.length = 0;
|
|
436
|
+
bufferedEvents.length = 0;
|
|
437
|
+
};
|
|
438
|
+
signal?.addEventListener("abort", abortListener, { once: true });
|
|
439
|
+
return new AsyncIteratorClass(async () => {
|
|
440
|
+
if (signal?.aborted) {
|
|
441
|
+
throw signal.reason;
|
|
442
|
+
}
|
|
443
|
+
if (bufferedEvents.length > 0) {
|
|
444
|
+
return { done: false, value: bufferedEvents.shift() };
|
|
445
|
+
}
|
|
446
|
+
return new Promise((resolve, reject) => {
|
|
447
|
+
pullResolvers.push([resolve, reject]);
|
|
448
|
+
});
|
|
449
|
+
}, async () => {
|
|
450
|
+
unsubscribe();
|
|
451
|
+
signal?.removeEventListener("abort", abortListener);
|
|
452
|
+
pullResolvers.forEach((resolver) => resolver[0]({ done: true, value: void 0 }));
|
|
453
|
+
pullResolvers.length = 0;
|
|
454
|
+
bufferedEvents.length = 0;
|
|
455
|
+
});
|
|
288
456
|
}
|
|
289
|
-
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
class SequentialIdGenerator {
|
|
460
|
+
index = BigInt(1);
|
|
461
|
+
generate() {
|
|
462
|
+
const id = this.index.toString(36);
|
|
463
|
+
this.index++;
|
|
464
|
+
return id;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
function compareSequentialIds(a, b) {
|
|
468
|
+
if (a.length !== b.length) {
|
|
469
|
+
return a.length - b.length;
|
|
470
|
+
}
|
|
471
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
function onStart(callback) {
|
|
475
|
+
return async (options, ...rest) => {
|
|
476
|
+
await callback(options, ...rest);
|
|
477
|
+
return await options.next();
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
function onSuccess(callback) {
|
|
481
|
+
return async (options, ...rest) => {
|
|
482
|
+
const result = await options.next();
|
|
483
|
+
await callback(result, options, ...rest);
|
|
484
|
+
return result;
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
function onError(callback) {
|
|
488
|
+
return async (options, ...rest) => {
|
|
489
|
+
try {
|
|
490
|
+
return await options.next();
|
|
491
|
+
} catch (error) {
|
|
492
|
+
await callback(error, options, ...rest);
|
|
493
|
+
throw error;
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
function onFinish(callback) {
|
|
498
|
+
let state;
|
|
499
|
+
return async (options, ...rest) => {
|
|
500
|
+
try {
|
|
501
|
+
const result = await options.next();
|
|
502
|
+
state = [null, result, true];
|
|
503
|
+
return result;
|
|
504
|
+
} catch (error) {
|
|
505
|
+
state = [error, void 0, false];
|
|
506
|
+
throw error;
|
|
507
|
+
} finally {
|
|
508
|
+
await callback(state, options, ...rest);
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
function intercept(interceptors, options, main) {
|
|
513
|
+
const next = (options2, index) => {
|
|
514
|
+
const interceptor = interceptors[index];
|
|
515
|
+
if (!interceptor) {
|
|
516
|
+
return main(options2);
|
|
517
|
+
}
|
|
518
|
+
return interceptor({
|
|
519
|
+
...options2,
|
|
520
|
+
next: (newOptions = options2) => next(newOptions, index + 1)
|
|
521
|
+
});
|
|
522
|
+
};
|
|
523
|
+
return next(options, 0);
|
|
290
524
|
}
|
|
291
525
|
|
|
292
526
|
function parseEmptyableJSON(text) {
|
|
@@ -314,6 +548,12 @@ function findDeepMatches(check, payload, segments = [], maps = [], values = [])
|
|
|
314
548
|
}
|
|
315
549
|
return { maps, values };
|
|
316
550
|
}
|
|
551
|
+
function getConstructor(value) {
|
|
552
|
+
if (!isTypescriptObject(value)) {
|
|
553
|
+
return null;
|
|
554
|
+
}
|
|
555
|
+
return Object.getPrototypeOf(value)?.constructor;
|
|
556
|
+
}
|
|
317
557
|
function isObject(value) {
|
|
318
558
|
if (!value || typeof value !== "object") {
|
|
319
559
|
return false;
|
|
@@ -333,6 +573,9 @@ function clone(value) {
|
|
|
333
573
|
for (const key in value) {
|
|
334
574
|
result[key] = clone(value[key]);
|
|
335
575
|
}
|
|
576
|
+
for (const sym of Object.getOwnPropertySymbols(value)) {
|
|
577
|
+
result[sym] = clone(value[sym]);
|
|
578
|
+
}
|
|
336
579
|
return result;
|
|
337
580
|
}
|
|
338
581
|
return value;
|
|
@@ -365,5 +608,104 @@ function value(value2, ...args) {
|
|
|
365
608
|
}
|
|
366
609
|
return value2;
|
|
367
610
|
}
|
|
611
|
+
function fallback(value2, fallback2) {
|
|
612
|
+
return value2 === void 0 ? fallback2 : value2;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
function preventNativeAwait(target) {
|
|
616
|
+
return new Proxy(target, {
|
|
617
|
+
get(target2, prop, receiver) {
|
|
618
|
+
const value2 = Reflect.get(target2, prop, receiver);
|
|
619
|
+
if (prop !== "then" || typeof value2 !== "function") {
|
|
620
|
+
return value2;
|
|
621
|
+
}
|
|
622
|
+
return new Proxy(value2, {
|
|
623
|
+
apply(targetFn, thisArg, args) {
|
|
624
|
+
if (args.length !== 2 || args.some((arg) => !isNativeFunction(arg))) {
|
|
625
|
+
return Reflect.apply(targetFn, thisArg, args);
|
|
626
|
+
}
|
|
627
|
+
let shouldOmit = true;
|
|
628
|
+
args[0].call(thisArg, preventNativeAwait(new Proxy(target2, {
|
|
629
|
+
get: (target3, prop2, receiver2) => {
|
|
630
|
+
if (shouldOmit && prop2 === "then") {
|
|
631
|
+
shouldOmit = false;
|
|
632
|
+
return void 0;
|
|
633
|
+
}
|
|
634
|
+
return Reflect.get(target3, prop2, receiver2);
|
|
635
|
+
}
|
|
636
|
+
})));
|
|
637
|
+
}
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
const NATIVE_FUNCTION_REGEX = /^\s*function\s*\(\)\s*\{\s*\[native code\]\s*\}\s*$/;
|
|
643
|
+
function isNativeFunction(fn) {
|
|
644
|
+
return typeof fn === "function" && NATIVE_FUNCTION_REGEX.test(fn.toString());
|
|
645
|
+
}
|
|
646
|
+
function overlayProxy(target, partial) {
|
|
647
|
+
const proxy = new Proxy(typeof target === "function" ? partial : target, {
|
|
648
|
+
get(_, prop) {
|
|
649
|
+
const targetValue = prop in partial ? partial : value(target);
|
|
650
|
+
const v = Reflect.get(targetValue, prop);
|
|
651
|
+
return typeof v === "function" ? v.bind(targetValue) : v;
|
|
652
|
+
},
|
|
653
|
+
has(_, prop) {
|
|
654
|
+
return Reflect.has(partial, prop) || Reflect.has(value(target), prop);
|
|
655
|
+
}
|
|
656
|
+
});
|
|
657
|
+
return proxy;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function streamToAsyncIteratorClass(stream) {
|
|
661
|
+
const reader = stream.getReader();
|
|
662
|
+
return new AsyncIteratorClass(
|
|
663
|
+
async () => {
|
|
664
|
+
return reader.read();
|
|
665
|
+
},
|
|
666
|
+
async () => {
|
|
667
|
+
await reader.cancel();
|
|
668
|
+
}
|
|
669
|
+
);
|
|
670
|
+
}
|
|
671
|
+
function asyncIteratorToStream(iterator) {
|
|
672
|
+
return new ReadableStream({
|
|
673
|
+
async pull(controller) {
|
|
674
|
+
const { done, value } = await iterator.next();
|
|
675
|
+
if (done) {
|
|
676
|
+
controller.close();
|
|
677
|
+
} else {
|
|
678
|
+
controller.enqueue(value);
|
|
679
|
+
}
|
|
680
|
+
},
|
|
681
|
+
async cancel() {
|
|
682
|
+
await iterator.return?.();
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
function asyncIteratorToUnproxiedDataStream(iterator) {
|
|
687
|
+
return new ReadableStream({
|
|
688
|
+
async pull(controller) {
|
|
689
|
+
const { done, value } = await iterator.next();
|
|
690
|
+
if (done) {
|
|
691
|
+
controller.close();
|
|
692
|
+
} else {
|
|
693
|
+
const unproxied = isObject(value) ? { ...value } : Array.isArray(value) ? value.map((i) => i) : value;
|
|
694
|
+
controller.enqueue(unproxied);
|
|
695
|
+
}
|
|
696
|
+
},
|
|
697
|
+
async cancel() {
|
|
698
|
+
await iterator.return?.();
|
|
699
|
+
}
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
function tryDecodeURIComponent(value) {
|
|
704
|
+
try {
|
|
705
|
+
return decodeURIComponent(value);
|
|
706
|
+
} catch {
|
|
707
|
+
return value;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
368
710
|
|
|
369
|
-
export { AsyncIdQueue, NullProtoObj, SequentialIdGenerator, clone,
|
|
711
|
+
export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, asyncIteratorWithSpan, clone, compareSequentialIds, defer, fallback, findDeepMatches, get, getConstructor, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, overlayProxy, parseEmptyableJSON, preventNativeAwait, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
|