@orpc/shared 0.0.0-next.d9acc8f → 0.0.0-next.d9b09a3
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 +210 -18
- package/dist/index.d.ts +210 -18
- package/dist/index.mjs +442 -141
- 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.d9b09a3";
|
|
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,286 @@ 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;
|
|
288
386
|
}
|
|
289
|
-
|
|
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
|
+
});
|
|
456
|
+
}
|
|
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);
|
|
290
518
|
}
|
|
291
519
|
|
|
292
520
|
function parseEmptyableJSON(text) {
|
|
@@ -314,6 +542,12 @@ function findDeepMatches(check, payload, segments = [], maps = [], values = [])
|
|
|
314
542
|
}
|
|
315
543
|
return { maps, values };
|
|
316
544
|
}
|
|
545
|
+
function getConstructor(value) {
|
|
546
|
+
if (!isTypescriptObject(value)) {
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
return Object.getPrototypeOf(value)?.constructor;
|
|
550
|
+
}
|
|
317
551
|
function isObject(value) {
|
|
318
552
|
if (!value || typeof value !== "object") {
|
|
319
553
|
return false;
|
|
@@ -359,6 +593,73 @@ const NullProtoObj = /* @__PURE__ */ (() => {
|
|
|
359
593
|
return e;
|
|
360
594
|
})();
|
|
361
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
|
+
|
|
362
663
|
function value(value2, ...args) {
|
|
363
664
|
if (typeof value2 === "function") {
|
|
364
665
|
return value2(...args);
|
|
@@ -366,4 +667,4 @@ function value(value2, ...args) {
|
|
|
366
667
|
return value2;
|
|
367
668
|
}
|
|
368
669
|
|
|
369
|
-
export { AsyncIdQueue, NullProtoObj, SequentialIdGenerator,
|
|
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 };
|