seroval 1.0.3 → 1.0.5

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 (71) hide show
  1. package/dist/cjs/development/index.cjs +556 -451
  2. package/dist/cjs/development/index.cjs.map +3 -3
  3. package/dist/cjs/production/index.cjs +17 -3
  4. package/dist/esm/development/index.mjs +556 -451
  5. package/dist/esm/development/index.mjs.map +3 -3
  6. package/dist/esm/production/index.mjs +17 -3
  7. package/dist/types/core/Serializer.d.ts.map +1 -1
  8. package/dist/types/core/base-primitives.d.ts.map +1 -1
  9. package/dist/types/core/constants.d.ts +4 -4
  10. package/dist/types/core/constants.d.ts.map +1 -1
  11. package/dist/types/core/context/deserializer.d.ts +1 -1
  12. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  13. package/dist/types/core/context/parser/async.d.ts +1 -1
  14. package/dist/types/core/context/parser/async.d.ts.map +1 -1
  15. package/dist/types/core/context/parser/stream.d.ts +2 -2
  16. package/dist/types/core/context/parser/stream.d.ts.map +1 -1
  17. package/dist/types/core/context/parser/sync.d.ts +3 -3
  18. package/dist/types/core/context/parser/sync.d.ts.map +1 -1
  19. package/dist/types/core/context/parser.d.ts.map +1 -1
  20. package/dist/types/core/context/serializer.d.ts +1 -1
  21. package/dist/types/core/context/serializer.d.ts.map +1 -1
  22. package/dist/types/core/cross/async.d.ts +1 -1
  23. package/dist/types/core/cross/async.d.ts.map +1 -1
  24. package/dist/types/core/cross/deserializer.d.ts +1 -1
  25. package/dist/types/core/cross/deserializer.d.ts.map +1 -1
  26. package/dist/types/core/cross/serializer.d.ts +1 -1
  27. package/dist/types/core/cross/serializer.d.ts.map +1 -1
  28. package/dist/types/core/errors.d.ts +38 -0
  29. package/dist/types/core/errors.d.ts.map +1 -0
  30. package/dist/types/core/plugin.d.ts +1 -1
  31. package/dist/types/core/plugin.d.ts.map +1 -1
  32. package/dist/types/core/reference.d.ts.map +1 -1
  33. package/dist/types/core/tree/async.d.ts +1 -1
  34. package/dist/types/core/tree/async.d.ts.map +1 -1
  35. package/dist/types/core/tree/deserializer.d.ts +1 -1
  36. package/dist/types/core/tree/deserializer.d.ts.map +1 -1
  37. package/dist/types/core/tree/index.d.ts.map +1 -1
  38. package/dist/types/core/tree/serializer.d.ts +1 -1
  39. package/dist/types/core/tree/serializer.d.ts.map +1 -1
  40. package/dist/types/core/types.d.ts +1 -1
  41. package/dist/types/core/types.d.ts.map +1 -1
  42. package/dist/types/core/utils/typed-array.d.ts.map +1 -1
  43. package/dist/types/index.d.ts +1 -0
  44. package/dist/types/index.d.ts.map +1 -1
  45. package/package.json +4 -5
  46. package/src/core/Serializer.ts +5 -5
  47. package/src/core/base-primitives.ts +4 -9
  48. package/src/core/constants.ts +12 -12
  49. package/src/core/context/deserializer.ts +100 -90
  50. package/src/core/context/parser/async.ts +56 -52
  51. package/src/core/context/parser/stream.ts +5 -5
  52. package/src/core/context/parser/sync.ts +45 -41
  53. package/src/core/context/parser.ts +5 -12
  54. package/src/core/context/serializer.ts +127 -118
  55. package/src/core/cross/async.ts +1 -1
  56. package/src/core/cross/deserializer.ts +1 -1
  57. package/src/core/cross/serializer.ts +2 -2
  58. package/src/core/errors.ts +102 -0
  59. package/src/core/literals.ts +4 -4
  60. package/src/core/plugin.ts +1 -1
  61. package/src/core/reference.ts +7 -3
  62. package/src/core/tree/async.ts +1 -1
  63. package/src/core/tree/deserializer.ts +1 -1
  64. package/src/core/tree/index.ts +8 -2
  65. package/src/core/tree/serializer.ts +8 -7
  66. package/src/core/types.ts +2 -2
  67. package/src/core/utils/typed-array.ts +3 -1
  68. package/src/index.ts +1 -0
  69. package/dist/types/core/UnsupportedTypeError.d.ts +0 -5
  70. package/dist/types/core/UnsupportedTypeError.d.ts.map +0 -1
  71. package/src/core/UnsupportedTypeError.ts +0 -7
@@ -1,66 +1,66 @@
1
- import UnsupportedTypeError from '../../UnsupportedTypeError';
2
1
  import {
3
- createPluginNode,
4
- createDateNode,
5
- createRegExpNode,
2
+ createAggregateErrorNode,
6
3
  createArrayBufferNode,
7
- createBigIntNode,
8
- createStringNode,
9
- createNumberNode,
10
4
  createArrayNode,
11
- createBoxedNode,
12
- createTypedArrayNode,
5
+ createAsyncIteratorFactoryInstanceNode,
6
+ createBigIntNode,
13
7
  createBigIntTypedArrayNode,
8
+ createBoxedNode,
14
9
  createDataViewNode,
10
+ createDateNode,
15
11
  createErrorNode,
16
- createSetNode,
17
- createAggregateErrorNode,
18
12
  createIteratorFactoryInstanceNode,
19
- createAsyncIteratorFactoryInstanceNode,
13
+ createNumberNode,
14
+ createPluginNode,
15
+ createRegExpNode,
16
+ createSetNode,
20
17
  createStreamConstructorNode,
21
18
  createStreamNextNode,
22
- createStreamThrowNode,
23
19
  createStreamReturnNode,
20
+ createStreamThrowNode,
21
+ createStringNode,
22
+ createTypedArrayNode,
24
23
  } from '../../base-primitives';
25
24
  import { Feature } from '../../compat';
26
25
  import { SerovalNodeType } from '../../constants';
26
+ import { SerovalParserError, SerovalUnsupportedTypeError } from '../../errors';
27
27
  import {
28
+ FALSE_NODE,
28
29
  NULL_NODE,
29
30
  TRUE_NODE,
30
- FALSE_NODE,
31
31
  UNDEFINED_NODE,
32
32
  } from '../../literals';
33
- import { iteratorToSequence } from '../../utils/iterator-to-sequence';
34
- import { BaseParserContext } from '../parser';
35
- import promiseToResult from '../../utils/promise-to-result';
36
- import { getErrorOptions } from '../../utils/error';
33
+ import { SpecialReference } from '../../special-reference';
34
+ import type { Stream } from '../../stream';
35
+ import { createStreamFromAsyncIterable, isStream } from '../../stream';
37
36
  import { serializeString } from '../../string';
38
37
  import type {
39
- SerovalErrorNode,
38
+ SerovalAggregateErrorNode,
40
39
  SerovalArrayNode,
41
40
  SerovalBigIntTypedArrayNode,
42
41
  SerovalBoxedNode,
42
+ SerovalDataViewNode,
43
+ SerovalErrorNode,
44
+ SerovalMapNode,
43
45
  SerovalNode,
44
46
  SerovalNullConstructorNode,
45
47
  SerovalObjectNode,
46
48
  SerovalObjectRecordKey,
47
49
  SerovalObjectRecordNode,
48
- SerovalPromiseNode,
49
- SerovalTypedArrayNode,
50
- SerovalAggregateErrorNode,
51
- SerovalMapNode,
52
50
  SerovalPluginNode,
51
+ SerovalPromiseNode,
53
52
  SerovalSetNode,
54
- SerovalDataViewNode,
55
53
  SerovalStreamConstructorNode,
54
+ SerovalTypedArrayNode,
56
55
  } from '../../types';
57
- import { SpecialReference } from '../../special-reference';
58
- import type { Stream } from '../../stream';
59
- import { createStreamFromAsyncIterable, isStream } from '../../stream';
56
+ import { getErrorOptions } from '../../utils/error';
57
+ import { iteratorToSequence } from '../../utils/iterator-to-sequence';
58
+ import promiseToResult from '../../utils/promise-to-result';
60
59
  import type {
61
60
  BigIntTypedArrayValue,
62
61
  TypedArrayValue,
63
62
  } from '../../utils/typed-array';
63
+ import { BaseParserContext } from '../parser';
64
64
 
65
65
  type ObjectLikeNode =
66
66
  | SerovalObjectNode
@@ -429,36 +429,40 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
429
429
  if (Symbol.iterator in current || Symbol.asyncIterator in current) {
430
430
  return this.parsePlainObject(id, current, !!currentClass);
431
431
  }
432
- throw new UnsupportedTypeError(current);
432
+ throw new SerovalUnsupportedTypeError(current);
433
433
  }
434
434
 
435
435
  async parse<T>(current: T): Promise<SerovalNode> {
436
- switch (typeof current) {
437
- case 'boolean':
438
- return current ? TRUE_NODE : FALSE_NODE;
439
- case 'undefined':
440
- return UNDEFINED_NODE;
441
- case 'string':
442
- return createStringNode(current as string);
443
- case 'number':
444
- return createNumberNode(current as number);
445
- case 'bigint':
446
- return createBigIntNode(current as bigint);
447
- case 'object': {
448
- if (current) {
449
- const ref = this.getReference(current);
450
- return ref.type === 0
451
- ? await this.parseObject(ref.value, current as object)
452
- : ref.value;
436
+ try {
437
+ switch (typeof current) {
438
+ case 'boolean':
439
+ return current ? TRUE_NODE : FALSE_NODE;
440
+ case 'undefined':
441
+ return UNDEFINED_NODE;
442
+ case 'string':
443
+ return createStringNode(current as string);
444
+ case 'number':
445
+ return createNumberNode(current as number);
446
+ case 'bigint':
447
+ return createBigIntNode(current as bigint);
448
+ case 'object': {
449
+ if (current) {
450
+ const ref = this.getReference(current);
451
+ return ref.type === 0
452
+ ? await this.parseObject(ref.value, current as object)
453
+ : ref.value;
454
+ }
455
+ return NULL_NODE;
453
456
  }
454
- return NULL_NODE;
457
+ case 'symbol':
458
+ return this.parseWellKnownSymbol(current);
459
+ case 'function':
460
+ return this.parseFunction(current as (...args: unknown[]) => unknown);
461
+ default:
462
+ throw new SerovalUnsupportedTypeError(current);
455
463
  }
456
- case 'symbol':
457
- return this.parseWellKnownSymbol(current);
458
- case 'function':
459
- return this.parseFunction(current as (...args: unknown[]) => unknown);
460
- default:
461
- throw new UnsupportedTypeError(current);
464
+ } catch (error) {
465
+ throw new SerovalParserError(error);
462
466
  }
463
467
  }
464
468
  }
@@ -8,10 +8,11 @@ import {
8
8
  createStreamThrowNode,
9
9
  createStringNode,
10
10
  } from '../../base-primitives';
11
- import type { BaseSyncParserContextOptions } from './sync';
12
- import BaseSyncParserContext from './sync';
13
11
  import { SerovalNodeType } from '../../constants';
14
12
  import { FALSE_NODE, TRUE_NODE } from '../../literals';
13
+ import { SpecialReference } from '../../special-reference';
14
+ import type { Stream } from '../../stream';
15
+ import { createStreamFromAsyncIterable } from '../../stream';
15
16
  import { serializeString } from '../../string';
16
17
  import type {
17
18
  SerovalNode,
@@ -21,9 +22,8 @@ import type {
21
22
  SerovalPromiseConstructorNode,
22
23
  } from '../../types';
23
24
  import { iteratorToSequence } from '../../utils/iterator-to-sequence';
24
- import { SpecialReference } from '../../special-reference';
25
- import type { Stream } from '../../stream';
26
- import { createStreamFromAsyncIterable } from '../../stream';
25
+ import type { BaseSyncParserContextOptions } from './sync';
26
+ import BaseSyncParserContext from './sync';
27
27
 
28
28
  export interface BaseStreamParserContextOptions
29
29
  extends BaseSyncParserContextOptions {
@@ -1,4 +1,3 @@
1
- import UnsupportedTypeError from '../../UnsupportedTypeError';
2
1
  import {
3
2
  createAggregateErrorNode,
4
3
  createArrayBufferNode,
@@ -20,42 +19,43 @@ import {
20
19
  createTypedArrayNode,
21
20
  } from '../../base-primitives';
22
21
  import { Feature } from '../../compat';
22
+ import { SerovalParserError, SerovalUnsupportedTypeError } from '../../errors';
23
23
  import {
24
24
  FALSE_NODE,
25
25
  NULL_NODE,
26
26
  TRUE_NODE,
27
27
  UNDEFINED_NODE,
28
28
  } from '../../literals';
29
- import { iteratorToSequence } from '../../utils/iterator-to-sequence';
30
- import type { BaseParserContextOptions } from '../parser';
31
- import { BaseParserContext } from '../parser';
32
- import { getErrorOptions } from '../../utils/error';
29
+ import { SpecialReference } from '../../special-reference';
30
+ import type { Stream } from '../../stream';
31
+ import { createStream, isStream } from '../../stream';
33
32
  import { serializeString } from '../../string';
34
33
  import type {
35
- SerovalBoxedNode,
34
+ SerovalAggregateErrorNode,
36
35
  SerovalArrayNode,
36
+ SerovalBigIntTypedArrayNode,
37
+ SerovalBoxedNode,
38
+ SerovalDataViewNode,
39
+ SerovalErrorNode,
40
+ SerovalMapNode,
37
41
  SerovalNode,
38
42
  SerovalNullConstructorNode,
39
43
  SerovalObjectNode,
40
44
  SerovalObjectRecordKey,
41
45
  SerovalObjectRecordNode,
42
- SerovalErrorNode,
43
- SerovalMapNode,
44
- SerovalSetNode,
45
46
  SerovalPluginNode,
46
- SerovalAggregateErrorNode,
47
- SerovalTypedArrayNode,
48
- SerovalBigIntTypedArrayNode,
49
- SerovalDataViewNode,
50
47
  SerovalPromiseConstructorNode,
48
+ SerovalSetNode,
49
+ SerovalTypedArrayNode,
51
50
  } from '../../types';
52
- import { SpecialReference } from '../../special-reference';
53
- import type { Stream } from '../../stream';
54
- import { createStream, isStream } from '../../stream';
51
+ import { getErrorOptions } from '../../utils/error';
52
+ import { iteratorToSequence } from '../../utils/iterator-to-sequence';
55
53
  import type {
56
54
  BigIntTypedArrayValue,
57
55
  TypedArrayValue,
58
56
  } from '../../utils/typed-array';
57
+ import type { BaseParserContextOptions } from '../parser';
58
+ import { BaseParserContext } from '../parser';
59
59
 
60
60
  type ObjectLikeNode = SerovalObjectNode | SerovalNullConstructorNode;
61
61
 
@@ -338,36 +338,40 @@ export default abstract class BaseSyncParserContext extends BaseParserContext {
338
338
  if (Symbol.iterator in current || Symbol.asyncIterator in current) {
339
339
  return this.parsePlainObject(id, current, !!currentClass);
340
340
  }
341
- throw new UnsupportedTypeError(current);
341
+ throw new SerovalUnsupportedTypeError(current);
342
342
  }
343
343
 
344
344
  parse<T>(current: T): SerovalNode {
345
- switch (typeof current) {
346
- case 'boolean':
347
- return current ? TRUE_NODE : FALSE_NODE;
348
- case 'undefined':
349
- return UNDEFINED_NODE;
350
- case 'string':
351
- return createStringNode(current as string);
352
- case 'number':
353
- return createNumberNode(current as number);
354
- case 'bigint':
355
- return createBigIntNode(current as bigint);
356
- case 'object': {
357
- if (current) {
358
- const ref = this.getReference(current);
359
- return ref.type === 0
360
- ? this.parseObject(ref.value, current as object)
361
- : ref.value;
345
+ try {
346
+ switch (typeof current) {
347
+ case 'boolean':
348
+ return current ? TRUE_NODE : FALSE_NODE;
349
+ case 'undefined':
350
+ return UNDEFINED_NODE;
351
+ case 'string':
352
+ return createStringNode(current as string);
353
+ case 'number':
354
+ return createNumberNode(current as number);
355
+ case 'bigint':
356
+ return createBigIntNode(current as bigint);
357
+ case 'object': {
358
+ if (current) {
359
+ const ref = this.getReference(current);
360
+ return ref.type === 0
361
+ ? this.parseObject(ref.value, current as object)
362
+ : ref.value;
363
+ }
364
+ return NULL_NODE;
362
365
  }
363
- return NULL_NODE;
366
+ case 'symbol':
367
+ return this.parseWellKnownSymbol(current);
368
+ case 'function':
369
+ return this.parseFunction(current as (...args: unknown[]) => unknown);
370
+ default:
371
+ throw new SerovalUnsupportedTypeError(current);
364
372
  }
365
- case 'symbol':
366
- return this.parseWellKnownSymbol(current);
367
- case 'function':
368
- return this.parseFunction(current as (...args: unknown[]) => unknown);
369
- default:
370
- throw new UnsupportedTypeError(current);
373
+ } catch (error) {
374
+ throw new SerovalParserError(error);
371
375
  }
372
376
  }
373
377
  }
@@ -1,4 +1,3 @@
1
- import assert from '../utils/assert';
2
1
  import {
3
2
  createIndexedValueNode,
4
3
  createReferenceNode,
@@ -7,13 +6,14 @@ import {
7
6
  import { ALL_ENABLED } from '../compat';
8
7
  import type { WellKnownSymbols } from '../constants';
9
8
  import { INV_SYMBOL_REF, SerovalNodeType } from '../constants';
9
+ import { SerovalUnsupportedTypeError } from '../errors';
10
10
  import type { Plugin, PluginAccessOptions, SerovalMode } from '../plugin';
11
11
  import { hasReferenceID } from '../reference';
12
12
  import {
13
- SpecialReference,
14
13
  ASYNC_ITERATOR,
15
14
  ITERATOR,
16
15
  SPECIAL_REFS,
16
+ SpecialReference,
17
17
  } from '../special-reference';
18
18
  import type {
19
19
  SerovalAsyncIteratorFactoryNode,
@@ -29,6 +29,7 @@ import type {
29
29
  SerovalSpecialReferenceNode,
30
30
  SerovalWKSymbolNode,
31
31
  } from '../types';
32
+ import assert from '../utils/assert';
32
33
  import { getObjectFlag } from '../utils/get-object-flag';
33
34
 
34
35
  export interface BaseParserContextOptions extends PluginAccessOptions {
@@ -118,12 +119,7 @@ export abstract class BaseParserContext implements PluginAccessOptions {
118
119
  protected getStrictReference<T>(
119
120
  current: T,
120
121
  ): SerovalIndexedValueNode | SerovalReferenceNode {
121
- assert(
122
- hasReferenceID(current),
123
- new Error(
124
- 'Cannot serialize ' + typeof current + ' without reference ID.',
125
- ),
126
- );
122
+ assert(hasReferenceID(current), new SerovalUnsupportedTypeError(current));
127
123
  const result = this.getIndexedValue(current);
128
124
  if (result.type === NodeType.Indexed) {
129
125
  return result.value;
@@ -144,10 +140,7 @@ export abstract class BaseParserContext implements PluginAccessOptions {
144
140
  if (ref.type !== NodeType.Fresh) {
145
141
  return ref.value;
146
142
  }
147
- assert(
148
- current in INV_SYMBOL_REF,
149
- new Error('Cannot serialized unsupported symbol.'),
150
- );
143
+ assert(current in INV_SYMBOL_REF, new SerovalUnsupportedTypeError(current));
151
144
  return createWKSymbolNode(ref.value, current as WellKnownSymbols);
152
145
  }
153
146