@orpc/shared 0.0.0-next.ff5907c → 0.0.0-next.ff7ad2e
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 +78 -0
- package/dist/index.d.mts +314 -0
- package/dist/index.d.ts +314 -0
- package/dist/index.mjs +658 -0
- package/package.json +21 -12
- package/dist/index.js +0 -155
- package/dist/src/chain.d.ts +0 -5
- package/dist/src/error.d.ts +0 -2
- package/dist/src/function.d.ts +0 -2
- package/dist/src/index.d.ts +0 -10
- package/dist/src/interceptor.d.ts +0 -45
- package/dist/src/object.d.ts +0 -12
- package/dist/src/types.d.ts +0 -3
- package/dist/src/value.d.ts +0 -4
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,658 @@
|
|
|
1
|
+
export { group, guard, mapEntries, mapValues, omit } from 'radash';
|
|
2
|
+
|
|
3
|
+
function resolveMaybeOptionalOptions(rest) {
|
|
4
|
+
return rest[0] ?? {};
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function toArray(value) {
|
|
8
|
+
return Array.isArray(value) ? value : value === void 0 || value === null ? [] : [value];
|
|
9
|
+
}
|
|
10
|
+
function splitInHalf(arr) {
|
|
11
|
+
const half = Math.ceil(arr.length / 2);
|
|
12
|
+
return [arr.slice(0, half), arr.slice(half)];
|
|
13
|
+
}
|
|
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.ff7ad2e";
|
|
25
|
+
|
|
26
|
+
class AbortError extends Error {
|
|
27
|
+
constructor(...rest) {
|
|
28
|
+
super(...rest);
|
|
29
|
+
this.name = "AbortError";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function once(fn) {
|
|
34
|
+
let cached;
|
|
35
|
+
return () => {
|
|
36
|
+
if (cached) {
|
|
37
|
+
return cached.result;
|
|
38
|
+
}
|
|
39
|
+
const result = fn();
|
|
40
|
+
cached = { result };
|
|
41
|
+
return result;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function sequential(fn) {
|
|
45
|
+
let lastOperationPromise = Promise.resolve();
|
|
46
|
+
return (...args) => {
|
|
47
|
+
return lastOperationPromise = lastOperationPromise.catch(() => {
|
|
48
|
+
}).then(() => {
|
|
49
|
+
return fn(...args);
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
}
|
|
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)));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
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;
|
|
65
|
+
}
|
|
66
|
+
function getGlobalOtelConfig() {
|
|
67
|
+
return globalThis[GLOBAL_OTEL_CONFIG_KEY];
|
|
68
|
+
}
|
|
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;
|
|
101
|
+
}
|
|
102
|
+
return exception;
|
|
103
|
+
}
|
|
104
|
+
return { message: String(error) };
|
|
105
|
+
}
|
|
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) => {
|
|
130
|
+
try {
|
|
131
|
+
return await fn(span);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
setSpanError(span, e, options);
|
|
134
|
+
throw e;
|
|
135
|
+
} finally {
|
|
136
|
+
span.end();
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
if (context) {
|
|
140
|
+
return tracer.startActiveSpan(name, options, context, callback);
|
|
141
|
+
} else {
|
|
142
|
+
return tracer.startActiveSpan(name, options, callback);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
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);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
class AsyncIdQueue {
|
|
155
|
+
openIds = /* @__PURE__ */ new Set();
|
|
156
|
+
items = /* @__PURE__ */ new Map();
|
|
157
|
+
pendingPulls = /* @__PURE__ */ new Map();
|
|
158
|
+
get length() {
|
|
159
|
+
return this.openIds.size;
|
|
160
|
+
}
|
|
161
|
+
open(id) {
|
|
162
|
+
this.openIds.add(id);
|
|
163
|
+
}
|
|
164
|
+
isOpen(id) {
|
|
165
|
+
return this.openIds.has(id);
|
|
166
|
+
}
|
|
167
|
+
push(id, item) {
|
|
168
|
+
this.assertOpen(id);
|
|
169
|
+
const pending = this.pendingPulls.get(id);
|
|
170
|
+
if (pending?.length) {
|
|
171
|
+
pending.shift()[0](item);
|
|
172
|
+
if (pending.length === 0) {
|
|
173
|
+
this.pendingPulls.delete(id);
|
|
174
|
+
}
|
|
175
|
+
} else {
|
|
176
|
+
const items = this.items.get(id);
|
|
177
|
+
if (items) {
|
|
178
|
+
items.push(item);
|
|
179
|
+
} else {
|
|
180
|
+
this.items.set(id, [item]);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
async pull(id) {
|
|
185
|
+
this.assertOpen(id);
|
|
186
|
+
const items = this.items.get(id);
|
|
187
|
+
if (items?.length) {
|
|
188
|
+
const item = items.shift();
|
|
189
|
+
if (items.length === 0) {
|
|
190
|
+
this.items.delete(id);
|
|
191
|
+
}
|
|
192
|
+
return item;
|
|
193
|
+
}
|
|
194
|
+
return new Promise((resolve, reject) => {
|
|
195
|
+
const waitingPulls = this.pendingPulls.get(id);
|
|
196
|
+
const pending = [resolve, reject];
|
|
197
|
+
if (waitingPulls) {
|
|
198
|
+
waitingPulls.push(pending);
|
|
199
|
+
} else {
|
|
200
|
+
this.pendingPulls.set(id, [pending]);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
close({ id, reason } = {}) {
|
|
205
|
+
if (id === void 0) {
|
|
206
|
+
this.pendingPulls.forEach((pendingPulls, id2) => {
|
|
207
|
+
pendingPulls.forEach(([, reject]) => {
|
|
208
|
+
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id2}] was closed or aborted while waiting for pulling.`));
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
this.pendingPulls.clear();
|
|
212
|
+
this.openIds.clear();
|
|
213
|
+
this.items.clear();
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
this.pendingPulls.get(id)?.forEach(([, reject]) => {
|
|
217
|
+
reject(reason ?? new Error(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`));
|
|
218
|
+
});
|
|
219
|
+
this.pendingPulls.delete(id);
|
|
220
|
+
this.openIds.delete(id);
|
|
221
|
+
this.items.delete(id);
|
|
222
|
+
}
|
|
223
|
+
assertOpen(id) {
|
|
224
|
+
if (!this.isOpen(id)) {
|
|
225
|
+
throw new Error(`[AsyncIdQueue] Cannot access queue[${id}] because it is not open or aborted.`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function isAsyncIteratorObject(maybe) {
|
|
231
|
+
if (!maybe || typeof maybe !== "object") {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
return "next" in maybe && typeof maybe.next === "function" && Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
|
235
|
+
}
|
|
236
|
+
const fallbackAsyncDisposeSymbol = Symbol.for("asyncDispose");
|
|
237
|
+
const asyncDisposeSymbol = Symbol.asyncDispose ?? fallbackAsyncDisposeSymbol;
|
|
238
|
+
class AsyncIteratorClass {
|
|
239
|
+
#isDone = false;
|
|
240
|
+
#isExecuteComplete = false;
|
|
241
|
+
#cleanup;
|
|
242
|
+
#next;
|
|
243
|
+
constructor(next, cleanup) {
|
|
244
|
+
this.#cleanup = cleanup;
|
|
245
|
+
this.#next = sequential(async () => {
|
|
246
|
+
if (this.#isDone) {
|
|
247
|
+
return { done: true, value: void 0 };
|
|
248
|
+
}
|
|
249
|
+
try {
|
|
250
|
+
const result = await next();
|
|
251
|
+
if (result.done) {
|
|
252
|
+
this.#isDone = true;
|
|
253
|
+
}
|
|
254
|
+
return result;
|
|
255
|
+
} catch (err) {
|
|
256
|
+
this.#isDone = true;
|
|
257
|
+
throw err;
|
|
258
|
+
} finally {
|
|
259
|
+
if (this.#isDone && !this.#isExecuteComplete) {
|
|
260
|
+
this.#isExecuteComplete = true;
|
|
261
|
+
await this.#cleanup("next");
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
next() {
|
|
267
|
+
return this.#next();
|
|
268
|
+
}
|
|
269
|
+
async return(value) {
|
|
270
|
+
this.#isDone = true;
|
|
271
|
+
if (!this.#isExecuteComplete) {
|
|
272
|
+
this.#isExecuteComplete = true;
|
|
273
|
+
await this.#cleanup("return");
|
|
274
|
+
}
|
|
275
|
+
return { done: true, value };
|
|
276
|
+
}
|
|
277
|
+
async throw(err) {
|
|
278
|
+
this.#isDone = true;
|
|
279
|
+
if (!this.#isExecuteComplete) {
|
|
280
|
+
this.#isExecuteComplete = true;
|
|
281
|
+
await this.#cleanup("throw");
|
|
282
|
+
}
|
|
283
|
+
throw err;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
287
|
+
*/
|
|
288
|
+
async [asyncDisposeSymbol]() {
|
|
289
|
+
this.#isDone = true;
|
|
290
|
+
if (!this.#isExecuteComplete) {
|
|
291
|
+
this.#isExecuteComplete = true;
|
|
292
|
+
await this.#cleanup("dispose");
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
[Symbol.asyncIterator]() {
|
|
296
|
+
return this;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
function replicateAsyncIterator(source, count) {
|
|
300
|
+
const queue = new AsyncIdQueue();
|
|
301
|
+
const replicated = [];
|
|
302
|
+
let error;
|
|
303
|
+
const start = once(async () => {
|
|
304
|
+
try {
|
|
305
|
+
while (true) {
|
|
306
|
+
const item = await source.next();
|
|
307
|
+
for (let id = 0; id < count; id++) {
|
|
308
|
+
if (queue.isOpen(id.toString())) {
|
|
309
|
+
queue.push(id.toString(), item);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (item.done) {
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
} catch (e) {
|
|
317
|
+
error = { value: e };
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
for (let id = 0; id < count; id++) {
|
|
321
|
+
queue.open(id.toString());
|
|
322
|
+
replicated.push(new AsyncIteratorClass(
|
|
323
|
+
() => {
|
|
324
|
+
start();
|
|
325
|
+
return new Promise((resolve, reject) => {
|
|
326
|
+
queue.pull(id.toString()).then(resolve).catch(reject);
|
|
327
|
+
defer(() => {
|
|
328
|
+
if (error) {
|
|
329
|
+
reject(error.value);
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
},
|
|
334
|
+
async (reason) => {
|
|
335
|
+
queue.close({ id: id.toString() });
|
|
336
|
+
if (reason !== "next") {
|
|
337
|
+
if (replicated.every((_, id2) => !queue.isOpen(id2.toString()))) {
|
|
338
|
+
await source?.return?.();
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
));
|
|
343
|
+
}
|
|
344
|
+
return replicated;
|
|
345
|
+
}
|
|
346
|
+
function asyncIteratorWithSpan({ name, ...options }, iterator) {
|
|
347
|
+
let span;
|
|
348
|
+
return new AsyncIteratorClass(
|
|
349
|
+
async () => {
|
|
350
|
+
span ??= startSpan(name);
|
|
351
|
+
try {
|
|
352
|
+
const result = await runInSpanContext(span, () => iterator.next());
|
|
353
|
+
span?.addEvent(result.done ? "completed" : "yielded");
|
|
354
|
+
return result;
|
|
355
|
+
} catch (err) {
|
|
356
|
+
setSpanError(span, err, options);
|
|
357
|
+
throw err;
|
|
358
|
+
}
|
|
359
|
+
},
|
|
360
|
+
async (reason) => {
|
|
361
|
+
try {
|
|
362
|
+
if (reason !== "next") {
|
|
363
|
+
await runInSpanContext(span, () => iterator.return?.());
|
|
364
|
+
}
|
|
365
|
+
} catch (err) {
|
|
366
|
+
setSpanError(span, err, options);
|
|
367
|
+
throw err;
|
|
368
|
+
} finally {
|
|
369
|
+
span?.end();
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
class EventPublisher {
|
|
376
|
+
#listenersMap = /* @__PURE__ */ new Map();
|
|
377
|
+
#maxBufferedEvents;
|
|
378
|
+
constructor(options = {}) {
|
|
379
|
+
this.#maxBufferedEvents = options.maxBufferedEvents ?? 100;
|
|
380
|
+
}
|
|
381
|
+
get size() {
|
|
382
|
+
return this.#listenersMap.size;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Emits an event and delivers the payload to all subscribed listeners.
|
|
386
|
+
*/
|
|
387
|
+
publish(event, payload) {
|
|
388
|
+
const listeners = this.#listenersMap.get(event);
|
|
389
|
+
if (!listeners) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
for (const listener of listeners) {
|
|
393
|
+
listener(payload);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
subscribe(event, listenerOrOptions) {
|
|
397
|
+
if (typeof listenerOrOptions === "function") {
|
|
398
|
+
let listeners = this.#listenersMap.get(event);
|
|
399
|
+
if (!listeners) {
|
|
400
|
+
this.#listenersMap.set(event, listeners = /* @__PURE__ */ new Set());
|
|
401
|
+
}
|
|
402
|
+
listeners.add(listenerOrOptions);
|
|
403
|
+
return () => {
|
|
404
|
+
listeners.delete(listenerOrOptions);
|
|
405
|
+
if (listeners.size === 0) {
|
|
406
|
+
this.#listenersMap.delete(event);
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
const signal = listenerOrOptions?.signal;
|
|
411
|
+
const maxBufferedEvents = listenerOrOptions?.maxBufferedEvents ?? this.#maxBufferedEvents;
|
|
412
|
+
signal?.throwIfAborted();
|
|
413
|
+
const bufferedEvents = [];
|
|
414
|
+
const pullResolvers = [];
|
|
415
|
+
const unsubscribe = this.subscribe(event, (payload) => {
|
|
416
|
+
const resolver = pullResolvers.shift();
|
|
417
|
+
if (resolver) {
|
|
418
|
+
resolver[0]({ done: false, value: payload });
|
|
419
|
+
} else {
|
|
420
|
+
bufferedEvents.push(payload);
|
|
421
|
+
if (bufferedEvents.length > maxBufferedEvents) {
|
|
422
|
+
bufferedEvents.shift();
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
const abortListener = (event2) => {
|
|
427
|
+
unsubscribe();
|
|
428
|
+
pullResolvers.forEach((resolver) => resolver[1](event2.target.reason));
|
|
429
|
+
pullResolvers.length = 0;
|
|
430
|
+
bufferedEvents.length = 0;
|
|
431
|
+
};
|
|
432
|
+
signal?.addEventListener("abort", abortListener, { once: true });
|
|
433
|
+
return new AsyncIteratorClass(async () => {
|
|
434
|
+
if (signal?.aborted) {
|
|
435
|
+
throw signal.reason;
|
|
436
|
+
}
|
|
437
|
+
if (bufferedEvents.length > 0) {
|
|
438
|
+
return { done: false, value: bufferedEvents.shift() };
|
|
439
|
+
}
|
|
440
|
+
return new Promise((resolve, reject) => {
|
|
441
|
+
pullResolvers.push([resolve, reject]);
|
|
442
|
+
});
|
|
443
|
+
}, async () => {
|
|
444
|
+
unsubscribe();
|
|
445
|
+
signal?.removeEventListener("abort", abortListener);
|
|
446
|
+
pullResolvers.forEach((resolver) => resolver[0]({ done: true, value: void 0 }));
|
|
447
|
+
pullResolvers.length = 0;
|
|
448
|
+
bufferedEvents.length = 0;
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
class SequentialIdGenerator {
|
|
454
|
+
index = BigInt(0);
|
|
455
|
+
generate() {
|
|
456
|
+
const id = this.index.toString(32);
|
|
457
|
+
this.index++;
|
|
458
|
+
return id;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function onStart(callback) {
|
|
463
|
+
return async (options, ...rest) => {
|
|
464
|
+
await callback(options, ...rest);
|
|
465
|
+
return await options.next();
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
function onSuccess(callback) {
|
|
469
|
+
return async (options, ...rest) => {
|
|
470
|
+
const result = await options.next();
|
|
471
|
+
await callback(result, options, ...rest);
|
|
472
|
+
return result;
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
function onError(callback) {
|
|
476
|
+
return async (options, ...rest) => {
|
|
477
|
+
try {
|
|
478
|
+
return await options.next();
|
|
479
|
+
} catch (error) {
|
|
480
|
+
await callback(error, options, ...rest);
|
|
481
|
+
throw error;
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
function onFinish(callback) {
|
|
486
|
+
let state;
|
|
487
|
+
return async (options, ...rest) => {
|
|
488
|
+
try {
|
|
489
|
+
const result = await options.next();
|
|
490
|
+
state = [null, result, true];
|
|
491
|
+
return result;
|
|
492
|
+
} catch (error) {
|
|
493
|
+
state = [error, void 0, false];
|
|
494
|
+
throw error;
|
|
495
|
+
} finally {
|
|
496
|
+
await callback(state, options, ...rest);
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
function intercept(interceptors, options, main) {
|
|
501
|
+
const next = (options2, index) => {
|
|
502
|
+
const interceptor = interceptors[index];
|
|
503
|
+
if (!interceptor) {
|
|
504
|
+
return main(options2);
|
|
505
|
+
}
|
|
506
|
+
return interceptor({
|
|
507
|
+
...options2,
|
|
508
|
+
next: (newOptions = options2) => next(newOptions, index + 1)
|
|
509
|
+
});
|
|
510
|
+
};
|
|
511
|
+
return next(options, 0);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function parseEmptyableJSON(text) {
|
|
515
|
+
if (!text) {
|
|
516
|
+
return void 0;
|
|
517
|
+
}
|
|
518
|
+
return JSON.parse(text);
|
|
519
|
+
}
|
|
520
|
+
function stringifyJSON(value) {
|
|
521
|
+
return JSON.stringify(value);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
525
|
+
if (check(payload)) {
|
|
526
|
+
maps.push(segments);
|
|
527
|
+
values.push(payload);
|
|
528
|
+
} else if (Array.isArray(payload)) {
|
|
529
|
+
payload.forEach((v, i) => {
|
|
530
|
+
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
531
|
+
});
|
|
532
|
+
} else if (isObject(payload)) {
|
|
533
|
+
for (const key in payload) {
|
|
534
|
+
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
return { maps, values };
|
|
538
|
+
}
|
|
539
|
+
function isObject(value) {
|
|
540
|
+
if (!value || typeof value !== "object") {
|
|
541
|
+
return false;
|
|
542
|
+
}
|
|
543
|
+
const proto = Object.getPrototypeOf(value);
|
|
544
|
+
return proto === Object.prototype || !proto || !proto.constructor;
|
|
545
|
+
}
|
|
546
|
+
function isTypescriptObject(value) {
|
|
547
|
+
return !!value && (typeof value === "object" || typeof value === "function");
|
|
548
|
+
}
|
|
549
|
+
function clone(value) {
|
|
550
|
+
if (Array.isArray(value)) {
|
|
551
|
+
return value.map(clone);
|
|
552
|
+
}
|
|
553
|
+
if (isObject(value)) {
|
|
554
|
+
const result = {};
|
|
555
|
+
for (const key in value) {
|
|
556
|
+
result[key] = clone(value[key]);
|
|
557
|
+
}
|
|
558
|
+
return result;
|
|
559
|
+
}
|
|
560
|
+
return value;
|
|
561
|
+
}
|
|
562
|
+
function get(object, path) {
|
|
563
|
+
let current = object;
|
|
564
|
+
for (const key of path) {
|
|
565
|
+
if (!isTypescriptObject(current)) {
|
|
566
|
+
return void 0;
|
|
567
|
+
}
|
|
568
|
+
current = current[key];
|
|
569
|
+
}
|
|
570
|
+
return current;
|
|
571
|
+
}
|
|
572
|
+
function isPropertyKey(value) {
|
|
573
|
+
const type = typeof value;
|
|
574
|
+
return type === "string" || type === "number" || type === "symbol";
|
|
575
|
+
}
|
|
576
|
+
const NullProtoObj = /* @__PURE__ */ (() => {
|
|
577
|
+
const e = function() {
|
|
578
|
+
};
|
|
579
|
+
e.prototype = /* @__PURE__ */ Object.create(null);
|
|
580
|
+
Object.freeze(e.prototype);
|
|
581
|
+
return e;
|
|
582
|
+
})();
|
|
583
|
+
|
|
584
|
+
function preventNativeAwait(target) {
|
|
585
|
+
return new Proxy(target, {
|
|
586
|
+
get(target2, prop, receiver) {
|
|
587
|
+
const value = Reflect.get(target2, prop, receiver);
|
|
588
|
+
if (prop !== "then" || typeof value !== "function") {
|
|
589
|
+
return value;
|
|
590
|
+
}
|
|
591
|
+
return new Proxy(value, {
|
|
592
|
+
apply(targetFn, thisArg, args) {
|
|
593
|
+
if (args.length !== 2 || args.some((arg) => !isNativeFunction(arg))) {
|
|
594
|
+
return Reflect.apply(targetFn, thisArg, args);
|
|
595
|
+
}
|
|
596
|
+
let shouldOmit = true;
|
|
597
|
+
args[0].call(thisArg, preventNativeAwait(new Proxy(target2, {
|
|
598
|
+
get: (target3, prop2, receiver2) => {
|
|
599
|
+
if (shouldOmit && prop2 === "then") {
|
|
600
|
+
shouldOmit = false;
|
|
601
|
+
return void 0;
|
|
602
|
+
}
|
|
603
|
+
return Reflect.get(target3, prop2, receiver2);
|
|
604
|
+
}
|
|
605
|
+
})));
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
const NATIVE_FUNCTION_REGEX = /^\s*function\s*\(\)\s*\{\s*\[native code\]\s*\}\s*$/;
|
|
612
|
+
function isNativeFunction(fn) {
|
|
613
|
+
return typeof fn === "function" && NATIVE_FUNCTION_REGEX.test(fn.toString());
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
function streamToAsyncIteratorClass(stream) {
|
|
617
|
+
const reader = stream.getReader();
|
|
618
|
+
return new AsyncIteratorClass(
|
|
619
|
+
async () => {
|
|
620
|
+
return reader.read();
|
|
621
|
+
},
|
|
622
|
+
async () => {
|
|
623
|
+
await reader.cancel();
|
|
624
|
+
}
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
function asyncIteratorToStream(iterator) {
|
|
628
|
+
return new ReadableStream({
|
|
629
|
+
async pull(controller) {
|
|
630
|
+
const { done, value } = await iterator.next();
|
|
631
|
+
if (done) {
|
|
632
|
+
controller.close();
|
|
633
|
+
} else {
|
|
634
|
+
controller.enqueue(value);
|
|
635
|
+
}
|
|
636
|
+
},
|
|
637
|
+
async cancel() {
|
|
638
|
+
await iterator.return?.();
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
function tryDecodeURIComponent(value) {
|
|
644
|
+
try {
|
|
645
|
+
return decodeURIComponent(value);
|
|
646
|
+
} catch {
|
|
647
|
+
return value;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
function value(value2, ...args) {
|
|
652
|
+
if (typeof value2 === "function") {
|
|
653
|
+
return value2(...args);
|
|
654
|
+
}
|
|
655
|
+
return value2;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, findDeepMatches, get, 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 };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.ff7ad2e",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -15,25 +15,34 @@
|
|
|
15
15
|
],
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
-
"types": "./dist/
|
|
19
|
-
"import": "./dist/index.
|
|
20
|
-
"default": "./dist/index.
|
|
21
|
-
},
|
|
22
|
-
"./🔒/*": {
|
|
23
|
-
"types": "./dist/src/*.d.ts"
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"default": "./dist/index.mjs"
|
|
24
21
|
}
|
|
25
22
|
},
|
|
26
23
|
"files": [
|
|
27
|
-
"!**/*.map",
|
|
28
|
-
"!**/*.tsbuildinfo",
|
|
29
24
|
"dist"
|
|
30
25
|
],
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@opentelemetry/api": ">=1.9.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependenciesMeta": {
|
|
30
|
+
"@opentelemetry/api": {
|
|
31
|
+
"optional": true
|
|
32
|
+
}
|
|
33
|
+
},
|
|
31
34
|
"dependencies": {
|
|
32
|
-
"radash": "^12.1.
|
|
33
|
-
"type-fest": "^4.
|
|
35
|
+
"radash": "^12.1.1",
|
|
36
|
+
"type-fest": "^4.39.1"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@opentelemetry/api": "^1.9.0",
|
|
40
|
+
"arktype": "2.1.20",
|
|
41
|
+
"valibot": "^1.1.0",
|
|
42
|
+
"zod": "^4.0.17"
|
|
34
43
|
},
|
|
35
44
|
"scripts": {
|
|
36
|
-
"build": "
|
|
45
|
+
"build": "unbuild",
|
|
37
46
|
"build:watch": "pnpm run build --watch",
|
|
38
47
|
"type:check": "tsc -b"
|
|
39
48
|
}
|