seroval 1.3.1 → 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 +2637 -2682
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -17
- package/dist/esm/development/index.mjs +2637 -2682
- 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 -37
- 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 +504 -307
- 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 -499
- package/src/core/context/parser/stream.ts +0 -323
- package/src/core/context/parser/sync.ts +0 -408
- 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
|
@@ -1,323 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createAsyncIteratorFactoryInstanceNode,
|
|
3
|
-
createIteratorFactoryInstanceNode,
|
|
4
|
-
createPluginNode,
|
|
5
|
-
createStreamConstructorNode,
|
|
6
|
-
createStreamNextNode,
|
|
7
|
-
createStreamReturnNode,
|
|
8
|
-
createStreamThrowNode,
|
|
9
|
-
createStringNode,
|
|
10
|
-
} from '../../base-primitives';
|
|
11
|
-
import { NIL, SerovalNodeType } from '../../constants';
|
|
12
|
-
import { FALSE_NODE, TRUE_NODE } from '../../literals';
|
|
13
|
-
import { createSerovalNode } from '../../node';
|
|
14
|
-
import { SpecialReference } from '../../special-reference';
|
|
15
|
-
import type { Stream } from '../../stream';
|
|
16
|
-
import { createStreamFromAsyncIterable } from '../../stream';
|
|
17
|
-
import { serializeString } from '../../string';
|
|
18
|
-
import type {
|
|
19
|
-
SerovalNode,
|
|
20
|
-
SerovalObjectRecordKey,
|
|
21
|
-
SerovalObjectRecordNode,
|
|
22
|
-
SerovalPluginNode,
|
|
23
|
-
SerovalPromiseConstructorNode,
|
|
24
|
-
} from '../../types';
|
|
25
|
-
import { iteratorToSequence } from '../../utils/iterator-to-sequence';
|
|
26
|
-
import type { BaseSyncParserContextOptions } from './sync';
|
|
27
|
-
import BaseSyncParserContext from './sync';
|
|
28
|
-
|
|
29
|
-
export interface BaseStreamParserContextOptions
|
|
30
|
-
extends BaseSyncParserContextOptions {
|
|
31
|
-
onParse: (node: SerovalNode, initial: boolean) => void;
|
|
32
|
-
onError?: (error: unknown) => void;
|
|
33
|
-
onDone?: () => void;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export default abstract class BaseStreamParserContext extends BaseSyncParserContext {
|
|
37
|
-
// Life
|
|
38
|
-
private alive = true;
|
|
39
|
-
|
|
40
|
-
// Amount of pending promises/streams
|
|
41
|
-
private pending = 0;
|
|
42
|
-
|
|
43
|
-
private onParseCallback: (node: SerovalNode, initial: boolean) => void;
|
|
44
|
-
|
|
45
|
-
private onErrorCallback?: (error: unknown) => void;
|
|
46
|
-
|
|
47
|
-
private onDoneCallback?: () => void;
|
|
48
|
-
|
|
49
|
-
constructor(options: BaseStreamParserContextOptions) {
|
|
50
|
-
super(options);
|
|
51
|
-
this.onParseCallback = options.onParse;
|
|
52
|
-
this.onErrorCallback = options.onError;
|
|
53
|
-
this.onDoneCallback = options.onDone;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private initial = true;
|
|
57
|
-
|
|
58
|
-
private buffer: SerovalNode[] = [];
|
|
59
|
-
|
|
60
|
-
private onParseInternal(node: SerovalNode, initial: boolean): void {
|
|
61
|
-
try {
|
|
62
|
-
this.onParseCallback(node, initial);
|
|
63
|
-
} catch (error) {
|
|
64
|
-
this.onError(error);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
private flush(): void {
|
|
69
|
-
for (let i = 0, len = this.buffer.length; i < len; i++) {
|
|
70
|
-
this.onParseInternal(this.buffer[i], false);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
onParse(node: SerovalNode): void {
|
|
75
|
-
if (this.initial) {
|
|
76
|
-
this.buffer.push(node);
|
|
77
|
-
} else {
|
|
78
|
-
this.onParseInternal(node, false);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
onError(error: unknown): void {
|
|
83
|
-
if (this.onErrorCallback) {
|
|
84
|
-
this.onErrorCallback(error);
|
|
85
|
-
} else {
|
|
86
|
-
throw error;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
private onDone(): void {
|
|
91
|
-
if (this.onDoneCallback) {
|
|
92
|
-
this.onDoneCallback();
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
pushPendingState(): void {
|
|
97
|
-
this.pending++;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
popPendingState(): void {
|
|
101
|
-
if (--this.pending <= 0) {
|
|
102
|
-
this.onDone();
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
protected parseProperties(
|
|
107
|
-
properties: Record<string | symbol, unknown>,
|
|
108
|
-
): SerovalObjectRecordNode {
|
|
109
|
-
const entries = Object.entries(properties);
|
|
110
|
-
const keyNodes: SerovalObjectRecordKey[] = [];
|
|
111
|
-
const valueNodes: SerovalNode[] = [];
|
|
112
|
-
for (let i = 0, len = entries.length; i < len; i++) {
|
|
113
|
-
keyNodes.push(serializeString(entries[i][0]));
|
|
114
|
-
valueNodes.push(this.parseTop(entries[i][1]));
|
|
115
|
-
}
|
|
116
|
-
// Check special properties, symbols in this case
|
|
117
|
-
let symbol = Symbol.iterator;
|
|
118
|
-
if (symbol in properties) {
|
|
119
|
-
keyNodes.push(this.parseWellKnownSymbol(symbol));
|
|
120
|
-
valueNodes.push(
|
|
121
|
-
createIteratorFactoryInstanceNode(
|
|
122
|
-
this.parseIteratorFactory(),
|
|
123
|
-
this.parseTop(
|
|
124
|
-
iteratorToSequence(properties as unknown as Iterable<unknown>),
|
|
125
|
-
),
|
|
126
|
-
),
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
symbol = Symbol.asyncIterator;
|
|
130
|
-
if (symbol in properties) {
|
|
131
|
-
keyNodes.push(this.parseWellKnownSymbol(symbol));
|
|
132
|
-
valueNodes.push(
|
|
133
|
-
createAsyncIteratorFactoryInstanceNode(
|
|
134
|
-
this.parseAsyncIteratorFactory(),
|
|
135
|
-
this.parseTop(
|
|
136
|
-
createStreamFromAsyncIterable(
|
|
137
|
-
properties as unknown as AsyncIterable<unknown>,
|
|
138
|
-
),
|
|
139
|
-
),
|
|
140
|
-
),
|
|
141
|
-
);
|
|
142
|
-
}
|
|
143
|
-
symbol = Symbol.toStringTag;
|
|
144
|
-
if (symbol in properties) {
|
|
145
|
-
keyNodes.push(this.parseWellKnownSymbol(symbol));
|
|
146
|
-
valueNodes.push(createStringNode(properties[symbol] as string));
|
|
147
|
-
}
|
|
148
|
-
symbol = Symbol.isConcatSpreadable;
|
|
149
|
-
if (symbol in properties) {
|
|
150
|
-
keyNodes.push(this.parseWellKnownSymbol(symbol));
|
|
151
|
-
valueNodes.push(properties[symbol] ? TRUE_NODE : FALSE_NODE);
|
|
152
|
-
}
|
|
153
|
-
return {
|
|
154
|
-
k: keyNodes,
|
|
155
|
-
v: valueNodes,
|
|
156
|
-
s: keyNodes.length,
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
protected handlePromiseSuccess(id: number, data: unknown): void {
|
|
161
|
-
const parsed = this.parseWithError(data);
|
|
162
|
-
if (parsed) {
|
|
163
|
-
this.onParse(
|
|
164
|
-
createSerovalNode(
|
|
165
|
-
SerovalNodeType.PromiseSuccess,
|
|
166
|
-
id,
|
|
167
|
-
NIL,
|
|
168
|
-
NIL,
|
|
169
|
-
NIL,
|
|
170
|
-
NIL,
|
|
171
|
-
NIL,
|
|
172
|
-
NIL,
|
|
173
|
-
[this.parseSpecialReference(SpecialReference.PromiseSuccess), parsed],
|
|
174
|
-
NIL,
|
|
175
|
-
NIL,
|
|
176
|
-
NIL,
|
|
177
|
-
),
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
this.popPendingState();
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
protected handlePromiseFailure(id: number, data: unknown): void {
|
|
184
|
-
if (this.alive) {
|
|
185
|
-
const parsed = this.parseWithError(data);
|
|
186
|
-
if (parsed) {
|
|
187
|
-
this.onParse(
|
|
188
|
-
createSerovalNode(
|
|
189
|
-
SerovalNodeType.PromiseFailure,
|
|
190
|
-
id,
|
|
191
|
-
NIL,
|
|
192
|
-
NIL,
|
|
193
|
-
NIL,
|
|
194
|
-
NIL,
|
|
195
|
-
NIL,
|
|
196
|
-
NIL,
|
|
197
|
-
[
|
|
198
|
-
this.parseSpecialReference(SpecialReference.PromiseFailure),
|
|
199
|
-
parsed,
|
|
200
|
-
],
|
|
201
|
-
NIL,
|
|
202
|
-
NIL,
|
|
203
|
-
NIL,
|
|
204
|
-
),
|
|
205
|
-
);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
this.popPendingState();
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
protected parsePromise(
|
|
212
|
-
id: number,
|
|
213
|
-
current: Promise<unknown>,
|
|
214
|
-
): SerovalPromiseConstructorNode {
|
|
215
|
-
const resolver = this.createIndex({});
|
|
216
|
-
current.then(
|
|
217
|
-
this.handlePromiseSuccess.bind(this, resolver),
|
|
218
|
-
this.handlePromiseFailure.bind(this, resolver),
|
|
219
|
-
);
|
|
220
|
-
this.pushPendingState();
|
|
221
|
-
return this.createPromiseConstructorNode(id, resolver);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
protected parsePlugin(
|
|
225
|
-
id: number,
|
|
226
|
-
current: unknown,
|
|
227
|
-
): SerovalPluginNode | undefined {
|
|
228
|
-
const currentPlugins = this.plugins;
|
|
229
|
-
if (currentPlugins) {
|
|
230
|
-
for (let i = 0, len = currentPlugins.length; i < len; i++) {
|
|
231
|
-
const plugin = currentPlugins[i];
|
|
232
|
-
if (plugin.parse.stream && plugin.test(current)) {
|
|
233
|
-
return createPluginNode(
|
|
234
|
-
id,
|
|
235
|
-
plugin.tag,
|
|
236
|
-
plugin.parse.stream(current, this, {
|
|
237
|
-
id,
|
|
238
|
-
}),
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
return NIL;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
protected parseStream(id: number, current: Stream<unknown>): SerovalNode {
|
|
247
|
-
const result = createStreamConstructorNode(
|
|
248
|
-
id,
|
|
249
|
-
this.parseSpecialReference(SpecialReference.StreamConstructor),
|
|
250
|
-
[],
|
|
251
|
-
);
|
|
252
|
-
this.pushPendingState();
|
|
253
|
-
current.on({
|
|
254
|
-
next: value => {
|
|
255
|
-
if (this.alive) {
|
|
256
|
-
const parsed = this.parseWithError(value);
|
|
257
|
-
if (parsed) {
|
|
258
|
-
this.onParse(createStreamNextNode(id, parsed));
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
},
|
|
262
|
-
throw: value => {
|
|
263
|
-
if (this.alive) {
|
|
264
|
-
const parsed = this.parseWithError(value);
|
|
265
|
-
if (parsed) {
|
|
266
|
-
this.onParse(createStreamThrowNode(id, parsed));
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
this.popPendingState();
|
|
270
|
-
},
|
|
271
|
-
return: value => {
|
|
272
|
-
if (this.alive) {
|
|
273
|
-
const parsed = this.parseWithError(value);
|
|
274
|
-
if (parsed) {
|
|
275
|
-
this.onParse(createStreamReturnNode(id, parsed));
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
this.popPendingState();
|
|
279
|
-
},
|
|
280
|
-
});
|
|
281
|
-
return result;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
parseWithError<T>(current: T): SerovalNode | undefined {
|
|
285
|
-
try {
|
|
286
|
-
return this.parseTop(current);
|
|
287
|
-
} catch (err) {
|
|
288
|
-
this.onError(err);
|
|
289
|
-
return NIL;
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* @private
|
|
295
|
-
*/
|
|
296
|
-
start<T>(current: T): void {
|
|
297
|
-
const parsed = this.parseWithError(current);
|
|
298
|
-
if (parsed) {
|
|
299
|
-
this.onParseInternal(parsed, true);
|
|
300
|
-
this.initial = false;
|
|
301
|
-
this.flush();
|
|
302
|
-
|
|
303
|
-
// Check if there's any pending pushes
|
|
304
|
-
if (this.pending <= 0) {
|
|
305
|
-
this.destroy();
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* @private
|
|
312
|
-
*/
|
|
313
|
-
destroy(): void {
|
|
314
|
-
if (this.alive) {
|
|
315
|
-
this.onDone();
|
|
316
|
-
this.alive = false;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
isAlive(): boolean {
|
|
321
|
-
return this.alive;
|
|
322
|
-
}
|
|
323
|
-
}
|