seroval 1.5.6 → 1.6.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/index.d.ts +761 -0
- package/dist/index.js +2928 -0
- package/package.json +14 -23
- package/dist/cjs/development/index.cjs +0 -4232
- package/dist/cjs/development/index.cjs.map +0 -7
- package/dist/cjs/production/index.cjs +0 -3
- package/dist/esm/development/index.mjs +0 -4209
- package/dist/esm/development/index.mjs.map +0 -7
- package/dist/esm/production/index.mjs +0 -3
- package/dist/types/core/Serializer.d.ts +0 -28
- package/dist/types/core/Serializer.d.ts.map +0 -1
- package/dist/types/core/base-primitives.d.ts +0 -28
- package/dist/types/core/base-primitives.d.ts.map +0 -1
- package/dist/types/core/compat.d.ts +0 -15
- package/dist/types/core/compat.d.ts.map +0 -1
- package/dist/types/core/constants.d.ts +0 -105
- package/dist/types/core/constants.d.ts.map +0 -1
- package/dist/types/core/constructors.d.ts +0 -50
- package/dist/types/core/constructors.d.ts.map +0 -1
- package/dist/types/core/context/async-parser.d.ts +0 -20
- package/dist/types/core/context/async-parser.d.ts.map +0 -1
- package/dist/types/core/context/deserializer.d.ts +0 -52
- package/dist/types/core/context/deserializer.d.ts.map +0 -1
- package/dist/types/core/context/parser.d.ts +0 -62
- package/dist/types/core/context/parser.d.ts.map +0 -1
- package/dist/types/core/context/serializer.d.ts +0 -103
- package/dist/types/core/context/serializer.d.ts.map +0 -1
- package/dist/types/core/context/sync-parser.d.ts +0 -61
- package/dist/types/core/context/sync-parser.d.ts.map +0 -1
- package/dist/types/core/cross/index.d.ts +0 -24
- package/dist/types/core/cross/index.d.ts.map +0 -1
- package/dist/types/core/errors.d.ts +0 -47
- package/dist/types/core/errors.d.ts.map +0 -1
- package/dist/types/core/function-string.d.ts +0 -3
- package/dist/types/core/function-string.d.ts.map +0 -1
- package/dist/types/core/keys.d.ts +0 -4
- package/dist/types/core/keys.d.ts.map +0 -1
- package/dist/types/core/literals.d.ts +0 -10
- package/dist/types/core/literals.d.ts.map +0 -1
- package/dist/types/core/node.d.ts +0 -8
- package/dist/types/core/node.d.ts.map +0 -1
- package/dist/types/core/opaque-reference.d.ts +0 -9
- package/dist/types/core/opaque-reference.d.ts.map +0 -1
- package/dist/types/core/plugin.d.ts +0 -52
- package/dist/types/core/plugin.d.ts.map +0 -1
- package/dist/types/core/reference.d.ts +0 -6
- package/dist/types/core/reference.d.ts.map +0 -1
- package/dist/types/core/sequence.d.ts +0 -11
- package/dist/types/core/sequence.d.ts.map +0 -1
- package/dist/types/core/special-reference.d.ts +0 -16
- package/dist/types/core/special-reference.d.ts.map +0 -1
- package/dist/types/core/stream.d.ts +0 -17
- package/dist/types/core/stream.d.ts.map +0 -1
- package/dist/types/core/string.d.ts +0 -4
- package/dist/types/core/string.d.ts.map +0 -1
- package/dist/types/core/symbols.d.ts +0 -14
- package/dist/types/core/symbols.d.ts.map +0 -1
- package/dist/types/core/tree/index.d.ts +0 -21
- package/dist/types/core/tree/index.d.ts.map +0 -1
- package/dist/types/core/types.d.ts +0 -233
- package/dist/types/core/types.d.ts.map +0 -1
- package/dist/types/core/utils/error.d.ts +0 -6
- package/dist/types/core/utils/error.d.ts.map +0 -1
- package/dist/types/core/utils/get-identifier.d.ts +0 -2
- package/dist/types/core/utils/get-identifier.d.ts.map +0 -1
- package/dist/types/core/utils/get-object-flag.d.ts +0 -3
- package/dist/types/core/utils/get-object-flag.d.ts.map +0 -1
- package/dist/types/core/utils/is-valid-identifier.d.ts +0 -2
- package/dist/types/core/utils/is-valid-identifier.d.ts.map +0 -1
- package/dist/types/core/utils/promise-to-result.d.ts +0 -2
- package/dist/types/core/utils/promise-to-result.d.ts.map +0 -1
- package/dist/types/core/utils/typed-array.d.ts +0 -6
- package/dist/types/core/utils/typed-array.d.ts.map +0 -1
- package/dist/types/core/utils/valid-properties.d.ts +0 -3
- package/dist/types/core/utils/valid-properties.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -18
- package/dist/types/index.d.ts.map +0 -1
- package/src/core/Serializer.ts +0 -119
- package/src/core/base-primitives.ts +0 -537
- package/src/core/compat.ts +0 -23
- package/src/core/constants.ts +0 -202
- package/src/core/constructors.ts +0 -265
- package/src/core/context/async-parser.ts +0 -666
- package/src/core/context/deserializer.ts +0 -832
- package/src/core/context/parser.ts +0 -335
- package/src/core/context/serializer.ts +0 -1524
- package/src/core/context/sync-parser.ts +0 -943
- package/src/core/cross/index.ts +0 -190
- package/src/core/errors.ts +0 -196
- package/src/core/function-string.ts +0 -31
- package/src/core/keys.ts +0 -17
- package/src/core/literals.ts +0 -43
- package/src/core/node.ts +0 -40
- package/src/core/opaque-reference.ts +0 -9
- package/src/core/plugin.ts +0 -105
- package/src/core/reference.ts +0 -66
- package/src/core/sequence.ts +0 -59
- package/src/core/special-reference.ts +0 -42
- package/src/core/stream.ts +0 -71
- package/src/core/string.ts +0 -86
- package/src/core/symbols.ts +0 -20
- package/src/core/tree/index.ts +0 -134
- package/src/core/types.ts +0 -377
- package/src/core/utils/error.ts +0 -73
- package/src/core/utils/get-identifier.ts +0 -18
- package/src/core/utils/get-object-flag.ts +0 -14
- package/src/core/utils/is-valid-identifier.ts +0 -12
- package/src/core/utils/promise-to-result.ts +0 -9
- package/src/core/utils/typed-array.ts +0 -56
- package/src/core/utils/valid-properties.ts +0 -34
- package/src/index.ts +0 -36
|
@@ -1,335 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createIndexedValueNode,
|
|
3
|
-
createReferenceNode,
|
|
4
|
-
createWKSymbolNode,
|
|
5
|
-
} from '../base-primitives';
|
|
6
|
-
import { ALL_ENABLED } from '../compat';
|
|
7
|
-
import type { WellKnownSymbols } from '../constants';
|
|
8
|
-
import { INV_SYMBOL_REF, NIL, SerovalNodeType } from '../constants';
|
|
9
|
-
import { SerovalUnsupportedTypeError } from '../errors';
|
|
10
|
-
import { createSerovalNode } from '../node';
|
|
11
|
-
import type { PluginAccessOptions, SerovalMode } from '../plugin';
|
|
12
|
-
import { hasReferenceID } from '../reference';
|
|
13
|
-
import {
|
|
14
|
-
ASYNC_ITERATOR,
|
|
15
|
-
ITERATOR,
|
|
16
|
-
SPECIAL_REFS,
|
|
17
|
-
SpecialReference,
|
|
18
|
-
} from '../special-reference';
|
|
19
|
-
import { serializeString } from '../string';
|
|
20
|
-
import { SYM_ASYNC_ITERATOR, SYM_ITERATOR } from '../symbols';
|
|
21
|
-
import type {
|
|
22
|
-
SerovalArrayBufferNode,
|
|
23
|
-
SerovalAsyncIteratorFactoryNode,
|
|
24
|
-
SerovalIndexedValueNode,
|
|
25
|
-
SerovalIteratorFactoryNode,
|
|
26
|
-
SerovalMapNode,
|
|
27
|
-
SerovalNode,
|
|
28
|
-
SerovalNullConstructorNode,
|
|
29
|
-
SerovalObjectNode,
|
|
30
|
-
SerovalObjectRecordNode,
|
|
31
|
-
SerovalPromiseConstructorNode,
|
|
32
|
-
SerovalReferenceNode,
|
|
33
|
-
SerovalSpecialReferenceNode,
|
|
34
|
-
SerovalWKSymbolNode,
|
|
35
|
-
} from '../types';
|
|
36
|
-
import { getObjectFlag } from '../utils/get-object-flag';
|
|
37
|
-
|
|
38
|
-
export interface BaseParserContextOptions extends PluginAccessOptions {
|
|
39
|
-
disabledFeatures?: number;
|
|
40
|
-
refs?: Map<unknown, number>;
|
|
41
|
-
depthLimit?: number;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export const enum ParserNodeType {
|
|
45
|
-
Fresh = 0,
|
|
46
|
-
Indexed = 1,
|
|
47
|
-
Referenced = 2,
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface FreshNode {
|
|
51
|
-
type: ParserNodeType.Fresh;
|
|
52
|
-
value: number;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export interface IndexedNode {
|
|
56
|
-
type: ParserNodeType.Indexed;
|
|
57
|
-
value: SerovalIndexedValueNode;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export interface ReferencedNode {
|
|
61
|
-
type: ParserNodeType.Referenced;
|
|
62
|
-
value: SerovalReferenceNode;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
type ObjectNode = FreshNode | IndexedNode | ReferencedNode;
|
|
66
|
-
|
|
67
|
-
export interface BaseParserContext extends PluginAccessOptions {
|
|
68
|
-
readonly mode: SerovalMode;
|
|
69
|
-
|
|
70
|
-
marked: Set<number>;
|
|
71
|
-
|
|
72
|
-
refs: Map<unknown, number>;
|
|
73
|
-
|
|
74
|
-
features: number;
|
|
75
|
-
|
|
76
|
-
depthLimit: number;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function createBaseParserContext(
|
|
80
|
-
mode: SerovalMode,
|
|
81
|
-
options: BaseParserContextOptions,
|
|
82
|
-
): BaseParserContext {
|
|
83
|
-
return {
|
|
84
|
-
plugins: options.plugins,
|
|
85
|
-
mode,
|
|
86
|
-
marked: new Set(),
|
|
87
|
-
features: ALL_ENABLED ^ (options.disabledFeatures || 0),
|
|
88
|
-
refs: options.refs || new Map(),
|
|
89
|
-
depthLimit: options.depthLimit || 1000,
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Ensures that the value (based on an identifier) has been visited by the parser.
|
|
95
|
-
* @param ctx
|
|
96
|
-
* @param id
|
|
97
|
-
*/
|
|
98
|
-
export function markParserRef(ctx: BaseParserContext, id: number): void {
|
|
99
|
-
ctx.marked.add(id);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export function isParserRefMarked(ctx: BaseParserContext, id: number): boolean {
|
|
103
|
-
return ctx.marked.has(id);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Creates an identifier for a value
|
|
108
|
-
* @param ctx
|
|
109
|
-
* @param current
|
|
110
|
-
*/
|
|
111
|
-
export function createIndexForValue<T>(
|
|
112
|
-
ctx: BaseParserContext,
|
|
113
|
-
current: T,
|
|
114
|
-
): number {
|
|
115
|
-
const id = ctx.refs.size;
|
|
116
|
-
ctx.refs.set(current, id);
|
|
117
|
-
return id;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export function getNodeForIndexedValue<T>(
|
|
121
|
-
ctx: BaseParserContext,
|
|
122
|
-
current: T,
|
|
123
|
-
): FreshNode | IndexedNode {
|
|
124
|
-
const registeredId = ctx.refs.get(current);
|
|
125
|
-
if (registeredId != null) {
|
|
126
|
-
markParserRef(ctx, registeredId);
|
|
127
|
-
return {
|
|
128
|
-
type: ParserNodeType.Indexed,
|
|
129
|
-
value: createIndexedValueNode(registeredId),
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
return {
|
|
133
|
-
type: ParserNodeType.Fresh,
|
|
134
|
-
value: createIndexForValue(ctx, current),
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function getReferenceNode<T>(
|
|
139
|
-
ctx: BaseParserContext,
|
|
140
|
-
current: T,
|
|
141
|
-
): ObjectNode {
|
|
142
|
-
const indexed = getNodeForIndexedValue(ctx, current);
|
|
143
|
-
if (indexed.type === ParserNodeType.Indexed) {
|
|
144
|
-
return indexed;
|
|
145
|
-
}
|
|
146
|
-
// Special references are special ;)
|
|
147
|
-
if (hasReferenceID(current)) {
|
|
148
|
-
return {
|
|
149
|
-
type: ParserNodeType.Referenced,
|
|
150
|
-
value: createReferenceNode(indexed.value, current),
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
return indexed;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Parsing methods
|
|
158
|
-
*/
|
|
159
|
-
export function parseWellKnownSymbol(
|
|
160
|
-
ctx: BaseParserContext,
|
|
161
|
-
current: symbol,
|
|
162
|
-
): SerovalIndexedValueNode | SerovalWKSymbolNode | SerovalReferenceNode {
|
|
163
|
-
const ref = getReferenceNode(ctx, current);
|
|
164
|
-
if (ref.type !== ParserNodeType.Fresh) {
|
|
165
|
-
return ref.value;
|
|
166
|
-
}
|
|
167
|
-
if (current in INV_SYMBOL_REF) {
|
|
168
|
-
return createWKSymbolNode(ref.value, current as WellKnownSymbols);
|
|
169
|
-
}
|
|
170
|
-
throw new SerovalUnsupportedTypeError(current);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
export function parseSpecialReference(
|
|
174
|
-
ctx: BaseParserContext,
|
|
175
|
-
ref: SpecialReference,
|
|
176
|
-
): SerovalIndexedValueNode | SerovalSpecialReferenceNode {
|
|
177
|
-
const result = getNodeForIndexedValue(ctx, SPECIAL_REFS[ref]);
|
|
178
|
-
if (result.type === ParserNodeType.Indexed) {
|
|
179
|
-
return result.value;
|
|
180
|
-
}
|
|
181
|
-
return createSerovalNode(
|
|
182
|
-
SerovalNodeType.SpecialReference,
|
|
183
|
-
result.value,
|
|
184
|
-
ref,
|
|
185
|
-
NIL,
|
|
186
|
-
NIL,
|
|
187
|
-
NIL,
|
|
188
|
-
NIL,
|
|
189
|
-
NIL,
|
|
190
|
-
NIL,
|
|
191
|
-
NIL,
|
|
192
|
-
NIL,
|
|
193
|
-
NIL,
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export function parseIteratorFactory(
|
|
198
|
-
ctx: BaseParserContext,
|
|
199
|
-
): SerovalIndexedValueNode | SerovalIteratorFactoryNode {
|
|
200
|
-
const result = getNodeForIndexedValue(ctx, ITERATOR);
|
|
201
|
-
if (result.type === ParserNodeType.Indexed) {
|
|
202
|
-
return result.value;
|
|
203
|
-
}
|
|
204
|
-
return createSerovalNode(
|
|
205
|
-
SerovalNodeType.IteratorFactory,
|
|
206
|
-
result.value,
|
|
207
|
-
NIL,
|
|
208
|
-
NIL,
|
|
209
|
-
NIL,
|
|
210
|
-
NIL,
|
|
211
|
-
NIL,
|
|
212
|
-
NIL,
|
|
213
|
-
parseWellKnownSymbol(ctx, SYM_ITERATOR),
|
|
214
|
-
NIL,
|
|
215
|
-
NIL,
|
|
216
|
-
NIL,
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export function parseAsyncIteratorFactory(
|
|
221
|
-
ctx: BaseParserContext,
|
|
222
|
-
): SerovalIndexedValueNode | SerovalAsyncIteratorFactoryNode {
|
|
223
|
-
const result = getNodeForIndexedValue(ctx, ASYNC_ITERATOR);
|
|
224
|
-
if (result.type === ParserNodeType.Indexed) {
|
|
225
|
-
return result.value;
|
|
226
|
-
}
|
|
227
|
-
return createSerovalNode(
|
|
228
|
-
SerovalNodeType.AsyncIteratorFactory,
|
|
229
|
-
result.value,
|
|
230
|
-
NIL,
|
|
231
|
-
NIL,
|
|
232
|
-
NIL,
|
|
233
|
-
NIL,
|
|
234
|
-
NIL,
|
|
235
|
-
[
|
|
236
|
-
parseSpecialReference(ctx, SpecialReference.PromiseConstructor),
|
|
237
|
-
parseWellKnownSymbol(ctx, SYM_ASYNC_ITERATOR),
|
|
238
|
-
],
|
|
239
|
-
NIL,
|
|
240
|
-
NIL,
|
|
241
|
-
NIL,
|
|
242
|
-
NIL,
|
|
243
|
-
);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
export function createObjectNode(
|
|
247
|
-
id: number,
|
|
248
|
-
current: Record<string, unknown>,
|
|
249
|
-
empty: boolean,
|
|
250
|
-
record: SerovalObjectRecordNode,
|
|
251
|
-
): SerovalObjectNode | SerovalNullConstructorNode {
|
|
252
|
-
return createSerovalNode(
|
|
253
|
-
empty ? SerovalNodeType.NullConstructor : SerovalNodeType.Object,
|
|
254
|
-
id,
|
|
255
|
-
NIL,
|
|
256
|
-
NIL,
|
|
257
|
-
NIL,
|
|
258
|
-
record,
|
|
259
|
-
NIL,
|
|
260
|
-
NIL,
|
|
261
|
-
NIL,
|
|
262
|
-
NIL,
|
|
263
|
-
getObjectFlag(current),
|
|
264
|
-
NIL,
|
|
265
|
-
);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
export function createMapNode(
|
|
269
|
-
ctx: BaseParserContext,
|
|
270
|
-
id: number,
|
|
271
|
-
k: SerovalNode[],
|
|
272
|
-
v: SerovalNode[],
|
|
273
|
-
): SerovalMapNode {
|
|
274
|
-
return createSerovalNode(
|
|
275
|
-
SerovalNodeType.Map,
|
|
276
|
-
id,
|
|
277
|
-
NIL,
|
|
278
|
-
NIL,
|
|
279
|
-
NIL,
|
|
280
|
-
NIL,
|
|
281
|
-
{ k, v },
|
|
282
|
-
NIL,
|
|
283
|
-
parseSpecialReference(ctx, SpecialReference.MapSentinel),
|
|
284
|
-
NIL,
|
|
285
|
-
NIL,
|
|
286
|
-
NIL,
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
export function createPromiseConstructorNode(
|
|
291
|
-
ctx: BaseParserContext,
|
|
292
|
-
id: number,
|
|
293
|
-
resolver: number,
|
|
294
|
-
): SerovalPromiseConstructorNode {
|
|
295
|
-
return createSerovalNode(
|
|
296
|
-
SerovalNodeType.PromiseConstructor,
|
|
297
|
-
id,
|
|
298
|
-
resolver,
|
|
299
|
-
NIL,
|
|
300
|
-
NIL,
|
|
301
|
-
NIL,
|
|
302
|
-
NIL,
|
|
303
|
-
NIL,
|
|
304
|
-
parseSpecialReference(ctx, SpecialReference.PromiseConstructor),
|
|
305
|
-
NIL,
|
|
306
|
-
NIL,
|
|
307
|
-
NIL,
|
|
308
|
-
);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
export function createArrayBufferNode(
|
|
312
|
-
ctx: BaseParserContext,
|
|
313
|
-
id: number,
|
|
314
|
-
current: ArrayBuffer,
|
|
315
|
-
): SerovalArrayBufferNode {
|
|
316
|
-
const bytes = new Uint8Array(current);
|
|
317
|
-
let result = '';
|
|
318
|
-
for (let i = 0, len = bytes.length; i < len; i++) {
|
|
319
|
-
result += String.fromCharCode(bytes[i]);
|
|
320
|
-
}
|
|
321
|
-
return createSerovalNode(
|
|
322
|
-
SerovalNodeType.ArrayBuffer,
|
|
323
|
-
id,
|
|
324
|
-
serializeString(btoa(result)),
|
|
325
|
-
NIL,
|
|
326
|
-
NIL,
|
|
327
|
-
NIL,
|
|
328
|
-
NIL,
|
|
329
|
-
NIL,
|
|
330
|
-
parseSpecialReference(ctx, SpecialReference.ArrayBufferConstructor),
|
|
331
|
-
NIL,
|
|
332
|
-
NIL,
|
|
333
|
-
NIL,
|
|
334
|
-
);
|
|
335
|
-
}
|