seroval 1.3.2 → 1.4.0
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 +2640 -2683
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -17
- package/dist/esm/development/index.mjs +2640 -2683
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +3 -17
- package/dist/types/core/base-primitives.d.ts +1 -2
- package/dist/types/core/base-primitives.d.ts.map +1 -1
- package/dist/types/core/compat.d.ts +1 -1
- package/dist/types/core/compat.d.ts.map +1 -1
- package/dist/types/core/constants.d.ts +1 -0
- package/dist/types/core/constants.d.ts.map +1 -1
- package/dist/types/core/constructors.d.ts +54 -0
- package/dist/types/core/constructors.d.ts.map +1 -0
- package/dist/types/core/context/async-parser.d.ts +19 -0
- package/dist/types/core/context/async-parser.d.ts.map +1 -0
- package/dist/types/core/context/deserializer.d.ts +35 -38
- package/dist/types/core/context/deserializer.d.ts.map +1 -1
- package/dist/types/core/context/parser.d.ts +32 -19
- package/dist/types/core/context/parser.d.ts.map +1 -1
- package/dist/types/core/context/serializer.d.ts +38 -99
- package/dist/types/core/context/serializer.d.ts.map +1 -1
- package/dist/types/core/context/sync-parser.d.ts +59 -0
- package/dist/types/core/context/sync-parser.d.ts.map +1 -0
- package/dist/types/core/cross/index.d.ts +12 -13
- package/dist/types/core/cross/index.d.ts.map +1 -1
- package/dist/types/core/errors.d.ts.map +1 -1
- package/dist/types/core/function-string.d.ts +2 -2
- package/dist/types/core/function-string.d.ts.map +1 -1
- package/dist/types/core/plugin.d.ts +13 -11
- package/dist/types/core/plugin.d.ts.map +1 -1
- package/dist/types/core/reference.d.ts.map +1 -1
- package/dist/types/core/special-reference.d.ts +3 -2
- package/dist/types/core/special-reference.d.ts.map +1 -1
- package/dist/types/core/stream.d.ts +1 -2
- package/dist/types/core/stream.d.ts.map +1 -1
- package/dist/types/core/symbols.d.ts +14 -0
- package/dist/types/core/symbols.d.ts.map +1 -0
- package/dist/types/core/tree/index.d.ts +3 -2
- package/dist/types/core/tree/index.d.ts.map +1 -1
- package/dist/types/core/types.d.ts +3 -1
- package/dist/types/core/types.d.ts.map +1 -1
- package/dist/types/core/utils/iterator-to-sequence.d.ts.map +1 -1
- package/dist/types/index.d.ts +10 -5
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +17 -19
- package/src/core/base-primitives.ts +0 -27
- package/src/core/compat.ts +2 -1
- package/src/core/constants.ts +28 -26
- package/src/core/constructors.ts +269 -0
- package/src/core/context/async-parser.ts +586 -0
- package/src/core/context/deserializer.ts +506 -316
- package/src/core/context/parser.ts +243 -180
- package/src/core/context/serializer.ts +1058 -827
- package/src/core/context/sync-parser.ts +833 -0
- package/src/core/cross/index.ts +63 -47
- package/src/core/errors.ts +73 -12
- package/src/core/function-string.ts +11 -10
- package/src/core/plugin.ts +20 -11
- package/src/core/reference.ts +8 -5
- package/src/core/special-reference.ts +18 -90
- package/src/core/stream.ts +18 -152
- package/src/core/symbols.ts +18 -0
- package/src/core/tree/index.ts +45 -31
- package/src/core/types.ts +7 -2
- package/src/core/utils/iterator-to-sequence.ts +6 -28
- package/src/index.ts +29 -11
- package/dist/types/core/context/parser/async.d.ts +0 -24
- package/dist/types/core/context/parser/async.d.ts.map +0 -1
- package/dist/types/core/context/parser/stream.d.ts +0 -43
- package/dist/types/core/context/parser/stream.d.ts.map +0 -1
- package/dist/types/core/context/parser/sync.d.ts +0 -30
- package/dist/types/core/context/parser/sync.d.ts.map +0 -1
- package/dist/types/core/cross/async.d.ts +0 -8
- package/dist/types/core/cross/async.d.ts.map +0 -1
- package/dist/types/core/cross/deserializer.d.ts +0 -9
- package/dist/types/core/cross/deserializer.d.ts.map +0 -1
- package/dist/types/core/cross/parser.d.ts +0 -6
- package/dist/types/core/cross/parser.d.ts.map +0 -1
- package/dist/types/core/cross/serializer.d.ts +0 -16
- package/dist/types/core/cross/serializer.d.ts.map +0 -1
- package/dist/types/core/cross/stream.d.ts +0 -8
- package/dist/types/core/cross/stream.d.ts.map +0 -1
- package/dist/types/core/cross/sync.d.ts +0 -8
- package/dist/types/core/cross/sync.d.ts.map +0 -1
- package/dist/types/core/tree/async.d.ts +0 -8
- package/dist/types/core/tree/async.d.ts.map +0 -1
- package/dist/types/core/tree/deserializer.d.ts +0 -13
- package/dist/types/core/tree/deserializer.d.ts.map +0 -1
- package/dist/types/core/tree/serializer.d.ts +0 -29
- package/dist/types/core/tree/serializer.d.ts.map +0 -1
- package/dist/types/core/tree/sync.d.ts +0 -8
- package/dist/types/core/tree/sync.d.ts.map +0 -1
- package/dist/types/core/utils/assert.d.ts +0 -2
- package/dist/types/core/utils/assert.d.ts.map +0 -1
- package/dist/types/core/utils/deferred.d.ts +0 -7
- package/dist/types/core/utils/deferred.d.ts.map +0 -1
- package/src/core/context/parser/async.ts +0 -495
- package/src/core/context/parser/stream.ts +0 -323
- package/src/core/context/parser/sync.ts +0 -404
- package/src/core/cross/async.ts +0 -9
- package/src/core/cross/deserializer.ts +0 -16
- package/src/core/cross/parser.ts +0 -7
- package/src/core/cross/serializer.ts +0 -72
- package/src/core/cross/stream.ts +0 -9
- package/src/core/cross/sync.ts +0 -9
- package/src/core/tree/async.ts +0 -9
- package/src/core/tree/deserializer.ts +0 -26
- package/src/core/tree/serializer.ts +0 -98
- package/src/core/tree/sync.ts +0 -9
- package/src/core/utils/assert.ts +0 -5
- package/src/core/utils/deferred.ts +0 -22
package/src/core/cross/async.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import BaseAsyncParserContext from '../context/parser/async';
|
|
2
|
-
import type { SerovalMode } from '../plugin';
|
|
3
|
-
import type { CrossParserContextOptions } from './parser';
|
|
4
|
-
|
|
5
|
-
export type CrossAsyncParserContextOptions = CrossParserContextOptions;
|
|
6
|
-
|
|
7
|
-
export default class CrossAsyncParserContext extends BaseAsyncParserContext {
|
|
8
|
-
readonly mode: SerovalMode = 'cross';
|
|
9
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { BaseDeserializerOptions } from '../context/deserializer';
|
|
2
|
-
import BaseDeserializerContext from '../context/deserializer';
|
|
3
|
-
import type { SerovalMode } from '../plugin';
|
|
4
|
-
|
|
5
|
-
export type CrossDeserializerContextOptions = BaseDeserializerOptions;
|
|
6
|
-
|
|
7
|
-
export default class CrossDeserializerContext extends BaseDeserializerContext {
|
|
8
|
-
readonly mode: SerovalMode = 'cross';
|
|
9
|
-
|
|
10
|
-
assignIndexedValue<T>(index: number, value: T): T {
|
|
11
|
-
if (!this.refs.has(index)) {
|
|
12
|
-
this.refs.set(index, value);
|
|
13
|
-
}
|
|
14
|
-
return value;
|
|
15
|
-
}
|
|
16
|
-
}
|
package/src/core/cross/parser.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { SerovalNodeType } from '../constants';
|
|
2
|
-
import type { BaseSerializerContextOptions } from '../context/serializer';
|
|
3
|
-
import BaseSerializerContext from '../context/serializer';
|
|
4
|
-
import { GLOBAL_CONTEXT_REFERENCES } from '../keys';
|
|
5
|
-
import type { SerovalMode } from '../plugin';
|
|
6
|
-
import { serializeString } from '../string';
|
|
7
|
-
import type { SerovalNode } from '../types';
|
|
8
|
-
import type { CrossContextOptions } from './parser';
|
|
9
|
-
|
|
10
|
-
export interface CrossSerializerContextOptions
|
|
11
|
-
extends BaseSerializerContextOptions,
|
|
12
|
-
CrossContextOptions {}
|
|
13
|
-
|
|
14
|
-
export default class CrossSerializerContext extends BaseSerializerContext {
|
|
15
|
-
readonly mode: SerovalMode = 'cross';
|
|
16
|
-
|
|
17
|
-
scopeId?: string;
|
|
18
|
-
|
|
19
|
-
constructor(options: CrossSerializerContextOptions) {
|
|
20
|
-
super(options);
|
|
21
|
-
this.scopeId = options.scopeId;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
getRefParam(id: number): string {
|
|
25
|
-
return GLOBAL_CONTEXT_REFERENCES + '[' + id + ']';
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
protected assignIndexedValue(index: number, value: string): string {
|
|
29
|
-
// In cross-reference, we have to assume that
|
|
30
|
-
// every reference are going to be referenced
|
|
31
|
-
// in the future, and so we need to store
|
|
32
|
-
// all of it into the reference array.
|
|
33
|
-
return this.getRefParam(index) + '=' + value;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
serializeTop(tree: SerovalNode): string {
|
|
37
|
-
// Get the serialized result
|
|
38
|
-
const result = this.serialize(tree);
|
|
39
|
-
// If the node is a non-reference, return
|
|
40
|
-
// the result immediately
|
|
41
|
-
const id = tree.i;
|
|
42
|
-
if (id == null) {
|
|
43
|
-
return result;
|
|
44
|
-
}
|
|
45
|
-
// Get the patches
|
|
46
|
-
const patches = this.resolvePatches();
|
|
47
|
-
// Get the variable that represents the root
|
|
48
|
-
const ref = this.getRefParam(id);
|
|
49
|
-
// Parameters needed for scoping
|
|
50
|
-
const params = this.scopeId == null ? '' : GLOBAL_CONTEXT_REFERENCES;
|
|
51
|
-
// If there are patches, append it after the result
|
|
52
|
-
const body = patches ? '(' + result + ',' + patches + ref + ')' : result;
|
|
53
|
-
// If there are no params, there's no need to generate a function
|
|
54
|
-
if (params === '') {
|
|
55
|
-
if (tree.t === SerovalNodeType.Object && !patches) {
|
|
56
|
-
return '(' + body + ')';
|
|
57
|
-
}
|
|
58
|
-
return body;
|
|
59
|
-
}
|
|
60
|
-
// Get the arguments for the IIFE
|
|
61
|
-
const args =
|
|
62
|
-
this.scopeId == null
|
|
63
|
-
? '()'
|
|
64
|
-
: '(' +
|
|
65
|
-
GLOBAL_CONTEXT_REFERENCES +
|
|
66
|
-
'["' +
|
|
67
|
-
serializeString(this.scopeId) +
|
|
68
|
-
'"])';
|
|
69
|
-
// Create the IIFE
|
|
70
|
-
return '(' + this.createFunction([params], body) + ')' + args;
|
|
71
|
-
}
|
|
72
|
-
}
|
package/src/core/cross/stream.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { BaseStreamParserContextOptions } from '../context/parser/stream';
|
|
2
|
-
import BaseStreamParserContext from '../context/parser/stream';
|
|
3
|
-
import type { SerovalMode } from '../plugin';
|
|
4
|
-
|
|
5
|
-
export type CrossStreamParserContextOptions = BaseStreamParserContextOptions;
|
|
6
|
-
|
|
7
|
-
export default class CrossStreamParserContext extends BaseStreamParserContext {
|
|
8
|
-
readonly mode: SerovalMode = 'cross';
|
|
9
|
-
}
|
package/src/core/cross/sync.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import BaseSyncParserContext from '../context/parser/sync';
|
|
2
|
-
import type { SerovalMode } from '../plugin';
|
|
3
|
-
import type { CrossParserContextOptions } from './parser';
|
|
4
|
-
|
|
5
|
-
export type CrossSyncParserContextOptions = CrossParserContextOptions;
|
|
6
|
-
|
|
7
|
-
export default class CrossSyncParserContext extends BaseSyncParserContext {
|
|
8
|
-
readonly mode: SerovalMode = 'cross';
|
|
9
|
-
}
|
package/src/core/tree/async.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { BaseParserContextOptions } from '../context/parser';
|
|
2
|
-
import BaseAsyncParserContext from '../context/parser/async';
|
|
3
|
-
import type { SerovalMode } from '../plugin';
|
|
4
|
-
|
|
5
|
-
export type AsyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>;
|
|
6
|
-
|
|
7
|
-
export default class AsyncParserContext extends BaseAsyncParserContext {
|
|
8
|
-
readonly mode: SerovalMode = 'vanilla';
|
|
9
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { BaseDeserializerOptions } from '../context/deserializer';
|
|
2
|
-
import BaseDeserializerContext from '../context/deserializer';
|
|
3
|
-
import type { SerovalMode } from '../plugin';
|
|
4
|
-
|
|
5
|
-
export interface VanillaDeserializerContextOptions
|
|
6
|
-
extends Omit<BaseDeserializerOptions, 'refs'> {
|
|
7
|
-
markedRefs: number[] | Set<number>;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export default class VanillaDeserializerContext extends BaseDeserializerContext {
|
|
11
|
-
readonly mode: SerovalMode = 'vanilla';
|
|
12
|
-
|
|
13
|
-
marked: Set<number>;
|
|
14
|
-
|
|
15
|
-
constructor(options: VanillaDeserializerContextOptions) {
|
|
16
|
-
super(options);
|
|
17
|
-
this.marked = new Set(options.markedRefs);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
assignIndexedValue<T>(index: number, value: T): T {
|
|
21
|
-
if (this.marked.has(index)) {
|
|
22
|
-
this.refs.set(index, value);
|
|
23
|
-
}
|
|
24
|
-
return value;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { SerovalNodeType } from '../constants';
|
|
2
|
-
import type { BaseSerializerContextOptions } from '../context/serializer';
|
|
3
|
-
import BaseSerializerContext from '../context/serializer';
|
|
4
|
-
import { SerovalUnsupportedNodeError } from '../errors';
|
|
5
|
-
import type { SerovalMode } from '../plugin';
|
|
6
|
-
import type {
|
|
7
|
-
SerovalNode,
|
|
8
|
-
SerovalPromiseConstructorNode,
|
|
9
|
-
SerovalPromiseRejectNode,
|
|
10
|
-
SerovalPromiseResolveNode,
|
|
11
|
-
} from '../types';
|
|
12
|
-
import getIdentifier from '../utils/get-identifier';
|
|
13
|
-
|
|
14
|
-
export type VanillaSerializerContextOptions = BaseSerializerContextOptions;
|
|
15
|
-
|
|
16
|
-
export default class VanillaSerializerContext extends BaseSerializerContext {
|
|
17
|
-
readonly mode: SerovalMode = 'vanilla';
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Map tree refs to actual refs
|
|
21
|
-
* @private
|
|
22
|
-
*/
|
|
23
|
-
valid = new Map<number, number>();
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Variables
|
|
27
|
-
* @private
|
|
28
|
-
*/
|
|
29
|
-
vars: string[] = [];
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Creates the reference param (identifier) from the given reference ID
|
|
33
|
-
* Calling this function means the value has been referenced somewhere
|
|
34
|
-
*/
|
|
35
|
-
getRefParam(index: number): string {
|
|
36
|
-
/**
|
|
37
|
-
* Creates a new reference ID from a given reference ID
|
|
38
|
-
* This new reference ID means that the reference itself
|
|
39
|
-
* has been referenced at least once, and is used to generate
|
|
40
|
-
* the variables
|
|
41
|
-
*/
|
|
42
|
-
let actualIndex = this.valid.get(index);
|
|
43
|
-
if (actualIndex == null) {
|
|
44
|
-
actualIndex = this.valid.size;
|
|
45
|
-
this.valid.set(index, actualIndex);
|
|
46
|
-
}
|
|
47
|
-
let identifier = this.vars[actualIndex];
|
|
48
|
-
if (identifier == null) {
|
|
49
|
-
identifier = getIdentifier(actualIndex);
|
|
50
|
-
this.vars[actualIndex] = identifier;
|
|
51
|
-
}
|
|
52
|
-
return identifier;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
protected assignIndexedValue(index: number, value: string): string {
|
|
56
|
-
if (this.isMarked(index)) {
|
|
57
|
-
return this.getRefParam(index) + '=' + value;
|
|
58
|
-
}
|
|
59
|
-
return value;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
protected serializePromiseConstructor(
|
|
63
|
-
node: SerovalPromiseConstructorNode,
|
|
64
|
-
): string {
|
|
65
|
-
throw new SerovalUnsupportedNodeError(node);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
protected serializePromiseResolve(node: SerovalPromiseResolveNode): string {
|
|
69
|
-
throw new SerovalUnsupportedNodeError(node);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
protected serializePromiseReject(node: SerovalPromiseRejectNode): string {
|
|
73
|
-
throw new SerovalUnsupportedNodeError(node);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
serializeTop(tree: SerovalNode): string {
|
|
77
|
-
const result = this.serialize(tree);
|
|
78
|
-
// Shared references detected
|
|
79
|
-
if (tree.i != null && this.vars.length) {
|
|
80
|
-
const patches = this.resolvePatches();
|
|
81
|
-
let body = result;
|
|
82
|
-
if (patches) {
|
|
83
|
-
// Get (or create) a ref from the source
|
|
84
|
-
const index = this.getRefParam(tree.i);
|
|
85
|
-
body = result + ',' + patches + index;
|
|
86
|
-
if (!result.startsWith(index + '=')) {
|
|
87
|
-
body = index + '=' + body;
|
|
88
|
-
}
|
|
89
|
-
body = '(' + body + ')';
|
|
90
|
-
}
|
|
91
|
-
return '(' + this.createFunction(this.vars, body) + ')()';
|
|
92
|
-
}
|
|
93
|
-
if (tree.t === SerovalNodeType.Object) {
|
|
94
|
-
return '(' + result + ')';
|
|
95
|
-
}
|
|
96
|
-
return result;
|
|
97
|
-
}
|
|
98
|
-
}
|
package/src/core/tree/sync.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import BaseSyncParserContext from '../context/parser/sync';
|
|
2
|
-
import type { BaseParserContextOptions } from '../context/parser';
|
|
3
|
-
import type { SerovalMode } from '../plugin';
|
|
4
|
-
|
|
5
|
-
export type SyncParserContextOptions = Omit<BaseParserContextOptions, 'refs'>;
|
|
6
|
-
|
|
7
|
-
export default class SyncParserContext extends BaseSyncParserContext {
|
|
8
|
-
readonly mode: SerovalMode = 'vanilla';
|
|
9
|
-
}
|
package/src/core/utils/assert.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export interface Deferred {
|
|
2
|
-
promise: Promise<unknown>;
|
|
3
|
-
resolve(value: unknown): void;
|
|
4
|
-
reject(value: unknown): void;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function createDeferred(): Deferred {
|
|
8
|
-
let resolve: Deferred['resolve'];
|
|
9
|
-
let reject: Deferred['reject'];
|
|
10
|
-
return {
|
|
11
|
-
promise: new Promise<unknown>((res, rej) => {
|
|
12
|
-
resolve = res;
|
|
13
|
-
reject = rej;
|
|
14
|
-
}),
|
|
15
|
-
resolve(value): void {
|
|
16
|
-
resolve(value);
|
|
17
|
-
},
|
|
18
|
-
reject(value): void {
|
|
19
|
-
reject(value);
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
}
|