@orpc/shared 2.0.0-beta.46 → 2.0.0-beta.48
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 +6 -4
- package/dist/args.d.mts +2 -0
- package/dist/args.mjs +3 -0
- package/dist/array.d.mts +2 -0
- package/dist/array.mjs +7 -0
- package/dist/bigint.d.mts +9 -0
- package/dist/bigint.mjs +12 -0
- package/dist/buffer.d.mts +18 -0
- package/dist/buffer.mjs +66 -0
- package/dist/compare.d.mts +1 -0
- package/dist/compare.mjs +43 -0
- package/dist/consts.d.mts +1 -0
- package/dist/consts.mjs +1 -0
- package/dist/error.d.mts +1 -0
- package/dist/error.mjs +3 -0
- package/dist/function.d.mts +12 -0
- package/dist/function.mjs +31 -0
- package/dist/http.d.mts +29 -0
- package/dist/http.mjs +83 -0
- package/dist/id.d.mts +8 -0
- package/dist/id.mjs +7 -0
- package/dist/index.d.mts +25 -492
- package/dist/index.mjs +23 -1117
- package/dist/interceptor.d.mts +96 -0
- package/dist/interceptor.mjs +102 -0
- package/dist/iterator.d.mts +82 -0
- package/dist/iterator.mjs +151 -0
- package/dist/object.d.mts +64 -0
- package/dist/object.mjs +221 -0
- package/dist/plugin.d.mts +12 -0
- package/dist/plugin.mjs +58 -0
- package/dist/promise.d.mts +17 -0
- package/dist/promise.mjs +23 -0
- package/dist/proxy.d.mts +15 -0
- package/dist/proxy.mjs +15 -0
- package/dist/queue.d.mts +16 -0
- package/dist/queue.mjs +74 -0
- package/dist/signal.d.mts +13 -0
- package/dist/signal.mjs +74 -0
- package/dist/stream.d.mts +54 -0
- package/dist/stream.mjs +138 -0
- package/dist/tracing.d.mts +73 -0
- package/dist/tracing.mjs +89 -0
- package/dist/types.d.mts +32 -0
- package/dist/types.mjs +1 -0
- package/dist/value.d.mts +8 -0
- package/dist/value.mjs +6 -0
- package/package.json +3 -4
- package/dist/index.d.ts +0 -492
package/dist/stream.mjs
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/value.d.mts
ADDED
package/dist/value.mjs
ADDED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.48",
|
|
5
5
|
"description": "Internal utilities shared across oRPC packages",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"funding": [
|
|
@@ -30,12 +30,11 @@
|
|
|
30
30
|
"dist"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@standard-server/shared": "~0.
|
|
34
|
-
"radash": "^12.1.1",
|
|
33
|
+
"@standard-server/shared": "~0.14.0",
|
|
35
34
|
"type-fest": "^5.10.0"
|
|
36
35
|
},
|
|
37
36
|
"devDependencies": {
|
|
38
|
-
"arktype": "^2.2.
|
|
37
|
+
"arktype": "^2.2.9",
|
|
39
38
|
"valibot": "^1.5.0",
|
|
40
39
|
"zod": "^4.6.7"
|
|
41
40
|
},
|