seroval 0.7.0 → 0.8.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 +1109 -1039
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -3
- package/dist/esm/development/index.mjs +1109 -1039
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +3 -3
- package/dist/types/assert.d.ts +1 -1
- package/dist/types/context.d.ts +2 -2
- package/dist/types/index.d.ts +3 -4
- package/dist/types/string.d.ts +1 -1
- package/dist/types/tree/UnsupportedTypeError.d.ts +3 -0
- package/dist/types/tree/async.d.ts +3 -3
- package/dist/types/tree/constants.d.ts +11 -0
- package/dist/types/tree/deserialize.d.ts +2 -2
- package/dist/types/tree/primitives.d.ts +9 -15
- package/dist/types/tree/serialize.d.ts +2 -2
- package/dist/types/tree/shared.d.ts +10 -8
- package/dist/types/tree/sync.d.ts +3 -3
- package/dist/types/tree/types.d.ts +64 -71
- package/dist/types/tree/web-api.d.ts +2 -2
- package/dist/types/types.d.ts +4 -4
- package/package.json +10 -8
- package/src/assert.ts +2 -2
- package/src/compat.ts +0 -1
- package/src/context.ts +10 -7
- package/src/get-identifier.ts +1 -1
- package/src/index.ts +35 -26
- package/src/string.ts +2 -2
- package/src/tree/UnsupportedTypeError.ts +8 -0
- package/src/tree/async.ts +245 -233
- package/src/tree/constants.ts +72 -0
- package/src/tree/deserialize.ts +96 -93
- package/src/tree/primitives.ts +81 -134
- package/src/tree/reference.ts +4 -3
- package/src/tree/serialize.ts +251 -197
- package/src/tree/shared.ts +31 -22
- package/src/tree/symbols.ts +13 -13
- package/src/tree/sync.ts +226 -198
- package/src/tree/types.ts +74 -93
- package/src/tree/web-api.ts +10 -7
- package/src/types.ts +7 -3
package/src/tree/primitives.ts
CHANGED
|
@@ -1,143 +1,64 @@
|
|
|
1
1
|
import assert from '../assert';
|
|
2
2
|
import { Feature } from '../compat';
|
|
3
|
-
import { ParserContext
|
|
3
|
+
import type { ParserContext } from '../context';
|
|
4
|
+
import { createIndexedValue } from '../context';
|
|
4
5
|
import { serializeString } from '../string';
|
|
5
|
-
import { BigIntTypedArrayValue, TypedArrayValue } from '../types';
|
|
6
|
-
import
|
|
7
|
-
import { INV_SYMBOL_REF, WellKnownSymbols } from './symbols';
|
|
6
|
+
import type { BigIntTypedArrayValue, TypedArrayValue } from '../types';
|
|
7
|
+
import UnsupportedTypeError from './UnsupportedTypeError';
|
|
8
8
|
import {
|
|
9
|
+
INFINITY_NODE,
|
|
10
|
+
NEG_INFINITY_NODE,
|
|
11
|
+
NAN_NODE,
|
|
12
|
+
NEG_ZERO_NODE,
|
|
13
|
+
} from './constants';
|
|
14
|
+
import { getReferenceID, hasReferenceID } from './reference';
|
|
15
|
+
import type { WellKnownSymbols } from './symbols';
|
|
16
|
+
import { INV_SYMBOL_REF } from './symbols';
|
|
17
|
+
import type {
|
|
9
18
|
SerovalBigIntNode,
|
|
10
19
|
SerovalBigIntTypedArrayNode,
|
|
11
|
-
SerovalBooleanNode,
|
|
12
20
|
SerovalDateNode,
|
|
13
|
-
SerovalInfinityNode,
|
|
14
|
-
SerovalNaNNode,
|
|
15
|
-
SerovalNegativeInfinityNode,
|
|
16
|
-
SerovalNegativeZeroNode,
|
|
17
|
-
SerovalNodeType,
|
|
18
|
-
SerovalNullNode,
|
|
19
|
-
SerovalNumberNode,
|
|
20
21
|
SerovalIndexedValueNode,
|
|
21
22
|
SerovalRegExpNode,
|
|
22
23
|
SerovalStringNode,
|
|
23
24
|
SerovalTypedArrayNode,
|
|
24
|
-
SerovalUndefinedNode,
|
|
25
25
|
SerovalWKSymbolNode,
|
|
26
26
|
SerovalReferenceNode,
|
|
27
27
|
SerovalArrayBufferNode,
|
|
28
28
|
SerovalDataViewNode,
|
|
29
|
+
SerovalNode,
|
|
30
|
+
} from './types';
|
|
31
|
+
import {
|
|
32
|
+
SerovalNodeType,
|
|
29
33
|
} from './types';
|
|
30
34
|
|
|
31
|
-
export
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
s: undefined,
|
|
59
|
-
l: undefined,
|
|
60
|
-
c: undefined,
|
|
61
|
-
m: undefined,
|
|
62
|
-
d: undefined,
|
|
63
|
-
a: undefined,
|
|
64
|
-
f: undefined,
|
|
65
|
-
b: undefined,
|
|
66
|
-
};
|
|
67
|
-
export const NULL_NODE: SerovalNullNode = {
|
|
68
|
-
t: SerovalNodeType.Null,
|
|
69
|
-
i: undefined,
|
|
70
|
-
s: undefined,
|
|
71
|
-
l: undefined,
|
|
72
|
-
c: undefined,
|
|
73
|
-
m: undefined,
|
|
74
|
-
d: undefined,
|
|
75
|
-
a: undefined,
|
|
76
|
-
f: undefined,
|
|
77
|
-
b: undefined,
|
|
78
|
-
};
|
|
79
|
-
export const NEG_ZERO_NODE: SerovalNegativeZeroNode = {
|
|
80
|
-
t: SerovalNodeType.NegativeZero,
|
|
81
|
-
i: undefined,
|
|
82
|
-
s: undefined,
|
|
83
|
-
l: undefined,
|
|
84
|
-
c: undefined,
|
|
85
|
-
m: undefined,
|
|
86
|
-
d: undefined,
|
|
87
|
-
a: undefined,
|
|
88
|
-
f: undefined,
|
|
89
|
-
b: undefined,
|
|
90
|
-
};
|
|
91
|
-
export const INFINITY_NODE: SerovalInfinityNode = {
|
|
92
|
-
t: SerovalNodeType.Infinity,
|
|
93
|
-
i: undefined,
|
|
94
|
-
s: undefined,
|
|
95
|
-
l: undefined,
|
|
96
|
-
c: undefined,
|
|
97
|
-
m: undefined,
|
|
98
|
-
d: undefined,
|
|
99
|
-
a: undefined,
|
|
100
|
-
f: undefined,
|
|
101
|
-
b: undefined,
|
|
102
|
-
};
|
|
103
|
-
export const NEG_INFINITY_NODE: SerovalNegativeInfinityNode = {
|
|
104
|
-
t: SerovalNodeType.NegativeInfinity,
|
|
105
|
-
i: undefined,
|
|
106
|
-
s: undefined,
|
|
107
|
-
l: undefined,
|
|
108
|
-
c: undefined,
|
|
109
|
-
m: undefined,
|
|
110
|
-
d: undefined,
|
|
111
|
-
a: undefined,
|
|
112
|
-
f: undefined,
|
|
113
|
-
b: undefined,
|
|
114
|
-
};
|
|
115
|
-
export const NAN_NODE: SerovalNaNNode = {
|
|
116
|
-
t: SerovalNodeType.NaN,
|
|
117
|
-
i: undefined,
|
|
118
|
-
s: undefined,
|
|
119
|
-
l: undefined,
|
|
120
|
-
c: undefined,
|
|
121
|
-
m: undefined,
|
|
122
|
-
d: undefined,
|
|
123
|
-
a: undefined,
|
|
124
|
-
f: undefined,
|
|
125
|
-
b: undefined,
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
export function createNumberNode(value: number): SerovalNumberNode {
|
|
129
|
-
return {
|
|
130
|
-
t: SerovalNodeType.Number,
|
|
131
|
-
i: undefined,
|
|
132
|
-
s: value,
|
|
133
|
-
l: undefined,
|
|
134
|
-
c: undefined,
|
|
135
|
-
m: undefined,
|
|
136
|
-
d: undefined,
|
|
137
|
-
a: undefined,
|
|
138
|
-
f: undefined,
|
|
139
|
-
b: undefined,
|
|
140
|
-
};
|
|
35
|
+
export function createNumberNode(value: number): SerovalNode {
|
|
36
|
+
switch (value) {
|
|
37
|
+
case Infinity:
|
|
38
|
+
return INFINITY_NODE;
|
|
39
|
+
case -Infinity:
|
|
40
|
+
return NEG_INFINITY_NODE;
|
|
41
|
+
default:
|
|
42
|
+
// eslint-disable-next-line no-self-compare
|
|
43
|
+
if (value !== value) {
|
|
44
|
+
return NAN_NODE;
|
|
45
|
+
}
|
|
46
|
+
if (Object.is(value, -0)) {
|
|
47
|
+
return NEG_ZERO_NODE;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
t: SerovalNodeType.Number,
|
|
51
|
+
i: undefined,
|
|
52
|
+
s: value,
|
|
53
|
+
l: undefined,
|
|
54
|
+
c: undefined,
|
|
55
|
+
m: undefined,
|
|
56
|
+
d: undefined,
|
|
57
|
+
a: undefined,
|
|
58
|
+
f: undefined,
|
|
59
|
+
b: undefined,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
141
62
|
}
|
|
142
63
|
|
|
143
64
|
export function createStringNode(value: string): SerovalStringNode {
|
|
@@ -159,7 +80,7 @@ export function createBigIntNode(
|
|
|
159
80
|
ctx: ParserContext,
|
|
160
81
|
current: bigint,
|
|
161
82
|
): SerovalBigIntNode {
|
|
162
|
-
assert(ctx.features & Feature.BigInt,
|
|
83
|
+
assert(ctx.features & Feature.BigInt, new UnsupportedTypeError(current));
|
|
163
84
|
return {
|
|
164
85
|
t: SerovalNodeType.BigInt,
|
|
165
86
|
i: undefined,
|
|
@@ -246,7 +167,7 @@ export function createArrayBufferNode(
|
|
|
246
167
|
export function serializeArrayBuffer(
|
|
247
168
|
ctx: ParserContext,
|
|
248
169
|
current: ArrayBuffer,
|
|
249
|
-
) {
|
|
170
|
+
): SerovalNode {
|
|
250
171
|
const id = createIndexedValue(ctx, current);
|
|
251
172
|
if (ctx.markedRefs.has(id)) {
|
|
252
173
|
return createIndexedValueNode(id);
|
|
@@ -259,14 +180,13 @@ export function createTypedArrayNode(
|
|
|
259
180
|
id: number,
|
|
260
181
|
current: TypedArrayValue,
|
|
261
182
|
): SerovalTypedArrayNode {
|
|
262
|
-
|
|
263
|
-
assert(ctx.features & Feature.TypedArray, `Unsupported value type "${constructor}"`);
|
|
183
|
+
assert(ctx.features & Feature.TypedArray, new UnsupportedTypeError(current));
|
|
264
184
|
return {
|
|
265
185
|
t: SerovalNodeType.TypedArray,
|
|
266
186
|
i: id,
|
|
267
187
|
s: undefined,
|
|
268
188
|
l: current.length,
|
|
269
|
-
c: constructor,
|
|
189
|
+
c: current.constructor.name,
|
|
270
190
|
m: undefined,
|
|
271
191
|
d: undefined,
|
|
272
192
|
a: undefined,
|
|
@@ -282,17 +202,16 @@ export function createBigIntTypedArrayNode(
|
|
|
282
202
|
id: number,
|
|
283
203
|
current: BigIntTypedArrayValue,
|
|
284
204
|
): SerovalBigIntTypedArrayNode {
|
|
285
|
-
const constructor = current.constructor.name;
|
|
286
205
|
assert(
|
|
287
206
|
(ctx.features & BIGINT_FLAG) === BIGINT_FLAG,
|
|
288
|
-
|
|
207
|
+
new UnsupportedTypeError(current),
|
|
289
208
|
);
|
|
290
209
|
return {
|
|
291
210
|
t: SerovalNodeType.BigIntTypedArray,
|
|
292
211
|
i: id,
|
|
293
212
|
s: undefined,
|
|
294
213
|
l: current.length,
|
|
295
|
-
c: constructor,
|
|
214
|
+
c: current.constructor.name,
|
|
296
215
|
m: undefined,
|
|
297
216
|
d: undefined,
|
|
298
217
|
a: undefined,
|
|
@@ -303,13 +222,14 @@ export function createBigIntTypedArrayNode(
|
|
|
303
222
|
|
|
304
223
|
export function createWKSymbolNode(
|
|
305
224
|
ctx: ParserContext,
|
|
225
|
+
id: number,
|
|
306
226
|
current: WellKnownSymbols,
|
|
307
227
|
): SerovalWKSymbolNode {
|
|
308
|
-
assert(ctx.features & Feature.Symbol,
|
|
309
|
-
assert(current in INV_SYMBOL_REF, '
|
|
228
|
+
assert(ctx.features & Feature.Symbol, new UnsupportedTypeError(current));
|
|
229
|
+
assert(current in INV_SYMBOL_REF, new Error('Only well-known symbols are supported.'));
|
|
310
230
|
return {
|
|
311
231
|
t: SerovalNodeType.WKSymbol,
|
|
312
|
-
i:
|
|
232
|
+
i: id,
|
|
313
233
|
s: INV_SYMBOL_REF[current],
|
|
314
234
|
l: undefined,
|
|
315
235
|
c: undefined,
|
|
@@ -357,3 +277,30 @@ export function createDataViewNode(
|
|
|
357
277
|
b: current.byteOffset,
|
|
358
278
|
};
|
|
359
279
|
}
|
|
280
|
+
|
|
281
|
+
export function createSymbolNode(
|
|
282
|
+
ctx: ParserContext,
|
|
283
|
+
current: symbol,
|
|
284
|
+
): SerovalNode {
|
|
285
|
+
const id = createIndexedValue(ctx, current);
|
|
286
|
+
if (ctx.markedRefs.has(id)) {
|
|
287
|
+
return createIndexedValueNode(id);
|
|
288
|
+
}
|
|
289
|
+
if (hasReferenceID(current)) {
|
|
290
|
+
return createReferenceNode(id, current);
|
|
291
|
+
}
|
|
292
|
+
return createWKSymbolNode(ctx, id, current as WellKnownSymbols);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export function createFunctionNode(
|
|
296
|
+
ctx: ParserContext,
|
|
297
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
298
|
+
current: Function,
|
|
299
|
+
): SerovalNode {
|
|
300
|
+
assert(hasReferenceID(current), new Error('Cannot serialize function without reference ID.'));
|
|
301
|
+
const id = createIndexedValue(ctx, current);
|
|
302
|
+
if (ctx.markedRefs.has(id)) {
|
|
303
|
+
return createIndexedValueNode(id);
|
|
304
|
+
}
|
|
305
|
+
return createReferenceNode(id, current);
|
|
306
|
+
}
|
package/src/tree/reference.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
|
|
1
2
|
/* eslint-disable no-restricted-globals */
|
|
2
3
|
import assert from '../assert';
|
|
3
4
|
|
|
@@ -19,12 +20,12 @@ export function hasReference(id: string): boolean {
|
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
export function getReferenceID<T>(value: T): string {
|
|
22
|
-
assert(hasReferenceID(value), 'Missing reference id');
|
|
23
|
-
return REFERENCE.get(value)
|
|
23
|
+
assert(hasReferenceID(value), new Error('Missing reference id'));
|
|
24
|
+
return REFERENCE.get(value)!;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
export function getReference<T>(id: string): T {
|
|
27
|
-
assert(hasReference(id), 'Missing reference for id:' + id);
|
|
28
|
+
assert(hasReference(id), new Error('Missing reference for id:' + id));
|
|
28
29
|
return INV_REFERENCE.get(id) as T;
|
|
29
30
|
}
|
|
30
31
|
|