seroval 1.7.8 → 1.7.10
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/dev/index.cjs +129 -29
- package/dist/dev/index.js +129 -29
- package/dist/index.cjs +129 -29
- package/dist/index.js +129 -29
- package/package.json +1 -1
package/dist/dev/index.cjs
CHANGED
|
@@ -482,6 +482,14 @@ function createBoxedNode(id, boxed) {
|
|
|
482
482
|
return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed);
|
|
483
483
|
}
|
|
484
484
|
const MAX_TYPED_ARRAY_LENGTH = 1e6;
|
|
485
|
+
/**
|
|
486
|
+
* Rejects a sparse array with more holes than the typed array limit. Each hole
|
|
487
|
+
* still costs a node and output, however few elements the array holds.
|
|
488
|
+
*/
|
|
489
|
+
function assertArrayHoles(current) {
|
|
490
|
+
const len = current.length;
|
|
491
|
+
if (len > MAX_TYPED_ARRAY_LENGTH && len - Object.keys(current).length > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
|
|
492
|
+
}
|
|
485
493
|
function createViewNode(type, id, current, view, buffer) {
|
|
486
494
|
const isDataView = type === 20;
|
|
487
495
|
const size = view.length;
|
|
@@ -1273,6 +1281,15 @@ function createIndexForValue(ctx, current) {
|
|
|
1273
1281
|
ctx.refs.set(current, id);
|
|
1274
1282
|
return id;
|
|
1275
1283
|
}
|
|
1284
|
+
/**
|
|
1285
|
+
* Removes the ids registered from `size` on. A parse that fails emits none of
|
|
1286
|
+
* them, so a caller-owned map must not keep them for later calls.
|
|
1287
|
+
*/
|
|
1288
|
+
function rollbackRefs(refs, size) {
|
|
1289
|
+
if (refs.size > size) {
|
|
1290
|
+
for (const [value, id] of refs) if (id >= size) refs.delete(value);
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1276
1293
|
function getNodeForIndexedValue(ctx, current) {
|
|
1277
1294
|
const registeredId = ctx.refs.get(current);
|
|
1278
1295
|
if (registeredId != null) {
|
|
@@ -1371,6 +1388,12 @@ function getObjectKind(current, currentClass, features) {
|
|
|
1371
1388
|
if (features & 16 && (currentClass === BigInt64Array || currentClass === BigUint64Array)) return 9;
|
|
1372
1389
|
if (features & 1 && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) return 5;
|
|
1373
1390
|
if (features & 64 && typeof Temporal !== "undefined" && getTemporalType(currentClass) != null) return 15;
|
|
1391
|
+
if (ArrayBuffer.isView(current)) {
|
|
1392
|
+
const name = getTypedArrayName(current);
|
|
1393
|
+
if (name === void 0) return 10;
|
|
1394
|
+
if (name !== "BigInt64Array" && name !== "BigUint64Array") return 8;
|
|
1395
|
+
if (features & 16) return 9;
|
|
1396
|
+
}
|
|
1374
1397
|
if (current instanceof Error) return 4;
|
|
1375
1398
|
if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) return 16;
|
|
1376
1399
|
return 0;
|
|
@@ -1411,11 +1434,29 @@ function createArrayBufferNode(ctx, id, current) {
|
|
|
1411
1434
|
function createAsyncParserContext(options) {
|
|
1412
1435
|
return { base: createBaseParserContext(options) };
|
|
1413
1436
|
}
|
|
1437
|
+
function ignoreRejection$1() {}
|
|
1438
|
+
/**
|
|
1439
|
+
* A promise gets its rejection handler only when the walk reaches it. While an
|
|
1440
|
+
* earlier sibling is still pending, a later one that rejects would be an
|
|
1441
|
+
* unhandled rejection, which ends a Node.js process by default. Mark the
|
|
1442
|
+
* promises among the children as handled before walking them.
|
|
1443
|
+
*/
|
|
1444
|
+
function handleChildPromises(values) {
|
|
1445
|
+
for (const value of values) if (value instanceof Promise) Promise.prototype.then.call(value, void 0, ignoreRejection$1);
|
|
1446
|
+
}
|
|
1414
1447
|
async function parseItems$1(ctx, depth, current) {
|
|
1448
|
+
assertArrayHoles(current);
|
|
1415
1449
|
const len = current.length;
|
|
1416
1450
|
const nodes = new Array(len);
|
|
1417
|
-
|
|
1418
|
-
|
|
1451
|
+
const values = new Array(len);
|
|
1452
|
+
const present = new Array(len);
|
|
1453
|
+
for (let i = 0; i < len; i++) {
|
|
1454
|
+
present[i] = i in current;
|
|
1455
|
+
if (present[i]) values[i] = current[i];
|
|
1456
|
+
}
|
|
1457
|
+
handleChildPromises(values);
|
|
1458
|
+
for (let i = 0, node; i < len; i++) if (present[i]) {
|
|
1459
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1419
1460
|
nodes[i] = node instanceof Promise ? await node : node;
|
|
1420
1461
|
} else nodes[i] = 0;
|
|
1421
1462
|
return nodes;
|
|
@@ -1428,10 +1469,12 @@ async function parseProperties$1(ctx, depth, properties) {
|
|
|
1428
1469
|
const len = keys.length;
|
|
1429
1470
|
const keyNodes = new Array(len);
|
|
1430
1471
|
const valueNodes = new Array(len);
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1472
|
+
const values = new Array(len);
|
|
1473
|
+
for (let i = 0; i < len; i++) values[i] = properties[keys[i]];
|
|
1474
|
+
handleChildPromises(values);
|
|
1475
|
+
for (let i = 0, node; i < len; i++) {
|
|
1476
|
+
keyNodes[i] = serializeString(keys[i]);
|
|
1477
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1435
1478
|
valueNodes[i] = node instanceof Promise ? await node : node;
|
|
1436
1479
|
}
|
|
1437
1480
|
if (SYM_ITERATOR in properties) {
|
|
@@ -1472,26 +1515,32 @@ async function parseError$1(ctx, depth, type, id, current) {
|
|
|
1472
1515
|
}
|
|
1473
1516
|
async function parseMap$1(ctx, depth, id, current) {
|
|
1474
1517
|
const sentinel = parseMapSentinel(ctx.base);
|
|
1475
|
-
const
|
|
1476
|
-
const
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1518
|
+
const entries = [...current.entries()];
|
|
1519
|
+
const keyNodes = new Array(entries.length);
|
|
1520
|
+
const valueNodes = new Array(entries.length);
|
|
1521
|
+
const keys = new Array(entries.length);
|
|
1522
|
+
const values = new Array(entries.length);
|
|
1523
|
+
for (let i = 0; i < entries.length; i++) {
|
|
1524
|
+
keys[i] = entries[i][0];
|
|
1525
|
+
values[i] = entries[i][1];
|
|
1526
|
+
}
|
|
1527
|
+
handleChildPromises(keys);
|
|
1528
|
+
handleChildPromises(values);
|
|
1529
|
+
for (let i = 0, node; i < entries.length; i++) {
|
|
1530
|
+
node = parseValueAsync(ctx, depth, keys[i]);
|
|
1481
1531
|
keyNodes[i] = node instanceof Promise ? await node : node;
|
|
1482
|
-
node = parseValueAsync(ctx, depth,
|
|
1532
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1483
1533
|
valueNodes[i] = node instanceof Promise ? await node : node;
|
|
1484
|
-
i++;
|
|
1485
1534
|
}
|
|
1486
1535
|
return createMapNode(id, sentinel, keyNodes, valueNodes);
|
|
1487
1536
|
}
|
|
1488
1537
|
async function parseSet$1(ctx, depth, id, current) {
|
|
1489
|
-
const
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
for (
|
|
1493
|
-
node = parseValueAsync(ctx, depth,
|
|
1494
|
-
items[i
|
|
1538
|
+
const values = [...current.keys()];
|
|
1539
|
+
const items = new Array(values.length);
|
|
1540
|
+
handleChildPromises(values);
|
|
1541
|
+
for (let i = 0, node; i < values.length; i++) {
|
|
1542
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1543
|
+
items[i] = node instanceof Promise ? await node : node;
|
|
1495
1544
|
}
|
|
1496
1545
|
return createSetNode(id, items);
|
|
1497
1546
|
}
|
|
@@ -1660,9 +1709,12 @@ async function parseAsync(ctx, depth, current) {
|
|
|
1660
1709
|
return await parseValueAsync(ctx, depth, current);
|
|
1661
1710
|
}
|
|
1662
1711
|
async function parseTopAsync(ctx, current) {
|
|
1712
|
+
const refs = ctx.base.refs;
|
|
1713
|
+
const size = refs.size;
|
|
1663
1714
|
try {
|
|
1664
1715
|
return await parseAsync(ctx, 0, current);
|
|
1665
1716
|
} catch (error) {
|
|
1717
|
+
rollbackRefs(refs, size);
|
|
1666
1718
|
throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
|
|
1667
1719
|
}
|
|
1668
1720
|
}
|
|
@@ -1703,6 +1755,8 @@ function applyObjectFlag(obj, flag) {
|
|
|
1703
1755
|
}
|
|
1704
1756
|
}
|
|
1705
1757
|
const DEFAULT_DEPTH_LIMIT = 1e3;
|
|
1758
|
+
const MAX_REGEXP_PROPERTY_ESCAPES = 32;
|
|
1759
|
+
const REGEXP_PROPERTY_ESCAPE = /\\[pP]\{/g;
|
|
1706
1760
|
function createBaseDeserializerContext(options) {
|
|
1707
1761
|
const maxBase64Length = options.maxBase64Length ?? DEFAULT_MAX_BASE64_LENGTH;
|
|
1708
1762
|
if (!Number.isSafeInteger(maxBase64Length) || maxBase64Length < 0) throw new RangeError("maxBase64Length must be a non-negative safe integer");
|
|
@@ -1713,7 +1767,8 @@ function createBaseDeserializerContext(options) {
|
|
|
1713
1767
|
refs,
|
|
1714
1768
|
features: options.features ?? 127 ^ (options.disabledFeatures ?? 0),
|
|
1715
1769
|
depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT,
|
|
1716
|
-
maxBase64Length
|
|
1770
|
+
maxBase64Length,
|
|
1771
|
+
regexpPropertyEscapes: MAX_REGEXP_PROPERTY_ESCAPES
|
|
1717
1772
|
};
|
|
1718
1773
|
}
|
|
1719
1774
|
function createVanillaDeserializerContext(options) {
|
|
@@ -1825,6 +1880,13 @@ function deserializeRegExp(ctx, node) {
|
|
|
1825
1880
|
if (ctx.base.features & 32) {
|
|
1826
1881
|
const source = deserializeString(node.c);
|
|
1827
1882
|
if (source.length > MAX_REGEXP_SOURCE_LENGTH) throw new SerovalMalformedNodeError(node);
|
|
1883
|
+
if (/[uv]/.test(String(node.m))) {
|
|
1884
|
+
const escapes = source.match(REGEXP_PROPERTY_ESCAPE);
|
|
1885
|
+
if (escapes) {
|
|
1886
|
+
ctx.base.regexpPropertyEscapes -= escapes.length;
|
|
1887
|
+
if (ctx.base.regexpPropertyEscapes < 0) throw new SerovalMalformedNodeError(node);
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1828
1890
|
return assignIndexedValue$1(ctx, node.i, new RegExp(source, node.m));
|
|
1829
1891
|
}
|
|
1830
1892
|
throw new SerovalUnsupportedNodeError(node);
|
|
@@ -2007,6 +2069,7 @@ function deserialize$1(ctx, depth, node) {
|
|
|
2007
2069
|
}
|
|
2008
2070
|
}
|
|
2009
2071
|
function deserializeTop(ctx, node) {
|
|
2072
|
+
ctx.base.regexpPropertyEscapes = MAX_REGEXP_PROPERTY_ESCAPES;
|
|
2010
2073
|
try {
|
|
2011
2074
|
return deserialize$1(ctx, 0, node);
|
|
2012
2075
|
} catch (error) {
|
|
@@ -2808,6 +2871,7 @@ function createSyncParserContext(options) {
|
|
|
2808
2871
|
};
|
|
2809
2872
|
}
|
|
2810
2873
|
function parseItems(ctx, depth, current) {
|
|
2874
|
+
assertArrayHoles(current);
|
|
2811
2875
|
const len = current.length;
|
|
2812
2876
|
const nodes = new Array(len);
|
|
2813
2877
|
for (let i = 0; i < len; i++) if (i in current) nodes[i] = parseSOS(ctx, depth, current[i]);
|
|
@@ -2997,9 +3061,12 @@ function parseSOS(ctx, depth, current) {
|
|
|
2997
3061
|
}
|
|
2998
3062
|
}
|
|
2999
3063
|
function parseTop(ctx, current) {
|
|
3064
|
+
const refs = ctx.base.refs;
|
|
3065
|
+
const size = refs.size;
|
|
3000
3066
|
try {
|
|
3001
3067
|
return parseSOS(ctx, 0, current);
|
|
3002
3068
|
} catch (error) {
|
|
3069
|
+
rollbackRefs(refs, size);
|
|
3003
3070
|
throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
|
|
3004
3071
|
}
|
|
3005
3072
|
}
|
|
@@ -3056,7 +3123,8 @@ function createStreamParsePluginContext(ctx, depth) {
|
|
|
3056
3123
|
state.queue.push({
|
|
3057
3124
|
node,
|
|
3058
3125
|
initial: false,
|
|
3059
|
-
accept: void 0
|
|
3126
|
+
accept: void 0,
|
|
3127
|
+
refs: ctx.base.refs.size
|
|
3060
3128
|
});
|
|
3061
3129
|
drainOutput(ctx);
|
|
3062
3130
|
}
|
|
@@ -3150,7 +3218,18 @@ function handlePromise(type, id, depth, data) {
|
|
|
3150
3218
|
}
|
|
3151
3219
|
function watchPromise(ctx, resolver, depth, current) {
|
|
3152
3220
|
pushPendingState(ctx);
|
|
3153
|
-
|
|
3221
|
+
let settled = false;
|
|
3222
|
+
current.then((data) => {
|
|
3223
|
+
if (!settled) {
|
|
3224
|
+
settled = true;
|
|
3225
|
+
handlePromise.call(ctx, 23, resolver, depth, data);
|
|
3226
|
+
}
|
|
3227
|
+
}, (data) => {
|
|
3228
|
+
if (!settled) {
|
|
3229
|
+
settled = true;
|
|
3230
|
+
handlePromise.call(ctx, 24, resolver, depth, data);
|
|
3231
|
+
}
|
|
3232
|
+
});
|
|
3154
3233
|
}
|
|
3155
3234
|
function returnIterator(iterator) {
|
|
3156
3235
|
Promise.resolve().then(() => iterator.return?.()).catch(() => {});
|
|
@@ -3201,6 +3280,7 @@ function createStreamParserState(options) {
|
|
|
3201
3280
|
pending: 0,
|
|
3202
3281
|
parsing: 0,
|
|
3203
3282
|
queue: [],
|
|
3283
|
+
head: 0,
|
|
3204
3284
|
writing: false,
|
|
3205
3285
|
inFlight: void 0,
|
|
3206
3286
|
reason: void 0,
|
|
@@ -3244,7 +3324,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
|
|
|
3244
3324
|
const record = {
|
|
3245
3325
|
node: void 0,
|
|
3246
3326
|
initial: false,
|
|
3247
|
-
accept
|
|
3327
|
+
accept,
|
|
3328
|
+
refs: ctx.base.refs.size
|
|
3248
3329
|
};
|
|
3249
3330
|
let failure;
|
|
3250
3331
|
state.queue.push(record);
|
|
@@ -3261,6 +3342,18 @@ function parseEvent(ctx, depth, value, wrap, accept) {
|
|
|
3261
3342
|
else drainOutput(ctx);
|
|
3262
3343
|
return failure?.value;
|
|
3263
3344
|
}
|
|
3345
|
+
function takeRecord(state) {
|
|
3346
|
+
const record = state.queue[state.head];
|
|
3347
|
+
state.queue[state.head++] = void 0;
|
|
3348
|
+
if (state.head === state.queue.length) {
|
|
3349
|
+
state.queue.length = 0;
|
|
3350
|
+
state.head = 0;
|
|
3351
|
+
} else if (state.head > 1024 && state.head * 2 > state.queue.length) {
|
|
3352
|
+
state.queue.splice(0, state.head);
|
|
3353
|
+
state.head = 0;
|
|
3354
|
+
}
|
|
3355
|
+
return record;
|
|
3356
|
+
}
|
|
3264
3357
|
/**
|
|
3265
3358
|
* Emits queued records one at a time. A callback that returns a promise
|
|
3266
3359
|
* pauses emission until it settles; the source event behind the record is
|
|
@@ -3269,8 +3362,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
|
|
|
3269
3362
|
function drainOutput(ctx) {
|
|
3270
3363
|
const state = ctx.state;
|
|
3271
3364
|
if (state.writing || state.parsing > 0) return;
|
|
3272
|
-
while (state.alive && state.queue.length
|
|
3273
|
-
const record = state
|
|
3365
|
+
while (state.alive && state.head < state.queue.length) {
|
|
3366
|
+
const record = takeRecord(state);
|
|
3274
3367
|
if (!record.node) continue;
|
|
3275
3368
|
let pending;
|
|
3276
3369
|
state.writing = true;
|
|
@@ -3327,12 +3420,14 @@ function drainOutput(ctx) {
|
|
|
3327
3420
|
}
|
|
3328
3421
|
function checkStreamParse(ctx) {
|
|
3329
3422
|
const state = ctx.state;
|
|
3330
|
-
if (state.alive && state.parsing === 0 && state.pending <= 0 && state.queue.length
|
|
3423
|
+
if (state.alive && state.parsing === 0 && state.pending <= 0 && state.head === state.queue.length && !state.writing) stopStreamParse(ctx, 0);
|
|
3331
3424
|
}
|
|
3332
3425
|
function startStreamParse(ctx, current) {
|
|
3333
3426
|
const state = ctx.state;
|
|
3334
3427
|
let parsed;
|
|
3335
3428
|
let failure;
|
|
3429
|
+
const refs = ctx.base.refs;
|
|
3430
|
+
const size = refs.size;
|
|
3336
3431
|
state.starting = true;
|
|
3337
3432
|
try {
|
|
3338
3433
|
state.parsing++;
|
|
@@ -3345,13 +3440,15 @@ function startStreamParse(ctx, current) {
|
|
|
3345
3440
|
if (!state.alive) releaseParserValues(ctx);
|
|
3346
3441
|
}
|
|
3347
3442
|
if (failure) {
|
|
3443
|
+
rollbackRefs(refs, size);
|
|
3348
3444
|
if (state.alive) stopStreamParse(ctx, 1, failure.value);
|
|
3349
3445
|
else throw failure.value;
|
|
3350
3446
|
} else if (parsed && state.alive) {
|
|
3351
3447
|
state.queue.unshift({
|
|
3352
3448
|
node: parsed,
|
|
3353
3449
|
initial: true,
|
|
3354
|
-
accept: void 0
|
|
3450
|
+
accept: void 0,
|
|
3451
|
+
refs: size
|
|
3355
3452
|
});
|
|
3356
3453
|
drainOutput(ctx);
|
|
3357
3454
|
}
|
|
@@ -3363,8 +3460,9 @@ function runCleanups(state) {
|
|
|
3363
3460
|
const cleanups = state.cleanups;
|
|
3364
3461
|
let failure;
|
|
3365
3462
|
state.cleanups = [];
|
|
3366
|
-
for (const record of state.queue) releaseRecord(record);
|
|
3463
|
+
for (const record of state.queue) if (record) releaseRecord(record);
|
|
3367
3464
|
state.queue.length = 0;
|
|
3465
|
+
state.head = 0;
|
|
3368
3466
|
if (state.inFlight) releaseRecord(state.inFlight);
|
|
3369
3467
|
state.inFlight = void 0;
|
|
3370
3468
|
for (let i = 0, len = cleanups.length; i < len; i++) try {
|
|
@@ -3394,6 +3492,8 @@ function stopStreamParse(ctx, mode, reason) {
|
|
|
3394
3492
|
const state = ctx.state;
|
|
3395
3493
|
if (state.alive) {
|
|
3396
3494
|
state.alive = false;
|
|
3495
|
+
const unsent = state.queue[state.head];
|
|
3496
|
+
if (mode !== 0 && unsent) rollbackRefs(ctx.base.refs, unsent.refs);
|
|
3397
3497
|
const onError = state.onError;
|
|
3398
3498
|
const onDone = state.onDone;
|
|
3399
3499
|
if (mode === 1 && !onError && !(reason instanceof SerovalParserError)) reason = new SerovalParserError(reason);
|
package/dist/dev/index.js
CHANGED
|
@@ -481,6 +481,14 @@ function createBoxedNode(id, boxed) {
|
|
|
481
481
|
return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed);
|
|
482
482
|
}
|
|
483
483
|
const MAX_TYPED_ARRAY_LENGTH = 1e6;
|
|
484
|
+
/**
|
|
485
|
+
* Rejects a sparse array with more holes than the typed array limit. Each hole
|
|
486
|
+
* still costs a node and output, however few elements the array holds.
|
|
487
|
+
*/
|
|
488
|
+
function assertArrayHoles(current) {
|
|
489
|
+
const len = current.length;
|
|
490
|
+
if (len > MAX_TYPED_ARRAY_LENGTH && len - Object.keys(current).length > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
|
|
491
|
+
}
|
|
484
492
|
function createViewNode(type, id, current, view, buffer) {
|
|
485
493
|
const isDataView = type === 20;
|
|
486
494
|
const size = view.length;
|
|
@@ -1272,6 +1280,15 @@ function createIndexForValue(ctx, current) {
|
|
|
1272
1280
|
ctx.refs.set(current, id);
|
|
1273
1281
|
return id;
|
|
1274
1282
|
}
|
|
1283
|
+
/**
|
|
1284
|
+
* Removes the ids registered from `size` on. A parse that fails emits none of
|
|
1285
|
+
* them, so a caller-owned map must not keep them for later calls.
|
|
1286
|
+
*/
|
|
1287
|
+
function rollbackRefs(refs, size) {
|
|
1288
|
+
if (refs.size > size) {
|
|
1289
|
+
for (const [value, id] of refs) if (id >= size) refs.delete(value);
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1275
1292
|
function getNodeForIndexedValue(ctx, current) {
|
|
1276
1293
|
const registeredId = ctx.refs.get(current);
|
|
1277
1294
|
if (registeredId != null) {
|
|
@@ -1370,6 +1387,12 @@ function getObjectKind(current, currentClass, features) {
|
|
|
1370
1387
|
if (features & 16 && (currentClass === BigInt64Array || currentClass === BigUint64Array)) return 9;
|
|
1371
1388
|
if (features & 1 && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) return 5;
|
|
1372
1389
|
if (features & 64 && typeof Temporal !== "undefined" && getTemporalType(currentClass) != null) return 15;
|
|
1390
|
+
if (ArrayBuffer.isView(current)) {
|
|
1391
|
+
const name = getTypedArrayName(current);
|
|
1392
|
+
if (name === void 0) return 10;
|
|
1393
|
+
if (name !== "BigInt64Array" && name !== "BigUint64Array") return 8;
|
|
1394
|
+
if (features & 16) return 9;
|
|
1395
|
+
}
|
|
1373
1396
|
if (current instanceof Error) return 4;
|
|
1374
1397
|
if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) return 16;
|
|
1375
1398
|
return 0;
|
|
@@ -1410,11 +1433,29 @@ function createArrayBufferNode(ctx, id, current) {
|
|
|
1410
1433
|
function createAsyncParserContext(options) {
|
|
1411
1434
|
return { base: createBaseParserContext(options) };
|
|
1412
1435
|
}
|
|
1436
|
+
function ignoreRejection$1() {}
|
|
1437
|
+
/**
|
|
1438
|
+
* A promise gets its rejection handler only when the walk reaches it. While an
|
|
1439
|
+
* earlier sibling is still pending, a later one that rejects would be an
|
|
1440
|
+
* unhandled rejection, which ends a Node.js process by default. Mark the
|
|
1441
|
+
* promises among the children as handled before walking them.
|
|
1442
|
+
*/
|
|
1443
|
+
function handleChildPromises(values) {
|
|
1444
|
+
for (const value of values) if (value instanceof Promise) Promise.prototype.then.call(value, void 0, ignoreRejection$1);
|
|
1445
|
+
}
|
|
1413
1446
|
async function parseItems$1(ctx, depth, current) {
|
|
1447
|
+
assertArrayHoles(current);
|
|
1414
1448
|
const len = current.length;
|
|
1415
1449
|
const nodes = new Array(len);
|
|
1416
|
-
|
|
1417
|
-
|
|
1450
|
+
const values = new Array(len);
|
|
1451
|
+
const present = new Array(len);
|
|
1452
|
+
for (let i = 0; i < len; i++) {
|
|
1453
|
+
present[i] = i in current;
|
|
1454
|
+
if (present[i]) values[i] = current[i];
|
|
1455
|
+
}
|
|
1456
|
+
handleChildPromises(values);
|
|
1457
|
+
for (let i = 0, node; i < len; i++) if (present[i]) {
|
|
1458
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1418
1459
|
nodes[i] = node instanceof Promise ? await node : node;
|
|
1419
1460
|
} else nodes[i] = 0;
|
|
1420
1461
|
return nodes;
|
|
@@ -1427,10 +1468,12 @@ async function parseProperties$1(ctx, depth, properties) {
|
|
|
1427
1468
|
const len = keys.length;
|
|
1428
1469
|
const keyNodes = new Array(len);
|
|
1429
1470
|
const valueNodes = new Array(len);
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1471
|
+
const values = new Array(len);
|
|
1472
|
+
for (let i = 0; i < len; i++) values[i] = properties[keys[i]];
|
|
1473
|
+
handleChildPromises(values);
|
|
1474
|
+
for (let i = 0, node; i < len; i++) {
|
|
1475
|
+
keyNodes[i] = serializeString(keys[i]);
|
|
1476
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1434
1477
|
valueNodes[i] = node instanceof Promise ? await node : node;
|
|
1435
1478
|
}
|
|
1436
1479
|
if (SYM_ITERATOR in properties) {
|
|
@@ -1471,26 +1514,32 @@ async function parseError$1(ctx, depth, type, id, current) {
|
|
|
1471
1514
|
}
|
|
1472
1515
|
async function parseMap$1(ctx, depth, id, current) {
|
|
1473
1516
|
const sentinel = parseMapSentinel(ctx.base);
|
|
1474
|
-
const
|
|
1475
|
-
const
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1517
|
+
const entries = [...current.entries()];
|
|
1518
|
+
const keyNodes = new Array(entries.length);
|
|
1519
|
+
const valueNodes = new Array(entries.length);
|
|
1520
|
+
const keys = new Array(entries.length);
|
|
1521
|
+
const values = new Array(entries.length);
|
|
1522
|
+
for (let i = 0; i < entries.length; i++) {
|
|
1523
|
+
keys[i] = entries[i][0];
|
|
1524
|
+
values[i] = entries[i][1];
|
|
1525
|
+
}
|
|
1526
|
+
handleChildPromises(keys);
|
|
1527
|
+
handleChildPromises(values);
|
|
1528
|
+
for (let i = 0, node; i < entries.length; i++) {
|
|
1529
|
+
node = parseValueAsync(ctx, depth, keys[i]);
|
|
1480
1530
|
keyNodes[i] = node instanceof Promise ? await node : node;
|
|
1481
|
-
node = parseValueAsync(ctx, depth,
|
|
1531
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1482
1532
|
valueNodes[i] = node instanceof Promise ? await node : node;
|
|
1483
|
-
i++;
|
|
1484
1533
|
}
|
|
1485
1534
|
return createMapNode(id, sentinel, keyNodes, valueNodes);
|
|
1486
1535
|
}
|
|
1487
1536
|
async function parseSet$1(ctx, depth, id, current) {
|
|
1488
|
-
const
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
for (
|
|
1492
|
-
node = parseValueAsync(ctx, depth,
|
|
1493
|
-
items[i
|
|
1537
|
+
const values = [...current.keys()];
|
|
1538
|
+
const items = new Array(values.length);
|
|
1539
|
+
handleChildPromises(values);
|
|
1540
|
+
for (let i = 0, node; i < values.length; i++) {
|
|
1541
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1542
|
+
items[i] = node instanceof Promise ? await node : node;
|
|
1494
1543
|
}
|
|
1495
1544
|
return createSetNode(id, items);
|
|
1496
1545
|
}
|
|
@@ -1659,9 +1708,12 @@ async function parseAsync(ctx, depth, current) {
|
|
|
1659
1708
|
return await parseValueAsync(ctx, depth, current);
|
|
1660
1709
|
}
|
|
1661
1710
|
async function parseTopAsync(ctx, current) {
|
|
1711
|
+
const refs = ctx.base.refs;
|
|
1712
|
+
const size = refs.size;
|
|
1662
1713
|
try {
|
|
1663
1714
|
return await parseAsync(ctx, 0, current);
|
|
1664
1715
|
} catch (error) {
|
|
1716
|
+
rollbackRefs(refs, size);
|
|
1665
1717
|
throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
|
|
1666
1718
|
}
|
|
1667
1719
|
}
|
|
@@ -1702,6 +1754,8 @@ function applyObjectFlag(obj, flag) {
|
|
|
1702
1754
|
}
|
|
1703
1755
|
}
|
|
1704
1756
|
const DEFAULT_DEPTH_LIMIT = 1e3;
|
|
1757
|
+
const MAX_REGEXP_PROPERTY_ESCAPES = 32;
|
|
1758
|
+
const REGEXP_PROPERTY_ESCAPE = /\\[pP]\{/g;
|
|
1705
1759
|
function createBaseDeserializerContext(options) {
|
|
1706
1760
|
const maxBase64Length = options.maxBase64Length ?? DEFAULT_MAX_BASE64_LENGTH;
|
|
1707
1761
|
if (!Number.isSafeInteger(maxBase64Length) || maxBase64Length < 0) throw new RangeError("maxBase64Length must be a non-negative safe integer");
|
|
@@ -1712,7 +1766,8 @@ function createBaseDeserializerContext(options) {
|
|
|
1712
1766
|
refs,
|
|
1713
1767
|
features: options.features ?? 127 ^ (options.disabledFeatures ?? 0),
|
|
1714
1768
|
depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT,
|
|
1715
|
-
maxBase64Length
|
|
1769
|
+
maxBase64Length,
|
|
1770
|
+
regexpPropertyEscapes: MAX_REGEXP_PROPERTY_ESCAPES
|
|
1716
1771
|
};
|
|
1717
1772
|
}
|
|
1718
1773
|
function createVanillaDeserializerContext(options) {
|
|
@@ -1824,6 +1879,13 @@ function deserializeRegExp(ctx, node) {
|
|
|
1824
1879
|
if (ctx.base.features & 32) {
|
|
1825
1880
|
const source = deserializeString(node.c);
|
|
1826
1881
|
if (source.length > MAX_REGEXP_SOURCE_LENGTH) throw new SerovalMalformedNodeError(node);
|
|
1882
|
+
if (/[uv]/.test(String(node.m))) {
|
|
1883
|
+
const escapes = source.match(REGEXP_PROPERTY_ESCAPE);
|
|
1884
|
+
if (escapes) {
|
|
1885
|
+
ctx.base.regexpPropertyEscapes -= escapes.length;
|
|
1886
|
+
if (ctx.base.regexpPropertyEscapes < 0) throw new SerovalMalformedNodeError(node);
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1827
1889
|
return assignIndexedValue$1(ctx, node.i, new RegExp(source, node.m));
|
|
1828
1890
|
}
|
|
1829
1891
|
throw new SerovalUnsupportedNodeError(node);
|
|
@@ -2006,6 +2068,7 @@ function deserialize$1(ctx, depth, node) {
|
|
|
2006
2068
|
}
|
|
2007
2069
|
}
|
|
2008
2070
|
function deserializeTop(ctx, node) {
|
|
2071
|
+
ctx.base.regexpPropertyEscapes = MAX_REGEXP_PROPERTY_ESCAPES;
|
|
2009
2072
|
try {
|
|
2010
2073
|
return deserialize$1(ctx, 0, node);
|
|
2011
2074
|
} catch (error) {
|
|
@@ -2807,6 +2870,7 @@ function createSyncParserContext(options) {
|
|
|
2807
2870
|
};
|
|
2808
2871
|
}
|
|
2809
2872
|
function parseItems(ctx, depth, current) {
|
|
2873
|
+
assertArrayHoles(current);
|
|
2810
2874
|
const len = current.length;
|
|
2811
2875
|
const nodes = new Array(len);
|
|
2812
2876
|
for (let i = 0; i < len; i++) if (i in current) nodes[i] = parseSOS(ctx, depth, current[i]);
|
|
@@ -2996,9 +3060,12 @@ function parseSOS(ctx, depth, current) {
|
|
|
2996
3060
|
}
|
|
2997
3061
|
}
|
|
2998
3062
|
function parseTop(ctx, current) {
|
|
3063
|
+
const refs = ctx.base.refs;
|
|
3064
|
+
const size = refs.size;
|
|
2999
3065
|
try {
|
|
3000
3066
|
return parseSOS(ctx, 0, current);
|
|
3001
3067
|
} catch (error) {
|
|
3068
|
+
rollbackRefs(refs, size);
|
|
3002
3069
|
throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
|
|
3003
3070
|
}
|
|
3004
3071
|
}
|
|
@@ -3055,7 +3122,8 @@ function createStreamParsePluginContext(ctx, depth) {
|
|
|
3055
3122
|
state.queue.push({
|
|
3056
3123
|
node,
|
|
3057
3124
|
initial: false,
|
|
3058
|
-
accept: void 0
|
|
3125
|
+
accept: void 0,
|
|
3126
|
+
refs: ctx.base.refs.size
|
|
3059
3127
|
});
|
|
3060
3128
|
drainOutput(ctx);
|
|
3061
3129
|
}
|
|
@@ -3149,7 +3217,18 @@ function handlePromise(type, id, depth, data) {
|
|
|
3149
3217
|
}
|
|
3150
3218
|
function watchPromise(ctx, resolver, depth, current) {
|
|
3151
3219
|
pushPendingState(ctx);
|
|
3152
|
-
|
|
3220
|
+
let settled = false;
|
|
3221
|
+
current.then((data) => {
|
|
3222
|
+
if (!settled) {
|
|
3223
|
+
settled = true;
|
|
3224
|
+
handlePromise.call(ctx, 23, resolver, depth, data);
|
|
3225
|
+
}
|
|
3226
|
+
}, (data) => {
|
|
3227
|
+
if (!settled) {
|
|
3228
|
+
settled = true;
|
|
3229
|
+
handlePromise.call(ctx, 24, resolver, depth, data);
|
|
3230
|
+
}
|
|
3231
|
+
});
|
|
3153
3232
|
}
|
|
3154
3233
|
function returnIterator(iterator) {
|
|
3155
3234
|
Promise.resolve().then(() => iterator.return?.()).catch(() => {});
|
|
@@ -3200,6 +3279,7 @@ function createStreamParserState(options) {
|
|
|
3200
3279
|
pending: 0,
|
|
3201
3280
|
parsing: 0,
|
|
3202
3281
|
queue: [],
|
|
3282
|
+
head: 0,
|
|
3203
3283
|
writing: false,
|
|
3204
3284
|
inFlight: void 0,
|
|
3205
3285
|
reason: void 0,
|
|
@@ -3243,7 +3323,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
|
|
|
3243
3323
|
const record = {
|
|
3244
3324
|
node: void 0,
|
|
3245
3325
|
initial: false,
|
|
3246
|
-
accept
|
|
3326
|
+
accept,
|
|
3327
|
+
refs: ctx.base.refs.size
|
|
3247
3328
|
};
|
|
3248
3329
|
let failure;
|
|
3249
3330
|
state.queue.push(record);
|
|
@@ -3260,6 +3341,18 @@ function parseEvent(ctx, depth, value, wrap, accept) {
|
|
|
3260
3341
|
else drainOutput(ctx);
|
|
3261
3342
|
return failure?.value;
|
|
3262
3343
|
}
|
|
3344
|
+
function takeRecord(state) {
|
|
3345
|
+
const record = state.queue[state.head];
|
|
3346
|
+
state.queue[state.head++] = void 0;
|
|
3347
|
+
if (state.head === state.queue.length) {
|
|
3348
|
+
state.queue.length = 0;
|
|
3349
|
+
state.head = 0;
|
|
3350
|
+
} else if (state.head > 1024 && state.head * 2 > state.queue.length) {
|
|
3351
|
+
state.queue.splice(0, state.head);
|
|
3352
|
+
state.head = 0;
|
|
3353
|
+
}
|
|
3354
|
+
return record;
|
|
3355
|
+
}
|
|
3263
3356
|
/**
|
|
3264
3357
|
* Emits queued records one at a time. A callback that returns a promise
|
|
3265
3358
|
* pauses emission until it settles; the source event behind the record is
|
|
@@ -3268,8 +3361,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
|
|
|
3268
3361
|
function drainOutput(ctx) {
|
|
3269
3362
|
const state = ctx.state;
|
|
3270
3363
|
if (state.writing || state.parsing > 0) return;
|
|
3271
|
-
while (state.alive && state.queue.length
|
|
3272
|
-
const record = state
|
|
3364
|
+
while (state.alive && state.head < state.queue.length) {
|
|
3365
|
+
const record = takeRecord(state);
|
|
3273
3366
|
if (!record.node) continue;
|
|
3274
3367
|
let pending;
|
|
3275
3368
|
state.writing = true;
|
|
@@ -3326,12 +3419,14 @@ function drainOutput(ctx) {
|
|
|
3326
3419
|
}
|
|
3327
3420
|
function checkStreamParse(ctx) {
|
|
3328
3421
|
const state = ctx.state;
|
|
3329
|
-
if (state.alive && state.parsing === 0 && state.pending <= 0 && state.queue.length
|
|
3422
|
+
if (state.alive && state.parsing === 0 && state.pending <= 0 && state.head === state.queue.length && !state.writing) stopStreamParse(ctx, 0);
|
|
3330
3423
|
}
|
|
3331
3424
|
function startStreamParse(ctx, current) {
|
|
3332
3425
|
const state = ctx.state;
|
|
3333
3426
|
let parsed;
|
|
3334
3427
|
let failure;
|
|
3428
|
+
const refs = ctx.base.refs;
|
|
3429
|
+
const size = refs.size;
|
|
3335
3430
|
state.starting = true;
|
|
3336
3431
|
try {
|
|
3337
3432
|
state.parsing++;
|
|
@@ -3344,13 +3439,15 @@ function startStreamParse(ctx, current) {
|
|
|
3344
3439
|
if (!state.alive) releaseParserValues(ctx);
|
|
3345
3440
|
}
|
|
3346
3441
|
if (failure) {
|
|
3442
|
+
rollbackRefs(refs, size);
|
|
3347
3443
|
if (state.alive) stopStreamParse(ctx, 1, failure.value);
|
|
3348
3444
|
else throw failure.value;
|
|
3349
3445
|
} else if (parsed && state.alive) {
|
|
3350
3446
|
state.queue.unshift({
|
|
3351
3447
|
node: parsed,
|
|
3352
3448
|
initial: true,
|
|
3353
|
-
accept: void 0
|
|
3449
|
+
accept: void 0,
|
|
3450
|
+
refs: size
|
|
3354
3451
|
});
|
|
3355
3452
|
drainOutput(ctx);
|
|
3356
3453
|
}
|
|
@@ -3362,8 +3459,9 @@ function runCleanups(state) {
|
|
|
3362
3459
|
const cleanups = state.cleanups;
|
|
3363
3460
|
let failure;
|
|
3364
3461
|
state.cleanups = [];
|
|
3365
|
-
for (const record of state.queue) releaseRecord(record);
|
|
3462
|
+
for (const record of state.queue) if (record) releaseRecord(record);
|
|
3366
3463
|
state.queue.length = 0;
|
|
3464
|
+
state.head = 0;
|
|
3367
3465
|
if (state.inFlight) releaseRecord(state.inFlight);
|
|
3368
3466
|
state.inFlight = void 0;
|
|
3369
3467
|
for (let i = 0, len = cleanups.length; i < len; i++) try {
|
|
@@ -3393,6 +3491,8 @@ function stopStreamParse(ctx, mode, reason) {
|
|
|
3393
3491
|
const state = ctx.state;
|
|
3394
3492
|
if (state.alive) {
|
|
3395
3493
|
state.alive = false;
|
|
3494
|
+
const unsent = state.queue[state.head];
|
|
3495
|
+
if (mode !== 0 && unsent) rollbackRefs(ctx.base.refs, unsent.refs);
|
|
3396
3496
|
const onError = state.onError;
|
|
3397
3497
|
const onDone = state.onDone;
|
|
3398
3498
|
if (mode === 1 && !onError && !(reason instanceof SerovalParserError)) reason = new SerovalParserError(reason);
|
package/dist/index.cjs
CHANGED
|
@@ -466,6 +466,14 @@ function createBoxedNode(id, boxed) {
|
|
|
466
466
|
return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed);
|
|
467
467
|
}
|
|
468
468
|
const MAX_TYPED_ARRAY_LENGTH = 1e6;
|
|
469
|
+
/**
|
|
470
|
+
* Rejects a sparse array with more holes than the typed array limit. Each hole
|
|
471
|
+
* still costs a node and output, however few elements the array holds.
|
|
472
|
+
*/
|
|
473
|
+
function assertArrayHoles(current) {
|
|
474
|
+
const len = current.length;
|
|
475
|
+
if (len > MAX_TYPED_ARRAY_LENGTH && len - Object.keys(current).length > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
|
|
476
|
+
}
|
|
469
477
|
function createViewNode(type, id, current, view, buffer) {
|
|
470
478
|
const isDataView = type === 20;
|
|
471
479
|
const size = view.length;
|
|
@@ -1257,6 +1265,15 @@ function createIndexForValue(ctx, current) {
|
|
|
1257
1265
|
ctx.refs.set(current, id);
|
|
1258
1266
|
return id;
|
|
1259
1267
|
}
|
|
1268
|
+
/**
|
|
1269
|
+
* Removes the ids registered from `size` on. A parse that fails emits none of
|
|
1270
|
+
* them, so a caller-owned map must not keep them for later calls.
|
|
1271
|
+
*/
|
|
1272
|
+
function rollbackRefs(refs, size) {
|
|
1273
|
+
if (refs.size > size) {
|
|
1274
|
+
for (const [value, id] of refs) if (id >= size) refs.delete(value);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1260
1277
|
function getNodeForIndexedValue(ctx, current) {
|
|
1261
1278
|
const registeredId = ctx.refs.get(current);
|
|
1262
1279
|
if (registeredId != null) {
|
|
@@ -1355,6 +1372,12 @@ function getObjectKind(current, currentClass, features) {
|
|
|
1355
1372
|
if (features & 16 && (currentClass === BigInt64Array || currentClass === BigUint64Array)) return 9;
|
|
1356
1373
|
if (features & 1 && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) return 5;
|
|
1357
1374
|
if (features & 64 && typeof Temporal !== "undefined" && getTemporalType(currentClass) != null) return 15;
|
|
1375
|
+
if (ArrayBuffer.isView(current)) {
|
|
1376
|
+
const name = getTypedArrayName(current);
|
|
1377
|
+
if (name === void 0) return 10;
|
|
1378
|
+
if (name !== "BigInt64Array" && name !== "BigUint64Array") return 8;
|
|
1379
|
+
if (features & 16) return 9;
|
|
1380
|
+
}
|
|
1358
1381
|
if (current instanceof Error) return 4;
|
|
1359
1382
|
if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) return 16;
|
|
1360
1383
|
return 0;
|
|
@@ -1395,11 +1418,29 @@ function createArrayBufferNode(ctx, id, current) {
|
|
|
1395
1418
|
function createAsyncParserContext(options) {
|
|
1396
1419
|
return { base: createBaseParserContext(options) };
|
|
1397
1420
|
}
|
|
1421
|
+
function ignoreRejection$1() {}
|
|
1422
|
+
/**
|
|
1423
|
+
* A promise gets its rejection handler only when the walk reaches it. While an
|
|
1424
|
+
* earlier sibling is still pending, a later one that rejects would be an
|
|
1425
|
+
* unhandled rejection, which ends a Node.js process by default. Mark the
|
|
1426
|
+
* promises among the children as handled before walking them.
|
|
1427
|
+
*/
|
|
1428
|
+
function handleChildPromises(values) {
|
|
1429
|
+
for (const value of values) if (value instanceof Promise) Promise.prototype.then.call(value, void 0, ignoreRejection$1);
|
|
1430
|
+
}
|
|
1398
1431
|
async function parseItems$1(ctx, depth, current) {
|
|
1432
|
+
assertArrayHoles(current);
|
|
1399
1433
|
const len = current.length;
|
|
1400
1434
|
const nodes = new Array(len);
|
|
1401
|
-
|
|
1402
|
-
|
|
1435
|
+
const values = new Array(len);
|
|
1436
|
+
const present = new Array(len);
|
|
1437
|
+
for (let i = 0; i < len; i++) {
|
|
1438
|
+
present[i] = i in current;
|
|
1439
|
+
if (present[i]) values[i] = current[i];
|
|
1440
|
+
}
|
|
1441
|
+
handleChildPromises(values);
|
|
1442
|
+
for (let i = 0, node; i < len; i++) if (present[i]) {
|
|
1443
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1403
1444
|
nodes[i] = node instanceof Promise ? await node : node;
|
|
1404
1445
|
} else nodes[i] = 0;
|
|
1405
1446
|
return nodes;
|
|
@@ -1412,10 +1453,12 @@ async function parseProperties$1(ctx, depth, properties) {
|
|
|
1412
1453
|
const len = keys.length;
|
|
1413
1454
|
const keyNodes = new Array(len);
|
|
1414
1455
|
const valueNodes = new Array(len);
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1456
|
+
const values = new Array(len);
|
|
1457
|
+
for (let i = 0; i < len; i++) values[i] = properties[keys[i]];
|
|
1458
|
+
handleChildPromises(values);
|
|
1459
|
+
for (let i = 0, node; i < len; i++) {
|
|
1460
|
+
keyNodes[i] = serializeString(keys[i]);
|
|
1461
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1419
1462
|
valueNodes[i] = node instanceof Promise ? await node : node;
|
|
1420
1463
|
}
|
|
1421
1464
|
if (SYM_ITERATOR in properties) {
|
|
@@ -1456,26 +1499,32 @@ async function parseError$1(ctx, depth, type, id, current) {
|
|
|
1456
1499
|
}
|
|
1457
1500
|
async function parseMap$1(ctx, depth, id, current) {
|
|
1458
1501
|
const sentinel = parseMapSentinel(ctx.base);
|
|
1459
|
-
const
|
|
1460
|
-
const
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1502
|
+
const entries = [...current.entries()];
|
|
1503
|
+
const keyNodes = new Array(entries.length);
|
|
1504
|
+
const valueNodes = new Array(entries.length);
|
|
1505
|
+
const keys = new Array(entries.length);
|
|
1506
|
+
const values = new Array(entries.length);
|
|
1507
|
+
for (let i = 0; i < entries.length; i++) {
|
|
1508
|
+
keys[i] = entries[i][0];
|
|
1509
|
+
values[i] = entries[i][1];
|
|
1510
|
+
}
|
|
1511
|
+
handleChildPromises(keys);
|
|
1512
|
+
handleChildPromises(values);
|
|
1513
|
+
for (let i = 0, node; i < entries.length; i++) {
|
|
1514
|
+
node = parseValueAsync(ctx, depth, keys[i]);
|
|
1465
1515
|
keyNodes[i] = node instanceof Promise ? await node : node;
|
|
1466
|
-
node = parseValueAsync(ctx, depth,
|
|
1516
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1467
1517
|
valueNodes[i] = node instanceof Promise ? await node : node;
|
|
1468
|
-
i++;
|
|
1469
1518
|
}
|
|
1470
1519
|
return createMapNode(id, sentinel, keyNodes, valueNodes);
|
|
1471
1520
|
}
|
|
1472
1521
|
async function parseSet$1(ctx, depth, id, current) {
|
|
1473
|
-
const
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
for (
|
|
1477
|
-
node = parseValueAsync(ctx, depth,
|
|
1478
|
-
items[i
|
|
1522
|
+
const values = [...current.keys()];
|
|
1523
|
+
const items = new Array(values.length);
|
|
1524
|
+
handleChildPromises(values);
|
|
1525
|
+
for (let i = 0, node; i < values.length; i++) {
|
|
1526
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1527
|
+
items[i] = node instanceof Promise ? await node : node;
|
|
1479
1528
|
}
|
|
1480
1529
|
return createSetNode(id, items);
|
|
1481
1530
|
}
|
|
@@ -1644,9 +1693,12 @@ async function parseAsync(ctx, depth, current) {
|
|
|
1644
1693
|
return await parseValueAsync(ctx, depth, current);
|
|
1645
1694
|
}
|
|
1646
1695
|
async function parseTopAsync(ctx, current) {
|
|
1696
|
+
const refs = ctx.base.refs;
|
|
1697
|
+
const size = refs.size;
|
|
1647
1698
|
try {
|
|
1648
1699
|
return await parseAsync(ctx, 0, current);
|
|
1649
1700
|
} catch (error) {
|
|
1701
|
+
rollbackRefs(refs, size);
|
|
1650
1702
|
throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
|
|
1651
1703
|
}
|
|
1652
1704
|
}
|
|
@@ -1687,6 +1739,8 @@ function applyObjectFlag(obj, flag) {
|
|
|
1687
1739
|
}
|
|
1688
1740
|
}
|
|
1689
1741
|
const DEFAULT_DEPTH_LIMIT = 1e3;
|
|
1742
|
+
const MAX_REGEXP_PROPERTY_ESCAPES = 32;
|
|
1743
|
+
const REGEXP_PROPERTY_ESCAPE = /\\[pP]\{/g;
|
|
1690
1744
|
function createBaseDeserializerContext(options) {
|
|
1691
1745
|
const maxBase64Length = options.maxBase64Length ?? DEFAULT_MAX_BASE64_LENGTH;
|
|
1692
1746
|
if (!Number.isSafeInteger(maxBase64Length) || maxBase64Length < 0) throw new RangeError("maxBase64Length must be a non-negative safe integer");
|
|
@@ -1697,7 +1751,8 @@ function createBaseDeserializerContext(options) {
|
|
|
1697
1751
|
refs,
|
|
1698
1752
|
features: options.features ?? 127 ^ (options.disabledFeatures ?? 0),
|
|
1699
1753
|
depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT,
|
|
1700
|
-
maxBase64Length
|
|
1754
|
+
maxBase64Length,
|
|
1755
|
+
regexpPropertyEscapes: MAX_REGEXP_PROPERTY_ESCAPES
|
|
1701
1756
|
};
|
|
1702
1757
|
}
|
|
1703
1758
|
function createVanillaDeserializerContext(options) {
|
|
@@ -1809,6 +1864,13 @@ function deserializeRegExp(ctx, node) {
|
|
|
1809
1864
|
if (ctx.base.features & 32) {
|
|
1810
1865
|
const source = deserializeString(node.c);
|
|
1811
1866
|
if (source.length > MAX_REGEXP_SOURCE_LENGTH) throw new SerovalMalformedNodeError(node);
|
|
1867
|
+
if (/[uv]/.test(String(node.m))) {
|
|
1868
|
+
const escapes = source.match(REGEXP_PROPERTY_ESCAPE);
|
|
1869
|
+
if (escapes) {
|
|
1870
|
+
ctx.base.regexpPropertyEscapes -= escapes.length;
|
|
1871
|
+
if (ctx.base.regexpPropertyEscapes < 0) throw new SerovalMalformedNodeError(node);
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1812
1874
|
return assignIndexedValue$1(ctx, node.i, new RegExp(source, node.m));
|
|
1813
1875
|
}
|
|
1814
1876
|
throw new SerovalUnsupportedNodeError(node);
|
|
@@ -1991,6 +2053,7 @@ function deserialize$1(ctx, depth, node) {
|
|
|
1991
2053
|
}
|
|
1992
2054
|
}
|
|
1993
2055
|
function deserializeTop(ctx, node) {
|
|
2056
|
+
ctx.base.regexpPropertyEscapes = MAX_REGEXP_PROPERTY_ESCAPES;
|
|
1994
2057
|
try {
|
|
1995
2058
|
return deserialize$1(ctx, 0, node);
|
|
1996
2059
|
} catch (error) {
|
|
@@ -2792,6 +2855,7 @@ function createSyncParserContext(options) {
|
|
|
2792
2855
|
};
|
|
2793
2856
|
}
|
|
2794
2857
|
function parseItems(ctx, depth, current) {
|
|
2858
|
+
assertArrayHoles(current);
|
|
2795
2859
|
const len = current.length;
|
|
2796
2860
|
const nodes = new Array(len);
|
|
2797
2861
|
for (let i = 0; i < len; i++) if (i in current) nodes[i] = parseSOS(ctx, depth, current[i]);
|
|
@@ -2981,9 +3045,12 @@ function parseSOS(ctx, depth, current) {
|
|
|
2981
3045
|
}
|
|
2982
3046
|
}
|
|
2983
3047
|
function parseTop(ctx, current) {
|
|
3048
|
+
const refs = ctx.base.refs;
|
|
3049
|
+
const size = refs.size;
|
|
2984
3050
|
try {
|
|
2985
3051
|
return parseSOS(ctx, 0, current);
|
|
2986
3052
|
} catch (error) {
|
|
3053
|
+
rollbackRefs(refs, size);
|
|
2987
3054
|
throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
|
|
2988
3055
|
}
|
|
2989
3056
|
}
|
|
@@ -3040,7 +3107,8 @@ function createStreamParsePluginContext(ctx, depth) {
|
|
|
3040
3107
|
state.queue.push({
|
|
3041
3108
|
node,
|
|
3042
3109
|
initial: false,
|
|
3043
|
-
accept: void 0
|
|
3110
|
+
accept: void 0,
|
|
3111
|
+
refs: ctx.base.refs.size
|
|
3044
3112
|
});
|
|
3045
3113
|
drainOutput(ctx);
|
|
3046
3114
|
}
|
|
@@ -3134,7 +3202,18 @@ function handlePromise(type, id, depth, data) {
|
|
|
3134
3202
|
}
|
|
3135
3203
|
function watchPromise(ctx, resolver, depth, current) {
|
|
3136
3204
|
pushPendingState(ctx);
|
|
3137
|
-
|
|
3205
|
+
let settled = false;
|
|
3206
|
+
current.then((data) => {
|
|
3207
|
+
if (!settled) {
|
|
3208
|
+
settled = true;
|
|
3209
|
+
handlePromise.call(ctx, 23, resolver, depth, data);
|
|
3210
|
+
}
|
|
3211
|
+
}, (data) => {
|
|
3212
|
+
if (!settled) {
|
|
3213
|
+
settled = true;
|
|
3214
|
+
handlePromise.call(ctx, 24, resolver, depth, data);
|
|
3215
|
+
}
|
|
3216
|
+
});
|
|
3138
3217
|
}
|
|
3139
3218
|
function returnIterator(iterator) {
|
|
3140
3219
|
Promise.resolve().then(() => iterator.return?.()).catch(() => {});
|
|
@@ -3185,6 +3264,7 @@ function createStreamParserState(options) {
|
|
|
3185
3264
|
pending: 0,
|
|
3186
3265
|
parsing: 0,
|
|
3187
3266
|
queue: [],
|
|
3267
|
+
head: 0,
|
|
3188
3268
|
writing: false,
|
|
3189
3269
|
inFlight: void 0,
|
|
3190
3270
|
reason: void 0,
|
|
@@ -3228,7 +3308,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
|
|
|
3228
3308
|
const record = {
|
|
3229
3309
|
node: void 0,
|
|
3230
3310
|
initial: false,
|
|
3231
|
-
accept
|
|
3311
|
+
accept,
|
|
3312
|
+
refs: ctx.base.refs.size
|
|
3232
3313
|
};
|
|
3233
3314
|
let failure;
|
|
3234
3315
|
state.queue.push(record);
|
|
@@ -3245,6 +3326,18 @@ function parseEvent(ctx, depth, value, wrap, accept) {
|
|
|
3245
3326
|
else drainOutput(ctx);
|
|
3246
3327
|
return failure?.value;
|
|
3247
3328
|
}
|
|
3329
|
+
function takeRecord(state) {
|
|
3330
|
+
const record = state.queue[state.head];
|
|
3331
|
+
state.queue[state.head++] = void 0;
|
|
3332
|
+
if (state.head === state.queue.length) {
|
|
3333
|
+
state.queue.length = 0;
|
|
3334
|
+
state.head = 0;
|
|
3335
|
+
} else if (state.head > 1024 && state.head * 2 > state.queue.length) {
|
|
3336
|
+
state.queue.splice(0, state.head);
|
|
3337
|
+
state.head = 0;
|
|
3338
|
+
}
|
|
3339
|
+
return record;
|
|
3340
|
+
}
|
|
3248
3341
|
/**
|
|
3249
3342
|
* Emits queued records one at a time. A callback that returns a promise
|
|
3250
3343
|
* pauses emission until it settles; the source event behind the record is
|
|
@@ -3253,8 +3346,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
|
|
|
3253
3346
|
function drainOutput(ctx) {
|
|
3254
3347
|
const state = ctx.state;
|
|
3255
3348
|
if (state.writing || state.parsing > 0) return;
|
|
3256
|
-
while (state.alive && state.queue.length
|
|
3257
|
-
const record = state
|
|
3349
|
+
while (state.alive && state.head < state.queue.length) {
|
|
3350
|
+
const record = takeRecord(state);
|
|
3258
3351
|
if (!record.node) continue;
|
|
3259
3352
|
let pending;
|
|
3260
3353
|
state.writing = true;
|
|
@@ -3311,12 +3404,14 @@ function drainOutput(ctx) {
|
|
|
3311
3404
|
}
|
|
3312
3405
|
function checkStreamParse(ctx) {
|
|
3313
3406
|
const state = ctx.state;
|
|
3314
|
-
if (state.alive && state.parsing === 0 && state.pending <= 0 && state.queue.length
|
|
3407
|
+
if (state.alive && state.parsing === 0 && state.pending <= 0 && state.head === state.queue.length && !state.writing) stopStreamParse(ctx, 0);
|
|
3315
3408
|
}
|
|
3316
3409
|
function startStreamParse(ctx, current) {
|
|
3317
3410
|
const state = ctx.state;
|
|
3318
3411
|
let parsed;
|
|
3319
3412
|
let failure;
|
|
3413
|
+
const refs = ctx.base.refs;
|
|
3414
|
+
const size = refs.size;
|
|
3320
3415
|
state.starting = true;
|
|
3321
3416
|
try {
|
|
3322
3417
|
state.parsing++;
|
|
@@ -3329,13 +3424,15 @@ function startStreamParse(ctx, current) {
|
|
|
3329
3424
|
if (!state.alive) releaseParserValues(ctx);
|
|
3330
3425
|
}
|
|
3331
3426
|
if (failure) {
|
|
3427
|
+
rollbackRefs(refs, size);
|
|
3332
3428
|
if (state.alive) stopStreamParse(ctx, 1, failure.value);
|
|
3333
3429
|
else throw failure.value;
|
|
3334
3430
|
} else if (parsed && state.alive) {
|
|
3335
3431
|
state.queue.unshift({
|
|
3336
3432
|
node: parsed,
|
|
3337
3433
|
initial: true,
|
|
3338
|
-
accept: void 0
|
|
3434
|
+
accept: void 0,
|
|
3435
|
+
refs: size
|
|
3339
3436
|
});
|
|
3340
3437
|
drainOutput(ctx);
|
|
3341
3438
|
}
|
|
@@ -3347,8 +3444,9 @@ function runCleanups(state) {
|
|
|
3347
3444
|
const cleanups = state.cleanups;
|
|
3348
3445
|
let failure;
|
|
3349
3446
|
state.cleanups = [];
|
|
3350
|
-
for (const record of state.queue) releaseRecord(record);
|
|
3447
|
+
for (const record of state.queue) if (record) releaseRecord(record);
|
|
3351
3448
|
state.queue.length = 0;
|
|
3449
|
+
state.head = 0;
|
|
3352
3450
|
if (state.inFlight) releaseRecord(state.inFlight);
|
|
3353
3451
|
state.inFlight = void 0;
|
|
3354
3452
|
for (let i = 0, len = cleanups.length; i < len; i++) try {
|
|
@@ -3378,6 +3476,8 @@ function stopStreamParse(ctx, mode, reason) {
|
|
|
3378
3476
|
const state = ctx.state;
|
|
3379
3477
|
if (state.alive) {
|
|
3380
3478
|
state.alive = false;
|
|
3479
|
+
const unsent = state.queue[state.head];
|
|
3480
|
+
if (mode !== 0 && unsent) rollbackRefs(ctx.base.refs, unsent.refs);
|
|
3381
3481
|
const onError = state.onError;
|
|
3382
3482
|
const onDone = state.onDone;
|
|
3383
3483
|
if (mode === 1 && !onError && !(reason instanceof SerovalParserError)) reason = new SerovalParserError(reason);
|
package/dist/index.js
CHANGED
|
@@ -465,6 +465,14 @@ function createBoxedNode(id, boxed) {
|
|
|
465
465
|
return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed);
|
|
466
466
|
}
|
|
467
467
|
const MAX_TYPED_ARRAY_LENGTH = 1e6;
|
|
468
|
+
/**
|
|
469
|
+
* Rejects a sparse array with more holes than the typed array limit. Each hole
|
|
470
|
+
* still costs a node and output, however few elements the array holds.
|
|
471
|
+
*/
|
|
472
|
+
function assertArrayHoles(current) {
|
|
473
|
+
const len = current.length;
|
|
474
|
+
if (len > MAX_TYPED_ARRAY_LENGTH && len - Object.keys(current).length > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
|
|
475
|
+
}
|
|
468
476
|
function createViewNode(type, id, current, view, buffer) {
|
|
469
477
|
const isDataView = type === 20;
|
|
470
478
|
const size = view.length;
|
|
@@ -1256,6 +1264,15 @@ function createIndexForValue(ctx, current) {
|
|
|
1256
1264
|
ctx.refs.set(current, id);
|
|
1257
1265
|
return id;
|
|
1258
1266
|
}
|
|
1267
|
+
/**
|
|
1268
|
+
* Removes the ids registered from `size` on. A parse that fails emits none of
|
|
1269
|
+
* them, so a caller-owned map must not keep them for later calls.
|
|
1270
|
+
*/
|
|
1271
|
+
function rollbackRefs(refs, size) {
|
|
1272
|
+
if (refs.size > size) {
|
|
1273
|
+
for (const [value, id] of refs) if (id >= size) refs.delete(value);
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1259
1276
|
function getNodeForIndexedValue(ctx, current) {
|
|
1260
1277
|
const registeredId = ctx.refs.get(current);
|
|
1261
1278
|
if (registeredId != null) {
|
|
@@ -1354,6 +1371,12 @@ function getObjectKind(current, currentClass, features) {
|
|
|
1354
1371
|
if (features & 16 && (currentClass === BigInt64Array || currentClass === BigUint64Array)) return 9;
|
|
1355
1372
|
if (features & 1 && typeof AggregateError !== "undefined" && (currentClass === AggregateError || current instanceof AggregateError)) return 5;
|
|
1356
1373
|
if (features & 64 && typeof Temporal !== "undefined" && getTemporalType(currentClass) != null) return 15;
|
|
1374
|
+
if (ArrayBuffer.isView(current)) {
|
|
1375
|
+
const name = getTypedArrayName(current);
|
|
1376
|
+
if (name === void 0) return 10;
|
|
1377
|
+
if (name !== "BigInt64Array" && name !== "BigUint64Array") return 8;
|
|
1378
|
+
if (features & 16) return 9;
|
|
1379
|
+
}
|
|
1357
1380
|
if (current instanceof Error) return 4;
|
|
1358
1381
|
if (SYM_ITERATOR in current || SYM_ASYNC_ITERATOR in current) return 16;
|
|
1359
1382
|
return 0;
|
|
@@ -1394,11 +1417,29 @@ function createArrayBufferNode(ctx, id, current) {
|
|
|
1394
1417
|
function createAsyncParserContext(options) {
|
|
1395
1418
|
return { base: createBaseParserContext(options) };
|
|
1396
1419
|
}
|
|
1420
|
+
function ignoreRejection$1() {}
|
|
1421
|
+
/**
|
|
1422
|
+
* A promise gets its rejection handler only when the walk reaches it. While an
|
|
1423
|
+
* earlier sibling is still pending, a later one that rejects would be an
|
|
1424
|
+
* unhandled rejection, which ends a Node.js process by default. Mark the
|
|
1425
|
+
* promises among the children as handled before walking them.
|
|
1426
|
+
*/
|
|
1427
|
+
function handleChildPromises(values) {
|
|
1428
|
+
for (const value of values) if (value instanceof Promise) Promise.prototype.then.call(value, void 0, ignoreRejection$1);
|
|
1429
|
+
}
|
|
1397
1430
|
async function parseItems$1(ctx, depth, current) {
|
|
1431
|
+
assertArrayHoles(current);
|
|
1398
1432
|
const len = current.length;
|
|
1399
1433
|
const nodes = new Array(len);
|
|
1400
|
-
|
|
1401
|
-
|
|
1434
|
+
const values = new Array(len);
|
|
1435
|
+
const present = new Array(len);
|
|
1436
|
+
for (let i = 0; i < len; i++) {
|
|
1437
|
+
present[i] = i in current;
|
|
1438
|
+
if (present[i]) values[i] = current[i];
|
|
1439
|
+
}
|
|
1440
|
+
handleChildPromises(values);
|
|
1441
|
+
for (let i = 0, node; i < len; i++) if (present[i]) {
|
|
1442
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1402
1443
|
nodes[i] = node instanceof Promise ? await node : node;
|
|
1403
1444
|
} else nodes[i] = 0;
|
|
1404
1445
|
return nodes;
|
|
@@ -1411,10 +1452,12 @@ async function parseProperties$1(ctx, depth, properties) {
|
|
|
1411
1452
|
const len = keys.length;
|
|
1412
1453
|
const keyNodes = new Array(len);
|
|
1413
1454
|
const valueNodes = new Array(len);
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1455
|
+
const values = new Array(len);
|
|
1456
|
+
for (let i = 0; i < len; i++) values[i] = properties[keys[i]];
|
|
1457
|
+
handleChildPromises(values);
|
|
1458
|
+
for (let i = 0, node; i < len; i++) {
|
|
1459
|
+
keyNodes[i] = serializeString(keys[i]);
|
|
1460
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1418
1461
|
valueNodes[i] = node instanceof Promise ? await node : node;
|
|
1419
1462
|
}
|
|
1420
1463
|
if (SYM_ITERATOR in properties) {
|
|
@@ -1455,26 +1498,32 @@ async function parseError$1(ctx, depth, type, id, current) {
|
|
|
1455
1498
|
}
|
|
1456
1499
|
async function parseMap$1(ctx, depth, id, current) {
|
|
1457
1500
|
const sentinel = parseMapSentinel(ctx.base);
|
|
1458
|
-
const
|
|
1459
|
-
const
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1501
|
+
const entries = [...current.entries()];
|
|
1502
|
+
const keyNodes = new Array(entries.length);
|
|
1503
|
+
const valueNodes = new Array(entries.length);
|
|
1504
|
+
const keys = new Array(entries.length);
|
|
1505
|
+
const values = new Array(entries.length);
|
|
1506
|
+
for (let i = 0; i < entries.length; i++) {
|
|
1507
|
+
keys[i] = entries[i][0];
|
|
1508
|
+
values[i] = entries[i][1];
|
|
1509
|
+
}
|
|
1510
|
+
handleChildPromises(keys);
|
|
1511
|
+
handleChildPromises(values);
|
|
1512
|
+
for (let i = 0, node; i < entries.length; i++) {
|
|
1513
|
+
node = parseValueAsync(ctx, depth, keys[i]);
|
|
1464
1514
|
keyNodes[i] = node instanceof Promise ? await node : node;
|
|
1465
|
-
node = parseValueAsync(ctx, depth,
|
|
1515
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1466
1516
|
valueNodes[i] = node instanceof Promise ? await node : node;
|
|
1467
|
-
i++;
|
|
1468
1517
|
}
|
|
1469
1518
|
return createMapNode(id, sentinel, keyNodes, valueNodes);
|
|
1470
1519
|
}
|
|
1471
1520
|
async function parseSet$1(ctx, depth, id, current) {
|
|
1472
|
-
const
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
for (
|
|
1476
|
-
node = parseValueAsync(ctx, depth,
|
|
1477
|
-
items[i
|
|
1521
|
+
const values = [...current.keys()];
|
|
1522
|
+
const items = new Array(values.length);
|
|
1523
|
+
handleChildPromises(values);
|
|
1524
|
+
for (let i = 0, node; i < values.length; i++) {
|
|
1525
|
+
node = parseValueAsync(ctx, depth, values[i]);
|
|
1526
|
+
items[i] = node instanceof Promise ? await node : node;
|
|
1478
1527
|
}
|
|
1479
1528
|
return createSetNode(id, items);
|
|
1480
1529
|
}
|
|
@@ -1643,9 +1692,12 @@ async function parseAsync(ctx, depth, current) {
|
|
|
1643
1692
|
return await parseValueAsync(ctx, depth, current);
|
|
1644
1693
|
}
|
|
1645
1694
|
async function parseTopAsync(ctx, current) {
|
|
1695
|
+
const refs = ctx.base.refs;
|
|
1696
|
+
const size = refs.size;
|
|
1646
1697
|
try {
|
|
1647
1698
|
return await parseAsync(ctx, 0, current);
|
|
1648
1699
|
} catch (error) {
|
|
1700
|
+
rollbackRefs(refs, size);
|
|
1649
1701
|
throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
|
|
1650
1702
|
}
|
|
1651
1703
|
}
|
|
@@ -1686,6 +1738,8 @@ function applyObjectFlag(obj, flag) {
|
|
|
1686
1738
|
}
|
|
1687
1739
|
}
|
|
1688
1740
|
const DEFAULT_DEPTH_LIMIT = 1e3;
|
|
1741
|
+
const MAX_REGEXP_PROPERTY_ESCAPES = 32;
|
|
1742
|
+
const REGEXP_PROPERTY_ESCAPE = /\\[pP]\{/g;
|
|
1689
1743
|
function createBaseDeserializerContext(options) {
|
|
1690
1744
|
const maxBase64Length = options.maxBase64Length ?? DEFAULT_MAX_BASE64_LENGTH;
|
|
1691
1745
|
if (!Number.isSafeInteger(maxBase64Length) || maxBase64Length < 0) throw new RangeError("maxBase64Length must be a non-negative safe integer");
|
|
@@ -1696,7 +1750,8 @@ function createBaseDeserializerContext(options) {
|
|
|
1696
1750
|
refs,
|
|
1697
1751
|
features: options.features ?? 127 ^ (options.disabledFeatures ?? 0),
|
|
1698
1752
|
depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT,
|
|
1699
|
-
maxBase64Length
|
|
1753
|
+
maxBase64Length,
|
|
1754
|
+
regexpPropertyEscapes: MAX_REGEXP_PROPERTY_ESCAPES
|
|
1700
1755
|
};
|
|
1701
1756
|
}
|
|
1702
1757
|
function createVanillaDeserializerContext(options) {
|
|
@@ -1808,6 +1863,13 @@ function deserializeRegExp(ctx, node) {
|
|
|
1808
1863
|
if (ctx.base.features & 32) {
|
|
1809
1864
|
const source = deserializeString(node.c);
|
|
1810
1865
|
if (source.length > MAX_REGEXP_SOURCE_LENGTH) throw new SerovalMalformedNodeError(node);
|
|
1866
|
+
if (/[uv]/.test(String(node.m))) {
|
|
1867
|
+
const escapes = source.match(REGEXP_PROPERTY_ESCAPE);
|
|
1868
|
+
if (escapes) {
|
|
1869
|
+
ctx.base.regexpPropertyEscapes -= escapes.length;
|
|
1870
|
+
if (ctx.base.regexpPropertyEscapes < 0) throw new SerovalMalformedNodeError(node);
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1811
1873
|
return assignIndexedValue$1(ctx, node.i, new RegExp(source, node.m));
|
|
1812
1874
|
}
|
|
1813
1875
|
throw new SerovalUnsupportedNodeError(node);
|
|
@@ -1990,6 +2052,7 @@ function deserialize$1(ctx, depth, node) {
|
|
|
1990
2052
|
}
|
|
1991
2053
|
}
|
|
1992
2054
|
function deserializeTop(ctx, node) {
|
|
2055
|
+
ctx.base.regexpPropertyEscapes = MAX_REGEXP_PROPERTY_ESCAPES;
|
|
1993
2056
|
try {
|
|
1994
2057
|
return deserialize$1(ctx, 0, node);
|
|
1995
2058
|
} catch (error) {
|
|
@@ -2791,6 +2854,7 @@ function createSyncParserContext(options) {
|
|
|
2791
2854
|
};
|
|
2792
2855
|
}
|
|
2793
2856
|
function parseItems(ctx, depth, current) {
|
|
2857
|
+
assertArrayHoles(current);
|
|
2794
2858
|
const len = current.length;
|
|
2795
2859
|
const nodes = new Array(len);
|
|
2796
2860
|
for (let i = 0; i < len; i++) if (i in current) nodes[i] = parseSOS(ctx, depth, current[i]);
|
|
@@ -2980,9 +3044,12 @@ function parseSOS(ctx, depth, current) {
|
|
|
2980
3044
|
}
|
|
2981
3045
|
}
|
|
2982
3046
|
function parseTop(ctx, current) {
|
|
3047
|
+
const refs = ctx.base.refs;
|
|
3048
|
+
const size = refs.size;
|
|
2983
3049
|
try {
|
|
2984
3050
|
return parseSOS(ctx, 0, current);
|
|
2985
3051
|
} catch (error) {
|
|
3052
|
+
rollbackRefs(refs, size);
|
|
2986
3053
|
throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
|
|
2987
3054
|
}
|
|
2988
3055
|
}
|
|
@@ -3039,7 +3106,8 @@ function createStreamParsePluginContext(ctx, depth) {
|
|
|
3039
3106
|
state.queue.push({
|
|
3040
3107
|
node,
|
|
3041
3108
|
initial: false,
|
|
3042
|
-
accept: void 0
|
|
3109
|
+
accept: void 0,
|
|
3110
|
+
refs: ctx.base.refs.size
|
|
3043
3111
|
});
|
|
3044
3112
|
drainOutput(ctx);
|
|
3045
3113
|
}
|
|
@@ -3133,7 +3201,18 @@ function handlePromise(type, id, depth, data) {
|
|
|
3133
3201
|
}
|
|
3134
3202
|
function watchPromise(ctx, resolver, depth, current) {
|
|
3135
3203
|
pushPendingState(ctx);
|
|
3136
|
-
|
|
3204
|
+
let settled = false;
|
|
3205
|
+
current.then((data) => {
|
|
3206
|
+
if (!settled) {
|
|
3207
|
+
settled = true;
|
|
3208
|
+
handlePromise.call(ctx, 23, resolver, depth, data);
|
|
3209
|
+
}
|
|
3210
|
+
}, (data) => {
|
|
3211
|
+
if (!settled) {
|
|
3212
|
+
settled = true;
|
|
3213
|
+
handlePromise.call(ctx, 24, resolver, depth, data);
|
|
3214
|
+
}
|
|
3215
|
+
});
|
|
3137
3216
|
}
|
|
3138
3217
|
function returnIterator(iterator) {
|
|
3139
3218
|
Promise.resolve().then(() => iterator.return?.()).catch(() => {});
|
|
@@ -3184,6 +3263,7 @@ function createStreamParserState(options) {
|
|
|
3184
3263
|
pending: 0,
|
|
3185
3264
|
parsing: 0,
|
|
3186
3265
|
queue: [],
|
|
3266
|
+
head: 0,
|
|
3187
3267
|
writing: false,
|
|
3188
3268
|
inFlight: void 0,
|
|
3189
3269
|
reason: void 0,
|
|
@@ -3227,7 +3307,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
|
|
|
3227
3307
|
const record = {
|
|
3228
3308
|
node: void 0,
|
|
3229
3309
|
initial: false,
|
|
3230
|
-
accept
|
|
3310
|
+
accept,
|
|
3311
|
+
refs: ctx.base.refs.size
|
|
3231
3312
|
};
|
|
3232
3313
|
let failure;
|
|
3233
3314
|
state.queue.push(record);
|
|
@@ -3244,6 +3325,18 @@ function parseEvent(ctx, depth, value, wrap, accept) {
|
|
|
3244
3325
|
else drainOutput(ctx);
|
|
3245
3326
|
return failure?.value;
|
|
3246
3327
|
}
|
|
3328
|
+
function takeRecord(state) {
|
|
3329
|
+
const record = state.queue[state.head];
|
|
3330
|
+
state.queue[state.head++] = void 0;
|
|
3331
|
+
if (state.head === state.queue.length) {
|
|
3332
|
+
state.queue.length = 0;
|
|
3333
|
+
state.head = 0;
|
|
3334
|
+
} else if (state.head > 1024 && state.head * 2 > state.queue.length) {
|
|
3335
|
+
state.queue.splice(0, state.head);
|
|
3336
|
+
state.head = 0;
|
|
3337
|
+
}
|
|
3338
|
+
return record;
|
|
3339
|
+
}
|
|
3247
3340
|
/**
|
|
3248
3341
|
* Emits queued records one at a time. A callback that returns a promise
|
|
3249
3342
|
* pauses emission until it settles; the source event behind the record is
|
|
@@ -3252,8 +3345,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
|
|
|
3252
3345
|
function drainOutput(ctx) {
|
|
3253
3346
|
const state = ctx.state;
|
|
3254
3347
|
if (state.writing || state.parsing > 0) return;
|
|
3255
|
-
while (state.alive && state.queue.length
|
|
3256
|
-
const record = state
|
|
3348
|
+
while (state.alive && state.head < state.queue.length) {
|
|
3349
|
+
const record = takeRecord(state);
|
|
3257
3350
|
if (!record.node) continue;
|
|
3258
3351
|
let pending;
|
|
3259
3352
|
state.writing = true;
|
|
@@ -3310,12 +3403,14 @@ function drainOutput(ctx) {
|
|
|
3310
3403
|
}
|
|
3311
3404
|
function checkStreamParse(ctx) {
|
|
3312
3405
|
const state = ctx.state;
|
|
3313
|
-
if (state.alive && state.parsing === 0 && state.pending <= 0 && state.queue.length
|
|
3406
|
+
if (state.alive && state.parsing === 0 && state.pending <= 0 && state.head === state.queue.length && !state.writing) stopStreamParse(ctx, 0);
|
|
3314
3407
|
}
|
|
3315
3408
|
function startStreamParse(ctx, current) {
|
|
3316
3409
|
const state = ctx.state;
|
|
3317
3410
|
let parsed;
|
|
3318
3411
|
let failure;
|
|
3412
|
+
const refs = ctx.base.refs;
|
|
3413
|
+
const size = refs.size;
|
|
3319
3414
|
state.starting = true;
|
|
3320
3415
|
try {
|
|
3321
3416
|
state.parsing++;
|
|
@@ -3328,13 +3423,15 @@ function startStreamParse(ctx, current) {
|
|
|
3328
3423
|
if (!state.alive) releaseParserValues(ctx);
|
|
3329
3424
|
}
|
|
3330
3425
|
if (failure) {
|
|
3426
|
+
rollbackRefs(refs, size);
|
|
3331
3427
|
if (state.alive) stopStreamParse(ctx, 1, failure.value);
|
|
3332
3428
|
else throw failure.value;
|
|
3333
3429
|
} else if (parsed && state.alive) {
|
|
3334
3430
|
state.queue.unshift({
|
|
3335
3431
|
node: parsed,
|
|
3336
3432
|
initial: true,
|
|
3337
|
-
accept: void 0
|
|
3433
|
+
accept: void 0,
|
|
3434
|
+
refs: size
|
|
3338
3435
|
});
|
|
3339
3436
|
drainOutput(ctx);
|
|
3340
3437
|
}
|
|
@@ -3346,8 +3443,9 @@ function runCleanups(state) {
|
|
|
3346
3443
|
const cleanups = state.cleanups;
|
|
3347
3444
|
let failure;
|
|
3348
3445
|
state.cleanups = [];
|
|
3349
|
-
for (const record of state.queue) releaseRecord(record);
|
|
3446
|
+
for (const record of state.queue) if (record) releaseRecord(record);
|
|
3350
3447
|
state.queue.length = 0;
|
|
3448
|
+
state.head = 0;
|
|
3351
3449
|
if (state.inFlight) releaseRecord(state.inFlight);
|
|
3352
3450
|
state.inFlight = void 0;
|
|
3353
3451
|
for (let i = 0, len = cleanups.length; i < len; i++) try {
|
|
@@ -3377,6 +3475,8 @@ function stopStreamParse(ctx, mode, reason) {
|
|
|
3377
3475
|
const state = ctx.state;
|
|
3378
3476
|
if (state.alive) {
|
|
3379
3477
|
state.alive = false;
|
|
3478
|
+
const unsent = state.queue[state.head];
|
|
3479
|
+
if (mode !== 0 && unsent) rollbackRefs(ctx.base.refs, unsent.refs);
|
|
3380
3480
|
const onError = state.onError;
|
|
3381
3481
|
const onDone = state.onDone;
|
|
3382
3482
|
if (mode === 1 && !onError && !(reason instanceof SerovalParserError)) reason = new SerovalParserError(reason);
|