seroval 0.4.0 → 0.5.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.
@@ -10,7 +10,7 @@ var Feature = /* @__PURE__ */ ((Feature2) => {
10
10
  Feature2[Feature2["ObjectAssign"] = 128] = "ObjectAssign";
11
11
  Feature2[Feature2["Promise"] = 256] = "Promise";
12
12
  Feature2[Feature2["Set"] = 512] = "Set";
13
- Feature2[Feature2["SymbolIterator"] = 1024] = "SymbolIterator";
13
+ Feature2[Feature2["Symbol"] = 1024] = "Symbol";
14
14
  Feature2[Feature2["TypedArray"] = 2048] = "TypedArray";
15
15
  Feature2[Feature2["BigIntTypedArray"] = 4096] = "BigIntTypedArray";
16
16
  return Feature2;
@@ -54,7 +54,8 @@ function createSerializationContext(options) {
54
54
  validRefs: [],
55
55
  refSize: 0,
56
56
  features: options.features,
57
- markedRefs: new Set(options.markedRefs)
57
+ markedRefs: new Set(options.markedRefs),
58
+ valueMap: /* @__PURE__ */ new Map()
58
59
  };
59
60
  }
60
61
  function markRef(ctx, current) {
@@ -137,13 +138,151 @@ function quote(str) {
137
138
  } else {
138
139
  result += str.slice(lastPos);
139
140
  }
140
- return '"' + result + '"';
141
+ return result;
142
+ }
143
+ function invQuote(str) {
144
+ return str.replace(/\\"/g, '"').replace(/\\\\/g, "\\").replace(/\\x3C/g, "<").replace(/\\n/g, "\n").replace(/\\r/g, "\r").replace(/\\u2028/g, "\u2028").replace(/\\u2029/g, "\u2029");
141
145
  }
142
146
 
147
+ // src/tree/symbols.ts
148
+ var SYMBOL_STRING = {
149
+ [0 /* AsyncIterator */]: "Symbol.asyncIterator",
150
+ [1 /* HasInstance */]: "Symbol.hasInstance",
151
+ [2 /* IsConcatSpreadable */]: "Symbol.isConcatSpreadable",
152
+ [3 /* Iterator */]: "Symbol.iterator",
153
+ [4 /* Match */]: "Symbol.match",
154
+ [5 /* MatchAll */]: "Symbol.matchAll",
155
+ [6 /* Replace */]: "Symbol.replace",
156
+ [7 /* Search */]: "Symbol.search",
157
+ [8 /* Species */]: "Symbol.species",
158
+ [9 /* Split */]: "Symbol.split",
159
+ [10 /* ToPrimitive */]: "Symbol.toPrimitive",
160
+ [11 /* ToStringTag */]: "Symbol.toStringTag",
161
+ [12 /* Unscopables */]: "Symbol.unscopables"
162
+ };
163
+ var INV_SYMBOL_REF = {
164
+ [Symbol.asyncIterator]: 0 /* AsyncIterator */,
165
+ [Symbol.hasInstance]: 1 /* HasInstance */,
166
+ [Symbol.isConcatSpreadable]: 2 /* IsConcatSpreadable */,
167
+ [Symbol.iterator]: 3 /* Iterator */,
168
+ [Symbol.match]: 4 /* Match */,
169
+ [Symbol.matchAll]: 5 /* MatchAll */,
170
+ [Symbol.replace]: 6 /* Replace */,
171
+ [Symbol.search]: 7 /* Search */,
172
+ [Symbol.species]: 8 /* Species */,
173
+ [Symbol.split]: 9 /* Split */,
174
+ [Symbol.toPrimitive]: 10 /* ToPrimitive */,
175
+ [Symbol.toStringTag]: 11 /* ToStringTag */,
176
+ [Symbol.unscopables]: 12 /* Unscopables */
177
+ };
178
+ var SYMBOL_REF = {
179
+ [0 /* AsyncIterator */]: Symbol.asyncIterator,
180
+ [1 /* HasInstance */]: Symbol.hasInstance,
181
+ [2 /* IsConcatSpreadable */]: Symbol.isConcatSpreadable,
182
+ [3 /* Iterator */]: Symbol.iterator,
183
+ [4 /* Match */]: Symbol.match,
184
+ [5 /* MatchAll */]: Symbol.matchAll,
185
+ [6 /* Replace */]: Symbol.replace,
186
+ [7 /* Search */]: Symbol.search,
187
+ [8 /* Species */]: Symbol.species,
188
+ [9 /* Split */]: Symbol.split,
189
+ [10 /* ToPrimitive */]: Symbol.toPrimitive,
190
+ [11 /* ToStringTag */]: Symbol.toStringTag,
191
+ [12 /* Unscopables */]: Symbol.unscopables
192
+ };
193
+
143
194
  // src/tree/primitives.ts
144
- function createPrimitiveNode(value) {
195
+ var TRUE_NODE = {
196
+ t: 2 /* Boolean */,
197
+ i: void 0,
198
+ s: true,
199
+ l: void 0,
200
+ c: void 0,
201
+ m: void 0,
202
+ d: void 0,
203
+ a: void 0,
204
+ n: void 0
205
+ };
206
+ var FALSE_NODE = {
207
+ t: 2 /* Boolean */,
208
+ i: void 0,
209
+ s: false,
210
+ l: void 0,
211
+ c: void 0,
212
+ m: void 0,
213
+ d: void 0,
214
+ a: void 0,
215
+ n: void 0
216
+ };
217
+ var UNDEFINED_NODE = {
218
+ t: 4 /* Undefined */,
219
+ i: void 0,
220
+ s: void 0,
221
+ l: void 0,
222
+ c: void 0,
223
+ m: void 0,
224
+ d: void 0,
225
+ a: void 0,
226
+ n: void 0
227
+ };
228
+ var NULL_NODE = {
229
+ t: 3 /* Null */,
230
+ i: void 0,
231
+ s: void 0,
232
+ l: void 0,
233
+ c: void 0,
234
+ m: void 0,
235
+ d: void 0,
236
+ a: void 0,
237
+ n: void 0
238
+ };
239
+ var NEG_ZERO_NODE = {
240
+ t: 5 /* NegativeZero */,
241
+ i: void 0,
242
+ s: void 0,
243
+ l: void 0,
244
+ c: void 0,
245
+ m: void 0,
246
+ d: void 0,
247
+ a: void 0,
248
+ n: void 0
249
+ };
250
+ var INFINITY_NODE = {
251
+ t: 6 /* Infinity */,
252
+ i: void 0,
253
+ s: void 0,
254
+ l: void 0,
255
+ c: void 0,
256
+ m: void 0,
257
+ d: void 0,
258
+ a: void 0,
259
+ n: void 0
260
+ };
261
+ var NEG_INFINITY_NODE = {
262
+ t: 7 /* NegativeInfinity */,
263
+ i: void 0,
264
+ s: void 0,
265
+ l: void 0,
266
+ c: void 0,
267
+ m: void 0,
268
+ d: void 0,
269
+ a: void 0,
270
+ n: void 0
271
+ };
272
+ var NAN_NODE = {
273
+ t: 8 /* NaN */,
274
+ i: void 0,
275
+ s: void 0,
276
+ l: void 0,
277
+ c: void 0,
278
+ m: void 0,
279
+ d: void 0,
280
+ a: void 0,
281
+ n: void 0
282
+ };
283
+ function createNumberNode(value) {
145
284
  return {
146
- t: 0 /* Primitive */,
285
+ t: 0 /* Number */,
147
286
  i: void 0,
148
287
  s: value,
149
288
  l: void 0,
@@ -154,19 +293,25 @@ function createPrimitiveNode(value) {
154
293
  n: void 0
155
294
  };
156
295
  }
157
- var TRUE_NODE = createPrimitiveNode("!0");
158
- var FALSE_NODE = createPrimitiveNode("!1");
159
- var UNDEFINED_NODE = createPrimitiveNode("void 0");
160
- var NULL_NODE = createPrimitiveNode(null);
161
- var NEG_ZERO_NODE = createPrimitiveNode("-0");
162
- var INFINITY_NODE = createPrimitiveNode("1/0");
163
- var NEG_INFINITY_NODE = createPrimitiveNode("-1/0");
296
+ function createStringNode(value) {
297
+ return {
298
+ t: 1 /* String */,
299
+ i: void 0,
300
+ s: quote(value),
301
+ l: void 0,
302
+ c: void 0,
303
+ m: void 0,
304
+ d: void 0,
305
+ a: void 0,
306
+ n: void 0
307
+ };
308
+ }
164
309
  function createBigIntNode(ctx, current) {
165
310
  assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
166
311
  return {
167
- t: 1 /* BigInt */,
312
+ t: 9 /* BigInt */,
168
313
  i: void 0,
169
- s: current.toString() + "n",
314
+ s: current.toString(),
170
315
  l: void 0,
171
316
  c: void 0,
172
317
  m: void 0,
@@ -177,7 +322,7 @@ function createBigIntNode(ctx, current) {
177
322
  }
178
323
  function createReferenceNode(id) {
179
324
  return {
180
- t: 2 /* Reference */,
325
+ t: 10 /* Reference */,
181
326
  i: id,
182
327
  s: void 0,
183
328
  l: void 0,
@@ -190,7 +335,7 @@ function createReferenceNode(id) {
190
335
  }
191
336
  function createDateNode(id, current) {
192
337
  return {
193
- t: 3 /* Date */,
338
+ t: 11 /* Date */,
194
339
  i: id,
195
340
  s: current.toISOString(),
196
341
  l: void 0,
@@ -203,12 +348,12 @@ function createDateNode(id, current) {
203
348
  }
204
349
  function createRegExpNode(id, current) {
205
350
  return {
206
- t: 4 /* RegExp */,
351
+ t: 12 /* RegExp */,
207
352
  i: id,
208
- s: String(current),
353
+ s: void 0,
209
354
  l: void 0,
210
- c: void 0,
211
- m: void 0,
355
+ c: current.source,
356
+ m: current.flags,
212
357
  d: void 0,
213
358
  a: void 0,
214
359
  n: void 0
@@ -217,10 +362,15 @@ function createRegExpNode(id, current) {
217
362
  function createTypedArrayNode(ctx, id, current) {
218
363
  const constructor = current.constructor.name;
219
364
  assert(ctx.features & 2048 /* TypedArray */, `Unsupported value type "${constructor}"`);
365
+ const len = current.length;
366
+ const values = new Array(len);
367
+ for (let i = 0; i < len; i++) {
368
+ values[i] = current[i].toString();
369
+ }
220
370
  return {
221
- t: 14 /* TypedArray */,
371
+ t: 22 /* TypedArray */,
222
372
  i: id,
223
- s: current.toString(),
373
+ s: values,
224
374
  l: current.byteOffset,
225
375
  c: constructor,
226
376
  m: void 0,
@@ -236,17 +386,15 @@ function createBigIntTypedArrayNode(ctx, id, current) {
236
386
  (ctx.features & BIGINT_FLAG) === BIGINT_FLAG,
237
387
  `Unsupported value type "${constructor}"`
238
388
  );
239
- let result = "";
240
- for (let i = 0, len = current.length; i < len; i++) {
241
- if (i !== 0) {
242
- result += ",";
243
- }
244
- result += current[i].toString() + "n";
389
+ const len = current.length;
390
+ const values = new Array(len);
391
+ for (let i = 0; i < len; i++) {
392
+ values[i] = current[i].toString();
245
393
  }
246
394
  return {
247
- t: 15 /* BigIntTypedArray */,
395
+ t: 23 /* BigIntTypedArray */,
248
396
  i: id,
249
- s: result,
397
+ s: values,
250
398
  l: current.byteOffset,
251
399
  c: constructor,
252
400
  m: void 0,
@@ -255,9 +403,22 @@ function createBigIntTypedArrayNode(ctx, id, current) {
255
403
  n: void 0
256
404
  };
257
405
  }
406
+ function createWKSymbolNode(current) {
407
+ return {
408
+ t: 24 /* WKSymbol */,
409
+ i: void 0,
410
+ s: INV_SYMBOL_REF[current],
411
+ l: void 0,
412
+ c: void 0,
413
+ m: void 0,
414
+ d: void 0,
415
+ a: void 0,
416
+ n: void 0
417
+ };
418
+ }
258
419
 
259
420
  // src/tree/shared.ts
260
- function getErrorConstructor(error) {
421
+ function getErrorConstructorName(error) {
261
422
  if (error instanceof EvalError) {
262
423
  return "EvalError";
263
424
  }
@@ -278,9 +439,29 @@ function getErrorConstructor(error) {
278
439
  }
279
440
  return "Error";
280
441
  }
442
+ function getErrorConstructor(errorName) {
443
+ switch (errorName) {
444
+ case "Error":
445
+ return Error;
446
+ case "EvalError":
447
+ return EvalError;
448
+ case "RangeError":
449
+ return RangeError;
450
+ case "ReferenceError":
451
+ return ReferenceError;
452
+ case "SyntaxError":
453
+ return SyntaxError;
454
+ case "TypeError":
455
+ return TypeError;
456
+ case "URIError":
457
+ return URIError;
458
+ default:
459
+ throw new Error(`Unknown Error constructor "${errorName}"`);
460
+ }
461
+ }
281
462
  function getErrorOptions(ctx, error) {
282
463
  let options;
283
- const constructor = getErrorConstructor(error);
464
+ const constructor = getErrorConstructorName(error);
284
465
  if (error.name !== constructor) {
285
466
  options = { name: error.name };
286
467
  } else if (error.constructor.name !== constructor) {
@@ -340,6 +521,39 @@ function isIterable(value) {
340
521
  }
341
522
  return Symbol.iterator in value;
342
523
  }
524
+ function getTypedArrayConstructor(name) {
525
+ switch (name) {
526
+ case "Int8Array":
527
+ return Int8Array;
528
+ case "Int16Array":
529
+ return Int16Array;
530
+ case "Int32Array":
531
+ return Int32Array;
532
+ case "Uint8Array":
533
+ return Uint8Array;
534
+ case "Uint16Array":
535
+ return Uint16Array;
536
+ case "Uint32Array":
537
+ return Uint32Array;
538
+ case "Uint8ClampedArray":
539
+ return Uint8ClampedArray;
540
+ case "Float32Array":
541
+ return Float32Array;
542
+ case "Float64Array":
543
+ return Float64Array;
544
+ case "BigInt64Array":
545
+ return BigInt64Array;
546
+ case "BigUint64Array":
547
+ return BigUint64Array;
548
+ default:
549
+ throw new Error(`Unknown TypedArray "${name}"`);
550
+ }
551
+ }
552
+ var IDENTIFIER_CHECK = /^[$A-Z_][0-9A-Z_$]*$/i;
553
+ function isValidIdentifier(name) {
554
+ const char = name[0];
555
+ return (char === "$" || char === "_" || char >= "A" && char <= "Z" || char >= "a" && char <= "z") && IDENTIFIER_CHECK.test(name);
556
+ }
343
557
 
344
558
  // src/tree/async.ts
345
559
  async function generateNodeList(ctx, current) {
@@ -366,10 +580,10 @@ async function generateNodeList(ctx, current) {
366
580
  }
367
581
  async function generateArrayNode(ctx, id, current) {
368
582
  return {
369
- t: 7 /* Array */,
583
+ t: 15 /* Array */,
370
584
  i: id,
371
585
  s: void 0,
372
- l: void 0,
586
+ l: current.length,
373
587
  c: void 0,
374
588
  m: void 0,
375
589
  d: void 0,
@@ -402,7 +616,7 @@ async function generateMapNode(ctx, id, current) {
402
616
  valueNodes[nodeSize + i] = await parse(ctx, deferredValue[i]);
403
617
  }
404
618
  return {
405
- t: 6 /* Map */,
619
+ t: 14 /* Map */,
406
620
  i: id,
407
621
  s: void 0,
408
622
  l: void 0,
@@ -431,10 +645,10 @@ async function generateSetNode(ctx, id, current) {
431
645
  nodes[nodeSize + i] = await parse(ctx, deferred[i]);
432
646
  }
433
647
  return {
434
- t: 5 /* Set */,
648
+ t: 13 /* Set */,
435
649
  i: id,
436
650
  s: void 0,
437
- l: void 0,
651
+ l: len,
438
652
  c: void 0,
439
653
  m: void 0,
440
654
  d: void 0,
@@ -475,25 +689,26 @@ async function generateProperties(ctx, properties) {
475
689
  };
476
690
  }
477
691
  async function generateIterableNode(ctx, id, current) {
478
- assert(ctx.features & 1024 /* SymbolIterator */, 'Unsupported type "Iterable"');
692
+ assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "Iterable"');
479
693
  const options = getIterableOptions(current);
694
+ const array = Array.from(current);
480
695
  return {
481
- t: 13 /* Iterable */,
696
+ t: 21 /* Iterable */,
482
697
  i: id,
483
698
  s: void 0,
484
- l: void 0,
699
+ l: array.length,
485
700
  c: void 0,
486
701
  m: void 0,
487
702
  // Parse options first before the items
488
703
  d: options ? await generateProperties(ctx, options) : void 0,
489
- a: await generateNodeList(ctx, Array.from(current)),
704
+ a: await generateNodeList(ctx, array),
490
705
  n: void 0
491
706
  };
492
707
  }
493
708
  async function generatePromiseNode(ctx, id, current) {
494
709
  assert(ctx.features & 256 /* Promise */, 'Unsupported type "Promise"');
495
710
  return current.then(async (value) => ({
496
- t: 10 /* Promise */,
711
+ t: 18 /* Promise */,
497
712
  i: id,
498
713
  s: void 0,
499
714
  l: void 0,
@@ -513,7 +728,7 @@ async function generateObjectNode(ctx, id, current, empty) {
513
728
  return generatePromiseNode(ctx, id, current);
514
729
  }
515
730
  return {
516
- t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
731
+ t: empty ? 17 /* NullConstructor */ : 16 /* Object */,
517
732
  i: id,
518
733
  s: void 0,
519
734
  l: void 0,
@@ -528,26 +743,26 @@ async function generateAggregateErrorNode(ctx, id, current) {
528
743
  const options = getErrorOptions(ctx, current);
529
744
  const optionsNode = options ? await generateProperties(ctx, options) : void 0;
530
745
  return {
531
- t: 12 /* AggregateError */,
746
+ t: 20 /* AggregateError */,
532
747
  i: id,
533
748
  s: void 0,
534
- l: void 0,
749
+ l: current.errors.length,
535
750
  c: void 0,
536
751
  m: current.message,
537
752
  d: optionsNode,
538
- a: void 0,
539
- n: await parse(ctx, current.errors)
753
+ a: await generateNodeList(ctx, current.errors),
754
+ n: void 0
540
755
  };
541
756
  }
542
757
  async function generateErrorNode(ctx, id, current) {
543
758
  const options = getErrorOptions(ctx, current);
544
759
  const optionsNode = options ? await generateProperties(ctx, options) : void 0;
545
760
  return {
546
- t: 11 /* Error */,
761
+ t: 19 /* Error */,
547
762
  i: id,
548
763
  s: void 0,
549
764
  l: void 0,
550
- c: getErrorConstructor(current),
765
+ c: getErrorConstructorName(current),
551
766
  m: current.message,
552
767
  d: optionsNode,
553
768
  a: void 0,
@@ -561,18 +776,21 @@ async function parse(ctx, current) {
561
776
  case "undefined":
562
777
  return UNDEFINED_NODE;
563
778
  case "string":
564
- return createPrimitiveNode(quote(current));
779
+ return createStringNode(current);
565
780
  case "number":
781
+ if (current !== current) {
782
+ return NAN_NODE;
783
+ }
566
784
  if (Object.is(current, -0)) {
567
785
  return NEG_ZERO_NODE;
568
786
  }
569
787
  if (Object.is(current, Infinity)) {
570
- return createPrimitiveNode("1/0");
788
+ return INFINITY_NODE;
571
789
  }
572
790
  if (Object.is(current, -Infinity)) {
573
- return createPrimitiveNode("-1/0");
791
+ return NEG_INFINITY_NODE;
574
792
  }
575
- return createPrimitiveNode(current);
793
+ return createNumberNode(current);
576
794
  case "bigint":
577
795
  return createBigIntNode(ctx, current);
578
796
  case "object": {
@@ -665,16 +883,207 @@ async function parse(ctx, current) {
665
883
  }
666
884
  throw new Error("Unsupported type");
667
885
  }
886
+ case "symbol":
887
+ assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "symbol"');
888
+ assert(current in INV_SYMBOL_REF, "seroval only supports well-known symbols");
889
+ return createWKSymbolNode(current);
668
890
  default:
669
891
  throw new Error("Unsupported type");
670
892
  }
671
893
  }
672
894
  async function parseAsync(ctx, current) {
673
895
  const result = await parse(ctx, current);
674
- const isObject = result.t === 8 /* Object */ || result.t === 13 /* Iterable */;
896
+ const isObject = result.t === 16 /* Object */ || result.t === 21 /* Iterable */;
675
897
  return [result, createRef(ctx, current, false), isObject];
676
898
  }
677
899
 
900
+ // src/tree/deserialize.ts
901
+ function assignRef(ctx, index, value) {
902
+ ctx.valueMap.set(index, value);
903
+ return value;
904
+ }
905
+ function deserializeNodeList(ctx, node, result) {
906
+ let item;
907
+ for (let i = 0, len = node.a.length; i < len; i++) {
908
+ item = node.a[i];
909
+ if (item) {
910
+ result[i] = deserializeTree(ctx, item);
911
+ }
912
+ }
913
+ return result;
914
+ }
915
+ function deserializeArray(ctx, node) {
916
+ const result = assignRef(
917
+ ctx,
918
+ node.i,
919
+ new Array(node.l)
920
+ );
921
+ ctx.stack.push(node.i);
922
+ deserializeNodeList(ctx, node, result);
923
+ ctx.stack.pop();
924
+ return result;
925
+ }
926
+ function deserializeProperties(ctx, node, result) {
927
+ if (node.s === 0) {
928
+ return {};
929
+ }
930
+ for (let i = 0; i < node.s; i++) {
931
+ result[node.k[i]] = deserializeTree(ctx, node.v[i]);
932
+ }
933
+ return result;
934
+ }
935
+ function deserializeNullConstructor(ctx, node) {
936
+ const result = assignRef(ctx, node.i, /* @__PURE__ */ Object.create(null));
937
+ ctx.stack.push(node.i);
938
+ deserializeProperties(ctx, node.d, result);
939
+ ctx.stack.pop();
940
+ return result;
941
+ }
942
+ function deserializeObject(ctx, node) {
943
+ const result = assignRef(ctx, node.i, {});
944
+ ctx.stack.push(node.i);
945
+ deserializeProperties(ctx, node.d, result);
946
+ ctx.stack.pop();
947
+ return result;
948
+ }
949
+ function deserializeSet(ctx, node) {
950
+ const result = assignRef(ctx, node.i, /* @__PURE__ */ new Set());
951
+ ctx.stack.push(node.i);
952
+ for (let i = 0, len = node.a.length; i < len; i++) {
953
+ result.add(deserializeTree(ctx, node.a[i]));
954
+ }
955
+ ctx.stack.pop();
956
+ return result;
957
+ }
958
+ function deserializeMap(ctx, node) {
959
+ const result = assignRef(
960
+ ctx,
961
+ node.i,
962
+ /* @__PURE__ */ new Map()
963
+ );
964
+ ctx.stack.push(node.i);
965
+ for (let i = 0; i < node.d.s; i++) {
966
+ result.set(
967
+ deserializeTree(ctx, node.d.k[i]),
968
+ deserializeTree(ctx, node.d.v[i])
969
+ );
970
+ }
971
+ ctx.stack.pop();
972
+ return result;
973
+ }
974
+ function deserializeDictionary(ctx, node, result) {
975
+ if (node.d) {
976
+ ctx.stack.push(node.i);
977
+ const fields = deserializeProperties(ctx, node.d, {});
978
+ ctx.stack.pop();
979
+ Object.assign(result, fields);
980
+ }
981
+ return result;
982
+ }
983
+ function deserializeAggregateError(ctx, node) {
984
+ const result = assignRef(ctx, node.i, new AggregateError([], node.m));
985
+ ctx.stack.push(node.i);
986
+ result.errors = deserializeNodeList(ctx, node, new Array(node.l));
987
+ ctx.stack.pop();
988
+ return deserializeDictionary(ctx, node, result);
989
+ }
990
+ function deserializeError(ctx, node) {
991
+ const ErrorConstructor = getErrorConstructor(node.c);
992
+ const result = assignRef(ctx, node.i, new ErrorConstructor(node.m));
993
+ return deserializeDictionary(ctx, node, result);
994
+ }
995
+ function createDeferred() {
996
+ let resolver;
997
+ return {
998
+ resolve(v) {
999
+ resolver(v);
1000
+ },
1001
+ promise: new Promise((res) => {
1002
+ resolver = res;
1003
+ })
1004
+ };
1005
+ }
1006
+ function deserializePromise(ctx, node) {
1007
+ const deferred = createDeferred();
1008
+ const result = assignRef(ctx, node.i, deferred.promise);
1009
+ deferred.resolve(deserializeTree(ctx, node.n));
1010
+ return result;
1011
+ }
1012
+ function deserializeTypedArray(ctx, node) {
1013
+ const TypedArray = getTypedArrayConstructor(node.c);
1014
+ const dummy = new TypedArray();
1015
+ const result = assignRef(ctx, node.i, new TypedArray(
1016
+ dummy.buffer,
1017
+ node.l
1018
+ ));
1019
+ for (let i = 0, len = node.s.length; i < len; i++) {
1020
+ result[i] = node.t === 23 /* BigIntTypedArray */ ? BigInt(node.s[i]) : Number(node.s[i]);
1021
+ }
1022
+ return result;
1023
+ }
1024
+ function deserializeIterable(ctx, node) {
1025
+ const values = [];
1026
+ deserializeNodeList(ctx, node, values);
1027
+ const result = assignRef(ctx, node.i, {
1028
+ [Symbol.iterator]: () => values.values()
1029
+ });
1030
+ return deserializeDictionary(ctx, node, result);
1031
+ }
1032
+ function deserializeTree(ctx, node) {
1033
+ switch (node.t) {
1034
+ case 0 /* Number */:
1035
+ case 2 /* Boolean */:
1036
+ return node.s;
1037
+ case 1 /* String */:
1038
+ return invQuote(node.s);
1039
+ case 4 /* Undefined */:
1040
+ return void 0;
1041
+ case 3 /* Null */:
1042
+ return null;
1043
+ case 5 /* NegativeZero */:
1044
+ return -0;
1045
+ case 6 /* Infinity */:
1046
+ return Infinity;
1047
+ case 7 /* NegativeInfinity */:
1048
+ return -Infinity;
1049
+ case 8 /* NaN */:
1050
+ return NaN;
1051
+ case 9 /* BigInt */:
1052
+ return BigInt(node.s);
1053
+ case 10 /* Reference */:
1054
+ return ctx.valueMap.get(node.i);
1055
+ case 15 /* Array */:
1056
+ return deserializeArray(ctx, node);
1057
+ case 16 /* Object */:
1058
+ return deserializeObject(ctx, node);
1059
+ case 17 /* NullConstructor */:
1060
+ return deserializeNullConstructor(ctx, node);
1061
+ case 11 /* Date */:
1062
+ return assignRef(ctx, node.i, new Date(node.s));
1063
+ case 12 /* RegExp */:
1064
+ return assignRef(ctx, node.i, new RegExp(node.c, node.m));
1065
+ case 13 /* Set */:
1066
+ return deserializeSet(ctx, node);
1067
+ case 14 /* Map */:
1068
+ return deserializeMap(ctx, node);
1069
+ case 23 /* BigIntTypedArray */:
1070
+ case 22 /* TypedArray */:
1071
+ return deserializeTypedArray(ctx, node);
1072
+ case 20 /* AggregateError */:
1073
+ return deserializeAggregateError(ctx, node);
1074
+ case 19 /* Error */:
1075
+ return deserializeError(ctx, node);
1076
+ case 21 /* Iterable */:
1077
+ return deserializeIterable(ctx, node);
1078
+ case 18 /* Promise */:
1079
+ return deserializePromise(ctx, node);
1080
+ case 24 /* WKSymbol */:
1081
+ return SYMBOL_REF[node.s];
1082
+ default:
1083
+ throw new Error("Unsupported type");
1084
+ }
1085
+ }
1086
+
678
1087
  // src/tree/serialize.ts
679
1088
  function getAssignmentExpression(assignment) {
680
1089
  switch (assignment.t) {
@@ -778,18 +1187,17 @@ function createObjectAssign(ctx, ref, key, value) {
778
1187
  markRef(ctx, ref);
779
1188
  createAssignment(ctx, getRefParam(ctx, ref) + "." + key, value);
780
1189
  }
781
- function assignRef(ctx, index, value) {
1190
+ function assignRef2(ctx, index, value) {
782
1191
  if (ctx.markedRefs.has(index)) {
783
1192
  return getRefParam(ctx, index) + "=" + value;
784
1193
  }
785
1194
  return value;
786
1195
  }
787
1196
  function isReferenceInStack(ctx, node) {
788
- return node.t === 2 /* Reference */ && ctx.stack.includes(node.i);
1197
+ return node.t === 10 /* Reference */ && ctx.stack.includes(node.i);
789
1198
  }
790
- var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
791
1199
  function serializeNodeList(ctx, node) {
792
- const size = node.a.length;
1200
+ const size = node.l;
793
1201
  let values = "";
794
1202
  let item;
795
1203
  let isHoley = false;
@@ -819,9 +1227,9 @@ function serializeArray(ctx, node) {
819
1227
  ctx.stack.push(node.i);
820
1228
  const result = serializeNodeList(ctx, node);
821
1229
  ctx.stack.pop();
822
- return assignRef(ctx, node.i, result);
1230
+ return assignRef2(ctx, node.i, result);
823
1231
  }
824
- function serializeObject(ctx, sourceID, node) {
1232
+ function serializeProperties(ctx, sourceID, node) {
825
1233
  if (node.s === 0) {
826
1234
  return "{}";
827
1235
  }
@@ -837,19 +1245,19 @@ function serializeObject(ctx, sourceID, node) {
837
1245
  key = node.k[i];
838
1246
  val = node.v[i];
839
1247
  check = Number(key);
840
- isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1248
+ isIdentifier = check >= 0 || isValidIdentifier(key);
841
1249
  if (isReferenceInStack(ctx, val)) {
842
1250
  refParam = getRefParam(ctx, val.i);
843
1251
  if (isIdentifier && Number.isNaN(check)) {
844
1252
  createObjectAssign(ctx, sourceID, key, refParam);
845
1253
  } else {
846
- createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
1254
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : '"' + quote(key) + '"', refParam);
847
1255
  }
848
1256
  } else {
849
1257
  if (hasPrev) {
850
1258
  result += ",";
851
1259
  }
852
- result += isIdentifier ? key : quote(key);
1260
+ result += isIdentifier ? key : '"' + quote(key) + '"';
853
1261
  result += ":" + serializeTree(ctx, val);
854
1262
  hasPrev = true;
855
1263
  }
@@ -858,7 +1266,7 @@ function serializeObject(ctx, sourceID, node) {
858
1266
  return "{" + result + "}";
859
1267
  }
860
1268
  function serializeWithObjectAssign(ctx, value, id, serialized) {
861
- const fields = serializeObject(ctx, id, value);
1269
+ const fields = serializeProperties(ctx, id, value);
862
1270
  if (fields !== "{}") {
863
1271
  return "Object.assign(" + serialized + "," + fields + ")";
864
1272
  }
@@ -882,11 +1290,11 @@ function serializeAssignments(ctx, sourceID, node) {
882
1290
  check = Number(key);
883
1291
  parentAssignment = ctx.assignments;
884
1292
  ctx.assignments = mainAssignments;
885
- isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1293
+ isIdentifier = check >= 0 || isValidIdentifier(key);
886
1294
  if (isIdentifier && Number.isNaN(check)) {
887
1295
  createObjectAssign(ctx, sourceID, key, refParam);
888
1296
  } else {
889
- createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
1297
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : '"' + quote(key) + '"', refParam);
890
1298
  }
891
1299
  ctx.assignments = parentAssignment;
892
1300
  }
@@ -902,18 +1310,21 @@ function serializeDictionary(ctx, i, d, init) {
902
1310
  const assignments = serializeAssignments(ctx, i, d);
903
1311
  if (assignments) {
904
1312
  const ref = getRefParam(ctx, i);
905
- return "(" + assignRef(ctx, i, init) + "," + assignments + ref + ")";
1313
+ return "(" + assignRef2(ctx, i, init) + "," + assignments + ref + ")";
906
1314
  }
907
1315
  }
908
1316
  }
909
- return assignRef(ctx, i, init);
1317
+ return assignRef2(ctx, i, init);
910
1318
  }
911
1319
  function serializeNullConstructor(ctx, node) {
912
1320
  return serializeDictionary(ctx, node.i, node.d, "Object.create(null)");
913
1321
  }
1322
+ function serializeObject(ctx, node) {
1323
+ return assignRef2(ctx, node.i, serializeProperties(ctx, node.i, node.d));
1324
+ }
914
1325
  function serializeSet(ctx, node) {
915
1326
  let serialized = "new Set";
916
- const size = node.a.length;
1327
+ const size = node.l;
917
1328
  if (size) {
918
1329
  let result = "";
919
1330
  ctx.stack.push(node.i);
@@ -936,7 +1347,7 @@ function serializeSet(ctx, node) {
936
1347
  serialized += "([" + result + "])";
937
1348
  }
938
1349
  }
939
- return assignRef(ctx, node.i, serialized);
1350
+ return assignRef2(ctx, node.i, serialized);
940
1351
  }
941
1352
  function serializeMap(ctx, node) {
942
1353
  let serialized = "new Map";
@@ -982,16 +1393,16 @@ function serializeMap(ctx, node) {
982
1393
  serialized += "([" + result + "])";
983
1394
  }
984
1395
  }
985
- return assignRef(ctx, node.i, serialized);
1396
+ return assignRef2(ctx, node.i, serialized);
986
1397
  }
987
1398
  function serializeAggregateError(ctx, node) {
988
1399
  ctx.stack.push(node.i);
989
- const serialized = "new AggregateError(" + serializeTree(ctx, node.n) + "," + quote(node.m) + ")";
1400
+ const serialized = "new AggregateError(" + serializeNodeList(ctx, node) + ',"' + quote(node.m) + '")';
990
1401
  ctx.stack.pop();
991
1402
  return serializeDictionary(ctx, node.i, node.d, serialized);
992
1403
  }
993
1404
  function serializeError(ctx, node) {
994
- const serialized = "new " + node.c + "(" + quote(node.m) + ")";
1405
+ const serialized = "new " + node.c + '("' + quote(node.m) + '")';
995
1406
  return serializeDictionary(ctx, node.i, node.d, serialized);
996
1407
  }
997
1408
  function serializePromise(ctx, node) {
@@ -1009,14 +1420,24 @@ function serializePromise(ctx, node) {
1009
1420
  ctx.stack.pop();
1010
1421
  serialized = "Promise.resolve(" + result + ")";
1011
1422
  }
1012
- return assignRef(ctx, node.i, serialized);
1423
+ return assignRef2(ctx, node.i, serialized);
1013
1424
  }
1014
1425
  function serializeTypedArray(ctx, node) {
1015
- let args = "[" + node.s + "]";
1426
+ let result = "";
1427
+ for (let i = 0, len = node.s.length; i < len; i++) {
1428
+ if (i !== 0) {
1429
+ result += ",";
1430
+ }
1431
+ result += node.s[i];
1432
+ if (node.t === 23 /* BigIntTypedArray */) {
1433
+ result += "n";
1434
+ }
1435
+ }
1436
+ let args = "[" + result + "]";
1016
1437
  if (node.l !== 0) {
1017
1438
  args += "," + node.l.toString();
1018
1439
  }
1019
- return assignRef(ctx, node.i, "new " + node.c + "(" + args + ")");
1440
+ return assignRef2(ctx, node.i, "new " + node.c + "(" + args + ")");
1020
1441
  }
1021
1442
  function serializeIterable(ctx, node) {
1022
1443
  const parent = ctx.stack;
@@ -1040,37 +1461,55 @@ function serializeIterable(ctx, node) {
1040
1461
  }
1041
1462
  function serializeTree(ctx, node) {
1042
1463
  switch (node.t) {
1043
- case 0 /* Primitive */:
1464
+ case 0 /* Number */:
1044
1465
  return String(node.s);
1045
- case 1 /* BigInt */:
1046
- return node.s;
1047
- case 2 /* Reference */:
1466
+ case 1 /* String */:
1467
+ return '"' + node.s + '"';
1468
+ case 2 /* Boolean */:
1469
+ return node.s ? "!0" : "!1";
1470
+ case 4 /* Undefined */:
1471
+ return "undefined";
1472
+ case 3 /* Null */:
1473
+ return "null";
1474
+ case 5 /* NegativeZero */:
1475
+ return "-0";
1476
+ case 6 /* Infinity */:
1477
+ return "1/0";
1478
+ case 7 /* NegativeInfinity */:
1479
+ return "-1/0";
1480
+ case 8 /* NaN */:
1481
+ return "NaN";
1482
+ case 9 /* BigInt */:
1483
+ return node.s + "n";
1484
+ case 10 /* Reference */:
1048
1485
  return getRefParam(ctx, node.i);
1049
- case 7 /* Array */:
1486
+ case 15 /* Array */:
1050
1487
  return serializeArray(ctx, node);
1051
- case 8 /* Object */:
1052
- return assignRef(ctx, node.i, serializeObject(ctx, node.i, node.d));
1053
- case 9 /* NullConstructor */:
1488
+ case 16 /* Object */:
1489
+ return serializeObject(ctx, node);
1490
+ case 17 /* NullConstructor */:
1054
1491
  return serializeNullConstructor(ctx, node);
1055
- case 3 /* Date */:
1056
- return assignRef(ctx, node.i, 'new Date("' + node.s + '")');
1057
- case 4 /* RegExp */:
1058
- return assignRef(ctx, node.i, node.s);
1059
- case 5 /* Set */:
1492
+ case 11 /* Date */:
1493
+ return assignRef2(ctx, node.i, 'new Date("' + node.s + '")');
1494
+ case 12 /* RegExp */:
1495
+ return assignRef2(ctx, node.i, "/" + node.c + "/" + node.m);
1496
+ case 13 /* Set */:
1060
1497
  return serializeSet(ctx, node);
1061
- case 6 /* Map */:
1498
+ case 14 /* Map */:
1062
1499
  return serializeMap(ctx, node);
1063
- case 15 /* BigIntTypedArray */:
1064
- case 14 /* TypedArray */:
1500
+ case 23 /* BigIntTypedArray */:
1501
+ case 22 /* TypedArray */:
1065
1502
  return serializeTypedArray(ctx, node);
1066
- case 12 /* AggregateError */:
1503
+ case 20 /* AggregateError */:
1067
1504
  return serializeAggregateError(ctx, node);
1068
- case 11 /* Error */:
1505
+ case 19 /* Error */:
1069
1506
  return serializeError(ctx, node);
1070
- case 13 /* Iterable */:
1507
+ case 21 /* Iterable */:
1071
1508
  return serializeIterable(ctx, node);
1072
- case 10 /* Promise */:
1509
+ case 18 /* Promise */:
1073
1510
  return serializePromise(ctx, node);
1511
+ case 24 /* WKSymbol */:
1512
+ return SYMBOL_STRING[node.s];
1074
1513
  default:
1075
1514
  throw new Error("Unsupported type");
1076
1515
  }
@@ -1101,10 +1540,10 @@ function generateNodeList2(ctx, current) {
1101
1540
  }
1102
1541
  function generateArrayNode2(ctx, id, current) {
1103
1542
  return {
1104
- t: 7 /* Array */,
1543
+ t: 15 /* Array */,
1105
1544
  i: id,
1106
1545
  s: void 0,
1107
- l: void 0,
1546
+ l: current.length,
1108
1547
  c: void 0,
1109
1548
  m: void 0,
1110
1549
  d: void 0,
@@ -1137,7 +1576,7 @@ function generateMapNode2(ctx, id, current) {
1137
1576
  valueNodes[nodeSize + i] = parse2(ctx, deferredValue[i]);
1138
1577
  }
1139
1578
  return {
1140
- t: 6 /* Map */,
1579
+ t: 14 /* Map */,
1141
1580
  i: id,
1142
1581
  s: void 0,
1143
1582
  l: void 0,
@@ -1166,10 +1605,10 @@ function generateSetNode2(ctx, id, current) {
1166
1605
  nodes[nodeSize + i] = parse2(ctx, deferred[i]);
1167
1606
  }
1168
1607
  return {
1169
- t: 5 /* Set */,
1608
+ t: 13 /* Set */,
1170
1609
  i: id,
1171
1610
  s: void 0,
1172
- l: void 0,
1611
+ l: len,
1173
1612
  c: void 0,
1174
1613
  m: void 0,
1175
1614
  d: void 0,
@@ -1210,18 +1649,19 @@ function generateProperties2(ctx, properties) {
1210
1649
  };
1211
1650
  }
1212
1651
  function generateIterableNode2(ctx, id, current) {
1213
- assert(ctx.features & 1024 /* SymbolIterator */, 'Unsupported type "Iterable"');
1652
+ assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "Iterable"');
1214
1653
  const options = getIterableOptions(current);
1654
+ const array = Array.from(current);
1215
1655
  return {
1216
- t: 13 /* Iterable */,
1656
+ t: 21 /* Iterable */,
1217
1657
  i: id,
1218
1658
  s: void 0,
1219
- l: void 0,
1659
+ l: array.length,
1220
1660
  c: void 0,
1221
1661
  m: void 0,
1222
1662
  // Parse options first before the items
1223
1663
  d: options ? generateProperties2(ctx, options) : void 0,
1224
- a: generateNodeList2(ctx, Array.from(current)),
1664
+ a: generateNodeList2(ctx, array),
1225
1665
  n: void 0
1226
1666
  };
1227
1667
  }
@@ -1230,7 +1670,7 @@ function generateObjectNode2(ctx, id, current, empty) {
1230
1670
  return generateIterableNode2(ctx, id, current);
1231
1671
  }
1232
1672
  return {
1233
- t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
1673
+ t: empty ? 17 /* NullConstructor */ : 16 /* Object */,
1234
1674
  i: id,
1235
1675
  s: void 0,
1236
1676
  l: void 0,
@@ -1245,26 +1685,26 @@ function generateAggregateErrorNode2(ctx, id, current) {
1245
1685
  const options = getErrorOptions(ctx, current);
1246
1686
  const optionsNode = options ? generateProperties2(ctx, options) : void 0;
1247
1687
  return {
1248
- t: 12 /* AggregateError */,
1688
+ t: 20 /* AggregateError */,
1249
1689
  i: id,
1250
1690
  s: void 0,
1251
- l: void 0,
1691
+ l: current.errors.length,
1252
1692
  c: void 0,
1253
1693
  m: current.message,
1254
1694
  d: optionsNode,
1255
- a: void 0,
1256
- n: parse2(ctx, current.errors)
1695
+ a: generateNodeList2(ctx, current.errors),
1696
+ n: void 0
1257
1697
  };
1258
1698
  }
1259
1699
  function generateErrorNode2(ctx, id, current) {
1260
1700
  const options = getErrorOptions(ctx, current);
1261
1701
  const optionsNode = options ? generateProperties2(ctx, options) : void 0;
1262
1702
  return {
1263
- t: 11 /* Error */,
1703
+ t: 19 /* Error */,
1264
1704
  i: id,
1265
1705
  s: void 0,
1266
1706
  l: void 0,
1267
- c: getErrorConstructor(current),
1707
+ c: getErrorConstructorName(current),
1268
1708
  m: current.message,
1269
1709
  d: optionsNode,
1270
1710
  a: void 0,
@@ -1278,18 +1718,21 @@ function parse2(ctx, current) {
1278
1718
  case "undefined":
1279
1719
  return UNDEFINED_NODE;
1280
1720
  case "string":
1281
- return createPrimitiveNode(quote(current));
1721
+ return createStringNode(current);
1282
1722
  case "number":
1723
+ if (current !== current) {
1724
+ return NAN_NODE;
1725
+ }
1283
1726
  if (Object.is(current, -0)) {
1284
1727
  return NEG_ZERO_NODE;
1285
1728
  }
1286
1729
  if (Object.is(current, Infinity)) {
1287
- return createPrimitiveNode("1/0");
1730
+ return INFINITY_NODE;
1288
1731
  }
1289
1732
  if (Object.is(current, -Infinity)) {
1290
- return createPrimitiveNode("-1/0");
1733
+ return NEG_INFINITY_NODE;
1291
1734
  }
1292
- return createPrimitiveNode(current);
1735
+ return createNumberNode(current);
1293
1736
  case "bigint":
1294
1737
  return createBigIntNode(ctx, current);
1295
1738
  case "object": {
@@ -1356,13 +1799,17 @@ function parse2(ctx, current) {
1356
1799
  }
1357
1800
  throw new Error("Unsupported type");
1358
1801
  }
1802
+ case "symbol":
1803
+ assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "symbol"');
1804
+ assert(current in INV_SYMBOL_REF, "seroval only supports well-known symbols");
1805
+ return createWKSymbolNode(current);
1359
1806
  default:
1360
1807
  throw new Error("Unsupported type");
1361
1808
  }
1362
1809
  }
1363
1810
  function parseSync(ctx, current) {
1364
1811
  const result = parse2(ctx, current);
1365
- const isObject = result.t === 8 /* Object */ || result.t === 13 /* Iterable */;
1812
+ const isObject = result.t === 16 /* Object */ || result.t === 21 /* Iterable */;
1366
1813
  return [result, createRef(ctx, current, false), isObject];
1367
1814
  }
1368
1815
 
@@ -1410,36 +1857,39 @@ function deserialize(source) {
1410
1857
  function toJSON(source, options) {
1411
1858
  const ctx = createParserContext(options);
1412
1859
  const [tree, root, isObject] = parseSync(ctx, source);
1413
- return JSON.stringify({
1860
+ return {
1414
1861
  t: tree,
1415
1862
  r: root,
1416
1863
  i: isObject,
1417
1864
  f: ctx.features,
1418
1865
  m: Array.from(ctx.markedRefs)
1419
- });
1866
+ };
1420
1867
  }
1421
1868
  async function toJSONAsync(source, options) {
1422
1869
  const ctx = createParserContext(options);
1423
1870
  const [tree, root, isObject] = await parseAsync(ctx, source);
1424
- return JSON.stringify({
1871
+ return {
1425
1872
  t: tree,
1426
1873
  r: root,
1427
1874
  i: isObject,
1428
1875
  f: ctx.features,
1429
1876
  m: Array.from(ctx.markedRefs)
1430
- });
1877
+ };
1431
1878
  }
1432
1879
  function compileJSON(source) {
1433
- const parsed = JSON.parse(source);
1434
1880
  const serial = createSerializationContext({
1435
- features: parsed.f,
1436
- markedRefs: parsed.m
1881
+ features: source.f,
1882
+ markedRefs: source.m
1437
1883
  });
1438
- const result = serializeTree(serial, parsed.t);
1439
- return finalize(serial, parsed.r, parsed.i, result);
1884
+ const result = serializeTree(serial, source.t);
1885
+ return finalize(serial, source.r, source.i, result);
1440
1886
  }
1441
1887
  function fromJSON(source) {
1442
- return deserialize(compileJSON(source));
1888
+ const serial = createSerializationContext({
1889
+ features: source.f,
1890
+ markedRefs: source.m
1891
+ });
1892
+ return deserializeTree(serial, source.t);
1443
1893
  }
1444
1894
  var src_default = serialize;
1445
1895
  export {