@orpc/client 2.0.0-beta.47 → 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 +6 -7
- package/dist/adapters/fetch/index.d.ts +2 -48
- package/dist/adapters/fetch/index.js +2 -0
- package/dist/adapters/fetch/rpc-link.d.ts +14 -0
- package/dist/adapters/fetch/rpc-link.js +14 -0
- package/dist/adapters/fetch/transport.d.ts +34 -0
- package/dist/adapters/fetch/transport.js +65 -0
- package/dist/adapters/message-port/index.d.ts +3 -80
- package/dist/adapters/message-port/index.js +3 -0
- package/dist/adapters/message-port/message-port.d.ts +29 -0
- package/dist/adapters/message-port/message-port.js +56 -0
- package/dist/adapters/message-port/rpc-link.d.ts +15 -0
- package/dist/adapters/message-port/rpc-link.js +15 -0
- package/dist/adapters/message-port/transport.d.ts +38 -0
- package/dist/adapters/message-port/transport.js +52 -0
- package/dist/adapters/standard/codec.d.ts +14 -0
- package/dist/adapters/standard/codec.js +1 -0
- package/dist/adapters/standard/index.d.ts +5 -60
- package/dist/adapters/standard/index.js +4 -0
- package/dist/adapters/standard/link.d.ts +40 -0
- package/dist/adapters/standard/link.js +85 -0
- package/dist/adapters/standard/plugin.d.ts +32 -0
- package/dist/adapters/standard/plugin.js +16 -0
- package/dist/adapters/standard/{index.d.mts → rpc-link-codec.d.ts} +17 -14
- package/dist/adapters/standard/rpc-link-codec.js +104 -0
- package/dist/adapters/standard/transport.d.ts +14 -0
- package/dist/adapters/standard/transport.js +1 -0
- package/dist/adapters/websocket/index.d.ts +2 -136
- package/dist/adapters/websocket/index.js +2 -0
- package/dist/adapters/websocket/rpc-link.d.ts +14 -0
- package/dist/adapters/websocket/rpc-link.js +14 -0
- package/dist/adapters/websocket/{index.d.mts → transport.d.ts} +32 -53
- package/dist/adapters/websocket/transport.js +133 -0
- package/dist/async-iterator-object.d.ts +2 -0
- package/dist/async-iterator-object.js +29 -0
- package/dist/client-safe.d.ts +18 -0
- package/dist/client-safe.js +36 -0
- package/dist/client.d.ts +40 -0
- package/dist/client.js +39 -0
- package/dist/consts.d.ts +10 -0
- package/dist/consts.js +43 -0
- package/dist/dynamic-link.d.ts +13 -0
- package/dist/dynamic-link.js +17 -0
- package/dist/error-utils.d.ts +21 -0
- package/dist/error-utils.js +58 -0
- package/dist/error.d.ts +132 -0
- package/dist/error.js +178 -0
- package/dist/index.d.ts +38 -129
- package/dist/index.js +40 -0
- package/dist/plugins/batch.d.ts +185 -0
- package/dist/plugins/batch.js +401 -0
- package/dist/plugins/cookie-jar.d.ts +82 -0
- package/dist/plugins/cookie-jar.js +268 -0
- package/dist/plugins/dedupe.d.ts +64 -0
- package/dist/plugins/dedupe.js +226 -0
- package/dist/plugins/index.d.ts +20 -412
- package/dist/plugins/index.js +23 -0
- package/dist/plugins/request-compression.d.ts +44 -0
- package/dist/plugins/request-compression.js +178 -0
- package/dist/plugins/response-compression.d.ts +28 -0
- package/dist/plugins/response-compression.js +91 -0
- package/dist/plugins/retry-after.d.ts +50 -0
- package/dist/plugins/retry-after.js +88 -0
- package/dist/plugins/retry.d.ts +84 -0
- package/dist/plugins/retry.js +140 -0
- package/dist/plugins/timeout.d.ts +25 -0
- package/dist/plugins/timeout.js +37 -0
- package/dist/{shared/client.DqIWeNhQ.d.mts → rpc-json-serializer.d.ts} +9 -41
- package/dist/rpc-json-serializer.js +392 -0
- package/dist/rpc-serializer.d.ts +32 -0
- package/dist/rpc-serializer.js +130 -0
- package/dist/types.d.ts +97 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +47 -0
- package/dist/utils.js +51 -0
- package/package.json +43 -45
- package/dist/adapters/fetch/index.d.mts +0 -48
- package/dist/adapters/fetch/index.mjs +0 -77
- package/dist/adapters/message-port/index.d.mts +0 -80
- package/dist/adapters/message-port/index.mjs +0 -101
- package/dist/adapters/standard/index.mjs +0 -6
- package/dist/adapters/websocket/index.mjs +0 -131
- package/dist/index.d.mts +0 -144
- package/dist/index.mjs +0 -147
- package/dist/plugins/index.d.mts +0 -420
- package/dist/plugins/index.mjs +0 -971
- 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.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
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Value } from '@orpc/shared';
|
|
2
|
+
import type { StandardLinkInterceptorOptions, StandardLinkOptions, StandardLinkPlugin } from '../adapters/standard/index.ts';
|
|
3
|
+
import type { ClientContext } from '../types.ts';
|
|
4
|
+
export interface TimeoutLinkPluginOptions<T extends ClientContext> {
|
|
5
|
+
/**
|
|
6
|
+
* Timeout in milliseconds before the request is aborted.
|
|
7
|
+
* Use `null` or `undefined` to disable the timeout.
|
|
8
|
+
*/
|
|
9
|
+
timeout: Value<number | null | undefined, [options: StandardLinkInterceptorOptions<T>]>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The Timeout Link Plugin aborts requests that exceed a configured timeout with an `AbortError`.
|
|
13
|
+
*
|
|
14
|
+
* @see {@link https://orpc.dev/docs/plugins/timeout | Timeout Plugin}
|
|
15
|
+
*/
|
|
16
|
+
export declare class TimeoutLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
17
|
+
private readonly timeout;
|
|
18
|
+
name: string;
|
|
19
|
+
/**
|
|
20
|
+
* Should abort if the total retry time exceeds the configured timeout
|
|
21
|
+
*/
|
|
22
|
+
after: string[];
|
|
23
|
+
constructor(options: NoInfer<TimeoutLinkPluginOptions<T>>);
|
|
24
|
+
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { AbortError, anyAbortSignal, toArray, value } from '@orpc/shared';
|
|
2
|
+
/**
|
|
3
|
+
* The Timeout Link Plugin aborts requests that exceed a configured timeout with an `AbortError`.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://orpc.dev/docs/plugins/timeout | Timeout Plugin}
|
|
6
|
+
*/
|
|
7
|
+
export class TimeoutLinkPlugin {
|
|
8
|
+
timeout;
|
|
9
|
+
name = '~timeout';
|
|
10
|
+
/**
|
|
11
|
+
* Should abort if the total retry time exceeds the configured timeout
|
|
12
|
+
*/
|
|
13
|
+
after = ['~retry'];
|
|
14
|
+
constructor(options) {
|
|
15
|
+
this.timeout = options.timeout;
|
|
16
|
+
}
|
|
17
|
+
init(options) {
|
|
18
|
+
const interceptor = async (interceptorOptions) => {
|
|
19
|
+
const timeoutMs = value(this.timeout, interceptorOptions);
|
|
20
|
+
if (timeoutMs === null || timeoutMs === undefined) {
|
|
21
|
+
return interceptorOptions.next();
|
|
22
|
+
}
|
|
23
|
+
const controller = new AbortController();
|
|
24
|
+
const timeoutId = setTimeout(() => {
|
|
25
|
+
controller.abort(new AbortError(`Request timed out after ${timeoutMs}ms`));
|
|
26
|
+
}, timeoutMs);
|
|
27
|
+
const signal = anyAbortSignal([interceptorOptions.signal, controller.signal]);
|
|
28
|
+
try {
|
|
29
|
+
return await interceptorOptions.next({ ...interceptorOptions, signal });
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
clearTimeout(timeoutId);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
return { ...options, interceptors: [interceptor, ...toArray(options.interceptors)] };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type RPCJsonSerializationMeta = [type: string, ...path: Segment[]];
|
|
5
|
-
type RPCJsonSerialization = {
|
|
1
|
+
import type { Segment } from '@orpc/shared';
|
|
2
|
+
export type RPCJsonSerializationMeta = [type: string, ...path: Segment[]];
|
|
3
|
+
export type RPCJsonSerialization = {
|
|
6
4
|
json: unknown;
|
|
7
5
|
meta?: RPCJsonSerializationMeta[] | undefined;
|
|
8
6
|
maps?: undefined;
|
|
@@ -13,7 +11,7 @@ type RPCJsonSerialization = {
|
|
|
13
11
|
maps: Segment[][];
|
|
14
12
|
blobs: Blob[];
|
|
15
13
|
};
|
|
16
|
-
interface RPCJsonSerializerHandler {
|
|
14
|
+
export interface RPCJsonSerializerHandler {
|
|
17
15
|
condition(value: unknown): boolean;
|
|
18
16
|
serialize(value: any): unknown;
|
|
19
17
|
/**
|
|
@@ -30,7 +28,7 @@ interface RPCJsonSerializerHandler {
|
|
|
30
28
|
*/
|
|
31
29
|
isTerminal?: boolean | undefined;
|
|
32
30
|
}
|
|
33
|
-
interface RPCJsonSerializerOptions {
|
|
31
|
+
export interface RPCJsonSerializerOptions {
|
|
34
32
|
/**
|
|
35
33
|
* Extend or override the built-in type handlers used during serialization and deserialization.
|
|
36
34
|
*
|
|
@@ -66,6 +64,9 @@ interface RPCJsonSerializerOptions {
|
|
|
66
64
|
* handlers: { url: undefined }
|
|
67
65
|
* ```
|
|
68
66
|
*
|
|
67
|
+
* During deserialization, values whose type has no handler are left in their serialized form,
|
|
68
|
+
* so a client and server with different handlers can still communicate.
|
|
69
|
+
*
|
|
69
70
|
* Built-in type keys: `undefined`, `bigint`, `date`, `nan`, `infinity`, `url`, `set`, `map`.
|
|
70
71
|
*/
|
|
71
72
|
handlers?: Record<string, undefined | RPCJsonSerializerHandler> | undefined;
|
|
@@ -82,7 +83,7 @@ interface RPCJsonSerializerOptions {
|
|
|
82
83
|
*
|
|
83
84
|
* @see {@link https://orpc.dev/docs/rpc/serializer#rpc-json-serializer | RPC Serializer - RPC JSON Serializer}
|
|
84
85
|
*/
|
|
85
|
-
declare class RPCJsonSerializer {
|
|
86
|
+
export declare class RPCJsonSerializer {
|
|
86
87
|
private readonly handlers;
|
|
87
88
|
private readonly inlineBuiltInHandlers;
|
|
88
89
|
private readonly handlerEntries;
|
|
@@ -96,36 +97,3 @@ declare class RPCJsonSerializer {
|
|
|
96
97
|
private serializeValue;
|
|
97
98
|
deserializeJson(serialized: RPCJsonSerialization): unknown;
|
|
98
99
|
}
|
|
99
|
-
|
|
100
|
-
interface RPCSerializerSerializeOptions {
|
|
101
|
-
/**
|
|
102
|
-
* Use FormData for serialization when nested blobs are present.
|
|
103
|
-
* Does not apply to root-level Blob values.
|
|
104
|
-
*
|
|
105
|
-
* @default true
|
|
106
|
-
*/
|
|
107
|
-
useFormDataForBlobFields?: boolean | undefined;
|
|
108
|
-
}
|
|
109
|
-
interface RPCSerializerOptions extends RPCJsonSerializerOptions {
|
|
110
|
-
/**
|
|
111
|
-
* Default options for serialize method
|
|
112
|
-
*/
|
|
113
|
-
serialize?: RPCSerializerSerializeOptions | undefined;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Serializes and deserializes data for the RPC protocol,
|
|
117
|
-
* preserving native types like Date, BigInt, Set, and Map that plain JSON cannot represent.
|
|
118
|
-
*
|
|
119
|
-
* @see {@link https://orpc.dev/docs/rpc/serializer | RPC Serializer}
|
|
120
|
-
*/
|
|
121
|
-
declare class RPCSerializer extends RPCJsonSerializer {
|
|
122
|
-
private readonly defaultSerializeOptions;
|
|
123
|
-
constructor(options?: RPCSerializerOptions);
|
|
124
|
-
serialize(data: unknown, options?: RPCSerializerSerializeOptions): StandardBody;
|
|
125
|
-
private serializePayload;
|
|
126
|
-
deserialize(data: StandardBody): unknown;
|
|
127
|
-
private deserializePayload;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export { RPCJsonSerializer as b, RPCSerializer as e };
|
|
131
|
-
export type { RPCJsonSerialization as R, RPCJsonSerializationMeta as a, RPCJsonSerializerHandler as c, RPCJsonSerializerOptions as d, RPCSerializerOptions as f, RPCSerializerSerializeOptions as g };
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import { copyOnWrite, DEFAULT_MAX_BIGINT_DIGITS, isPlainObject, NullProtoObj, parseBigIntString, } from '@orpc/shared';
|
|
2
|
+
function invalidSerializedData(detail) {
|
|
3
|
+
return new TypeError(`Invalid RPC serialized data: ${detail}`);
|
|
4
|
+
}
|
|
5
|
+
function assertSerializedType(ok, type, expected) {
|
|
6
|
+
if (!ok) {
|
|
7
|
+
throw invalidSerializedData(`type "${type}" expects ${expected}.`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function isWalkable(value) {
|
|
11
|
+
return Array.isArray(value) || isPlainObject(value);
|
|
12
|
+
}
|
|
13
|
+
function walkInto(parent, key, original, restored) {
|
|
14
|
+
const value = parent[key];
|
|
15
|
+
if (!isWalkable(value) || restored?.has(value)) {
|
|
16
|
+
throw invalidSerializedData(`segment "${key}" cannot be walked into.`);
|
|
17
|
+
}
|
|
18
|
+
return value === original ? copyOnWrite(parent, key, original) : value;
|
|
19
|
+
}
|
|
20
|
+
const DEFAULT_RPC_JSON_SERIALIZER_HANDLERS = {
|
|
21
|
+
undefined: {
|
|
22
|
+
condition(data) {
|
|
23
|
+
return data === undefined;
|
|
24
|
+
},
|
|
25
|
+
serialize() {
|
|
26
|
+
return null;
|
|
27
|
+
},
|
|
28
|
+
deserialize(serialized) {
|
|
29
|
+
assertSerializedType(serialized === null, 'undefined', 'null');
|
|
30
|
+
return undefined;
|
|
31
|
+
},
|
|
32
|
+
isTerminal: true,
|
|
33
|
+
},
|
|
34
|
+
bigint: {
|
|
35
|
+
condition(data) {
|
|
36
|
+
return typeof data === 'bigint';
|
|
37
|
+
},
|
|
38
|
+
serialize(data) {
|
|
39
|
+
return data.toString();
|
|
40
|
+
},
|
|
41
|
+
deserialize(serialized) {
|
|
42
|
+
assertSerializedType(typeof serialized === 'string', 'bigint', 'a string');
|
|
43
|
+
const value = parseBigIntString(serialized, DEFAULT_MAX_BIGINT_DIGITS);
|
|
44
|
+
if (value === undefined) {
|
|
45
|
+
throw invalidSerializedData(`type "bigint" expects a decimal integer string of at most ${DEFAULT_MAX_BIGINT_DIGITS} digits.`);
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
},
|
|
49
|
+
isTerminal: true,
|
|
50
|
+
},
|
|
51
|
+
date: {
|
|
52
|
+
condition(data) {
|
|
53
|
+
return data instanceof Date;
|
|
54
|
+
},
|
|
55
|
+
serialize(data) {
|
|
56
|
+
if (Number.isNaN(data.getTime())) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
return data.toISOString();
|
|
60
|
+
},
|
|
61
|
+
deserialize(serialized) {
|
|
62
|
+
assertSerializedType(typeof serialized === 'string' || serialized === null, 'date', 'a string or null');
|
|
63
|
+
return new Date(serialized ?? Number.NaN);
|
|
64
|
+
},
|
|
65
|
+
isTerminal: true,
|
|
66
|
+
},
|
|
67
|
+
nan: {
|
|
68
|
+
condition(data) {
|
|
69
|
+
return typeof data === 'number' && Number.isNaN(data);
|
|
70
|
+
},
|
|
71
|
+
serialize() {
|
|
72
|
+
return null;
|
|
73
|
+
},
|
|
74
|
+
deserialize(serialized) {
|
|
75
|
+
assertSerializedType(serialized === null, 'nan', 'null');
|
|
76
|
+
return Number.NaN;
|
|
77
|
+
},
|
|
78
|
+
isTerminal: true,
|
|
79
|
+
},
|
|
80
|
+
infinity: {
|
|
81
|
+
condition(data) {
|
|
82
|
+
return data === Number.POSITIVE_INFINITY || data === Number.NEGATIVE_INFINITY;
|
|
83
|
+
},
|
|
84
|
+
serialize(data) {
|
|
85
|
+
return data > 0 ? 'Infinity' : '-Infinity';
|
|
86
|
+
},
|
|
87
|
+
deserialize(serialized) {
|
|
88
|
+
assertSerializedType(serialized === 'Infinity' || serialized === '-Infinity', 'infinity', '"Infinity" or "-Infinity"');
|
|
89
|
+
return serialized === 'Infinity' ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
|
|
90
|
+
},
|
|
91
|
+
isTerminal: true,
|
|
92
|
+
},
|
|
93
|
+
url: {
|
|
94
|
+
condition(data) {
|
|
95
|
+
return data instanceof URL;
|
|
96
|
+
},
|
|
97
|
+
serialize(data) {
|
|
98
|
+
return data.toString();
|
|
99
|
+
},
|
|
100
|
+
deserialize(serialized) {
|
|
101
|
+
assertSerializedType(typeof serialized === 'string', 'url', 'a string');
|
|
102
|
+
return new URL(serialized);
|
|
103
|
+
},
|
|
104
|
+
isTerminal: true,
|
|
105
|
+
},
|
|
106
|
+
set: {
|
|
107
|
+
condition(data) {
|
|
108
|
+
return data instanceof Set;
|
|
109
|
+
},
|
|
110
|
+
serialize(data) {
|
|
111
|
+
return Array.from(data);
|
|
112
|
+
},
|
|
113
|
+
deserialize(serialized) {
|
|
114
|
+
assertSerializedType(Array.isArray(serialized), 'set', 'an array');
|
|
115
|
+
return new Set(serialized);
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
map: {
|
|
119
|
+
condition(data) {
|
|
120
|
+
return data instanceof Map;
|
|
121
|
+
},
|
|
122
|
+
serialize(data) {
|
|
123
|
+
return Array.from(data.entries());
|
|
124
|
+
},
|
|
125
|
+
deserialize(serialized) {
|
|
126
|
+
assertSerializedType(Array.isArray(serialized), 'map', 'an array');
|
|
127
|
+
return new Map(serialized);
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Serializes and deserializes native types like Date, BigInt, Set, and Map
|
|
133
|
+
* into a JSON value plus separate metadata describing how to restore them.
|
|
134
|
+
*
|
|
135
|
+
* @see {@link https://orpc.dev/docs/rpc/serializer#rpc-json-serializer | RPC Serializer - RPC JSON Serializer}
|
|
136
|
+
*/
|
|
137
|
+
export class RPCJsonSerializer {
|
|
138
|
+
handlers;
|
|
139
|
+
inlineBuiltInHandlers;
|
|
140
|
+
handlerEntries;
|
|
141
|
+
omitUndefinedProperties;
|
|
142
|
+
constructor(options = {}) {
|
|
143
|
+
this.omitUndefinedProperties = options.omitUndefinedProperties !== false;
|
|
144
|
+
this.handlers = Object.assign(new NullProtoObj(), DEFAULT_RPC_JSON_SERIALIZER_HANDLERS);
|
|
145
|
+
const customHandlers = options.handlers;
|
|
146
|
+
if (customHandlers === undefined) {
|
|
147
|
+
this.inlineBuiltInHandlers = true;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
let inlineBuiltInHandlers = true;
|
|
151
|
+
let handlerEntries = [];
|
|
152
|
+
for (const key of Object.keys(customHandlers)) {
|
|
153
|
+
const handler = customHandlers[key];
|
|
154
|
+
this.handlers[key] = handler;
|
|
155
|
+
if (inlineBuiltInHandlers && key in DEFAULT_RPC_JSON_SERIALIZER_HANDLERS) {
|
|
156
|
+
inlineBuiltInHandlers = false;
|
|
157
|
+
}
|
|
158
|
+
if (inlineBuiltInHandlers && handler !== undefined) {
|
|
159
|
+
handlerEntries.push([key, handler]);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (!inlineBuiltInHandlers) {
|
|
163
|
+
handlerEntries = [];
|
|
164
|
+
for (const key of Object.keys(this.handlers)) {
|
|
165
|
+
const handler = this.handlers[key];
|
|
166
|
+
if (handler !== undefined) {
|
|
167
|
+
handlerEntries.push([key, handler]);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
this.inlineBuiltInHandlers = inlineBuiltInHandlers;
|
|
172
|
+
// undefined = no handlers to check, so the inline path can return primitives early
|
|
173
|
+
this.handlerEntries = handlerEntries.length === 0 ? undefined : handlerEntries;
|
|
174
|
+
}
|
|
175
|
+
serializeJson(data) {
|
|
176
|
+
let meta = [];
|
|
177
|
+
const maps = [];
|
|
178
|
+
const blobs = [];
|
|
179
|
+
const json = this.serializeValue(data, [], meta, maps, blobs);
|
|
180
|
+
meta = meta.length === 0 ? undefined : meta;
|
|
181
|
+
if (maps.length === 0) {
|
|
182
|
+
return { json, meta };
|
|
183
|
+
}
|
|
184
|
+
return { json, meta, maps, blobs };
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* `segments` is a shared mutable stack (push/pop while walking),
|
|
188
|
+
* so it must be copied before being stored in `meta` or `maps`.
|
|
189
|
+
*/
|
|
190
|
+
serializeValue(data, segments, meta, maps, blobs) {
|
|
191
|
+
const handlerEntries = this.handlerEntries;
|
|
192
|
+
/**
|
|
193
|
+
* Inlined version of DEFAULT_RPC_JSON_SERIALIZER_HANDLERS: primitives are
|
|
194
|
+
* dispatched on typeof and skip every handler condition check.
|
|
195
|
+
* Must match the built-in handlers exactly.
|
|
196
|
+
* Primitives without a built-in handler return early unless custom handlers must see them.
|
|
197
|
+
*/
|
|
198
|
+
if (this.inlineBuiltInHandlers) {
|
|
199
|
+
switch (typeof data) {
|
|
200
|
+
case 'string':
|
|
201
|
+
case 'boolean':
|
|
202
|
+
if (handlerEntries === undefined) {
|
|
203
|
+
return data;
|
|
204
|
+
}
|
|
205
|
+
break;
|
|
206
|
+
case 'number':
|
|
207
|
+
if (Number.isFinite(data)) {
|
|
208
|
+
if (handlerEntries === undefined) {
|
|
209
|
+
return data;
|
|
210
|
+
}
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
if (Number.isNaN(data)) {
|
|
214
|
+
meta.push(['nan', ...segments]);
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
meta.push(['infinity', ...segments]);
|
|
218
|
+
return data > 0 ? 'Infinity' : '-Infinity';
|
|
219
|
+
case 'undefined':
|
|
220
|
+
meta.push(['undefined', ...segments]);
|
|
221
|
+
return null;
|
|
222
|
+
case 'bigint':
|
|
223
|
+
meta.push(['bigint', ...segments]);
|
|
224
|
+
return data.toString();
|
|
225
|
+
case 'object': {
|
|
226
|
+
if (data === null) {
|
|
227
|
+
if (handlerEntries === undefined) {
|
|
228
|
+
return data;
|
|
229
|
+
}
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
if (data instanceof Date) {
|
|
233
|
+
meta.push(['date', ...segments]);
|
|
234
|
+
return Number.isNaN(data.getTime()) ? null : data.toISOString();
|
|
235
|
+
}
|
|
236
|
+
if (data instanceof URL) {
|
|
237
|
+
meta.push(['url', ...segments]);
|
|
238
|
+
return data.toString();
|
|
239
|
+
}
|
|
240
|
+
if (data instanceof Set) {
|
|
241
|
+
const result = this.serializeValue(Array.from(data), segments, meta, maps, blobs);
|
|
242
|
+
meta.push(['set', ...segments]);
|
|
243
|
+
return result;
|
|
244
|
+
}
|
|
245
|
+
if (data instanceof Map) {
|
|
246
|
+
const result = this.serializeValue(Array.from(data.entries()), segments, meta, maps, blobs);
|
|
247
|
+
meta.push(['map', ...segments]);
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (handlerEntries) {
|
|
254
|
+
for (let i = 0; i < handlerEntries.length; i++) {
|
|
255
|
+
const entry = handlerEntries[i];
|
|
256
|
+
const handler = entry[1];
|
|
257
|
+
if (handler.condition(data)) {
|
|
258
|
+
const serialized = handler.serialize(data);
|
|
259
|
+
if (handler.isTerminal) {
|
|
260
|
+
meta.push([entry[0], ...segments]);
|
|
261
|
+
if (serialized instanceof Blob) {
|
|
262
|
+
maps.push(segments.slice());
|
|
263
|
+
blobs.push(serialized);
|
|
264
|
+
}
|
|
265
|
+
return serialized;
|
|
266
|
+
}
|
|
267
|
+
const result = this.serializeValue(serialized, segments, meta, maps, blobs);
|
|
268
|
+
meta.push([entry[0], ...segments]);
|
|
269
|
+
return result;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (typeof data !== 'object' || data === null) {
|
|
274
|
+
return data;
|
|
275
|
+
}
|
|
276
|
+
if (data instanceof Blob) {
|
|
277
|
+
maps.push(segments.slice());
|
|
278
|
+
blobs.push(data);
|
|
279
|
+
return data;
|
|
280
|
+
}
|
|
281
|
+
if (Array.isArray(data)) {
|
|
282
|
+
const json = [];
|
|
283
|
+
for (let i = 0; i < data.length; i++) {
|
|
284
|
+
segments.push(i);
|
|
285
|
+
json.push(this.serializeValue(data[i], segments, meta, maps, blobs));
|
|
286
|
+
segments.pop();
|
|
287
|
+
}
|
|
288
|
+
return json;
|
|
289
|
+
}
|
|
290
|
+
if (isPlainObject(data)) {
|
|
291
|
+
const json = new NullProtoObj();
|
|
292
|
+
for (const k of Object.keys(data)) {
|
|
293
|
+
const v = data[k];
|
|
294
|
+
/**
|
|
295
|
+
* Skip custom toJSON methods to avoid JSON.stringify invoking them,
|
|
296
|
+
* which could cause meta and serialized data mismatches during deserialization.
|
|
297
|
+
* Instead, rely on custom handlers.
|
|
298
|
+
*/
|
|
299
|
+
if (k === 'toJSON' && typeof v === 'function') {
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (v === undefined && this.omitUndefinedProperties) {
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
segments.push(k);
|
|
306
|
+
json[k] = this.serializeValue(v, segments, meta, maps, blobs);
|
|
307
|
+
segments.pop();
|
|
308
|
+
}
|
|
309
|
+
return json;
|
|
310
|
+
}
|
|
311
|
+
return data;
|
|
312
|
+
}
|
|
313
|
+
deserializeJson(serialized) {
|
|
314
|
+
const ref = { json: serialized.json };
|
|
315
|
+
if (serialized.blobs?.length) {
|
|
316
|
+
for (let i = 0; i < serialized.maps.length; i++) {
|
|
317
|
+
const blob = serialized.blobs[i];
|
|
318
|
+
if (!(blob instanceof Blob)) {
|
|
319
|
+
throw invalidSerializedData(`blob ${i} is not a Blob.`);
|
|
320
|
+
}
|
|
321
|
+
const segments = serialized.maps[i];
|
|
322
|
+
let original = serialized;
|
|
323
|
+
let currentRef = ref;
|
|
324
|
+
let preSegment = 'json';
|
|
325
|
+
for (let j = 0; j < segments.length; j++) {
|
|
326
|
+
original = original[preSegment];
|
|
327
|
+
currentRef = walkInto(currentRef, preSegment, original);
|
|
328
|
+
preSegment = segments[j];
|
|
329
|
+
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
330
|
+
throw invalidSerializedData(`segment "${preSegment}" does not exist.`);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
currentRef[preSegment] = blob;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
const meta = serialized.meta;
|
|
337
|
+
if (meta) {
|
|
338
|
+
let restored;
|
|
339
|
+
/**
|
|
340
|
+
* `[segment, container, original]` per step of the previous entry's walk, reused for the shared path prefix.
|
|
341
|
+
* Only containers above its target are kept, so none can be a value a handler restored.
|
|
342
|
+
*/
|
|
343
|
+
const chain = meta.length === 1 ? undefined : [];
|
|
344
|
+
let chainDepth = 0;
|
|
345
|
+
for (const item of meta) {
|
|
346
|
+
const handler = this.handlers[item[0]];
|
|
347
|
+
if (handler === undefined) {
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
const depth = item.length - 1;
|
|
351
|
+
const shared = Math.min(depth, chainDepth);
|
|
352
|
+
let d = 0;
|
|
353
|
+
let original = serialized;
|
|
354
|
+
let currentRef = ref;
|
|
355
|
+
let preSegment = 'json';
|
|
356
|
+
if (chain !== undefined && shared > 0) {
|
|
357
|
+
d = 1;
|
|
358
|
+
while (d < shared && chain[3 * d] === item[d]) {
|
|
359
|
+
d++;
|
|
360
|
+
}
|
|
361
|
+
currentRef = chain[3 * d - 2];
|
|
362
|
+
original = chain[3 * d - 1];
|
|
363
|
+
preSegment = item[d];
|
|
364
|
+
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
365
|
+
throw invalidSerializedData(`segment "${preSegment}" does not exist.`);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
while (d < depth) {
|
|
369
|
+
original = original[preSegment];
|
|
370
|
+
currentRef = walkInto(currentRef, preSegment, original, restored);
|
|
371
|
+
if (chain !== undefined) {
|
|
372
|
+
chain[3 * d] = preSegment;
|
|
373
|
+
chain[3 * d + 1] = currentRef;
|
|
374
|
+
chain[3 * d + 2] = original;
|
|
375
|
+
}
|
|
376
|
+
preSegment = item[++d];
|
|
377
|
+
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
378
|
+
throw invalidSerializedData(`segment "${preSegment}" does not exist.`);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
chainDepth = depth;
|
|
382
|
+
const value = handler.deserialize(currentRef[preSegment]);
|
|
383
|
+
currentRef[preSegment] = value;
|
|
384
|
+
if (isWalkable(value)) {
|
|
385
|
+
;
|
|
386
|
+
(restored ??= new Set()).add(value);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return ref.json;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { StandardBody } from '@standard-server/core';
|
|
2
|
+
import type { RPCJsonSerializerOptions } from './rpc-json-serializer.ts';
|
|
3
|
+
import { RPCJsonSerializer } from './rpc-json-serializer.ts';
|
|
4
|
+
export interface RPCSerializerSerializeOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Use FormData for serialization when nested blobs are present.
|
|
7
|
+
* Does not apply to root-level Blob values.
|
|
8
|
+
*
|
|
9
|
+
* @default true
|
|
10
|
+
*/
|
|
11
|
+
useFormDataForBlobFields?: boolean | undefined;
|
|
12
|
+
}
|
|
13
|
+
export interface RPCSerializerOptions extends RPCJsonSerializerOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Default options for serialize method
|
|
16
|
+
*/
|
|
17
|
+
serialize?: RPCSerializerSerializeOptions | undefined;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Serializes and deserializes data for the RPC protocol,
|
|
21
|
+
* preserving native types like Date, BigInt, Set, and Map that plain JSON cannot represent.
|
|
22
|
+
*
|
|
23
|
+
* @see {@link https://orpc.dev/docs/rpc/serializer | RPC Serializer}
|
|
24
|
+
*/
|
|
25
|
+
export declare class RPCSerializer extends RPCJsonSerializer {
|
|
26
|
+
private readonly defaultSerializeOptions;
|
|
27
|
+
constructor(options?: RPCSerializerOptions);
|
|
28
|
+
serialize(data: unknown, options?: RPCSerializerSerializeOptions): StandardBody;
|
|
29
|
+
private serializePayload;
|
|
30
|
+
deserialize(data: StandardBody): unknown;
|
|
31
|
+
private deserializePayload;
|
|
32
|
+
}
|