seroval 0.9.0 → 0.10.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.
- package/README.md +4 -287
- package/dist/cjs/development/index.cjs +3529 -1140
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -3
- package/dist/esm/development/index.mjs +3533 -1142
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +3 -3
- package/dist/types/core/Serializer.d.ts +15 -0
- package/dist/types/{tree → core}/UnsupportedTypeError.d.ts +4 -3
- package/dist/types/{assert.d.ts → core/assert.d.ts} +1 -1
- package/dist/types/core/assignments.d.ts +33 -0
- package/dist/types/core/base-primitives.d.ts +12 -0
- package/dist/types/{compat.d.ts → core/compat.d.ts} +22 -22
- package/dist/types/core/constants.d.ts +89 -0
- package/dist/types/core/context.d.ts +9 -0
- package/dist/types/core/cross/async.d.ts +3 -0
- package/dist/types/core/cross/context.d.ts +37 -0
- package/dist/types/core/cross/index.d.ts +8 -0
- package/dist/types/core/cross/primitives.d.ts +9 -0
- package/dist/types/core/cross/serialize.d.ts +5 -0
- package/dist/types/core/cross/stream.d.ts +3 -0
- package/dist/types/core/cross/sync.d.ts +3 -0
- package/dist/types/core/cross/web-api.d.ts +4 -0
- package/dist/types/{get-identifier.d.ts → core/get-identifier.d.ts} +1 -1
- package/dist/types/core/keys.d.ts +13 -0
- package/dist/types/{tree/constants.d.ts → core/literals.d.ts} +9 -11
- package/dist/types/core/promise-to-result.d.ts +1 -0
- package/dist/types/{tree → core}/reference.d.ts +5 -6
- package/dist/types/{tree → core}/shared.d.ts +13 -13
- package/dist/types/{string.d.ts → core/string.d.ts} +3 -3
- package/dist/types/core/tree/async.d.ts +3 -0
- package/dist/types/core/tree/context.d.ts +44 -0
- package/dist/types/core/tree/deserialize.d.ts +3 -0
- package/dist/types/core/tree/index.d.ts +14 -0
- package/dist/types/core/tree/primitives.d.ts +9 -0
- package/dist/types/core/tree/serialize.d.ts +4 -0
- package/dist/types/core/tree/sync.d.ts +3 -0
- package/dist/types/core/tree/web-api.d.ts +4 -0
- package/dist/types/{tree → core}/types.d.ts +228 -238
- package/dist/types/core/web-api.d.ts +4 -0
- package/dist/types/index.d.ts +9 -19
- package/dist/types/types.d.ts +20 -20
- package/package.json +5 -5
- package/src/core/Serializer.ts +48 -0
- package/src/{tree → core}/UnsupportedTypeError.ts +1 -1
- package/src/core/assignments.ts +159 -0
- package/src/{tree/primitives.ts → core/base-primitives.ts} +33 -136
- package/src/core/constants.ts +169 -0
- package/src/core/context.ts +15 -0
- package/src/core/cross/async.ts +614 -0
- package/src/core/cross/context.ts +102 -0
- package/src/core/cross/index.ts +169 -0
- package/src/core/cross/primitives.ts +137 -0
- package/src/core/cross/serialize.ts +847 -0
- package/src/core/cross/stream.ts +745 -0
- package/src/core/cross/sync.ts +561 -0
- package/src/core/cross/web-api.ts +55 -0
- package/src/core/header.js +61 -0
- package/src/core/keys.ts +41 -0
- package/src/core/literals.ts +28 -0
- package/src/core/promise-to-result.ts +9 -0
- package/src/{tree → core}/reference.ts +6 -7
- package/src/{tree → core}/shared.ts +4 -4
- package/src/{tree → core/tree}/async.ts +46 -34
- package/src/core/tree/context.ts +125 -0
- package/src/{tree → core/tree}/deserialize.ts +68 -55
- package/src/core/tree/index.ts +142 -0
- package/src/core/tree/primitives.ts +132 -0
- package/src/{tree → core/tree}/serialize.ts +87 -193
- package/src/{tree → core/tree}/sync.ts +50 -39
- package/src/{tree → core/tree}/web-api.ts +8 -52
- package/src/{tree → core}/types.ts +76 -60
- package/src/core/web-api.ts +51 -0
- package/src/index.ts +7 -141
- package/src/types.ts +1 -1
- package/dist/types/context.d.ts +0 -67
- package/dist/types/tree/async.d.ts +0 -3
- package/dist/types/tree/deserialize.d.ts +0 -3
- package/dist/types/tree/primitives.d.ts +0 -19
- package/dist/types/tree/serialize.d.ts +0 -4
- package/dist/types/tree/symbols.d.ts +0 -33
- package/dist/types/tree/sync.d.ts +0 -3
- package/dist/types/tree/web-api.d.ts +0 -6
- package/src/context.ts +0 -164
- package/src/tree/constants.ts +0 -73
- package/src/tree/symbols.ts +0 -65
- /package/src/{assert.ts → core/assert.ts} +0 -0
- /package/src/{compat.ts → core/compat.ts} +0 -0
- /package/src/{get-identifier.ts → core/get-identifier.ts} +0 -0
- /package/src/{string.ts → core/string.ts} +0 -0
package/src/index.ts
CHANGED
|
@@ -1,21 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import { Feature } from './compat';
|
|
3
|
-
import type {
|
|
4
|
-
SerializationContext,
|
|
5
|
-
Options,
|
|
6
|
-
} from './context';
|
|
7
|
-
import {
|
|
8
|
-
getRefParam,
|
|
9
|
-
createParserContext,
|
|
10
|
-
createSerializationContext,
|
|
11
|
-
getRootID,
|
|
12
|
-
} from './context';
|
|
13
|
-
import parseSync from './tree/sync';
|
|
14
|
-
import parseAsync from './tree/async';
|
|
15
|
-
import deserializeTree from './tree/deserialize';
|
|
16
|
-
import serializeTree, { resolvePatches } from './tree/serialize';
|
|
17
|
-
import type { SerovalNode } from './tree/types';
|
|
18
|
-
import { SerovalNodeType } from './tree/types';
|
|
1
|
+
import { serialize } from './core/tree';
|
|
19
2
|
|
|
20
3
|
export type {
|
|
21
4
|
AsyncServerValue,
|
|
@@ -25,131 +8,14 @@ export type {
|
|
|
25
8
|
SemiPrimitiveValue,
|
|
26
9
|
ErrorValue,
|
|
27
10
|
} from './types';
|
|
28
|
-
export { Feature } from './compat';
|
|
11
|
+
export { Feature } from './core/compat';
|
|
12
|
+
export { createReference } from './core/reference';
|
|
29
13
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
rootID: number,
|
|
33
|
-
isObject: boolean,
|
|
34
|
-
result: string,
|
|
35
|
-
): string {
|
|
36
|
-
// Shared references detected
|
|
37
|
-
if (ctx.vars.length) {
|
|
38
|
-
const patches = resolvePatches(ctx);
|
|
39
|
-
let body = result;
|
|
40
|
-
if (patches) {
|
|
41
|
-
// Get (or create) a ref from the source
|
|
42
|
-
const index = getRefParam(ctx, rootID);
|
|
43
|
-
body = result + ',' + patches + index;
|
|
44
|
-
if (!result.startsWith(index + '=')) {
|
|
45
|
-
body = index + '=' + body;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
let params = ctx.vars.length > 1
|
|
49
|
-
? ctx.vars.join(',')
|
|
50
|
-
: ctx.vars[0];
|
|
51
|
-
// Source is probably already assigned
|
|
52
|
-
if (ctx.features & Feature.ArrowFunction) {
|
|
53
|
-
params = ctx.vars.length > 1 || ctx.vars.length === 0
|
|
54
|
-
? '(' + params + ')'
|
|
55
|
-
: params;
|
|
56
|
-
return '(' + params + '=>(' + body + '))()';
|
|
57
|
-
}
|
|
58
|
-
return '(function(' + params + '){return ' + body + '})()';
|
|
59
|
-
}
|
|
60
|
-
if (isObject) {
|
|
61
|
-
return '(' + result + ')';
|
|
62
|
-
}
|
|
63
|
-
return result;
|
|
64
|
-
}
|
|
14
|
+
export * from './core/tree';
|
|
15
|
+
export * from './core/cross';
|
|
65
16
|
|
|
66
|
-
export
|
|
67
|
-
source: T,
|
|
68
|
-
options?: Partial<Options>,
|
|
69
|
-
): string {
|
|
70
|
-
const ctx = createParserContext(options);
|
|
71
|
-
const tree = parseSync(ctx, source);
|
|
72
|
-
const serial = createSerializationContext(ctx);
|
|
73
|
-
const result = serializeTree(serial, tree);
|
|
74
|
-
return finalize(
|
|
75
|
-
serial,
|
|
76
|
-
getRootID(ctx, source),
|
|
77
|
-
tree.t === SerovalNodeType.Object,
|
|
78
|
-
result,
|
|
79
|
-
);
|
|
80
|
-
}
|
|
17
|
+
export { GLOBAL_CONTEXT_API_SCRIPT, getCrossReferenceHeader } from './core/keys';
|
|
81
18
|
|
|
82
|
-
export
|
|
83
|
-
source: T,
|
|
84
|
-
options?: Partial<Options>,
|
|
85
|
-
): Promise<string> {
|
|
86
|
-
const ctx = createParserContext(options);
|
|
87
|
-
const tree = await parseAsync(ctx, source);
|
|
88
|
-
const serial = createSerializationContext(ctx);
|
|
89
|
-
const result = serializeTree(serial, tree);
|
|
90
|
-
return finalize(
|
|
91
|
-
serial,
|
|
92
|
-
getRootID(ctx, source),
|
|
93
|
-
tree.t === SerovalNodeType.Object,
|
|
94
|
-
result,
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function deserialize<T>(source: string): T {
|
|
99
|
-
// eslint-disable-next-line no-eval
|
|
100
|
-
return (0, eval)(source) as T;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export interface SerovalJSON {
|
|
104
|
-
t: SerovalNode;
|
|
105
|
-
r: number;
|
|
106
|
-
f: number;
|
|
107
|
-
m: number[];
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export function toJSON<T>(
|
|
111
|
-
source: T,
|
|
112
|
-
options?: Partial<Options>,
|
|
113
|
-
): SerovalJSON {
|
|
114
|
-
const ctx = createParserContext(options);
|
|
115
|
-
return {
|
|
116
|
-
t: parseSync(ctx, source),
|
|
117
|
-
r: getRootID(ctx, source),
|
|
118
|
-
f: ctx.features,
|
|
119
|
-
m: Array.from(ctx.markedRefs),
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export async function toJSONAsync<T>(
|
|
124
|
-
source: T,
|
|
125
|
-
options?: Partial<Options>,
|
|
126
|
-
): Promise<SerovalJSON> {
|
|
127
|
-
const ctx = createParserContext(options);
|
|
128
|
-
return {
|
|
129
|
-
t: await parseAsync(ctx, source),
|
|
130
|
-
r: getRootID(ctx, source),
|
|
131
|
-
f: ctx.features,
|
|
132
|
-
m: Array.from(ctx.markedRefs),
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export function compileJSON(source: SerovalJSON): string {
|
|
137
|
-
const serial = createSerializationContext({
|
|
138
|
-
features: source.f,
|
|
139
|
-
markedRefs: source.m,
|
|
140
|
-
});
|
|
141
|
-
const result = serializeTree(serial, source.t);
|
|
142
|
-
return finalize(serial, source.r, source.t.i === SerovalNodeType.Object, result);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export function fromJSON<T>(source: SerovalJSON): T {
|
|
146
|
-
const serial = createSerializationContext({
|
|
147
|
-
features: source.f,
|
|
148
|
-
markedRefs: source.m,
|
|
149
|
-
});
|
|
150
|
-
return deserializeTree(serial, source.t) as T;
|
|
151
|
-
}
|
|
19
|
+
export { default as Serializer } from './core/Serializer';
|
|
152
20
|
|
|
153
21
|
export default serialize;
|
|
154
|
-
|
|
155
|
-
export { createReference } from './tree/reference';
|
package/src/types.ts
CHANGED
package/dist/types/context.d.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import type { SerovalObjectFlags } from './tree/types';
|
|
2
|
-
interface IndexAssignment {
|
|
3
|
-
t: 'index';
|
|
4
|
-
s: string;
|
|
5
|
-
k: undefined;
|
|
6
|
-
v: string;
|
|
7
|
-
}
|
|
8
|
-
interface SetAssignment {
|
|
9
|
-
t: 'set';
|
|
10
|
-
s: string;
|
|
11
|
-
k: string;
|
|
12
|
-
v: string;
|
|
13
|
-
}
|
|
14
|
-
interface AddAssignment {
|
|
15
|
-
t: 'add';
|
|
16
|
-
s: string;
|
|
17
|
-
k: undefined;
|
|
18
|
-
v: string;
|
|
19
|
-
}
|
|
20
|
-
interface AppendAssignment {
|
|
21
|
-
t: 'append';
|
|
22
|
-
s: string;
|
|
23
|
-
k: string;
|
|
24
|
-
v: string;
|
|
25
|
-
}
|
|
26
|
-
export type Assignment = IndexAssignment | AddAssignment | SetAssignment | AppendAssignment;
|
|
27
|
-
export interface ParserContext {
|
|
28
|
-
refs: Map<unknown, number>;
|
|
29
|
-
markedRefs: Set<number>;
|
|
30
|
-
features: number;
|
|
31
|
-
}
|
|
32
|
-
export interface FlaggedObject {
|
|
33
|
-
type: SerovalObjectFlags;
|
|
34
|
-
value: string;
|
|
35
|
-
}
|
|
36
|
-
export interface SerializationContext {
|
|
37
|
-
stack: number[];
|
|
38
|
-
validRefs: (number | undefined)[];
|
|
39
|
-
refSize: number;
|
|
40
|
-
markedRefs: Set<number>;
|
|
41
|
-
vars: (string | undefined)[];
|
|
42
|
-
assignments: Assignment[];
|
|
43
|
-
features: number;
|
|
44
|
-
valueMap: Map<number, unknown>;
|
|
45
|
-
flags: FlaggedObject[];
|
|
46
|
-
}
|
|
47
|
-
export interface Options {
|
|
48
|
-
disabledFeatures: number;
|
|
49
|
-
}
|
|
50
|
-
export declare function createParserContext(options?: Partial<Options>): ParserContext;
|
|
51
|
-
export interface SerializationOptions {
|
|
52
|
-
markedRefs: number[] | Set<number>;
|
|
53
|
-
features: number;
|
|
54
|
-
}
|
|
55
|
-
export declare function createSerializationContext(options: SerializationOptions): SerializationContext;
|
|
56
|
-
/**
|
|
57
|
-
* Increments the number of references the referenced value has
|
|
58
|
-
*/
|
|
59
|
-
export declare function markRef(ctx: ParserContext | SerializationContext, current: number): void;
|
|
60
|
-
/**
|
|
61
|
-
* Creates the reference param (identifier) from the given reference ID
|
|
62
|
-
* Calling this function means the value has been referenced somewhere
|
|
63
|
-
*/
|
|
64
|
-
export declare function getRefParam(ctx: SerializationContext, index: number): string;
|
|
65
|
-
export declare function getRootID<T>(ctx: ParserContext, current: T): number;
|
|
66
|
-
export declare function createIndexedValue<T>(ctx: ParserContext, current: T): number;
|
|
67
|
-
export {};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { ParserContext } from '../context';
|
|
2
|
-
import type { BigIntTypedArrayValue, TypedArrayValue } from '../types';
|
|
3
|
-
import type { WellKnownSymbols } from './symbols';
|
|
4
|
-
import type { SerovalBigIntNode, SerovalBigIntTypedArrayNode, SerovalDateNode, SerovalIndexedValueNode, SerovalRegExpNode, SerovalStringNode, SerovalTypedArrayNode, SerovalWKSymbolNode, SerovalReferenceNode, SerovalArrayBufferNode, SerovalDataViewNode, SerovalNode } from './types';
|
|
5
|
-
export declare function createNumberNode(value: number): SerovalNode;
|
|
6
|
-
export declare function createStringNode(value: string): SerovalStringNode;
|
|
7
|
-
export declare function createBigIntNode(ctx: ParserContext, current: bigint): SerovalBigIntNode;
|
|
8
|
-
export declare function createIndexedValueNode(id: number): SerovalIndexedValueNode;
|
|
9
|
-
export declare function createDateNode(id: number, current: Date): SerovalDateNode;
|
|
10
|
-
export declare function createRegExpNode(id: number, current: RegExp): SerovalRegExpNode;
|
|
11
|
-
export declare function createArrayBufferNode(id: number, current: ArrayBuffer): SerovalArrayBufferNode;
|
|
12
|
-
export declare function serializeArrayBuffer(ctx: ParserContext, current: ArrayBuffer): SerovalNode;
|
|
13
|
-
export declare function createTypedArrayNode(ctx: ParserContext, id: number, current: TypedArrayValue): SerovalTypedArrayNode;
|
|
14
|
-
export declare function createBigIntTypedArrayNode(ctx: ParserContext, id: number, current: BigIntTypedArrayValue): SerovalBigIntTypedArrayNode;
|
|
15
|
-
export declare function createWKSymbolNode(ctx: ParserContext, id: number, current: WellKnownSymbols): SerovalWKSymbolNode;
|
|
16
|
-
export declare function createReferenceNode<T>(id: number, ref: T): SerovalReferenceNode;
|
|
17
|
-
export declare function createDataViewNode(ctx: ParserContext, id: number, current: DataView): SerovalDataViewNode;
|
|
18
|
-
export declare function createSymbolNode(ctx: ParserContext, current: symbol): SerovalNode;
|
|
19
|
-
export declare function createFunctionNode(ctx: ParserContext, current: Function): SerovalNode;
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { SerializationContext } from '../context';
|
|
2
|
-
import type { SerovalNode } from './types';
|
|
3
|
-
export declare function resolvePatches(ctx: SerializationContext): string | undefined;
|
|
4
|
-
export default function serializeTree(ctx: SerializationContext, node: SerovalNode): string;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
export declare const enum Symbols {
|
|
2
|
-
AsyncIterator = 0,
|
|
3
|
-
HasInstance = 1,
|
|
4
|
-
IsConcatSpreadable = 2,
|
|
5
|
-
Iterator = 3,
|
|
6
|
-
Match = 4,
|
|
7
|
-
MatchAll = 5,
|
|
8
|
-
Replace = 6,
|
|
9
|
-
Search = 7,
|
|
10
|
-
Species = 8,
|
|
11
|
-
Split = 9,
|
|
12
|
-
ToPrimitive = 10,
|
|
13
|
-
ToStringTag = 11,
|
|
14
|
-
Unscopables = 12
|
|
15
|
-
}
|
|
16
|
-
export declare const SYMBOL_STRING: Record<Symbols, string>;
|
|
17
|
-
export declare const INV_SYMBOL_REF: {
|
|
18
|
-
[Symbol.asyncIterator]: Symbols;
|
|
19
|
-
[Symbol.hasInstance]: Symbols;
|
|
20
|
-
[Symbol.isConcatSpreadable]: Symbols;
|
|
21
|
-
[Symbol.iterator]: Symbols;
|
|
22
|
-
[Symbol.match]: Symbols;
|
|
23
|
-
[Symbol.matchAll]: Symbols;
|
|
24
|
-
[Symbol.replace]: Symbols;
|
|
25
|
-
[Symbol.search]: Symbols;
|
|
26
|
-
[Symbol.species]: Symbols;
|
|
27
|
-
[Symbol.split]: Symbols;
|
|
28
|
-
[Symbol.toPrimitive]: Symbols;
|
|
29
|
-
[Symbol.toStringTag]: Symbols;
|
|
30
|
-
[Symbol.unscopables]: Symbols;
|
|
31
|
-
};
|
|
32
|
-
export type WellKnownSymbols = keyof typeof INV_SYMBOL_REF;
|
|
33
|
-
export declare const SYMBOL_REF: Record<Symbols, WellKnownSymbols>;
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { ParserContext } from '../context';
|
|
2
|
-
import type { SerovalBlobNode, SerovalFileNode, SerovalURLNode, SerovalURLSearchParamsNode } from './types';
|
|
3
|
-
export declare function createURLNode(ctx: ParserContext, id: number, current: URL): SerovalURLNode;
|
|
4
|
-
export declare function createURLSearchParamsNode(ctx: ParserContext, id: number, current: URLSearchParams): SerovalURLSearchParamsNode;
|
|
5
|
-
export declare function createBlobNode(ctx: ParserContext, id: number, current: Blob): Promise<SerovalBlobNode>;
|
|
6
|
-
export declare function createFileNode(ctx: ParserContext, id: number, current: File): Promise<SerovalFileNode>;
|
package/src/context.ts
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import { ALL_ENABLED } from './compat';
|
|
2
|
-
import getIdentifier from './get-identifier';
|
|
3
|
-
import type { SerovalObjectFlags } from './tree/types';
|
|
4
|
-
|
|
5
|
-
interface IndexAssignment {
|
|
6
|
-
t: 'index';
|
|
7
|
-
s: string;
|
|
8
|
-
k: undefined;
|
|
9
|
-
v: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
interface SetAssignment {
|
|
13
|
-
t: 'set';
|
|
14
|
-
s: string;
|
|
15
|
-
k: string;
|
|
16
|
-
v: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
interface AddAssignment {
|
|
20
|
-
t: 'add';
|
|
21
|
-
s: string;
|
|
22
|
-
k: undefined;
|
|
23
|
-
v: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface AppendAssignment {
|
|
27
|
-
t: 'append';
|
|
28
|
-
s: string;
|
|
29
|
-
k: string;
|
|
30
|
-
v: string;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Array of assignments to be done (used for recursion)
|
|
34
|
-
export type Assignment =
|
|
35
|
-
| IndexAssignment
|
|
36
|
-
| AddAssignment
|
|
37
|
-
| SetAssignment
|
|
38
|
-
| AppendAssignment;
|
|
39
|
-
|
|
40
|
-
export interface ParserContext {
|
|
41
|
-
refs: Map<unknown, number>;
|
|
42
|
-
markedRefs: Set<number>;
|
|
43
|
-
features: number;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface FlaggedObject {
|
|
47
|
-
type: SerovalObjectFlags;
|
|
48
|
-
value: string;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface SerializationContext {
|
|
52
|
-
stack: number[];
|
|
53
|
-
// Map tree refs to actual refs
|
|
54
|
-
validRefs: (number | undefined)[];
|
|
55
|
-
refSize: number;
|
|
56
|
-
// Refs that are...referenced
|
|
57
|
-
markedRefs: Set<number>;
|
|
58
|
-
// Variables
|
|
59
|
-
vars: (string | undefined)[];
|
|
60
|
-
// Array of assignments to be done (used for recursion)
|
|
61
|
-
assignments: Assignment[];
|
|
62
|
-
// Supported features
|
|
63
|
-
features: number;
|
|
64
|
-
|
|
65
|
-
valueMap: Map<number, unknown>;
|
|
66
|
-
|
|
67
|
-
// Object flags
|
|
68
|
-
flags: FlaggedObject[];
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export interface Options {
|
|
72
|
-
disabledFeatures: number;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const DEFAULT_OPTIONS: Options = {
|
|
76
|
-
disabledFeatures: 0,
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
export function createParserContext(options: Partial<Options> = {}): ParserContext {
|
|
80
|
-
// eslint-disable-next-line prefer-object-spread
|
|
81
|
-
const result = Object.assign({}, DEFAULT_OPTIONS, options);
|
|
82
|
-
return {
|
|
83
|
-
markedRefs: new Set(),
|
|
84
|
-
refs: new Map(),
|
|
85
|
-
features: ALL_ENABLED ^ result.disabledFeatures,
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export interface SerializationOptions {
|
|
90
|
-
markedRefs: number[] | Set<number>;
|
|
91
|
-
features: number;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function createSerializationContext(options: SerializationOptions): SerializationContext {
|
|
95
|
-
return {
|
|
96
|
-
stack: [],
|
|
97
|
-
vars: [],
|
|
98
|
-
assignments: [],
|
|
99
|
-
validRefs: [],
|
|
100
|
-
refSize: 0,
|
|
101
|
-
features: options.features,
|
|
102
|
-
markedRefs: new Set(options.markedRefs),
|
|
103
|
-
valueMap: new Map(),
|
|
104
|
-
flags: [],
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Increments the number of references the referenced value has
|
|
110
|
-
*/
|
|
111
|
-
export function markRef(
|
|
112
|
-
ctx: ParserContext | SerializationContext,
|
|
113
|
-
current: number,
|
|
114
|
-
): void {
|
|
115
|
-
ctx.markedRefs.add(current);
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Creates the reference param (identifier) from the given reference ID
|
|
119
|
-
* Calling this function means the value has been referenced somewhere
|
|
120
|
-
*/
|
|
121
|
-
export function getRefParam(ctx: SerializationContext, index: number): string {
|
|
122
|
-
/**
|
|
123
|
-
* Creates a new reference ID from a given reference ID
|
|
124
|
-
* This new reference ID means that the reference itself
|
|
125
|
-
* has been referenced at least once, and is used to generate
|
|
126
|
-
* the variables
|
|
127
|
-
*/
|
|
128
|
-
let actualIndex = ctx.validRefs[index];
|
|
129
|
-
if (actualIndex == null) {
|
|
130
|
-
actualIndex = ctx.refSize++;
|
|
131
|
-
ctx.validRefs[index] = actualIndex;
|
|
132
|
-
}
|
|
133
|
-
let identifier = ctx.vars[actualIndex];
|
|
134
|
-
if (identifier == null) {
|
|
135
|
-
identifier = getIdentifier(actualIndex);
|
|
136
|
-
ctx.vars[actualIndex] = identifier;
|
|
137
|
-
}
|
|
138
|
-
return identifier;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export function getRootID<T>(
|
|
142
|
-
ctx: ParserContext,
|
|
143
|
-
current: T,
|
|
144
|
-
): number {
|
|
145
|
-
const ref = ctx.refs.get(current);
|
|
146
|
-
if (ref == null) {
|
|
147
|
-
return ctx.refs.size;
|
|
148
|
-
}
|
|
149
|
-
return ref;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export function createIndexedValue<T>(
|
|
153
|
-
ctx: ParserContext,
|
|
154
|
-
current: T,
|
|
155
|
-
): number {
|
|
156
|
-
const ref = ctx.refs.get(current);
|
|
157
|
-
if (ref == null) {
|
|
158
|
-
const id = ctx.refs.size;
|
|
159
|
-
ctx.refs.set(current, id);
|
|
160
|
-
return id;
|
|
161
|
-
}
|
|
162
|
-
markRef(ctx, ref);
|
|
163
|
-
return ref;
|
|
164
|
-
}
|
package/src/tree/constants.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import type { SerovalConstantNode } from './types';
|
|
2
|
-
import { SerovalConstant, SerovalNodeType } from './types';
|
|
3
|
-
|
|
4
|
-
function createConstantNode(value: SerovalConstant): SerovalConstantNode {
|
|
5
|
-
return {
|
|
6
|
-
t: SerovalNodeType.Constant,
|
|
7
|
-
i: undefined,
|
|
8
|
-
s: value,
|
|
9
|
-
l: undefined,
|
|
10
|
-
c: undefined,
|
|
11
|
-
m: undefined,
|
|
12
|
-
d: undefined,
|
|
13
|
-
a: undefined,
|
|
14
|
-
f: undefined,
|
|
15
|
-
b: undefined,
|
|
16
|
-
o: undefined,
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const TRUE_NODE = createConstantNode(SerovalConstant.True);
|
|
21
|
-
export const FALSE_NODE = createConstantNode(SerovalConstant.False);
|
|
22
|
-
export const UNDEFINED_NODE = createConstantNode(SerovalConstant.Undefined);
|
|
23
|
-
export const NULL_NODE = createConstantNode(SerovalConstant.Null);
|
|
24
|
-
export const NEG_ZERO_NODE = createConstantNode(SerovalConstant.NegativeZero);
|
|
25
|
-
export const INFINITY_NODE = createConstantNode(SerovalConstant.Infinity);
|
|
26
|
-
export const NEG_INFINITY_NODE = createConstantNode(SerovalConstant.NegativeInfinity);
|
|
27
|
-
export const NAN_NODE = createConstantNode(SerovalConstant.NaN);
|
|
28
|
-
|
|
29
|
-
export function serializeConstant(node: SerovalConstantNode): string {
|
|
30
|
-
switch (node.s) {
|
|
31
|
-
case SerovalConstant.True:
|
|
32
|
-
return '!0';
|
|
33
|
-
case SerovalConstant.False:
|
|
34
|
-
return '!1';
|
|
35
|
-
case SerovalConstant.Undefined:
|
|
36
|
-
return 'void 0';
|
|
37
|
-
case SerovalConstant.Null:
|
|
38
|
-
return 'null';
|
|
39
|
-
case SerovalConstant.NegativeZero:
|
|
40
|
-
return '-0';
|
|
41
|
-
case SerovalConstant.Infinity:
|
|
42
|
-
return '1/0';
|
|
43
|
-
case SerovalConstant.NegativeInfinity:
|
|
44
|
-
return '-1/0';
|
|
45
|
-
case SerovalConstant.NaN:
|
|
46
|
-
return 'NaN';
|
|
47
|
-
default:
|
|
48
|
-
throw new Error('invariant');
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function deserializeConstant(node: SerovalConstantNode): unknown {
|
|
53
|
-
switch (node.s) {
|
|
54
|
-
case SerovalConstant.True:
|
|
55
|
-
return true;
|
|
56
|
-
case SerovalConstant.False:
|
|
57
|
-
return false;
|
|
58
|
-
case SerovalConstant.Undefined:
|
|
59
|
-
return undefined;
|
|
60
|
-
case SerovalConstant.Null:
|
|
61
|
-
return null;
|
|
62
|
-
case SerovalConstant.NegativeZero:
|
|
63
|
-
return -0;
|
|
64
|
-
case SerovalConstant.Infinity:
|
|
65
|
-
return Infinity;
|
|
66
|
-
case SerovalConstant.NegativeInfinity:
|
|
67
|
-
return -Infinity;
|
|
68
|
-
case SerovalConstant.NaN:
|
|
69
|
-
return NaN;
|
|
70
|
-
default:
|
|
71
|
-
throw new Error('invariant');
|
|
72
|
-
}
|
|
73
|
-
}
|
package/src/tree/symbols.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
export const enum Symbols {
|
|
2
|
-
AsyncIterator = 0,
|
|
3
|
-
HasInstance = 1,
|
|
4
|
-
IsConcatSpreadable = 2,
|
|
5
|
-
Iterator = 3,
|
|
6
|
-
Match = 4,
|
|
7
|
-
MatchAll = 5,
|
|
8
|
-
Replace = 6,
|
|
9
|
-
Search = 7,
|
|
10
|
-
Species = 8,
|
|
11
|
-
Split = 9,
|
|
12
|
-
ToPrimitive = 10,
|
|
13
|
-
ToStringTag = 11,
|
|
14
|
-
Unscopables = 12,
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export const SYMBOL_STRING: Record<Symbols, string> = {
|
|
18
|
-
[Symbols.AsyncIterator]: 'Symbol.asyncIterator',
|
|
19
|
-
[Symbols.HasInstance]: 'Symbol.hasInstance',
|
|
20
|
-
[Symbols.IsConcatSpreadable]: 'Symbol.isConcatSpreadable',
|
|
21
|
-
[Symbols.Iterator]: 'Symbol.iterator',
|
|
22
|
-
[Symbols.Match]: 'Symbol.match',
|
|
23
|
-
[Symbols.MatchAll]: 'Symbol.matchAll',
|
|
24
|
-
[Symbols.Replace]: 'Symbol.replace',
|
|
25
|
-
[Symbols.Search]: 'Symbol.search',
|
|
26
|
-
[Symbols.Species]: 'Symbol.species',
|
|
27
|
-
[Symbols.Split]: 'Symbol.split',
|
|
28
|
-
[Symbols.ToPrimitive]: 'Symbol.toPrimitive',
|
|
29
|
-
[Symbols.ToStringTag]: 'Symbol.toStringTag',
|
|
30
|
-
[Symbols.Unscopables]: 'Symbol.unscopables',
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export const INV_SYMBOL_REF = {
|
|
34
|
-
[Symbol.asyncIterator]: Symbols.AsyncIterator,
|
|
35
|
-
[Symbol.hasInstance]: Symbols.HasInstance,
|
|
36
|
-
[Symbol.isConcatSpreadable]: Symbols.IsConcatSpreadable,
|
|
37
|
-
[Symbol.iterator]: Symbols.Iterator,
|
|
38
|
-
[Symbol.match]: Symbols.Match,
|
|
39
|
-
[Symbol.matchAll]: Symbols.MatchAll,
|
|
40
|
-
[Symbol.replace]: Symbols.Replace,
|
|
41
|
-
[Symbol.search]: Symbols.Search,
|
|
42
|
-
[Symbol.species]: Symbols.Species,
|
|
43
|
-
[Symbol.split]: Symbols.Split,
|
|
44
|
-
[Symbol.toPrimitive]: Symbols.ToPrimitive,
|
|
45
|
-
[Symbol.toStringTag]: Symbols.ToStringTag,
|
|
46
|
-
[Symbol.unscopables]: Symbols.Unscopables,
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export type WellKnownSymbols = keyof typeof INV_SYMBOL_REF;
|
|
50
|
-
|
|
51
|
-
export const SYMBOL_REF: Record<Symbols, WellKnownSymbols> = {
|
|
52
|
-
[Symbols.AsyncIterator]: Symbol.asyncIterator,
|
|
53
|
-
[Symbols.HasInstance]: Symbol.hasInstance,
|
|
54
|
-
[Symbols.IsConcatSpreadable]: Symbol.isConcatSpreadable,
|
|
55
|
-
[Symbols.Iterator]: Symbol.iterator,
|
|
56
|
-
[Symbols.Match]: Symbol.match,
|
|
57
|
-
[Symbols.MatchAll]: Symbol.matchAll,
|
|
58
|
-
[Symbols.Replace]: Symbol.replace,
|
|
59
|
-
[Symbols.Search]: Symbol.search,
|
|
60
|
-
[Symbols.Species]: Symbol.species,
|
|
61
|
-
[Symbols.Split]: Symbol.split,
|
|
62
|
-
[Symbols.ToPrimitive]: Symbol.toPrimitive,
|
|
63
|
-
[Symbols.ToStringTag]: Symbol.toStringTag,
|
|
64
|
-
[Symbols.Unscopables]: Symbol.unscopables,
|
|
65
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|