seroval 1.7.9 → 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.
@@ -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
- for (let i = 0, node; i < len; i++) if (i in current) {
1418
- node = parseValueAsync(ctx, depth, current[i]);
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
- for (let i = 0, key, node; i < len; i++) {
1432
- key = keys[i];
1433
- keyNodes[i] = serializeString(key);
1434
- node = parseValueAsync(ctx, depth, properties[key]);
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 keyNodes = new Array(current.size);
1476
- const valueNodes = new Array(current.size);
1477
- let node;
1478
- let i = 0;
1479
- for (const [key, value] of current.entries()) {
1480
- node = parseValueAsync(ctx, depth, key);
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, value);
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 items = new Array(current.size);
1490
- let node;
1491
- let i = 0;
1492
- for (const item of current.keys()) {
1493
- node = parseValueAsync(ctx, depth, item);
1494
- items[i++] = node instanceof Promise ? await node : node;
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
  }
@@ -2819,6 +2871,7 @@ function createSyncParserContext(options) {
2819
2871
  };
2820
2872
  }
2821
2873
  function parseItems(ctx, depth, current) {
2874
+ assertArrayHoles(current);
2822
2875
  const len = current.length;
2823
2876
  const nodes = new Array(len);
2824
2877
  for (let i = 0; i < len; i++) if (i in current) nodes[i] = parseSOS(ctx, depth, current[i]);
@@ -3008,9 +3061,12 @@ function parseSOS(ctx, depth, current) {
3008
3061
  }
3009
3062
  }
3010
3063
  function parseTop(ctx, current) {
3064
+ const refs = ctx.base.refs;
3065
+ const size = refs.size;
3011
3066
  try {
3012
3067
  return parseSOS(ctx, 0, current);
3013
3068
  } catch (error) {
3069
+ rollbackRefs(refs, size);
3014
3070
  throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3015
3071
  }
3016
3072
  }
@@ -3067,7 +3123,8 @@ function createStreamParsePluginContext(ctx, depth) {
3067
3123
  state.queue.push({
3068
3124
  node,
3069
3125
  initial: false,
3070
- accept: void 0
3126
+ accept: void 0,
3127
+ refs: ctx.base.refs.size
3071
3128
  });
3072
3129
  drainOutput(ctx);
3073
3130
  }
@@ -3161,7 +3218,18 @@ function handlePromise(type, id, depth, data) {
3161
3218
  }
3162
3219
  function watchPromise(ctx, resolver, depth, current) {
3163
3220
  pushPendingState(ctx);
3164
- current.then(handlePromise.bind(ctx, 23, resolver, depth), handlePromise.bind(ctx, 24, resolver, depth));
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
+ });
3165
3233
  }
3166
3234
  function returnIterator(iterator) {
3167
3235
  Promise.resolve().then(() => iterator.return?.()).catch(() => {});
@@ -3212,6 +3280,7 @@ function createStreamParserState(options) {
3212
3280
  pending: 0,
3213
3281
  parsing: 0,
3214
3282
  queue: [],
3283
+ head: 0,
3215
3284
  writing: false,
3216
3285
  inFlight: void 0,
3217
3286
  reason: void 0,
@@ -3255,7 +3324,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
3255
3324
  const record = {
3256
3325
  node: void 0,
3257
3326
  initial: false,
3258
- accept
3327
+ accept,
3328
+ refs: ctx.base.refs.size
3259
3329
  };
3260
3330
  let failure;
3261
3331
  state.queue.push(record);
@@ -3272,6 +3342,18 @@ function parseEvent(ctx, depth, value, wrap, accept) {
3272
3342
  else drainOutput(ctx);
3273
3343
  return failure?.value;
3274
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
+ }
3275
3357
  /**
3276
3358
  * Emits queued records one at a time. A callback that returns a promise
3277
3359
  * pauses emission until it settles; the source event behind the record is
@@ -3280,8 +3362,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
3280
3362
  function drainOutput(ctx) {
3281
3363
  const state = ctx.state;
3282
3364
  if (state.writing || state.parsing > 0) return;
3283
- while (state.alive && state.queue.length > 0) {
3284
- const record = state.queue.shift();
3365
+ while (state.alive && state.head < state.queue.length) {
3366
+ const record = takeRecord(state);
3285
3367
  if (!record.node) continue;
3286
3368
  let pending;
3287
3369
  state.writing = true;
@@ -3338,12 +3420,14 @@ function drainOutput(ctx) {
3338
3420
  }
3339
3421
  function checkStreamParse(ctx) {
3340
3422
  const state = ctx.state;
3341
- if (state.alive && state.parsing === 0 && state.pending <= 0 && state.queue.length === 0 && !state.writing) stopStreamParse(ctx, 0);
3423
+ if (state.alive && state.parsing === 0 && state.pending <= 0 && state.head === state.queue.length && !state.writing) stopStreamParse(ctx, 0);
3342
3424
  }
3343
3425
  function startStreamParse(ctx, current) {
3344
3426
  const state = ctx.state;
3345
3427
  let parsed;
3346
3428
  let failure;
3429
+ const refs = ctx.base.refs;
3430
+ const size = refs.size;
3347
3431
  state.starting = true;
3348
3432
  try {
3349
3433
  state.parsing++;
@@ -3356,13 +3440,15 @@ function startStreamParse(ctx, current) {
3356
3440
  if (!state.alive) releaseParserValues(ctx);
3357
3441
  }
3358
3442
  if (failure) {
3443
+ rollbackRefs(refs, size);
3359
3444
  if (state.alive) stopStreamParse(ctx, 1, failure.value);
3360
3445
  else throw failure.value;
3361
3446
  } else if (parsed && state.alive) {
3362
3447
  state.queue.unshift({
3363
3448
  node: parsed,
3364
3449
  initial: true,
3365
- accept: void 0
3450
+ accept: void 0,
3451
+ refs: size
3366
3452
  });
3367
3453
  drainOutput(ctx);
3368
3454
  }
@@ -3374,8 +3460,9 @@ function runCleanups(state) {
3374
3460
  const cleanups = state.cleanups;
3375
3461
  let failure;
3376
3462
  state.cleanups = [];
3377
- for (const record of state.queue) releaseRecord(record);
3463
+ for (const record of state.queue) if (record) releaseRecord(record);
3378
3464
  state.queue.length = 0;
3465
+ state.head = 0;
3379
3466
  if (state.inFlight) releaseRecord(state.inFlight);
3380
3467
  state.inFlight = void 0;
3381
3468
  for (let i = 0, len = cleanups.length; i < len; i++) try {
@@ -3405,6 +3492,8 @@ function stopStreamParse(ctx, mode, reason) {
3405
3492
  const state = ctx.state;
3406
3493
  if (state.alive) {
3407
3494
  state.alive = false;
3495
+ const unsent = state.queue[state.head];
3496
+ if (mode !== 0 && unsent) rollbackRefs(ctx.base.refs, unsent.refs);
3408
3497
  const onError = state.onError;
3409
3498
  const onDone = state.onDone;
3410
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
- for (let i = 0, node; i < len; i++) if (i in current) {
1417
- node = parseValueAsync(ctx, depth, current[i]);
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
- for (let i = 0, key, node; i < len; i++) {
1431
- key = keys[i];
1432
- keyNodes[i] = serializeString(key);
1433
- node = parseValueAsync(ctx, depth, properties[key]);
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 keyNodes = new Array(current.size);
1475
- const valueNodes = new Array(current.size);
1476
- let node;
1477
- let i = 0;
1478
- for (const [key, value] of current.entries()) {
1479
- node = parseValueAsync(ctx, depth, key);
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, value);
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 items = new Array(current.size);
1489
- let node;
1490
- let i = 0;
1491
- for (const item of current.keys()) {
1492
- node = parseValueAsync(ctx, depth, item);
1493
- items[i++] = node instanceof Promise ? await node : node;
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
  }
@@ -2818,6 +2870,7 @@ function createSyncParserContext(options) {
2818
2870
  };
2819
2871
  }
2820
2872
  function parseItems(ctx, depth, current) {
2873
+ assertArrayHoles(current);
2821
2874
  const len = current.length;
2822
2875
  const nodes = new Array(len);
2823
2876
  for (let i = 0; i < len; i++) if (i in current) nodes[i] = parseSOS(ctx, depth, current[i]);
@@ -3007,9 +3060,12 @@ function parseSOS(ctx, depth, current) {
3007
3060
  }
3008
3061
  }
3009
3062
  function parseTop(ctx, current) {
3063
+ const refs = ctx.base.refs;
3064
+ const size = refs.size;
3010
3065
  try {
3011
3066
  return parseSOS(ctx, 0, current);
3012
3067
  } catch (error) {
3068
+ rollbackRefs(refs, size);
3013
3069
  throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3014
3070
  }
3015
3071
  }
@@ -3066,7 +3122,8 @@ function createStreamParsePluginContext(ctx, depth) {
3066
3122
  state.queue.push({
3067
3123
  node,
3068
3124
  initial: false,
3069
- accept: void 0
3125
+ accept: void 0,
3126
+ refs: ctx.base.refs.size
3070
3127
  });
3071
3128
  drainOutput(ctx);
3072
3129
  }
@@ -3160,7 +3217,18 @@ function handlePromise(type, id, depth, data) {
3160
3217
  }
3161
3218
  function watchPromise(ctx, resolver, depth, current) {
3162
3219
  pushPendingState(ctx);
3163
- current.then(handlePromise.bind(ctx, 23, resolver, depth), handlePromise.bind(ctx, 24, resolver, depth));
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
+ });
3164
3232
  }
3165
3233
  function returnIterator(iterator) {
3166
3234
  Promise.resolve().then(() => iterator.return?.()).catch(() => {});
@@ -3211,6 +3279,7 @@ function createStreamParserState(options) {
3211
3279
  pending: 0,
3212
3280
  parsing: 0,
3213
3281
  queue: [],
3282
+ head: 0,
3214
3283
  writing: false,
3215
3284
  inFlight: void 0,
3216
3285
  reason: void 0,
@@ -3254,7 +3323,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
3254
3323
  const record = {
3255
3324
  node: void 0,
3256
3325
  initial: false,
3257
- accept
3326
+ accept,
3327
+ refs: ctx.base.refs.size
3258
3328
  };
3259
3329
  let failure;
3260
3330
  state.queue.push(record);
@@ -3271,6 +3341,18 @@ function parseEvent(ctx, depth, value, wrap, accept) {
3271
3341
  else drainOutput(ctx);
3272
3342
  return failure?.value;
3273
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
+ }
3274
3356
  /**
3275
3357
  * Emits queued records one at a time. A callback that returns a promise
3276
3358
  * pauses emission until it settles; the source event behind the record is
@@ -3279,8 +3361,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
3279
3361
  function drainOutput(ctx) {
3280
3362
  const state = ctx.state;
3281
3363
  if (state.writing || state.parsing > 0) return;
3282
- while (state.alive && state.queue.length > 0) {
3283
- const record = state.queue.shift();
3364
+ while (state.alive && state.head < state.queue.length) {
3365
+ const record = takeRecord(state);
3284
3366
  if (!record.node) continue;
3285
3367
  let pending;
3286
3368
  state.writing = true;
@@ -3337,12 +3419,14 @@ function drainOutput(ctx) {
3337
3419
  }
3338
3420
  function checkStreamParse(ctx) {
3339
3421
  const state = ctx.state;
3340
- if (state.alive && state.parsing === 0 && state.pending <= 0 && state.queue.length === 0 && !state.writing) stopStreamParse(ctx, 0);
3422
+ if (state.alive && state.parsing === 0 && state.pending <= 0 && state.head === state.queue.length && !state.writing) stopStreamParse(ctx, 0);
3341
3423
  }
3342
3424
  function startStreamParse(ctx, current) {
3343
3425
  const state = ctx.state;
3344
3426
  let parsed;
3345
3427
  let failure;
3428
+ const refs = ctx.base.refs;
3429
+ const size = refs.size;
3346
3430
  state.starting = true;
3347
3431
  try {
3348
3432
  state.parsing++;
@@ -3355,13 +3439,15 @@ function startStreamParse(ctx, current) {
3355
3439
  if (!state.alive) releaseParserValues(ctx);
3356
3440
  }
3357
3441
  if (failure) {
3442
+ rollbackRefs(refs, size);
3358
3443
  if (state.alive) stopStreamParse(ctx, 1, failure.value);
3359
3444
  else throw failure.value;
3360
3445
  } else if (parsed && state.alive) {
3361
3446
  state.queue.unshift({
3362
3447
  node: parsed,
3363
3448
  initial: true,
3364
- accept: void 0
3449
+ accept: void 0,
3450
+ refs: size
3365
3451
  });
3366
3452
  drainOutput(ctx);
3367
3453
  }
@@ -3373,8 +3459,9 @@ function runCleanups(state) {
3373
3459
  const cleanups = state.cleanups;
3374
3460
  let failure;
3375
3461
  state.cleanups = [];
3376
- for (const record of state.queue) releaseRecord(record);
3462
+ for (const record of state.queue) if (record) releaseRecord(record);
3377
3463
  state.queue.length = 0;
3464
+ state.head = 0;
3378
3465
  if (state.inFlight) releaseRecord(state.inFlight);
3379
3466
  state.inFlight = void 0;
3380
3467
  for (let i = 0, len = cleanups.length; i < len; i++) try {
@@ -3404,6 +3491,8 @@ function stopStreamParse(ctx, mode, reason) {
3404
3491
  const state = ctx.state;
3405
3492
  if (state.alive) {
3406
3493
  state.alive = false;
3494
+ const unsent = state.queue[state.head];
3495
+ if (mode !== 0 && unsent) rollbackRefs(ctx.base.refs, unsent.refs);
3407
3496
  const onError = state.onError;
3408
3497
  const onDone = state.onDone;
3409
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
- for (let i = 0, node; i < len; i++) if (i in current) {
1402
- node = parseValueAsync(ctx, depth, current[i]);
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
- for (let i = 0, key, node; i < len; i++) {
1416
- key = keys[i];
1417
- keyNodes[i] = serializeString(key);
1418
- node = parseValueAsync(ctx, depth, properties[key]);
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 keyNodes = new Array(current.size);
1460
- const valueNodes = new Array(current.size);
1461
- let node;
1462
- let i = 0;
1463
- for (const [key, value] of current.entries()) {
1464
- node = parseValueAsync(ctx, depth, key);
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, value);
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 items = new Array(current.size);
1474
- let node;
1475
- let i = 0;
1476
- for (const item of current.keys()) {
1477
- node = parseValueAsync(ctx, depth, item);
1478
- items[i++] = node instanceof Promise ? await node : node;
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
  }
@@ -2803,6 +2855,7 @@ function createSyncParserContext(options) {
2803
2855
  };
2804
2856
  }
2805
2857
  function parseItems(ctx, depth, current) {
2858
+ assertArrayHoles(current);
2806
2859
  const len = current.length;
2807
2860
  const nodes = new Array(len);
2808
2861
  for (let i = 0; i < len; i++) if (i in current) nodes[i] = parseSOS(ctx, depth, current[i]);
@@ -2992,9 +3045,12 @@ function parseSOS(ctx, depth, current) {
2992
3045
  }
2993
3046
  }
2994
3047
  function parseTop(ctx, current) {
3048
+ const refs = ctx.base.refs;
3049
+ const size = refs.size;
2995
3050
  try {
2996
3051
  return parseSOS(ctx, 0, current);
2997
3052
  } catch (error) {
3053
+ rollbackRefs(refs, size);
2998
3054
  throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
2999
3055
  }
3000
3056
  }
@@ -3051,7 +3107,8 @@ function createStreamParsePluginContext(ctx, depth) {
3051
3107
  state.queue.push({
3052
3108
  node,
3053
3109
  initial: false,
3054
- accept: void 0
3110
+ accept: void 0,
3111
+ refs: ctx.base.refs.size
3055
3112
  });
3056
3113
  drainOutput(ctx);
3057
3114
  }
@@ -3145,7 +3202,18 @@ function handlePromise(type, id, depth, data) {
3145
3202
  }
3146
3203
  function watchPromise(ctx, resolver, depth, current) {
3147
3204
  pushPendingState(ctx);
3148
- current.then(handlePromise.bind(ctx, 23, resolver, depth), handlePromise.bind(ctx, 24, resolver, depth));
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
+ });
3149
3217
  }
3150
3218
  function returnIterator(iterator) {
3151
3219
  Promise.resolve().then(() => iterator.return?.()).catch(() => {});
@@ -3196,6 +3264,7 @@ function createStreamParserState(options) {
3196
3264
  pending: 0,
3197
3265
  parsing: 0,
3198
3266
  queue: [],
3267
+ head: 0,
3199
3268
  writing: false,
3200
3269
  inFlight: void 0,
3201
3270
  reason: void 0,
@@ -3239,7 +3308,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
3239
3308
  const record = {
3240
3309
  node: void 0,
3241
3310
  initial: false,
3242
- accept
3311
+ accept,
3312
+ refs: ctx.base.refs.size
3243
3313
  };
3244
3314
  let failure;
3245
3315
  state.queue.push(record);
@@ -3256,6 +3326,18 @@ function parseEvent(ctx, depth, value, wrap, accept) {
3256
3326
  else drainOutput(ctx);
3257
3327
  return failure?.value;
3258
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
+ }
3259
3341
  /**
3260
3342
  * Emits queued records one at a time. A callback that returns a promise
3261
3343
  * pauses emission until it settles; the source event behind the record is
@@ -3264,8 +3346,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
3264
3346
  function drainOutput(ctx) {
3265
3347
  const state = ctx.state;
3266
3348
  if (state.writing || state.parsing > 0) return;
3267
- while (state.alive && state.queue.length > 0) {
3268
- const record = state.queue.shift();
3349
+ while (state.alive && state.head < state.queue.length) {
3350
+ const record = takeRecord(state);
3269
3351
  if (!record.node) continue;
3270
3352
  let pending;
3271
3353
  state.writing = true;
@@ -3322,12 +3404,14 @@ function drainOutput(ctx) {
3322
3404
  }
3323
3405
  function checkStreamParse(ctx) {
3324
3406
  const state = ctx.state;
3325
- if (state.alive && state.parsing === 0 && state.pending <= 0 && state.queue.length === 0 && !state.writing) stopStreamParse(ctx, 0);
3407
+ if (state.alive && state.parsing === 0 && state.pending <= 0 && state.head === state.queue.length && !state.writing) stopStreamParse(ctx, 0);
3326
3408
  }
3327
3409
  function startStreamParse(ctx, current) {
3328
3410
  const state = ctx.state;
3329
3411
  let parsed;
3330
3412
  let failure;
3413
+ const refs = ctx.base.refs;
3414
+ const size = refs.size;
3331
3415
  state.starting = true;
3332
3416
  try {
3333
3417
  state.parsing++;
@@ -3340,13 +3424,15 @@ function startStreamParse(ctx, current) {
3340
3424
  if (!state.alive) releaseParserValues(ctx);
3341
3425
  }
3342
3426
  if (failure) {
3427
+ rollbackRefs(refs, size);
3343
3428
  if (state.alive) stopStreamParse(ctx, 1, failure.value);
3344
3429
  else throw failure.value;
3345
3430
  } else if (parsed && state.alive) {
3346
3431
  state.queue.unshift({
3347
3432
  node: parsed,
3348
3433
  initial: true,
3349
- accept: void 0
3434
+ accept: void 0,
3435
+ refs: size
3350
3436
  });
3351
3437
  drainOutput(ctx);
3352
3438
  }
@@ -3358,8 +3444,9 @@ function runCleanups(state) {
3358
3444
  const cleanups = state.cleanups;
3359
3445
  let failure;
3360
3446
  state.cleanups = [];
3361
- for (const record of state.queue) releaseRecord(record);
3447
+ for (const record of state.queue) if (record) releaseRecord(record);
3362
3448
  state.queue.length = 0;
3449
+ state.head = 0;
3363
3450
  if (state.inFlight) releaseRecord(state.inFlight);
3364
3451
  state.inFlight = void 0;
3365
3452
  for (let i = 0, len = cleanups.length; i < len; i++) try {
@@ -3389,6 +3476,8 @@ function stopStreamParse(ctx, mode, reason) {
3389
3476
  const state = ctx.state;
3390
3477
  if (state.alive) {
3391
3478
  state.alive = false;
3479
+ const unsent = state.queue[state.head];
3480
+ if (mode !== 0 && unsent) rollbackRefs(ctx.base.refs, unsent.refs);
3392
3481
  const onError = state.onError;
3393
3482
  const onDone = state.onDone;
3394
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
- for (let i = 0, node; i < len; i++) if (i in current) {
1401
- node = parseValueAsync(ctx, depth, current[i]);
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
- for (let i = 0, key, node; i < len; i++) {
1415
- key = keys[i];
1416
- keyNodes[i] = serializeString(key);
1417
- node = parseValueAsync(ctx, depth, properties[key]);
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 keyNodes = new Array(current.size);
1459
- const valueNodes = new Array(current.size);
1460
- let node;
1461
- let i = 0;
1462
- for (const [key, value] of current.entries()) {
1463
- node = parseValueAsync(ctx, depth, key);
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, value);
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 items = new Array(current.size);
1473
- let node;
1474
- let i = 0;
1475
- for (const item of current.keys()) {
1476
- node = parseValueAsync(ctx, depth, item);
1477
- items[i++] = node instanceof Promise ? await node : node;
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
  }
@@ -2802,6 +2854,7 @@ function createSyncParserContext(options) {
2802
2854
  };
2803
2855
  }
2804
2856
  function parseItems(ctx, depth, current) {
2857
+ assertArrayHoles(current);
2805
2858
  const len = current.length;
2806
2859
  const nodes = new Array(len);
2807
2860
  for (let i = 0; i < len; i++) if (i in current) nodes[i] = parseSOS(ctx, depth, current[i]);
@@ -2991,9 +3044,12 @@ function parseSOS(ctx, depth, current) {
2991
3044
  }
2992
3045
  }
2993
3046
  function parseTop(ctx, current) {
3047
+ const refs = ctx.base.refs;
3048
+ const size = refs.size;
2994
3049
  try {
2995
3050
  return parseSOS(ctx, 0, current);
2996
3051
  } catch (error) {
3052
+ rollbackRefs(refs, size);
2997
3053
  throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
2998
3054
  }
2999
3055
  }
@@ -3050,7 +3106,8 @@ function createStreamParsePluginContext(ctx, depth) {
3050
3106
  state.queue.push({
3051
3107
  node,
3052
3108
  initial: false,
3053
- accept: void 0
3109
+ accept: void 0,
3110
+ refs: ctx.base.refs.size
3054
3111
  });
3055
3112
  drainOutput(ctx);
3056
3113
  }
@@ -3144,7 +3201,18 @@ function handlePromise(type, id, depth, data) {
3144
3201
  }
3145
3202
  function watchPromise(ctx, resolver, depth, current) {
3146
3203
  pushPendingState(ctx);
3147
- current.then(handlePromise.bind(ctx, 23, resolver, depth), handlePromise.bind(ctx, 24, resolver, depth));
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
+ });
3148
3216
  }
3149
3217
  function returnIterator(iterator) {
3150
3218
  Promise.resolve().then(() => iterator.return?.()).catch(() => {});
@@ -3195,6 +3263,7 @@ function createStreamParserState(options) {
3195
3263
  pending: 0,
3196
3264
  parsing: 0,
3197
3265
  queue: [],
3266
+ head: 0,
3198
3267
  writing: false,
3199
3268
  inFlight: void 0,
3200
3269
  reason: void 0,
@@ -3238,7 +3307,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
3238
3307
  const record = {
3239
3308
  node: void 0,
3240
3309
  initial: false,
3241
- accept
3310
+ accept,
3311
+ refs: ctx.base.refs.size
3242
3312
  };
3243
3313
  let failure;
3244
3314
  state.queue.push(record);
@@ -3255,6 +3325,18 @@ function parseEvent(ctx, depth, value, wrap, accept) {
3255
3325
  else drainOutput(ctx);
3256
3326
  return failure?.value;
3257
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
+ }
3258
3340
  /**
3259
3341
  * Emits queued records one at a time. A callback that returns a promise
3260
3342
  * pauses emission until it settles; the source event behind the record is
@@ -3263,8 +3345,8 @@ function parseEvent(ctx, depth, value, wrap, accept) {
3263
3345
  function drainOutput(ctx) {
3264
3346
  const state = ctx.state;
3265
3347
  if (state.writing || state.parsing > 0) return;
3266
- while (state.alive && state.queue.length > 0) {
3267
- const record = state.queue.shift();
3348
+ while (state.alive && state.head < state.queue.length) {
3349
+ const record = takeRecord(state);
3268
3350
  if (!record.node) continue;
3269
3351
  let pending;
3270
3352
  state.writing = true;
@@ -3321,12 +3403,14 @@ function drainOutput(ctx) {
3321
3403
  }
3322
3404
  function checkStreamParse(ctx) {
3323
3405
  const state = ctx.state;
3324
- if (state.alive && state.parsing === 0 && state.pending <= 0 && state.queue.length === 0 && !state.writing) stopStreamParse(ctx, 0);
3406
+ if (state.alive && state.parsing === 0 && state.pending <= 0 && state.head === state.queue.length && !state.writing) stopStreamParse(ctx, 0);
3325
3407
  }
3326
3408
  function startStreamParse(ctx, current) {
3327
3409
  const state = ctx.state;
3328
3410
  let parsed;
3329
3411
  let failure;
3412
+ const refs = ctx.base.refs;
3413
+ const size = refs.size;
3330
3414
  state.starting = true;
3331
3415
  try {
3332
3416
  state.parsing++;
@@ -3339,13 +3423,15 @@ function startStreamParse(ctx, current) {
3339
3423
  if (!state.alive) releaseParserValues(ctx);
3340
3424
  }
3341
3425
  if (failure) {
3426
+ rollbackRefs(refs, size);
3342
3427
  if (state.alive) stopStreamParse(ctx, 1, failure.value);
3343
3428
  else throw failure.value;
3344
3429
  } else if (parsed && state.alive) {
3345
3430
  state.queue.unshift({
3346
3431
  node: parsed,
3347
3432
  initial: true,
3348
- accept: void 0
3433
+ accept: void 0,
3434
+ refs: size
3349
3435
  });
3350
3436
  drainOutput(ctx);
3351
3437
  }
@@ -3357,8 +3443,9 @@ function runCleanups(state) {
3357
3443
  const cleanups = state.cleanups;
3358
3444
  let failure;
3359
3445
  state.cleanups = [];
3360
- for (const record of state.queue) releaseRecord(record);
3446
+ for (const record of state.queue) if (record) releaseRecord(record);
3361
3447
  state.queue.length = 0;
3448
+ state.head = 0;
3362
3449
  if (state.inFlight) releaseRecord(state.inFlight);
3363
3450
  state.inFlight = void 0;
3364
3451
  for (let i = 0, len = cleanups.length; i < len; i++) try {
@@ -3388,6 +3475,8 @@ function stopStreamParse(ctx, mode, reason) {
3388
3475
  const state = ctx.state;
3389
3476
  if (state.alive) {
3390
3477
  state.alive = false;
3478
+ const unsent = state.queue[state.head];
3479
+ if (mode !== 0 && unsent) rollbackRefs(ctx.base.refs, unsent.refs);
3391
3480
  const onError = state.onError;
3392
3481
  const onDone = state.onDone;
3393
3482
  if (mode === 1 && !onError && !(reason instanceof SerovalParserError)) reason = new SerovalParserError(reason);
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "seroval",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "1.7.9",
5
+ "version": "1.7.10",
6
6
  "files": [
7
7
  "dist"
8
8
  ],