@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.
Files changed (75) hide show
  1. package/dist/adapters/fetch/index.d.ts +3 -2
  2. package/dist/adapters/fetch/index.js +2 -2
  3. package/dist/adapters/fetch/rpc-link.d.ts +8 -8
  4. package/dist/adapters/fetch/rpc-link.js +16 -13
  5. package/dist/adapters/fetch/transport.d.ts +29 -31
  6. package/dist/adapters/fetch/transport.js +49 -63
  7. package/dist/adapters/message-port/index.d.ts +4 -3
  8. package/dist/adapters/message-port/index.js +3 -3
  9. package/dist/adapters/message-port/message-port.d.ts +13 -13
  10. package/dist/adapters/message-port/message-port.js +35 -53
  11. package/dist/adapters/message-port/rpc-link.d.ts +8 -8
  12. package/dist/adapters/message-port/rpc-link.js +17 -14
  13. package/dist/adapters/message-port/transport.d.ts +31 -34
  14. package/dist/adapters/message-port/transport.js +38 -52
  15. package/dist/adapters/standard/codec.d.ts +10 -10
  16. package/dist/adapters/standard/index.d.ts +6 -5
  17. package/dist/adapters/standard/index.js +2 -4
  18. package/dist/adapters/standard/link.d.ts +31 -31
  19. package/dist/adapters/standard/link.js +101 -85
  20. package/dist/adapters/standard/plugin.d.ts +25 -31
  21. package/dist/adapters/standard/plugin.js +14 -16
  22. package/dist/adapters/standard/rpc-link-codec.d.ts +53 -60
  23. package/dist/adapters/standard/rpc-link-codec.js +98 -102
  24. package/dist/adapters/standard/transport.d.ts +7 -7
  25. package/dist/adapters/websocket/index.d.ts +3 -2
  26. package/dist/adapters/websocket/index.js +2 -2
  27. package/dist/adapters/websocket/rpc-link.d.ts +8 -8
  28. package/dist/adapters/websocket/rpc-link.js +16 -13
  29. package/dist/adapters/websocket/transport.d.ts +101 -100
  30. package/dist/adapters/websocket/transport.js +102 -130
  31. package/dist/async-iterator-object.d.ts +2 -2
  32. package/dist/async-iterator-object.js +27 -28
  33. package/dist/client-safe.d.ts +4 -6
  34. package/dist/client-safe.js +29 -34
  35. package/dist/client.d.ts +24 -26
  36. package/dist/client.js +35 -37
  37. package/dist/consts.d.ts +1 -1
  38. package/dist/consts.js +18 -41
  39. package/dist/dynamic-link.d.ts +6 -6
  40. package/dist/dynamic-link.js +16 -16
  41. package/dist/error-utils.d.ts +3 -4
  42. package/dist/error-utils.js +47 -51
  43. package/dist/error.d.ts +79 -79
  44. package/dist/error.js +154 -166
  45. package/dist/index.d.ts +14 -53
  46. package/dist/index.js +12 -40
  47. package/dist/plugins/batch.d.ts +122 -155
  48. package/dist/plugins/batch.js +320 -391
  49. package/dist/plugins/cookie-jar.d.ts +64 -61
  50. package/dist/plugins/cookie-jar.js +208 -245
  51. package/dist/plugins/dedupe.d.ts +49 -53
  52. package/dist/plugins/dedupe.js +168 -211
  53. package/dist/plugins/index.d.ts +9 -28
  54. package/dist/plugins/index.js +8 -23
  55. package/dist/plugins/request-compression.d.ts +38 -35
  56. package/dist/plugins/request-compression.js +131 -177
  57. package/dist/plugins/response-compression.d.ts +21 -18
  58. package/dist/plugins/response-compression.js +64 -88
  59. package/dist/plugins/retry-after.d.ts +31 -38
  60. package/dist/plugins/retry-after.js +60 -85
  61. package/dist/plugins/retry.d.ts +55 -55
  62. package/dist/plugins/retry.js +118 -137
  63. package/dist/plugins/timeout.d.ts +19 -17
  64. package/dist/plugins/timeout.js +41 -36
  65. package/dist/rpc-json-serializer.d.ts +84 -84
  66. package/dist/rpc-json-serializer.js +338 -383
  67. package/dist/rpc-serializer.d.ts +20 -21
  68. package/dist/rpc-serializer.js +97 -125
  69. package/dist/types.d.ts +19 -31
  70. package/dist/utils.d.ts +18 -29
  71. package/dist/utils.js +63 -47
  72. package/package.json +5 -5
  73. package/dist/adapters/standard/codec.js +0 -1
  74. package/dist/adapters/standard/transport.js +0 -1
  75. package/dist/types.js +0 -1
@@ -1,392 +1,347 @@
1
- import { copyOnWrite, DEFAULT_MAX_BIGINT_DIGITS, isPlainObject, NullProtoObj, parseBigIntString, } from '@orpc/shared';
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
- return new TypeError(`Invalid RPC serialized data: ${detail}`);
4
+ return /* @__PURE__ */ new TypeError(`Invalid RPC serialized data: ${detail}`);
4
5
  }
5
6
  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);
7
+ if (!ok) throw invalidSerializedData(`type "${type}" expects ${expected}.`);
12
8
  }
13
9
  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;
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
- 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
- },
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
- * 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
- }
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 };