seroval 0.4.1 → 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.
@@ -44,7 +44,7 @@ var Feature = /* @__PURE__ */ ((Feature2) => {
44
44
  Feature2[Feature2["ObjectAssign"] = 128] = "ObjectAssign";
45
45
  Feature2[Feature2["Promise"] = 256] = "Promise";
46
46
  Feature2[Feature2["Set"] = 512] = "Set";
47
- Feature2[Feature2["SymbolIterator"] = 1024] = "SymbolIterator";
47
+ Feature2[Feature2["Symbol"] = 1024] = "Symbol";
48
48
  Feature2[Feature2["TypedArray"] = 2048] = "TypedArray";
49
49
  Feature2[Feature2["BigIntTypedArray"] = 4096] = "BigIntTypedArray";
50
50
  return Feature2;
@@ -88,7 +88,8 @@ function createSerializationContext(options) {
88
88
  validRefs: [],
89
89
  refSize: 0,
90
90
  features: options.features,
91
- markedRefs: new Set(options.markedRefs)
91
+ markedRefs: new Set(options.markedRefs),
92
+ valueMap: /* @__PURE__ */ new Map()
92
93
  };
93
94
  }
94
95
  function markRef(ctx, current) {
@@ -171,13 +172,151 @@ function quote(str) {
171
172
  } else {
172
173
  result += str.slice(lastPos);
173
174
  }
174
- return '"' + result + '"';
175
+ return result;
176
+ }
177
+ function invQuote(str) {
178
+ 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");
175
179
  }
176
180
 
181
+ // src/tree/symbols.ts
182
+ var SYMBOL_STRING = {
183
+ [0 /* AsyncIterator */]: "Symbol.asyncIterator",
184
+ [1 /* HasInstance */]: "Symbol.hasInstance",
185
+ [2 /* IsConcatSpreadable */]: "Symbol.isConcatSpreadable",
186
+ [3 /* Iterator */]: "Symbol.iterator",
187
+ [4 /* Match */]: "Symbol.match",
188
+ [5 /* MatchAll */]: "Symbol.matchAll",
189
+ [6 /* Replace */]: "Symbol.replace",
190
+ [7 /* Search */]: "Symbol.search",
191
+ [8 /* Species */]: "Symbol.species",
192
+ [9 /* Split */]: "Symbol.split",
193
+ [10 /* ToPrimitive */]: "Symbol.toPrimitive",
194
+ [11 /* ToStringTag */]: "Symbol.toStringTag",
195
+ [12 /* Unscopables */]: "Symbol.unscopables"
196
+ };
197
+ var INV_SYMBOL_REF = {
198
+ [Symbol.asyncIterator]: 0 /* AsyncIterator */,
199
+ [Symbol.hasInstance]: 1 /* HasInstance */,
200
+ [Symbol.isConcatSpreadable]: 2 /* IsConcatSpreadable */,
201
+ [Symbol.iterator]: 3 /* Iterator */,
202
+ [Symbol.match]: 4 /* Match */,
203
+ [Symbol.matchAll]: 5 /* MatchAll */,
204
+ [Symbol.replace]: 6 /* Replace */,
205
+ [Symbol.search]: 7 /* Search */,
206
+ [Symbol.species]: 8 /* Species */,
207
+ [Symbol.split]: 9 /* Split */,
208
+ [Symbol.toPrimitive]: 10 /* ToPrimitive */,
209
+ [Symbol.toStringTag]: 11 /* ToStringTag */,
210
+ [Symbol.unscopables]: 12 /* Unscopables */
211
+ };
212
+ var SYMBOL_REF = {
213
+ [0 /* AsyncIterator */]: Symbol.asyncIterator,
214
+ [1 /* HasInstance */]: Symbol.hasInstance,
215
+ [2 /* IsConcatSpreadable */]: Symbol.isConcatSpreadable,
216
+ [3 /* Iterator */]: Symbol.iterator,
217
+ [4 /* Match */]: Symbol.match,
218
+ [5 /* MatchAll */]: Symbol.matchAll,
219
+ [6 /* Replace */]: Symbol.replace,
220
+ [7 /* Search */]: Symbol.search,
221
+ [8 /* Species */]: Symbol.species,
222
+ [9 /* Split */]: Symbol.split,
223
+ [10 /* ToPrimitive */]: Symbol.toPrimitive,
224
+ [11 /* ToStringTag */]: Symbol.toStringTag,
225
+ [12 /* Unscopables */]: Symbol.unscopables
226
+ };
227
+
177
228
  // src/tree/primitives.ts
178
- function createPrimitiveNode(value) {
229
+ var TRUE_NODE = {
230
+ t: 2 /* Boolean */,
231
+ i: void 0,
232
+ s: true,
233
+ l: void 0,
234
+ c: void 0,
235
+ m: void 0,
236
+ d: void 0,
237
+ a: void 0,
238
+ n: void 0
239
+ };
240
+ var FALSE_NODE = {
241
+ t: 2 /* Boolean */,
242
+ i: void 0,
243
+ s: false,
244
+ l: void 0,
245
+ c: void 0,
246
+ m: void 0,
247
+ d: void 0,
248
+ a: void 0,
249
+ n: void 0
250
+ };
251
+ var UNDEFINED_NODE = {
252
+ t: 4 /* Undefined */,
253
+ i: void 0,
254
+ s: void 0,
255
+ l: void 0,
256
+ c: void 0,
257
+ m: void 0,
258
+ d: void 0,
259
+ a: void 0,
260
+ n: void 0
261
+ };
262
+ var NULL_NODE = {
263
+ t: 3 /* Null */,
264
+ i: void 0,
265
+ s: void 0,
266
+ l: void 0,
267
+ c: void 0,
268
+ m: void 0,
269
+ d: void 0,
270
+ a: void 0,
271
+ n: void 0
272
+ };
273
+ var NEG_ZERO_NODE = {
274
+ t: 5 /* NegativeZero */,
275
+ i: void 0,
276
+ s: void 0,
277
+ l: void 0,
278
+ c: void 0,
279
+ m: void 0,
280
+ d: void 0,
281
+ a: void 0,
282
+ n: void 0
283
+ };
284
+ var INFINITY_NODE = {
285
+ t: 6 /* Infinity */,
286
+ i: void 0,
287
+ s: void 0,
288
+ l: void 0,
289
+ c: void 0,
290
+ m: void 0,
291
+ d: void 0,
292
+ a: void 0,
293
+ n: void 0
294
+ };
295
+ var NEG_INFINITY_NODE = {
296
+ t: 7 /* NegativeInfinity */,
297
+ i: void 0,
298
+ s: void 0,
299
+ l: void 0,
300
+ c: void 0,
301
+ m: void 0,
302
+ d: void 0,
303
+ a: void 0,
304
+ n: void 0
305
+ };
306
+ var NAN_NODE = {
307
+ t: 8 /* NaN */,
308
+ i: void 0,
309
+ s: void 0,
310
+ l: void 0,
311
+ c: void 0,
312
+ m: void 0,
313
+ d: void 0,
314
+ a: void 0,
315
+ n: void 0
316
+ };
317
+ function createNumberNode(value) {
179
318
  return {
180
- t: 0 /* Primitive */,
319
+ t: 0 /* Number */,
181
320
  i: void 0,
182
321
  s: value,
183
322
  l: void 0,
@@ -188,19 +327,25 @@ function createPrimitiveNode(value) {
188
327
  n: void 0
189
328
  };
190
329
  }
191
- var TRUE_NODE = createPrimitiveNode("!0");
192
- var FALSE_NODE = createPrimitiveNode("!1");
193
- var UNDEFINED_NODE = createPrimitiveNode("void 0");
194
- var NULL_NODE = createPrimitiveNode(null);
195
- var NEG_ZERO_NODE = createPrimitiveNode("-0");
196
- var INFINITY_NODE = createPrimitiveNode("1/0");
197
- var NEG_INFINITY_NODE = createPrimitiveNode("-1/0");
330
+ function createStringNode(value) {
331
+ return {
332
+ t: 1 /* String */,
333
+ i: void 0,
334
+ s: quote(value),
335
+ l: void 0,
336
+ c: void 0,
337
+ m: void 0,
338
+ d: void 0,
339
+ a: void 0,
340
+ n: void 0
341
+ };
342
+ }
198
343
  function createBigIntNode(ctx, current) {
199
344
  assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
200
345
  return {
201
- t: 1 /* BigInt */,
346
+ t: 9 /* BigInt */,
202
347
  i: void 0,
203
- s: current.toString() + "n",
348
+ s: current.toString(),
204
349
  l: void 0,
205
350
  c: void 0,
206
351
  m: void 0,
@@ -211,7 +356,7 @@ function createBigIntNode(ctx, current) {
211
356
  }
212
357
  function createReferenceNode(id) {
213
358
  return {
214
- t: 2 /* Reference */,
359
+ t: 10 /* Reference */,
215
360
  i: id,
216
361
  s: void 0,
217
362
  l: void 0,
@@ -224,7 +369,7 @@ function createReferenceNode(id) {
224
369
  }
225
370
  function createDateNode(id, current) {
226
371
  return {
227
- t: 3 /* Date */,
372
+ t: 11 /* Date */,
228
373
  i: id,
229
374
  s: current.toISOString(),
230
375
  l: void 0,
@@ -237,12 +382,12 @@ function createDateNode(id, current) {
237
382
  }
238
383
  function createRegExpNode(id, current) {
239
384
  return {
240
- t: 4 /* RegExp */,
385
+ t: 12 /* RegExp */,
241
386
  i: id,
242
- s: String(current),
387
+ s: void 0,
243
388
  l: void 0,
244
- c: void 0,
245
- m: void 0,
389
+ c: current.source,
390
+ m: current.flags,
246
391
  d: void 0,
247
392
  a: void 0,
248
393
  n: void 0
@@ -251,10 +396,15 @@ function createRegExpNode(id, current) {
251
396
  function createTypedArrayNode(ctx, id, current) {
252
397
  const constructor = current.constructor.name;
253
398
  assert(ctx.features & 2048 /* TypedArray */, `Unsupported value type "${constructor}"`);
399
+ const len = current.length;
400
+ const values = new Array(len);
401
+ for (let i = 0; i < len; i++) {
402
+ values[i] = current[i].toString();
403
+ }
254
404
  return {
255
- t: 14 /* TypedArray */,
405
+ t: 22 /* TypedArray */,
256
406
  i: id,
257
- s: current.toString(),
407
+ s: values,
258
408
  l: current.byteOffset,
259
409
  c: constructor,
260
410
  m: void 0,
@@ -270,17 +420,15 @@ function createBigIntTypedArrayNode(ctx, id, current) {
270
420
  (ctx.features & BIGINT_FLAG) === BIGINT_FLAG,
271
421
  `Unsupported value type "${constructor}"`
272
422
  );
273
- let result = "";
274
- for (let i = 0, len = current.length; i < len; i++) {
275
- if (i !== 0) {
276
- result += ",";
277
- }
278
- result += current[i].toString() + "n";
423
+ const len = current.length;
424
+ const values = new Array(len);
425
+ for (let i = 0; i < len; i++) {
426
+ values[i] = current[i].toString();
279
427
  }
280
428
  return {
281
- t: 15 /* BigIntTypedArray */,
429
+ t: 23 /* BigIntTypedArray */,
282
430
  i: id,
283
- s: result,
431
+ s: values,
284
432
  l: current.byteOffset,
285
433
  c: constructor,
286
434
  m: void 0,
@@ -289,9 +437,22 @@ function createBigIntTypedArrayNode(ctx, id, current) {
289
437
  n: void 0
290
438
  };
291
439
  }
440
+ function createWKSymbolNode(current) {
441
+ return {
442
+ t: 24 /* WKSymbol */,
443
+ i: void 0,
444
+ s: INV_SYMBOL_REF[current],
445
+ l: void 0,
446
+ c: void 0,
447
+ m: void 0,
448
+ d: void 0,
449
+ a: void 0,
450
+ n: void 0
451
+ };
452
+ }
292
453
 
293
454
  // src/tree/shared.ts
294
- function getErrorConstructor(error) {
455
+ function getErrorConstructorName(error) {
295
456
  if (error instanceof EvalError) {
296
457
  return "EvalError";
297
458
  }
@@ -312,9 +473,29 @@ function getErrorConstructor(error) {
312
473
  }
313
474
  return "Error";
314
475
  }
476
+ function getErrorConstructor(errorName) {
477
+ switch (errorName) {
478
+ case "Error":
479
+ return Error;
480
+ case "EvalError":
481
+ return EvalError;
482
+ case "RangeError":
483
+ return RangeError;
484
+ case "ReferenceError":
485
+ return ReferenceError;
486
+ case "SyntaxError":
487
+ return SyntaxError;
488
+ case "TypeError":
489
+ return TypeError;
490
+ case "URIError":
491
+ return URIError;
492
+ default:
493
+ throw new Error(`Unknown Error constructor "${errorName}"`);
494
+ }
495
+ }
315
496
  function getErrorOptions(ctx, error) {
316
497
  let options;
317
- const constructor = getErrorConstructor(error);
498
+ const constructor = getErrorConstructorName(error);
318
499
  if (error.name !== constructor) {
319
500
  options = { name: error.name };
320
501
  } else if (error.constructor.name !== constructor) {
@@ -374,6 +555,39 @@ function isIterable(value) {
374
555
  }
375
556
  return Symbol.iterator in value;
376
557
  }
558
+ function getTypedArrayConstructor(name) {
559
+ switch (name) {
560
+ case "Int8Array":
561
+ return Int8Array;
562
+ case "Int16Array":
563
+ return Int16Array;
564
+ case "Int32Array":
565
+ return Int32Array;
566
+ case "Uint8Array":
567
+ return Uint8Array;
568
+ case "Uint16Array":
569
+ return Uint16Array;
570
+ case "Uint32Array":
571
+ return Uint32Array;
572
+ case "Uint8ClampedArray":
573
+ return Uint8ClampedArray;
574
+ case "Float32Array":
575
+ return Float32Array;
576
+ case "Float64Array":
577
+ return Float64Array;
578
+ case "BigInt64Array":
579
+ return BigInt64Array;
580
+ case "BigUint64Array":
581
+ return BigUint64Array;
582
+ default:
583
+ throw new Error(`Unknown TypedArray "${name}"`);
584
+ }
585
+ }
586
+ var IDENTIFIER_CHECK = /^[$A-Z_][0-9A-Z_$]*$/i;
587
+ function isValidIdentifier(name) {
588
+ const char = name[0];
589
+ return (char === "$" || char === "_" || char >= "A" && char <= "Z" || char >= "a" && char <= "z") && IDENTIFIER_CHECK.test(name);
590
+ }
377
591
 
378
592
  // src/tree/async.ts
379
593
  async function generateNodeList(ctx, current) {
@@ -400,10 +614,10 @@ async function generateNodeList(ctx, current) {
400
614
  }
401
615
  async function generateArrayNode(ctx, id, current) {
402
616
  return {
403
- t: 7 /* Array */,
617
+ t: 15 /* Array */,
404
618
  i: id,
405
619
  s: void 0,
406
- l: void 0,
620
+ l: current.length,
407
621
  c: void 0,
408
622
  m: void 0,
409
623
  d: void 0,
@@ -436,7 +650,7 @@ async function generateMapNode(ctx, id, current) {
436
650
  valueNodes[nodeSize + i] = await parse(ctx, deferredValue[i]);
437
651
  }
438
652
  return {
439
- t: 6 /* Map */,
653
+ t: 14 /* Map */,
440
654
  i: id,
441
655
  s: void 0,
442
656
  l: void 0,
@@ -465,10 +679,10 @@ async function generateSetNode(ctx, id, current) {
465
679
  nodes[nodeSize + i] = await parse(ctx, deferred[i]);
466
680
  }
467
681
  return {
468
- t: 5 /* Set */,
682
+ t: 13 /* Set */,
469
683
  i: id,
470
684
  s: void 0,
471
- l: void 0,
685
+ l: len,
472
686
  c: void 0,
473
687
  m: void 0,
474
688
  d: void 0,
@@ -509,25 +723,26 @@ async function generateProperties(ctx, properties) {
509
723
  };
510
724
  }
511
725
  async function generateIterableNode(ctx, id, current) {
512
- assert(ctx.features & 1024 /* SymbolIterator */, 'Unsupported type "Iterable"');
726
+ assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "Iterable"');
513
727
  const options = getIterableOptions(current);
728
+ const array = Array.from(current);
514
729
  return {
515
- t: 13 /* Iterable */,
730
+ t: 21 /* Iterable */,
516
731
  i: id,
517
732
  s: void 0,
518
- l: void 0,
733
+ l: array.length,
519
734
  c: void 0,
520
735
  m: void 0,
521
736
  // Parse options first before the items
522
737
  d: options ? await generateProperties(ctx, options) : void 0,
523
- a: await generateNodeList(ctx, Array.from(current)),
738
+ a: await generateNodeList(ctx, array),
524
739
  n: void 0
525
740
  };
526
741
  }
527
742
  async function generatePromiseNode(ctx, id, current) {
528
743
  assert(ctx.features & 256 /* Promise */, 'Unsupported type "Promise"');
529
744
  return current.then(async (value) => ({
530
- t: 10 /* Promise */,
745
+ t: 18 /* Promise */,
531
746
  i: id,
532
747
  s: void 0,
533
748
  l: void 0,
@@ -547,7 +762,7 @@ async function generateObjectNode(ctx, id, current, empty) {
547
762
  return generatePromiseNode(ctx, id, current);
548
763
  }
549
764
  return {
550
- t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
765
+ t: empty ? 17 /* NullConstructor */ : 16 /* Object */,
551
766
  i: id,
552
767
  s: void 0,
553
768
  l: void 0,
@@ -562,26 +777,26 @@ async function generateAggregateErrorNode(ctx, id, current) {
562
777
  const options = getErrorOptions(ctx, current);
563
778
  const optionsNode = options ? await generateProperties(ctx, options) : void 0;
564
779
  return {
565
- t: 12 /* AggregateError */,
780
+ t: 20 /* AggregateError */,
566
781
  i: id,
567
782
  s: void 0,
568
- l: void 0,
783
+ l: current.errors.length,
569
784
  c: void 0,
570
785
  m: current.message,
571
786
  d: optionsNode,
572
- a: void 0,
573
- n: await parse(ctx, current.errors)
787
+ a: await generateNodeList(ctx, current.errors),
788
+ n: void 0
574
789
  };
575
790
  }
576
791
  async function generateErrorNode(ctx, id, current) {
577
792
  const options = getErrorOptions(ctx, current);
578
793
  const optionsNode = options ? await generateProperties(ctx, options) : void 0;
579
794
  return {
580
- t: 11 /* Error */,
795
+ t: 19 /* Error */,
581
796
  i: id,
582
797
  s: void 0,
583
798
  l: void 0,
584
- c: getErrorConstructor(current),
799
+ c: getErrorConstructorName(current),
585
800
  m: current.message,
586
801
  d: optionsNode,
587
802
  a: void 0,
@@ -595,18 +810,21 @@ async function parse(ctx, current) {
595
810
  case "undefined":
596
811
  return UNDEFINED_NODE;
597
812
  case "string":
598
- return createPrimitiveNode(quote(current));
813
+ return createStringNode(current);
599
814
  case "number":
815
+ if (current !== current) {
816
+ return NAN_NODE;
817
+ }
600
818
  if (Object.is(current, -0)) {
601
819
  return NEG_ZERO_NODE;
602
820
  }
603
821
  if (Object.is(current, Infinity)) {
604
- return createPrimitiveNode("1/0");
822
+ return INFINITY_NODE;
605
823
  }
606
824
  if (Object.is(current, -Infinity)) {
607
- return createPrimitiveNode("-1/0");
825
+ return NEG_INFINITY_NODE;
608
826
  }
609
- return createPrimitiveNode(current);
827
+ return createNumberNode(current);
610
828
  case "bigint":
611
829
  return createBigIntNode(ctx, current);
612
830
  case "object": {
@@ -699,16 +917,207 @@ async function parse(ctx, current) {
699
917
  }
700
918
  throw new Error("Unsupported type");
701
919
  }
920
+ case "symbol":
921
+ assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "symbol"');
922
+ assert(current in INV_SYMBOL_REF, "seroval only supports well-known symbols");
923
+ return createWKSymbolNode(current);
702
924
  default:
703
925
  throw new Error("Unsupported type");
704
926
  }
705
927
  }
706
928
  async function parseAsync(ctx, current) {
707
929
  const result = await parse(ctx, current);
708
- const isObject = result.t === 8 /* Object */ || result.t === 13 /* Iterable */;
930
+ const isObject = result.t === 16 /* Object */ || result.t === 21 /* Iterable */;
709
931
  return [result, createRef(ctx, current, false), isObject];
710
932
  }
711
933
 
934
+ // src/tree/deserialize.ts
935
+ function assignRef(ctx, index, value) {
936
+ ctx.valueMap.set(index, value);
937
+ return value;
938
+ }
939
+ function deserializeNodeList(ctx, node, result) {
940
+ let item;
941
+ for (let i = 0, len = node.a.length; i < len; i++) {
942
+ item = node.a[i];
943
+ if (item) {
944
+ result[i] = deserializeTree(ctx, item);
945
+ }
946
+ }
947
+ return result;
948
+ }
949
+ function deserializeArray(ctx, node) {
950
+ const result = assignRef(
951
+ ctx,
952
+ node.i,
953
+ new Array(node.l)
954
+ );
955
+ ctx.stack.push(node.i);
956
+ deserializeNodeList(ctx, node, result);
957
+ ctx.stack.pop();
958
+ return result;
959
+ }
960
+ function deserializeProperties(ctx, node, result) {
961
+ if (node.s === 0) {
962
+ return {};
963
+ }
964
+ for (let i = 0; i < node.s; i++) {
965
+ result[node.k[i]] = deserializeTree(ctx, node.v[i]);
966
+ }
967
+ return result;
968
+ }
969
+ function deserializeNullConstructor(ctx, node) {
970
+ const result = assignRef(ctx, node.i, /* @__PURE__ */ Object.create(null));
971
+ ctx.stack.push(node.i);
972
+ deserializeProperties(ctx, node.d, result);
973
+ ctx.stack.pop();
974
+ return result;
975
+ }
976
+ function deserializeObject(ctx, node) {
977
+ const result = assignRef(ctx, node.i, {});
978
+ ctx.stack.push(node.i);
979
+ deserializeProperties(ctx, node.d, result);
980
+ ctx.stack.pop();
981
+ return result;
982
+ }
983
+ function deserializeSet(ctx, node) {
984
+ const result = assignRef(ctx, node.i, /* @__PURE__ */ new Set());
985
+ ctx.stack.push(node.i);
986
+ for (let i = 0, len = node.a.length; i < len; i++) {
987
+ result.add(deserializeTree(ctx, node.a[i]));
988
+ }
989
+ ctx.stack.pop();
990
+ return result;
991
+ }
992
+ function deserializeMap(ctx, node) {
993
+ const result = assignRef(
994
+ ctx,
995
+ node.i,
996
+ /* @__PURE__ */ new Map()
997
+ );
998
+ ctx.stack.push(node.i);
999
+ for (let i = 0; i < node.d.s; i++) {
1000
+ result.set(
1001
+ deserializeTree(ctx, node.d.k[i]),
1002
+ deserializeTree(ctx, node.d.v[i])
1003
+ );
1004
+ }
1005
+ ctx.stack.pop();
1006
+ return result;
1007
+ }
1008
+ function deserializeDictionary(ctx, node, result) {
1009
+ if (node.d) {
1010
+ ctx.stack.push(node.i);
1011
+ const fields = deserializeProperties(ctx, node.d, {});
1012
+ ctx.stack.pop();
1013
+ Object.assign(result, fields);
1014
+ }
1015
+ return result;
1016
+ }
1017
+ function deserializeAggregateError(ctx, node) {
1018
+ const result = assignRef(ctx, node.i, new AggregateError([], node.m));
1019
+ ctx.stack.push(node.i);
1020
+ result.errors = deserializeNodeList(ctx, node, new Array(node.l));
1021
+ ctx.stack.pop();
1022
+ return deserializeDictionary(ctx, node, result);
1023
+ }
1024
+ function deserializeError(ctx, node) {
1025
+ const ErrorConstructor = getErrorConstructor(node.c);
1026
+ const result = assignRef(ctx, node.i, new ErrorConstructor(node.m));
1027
+ return deserializeDictionary(ctx, node, result);
1028
+ }
1029
+ function createDeferred() {
1030
+ let resolver;
1031
+ return {
1032
+ resolve(v) {
1033
+ resolver(v);
1034
+ },
1035
+ promise: new Promise((res) => {
1036
+ resolver = res;
1037
+ })
1038
+ };
1039
+ }
1040
+ function deserializePromise(ctx, node) {
1041
+ const deferred = createDeferred();
1042
+ const result = assignRef(ctx, node.i, deferred.promise);
1043
+ deferred.resolve(deserializeTree(ctx, node.n));
1044
+ return result;
1045
+ }
1046
+ function deserializeTypedArray(ctx, node) {
1047
+ const TypedArray = getTypedArrayConstructor(node.c);
1048
+ const dummy = new TypedArray();
1049
+ const result = assignRef(ctx, node.i, new TypedArray(
1050
+ dummy.buffer,
1051
+ node.l
1052
+ ));
1053
+ for (let i = 0, len = node.s.length; i < len; i++) {
1054
+ result[i] = node.t === 23 /* BigIntTypedArray */ ? BigInt(node.s[i]) : Number(node.s[i]);
1055
+ }
1056
+ return result;
1057
+ }
1058
+ function deserializeIterable(ctx, node) {
1059
+ const values = [];
1060
+ deserializeNodeList(ctx, node, values);
1061
+ const result = assignRef(ctx, node.i, {
1062
+ [Symbol.iterator]: () => values.values()
1063
+ });
1064
+ return deserializeDictionary(ctx, node, result);
1065
+ }
1066
+ function deserializeTree(ctx, node) {
1067
+ switch (node.t) {
1068
+ case 0 /* Number */:
1069
+ case 2 /* Boolean */:
1070
+ return node.s;
1071
+ case 1 /* String */:
1072
+ return invQuote(node.s);
1073
+ case 4 /* Undefined */:
1074
+ return void 0;
1075
+ case 3 /* Null */:
1076
+ return null;
1077
+ case 5 /* NegativeZero */:
1078
+ return -0;
1079
+ case 6 /* Infinity */:
1080
+ return Infinity;
1081
+ case 7 /* NegativeInfinity */:
1082
+ return -Infinity;
1083
+ case 8 /* NaN */:
1084
+ return NaN;
1085
+ case 9 /* BigInt */:
1086
+ return BigInt(node.s);
1087
+ case 10 /* Reference */:
1088
+ return ctx.valueMap.get(node.i);
1089
+ case 15 /* Array */:
1090
+ return deserializeArray(ctx, node);
1091
+ case 16 /* Object */:
1092
+ return deserializeObject(ctx, node);
1093
+ case 17 /* NullConstructor */:
1094
+ return deserializeNullConstructor(ctx, node);
1095
+ case 11 /* Date */:
1096
+ return assignRef(ctx, node.i, new Date(node.s));
1097
+ case 12 /* RegExp */:
1098
+ return assignRef(ctx, node.i, new RegExp(node.c, node.m));
1099
+ case 13 /* Set */:
1100
+ return deserializeSet(ctx, node);
1101
+ case 14 /* Map */:
1102
+ return deserializeMap(ctx, node);
1103
+ case 23 /* BigIntTypedArray */:
1104
+ case 22 /* TypedArray */:
1105
+ return deserializeTypedArray(ctx, node);
1106
+ case 20 /* AggregateError */:
1107
+ return deserializeAggregateError(ctx, node);
1108
+ case 19 /* Error */:
1109
+ return deserializeError(ctx, node);
1110
+ case 21 /* Iterable */:
1111
+ return deserializeIterable(ctx, node);
1112
+ case 18 /* Promise */:
1113
+ return deserializePromise(ctx, node);
1114
+ case 24 /* WKSymbol */:
1115
+ return SYMBOL_REF[node.s];
1116
+ default:
1117
+ throw new Error("Unsupported type");
1118
+ }
1119
+ }
1120
+
712
1121
  // src/tree/serialize.ts
713
1122
  function getAssignmentExpression(assignment) {
714
1123
  switch (assignment.t) {
@@ -812,18 +1221,17 @@ function createObjectAssign(ctx, ref, key, value) {
812
1221
  markRef(ctx, ref);
813
1222
  createAssignment(ctx, getRefParam(ctx, ref) + "." + key, value);
814
1223
  }
815
- function assignRef(ctx, index, value) {
1224
+ function assignRef2(ctx, index, value) {
816
1225
  if (ctx.markedRefs.has(index)) {
817
1226
  return getRefParam(ctx, index) + "=" + value;
818
1227
  }
819
1228
  return value;
820
1229
  }
821
1230
  function isReferenceInStack(ctx, node) {
822
- return node.t === 2 /* Reference */ && ctx.stack.includes(node.i);
1231
+ return node.t === 10 /* Reference */ && ctx.stack.includes(node.i);
823
1232
  }
824
- var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
825
1233
  function serializeNodeList(ctx, node) {
826
- const size = node.a.length;
1234
+ const size = node.l;
827
1235
  let values = "";
828
1236
  let item;
829
1237
  let isHoley = false;
@@ -853,9 +1261,9 @@ function serializeArray(ctx, node) {
853
1261
  ctx.stack.push(node.i);
854
1262
  const result = serializeNodeList(ctx, node);
855
1263
  ctx.stack.pop();
856
- return assignRef(ctx, node.i, result);
1264
+ return assignRef2(ctx, node.i, result);
857
1265
  }
858
- function serializeObject(ctx, sourceID, node) {
1266
+ function serializeProperties(ctx, sourceID, node) {
859
1267
  if (node.s === 0) {
860
1268
  return "{}";
861
1269
  }
@@ -871,19 +1279,19 @@ function serializeObject(ctx, sourceID, node) {
871
1279
  key = node.k[i];
872
1280
  val = node.v[i];
873
1281
  check = Number(key);
874
- isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1282
+ isIdentifier = check >= 0 || isValidIdentifier(key);
875
1283
  if (isReferenceInStack(ctx, val)) {
876
1284
  refParam = getRefParam(ctx, val.i);
877
1285
  if (isIdentifier && Number.isNaN(check)) {
878
1286
  createObjectAssign(ctx, sourceID, key, refParam);
879
1287
  } else {
880
- createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
1288
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : '"' + quote(key) + '"', refParam);
881
1289
  }
882
1290
  } else {
883
1291
  if (hasPrev) {
884
1292
  result += ",";
885
1293
  }
886
- result += isIdentifier ? key : quote(key);
1294
+ result += isIdentifier ? key : '"' + quote(key) + '"';
887
1295
  result += ":" + serializeTree(ctx, val);
888
1296
  hasPrev = true;
889
1297
  }
@@ -892,7 +1300,7 @@ function serializeObject(ctx, sourceID, node) {
892
1300
  return "{" + result + "}";
893
1301
  }
894
1302
  function serializeWithObjectAssign(ctx, value, id, serialized) {
895
- const fields = serializeObject(ctx, id, value);
1303
+ const fields = serializeProperties(ctx, id, value);
896
1304
  if (fields !== "{}") {
897
1305
  return "Object.assign(" + serialized + "," + fields + ")";
898
1306
  }
@@ -916,11 +1324,11 @@ function serializeAssignments(ctx, sourceID, node) {
916
1324
  check = Number(key);
917
1325
  parentAssignment = ctx.assignments;
918
1326
  ctx.assignments = mainAssignments;
919
- isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1327
+ isIdentifier = check >= 0 || isValidIdentifier(key);
920
1328
  if (isIdentifier && Number.isNaN(check)) {
921
1329
  createObjectAssign(ctx, sourceID, key, refParam);
922
1330
  } else {
923
- createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
1331
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : '"' + quote(key) + '"', refParam);
924
1332
  }
925
1333
  ctx.assignments = parentAssignment;
926
1334
  }
@@ -936,18 +1344,21 @@ function serializeDictionary(ctx, i, d, init) {
936
1344
  const assignments = serializeAssignments(ctx, i, d);
937
1345
  if (assignments) {
938
1346
  const ref = getRefParam(ctx, i);
939
- return "(" + assignRef(ctx, i, init) + "," + assignments + ref + ")";
1347
+ return "(" + assignRef2(ctx, i, init) + "," + assignments + ref + ")";
940
1348
  }
941
1349
  }
942
1350
  }
943
- return assignRef(ctx, i, init);
1351
+ return assignRef2(ctx, i, init);
944
1352
  }
945
1353
  function serializeNullConstructor(ctx, node) {
946
1354
  return serializeDictionary(ctx, node.i, node.d, "Object.create(null)");
947
1355
  }
1356
+ function serializeObject(ctx, node) {
1357
+ return assignRef2(ctx, node.i, serializeProperties(ctx, node.i, node.d));
1358
+ }
948
1359
  function serializeSet(ctx, node) {
949
1360
  let serialized = "new Set";
950
- const size = node.a.length;
1361
+ const size = node.l;
951
1362
  if (size) {
952
1363
  let result = "";
953
1364
  ctx.stack.push(node.i);
@@ -970,7 +1381,7 @@ function serializeSet(ctx, node) {
970
1381
  serialized += "([" + result + "])";
971
1382
  }
972
1383
  }
973
- return assignRef(ctx, node.i, serialized);
1384
+ return assignRef2(ctx, node.i, serialized);
974
1385
  }
975
1386
  function serializeMap(ctx, node) {
976
1387
  let serialized = "new Map";
@@ -1016,16 +1427,16 @@ function serializeMap(ctx, node) {
1016
1427
  serialized += "([" + result + "])";
1017
1428
  }
1018
1429
  }
1019
- return assignRef(ctx, node.i, serialized);
1430
+ return assignRef2(ctx, node.i, serialized);
1020
1431
  }
1021
1432
  function serializeAggregateError(ctx, node) {
1022
1433
  ctx.stack.push(node.i);
1023
- const serialized = "new AggregateError(" + serializeTree(ctx, node.n) + "," + quote(node.m) + ")";
1434
+ const serialized = "new AggregateError(" + serializeNodeList(ctx, node) + ',"' + quote(node.m) + '")';
1024
1435
  ctx.stack.pop();
1025
1436
  return serializeDictionary(ctx, node.i, node.d, serialized);
1026
1437
  }
1027
1438
  function serializeError(ctx, node) {
1028
- const serialized = "new " + node.c + "(" + quote(node.m) + ")";
1439
+ const serialized = "new " + node.c + '("' + quote(node.m) + '")';
1029
1440
  return serializeDictionary(ctx, node.i, node.d, serialized);
1030
1441
  }
1031
1442
  function serializePromise(ctx, node) {
@@ -1043,14 +1454,24 @@ function serializePromise(ctx, node) {
1043
1454
  ctx.stack.pop();
1044
1455
  serialized = "Promise.resolve(" + result + ")";
1045
1456
  }
1046
- return assignRef(ctx, node.i, serialized);
1457
+ return assignRef2(ctx, node.i, serialized);
1047
1458
  }
1048
1459
  function serializeTypedArray(ctx, node) {
1049
- let args = "[" + node.s + "]";
1460
+ let result = "";
1461
+ for (let i = 0, len = node.s.length; i < len; i++) {
1462
+ if (i !== 0) {
1463
+ result += ",";
1464
+ }
1465
+ result += node.s[i];
1466
+ if (node.t === 23 /* BigIntTypedArray */) {
1467
+ result += "n";
1468
+ }
1469
+ }
1470
+ let args = "[" + result + "]";
1050
1471
  if (node.l !== 0) {
1051
1472
  args += "," + node.l.toString();
1052
1473
  }
1053
- return assignRef(ctx, node.i, "new " + node.c + "(" + args + ")");
1474
+ return assignRef2(ctx, node.i, "new " + node.c + "(" + args + ")");
1054
1475
  }
1055
1476
  function serializeIterable(ctx, node) {
1056
1477
  const parent = ctx.stack;
@@ -1074,37 +1495,55 @@ function serializeIterable(ctx, node) {
1074
1495
  }
1075
1496
  function serializeTree(ctx, node) {
1076
1497
  switch (node.t) {
1077
- case 0 /* Primitive */:
1498
+ case 0 /* Number */:
1078
1499
  return String(node.s);
1079
- case 1 /* BigInt */:
1080
- return node.s;
1081
- case 2 /* Reference */:
1500
+ case 1 /* String */:
1501
+ return '"' + node.s + '"';
1502
+ case 2 /* Boolean */:
1503
+ return node.s ? "!0" : "!1";
1504
+ case 4 /* Undefined */:
1505
+ return "undefined";
1506
+ case 3 /* Null */:
1507
+ return "null";
1508
+ case 5 /* NegativeZero */:
1509
+ return "-0";
1510
+ case 6 /* Infinity */:
1511
+ return "1/0";
1512
+ case 7 /* NegativeInfinity */:
1513
+ return "-1/0";
1514
+ case 8 /* NaN */:
1515
+ return "NaN";
1516
+ case 9 /* BigInt */:
1517
+ return node.s + "n";
1518
+ case 10 /* Reference */:
1082
1519
  return getRefParam(ctx, node.i);
1083
- case 7 /* Array */:
1520
+ case 15 /* Array */:
1084
1521
  return serializeArray(ctx, node);
1085
- case 8 /* Object */:
1086
- return assignRef(ctx, node.i, serializeObject(ctx, node.i, node.d));
1087
- case 9 /* NullConstructor */:
1522
+ case 16 /* Object */:
1523
+ return serializeObject(ctx, node);
1524
+ case 17 /* NullConstructor */:
1088
1525
  return serializeNullConstructor(ctx, node);
1089
- case 3 /* Date */:
1090
- return assignRef(ctx, node.i, 'new Date("' + node.s + '")');
1091
- case 4 /* RegExp */:
1092
- return assignRef(ctx, node.i, node.s);
1093
- case 5 /* Set */:
1526
+ case 11 /* Date */:
1527
+ return assignRef2(ctx, node.i, 'new Date("' + node.s + '")');
1528
+ case 12 /* RegExp */:
1529
+ return assignRef2(ctx, node.i, "/" + node.c + "/" + node.m);
1530
+ case 13 /* Set */:
1094
1531
  return serializeSet(ctx, node);
1095
- case 6 /* Map */:
1532
+ case 14 /* Map */:
1096
1533
  return serializeMap(ctx, node);
1097
- case 15 /* BigIntTypedArray */:
1098
- case 14 /* TypedArray */:
1534
+ case 23 /* BigIntTypedArray */:
1535
+ case 22 /* TypedArray */:
1099
1536
  return serializeTypedArray(ctx, node);
1100
- case 12 /* AggregateError */:
1537
+ case 20 /* AggregateError */:
1101
1538
  return serializeAggregateError(ctx, node);
1102
- case 11 /* Error */:
1539
+ case 19 /* Error */:
1103
1540
  return serializeError(ctx, node);
1104
- case 13 /* Iterable */:
1541
+ case 21 /* Iterable */:
1105
1542
  return serializeIterable(ctx, node);
1106
- case 10 /* Promise */:
1543
+ case 18 /* Promise */:
1107
1544
  return serializePromise(ctx, node);
1545
+ case 24 /* WKSymbol */:
1546
+ return SYMBOL_STRING[node.s];
1108
1547
  default:
1109
1548
  throw new Error("Unsupported type");
1110
1549
  }
@@ -1135,10 +1574,10 @@ function generateNodeList2(ctx, current) {
1135
1574
  }
1136
1575
  function generateArrayNode2(ctx, id, current) {
1137
1576
  return {
1138
- t: 7 /* Array */,
1577
+ t: 15 /* Array */,
1139
1578
  i: id,
1140
1579
  s: void 0,
1141
- l: void 0,
1580
+ l: current.length,
1142
1581
  c: void 0,
1143
1582
  m: void 0,
1144
1583
  d: void 0,
@@ -1171,7 +1610,7 @@ function generateMapNode2(ctx, id, current) {
1171
1610
  valueNodes[nodeSize + i] = parse2(ctx, deferredValue[i]);
1172
1611
  }
1173
1612
  return {
1174
- t: 6 /* Map */,
1613
+ t: 14 /* Map */,
1175
1614
  i: id,
1176
1615
  s: void 0,
1177
1616
  l: void 0,
@@ -1200,10 +1639,10 @@ function generateSetNode2(ctx, id, current) {
1200
1639
  nodes[nodeSize + i] = parse2(ctx, deferred[i]);
1201
1640
  }
1202
1641
  return {
1203
- t: 5 /* Set */,
1642
+ t: 13 /* Set */,
1204
1643
  i: id,
1205
1644
  s: void 0,
1206
- l: void 0,
1645
+ l: len,
1207
1646
  c: void 0,
1208
1647
  m: void 0,
1209
1648
  d: void 0,
@@ -1244,18 +1683,19 @@ function generateProperties2(ctx, properties) {
1244
1683
  };
1245
1684
  }
1246
1685
  function generateIterableNode2(ctx, id, current) {
1247
- assert(ctx.features & 1024 /* SymbolIterator */, 'Unsupported type "Iterable"');
1686
+ assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "Iterable"');
1248
1687
  const options = getIterableOptions(current);
1688
+ const array = Array.from(current);
1249
1689
  return {
1250
- t: 13 /* Iterable */,
1690
+ t: 21 /* Iterable */,
1251
1691
  i: id,
1252
1692
  s: void 0,
1253
- l: void 0,
1693
+ l: array.length,
1254
1694
  c: void 0,
1255
1695
  m: void 0,
1256
1696
  // Parse options first before the items
1257
1697
  d: options ? generateProperties2(ctx, options) : void 0,
1258
- a: generateNodeList2(ctx, Array.from(current)),
1698
+ a: generateNodeList2(ctx, array),
1259
1699
  n: void 0
1260
1700
  };
1261
1701
  }
@@ -1264,7 +1704,7 @@ function generateObjectNode2(ctx, id, current, empty) {
1264
1704
  return generateIterableNode2(ctx, id, current);
1265
1705
  }
1266
1706
  return {
1267
- t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
1707
+ t: empty ? 17 /* NullConstructor */ : 16 /* Object */,
1268
1708
  i: id,
1269
1709
  s: void 0,
1270
1710
  l: void 0,
@@ -1279,26 +1719,26 @@ function generateAggregateErrorNode2(ctx, id, current) {
1279
1719
  const options = getErrorOptions(ctx, current);
1280
1720
  const optionsNode = options ? generateProperties2(ctx, options) : void 0;
1281
1721
  return {
1282
- t: 12 /* AggregateError */,
1722
+ t: 20 /* AggregateError */,
1283
1723
  i: id,
1284
1724
  s: void 0,
1285
- l: void 0,
1725
+ l: current.errors.length,
1286
1726
  c: void 0,
1287
1727
  m: current.message,
1288
1728
  d: optionsNode,
1289
- a: void 0,
1290
- n: parse2(ctx, current.errors)
1729
+ a: generateNodeList2(ctx, current.errors),
1730
+ n: void 0
1291
1731
  };
1292
1732
  }
1293
1733
  function generateErrorNode2(ctx, id, current) {
1294
1734
  const options = getErrorOptions(ctx, current);
1295
1735
  const optionsNode = options ? generateProperties2(ctx, options) : void 0;
1296
1736
  return {
1297
- t: 11 /* Error */,
1737
+ t: 19 /* Error */,
1298
1738
  i: id,
1299
1739
  s: void 0,
1300
1740
  l: void 0,
1301
- c: getErrorConstructor(current),
1741
+ c: getErrorConstructorName(current),
1302
1742
  m: current.message,
1303
1743
  d: optionsNode,
1304
1744
  a: void 0,
@@ -1312,18 +1752,21 @@ function parse2(ctx, current) {
1312
1752
  case "undefined":
1313
1753
  return UNDEFINED_NODE;
1314
1754
  case "string":
1315
- return createPrimitiveNode(quote(current));
1755
+ return createStringNode(current);
1316
1756
  case "number":
1757
+ if (current !== current) {
1758
+ return NAN_NODE;
1759
+ }
1317
1760
  if (Object.is(current, -0)) {
1318
1761
  return NEG_ZERO_NODE;
1319
1762
  }
1320
1763
  if (Object.is(current, Infinity)) {
1321
- return createPrimitiveNode("1/0");
1764
+ return INFINITY_NODE;
1322
1765
  }
1323
1766
  if (Object.is(current, -Infinity)) {
1324
- return createPrimitiveNode("-1/0");
1767
+ return NEG_INFINITY_NODE;
1325
1768
  }
1326
- return createPrimitiveNode(current);
1769
+ return createNumberNode(current);
1327
1770
  case "bigint":
1328
1771
  return createBigIntNode(ctx, current);
1329
1772
  case "object": {
@@ -1390,13 +1833,17 @@ function parse2(ctx, current) {
1390
1833
  }
1391
1834
  throw new Error("Unsupported type");
1392
1835
  }
1836
+ case "symbol":
1837
+ assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "symbol"');
1838
+ assert(current in INV_SYMBOL_REF, "seroval only supports well-known symbols");
1839
+ return createWKSymbolNode(current);
1393
1840
  default:
1394
1841
  throw new Error("Unsupported type");
1395
1842
  }
1396
1843
  }
1397
1844
  function parseSync(ctx, current) {
1398
1845
  const result = parse2(ctx, current);
1399
- const isObject = result.t === 8 /* Object */ || result.t === 13 /* Iterable */;
1846
+ const isObject = result.t === 16 /* Object */ || result.t === 21 /* Iterable */;
1400
1847
  return [result, createRef(ctx, current, false), isObject];
1401
1848
  }
1402
1849
 
@@ -1472,7 +1919,11 @@ function compileJSON(source) {
1472
1919
  return finalize(serial, source.r, source.i, result);
1473
1920
  }
1474
1921
  function fromJSON(source) {
1475
- return deserialize(compileJSON(source));
1922
+ const serial = createSerializationContext({
1923
+ features: source.f,
1924
+ markedRefs: source.m
1925
+ });
1926
+ return deserializeTree(serial, source.t);
1476
1927
  }
1477
1928
  var src_default = serialize;
1478
1929
  //# sourceMappingURL=index.cjs.map