seroval 1.6.11 → 1.7.0

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/index.cjs CHANGED
@@ -1,12 +1,15 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  //#region src/core/compat.ts
3
3
  /**
4
- * References
5
- * - https://compat-table.github.io/compat-table/es6/
6
- * - MDN
4
+ * Public feature bits for `disabledFeatures`. Stays a runtime `enum` so the
5
+ * published declaration is unchanged for consumers. Its values must match
6
+ * the internal bits (checked by test/feature.test.ts).
7
7
  */
8
8
  let Feature = /* @__PURE__ */ function(Feature) {
9
9
  Feature[Feature["AggregateError"] = 1] = "AggregateError";
10
+ /**
11
+ * @deprecated Has no effect. Output always uses arrow functions.
12
+ */
10
13
  Feature[Feature["ArrowFunction"] = 2] = "ArrowFunction";
11
14
  Feature[Feature["ErrorPrototypeStack"] = 4] = "ErrorPrototypeStack";
12
15
  Feature[Feature["ObjectAssign"] = 8] = "ObjectAssign";
@@ -32,35 +35,15 @@ const SYM_TO_STRING_TAG = Symbol.toStringTag;
32
35
  const SYM_UNSCOPABLES = Symbol.unscopables;
33
36
  //#endregion
34
37
  //#region src/core/constants.ts
35
- const SYMBOL_STRING = {
36
- [0]: "Symbol.asyncIterator",
37
- [1]: "Symbol.hasInstance",
38
- [2]: "Symbol.isConcatSpreadable",
39
- [3]: "Symbol.iterator",
40
- [4]: "Symbol.match",
41
- [5]: "Symbol.matchAll",
42
- [6]: "Symbol.replace",
43
- [7]: "Symbol.search",
44
- [8]: "Symbol.species",
45
- [9]: "Symbol.split",
46
- [10]: "Symbol.toPrimitive",
47
- [11]: "Symbol.toStringTag",
48
- [12]: "Symbol.unscopables"
49
- };
50
- const INV_SYMBOL_REF = {
51
- [SYM_ASYNC_ITERATOR]: 0,
52
- [SYM_HAS_INSTANCE]: 1,
53
- [SYM_IS_CONCAT_SPREADABLE]: 2,
54
- [SYM_ITERATOR]: 3,
55
- [SYM_MATCH]: 4,
56
- [SYM_MATCH_ALL]: 5,
57
- [SYM_REPLACE]: 6,
58
- [SYM_SEARCH]: 7,
59
- [SYM_SPECIES]: 8,
60
- [SYM_SPLIT]: 9,
61
- [SYM_TO_PRIMITIVE]: 10,
62
- [SYM_TO_STRING_TAG]: 11,
63
- [SYM_UNSCOPABLES]: 12
38
+ const TEMPORAL_TYPE_NAME = {
39
+ [0]: "Instant",
40
+ [1]: "Duration",
41
+ [2]: "PlainDate",
42
+ [3]: "PlainDateTime",
43
+ [4]: "PlainMonthDay",
44
+ [5]: "PlainTime",
45
+ [6]: "PlainYearMonth",
46
+ [7]: "ZonedDateTime"
64
47
  };
65
48
  const SYMBOL_REF = {
66
49
  [0]: SYM_ASYNC_ITERATOR,
@@ -77,6 +60,18 @@ const SYMBOL_REF = {
77
60
  [11]: SYM_TO_STRING_TAG,
78
61
  [12]: SYM_UNSCOPABLES
79
62
  };
63
+ function createSymbolStringTable() {
64
+ const result = {};
65
+ for (let i = 0; i <= 12; i++) result[i] = SYMBOL_REF[i].description;
66
+ return result;
67
+ }
68
+ const SYMBOL_STRING = /* @__PURE__ */ createSymbolStringTable();
69
+ function createInverseSymbolTable() {
70
+ const result = {};
71
+ for (let i = 0; i <= 12; i++) result[SYMBOL_REF[i]] = i;
72
+ return result;
73
+ }
74
+ const INV_SYMBOL_REF = /* @__PURE__ */ createInverseSymbolTable();
80
75
  const CONSTANT_STRING = {
81
76
  [2]: "!0",
82
77
  [3]: "!1",
@@ -93,19 +88,10 @@ const CONSTANT_VAL = {
93
88
  [1]: void 0,
94
89
  [0]: null,
95
90
  [4]: -0,
96
- [5]: Number.POSITIVE_INFINITY,
97
- [6]: Number.NEGATIVE_INFINITY,
91
+ [5]: Infinity,
92
+ [6]: -Infinity,
98
93
  [7]: NaN
99
94
  };
100
- const ERROR_CONSTRUCTOR_STRING = {
101
- [0]: "Error",
102
- [1]: "EvalError",
103
- [2]: "RangeError",
104
- [3]: "ReferenceError",
105
- [4]: "SyntaxError",
106
- [5]: "TypeError",
107
- [6]: "URIError"
108
- };
109
95
  const ERROR_CONSTRUCTOR = {
110
96
  [0]: Error,
111
97
  [1]: EvalError,
@@ -115,9 +101,20 @@ const ERROR_CONSTRUCTOR = {
115
101
  [5]: TypeError,
116
102
  [6]: URIError
117
103
  };
104
+ function createErrorConstructorStringTable() {
105
+ const result = {};
106
+ for (let i = 0; i <= 6; i++) result[i] = ERROR_CONSTRUCTOR[i].name;
107
+ return result;
108
+ }
109
+ const ERROR_CONSTRUCTOR_STRING = /* @__PURE__ */ createErrorConstructorStringTable();
118
110
  //#endregion
119
111
  //#region src/core/node.ts
120
112
  function createSerovalNode(t, i, s, c, m, p, e, a, f, b, o, l) {
113
+ if (c === void 0 && m === void 0 && p === void 0 && e === void 0 && a === void 0 && f === void 0 && b === void 0 && o === void 0 && l === void 0) return {
114
+ t,
115
+ i,
116
+ s
117
+ };
121
118
  return {
122
119
  t,
123
120
  i,
@@ -136,7 +133,7 @@ function createSerovalNode(t, i, s, c, m, p, e, a, f, b, o, l) {
136
133
  //#endregion
137
134
  //#region src/core/literals.ts
138
135
  function createConstantNode(value) {
139
- return createSerovalNode(2, void 0, value, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
136
+ return createSerovalNode(2, void 0, value);
140
137
  }
141
138
  const TRUE_NODE = /* @__PURE__ */ createConstantNode(2);
142
139
  const FALSE_NODE = /* @__PURE__ */ createConstantNode(3);
@@ -149,60 +146,106 @@ const NAN_NODE = /* @__PURE__ */ createConstantNode(7);
149
146
  //#endregion
150
147
  //#region src/core/string.ts
151
148
  const MIN_JSON_STRINGIFY_LENGTH = 64;
152
- const JSON_ESCAPE_DIFFERENCES = /[\x00-\x07\x0b\x0e-\x1f<\u2028\u2029\ud800-\udfff]/;
153
- function serializeChar(str) {
154
- switch (str) {
155
- case "\"": return "\\\"";
156
- case "\\": return "\\\\";
157
- case "\n": return "\\n";
158
- case "\r": return "\\r";
159
- case "\b": return "\\b";
160
- case " ": return "\\t";
161
- case "\f": return "\\f";
162
- case "<": return "\\x3C";
163
- case "\u2028": return "\\u2028";
164
- case "\u2029": return "\\u2029";
149
+ const NEEDS_ESCAPE = /["\\\n\r\b\t\f<\u2028\u2029]/;
150
+ const JSON_ESCAPE_DIFFERENCES = /[\x00-\x07\x0b\x0e-\x1f\ud800-\udfff]/;
151
+ const LESS_THAN = /</g;
152
+ const LINE_SEPARATOR = /\u2028/g;
153
+ const PARAGRAPH_SEPARATOR = /\u2029/g;
154
+ function escapeCharCode(code) {
155
+ switch (code) {
156
+ case 34: return "\\\"";
157
+ case 92: return "\\\\";
158
+ case 10: return "\\n";
159
+ case 13: return "\\r";
160
+ case 8: return "\\b";
161
+ case 9: return "\\t";
162
+ case 12: return "\\f";
163
+ case 60: return "\\x3C";
164
+ case 8232: return "\\u2028";
165
+ case 8233: return "\\u2029";
165
166
  default: return;
166
167
  }
167
168
  }
168
169
  function serializeString(str) {
169
- if (str.length >= MIN_JSON_STRINGIFY_LENGTH && !JSON_ESCAPE_DIFFERENCES.test(str)) return JSON.stringify(str).slice(1, -1);
170
+ if (!NEEDS_ESCAPE.test(str)) return str;
171
+ if (str.length >= MIN_JSON_STRINGIFY_LENGTH && !JSON_ESCAPE_DIFFERENCES.test(str)) {
172
+ let result = JSON.stringify(str).slice(1, -1);
173
+ if (result.includes("<")) result = result.replace(LESS_THAN, "\\x3C");
174
+ if (result.includes("\u2028")) result = result.replace(LINE_SEPARATOR, "\\u2028");
175
+ if (result.includes("\u2029")) result = result.replace(PARAGRAPH_SEPARATOR, "\\u2029");
176
+ return result;
177
+ }
170
178
  let result = "";
171
179
  let lastPos = 0;
172
180
  let replacement;
173
181
  for (let i = 0, len = str.length; i < len; i++) {
174
- replacement = serializeChar(str[i]);
182
+ replacement = escapeCharCode(str.charCodeAt(i));
175
183
  if (replacement) {
176
184
  result += str.slice(lastPos, i) + replacement;
177
185
  lastPos = i + 1;
178
186
  }
179
187
  }
180
- if (lastPos === 0) result = str;
181
- else result += str.slice(lastPos);
182
- return result;
183
- }
184
- function deserializeReplacer(str) {
185
- switch (str) {
186
- case "\\\\": return "\\";
187
- case "\\\"": return "\"";
188
- case "\\n": return "\n";
189
- case "\\r": return "\r";
190
- case "\\b": return "\b";
191
- case "\\t": return " ";
192
- case "\\f": return "\f";
193
- case "\\x3C": return "<";
194
- case "\\u2028": return "\u2028";
195
- case "\\u2029": return "\u2029";
196
- default: return str;
197
- }
188
+ return result + str.slice(lastPos);
198
189
  }
190
+ const JSON_COMPATIBLE = /^(?:[^"\\\x00-\x1f]|\\["\\nrbtf])*$/;
191
+ const ESCAPE_SEQUENCE = /(\\\\|\\"|\\n|\\r|\\b|\\t|\\f|\\u2028|\\u2029|\\x3C)/g;
199
192
  function deserializeString(str) {
200
- if (typeof str === "string" && !str.includes("\\")) return str;
201
- return str.replace(/(\\\\|\\"|\\n|\\r|\\b|\\t|\\f|\\u2028|\\u2029|\\x3C)/g, deserializeReplacer);
193
+ if (typeof str !== "string") return str.replace(ESCAPE_SEQUENCE, deserializeString);
194
+ let index = str.indexOf("\\");
195
+ if (index === -1) return str;
196
+ if (JSON_COMPATIBLE.test(str)) return JSON.parse("\"" + str + "\"");
197
+ let result = "";
198
+ let lastPos = 0;
199
+ const last = str.length - 1;
200
+ while (index !== -1 && index < last) {
201
+ let replacement;
202
+ let length = 2;
203
+ switch (str.charCodeAt(index + 1)) {
204
+ case 92:
205
+ replacement = "\\";
206
+ break;
207
+ case 34:
208
+ replacement = "\"";
209
+ break;
210
+ case 110:
211
+ replacement = "\n";
212
+ break;
213
+ case 114:
214
+ replacement = "\r";
215
+ break;
216
+ case 98:
217
+ replacement = "\b";
218
+ break;
219
+ case 116:
220
+ replacement = " ";
221
+ break;
222
+ case 102:
223
+ replacement = "\f";
224
+ break;
225
+ case 120:
226
+ if (str.startsWith("3C", index + 2)) {
227
+ replacement = "<";
228
+ length = 4;
229
+ }
230
+ break;
231
+ case 117: if (str.startsWith("2028", index + 2)) {
232
+ replacement = "\u2028";
233
+ length = 6;
234
+ } else if (str.startsWith("2029", index + 2)) {
235
+ replacement = "\u2029";
236
+ length = 6;
237
+ }
238
+ }
239
+ if (replacement) {
240
+ result += str.slice(lastPos, index) + replacement;
241
+ lastPos = index + length;
242
+ index = str.indexOf("\\", lastPos);
243
+ } else index = str.indexOf("\\", index + 1);
244
+ }
245
+ return result + str.slice(lastPos);
202
246
  }
203
247
  //#endregion
204
248
  //#region src/core/errors.ts
205
- const { toString: objectToString } = Object.prototype;
206
249
  const STEP_ERROR_CODES = {
207
250
  parsing: 1,
208
251
  serialization: 2,
@@ -211,10 +254,9 @@ const STEP_ERROR_CODES = {
211
254
  function getErrorMessageProd(type) {
212
255
  return `Seroval Error (step: ${STEP_ERROR_CODES[type]})`;
213
256
  }
214
- const getErrorMessage = (type, cause) => getErrorMessageProd(type);
215
257
  var SerovalError = class extends Error {
216
258
  constructor(type, cause) {
217
- super(getErrorMessage(type, cause));
259
+ super(getErrorMessageProd(type));
218
260
  this.cause = cause;
219
261
  }
220
262
  };
@@ -257,6 +299,10 @@ var SerovalMissingInstanceError = class extends Error {
257
299
  super(getSpecificErrorMessage(4));
258
300
  }
259
301
  };
302
+ /**
303
+ * @deprecated Not thrown by seroval. A missing `createReference` key throws
304
+ * {@link SerovalMissingReferenceForIdError} instead.
305
+ */
260
306
  var SerovalMissingReferenceError = class extends Error {
261
307
  constructor(value) {
262
308
  super(getSpecificErrorMessage(5));
@@ -285,106 +331,40 @@ var SerovalConflictedNodeIdError = class extends Error {
285
331
  };
286
332
  var SerovalDepthLimitError = class extends Error {
287
333
  constructor(limit) {
288
- super(getSpecificErrorMessage(9));
334
+ super(getSpecificErrorMessage(10));
335
+ }
336
+ };
337
+ var SerovalAbortedError = class extends Error {
338
+ constructor() {
339
+ super(getSpecificErrorMessage(11));
340
+ }
341
+ };
342
+ var SerovalLiveStreamError = class extends Error {
343
+ constructor(reason) {
344
+ super(getSpecificErrorMessage(12));
345
+ this.reason = reason;
289
346
  }
290
347
  };
291
- //#endregion
292
- //#region src/core/keys.ts
293
- const REFERENCES_KEY = "__SEROVAL_REFS__";
294
- const GLOBAL_CONTEXT_R = `self.\$R`;
295
- function getCrossReferenceHeader(id) {
296
- if (id == null) return `${GLOBAL_CONTEXT_R}=${GLOBAL_CONTEXT_R}||[]`;
297
- return `(${GLOBAL_CONTEXT_R}=${GLOBAL_CONTEXT_R}||{})["${serializeString(id)}"]=[]`;
298
- }
299
- //#endregion
300
- //#region src/core/reference.ts
301
- const REFERENCE = /* @__PURE__ */ new Map();
302
- const INV_REFERENCE = /* @__PURE__ */ new Map();
303
- function createReference(id, value) {
304
- REFERENCE.set(value, id);
305
- INV_REFERENCE.set(id, value);
306
- return value;
307
- }
308
- function hasReferenceID(value) {
309
- return REFERENCE.has(value);
310
- }
311
- function hasReference(id) {
312
- return INV_REFERENCE.has(id);
313
- }
314
- function getReferenceID(value) {
315
- if (hasReferenceID(value)) return REFERENCE.get(value);
316
- throw new SerovalMissingReferenceError(value);
317
- }
318
- function getReference(id) {
319
- if (hasReference(id)) return INV_REFERENCE.get(id);
320
- throw new SerovalMissingReferenceForIdError(id);
321
- }
322
- if (typeof globalThis !== "undefined") Object.defineProperty(globalThis, REFERENCES_KEY, {
323
- value: INV_REFERENCE,
324
- configurable: true,
325
- writable: false,
326
- enumerable: false
327
- });
328
- else if (typeof window !== "undefined") Object.defineProperty(window, REFERENCES_KEY, {
329
- value: INV_REFERENCE,
330
- configurable: true,
331
- writable: false,
332
- enumerable: false
333
- });
334
- else if (typeof self !== "undefined") Object.defineProperty(self, REFERENCES_KEY, {
335
- value: INV_REFERENCE,
336
- configurable: true,
337
- writable: false,
338
- enumerable: false
339
- });
340
- else if (typeof global !== "undefined") Object.defineProperty(global, REFERENCES_KEY, {
341
- value: INV_REFERENCE,
342
- configurable: true,
343
- writable: false,
344
- enumerable: false
345
- });
346
348
  //#endregion
347
349
  //#region src/core/utils/error.ts
348
350
  function getErrorConstructor(error) {
349
- if (error instanceof EvalError) return 1;
350
- if (error instanceof RangeError) return 2;
351
- if (error instanceof ReferenceError) return 3;
352
- if (error instanceof SyntaxError) return 4;
353
- if (error instanceof TypeError) return 5;
354
- if (error instanceof URIError) return 6;
351
+ for (let i = 1; i <= 6; i++) if (error instanceof ERROR_CONSTRUCTOR[i]) return i;
355
352
  return 0;
356
353
  }
357
- function getInitialErrorOptions(error) {
358
- const construct = ERROR_CONSTRUCTOR_STRING[getErrorConstructor(error)];
359
- if (error.name !== construct) return { name: error.name };
360
- if (error.constructor.name !== construct) return { name: error.constructor.name };
361
- return {};
362
- }
363
354
  function getErrorOptions(error, features) {
364
- let options = getInitialErrorOptions(error);
365
- const names = Object.getOwnPropertyNames(error);
366
- for (let i = 0, len = names.length, name; i < len; i++) {
367
- name = names[i];
368
- if (name !== "name" && name !== "message") {
369
- if (name === "stack") {
370
- if (features & 4) {
371
- options = options || {};
372
- options[name] = error[name];
373
- }
374
- } else {
375
- options = options || {};
376
- options[name] = error[name];
377
- }
378
- }
379
- }
355
+ const options = Object.create(null);
356
+ const construct = ERROR_CONSTRUCTOR_STRING[getErrorConstructor(error)];
357
+ if (error.name !== construct) options.name = error.name;
358
+ else if (error.constructor.name !== construct) options.name = error.constructor.name;
359
+ for (const name of Object.getOwnPropertyNames(error)) if (name !== "name" && name !== "message" && (name !== "stack" || features & 4)) options[name] = error[name];
380
360
  return options;
381
361
  }
382
362
  //#endregion
383
363
  //#region src/core/utils/get-object-flag.ts
384
364
  function getObjectFlag(obj) {
365
+ if (Object.isExtensible(obj)) return 0;
385
366
  if (Object.isFrozen(obj)) return 3;
386
367
  if (Object.isSealed(obj)) return 2;
387
- if (Object.isExtensible(obj)) return 0;
388
368
  return 1;
389
369
  }
390
370
  //#endregion
@@ -419,86 +399,265 @@ function createNumberNode(value) {
419
399
  }
420
400
  if (value !== value) return NAN_NODE;
421
401
  if (Object.is(value, -0)) return NEG_ZERO_NODE;
422
- return createSerovalNode(0, void 0, value, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
402
+ return createSerovalNode(0, void 0, value);
423
403
  }
424
404
  function createStringNode(value) {
425
- return createSerovalNode(1, void 0, serializeString(value), void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
405
+ return createSerovalNode(1, void 0, serializeString(value));
426
406
  }
427
407
  function createBigIntNode(current) {
428
- return createSerovalNode(3, void 0, "" + current, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
408
+ return createSerovalNode(3, void 0, "" + current);
429
409
  }
430
410
  function createIndexedValueNode(id) {
431
- return createSerovalNode(4, id, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
411
+ return createSerovalNode(4, id);
432
412
  }
433
413
  function createDateNode(id, current) {
434
414
  const timestamp = current.valueOf();
435
- return createSerovalNode(5, id, timestamp !== timestamp ? "" : current.toISOString(), void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
415
+ return createSerovalNode(5, id, timestamp === timestamp ? current.toISOString() : "");
436
416
  }
437
417
  function createTemporalNode(id, type, current) {
438
- return createSerovalNode(36, id, current.toString(), type, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
418
+ return createSerovalNode(36, id, current.toString(), type);
439
419
  }
440
420
  function createRegExpNode(id, current) {
441
- return createSerovalNode(6, id, void 0, serializeString(current.source), current.flags, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
421
+ return createSerovalNode(6, id, void 0, serializeString(current.source), current.flags);
442
422
  }
443
423
  function createWKSymbolNode(id, current) {
444
- return createSerovalNode(17, id, INV_SYMBOL_REF[current], void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
424
+ return createSerovalNode(17, id, INV_SYMBOL_REF[current]);
445
425
  }
446
- function createReferenceNode(id, ref) {
447
- return createSerovalNode(18, id, serializeString(getReferenceID(ref)), void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
426
+ function createReferenceNode(id, referenceId) {
427
+ return createSerovalNode(18, id, serializeString(referenceId));
448
428
  }
449
429
  function createPluginNode(id, tag, value) {
450
- return createSerovalNode(25, id, value, serializeString(tag), void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
430
+ return createSerovalNode(25, id, value, serializeString(tag));
451
431
  }
452
432
  function createArrayNode(id, current, parsedItems) {
453
- return createSerovalNode(9, id, void 0, void 0, void 0, void 0, void 0, parsedItems, void 0, void 0, getObjectFlag(current), void 0);
433
+ return createSerovalNode(9, id, void 0, void 0, void 0, void 0, void 0, parsedItems, void 0, void 0, getObjectFlag(current));
454
434
  }
455
435
  function createBoxedNode(id, boxed) {
456
- return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed, void 0, void 0, void 0);
436
+ return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed);
457
437
  }
458
438
  const MAX_TYPED_ARRAY_LENGTH = 1e6;
459
- function createTypedArrayNode(id, current, buffer) {
460
- if (current.length > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
461
- return createSerovalNode(15, id, void 0, getTypedArrayName(current), void 0, void 0, void 0, void 0, buffer, current.byteOffset, void 0, current.length);
462
- }
463
- function createBigIntTypedArrayNode(id, current, buffer) {
464
- if (current.length > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
465
- return createSerovalNode(16, id, void 0, getTypedArrayName(current), void 0, void 0, void 0, void 0, buffer, current.byteOffset, void 0, current.length);
466
- }
467
- function createDataViewNode(id, current, buffer) {
468
- if (current.byteLength > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
469
- return createSerovalNode(20, id, void 0, void 0, void 0, void 0, void 0, void 0, buffer, current.byteOffset, void 0, current.byteLength);
470
- }
471
- function createErrorNode(id, current, options) {
472
- return createSerovalNode(13, id, getErrorConstructor(current), void 0, serializeString(current.message), options, void 0, void 0, void 0, void 0, void 0, void 0);
439
+ function createViewNode(type, id, current, buffer) {
440
+ const isDataView = type === 20;
441
+ const size = isDataView ? current.byteLength : current.length;
442
+ if (size > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
443
+ return createSerovalNode(type, id, void 0, isDataView ? void 0 : getTypedArrayName(current), void 0, void 0, void 0, void 0, buffer, current.byteOffset, void 0, size);
473
444
  }
474
- function createAggregateErrorNode(id, current, options) {
475
- return createSerovalNode(14, id, getErrorConstructor(current), void 0, serializeString(current.message), options, void 0, void 0, void 0, void 0, void 0, void 0);
445
+ function createErrorNode(type, id, current, options) {
446
+ return createSerovalNode(type, id, getErrorConstructor(current), void 0, serializeString(current.message), options);
476
447
  }
477
448
  function createSetNode(id, items) {
478
- return createSerovalNode(7, id, void 0, void 0, void 0, void 0, void 0, items, void 0, void 0, void 0, void 0);
449
+ return createSerovalNode(7, id, void 0, void 0, void 0, void 0, void 0, items);
479
450
  }
480
- function createIteratorFactoryInstanceNode(factory, items) {
481
- return createSerovalNode(28, void 0, void 0, void 0, void 0, void 0, void 0, [factory, items], void 0, void 0, void 0, void 0);
451
+ function createIteratorFactoryInstanceNode(type, factory, items) {
452
+ return createSerovalNode(type, void 0, void 0, void 0, void 0, void 0, void 0, [factory, items]);
482
453
  }
483
- function createAsyncIteratorFactoryInstanceNode(factory, items) {
484
- return createSerovalNode(30, void 0, void 0, void 0, void 0, void 0, void 0, [factory, items], void 0, void 0, void 0, void 0);
454
+ function createStreamConstructorNode(id, factory, sequence, live) {
455
+ return createSerovalNode(31, id, void 0, void 0, void 0, void 0, void 0, sequence, factory, void 0, void 0, live);
485
456
  }
486
- function createStreamConstructorNode(id, factory, sequence) {
487
- return createSerovalNode(31, id, void 0, void 0, void 0, void 0, void 0, sequence, factory, void 0, void 0, void 0);
488
- }
489
- function createStreamNextNode(id, parsed) {
490
- return createSerovalNode(32, id, void 0, void 0, void 0, void 0, void 0, void 0, parsed, void 0, void 0, void 0);
491
- }
492
- function createStreamThrowNode(id, parsed) {
493
- return createSerovalNode(33, id, void 0, void 0, void 0, void 0, void 0, void 0, parsed, void 0, void 0, void 0);
494
- }
495
- function createStreamReturnNode(id, parsed) {
496
- return createSerovalNode(34, id, void 0, void 0, void 0, void 0, void 0, void 0, parsed, void 0, void 0, void 0);
457
+ function createStreamEventNode(type, id, parsed) {
458
+ return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, void 0, parsed);
497
459
  }
498
460
  function createSequenceNode(id, sequence, throwAt, doneAt) {
499
461
  return createSerovalNode(35, id, throwAt, void 0, void 0, void 0, void 0, sequence, void 0, void 0, void 0, doneAt);
500
462
  }
501
463
  //#endregion
464
+ //#region src/core/live-stream.ts
465
+ function isLiveStream(value) {
466
+ return value instanceof LiveStream;
467
+ }
468
+ /**
469
+ * A single-consumer stream for incremental serialization. Unlike
470
+ * `createStream`, it keeps no history: at most one event is held, and the
471
+ * producer operation that sent it settles only when the consumer accepts it.
472
+ *
473
+ * An internal class rather than a tagged POJO: identity is checked with
474
+ * `instanceof`, which untrusted input cannot forge. The class is exported
475
+ * because bundlers cannot drop an unexported top-level class.
476
+ */
477
+ var LiveStream = class {
478
+ constructor(options) {
479
+ this.consumed = false;
480
+ this.closed = false;
481
+ this.cancelled = false;
482
+ this.options = options;
483
+ }
484
+ deliver(reader, record) {
485
+ record.delivered = true;
486
+ reader.resolve({
487
+ event: record.event,
488
+ accept: () => {
489
+ if (this.pending === record && record.delivered) {
490
+ const terminal = record.event.type !== "next";
491
+ record.event = void 0;
492
+ this.pending = void 0;
493
+ const resolve = record.resolve;
494
+ record.resolve = void 0;
495
+ record.reject = void 0;
496
+ if (terminal) {
497
+ this.closed = true;
498
+ this.options = void 0;
499
+ const waiter = this.waiting;
500
+ this.waiting = void 0;
501
+ waiter?.reject(new SerovalLiveStreamError("closed"));
502
+ }
503
+ resolve?.();
504
+ }
505
+ }
506
+ });
507
+ }
508
+ /** @internal */
509
+ push(event) {
510
+ if (this.cancelled) return Promise.reject(this.cancelReason);
511
+ if (this.closed) return Promise.reject(new SerovalLiveStreamError("closed"));
512
+ if (this.pending) return Promise.reject(new SerovalLiveStreamError("pending"));
513
+ return new Promise((resolve, reject) => {
514
+ const record = {
515
+ event,
516
+ delivered: false,
517
+ resolve,
518
+ reject
519
+ };
520
+ this.pending = record;
521
+ if (this.waiting) {
522
+ const waiter = this.waiting;
523
+ this.waiting = void 0;
524
+ this.deliver(waiter, record);
525
+ }
526
+ });
527
+ }
528
+ cancel(reason) {
529
+ if (this.cancelled || this.closed) return;
530
+ this.cancelled = true;
531
+ this.cancelReason = reason;
532
+ const record = this.pending;
533
+ const waiter = this.waiting;
534
+ this.pending = void 0;
535
+ this.waiting = void 0;
536
+ const callbackOwner = this.options;
537
+ this.options = void 0;
538
+ if (record) {
539
+ record.event = void 0;
540
+ const reject = record.reject;
541
+ record.resolve = void 0;
542
+ record.reject = void 0;
543
+ reject?.(reason);
544
+ }
545
+ if (waiter) waiter.reject(reason);
546
+ callbackOwner?.onCancel?.(reason);
547
+ }
548
+ read() {
549
+ if (this.cancelled) return Promise.reject(this.cancelReason);
550
+ if (this.closed) return Promise.reject(new SerovalLiveStreamError("closed"));
551
+ if (this.waiting) return Promise.reject(new SerovalLiveStreamError("reading"));
552
+ return new Promise((resolve, reject) => {
553
+ const next = {
554
+ resolve,
555
+ reject
556
+ };
557
+ if (this.pending && !this.pending.delivered) this.deliver(next, this.pending);
558
+ else this.waiting = next;
559
+ });
560
+ }
561
+ consume() {
562
+ if (this.consumed) throw new SerovalLiveStreamError("consumed");
563
+ this.consumed = true;
564
+ return {
565
+ read: () => this.read(),
566
+ cancel: (reason) => this.cancel(reason)
567
+ };
568
+ }
569
+ /**
570
+ * Consumes the stream through a replay-style listener, accepting each
571
+ * event as soon as it has been delivered. Use `consume` or `pump` when
572
+ * acceptance must wait for processing.
573
+ */
574
+ on(listener) {
575
+ const wrap = (key) => (value, accept) => {
576
+ listener[key](value);
577
+ accept();
578
+ };
579
+ return this.pump({
580
+ next: wrap("next"),
581
+ throw: wrap("throw"),
582
+ return: wrap("return")
583
+ });
584
+ }
585
+ /**
586
+ * Consumes the stream into a sink that controls acceptance. Returns the
587
+ * cancel function.
588
+ * @internal
589
+ */
590
+ pump(sink) {
591
+ const consumer = this.consume();
592
+ let current = sink;
593
+ function next() {
594
+ consumer.read().then((delivery) => {
595
+ const target = current;
596
+ if (!target) return;
597
+ const event = delivery.event;
598
+ let failure;
599
+ let failed = false;
600
+ try {
601
+ failure = target[event.type](event.type === "throw" ? event.error : event.value, delivery.accept);
602
+ } catch (error) {
603
+ failed = true;
604
+ failure = error;
605
+ }
606
+ if (failed || failure !== void 0) {
607
+ current = void 0;
608
+ try {
609
+ consumer.cancel(failure);
610
+ } finally {
611
+ target.done?.();
612
+ }
613
+ return;
614
+ }
615
+ if (event.type === "next") {
616
+ next();
617
+ return;
618
+ }
619
+ current = void 0;
620
+ target.done?.();
621
+ }, (reason) => {
622
+ const target = current;
623
+ current = void 0;
624
+ target?.error?.(reason);
625
+ });
626
+ }
627
+ next();
628
+ return consumer.cancel;
629
+ }
630
+ };
631
+ /**
632
+ * Creates a live stream and the producer that writes to it.
633
+ */
634
+ function createLiveStream(options) {
635
+ const stream = new LiveStream(options);
636
+ return {
637
+ stream,
638
+ producer: {
639
+ write(value) {
640
+ return stream.push({
641
+ type: "next",
642
+ value
643
+ });
644
+ },
645
+ close(value) {
646
+ return stream.push({
647
+ type: "return",
648
+ value
649
+ });
650
+ },
651
+ fail(error) {
652
+ return stream.push({
653
+ type: "throw",
654
+ error
655
+ });
656
+ }
657
+ }
658
+ };
659
+ }
660
+ //#endregion
502
661
  //#region src/core/opaque-reference.ts
503
662
  /**
504
663
  * An opaque reference allows hiding values from the serializer.
@@ -536,7 +695,7 @@ const PROMISE_FAILURE = (resolver, data) => {
536
695
  const SERIALIZED_PROMISE_CONSTRUCTOR = /* @__PURE__ */ PROMISE_CONSTRUCTOR.toString();
537
696
  const SERIALIZED_PROMISE_SUCCESS = /* @__PURE__ */ PROMISE_SUCCESS.toString();
538
697
  const SERIALIZED_PROMISE_FAILURE = /* @__PURE__ */ PROMISE_FAILURE.toString();
539
- const STREAM_CONSTRUCTOR = () => {
698
+ const STREAM_CONSTRUCTOR = (live) => {
540
699
  const buffer = [];
541
700
  const listeners = [];
542
701
  let alive = true;
@@ -544,6 +703,7 @@ const STREAM_CONSTRUCTOR = () => {
544
703
  let count = 0;
545
704
  const internal = {
546
705
  flush(value, mode, x) {
706
+ live > 1 || buffer.push(value);
547
707
  for (x = 0; x < count; x++) {
548
708
  const listener = listeners[x];
549
709
  if (listener) listener[mode](value);
@@ -557,6 +717,7 @@ const STREAM_CONSTRUCTOR = () => {
557
717
  }
558
718
  },
559
719
  on(listener, temp = 0) {
720
+ if (live > 1) throw new Error("Stream consumed");
560
721
  let subscribed = alive;
561
722
  if (alive) {
562
723
  for (temp = 0; temp < count; temp++) if (!listeners[temp]) break;
@@ -564,6 +725,10 @@ const STREAM_CONSTRUCTOR = () => {
564
725
  listeners[temp] = listener;
565
726
  }
566
727
  internal.up(listener);
728
+ if (live) {
729
+ live = 2;
730
+ buffer.length = 0;
731
+ }
567
732
  return () => {
568
733
  if (alive && subscribed) {
569
734
  subscribed = false;
@@ -580,14 +745,10 @@ const STREAM_CONSTRUCTOR = () => {
580
745
  return internal.on(listener);
581
746
  },
582
747
  next(value) {
583
- if (alive) {
584
- buffer.push(value);
585
- internal.flush(value, "next");
586
- }
748
+ if (alive) internal.flush(value, "next");
587
749
  },
588
750
  throw(value) {
589
751
  if (alive) {
590
- buffer.push(value);
591
752
  internal.flush(value, "throw");
592
753
  alive = false;
593
754
  success = false;
@@ -596,7 +757,6 @@ const STREAM_CONSTRUCTOR = () => {
596
757
  },
597
758
  return(value) {
598
759
  if (alive) {
599
- buffer.push(value);
600
760
  internal.flush(value, "return");
601
761
  alive = false;
602
762
  success = true;
@@ -718,7 +878,8 @@ const SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR = /* @__PURE__ */ ARRAY_BUFFER_CONSTRU
718
878
  //#region src/core/sequence.ts
719
879
  /**
720
880
  * An internal class rather than a tagged POJO: identity is checked with
721
- * `instanceof`, which untrusted input cannot forge (the class is not exported).
881
+ * `instanceof`, which untrusted input cannot forge. The class is exported
882
+ * because bundlers cannot drop an unexported top-level class.
722
883
  * The eval-based `deserialize` path still rebuilds a `{__SEROVAL_SEQUENCE__…}`
723
884
  * POJO from embedded source - it has no access to this class - so a value read
724
885
  * back through `deserialize` is not an instance and, by design, is not treated
@@ -742,7 +903,7 @@ function createSequenceFromIterable(source) {
742
903
  let throwsAt = -1;
743
904
  let doneAt = -1;
744
905
  const iterator = source[SYM_ITERATOR]();
745
- while (true) try {
906
+ for (;;) try {
746
907
  const value = iterator.next();
747
908
  values.push(value.value);
748
909
  if (value.done) {
@@ -757,7 +918,7 @@ function createSequenceFromIterable(source) {
757
918
  }
758
919
  return createSequence(values, throwsAt, doneAt);
759
920
  }
760
- const createIterator = ITERATOR_CONSTRUCTOR(SYM_ITERATOR);
921
+ const createIterator = /* @__PURE__ */ ITERATOR_CONSTRUCTOR(SYM_ITERATOR);
761
922
  function sequenceToIterator(sequence) {
762
923
  return createIterator(sequence);
763
924
  }
@@ -788,7 +949,8 @@ const SPECIAL_REF_STRING = {
788
949
  //#region src/core/stream.ts
789
950
  /**
790
951
  * An internal class rather than a tagged POJO: identity is checked with
791
- * `instanceof`, which untrusted input cannot forge (the class is not exported).
952
+ * `instanceof`, which untrusted input cannot forge. The class is exported
953
+ * because bundlers cannot drop an unexported top-level class.
792
954
  *
793
955
  * The behavior is intentionally duplicated from `STREAM_CONSTRUCTOR`. That
794
956
  * constructor's source is embedded verbatim into the eval-based `deserialize`
@@ -798,18 +960,17 @@ const SPECIAL_REF_STRING = {
798
960
  * implementations in sync.
799
961
  */
800
962
  var Stream = class {
801
- constructor() {
963
+ constructor(live) {
802
964
  this.buffer = [];
803
965
  this.listeners = [];
804
966
  this.alive = true;
805
967
  this.success = false;
806
968
  this.count = 0;
969
+ this.live = live;
807
970
  }
808
971
  flush(value, mode) {
809
- for (let x = 0; x < this.count; x++) {
810
- var _this$listeners$x;
811
- (_this$listeners$x = this.listeners[x]) === null || _this$listeners$x === void 0 || _this$listeners$x[mode](value);
812
- }
972
+ this.live > 1 || this.buffer.push(value);
973
+ for (let x = 0; x < this.count; x++) this.listeners[x]?.[mode](value);
813
974
  }
814
975
  replay(listener) {
815
976
  for (let x = 0, z = this.buffer.length; x < z; x++) {
@@ -819,6 +980,7 @@ var Stream = class {
819
980
  }
820
981
  }
821
982
  on(listener) {
983
+ if (this.live > 1) throw new Error("Stream consumed");
822
984
  let subscribed = this.alive;
823
985
  let temp = 0;
824
986
  if (subscribed) {
@@ -827,6 +989,10 @@ var Stream = class {
827
989
  this.listeners[temp] = listener;
828
990
  }
829
991
  this.replay(listener);
992
+ if (this.live) {
993
+ this.live = 2;
994
+ this.buffer.length = 0;
995
+ }
830
996
  return () => {
831
997
  if (this.alive && subscribed) {
832
998
  subscribed = false;
@@ -837,14 +1003,10 @@ var Stream = class {
837
1003
  };
838
1004
  }
839
1005
  next(value) {
840
- if (this.alive) {
841
- this.buffer.push(value);
842
- this.flush(value, "next");
843
- }
1006
+ if (this.alive) this.flush(value, "next");
844
1007
  }
845
1008
  throw(value) {
846
1009
  if (this.alive) {
847
- this.buffer.push(value);
848
1010
  this.flush(value, "throw");
849
1011
  this.alive = false;
850
1012
  this.success = false;
@@ -853,7 +1015,6 @@ var Stream = class {
853
1015
  }
854
1016
  return(value) {
855
1017
  if (this.alive) {
856
- this.buffer.push(value);
857
1018
  this.flush(value, "return");
858
1019
  this.alive = false;
859
1020
  this.success = true;
@@ -862,46 +1023,12 @@ var Stream = class {
862
1023
  }
863
1024
  };
864
1025
  function isStream(value) {
865
- return value instanceof Stream;
1026
+ return value instanceof Stream || isLiveStream(value);
866
1027
  }
867
1028
  function createStream() {
868
1029
  return new Stream();
869
1030
  }
870
- function createStreamFromAsyncIterable(iterable, cleanups) {
871
- const stream = createStream();
872
- const iterator = iterable[SYM_ASYNC_ITERATOR]();
873
- let cancelled = false;
874
- let done = false;
875
- cleanups === null || cleanups === void 0 || cleanups.push(() => {
876
- if (!(done || cancelled)) {
877
- cancelled = true;
878
- Promise.resolve().then(() => {
879
- var _iterator$return;
880
- return (_iterator$return = iterator.return) === null || _iterator$return === void 0 ? void 0 : _iterator$return.call(iterator);
881
- }).catch(() => {});
882
- }
883
- });
884
- async function push() {
885
- try {
886
- while (!cancelled) {
887
- const value = await iterator.next();
888
- if (cancelled) return;
889
- if (value.done) {
890
- done = true;
891
- stream.return(value.value);
892
- break;
893
- }
894
- stream.next(value.value);
895
- }
896
- } catch (error) {
897
- done = true;
898
- if (!cancelled) stream.throw(error);
899
- }
900
- }
901
- push().catch(() => {});
902
- return stream;
903
- }
904
- const createAsyncIterable = ASYNC_ITERATOR_CONSTRUCTOR(SYM_ASYNC_ITERATOR, PROMISE_CONSTRUCTOR);
1031
+ const createAsyncIterable = /* @__PURE__ */ ASYNC_ITERATOR_CONSTRUCTOR(SYM_ASYNC_ITERATOR, PROMISE_CONSTRUCTOR);
905
1032
  function streamToAsyncIterable(stream) {
906
1033
  return createAsyncIterable(stream);
907
1034
  }
@@ -915,20 +1042,158 @@ async function promiseToResult(current) {
915
1042
  }
916
1043
  }
917
1044
  //#endregion
1045
+ //#region src/core/binary-browser.ts
1046
+ function encodeArrayBuffer$1(current) {
1047
+ const bytes = new Uint8Array(current);
1048
+ if (typeof bytes.toBase64 === "function") return bytes.toBase64();
1049
+ let result = "";
1050
+ for (let i = 0, len = bytes.length; i < len; i++) result += String.fromCharCode(bytes[i]);
1051
+ return btoa(result);
1052
+ }
1053
+ //#endregion
1054
+ //#region src/core/binary-neutral.ts
1055
+ const MIN_NATIVE_BASE64_LENGTH = 512;
1056
+ function encodeArrayBuffer(current) {
1057
+ if (typeof Buffer !== "undefined") return Buffer.from(current).toString("base64");
1058
+ return encodeArrayBuffer$1(current);
1059
+ }
1060
+ function decodeArrayBuffer(source) {
1061
+ if (source.length < MIN_NATIVE_BASE64_LENGTH || typeof Buffer === "undefined") return ARRAY_BUFFER_CONSTRUCTOR(source);
1062
+ const decoded = atob(source);
1063
+ const buffer = new ArrayBuffer(decoded.length);
1064
+ Buffer.from(buffer).write(decoded, "latin1");
1065
+ return buffer;
1066
+ }
1067
+ //#endregion
1068
+ //#region src/core/plugin.ts
1069
+ let SerovalMode = /* @__PURE__ */ function(SerovalMode) {
1070
+ SerovalMode[SerovalMode["Vanilla"] = 1] = "Vanilla";
1071
+ SerovalMode[SerovalMode["Cross"] = 2] = "Cross";
1072
+ return SerovalMode;
1073
+ }({});
1074
+ function createPlugin(plugin) {
1075
+ return plugin;
1076
+ }
1077
+ function dedupePlugins(deduped, plugins) {
1078
+ for (let i = 0, len = plugins.length; i < len; i++) {
1079
+ const current = plugins[i];
1080
+ if (!deduped.has(current)) {
1081
+ deduped.add(current);
1082
+ if (current.extends) dedupePlugins(deduped, current.extends);
1083
+ }
1084
+ }
1085
+ }
1086
+ const RESOLVED_PLUGINS = /* @__PURE__ */ new WeakMap();
1087
+ function matchResolved(resolved, plugins, added) {
1088
+ for (let i = 0, len = plugins.length; i < len; i++) {
1089
+ const current = plugins[i];
1090
+ if (current === resolved[added]) {
1091
+ added++;
1092
+ if (current.extends) {
1093
+ added = matchResolved(resolved, current.extends, added);
1094
+ if (added === -1) return -1;
1095
+ }
1096
+ } else {
1097
+ const index = resolved.indexOf(current);
1098
+ if (index === -1 || index >= added) return -1;
1099
+ }
1100
+ }
1101
+ return added;
1102
+ }
1103
+ function resolvePlugins(plugins) {
1104
+ if (plugins) {
1105
+ const cached = RESOLVED_PLUGINS.get(plugins);
1106
+ if (cached && matchResolved(cached, plugins, 0) === cached.length) return cached;
1107
+ const deduped = /* @__PURE__ */ new Set();
1108
+ dedupePlugins(deduped, plugins);
1109
+ const resolved = [...deduped];
1110
+ RESOLVED_PLUGINS.set(plugins, resolved);
1111
+ return resolved;
1112
+ }
1113
+ }
1114
+ /**
1115
+ * Returns the plugins that are tested for plain objects. It is the same array
1116
+ * when no plugin sets `skipPlainObjects`.
1117
+ */
1118
+ function getPlainObjectPlugins(plugins) {
1119
+ if (plugins) {
1120
+ for (let i = 0, len = plugins.length; i < len; i++) if (plugins[i].skipPlainObjects) {
1121
+ const result = plugins.filter((plugin) => !plugin.skipPlainObjects);
1122
+ return result.length ? result : void 0;
1123
+ }
1124
+ }
1125
+ return plugins;
1126
+ }
1127
+ //#endregion
1128
+ //#region src/core/keys.ts
1129
+ const REFERENCES_KEY = "__SEROVAL_REFS__";
1130
+ const GLOBAL_CONTEXT_R = `self.\$R`;
1131
+ function getCrossReferenceHeader(id) {
1132
+ if (id == null) return `${GLOBAL_CONTEXT_R}=${GLOBAL_CONTEXT_R}||[]`;
1133
+ return `(${GLOBAL_CONTEXT_R}=${GLOBAL_CONTEXT_R}||{})["${serializeString(id)}"]=[]`;
1134
+ }
1135
+ //#endregion
1136
+ //#region src/core/reference.ts
1137
+ const REFERENCE = /* @__PURE__ */ new Map();
1138
+ const INV_REFERENCE = /* @__PURE__ */ new Map();
1139
+ let installed = false;
1140
+ function getGlobalObject() {
1141
+ if (typeof globalThis !== "undefined") return globalThis;
1142
+ if (typeof window !== "undefined") return window;
1143
+ if (typeof self !== "undefined") return self;
1144
+ if (typeof global !== "undefined") return global;
1145
+ }
1146
+ function installReferenceStore() {
1147
+ if (!installed) {
1148
+ installed = true;
1149
+ const globalObject = getGlobalObject();
1150
+ if (globalObject) Object.defineProperty(globalObject, REFERENCES_KEY, {
1151
+ value: INV_REFERENCE,
1152
+ configurable: true,
1153
+ writable: false,
1154
+ enumerable: false
1155
+ });
1156
+ }
1157
+ }
1158
+ function createReference(id, value) {
1159
+ installReferenceStore();
1160
+ REFERENCE.set(value, id);
1161
+ INV_REFERENCE.set(id, value);
1162
+ return value;
1163
+ }
1164
+ function hasReference(id) {
1165
+ return INV_REFERENCE.has(id);
1166
+ }
1167
+ function getReferenceID(value) {
1168
+ return REFERENCE.get(value);
1169
+ }
1170
+ function getReference(id) {
1171
+ if (hasReference(id)) return INV_REFERENCE.get(id);
1172
+ throw new SerovalMissingReferenceForIdError(id);
1173
+ }
1174
+ //#endregion
918
1175
  //#region src/core/context/parser.ts
919
- function createBaseParserContext(mode, options) {
920
- var _options$compactArray;
1176
+ function createBaseParserContext(options) {
921
1177
  return {
922
1178
  plugins: options.plugins,
923
- mode,
1179
+ plainObjectPlugins: getPlainObjectPlugins(options.plugins),
924
1180
  marked: /* @__PURE__ */ new Set(),
925
- features: 127 ^ (options.disabledFeatures || 0),
926
- refs: options.refs || /* @__PURE__ */ new Map(),
1181
+ features: 127 ^ (options.disabledFeatures ?? 0),
1182
+ refs: options.refs ?? /* @__PURE__ */ new Map(),
927
1183
  depthLimit: options.depthLimit || 1e3,
928
- compactArrayBufferViews: (_options$compactArray = options.compactArrayBufferViews) !== null && _options$compactArray !== void 0 ? _options$compactArray : false
1184
+ compactArrayBufferViews: options.compactArrayBufferViews ?? false
929
1185
  };
930
1186
  }
931
1187
  /**
1188
+ * Returns the plugins to test for an object. Plain objects, whose prototype is
1189
+ * `Object.prototype`, skip the plugins that set `skipPlainObjects`. Checking
1190
+ * the prototype as well as `constructor` keeps instances whose prototype has
1191
+ * no own `constructor` property from skipping those plugins.
1192
+ */
1193
+ function getObjectPlugins(ctx, current, currentClass) {
1194
+ return currentClass === Object && Object.getPrototypeOf(current) === Object.prototype ? ctx.plainObjectPlugins : ctx.plugins;
1195
+ }
1196
+ /**
932
1197
  * Ensures that the value (based on an identifier) has been visited by the parser.
933
1198
  * @param ctx
934
1199
  * @param id
@@ -950,118 +1215,166 @@ function getNodeForIndexedValue(ctx, current) {
950
1215
  const registeredId = ctx.refs.get(current);
951
1216
  if (registeredId != null) {
952
1217
  markParserRef(ctx, registeredId);
953
- return {
954
- type: 1,
955
- value: createIndexedValueNode(registeredId)
956
- };
1218
+ return createIndexedValueNode(registeredId);
957
1219
  }
958
- return {
959
- type: 0,
960
- value: createIndexForValue(ctx, current)
961
- };
1220
+ return createIndexForValue(ctx, current);
962
1221
  }
963
1222
  function getReferenceNode(ctx, current) {
964
1223
  const indexed = getNodeForIndexedValue(ctx, current);
965
- if (indexed.type === 1) return indexed;
966
- if (hasReferenceID(current)) return {
967
- type: 2,
968
- value: createReferenceNode(indexed.value, current)
969
- };
970
- return indexed;
1224
+ if (typeof indexed !== "number") return indexed;
1225
+ const referenceId = getReferenceID(current);
1226
+ return referenceId === void 0 ? indexed : createReferenceNode(indexed, referenceId);
971
1227
  }
972
1228
  /**
973
1229
  * Parsing methods
974
1230
  */
975
1231
  function parseWellKnownSymbol(ctx, current) {
976
1232
  const ref = getReferenceNode(ctx, current);
977
- if (ref.type !== 0) return ref.value;
978
- if (current in INV_SYMBOL_REF) return createWKSymbolNode(ref.value, current);
1233
+ if (typeof ref !== "number") return ref;
1234
+ if (current in INV_SYMBOL_REF) return createWKSymbolNode(ref, current);
979
1235
  throw new SerovalUnsupportedTypeError(current);
980
1236
  }
981
1237
  function parseSpecialReference(ctx, ref) {
982
1238
  const result = getNodeForIndexedValue(ctx, SPECIAL_REFS[ref]);
983
- if (result.type === 1) return result.value;
984
- return createSerovalNode(26, result.value, ref, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0, void 0);
1239
+ if (typeof result !== "number") return result;
1240
+ return createSerovalNode(26, result, ref);
985
1241
  }
986
1242
  function parseIteratorFactory(ctx) {
987
1243
  const result = getNodeForIndexedValue(ctx, ITERATOR);
988
- if (result.type === 1) return result.value;
989
- return createSerovalNode(27, result.value, void 0, void 0, void 0, void 0, void 0, void 0, parseWellKnownSymbol(ctx, SYM_ITERATOR), void 0, void 0, void 0);
1244
+ if (typeof result !== "number") return result;
1245
+ return createSerovalNode(27, result, void 0, void 0, void 0, void 0, void 0, void 0, parseWellKnownSymbol(ctx, SYM_ITERATOR));
990
1246
  }
991
1247
  function parseAsyncIteratorFactory(ctx) {
992
1248
  const result = getNodeForIndexedValue(ctx, ASYNC_ITERATOR);
993
- if (result.type === 1) return result.value;
994
- return createSerovalNode(29, result.value, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx, 1), parseWellKnownSymbol(ctx, SYM_ASYNC_ITERATOR)], void 0, void 0, void 0, void 0);
995
- }
996
- function createObjectNode(id, current, empty, record) {
997
- return createSerovalNode(empty ? 11 : 10, id, void 0, void 0, void 0, record, void 0, void 0, void 0, void 0, getObjectFlag(current), void 0);
998
- }
999
- function createMapNode(ctx, id, k, v) {
1000
- return createSerovalNode(8, id, void 0, void 0, void 0, void 0, {
1001
- k,
1002
- v
1003
- }, void 0, parseSpecialReference(ctx, 0), void 0, void 0, void 0);
1249
+ if (typeof result !== "number") return result;
1250
+ return createSerovalNode(29, result, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx, 1), parseWellKnownSymbol(ctx, SYM_ASYNC_ITERATOR)]);
1004
1251
  }
1005
- function createPromiseConstructorNode(ctx, id, resolver) {
1006
- return createSerovalNode(22, id, resolver, void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 1), void 0, void 0, void 0);
1252
+ function getTemporalType(currentClass) {
1253
+ switch (currentClass) {
1254
+ case Temporal.Instant: return 0;
1255
+ case Temporal.Duration: return 1;
1256
+ case Temporal.PlainDate: return 2;
1257
+ case Temporal.PlainDateTime: return 3;
1258
+ case Temporal.PlainMonthDay: return 4;
1259
+ case Temporal.PlainTime: return 5;
1260
+ case Temporal.PlainYearMonth: return 6;
1261
+ case Temporal.ZonedDateTime: return 7;
1262
+ default: return;
1263
+ }
1007
1264
  }
1008
- function getArrayBufferView(ctx, current) {
1009
- if (!ctx.compactArrayBufferViews) return current;
1010
- const buffer = new Uint8Array(current.buffer, current.byteOffset, current.byteLength).slice().buffer;
1011
- return new (current instanceof DataView ? DataView : getTypedArrayConstructor(getTypedArrayName(current)))(buffer);
1265
+ function getObjectClass(current) {
1266
+ const currentClass = current.constructor;
1267
+ if (currentClass !== void 0 && typeof currentClass !== "function") {
1268
+ const proto = Object.getPrototypeOf(current);
1269
+ return proto === null ? void 0 : proto.constructor;
1270
+ }
1271
+ return currentClass;
1012
1272
  }
1013
- function encodeArrayBuffer(current) {
1014
- if (typeof Buffer !== "undefined") return Buffer.from(current).toString("base64");
1015
- const bytes = new Uint8Array(current);
1016
- if (typeof bytes.toBase64 === "function") return bytes.toBase64();
1017
- let result = "";
1018
- for (let i = 0, len = bytes.length; i < len; i++) result += String.fromCharCode(bytes[i]);
1019
- return btoa(result);
1273
+ /**
1274
+ * Decides how an object is parsed. Shared by the sync, stream and async
1275
+ * parsers so the class checks and feature gates live in one place.
1276
+ */
1277
+ function getObjectKind(current, currentClass, features) {
1278
+ switch (currentClass) {
1279
+ case Object: return 1;
1280
+ case void 0: return 2;
1281
+ case Date: return 3;
1282
+ case Error:
1283
+ case EvalError:
1284
+ case RangeError:
1285
+ case ReferenceError:
1286
+ case SyntaxError:
1287
+ case TypeError:
1288
+ case URIError: return 4;
1289
+ case Number:
1290
+ case Boolean:
1291
+ case String:
1292
+ case BigInt: return 6;
1293
+ case ArrayBuffer: return 7;
1294
+ case Int8Array:
1295
+ case Int16Array:
1296
+ case Int32Array:
1297
+ case Uint8Array:
1298
+ case Uint16Array:
1299
+ case Uint32Array:
1300
+ case Uint8ClampedArray:
1301
+ case Float32Array:
1302
+ case Float64Array: return 8;
1303
+ case DataView: return 10;
1304
+ case Map: return 11;
1305
+ case Set: return 12;
1306
+ }
1307
+ if (currentClass === Promise || current instanceof Promise) return 13;
1308
+ if (features & 32 && currentClass === RegExp) return 14;
1309
+ if (features & 16 && (currentClass === BigInt64Array || currentClass === BigUint64Array)) return 9;
1310
+ if (features & 1 && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) return 5;
1311
+ if (features & 64 && typeof Temporal !== "undefined" && getTemporalType(currentClass) != null) return 15;
1312
+ if (current instanceof Error) return 4;
1313
+ if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) return 16;
1314
+ return 0;
1315
+ }
1316
+ function createObjectNode(id, current, empty, record) {
1317
+ return createSerovalNode(empty ? 11 : 10, id, void 0, void 0, void 0, record, void 0, void 0, void 0, void 0, getObjectFlag(current));
1318
+ }
1319
+ /**
1320
+ * Gets the sentinel node for a Map. Call it before parsing the entries, so
1321
+ * that an outer Map declares the sentinel before any Map inside it uses it.
1322
+ */
1323
+ function parseMapSentinel(ctx) {
1324
+ return parseSpecialReference(ctx, 0);
1325
+ }
1326
+ function createMapNode(id, sentinel, k, v) {
1327
+ return createSerovalNode(8, id, void 0, void 0, void 0, void 0, {
1328
+ k,
1329
+ v
1330
+ }, void 0, sentinel);
1331
+ }
1332
+ function createPromiseConstructorNode(ctx, id, resolver) {
1333
+ return createSerovalNode(22, id, resolver, void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 1));
1334
+ }
1335
+ function getArrayBufferView(ctx, current) {
1336
+ if (!ctx.compactArrayBufferViews) return current;
1337
+ const buffer = new Uint8Array(current.buffer, current.byteOffset, current.byteLength).slice().buffer;
1338
+ return new (current instanceof DataView ? DataView : getTypedArrayConstructor(getTypedArrayName(current)))(buffer);
1020
1339
  }
1021
1340
  function createArrayBufferNode(ctx, id, current) {
1022
- return createSerovalNode(19, id, encodeArrayBuffer(current), void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 5), void 0, void 0, void 0);
1341
+ return createSerovalNode(19, id, encodeArrayBuffer(current), void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 5));
1023
1342
  }
1024
1343
  //#endregion
1025
1344
  //#region src/core/context/async-parser.ts
1026
- function createAsyncParserContext(mode, options) {
1027
- return {
1028
- base: createBaseParserContext(mode, options),
1029
- child: void 0
1030
- };
1345
+ function createAsyncParserContext(options) {
1346
+ return { base: createBaseParserContext(options) };
1031
1347
  }
1032
- var AsyncParsePluginContext = class {
1033
- constructor(_p, depth) {
1034
- this._p = _p;
1035
- this.depth = depth;
1036
- }
1037
- parse(current) {
1038
- return parseAsync(this._p, this.depth, current);
1039
- }
1040
- };
1041
1348
  async function parseItems$1(ctx, depth, current) {
1042
- const nodes = [];
1043
- for (let i = 0, len = current.length; i < len; i++) if (i in current) nodes[i] = await parseAsync(ctx, depth, current[i]);
1044
- else nodes[i] = 0;
1349
+ const len = current.length;
1350
+ const nodes = new Array(len);
1351
+ for (let i = 0, node; i < len; i++) if (i in current) {
1352
+ node = parseValueAsync(ctx, depth, current[i]);
1353
+ nodes[i] = node instanceof Promise ? await node : node;
1354
+ } else nodes[i] = 0;
1045
1355
  return nodes;
1046
1356
  }
1047
1357
  async function parseArray$1(ctx, depth, id, current) {
1048
1358
  return createArrayNode(id, current, await parseItems$1(ctx, depth, current));
1049
1359
  }
1050
1360
  async function parseProperties$1(ctx, depth, properties) {
1051
- const entries = Object.entries(properties);
1052
- const keyNodes = [];
1053
- const valueNodes = [];
1054
- for (let i = 0, len = entries.length; i < len; i++) {
1055
- keyNodes.push(serializeString(entries[i][0]));
1056
- valueNodes.push(await parseAsync(ctx, depth, entries[i][1]));
1361
+ const keys = Object.keys(properties);
1362
+ const len = keys.length;
1363
+ const keyNodes = new Array(len);
1364
+ const valueNodes = new Array(len);
1365
+ for (let i = 0, key, node; i < len; i++) {
1366
+ key = keys[i];
1367
+ keyNodes[i] = serializeString(key);
1368
+ node = parseValueAsync(ctx, depth, properties[key]);
1369
+ valueNodes[i] = node instanceof Promise ? await node : node;
1057
1370
  }
1058
1371
  if (SYM_ITERATOR in properties) {
1059
1372
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ITERATOR));
1060
- valueNodes.push(createIteratorFactoryInstanceNode(parseIteratorFactory(ctx.base), await parseAsync(ctx, depth, createSequenceFromIterable(properties))));
1373
+ valueNodes.push(createIteratorFactoryInstanceNode(28, parseIteratorFactory(ctx.base), await parseAsync(ctx, depth, createSequenceFromIterable(properties))));
1061
1374
  }
1062
1375
  if (SYM_ASYNC_ITERATOR in properties) {
1063
1376
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ASYNC_ITERATOR));
1064
- valueNodes.push(createAsyncIteratorFactoryInstanceNode(parseAsyncIteratorFactory(ctx.base), await parseAsync(ctx, depth, createStreamFromAsyncIterable(properties))));
1377
+ valueNodes.push(createIteratorFactoryInstanceNode(30, parseAsyncIteratorFactory(ctx.base), await parseAsyncIterable$1(ctx, depth, properties)));
1065
1378
  }
1066
1379
  if (SYM_TO_STRING_TAG in properties) {
1067
1380
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_TO_STRING_TAG));
@@ -1082,166 +1395,183 @@ async function parsePlainObject$1(ctx, depth, id, current, empty) {
1082
1395
  async function parseBoxed$1(ctx, depth, id, current) {
1083
1396
  return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
1084
1397
  }
1085
- async function parseTypedArray$1(ctx, depth, id, current) {
1086
- current = getArrayBufferView(ctx.base, current);
1087
- return createTypedArrayNode(id, current, await parseAsync(ctx, depth, current.buffer));
1088
- }
1089
- async function parseBigIntTypedArray$1(ctx, depth, id, current) {
1398
+ async function parseView$1(ctx, depth, type, id, current) {
1090
1399
  current = getArrayBufferView(ctx.base, current);
1091
- return createBigIntTypedArrayNode(id, current, await parseAsync(ctx, depth, current.buffer));
1092
- }
1093
- async function parseDataView$1(ctx, depth, id, current) {
1094
- current = getArrayBufferView(ctx.base, current);
1095
- return createDataViewNode(id, current, await parseAsync(ctx, depth, current.buffer));
1096
- }
1097
- async function parseError$1(ctx, depth, id, current) {
1098
- const options = getErrorOptions(current, ctx.base.features);
1099
- return createErrorNode(id, current, options ? await parseProperties$1(ctx, depth, options) : void 0);
1400
+ return createViewNode(type, id, current, await parseAsync(ctx, depth, current.buffer));
1100
1401
  }
1101
- async function parseAggregateError$1(ctx, depth, id, current) {
1402
+ async function parseError$1(ctx, depth, type, id, current) {
1102
1403
  const options = getErrorOptions(current, ctx.base.features);
1103
- return createAggregateErrorNode(id, current, options ? await parseProperties$1(ctx, depth, options) : void 0);
1404
+ return createErrorNode(type, id, current, options ? await parseProperties$1(ctx, depth, options) : void 0);
1104
1405
  }
1105
1406
  async function parseMap$1(ctx, depth, id, current) {
1106
- const keyNodes = [];
1107
- const valueNodes = [];
1407
+ const sentinel = parseMapSentinel(ctx.base);
1408
+ const keyNodes = new Array(current.size);
1409
+ const valueNodes = new Array(current.size);
1410
+ let node;
1411
+ let i = 0;
1108
1412
  for (const [key, value] of current.entries()) {
1109
- keyNodes.push(await parseAsync(ctx, depth, key));
1110
- valueNodes.push(await parseAsync(ctx, depth, value));
1413
+ node = parseValueAsync(ctx, depth, key);
1414
+ keyNodes[i] = node instanceof Promise ? await node : node;
1415
+ node = parseValueAsync(ctx, depth, value);
1416
+ valueNodes[i] = node instanceof Promise ? await node : node;
1417
+ i++;
1111
1418
  }
1112
- return createMapNode(ctx.base, id, keyNodes, valueNodes);
1419
+ return createMapNode(id, sentinel, keyNodes, valueNodes);
1113
1420
  }
1114
1421
  async function parseSet$1(ctx, depth, id, current) {
1115
- const items = [];
1116
- for (const item of current.keys()) items.push(await parseAsync(ctx, depth, item));
1422
+ const items = new Array(current.size);
1423
+ let node;
1424
+ let i = 0;
1425
+ for (const item of current.keys()) {
1426
+ node = parseValueAsync(ctx, depth, item);
1427
+ items[i++] = node instanceof Promise ? await node : node;
1428
+ }
1117
1429
  return createSetNode(id, items);
1118
1430
  }
1119
- async function parsePlugin$1(ctx, depth, id, current) {
1120
- const currentPlugins = ctx.base.plugins;
1431
+ async function parsePlugin$1(ctx, depth, id, current, currentPlugins) {
1121
1432
  if (currentPlugins) for (let i = 0, len = currentPlugins.length; i < len; i++) {
1122
1433
  const plugin = currentPlugins[i];
1123
- if (plugin.parse.async && plugin.test(current)) return createPluginNode(id, plugin.tag, await plugin.parse.async(current, new AsyncParsePluginContext(ctx, depth), { id }));
1434
+ if (plugin.parse.async && plugin.test(current)) return createPluginNode(id, plugin.tag, await plugin.parse.async(current, {
1435
+ parse: (value) => parseAsync(ctx, depth, value),
1436
+ parseStreamSource(source) {
1437
+ if (depth >= ctx.base.depthLimit) throw new SerovalDepthLimitError(ctx.base.depthLimit);
1438
+ return parseAsyncIterable$1(ctx, depth + 1, source);
1439
+ }
1440
+ }, { id }));
1124
1441
  }
1125
1442
  }
1126
1443
  async function parsePromise$1(ctx, depth, id, current) {
1127
1444
  const [status, result] = await promiseToResult(current);
1128
- return createSerovalNode(12, id, status, void 0, void 0, void 0, void 0, void 0, await parseAsync(ctx, depth, result), void 0, void 0, void 0);
1445
+ return createSerovalNode(12, id, status, void 0, void 0, void 0, void 0, void 0, await parseAsync(ctx, depth, result));
1129
1446
  }
1130
1447
  function parseStreamHandle(depth, id, current, resolve, reject) {
1131
1448
  const sequence = [];
1132
- const cleanup = current.on({
1133
- next: (value) => {
1134
- markParserRef(this.base, id);
1135
- parseAsync(this, depth, value).then((data) => {
1136
- sequence.push(createStreamNextNode(id, data));
1137
- }, (data) => {
1138
- reject(data);
1139
- cleanup();
1140
- });
1141
- },
1142
- throw: (value) => {
1143
- markParserRef(this.base, id);
1144
- parseAsync(this, depth, value).then((data) => {
1145
- sequence.push(createStreamThrowNode(id, data));
1146
- resolve(sequence);
1147
- cleanup();
1148
- }, (data) => {
1149
- reject(data);
1150
- cleanup();
1151
- });
1152
- },
1153
- return: (value) => {
1154
- markParserRef(this.base, id);
1155
- parseAsync(this, depth, value).then((data) => {
1156
- sequence.push(createStreamReturnNode(id, data));
1157
- resolve(sequence);
1158
- cleanup();
1159
- }, (data) => {
1160
- reject(data);
1161
- cleanup();
1162
- });
1449
+ let unparsed = 0;
1450
+ let ended = false;
1451
+ let settled = false;
1452
+ const fail = (error) => {
1453
+ if (!settled) {
1454
+ settled = true;
1455
+ try {
1456
+ cleanup(error);
1457
+ } catch {}
1458
+ reject(error);
1163
1459
  }
1164
- });
1460
+ };
1461
+ const handle = (type) => (value, accept) => {
1462
+ if (settled) return;
1463
+ markParserRef(this.base, id);
1464
+ const slot = sequence.push(void 0) - 1;
1465
+ unparsed++;
1466
+ if (type !== 32) ended = true;
1467
+ parseAsync(this, depth, value).then((data) => {
1468
+ if (!settled) {
1469
+ sequence[slot] = createStreamEventNode(type, id, data);
1470
+ unparsed--;
1471
+ accept?.();
1472
+ if (ended && unparsed === 0) {
1473
+ settled = true;
1474
+ try {
1475
+ cleanup();
1476
+ } catch (error) {
1477
+ reject(error);
1478
+ return;
1479
+ }
1480
+ resolve(sequence);
1481
+ }
1482
+ }
1483
+ }, fail);
1484
+ };
1485
+ const listener = {
1486
+ next: handle(32),
1487
+ throw: handle(33),
1488
+ return: handle(34)
1489
+ };
1490
+ const cleanup = isLiveStream(current) ? current.pump(listener) : current.on(listener);
1165
1491
  }
1166
1492
  async function parseStream$1(ctx, depth, id, current) {
1167
1493
  return createStreamConstructorNode(id, parseSpecialReference(ctx.base, 4), await new Promise(parseStreamHandle.bind(ctx, depth, id, current)));
1168
1494
  }
1495
+ /**
1496
+ * Collects an async iterable into a replay node, one value at a time.
1497
+ */
1498
+ async function parseAsyncIterable$1(ctx, depth, current) {
1499
+ const id = createIndexForValue(ctx.base, {});
1500
+ const reference = parseSpecialReference(ctx.base, 4);
1501
+ const iterator = current[SYM_ASYNC_ITERATOR]();
1502
+ const sequence = [];
1503
+ for (;;) {
1504
+ let result;
1505
+ try {
1506
+ result = await iterator.next();
1507
+ } catch (error) {
1508
+ markParserRef(ctx.base, id);
1509
+ sequence.push(createStreamEventNode(33, id, await parseAsync(ctx, depth, error)));
1510
+ break;
1511
+ }
1512
+ markParserRef(ctx.base, id);
1513
+ let parsed;
1514
+ try {
1515
+ parsed = await parseAsync(ctx, depth, result.value);
1516
+ } catch (error) {
1517
+ Promise.resolve().then(() => iterator.return?.()).catch(() => {});
1518
+ throw error;
1519
+ }
1520
+ sequence.push(createStreamEventNode(result.done ? 34 : 32, id, parsed));
1521
+ if (result.done) break;
1522
+ }
1523
+ return createStreamConstructorNode(id, reference, sequence);
1524
+ }
1169
1525
  async function parseSequence$1(ctx, depth, id, current) {
1170
- const nodes = [];
1171
- for (let i = 0, len = current.v.length; i < len; i++) nodes[i] = await parseAsync(ctx, depth, current.v[i]);
1526
+ const len = current.v.length;
1527
+ const nodes = new Array(len);
1528
+ for (let i = 0, node; i < len; i++) {
1529
+ node = parseValueAsync(ctx, depth, current.v[i]);
1530
+ nodes[i] = node instanceof Promise ? await node : node;
1531
+ }
1172
1532
  return createSequenceNode(id, nodes, current.t, current.d);
1173
1533
  }
1174
1534
  async function parseObjectAsync(ctx, depth, id, current) {
1175
1535
  if (Array.isArray(current)) return parseArray$1(ctx, depth, id, current);
1176
1536
  if (isStream(current)) return parseStream$1(ctx, depth, id, current);
1177
1537
  if (isSequence(current)) return parseSequence$1(ctx, depth, id, current);
1178
- let currentClass = current.constructor;
1179
- if (currentClass !== void 0 && typeof currentClass !== "function") {
1180
- const proto = Object.getPrototypeOf(current);
1181
- currentClass = proto === null ? void 0 : proto.constructor;
1182
- }
1538
+ const currentClass = getObjectClass(current);
1183
1539
  if (currentClass === OpaqueReference) return parseAsync(ctx, depth, current.replacement);
1184
- const parsed = await parsePlugin$1(ctx, depth, id, current);
1540
+ const parsed = await parsePlugin$1(ctx, depth, id, current, getObjectPlugins(ctx.base, current, currentClass));
1185
1541
  if (parsed) return parsed;
1186
- switch (currentClass) {
1187
- case Object: return parsePlainObject$1(ctx, depth, id, current, false);
1188
- case void 0: return parsePlainObject$1(ctx, depth, id, current, true);
1189
- case Date: return createDateNode(id, current);
1190
- case Error:
1191
- case EvalError:
1192
- case RangeError:
1193
- case ReferenceError:
1194
- case SyntaxError:
1195
- case TypeError:
1196
- case URIError: return parseError$1(ctx, depth, id, current);
1197
- case Number:
1198
- case Boolean:
1199
- case String:
1200
- case BigInt: return parseBoxed$1(ctx, depth, id, current);
1201
- case ArrayBuffer: return createArrayBufferNode(ctx.base, id, current);
1202
- case Int8Array:
1203
- case Int16Array:
1204
- case Int32Array:
1205
- case Uint8Array:
1206
- case Uint16Array:
1207
- case Uint32Array:
1208
- case Uint8ClampedArray:
1209
- case Float32Array:
1210
- case Float64Array: return parseTypedArray$1(ctx, depth, id, current);
1211
- case DataView: return parseDataView$1(ctx, depth, id, current);
1212
- case Map: return parseMap$1(ctx, depth, id, current);
1213
- case Set: return parseSet$1(ctx, depth, id, current);
1214
- }
1215
- if (currentClass === Promise || current instanceof Promise) return parsePromise$1(ctx, depth, id, current);
1216
- const currentFeatures = ctx.base.features;
1217
- if (currentFeatures & 32 && currentClass === RegExp) return createRegExpNode(id, current);
1218
- if (currentFeatures & 16) switch (currentClass) {
1219
- case BigInt64Array:
1220
- case BigUint64Array: return parseBigIntTypedArray$1(ctx, depth, id, current);
1221
- }
1222
- if (currentFeatures & 1 && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) return parseAggregateError$1(ctx, depth, id, current);
1223
- if (currentFeatures & 64 && typeof Temporal !== "undefined") switch (currentClass) {
1224
- case Temporal.Instant: return createTemporalNode(id, 0, current);
1225
- case Temporal.Duration: return createTemporalNode(id, 1, current);
1226
- case Temporal.PlainDate: return createTemporalNode(id, 2, current);
1227
- case Temporal.PlainDateTime: return createTemporalNode(id, 3, current);
1228
- case Temporal.PlainMonthDay: return createTemporalNode(id, 4, current);
1229
- case Temporal.PlainTime: return createTemporalNode(id, 5, current);
1230
- case Temporal.PlainYearMonth: return createTemporalNode(id, 6, current);
1231
- case Temporal.ZonedDateTime: return createTemporalNode(id, 7, current);
1232
- }
1233
- if (current instanceof Error) return parseError$1(ctx, depth, id, current);
1234
- if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) return parsePlainObject$1(ctx, depth, id, current, !!currentClass);
1235
- throw new SerovalUnsupportedTypeError(current);
1542
+ if (currentClass === Object) return parsePlainObject$1(ctx, depth, id, current, false);
1543
+ switch (getObjectKind(current, currentClass, ctx.base.features)) {
1544
+ case 2: return parsePlainObject$1(ctx, depth, id, current, true);
1545
+ case 3: return createDateNode(id, current);
1546
+ case 4: return parseError$1(ctx, depth, 13, id, current);
1547
+ case 5: return parseError$1(ctx, depth, 14, id, current);
1548
+ case 6: return parseBoxed$1(ctx, depth, id, current);
1549
+ case 7: return createArrayBufferNode(ctx.base, id, current);
1550
+ case 8: return parseView$1(ctx, depth, 15, id, current);
1551
+ case 9: return parseView$1(ctx, depth, 16, id, current);
1552
+ case 10: return parseView$1(ctx, depth, 20, id, current);
1553
+ case 11: return parseMap$1(ctx, depth, id, current);
1554
+ case 12: return parseSet$1(ctx, depth, id, current);
1555
+ case 13: return parsePromise$1(ctx, depth, id, current);
1556
+ case 14: return createRegExpNode(id, current);
1557
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
1558
+ case 16: return parsePlainObject$1(ctx, depth, id, current, !!currentClass);
1559
+ default: throw new SerovalUnsupportedTypeError(current);
1560
+ }
1236
1561
  }
1237
1562
  async function parseFunctionAsync(ctx, depth, current) {
1238
1563
  const ref = getReferenceNode(ctx.base, current);
1239
- if (ref.type !== 0) return ref.value;
1240
- const plugin = await parsePlugin$1(ctx, depth, ref.value, current);
1564
+ if (typeof ref !== "number") return ref;
1565
+ const plugin = await parsePlugin$1(ctx, depth, ref, current, ctx.base.plugins);
1241
1566
  if (plugin) return plugin;
1242
1567
  throw new SerovalUnsupportedTypeError(current);
1243
1568
  }
1244
- async function parseAsync(ctx, depth, current) {
1569
+ /**
1570
+ * Parses primitives and already-seen values synchronously; only objects and
1571
+ * functions come back as a Promise. Errors are thrown synchronously, which
1572
+ * an async caller turns into a rejection as before.
1573
+ */
1574
+ function parseValueAsync(ctx, depth, current) {
1245
1575
  if (depth >= ctx.base.depthLimit) throw new SerovalDepthLimitError(ctx.base.depthLimit);
1246
1576
  switch (typeof current) {
1247
1577
  case "boolean": return current ? TRUE_NODE : FALSE_NODE;
@@ -1252,7 +1582,7 @@ async function parseAsync(ctx, depth, current) {
1252
1582
  case "object":
1253
1583
  if (current) {
1254
1584
  const ref = getReferenceNode(ctx.base, current);
1255
- return ref.type === 0 ? await parseObjectAsync(ctx, depth + 1, ref.value, current) : ref.value;
1585
+ return typeof ref === "number" ? parseObjectAsync(ctx, depth + 1, ref, current) : ref;
1256
1586
  }
1257
1587
  return NULL_NODE;
1258
1588
  case "symbol": return parseWellKnownSymbol(ctx.base, current);
@@ -1260,6 +1590,9 @@ async function parseAsync(ctx, depth, current) {
1260
1590
  default: throw new SerovalUnsupportedTypeError(current);
1261
1591
  }
1262
1592
  }
1593
+ async function parseAsync(ctx, depth, current) {
1594
+ return await parseValueAsync(ctx, depth, current);
1595
+ }
1263
1596
  async function parseTopAsync(ctx, current) {
1264
1597
  try {
1265
1598
  return await parseAsync(ctx, 0, current);
@@ -1268,32 +1601,6 @@ async function parseTopAsync(ctx, current) {
1268
1601
  }
1269
1602
  }
1270
1603
  //#endregion
1271
- //#region src/core/plugin.ts
1272
- let SerovalMode = /* @__PURE__ */ function(SerovalMode) {
1273
- SerovalMode[SerovalMode["Vanilla"] = 1] = "Vanilla";
1274
- SerovalMode[SerovalMode["Cross"] = 2] = "Cross";
1275
- return SerovalMode;
1276
- }({});
1277
- function createPlugin(plugin) {
1278
- return plugin;
1279
- }
1280
- function dedupePlugins(deduped, plugins) {
1281
- for (let i = 0, len = plugins.length; i < len; i++) {
1282
- const current = plugins[i];
1283
- if (!deduped.has(current)) {
1284
- deduped.add(current);
1285
- if (current.extends) dedupePlugins(deduped, current.extends);
1286
- }
1287
- }
1288
- }
1289
- function resolvePlugins(plugins) {
1290
- if (plugins) {
1291
- const deduped = /* @__PURE__ */ new Set();
1292
- dedupePlugins(deduped, plugins);
1293
- return [...deduped];
1294
- }
1295
- }
1296
- //#endregion
1297
1604
  //#region src/core/utils/valid-properties.ts
1298
1605
  function isValidKey(key) {
1299
1606
  switch (key) {
@@ -1319,7 +1626,6 @@ function isValidSymbol(symbol) {
1319
1626
  //#endregion
1320
1627
  //#region src/core/context/deserializer.ts
1321
1628
  const DEFAULT_MAX_BASE64_LENGTH = 1e6;
1322
- const MIN_NATIVE_BASE64_LENGTH = 512;
1323
1629
  const MAX_BIGINT_LENGTH = 1e4;
1324
1630
  const MAX_REGEXP_SOURCE_LENGTH = 2e4;
1325
1631
  function applyObjectFlag(obj, flag) {
@@ -1331,68 +1637,48 @@ function applyObjectFlag(obj, flag) {
1331
1637
  }
1332
1638
  }
1333
1639
  const DEFAULT_DEPTH_LIMIT = 1e3;
1334
- function createBaseDeserializerContext(mode, options) {
1335
- var _options$maxBase64Len, _options$features;
1336
- const maxBase64Length = (_options$maxBase64Len = options.maxBase64Length) !== null && _options$maxBase64Len !== void 0 ? _options$maxBase64Len : DEFAULT_MAX_BASE64_LENGTH;
1640
+ function createBaseDeserializerContext(options) {
1641
+ const maxBase64Length = options.maxBase64Length ?? DEFAULT_MAX_BASE64_LENGTH;
1337
1642
  if (!Number.isSafeInteger(maxBase64Length) || maxBase64Length < 0) throw new RangeError("maxBase64Length must be a non-negative safe integer");
1338
- const refs = options.refs || /* @__PURE__ */ new Map();
1643
+ const refs = options.refs ?? /* @__PURE__ */ new Map();
1339
1644
  if (!("types" in refs)) Object.assign(refs, { types: /* @__PURE__ */ new Map() });
1340
1645
  return {
1341
- mode,
1342
1646
  plugins: options.plugins,
1343
1647
  refs,
1344
- features: (_options$features = options.features) !== null && _options$features !== void 0 ? _options$features : 127 ^ (options.disabledFeatures || 0),
1648
+ features: options.features ?? 127 ^ (options.disabledFeatures ?? 0),
1345
1649
  depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT,
1346
1650
  maxBase64Length
1347
1651
  };
1348
1652
  }
1349
1653
  function createVanillaDeserializerContext(options) {
1350
1654
  return {
1351
- mode: 1,
1352
- base: createBaseDeserializerContext(1, options),
1353
- child: void 0,
1354
- state: { marked: new Set(options.markedRefs) }
1655
+ base: createBaseDeserializerContext(options),
1656
+ marked: new Set(options.markedRefs)
1355
1657
  };
1356
1658
  }
1357
1659
  function createCrossDeserializerContext(options) {
1358
1660
  return {
1359
- mode: 2,
1360
- base: createBaseDeserializerContext(2, options),
1361
- child: void 0
1661
+ base: createBaseDeserializerContext(options),
1662
+ marked: void 0
1362
1663
  };
1363
1664
  }
1364
- var DeserializePluginContext = class {
1365
- constructor(_p, depth) {
1366
- this._p = _p;
1367
- this.depth = depth;
1368
- }
1369
- deserialize(node) {
1370
- return deserialize$1(this._p, this.depth, node);
1371
- }
1372
- };
1373
1665
  function guardIndexedValue(ctx, id) {
1374
- if (id < 0 || !Number.isFinite(id) || !Number.isInteger(id)) throw new SerovalMalformedNodeError({
1666
+ const node = {
1375
1667
  t: 4,
1376
1668
  i: id
1377
- });
1378
- if (ctx.refs.has(id)) throw new Error("Conflicted ref id: " + id);
1669
+ };
1670
+ if (id < 0 || !Number.isInteger(id)) throw new SerovalMalformedNodeError(node);
1671
+ if (ctx.refs.has(id)) throw new SerovalConflictedNodeIdError(node);
1379
1672
  }
1380
1673
  function isThennable(value) {
1381
1674
  return !!value && (typeof value === "object" || typeof value === "function") && "then" in value && typeof value.then === "function";
1382
1675
  }
1383
- function assignIndexedValueVanilla(ctx, id, value) {
1384
- guardIndexedValue(ctx.base, id);
1385
- if (ctx.state.marked.has(id)) ctx.base.refs.set(id, value);
1386
- return value;
1387
- }
1388
- function assignIndexedValueCross(ctx, id, value) {
1676
+ function assignIndexedValue$1(ctx, id, value) {
1389
1677
  guardIndexedValue(ctx.base, id);
1390
- ctx.base.refs.set(id, value);
1678
+ const marked = ctx.marked;
1679
+ if (!marked || marked.has(id)) ctx.base.refs.set(id, value);
1391
1680
  return value;
1392
1681
  }
1393
- function assignIndexedValue$1(ctx, id, value) {
1394
- return ctx.mode === 1 ? assignIndexedValueVanilla(ctx, id, value) : assignIndexedValueCross(ctx, id, value);
1395
- }
1396
1682
  function deserializeKnownValue(node, record, key) {
1397
1683
  if (Object.hasOwn(record, key)) return record[key];
1398
1684
  throw new SerovalMalformedNodeError(node);
@@ -1449,7 +1735,8 @@ function deserializeProperties(ctx, depth, node, result) {
1449
1735
  const keys = node.k;
1450
1736
  validateNodeList(node, keys);
1451
1737
  validateNodeList(node, node.v);
1452
- if (keys.length > 0) for (let i = 0, vals = node.v, len = keys.length; i < len; i++) assignProperty(ctx, depth, result, keys[i], vals[i]);
1738
+ const len = keys.length;
1739
+ if (len > 0) for (let i = 0, vals = node.v; i < len; i++) assignProperty(ctx, depth, result, keys[i], vals[i]);
1453
1740
  return result;
1454
1741
  }
1455
1742
  function deserializeObject(ctx, depth, node) {
@@ -1463,35 +1750,8 @@ function deserializeDate(ctx, node) {
1463
1750
  }
1464
1751
  function deserializeTemporal(ctx, node) {
1465
1752
  if (!(ctx.base.features & 64)) throw new SerovalUnsupportedNodeError(node);
1466
- let value;
1467
- switch (node.c) {
1468
- case 0:
1469
- value = Temporal.Instant.from(node.s);
1470
- break;
1471
- case 1:
1472
- value = Temporal.Duration.from(node.s);
1473
- break;
1474
- case 2:
1475
- value = Temporal.PlainDate.from(node.s);
1476
- break;
1477
- case 3:
1478
- value = Temporal.PlainDateTime.from(node.s);
1479
- break;
1480
- case 4:
1481
- value = Temporal.PlainMonthDay.from(node.s);
1482
- break;
1483
- case 5:
1484
- value = Temporal.PlainTime.from(node.s);
1485
- break;
1486
- case 6:
1487
- value = Temporal.PlainYearMonth.from(node.s);
1488
- break;
1489
- case 7:
1490
- value = Temporal.ZonedDateTime.from(node.s);
1491
- break;
1492
- default: throw new SerovalMalformedNodeError(node);
1493
- }
1494
- return assignIndexedValue$1(ctx, node.i, value);
1753
+ const construct = Temporal[deserializeKnownValue(node, TEMPORAL_TYPE_NAME, node.c)];
1754
+ return assignIndexedValue$1(ctx, node.i, construct.from(node.s));
1495
1755
  }
1496
1756
  function deserializeRegExp(ctx, node) {
1497
1757
  if (ctx.base.features & 32) {
@@ -1517,32 +1777,15 @@ function deserializeMap(ctx, depth, node) {
1517
1777
  function deserializeArrayBuffer(ctx, node) {
1518
1778
  if (typeof node.s !== "string") throw new SerovalMalformedNodeError(node);
1519
1779
  if (node.s.length > ctx.base.maxBase64Length) throw new RangeError("ArrayBuffer exceeds maxBase64Length (" + ctx.base.maxBase64Length + ")");
1520
- const source = deserializeString(node.s);
1521
- let buffer;
1522
- if (source.length < MIN_NATIVE_BASE64_LENGTH || typeof Buffer === "undefined") buffer = ARRAY_BUFFER_CONSTRUCTOR(source);
1523
- else {
1524
- const decoded = atob(source);
1525
- buffer = new ArrayBuffer(decoded.length);
1526
- Buffer.from(buffer).write(decoded, "latin1");
1527
- }
1780
+ const buffer = decodeArrayBuffer(deserializeString(node.s));
1528
1781
  return assignIndexedValue$1(ctx, node.i, buffer);
1529
1782
  }
1530
- function deserializeTypedArray(ctx, depth, node) {
1531
- var _node$b;
1532
- const construct = getTypedArrayConstructor(node.c);
1533
- const source = deserialize$1(ctx, depth, node.f);
1534
- if (!(source instanceof ArrayBuffer)) throw new SerovalMalformedNodeError(node);
1535
- const offset = (_node$b = node.b) !== null && _node$b !== void 0 ? _node$b : 0;
1536
- if (offset < 0 || offset > source.byteLength) throw new SerovalMalformedNodeError(node);
1537
- return assignIndexedValue$1(ctx, node.i, new construct(source, offset, node.l));
1538
- }
1539
- function deserializeDataView(ctx, depth, node) {
1540
- var _node$b2;
1783
+ function deserializeView(ctx, depth, node, construct) {
1541
1784
  const source = deserialize$1(ctx, depth, node.f);
1542
1785
  if (!(source instanceof ArrayBuffer)) throw new SerovalMalformedNodeError(node);
1543
- const offset = (_node$b2 = node.b) !== null && _node$b2 !== void 0 ? _node$b2 : 0;
1786
+ const offset = node.b ?? 0;
1544
1787
  if (offset < 0 || offset > source.byteLength) throw new SerovalMalformedNodeError(node);
1545
- return assignIndexedValue$1(ctx, node.i, new DataView(source, offset, node.l));
1788
+ return assignIndexedValue$1(ctx, node.i, construct ? new construct(source, offset, node.l) : new DataView(source, offset, node.l));
1546
1789
  }
1547
1790
  function deserializeDictionary(ctx, depth, node, result) {
1548
1791
  if (node.p) {
@@ -1576,7 +1819,7 @@ function deserializePlugin(ctx, depth, node) {
1576
1819
  const tag = deserializeString(node.c);
1577
1820
  for (let i = 0, len = currentPlugins.length; i < len; i++) {
1578
1821
  const plugin = currentPlugins[i];
1579
- if (plugin.tag === tag) return assignIndexedValue$1(ctx, node.i, plugin.deserialize(node.s, new DeserializePluginContext(ctx, depth), { id: node.i }));
1822
+ if (plugin.tag === tag) return assignIndexedValue$1(ctx, node.i, plugin.deserialize(node.s, { deserialize: (current) => deserialize$1(ctx, depth, current) }, { id: node.i }));
1580
1823
  }
1581
1824
  }
1582
1825
  throw new SerovalMissingPluginError(node.c);
@@ -1584,6 +1827,7 @@ function deserializePlugin(ctx, depth, node) {
1584
1827
  function deserializePromiseConstructor(ctx, node) {
1585
1828
  const value = assignIndexedValue$1(ctx, node.i, assignIndexedValue$1(ctx, node.s, PROMISE_CONSTRUCTOR()).p);
1586
1829
  assignNodeType(ctx, node.s, 22);
1830
+ ctx.base.pending?.add(node.s);
1587
1831
  return value;
1588
1832
  }
1589
1833
  function deserializePromiseFulfill(ctx, depth, node) {
@@ -1594,6 +1838,7 @@ function deserializePromiseFulfill(ctx, depth, node) {
1594
1838
  if (isThennable(deserialized)) throw new SerovalMalformedNodeError(node.a[1]);
1595
1839
  if (node.t === 23) deferred.s(deserialized);
1596
1840
  else deferred.f(deserialized);
1841
+ ctx.base.pending?.delete(node.i);
1597
1842
  return;
1598
1843
  }
1599
1844
  throw new SerovalMissingInstanceError("Promise");
@@ -1611,37 +1856,21 @@ function deserializeAsyncIteratorFactoryInstance(ctx, depth, node) {
1611
1856
  return streamToAsyncIterable(source);
1612
1857
  }
1613
1858
  function deserializeStreamConstructor(ctx, depth, node) {
1614
- const result = assignIndexedValue$1(ctx, node.i, createStream());
1859
+ const result = assignIndexedValue$1(ctx, node.i, new Stream(node.l === 1 ? 1 : void 0));
1615
1860
  assignNodeType(ctx, node.i, 31);
1861
+ ctx.base.pending?.add(node.i);
1616
1862
  const items = node.a;
1617
1863
  validateNodeList(node, items);
1618
1864
  const len = items.length;
1619
1865
  if (len) for (let i = 0; i < len; i++) deserialize$1(ctx, depth, items[i]);
1620
1866
  return result;
1621
1867
  }
1622
- function deserializeStreamNext(ctx, depth, node) {
1623
- const deferred = ctx.base.refs.get(node.i);
1624
- if (deferred) {
1625
- validateNodeType(ctx, node, node.i, 31);
1626
- deferred.next(deserialize$1(ctx, depth, node.f));
1627
- return;
1628
- }
1629
- throw new SerovalMissingInstanceError("Stream");
1630
- }
1631
- function deserializeStreamThrow(ctx, depth, node) {
1632
- const deferred = ctx.base.refs.get(node.i);
1633
- if (deferred) {
1634
- validateNodeType(ctx, node, node.i, 31);
1635
- deferred.throw(deserialize$1(ctx, depth, node.f));
1636
- return;
1637
- }
1638
- throw new SerovalMissingInstanceError("Stream");
1639
- }
1640
- function deserializeStreamReturn(ctx, depth, node) {
1868
+ function deserializeStreamCall(ctx, depth, node, method) {
1641
1869
  const deferred = ctx.base.refs.get(node.i);
1642
1870
  if (deferred) {
1643
1871
  validateNodeType(ctx, node, node.i, 31);
1644
- deferred.return(deserialize$1(ctx, depth, node.f));
1872
+ deferred[method](deserialize$1(ctx, depth, node.f));
1873
+ if (method !== "next") ctx.base.pending?.delete(node.i);
1645
1874
  return;
1646
1875
  }
1647
1876
  throw new SerovalMissingInstanceError("Stream");
@@ -1660,6 +1889,7 @@ function deserializeSequence(ctx, depth, node) {
1660
1889
  const size = node.a.length;
1661
1890
  if (!(isSequenceIndex(node.s, size) && isSequenceIndex(node.l, size))) throw new SerovalMalformedNodeError(node);
1662
1891
  const result = assignIndexedValue$1(ctx, node.i, createSequence([], node.s, node.l));
1892
+ assignNodeType(ctx, node.i, 35);
1663
1893
  for (let i = 0; i < size; i++) result.v[i] = deserialize$1(ctx, depth, node.a[i]);
1664
1894
  return result;
1665
1895
  }
@@ -1684,8 +1914,8 @@ function deserialize$1(ctx, depth, node) {
1684
1914
  case 8: return deserializeMap(ctx, depth, node);
1685
1915
  case 19: return deserializeArrayBuffer(ctx, node);
1686
1916
  case 16:
1687
- case 15: return deserializeTypedArray(ctx, depth, node);
1688
- case 20: return deserializeDataView(ctx, depth, node);
1917
+ case 15: return deserializeView(ctx, depth, node, getTypedArrayConstructor(node.c));
1918
+ case 20: return deserializeView(ctx, depth, node);
1689
1919
  case 14: return deserializeAggregateError(ctx, depth, node);
1690
1920
  case 13: return deserializeError(ctx, depth, node);
1691
1921
  case 12: return deserializePromise(ctx, depth, node);
@@ -1698,9 +1928,9 @@ function deserialize$1(ctx, depth, node) {
1698
1928
  case 28: return deserializeIteratorFactoryInstance(ctx, depth, node);
1699
1929
  case 30: return deserializeAsyncIteratorFactoryInstance(ctx, depth, node);
1700
1930
  case 31: return deserializeStreamConstructor(ctx, depth, node);
1701
- case 32: return deserializeStreamNext(ctx, depth, node);
1702
- case 33: return deserializeStreamThrow(ctx, depth, node);
1703
- case 34: return deserializeStreamReturn(ctx, depth, node);
1931
+ case 32: return deserializeStreamCall(ctx, depth, node, "next");
1932
+ case 33: return deserializeStreamCall(ctx, depth, node, "throw");
1933
+ case 34: return deserializeStreamCall(ctx, depth, node, "return");
1704
1934
  case 27: return deserializeIteratorFactory(ctx, depth, node);
1705
1935
  case 29: return deserializeAsyncIteratorFactory(ctx, depth, node);
1706
1936
  case 35: return deserializeSequence(ctx, depth, node);
@@ -1715,11 +1945,31 @@ function deserializeTop(ctx, node) {
1715
1945
  throw new SerovalDeserializationError(error);
1716
1946
  }
1717
1947
  }
1948
+ /**
1949
+ * Rejects every pending promise and throws into every open stream tracked
1950
+ * by `ctx.base.pending`, then drops the bookkeeping so the context no longer
1951
+ * accepts deferred values. Does nothing once the bookkeeping is gone.
1952
+ */
1953
+ function abortDeferred(ctx, reason) {
1954
+ const pending = ctx.base.pending;
1955
+ if (pending) {
1956
+ ctx.base.pending = void 0;
1957
+ const refs = ctx.base.refs;
1958
+ const errors = [];
1959
+ for (const id of pending) try {
1960
+ const value = refs.get(id);
1961
+ if (refs.types.get(id) === 22) value.f(reason);
1962
+ else value.throw(reason);
1963
+ } catch (error) {
1964
+ errors.push(error);
1965
+ }
1966
+ if (errors.length) throw errors[0];
1967
+ }
1968
+ }
1718
1969
  //#endregion
1719
1970
  //#region src/core/function-string.ts
1720
1971
  const RETURN = () => T;
1721
- const SERIALIZED_RETURN = /* @__PURE__ */ RETURN.toString();
1722
- const IS_MODERN = /* @__PURE__ */ /=>/.test(SERIALIZED_RETURN);
1972
+ const IS_MODERN = /* @__PURE__ */ (/* @__PURE__ */ RETURN.toString()).includes("=>");
1723
1973
  function createFunction(parameters, body) {
1724
1974
  if (IS_MODERN) return (parameters.length === 1 ? parameters[0] : "(" + parameters.join(",") + ")") + "=>" + (body.startsWith("{") ? "(" + body + ")" : body);
1725
1975
  return "function(" + parameters.join(",") + "){return " + body + "}";
@@ -1747,10 +1997,13 @@ function getIdentifier(index) {
1747
1997
  }
1748
1998
  //#endregion
1749
1999
  //#region src/core/utils/is-valid-identifier.ts
1750
- const IDENTIFIER_CHECK = /^[$A-Z_][0-9A-Z_$]*$/i;
1751
2000
  function isValidIdentifier(name) {
1752
- const char = name[0];
1753
- return (char === "$" || char === "_" || char >= "A" && char <= "Z" || char >= "a" && char <= "z") && IDENTIFIER_CHECK.test(name);
2001
+ const len = name.length;
2002
+ for (let i = 0; i < len; i++) {
2003
+ const code = name.charCodeAt(i);
2004
+ if (!(code >= 97 && code <= 122 || code >= 65 && code <= 90 || code >= 48 && code <= 57 && i > 0 || code === 95 || code === 36)) return false;
2005
+ }
2006
+ return len > 0;
1754
2007
  }
1755
2008
  //#endregion
1756
2009
  //#region src/core/context/serializer.ts
@@ -1763,51 +2016,32 @@ function getAssignmentExpression(assignment) {
1763
2016
  case 4: return "Object.defineProperty(" + assignment.s + ",\"__proto__\",{value:" + assignment.k + ",configurable:!0,enumerable:!0,writable:!0})";
1764
2017
  }
1765
2018
  }
1766
- function mergeAssignments(assignments) {
1767
- const newAssignments = [];
1768
- let current = assignments[0];
1769
- for (let i = 1, len = assignments.length, item, prev = current; i < len; i++) {
1770
- item = assignments[i];
1771
- if (item.t === 0 && item.v === prev.v) current = {
1772
- t: 0,
1773
- s: item.s,
1774
- k: void 0,
1775
- v: getAssignmentExpression(current)
1776
- };
1777
- else if (item.t === 2 && item.s === prev.s) current = {
1778
- t: 2,
1779
- s: getAssignmentExpression(current),
1780
- k: item.k,
1781
- v: item.v
1782
- };
1783
- else if (item.t === 1 && item.s === prev.s) current = {
1784
- t: 1,
1785
- s: getAssignmentExpression(current),
1786
- k: void 0,
1787
- v: item.v
1788
- };
1789
- else if (item.t === 3 && item.s === prev.s) current = {
1790
- t: 3,
1791
- s: getAssignmentExpression(current),
1792
- k: item.k,
1793
- v: void 0
1794
- };
2019
+ const MAX_ASSIGNMENT_CHAIN_LENGTH = 100;
2020
+ function resolveAssignments(assignments) {
2021
+ if (!assignments.length) return void 0;
2022
+ let previous = assignments[0];
2023
+ let expression = getAssignmentExpression(previous);
2024
+ let result = "";
2025
+ let chainLength = 1;
2026
+ for (let index = 1, length = assignments.length; index < length; index++) {
2027
+ const assignment = assignments[index];
2028
+ if (chainLength === MAX_ASSIGNMENT_CHAIN_LENGTH) {
2029
+ result += expression + ",";
2030
+ expression = getAssignmentExpression(assignment);
2031
+ chainLength = 0;
2032
+ } else if (assignment.t === 0 && previous.t === 0 && assignment.v === previous.v) expression = assignment.s + "=" + expression;
2033
+ else if (assignment.t === 2 && previous.t === 2 && assignment.s === previous.s) expression += ".set(" + assignment.k + "," + assignment.v + ")";
2034
+ else if (assignment.t === 1 && previous.t === 1 && assignment.s === previous.s) expression += ".add(" + assignment.v + ")";
2035
+ else if (assignment.t === 3 && previous.t === 2 && assignment.s === previous.s) expression += ".delete(" + assignment.k + ")";
1795
2036
  else {
1796
- newAssignments.push(current);
1797
- current = item;
2037
+ result += expression + ",";
2038
+ expression = getAssignmentExpression(assignment);
2039
+ chainLength = 0;
1798
2040
  }
1799
- prev = item;
1800
- }
1801
- newAssignments.push(current);
1802
- return newAssignments;
1803
- }
1804
- function resolveAssignments(assignments) {
1805
- if (assignments.length) {
1806
- let result = "";
1807
- const merged = mergeAssignments(assignments);
1808
- for (let i = 0, len = merged.length; i < len; i++) result += getAssignmentExpression(merged[i]) + ",";
1809
- return result;
2041
+ chainLength++;
2042
+ previous = assignment;
1810
2043
  }
2044
+ return result + expression + ",";
1811
2045
  }
1812
2046
  const NULL_CONSTRUCTOR = "Object.create(null)";
1813
2047
  const SET_CONSTRUCTOR = "new Set";
@@ -1825,42 +2059,34 @@ function createBaseSerializerContext(mode, options) {
1825
2059
  mode,
1826
2060
  plugins: options.plugins,
1827
2061
  features: options.features,
1828
- marked: new Set(options.markedRefs),
2062
+ marked: Array.isArray(options.markedRefs) ? new Set(options.markedRefs) : options.markedRefs,
1829
2063
  stack: [],
1830
2064
  flags: [],
1831
- assignments: []
2065
+ assignments: [],
2066
+ assignAll: mode === 2 && !options.assignSharedRefsOnly
1832
2067
  };
1833
2068
  }
1834
2069
  function createVanillaSerializerState() {
1835
2070
  return {
1836
2071
  valid: /* @__PURE__ */ new Map(),
1837
- vars: []
2072
+ vars: [],
2073
+ sentinels: /* @__PURE__ */ new Set()
1838
2074
  };
1839
2075
  }
1840
2076
  function createVanillaSerializerContext(options) {
1841
2077
  return {
1842
2078
  mode: 1,
1843
2079
  base: createBaseSerializerContext(1, options),
1844
- state: createVanillaSerializerState(),
1845
- child: void 0
2080
+ state: createVanillaSerializerState()
1846
2081
  };
1847
2082
  }
1848
2083
  function createCrossSerializerContext(options) {
1849
2084
  return {
1850
2085
  mode: 2,
1851
2086
  base: createBaseSerializerContext(2, options),
1852
- state: options,
1853
- child: void 0
2087
+ state: options
1854
2088
  };
1855
2089
  }
1856
- var SerializePluginContext = class {
1857
- constructor(_p) {
1858
- this._p = _p;
1859
- }
1860
- serialize(node) {
1861
- return serialize$1(this._p, node);
1862
- }
1863
- };
1864
2090
  /**
1865
2091
  * Creates the reference param (identifier) from the given reference ID
1866
2092
  * Calling this function means the value has been referenced somewhere
@@ -1968,7 +2194,7 @@ function createArrayAssign(ctx, ref, index, value) {
1968
2194
  createAssignment(ctx.base, getRefParam(ctx, ref) + "[" + index + "]", value);
1969
2195
  }
1970
2196
  function createObjectAssign(ctx, ref, key, value) {
1971
- if (!isValidKey(key)) {
2197
+ if (key === "__proto__") {
1972
2198
  ctx.base.assignments.push({
1973
2199
  t: 4,
1974
2200
  s: getRefParam(ctx, ref),
@@ -1986,17 +2212,42 @@ function createSequenceAssign(ctx, ref, index, value) {
1986
2212
  * Checks if the value is in the stack. Stack here is a reference
1987
2213
  * structure to know if a object is to be accessed in a TDZ.
1988
2214
  */
2215
+ function getKeyKind(key) {
2216
+ const first = key.charCodeAt(0);
2217
+ if (first >= 48 && first <= 57) {
2218
+ const check = Number(key);
2219
+ return check >= 0 && check.toString() === key ? 2 : 0;
2220
+ }
2221
+ if (key === "Infinity") return 2;
2222
+ return isValidIdentifier(key) ? 1 : 0;
2223
+ }
1989
2224
  function isIndexedValueInStack(ctx, node) {
1990
2225
  return node.t === 4 && ctx.stack.includes(node.i);
1991
2226
  }
1992
2227
  /**
2228
+ * Checks if the node, or any node inside it, is read from more than one
2229
+ * place. Such a node must not be created inside a deferred assignment,
2230
+ * because the places that read it may run before the assignment does.
2231
+ */
2232
+ function hasMarkedNode(ctx, node) {
2233
+ if (Array.isArray(node)) {
2234
+ for (let i = 0, len = node.length; i < len; i++) if (hasMarkedNode(ctx, node[i])) return true;
2235
+ return false;
2236
+ }
2237
+ if (node == null || typeof node !== "object") return false;
2238
+ const current = node;
2239
+ if (current.t !== 4 && typeof current.i === "number" && isSerializerRefMarked(ctx, current.i)) return true;
2240
+ for (const key in current) if (hasMarkedNode(ctx, current[key])) return true;
2241
+ return false;
2242
+ }
2243
+ /**
1993
2244
  * Produces an assignment expression. `id` generates a reference
1994
2245
  * parameter (through `getRefParam`) and has the option to
1995
2246
  * return the reference parameter directly or assign a value to
1996
2247
  * it.
1997
2248
  */
1998
2249
  function assignIndexedValue(ctx, index, value) {
1999
- if (ctx.mode === 1 && !isSerializerRefMarked(ctx.base, index)) return value;
2250
+ if (!ctx.base.assignAll && !isSerializerRefMarked(ctx.base, index)) return value;
2000
2251
  /**
2001
2252
  * In cross-reference, we have to assume that
2002
2253
  * every reference are going to be referenced
@@ -2004,12 +2255,13 @@ function assignIndexedValue(ctx, index, value) {
2004
2255
  * all of it into the reference array.
2005
2256
  *
2006
2257
  * otherwise in vanilla, we only do this if it
2007
- * is actually referenced
2258
+ * is actually referenced. `assignSharedRefsOnly` gives cross-reference
2259
+ * output the vanilla behavior.
2008
2260
  */
2009
2261
  return getRefParam(ctx, index) + "=" + value;
2010
2262
  }
2011
2263
  function serializeReference(node) {
2012
- return "__SEROVAL_REFS__.get(\"" + node.s + "\")";
2264
+ return REFERENCES_KEY + ".get(\"" + node.s + "\")";
2013
2265
  }
2014
2266
  function serializeArrayItem(ctx, id, item, index) {
2015
2267
  if (item) {
@@ -2043,16 +2295,15 @@ function serializeArray(ctx, node) {
2043
2295
  }
2044
2296
  function serializeProperty(ctx, source, key, val) {
2045
2297
  if (typeof key === "string") {
2046
- const check = Number(key);
2047
- const isIdentifier = check >= 0 && check.toString() === key || isValidIdentifier(key);
2298
+ const kind = getKeyKind(key);
2048
2299
  if (isIndexedValueInStack(ctx.base, val)) {
2049
2300
  const refParam = getRefParam(ctx, val.i);
2050
2301
  markSerializerRef(ctx.base, source.i);
2051
- if (isIdentifier && check !== check) createObjectAssign(ctx, source.i, key, refParam);
2052
- else createArrayAssign(ctx, source.i, isIdentifier ? key : "\"" + key + "\"", refParam);
2302
+ if (kind === 1) createObjectAssign(ctx, source.i, key, refParam);
2303
+ else createArrayAssign(ctx, source.i, kind === 2 ? key : "\"" + key + "\"", refParam);
2053
2304
  return "";
2054
2305
  }
2055
- if (isValidKey(key)) return (isIdentifier ? key : "\"" + key + "\"") + ":" + serialize$1(ctx, val);
2306
+ if (key !== "__proto__") return (kind === 0 ? "\"" + key + "\"" : key) + ":" + serialize$1(ctx, val);
2056
2307
  return "[\"" + key + "\"]:" + serialize$1(ctx, val);
2057
2308
  }
2058
2309
  return "[" + serialize$1(ctx, key) + "]:" + serialize$1(ctx, val);
@@ -2064,7 +2315,7 @@ function serializeProperties(ctx, source, record) {
2064
2315
  const values = record.v;
2065
2316
  ctx.base.stack.push(source.i);
2066
2317
  let result = serializeProperty(ctx, source, keys[0], values[0]);
2067
- for (let i = 1, item = result; i < len; i++) {
2318
+ for (let i = 1, item; i < len; i++) {
2068
2319
  item = serializeProperty(ctx, source, keys[i], values[i]);
2069
2320
  result += (item && result && ",") + item;
2070
2321
  }
@@ -2085,18 +2336,14 @@ function serializeWithObjectAssign(ctx, source, value, serialized) {
2085
2336
  function serializeStringKeyAssignment(ctx, source, mainAssignments, key, value) {
2086
2337
  const base = ctx.base;
2087
2338
  const serialized = serialize$1(ctx, value);
2088
- const check = Number(key);
2089
- const isIdentifier = check >= 0 && check.toString() === key || isValidIdentifier(key);
2090
- if (isIndexedValueInStack(base, value)) {
2091
- if (isIdentifier && check !== check) createObjectAssign(ctx, source.i, key, serialized);
2092
- else createArrayAssign(ctx, source.i, isIdentifier ? key : "\"" + key + "\"", serialized);
2093
- } else {
2094
- const parentAssignment = base.assignments;
2095
- base.assignments = mainAssignments;
2096
- if (isIdentifier && check !== check) createObjectAssign(ctx, source.i, key, serialized);
2097
- else createArrayAssign(ctx, source.i, isIdentifier ? key : "\"" + key + "\"", serialized);
2098
- base.assignments = parentAssignment;
2099
- }
2339
+ const parentAssignment = base.assignments;
2340
+ if (!isIndexedValueInStack(base, value)) base.assignments = mainAssignments;
2341
+ createKeyAssign(ctx, source.i, getKeyKind(key), key, serialized);
2342
+ base.assignments = parentAssignment;
2343
+ }
2344
+ function createKeyAssign(ctx, ref, kind, key, value) {
2345
+ if (kind === 1) createObjectAssign(ctx, ref, key, value);
2346
+ else createArrayAssign(ctx, ref, kind === 2 ? key : "\"" + key + "\"", value);
2100
2347
  }
2101
2348
  function serializeAssignment(ctx, source, mainAssignments, key, value) {
2102
2349
  if (typeof key === "string") serializeStringKeyAssignment(ctx, source, mainAssignments, key, value);
@@ -2127,7 +2374,7 @@ function serializeAssignments(ctx, source, node) {
2127
2374
  function serializeDictionary(ctx, node, init) {
2128
2375
  if (node.p) {
2129
2376
  const base = ctx.base;
2130
- if (base.features & 8) init = serializeWithObjectAssign(ctx, node, node.p, init);
2377
+ if (base.features & 8 && !node.p.k.includes("__proto__")) init = serializeWithObjectAssign(ctx, node, node.p, init);
2131
2378
  else {
2132
2379
  markSerializerRef(base, node.i);
2133
2380
  const assignments = serializeAssignments(ctx, node, node.p);
@@ -2143,18 +2390,8 @@ function serializeNullConstructor(ctx, node) {
2143
2390
  function serializeDate(node) {
2144
2391
  return "new Date(\"" + node.s + "\")";
2145
2392
  }
2146
- const TEMPORAL_CONSTRUCTOR = {
2147
- [0]: "Temporal.Instant",
2148
- [1]: "Temporal.Duration",
2149
- [2]: "Temporal.PlainDate",
2150
- [3]: "Temporal.PlainDateTime",
2151
- [4]: "Temporal.PlainMonthDay",
2152
- [5]: "Temporal.PlainTime",
2153
- [6]: "Temporal.PlainYearMonth",
2154
- [7]: "Temporal.ZonedDateTime"
2155
- };
2156
2393
  function serializeTemporal(ctx, node) {
2157
- if (ctx.base.features & 64) return TEMPORAL_CONSTRUCTOR[node.c] + ".from(\"" + node.s + "\")";
2394
+ if (ctx.base.features & 64) return "Temporal." + TEMPORAL_TYPE_NAME[node.c] + ".from(\"" + node.s + "\")";
2158
2395
  throw new SerovalUnsupportedNodeError(node);
2159
2396
  }
2160
2397
  function serializeRegExp(ctx, node) {
@@ -2181,7 +2418,7 @@ function serializeSet(ctx, node) {
2181
2418
  if (size > 0) {
2182
2419
  ctx.base.stack.push(id);
2183
2420
  let result = serializeSetItem(ctx, id, items[0]);
2184
- for (let i = 1, item = result; i < size; i++) {
2421
+ for (let i = 1, item; i < size; i++) {
2185
2422
  item = serializeSetItem(ctx, id, items[i]);
2186
2423
  result += (item && result && ",") + item;
2187
2424
  }
@@ -2190,7 +2427,7 @@ function serializeSet(ctx, node) {
2190
2427
  }
2191
2428
  return serialized;
2192
2429
  }
2193
- function serializeMapEntry(ctx, id, key, val, sentinel) {
2430
+ function serializeMapEntry(ctx, id, key, val, getSentinel) {
2194
2431
  const base = ctx.base;
2195
2432
  if (isIndexedValueInStack(base, key)) {
2196
2433
  const keyRef = getRefParam(ctx, key.i);
@@ -2199,7 +2436,9 @@ function serializeMapEntry(ctx, id, key, val, sentinel) {
2199
2436
  createSetAssignment(ctx, id, keyRef, getRefParam(ctx, val.i));
2200
2437
  return "";
2201
2438
  }
2202
- if (val.t !== 4 && val.i != null && isSerializerRefMarked(base, val.i)) {
2439
+ if (val.t !== 4 && val.i != null && hasMarkedNode(base, val)) {
2440
+ markSerializerRef(base, val.i);
2441
+ const sentinel = getSentinel();
2203
2442
  const serialized = "(" + serialize$1(ctx, val) + ",[" + sentinel + "," + sentinel + "])";
2204
2443
  createSetAssignment(ctx, id, keyRef, getRefParam(ctx, val.i));
2205
2444
  createDeleteAssignment(ctx, id, sentinel);
@@ -2214,7 +2453,9 @@ function serializeMapEntry(ctx, id, key, val, sentinel) {
2214
2453
  if (isIndexedValueInStack(base, val)) {
2215
2454
  const valueRef = getRefParam(ctx, val.i);
2216
2455
  markSerializerRef(base, id);
2217
- if (key.t !== 4 && key.i != null && isSerializerRefMarked(base, key.i)) {
2456
+ if (key.t !== 4 && key.i != null && hasMarkedNode(base, key)) {
2457
+ markSerializerRef(base, key.i);
2458
+ const sentinel = getSentinel();
2218
2459
  const serialized = "(" + serialize$1(ctx, key) + ",[" + sentinel + "," + sentinel + "])";
2219
2460
  createSetAssignment(ctx, id, getRefParam(ctx, key.i), valueRef);
2220
2461
  createDeleteAssignment(ctx, id, sentinel);
@@ -2234,20 +2475,27 @@ function serializeMap(ctx, node) {
2234
2475
  const size = keys.length;
2235
2476
  const id = node.i;
2236
2477
  const sentinel = node.f;
2237
- const sentinelId = getRefParam(ctx, sentinel.i);
2478
+ let sentinelRef;
2479
+ const getSentinel = () => {
2480
+ if (sentinelRef == null) {
2481
+ sentinelRef = getRefParam(ctx, sentinel.i);
2482
+ if (ctx.mode === 1) ctx.state.sentinels.add(ctx.state.valid.get(sentinel.i));
2483
+ }
2484
+ return sentinelRef;
2485
+ };
2238
2486
  const base = ctx.base;
2239
2487
  if (size > 0) {
2240
2488
  const vals = node.e.v;
2241
2489
  base.stack.push(id);
2242
- let result = serializeMapEntry(ctx, id, keys[0], vals[0], sentinelId);
2243
- for (let i = 1, item = result; i < size; i++) {
2244
- item = serializeMapEntry(ctx, id, keys[i], vals[i], sentinelId);
2490
+ let result = serializeMapEntry(ctx, id, keys[0], vals[0], getSentinel);
2491
+ for (let i = 1, item; i < size; i++) {
2492
+ item = serializeMapEntry(ctx, id, keys[i], vals[i], getSentinel);
2245
2493
  result += (item && result && ",") + item;
2246
2494
  }
2247
2495
  base.stack.pop();
2248
2496
  if (result) serialized += "([" + result + "])";
2249
2497
  }
2250
- if (sentinel.t === 26) {
2498
+ if (ctx.mode === 2 && sentinel.t === 26) {
2251
2499
  markSerializerRef(base, sentinel.i);
2252
2500
  serialized = "(" + serialize$1(ctx, sentinel) + "," + serialized + ")";
2253
2501
  }
@@ -2256,11 +2504,8 @@ function serializeMap(ctx, node) {
2256
2504
  function serializeArrayBuffer(ctx, node) {
2257
2505
  return getConstructor(ctx, node.f) + "(\"" + node.s + "\")";
2258
2506
  }
2259
- function serializeTypedArray(ctx, node) {
2260
- return "new " + node.c + "(" + serialize$1(ctx, node.f) + "," + node.b + "," + node.l + ")";
2261
- }
2262
- function serializeDataView(ctx, node) {
2263
- return "new DataView(" + serialize$1(ctx, node.f) + "," + node.b + "," + node.l + ")";
2507
+ function serializeView(ctx, node) {
2508
+ return "new " + (node.t === 20 ? "DataView" : node.c) + "(" + serialize$1(ctx, node.f) + "," + node.b + "," + node.l + ")";
2264
2509
  }
2265
2510
  function serializeAggregateError(ctx, node) {
2266
2511
  const id = node.i;
@@ -2300,11 +2545,7 @@ function serializePromiseConstructor(ctx, node) {
2300
2545
  if (ctx.mode === 1) throw new SerovalUnsupportedNodeError(node);
2301
2546
  return "(" + assignIndexedValue(ctx, node.s, getConstructor(ctx, node.f) + "()") + ").p";
2302
2547
  }
2303
- function serializePromiseResolve(ctx, node) {
2304
- if (ctx.mode === 1) throw new SerovalUnsupportedNodeError(node);
2305
- return getConstructor(ctx, node.a[0]) + "(" + getRefParam(ctx, node.i) + "," + serialize$1(ctx, node.a[1]) + ")";
2306
- }
2307
- function serializePromiseReject(ctx, node) {
2548
+ function serializePromiseResult(ctx, node) {
2308
2549
  if (ctx.mode === 1) throw new SerovalUnsupportedNodeError(node);
2309
2550
  return getConstructor(ctx, node.a[0]) + "(" + getRefParam(ctx, node.i) + "," + serialize$1(ctx, node.a[1]) + ")";
2310
2551
  }
@@ -2312,10 +2553,7 @@ function serializePlugin(ctx, node) {
2312
2553
  const currentPlugins = ctx.base.plugins;
2313
2554
  if (currentPlugins) for (let i = 0, len = currentPlugins.length; i < len; i++) {
2314
2555
  const plugin = currentPlugins[i];
2315
- if (plugin.tag === node.c) {
2316
- if (ctx.child == null) ctx.child = new SerializePluginContext(ctx);
2317
- return plugin.serialize(node.s, ctx.child, { id: node.i });
2318
- }
2556
+ if (plugin.tag === node.c) return plugin.serialize(node.s, { serialize: (current) => serialize$1(ctx, current) }, { id: node.i });
2319
2557
  }
2320
2558
  throw new SerovalMissingPluginError(node.c);
2321
2559
  }
@@ -2352,11 +2590,8 @@ function serializeAsyncIteratorFactory(ctx, node) {
2352
2590
  if (result) return result + iterator + ")";
2353
2591
  return iterator;
2354
2592
  }
2355
- function serializeAsyncIteratorFactoryInstance(ctx, node) {
2356
- return getConstructor(ctx, node.a[0]) + "(" + serialize$1(ctx, node.a[1]) + ")";
2357
- }
2358
2593
  function serializeStreamConstructor(ctx, node) {
2359
- const result = assignIndexedValue(ctx, node.i, getConstructor(ctx, node.f) + "()");
2594
+ const result = assignIndexedValue(ctx, node.i, getConstructor(ctx, node.f) + (node.l === 1 ? "(1)" : "()"));
2360
2595
  const len = node.a.length;
2361
2596
  if (len) {
2362
2597
  let values = serialize$1(ctx, node.a[0]);
@@ -2365,14 +2600,8 @@ function serializeStreamConstructor(ctx, node) {
2365
2600
  }
2366
2601
  return result;
2367
2602
  }
2368
- function serializeStreamNext(ctx, node) {
2369
- return getRefParam(ctx, node.i) + ".next(" + serialize$1(ctx, node.f) + ")";
2370
- }
2371
- function serializeStreamThrow(ctx, node) {
2372
- return getRefParam(ctx, node.i) + ".throw(" + serialize$1(ctx, node.f) + ")";
2373
- }
2374
- function serializeStreamReturn(ctx, node) {
2375
- return getRefParam(ctx, node.i) + ".return(" + serialize$1(ctx, node.f) + ")";
2603
+ function serializeStreamCall(ctx, node, method) {
2604
+ return getRefParam(ctx, node.i) + "." + method + "(" + serialize$1(ctx, node.f) + ")";
2376
2605
  }
2377
2606
  function serializeSequenceItem(ctx, id, index, item) {
2378
2607
  const base = ctx.base;
@@ -2390,7 +2619,7 @@ function serializeSequence(ctx, node) {
2390
2619
  if (size > 0) {
2391
2620
  ctx.base.stack.push(id);
2392
2621
  let result = serializeSequenceItem(ctx, id, 0, items[0]);
2393
- for (let i = 1, item = result; i < size; i++) {
2622
+ for (let i = 1, item; i < size; i++) {
2394
2623
  item = serializeSequenceItem(ctx, id, i, items[i]);
2395
2624
  result += (item && result && ",") + item;
2396
2625
  }
@@ -2412,8 +2641,8 @@ function serializeAssignable(ctx, node) {
2412
2641
  case 8: return serializeMap(ctx, node);
2413
2642
  case 19: return serializeArrayBuffer(ctx, node);
2414
2643
  case 16:
2415
- case 15: return serializeTypedArray(ctx, node);
2416
- case 20: return serializeDataView(ctx, node);
2644
+ case 15:
2645
+ case 20: return serializeView(ctx, node);
2417
2646
  case 14: return serializeAggregateError(ctx, node);
2418
2647
  case 13: return serializeError(ctx, node);
2419
2648
  case 12: return serializePromise(ctx, node);
@@ -2433,20 +2662,20 @@ function serialize$1(ctx, node) {
2433
2662
  case 1: return "\"" + node.s + "\"";
2434
2663
  case 3: return node.s + "n";
2435
2664
  case 4: return getRefParam(ctx, node.i);
2436
- case 23: return serializePromiseResolve(ctx, node);
2437
- case 24: return serializePromiseReject(ctx, node);
2665
+ case 23:
2666
+ case 24: return serializePromiseResult(ctx, node);
2438
2667
  case 27: return serializeIteratorFactory(ctx, node);
2439
- case 28: return serializeIteratorFactoryInstance(ctx, node);
2668
+ case 28:
2669
+ case 30: return serializeIteratorFactoryInstance(ctx, node);
2440
2670
  case 29: return serializeAsyncIteratorFactory(ctx, node);
2441
- case 30: return serializeAsyncIteratorFactoryInstance(ctx, node);
2442
2671
  case 31: return serializeStreamConstructor(ctx, node);
2443
- case 32: return serializeStreamNext(ctx, node);
2444
- case 33: return serializeStreamThrow(ctx, node);
2445
- case 34: return serializeStreamReturn(ctx, node);
2672
+ case 32: return serializeStreamCall(ctx, node, "next");
2673
+ case 33: return serializeStreamCall(ctx, node, "throw");
2674
+ case 34: return serializeStreamCall(ctx, node, "return");
2446
2675
  default: return assignIndexedValue(ctx, node.i, serializeAssignable(ctx, node));
2447
2676
  }
2448
2677
  }
2449
- function serializeTopVanilla(ctx, tree) {
2678
+ function serializeVanilla(ctx, tree) {
2450
2679
  const result = serialize$1(ctx, tree);
2451
2680
  if (tree.i != null && ctx.state.vars.length) {
2452
2681
  const patches = resolvePatches(ctx.base);
@@ -2457,12 +2686,18 @@ function serializeTopVanilla(ctx, tree) {
2457
2686
  if (!result.startsWith(index + "=")) body = index + "=" + body;
2458
2687
  body = "(" + body + ")";
2459
2688
  }
2689
+ const sentinels = ctx.state.sentinels;
2690
+ if (sentinels.size) {
2691
+ let declarations = "";
2692
+ for (const index of sentinels) declarations += ctx.state.vars[index] + "=[],";
2693
+ body = "(" + declarations + body + ")";
2694
+ }
2460
2695
  return "(" + createFunction(ctx.state.vars, body) + ")()";
2461
2696
  }
2462
2697
  if (tree.t === 10) return "(" + result + ")";
2463
2698
  return result;
2464
2699
  }
2465
- function serializeTopCross(ctx, tree) {
2700
+ function serializeCross(ctx, tree) {
2466
2701
  const result = serialize$1(ctx, tree);
2467
2702
  const id = tree.i;
2468
2703
  if (id == null) return result;
@@ -2470,7 +2705,11 @@ function serializeTopCross(ctx, tree) {
2470
2705
  const ref = getRefParam(ctx, id);
2471
2706
  const scopeId = ctx.state.scopeId;
2472
2707
  const params = scopeId == null ? "" : "$R";
2473
- const body = patches ? "(" + result + "," + patches + ref + ")" : result;
2708
+ let body = result;
2709
+ if (patches) {
2710
+ if (!ctx.base.assignAll && !result.startsWith(ref + "=")) body = ref + "=" + result;
2711
+ body = "(" + body + "," + patches + ref + ")";
2712
+ }
2474
2713
  if (params === "") {
2475
2714
  if (tree.t === 10 && !patches) return "(" + body + ")";
2476
2715
  return body;
@@ -2478,76 +2717,35 @@ function serializeTopCross(ctx, tree) {
2478
2717
  const args = scopeId == null ? "()" : "($R[\"" + serializeString(scopeId) + "\"])";
2479
2718
  return "(" + createFunction([params], body) + ")" + args;
2480
2719
  }
2481
- //#endregion
2482
- //#region src/core/context/sync-parser.ts
2483
- function createSyncParserContext(mode, options) {
2484
- return {
2485
- type: 1,
2486
- base: createBaseParserContext(mode, options),
2487
- child: void 0
2488
- };
2720
+ function wrapSerializationError(error) {
2721
+ return error instanceof SerovalSerializationError ? error : new SerovalSerializationError(error);
2489
2722
  }
2490
- var SyncParsePluginContext = class {
2491
- constructor(_p, depth) {
2492
- this._p = _p;
2493
- this.depth = depth;
2494
- }
2495
- parse(current) {
2496
- return parseSOS(this._p, this.depth, current);
2497
- }
2498
- };
2499
- var StreamParsePluginContext = class {
2500
- constructor(_p, depth) {
2501
- this._p = _p;
2502
- this.depth = depth;
2503
- }
2504
- parse(current) {
2505
- return parseSOS(this._p, this.depth, current);
2506
- }
2507
- parseWithError(current) {
2508
- return parseWithError(this._p, this.depth, current);
2509
- }
2510
- isAlive() {
2511
- return this._p.state.alive;
2512
- }
2513
- pushPendingState() {
2514
- pushPendingState(this._p);
2515
- }
2516
- popPendingState() {
2517
- popPendingState(this._p);
2518
- }
2519
- onParse(node) {
2520
- onParse(this._p, node);
2521
- }
2522
- onError(error) {
2523
- onError(this._p, error);
2723
+ function serializeTopVanilla(ctx, tree) {
2724
+ try {
2725
+ return serializeVanilla(ctx, tree);
2726
+ } catch (error) {
2727
+ throw wrapSerializationError(error);
2524
2728
  }
2525
- addCleanup(callback) {
2526
- this._p.state.cleanups.push(callback);
2729
+ }
2730
+ function serializeTopCross(ctx, tree) {
2731
+ try {
2732
+ return serializeCross(ctx, tree);
2733
+ } catch (error) {
2734
+ throw wrapSerializationError(error);
2527
2735
  }
2528
- };
2529
- function createStreamParserState(options) {
2530
- return {
2531
- alive: true,
2532
- pending: 0,
2533
- initial: true,
2534
- buffer: [],
2535
- onParse: options.onParse,
2536
- onError: options.onError,
2537
- onDone: options.onDone,
2538
- cleanups: []
2539
- };
2540
2736
  }
2541
- function createStreamParserContext(options) {
2737
+ //#endregion
2738
+ //#region src/core/context/sync-parser.ts
2739
+ function createSyncParserContext(options) {
2542
2740
  return {
2543
- type: 2,
2544
- base: createBaseParserContext(2, options),
2545
- state: createStreamParserState(options)
2741
+ type: 1,
2742
+ base: createBaseParserContext(options)
2546
2743
  };
2547
2744
  }
2548
2745
  function parseItems(ctx, depth, current) {
2549
- const nodes = [];
2550
- for (let i = 0, len = current.length; i < len; i++) if (i in current) nodes[i] = parseSOS(ctx, depth, current[i]);
2746
+ const len = current.length;
2747
+ const nodes = new Array(len);
2748
+ for (let i = 0; i < len; i++) if (i in current) nodes[i] = parseSOS(ctx, depth, current[i]);
2551
2749
  else nodes[i] = 0;
2552
2750
  return nodes;
2553
2751
  }
@@ -2555,20 +2753,22 @@ function parseArray(ctx, depth, id, current) {
2555
2753
  return createArrayNode(id, current, parseItems(ctx, depth, current));
2556
2754
  }
2557
2755
  function parseProperties(ctx, depth, properties) {
2558
- const entries = Object.entries(properties);
2559
- const keyNodes = [];
2560
- const valueNodes = [];
2561
- for (let i = 0, len = entries.length; i < len; i++) {
2562
- keyNodes.push(serializeString(entries[i][0]));
2563
- valueNodes.push(parseSOS(ctx, depth, entries[i][1]));
2756
+ const keys = Object.keys(properties);
2757
+ const len = keys.length;
2758
+ const keyNodes = new Array(len);
2759
+ const valueNodes = new Array(len);
2760
+ for (let i = 0, key; i < len; i++) {
2761
+ key = keys[i];
2762
+ keyNodes[i] = serializeString(key);
2763
+ valueNodes[i] = parseSOS(ctx, depth, properties[key]);
2564
2764
  }
2565
2765
  if (SYM_ITERATOR in properties) {
2566
2766
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ITERATOR));
2567
- valueNodes.push(createIteratorFactoryInstanceNode(parseIteratorFactory(ctx.base), parseSOS(ctx, depth, createSequenceFromIterable(properties))));
2767
+ valueNodes.push(createIteratorFactoryInstanceNode(28, parseIteratorFactory(ctx.base), parseSOS(ctx, depth, createSequenceFromIterable(properties))));
2568
2768
  }
2569
2769
  if (SYM_ASYNC_ITERATOR in properties) {
2570
2770
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ASYNC_ITERATOR));
2571
- valueNodes.push(createAsyncIteratorFactoryInstanceNode(parseAsyncIteratorFactory(ctx.base), parseSOS(ctx, depth, ctx.type === 1 ? createStream() : createStreamFromAsyncIterable(properties, ctx.state.cleanups))));
2771
+ valueNodes.push(createIteratorFactoryInstanceNode(30, parseAsyncIteratorFactory(ctx.base), parseAsyncIterable(ctx, depth, properties)));
2572
2772
  }
2573
2773
  if (SYM_TO_STRING_TAG in properties) {
2574
2774
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_TO_STRING_TAG));
@@ -2583,187 +2783,111 @@ function parseProperties(ctx, depth, properties) {
2583
2783
  v: valueNodes
2584
2784
  };
2585
2785
  }
2786
+ function parseAsyncIterable(ctx, depth, current) {
2787
+ const id = createIndexForValue(ctx.base, {});
2788
+ const result = createStreamConstructorNode(id, parseSpecialReference(ctx.base, 4), []);
2789
+ if (ctx.type === 2) ctx.state.iterable(ctx, depth, id, current);
2790
+ return result;
2791
+ }
2586
2792
  function parsePlainObject(ctx, depth, id, current, empty) {
2587
2793
  return createObjectNode(id, current, empty, parseProperties(ctx, depth, current));
2588
2794
  }
2589
2795
  function parseBoxed(ctx, depth, id, current) {
2590
2796
  return createBoxedNode(id, parseSOS(ctx, depth, current.valueOf()));
2591
2797
  }
2592
- function parseTypedArray(ctx, depth, id, current) {
2593
- current = getArrayBufferView(ctx.base, current);
2594
- return createTypedArrayNode(id, current, parseSOS(ctx, depth, current.buffer));
2595
- }
2596
- function parseBigIntTypedArray(ctx, depth, id, current) {
2798
+ function parseView(ctx, depth, type, id, current) {
2597
2799
  current = getArrayBufferView(ctx.base, current);
2598
- return createBigIntTypedArrayNode(id, current, parseSOS(ctx, depth, current.buffer));
2599
- }
2600
- function parseDataView(ctx, depth, id, current) {
2601
- current = getArrayBufferView(ctx.base, current);
2602
- return createDataViewNode(id, current, parseSOS(ctx, depth, current.buffer));
2603
- }
2604
- function parseError(ctx, depth, id, current) {
2605
- const options = getErrorOptions(current, ctx.base.features);
2606
- return createErrorNode(id, current, options ? parseProperties(ctx, depth, options) : void 0);
2800
+ return createViewNode(type, id, current, parseSOS(ctx, depth, current.buffer));
2607
2801
  }
2608
- function parseAggregateError(ctx, depth, id, current) {
2802
+ function parseError(ctx, depth, type, id, current) {
2609
2803
  const options = getErrorOptions(current, ctx.base.features);
2610
- return createAggregateErrorNode(id, current, options ? parseProperties(ctx, depth, options) : void 0);
2804
+ return createErrorNode(type, id, current, options ? parseProperties(ctx, depth, options) : void 0);
2611
2805
  }
2612
2806
  function parseMap(ctx, depth, id, current) {
2613
- const keyNodes = [];
2614
- const valueNodes = [];
2807
+ const sentinel = parseMapSentinel(ctx.base);
2808
+ const keyNodes = new Array(current.size);
2809
+ const valueNodes = new Array(current.size);
2810
+ let i = 0;
2615
2811
  for (const [key, value] of current.entries()) {
2616
- keyNodes.push(parseSOS(ctx, depth, key));
2617
- valueNodes.push(parseSOS(ctx, depth, value));
2812
+ keyNodes[i] = parseSOS(ctx, depth, key);
2813
+ valueNodes[i] = parseSOS(ctx, depth, value);
2814
+ i++;
2618
2815
  }
2619
- return createMapNode(ctx.base, id, keyNodes, valueNodes);
2816
+ return createMapNode(id, sentinel, keyNodes, valueNodes);
2620
2817
  }
2621
2818
  function parseSet(ctx, depth, id, current) {
2622
- const items = [];
2623
- for (const item of current.keys()) items.push(parseSOS(ctx, depth, item));
2819
+ const items = new Array(current.size);
2820
+ let i = 0;
2821
+ for (const item of current.keys()) items[i++] = parseSOS(ctx, depth, item);
2624
2822
  return createSetNode(id, items);
2625
2823
  }
2626
2824
  function parseStream(ctx, depth, id, current) {
2627
2825
  const result = createStreamConstructorNode(id, parseSpecialReference(ctx.base, 4), []);
2628
- if (ctx.type === 1) return result;
2629
- pushPendingState(ctx);
2630
- current.on({
2631
- next: (value) => {
2632
- if (ctx.state.alive) {
2633
- const parsed = parseWithError(ctx, depth, value);
2634
- if (parsed) onParse(ctx, createStreamNextNode(id, parsed));
2635
- }
2636
- },
2637
- throw: (value) => {
2638
- if (ctx.state.alive) {
2639
- const parsed = parseWithError(ctx, depth, value);
2640
- if (parsed) onParse(ctx, createStreamThrowNode(id, parsed));
2641
- }
2642
- popPendingState(ctx);
2643
- },
2644
- return: (value) => {
2645
- if (ctx.state.alive) {
2646
- const parsed = parseWithError(ctx, depth, value);
2647
- if (parsed) onParse(ctx, createStreamReturnNode(id, parsed));
2648
- }
2649
- popPendingState(ctx);
2650
- }
2651
- });
2826
+ if (ctx.type === 2) ctx.state.stream(ctx, depth, id, current);
2652
2827
  return result;
2653
2828
  }
2654
- function handlePromiseSuccess(id, depth, data) {
2655
- if (this.state.alive) {
2656
- const parsed = parseWithError(this, depth, data);
2657
- if (parsed) onParse(this, createSerovalNode(23, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(this.base, 2), parsed], void 0, void 0, void 0, void 0));
2658
- popPendingState(this);
2659
- }
2660
- }
2661
- function handlePromiseFailure(id, depth, data) {
2662
- if (this.state.alive) {
2663
- const parsed = parseWithError(this, depth, data);
2664
- if (parsed) onParse(this, createSerovalNode(24, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(this.base, 3), parsed], void 0, void 0, void 0, void 0));
2665
- }
2666
- popPendingState(this);
2829
+ function parseLiveStream(ctx, depth, id, current) {
2830
+ const stream = ctx.type === 2;
2831
+ const result = createStreamConstructorNode(id, parseSpecialReference(ctx.base, 4), [], stream ? 1 : void 0);
2832
+ if (stream) ctx.state.live(ctx, depth, id, current);
2833
+ return result;
2667
2834
  }
2668
2835
  function parsePromise(ctx, depth, id, current) {
2836
+ if (ctx.type !== 2) throw new SerovalUnsupportedTypeError(current);
2669
2837
  const resolver = createIndexForValue(ctx.base, {});
2670
- if (ctx.type === 2) {
2671
- pushPendingState(ctx);
2672
- current.then(handlePromiseSuccess.bind(ctx, resolver, depth), handlePromiseFailure.bind(ctx, resolver, depth));
2673
- }
2838
+ ctx.state.promise(ctx, resolver, depth, current);
2674
2839
  return createPromiseConstructorNode(ctx.base, id, resolver);
2675
2840
  }
2676
2841
  function parsePluginSync(ctx, depth, id, current, currentPlugins) {
2677
2842
  for (let i = 0, len = currentPlugins.length; i < len; i++) {
2678
2843
  const plugin = currentPlugins[i];
2679
- if (plugin.parse.sync && plugin.test(current)) return createPluginNode(id, plugin.tag, plugin.parse.sync(current, new SyncParsePluginContext(ctx, depth), { id }));
2844
+ if (plugin.parse.sync && plugin.test(current)) return createPluginNode(id, plugin.tag, plugin.parse.sync(current, { parse: (value) => parseSOS(ctx, depth, value) }, { id }));
2680
2845
  }
2681
2846
  }
2682
- function parsePluginStream(ctx, depth, id, current, currentPlugins) {
2683
- for (let i = 0, len = currentPlugins.length; i < len; i++) {
2684
- const plugin = currentPlugins[i];
2685
- if (plugin.parse.stream && plugin.test(current)) return createPluginNode(id, plugin.tag, plugin.parse.stream(current, new StreamParsePluginContext(ctx, depth), { id }));
2686
- }
2687
- }
2688
- function parsePlugin(ctx, depth, id, current) {
2689
- const currentPlugins = ctx.base.plugins;
2690
- if (currentPlugins) return ctx.type === 1 ? parsePluginSync(ctx, depth, id, current, currentPlugins) : parsePluginStream(ctx, depth, id, current, currentPlugins);
2847
+ function parsePlugin(ctx, depth, id, current, currentPlugins) {
2848
+ if (currentPlugins) return ctx.type === 1 ? parsePluginSync(ctx, depth, id, current, currentPlugins) : ctx.state.plugin(ctx, depth, id, current, currentPlugins);
2691
2849
  }
2692
2850
  function parseSequence(ctx, depth, id, current) {
2693
- const nodes = [];
2694
- for (let i = 0, len = current.v.length; i < len; i++) nodes[i] = parseSOS(ctx, depth, current.v[i]);
2851
+ const len = current.v.length;
2852
+ const nodes = new Array(len);
2853
+ for (let i = 0; i < len; i++) nodes[i] = parseSOS(ctx, depth, current.v[i]);
2695
2854
  return createSequenceNode(id, nodes, current.t, current.d);
2696
2855
  }
2697
2856
  function parseObjectPhase2(ctx, depth, id, current, currentClass) {
2698
- switch (currentClass) {
2699
- case Object: return parsePlainObject(ctx, depth, id, current, false);
2700
- case void 0: return parsePlainObject(ctx, depth, id, current, true);
2701
- case Date: return createDateNode(id, current);
2702
- case Error:
2703
- case EvalError:
2704
- case RangeError:
2705
- case ReferenceError:
2706
- case SyntaxError:
2707
- case TypeError:
2708
- case URIError: return parseError(ctx, depth, id, current);
2709
- case Number:
2710
- case Boolean:
2711
- case String:
2712
- case BigInt: return parseBoxed(ctx, depth, id, current);
2713
- case ArrayBuffer: return createArrayBufferNode(ctx.base, id, current);
2714
- case Int8Array:
2715
- case Int16Array:
2716
- case Int32Array:
2717
- case Uint8Array:
2718
- case Uint16Array:
2719
- case Uint32Array:
2720
- case Uint8ClampedArray:
2721
- case Float32Array:
2722
- case Float64Array: return parseTypedArray(ctx, depth, id, current);
2723
- case DataView: return parseDataView(ctx, depth, id, current);
2724
- case Map: return parseMap(ctx, depth, id, current);
2725
- case Set: return parseSet(ctx, depth, id, current);
2726
- }
2727
- if (currentClass === Promise || current instanceof Promise) return parsePromise(ctx, depth, id, current);
2728
- const currentFeatures = ctx.base.features;
2729
- if (currentFeatures & 32 && currentClass === RegExp) return createRegExpNode(id, current);
2730
- if (currentFeatures & 16) switch (currentClass) {
2731
- case BigInt64Array:
2732
- case BigUint64Array: return parseBigIntTypedArray(ctx, depth, id, current);
2733
- }
2734
- if (currentFeatures & 1 && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) return parseAggregateError(ctx, depth, id, current);
2735
- if (currentFeatures & 64 && typeof Temporal !== "undefined") switch (currentClass) {
2736
- case Temporal.Instant: return createTemporalNode(id, 0, current);
2737
- case Temporal.Duration: return createTemporalNode(id, 1, current);
2738
- case Temporal.PlainDate: return createTemporalNode(id, 2, current);
2739
- case Temporal.PlainDateTime: return createTemporalNode(id, 3, current);
2740
- case Temporal.PlainMonthDay: return createTemporalNode(id, 4, current);
2741
- case Temporal.PlainTime: return createTemporalNode(id, 5, current);
2742
- case Temporal.PlainYearMonth: return createTemporalNode(id, 6, current);
2743
- case Temporal.ZonedDateTime: return createTemporalNode(id, 7, current);
2744
- }
2745
- if (current instanceof Error) return parseError(ctx, depth, id, current);
2746
- if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) return parsePlainObject(ctx, depth, id, current, !!currentClass);
2747
- throw new SerovalUnsupportedTypeError(current);
2857
+ if (currentClass === Object) return parsePlainObject(ctx, depth, id, current, false);
2858
+ switch (getObjectKind(current, currentClass, ctx.base.features)) {
2859
+ case 2: return parsePlainObject(ctx, depth, id, current, true);
2860
+ case 3: return createDateNode(id, current);
2861
+ case 4: return parseError(ctx, depth, 13, id, current);
2862
+ case 5: return parseError(ctx, depth, 14, id, current);
2863
+ case 6: return parseBoxed(ctx, depth, id, current);
2864
+ case 7: return createArrayBufferNode(ctx.base, id, current);
2865
+ case 8: return parseView(ctx, depth, 15, id, current);
2866
+ case 9: return parseView(ctx, depth, 16, id, current);
2867
+ case 10: return parseView(ctx, depth, 20, id, current);
2868
+ case 11: return parseMap(ctx, depth, id, current);
2869
+ case 12: return parseSet(ctx, depth, id, current);
2870
+ case 13: return parsePromise(ctx, depth, id, current);
2871
+ case 14: return createRegExpNode(id, current);
2872
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
2873
+ case 16: return parsePlainObject(ctx, depth, id, current, !!currentClass);
2874
+ default: throw new SerovalUnsupportedTypeError(current);
2875
+ }
2748
2876
  }
2749
2877
  function parseObject(ctx, depth, id, current) {
2750
2878
  if (Array.isArray(current)) return parseArray(ctx, depth, id, current);
2751
- if (isStream(current)) return parseStream(ctx, depth, id, current);
2879
+ if (isStream(current)) return isLiveStream(current) ? parseLiveStream(ctx, depth, id, current) : parseStream(ctx, depth, id, current);
2752
2880
  if (isSequence(current)) return parseSequence(ctx, depth, id, current);
2753
- let currentClass = current.constructor;
2754
- if (currentClass !== void 0 && typeof currentClass !== "function") {
2755
- const proto = Object.getPrototypeOf(current);
2756
- currentClass = proto === null ? void 0 : proto.constructor;
2757
- }
2881
+ const currentClass = getObjectClass(current);
2758
2882
  if (currentClass === OpaqueReference) return parseSOS(ctx, depth, current.replacement);
2759
- const parsed = parsePlugin(ctx, depth, id, current);
2883
+ const parsed = parsePlugin(ctx, depth, id, current, getObjectPlugins(ctx.base, current, currentClass));
2760
2884
  if (parsed) return parsed;
2761
2885
  return parseObjectPhase2(ctx, depth, id, current, currentClass);
2762
2886
  }
2763
2887
  function parseFunction(ctx, depth, current) {
2764
2888
  const ref = getReferenceNode(ctx.base, current);
2765
- if (ref.type !== 0) return ref.value;
2766
- const plugin = parsePlugin(ctx, depth, ref.value, current);
2889
+ if (typeof ref !== "number") return ref;
2890
+ const plugin = parsePlugin(ctx, depth, ref, current, ctx.base.plugins);
2767
2891
  if (plugin) return plugin;
2768
2892
  throw new SerovalUnsupportedTypeError(current);
2769
2893
  }
@@ -2778,7 +2902,7 @@ function parseSOS(ctx, depth, current) {
2778
2902
  case "object":
2779
2903
  if (current) {
2780
2904
  const ref = getReferenceNode(ctx.base, current);
2781
- return ref.type === 0 ? parseObject(ctx, depth + 1, ref.value, current) : ref.value;
2905
+ return typeof ref === "number" ? parseObject(ctx, depth + 1, ref, current) : ref;
2782
2906
  }
2783
2907
  return NULL_NODE;
2784
2908
  case "symbol": return parseWellKnownSymbol(ctx.base, current);
@@ -2793,63 +2917,431 @@ function parseTop(ctx, current) {
2793
2917
  throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
2794
2918
  }
2795
2919
  }
2796
- function onParse(ctx, node) {
2797
- if (ctx.state.initial) ctx.state.buffer.push(node);
2798
- else onParseInternal(ctx, node, false);
2920
+ //#endregion
2921
+ //#region src/core/context/stream-parser.ts
2922
+ /**
2923
+ * Streaming parse mode. Everything here is reached through the function
2924
+ * fields of the stream parser state, which only `createStreamParserContext`
2925
+ * sets, so synchronous consumers (`serialize`, `toJSON`) never bundle it.
2926
+ */
2927
+ function createStreamParsePluginContext(ctx, depth) {
2928
+ const state = ctx.state;
2929
+ function parse(current) {
2930
+ state.parsing++;
2931
+ try {
2932
+ return parseSOS(ctx, depth, current);
2933
+ } finally {
2934
+ state.parsing--;
2935
+ if (!state.alive) releaseParserValues(ctx);
2936
+ }
2937
+ }
2938
+ return {
2939
+ parse,
2940
+ parseStreamSource(current) {
2941
+ state.parsing++;
2942
+ try {
2943
+ if (depth >= ctx.base.depthLimit) throw new SerovalDepthLimitError(ctx.base.depthLimit);
2944
+ const id = createIndexForValue(ctx.base, {});
2945
+ const node = createStreamConstructorNode(id, parseSpecialReference(ctx.base, 4), [], 1);
2946
+ iterateAsync(ctx, depth + 1, id, current, true);
2947
+ return node;
2948
+ } finally {
2949
+ state.parsing--;
2950
+ if (!state.alive) releaseParserValues(ctx);
2951
+ }
2952
+ },
2953
+ parseWithError(current) {
2954
+ try {
2955
+ return parse(current);
2956
+ } catch (error) {
2957
+ stopStreamParse(ctx, 1, error);
2958
+ return;
2959
+ }
2960
+ },
2961
+ isAlive: () => state.alive,
2962
+ pushPendingState: () => {
2963
+ pushPendingState(ctx);
2964
+ },
2965
+ popPendingState: () => {
2966
+ popPendingState(ctx);
2967
+ },
2968
+ onParse(node) {
2969
+ if (state.alive) {
2970
+ state.queue.push({
2971
+ node,
2972
+ initial: false,
2973
+ accept: void 0
2974
+ });
2975
+ drainOutput(ctx);
2976
+ }
2977
+ },
2978
+ onError(error) {
2979
+ stopStreamParse(ctx, 1, error);
2980
+ },
2981
+ addCleanup(callback) {
2982
+ addCleanup(ctx, callback);
2983
+ }
2984
+ };
2985
+ }
2986
+ function getStreamParsePluginContext(ctx, depth) {
2987
+ const contexts = ctx.state.pluginContexts;
2988
+ return contexts[depth] ?? (contexts[depth] = createStreamParsePluginContext(ctx, depth));
2799
2989
  }
2800
- function onError(ctx, error) {
2801
- if (ctx.state.onError) ctx.state.onError(error);
2802
- else throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
2990
+ function addCleanup(ctx, callback) {
2991
+ if (ctx.state.alive) ctx.state.cleanups.push(callback);
2992
+ else callback();
2803
2993
  }
2804
- function onDone(ctx) {
2805
- if (ctx.state.onDone) ctx.state.onDone();
2806
- for (let i = 0, len = ctx.state.cleanups.length; i < len; i++) ctx.state.cleanups[i]();
2994
+ function parsePluginStream(ctx, depth, id, current, currentPlugins) {
2995
+ for (let i = 0, len = currentPlugins.length; i < len; i++) {
2996
+ const plugin = currentPlugins[i];
2997
+ if (plugin.parse.stream && plugin.test(current)) return createPluginNode(id, plugin.tag, plugin.parse.stream(current, getStreamParsePluginContext(ctx, depth), { id }));
2998
+ }
2999
+ }
3000
+ /**
3001
+ * One listener shape serves both `Stream.on` (no accept) and
3002
+ * `LiveStream.pump` (accept tied to record acceptance). Handlers return the
3003
+ * parse error so the pump can stop the source.
3004
+ */
3005
+ function streamListener(ctx, depth, id, subscription) {
3006
+ const handle = (type, terminal) => (value, accept) => {
3007
+ let failure = void 0;
3008
+ if (ctx.state.alive) try {
3009
+ failure = parseEvent(ctx, depth, value, createStreamEventNode.bind(null, type, id), accept);
3010
+ } catch (error) {
3011
+ if (!subscription?.active) throw error;
3012
+ subscription.failure ?? (subscription.failure = { value: error });
3013
+ failure = error;
3014
+ }
3015
+ if (terminal && !accept) popPendingState(ctx);
3016
+ return failure;
3017
+ };
3018
+ return {
3019
+ next: handle(32, false),
3020
+ throw: handle(33, true),
3021
+ return: handle(34, true),
3022
+ done() {
3023
+ popPendingState(ctx);
3024
+ },
3025
+ error(reason) {
3026
+ if (ctx.state.alive) stopStreamParse(ctx, 1, reason);
3027
+ }
3028
+ };
2807
3029
  }
2808
- function onParseInternal(ctx, node, initial) {
3030
+ function subscribeStream(ctx, depth, id, current) {
3031
+ pushPendingState(ctx);
3032
+ const subscription = {
3033
+ active: true,
3034
+ failure: void 0
3035
+ };
3036
+ let cleanup;
3037
+ try {
3038
+ cleanup = current.on(streamListener(ctx, depth, id, subscription));
3039
+ } finally {
3040
+ subscription.active = false;
3041
+ }
2809
3042
  try {
2810
- ctx.state.onParse(node, initial);
3043
+ addCleanup(ctx, cleanup);
2811
3044
  } catch (error) {
2812
- onError(ctx, error);
3045
+ subscription.failure ?? (subscription.failure = { value: error });
2813
3046
  }
3047
+ const failure = subscription.failure;
3048
+ subscription.failure = void 0;
3049
+ if (failure) throw failure.value;
3050
+ }
3051
+ function consumeLiveStream(ctx, depth, id, current) {
3052
+ const cancel = current.pump(streamListener(ctx, depth, id));
3053
+ pushPendingState(ctx);
3054
+ addCleanup(ctx, () => {
3055
+ cancel(ctx.state.reason);
3056
+ });
3057
+ }
3058
+ function wrapPromiseResult(ctx, type, id, node) {
3059
+ return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx.base, type === 23 ? 2 : 3), node], void 0, void 0, void 0, void 0);
3060
+ }
3061
+ function handlePromise(type, id, depth, data) {
3062
+ if (this.state.alive) parseEvent(this, depth, data, wrapPromiseResult.bind(null, this, type, id), void 0);
3063
+ popPendingState(this);
3064
+ }
3065
+ function watchPromise(ctx, resolver, depth, current) {
3066
+ pushPendingState(ctx);
3067
+ current.then(handlePromise.bind(ctx, 23, resolver, depth), handlePromise.bind(ctx, 24, resolver, depth));
3068
+ }
3069
+ function returnIterator(iterator) {
3070
+ Promise.resolve().then(() => iterator.return?.()).catch(() => {});
3071
+ }
3072
+ /**
3073
+ * Drives an async iterator directly. The next value is pulled only after the
3074
+ * record for the previous one has been accepted, and cancellation returns
3075
+ * the iterator once.
3076
+ */
3077
+ function iterateAsync(ctx, depth, id, current, forwardReason) {
3078
+ const iterator = current[SYM_ASYNC_ITERATOR]();
3079
+ const listener = streamListener(ctx, depth, id);
3080
+ let active = true;
3081
+ function stop() {
3082
+ if (active) {
3083
+ active = false;
3084
+ if (forwardReason) try {
3085
+ Promise.resolve(iterator.return?.(ctx.state.reason)).catch(() => {});
3086
+ } catch {}
3087
+ else returnIterator(iterator);
3088
+ }
3089
+ }
3090
+ function pull() {
3091
+ if (active) iterator.next().then((result) => {
3092
+ if (active) {
3093
+ if (result.done) {
3094
+ active = false;
3095
+ listener.return(result.value, void 0);
3096
+ } else if (listener.next(result.value, pull) !== void 0) {
3097
+ stop();
3098
+ listener.done();
3099
+ }
3100
+ }
3101
+ }, (error) => {
3102
+ if (active) {
3103
+ active = false;
3104
+ listener.throw(error, void 0);
3105
+ }
3106
+ });
3107
+ }
3108
+ pushPendingState(ctx);
3109
+ addCleanup(ctx, stop);
3110
+ pull();
3111
+ }
3112
+ function createStreamParserState(options) {
3113
+ return {
3114
+ alive: true,
3115
+ pending: 0,
3116
+ parsing: 0,
3117
+ queue: [],
3118
+ writing: false,
3119
+ inFlight: void 0,
3120
+ reason: void 0,
3121
+ onParse: options.onParse,
3122
+ onError: options.onError,
3123
+ onDone: options.onDone,
3124
+ cleanups: [],
3125
+ pluginContexts: [],
3126
+ stream: subscribeStream,
3127
+ live: consumeLiveStream,
3128
+ promise: watchPromise,
3129
+ iterable: iterateAsync,
3130
+ plugin: parsePluginStream
3131
+ };
3132
+ }
3133
+ function createStreamParserContext(options) {
3134
+ return {
3135
+ type: 2,
3136
+ base: createBaseParserContext(options),
3137
+ state: createStreamParserState(options)
3138
+ };
2814
3139
  }
2815
3140
  function pushPendingState(ctx) {
2816
- ctx.state.pending++;
3141
+ if (ctx.state.alive) ctx.state.pending++;
2817
3142
  }
2818
3143
  function popPendingState(ctx) {
2819
- if (--ctx.state.pending <= 0) onDone(ctx);
3144
+ if (ctx.state.alive) {
3145
+ ctx.state.pending--;
3146
+ checkStreamParse(ctx);
3147
+ }
2820
3148
  }
2821
- function parseWithError(ctx, depth, current) {
3149
+ /**
3150
+ * Parses a value that arrived after the root. Its record is queued before
3151
+ * parsing starts so that records discovered inside it follow it, and it is
3152
+ * dropped again if parsing fails. Returns the parse error after reporting
3153
+ * it, or `undefined`.
3154
+ */
3155
+ function parseEvent(ctx, depth, value, wrap, accept) {
3156
+ const state = ctx.state;
3157
+ const record = {
3158
+ node: void 0,
3159
+ initial: false,
3160
+ accept
3161
+ };
3162
+ let failure;
3163
+ state.queue.push(record);
3164
+ state.parsing++;
2822
3165
  try {
2823
- return parseSOS(ctx, depth, current);
2824
- } catch (err) {
2825
- onError(ctx, err);
2826
- return;
3166
+ record.node = wrap(parseSOS(ctx, depth, value));
3167
+ } catch (error) {
3168
+ failure = { value: error };
3169
+ } finally {
3170
+ state.parsing--;
3171
+ if (!state.alive) releaseParserValues(ctx);
2827
3172
  }
3173
+ if (failure) stopStreamParse(ctx, 1, failure.value);
3174
+ else drainOutput(ctx);
3175
+ return failure?.value;
3176
+ }
3177
+ /**
3178
+ * Emits queued records one at a time. A callback that returns a promise
3179
+ * pauses emission until it settles; the source event behind the record is
3180
+ * accepted only after that.
3181
+ */
3182
+ function drainOutput(ctx) {
3183
+ const state = ctx.state;
3184
+ if (state.writing || state.parsing > 0) return;
3185
+ while (state.alive && state.queue.length > 0) {
3186
+ const record = state.queue.shift();
3187
+ if (!record.node) continue;
3188
+ let pending;
3189
+ state.writing = true;
3190
+ state.inFlight = record;
3191
+ state.parsing++;
3192
+ try {
3193
+ if (!state.onParse) throw new Error("Stream output callback is unavailable");
3194
+ const result = state.onParse(record.node, record.initial);
3195
+ if (result !== null && (typeof result === "object" || typeof result === "function")) {
3196
+ const then = Reflect.get(result, "then");
3197
+ if (typeof then === "function") pending = new Promise((resolve, reject) => {
3198
+ Reflect.apply(then, result, [resolve, reject]);
3199
+ });
3200
+ }
3201
+ } catch (error) {
3202
+ releaseRecord(record);
3203
+ stopStreamParse(ctx, 1, error);
3204
+ return;
3205
+ } finally {
3206
+ state.parsing--;
3207
+ if (!state.alive) releaseParserValues(ctx);
3208
+ }
3209
+ record.node = void 0;
3210
+ if (pending) {
3211
+ if (!state.alive) releaseRecord(record);
3212
+ pending.then(() => {
3213
+ state.writing = false;
3214
+ state.inFlight = void 0;
3215
+ if (state.alive) {
3216
+ const accept = record.accept;
3217
+ releaseRecord(record);
3218
+ accept?.();
3219
+ drainOutput(ctx);
3220
+ }
3221
+ }, (error) => {
3222
+ state.writing = false;
3223
+ state.inFlight = void 0;
3224
+ releaseRecord(record);
3225
+ stopStreamParse(ctx, 1, error);
3226
+ });
3227
+ return;
3228
+ }
3229
+ if (!state.alive) {
3230
+ releaseRecord(record);
3231
+ return;
3232
+ }
3233
+ state.writing = false;
3234
+ state.inFlight = void 0;
3235
+ const accept = record.accept;
3236
+ releaseRecord(record);
3237
+ accept?.();
3238
+ }
3239
+ checkStreamParse(ctx);
3240
+ }
3241
+ function checkStreamParse(ctx) {
3242
+ const state = ctx.state;
3243
+ if (state.alive && state.parsing === 0 && state.pending <= 0 && state.queue.length === 0 && !state.writing) stopStreamParse(ctx, 0);
2828
3244
  }
2829
3245
  function startStreamParse(ctx, current) {
2830
- const parsed = parseWithError(ctx, 0, current);
2831
- if (parsed) {
2832
- onParseInternal(ctx, parsed, true);
2833
- ctx.state.initial = false;
2834
- flushStreamParse(ctx, ctx.state);
2835
- if (ctx.state.pending <= 0) destroyStreamParse(ctx);
3246
+ const state = ctx.state;
3247
+ let parsed;
3248
+ let failure;
3249
+ state.parsing++;
3250
+ try {
3251
+ parsed = parseSOS(ctx, 0, current);
3252
+ } catch (error) {
3253
+ failure = { value: error };
3254
+ } finally {
3255
+ state.parsing--;
3256
+ if (!state.alive) releaseParserValues(ctx);
3257
+ }
3258
+ if (failure) {
3259
+ if (state.alive) stopStreamParse(ctx, 1, failure.value);
3260
+ else throw failure.value;
3261
+ } else if (parsed && state.alive) {
3262
+ state.queue.unshift({
3263
+ node: parsed,
3264
+ initial: true,
3265
+ accept: void 0
3266
+ });
3267
+ drainOutput(ctx);
3268
+ }
3269
+ }
3270
+ function runCleanups(state) {
3271
+ const cleanups = state.cleanups;
3272
+ let failure;
3273
+ state.cleanups = [];
3274
+ for (const record of state.queue) releaseRecord(record);
3275
+ state.queue.length = 0;
3276
+ if (state.inFlight) releaseRecord(state.inFlight);
3277
+ state.inFlight = void 0;
3278
+ for (let i = 0, len = cleanups.length; i < len; i++) try {
3279
+ cleanups[i]();
3280
+ } catch (error) {
3281
+ failure ?? (failure = { value: error });
2836
3282
  }
3283
+ if (failure) throw failure.value;
2837
3284
  }
2838
- function flushStreamParse(ctx, state) {
2839
- for (let i = 0, len = state.buffer.length; i < len; i++) onParseInternal(ctx, state.buffer[i], false);
3285
+ function releaseRecord(record) {
3286
+ record.node = void 0;
3287
+ record.accept = void 0;
2840
3288
  }
2841
- function destroyStreamParse(ctx) {
2842
- if (ctx.state.alive) {
2843
- onDone(ctx);
2844
- ctx.state.alive = false;
3289
+ function releaseParserValues(ctx) {
3290
+ ctx.base.refs = /* @__PURE__ */ new Map();
3291
+ ctx.base.marked.clear();
3292
+ ctx.base.plugins = void 0;
3293
+ ctx.base.plainObjectPlugins = void 0;
3294
+ ctx.state.pluginContexts = [];
3295
+ if (ctx.state.parsing === 0) ctx.state.reason = void 0;
3296
+ }
3297
+ /**
3298
+ * Ends the parse once: `onDone` on success, `onError` on failure, and every
3299
+ * cleanup either way.
3300
+ */
3301
+ function stopStreamParse(ctx, mode, reason) {
3302
+ const state = ctx.state;
3303
+ if (state.alive) {
3304
+ state.alive = false;
3305
+ const onError = state.onError;
3306
+ const onDone = state.onDone;
3307
+ if (mode === 1 && !onError && !(reason instanceof SerovalParserError)) reason = new SerovalParserError(reason);
3308
+ state.reason = reason;
3309
+ state.onParse = void 0;
3310
+ state.onError = void 0;
3311
+ state.onDone = void 0;
3312
+ state.writing = false;
3313
+ state.parsing++;
3314
+ let failure;
3315
+ try {
3316
+ if (mode === 1) {
3317
+ if (onError) onError(reason);
3318
+ else throw reason;
3319
+ } else if (mode === 0) onDone?.();
3320
+ } catch (error) {
3321
+ failure = { value: error };
3322
+ }
3323
+ try {
3324
+ runCleanups(state);
3325
+ } catch (error) {
3326
+ failure ?? (failure = { value: error });
3327
+ } finally {
3328
+ state.parsing--;
3329
+ releaseParserValues(ctx);
3330
+ state.pending = 0;
3331
+ }
3332
+ if (failure) throw failure.value;
2845
3333
  }
2846
3334
  }
3335
+ function destroyStreamParse(ctx, reason) {
3336
+ stopStreamParse(ctx, 2, reason);
3337
+ }
2847
3338
  //#endregion
2848
3339
  //#region src/core/cross/index.ts
2849
3340
  function crossSerialize(source, options = {}) {
2850
3341
  const plugins = resolvePlugins(options.plugins);
2851
- const ctx = createSyncParserContext(2, {
3342
+ const ctx = createSyncParserContext({
2852
3343
  compactArrayBufferViews: options.compactArrayBufferViews,
3344
+ depthLimit: options.depthLimit,
2853
3345
  plugins,
2854
3346
  disabledFeatures: options.disabledFeatures,
2855
3347
  refs: options.refs
@@ -2859,13 +3351,15 @@ function crossSerialize(source, options = {}) {
2859
3351
  plugins,
2860
3352
  features: ctx.base.features,
2861
3353
  scopeId: options.scopeId,
2862
- markedRefs: ctx.base.marked
3354
+ markedRefs: ctx.base.marked,
3355
+ assignSharedRefsOnly: options.assignSharedRefsOnly
2863
3356
  }), tree);
2864
3357
  }
2865
3358
  async function crossSerializeAsync(source, options = {}) {
2866
3359
  const plugins = resolvePlugins(options.plugins);
2867
- const ctx = createAsyncParserContext(2, {
3360
+ const ctx = createAsyncParserContext({
2868
3361
  compactArrayBufferViews: options.compactArrayBufferViews,
3362
+ depthLimit: options.depthLimit,
2869
3363
  plugins,
2870
3364
  disabledFeatures: options.disabledFeatures,
2871
3365
  refs: options.refs
@@ -2875,13 +3369,15 @@ async function crossSerializeAsync(source, options = {}) {
2875
3369
  plugins,
2876
3370
  features: ctx.base.features,
2877
3371
  scopeId: options.scopeId,
2878
- markedRefs: ctx.base.marked
3372
+ markedRefs: ctx.base.marked,
3373
+ assignSharedRefsOnly: options.assignSharedRefsOnly
2879
3374
  }), tree);
2880
3375
  }
2881
3376
  function toCrossJSON(source, options = {}) {
2882
3377
  const plugins = resolvePlugins(options.plugins);
2883
- return parseTop(createSyncParserContext(2, {
3378
+ return parseTop(createSyncParserContext({
2884
3379
  compactArrayBufferViews: options.compactArrayBufferViews,
3380
+ depthLimit: options.depthLimit,
2885
3381
  plugins,
2886
3382
  disabledFeatures: options.disabledFeatures,
2887
3383
  refs: options.refs
@@ -2889,8 +3385,9 @@ function toCrossJSON(source, options = {}) {
2889
3385
  }
2890
3386
  async function toCrossJSONAsync(source, options = {}) {
2891
3387
  const plugins = resolvePlugins(options.plugins);
2892
- return await parseTopAsync(createAsyncParserContext(2, {
3388
+ return await parseTopAsync(createAsyncParserContext({
2893
3389
  compactArrayBufferViews: options.compactArrayBufferViews,
3390
+ depthLimit: options.depthLimit,
2894
3391
  plugins,
2895
3392
  disabledFeatures: options.disabledFeatures,
2896
3393
  refs: options.refs
@@ -2898,8 +3395,11 @@ async function toCrossJSONAsync(source, options = {}) {
2898
3395
  }
2899
3396
  function crossSerializeStream(source, options) {
2900
3397
  const plugins = resolvePlugins(options.plugins);
3398
+ const onSerialize = options.onSerialize;
3399
+ const scopeId = options.scopeId;
2901
3400
  const ctx = createStreamParserContext({
2902
3401
  compactArrayBufferViews: options.compactArrayBufferViews,
3402
+ depthLimit: options.depthLimit,
2903
3403
  plugins,
2904
3404
  refs: options.refs,
2905
3405
  disabledFeatures: options.disabledFeatures,
@@ -2907,17 +3407,10 @@ function crossSerializeStream(source, options) {
2907
3407
  const serial = createCrossSerializerContext({
2908
3408
  plugins,
2909
3409
  features: ctx.base.features,
2910
- scopeId: options.scopeId,
3410
+ scopeId,
2911
3411
  markedRefs: ctx.base.marked
2912
3412
  });
2913
- let serialized;
2914
- try {
2915
- serialized = serializeTopCross(serial, node);
2916
- } catch (err) {
2917
- if (options.onError) options.onError(err);
2918
- return;
2919
- }
2920
- options.onSerialize(serialized, initial);
3413
+ return onSerialize(serializeTopCross(serial, node), initial);
2921
3414
  },
2922
3415
  onError: options.onError,
2923
3416
  onDone: options.onDone
@@ -2940,22 +3433,39 @@ function toCrossJSONStream(source, options) {
2940
3433
  startStreamParse(ctx, source);
2941
3434
  return destroyStreamParse.bind(null, ctx);
2942
3435
  }
2943
- function fromCrossJSON(source, options) {
2944
- const plugins = resolvePlugins(options.plugins);
2945
- return deserializeTop(createCrossDeserializerContext({
3436
+ function createFromCrossJSONContext(options) {
3437
+ return createCrossDeserializerContext({
2946
3438
  maxBase64Length: options.maxBase64Length,
2947
- plugins,
3439
+ plugins: resolvePlugins(options.plugins),
2948
3440
  refs: options.refs,
2949
3441
  features: options.features,
2950
3442
  disabledFeatures: options.disabledFeatures,
2951
3443
  depthLimit: options.depthLimit
2952
- }), source);
3444
+ });
3445
+ }
3446
+ function fromCrossJSON(source, options) {
3447
+ return deserializeTop(createFromCrossJSONContext(options), source);
3448
+ }
3449
+ function createCrossDeserializer(options) {
3450
+ const ctx = createFromCrossJSONContext(options);
3451
+ ctx.base.pending = /* @__PURE__ */ new Set();
3452
+ return {
3453
+ deserialize(node) {
3454
+ if (!ctx.base.pending) throw new SerovalAbortedError();
3455
+ return deserializeTop(ctx, node);
3456
+ },
3457
+ get pending() {
3458
+ return ctx.base.pending?.size ?? 0;
3459
+ },
3460
+ abort(reason) {
3461
+ abortDeferred(ctx, reason);
3462
+ }
3463
+ };
2953
3464
  }
2954
3465
  //#endregion
2955
3466
  //#region src/core/Serializer.ts
2956
3467
  var Serializer = class {
2957
3468
  constructor(options) {
2958
- this.options = options;
2959
3469
  this.alive = true;
2960
3470
  this.flushed = false;
2961
3471
  this.done = false;
@@ -2964,32 +3474,52 @@ var Serializer = class {
2964
3474
  this.refs = /* @__PURE__ */ new Map();
2965
3475
  this.keys = /* @__PURE__ */ new Set();
2966
3476
  this.ids = 0;
3477
+ this.options = options;
2967
3478
  this.plugins = resolvePlugins(options.plugins);
2968
3479
  }
2969
3480
  write(key, value) {
2970
- if (this.alive && !this.flushed) {
3481
+ const options = this.options;
3482
+ if (this.alive && !this.flushed && options) {
2971
3483
  this.pending++;
2972
3484
  this.keys.add(key);
2973
- this.cleanups.push(crossSerializeStream(value, {
3485
+ const cleanup = crossSerializeStream(value, {
2974
3486
  plugins: this.plugins,
2975
- scopeId: this.options.scopeId,
3487
+ scopeId: options.scopeId,
2976
3488
  refs: this.refs,
2977
- disabledFeatures: this.options.disabledFeatures,
2978
- compactArrayBufferViews: this.options.compactArrayBufferViews,
2979
- onError: this.options.onError,
3489
+ disabledFeatures: options.disabledFeatures,
3490
+ compactArrayBufferViews: options.compactArrayBufferViews,
3491
+ depthLimit: options.depthLimit,
3492
+ onError: (error) => {
3493
+ let failure;
3494
+ try {
3495
+ if (options.onError) options.onError(error);
3496
+ else throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3497
+ } catch (handlerError) {
3498
+ failure = { value: handlerError };
3499
+ }
3500
+ try {
3501
+ this.finishWrite();
3502
+ } catch (finishError) {
3503
+ failure ?? (failure = { value: finishError });
3504
+ }
3505
+ if (failure) throw failure.value;
3506
+ },
2980
3507
  onSerialize: (data, initial) => {
2981
- if (this.alive) this.options.onData(initial ? this.options.globalIdentifier + "[\"" + serializeString(key) + "\"]=" + data : data);
3508
+ const current = this.options;
3509
+ if (this.alive && current) current.onData(initial ? current.globalIdentifier + "[\"" + serializeString(key) + "\"]=" + data : data);
2982
3510
  },
2983
3511
  onDone: () => {
2984
- if (this.alive) {
2985
- this.pending--;
2986
- if (this.pending <= 0 && this.flushed && !this.done && this.options.onDone) {
2987
- this.options.onDone();
2988
- this.done = true;
2989
- }
2990
- }
3512
+ this.finishWrite();
2991
3513
  }
2992
- }));
3514
+ });
3515
+ if (this.alive) this.cleanups.push(cleanup);
3516
+ else cleanup();
3517
+ }
3518
+ }
3519
+ finishWrite() {
3520
+ if (this.alive) {
3521
+ this.pending--;
3522
+ if (this.pending <= 0 && this.flushed && !this.done) this.close();
2993
3523
  }
2994
3524
  }
2995
3525
  getNextID() {
@@ -3004,20 +3534,36 @@ var Serializer = class {
3004
3534
  flush() {
3005
3535
  if (this.alive) {
3006
3536
  this.flushed = true;
3007
- if (this.pending <= 0 && !this.done && this.options.onDone) {
3008
- this.options.onDone();
3009
- this.done = true;
3010
- }
3537
+ if (this.pending <= 0 && !this.done) this.close();
3011
3538
  }
3012
3539
  }
3013
3540
  close() {
3541
+ this.finish();
3542
+ }
3543
+ finish() {
3014
3544
  if (this.alive) {
3015
- for (let i = 0, len = this.cleanups.length; i < len; i++) this.cleanups[i]();
3016
- if (!this.done && this.options.onDone) {
3017
- this.options.onDone();
3545
+ this.alive = false;
3546
+ const options = this.options;
3547
+ this.options = void 0;
3548
+ this.plugins = void 0;
3549
+ const cleanups = this.cleanups;
3550
+ this.cleanups = [];
3551
+ let failure;
3552
+ for (let index = 0, length = cleanups.length; index < length; index++) try {
3553
+ cleanups[index]();
3554
+ } catch (error) {
3555
+ failure ?? (failure = { value: error });
3556
+ }
3557
+ this.refs.clear();
3558
+ if (!this.done) {
3018
3559
  this.done = true;
3560
+ try {
3561
+ options?.onDone?.();
3562
+ } catch (error) {
3563
+ failure ?? (failure = { value: error });
3564
+ }
3019
3565
  }
3020
- this.alive = false;
3566
+ if (failure) throw failure.value;
3021
3567
  }
3022
3568
  }
3023
3569
  };
@@ -3025,8 +3571,9 @@ var Serializer = class {
3025
3571
  //#region src/core/tree/index.ts
3026
3572
  function serialize(source, options = {}) {
3027
3573
  const plugins = resolvePlugins(options.plugins);
3028
- const ctx = createSyncParserContext(1, {
3574
+ const ctx = createSyncParserContext({
3029
3575
  compactArrayBufferViews: options.compactArrayBufferViews,
3576
+ depthLimit: options.depthLimit,
3030
3577
  plugins,
3031
3578
  disabledFeatures: options.disabledFeatures
3032
3579
  });
@@ -3039,8 +3586,9 @@ function serialize(source, options = {}) {
3039
3586
  }
3040
3587
  async function serializeAsync(source, options = {}) {
3041
3588
  const plugins = resolvePlugins(options.plugins);
3042
- const ctx = createAsyncParserContext(1, {
3589
+ const ctx = createAsyncParserContext({
3043
3590
  compactArrayBufferViews: options.compactArrayBufferViews,
3591
+ depthLimit: options.depthLimit,
3044
3592
  plugins,
3045
3593
  disabledFeatures: options.disabledFeatures
3046
3594
  });
@@ -3056,8 +3604,9 @@ function deserialize(source) {
3056
3604
  }
3057
3605
  function toJSON(source, options = {}) {
3058
3606
  const plugins = resolvePlugins(options.plugins);
3059
- const ctx = createSyncParserContext(1, {
3607
+ const ctx = createSyncParserContext({
3060
3608
  compactArrayBufferViews: options.compactArrayBufferViews,
3609
+ depthLimit: options.depthLimit,
3061
3610
  plugins,
3062
3611
  disabledFeatures: options.disabledFeatures
3063
3612
  });
@@ -3069,8 +3618,9 @@ function toJSON(source, options = {}) {
3069
3618
  }
3070
3619
  async function toJSONAsync(source, options = {}) {
3071
3620
  const plugins = resolvePlugins(options.plugins);
3072
- const ctx = createAsyncParserContext(1, {
3621
+ const ctx = createAsyncParserContext({
3073
3622
  compactArrayBufferViews: options.compactArrayBufferViews,
3623
+ depthLimit: options.depthLimit,
3074
3624
  plugins,
3075
3625
  disabledFeatures: options.disabledFeatures
3076
3626
  });
@@ -3088,11 +3638,11 @@ function compileJSON(source, options = {}) {
3088
3638
  }), source.t);
3089
3639
  }
3090
3640
  function fromJSON(source, options = {}) {
3091
- var _source$f;
3092
3641
  const plugins = resolvePlugins(options.plugins);
3093
- const disabledFeatures = options.disabledFeatures || 0;
3094
- const sourceFeatures = (_source$f = source.f) !== null && _source$f !== void 0 ? _source$f : 127;
3642
+ const disabledFeatures = options.disabledFeatures ?? 0;
3643
+ const sourceFeatures = source.f ?? 127;
3095
3644
  return deserializeTop(createVanillaDeserializerContext({
3645
+ depthLimit: options.depthLimit,
3096
3646
  maxBase64Length: options.maxBase64Length,
3097
3647
  plugins,
3098
3648
  markedRefs: source.m,
@@ -3102,12 +3652,16 @@ function fromJSON(source, options = {}) {
3102
3652
  }
3103
3653
  //#endregion
3104
3654
  exports.Feature = Feature;
3655
+ exports.LiveStream = LiveStream;
3105
3656
  exports.OpaqueReference = OpaqueReference;
3657
+ exports.Sequence = Sequence;
3106
3658
  exports.Serializer = Serializer;
3659
+ exports.SerovalAbortedError = SerovalAbortedError;
3107
3660
  exports.SerovalConflictedNodeIdError = SerovalConflictedNodeIdError;
3108
3661
  exports.SerovalDepthLimitError = SerovalDepthLimitError;
3109
3662
  exports.SerovalDeserializationError = SerovalDeserializationError;
3110
3663
  exports.SerovalError = SerovalError;
3664
+ exports.SerovalLiveStreamError = SerovalLiveStreamError;
3111
3665
  exports.SerovalMalformedNodeError = SerovalMalformedNodeError;
3112
3666
  exports.SerovalMissingInstanceError = SerovalMissingInstanceError;
3113
3667
  exports.SerovalMissingPluginError = SerovalMissingPluginError;
@@ -3119,7 +3673,10 @@ exports.SerovalSerializationError = SerovalSerializationError;
3119
3673
  exports.SerovalUnknownTypedArrayError = SerovalUnknownTypedArrayError;
3120
3674
  exports.SerovalUnsupportedNodeError = SerovalUnsupportedNodeError;
3121
3675
  exports.SerovalUnsupportedTypeError = SerovalUnsupportedTypeError;
3676
+ exports.Stream = Stream;
3122
3677
  exports.compileJSON = compileJSON;
3678
+ exports.createCrossDeserializer = createCrossDeserializer;
3679
+ exports.createLiveStream = createLiveStream;
3123
3680
  exports.createPlugin = createPlugin;
3124
3681
  exports.createReference = createReference;
3125
3682
  exports.createStream = createStream;
@@ -3130,6 +3687,8 @@ exports.deserialize = deserialize;
3130
3687
  exports.fromCrossJSON = fromCrossJSON;
3131
3688
  exports.fromJSON = fromJSON;
3132
3689
  exports.getCrossReferenceHeader = getCrossReferenceHeader;
3690
+ exports.getPlainObjectPlugins = getPlainObjectPlugins;
3691
+ exports.isLiveStream = isLiveStream;
3133
3692
  exports.isStream = isStream;
3134
3693
  exports.resolvePlugins = resolvePlugins;
3135
3694
  exports.serialize = serialize;