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