@orpc/shared 2.0.0-beta.48 → 2.0.0-beta.49
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 +4 -4
- package/dist/args.js +3 -0
- package/dist/array.js +7 -0
- package/dist/bigint.d.ts +9 -0
- package/dist/bigint.js +19 -0
- package/dist/buffer.d.ts +18 -0
- package/dist/buffer.js +77 -0
- package/dist/compare.js +45 -0
- package/dist/consts.js +1 -0
- package/dist/error.js +3 -0
- package/dist/{function.d.mts → function.d.ts} +5 -5
- package/dist/function.js +38 -0
- package/dist/{http.d.mts → http.d.ts} +16 -16
- package/dist/http.js +113 -0
- package/dist/id.d.ts +8 -0
- package/dist/id.js +13 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +32 -0
- package/dist/interceptor.d.ts +60 -0
- package/dist/interceptor.js +139 -0
- package/dist/iterator.d.ts +56 -0
- package/dist/iterator.js +157 -0
- package/dist/object.d.ts +60 -0
- package/dist/object.js +245 -0
- package/dist/plugin.d.ts +12 -0
- package/dist/plugin.js +61 -0
- package/dist/promise.d.ts +17 -0
- package/dist/promise.js +32 -0
- package/dist/proxy.d.ts +12 -0
- package/dist/proxy.js +24 -0
- package/dist/queue.d.ts +16 -0
- package/dist/queue.js +79 -0
- package/dist/signal.d.ts +18 -0
- package/dist/signal.js +85 -0
- package/dist/stream.d.ts +48 -0
- package/dist/stream.js +155 -0
- package/dist/tracing.d.ts +73 -0
- package/dist/tracing.js +94 -0
- package/dist/types.d.ts +26 -0
- package/dist/value.d.ts +2 -0
- package/dist/value.js +6 -0
- package/package.json +16 -18
- package/dist/args.mjs +0 -3
- package/dist/array.mjs +0 -7
- package/dist/bigint.d.mts +0 -9
- package/dist/bigint.mjs +0 -12
- package/dist/buffer.d.mts +0 -18
- package/dist/buffer.mjs +0 -66
- package/dist/compare.mjs +0 -43
- package/dist/consts.mjs +0 -1
- package/dist/error.mjs +0 -3
- package/dist/function.mjs +0 -31
- package/dist/http.mjs +0 -83
- package/dist/id.d.mts +0 -8
- package/dist/id.mjs +0 -7
- package/dist/index.d.mts +0 -25
- package/dist/index.mjs +0 -23
- package/dist/interceptor.d.mts +0 -96
- package/dist/interceptor.mjs +0 -102
- package/dist/iterator.d.mts +0 -82
- package/dist/iterator.mjs +0 -151
- package/dist/object.d.mts +0 -64
- package/dist/object.mjs +0 -221
- package/dist/plugin.d.mts +0 -12
- package/dist/plugin.mjs +0 -58
- package/dist/promise.d.mts +0 -17
- package/dist/promise.mjs +0 -23
- package/dist/proxy.d.mts +0 -15
- package/dist/proxy.mjs +0 -15
- package/dist/queue.d.mts +0 -16
- package/dist/queue.mjs +0 -74
- package/dist/signal.d.mts +0 -13
- package/dist/signal.mjs +0 -74
- package/dist/stream.d.mts +0 -54
- package/dist/stream.mjs +0 -138
- package/dist/tracing.d.mts +0 -73
- package/dist/tracing.mjs +0 -89
- package/dist/types.d.mts +0 -32
- package/dist/value.d.mts +0 -8
- package/dist/value.mjs +0 -6
- /package/dist/{args.d.mts → args.d.ts} +0 -0
- /package/dist/{array.d.mts → array.d.ts} +0 -0
- /package/dist/{compare.d.mts → compare.d.ts} +0 -0
- /package/dist/{consts.d.mts → consts.d.ts} +0 -0
- /package/dist/{error.d.mts → error.d.ts} +0 -0
- /package/dist/{types.mjs → types.js} +0 -0
package/dist/promise.mjs
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
export function promiseWithResolvers() {
|
|
3
|
-
const result = {};
|
|
4
|
-
result.promise = new Promise((resolve, reject) => {
|
|
5
|
-
result.resolve = resolve;
|
|
6
|
-
result.reject = reject;
|
|
7
|
-
});
|
|
8
|
-
return result;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function captureAsyncContext() {
|
|
12
|
-
const { promise, resolve } = promiseWithResolvers();
|
|
13
|
-
const result = promise.then((callback) => callback());
|
|
14
|
-
let called = false;
|
|
15
|
-
return (callback) => {
|
|
16
|
-
if (called) {
|
|
17
|
-
return Promise.reject(new Error("A captured async context can be run only once."));
|
|
18
|
-
}
|
|
19
|
-
called = true;
|
|
20
|
-
resolve(callback);
|
|
21
|
-
return result;
|
|
22
|
-
};
|
|
23
|
-
}
|
package/dist/proxy.d.mts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { Value } from "./value.mjs";
|
|
2
|
-
/**
|
|
3
|
-
* Creates a proxy that overlays a `partial` object on top of a `target`.
|
|
4
|
-
*
|
|
5
|
-
* - Properties from `partial` take precedence.
|
|
6
|
-
* - Properties not present in `partial` fall back to the resolved `target`.
|
|
7
|
-
* - Methods are bound to the proxy to ensure a consistent `this` context.
|
|
8
|
-
*
|
|
9
|
-
* Useful for overriding specific properties of an object while delegating
|
|
10
|
-
* all other access to the original target without needing to know its full structure.
|
|
11
|
-
*/
|
|
12
|
-
export declare function override<
|
|
13
|
-
T extends object,
|
|
14
|
-
U extends object
|
|
15
|
-
>(target: Value<T>, partial: U): U & Omit<T, keyof U>;
|
package/dist/proxy.mjs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { getOrBind } from "@standard-server/shared";
|
|
2
|
-
import { value } from "./value.mjs";
|
|
3
|
-
|
|
4
|
-
export function override(target, partial) {
|
|
5
|
-
const proxy = new Proxy(typeof target === "function" ? partial : target, {
|
|
6
|
-
get(_, prop) {
|
|
7
|
-
const targetValue = prop in partial ? partial : value(target);
|
|
8
|
-
return getOrBind(targetValue, prop);
|
|
9
|
-
},
|
|
10
|
-
has(_, prop) {
|
|
11
|
-
return Reflect.has(partial, prop) || Reflect.has(value(target), prop);
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
return proxy;
|
|
15
|
-
}
|
package/dist/queue.d.mts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export interface AsyncIdQueueCloseOptions {
|
|
2
|
-
id?: string | undefined;
|
|
3
|
-
reason?: unknown;
|
|
4
|
-
}
|
|
5
|
-
export declare class AsyncIdQueue<T> {
|
|
6
|
-
private readonly openIds;
|
|
7
|
-
private readonly queues;
|
|
8
|
-
private readonly waiters;
|
|
9
|
-
get size(): number;
|
|
10
|
-
open(id: string): void;
|
|
11
|
-
isOpen(id: string): boolean;
|
|
12
|
-
push(id: string, item: T): void;
|
|
13
|
-
pull(id: string): Promise<T>;
|
|
14
|
-
close({ id, reason }?: AsyncIdQueueCloseOptions): void;
|
|
15
|
-
private assertOpen;
|
|
16
|
-
}
|
package/dist/queue.mjs
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { AbortError } from "@standard-server/shared";
|
|
2
|
-
export class AsyncIdQueue {
|
|
3
|
-
openIds = new Set();
|
|
4
|
-
queues = new Map();
|
|
5
|
-
waiters = new Map();
|
|
6
|
-
get size() {
|
|
7
|
-
return this.openIds.size;
|
|
8
|
-
}
|
|
9
|
-
open(id) {
|
|
10
|
-
this.openIds.add(id);
|
|
11
|
-
}
|
|
12
|
-
isOpen(id) {
|
|
13
|
-
return this.openIds.has(id);
|
|
14
|
-
}
|
|
15
|
-
push(id, item) {
|
|
16
|
-
this.assertOpen(id);
|
|
17
|
-
const pending = this.waiters.get(id);
|
|
18
|
-
if (pending?.length) {
|
|
19
|
-
pending.shift()[0](item);
|
|
20
|
-
if (pending.length === 0) {
|
|
21
|
-
this.waiters.delete(id);
|
|
22
|
-
}
|
|
23
|
-
} else {
|
|
24
|
-
const items = this.queues.get(id);
|
|
25
|
-
if (items) {
|
|
26
|
-
items.push(item);
|
|
27
|
-
} else {
|
|
28
|
-
this.queues.set(id, [item]);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
async pull(id) {
|
|
33
|
-
this.assertOpen(id);
|
|
34
|
-
const items = this.queues.get(id);
|
|
35
|
-
if (items?.length) {
|
|
36
|
-
const item = items.shift();
|
|
37
|
-
if (items.length === 0) {
|
|
38
|
-
this.queues.delete(id);
|
|
39
|
-
}
|
|
40
|
-
return item;
|
|
41
|
-
}
|
|
42
|
-
return new Promise((resolve, reject) => {
|
|
43
|
-
const waitingPulls = this.waiters.get(id);
|
|
44
|
-
const pending = [resolve, reject];
|
|
45
|
-
if (waitingPulls) {
|
|
46
|
-
waitingPulls.push(pending);
|
|
47
|
-
} else {
|
|
48
|
-
this.waiters.set(id, [pending]);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
close({ id, reason } = {}) {
|
|
53
|
-
if (id === undefined) {
|
|
54
|
-
this.waiters.forEach((pendingPulls, id) => {
|
|
55
|
-
const error = reason ?? new AbortError(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`);
|
|
56
|
-
pendingPulls.forEach(([, reject]) => reject(error));
|
|
57
|
-
});
|
|
58
|
-
this.waiters.clear();
|
|
59
|
-
this.openIds.clear();
|
|
60
|
-
this.queues.clear();
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
const error = reason ?? new AbortError(`[AsyncIdQueue] Queue[${id}] was closed or aborted while waiting for pulling.`);
|
|
64
|
-
this.waiters.get(id)?.forEach(([, reject]) => reject(error));
|
|
65
|
-
this.waiters.delete(id);
|
|
66
|
-
this.openIds.delete(id);
|
|
67
|
-
this.queues.delete(id);
|
|
68
|
-
}
|
|
69
|
-
assertOpen(id) {
|
|
70
|
-
if (!this.isOpen(id)) {
|
|
71
|
-
throw new Error(`[AsyncIdQueue] Cannot access queue[${id}] because it is not open or aborted.`);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
package/dist/signal.d.mts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns a signal that aborts only after all provided signals are aborted.
|
|
3
|
-
*/
|
|
4
|
-
export declare function allAbortSignal(signals: readonly (AbortSignal | undefined)[]): AbortSignal | undefined;
|
|
5
|
-
/**
|
|
6
|
-
* Returns a signal that aborts as soon as any of the provided signals aborts,
|
|
7
|
-
* with the same abort reason.
|
|
8
|
-
*
|
|
9
|
-
* Prefers the native `AbortSignal.any` when available, and falls back to a manual
|
|
10
|
-
* implementation otherwise, including in runtimes without the `AbortSignal` global.
|
|
11
|
-
*/
|
|
12
|
-
export declare function anyAbortSignal(signals: readonly (AbortSignal | undefined)[]): AbortSignal | undefined;
|
|
13
|
-
export declare function runWithSignal<T>(signal: AbortSignal | undefined, fn: () => Promise<T>): Promise<T>;
|
package/dist/signal.mjs
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { throwIfAborted } from "@standard-server/shared";
|
|
2
|
-
import { promiseWithResolvers } from "./promise.mjs";
|
|
3
|
-
|
|
4
|
-
export function allAbortSignal(signals) {
|
|
5
|
-
const realSignals = signals.filter((signal) => signal !== undefined);
|
|
6
|
-
if (realSignals.length === 0 || realSignals.length !== signals.length) {
|
|
7
|
-
return undefined;
|
|
8
|
-
}
|
|
9
|
-
const controller = new AbortController();
|
|
10
|
-
const abortIfAllAborted = () => {
|
|
11
|
-
if (realSignals.every((signal) => signal.aborted)) {
|
|
12
|
-
controller.abort();
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
abortIfAllAborted();
|
|
16
|
-
for (const signal of realSignals) {
|
|
17
|
-
signal.addEventListener("abort", () => {
|
|
18
|
-
abortIfAllAborted();
|
|
19
|
-
}, {
|
|
20
|
-
once: true,
|
|
21
|
-
signal: controller.signal
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
return controller.signal;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function anyAbortSignal(signals) {
|
|
28
|
-
const realSignals = signals.filter((signal) => signal !== undefined);
|
|
29
|
-
if (realSignals.length === 0) {
|
|
30
|
-
return undefined;
|
|
31
|
-
}
|
|
32
|
-
if (realSignals.length === 1) {
|
|
33
|
-
return realSignals[0];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (typeof AbortSignal !== "undefined" && typeof AbortSignal.any === "function") {
|
|
37
|
-
|
|
38
|
-
return AbortSignal.any(realSignals);
|
|
39
|
-
}
|
|
40
|
-
const controller = new AbortController();
|
|
41
|
-
for (const signal of realSignals) {
|
|
42
|
-
if (signal.aborted) {
|
|
43
|
-
controller.abort(signal.reason);
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
signal.addEventListener("abort", () => {
|
|
47
|
-
controller.abort(signal.reason);
|
|
48
|
-
}, {
|
|
49
|
-
once: true,
|
|
50
|
-
signal: controller.signal
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
return controller.signal;
|
|
54
|
-
}
|
|
55
|
-
export async function runWithSignal(signal, fn) {
|
|
56
|
-
if (!signal) {
|
|
57
|
-
return fn();
|
|
58
|
-
}
|
|
59
|
-
throwIfAborted(signal);
|
|
60
|
-
const { promise, reject, resolve } = promiseWithResolvers();
|
|
61
|
-
let abortListener;
|
|
62
|
-
signal.addEventListener("abort", abortListener = () => {
|
|
63
|
-
reject(signal.reason);
|
|
64
|
-
abortListener = undefined;
|
|
65
|
-
});
|
|
66
|
-
try {
|
|
67
|
-
fn().then(resolve, reject);
|
|
68
|
-
return await promise;
|
|
69
|
-
} finally {
|
|
70
|
-
if (abortListener) {
|
|
71
|
-
signal.removeEventListener("abort", abortListener);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
package/dist/stream.d.mts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { Promisable } from "type-fest";
|
|
2
|
-
import type { StartSpanOptions } from "./tracing.mjs";
|
|
3
|
-
import type { ThrowableError } from "./types.mjs";
|
|
4
|
-
import { AsyncIteratorClass } from "@standard-server/shared";
|
|
5
|
-
export declare function replicateReadableStream<T>(stream: ReadableStream<T>, count: number): ReadableStream<T>[];
|
|
6
|
-
export type ReadableStreamReadResult<T> = {
|
|
7
|
-
done: false;
|
|
8
|
-
value: T;
|
|
9
|
-
} | {
|
|
10
|
-
done: true;
|
|
11
|
-
value?: T | undefined;
|
|
12
|
-
};
|
|
13
|
-
export interface WrapReadableStreamOptions<
|
|
14
|
-
T,
|
|
15
|
-
TMapped
|
|
16
|
-
> {
|
|
17
|
-
/**
|
|
18
|
-
* Any call to the original stream reader will be executed inside this function.
|
|
19
|
-
* Useful when you want execution to happen within a specific context,
|
|
20
|
-
* such as AsyncLocalStorage.
|
|
21
|
-
*/
|
|
22
|
-
runWith?: (<T>(run: () => Promise<T>) => Promisable<T>) | undefined;
|
|
23
|
-
mapResult?: ((result: ReadableStreamReadResult<T>) => Promisable<ReadableStreamReadResult<TMapped>>) | undefined;
|
|
24
|
-
mapError?: ((error: ThrowableError) => Promisable<ThrowableError>) | undefined;
|
|
25
|
-
onError?: ((error: ThrowableError) => Promisable<void>) | undefined;
|
|
26
|
-
/**
|
|
27
|
-
* Guaranteed to execute exactly once after the stream finishes or is cancelled.
|
|
28
|
-
*/
|
|
29
|
-
onFinish?: (() => Promisable<void>) | undefined;
|
|
30
|
-
}
|
|
31
|
-
export declare function wrapReadableStream<
|
|
32
|
-
T,
|
|
33
|
-
TMapped = T
|
|
34
|
-
>(stream: ReadableStream<T>, { runWith, mapResult, mapError, onError, onFinish }: WrapReadableStreamOptions<T, TMapped>): NoInfer<ReadableStream<TMapped>>;
|
|
35
|
-
export declare function traceReadableStream<T>(options: StartSpanOptions | string, stream: ReadableStream<T>): ReadableStream<T>;
|
|
36
|
-
/**
|
|
37
|
-
* Converts a {@link ReadableStream} into an {@link AsyncIteratorClass}.
|
|
38
|
-
*
|
|
39
|
-
* @see {@link https://orpc.dev/docs/integrations/ai-sdk | AI SDK Integration}
|
|
40
|
-
*/
|
|
41
|
-
export declare function streamToAsyncIteratorObject<T>(stream: ReadableStream<T>, { signal }?: {
|
|
42
|
-
signal?: AbortSignal | undefined;
|
|
43
|
-
}): AsyncIteratorClass<T>;
|
|
44
|
-
/**
|
|
45
|
-
* Converts an `AsyncIterator` into a `ReadableStream`.
|
|
46
|
-
*/
|
|
47
|
-
export declare function asyncIteratorToStream<T>(iterator: AsyncIterator<T>): ReadableStream<T>;
|
|
48
|
-
/**
|
|
49
|
-
* Converts an `AsyncIterator` into a `ReadableStream`, ensuring that
|
|
50
|
-
* all emitted object values are *unproxied* before enqueuing.
|
|
51
|
-
*
|
|
52
|
-
* @see {@link https://orpc.dev/docs/integrations/ai-sdk | AI SDK Integration}
|
|
53
|
-
*/
|
|
54
|
-
export declare function asyncIteratorToUnproxiedDataStream<T>(iterator: AsyncIterator<T>): ReadableStream<T>;
|
package/dist/stream.mjs
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { AsyncIteratorClass } from "@standard-server/shared";
|
|
2
|
-
import { once } from "./function.mjs";
|
|
3
|
-
import { isPlainObject } from "./object.mjs";
|
|
4
|
-
import { promiseWithResolvers } from "./promise.mjs";
|
|
5
|
-
import { recordSpanError, runInSpanContext, startSpan } from "./tracing.mjs";
|
|
6
|
-
export function replicateReadableStream(stream, count) {
|
|
7
|
-
if (count <= 0) {
|
|
8
|
-
return [];
|
|
9
|
-
}
|
|
10
|
-
const replicated = [];
|
|
11
|
-
let pending = stream;
|
|
12
|
-
for (let index = 0; index < count - 1; index++) {
|
|
13
|
-
const [replica, remainder] = pending.tee();
|
|
14
|
-
replicated.push(replica);
|
|
15
|
-
pending = remainder;
|
|
16
|
-
}
|
|
17
|
-
replicated.push(pending);
|
|
18
|
-
return replicated;
|
|
19
|
-
}
|
|
20
|
-
export function wrapReadableStream(stream, { runWith, mapResult, mapError, onError, onFinish }) {
|
|
21
|
-
runWith ??= (run) => run();
|
|
22
|
-
const reader = once(() => stream.getReader());
|
|
23
|
-
const finish = once(async () => onFinish?.());
|
|
24
|
-
|
|
25
|
-
return new ReadableStream({
|
|
26
|
-
async pull(controller) {
|
|
27
|
-
let result;
|
|
28
|
-
try {
|
|
29
|
-
const readResult = await runWith(() => reader().read());
|
|
30
|
-
result = mapResult ? await mapResult(readResult) : readResult;
|
|
31
|
-
} catch (error) {
|
|
32
|
-
try {
|
|
33
|
-
await onError?.(error);
|
|
34
|
-
controller.error(mapError ? await mapError(error) : error);
|
|
35
|
-
} finally {
|
|
36
|
-
await finish();
|
|
37
|
-
}
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
if (result.done) {
|
|
41
|
-
controller.close();
|
|
42
|
-
await finish();
|
|
43
|
-
} else {
|
|
44
|
-
controller.enqueue(result.value);
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
async cancel(reason) {
|
|
48
|
-
try {
|
|
49
|
-
try {
|
|
50
|
-
await runWith(() => reader().cancel(reason));
|
|
51
|
-
} catch (error) {
|
|
52
|
-
await onError?.(error);
|
|
53
|
-
throw error;
|
|
54
|
-
}
|
|
55
|
-
} finally {
|
|
56
|
-
await finish();
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}, { highWaterMark: 0 });
|
|
60
|
-
}
|
|
61
|
-
export function traceReadableStream(options, stream) {
|
|
62
|
-
const getSpan = once(() => startSpan(options));
|
|
63
|
-
return wrapReadableStream(stream, {
|
|
64
|
-
runWith: (run) => runInSpanContext(getSpan(), run),
|
|
65
|
-
mapResult(result) {
|
|
66
|
-
getSpan()?.addEvent(result.done ? "closed" : "enqueued");
|
|
67
|
-
return result;
|
|
68
|
-
},
|
|
69
|
-
onError(error) {
|
|
70
|
-
recordSpanError(getSpan(), error);
|
|
71
|
-
},
|
|
72
|
-
onFinish() {
|
|
73
|
-
getSpan()?.end();
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function streamToAsyncIteratorObject(stream, { signal } = {}) {
|
|
79
|
-
const reader = stream.getReader();
|
|
80
|
-
let cancelledBySignal = false;
|
|
81
|
-
return new AsyncIteratorClass(async () => {
|
|
82
|
-
if (signal?.aborted) {
|
|
83
|
-
cancelledBySignal = true;
|
|
84
|
-
throw signal.reason;
|
|
85
|
-
}
|
|
86
|
-
if (!signal) {
|
|
87
|
-
return reader.read();
|
|
88
|
-
}
|
|
89
|
-
const { promise, reject } = promiseWithResolvers();
|
|
90
|
-
const onAbort = () => reject(signal.reason);
|
|
91
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
92
|
-
try {
|
|
93
|
-
return await Promise.race([reader.read(), promise.catch(async (reason) => {
|
|
94
|
-
cancelledBySignal = true;
|
|
95
|
-
throw reason;
|
|
96
|
-
})]);
|
|
97
|
-
} finally {
|
|
98
|
-
signal.removeEventListener("abort", onAbort);
|
|
99
|
-
}
|
|
100
|
-
}, async ({ kind, error }) => {
|
|
101
|
-
if (kind === "cancelled" || kind === "error" && cancelledBySignal) {
|
|
102
|
-
await reader.cancel(error);
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function asyncIteratorToStream(iterator) {
|
|
108
|
-
return new ReadableStream({
|
|
109
|
-
async pull(controller) {
|
|
110
|
-
const { done, value } = await iterator.next();
|
|
111
|
-
if (done) {
|
|
112
|
-
controller.close();
|
|
113
|
-
} else {
|
|
114
|
-
controller.enqueue(value);
|
|
115
|
-
}
|
|
116
|
-
},
|
|
117
|
-
async cancel() {
|
|
118
|
-
await iterator.return?.();
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export function asyncIteratorToUnproxiedDataStream(iterator) {
|
|
124
|
-
return new ReadableStream({
|
|
125
|
-
async pull(controller) {
|
|
126
|
-
const { done, value } = await iterator.next();
|
|
127
|
-
if (done) {
|
|
128
|
-
controller.close();
|
|
129
|
-
} else {
|
|
130
|
-
const unproxied = isPlainObject(value) ? { ...value } : Array.isArray(value) ? value.map((i) => i) : value;
|
|
131
|
-
controller.enqueue(unproxied);
|
|
132
|
-
}
|
|
133
|
-
},
|
|
134
|
-
async cancel() {
|
|
135
|
-
await iterator.return?.();
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
}
|
package/dist/tracing.d.mts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import type { Promisable } from "type-fest";
|
|
2
|
-
export type TracingAttributeValue = string | number | boolean | string[] | number[] | boolean[];
|
|
3
|
-
export interface TracingException {
|
|
4
|
-
name?: string;
|
|
5
|
-
message: string;
|
|
6
|
-
stack?: string;
|
|
7
|
-
code?: string | number;
|
|
8
|
-
}
|
|
9
|
-
export type TracingHeaders = Record<string, string | string[] | undefined>;
|
|
10
|
-
export type TracingExceptionLevel = "error" | "info";
|
|
11
|
-
export interface TracingSpan {
|
|
12
|
-
setAttribute(key: string, value: TracingAttributeValue): void;
|
|
13
|
-
/**
|
|
14
|
-
* Renames the span. Backends that cannot rename spans ignore it.
|
|
15
|
-
*/
|
|
16
|
-
updateName(name: string): void;
|
|
17
|
-
/**
|
|
18
|
-
* Adds an event to the span. Backends without span events ignore it.
|
|
19
|
-
*/
|
|
20
|
-
addEvent(name: string): void;
|
|
21
|
-
/**
|
|
22
|
-
* Records an exception on the span. The `error` level also marks the span as failed.
|
|
23
|
-
*/
|
|
24
|
-
recordException(level: TracingExceptionLevel, exception: TracingException): void;
|
|
25
|
-
end(): void;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* The tracing backend oRPC records spans with, see `@orpc/opentelemetry` and `@orpc/cloudflare`.
|
|
29
|
-
*
|
|
30
|
-
* A `parent` defaults to the active span of the backend when omitted.
|
|
31
|
-
*/
|
|
32
|
-
export interface Tracer {
|
|
33
|
-
/**
|
|
34
|
-
* Starts a span without making it active. The caller must end it.
|
|
35
|
-
*/
|
|
36
|
-
startSpan(name: string, parent?: TracingSpan): TracingSpan;
|
|
37
|
-
/**
|
|
38
|
-
* Starts a span, makes it active while `fn` runs, and passes it to `fn`. The caller must end it.
|
|
39
|
-
*/
|
|
40
|
-
startActiveSpan<T>(name: string, parent: TracingSpan | undefined, fn: (span: TracingSpan) => Promise<T>): Promise<T>;
|
|
41
|
-
getActiveSpan(): TracingSpan | undefined;
|
|
42
|
-
/**
|
|
43
|
-
* Runs `fn` with `span` as the active span. Backends that cannot activate a span run `fn` directly.
|
|
44
|
-
*/
|
|
45
|
-
withActiveSpan<T>(span: TracingSpan, fn: () => Promisable<T>): Promisable<T>;
|
|
46
|
-
/**
|
|
47
|
-
* Writes the trace context of `span` into `headers`. Absent when the backend does not propagate context.
|
|
48
|
-
*/
|
|
49
|
-
inject?(span: TracingSpan, headers: TracingHeaders): void;
|
|
50
|
-
/**
|
|
51
|
-
* Reads the remote parent from `headers`. Absent when the backend does not propagate context.
|
|
52
|
-
*/
|
|
53
|
-
extract?(headers: TracingHeaders): TracingSpan | undefined;
|
|
54
|
-
}
|
|
55
|
-
export declare function setTracer(tracer: Tracer | undefined): void;
|
|
56
|
-
export declare function getTracer(): Tracer | undefined;
|
|
57
|
-
export interface StartSpanOptions {
|
|
58
|
-
/**
|
|
59
|
-
* The name of the span to create.
|
|
60
|
-
*/
|
|
61
|
-
name: string;
|
|
62
|
-
/**
|
|
63
|
-
* The span to use as parent. Defaults to the active span of the backend.
|
|
64
|
-
*/
|
|
65
|
-
parent?: TracingSpan | undefined;
|
|
66
|
-
}
|
|
67
|
-
export declare function startSpan(options: StartSpanOptions | string): TracingSpan | undefined;
|
|
68
|
-
export declare function recordSpanError(span: TracingSpan | undefined, error: unknown): void;
|
|
69
|
-
export declare function setSpanAttributeIfDefined(span: TracingSpan | undefined, key: string, value: TracingAttributeValue | undefined): void;
|
|
70
|
-
export declare function toTracingException(error: unknown): TracingException;
|
|
71
|
-
export declare function toSpanAttributeValue(data: unknown): string;
|
|
72
|
-
export declare function runWithSpan<T>(options: string | StartSpanOptions, fn: (span?: TracingSpan) => Promisable<T>): Promise<T>;
|
|
73
|
-
export declare function runInSpanContext<T>(span: TracingSpan | undefined, fn: () => Promisable<T>): Promisable<T>;
|
package/dist/tracing.mjs
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { isAbortError } from "./error.mjs";
|
|
2
|
-
const TRACER_SYMBOL = /* @__PURE__ */ Symbol.for("ORPC_TRACER");
|
|
3
|
-
export function setTracer(tracer) {
|
|
4
|
-
globalThis[TRACER_SYMBOL] = tracer;
|
|
5
|
-
}
|
|
6
|
-
export function getTracer() {
|
|
7
|
-
return globalThis[TRACER_SYMBOL];
|
|
8
|
-
}
|
|
9
|
-
export function startSpan(options) {
|
|
10
|
-
const tracer = getTracer();
|
|
11
|
-
if (!tracer) {
|
|
12
|
-
return undefined;
|
|
13
|
-
}
|
|
14
|
-
if (typeof options === "string") {
|
|
15
|
-
return tracer.startSpan(options);
|
|
16
|
-
}
|
|
17
|
-
return tracer.startSpan(options.name, options.parent);
|
|
18
|
-
}
|
|
19
|
-
export function recordSpanError(span, error) {
|
|
20
|
-
if (!span) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
span.recordException(isAbortError(error) ? "info" : "error", toTracingException(error));
|
|
25
|
-
}
|
|
26
|
-
export function setSpanAttributeIfDefined(span, key, value) {
|
|
27
|
-
if (!span || value === undefined) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
span.setAttribute(key, value);
|
|
31
|
-
}
|
|
32
|
-
export function toTracingException(error) {
|
|
33
|
-
if (error instanceof Error) {
|
|
34
|
-
const exception = {
|
|
35
|
-
message: error.message,
|
|
36
|
-
name: error.name,
|
|
37
|
-
stack: error.stack
|
|
38
|
-
};
|
|
39
|
-
if ("code" in error && (typeof error.code === "string" || typeof error.code === "number")) {
|
|
40
|
-
exception.code = error.code;
|
|
41
|
-
}
|
|
42
|
-
return exception;
|
|
43
|
-
}
|
|
44
|
-
return { message: String(error) };
|
|
45
|
-
}
|
|
46
|
-
export function toSpanAttributeValue(data) {
|
|
47
|
-
if (data === undefined) {
|
|
48
|
-
return "undefined";
|
|
49
|
-
}
|
|
50
|
-
try {
|
|
51
|
-
|
|
52
|
-
return JSON.stringify(data, (_, value) => {
|
|
53
|
-
if (typeof value === "bigint") {
|
|
54
|
-
return value.toString();
|
|
55
|
-
}
|
|
56
|
-
if (value instanceof Map || value instanceof Set) {
|
|
57
|
-
return Array.from(value);
|
|
58
|
-
}
|
|
59
|
-
return value;
|
|
60
|
-
});
|
|
61
|
-
} catch {
|
|
62
|
-
return String(data);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
export async function runWithSpan(options, fn) {
|
|
66
|
-
const tracer = getTracer();
|
|
67
|
-
if (!tracer) {
|
|
68
|
-
return fn();
|
|
69
|
-
}
|
|
70
|
-
const name = typeof options === "string" ? options : options.name;
|
|
71
|
-
const parent = typeof options === "string" ? undefined : options.parent;
|
|
72
|
-
return tracer.startActiveSpan(name, parent, async (span) => {
|
|
73
|
-
try {
|
|
74
|
-
return await fn(span);
|
|
75
|
-
} catch (e) {
|
|
76
|
-
recordSpanError(span, e);
|
|
77
|
-
throw e;
|
|
78
|
-
} finally {
|
|
79
|
-
span.end();
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
export function runInSpanContext(span, fn) {
|
|
84
|
-
const tracer = getTracer();
|
|
85
|
-
if (!span || !tracer) {
|
|
86
|
-
return fn();
|
|
87
|
-
}
|
|
88
|
-
return tracer.withActiveSpan(span, fn);
|
|
89
|
-
}
|
package/dist/types.d.mts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
export type IntersectPick<
|
|
2
|
-
T,
|
|
3
|
-
U
|
|
4
|
-
> = Pick<T, keyof T & keyof U>;
|
|
5
|
-
/**
|
|
6
|
-
* Like `Omit`, but distributes over unions and keeps known keys beside index signatures.
|
|
7
|
-
*/
|
|
8
|
-
export type DistributiveOmit<
|
|
9
|
-
T,
|
|
10
|
-
K extends PropertyKey
|
|
11
|
-
> = { [P in keyof T as P extends K ? never : P]: T[P] };
|
|
12
|
-
/**
|
|
13
|
-
* Remove protected/private properties/methods
|
|
14
|
-
*/
|
|
15
|
-
export type Public<T> = Pick<T, keyof T>;
|
|
16
|
-
export type PromiseWithError<
|
|
17
|
-
T,
|
|
18
|
-
TError
|
|
19
|
-
> = Promise<T> & {
|
|
20
|
-
__error?: {
|
|
21
|
-
type: TError;
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* The place where you can config the orpc types.
|
|
26
|
-
*
|
|
27
|
-
* - `ThrowableError` the error type that represent throwable errors should be `Error` or `null | undefined | {}` if you want more strict.
|
|
28
|
-
*/
|
|
29
|
-
export interface Registry {}
|
|
30
|
-
export type ThrowableError = Registry extends {
|
|
31
|
-
ThrowableError: infer T;
|
|
32
|
-
} ? T : Error;
|
package/dist/value.d.mts
DELETED
package/dist/value.mjs
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|