seroval 1.6.6 → 1.6.8

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/dev/index.js CHANGED
@@ -196,6 +196,7 @@ function deserializeReplacer(str) {
196
196
  }
197
197
  }
198
198
  function deserializeString(str) {
199
+ if (typeof str === "string" && !str.includes("\\")) return str;
199
200
  return str.replace(/(\\\\|\\"|\\n|\\r|\\b|\\t|\\f|\\u2028|\\u2029|\\x3C)/g, deserializeReplacer);
200
201
  }
201
202
  //#endregion
@@ -698,16 +699,26 @@ const ARRAY_BUFFER_CONSTRUCTOR = (b64) => {
698
699
  const SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR = /* @__PURE__ */ ARRAY_BUFFER_CONSTRUCTOR.toString();
699
700
  //#endregion
700
701
  //#region src/core/sequence.ts
702
+ /**
703
+ * An internal class rather than a tagged POJO: identity is checked with
704
+ * `instanceof`, which untrusted input cannot forge (the class is not exported).
705
+ * The eval-based `deserialize` path still rebuilds a `{__SEROVAL_SEQUENCE__…}`
706
+ * POJO from embedded source - it has no access to this class - so a value read
707
+ * back through `deserialize` is not an instance and, by design, is not treated
708
+ * as a genuine Sequence on re-serialization.
709
+ */
710
+ var Sequence = class {
711
+ constructor(values, throwAt, doneAt) {
712
+ this.v = values;
713
+ this.t = throwAt;
714
+ this.d = doneAt;
715
+ }
716
+ };
701
717
  function isSequence(value) {
702
- return "__SEROVAL_SEQUENCE__" in value;
718
+ return value instanceof Sequence;
703
719
  }
704
720
  function createSequence(values, throwAt, doneAt) {
705
- return {
706
- __SEROVAL_SEQUENCE__: true,
707
- v: values,
708
- t: throwAt,
709
- d: doneAt
710
- };
721
+ return new Sequence(values, throwAt, doneAt);
711
722
  }
712
723
  function createSequenceFromIterable(source) {
713
724
  const values = [];
@@ -723,7 +734,9 @@ function createSequenceFromIterable(source) {
723
734
  }
724
735
  } catch (error) {
725
736
  throwsAt = values.length;
737
+ doneAt = throwsAt;
726
738
  values.push(error);
739
+ break;
727
740
  }
728
741
  return createSequence(values, throwsAt, doneAt);
729
742
  }
@@ -755,12 +768,131 @@ const SPECIAL_REF_STRING = {
755
768
  [5]: SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR
756
769
  };
757
770
  //#endregion
771
+ //#region \0@oxc-project+runtime@0.143.0/helpers/esm/checkPrivateRedeclaration.js
772
+ function _checkPrivateRedeclaration(e, t) {
773
+ if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object");
774
+ }
775
+ //#endregion
776
+ //#region \0@oxc-project+runtime@0.143.0/helpers/esm/classPrivateMethodInitSpec.js
777
+ function _classPrivateMethodInitSpec(e, a) {
778
+ _checkPrivateRedeclaration(e, a), a.add(e);
779
+ }
780
+ //#endregion
781
+ //#region \0@oxc-project+runtime@0.143.0/helpers/esm/classPrivateFieldInitSpec.js
782
+ function _classPrivateFieldInitSpec(e, t, a) {
783
+ _checkPrivateRedeclaration(e, t), t.set(e, a);
784
+ }
785
+ //#endregion
786
+ //#region \0@oxc-project+runtime@0.143.0/helpers/esm/assertClassBrand.js
787
+ function _assertClassBrand(e, t, n) {
788
+ if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n;
789
+ throw new TypeError("Private element is not present on this object");
790
+ }
791
+ //#endregion
792
+ //#region \0@oxc-project+runtime@0.143.0/helpers/esm/classPrivateFieldGet2.js
793
+ function _classPrivateFieldGet2(s, a) {
794
+ return s.get(_assertClassBrand(s, a));
795
+ }
796
+ //#endregion
797
+ //#region \0@oxc-project+runtime@0.143.0/helpers/esm/classPrivateFieldSet2.js
798
+ function _classPrivateFieldSet2(s, a, r) {
799
+ return s.set(_assertClassBrand(s, a), r), r;
800
+ }
801
+ //#endregion
758
802
  //#region src/core/stream.ts
803
+ var _buffer = /* @__PURE__ */ new WeakMap();
804
+ var _listeners = /* @__PURE__ */ new WeakMap();
805
+ var _alive = /* @__PURE__ */ new WeakMap();
806
+ var _success = /* @__PURE__ */ new WeakMap();
807
+ var _count = /* @__PURE__ */ new WeakMap();
808
+ var _Stream_brand = /* @__PURE__ */ new WeakSet();
809
+ /**
810
+ * An internal class rather than a tagged POJO: identity is checked with
811
+ * `instanceof`, which untrusted input cannot forge (the class is not exported).
812
+ *
813
+ * The behavior is intentionally duplicated from `STREAM_CONSTRUCTOR`. That
814
+ * constructor's source is embedded verbatim into the eval-based `deserialize`
815
+ * output, which has no access to this class, so the two cannot be shared. A
816
+ * stream read back through `deserialize` is therefore a plain POJO and, by
817
+ * design, is not treated as a genuine Stream on re-serialization. Keep the two
818
+ * implementations in sync.
819
+ */
820
+ var Stream = class {
821
+ constructor() {
822
+ _classPrivateMethodInitSpec(this, _Stream_brand);
823
+ _classPrivateFieldInitSpec(this, _buffer, []);
824
+ _classPrivateFieldInitSpec(this, _listeners, []);
825
+ _classPrivateFieldInitSpec(this, _alive, true);
826
+ _classPrivateFieldInitSpec(this, _success, false);
827
+ _classPrivateFieldInitSpec(this, _count, 0);
828
+ }
829
+ on(listener) {
830
+ let subscribed = _classPrivateFieldGet2(_alive, this);
831
+ let temp = 0;
832
+ if (subscribed) {
833
+ for (; temp < _classPrivateFieldGet2(_count, this); temp++) if (!_classPrivateFieldGet2(_listeners, this)[temp]) break;
834
+ if (temp === _classPrivateFieldGet2(_count, this)) {
835
+ var _this$count;
836
+ _classPrivateFieldSet2(_count, this, (_this$count = _classPrivateFieldGet2(_count, this), _this$count++, _this$count));
837
+ }
838
+ _classPrivateFieldGet2(_listeners, this)[temp] = listener;
839
+ }
840
+ _assertClassBrand(_Stream_brand, this, _replay).call(this, listener);
841
+ return () => {
842
+ if (_classPrivateFieldGet2(_alive, this) && subscribed) {
843
+ subscribed = false;
844
+ _classPrivateFieldGet2(_listeners, this)[temp] = void 0;
845
+ while (_classPrivateFieldGet2(_count, this) > 0 && !_classPrivateFieldGet2(_listeners, this)[_classPrivateFieldGet2(_count, this) - 1]) {
846
+ var _this$count3;
847
+ _classPrivateFieldSet2(_count, this, (_this$count3 = _classPrivateFieldGet2(_count, this), _this$count3--, _this$count3));
848
+ }
849
+ _classPrivateFieldGet2(_listeners, this).length = _classPrivateFieldGet2(_count, this);
850
+ }
851
+ };
852
+ }
853
+ next(value) {
854
+ if (_classPrivateFieldGet2(_alive, this)) {
855
+ _classPrivateFieldGet2(_buffer, this).push(value);
856
+ _assertClassBrand(_Stream_brand, this, _flush).call(this, value, "next");
857
+ }
858
+ }
859
+ throw(value) {
860
+ if (_classPrivateFieldGet2(_alive, this)) {
861
+ _classPrivateFieldGet2(_buffer, this).push(value);
862
+ _assertClassBrand(_Stream_brand, this, _flush).call(this, value, "throw");
863
+ _classPrivateFieldSet2(_alive, this, false);
864
+ _classPrivateFieldSet2(_success, this, false);
865
+ _classPrivateFieldGet2(_listeners, this).length = 0;
866
+ }
867
+ }
868
+ return(value) {
869
+ if (_classPrivateFieldGet2(_alive, this)) {
870
+ _classPrivateFieldGet2(_buffer, this).push(value);
871
+ _assertClassBrand(_Stream_brand, this, _flush).call(this, value, "return");
872
+ _classPrivateFieldSet2(_alive, this, false);
873
+ _classPrivateFieldSet2(_success, this, true);
874
+ _classPrivateFieldGet2(_listeners, this).length = 0;
875
+ }
876
+ }
877
+ };
878
+ function _flush(value, mode) {
879
+ for (let x = 0; x < _classPrivateFieldGet2(_count, this); x++) {
880
+ var _classPrivateFieldGet2$1;
881
+ (_classPrivateFieldGet2$1 = _classPrivateFieldGet2(_listeners, this)[x]) === null || _classPrivateFieldGet2$1 === void 0 || _classPrivateFieldGet2$1[mode](value);
882
+ }
883
+ }
884
+ function _replay(listener) {
885
+ for (let x = 0, z = _classPrivateFieldGet2(_buffer, this).length; x < z; x++) {
886
+ const current = _classPrivateFieldGet2(_buffer, this)[x];
887
+ if (!_classPrivateFieldGet2(_alive, this) && x === z - 1) listener[_classPrivateFieldGet2(_success, this) ? "return" : "throw"](current);
888
+ else listener.next(current);
889
+ }
890
+ }
759
891
  function isStream(value) {
760
- return "__SEROVAL_STREAM__" in value;
892
+ return value instanceof Stream;
761
893
  }
762
894
  function createStream() {
763
- return STREAM_CONSTRUCTOR();
895
+ return new Stream();
764
896
  }
765
897
  function createStreamFromAsyncIterable(iterable, cleanups) {
766
898
  const stream = createStream();
@@ -1233,6 +1365,7 @@ function isValidSymbol(symbol) {
1233
1365
  //#endregion
1234
1366
  //#region src/core/context/deserializer.ts
1235
1367
  const DEFAULT_MAX_BASE64_LENGTH = 1e6;
1368
+ const MIN_NATIVE_BASE64_LENGTH = 512;
1236
1369
  const MAX_BIGINT_LENGTH = 1e4;
1237
1370
  const MAX_REGEXP_SOURCE_LENGTH = 2e4;
1238
1371
  function applyObjectFlag(obj, flag) {
@@ -1291,7 +1424,7 @@ function guardIndexedValue(ctx, id) {
1291
1424
  if (ctx.refs.has(id)) throw new Error("Conflicted ref id: " + id);
1292
1425
  }
1293
1426
  function isThennable(value) {
1294
- return !!value && typeof value === "object" && "then" in value && typeof value.then === "function";
1427
+ return !!value && (typeof value === "object" || typeof value === "function") && "then" in value && typeof value.then === "function";
1295
1428
  }
1296
1429
  function assignIndexedValueVanilla(ctx, id, value) {
1297
1430
  guardIndexedValue(ctx.base, id);
@@ -1313,8 +1446,12 @@ function deserializeKnownValue(node, record, key) {
1313
1446
  function deserializeReference(ctx, node) {
1314
1447
  return assignIndexedValue$1(ctx, node.i, getReference(deserializeString(node.s)));
1315
1448
  }
1449
+ function validateNodeList(node, list) {
1450
+ if (!Array.isArray(list)) throw new SerovalMalformedNodeError(node);
1451
+ }
1316
1452
  function deserializeArray(ctx, depth, node) {
1317
1453
  const items = node.a;
1454
+ validateNodeList(node, items);
1318
1455
  const len = items.length;
1319
1456
  const result = assignIndexedValue$1(ctx, node.i, new Array(len));
1320
1457
  for (let i = 0, item; i < len; i++) {
@@ -1356,6 +1493,8 @@ function validateNodeType(ctx, node, id, type) {
1356
1493
  }
1357
1494
  function deserializeProperties(ctx, depth, node, result) {
1358
1495
  const keys = node.k;
1496
+ validateNodeList(node, keys);
1497
+ validateNodeList(node, node.v);
1359
1498
  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]);
1360
1499
  return result;
1361
1500
  }
@@ -1410,18 +1549,29 @@ function deserializeRegExp(ctx, node) {
1410
1549
  }
1411
1550
  function deserializeSet(ctx, depth, node) {
1412
1551
  const result = assignIndexedValue$1(ctx, node.i, /* @__PURE__ */ new Set());
1552
+ validateNodeList(node, node.a);
1413
1553
  for (let i = 0, items = node.a, len = items.length; i < len; i++) result.add(deserialize$1(ctx, depth, items[i]));
1414
1554
  return result;
1415
1555
  }
1416
1556
  function deserializeMap(ctx, depth, node) {
1417
1557
  const result = assignIndexedValue$1(ctx, node.i, /* @__PURE__ */ new Map());
1558
+ validateNodeList(node, node.e.k);
1559
+ validateNodeList(node, node.e.v);
1418
1560
  for (let i = 0, keys = node.e.k, vals = node.e.v, len = keys.length; i < len; i++) result.set(deserialize$1(ctx, depth, keys[i]), deserialize$1(ctx, depth, vals[i]));
1419
1561
  return result;
1420
1562
  }
1421
1563
  function deserializeArrayBuffer(ctx, node) {
1422
1564
  if (typeof node.s !== "string") throw new SerovalMalformedNodeError(node);
1423
1565
  if (node.s.length > ctx.base.maxBase64Length) throw new RangeError("ArrayBuffer exceeds maxBase64Length (" + ctx.base.maxBase64Length + ")");
1424
- return assignIndexedValue$1(ctx, node.i, ARRAY_BUFFER_CONSTRUCTOR(deserializeString(node.s)));
1566
+ const source = deserializeString(node.s);
1567
+ let buffer;
1568
+ if (source.length < MIN_NATIVE_BASE64_LENGTH || typeof Buffer === "undefined") buffer = ARRAY_BUFFER_CONSTRUCTOR(source);
1569
+ else {
1570
+ const decoded = atob(source);
1571
+ buffer = new ArrayBuffer(decoded.length);
1572
+ Buffer.from(buffer).write(decoded, "latin1");
1573
+ }
1574
+ return assignIndexedValue$1(ctx, node.i, buffer);
1425
1575
  }
1426
1576
  function deserializeTypedArray(ctx, depth, node) {
1427
1577
  var _node$b;
@@ -1429,7 +1579,7 @@ function deserializeTypedArray(ctx, depth, node) {
1429
1579
  const source = deserialize$1(ctx, depth, node.f);
1430
1580
  if (!(source instanceof ArrayBuffer)) throw new SerovalMalformedNodeError(node);
1431
1581
  const offset = (_node$b = node.b) !== null && _node$b !== void 0 ? _node$b : 0;
1432
- if (offset < 0 || offset > source.byteLength || node.l > ctx.base.maxBase64Length) throw new SerovalMalformedNodeError(node);
1582
+ if (offset < 0 || offset > source.byteLength) throw new SerovalMalformedNodeError(node);
1433
1583
  return assignIndexedValue$1(ctx, node.i, new construct(source, offset, node.l));
1434
1584
  }
1435
1585
  function deserializeDataView(ctx, depth, node) {
@@ -1437,7 +1587,7 @@ function deserializeDataView(ctx, depth, node) {
1437
1587
  const source = deserialize$1(ctx, depth, node.f);
1438
1588
  if (!(source instanceof ArrayBuffer)) throw new SerovalMalformedNodeError(node);
1439
1589
  const offset = (_node$b2 = node.b) !== null && _node$b2 !== void 0 ? _node$b2 : 0;
1440
- if (offset < 0 || offset > source.byteLength || node.l > ctx.base.maxBase64Length) throw new SerovalMalformedNodeError(node);
1590
+ if (offset < 0 || offset > source.byteLength) throw new SerovalMalformedNodeError(node);
1441
1591
  return assignIndexedValue$1(ctx, node.i, new DataView(source, offset, node.l));
1442
1592
  }
1443
1593
  function deserializeDictionary(ctx, depth, node, result) {
@@ -1497,21 +1647,20 @@ function deserializePromiseFulfill(ctx, depth, node) {
1497
1647
  function deserializeIteratorFactoryInstance(ctx, depth, node) {
1498
1648
  deserialize$1(ctx, depth, node.a[0]);
1499
1649
  const source = deserialize$1(ctx, depth, node.a[1]);
1500
- validateNodeType(ctx, node, node.a[1].i, 35);
1501
- if (!source) throw new SerovalMalformedNodeError(node.a[1]);
1650
+ if (!isSequence(source)) throw new SerovalMalformedNodeError(node.a[1]);
1502
1651
  return sequenceToIterator(source);
1503
1652
  }
1504
1653
  function deserializeAsyncIteratorFactoryInstance(ctx, depth, node) {
1505
1654
  deserialize$1(ctx, depth, node.a[0]);
1506
1655
  const source = deserialize$1(ctx, depth, node.a[1]);
1507
- validateNodeType(ctx, node, node.a[1].i, 31);
1508
- if (!source) throw new SerovalMalformedNodeError(node.a[1]);
1656
+ if (!isStream(source)) throw new SerovalMalformedNodeError(node.a[1]);
1509
1657
  return streamToAsyncIterable(source);
1510
1658
  }
1511
1659
  function deserializeStreamConstructor(ctx, depth, node) {
1512
1660
  const result = assignIndexedValue$1(ctx, node.i, createStream());
1513
1661
  assignNodeType(ctx, node.i, 31);
1514
1662
  const items = node.a;
1663
+ validateNodeList(node, items);
1515
1664
  const len = items.length;
1516
1665
  if (len) for (let i = 0; i < len; i++) deserialize$1(ctx, depth, items[i]);
1517
1666
  return result;
@@ -1549,10 +1698,15 @@ function deserializeIteratorFactory(ctx, depth, node) {
1549
1698
  function deserializeAsyncIteratorFactory(ctx, depth, node) {
1550
1699
  deserialize$1(ctx, depth, node.a[1]);
1551
1700
  }
1701
+ function isSequenceIndex(value, size) {
1702
+ return Number.isInteger(value) && value >= -1 && value < size;
1703
+ }
1552
1704
  function deserializeSequence(ctx, depth, node) {
1705
+ validateNodeList(node, node.a);
1706
+ const size = node.a.length;
1707
+ if (!(isSequenceIndex(node.s, size) && isSequenceIndex(node.l, size))) throw new SerovalMalformedNodeError(node);
1553
1708
  const result = assignIndexedValue$1(ctx, node.i, createSequence([], node.s, node.l));
1554
- assignNodeType(ctx, node.i, 35);
1555
- for (let i = 0, len = node.a.length; i < len; i++) result.v[i] = deserialize$1(ctx, depth, node.a[i]);
1709
+ for (let i = 0; i < size; i++) result.v[i] = deserialize$1(ctx, depth, node.a[i]);
1556
1710
  return result;
1557
1711
  }
1558
1712
  function deserialize$1(ctx, depth, node) {
package/dist/index.cjs CHANGED
@@ -197,6 +197,7 @@ function deserializeReplacer(str) {
197
197
  }
198
198
  }
199
199
  function deserializeString(str) {
200
+ if (typeof str === "string" && !str.includes("\\")) return str;
200
201
  return str.replace(/(\\\\|\\"|\\n|\\r|\\b|\\t|\\f|\\u2028|\\u2029|\\x3C)/g, deserializeReplacer);
201
202
  }
202
203
  //#endregion
@@ -692,16 +693,26 @@ const ARRAY_BUFFER_CONSTRUCTOR = (b64) => {
692
693
  const SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR = /* @__PURE__ */ ARRAY_BUFFER_CONSTRUCTOR.toString();
693
694
  //#endregion
694
695
  //#region src/core/sequence.ts
696
+ /**
697
+ * An internal class rather than a tagged POJO: identity is checked with
698
+ * `instanceof`, which untrusted input cannot forge (the class is not exported).
699
+ * The eval-based `deserialize` path still rebuilds a `{__SEROVAL_SEQUENCE__…}`
700
+ * POJO from embedded source - it has no access to this class - so a value read
701
+ * back through `deserialize` is not an instance and, by design, is not treated
702
+ * as a genuine Sequence on re-serialization.
703
+ */
704
+ var Sequence = class {
705
+ constructor(values, throwAt, doneAt) {
706
+ this.v = values;
707
+ this.t = throwAt;
708
+ this.d = doneAt;
709
+ }
710
+ };
695
711
  function isSequence(value) {
696
- return "__SEROVAL_SEQUENCE__" in value;
712
+ return value instanceof Sequence;
697
713
  }
698
714
  function createSequence(values, throwAt, doneAt) {
699
- return {
700
- __SEROVAL_SEQUENCE__: true,
701
- v: values,
702
- t: throwAt,
703
- d: doneAt
704
- };
715
+ return new Sequence(values, throwAt, doneAt);
705
716
  }
706
717
  function createSequenceFromIterable(source) {
707
718
  const values = [];
@@ -717,7 +728,9 @@ function createSequenceFromIterable(source) {
717
728
  }
718
729
  } catch (error) {
719
730
  throwsAt = values.length;
731
+ doneAt = throwsAt;
720
732
  values.push(error);
733
+ break;
721
734
  }
722
735
  return createSequence(values, throwsAt, doneAt);
723
736
  }
@@ -749,12 +762,131 @@ const SPECIAL_REF_STRING = {
749
762
  [5]: SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR
750
763
  };
751
764
  //#endregion
765
+ //#region \0@oxc-project+runtime@0.143.0/helpers/esm/checkPrivateRedeclaration.js
766
+ function _checkPrivateRedeclaration(e, t) {
767
+ if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object");
768
+ }
769
+ //#endregion
770
+ //#region \0@oxc-project+runtime@0.143.0/helpers/esm/classPrivateMethodInitSpec.js
771
+ function _classPrivateMethodInitSpec(e, a) {
772
+ _checkPrivateRedeclaration(e, a), a.add(e);
773
+ }
774
+ //#endregion
775
+ //#region \0@oxc-project+runtime@0.143.0/helpers/esm/classPrivateFieldInitSpec.js
776
+ function _classPrivateFieldInitSpec(e, t, a) {
777
+ _checkPrivateRedeclaration(e, t), t.set(e, a);
778
+ }
779
+ //#endregion
780
+ //#region \0@oxc-project+runtime@0.143.0/helpers/esm/assertClassBrand.js
781
+ function _assertClassBrand(e, t, n) {
782
+ if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n;
783
+ throw new TypeError("Private element is not present on this object");
784
+ }
785
+ //#endregion
786
+ //#region \0@oxc-project+runtime@0.143.0/helpers/esm/classPrivateFieldGet2.js
787
+ function _classPrivateFieldGet2(s, a) {
788
+ return s.get(_assertClassBrand(s, a));
789
+ }
790
+ //#endregion
791
+ //#region \0@oxc-project+runtime@0.143.0/helpers/esm/classPrivateFieldSet2.js
792
+ function _classPrivateFieldSet2(s, a, r) {
793
+ return s.set(_assertClassBrand(s, a), r), r;
794
+ }
795
+ //#endregion
752
796
  //#region src/core/stream.ts
797
+ var _buffer = /* @__PURE__ */ new WeakMap();
798
+ var _listeners = /* @__PURE__ */ new WeakMap();
799
+ var _alive = /* @__PURE__ */ new WeakMap();
800
+ var _success = /* @__PURE__ */ new WeakMap();
801
+ var _count = /* @__PURE__ */ new WeakMap();
802
+ var _Stream_brand = /* @__PURE__ */ new WeakSet();
803
+ /**
804
+ * An internal class rather than a tagged POJO: identity is checked with
805
+ * `instanceof`, which untrusted input cannot forge (the class is not exported).
806
+ *
807
+ * The behavior is intentionally duplicated from `STREAM_CONSTRUCTOR`. That
808
+ * constructor's source is embedded verbatim into the eval-based `deserialize`
809
+ * output, which has no access to this class, so the two cannot be shared. A
810
+ * stream read back through `deserialize` is therefore a plain POJO and, by
811
+ * design, is not treated as a genuine Stream on re-serialization. Keep the two
812
+ * implementations in sync.
813
+ */
814
+ var Stream = class {
815
+ constructor() {
816
+ _classPrivateMethodInitSpec(this, _Stream_brand);
817
+ _classPrivateFieldInitSpec(this, _buffer, []);
818
+ _classPrivateFieldInitSpec(this, _listeners, []);
819
+ _classPrivateFieldInitSpec(this, _alive, true);
820
+ _classPrivateFieldInitSpec(this, _success, false);
821
+ _classPrivateFieldInitSpec(this, _count, 0);
822
+ }
823
+ on(listener) {
824
+ let subscribed = _classPrivateFieldGet2(_alive, this);
825
+ let temp = 0;
826
+ if (subscribed) {
827
+ for (; temp < _classPrivateFieldGet2(_count, this); temp++) if (!_classPrivateFieldGet2(_listeners, this)[temp]) break;
828
+ if (temp === _classPrivateFieldGet2(_count, this)) {
829
+ var _this$count;
830
+ _classPrivateFieldSet2(_count, this, (_this$count = _classPrivateFieldGet2(_count, this), _this$count++, _this$count));
831
+ }
832
+ _classPrivateFieldGet2(_listeners, this)[temp] = listener;
833
+ }
834
+ _assertClassBrand(_Stream_brand, this, _replay).call(this, listener);
835
+ return () => {
836
+ if (_classPrivateFieldGet2(_alive, this) && subscribed) {
837
+ subscribed = false;
838
+ _classPrivateFieldGet2(_listeners, this)[temp] = void 0;
839
+ while (_classPrivateFieldGet2(_count, this) > 0 && !_classPrivateFieldGet2(_listeners, this)[_classPrivateFieldGet2(_count, this) - 1]) {
840
+ var _this$count3;
841
+ _classPrivateFieldSet2(_count, this, (_this$count3 = _classPrivateFieldGet2(_count, this), _this$count3--, _this$count3));
842
+ }
843
+ _classPrivateFieldGet2(_listeners, this).length = _classPrivateFieldGet2(_count, this);
844
+ }
845
+ };
846
+ }
847
+ next(value) {
848
+ if (_classPrivateFieldGet2(_alive, this)) {
849
+ _classPrivateFieldGet2(_buffer, this).push(value);
850
+ _assertClassBrand(_Stream_brand, this, _flush).call(this, value, "next");
851
+ }
852
+ }
853
+ throw(value) {
854
+ if (_classPrivateFieldGet2(_alive, this)) {
855
+ _classPrivateFieldGet2(_buffer, this).push(value);
856
+ _assertClassBrand(_Stream_brand, this, _flush).call(this, value, "throw");
857
+ _classPrivateFieldSet2(_alive, this, false);
858
+ _classPrivateFieldSet2(_success, this, false);
859
+ _classPrivateFieldGet2(_listeners, this).length = 0;
860
+ }
861
+ }
862
+ return(value) {
863
+ if (_classPrivateFieldGet2(_alive, this)) {
864
+ _classPrivateFieldGet2(_buffer, this).push(value);
865
+ _assertClassBrand(_Stream_brand, this, _flush).call(this, value, "return");
866
+ _classPrivateFieldSet2(_alive, this, false);
867
+ _classPrivateFieldSet2(_success, this, true);
868
+ _classPrivateFieldGet2(_listeners, this).length = 0;
869
+ }
870
+ }
871
+ };
872
+ function _flush(value, mode) {
873
+ for (let x = 0; x < _classPrivateFieldGet2(_count, this); x++) {
874
+ var _classPrivateFieldGet2$1;
875
+ (_classPrivateFieldGet2$1 = _classPrivateFieldGet2(_listeners, this)[x]) === null || _classPrivateFieldGet2$1 === void 0 || _classPrivateFieldGet2$1[mode](value);
876
+ }
877
+ }
878
+ function _replay(listener) {
879
+ for (let x = 0, z = _classPrivateFieldGet2(_buffer, this).length; x < z; x++) {
880
+ const current = _classPrivateFieldGet2(_buffer, this)[x];
881
+ if (!_classPrivateFieldGet2(_alive, this) && x === z - 1) listener[_classPrivateFieldGet2(_success, this) ? "return" : "throw"](current);
882
+ else listener.next(current);
883
+ }
884
+ }
753
885
  function isStream(value) {
754
- return "__SEROVAL_STREAM__" in value;
886
+ return value instanceof Stream;
755
887
  }
756
888
  function createStream() {
757
- return STREAM_CONSTRUCTOR();
889
+ return new Stream();
758
890
  }
759
891
  function createStreamFromAsyncIterable(iterable, cleanups) {
760
892
  const stream = createStream();
@@ -1227,6 +1359,7 @@ function isValidSymbol(symbol) {
1227
1359
  //#endregion
1228
1360
  //#region src/core/context/deserializer.ts
1229
1361
  const DEFAULT_MAX_BASE64_LENGTH = 1e6;
1362
+ const MIN_NATIVE_BASE64_LENGTH = 512;
1230
1363
  const MAX_BIGINT_LENGTH = 1e4;
1231
1364
  const MAX_REGEXP_SOURCE_LENGTH = 2e4;
1232
1365
  function applyObjectFlag(obj, flag) {
@@ -1285,7 +1418,7 @@ function guardIndexedValue(ctx, id) {
1285
1418
  if (ctx.refs.has(id)) throw new Error("Conflicted ref id: " + id);
1286
1419
  }
1287
1420
  function isThennable(value) {
1288
- return !!value && typeof value === "object" && "then" in value && typeof value.then === "function";
1421
+ return !!value && (typeof value === "object" || typeof value === "function") && "then" in value && typeof value.then === "function";
1289
1422
  }
1290
1423
  function assignIndexedValueVanilla(ctx, id, value) {
1291
1424
  guardIndexedValue(ctx.base, id);
@@ -1307,8 +1440,12 @@ function deserializeKnownValue(node, record, key) {
1307
1440
  function deserializeReference(ctx, node) {
1308
1441
  return assignIndexedValue$1(ctx, node.i, getReference(deserializeString(node.s)));
1309
1442
  }
1443
+ function validateNodeList(node, list) {
1444
+ if (!Array.isArray(list)) throw new SerovalMalformedNodeError(node);
1445
+ }
1310
1446
  function deserializeArray(ctx, depth, node) {
1311
1447
  const items = node.a;
1448
+ validateNodeList(node, items);
1312
1449
  const len = items.length;
1313
1450
  const result = assignIndexedValue$1(ctx, node.i, new Array(len));
1314
1451
  for (let i = 0, item; i < len; i++) {
@@ -1350,6 +1487,8 @@ function validateNodeType(ctx, node, id, type) {
1350
1487
  }
1351
1488
  function deserializeProperties(ctx, depth, node, result) {
1352
1489
  const keys = node.k;
1490
+ validateNodeList(node, keys);
1491
+ validateNodeList(node, node.v);
1353
1492
  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]);
1354
1493
  return result;
1355
1494
  }
@@ -1404,18 +1543,29 @@ function deserializeRegExp(ctx, node) {
1404
1543
  }
1405
1544
  function deserializeSet(ctx, depth, node) {
1406
1545
  const result = assignIndexedValue$1(ctx, node.i, /* @__PURE__ */ new Set());
1546
+ validateNodeList(node, node.a);
1407
1547
  for (let i = 0, items = node.a, len = items.length; i < len; i++) result.add(deserialize$1(ctx, depth, items[i]));
1408
1548
  return result;
1409
1549
  }
1410
1550
  function deserializeMap(ctx, depth, node) {
1411
1551
  const result = assignIndexedValue$1(ctx, node.i, /* @__PURE__ */ new Map());
1552
+ validateNodeList(node, node.e.k);
1553
+ validateNodeList(node, node.e.v);
1412
1554
  for (let i = 0, keys = node.e.k, vals = node.e.v, len = keys.length; i < len; i++) result.set(deserialize$1(ctx, depth, keys[i]), deserialize$1(ctx, depth, vals[i]));
1413
1555
  return result;
1414
1556
  }
1415
1557
  function deserializeArrayBuffer(ctx, node) {
1416
1558
  if (typeof node.s !== "string") throw new SerovalMalformedNodeError(node);
1417
1559
  if (node.s.length > ctx.base.maxBase64Length) throw new RangeError("ArrayBuffer exceeds maxBase64Length (" + ctx.base.maxBase64Length + ")");
1418
- return assignIndexedValue$1(ctx, node.i, ARRAY_BUFFER_CONSTRUCTOR(deserializeString(node.s)));
1560
+ const source = deserializeString(node.s);
1561
+ let buffer;
1562
+ if (source.length < MIN_NATIVE_BASE64_LENGTH || typeof Buffer === "undefined") buffer = ARRAY_BUFFER_CONSTRUCTOR(source);
1563
+ else {
1564
+ const decoded = atob(source);
1565
+ buffer = new ArrayBuffer(decoded.length);
1566
+ Buffer.from(buffer).write(decoded, "latin1");
1567
+ }
1568
+ return assignIndexedValue$1(ctx, node.i, buffer);
1419
1569
  }
1420
1570
  function deserializeTypedArray(ctx, depth, node) {
1421
1571
  var _node$b;
@@ -1423,7 +1573,7 @@ function deserializeTypedArray(ctx, depth, node) {
1423
1573
  const source = deserialize$1(ctx, depth, node.f);
1424
1574
  if (!(source instanceof ArrayBuffer)) throw new SerovalMalformedNodeError(node);
1425
1575
  const offset = (_node$b = node.b) !== null && _node$b !== void 0 ? _node$b : 0;
1426
- if (offset < 0 || offset > source.byteLength || node.l > ctx.base.maxBase64Length) throw new SerovalMalformedNodeError(node);
1576
+ if (offset < 0 || offset > source.byteLength) throw new SerovalMalformedNodeError(node);
1427
1577
  return assignIndexedValue$1(ctx, node.i, new construct(source, offset, node.l));
1428
1578
  }
1429
1579
  function deserializeDataView(ctx, depth, node) {
@@ -1431,7 +1581,7 @@ function deserializeDataView(ctx, depth, node) {
1431
1581
  const source = deserialize$1(ctx, depth, node.f);
1432
1582
  if (!(source instanceof ArrayBuffer)) throw new SerovalMalformedNodeError(node);
1433
1583
  const offset = (_node$b2 = node.b) !== null && _node$b2 !== void 0 ? _node$b2 : 0;
1434
- if (offset < 0 || offset > source.byteLength || node.l > ctx.base.maxBase64Length) throw new SerovalMalformedNodeError(node);
1584
+ if (offset < 0 || offset > source.byteLength) throw new SerovalMalformedNodeError(node);
1435
1585
  return assignIndexedValue$1(ctx, node.i, new DataView(source, offset, node.l));
1436
1586
  }
1437
1587
  function deserializeDictionary(ctx, depth, node, result) {
@@ -1491,21 +1641,20 @@ function deserializePromiseFulfill(ctx, depth, node) {
1491
1641
  function deserializeIteratorFactoryInstance(ctx, depth, node) {
1492
1642
  deserialize$1(ctx, depth, node.a[0]);
1493
1643
  const source = deserialize$1(ctx, depth, node.a[1]);
1494
- validateNodeType(ctx, node, node.a[1].i, 35);
1495
- if (!source) throw new SerovalMalformedNodeError(node.a[1]);
1644
+ if (!isSequence(source)) throw new SerovalMalformedNodeError(node.a[1]);
1496
1645
  return sequenceToIterator(source);
1497
1646
  }
1498
1647
  function deserializeAsyncIteratorFactoryInstance(ctx, depth, node) {
1499
1648
  deserialize$1(ctx, depth, node.a[0]);
1500
1649
  const source = deserialize$1(ctx, depth, node.a[1]);
1501
- validateNodeType(ctx, node, node.a[1].i, 31);
1502
- if (!source) throw new SerovalMalformedNodeError(node.a[1]);
1650
+ if (!isStream(source)) throw new SerovalMalformedNodeError(node.a[1]);
1503
1651
  return streamToAsyncIterable(source);
1504
1652
  }
1505
1653
  function deserializeStreamConstructor(ctx, depth, node) {
1506
1654
  const result = assignIndexedValue$1(ctx, node.i, createStream());
1507
1655
  assignNodeType(ctx, node.i, 31);
1508
1656
  const items = node.a;
1657
+ validateNodeList(node, items);
1509
1658
  const len = items.length;
1510
1659
  if (len) for (let i = 0; i < len; i++) deserialize$1(ctx, depth, items[i]);
1511
1660
  return result;
@@ -1543,10 +1692,15 @@ function deserializeIteratorFactory(ctx, depth, node) {
1543
1692
  function deserializeAsyncIteratorFactory(ctx, depth, node) {
1544
1693
  deserialize$1(ctx, depth, node.a[1]);
1545
1694
  }
1695
+ function isSequenceIndex(value, size) {
1696
+ return Number.isInteger(value) && value >= -1 && value < size;
1697
+ }
1546
1698
  function deserializeSequence(ctx, depth, node) {
1699
+ validateNodeList(node, node.a);
1700
+ const size = node.a.length;
1701
+ if (!(isSequenceIndex(node.s, size) && isSequenceIndex(node.l, size))) throw new SerovalMalformedNodeError(node);
1547
1702
  const result = assignIndexedValue$1(ctx, node.i, createSequence([], node.s, node.l));
1548
- assignNodeType(ctx, node.i, 35);
1549
- for (let i = 0, len = node.a.length; i < len; i++) result.v[i] = deserialize$1(ctx, depth, node.a[i]);
1703
+ for (let i = 0; i < size; i++) result.v[i] = deserialize$1(ctx, depth, node.a[i]);
1550
1704
  return result;
1551
1705
  }
1552
1706
  function deserialize$1(ctx, depth, node) {
package/dist/index.d.cts CHANGED
@@ -737,14 +737,25 @@ interface StreamListener<T> {
737
737
  throw(value: unknown): void;
738
738
  return(value: T): void;
739
739
  }
740
- interface Stream<T> {
741
- __SEROVAL_STREAM__: true;
740
+ /**
741
+ * An internal class rather than a tagged POJO: identity is checked with
742
+ * `instanceof`, which untrusted input cannot forge (the class is not exported).
743
+ *
744
+ * The behavior is intentionally duplicated from `STREAM_CONSTRUCTOR`. That
745
+ * constructor's source is embedded verbatim into the eval-based `deserialize`
746
+ * output, which has no access to this class, so the two cannot be shared. A
747
+ * stream read back through `deserialize` is therefore a plain POJO and, by
748
+ * design, is not treated as a genuine Stream on re-serialization. Keep the two
749
+ * implementations in sync.
750
+ */
751
+ declare class Stream<T> {
752
+ #private;
742
753
  on(listener: StreamListener<T>): () => void;
743
754
  next(value: T): void;
744
755
  throw(value: unknown): void;
745
756
  return(value: T): void;
746
757
  }
747
- declare function isStream<T>(value: object): value is Stream<T>;
758
+ declare function isStream<T>(value: unknown): value is Stream<T>;
748
759
  declare function createStream<T>(): Stream<T>;
749
760
  //#endregion
750
761
  //#region src/core/tree/index.d.ts