seroval 0.7.0 → 0.9.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 +1247 -1069
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -3
- package/dist/esm/development/index.mjs +1247 -1069
- 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 +8 -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 +12 -8
- package/dist/types/tree/sync.d.ts +3 -3
- package/dist/types/tree/types.d.ts +74 -71
- package/dist/types/tree/web-api.d.ts +2 -2
- package/dist/types/types.d.ts +4 -4
- package/package.json +11 -9
- package/src/assert.ts +2 -2
- package/src/compat.ts +0 -1
- package/src/context.ts +20 -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 +256 -233
- package/src/tree/constants.ts +73 -0
- package/src/tree/deserialize.ts +112 -93
- package/src/tree/primitives.ts +93 -134
- package/src/tree/reference.ts +3 -3
- package/src/tree/serialize.ts +307 -201
- package/src/tree/shared.ts +45 -22
- package/src/tree/symbols.ts +13 -13
- package/src/tree/sync.ts +236 -198
- package/src/tree/types.ts +87 -94
- package/src/tree/web-api.ts +14 -7
- package/src/types.ts +7 -3
- package/src/tree/iterator.ts +0 -14
package/src/tree/primitives.ts
CHANGED
|
@@ -1,143 +1,65 @@
|
|
|
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
|
-
|
|
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
|
+
o: undefined,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
141
63
|
}
|
|
142
64
|
|
|
143
65
|
export function createStringNode(value: string): SerovalStringNode {
|
|
@@ -152,6 +74,7 @@ export function createStringNode(value: string): SerovalStringNode {
|
|
|
152
74
|
a: undefined,
|
|
153
75
|
f: undefined,
|
|
154
76
|
b: undefined,
|
|
77
|
+
o: undefined,
|
|
155
78
|
};
|
|
156
79
|
}
|
|
157
80
|
|
|
@@ -159,7 +82,7 @@ export function createBigIntNode(
|
|
|
159
82
|
ctx: ParserContext,
|
|
160
83
|
current: bigint,
|
|
161
84
|
): SerovalBigIntNode {
|
|
162
|
-
assert(ctx.features & Feature.BigInt,
|
|
85
|
+
assert(ctx.features & Feature.BigInt, new UnsupportedTypeError(current));
|
|
163
86
|
return {
|
|
164
87
|
t: SerovalNodeType.BigInt,
|
|
165
88
|
i: undefined,
|
|
@@ -171,6 +94,7 @@ export function createBigIntNode(
|
|
|
171
94
|
a: undefined,
|
|
172
95
|
f: undefined,
|
|
173
96
|
b: undefined,
|
|
97
|
+
o: undefined,
|
|
174
98
|
};
|
|
175
99
|
}
|
|
176
100
|
|
|
@@ -186,6 +110,7 @@ export function createIndexedValueNode(id: number): SerovalIndexedValueNode {
|
|
|
186
110
|
a: undefined,
|
|
187
111
|
f: undefined,
|
|
188
112
|
b: undefined,
|
|
113
|
+
o: undefined,
|
|
189
114
|
};
|
|
190
115
|
}
|
|
191
116
|
|
|
@@ -201,6 +126,7 @@ export function createDateNode(id: number, current: Date): SerovalDateNode {
|
|
|
201
126
|
f: undefined,
|
|
202
127
|
a: undefined,
|
|
203
128
|
b: undefined,
|
|
129
|
+
o: undefined,
|
|
204
130
|
};
|
|
205
131
|
}
|
|
206
132
|
|
|
@@ -216,6 +142,7 @@ export function createRegExpNode(id: number, current: RegExp): SerovalRegExpNode
|
|
|
216
142
|
a: undefined,
|
|
217
143
|
f: undefined,
|
|
218
144
|
b: undefined,
|
|
145
|
+
o: undefined,
|
|
219
146
|
};
|
|
220
147
|
}
|
|
221
148
|
|
|
@@ -240,13 +167,14 @@ export function createArrayBufferNode(
|
|
|
240
167
|
a: undefined,
|
|
241
168
|
f: undefined,
|
|
242
169
|
b: undefined,
|
|
170
|
+
o: undefined,
|
|
243
171
|
};
|
|
244
172
|
}
|
|
245
173
|
|
|
246
174
|
export function serializeArrayBuffer(
|
|
247
175
|
ctx: ParserContext,
|
|
248
176
|
current: ArrayBuffer,
|
|
249
|
-
) {
|
|
177
|
+
): SerovalNode {
|
|
250
178
|
const id = createIndexedValue(ctx, current);
|
|
251
179
|
if (ctx.markedRefs.has(id)) {
|
|
252
180
|
return createIndexedValueNode(id);
|
|
@@ -259,19 +187,19 @@ export function createTypedArrayNode(
|
|
|
259
187
|
id: number,
|
|
260
188
|
current: TypedArrayValue,
|
|
261
189
|
): SerovalTypedArrayNode {
|
|
262
|
-
|
|
263
|
-
assert(ctx.features & Feature.TypedArray, `Unsupported value type "${constructor}"`);
|
|
190
|
+
assert(ctx.features & Feature.TypedArray, new UnsupportedTypeError(current));
|
|
264
191
|
return {
|
|
265
192
|
t: SerovalNodeType.TypedArray,
|
|
266
193
|
i: id,
|
|
267
194
|
s: undefined,
|
|
268
195
|
l: current.length,
|
|
269
|
-
c: constructor,
|
|
196
|
+
c: current.constructor.name,
|
|
270
197
|
m: undefined,
|
|
271
198
|
d: undefined,
|
|
272
199
|
a: undefined,
|
|
273
200
|
f: serializeArrayBuffer(ctx, current.buffer),
|
|
274
201
|
b: current.byteOffset,
|
|
202
|
+
o: undefined,
|
|
275
203
|
};
|
|
276
204
|
}
|
|
277
205
|
|
|
@@ -282,34 +210,35 @@ export function createBigIntTypedArrayNode(
|
|
|
282
210
|
id: number,
|
|
283
211
|
current: BigIntTypedArrayValue,
|
|
284
212
|
): SerovalBigIntTypedArrayNode {
|
|
285
|
-
const constructor = current.constructor.name;
|
|
286
213
|
assert(
|
|
287
214
|
(ctx.features & BIGINT_FLAG) === BIGINT_FLAG,
|
|
288
|
-
|
|
215
|
+
new UnsupportedTypeError(current),
|
|
289
216
|
);
|
|
290
217
|
return {
|
|
291
218
|
t: SerovalNodeType.BigIntTypedArray,
|
|
292
219
|
i: id,
|
|
293
220
|
s: undefined,
|
|
294
221
|
l: current.length,
|
|
295
|
-
c: constructor,
|
|
222
|
+
c: current.constructor.name,
|
|
296
223
|
m: undefined,
|
|
297
224
|
d: undefined,
|
|
298
225
|
a: undefined,
|
|
299
226
|
f: serializeArrayBuffer(ctx, current.buffer),
|
|
300
227
|
b: current.byteOffset,
|
|
228
|
+
o: undefined,
|
|
301
229
|
};
|
|
302
230
|
}
|
|
303
231
|
|
|
304
232
|
export function createWKSymbolNode(
|
|
305
233
|
ctx: ParserContext,
|
|
234
|
+
id: number,
|
|
306
235
|
current: WellKnownSymbols,
|
|
307
236
|
): SerovalWKSymbolNode {
|
|
308
|
-
assert(ctx.features & Feature.Symbol,
|
|
309
|
-
assert(current in INV_SYMBOL_REF, '
|
|
237
|
+
assert(ctx.features & Feature.Symbol, new UnsupportedTypeError(current));
|
|
238
|
+
assert(current in INV_SYMBOL_REF, new Error('Only well-known symbols are supported.'));
|
|
310
239
|
return {
|
|
311
240
|
t: SerovalNodeType.WKSymbol,
|
|
312
|
-
i:
|
|
241
|
+
i: id,
|
|
313
242
|
s: INV_SYMBOL_REF[current],
|
|
314
243
|
l: undefined,
|
|
315
244
|
c: undefined,
|
|
@@ -318,6 +247,7 @@ export function createWKSymbolNode(
|
|
|
318
247
|
a: undefined,
|
|
319
248
|
f: undefined,
|
|
320
249
|
b: undefined,
|
|
250
|
+
o: undefined,
|
|
321
251
|
};
|
|
322
252
|
}
|
|
323
253
|
|
|
@@ -336,6 +266,7 @@ export function createReferenceNode<T>(
|
|
|
336
266
|
a: undefined,
|
|
337
267
|
f: undefined,
|
|
338
268
|
b: undefined,
|
|
269
|
+
o: undefined,
|
|
339
270
|
};
|
|
340
271
|
}
|
|
341
272
|
|
|
@@ -355,5 +286,33 @@ export function createDataViewNode(
|
|
|
355
286
|
a: undefined,
|
|
356
287
|
f: serializeArrayBuffer(ctx, current.buffer),
|
|
357
288
|
b: current.byteOffset,
|
|
289
|
+
o: undefined,
|
|
358
290
|
};
|
|
359
291
|
}
|
|
292
|
+
|
|
293
|
+
export function createSymbolNode(
|
|
294
|
+
ctx: ParserContext,
|
|
295
|
+
current: symbol,
|
|
296
|
+
): SerovalNode {
|
|
297
|
+
const id = createIndexedValue(ctx, current);
|
|
298
|
+
if (ctx.markedRefs.has(id)) {
|
|
299
|
+
return createIndexedValueNode(id);
|
|
300
|
+
}
|
|
301
|
+
if (hasReferenceID(current)) {
|
|
302
|
+
return createReferenceNode(id, current);
|
|
303
|
+
}
|
|
304
|
+
return createWKSymbolNode(ctx, id, current as WellKnownSymbols);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function createFunctionNode(
|
|
308
|
+
ctx: ParserContext,
|
|
309
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
310
|
+
current: Function,
|
|
311
|
+
): SerovalNode {
|
|
312
|
+
assert(hasReferenceID(current), new Error('Cannot serialize function without reference ID.'));
|
|
313
|
+
const id = createIndexedValue(ctx, current);
|
|
314
|
+
if (ctx.markedRefs.has(id)) {
|
|
315
|
+
return createIndexedValueNode(id);
|
|
316
|
+
}
|
|
317
|
+
return createReferenceNode(id, current);
|
|
318
|
+
}
|
package/src/tree/reference.ts
CHANGED
|
@@ -19,12 +19,12 @@ export function hasReference(id: string): boolean {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export function getReferenceID<T>(value: T): string {
|
|
22
|
-
assert(hasReferenceID(value), 'Missing reference id');
|
|
23
|
-
return REFERENCE.get(value)
|
|
22
|
+
assert(hasReferenceID(value), new Error('Missing reference id'));
|
|
23
|
+
return REFERENCE.get(value)!;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export function getReference<T>(id: string): T {
|
|
27
|
-
assert(hasReference(id), 'Missing reference for id:' + id);
|
|
27
|
+
assert(hasReference(id), new Error('Missing reference for id:' + id));
|
|
28
28
|
return INV_REFERENCE.get(id) as T;
|
|
29
29
|
}
|
|
30
30
|
|