@orpc/shared 0.0.0-next.f16d90e → 0.0.0-next.f21e305
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 +8 -13
- package/dist/index.d.mts +214 -18
- package/dist/index.d.ts +214 -18
- package/dist/index.mjs +446 -138
- package/package.json +13 -4
package/dist/index.mjs
CHANGED
|
@@ -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.f21e305";
|
|
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 () => {
|
|
@@ -32,77 +50,120 @@ function sequential(fn) {
|
|
|
32
50
|
});
|
|
33
51
|
};
|
|
34
52
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
this.nextId = 0;
|
|
41
|
-
return Number.MAX_SAFE_INTEGER;
|
|
42
|
-
}
|
|
43
|
-
return this.nextId++;
|
|
53
|
+
function defer(callback) {
|
|
54
|
+
if (typeof setTimeout === "function") {
|
|
55
|
+
setTimeout(callback, 0);
|
|
56
|
+
} else {
|
|
57
|
+
Promise.resolve().then(() => Promise.resolve().then(() => Promise.resolve().then(callback)));
|
|
44
58
|
}
|
|
45
59
|
}
|
|
46
60
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
function
|
|
54
|
-
return
|
|
55
|
-
const result = await options.next();
|
|
56
|
-
await callback(result, options, ...rest);
|
|
57
|
-
return result;
|
|
58
|
-
};
|
|
66
|
+
function getGlobalOtelConfig() {
|
|
67
|
+
return globalThis[GLOBAL_OTEL_CONFIG_KEY];
|
|
59
68
|
}
|
|
60
|
-
function
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
69
|
+
function startSpan(name, options = {}, context) {
|
|
70
|
+
const tracer = getGlobalOtelConfig()?.tracer;
|
|
71
|
+
return tracer?.startSpan(name, options, context);
|
|
72
|
+
}
|
|
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;
|
|
67
101
|
}
|
|
68
|
-
|
|
102
|
+
return exception;
|
|
103
|
+
}
|
|
104
|
+
return { message: String(error) };
|
|
69
105
|
}
|
|
70
|
-
function
|
|
71
|
-
|
|
72
|
-
|
|
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) => {
|
|
73
130
|
try {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
state = [error, void 0, false];
|
|
79
|
-
throw error;
|
|
131
|
+
return await fn(span);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
setSpanError(span, e, options);
|
|
134
|
+
throw e;
|
|
80
135
|
} finally {
|
|
81
|
-
|
|
136
|
+
span.end();
|
|
82
137
|
}
|
|
83
138
|
};
|
|
139
|
+
if (context) {
|
|
140
|
+
return tracer.startActiveSpan(name, options, context, callback);
|
|
141
|
+
} else {
|
|
142
|
+
return tracer.startActiveSpan(name, options, callback);
|
|
143
|
+
}
|
|
84
144
|
}
|
|
85
|
-
function
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
...options2,
|
|
93
|
-
next: (newOptions = options2) => next(newOptions, index + 1)
|
|
94
|
-
});
|
|
95
|
-
};
|
|
96
|
-
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);
|
|
97
152
|
}
|
|
98
153
|
|
|
99
154
|
class AsyncIdQueue {
|
|
100
155
|
openIds = /* @__PURE__ */ new Set();
|
|
101
|
-
|
|
102
|
-
|
|
156
|
+
queues = /* @__PURE__ */ new Map();
|
|
157
|
+
waiters = /* @__PURE__ */ new Map();
|
|
103
158
|
get length() {
|
|
104
159
|
return this.openIds.size;
|
|
105
160
|
}
|
|
161
|
+
get waiterIds() {
|
|
162
|
+
return Array.from(this.waiters.keys());
|
|
163
|
+
}
|
|
164
|
+
hasBufferedItems(id) {
|
|
165
|
+
return Boolean(this.queues.get(id)?.length);
|
|
166
|
+
}
|
|
106
167
|
open(id) {
|
|
107
168
|
this.openIds.add(id);
|
|
108
169
|
}
|
|
@@ -111,59 +172,57 @@ class AsyncIdQueue {
|
|
|
111
172
|
}
|
|
112
173
|
push(id, item) {
|
|
113
174
|
this.assertOpen(id);
|
|
114
|
-
const pending = this.
|
|
175
|
+
const pending = this.waiters.get(id);
|
|
115
176
|
if (pending?.length) {
|
|
116
177
|
pending.shift()[0](item);
|
|
117
178
|
if (pending.length === 0) {
|
|
118
|
-
this.
|
|
179
|
+
this.waiters.delete(id);
|
|
119
180
|
}
|
|
120
181
|
} else {
|
|
121
|
-
const items = this.
|
|
182
|
+
const items = this.queues.get(id);
|
|
122
183
|
if (items) {
|
|
123
184
|
items.push(item);
|
|
124
185
|
} else {
|
|
125
|
-
this.
|
|
186
|
+
this.queues.set(id, [item]);
|
|
126
187
|
}
|
|
127
188
|
}
|
|
128
189
|
}
|
|
129
190
|
async pull(id) {
|
|
130
191
|
this.assertOpen(id);
|
|
131
|
-
const items = this.
|
|
192
|
+
const items = this.queues.get(id);
|
|
132
193
|
if (items?.length) {
|
|
133
194
|
const item = items.shift();
|
|
134
195
|
if (items.length === 0) {
|
|
135
|
-
this.
|
|
196
|
+
this.queues.delete(id);
|
|
136
197
|
}
|
|
137
198
|
return item;
|
|
138
199
|
}
|
|
139
200
|
return new Promise((resolve, reject) => {
|
|
140
|
-
const waitingPulls = this.
|
|
201
|
+
const waitingPulls = this.waiters.get(id);
|
|
141
202
|
const pending = [resolve, reject];
|
|
142
203
|
if (waitingPulls) {
|
|
143
204
|
waitingPulls.push(pending);
|
|
144
205
|
} else {
|
|
145
|
-
this.
|
|
206
|
+
this.waiters.set(id, [pending]);
|
|
146
207
|
}
|
|
147
208
|
});
|
|
148
209
|
}
|
|
149
210
|
close({ id, reason } = {}) {
|
|
150
211
|
if (id === void 0) {
|
|
151
|
-
this.
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
});
|
|
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));
|
|
155
215
|
});
|
|
156
|
-
this.
|
|
216
|
+
this.waiters.clear();
|
|
157
217
|
this.openIds.clear();
|
|
158
|
-
this.
|
|
218
|
+
this.queues.clear();
|
|
159
219
|
return;
|
|
160
220
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
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);
|
|
165
224
|
this.openIds.delete(id);
|
|
166
|
-
this.
|
|
225
|
+
this.queues.delete(id);
|
|
167
226
|
}
|
|
168
227
|
assertOpen(id) {
|
|
169
228
|
if (!this.isOpen(id)) {
|
|
@@ -176,110 +235,286 @@ function isAsyncIteratorObject(maybe) {
|
|
|
176
235
|
if (!maybe || typeof maybe !== "object") {
|
|
177
236
|
return false;
|
|
178
237
|
}
|
|
179
|
-
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";
|
|
180
239
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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) {
|
|
187
251
|
return { done: true, value: void 0 };
|
|
188
252
|
}
|
|
189
253
|
try {
|
|
190
254
|
const result = await next();
|
|
191
255
|
if (result.done) {
|
|
192
|
-
isDone = true;
|
|
256
|
+
this.#isDone = true;
|
|
193
257
|
}
|
|
194
258
|
return result;
|
|
195
259
|
} catch (err) {
|
|
196
|
-
isDone = true;
|
|
260
|
+
this.#isDone = true;
|
|
197
261
|
throw err;
|
|
198
262
|
} finally {
|
|
199
|
-
if (isDone && !isExecuteComplete) {
|
|
200
|
-
isExecuteComplete = true;
|
|
201
|
-
await cleanup("next");
|
|
263
|
+
if (this.#isDone && !this.#isExecuteComplete) {
|
|
264
|
+
this.#isExecuteComplete = true;
|
|
265
|
+
await this.#cleanup("next");
|
|
202
266
|
}
|
|
203
267
|
}
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
isDone = true;
|
|
215
|
-
if (!isExecuteComplete) {
|
|
216
|
-
isExecuteComplete = true;
|
|
217
|
-
await cleanup("throw");
|
|
218
|
-
}
|
|
219
|
-
throw err;
|
|
220
|
-
},
|
|
221
|
-
/**
|
|
222
|
-
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
223
|
-
*/
|
|
224
|
-
async [Symbol.asyncDispose ?? Symbol.for("asyncDispose")]() {
|
|
225
|
-
isDone = true;
|
|
226
|
-
if (!isExecuteComplete) {
|
|
227
|
-
isExecuteComplete = true;
|
|
228
|
-
await cleanup("dispose");
|
|
229
|
-
}
|
|
230
|
-
},
|
|
231
|
-
[Symbol.asyncIterator]() {
|
|
232
|
-
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");
|
|
233
278
|
}
|
|
234
|
-
|
|
235
|
-
|
|
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
|
+
}
|
|
236
302
|
}
|
|
237
303
|
function replicateAsyncIterator(source, count) {
|
|
238
304
|
const queue = new AsyncIdQueue();
|
|
239
|
-
const
|
|
240
|
-
let
|
|
305
|
+
const ids = Array.from({ length: count }, (_, i) => i.toString());
|
|
306
|
+
let isSourceFinished = false;
|
|
241
307
|
const start = once(async () => {
|
|
242
308
|
try {
|
|
243
309
|
while (true) {
|
|
244
310
|
const item = await source.next();
|
|
245
|
-
|
|
311
|
+
ids.forEach((id) => {
|
|
246
312
|
if (queue.isOpen(id)) {
|
|
247
|
-
queue.push(id, item);
|
|
313
|
+
queue.push(id, { next: item });
|
|
248
314
|
}
|
|
249
|
-
}
|
|
315
|
+
});
|
|
250
316
|
if (item.done) {
|
|
251
317
|
break;
|
|
252
318
|
}
|
|
253
319
|
}
|
|
254
|
-
} catch (
|
|
255
|
-
|
|
320
|
+
} catch (error) {
|
|
321
|
+
ids.forEach((id) => {
|
|
322
|
+
if (queue.isOpen(id)) {
|
|
323
|
+
queue.push(id, { error });
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
} finally {
|
|
327
|
+
isSourceFinished = true;
|
|
256
328
|
}
|
|
257
329
|
});
|
|
258
|
-
|
|
330
|
+
const replicated = ids.map((id) => {
|
|
259
331
|
queue.open(id);
|
|
260
|
-
|
|
261
|
-
() => {
|
|
332
|
+
return new AsyncIteratorClass(
|
|
333
|
+
async () => {
|
|
262
334
|
start();
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}
|
|
269
|
-
});
|
|
270
|
-
});
|
|
335
|
+
const item = await queue.pull(id);
|
|
336
|
+
if (item.next) {
|
|
337
|
+
return item.next;
|
|
338
|
+
}
|
|
339
|
+
throw item.error;
|
|
271
340
|
},
|
|
272
341
|
async (reason) => {
|
|
273
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 {
|
|
274
368
|
if (reason !== "next") {
|
|
275
|
-
|
|
276
|
-
await source?.return?.();
|
|
277
|
-
}
|
|
369
|
+
await runInSpanContext(span, () => iterator.return?.());
|
|
278
370
|
}
|
|
371
|
+
} catch (err) {
|
|
372
|
+
setSpanError(span, err, options);
|
|
373
|
+
throw err;
|
|
374
|
+
} finally {
|
|
375
|
+
span?.end();
|
|
279
376
|
}
|
|
280
|
-
|
|
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 = /* @__PURE__ */ new Set());
|
|
407
|
+
}
|
|
408
|
+
listeners.add(listenerOrOptions);
|
|
409
|
+
return () => {
|
|
410
|
+
listeners.delete(listenerOrOptions);
|
|
411
|
+
if (listeners.size === 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
|
+
});
|
|
281
456
|
}
|
|
282
|
-
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
class SequentialIdGenerator {
|
|
460
|
+
index = BigInt(0);
|
|
461
|
+
generate() {
|
|
462
|
+
const id = this.index.toString(32);
|
|
463
|
+
this.index++;
|
|
464
|
+
return id;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function onStart(callback) {
|
|
469
|
+
return async (options, ...rest) => {
|
|
470
|
+
await callback(options, ...rest);
|
|
471
|
+
return await options.next();
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
function onSuccess(callback) {
|
|
475
|
+
return async (options, ...rest) => {
|
|
476
|
+
const result = await options.next();
|
|
477
|
+
await callback(result, options, ...rest);
|
|
478
|
+
return result;
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
function onError(callback) {
|
|
482
|
+
return async (options, ...rest) => {
|
|
483
|
+
try {
|
|
484
|
+
return await options.next();
|
|
485
|
+
} catch (error) {
|
|
486
|
+
await callback(error, options, ...rest);
|
|
487
|
+
throw error;
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
function onFinish(callback) {
|
|
492
|
+
let state;
|
|
493
|
+
return async (options, ...rest) => {
|
|
494
|
+
try {
|
|
495
|
+
const result = await options.next();
|
|
496
|
+
state = [null, result, true];
|
|
497
|
+
return result;
|
|
498
|
+
} catch (error) {
|
|
499
|
+
state = [error, void 0, false];
|
|
500
|
+
throw error;
|
|
501
|
+
} finally {
|
|
502
|
+
await callback(state, options, ...rest);
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
function intercept(interceptors, options, main) {
|
|
507
|
+
const next = (options2, index) => {
|
|
508
|
+
const interceptor = interceptors[index];
|
|
509
|
+
if (!interceptor) {
|
|
510
|
+
return main(options2);
|
|
511
|
+
}
|
|
512
|
+
return interceptor({
|
|
513
|
+
...options2,
|
|
514
|
+
next: (newOptions = options2) => next(newOptions, index + 1)
|
|
515
|
+
});
|
|
516
|
+
};
|
|
517
|
+
return next(options, 0);
|
|
283
518
|
}
|
|
284
519
|
|
|
285
520
|
function parseEmptyableJSON(text) {
|
|
@@ -307,6 +542,12 @@ function findDeepMatches(check, payload, segments = [], maps = [], values = [])
|
|
|
307
542
|
}
|
|
308
543
|
return { maps, values };
|
|
309
544
|
}
|
|
545
|
+
function getConstructor(value) {
|
|
546
|
+
if (!isTypescriptObject(value)) {
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
return Object.getPrototypeOf(value)?.constructor;
|
|
550
|
+
}
|
|
310
551
|
function isObject(value) {
|
|
311
552
|
if (!value || typeof value !== "object") {
|
|
312
553
|
return false;
|
|
@@ -352,6 +593,73 @@ const NullProtoObj = /* @__PURE__ */ (() => {
|
|
|
352
593
|
return e;
|
|
353
594
|
})();
|
|
354
595
|
|
|
596
|
+
function preventNativeAwait(target) {
|
|
597
|
+
return new Proxy(target, {
|
|
598
|
+
get(target2, prop, receiver) {
|
|
599
|
+
const value = Reflect.get(target2, prop, receiver);
|
|
600
|
+
if (prop !== "then" || typeof value !== "function") {
|
|
601
|
+
return value;
|
|
602
|
+
}
|
|
603
|
+
return new Proxy(value, {
|
|
604
|
+
apply(targetFn, thisArg, args) {
|
|
605
|
+
if (args.length !== 2 || args.some((arg) => !isNativeFunction(arg))) {
|
|
606
|
+
return Reflect.apply(targetFn, thisArg, args);
|
|
607
|
+
}
|
|
608
|
+
let shouldOmit = true;
|
|
609
|
+
args[0].call(thisArg, preventNativeAwait(new Proxy(target2, {
|
|
610
|
+
get: (target3, prop2, receiver2) => {
|
|
611
|
+
if (shouldOmit && prop2 === "then") {
|
|
612
|
+
shouldOmit = false;
|
|
613
|
+
return void 0;
|
|
614
|
+
}
|
|
615
|
+
return Reflect.get(target3, prop2, receiver2);
|
|
616
|
+
}
|
|
617
|
+
})));
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
const NATIVE_FUNCTION_REGEX = /^\s*function\s*\(\)\s*\{\s*\[native code\]\s*\}\s*$/;
|
|
624
|
+
function isNativeFunction(fn) {
|
|
625
|
+
return typeof fn === "function" && NATIVE_FUNCTION_REGEX.test(fn.toString());
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
function streamToAsyncIteratorClass(stream) {
|
|
629
|
+
const reader = stream.getReader();
|
|
630
|
+
return new AsyncIteratorClass(
|
|
631
|
+
async () => {
|
|
632
|
+
return reader.read();
|
|
633
|
+
},
|
|
634
|
+
async () => {
|
|
635
|
+
await reader.cancel();
|
|
636
|
+
}
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
function asyncIteratorToStream(iterator) {
|
|
640
|
+
return new ReadableStream({
|
|
641
|
+
async pull(controller) {
|
|
642
|
+
const { done, value } = await iterator.next();
|
|
643
|
+
if (done) {
|
|
644
|
+
controller.close();
|
|
645
|
+
} else {
|
|
646
|
+
controller.enqueue(value);
|
|
647
|
+
}
|
|
648
|
+
},
|
|
649
|
+
async cancel() {
|
|
650
|
+
await iterator.return?.();
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
function tryDecodeURIComponent(value) {
|
|
656
|
+
try {
|
|
657
|
+
return decodeURIComponent(value);
|
|
658
|
+
} catch {
|
|
659
|
+
return value;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
355
663
|
function value(value2, ...args) {
|
|
356
664
|
if (typeof value2 === "function") {
|
|
357
665
|
return value2(...args);
|
|
@@ -359,4 +667,4 @@ function value(value2, ...args) {
|
|
|
359
667
|
return value2;
|
|
360
668
|
}
|
|
361
669
|
|
|
362
|
-
export { AsyncIdQueue, NullProtoObj, SequentialIdGenerator, clone,
|
|
670
|
+
export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, findDeepMatches, get, getConstructor, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, preventNativeAwait, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
|