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