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/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
@@ -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
@@ -691,16 +692,26 @@ const ARRAY_BUFFER_CONSTRUCTOR = (b64) => {
691
692
  const SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR = /* @__PURE__ */ ARRAY_BUFFER_CONSTRUCTOR.toString();
692
693
  //#endregion
693
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
+ };
694
710
  function isSequence(value) {
695
- return "__SEROVAL_SEQUENCE__" in value;
711
+ return value instanceof Sequence;
696
712
  }
697
713
  function createSequence(values, throwAt, doneAt) {
698
- return {
699
- __SEROVAL_SEQUENCE__: true,
700
- v: values,
701
- t: throwAt,
702
- d: doneAt
703
- };
714
+ return new Sequence(values, throwAt, doneAt);
704
715
  }
705
716
  function createSequenceFromIterable(source) {
706
717
  const values = [];
@@ -716,7 +727,9 @@ function createSequenceFromIterable(source) {
716
727
  }
717
728
  } catch (error) {
718
729
  throwsAt = values.length;
730
+ doneAt = throwsAt;
719
731
  values.push(error);
732
+ break;
720
733
  }
721
734
  return createSequence(values, throwsAt, doneAt);
722
735
  }
@@ -748,12 +761,131 @@ const SPECIAL_REF_STRING = {
748
761
  [5]: SERIALIZED_ARRAY_BUFFER_CONSTRUCTOR
749
762
  };
750
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
751
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
+ }
752
884
  function isStream(value) {
753
- return "__SEROVAL_STREAM__" in value;
885
+ return value instanceof Stream;
754
886
  }
755
887
  function createStream() {
756
- return STREAM_CONSTRUCTOR();
888
+ return new Stream();
757
889
  }
758
890
  function createStreamFromAsyncIterable(iterable, cleanups) {
759
891
  const stream = createStream();
@@ -1226,6 +1358,7 @@ function isValidSymbol(symbol) {
1226
1358
  //#endregion
1227
1359
  //#region src/core/context/deserializer.ts
1228
1360
  const DEFAULT_MAX_BASE64_LENGTH = 1e6;
1361
+ const MIN_NATIVE_BASE64_LENGTH = 512;
1229
1362
  const MAX_BIGINT_LENGTH = 1e4;
1230
1363
  const MAX_REGEXP_SOURCE_LENGTH = 2e4;
1231
1364
  function applyObjectFlag(obj, flag) {
@@ -1284,7 +1417,7 @@ function guardIndexedValue(ctx, id) {
1284
1417
  if (ctx.refs.has(id)) throw new Error("Conflicted ref id: " + id);
1285
1418
  }
1286
1419
  function isThennable(value) {
1287
- 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";
1288
1421
  }
1289
1422
  function assignIndexedValueVanilla(ctx, id, value) {
1290
1423
  guardIndexedValue(ctx.base, id);
@@ -1306,8 +1439,12 @@ function deserializeKnownValue(node, record, key) {
1306
1439
  function deserializeReference(ctx, node) {
1307
1440
  return assignIndexedValue$1(ctx, node.i, getReference(deserializeString(node.s)));
1308
1441
  }
1442
+ function validateNodeList(node, list) {
1443
+ if (!Array.isArray(list)) throw new SerovalMalformedNodeError(node);
1444
+ }
1309
1445
  function deserializeArray(ctx, depth, node) {
1310
1446
  const items = node.a;
1447
+ validateNodeList(node, items);
1311
1448
  const len = items.length;
1312
1449
  const result = assignIndexedValue$1(ctx, node.i, new Array(len));
1313
1450
  for (let i = 0, item; i < len; i++) {
@@ -1349,6 +1486,8 @@ function validateNodeType(ctx, node, id, type) {
1349
1486
  }
1350
1487
  function deserializeProperties(ctx, depth, node, result) {
1351
1488
  const keys = node.k;
1489
+ validateNodeList(node, keys);
1490
+ validateNodeList(node, node.v);
1352
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]);
1353
1492
  return result;
1354
1493
  }
@@ -1403,18 +1542,29 @@ function deserializeRegExp(ctx, node) {
1403
1542
  }
1404
1543
  function deserializeSet(ctx, depth, node) {
1405
1544
  const result = assignIndexedValue$1(ctx, node.i, /* @__PURE__ */ new Set());
1545
+ validateNodeList(node, node.a);
1406
1546
  for (let i = 0, items = node.a, len = items.length; i < len; i++) result.add(deserialize$1(ctx, depth, items[i]));
1407
1547
  return result;
1408
1548
  }
1409
1549
  function deserializeMap(ctx, depth, node) {
1410
1550
  const result = assignIndexedValue$1(ctx, node.i, /* @__PURE__ */ new Map());
1551
+ validateNodeList(node, node.e.k);
1552
+ validateNodeList(node, node.e.v);
1411
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]));
1412
1554
  return result;
1413
1555
  }
1414
1556
  function deserializeArrayBuffer(ctx, node) {
1415
1557
  if (typeof node.s !== "string") throw new SerovalMalformedNodeError(node);
1416
1558
  if (node.s.length > ctx.base.maxBase64Length) throw new RangeError("ArrayBuffer exceeds maxBase64Length (" + ctx.base.maxBase64Length + ")");
1417
- return assignIndexedValue$1(ctx, node.i, ARRAY_BUFFER_CONSTRUCTOR(deserializeString(node.s)));
1559
+ const source = deserializeString(node.s);
1560
+ let buffer;
1561
+ if (source.length < MIN_NATIVE_BASE64_LENGTH || typeof Buffer === "undefined") buffer = ARRAY_BUFFER_CONSTRUCTOR(source);
1562
+ else {
1563
+ const decoded = atob(source);
1564
+ buffer = new ArrayBuffer(decoded.length);
1565
+ Buffer.from(buffer).write(decoded, "latin1");
1566
+ }
1567
+ return assignIndexedValue$1(ctx, node.i, buffer);
1418
1568
  }
1419
1569
  function deserializeTypedArray(ctx, depth, node) {
1420
1570
  var _node$b;
@@ -1422,7 +1572,7 @@ function deserializeTypedArray(ctx, depth, node) {
1422
1572
  const source = deserialize$1(ctx, depth, node.f);
1423
1573
  if (!(source instanceof ArrayBuffer)) throw new SerovalMalformedNodeError(node);
1424
1574
  const offset = (_node$b = node.b) !== null && _node$b !== void 0 ? _node$b : 0;
1425
- if (offset < 0 || offset > source.byteLength || node.l > ctx.base.maxBase64Length) throw new SerovalMalformedNodeError(node);
1575
+ if (offset < 0 || offset > source.byteLength) throw new SerovalMalformedNodeError(node);
1426
1576
  return assignIndexedValue$1(ctx, node.i, new construct(source, offset, node.l));
1427
1577
  }
1428
1578
  function deserializeDataView(ctx, depth, node) {
@@ -1430,7 +1580,7 @@ function deserializeDataView(ctx, depth, node) {
1430
1580
  const source = deserialize$1(ctx, depth, node.f);
1431
1581
  if (!(source instanceof ArrayBuffer)) throw new SerovalMalformedNodeError(node);
1432
1582
  const offset = (_node$b2 = node.b) !== null && _node$b2 !== void 0 ? _node$b2 : 0;
1433
- if (offset < 0 || offset > source.byteLength || node.l > ctx.base.maxBase64Length) throw new SerovalMalformedNodeError(node);
1583
+ if (offset < 0 || offset > source.byteLength) throw new SerovalMalformedNodeError(node);
1434
1584
  return assignIndexedValue$1(ctx, node.i, new DataView(source, offset, node.l));
1435
1585
  }
1436
1586
  function deserializeDictionary(ctx, depth, node, result) {
@@ -1490,21 +1640,20 @@ function deserializePromiseFulfill(ctx, depth, node) {
1490
1640
  function deserializeIteratorFactoryInstance(ctx, depth, node) {
1491
1641
  deserialize$1(ctx, depth, node.a[0]);
1492
1642
  const source = deserialize$1(ctx, depth, node.a[1]);
1493
- validateNodeType(ctx, node, node.a[1].i, 35);
1494
- if (!source) throw new SerovalMalformedNodeError(node.a[1]);
1643
+ if (!isSequence(source)) throw new SerovalMalformedNodeError(node.a[1]);
1495
1644
  return sequenceToIterator(source);
1496
1645
  }
1497
1646
  function deserializeAsyncIteratorFactoryInstance(ctx, depth, node) {
1498
1647
  deserialize$1(ctx, depth, node.a[0]);
1499
1648
  const source = deserialize$1(ctx, depth, node.a[1]);
1500
- validateNodeType(ctx, node, node.a[1].i, 31);
1501
- if (!source) throw new SerovalMalformedNodeError(node.a[1]);
1649
+ if (!isStream(source)) throw new SerovalMalformedNodeError(node.a[1]);
1502
1650
  return streamToAsyncIterable(source);
1503
1651
  }
1504
1652
  function deserializeStreamConstructor(ctx, depth, node) {
1505
1653
  const result = assignIndexedValue$1(ctx, node.i, createStream());
1506
1654
  assignNodeType(ctx, node.i, 31);
1507
1655
  const items = node.a;
1656
+ validateNodeList(node, items);
1508
1657
  const len = items.length;
1509
1658
  if (len) for (let i = 0; i < len; i++) deserialize$1(ctx, depth, items[i]);
1510
1659
  return result;
@@ -1542,10 +1691,15 @@ function deserializeIteratorFactory(ctx, depth, node) {
1542
1691
  function deserializeAsyncIteratorFactory(ctx, depth, node) {
1543
1692
  deserialize$1(ctx, depth, node.a[1]);
1544
1693
  }
1694
+ function isSequenceIndex(value, size) {
1695
+ return Number.isInteger(value) && value >= -1 && value < size;
1696
+ }
1545
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);
1546
1701
  const result = assignIndexedValue$1(ctx, node.i, createSequence([], node.s, node.l));
1547
- assignNodeType(ctx, node.i, 35);
1548
- 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]);
1549
1703
  return result;
1550
1704
  }
1551
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.6",
4
+ "version": "1.6.8",
5
5
  "files": [
6
6
  "dist"
7
7
  ],