seroval 1.4.0 → 1.4.2

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.
Files changed (48) hide show
  1. package/dist/cjs/development/index.cjs +553 -350
  2. package/dist/cjs/development/index.cjs.map +3 -3
  3. package/dist/cjs/production/index.cjs +3 -3
  4. package/dist/esm/development/index.mjs +553 -350
  5. package/dist/esm/development/index.mjs.map +3 -3
  6. package/dist/esm/production/index.mjs +3 -3
  7. package/dist/types/core/base-primitives.d.ts +2 -2
  8. package/dist/types/core/base-primitives.d.ts.map +1 -1
  9. package/dist/types/core/compat.d.ts +2 -1
  10. package/dist/types/core/compat.d.ts.map +1 -1
  11. package/dist/types/core/constants.d.ts +2 -2
  12. package/dist/types/core/constants.d.ts.map +1 -1
  13. package/dist/types/core/constructors.d.ts +1 -1
  14. package/dist/types/core/constructors.d.ts.map +1 -1
  15. package/dist/types/core/context/async-parser.d.ts +5 -4
  16. package/dist/types/core/context/async-parser.d.ts.map +1 -1
  17. package/dist/types/core/context/deserializer.d.ts +7 -2
  18. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  19. package/dist/types/core/context/parser.d.ts +3 -1
  20. package/dist/types/core/context/parser.d.ts.map +1 -1
  21. package/dist/types/core/context/serializer.d.ts.map +1 -1
  22. package/dist/types/core/context/sync-parser.d.ts +5 -5
  23. package/dist/types/core/context/sync-parser.d.ts.map +1 -1
  24. package/dist/types/core/cross/index.d.ts.map +1 -1
  25. package/dist/types/core/errors.d.ts +9 -0
  26. package/dist/types/core/errors.d.ts.map +1 -1
  27. package/dist/types/core/node.d.ts +1 -1
  28. package/dist/types/core/string.d.ts.map +1 -1
  29. package/dist/types/core/tree/index.d.ts +4 -1
  30. package/dist/types/core/tree/index.d.ts.map +1 -1
  31. package/dist/types/core/types.d.ts +6 -11
  32. package/dist/types/core/types.d.ts.map +1 -1
  33. package/package.json +1 -1
  34. package/src/core/base-primitives.ts +17 -18
  35. package/src/core/compat.ts +3 -1
  36. package/src/core/constants.ts +18 -4
  37. package/src/core/constructors.ts +2 -1
  38. package/src/core/context/async-parser.ts +102 -50
  39. package/src/core/context/deserializer.ts +269 -105
  40. package/src/core/context/parser.ts +12 -10
  41. package/src/core/context/serializer.ts +31 -36
  42. package/src/core/context/sync-parser.ts +149 -75
  43. package/src/core/cross/index.ts +2 -0
  44. package/src/core/errors.ts +33 -0
  45. package/src/core/node.ts +2 -2
  46. package/src/core/string.ts +3 -1
  47. package/src/core/tree/index.ts +10 -1
  48. package/src/core/types.ts +10 -19
@@ -38,6 +38,7 @@ import { getObjectFlag } from '../utils/get-object-flag';
38
38
  export interface BaseParserContextOptions extends PluginAccessOptions {
39
39
  disabledFeatures?: number;
40
40
  refs?: Map<unknown, number>;
41
+ depthLimit?: number;
41
42
  }
42
43
 
43
44
  export const enum ParserNodeType {
@@ -71,6 +72,8 @@ export interface BaseParserContext extends PluginAccessOptions {
71
72
  refs: Map<unknown, number>;
72
73
 
73
74
  features: number;
75
+
76
+ depthLimit: number;
74
77
  }
75
78
 
76
79
  export function createBaseParserContext(
@@ -83,6 +86,7 @@ export function createBaseParserContext(
83
86
  marked: new Set(),
84
87
  features: ALL_ENABLED ^ (options.disabledFeatures || 0),
85
88
  refs: options.refs || new Map(),
89
+ depthLimit: options.depthLimit || 1000,
86
90
  };
87
91
  }
88
92
 
@@ -206,10 +210,10 @@ export function parseIteratorFactory(
206
210
  NIL,
207
211
  NIL,
208
212
  NIL,
209
- NIL,
210
213
  parseWellKnownSymbol(ctx, SYM_ITERATOR),
211
214
  NIL,
212
215
  NIL,
216
+ NIL,
213
217
  );
214
218
  }
215
219
 
@@ -228,7 +232,6 @@ export function parseAsyncIteratorFactory(
228
232
  NIL,
229
233
  NIL,
230
234
  NIL,
231
- NIL,
232
235
  [
233
236
  parseSpecialReference(ctx, SpecialReference.PromiseConstructor),
234
237
  parseWellKnownSymbol(ctx, SYM_ASYNC_ITERATOR),
@@ -236,6 +239,7 @@ export function parseAsyncIteratorFactory(
236
239
  NIL,
237
240
  NIL,
238
241
  NIL,
242
+ NIL,
239
243
  );
240
244
  }
241
245
 
@@ -251,13 +255,13 @@ export function createObjectNode(
251
255
  NIL,
252
256
  NIL,
253
257
  NIL,
254
- NIL,
255
258
  record,
256
259
  NIL,
257
260
  NIL,
258
261
  NIL,
259
262
  NIL,
260
263
  getObjectFlag(current),
264
+ NIL,
261
265
  );
262
266
  }
263
267
 
@@ -266,7 +270,6 @@ export function createMapNode(
266
270
  id: number,
267
271
  k: SerovalNode[],
268
272
  v: SerovalNode[],
269
- s: number,
270
273
  ): SerovalMapNode {
271
274
  return createSerovalNode(
272
275
  SerovalNodeType.Map,
@@ -275,12 +278,12 @@ export function createMapNode(
275
278
  NIL,
276
279
  NIL,
277
280
  NIL,
278
- NIL,
279
- { k, v, s },
281
+ { k, v },
280
282
  NIL,
281
283
  parseSpecialReference(ctx, SpecialReference.MapSentinel),
282
284
  NIL,
283
285
  NIL,
286
+ NIL,
284
287
  );
285
288
  }
286
289
 
@@ -298,10 +301,10 @@ export function createPromiseConstructorNode(
298
301
  NIL,
299
302
  NIL,
300
303
  NIL,
301
- NIL,
302
304
  parseSpecialReference(ctx, SpecialReference.PromiseConstructor),
303
305
  NIL,
304
306
  NIL,
307
+ NIL,
305
308
  );
306
309
  }
307
310
 
@@ -311,16 +314,14 @@ export function createArrayBufferNode(
311
314
  current: ArrayBuffer,
312
315
  ): SerovalArrayBufferNode {
313
316
  const bytes = new Uint8Array(current);
314
- const len = bytes.length;
315
317
  let result = '';
316
- for (let i = 0; i < len; i++) {
318
+ for (let i = 0, len = bytes.length; i < len; i++) {
317
319
  result += String.fromCharCode(bytes[i]);
318
320
  }
319
321
  return createSerovalNode(
320
322
  SerovalNodeType.ArrayBuffer,
321
323
  id,
322
324
  serializeString(btoa(result)),
323
- len,
324
325
  NIL,
325
326
  NIL,
326
327
  NIL,
@@ -329,5 +330,6 @@ export function createArrayBufferNode(
329
330
  parseSpecialReference(ctx, SpecialReference.ArrayBufferConstructor),
330
331
  NIL,
331
332
  NIL,
333
+ NIL,
332
334
  );
333
335
  }
@@ -282,7 +282,7 @@ export function createVanillaSerializerContext(
282
282
  mode: SerovalMode.Vanilla,
283
283
  base: createBaseSerializerContext(SerovalMode.Vanilla, options),
284
284
  state: createVanillaSerializerState(),
285
- child: undefined,
285
+ child: NIL,
286
286
  };
287
287
  }
288
288
 
@@ -306,7 +306,7 @@ export function createCrossSerializerContext(
306
306
  mode: SerovalMode.Cross,
307
307
  base: createBaseSerializerContext(SerovalMode.Cross, options),
308
308
  state: options,
309
- child: undefined,
309
+ child: NIL,
310
310
  };
311
311
  }
312
312
 
@@ -531,7 +531,7 @@ function serializeReference(node: SerovalReferenceNode): string {
531
531
  function serializeArrayItem(
532
532
  ctx: SerializerContext,
533
533
  id: number,
534
- item: SerovalNode | undefined,
534
+ item: SerovalNode | 0,
535
535
  index: number,
536
536
  ): string {
537
537
  // Check if index is a hole
@@ -557,15 +557,16 @@ function serializeArray(
557
557
  node: SerovalArrayNode,
558
558
  ): string {
559
559
  const id = node.i;
560
- if (node.l) {
560
+ const list = node.a;
561
+ const len = list.length;
562
+ if (len > 0) {
561
563
  ctx.base.stack.push(id);
562
- const list = node.a;
563
564
  let values = serializeArrayItem(ctx, id, list[0], 0);
564
565
  // This is different than Map and Set
565
566
  // because we also need to serialize
566
567
  // the holes of the Array
567
568
  let isHoley = values === '';
568
- for (let i = 1, len = node.l, item: string; i < len; i++) {
569
+ for (let i = 1, item: string; i < len; i++) {
569
570
  item = serializeArrayItem(ctx, id, list[i], i);
570
571
  values += ',' + item;
571
572
  isHoley = item === '';
@@ -625,9 +626,9 @@ function serializeProperties(
625
626
  source: SerovalNodeWithProperties,
626
627
  record: SerovalObjectRecordNode,
627
628
  ): string {
628
- const len = record.s;
629
- if (len) {
630
- const keys = record.k;
629
+ const keys = record.k;
630
+ const len = keys.length;
631
+ if (len > 0) {
631
632
  const values = record.v;
632
633
  ctx.base.stack.push(source.i);
633
634
  let result = serializeProperty(ctx, source, keys[0], values[0]);
@@ -742,10 +743,10 @@ function serializeAssignments(
742
743
  source: SerovalNodeWithProperties,
743
744
  node: SerovalObjectRecordNode,
744
745
  ): string | undefined {
745
- const len = node.s;
746
- if (len) {
746
+ const keys = node.k;
747
+ const len = keys.length;
748
+ if (len > 0) {
747
749
  const mainAssignments: Assignment[] = [];
748
- const keys = node.k;
749
750
  const values = node.v;
750
751
  ctx.base.stack.push(source.i);
751
752
  for (let i = 0; i < len; i++) {
@@ -796,8 +797,14 @@ function serializeDate(node: SerovalDateNode): string {
796
797
  return 'new Date("' + node.s + '")';
797
798
  }
798
799
 
799
- function serializeRegExp(node: SerovalRegExpNode): string {
800
- return '/' + node.c + '/' + node.m;
800
+ function serializeRegExp(
801
+ ctx: SerializerContext,
802
+ node: SerovalRegExpNode,
803
+ ): string {
804
+ if (ctx.base.features & Feature.RegExp) {
805
+ return '/' + node.c + '/' + node.m;
806
+ }
807
+ throw new SerovalUnsupportedNodeError(node);
801
808
  }
802
809
 
803
810
  function serializeSetItem(
@@ -820,10 +827,10 @@ function serializeSetItem(
820
827
 
821
828
  function serializeSet(ctx: SerializerContext, node: SerovalSetNode): string {
822
829
  let serialized = SET_CONSTRUCTOR;
823
- const size = node.l;
830
+ const items = node.a;
831
+ const size = items.length;
824
832
  const id = node.i;
825
- if (size) {
826
- const items = node.a;
833
+ if (size > 0) {
827
834
  ctx.base.stack.push(id);
828
835
  let result = serializeSetItem(ctx, id, items[0]);
829
836
  for (let i = 1, item = result; i < size; i++) {
@@ -913,13 +920,13 @@ function serializeMapEntry(
913
920
 
914
921
  function serializeMap(ctx: SerializerContext, node: SerovalMapNode): string {
915
922
  let serialized = MAP_CONSTRUCTOR;
916
- const size = node.e.s;
923
+ const keys = node.e.k;
924
+ const size = keys.length;
917
925
  const id = node.i;
918
926
  const sentinel = node.f;
919
927
  const sentinelId = getRefParam(ctx, sentinel.i);
920
928
  const base = ctx.base;
921
- if (size) {
922
- const keys = node.e.k;
929
+ if (size > 0) {
923
930
  const vals = node.e.v;
924
931
  base.stack.push(id);
925
932
  let result = serializeMapEntry(ctx, id, keys[0], vals[0], sentinelId);
@@ -946,33 +953,21 @@ function serializeArrayBuffer(
946
953
  ctx: SerializerContext,
947
954
  node: SerovalArrayBufferNode,
948
955
  ): string {
949
- return getConstructor(ctx, node.f) + '(' + node.l + ',"' + node.s + '")';
956
+ return getConstructor(ctx, node.f) + '("' + node.s + '")';
950
957
  }
951
958
 
952
959
  function serializeTypedArray(
953
960
  ctx: SerializerContext,
954
961
  node: SerovalTypedArrayNode | SerovalBigIntTypedArrayNode,
955
962
  ): string {
956
- return (
957
- 'new ' +
958
- node.c +
959
- '(' +
960
- serialize(ctx, node.f) +
961
- ',' +
962
- node.b +
963
- ',' +
964
- node.l +
965
- ')'
966
- );
963
+ return 'new ' + node.c + '(' + serialize(ctx, node.f) + ',' + node.b + ',' + node.l + ')';
967
964
  }
968
965
 
969
966
  function serializeDataView(
970
967
  ctx: SerializerContext,
971
968
  node: SerovalDataViewNode,
972
969
  ): string {
973
- return (
974
- 'new DataView(' + serialize(ctx, node.f) + ',' + node.b + ',' + node.l + ')'
975
- );
970
+ return 'new DataView(' + serialize(ctx, node.f) + ',' + node.b + ',' + node.l + ')';
976
971
  }
977
972
 
978
973
  function serializeAggregateError(
@@ -1262,7 +1257,7 @@ function serializeAssignable(
1262
1257
  case SerovalNodeType.Date:
1263
1258
  return serializeDate(node);
1264
1259
  case SerovalNodeType.RegExp:
1265
- return serializeRegExp(node);
1260
+ return serializeRegExp(ctx, node);
1266
1261
  case SerovalNodeType.Set:
1267
1262
  return serializeSet(ctx, node);
1268
1263
  case SerovalNodeType.Map: