@orpc/client 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/adapters/fetch/index.d.mts +2 -48
- package/dist/adapters/fetch/index.mjs +2 -77
- package/dist/adapters/fetch/rpc-link.d.mts +13 -0
- package/dist/adapters/fetch/rpc-link.mjs +10 -0
- package/dist/adapters/fetch/transport.d.mts +31 -0
- package/dist/adapters/fetch/transport.mjs +65 -0
- package/dist/adapters/message-port/index.d.mts +3 -80
- package/dist/adapters/message-port/index.mjs +3 -101
- package/dist/adapters/message-port/message-port.d.mts +29 -0
- package/dist/adapters/message-port/message-port.mjs +43 -0
- package/dist/adapters/message-port/rpc-link.d.mts +14 -0
- package/dist/adapters/message-port/rpc-link.mjs +10 -0
- package/dist/adapters/message-port/transport.d.mts +35 -0
- package/dist/adapters/message-port/transport.mjs +47 -0
- package/dist/adapters/standard/codec.d.mts +14 -0
- package/dist/adapters/standard/codec.mjs +1 -0
- package/dist/adapters/standard/index.d.mts +5 -60
- package/dist/adapters/standard/index.mjs +4 -6
- package/dist/adapters/standard/link.d.mts +40 -0
- package/dist/adapters/standard/link.mjs +79 -0
- package/dist/adapters/standard/plugin.d.mts +32 -0
- package/dist/adapters/standard/plugin.mjs +16 -0
- package/dist/adapters/standard/rpc-link-codec.d.mts +55 -0
- package/dist/adapters/standard/rpc-link-codec.mjs +104 -0
- package/dist/adapters/standard/transport.d.mts +14 -0
- package/dist/adapters/standard/transport.mjs +1 -0
- package/dist/adapters/websocket/index.d.mts +2 -136
- package/dist/adapters/websocket/index.mjs +2 -131
- package/dist/adapters/websocket/rpc-link.d.mts +13 -0
- package/dist/adapters/websocket/rpc-link.mjs +10 -0
- package/dist/adapters/websocket/transport.d.mts +119 -0
- package/dist/adapters/websocket/transport.mjs +123 -0
- package/dist/async-iterator-object.d.mts +7 -0
- package/dist/async-iterator-object.mjs +30 -0
- package/dist/client-safe.d.mts +16 -0
- package/dist/client-safe.mjs +23 -0
- package/dist/client.d.mts +51 -0
- package/dist/client.mjs +32 -0
- package/dist/consts.d.mts +10 -0
- package/dist/consts.mjs +10 -0
- package/dist/dynamic-link.d.mts +13 -0
- package/dist/dynamic-link.mjs +12 -0
- package/dist/error-utils.d.mts +24 -0
- package/dist/error-utils.mjs +44 -0
- package/dist/error.d.mts +138 -0
- package/dist/error.mjs +105 -0
- package/dist/index.d.mts +42 -137
- package/dist/index.mjs +18 -142
- package/dist/plugins/batch.d.mts +150 -0
- package/dist/plugins/batch.mjs +339 -0
- package/dist/plugins/cookie-jar.d.mts +82 -0
- package/dist/plugins/cookie-jar.mjs +229 -0
- package/dist/plugins/dedupe.d.mts +58 -0
- package/dist/plugins/dedupe.mjs +199 -0
- package/dist/plugins/index.d.mts +28 -420
- package/dist/plugins/index.mjs +17 -971
- package/dist/plugins/request-compression.d.mts +44 -0
- package/dist/plugins/request-compression.mjs +150 -0
- package/dist/plugins/response-compression.d.mts +28 -0
- package/dist/plugins/response-compression.mjs +78 -0
- package/dist/plugins/retry-after.d.mts +41 -0
- package/dist/plugins/retry-after.mjs +71 -0
- package/dist/plugins/retry.d.mts +82 -0
- package/dist/plugins/retry.mjs +127 -0
- package/dist/plugins/timeout.d.mts +25 -0
- package/dist/plugins/timeout.mjs +36 -0
- package/dist/rpc-json-serializer.d.mts +99 -0
- package/dist/rpc-json-serializer.mjs +365 -0
- package/dist/rpc-serializer.d.mts +32 -0
- package/dist/rpc-serializer.mjs +100 -0
- package/dist/types.d.mts +93 -0
- package/dist/types.mjs +1 -0
- package/dist/utils.d.mts +46 -0
- package/dist/utils.mjs +53 -0
- package/package.json +5 -5
- package/dist/adapters/fetch/index.d.ts +0 -48
- package/dist/adapters/message-port/index.d.ts +0 -80
- package/dist/adapters/standard/index.d.ts +0 -60
- package/dist/adapters/websocket/index.d.ts +0 -136
- package/dist/index.d.ts +0 -144
- package/dist/plugins/index.d.ts +0 -420
- package/dist/shared/client.BoHJeq15.d.mts +0 -233
- package/dist/shared/client.BoHJeq15.d.ts +0 -233
- package/dist/shared/client.CKQeJJ2U.mjs +0 -172
- package/dist/shared/client.DqIWeNhQ.d.mts +0 -131
- package/dist/shared/client.DqIWeNhQ.d.ts +0 -131
- package/dist/shared/client.JBJwBhR8.d.ts +0 -90
- package/dist/shared/client.LF04eOvL.mjs +0 -115
- package/dist/shared/client._wG5INmC.mjs +0 -505
- package/dist/shared/client.euYjjcFz.d.mts +0 -90
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { Promisable, OrderablePlugin, Interceptor } from '@orpc/shared';
|
|
2
|
-
import { StandardRequest, StandardLazyResponse } from '@standard-server/core';
|
|
3
|
-
import { C as ClientContext, a as ClientOptions, A as AnyORPCError, b as ClientLink } from './client.BoHJeq15.js';
|
|
4
|
-
|
|
5
|
-
type StandardLinkCodecDecodedResponse = {
|
|
6
|
-
kind: 'output';
|
|
7
|
-
output: unknown;
|
|
8
|
-
} | {
|
|
9
|
-
kind: 'error';
|
|
10
|
-
error: AnyORPCError;
|
|
11
|
-
};
|
|
12
|
-
interface StandardLinkCodec<T extends ClientContext> {
|
|
13
|
-
encodeInput(input: unknown, path: string[], options: ClientOptions<T>): Promisable<StandardRequest>;
|
|
14
|
-
decodeResponse(response: StandardLazyResponse, path: string[], options: ClientOptions<T>): Promisable<StandardLinkCodecDecodedResponse>;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface StandardLinkPlugin<T extends ClientContext> extends OrderablePlugin {
|
|
18
|
-
/**
|
|
19
|
-
* Initializes the plugin and returns new link options.
|
|
20
|
-
* Called once per plugin instance during composition.
|
|
21
|
-
*
|
|
22
|
-
* This method allows plugins to wrap, extend, or transform link options
|
|
23
|
-
* such as interceptors, or configuration.
|
|
24
|
-
*
|
|
25
|
-
* @param options - The current link options from previous plugins or base configuration
|
|
26
|
-
* @returns Transformed link options with plugin's modifications applied
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```ts
|
|
30
|
-
* init(options) {
|
|
31
|
-
* return {
|
|
32
|
-
* ...options,
|
|
33
|
-
* interceptors: [...(options.interceptors || []), myInterceptor]
|
|
34
|
-
* }
|
|
35
|
-
* }
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
init?(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Handles the transport layer for sending requests and receiving responses.
|
|
43
|
-
*
|
|
44
|
-
* Implementations are responsible for the actual network communication,
|
|
45
|
-
* such as HTTP fetch, WebSocket, or other transport mechanisms.
|
|
46
|
-
*/
|
|
47
|
-
interface StandardLinkTransport<T extends ClientContext> {
|
|
48
|
-
/**
|
|
49
|
-
* @throws Transport-level errors (network failures, timeouts, etc.)
|
|
50
|
-
*/
|
|
51
|
-
send(request: StandardRequest, path: string[], options: ClientOptions<T>): Promise<StandardLazyResponse>;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
55
|
-
path: string[];
|
|
56
|
-
input: unknown;
|
|
57
|
-
}
|
|
58
|
-
type StandardLinkInterceptor<T extends ClientContext> = Interceptor<StandardLinkInterceptorOptions<T>, Promise<unknown>>;
|
|
59
|
-
interface StandardLinkTransportInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
60
|
-
path: string[];
|
|
61
|
-
request: StandardRequest;
|
|
62
|
-
}
|
|
63
|
-
type StandardLinkTransportInterceptor<T extends ClientContext> = Interceptor<StandardLinkTransportInterceptorOptions<T>, Promise<StandardLazyResponse>>;
|
|
64
|
-
interface StandardLinkOptions<T extends ClientContext> {
|
|
65
|
-
/**
|
|
66
|
-
* Interceptors that execute around the entire call, including transport and codec.
|
|
67
|
-
* Useful for error handling, logging, metrics, ...
|
|
68
|
-
*/
|
|
69
|
-
interceptors?: StandardLinkInterceptor<T>[] | undefined;
|
|
70
|
-
/**
|
|
71
|
-
* Interceptors that execute around the transport layer, after encoding and before decoding.
|
|
72
|
-
* Useful for modifying the request or response, adding transport-level logging, ...
|
|
73
|
-
*/
|
|
74
|
-
transportInterceptors?: StandardLinkTransportInterceptor<T>[] | undefined;
|
|
75
|
-
plugins?: StandardLinkPlugin<T>[] | undefined;
|
|
76
|
-
}
|
|
77
|
-
declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
78
|
-
private readonly codec;
|
|
79
|
-
private readonly transport;
|
|
80
|
-
private readonly interceptors;
|
|
81
|
-
private readonly transportInterceptors;
|
|
82
|
-
constructor(codec: StandardLinkCodec<T>, transport: StandardLinkTransport<T>, options?: StandardLinkOptions<T>);
|
|
83
|
-
/**
|
|
84
|
-
* @throws ORPCError, MalformedResponseError, transport-level errors (network failures, timeouts, etc.)
|
|
85
|
-
*/
|
|
86
|
-
call(path: string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export { StandardLink as a };
|
|
90
|
-
export type { StandardLinkTransport as S, StandardLinkOptions as b, StandardLinkPlugin as c, StandardLinkTransportInterceptorOptions as d, StandardLinkInterceptorOptions as e, StandardLinkCodec as f, StandardLinkCodecDecodedResponse as g, StandardLinkInterceptor as h, StandardLinkTransportInterceptor as i };
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { resolveMaybeOptionalOptions, getConstructors, isPlainObject } from '@orpc/shared';
|
|
2
|
-
|
|
3
|
-
const COMMON_ERROR_STATUS_MAP = {
|
|
4
|
-
BAD_REQUEST: 400,
|
|
5
|
-
UNAUTHORIZED: 401,
|
|
6
|
-
PAYMENT_REQUIRED: 402,
|
|
7
|
-
FORBIDDEN: 403,
|
|
8
|
-
NOT_FOUND: 404,
|
|
9
|
-
METHOD_NOT_SUPPORTED: 405,
|
|
10
|
-
NOT_ACCEPTABLE: 406,
|
|
11
|
-
TIMEOUT: 408,
|
|
12
|
-
CONFLICT: 409,
|
|
13
|
-
GONE: 410,
|
|
14
|
-
LENGTH_REQUIRED: 411,
|
|
15
|
-
PRECONDITION_FAILED: 412,
|
|
16
|
-
PAYLOAD_TOO_LARGE: 413,
|
|
17
|
-
UNSUPPORTED_MEDIA_TYPE: 415,
|
|
18
|
-
UNPROCESSABLE_CONTENT: 422,
|
|
19
|
-
PRECONDITION_REQUIRED: 428,
|
|
20
|
-
TOO_MANY_REQUESTS: 429,
|
|
21
|
-
CLIENT_CLOSED_REQUEST: 499,
|
|
22
|
-
INTERNAL_SERVER_ERROR: 500,
|
|
23
|
-
NOT_IMPLEMENTED: 501,
|
|
24
|
-
BAD_GATEWAY: 502,
|
|
25
|
-
SERVICE_UNAVAILABLE: 503,
|
|
26
|
-
GATEWAY_TIMEOUT: 504
|
|
27
|
-
};
|
|
28
|
-
let ORPCErrorConstructors;
|
|
29
|
-
class ORPCError extends Error {
|
|
30
|
-
static {
|
|
31
|
-
const ORPC_ERROR_CONSTRUCTORS_SYMBOL = Symbol.for("ORPC_ERROR_CONSTRUCTORS");
|
|
32
|
-
void (globalThis[ORPC_ERROR_CONSTRUCTORS_SYMBOL] ??= /* @__PURE__ */ new WeakSet());
|
|
33
|
-
ORPCErrorConstructors = globalThis[ORPC_ERROR_CONSTRUCTORS_SYMBOL];
|
|
34
|
-
ORPCErrorConstructors.add(ORPCError);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* @remarks
|
|
38
|
-
* **Note**: The `__branch` property is used for type branding, helping TypeScript distinguish
|
|
39
|
-
* an `ORPCError` instance from plain objects with a similar structure.
|
|
40
|
-
*/
|
|
41
|
-
name = "ORPCError";
|
|
42
|
-
/**
|
|
43
|
-
* Indicates whether the error matches a definition in the procedure's `.errors` map,
|
|
44
|
-
* which makes its type inferable on the client.
|
|
45
|
-
*/
|
|
46
|
-
defined = false;
|
|
47
|
-
code;
|
|
48
|
-
data;
|
|
49
|
-
constructor(code, ...rest) {
|
|
50
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
51
|
-
const message = options.message ?? codeToMessage(code);
|
|
52
|
-
super(message, options);
|
|
53
|
-
this.code = code;
|
|
54
|
-
this.data = options.data;
|
|
55
|
-
}
|
|
56
|
-
toJSON() {
|
|
57
|
-
return {
|
|
58
|
-
defined: this.defined,
|
|
59
|
-
code: this.code,
|
|
60
|
-
message: this.message,
|
|
61
|
-
data: this.data
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Workaround for Next.js where different contexts use separate
|
|
66
|
-
* dependency graphs, causing multiple ORPCError constructors existing and breaking
|
|
67
|
-
* `instanceof` checks across contexts.
|
|
68
|
-
*
|
|
69
|
-
* This is particularly problematic with "Optimized SSR", where orpc-client
|
|
70
|
-
* executes in one context but is invoked from another. When an error is thrown
|
|
71
|
-
* in the execution context, `instanceof ORPCError` checks fail in the
|
|
72
|
-
* invocation context due to separate class constructors.
|
|
73
|
-
*
|
|
74
|
-
* @todo Remove this and related code if Next.js resolves the multiple dependency graph issue.
|
|
75
|
-
*/
|
|
76
|
-
static [Symbol.hasInstance](instance) {
|
|
77
|
-
if (!ORPCErrorConstructors.has(this)) {
|
|
78
|
-
return super[Symbol.hasInstance](instance);
|
|
79
|
-
}
|
|
80
|
-
for (const constructor of getConstructors(instance)) {
|
|
81
|
-
if (ORPCErrorConstructors.has(constructor)) {
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
class ResponseError extends Error {
|
|
89
|
-
name = "ResponseError";
|
|
90
|
-
response;
|
|
91
|
-
constructor(options) {
|
|
92
|
-
super(options.message ?? `Received a ${options.response.status} response.`, options);
|
|
93
|
-
this.response = options.response;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
class MalformedResponseError extends ResponseError {
|
|
97
|
-
name = "MalformedResponseError";
|
|
98
|
-
constructor(options) {
|
|
99
|
-
super({ ...options, message: options.message ?? inferMalformedResponseMessage(options.response) });
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
const INFERRED_MESSAGE_MAX_LENGTH = 256;
|
|
103
|
-
function inferMalformedResponseMessage(response) {
|
|
104
|
-
const candidate = isPlainObject(response.body) ? response.body.message : response.body;
|
|
105
|
-
if (typeof candidate === "string" && candidate.length > 0 && candidate.length <= INFERRED_MESSAGE_MAX_LENGTH) {
|
|
106
|
-
return candidate;
|
|
107
|
-
}
|
|
108
|
-
const commonCode = Object.entries(COMMON_ERROR_STATUS_MAP).find(([, status]) => status === response.status)?.[0];
|
|
109
|
-
return commonCode ? codeToMessage(commonCode) : "Malformed oRPC Response";
|
|
110
|
-
}
|
|
111
|
-
function codeToMessage(code) {
|
|
112
|
-
return code.split("_").map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(" ");
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export { COMMON_ERROR_STATUS_MAP as C, MalformedResponseError as M, ORPCError as O, ResponseError as R };
|
|
@@ -1,505 +0,0 @@
|
|
|
1
|
-
import { isPlainObject, wrapAsyncIterator, isTypescriptObject, NullProtoObj, parseBigIntString, DEFAULT_MAX_BIGINT_DIGITS, copyOnWrite, isAsyncIteratorObject, stringifyJSON } from '@orpc/shared';
|
|
2
|
-
import { getEventMeta, withEventMeta, ErrorEvent } from '@standard-server/core';
|
|
3
|
-
import { O as ORPCError } from './client.LF04eOvL.mjs';
|
|
4
|
-
|
|
5
|
-
function isDefinedError(error) {
|
|
6
|
-
return error instanceof ORPCError && error.defined;
|
|
7
|
-
}
|
|
8
|
-
function toORPCError(error) {
|
|
9
|
-
return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", { cause: error });
|
|
10
|
-
}
|
|
11
|
-
function isORPCErrorJson(json) {
|
|
12
|
-
if (!isPlainObject(json)) {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
const validKeys = ["defined", "code", "message", "data"];
|
|
16
|
-
if (Object.keys(json).some((k) => !validKeys.includes(k))) {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "message" in json && typeof json.message === "string";
|
|
20
|
-
}
|
|
21
|
-
function createORPCErrorFromJson(json, options = {}) {
|
|
22
|
-
const error = new ORPCError(json.code, {
|
|
23
|
-
...json,
|
|
24
|
-
...options
|
|
25
|
-
});
|
|
26
|
-
error.defined = json.defined;
|
|
27
|
-
return error;
|
|
28
|
-
}
|
|
29
|
-
function cloneORPCError(error) {
|
|
30
|
-
const cloned = new ORPCError(error.code, {
|
|
31
|
-
message: error.message,
|
|
32
|
-
data: error.data,
|
|
33
|
-
cause: error.cause
|
|
34
|
-
});
|
|
35
|
-
Object.setPrototypeOf(cloned, Object.getPrototypeOf(error));
|
|
36
|
-
Object.defineProperties(cloned, Object.getOwnPropertyDescriptors(error));
|
|
37
|
-
cloned.stack = error.stack;
|
|
38
|
-
return cloned;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function wrapAsyncIteratorPreservingEventMeta(iterator, { mapResult, mapError, ...rest }) {
|
|
42
|
-
return wrapAsyncIterator(iterator, {
|
|
43
|
-
...rest,
|
|
44
|
-
mapResult: mapResult && (async (result) => {
|
|
45
|
-
const mapped = await mapResult(result);
|
|
46
|
-
if (mapped.value !== result.value) {
|
|
47
|
-
const meta = getEventMeta(result.value);
|
|
48
|
-
if (meta && isTypescriptObject(mapped.value)) {
|
|
49
|
-
return { done: mapped.done, value: withEventMeta(mapped.value, meta) };
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return mapped;
|
|
53
|
-
}),
|
|
54
|
-
mapError: mapError && (async (error) => {
|
|
55
|
-
const mapped = await mapError(error);
|
|
56
|
-
if (mapped !== error) {
|
|
57
|
-
const meta = getEventMeta(error);
|
|
58
|
-
if (meta && isTypescriptObject(mapped)) {
|
|
59
|
-
return withEventMeta(mapped, meta);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return mapped;
|
|
63
|
-
})
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function invalidSerializedData(detail) {
|
|
68
|
-
return new TypeError(`Invalid RPC serialized data: ${detail}`);
|
|
69
|
-
}
|
|
70
|
-
function assertSerializedType(ok, type, expected) {
|
|
71
|
-
if (!ok) {
|
|
72
|
-
throw invalidSerializedData(`type "${type}" expects ${expected}.`);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
function isWalkable(value) {
|
|
76
|
-
return Array.isArray(value) || isPlainObject(value);
|
|
77
|
-
}
|
|
78
|
-
function walkInto(parent, key, original, restored) {
|
|
79
|
-
const value = parent[key];
|
|
80
|
-
if (!isWalkable(value) || restored?.has(value)) {
|
|
81
|
-
throw invalidSerializedData(`segment "${key}" cannot be walked into.`);
|
|
82
|
-
}
|
|
83
|
-
return value === original ? copyOnWrite(parent, key, original) : value;
|
|
84
|
-
}
|
|
85
|
-
const DEFAULT_RPC_JSON_SERIALIZER_HANDLERS = {
|
|
86
|
-
undefined: {
|
|
87
|
-
condition(data) {
|
|
88
|
-
return data === void 0;
|
|
89
|
-
},
|
|
90
|
-
serialize() {
|
|
91
|
-
return null;
|
|
92
|
-
},
|
|
93
|
-
deserialize(serialized) {
|
|
94
|
-
assertSerializedType(serialized === null, "undefined", "null");
|
|
95
|
-
return void 0;
|
|
96
|
-
},
|
|
97
|
-
isTerminal: true
|
|
98
|
-
},
|
|
99
|
-
bigint: {
|
|
100
|
-
condition(data) {
|
|
101
|
-
return typeof data === "bigint";
|
|
102
|
-
},
|
|
103
|
-
serialize(data) {
|
|
104
|
-
return data.toString();
|
|
105
|
-
},
|
|
106
|
-
deserialize(serialized) {
|
|
107
|
-
assertSerializedType(typeof serialized === "string", "bigint", "a string");
|
|
108
|
-
const value = parseBigIntString(serialized, DEFAULT_MAX_BIGINT_DIGITS);
|
|
109
|
-
if (value === void 0) {
|
|
110
|
-
throw invalidSerializedData(`type "bigint" expects a decimal integer string of at most ${DEFAULT_MAX_BIGINT_DIGITS} digits.`);
|
|
111
|
-
}
|
|
112
|
-
return value;
|
|
113
|
-
},
|
|
114
|
-
isTerminal: true
|
|
115
|
-
},
|
|
116
|
-
date: {
|
|
117
|
-
condition(data) {
|
|
118
|
-
return data instanceof Date;
|
|
119
|
-
},
|
|
120
|
-
serialize(data) {
|
|
121
|
-
if (Number.isNaN(data.getTime())) {
|
|
122
|
-
return null;
|
|
123
|
-
}
|
|
124
|
-
return data.toISOString();
|
|
125
|
-
},
|
|
126
|
-
deserialize(serialized) {
|
|
127
|
-
assertSerializedType(typeof serialized === "string" || serialized === null, "date", "a string or null");
|
|
128
|
-
return new Date(serialized ?? Number.NaN);
|
|
129
|
-
},
|
|
130
|
-
isTerminal: true
|
|
131
|
-
},
|
|
132
|
-
nan: {
|
|
133
|
-
condition(data) {
|
|
134
|
-
return typeof data === "number" && Number.isNaN(data);
|
|
135
|
-
},
|
|
136
|
-
serialize() {
|
|
137
|
-
return null;
|
|
138
|
-
},
|
|
139
|
-
deserialize(serialized) {
|
|
140
|
-
assertSerializedType(serialized === null, "nan", "null");
|
|
141
|
-
return Number.NaN;
|
|
142
|
-
},
|
|
143
|
-
isTerminal: true
|
|
144
|
-
},
|
|
145
|
-
infinity: {
|
|
146
|
-
condition(data) {
|
|
147
|
-
return data === Number.POSITIVE_INFINITY || data === Number.NEGATIVE_INFINITY;
|
|
148
|
-
},
|
|
149
|
-
serialize(data) {
|
|
150
|
-
return data > 0 ? "Infinity" : "-Infinity";
|
|
151
|
-
},
|
|
152
|
-
deserialize(serialized) {
|
|
153
|
-
assertSerializedType(serialized === "Infinity" || serialized === "-Infinity", "infinity", '"Infinity" or "-Infinity"');
|
|
154
|
-
return serialized === "Infinity" ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
|
|
155
|
-
},
|
|
156
|
-
isTerminal: true
|
|
157
|
-
},
|
|
158
|
-
url: {
|
|
159
|
-
condition(data) {
|
|
160
|
-
return data instanceof URL;
|
|
161
|
-
},
|
|
162
|
-
serialize(data) {
|
|
163
|
-
return data.toString();
|
|
164
|
-
},
|
|
165
|
-
deserialize(serialized) {
|
|
166
|
-
assertSerializedType(typeof serialized === "string", "url", "a string");
|
|
167
|
-
return new URL(serialized);
|
|
168
|
-
},
|
|
169
|
-
isTerminal: true
|
|
170
|
-
},
|
|
171
|
-
set: {
|
|
172
|
-
condition(data) {
|
|
173
|
-
return data instanceof Set;
|
|
174
|
-
},
|
|
175
|
-
serialize(data) {
|
|
176
|
-
return Array.from(data);
|
|
177
|
-
},
|
|
178
|
-
deserialize(serialized) {
|
|
179
|
-
assertSerializedType(Array.isArray(serialized), "set", "an array");
|
|
180
|
-
return new Set(serialized);
|
|
181
|
-
}
|
|
182
|
-
},
|
|
183
|
-
map: {
|
|
184
|
-
condition(data) {
|
|
185
|
-
return data instanceof Map;
|
|
186
|
-
},
|
|
187
|
-
serialize(data) {
|
|
188
|
-
return Array.from(data.entries());
|
|
189
|
-
},
|
|
190
|
-
deserialize(serialized) {
|
|
191
|
-
assertSerializedType(Array.isArray(serialized), "map", "an array");
|
|
192
|
-
return new Map(serialized);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
class RPCJsonSerializer {
|
|
197
|
-
handlers;
|
|
198
|
-
inlineBuiltInHandlers;
|
|
199
|
-
handlerEntries;
|
|
200
|
-
omitUndefinedProperties;
|
|
201
|
-
constructor(options = {}) {
|
|
202
|
-
this.omitUndefinedProperties = options.omitUndefinedProperties !== false;
|
|
203
|
-
this.handlers = Object.assign(new NullProtoObj(), DEFAULT_RPC_JSON_SERIALIZER_HANDLERS);
|
|
204
|
-
const customHandlers = options.handlers;
|
|
205
|
-
if (customHandlers === void 0) {
|
|
206
|
-
this.inlineBuiltInHandlers = true;
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
let inlineBuiltInHandlers = true;
|
|
210
|
-
let handlerEntries = [];
|
|
211
|
-
for (const key of Object.keys(customHandlers)) {
|
|
212
|
-
const handler = customHandlers[key];
|
|
213
|
-
this.handlers[key] = handler;
|
|
214
|
-
if (inlineBuiltInHandlers && key in DEFAULT_RPC_JSON_SERIALIZER_HANDLERS) {
|
|
215
|
-
inlineBuiltInHandlers = false;
|
|
216
|
-
}
|
|
217
|
-
if (inlineBuiltInHandlers && handler !== void 0) {
|
|
218
|
-
handlerEntries.push([key, handler]);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
if (!inlineBuiltInHandlers) {
|
|
222
|
-
handlerEntries = [];
|
|
223
|
-
for (const key of Object.keys(this.handlers)) {
|
|
224
|
-
const handler = this.handlers[key];
|
|
225
|
-
if (handler !== void 0) {
|
|
226
|
-
handlerEntries.push([key, handler]);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
this.inlineBuiltInHandlers = inlineBuiltInHandlers;
|
|
231
|
-
this.handlerEntries = handlerEntries;
|
|
232
|
-
}
|
|
233
|
-
serializeJson(data) {
|
|
234
|
-
let meta = [];
|
|
235
|
-
const maps = [];
|
|
236
|
-
const blobs = [];
|
|
237
|
-
const json = this.serializeValue(data, [], meta, maps, blobs);
|
|
238
|
-
meta = meta.length === 0 ? void 0 : meta;
|
|
239
|
-
if (maps.length === 0) {
|
|
240
|
-
return { json, meta };
|
|
241
|
-
}
|
|
242
|
-
return { json, meta, maps, blobs };
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* `segments` is a shared mutable stack (push/pop while walking),
|
|
246
|
-
* so it must be copied before being stored in `meta` or `maps`.
|
|
247
|
-
*/
|
|
248
|
-
serializeValue(data, segments, meta, maps, blobs) {
|
|
249
|
-
if (this.inlineBuiltInHandlers) {
|
|
250
|
-
switch (typeof data) {
|
|
251
|
-
case "string":
|
|
252
|
-
case "boolean":
|
|
253
|
-
return data;
|
|
254
|
-
case "number":
|
|
255
|
-
if (Number.isFinite(data)) {
|
|
256
|
-
return data;
|
|
257
|
-
}
|
|
258
|
-
if (Number.isNaN(data)) {
|
|
259
|
-
meta.push(["nan", ...segments]);
|
|
260
|
-
return null;
|
|
261
|
-
}
|
|
262
|
-
meta.push(["infinity", ...segments]);
|
|
263
|
-
return data > 0 ? "Infinity" : "-Infinity";
|
|
264
|
-
case "undefined":
|
|
265
|
-
meta.push(["undefined", ...segments]);
|
|
266
|
-
return null;
|
|
267
|
-
case "bigint":
|
|
268
|
-
meta.push(["bigint", ...segments]);
|
|
269
|
-
return data.toString();
|
|
270
|
-
case "object": {
|
|
271
|
-
if (data === null) {
|
|
272
|
-
return data;
|
|
273
|
-
}
|
|
274
|
-
if (data instanceof Date) {
|
|
275
|
-
meta.push(["date", ...segments]);
|
|
276
|
-
return Number.isNaN(data.getTime()) ? null : data.toISOString();
|
|
277
|
-
}
|
|
278
|
-
if (data instanceof URL) {
|
|
279
|
-
meta.push(["url", ...segments]);
|
|
280
|
-
return data.toString();
|
|
281
|
-
}
|
|
282
|
-
if (data instanceof Set) {
|
|
283
|
-
const result = this.serializeValue(Array.from(data), segments, meta, maps, blobs);
|
|
284
|
-
meta.push(["set", ...segments]);
|
|
285
|
-
return result;
|
|
286
|
-
}
|
|
287
|
-
if (data instanceof Map) {
|
|
288
|
-
const result = this.serializeValue(Array.from(data.entries()), segments, meta, maps, blobs);
|
|
289
|
-
meta.push(["map", ...segments]);
|
|
290
|
-
return result;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
const handlerEntries = this.handlerEntries;
|
|
296
|
-
if (handlerEntries) {
|
|
297
|
-
for (let i = 0; i < handlerEntries.length; i++) {
|
|
298
|
-
const entry = handlerEntries[i];
|
|
299
|
-
const handler = entry[1];
|
|
300
|
-
if (handler.condition(data)) {
|
|
301
|
-
const serialized = handler.serialize(data);
|
|
302
|
-
if (handler.isTerminal) {
|
|
303
|
-
meta.push([entry[0], ...segments]);
|
|
304
|
-
if (serialized instanceof Blob) {
|
|
305
|
-
maps.push(segments.slice());
|
|
306
|
-
blobs.push(serialized);
|
|
307
|
-
}
|
|
308
|
-
return serialized;
|
|
309
|
-
}
|
|
310
|
-
const result = this.serializeValue(serialized, segments, meta, maps, blobs);
|
|
311
|
-
meta.push([entry[0], ...segments]);
|
|
312
|
-
return result;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
if (data instanceof Blob) {
|
|
317
|
-
maps.push(segments.slice());
|
|
318
|
-
blobs.push(data);
|
|
319
|
-
return data;
|
|
320
|
-
}
|
|
321
|
-
if (Array.isArray(data)) {
|
|
322
|
-
const json = [];
|
|
323
|
-
for (let i = 0; i < data.length; i++) {
|
|
324
|
-
segments.push(i);
|
|
325
|
-
json.push(this.serializeValue(data[i], segments, meta, maps, blobs));
|
|
326
|
-
segments.pop();
|
|
327
|
-
}
|
|
328
|
-
return json;
|
|
329
|
-
}
|
|
330
|
-
if (isPlainObject(data)) {
|
|
331
|
-
const json = new NullProtoObj();
|
|
332
|
-
for (const k of Object.keys(data)) {
|
|
333
|
-
const v = data[k];
|
|
334
|
-
if (k === "toJSON" && typeof v === "function") {
|
|
335
|
-
continue;
|
|
336
|
-
}
|
|
337
|
-
if (v === void 0 && this.omitUndefinedProperties) {
|
|
338
|
-
continue;
|
|
339
|
-
}
|
|
340
|
-
segments.push(k);
|
|
341
|
-
json[k] = this.serializeValue(v, segments, meta, maps, blobs);
|
|
342
|
-
segments.pop();
|
|
343
|
-
}
|
|
344
|
-
return json;
|
|
345
|
-
}
|
|
346
|
-
return data;
|
|
347
|
-
}
|
|
348
|
-
deserializeJson(serialized) {
|
|
349
|
-
const ref = { json: serialized.json };
|
|
350
|
-
if (serialized.blobs?.length) {
|
|
351
|
-
for (let i = 0; i < serialized.maps.length; i++) {
|
|
352
|
-
const blob = serialized.blobs[i];
|
|
353
|
-
if (!(blob instanceof Blob)) {
|
|
354
|
-
throw invalidSerializedData(`blob ${i} is not a Blob.`);
|
|
355
|
-
}
|
|
356
|
-
const segments = serialized.maps[i];
|
|
357
|
-
let original = serialized;
|
|
358
|
-
let currentRef = ref;
|
|
359
|
-
let preSegment = "json";
|
|
360
|
-
for (let j = 0; j < segments.length; j++) {
|
|
361
|
-
original = original[preSegment];
|
|
362
|
-
currentRef = walkInto(currentRef, preSegment, original);
|
|
363
|
-
preSegment = segments[j];
|
|
364
|
-
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
365
|
-
throw invalidSerializedData(`segment "${preSegment}" does not exist.`);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
currentRef[preSegment] = blob;
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
const meta = serialized.meta;
|
|
372
|
-
if (meta) {
|
|
373
|
-
let restored;
|
|
374
|
-
const chain = meta.length === 1 ? void 0 : [];
|
|
375
|
-
let chainDepth = 0;
|
|
376
|
-
for (const item of meta) {
|
|
377
|
-
const type = item[0];
|
|
378
|
-
const handler = this.handlers[type];
|
|
379
|
-
if (handler === void 0) {
|
|
380
|
-
throw invalidSerializedData(`type "${type}" is not supported.`);
|
|
381
|
-
}
|
|
382
|
-
const depth = item.length - 1;
|
|
383
|
-
const shared = Math.min(depth, chainDepth);
|
|
384
|
-
let d = 0;
|
|
385
|
-
let original = serialized;
|
|
386
|
-
let currentRef = ref;
|
|
387
|
-
let preSegment = "json";
|
|
388
|
-
if (chain !== void 0 && shared > 0) {
|
|
389
|
-
d = 1;
|
|
390
|
-
while (d < shared && chain[3 * d] === item[d]) {
|
|
391
|
-
d++;
|
|
392
|
-
}
|
|
393
|
-
currentRef = chain[3 * d - 2];
|
|
394
|
-
original = chain[3 * d - 1];
|
|
395
|
-
preSegment = item[d];
|
|
396
|
-
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
397
|
-
throw invalidSerializedData(`segment "${preSegment}" does not exist.`);
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
while (d < depth) {
|
|
401
|
-
original = original[preSegment];
|
|
402
|
-
currentRef = walkInto(currentRef, preSegment, original, restored);
|
|
403
|
-
if (chain !== void 0) {
|
|
404
|
-
chain[3 * d] = preSegment;
|
|
405
|
-
chain[3 * d + 1] = currentRef;
|
|
406
|
-
chain[3 * d + 2] = original;
|
|
407
|
-
}
|
|
408
|
-
preSegment = item[++d];
|
|
409
|
-
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
410
|
-
throw invalidSerializedData(`segment "${preSegment}" does not exist.`);
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
chainDepth = depth;
|
|
414
|
-
const value = handler.deserialize(currentRef[preSegment]);
|
|
415
|
-
currentRef[preSegment] = value;
|
|
416
|
-
if (isWalkable(value)) {
|
|
417
|
-
(restored ??= /* @__PURE__ */ new Set()).add(value);
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
return ref.json;
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
class RPCSerializer extends RPCJsonSerializer {
|
|
426
|
-
defaultSerializeOptions;
|
|
427
|
-
constructor(options = {}) {
|
|
428
|
-
super(options);
|
|
429
|
-
this.defaultSerializeOptions = options.serialize;
|
|
430
|
-
}
|
|
431
|
-
serialize(data, options = {}) {
|
|
432
|
-
if (data === void 0 || data instanceof ReadableStream || data instanceof Blob) {
|
|
433
|
-
return data;
|
|
434
|
-
}
|
|
435
|
-
if (isAsyncIteratorObject(data)) {
|
|
436
|
-
return wrapAsyncIteratorPreservingEventMeta(data, {
|
|
437
|
-
mapResult: (result) => {
|
|
438
|
-
if (result.value === void 0) {
|
|
439
|
-
return result;
|
|
440
|
-
}
|
|
441
|
-
return { done: result.done, value: this.serializePayload(result.value, false) };
|
|
442
|
-
},
|
|
443
|
-
mapError: (e) => new ErrorEvent(
|
|
444
|
-
this.serializePayload(toORPCError(e).toJSON(), false),
|
|
445
|
-
{ cause: e }
|
|
446
|
-
)
|
|
447
|
-
});
|
|
448
|
-
}
|
|
449
|
-
const useFormDataForBlobs = options.useFormDataForBlobFields ?? this.defaultSerializeOptions?.useFormDataForBlobFields ?? true;
|
|
450
|
-
return this.serializePayload(data, useFormDataForBlobs);
|
|
451
|
-
}
|
|
452
|
-
serializePayload(data, useFormDataForBlobs) {
|
|
453
|
-
const { json, meta, maps, blobs } = this.serializeJson(data);
|
|
454
|
-
if (!useFormDataForBlobs || !blobs?.length) {
|
|
455
|
-
return { json, meta };
|
|
456
|
-
}
|
|
457
|
-
const form = new FormData();
|
|
458
|
-
form.set("data", stringifyJSON({ json, meta, maps }));
|
|
459
|
-
blobs.forEach((blob, i) => {
|
|
460
|
-
form.set(i.toString(), blob);
|
|
461
|
-
});
|
|
462
|
-
return form;
|
|
463
|
-
}
|
|
464
|
-
deserialize(data) {
|
|
465
|
-
if (data === void 0 || data instanceof ReadableStream || data instanceof Blob) {
|
|
466
|
-
return data;
|
|
467
|
-
}
|
|
468
|
-
if (isAsyncIteratorObject(data)) {
|
|
469
|
-
return wrapAsyncIteratorPreservingEventMeta(data, {
|
|
470
|
-
mapResult: (result) => {
|
|
471
|
-
if (result.value === void 0) {
|
|
472
|
-
return result;
|
|
473
|
-
}
|
|
474
|
-
return { done: result.done, value: this.deserializePayload(result.value) };
|
|
475
|
-
},
|
|
476
|
-
mapError: (e) => {
|
|
477
|
-
if (!(e instanceof ErrorEvent)) {
|
|
478
|
-
return e;
|
|
479
|
-
}
|
|
480
|
-
const deserialized = this.deserializePayload(e.data);
|
|
481
|
-
if (isORPCErrorJson(deserialized)) {
|
|
482
|
-
return createORPCErrorFromJson(deserialized, { cause: e });
|
|
483
|
-
}
|
|
484
|
-
return new ErrorEvent(deserialized, { cause: e });
|
|
485
|
-
}
|
|
486
|
-
});
|
|
487
|
-
}
|
|
488
|
-
return this.deserializePayload(data);
|
|
489
|
-
}
|
|
490
|
-
deserializePayload(data) {
|
|
491
|
-
if (!(data instanceof FormData)) {
|
|
492
|
-
return this.deserializeJson(data);
|
|
493
|
-
}
|
|
494
|
-
const serialized = JSON.parse(data.get("data"));
|
|
495
|
-
const blobs = [];
|
|
496
|
-
for (const [key, value] of data) {
|
|
497
|
-
if (value instanceof Blob) {
|
|
498
|
-
blobs[Number(key)] = value;
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
return this.deserializeJson({ ...serialized, blobs });
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
export { RPCSerializer as R, isDefinedError as a, RPCJsonSerializer as b, createORPCErrorFromJson as c, cloneORPCError as d, isORPCErrorJson as i, toORPCError as t, wrapAsyncIteratorPreservingEventMeta as w };
|