seroval 1.6.5 → 1.6.6

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
@@ -147,6 +147,8 @@ const NEG_INFINITY_NODE = /* @__PURE__ */ createConstantNode(6);
147
147
  const NAN_NODE = /* @__PURE__ */ createConstantNode(7);
148
148
  //#endregion
149
149
  //#region src/core/string.ts
150
+ const MIN_JSON_STRINGIFY_LENGTH = 64;
151
+ const JSON_ESCAPE_DIFFERENCES = /[\x00-\x07\x0b\x0e-\x1f<\u2028\u2029\ud800-\udfff]/;
150
152
  function serializeChar(str) {
151
153
  switch (str) {
152
154
  case "\"": return "\\\"";
@@ -163,6 +165,7 @@ function serializeChar(str) {
163
165
  }
164
166
  }
165
167
  function serializeString(str) {
168
+ if (str.length >= MIN_JSON_STRINGIFY_LENGTH && !JSON_ESCAPE_DIFFERENCES.test(str)) return JSON.stringify(str).slice(1, -1);
166
169
  let result = "";
167
170
  let lastPos = 0;
168
171
  let replacement;
@@ -523,7 +526,10 @@ const STREAM_CONSTRUCTOR = () => {
523
526
  let count = 0;
524
527
  const internal = {
525
528
  flush(value, mode, x) {
526
- for (x = 0; x < count; x++) if (listeners[x]) listeners[x][mode](value);
529
+ for (x = 0; x < count; x++) {
530
+ const listener = listeners[x];
531
+ if (listener) listener[mode](value);
532
+ }
527
533
  },
528
534
  up(listener, x, z, current) {
529
535
  for (x = 0, z = buffer.length; x < z; x++) {
@@ -532,16 +538,20 @@ const STREAM_CONSTRUCTOR = () => {
532
538
  else listener.next(current);
533
539
  }
534
540
  },
535
- on(listener, temp) {
541
+ on(listener, temp = 0) {
542
+ let subscribed = alive;
536
543
  if (alive) {
537
- temp = count++;
544
+ for (temp = 0; temp < count; temp++) if (!listeners[temp]) break;
545
+ if (temp === count) count++;
538
546
  listeners[temp] = listener;
539
547
  }
540
548
  internal.up(listener);
541
549
  return () => {
542
- if (alive) {
543
- listeners[temp] = listeners[count];
544
- listeners[count--] = void 0;
550
+ if (alive && subscribed) {
551
+ subscribed = false;
552
+ listeners[temp] = void 0;
553
+ while (count > 0 && !listeners[count - 1]) count--;
554
+ listeners.length = count;
545
555
  }
546
556
  };
547
557
  }
@@ -752,19 +762,35 @@ function isStream(value) {
752
762
  function createStream() {
753
763
  return STREAM_CONSTRUCTOR();
754
764
  }
755
- function createStreamFromAsyncIterable(iterable) {
765
+ function createStreamFromAsyncIterable(iterable, cleanups) {
756
766
  const stream = createStream();
757
767
  const iterator = iterable[SYM_ASYNC_ITERATOR]();
768
+ let cancelled = false;
769
+ let done = false;
770
+ cleanups === null || cleanups === void 0 || cleanups.push(() => {
771
+ if (!(done || cancelled)) {
772
+ cancelled = true;
773
+ Promise.resolve().then(() => {
774
+ var _iterator$return;
775
+ return (_iterator$return = iterator.return) === null || _iterator$return === void 0 ? void 0 : _iterator$return.call(iterator);
776
+ }).catch(() => {});
777
+ }
778
+ });
758
779
  async function push() {
759
780
  try {
760
- const value = await iterator.next();
761
- if (value.done) stream.return(value.value);
762
- else {
781
+ while (!cancelled) {
782
+ const value = await iterator.next();
783
+ if (cancelled) return;
784
+ if (value.done) {
785
+ done = true;
786
+ stream.return(value.value);
787
+ break;
788
+ }
763
789
  stream.next(value.value);
764
- await push();
765
790
  }
766
791
  } catch (error) {
767
- stream.throw(error);
792
+ done = true;
793
+ if (!cancelled) stream.throw(error);
768
794
  }
769
795
  }
770
796
  push().catch(() => {});
@@ -786,13 +812,15 @@ async function promiseToResult(current) {
786
812
  //#endregion
787
813
  //#region src/core/context/parser.ts
788
814
  function createBaseParserContext(mode, options) {
815
+ var _options$compactArray;
789
816
  return {
790
817
  plugins: options.plugins,
791
818
  mode,
792
819
  marked: /* @__PURE__ */ new Set(),
793
820
  features: 127 ^ (options.disabledFeatures || 0),
794
821
  refs: options.refs || /* @__PURE__ */ new Map(),
795
- depthLimit: options.depthLimit || 1e3
822
+ depthLimit: options.depthLimit || 1e3,
823
+ compactArrayBufferViews: (_options$compactArray = options.compactArrayBufferViews) !== null && _options$compactArray !== void 0 ? _options$compactArray : false
796
824
  };
797
825
  }
798
826
  /**
@@ -872,11 +900,22 @@ function createMapNode(ctx, id, k, v) {
872
900
  function createPromiseConstructorNode(ctx, id, resolver) {
873
901
  return createSerovalNode(22, id, resolver, void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 1), void 0, void 0, void 0);
874
902
  }
875
- function createArrayBufferNode(ctx, id, current) {
903
+ function getArrayBufferView(ctx, current) {
904
+ if (!ctx.compactArrayBufferViews) return current;
905
+ const buffer = new Uint8Array(current.buffer, current.byteOffset, current.byteLength).slice().buffer;
906
+ const Constructor = current.constructor;
907
+ return new Constructor(buffer);
908
+ }
909
+ function encodeArrayBuffer(current) {
910
+ if (typeof Buffer !== "undefined") return Buffer.from(current).toString("base64");
876
911
  const bytes = new Uint8Array(current);
912
+ if (typeof bytes.toBase64 === "function") return bytes.toBase64();
877
913
  let result = "";
878
914
  for (let i = 0, len = bytes.length; i < len; i++) result += String.fromCharCode(bytes[i]);
879
- return createSerovalNode(19, id, serializeString(btoa(result)), void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 5), void 0, void 0, void 0);
915
+ return btoa(result);
916
+ }
917
+ function createArrayBufferNode(ctx, id, current) {
918
+ return createSerovalNode(19, id, encodeArrayBuffer(current), void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 5), void 0, void 0, void 0);
880
919
  }
881
920
  //#endregion
882
921
  //#region src/core/context/async-parser.ts
@@ -940,12 +979,15 @@ async function parseBoxed$1(ctx, depth, id, current) {
940
979
  return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
941
980
  }
942
981
  async function parseTypedArray$1(ctx, depth, id, current) {
982
+ current = getArrayBufferView(ctx.base, current);
943
983
  return createTypedArrayNode(id, current, await parseAsync(ctx, depth, current.buffer));
944
984
  }
945
985
  async function parseBigIntTypedArray$1(ctx, depth, id, current) {
986
+ current = getArrayBufferView(ctx.base, current);
946
987
  return createBigIntTypedArrayNode(id, current, await parseAsync(ctx, depth, current.buffer));
947
988
  }
948
989
  async function parseDataView$1(ctx, depth, id, current) {
990
+ current = getArrayBufferView(ctx.base, current);
949
991
  return createDataViewNode(id, current, await parseAsync(ctx, depth, current.buffer));
950
992
  }
951
993
  async function parseError$1(ctx, depth, id, current) {
@@ -1190,7 +1232,7 @@ function isValidSymbol(symbol) {
1190
1232
  }
1191
1233
  //#endregion
1192
1234
  //#region src/core/context/deserializer.ts
1193
- const MAX_BASE64_LENGTH = 1e6;
1235
+ const DEFAULT_MAX_BASE64_LENGTH = 1e6;
1194
1236
  const MAX_BIGINT_LENGTH = 1e4;
1195
1237
  const MAX_REGEXP_SOURCE_LENGTH = 2e4;
1196
1238
  function applyObjectFlag(obj, flag) {
@@ -1203,7 +1245,9 @@ function applyObjectFlag(obj, flag) {
1203
1245
  }
1204
1246
  const DEFAULT_DEPTH_LIMIT = 1e3;
1205
1247
  function createBaseDeserializerContext(mode, options) {
1206
- var _options$features;
1248
+ var _options$maxBase64Len, _options$features;
1249
+ const maxBase64Length = (_options$maxBase64Len = options.maxBase64Length) !== null && _options$maxBase64Len !== void 0 ? _options$maxBase64Len : DEFAULT_MAX_BASE64_LENGTH;
1250
+ if (!Number.isSafeInteger(maxBase64Length) || maxBase64Length < 0) throw new RangeError("maxBase64Length must be a non-negative safe integer");
1207
1251
  const refs = options.refs || /* @__PURE__ */ new Map();
1208
1252
  if (!("types" in refs)) Object.assign(refs, { types: /* @__PURE__ */ new Map() });
1209
1253
  return {
@@ -1211,7 +1255,8 @@ function createBaseDeserializerContext(mode, options) {
1211
1255
  plugins: options.plugins,
1212
1256
  refs,
1213
1257
  features: (_options$features = options.features) !== null && _options$features !== void 0 ? _options$features : 127 ^ (options.disabledFeatures || 0),
1214
- depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT
1258
+ depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT,
1259
+ maxBase64Length
1215
1260
  };
1216
1261
  }
1217
1262
  function createVanillaDeserializerContext(options) {
@@ -1374,7 +1419,8 @@ function deserializeMap(ctx, depth, node) {
1374
1419
  return result;
1375
1420
  }
1376
1421
  function deserializeArrayBuffer(ctx, node) {
1377
- if (node.s.length > MAX_BASE64_LENGTH) throw new SerovalMalformedNodeError(node);
1422
+ if (typeof node.s !== "string") throw new SerovalMalformedNodeError(node);
1423
+ if (node.s.length > ctx.base.maxBase64Length) throw new RangeError("ArrayBuffer exceeds maxBase64Length (" + ctx.base.maxBase64Length + ")");
1378
1424
  return assignIndexedValue$1(ctx, node.i, ARRAY_BUFFER_CONSTRUCTOR(deserializeString(node.s)));
1379
1425
  }
1380
1426
  function deserializeTypedArray(ctx, depth, node) {
@@ -1383,7 +1429,7 @@ function deserializeTypedArray(ctx, depth, node) {
1383
1429
  const source = deserialize$1(ctx, depth, node.f);
1384
1430
  if (!(source instanceof ArrayBuffer)) throw new SerovalMalformedNodeError(node);
1385
1431
  const offset = (_node$b = node.b) !== null && _node$b !== void 0 ? _node$b : 0;
1386
- if (offset < 0 || offset > source.byteLength || node.l > MAX_BASE64_LENGTH) throw new SerovalMalformedNodeError(node);
1432
+ if (offset < 0 || offset > source.byteLength || node.l > ctx.base.maxBase64Length) throw new SerovalMalformedNodeError(node);
1387
1433
  return assignIndexedValue$1(ctx, node.i, new construct(source, offset, node.l));
1388
1434
  }
1389
1435
  function deserializeDataView(ctx, depth, node) {
@@ -1391,7 +1437,7 @@ function deserializeDataView(ctx, depth, node) {
1391
1437
  const source = deserialize$1(ctx, depth, node.f);
1392
1438
  if (!(source instanceof ArrayBuffer)) throw new SerovalMalformedNodeError(node);
1393
1439
  const offset = (_node$b2 = node.b) !== null && _node$b2 !== void 0 ? _node$b2 : 0;
1394
- if (offset < 0 || offset > source.byteLength || node.l > MAX_BASE64_LENGTH) throw new SerovalMalformedNodeError(node);
1440
+ if (offset < 0 || offset > source.byteLength || node.l > ctx.base.maxBase64Length) throw new SerovalMalformedNodeError(node);
1395
1441
  return assignIndexedValue$1(ctx, node.i, new DataView(source, offset, node.l));
1396
1442
  }
1397
1443
  function deserializeDictionary(ctx, depth, node, result) {
@@ -2411,7 +2457,7 @@ function parseProperties(ctx, depth, properties) {
2411
2457
  }
2412
2458
  if (SYM_ASYNC_ITERATOR in properties) {
2413
2459
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_ASYNC_ITERATOR));
2414
- valueNodes.push(createAsyncIteratorFactoryInstanceNode(parseAsyncIteratorFactory(ctx.base), parseSOS(ctx, depth, ctx.type === 1 ? createStream() : createStreamFromAsyncIterable(properties))));
2460
+ valueNodes.push(createAsyncIteratorFactoryInstanceNode(parseAsyncIteratorFactory(ctx.base), parseSOS(ctx, depth, ctx.type === 1 ? createStream() : createStreamFromAsyncIterable(properties, ctx.state.cleanups))));
2415
2461
  }
2416
2462
  if (SYM_TO_STRING_TAG in properties) {
2417
2463
  keyNodes.push(parseWellKnownSymbol(ctx.base, SYM_TO_STRING_TAG));
@@ -2433,12 +2479,15 @@ function parseBoxed(ctx, depth, id, current) {
2433
2479
  return createBoxedNode(id, parseSOS(ctx, depth, current.valueOf()));
2434
2480
  }
2435
2481
  function parseTypedArray(ctx, depth, id, current) {
2482
+ current = getArrayBufferView(ctx.base, current);
2436
2483
  return createTypedArrayNode(id, current, parseSOS(ctx, depth, current.buffer));
2437
2484
  }
2438
2485
  function parseBigIntTypedArray(ctx, depth, id, current) {
2486
+ current = getArrayBufferView(ctx.base, current);
2439
2487
  return createBigIntTypedArrayNode(id, current, parseSOS(ctx, depth, current.buffer));
2440
2488
  }
2441
2489
  function parseDataView(ctx, depth, id, current) {
2490
+ current = getArrayBufferView(ctx.base, current);
2442
2491
  return createDataViewNode(id, current, parseSOS(ctx, depth, current.buffer));
2443
2492
  }
2444
2493
  function parseError(ctx, depth, id, current) {
@@ -2689,6 +2738,7 @@ function destroyStreamParse(ctx) {
2689
2738
  function crossSerialize(source, options = {}) {
2690
2739
  const plugins = resolvePlugins(options.plugins);
2691
2740
  const ctx = createSyncParserContext(2, {
2741
+ compactArrayBufferViews: options.compactArrayBufferViews,
2692
2742
  plugins,
2693
2743
  disabledFeatures: options.disabledFeatures,
2694
2744
  refs: options.refs
@@ -2704,6 +2754,7 @@ function crossSerialize(source, options = {}) {
2704
2754
  async function crossSerializeAsync(source, options = {}) {
2705
2755
  const plugins = resolvePlugins(options.plugins);
2706
2756
  const ctx = createAsyncParserContext(2, {
2757
+ compactArrayBufferViews: options.compactArrayBufferViews,
2707
2758
  plugins,
2708
2759
  disabledFeatures: options.disabledFeatures,
2709
2760
  refs: options.refs
@@ -2717,15 +2768,19 @@ async function crossSerializeAsync(source, options = {}) {
2717
2768
  }), tree);
2718
2769
  }
2719
2770
  function toCrossJSON(source, options = {}) {
2771
+ const plugins = resolvePlugins(options.plugins);
2720
2772
  return parseTop(createSyncParserContext(2, {
2721
- plugins: resolvePlugins(options.plugins),
2773
+ compactArrayBufferViews: options.compactArrayBufferViews,
2774
+ plugins,
2722
2775
  disabledFeatures: options.disabledFeatures,
2723
2776
  refs: options.refs
2724
2777
  }), source);
2725
2778
  }
2726
2779
  async function toCrossJSONAsync(source, options = {}) {
2780
+ const plugins = resolvePlugins(options.plugins);
2727
2781
  return await parseTopAsync(createAsyncParserContext(2, {
2728
- plugins: resolvePlugins(options.plugins),
2782
+ compactArrayBufferViews: options.compactArrayBufferViews,
2783
+ plugins,
2729
2784
  disabledFeatures: options.disabledFeatures,
2730
2785
  refs: options.refs
2731
2786
  }), source);
@@ -2733,6 +2788,7 @@ async function toCrossJSONAsync(source, options = {}) {
2733
2788
  function crossSerializeStream(source, options) {
2734
2789
  const plugins = resolvePlugins(options.plugins);
2735
2790
  const ctx = createStreamParserContext({
2791
+ compactArrayBufferViews: options.compactArrayBufferViews,
2736
2792
  plugins,
2737
2793
  refs: options.refs,
2738
2794
  disabledFeatures: options.disabledFeatures,
@@ -2759,8 +2815,10 @@ function crossSerializeStream(source, options) {
2759
2815
  return destroyStreamParse.bind(null, ctx);
2760
2816
  }
2761
2817
  function toCrossJSONStream(source, options) {
2818
+ const plugins = resolvePlugins(options.plugins);
2762
2819
  const ctx = createStreamParserContext({
2763
- plugins: resolvePlugins(options.plugins),
2820
+ compactArrayBufferViews: options.compactArrayBufferViews,
2821
+ plugins,
2764
2822
  refs: options.refs,
2765
2823
  disabledFeatures: options.disabledFeatures,
2766
2824
  depthLimit: options.depthLimit,
@@ -2772,8 +2830,10 @@ function toCrossJSONStream(source, options) {
2772
2830
  return destroyStreamParse.bind(null, ctx);
2773
2831
  }
2774
2832
  function fromCrossJSON(source, options) {
2833
+ const plugins = resolvePlugins(options.plugins);
2775
2834
  return deserializeTop(createCrossDeserializerContext({
2776
- plugins: resolvePlugins(options.plugins),
2835
+ maxBase64Length: options.maxBase64Length,
2836
+ plugins,
2777
2837
  refs: options.refs,
2778
2838
  features: options.features,
2779
2839
  disabledFeatures: options.disabledFeatures,
@@ -2804,6 +2864,7 @@ var Serializer = class {
2804
2864
  scopeId: this.options.scopeId,
2805
2865
  refs: this.refs,
2806
2866
  disabledFeatures: this.options.disabledFeatures,
2867
+ compactArrayBufferViews: this.options.compactArrayBufferViews,
2807
2868
  onError: this.options.onError,
2808
2869
  onSerialize: (data, initial) => {
2809
2870
  if (this.alive) this.options.onData(initial ? this.options.globalIdentifier + "[\"" + serializeString(key) + "\"]=" + data : data);
@@ -2854,6 +2915,7 @@ var Serializer = class {
2854
2915
  function serialize(source, options = {}) {
2855
2916
  const plugins = resolvePlugins(options.plugins);
2856
2917
  const ctx = createSyncParserContext(1, {
2918
+ compactArrayBufferViews: options.compactArrayBufferViews,
2857
2919
  plugins,
2858
2920
  disabledFeatures: options.disabledFeatures
2859
2921
  });
@@ -2867,6 +2929,7 @@ function serialize(source, options = {}) {
2867
2929
  async function serializeAsync(source, options = {}) {
2868
2930
  const plugins = resolvePlugins(options.plugins);
2869
2931
  const ctx = createAsyncParserContext(1, {
2932
+ compactArrayBufferViews: options.compactArrayBufferViews,
2870
2933
  plugins,
2871
2934
  disabledFeatures: options.disabledFeatures
2872
2935
  });
@@ -2881,8 +2944,10 @@ function deserialize(source) {
2881
2944
  return (0, eval)(source);
2882
2945
  }
2883
2946
  function toJSON(source, options = {}) {
2947
+ const plugins = resolvePlugins(options.plugins);
2884
2948
  const ctx = createSyncParserContext(1, {
2885
- plugins: resolvePlugins(options.plugins),
2949
+ compactArrayBufferViews: options.compactArrayBufferViews,
2950
+ plugins,
2886
2951
  disabledFeatures: options.disabledFeatures
2887
2952
  });
2888
2953
  return {
@@ -2892,8 +2957,10 @@ function toJSON(source, options = {}) {
2892
2957
  };
2893
2958
  }
2894
2959
  async function toJSONAsync(source, options = {}) {
2960
+ const plugins = resolvePlugins(options.plugins);
2895
2961
  const ctx = createAsyncParserContext(1, {
2896
- plugins: resolvePlugins(options.plugins),
2962
+ compactArrayBufferViews: options.compactArrayBufferViews,
2963
+ plugins,
2897
2964
  disabledFeatures: options.disabledFeatures
2898
2965
  });
2899
2966
  return {
@@ -2915,6 +2982,7 @@ function fromJSON(source, options = {}) {
2915
2982
  const disabledFeatures = options.disabledFeatures || 0;
2916
2983
  const sourceFeatures = (_source$f = source.f) !== null && _source$f !== void 0 ? _source$f : 127;
2917
2984
  return deserializeTop(createVanillaDeserializerContext({
2985
+ maxBase64Length: options.maxBase64Length,
2918
2986
  plugins,
2919
2987
  markedRefs: source.m,
2920
2988
  features: sourceFeatures & ~disabledFeatures,