seroval 1.4.2 → 1.5.1
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 +133 -34
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +3 -3
- package/dist/esm/development/index.mjs +133 -34
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +3 -3
- package/dist/types/core/base-primitives.d.ts +5 -4
- package/dist/types/core/base-primitives.d.ts.map +1 -1
- package/dist/types/core/constants.d.ts +2 -1
- package/dist/types/core/constants.d.ts.map +1 -1
- package/dist/types/core/constructors.d.ts +1 -5
- package/dist/types/core/constructors.d.ts.map +1 -1
- package/dist/types/core/context/async-parser.d.ts.map +1 -1
- package/dist/types/core/context/deserializer.d.ts.map +1 -1
- package/dist/types/core/context/serializer.d.ts.map +1 -1
- package/dist/types/core/context/sync-parser.d.ts.map +1 -1
- package/dist/types/core/cross/index.d.ts.map +1 -1
- package/dist/types/core/errors.d.ts +5 -5
- package/dist/types/core/errors.d.ts.map +1 -1
- package/dist/types/core/plugin.d.ts +11 -7
- package/dist/types/core/plugin.d.ts.map +1 -1
- package/dist/types/core/sequence.d.ts +11 -0
- package/dist/types/core/sequence.d.ts.map +1 -0
- package/dist/types/core/types.d.ts +11 -4
- package/dist/types/core/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/core/base-primitives.ts +26 -3
- package/src/core/constants.ts +1 -0
- package/src/core/constructors.ts +1 -6
- package/src/core/context/async-parser.ts +31 -6
- package/src/core/context/deserializer.ts +20 -2
- package/src/core/context/serializer.ts +54 -0
- package/src/core/context/sync-parser.ts +29 -4
- package/src/core/cross/index.ts +2 -0
- package/src/core/errors.ts +6 -6
- package/src/core/plugin.ts +14 -9
- package/src/core/{utils/iterator-to-sequence.ts → sequence.ts} +20 -9
- package/src/core/types.ts +15 -4
- package/dist/types/core/utils/iterator-to-sequence.d.ts +0 -8
- package/dist/types/core/utils/iterator-to-sequence.d.ts.map +0 -1
|
@@ -29,6 +29,7 @@ import type {
|
|
|
29
29
|
SerovalPluginNode,
|
|
30
30
|
SerovalReferenceNode,
|
|
31
31
|
SerovalRegExpNode,
|
|
32
|
+
SerovalSequenceNode,
|
|
32
33
|
SerovalSetNode,
|
|
33
34
|
SerovalStreamConstructorNode,
|
|
34
35
|
SerovalStreamNextNode,
|
|
@@ -208,7 +209,7 @@ export function createReferenceNode<T>(
|
|
|
208
209
|
export function createPluginNode(
|
|
209
210
|
id: number,
|
|
210
211
|
tag: string,
|
|
211
|
-
value:
|
|
212
|
+
value: Record<string, SerovalNode>,
|
|
212
213
|
): SerovalPluginNode {
|
|
213
214
|
return createSerovalNode(
|
|
214
215
|
SerovalNodeType.Plugin,
|
|
@@ -394,7 +395,7 @@ export function createSetNode(
|
|
|
394
395
|
|
|
395
396
|
export function createIteratorFactoryInstanceNode(
|
|
396
397
|
factory: SerovalNodeWithID,
|
|
397
|
-
items:
|
|
398
|
+
items: SerovalNodeWithID,
|
|
398
399
|
): SerovalIteratorFactoryInstanceNode {
|
|
399
400
|
return createSerovalNode(
|
|
400
401
|
SerovalNodeType.IteratorFactoryInstance,
|
|
@@ -414,7 +415,7 @@ export function createIteratorFactoryInstanceNode(
|
|
|
414
415
|
|
|
415
416
|
export function createAsyncIteratorFactoryInstanceNode(
|
|
416
417
|
factory: SerovalNodeWithID,
|
|
417
|
-
items:
|
|
418
|
+
items: SerovalNodeWithID,
|
|
418
419
|
): SerovalAsyncIteratorFactoryInstanceNode {
|
|
419
420
|
return createSerovalNode(
|
|
420
421
|
SerovalNodeType.AsyncIteratorFactoryInstance,
|
|
@@ -512,3 +513,25 @@ export function createStreamReturnNode(
|
|
|
512
513
|
NIL,
|
|
513
514
|
);
|
|
514
515
|
}
|
|
516
|
+
|
|
517
|
+
export function createSequenceNode(
|
|
518
|
+
id: number,
|
|
519
|
+
sequence: SerovalNode[],
|
|
520
|
+
throwAt: number,
|
|
521
|
+
doneAt: number,
|
|
522
|
+
): SerovalSequenceNode {
|
|
523
|
+
return createSerovalNode(
|
|
524
|
+
SerovalNodeType.Sequence,
|
|
525
|
+
id,
|
|
526
|
+
throwAt,
|
|
527
|
+
NIL,
|
|
528
|
+
NIL,
|
|
529
|
+
NIL,
|
|
530
|
+
NIL,
|
|
531
|
+
sequence,
|
|
532
|
+
NIL,
|
|
533
|
+
NIL,
|
|
534
|
+
NIL,
|
|
535
|
+
doneAt,
|
|
536
|
+
);
|
|
537
|
+
}
|
package/src/core/constants.ts
CHANGED
package/src/core/constructors.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Sequence } from './sequence';
|
|
1
2
|
import type { Stream } from './stream';
|
|
2
3
|
|
|
3
4
|
type SpecialPromise = Promise<unknown> & { s?: 1 | 2; v?: unknown };
|
|
@@ -130,12 +131,6 @@ export const STREAM_CONSTRUCTOR = () => {
|
|
|
130
131
|
export const SERIALIZED_STREAM_CONSTRUCTOR =
|
|
131
132
|
/* @__PURE__ */ STREAM_CONSTRUCTOR.toString();
|
|
132
133
|
|
|
133
|
-
export interface Sequence {
|
|
134
|
-
v: unknown[];
|
|
135
|
-
t: number;
|
|
136
|
-
d: number;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
134
|
export const ITERATOR_CONSTRUCTOR =
|
|
140
135
|
(symbol: symbol) => (sequence: Sequence) => () => {
|
|
141
136
|
let index = 0;
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
createNumberNode,
|
|
13
13
|
createPluginNode,
|
|
14
14
|
createRegExpNode,
|
|
15
|
+
createSequenceNode,
|
|
15
16
|
createSetNode,
|
|
16
17
|
createStreamConstructorNode,
|
|
17
18
|
createStreamNextNode,
|
|
@@ -27,6 +28,11 @@ import { FALSE_NODE, NULL_NODE, TRUE_NODE, UNDEFINED_NODE } from '../literals';
|
|
|
27
28
|
import { createSerovalNode } from '../node';
|
|
28
29
|
import { OpaqueReference } from '../opaque-reference';
|
|
29
30
|
import type { SerovalMode } from '../plugin';
|
|
31
|
+
import {
|
|
32
|
+
createSequenceFromIterable,
|
|
33
|
+
isSequence,
|
|
34
|
+
type Sequence,
|
|
35
|
+
} from '../sequence';
|
|
30
36
|
import { SpecialReference } from '../special-reference';
|
|
31
37
|
import type { Stream } from '../stream';
|
|
32
38
|
import { createStreamFromAsyncIterable, isStream } from '../stream';
|
|
@@ -46,18 +52,19 @@ import type {
|
|
|
46
52
|
SerovalErrorNode,
|
|
47
53
|
SerovalMapNode,
|
|
48
54
|
SerovalNode,
|
|
55
|
+
SerovalNodeWithID,
|
|
49
56
|
SerovalNullConstructorNode,
|
|
50
57
|
SerovalObjectNode,
|
|
51
58
|
SerovalObjectRecordKey,
|
|
52
59
|
SerovalObjectRecordNode,
|
|
53
60
|
SerovalPluginNode,
|
|
54
61
|
SerovalPromiseNode,
|
|
62
|
+
SerovalSequenceNode,
|
|
55
63
|
SerovalSetNode,
|
|
56
64
|
SerovalStreamConstructorNode,
|
|
57
65
|
SerovalTypedArrayNode,
|
|
58
66
|
} from '../types';
|
|
59
67
|
import { getErrorOptions } from '../utils/error';
|
|
60
|
-
import { iteratorToSequence } from '../utils/iterator-to-sequence';
|
|
61
68
|
import promiseToResult from '../utils/promise-to-result';
|
|
62
69
|
import type {
|
|
63
70
|
BigIntTypedArrayValue,
|
|
@@ -155,11 +162,13 @@ async function parseProperties(
|
|
|
155
162
|
valueNodes.push(
|
|
156
163
|
createIteratorFactoryInstanceNode(
|
|
157
164
|
parseIteratorFactory(ctx.base),
|
|
158
|
-
await parseAsync(
|
|
165
|
+
(await parseAsync(
|
|
159
166
|
ctx,
|
|
160
167
|
depth,
|
|
161
|
-
|
|
162
|
-
|
|
168
|
+
createSequenceFromIterable(
|
|
169
|
+
properties as unknown as Iterable<unknown>,
|
|
170
|
+
),
|
|
171
|
+
)) as SerovalNodeWithID,
|
|
163
172
|
),
|
|
164
173
|
);
|
|
165
174
|
}
|
|
@@ -168,13 +177,13 @@ async function parseProperties(
|
|
|
168
177
|
valueNodes.push(
|
|
169
178
|
createAsyncIteratorFactoryInstanceNode(
|
|
170
179
|
parseAsyncIteratorFactory(ctx.base),
|
|
171
|
-
await parseAsync(
|
|
180
|
+
(await parseAsync(
|
|
172
181
|
ctx,
|
|
173
182
|
depth,
|
|
174
183
|
createStreamFromAsyncIterable(
|
|
175
184
|
properties as unknown as AsyncIterable<unknown>,
|
|
176
185
|
),
|
|
177
|
-
),
|
|
186
|
+
)) as SerovalNodeWithID,
|
|
178
187
|
),
|
|
179
188
|
);
|
|
180
189
|
}
|
|
@@ -435,6 +444,19 @@ async function parseStream(
|
|
|
435
444
|
);
|
|
436
445
|
}
|
|
437
446
|
|
|
447
|
+
async function parseSequence(
|
|
448
|
+
ctx: AsyncParserContext,
|
|
449
|
+
depth: number,
|
|
450
|
+
id: number,
|
|
451
|
+
current: Sequence,
|
|
452
|
+
): Promise<SerovalSequenceNode> {
|
|
453
|
+
const nodes: SerovalNode[] = [];
|
|
454
|
+
for (let i = 0, len = current.v.length; i < len; i++) {
|
|
455
|
+
nodes[i] = await parseAsync(ctx, depth, current.v[i]);
|
|
456
|
+
}
|
|
457
|
+
return createSequenceNode(id, nodes, current.t, current.d);
|
|
458
|
+
}
|
|
459
|
+
|
|
438
460
|
export async function parseObjectAsync(
|
|
439
461
|
ctx: AsyncParserContext,
|
|
440
462
|
depth: number,
|
|
@@ -447,6 +469,9 @@ export async function parseObjectAsync(
|
|
|
447
469
|
if (isStream(current)) {
|
|
448
470
|
return parseStream(ctx, depth, id, current);
|
|
449
471
|
}
|
|
472
|
+
if (isSequence(current)) {
|
|
473
|
+
return parseSequence(ctx, depth, id, current);
|
|
474
|
+
}
|
|
450
475
|
const currentClass = current.constructor;
|
|
451
476
|
if (currentClass === OpaqueReference) {
|
|
452
477
|
return parseAsync(
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
import type { PluginAccessOptions } from '../plugin';
|
|
24
24
|
import { SerovalMode } from '../plugin';
|
|
25
25
|
import { getReference } from '../reference';
|
|
26
|
+
import { createSequence, type Sequence, sequenceToIterator } from '../sequence';
|
|
26
27
|
import type { Stream } from '../stream';
|
|
27
28
|
import { createStream, isStream, streamToAsyncIterable } from '../stream';
|
|
28
29
|
import { deserializeString } from '../string';
|
|
@@ -57,6 +58,7 @@ import type {
|
|
|
57
58
|
SerovalPromiseResolveNode,
|
|
58
59
|
SerovalReferenceNode,
|
|
59
60
|
SerovalRegExpNode,
|
|
61
|
+
SerovalSequenceNode,
|
|
60
62
|
SerovalSetNode,
|
|
61
63
|
SerovalStreamConstructorNode,
|
|
62
64
|
SerovalStreamNextNode,
|
|
@@ -64,8 +66,6 @@ import type {
|
|
|
64
66
|
SerovalStreamThrowNode,
|
|
65
67
|
SerovalTypedArrayNode,
|
|
66
68
|
} from '../types';
|
|
67
|
-
import type { Sequence } from '../utils/iterator-to-sequence';
|
|
68
|
-
import { sequenceToIterator } from '../utils/iterator-to-sequence';
|
|
69
69
|
import type {
|
|
70
70
|
BigIntTypedArrayValue,
|
|
71
71
|
TypedArrayValue,
|
|
@@ -713,6 +713,22 @@ function deserializeAsyncIteratorFactory(
|
|
|
713
713
|
return NIL;
|
|
714
714
|
}
|
|
715
715
|
|
|
716
|
+
function deserializeSequence(
|
|
717
|
+
ctx: DeserializerContext,
|
|
718
|
+
depth: number,
|
|
719
|
+
node: SerovalSequenceNode,
|
|
720
|
+
): Sequence {
|
|
721
|
+
const result = assignIndexedValue(
|
|
722
|
+
ctx,
|
|
723
|
+
node.i,
|
|
724
|
+
createSequence([], node.s, node.l),
|
|
725
|
+
);
|
|
726
|
+
for (let i = 0, len = node.a.length; i < len; i++) {
|
|
727
|
+
result.v[i] = deserialize(ctx, depth, node.a[i]);
|
|
728
|
+
}
|
|
729
|
+
return result;
|
|
730
|
+
}
|
|
731
|
+
|
|
716
732
|
function deserialize(
|
|
717
733
|
ctx: DeserializerContext,
|
|
718
734
|
depth: number,
|
|
@@ -793,6 +809,8 @@ function deserialize(
|
|
|
793
809
|
case SerovalNodeType.AsyncIteratorFactory:
|
|
794
810
|
return deserializeAsyncIteratorFactory(ctx, depth, node);
|
|
795
811
|
// case SerovalNodeType.SpecialReference:
|
|
812
|
+
case SerovalNodeType.Sequence:
|
|
813
|
+
return deserializeSequence(ctx, depth, node);
|
|
796
814
|
default:
|
|
797
815
|
throw new SerovalUnsupportedNodeError(node);
|
|
798
816
|
}
|
|
@@ -50,6 +50,7 @@ import type {
|
|
|
50
50
|
SerovalPromiseResolveNode,
|
|
51
51
|
SerovalReferenceNode,
|
|
52
52
|
SerovalRegExpNode,
|
|
53
|
+
SerovalSequenceNode,
|
|
53
54
|
SerovalSetNode,
|
|
54
55
|
SerovalStreamConstructorNode,
|
|
55
56
|
SerovalStreamNextNode,
|
|
@@ -484,6 +485,15 @@ function createObjectAssign(
|
|
|
484
485
|
createAssignment(ctx.base, getRefParam(ctx, ref) + '.' + key, value);
|
|
485
486
|
}
|
|
486
487
|
|
|
488
|
+
function createSequenceAssign(
|
|
489
|
+
ctx: SerializerContext,
|
|
490
|
+
ref: number,
|
|
491
|
+
index: number | string,
|
|
492
|
+
value: string,
|
|
493
|
+
): void {
|
|
494
|
+
createAssignment(ctx.base, getRefParam(ctx, ref) + '.v[' + index + ']', value);
|
|
495
|
+
}
|
|
496
|
+
|
|
487
497
|
/**
|
|
488
498
|
* Checks if the value is in the stack. Stack here is a reference
|
|
489
499
|
* structure to know if a object is to be accessed in a TDZ.
|
|
@@ -1239,6 +1249,48 @@ function serializeStreamReturn(
|
|
|
1239
1249
|
return getRefParam(ctx, node.i) + '.return(' + serialize(ctx, node.f) + ')';
|
|
1240
1250
|
}
|
|
1241
1251
|
|
|
1252
|
+
function serializeSequenceItem(
|
|
1253
|
+
ctx: SerializerContext,
|
|
1254
|
+
id: number,
|
|
1255
|
+
index: number,
|
|
1256
|
+
item: SerovalNode,
|
|
1257
|
+
): string {
|
|
1258
|
+
const base = ctx.base;
|
|
1259
|
+
if (isIndexedValueInStack(base, item)) {
|
|
1260
|
+
markSerializerRef(base, id);
|
|
1261
|
+
createSequenceAssign(
|
|
1262
|
+
ctx,
|
|
1263
|
+
id,
|
|
1264
|
+
index,
|
|
1265
|
+
getRefParam(ctx, (item as SerovalIndexedValueNode).i),
|
|
1266
|
+
);
|
|
1267
|
+
return '';
|
|
1268
|
+
}
|
|
1269
|
+
return serialize(ctx, item);
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
function serializeSequence(
|
|
1273
|
+
ctx: SerializerContext,
|
|
1274
|
+
node: SerovalSequenceNode,
|
|
1275
|
+
): string {
|
|
1276
|
+
const items = node.a;
|
|
1277
|
+
const size = items.length;
|
|
1278
|
+
const id = node.i;
|
|
1279
|
+
if (size > 0) {
|
|
1280
|
+
ctx.base.stack.push(id);
|
|
1281
|
+
let result = serializeSequenceItem(ctx, id, 0, items[0]);
|
|
1282
|
+
for (let i = 1, item = result; i < size; i++) {
|
|
1283
|
+
item = serializeSequenceItem(ctx, id, i, items[i]);
|
|
1284
|
+
result += (item && result && ',') + item;
|
|
1285
|
+
}
|
|
1286
|
+
ctx.base.stack.pop();
|
|
1287
|
+
if (result) {
|
|
1288
|
+
return '{__SEROVAL_SEQUENCE__:!0,v:[' + result + '],t:' + node.s + ',d:' + node.l + '}';
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
return '{__SEROVAL_SEQUENCE__:!0,v:[],t:-1,d:0}';
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1242
1294
|
function serializeAssignable(
|
|
1243
1295
|
ctx: SerializerContext,
|
|
1244
1296
|
node: SerovalNode,
|
|
@@ -1283,6 +1335,8 @@ function serializeAssignable(
|
|
|
1283
1335
|
return serializePlugin(ctx, node);
|
|
1284
1336
|
case SerovalNodeType.SpecialReference:
|
|
1285
1337
|
return SPECIAL_REF_STRING[node.s];
|
|
1338
|
+
case SerovalNodeType.Sequence:
|
|
1339
|
+
return serializeSequence(ctx, node);
|
|
1286
1340
|
default:
|
|
1287
1341
|
throw new SerovalUnsupportedNodeError(node);
|
|
1288
1342
|
}
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
createNumberNode,
|
|
13
13
|
createPluginNode,
|
|
14
14
|
createRegExpNode,
|
|
15
|
+
createSequenceNode,
|
|
15
16
|
createSetNode,
|
|
16
17
|
createStreamConstructorNode,
|
|
17
18
|
createStreamNextNode,
|
|
@@ -31,6 +32,11 @@ import { FALSE_NODE, NULL_NODE, TRUE_NODE, UNDEFINED_NODE } from '../literals';
|
|
|
31
32
|
import { createSerovalNode } from '../node';
|
|
32
33
|
import { OpaqueReference } from '../opaque-reference';
|
|
33
34
|
import { type Plugin, SerovalMode } from '../plugin';
|
|
35
|
+
import {
|
|
36
|
+
createSequenceFromIterable,
|
|
37
|
+
isSequence,
|
|
38
|
+
type Sequence,
|
|
39
|
+
} from '../sequence';
|
|
34
40
|
import { SpecialReference } from '../special-reference';
|
|
35
41
|
import type { Stream } from '../stream';
|
|
36
42
|
import {
|
|
@@ -54,17 +60,18 @@ import type {
|
|
|
54
60
|
SerovalErrorNode,
|
|
55
61
|
SerovalMapNode,
|
|
56
62
|
SerovalNode,
|
|
63
|
+
SerovalNodeWithID,
|
|
57
64
|
SerovalNullConstructorNode,
|
|
58
65
|
SerovalObjectNode,
|
|
59
66
|
SerovalObjectRecordKey,
|
|
60
67
|
SerovalObjectRecordNode,
|
|
61
68
|
SerovalPluginNode,
|
|
62
69
|
SerovalPromiseConstructorNode,
|
|
70
|
+
SerovalSequenceNode,
|
|
63
71
|
SerovalSetNode,
|
|
64
72
|
SerovalTypedArrayNode,
|
|
65
73
|
} from '../types';
|
|
66
74
|
import { getErrorOptions } from '../utils/error';
|
|
67
|
-
import { iteratorToSequence } from '../utils/iterator-to-sequence';
|
|
68
75
|
import type {
|
|
69
76
|
BigIntTypedArrayValue,
|
|
70
77
|
TypedArrayValue,
|
|
@@ -255,8 +262,10 @@ function parseProperties(
|
|
|
255
262
|
parseSOS(
|
|
256
263
|
ctx,
|
|
257
264
|
depth,
|
|
258
|
-
|
|
259
|
-
|
|
265
|
+
createSequenceFromIterable(
|
|
266
|
+
properties as unknown as Iterable<unknown>,
|
|
267
|
+
),
|
|
268
|
+
) as SerovalNodeWithID,
|
|
260
269
|
),
|
|
261
270
|
);
|
|
262
271
|
}
|
|
@@ -273,7 +282,7 @@ function parseProperties(
|
|
|
273
282
|
: createStreamFromAsyncIterable(
|
|
274
283
|
properties as unknown as AsyncIterable<unknown>,
|
|
275
284
|
),
|
|
276
|
-
),
|
|
285
|
+
) as SerovalNodeWithID,
|
|
277
286
|
),
|
|
278
287
|
);
|
|
279
288
|
}
|
|
@@ -599,6 +608,19 @@ function parsePlugin(
|
|
|
599
608
|
return NIL;
|
|
600
609
|
}
|
|
601
610
|
|
|
611
|
+
function parseSequence(
|
|
612
|
+
ctx: SOSParserContext,
|
|
613
|
+
depth: number,
|
|
614
|
+
id: number,
|
|
615
|
+
current: Sequence,
|
|
616
|
+
): SerovalSequenceNode {
|
|
617
|
+
const nodes: SerovalNode[] = [];
|
|
618
|
+
for (let i = 0, len = current.v.length; i < len; i++) {
|
|
619
|
+
nodes[i] = parseSOS(ctx, depth, current.v[i]);
|
|
620
|
+
}
|
|
621
|
+
return createSequenceNode(id, nodes, current.t, current.d);
|
|
622
|
+
}
|
|
623
|
+
|
|
602
624
|
function parseObjectPhase2(
|
|
603
625
|
ctx: SOSParserContext,
|
|
604
626
|
depth: number,
|
|
@@ -733,6 +755,9 @@ function parseObject(
|
|
|
733
755
|
if (isStream(current)) {
|
|
734
756
|
return parseStream(ctx, depth, id, current);
|
|
735
757
|
}
|
|
758
|
+
if (isSequence(current)) {
|
|
759
|
+
return parseSequence(ctx, depth, id, current);
|
|
760
|
+
}
|
|
736
761
|
const currentClass = current.constructor;
|
|
737
762
|
if (currentClass === OpaqueReference) {
|
|
738
763
|
return parseSOS(
|
package/src/core/cross/index.ts
CHANGED
|
@@ -161,6 +161,7 @@ export function toCrossJSONStream<T>(
|
|
|
161
161
|
plugins,
|
|
162
162
|
refs: options.refs,
|
|
163
163
|
disabledFeatures: options.disabledFeatures,
|
|
164
|
+
depthLimit: options.depthLimit,
|
|
164
165
|
onParse: options.onParse,
|
|
165
166
|
onError: options.onError,
|
|
166
167
|
onDone: options.onDone,
|
|
@@ -183,6 +184,7 @@ export function fromCrossJSON<T>(
|
|
|
183
184
|
refs: options.refs,
|
|
184
185
|
features: options.features,
|
|
185
186
|
disabledFeatures: options.disabledFeatures,
|
|
187
|
+
depthLimit: options.depthLimit,
|
|
186
188
|
});
|
|
187
189
|
return deserializeTop(ctx, source) as T;
|
|
188
190
|
}
|
package/src/core/errors.ts
CHANGED
|
@@ -11,10 +11,10 @@ const enum StepErrorCodes {
|
|
|
11
11
|
Deserialize = 3,
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
function getErrorMessageDev(type: string, cause:
|
|
14
|
+
function getErrorMessageDev(type: string, cause: unknown): string {
|
|
15
15
|
if (cause instanceof Error) {
|
|
16
16
|
return `Seroval caught an error during the ${type} process.
|
|
17
|
-
|
|
17
|
+
|
|
18
18
|
${cause.name}
|
|
19
19
|
${cause.message}
|
|
20
20
|
|
|
@@ -46,26 +46,26 @@ const getErrorMessage = (type: string, cause: any) =>
|
|
|
46
46
|
export class SerovalError extends Error {
|
|
47
47
|
constructor(
|
|
48
48
|
type: string,
|
|
49
|
-
public cause:
|
|
49
|
+
public cause: unknown,
|
|
50
50
|
) {
|
|
51
51
|
super(getErrorMessage(type, cause));
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
export class SerovalParserError extends SerovalError {
|
|
56
|
-
constructor(cause:
|
|
56
|
+
constructor(cause: unknown) {
|
|
57
57
|
super('parsing', cause);
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
export class SerovalSerializationError extends SerovalError {
|
|
62
|
-
constructor(cause:
|
|
62
|
+
constructor(cause: unknown) {
|
|
63
63
|
super('serialization', cause);
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
export class SerovalDeserializationError extends SerovalError {
|
|
68
|
-
constructor(cause:
|
|
68
|
+
constructor(cause: unknown) {
|
|
69
69
|
super('deserialization', cause);
|
|
70
70
|
}
|
|
71
71
|
}
|
package/src/core/plugin.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
StreamParsePluginContext,
|
|
6
6
|
SyncParsePluginContext,
|
|
7
7
|
} from './context/sync-parser';
|
|
8
|
+
import type { SerovalNode } from './types';
|
|
8
9
|
|
|
9
10
|
export const enum SerovalMode {
|
|
10
11
|
Vanilla = 1,
|
|
@@ -15,7 +16,11 @@ export interface PluginData {
|
|
|
15
16
|
id: number;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
export
|
|
19
|
+
export type PluginInfo = {
|
|
20
|
+
[key: string]: SerovalNode;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export interface Plugin<Value, Info extends PluginInfo> {
|
|
19
24
|
/**
|
|
20
25
|
* A unique string that helps idenfity the plugin
|
|
21
26
|
*/
|
|
@@ -37,35 +42,35 @@ export interface Plugin<Value, Node> {
|
|
|
37
42
|
value: Value,
|
|
38
43
|
ctx: SyncParsePluginContext,
|
|
39
44
|
data: PluginData,
|
|
40
|
-
) =>
|
|
45
|
+
) => Info;
|
|
41
46
|
async?: (
|
|
42
47
|
value: Value,
|
|
43
48
|
ctx: AsyncParsePluginContext,
|
|
44
49
|
data: PluginData,
|
|
45
|
-
) => Promise<
|
|
50
|
+
) => Promise<Info>;
|
|
46
51
|
stream?: (
|
|
47
52
|
value: Value,
|
|
48
53
|
ctx: StreamParsePluginContext,
|
|
49
54
|
data: PluginData,
|
|
50
|
-
) =>
|
|
55
|
+
) => Info;
|
|
51
56
|
};
|
|
52
57
|
/**
|
|
53
58
|
* Convert the parsed node into a JS string
|
|
54
59
|
*/
|
|
55
|
-
serialize(node:
|
|
60
|
+
serialize(node: Info, ctx: SerializePluginContext, data: PluginData): string;
|
|
56
61
|
/**
|
|
57
62
|
* Convert the parsed node into its runtime equivalent.
|
|
58
63
|
*/
|
|
59
64
|
deserialize(
|
|
60
|
-
node:
|
|
65
|
+
node: Info,
|
|
61
66
|
ctx: DeserializePluginContext,
|
|
62
67
|
data: PluginData,
|
|
63
68
|
): Value;
|
|
64
69
|
}
|
|
65
70
|
|
|
66
|
-
export function createPlugin<Value,
|
|
67
|
-
plugin: Plugin<Value,
|
|
68
|
-
): Plugin<Value,
|
|
71
|
+
export function createPlugin<Value, Info extends PluginInfo>(
|
|
72
|
+
plugin: Plugin<Value, Info>,
|
|
73
|
+
): Plugin<Value, Info> {
|
|
69
74
|
return plugin;
|
|
70
75
|
}
|
|
71
76
|
|
|
@@ -1,17 +1,32 @@
|
|
|
1
|
-
import { ITERATOR_CONSTRUCTOR } from
|
|
2
|
-
import { SYM_ITERATOR } from
|
|
1
|
+
import { ITERATOR_CONSTRUCTOR } from "./constructors";
|
|
2
|
+
import { SYM_ITERATOR } from "./symbols";
|
|
3
3
|
|
|
4
4
|
export interface Sequence {
|
|
5
|
+
__SEROVAL_SEQUENCE__: true;
|
|
6
|
+
|
|
5
7
|
v: unknown[];
|
|
6
8
|
t: number;
|
|
7
9
|
d: number;
|
|
8
10
|
}
|
|
9
11
|
|
|
10
|
-
export function
|
|
12
|
+
export function isSequence(value: object): value is Sequence {
|
|
13
|
+
return '__SEROVAL_SEQUENCE__' in value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function createSequence(values: unknown[], throwAt: number, doneAt: number): Sequence {
|
|
17
|
+
return {
|
|
18
|
+
__SEROVAL_SEQUENCE__: true,
|
|
19
|
+
|
|
20
|
+
v: values,
|
|
21
|
+
t: throwAt,
|
|
22
|
+
d: doneAt,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function createSequenceFromIterable<T>(source: Iterable<T>): Sequence {
|
|
11
27
|
const values: unknown[] = [];
|
|
12
28
|
let throwsAt = -1;
|
|
13
29
|
let doneAt = -1;
|
|
14
|
-
|
|
15
30
|
const iterator = source[SYM_ITERATOR]();
|
|
16
31
|
|
|
17
32
|
while (true) {
|
|
@@ -28,11 +43,7 @@ export function iteratorToSequence<T>(source: Iterable<T>): Sequence {
|
|
|
28
43
|
}
|
|
29
44
|
}
|
|
30
45
|
|
|
31
|
-
return
|
|
32
|
-
v: values,
|
|
33
|
-
t: throwsAt,
|
|
34
|
-
d: doneAt,
|
|
35
|
-
};
|
|
46
|
+
return createSequence(values, throwsAt, doneAt);
|
|
36
47
|
}
|
|
37
48
|
|
|
38
49
|
const createIterator = ITERATOR_CONSTRUCTOR(SYM_ITERATOR);
|
package/src/core/types.ts
CHANGED
|
@@ -261,7 +261,7 @@ export interface SerovalPluginNode extends SerovalBaseNode {
|
|
|
261
261
|
t: SerovalNodeType.Plugin;
|
|
262
262
|
i: number;
|
|
263
263
|
// value
|
|
264
|
-
s:
|
|
264
|
+
s: Record<string, SerovalNode>;
|
|
265
265
|
// tag name
|
|
266
266
|
c: string;
|
|
267
267
|
}
|
|
@@ -283,7 +283,7 @@ export interface SerovalIteratorFactoryNode extends SerovalBaseNode {
|
|
|
283
283
|
|
|
284
284
|
export interface SerovalIteratorFactoryInstanceNode extends SerovalBaseNode {
|
|
285
285
|
t: SerovalNodeType.IteratorFactoryInstance;
|
|
286
|
-
a: [instance: SerovalNodeWithID, sequence:
|
|
286
|
+
a: [instance: SerovalNodeWithID, sequence: SerovalNodeWithID];
|
|
287
287
|
}
|
|
288
288
|
|
|
289
289
|
export interface SerovalAsyncIteratorFactoryNode extends SerovalBaseNode {
|
|
@@ -295,7 +295,7 @@ export interface SerovalAsyncIteratorFactoryNode extends SerovalBaseNode {
|
|
|
295
295
|
export interface SerovalAsyncIteratorFactoryInstanceNode
|
|
296
296
|
extends SerovalBaseNode {
|
|
297
297
|
t: SerovalNodeType.AsyncIteratorFactoryInstance;
|
|
298
|
-
a: [instance: SerovalNodeWithID, sequence:
|
|
298
|
+
a: [instance: SerovalNodeWithID, sequence: SerovalNodeWithID];
|
|
299
299
|
}
|
|
300
300
|
|
|
301
301
|
export interface SerovalStreamConstructorNode extends SerovalBaseNode {
|
|
@@ -327,6 +327,16 @@ export interface SerovalStreamReturnNode extends SerovalBaseNode {
|
|
|
327
327
|
f: SerovalNode;
|
|
328
328
|
}
|
|
329
329
|
|
|
330
|
+
export interface SerovalSequenceNode extends SerovalBaseNode {
|
|
331
|
+
t: SerovalNodeType.Sequence;
|
|
332
|
+
i: number;
|
|
333
|
+
// Throw at
|
|
334
|
+
s: number;
|
|
335
|
+
a: SerovalNode[];
|
|
336
|
+
// Done at
|
|
337
|
+
l: number;
|
|
338
|
+
}
|
|
339
|
+
|
|
330
340
|
export type SerovalSyncNode =
|
|
331
341
|
| SerovalPrimitiveNode
|
|
332
342
|
| SerovalIndexedValueNode
|
|
@@ -349,7 +359,8 @@ export type SerovalSyncNode =
|
|
|
349
359
|
| SerovalIteratorFactoryNode
|
|
350
360
|
| SerovalIteratorFactoryInstanceNode
|
|
351
361
|
| SerovalAsyncIteratorFactoryNode
|
|
352
|
-
| SerovalAsyncIteratorFactoryInstanceNode
|
|
362
|
+
| SerovalAsyncIteratorFactoryInstanceNode
|
|
363
|
+
| SerovalSequenceNode;
|
|
353
364
|
|
|
354
365
|
export type SerovalAsyncNode =
|
|
355
366
|
| SerovalPromiseNode
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export interface Sequence {
|
|
2
|
-
v: unknown[];
|
|
3
|
-
t: number;
|
|
4
|
-
d: number;
|
|
5
|
-
}
|
|
6
|
-
export declare function iteratorToSequence<T>(source: Iterable<T>): Sequence;
|
|
7
|
-
export declare function sequenceToIterator<T>(sequence: Sequence): () => IterableIterator<T>;
|
|
8
|
-
//# sourceMappingURL=iterator-to-sequence.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"iterator-to-sequence.d.ts","sourceRoot":"","sources":["../../../../src/core/utils/iterator-to-sequence.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,QAAQ;IACvB,CAAC,EAAE,OAAO,EAAE,CAAC;IACb,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACX;AAED,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,CA0BnE;AAID,wBAAgB,kBAAkB,CAAC,CAAC,EAClC,QAAQ,EAAE,QAAQ,GACjB,MAAM,gBAAgB,CAAC,CAAC,CAAC,CAE3B"}
|