@orpc/shared 0.0.0-next.75f1e0e → 0.0.0-next.7605f6c
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 +125 -18
- package/dist/index.d.mts +192 -26
- package/dist/index.d.ts +192 -26
- package/dist/index.mjs +368 -103
- package/package.json +18 -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.7605f6c";
|
|
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 () => {
|
|
@@ -40,13 +58,112 @@ function defer(callback) {
|
|
|
40
58
|
}
|
|
41
59
|
}
|
|
42
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
|
+
|
|
43
154
|
class AsyncIdQueue {
|
|
44
155
|
openIds = /* @__PURE__ */ new Set();
|
|
45
|
-
|
|
46
|
-
|
|
156
|
+
queues = /* @__PURE__ */ new Map();
|
|
157
|
+
waiters = /* @__PURE__ */ new Map();
|
|
47
158
|
get length() {
|
|
48
159
|
return this.openIds.size;
|
|
49
160
|
}
|
|
161
|
+
get waiterIds() {
|
|
162
|
+
return Array.from(this.waiters.keys());
|
|
163
|
+
}
|
|
164
|
+
hasBufferedItems(id) {
|
|
165
|
+
return Boolean(this.queues.get(id)?.length);
|
|
166
|
+
}
|
|
50
167
|
open(id) {
|
|
51
168
|
this.openIds.add(id);
|
|
52
169
|
}
|
|
@@ -55,59 +172,57 @@ class AsyncIdQueue {
|
|
|
55
172
|
}
|
|
56
173
|
push(id, item) {
|
|
57
174
|
this.assertOpen(id);
|
|
58
|
-
const pending = this.
|
|
175
|
+
const pending = this.waiters.get(id);
|
|
59
176
|
if (pending?.length) {
|
|
60
177
|
pending.shift()[0](item);
|
|
61
178
|
if (pending.length === 0) {
|
|
62
|
-
this.
|
|
179
|
+
this.waiters.delete(id);
|
|
63
180
|
}
|
|
64
181
|
} else {
|
|
65
|
-
const items = this.
|
|
182
|
+
const items = this.queues.get(id);
|
|
66
183
|
if (items) {
|
|
67
184
|
items.push(item);
|
|
68
185
|
} else {
|
|
69
|
-
this.
|
|
186
|
+
this.queues.set(id, [item]);
|
|
70
187
|
}
|
|
71
188
|
}
|
|
72
189
|
}
|
|
73
190
|
async pull(id) {
|
|
74
191
|
this.assertOpen(id);
|
|
75
|
-
const items = this.
|
|
192
|
+
const items = this.queues.get(id);
|
|
76
193
|
if (items?.length) {
|
|
77
194
|
const item = items.shift();
|
|
78
195
|
if (items.length === 0) {
|
|
79
|
-
this.
|
|
196
|
+
this.queues.delete(id);
|
|
80
197
|
}
|
|
81
198
|
return item;
|
|
82
199
|
}
|
|
83
200
|
return new Promise((resolve, reject) => {
|
|
84
|
-
const waitingPulls = this.
|
|
201
|
+
const waitingPulls = this.waiters.get(id);
|
|
85
202
|
const pending = [resolve, reject];
|
|
86
203
|
if (waitingPulls) {
|
|
87
204
|
waitingPulls.push(pending);
|
|
88
205
|
} else {
|
|
89
|
-
this.
|
|
206
|
+
this.waiters.set(id, [pending]);
|
|
90
207
|
}
|
|
91
208
|
});
|
|
92
209
|
}
|
|
93
210
|
close({ id, reason } = {}) {
|
|
94
211
|
if (id === void 0) {
|
|
95
|
-
this.
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
});
|
|
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));
|
|
99
215
|
});
|
|
100
|
-
this.
|
|
216
|
+
this.waiters.clear();
|
|
101
217
|
this.openIds.clear();
|
|
102
|
-
this.
|
|
218
|
+
this.queues.clear();
|
|
103
219
|
return;
|
|
104
220
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
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);
|
|
109
224
|
this.openIds.delete(id);
|
|
110
|
-
this.
|
|
225
|
+
this.queues.delete(id);
|
|
111
226
|
}
|
|
112
227
|
assertOpen(id) {
|
|
113
228
|
if (!this.isOpen(id)) {
|
|
@@ -120,111 +235,148 @@ function isAsyncIteratorObject(maybe) {
|
|
|
120
235
|
if (!maybe || typeof maybe !== "object") {
|
|
121
236
|
return false;
|
|
122
237
|
}
|
|
123
|
-
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";
|
|
124
239
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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) {
|
|
131
251
|
return { done: true, value: void 0 };
|
|
132
252
|
}
|
|
133
253
|
try {
|
|
134
254
|
const result = await next();
|
|
135
255
|
if (result.done) {
|
|
136
|
-
isDone = true;
|
|
256
|
+
this.#isDone = true;
|
|
137
257
|
}
|
|
138
258
|
return result;
|
|
139
259
|
} catch (err) {
|
|
140
|
-
isDone = true;
|
|
260
|
+
this.#isDone = true;
|
|
141
261
|
throw err;
|
|
142
262
|
} finally {
|
|
143
|
-
if (isDone && !isExecuteComplete) {
|
|
144
|
-
isExecuteComplete = true;
|
|
145
|
-
await cleanup("next");
|
|
263
|
+
if (this.#isDone && !this.#isExecuteComplete) {
|
|
264
|
+
this.#isExecuteComplete = true;
|
|
265
|
+
await this.#cleanup("next");
|
|
146
266
|
}
|
|
147
267
|
}
|
|
148
|
-
})
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
isDone = true;
|
|
159
|
-
if (!isExecuteComplete) {
|
|
160
|
-
isExecuteComplete = true;
|
|
161
|
-
await cleanup("throw");
|
|
162
|
-
}
|
|
163
|
-
throw err;
|
|
164
|
-
},
|
|
165
|
-
/**
|
|
166
|
-
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
167
|
-
*/
|
|
168
|
-
async [Symbol.asyncDispose ?? Symbol.for("asyncDispose")]() {
|
|
169
|
-
isDone = true;
|
|
170
|
-
if (!isExecuteComplete) {
|
|
171
|
-
isExecuteComplete = true;
|
|
172
|
-
await cleanup("dispose");
|
|
173
|
-
}
|
|
174
|
-
},
|
|
175
|
-
[Symbol.asyncIterator]() {
|
|
176
|
-
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");
|
|
177
278
|
}
|
|
178
|
-
|
|
179
|
-
|
|
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
|
+
}
|
|
180
302
|
}
|
|
181
303
|
function replicateAsyncIterator(source, count) {
|
|
182
304
|
const queue = new AsyncIdQueue();
|
|
183
|
-
const
|
|
184
|
-
let
|
|
305
|
+
const ids = Array.from({ length: count }, (_, i) => i.toString());
|
|
306
|
+
let isSourceFinished = false;
|
|
185
307
|
const start = once(async () => {
|
|
186
308
|
try {
|
|
187
309
|
while (true) {
|
|
188
310
|
const item = await source.next();
|
|
189
|
-
|
|
311
|
+
ids.forEach((id) => {
|
|
190
312
|
if (queue.isOpen(id)) {
|
|
191
|
-
queue.push(id, item);
|
|
313
|
+
queue.push(id, { next: item });
|
|
192
314
|
}
|
|
193
|
-
}
|
|
315
|
+
});
|
|
194
316
|
if (item.done) {
|
|
195
317
|
break;
|
|
196
318
|
}
|
|
197
319
|
}
|
|
198
|
-
} catch (
|
|
199
|
-
|
|
320
|
+
} catch (error) {
|
|
321
|
+
ids.forEach((id) => {
|
|
322
|
+
if (queue.isOpen(id)) {
|
|
323
|
+
queue.push(id, { error });
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
} finally {
|
|
327
|
+
isSourceFinished = true;
|
|
200
328
|
}
|
|
201
329
|
});
|
|
202
|
-
|
|
330
|
+
const replicated = ids.map((id) => {
|
|
203
331
|
queue.open(id);
|
|
204
|
-
|
|
205
|
-
() => {
|
|
332
|
+
return new AsyncIteratorClass(
|
|
333
|
+
async () => {
|
|
206
334
|
start();
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
});
|
|
335
|
+
const item = await queue.pull(id);
|
|
336
|
+
if (item.next) {
|
|
337
|
+
return item.next;
|
|
338
|
+
}
|
|
339
|
+
throw item.error;
|
|
215
340
|
},
|
|
216
341
|
async (reason) => {
|
|
217
342
|
queue.close({ id });
|
|
218
|
-
if (reason !== "next") {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
343
|
+
if (reason !== "next" && !queue.length && !isSourceFinished) {
|
|
344
|
+
isSourceFinished = true;
|
|
345
|
+
await source?.return?.();
|
|
222
346
|
}
|
|
223
347
|
}
|
|
224
|
-
)
|
|
225
|
-
}
|
|
348
|
+
);
|
|
349
|
+
});
|
|
226
350
|
return replicated;
|
|
227
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 {
|
|
368
|
+
if (reason !== "next") {
|
|
369
|
+
await runInSpanContext(span, () => iterator.return?.());
|
|
370
|
+
}
|
|
371
|
+
} catch (err) {
|
|
372
|
+
setSpanError(span, err, options);
|
|
373
|
+
throw err;
|
|
374
|
+
} finally {
|
|
375
|
+
span?.end();
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
);
|
|
379
|
+
}
|
|
228
380
|
|
|
229
381
|
class EventPublisher {
|
|
230
382
|
#listenersMap = /* @__PURE__ */ new Map();
|
|
@@ -251,18 +403,19 @@ class EventPublisher {
|
|
|
251
403
|
if (typeof listenerOrOptions === "function") {
|
|
252
404
|
let listeners = this.#listenersMap.get(event);
|
|
253
405
|
if (!listeners) {
|
|
254
|
-
this.#listenersMap.set(event, listeners =
|
|
406
|
+
this.#listenersMap.set(event, listeners = []);
|
|
255
407
|
}
|
|
256
|
-
listeners.
|
|
257
|
-
return () => {
|
|
258
|
-
listeners.
|
|
259
|
-
if (listeners.
|
|
408
|
+
listeners.push(listenerOrOptions);
|
|
409
|
+
return once(() => {
|
|
410
|
+
listeners.splice(listeners.indexOf(listenerOrOptions), 1);
|
|
411
|
+
if (listeners.length === 0) {
|
|
260
412
|
this.#listenersMap.delete(event);
|
|
261
413
|
}
|
|
262
|
-
};
|
|
414
|
+
});
|
|
263
415
|
}
|
|
264
416
|
const signal = listenerOrOptions?.signal;
|
|
265
417
|
const maxBufferedEvents = listenerOrOptions?.maxBufferedEvents ?? this.#maxBufferedEvents;
|
|
418
|
+
signal?.throwIfAborted();
|
|
266
419
|
const bufferedEvents = [];
|
|
267
420
|
const pullResolvers = [];
|
|
268
421
|
const unsubscribe = this.subscribe(event, (payload) => {
|
|
@@ -283,7 +436,7 @@ class EventPublisher {
|
|
|
283
436
|
bufferedEvents.length = 0;
|
|
284
437
|
};
|
|
285
438
|
signal?.addEventListener("abort", abortListener, { once: true });
|
|
286
|
-
return
|
|
439
|
+
return new AsyncIteratorClass(async () => {
|
|
287
440
|
if (signal?.aborted) {
|
|
288
441
|
throw signal.reason;
|
|
289
442
|
}
|
|
@@ -304,14 +457,18 @@ class EventPublisher {
|
|
|
304
457
|
}
|
|
305
458
|
|
|
306
459
|
class SequentialIdGenerator {
|
|
307
|
-
|
|
460
|
+
index = BigInt(1);
|
|
308
461
|
generate() {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
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;
|
|
314
470
|
}
|
|
471
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
315
472
|
}
|
|
316
473
|
|
|
317
474
|
function onStart(callback) {
|
|
@@ -391,6 +548,12 @@ function findDeepMatches(check, payload, segments = [], maps = [], values = [])
|
|
|
391
548
|
}
|
|
392
549
|
return { maps, values };
|
|
393
550
|
}
|
|
551
|
+
function getConstructor(value) {
|
|
552
|
+
if (!isTypescriptObject(value)) {
|
|
553
|
+
return null;
|
|
554
|
+
}
|
|
555
|
+
return Object.getPrototypeOf(value)?.constructor;
|
|
556
|
+
}
|
|
394
557
|
function isObject(value) {
|
|
395
558
|
if (!value || typeof value !== "object") {
|
|
396
559
|
return false;
|
|
@@ -410,6 +573,9 @@ function clone(value) {
|
|
|
410
573
|
for (const key in value) {
|
|
411
574
|
result[key] = clone(value[key]);
|
|
412
575
|
}
|
|
576
|
+
for (const sym of Object.getOwnPropertySymbols(value)) {
|
|
577
|
+
result[sym] = clone(value[sym]);
|
|
578
|
+
}
|
|
413
579
|
return result;
|
|
414
580
|
}
|
|
415
581
|
return value;
|
|
@@ -442,5 +608,104 @@ function value(value2, ...args) {
|
|
|
442
608
|
}
|
|
443
609
|
return value2;
|
|
444
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
|
+
}
|
|
445
710
|
|
|
446
|
-
export { AsyncIdQueue, EventPublisher, 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 };
|
package/package.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.7605f6c",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"homepage": "https://orpc.
|
|
6
|
+
"homepage": "https://orpc.dev",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/
|
|
9
|
+
"url": "git+https://github.com/middleapi/orpc.git",
|
|
10
10
|
"directory": "packages/shared"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [
|
|
13
|
-
"unnoq",
|
|
14
13
|
"orpc"
|
|
15
14
|
],
|
|
15
|
+
"sideEffects": false,
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
18
|
"types": "./dist/index.d.mts",
|
|
@@ -23,14 +23,23 @@
|
|
|
23
23
|
"files": [
|
|
24
24
|
"dist"
|
|
25
25
|
],
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@opentelemetry/api": ">=1.9.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependenciesMeta": {
|
|
30
|
+
"@opentelemetry/api": {
|
|
31
|
+
"optional": true
|
|
32
|
+
}
|
|
33
|
+
},
|
|
26
34
|
"dependencies": {
|
|
27
|
-
"radash": "^12.1.
|
|
28
|
-
"type-fest": "^4.
|
|
35
|
+
"radash": "^12.1.1",
|
|
36
|
+
"type-fest": "^5.4.4"
|
|
29
37
|
},
|
|
30
38
|
"devDependencies": {
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
39
|
+
"@opentelemetry/api": "^1.9.0",
|
|
40
|
+
"arktype": "2.2.0",
|
|
41
|
+
"valibot": "^1.2.0",
|
|
42
|
+
"zod": "^4.3.6"
|
|
34
43
|
},
|
|
35
44
|
"scripts": {
|
|
36
45
|
"build": "unbuild",
|