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.
- package/dist/cjs/development/index.cjs +556 -451
- package/dist/cjs/development/index.cjs.map +3 -3
- package/dist/cjs/production/index.cjs +17 -3
- package/dist/esm/development/index.mjs +556 -451
- package/dist/esm/development/index.mjs.map +3 -3
- package/dist/esm/production/index.mjs +17 -3
- package/dist/types/core/Serializer.d.ts.map +1 -1
- package/dist/types/core/base-primitives.d.ts.map +1 -1
- package/dist/types/core/constants.d.ts +4 -4
- package/dist/types/core/constants.d.ts.map +1 -1
- package/dist/types/core/context/deserializer.d.ts +1 -1
- package/dist/types/core/context/deserializer.d.ts.map +1 -1
- package/dist/types/core/context/parser/async.d.ts +1 -1
- package/dist/types/core/context/parser/async.d.ts.map +1 -1
- package/dist/types/core/context/parser/stream.d.ts +2 -2
- package/dist/types/core/context/parser/stream.d.ts.map +1 -1
- package/dist/types/core/context/parser/sync.d.ts +3 -3
- package/dist/types/core/context/parser/sync.d.ts.map +1 -1
- package/dist/types/core/context/parser.d.ts.map +1 -1
- package/dist/types/core/context/serializer.d.ts +1 -1
- package/dist/types/core/context/serializer.d.ts.map +1 -1
- package/dist/types/core/cross/async.d.ts +1 -1
- package/dist/types/core/cross/async.d.ts.map +1 -1
- package/dist/types/core/cross/deserializer.d.ts +1 -1
- package/dist/types/core/cross/deserializer.d.ts.map +1 -1
- package/dist/types/core/cross/serializer.d.ts +1 -1
- package/dist/types/core/cross/serializer.d.ts.map +1 -1
- package/dist/types/core/errors.d.ts +38 -0
- package/dist/types/core/errors.d.ts.map +1 -0
- package/dist/types/core/plugin.d.ts +1 -1
- package/dist/types/core/plugin.d.ts.map +1 -1
- package/dist/types/core/reference.d.ts.map +1 -1
- package/dist/types/core/tree/async.d.ts +1 -1
- package/dist/types/core/tree/async.d.ts.map +1 -1
- package/dist/types/core/tree/deserializer.d.ts +1 -1
- package/dist/types/core/tree/deserializer.d.ts.map +1 -1
- package/dist/types/core/tree/index.d.ts.map +1 -1
- package/dist/types/core/tree/serializer.d.ts +1 -1
- package/dist/types/core/tree/serializer.d.ts.map +1 -1
- package/dist/types/core/types.d.ts +1 -1
- package/dist/types/core/types.d.ts.map +1 -1
- package/dist/types/core/utils/typed-array.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +4 -5
- package/src/core/Serializer.ts +5 -5
- package/src/core/base-primitives.ts +4 -9
- package/src/core/constants.ts +12 -12
- package/src/core/context/deserializer.ts +100 -90
- package/src/core/context/parser/async.ts +56 -52
- package/src/core/context/parser/stream.ts +5 -5
- package/src/core/context/parser/sync.ts +45 -41
- package/src/core/context/parser.ts +5 -12
- package/src/core/context/serializer.ts +127 -118
- package/src/core/cross/async.ts +1 -1
- package/src/core/cross/deserializer.ts +1 -1
- package/src/core/cross/serializer.ts +2 -2
- package/src/core/errors.ts +102 -0
- package/src/core/literals.ts +4 -4
- package/src/core/plugin.ts +1 -1
- package/src/core/reference.ts +7 -3
- package/src/core/tree/async.ts +1 -1
- package/src/core/tree/deserializer.ts +1 -1
- package/src/core/tree/index.ts +8 -2
- package/src/core/tree/serializer.ts +8 -7
- package/src/core/types.ts +2 -2
- package/src/core/utils/typed-array.ts +3 -1
- package/src/index.ts +1 -0
- package/dist/types/core/UnsupportedTypeError.d.ts +0 -5
- package/dist/types/core/UnsupportedTypeError.d.ts.map +0 -1
- package/src/core/UnsupportedTypeError.ts +0 -7
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
import UnsupportedTypeError from '../../UnsupportedTypeError';
|
|
2
1
|
import {
|
|
3
|
-
|
|
4
|
-
createDateNode,
|
|
5
|
-
createRegExpNode,
|
|
2
|
+
createAggregateErrorNode,
|
|
6
3
|
createArrayBufferNode,
|
|
7
|
-
createBigIntNode,
|
|
8
|
-
createStringNode,
|
|
9
|
-
createNumberNode,
|
|
10
4
|
createArrayNode,
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
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 {
|
|
34
|
-
import {
|
|
35
|
-
import
|
|
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
|
-
|
|
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 {
|
|
58
|
-
import
|
|
59
|
-
import
|
|
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
|
|
432
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
433
433
|
}
|
|
434
434
|
|
|
435
435
|
async parse<T>(current: T): Promise<SerovalNode> {
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
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
|
-
|
|
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
|
-
|
|
457
|
-
|
|
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 {
|
|
25
|
-
import
|
|
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 {
|
|
30
|
-
import type {
|
|
31
|
-
import {
|
|
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
|
-
|
|
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 {
|
|
53
|
-
import
|
|
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
|
|
341
|
+
throw new SerovalUnsupportedTypeError(current);
|
|
342
342
|
}
|
|
343
343
|
|
|
344
344
|
parse<T>(current: T): SerovalNode {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
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
|
-
|
|
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
|
-
|
|
366
|
-
|
|
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
|
|