seroval 1.7.7 → 1.7.9

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.
@@ -8,7 +8,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
8
8
  let Feature = /* @__PURE__ */ function(Feature) {
9
9
  Feature[Feature["AggregateError"] = 1] = "AggregateError";
10
10
  /**
11
- * @deprecated Has no effect. Output always uses arrow functions.
11
+ * @deprecated Has no effect. Output follows the syntax of the build seroval
12
+ * runs in: arrow functions, or `function` expressions in an ES5 build.
12
13
  */
13
14
  Feature[Feature["ArrowFunction"] = 2] = "ArrowFunction";
14
15
  Feature[Feature["ErrorPrototypeStack"] = 4] = "ErrorPrototypeStack";
@@ -166,7 +167,8 @@ function escapeCharCode(code) {
166
167
  default: return;
167
168
  }
168
169
  }
169
- function serializeString(str) {
170
+ function serializeString(input) {
171
+ const str = "" + input;
170
172
  if (!NEEDS_ESCAPE.test(str)) return str;
171
173
  if (str.length >= MIN_JSON_STRINGIFY_LENGTH && !JSON_ESCAPE_DIFFERENCES.test(str)) {
172
174
  let result = JSON.stringify(str).slice(1, -1);
@@ -1457,7 +1459,8 @@ async function parsePlainObject$1(ctx, depth, id, current, empty) {
1457
1459
  return createObjectNode(id, current, empty, await parseProperties$1(ctx, depth, current));
1458
1460
  }
1459
1461
  async function parseBoxed$1(ctx, depth, id, current) {
1460
- return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
1462
+ const node = parseValueAsync(ctx, depth, current.valueOf());
1463
+ return createBoxedNode(id, node instanceof Promise ? await node : node);
1461
1464
  }
1462
1465
  async function parseView$1(ctx, depth, type, id, current) {
1463
1466
  const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
@@ -1566,14 +1569,13 @@ async function parseAsyncIterable$1(ctx, depth, current) {
1566
1569
  const sequence = [];
1567
1570
  for (;;) {
1568
1571
  let result;
1572
+ markParserRef(ctx.base, id);
1569
1573
  try {
1570
1574
  result = await iterator.next();
1571
1575
  } catch (error) {
1572
- markParserRef(ctx.base, id);
1573
1576
  sequence.push(createStreamEventNode(33, id, await parseAsync(ctx, depth, error)));
1574
1577
  break;
1575
1578
  }
1576
- markParserRef(ctx.base, id);
1577
1579
  let parsed;
1578
1580
  try {
1579
1581
  parsed = await parseAsync(ctx, depth, result.value);
@@ -1701,6 +1703,8 @@ function applyObjectFlag(obj, flag) {
1701
1703
  }
1702
1704
  }
1703
1705
  const DEFAULT_DEPTH_LIMIT = 1e3;
1706
+ const MAX_REGEXP_PROPERTY_ESCAPES = 32;
1707
+ const REGEXP_PROPERTY_ESCAPE = /\\[pP]\{/g;
1704
1708
  function createBaseDeserializerContext(options) {
1705
1709
  const maxBase64Length = options.maxBase64Length ?? DEFAULT_MAX_BASE64_LENGTH;
1706
1710
  if (!Number.isSafeInteger(maxBase64Length) || maxBase64Length < 0) throw new RangeError("maxBase64Length must be a non-negative safe integer");
@@ -1711,7 +1715,8 @@ function createBaseDeserializerContext(options) {
1711
1715
  refs,
1712
1716
  features: options.features ?? 127 ^ (options.disabledFeatures ?? 0),
1713
1717
  depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT,
1714
- maxBase64Length
1718
+ maxBase64Length,
1719
+ regexpPropertyEscapes: MAX_REGEXP_PROPERTY_ESCAPES
1715
1720
  };
1716
1721
  }
1717
1722
  function createVanillaDeserializerContext(options) {
@@ -1823,6 +1828,13 @@ function deserializeRegExp(ctx, node) {
1823
1828
  if (ctx.base.features & 32) {
1824
1829
  const source = deserializeString(node.c);
1825
1830
  if (source.length > MAX_REGEXP_SOURCE_LENGTH) throw new SerovalMalformedNodeError(node);
1831
+ if (/[uv]/.test(String(node.m))) {
1832
+ const escapes = source.match(REGEXP_PROPERTY_ESCAPE);
1833
+ if (escapes) {
1834
+ ctx.base.regexpPropertyEscapes -= escapes.length;
1835
+ if (ctx.base.regexpPropertyEscapes < 0) throw new SerovalMalformedNodeError(node);
1836
+ }
1837
+ }
1826
1838
  return assignIndexedValue$1(ctx, node.i, new RegExp(source, node.m));
1827
1839
  }
1828
1840
  throw new SerovalUnsupportedNodeError(node);
@@ -2005,6 +2017,7 @@ function deserialize$1(ctx, depth, node) {
2005
2017
  }
2006
2018
  }
2007
2019
  function deserializeTop(ctx, node) {
2020
+ ctx.base.regexpPropertyEscapes = MAX_REGEXP_PROPERTY_ESCAPES;
2008
2021
  try {
2009
2022
  return deserialize$1(ctx, 0, node);
2010
2023
  } catch (error) {
@@ -2274,10 +2287,6 @@ function createObjectAssign(ctx, ref, key, value) {
2274
2287
  function createSequenceAssign(ctx, ref, index, value) {
2275
2288
  createAssignment(ctx.base, getRefParam(ctx, ref) + ".v[" + index + "]", value);
2276
2289
  }
2277
- /**
2278
- * Checks if the value is in the stack. Stack here is a reference
2279
- * structure to know if a object is to be accessed in a TDZ.
2280
- */
2281
2290
  function getKeyKind(key) {
2282
2291
  const first = key.charCodeAt(0);
2283
2292
  if (first >= 48 && first <= 57) {
@@ -2287,6 +2296,10 @@ function getKeyKind(key) {
2287
2296
  if (key === "Infinity") return 2;
2288
2297
  return isValidIdentifier(key) ? 1 : 0;
2289
2298
  }
2299
+ /**
2300
+ * Checks if the value is in the stack. Stack here is a reference
2301
+ * structure to know if a object is to be accessed in a TDZ.
2302
+ */
2290
2303
  function isIndexedValueInStack(ctx, node) {
2291
2304
  return node.t === 4 && ctx.stack.includes(node.i);
2292
2305
  }
@@ -2365,8 +2378,7 @@ function serializeProperty(ctx, source, key, val) {
2365
2378
  if (isIndexedValueInStack(ctx.base, val)) {
2366
2379
  const refParam = getRefParam(ctx, val.i);
2367
2380
  markSerializerRef(ctx.base, source.i);
2368
- if (kind === 1) createObjectAssign(ctx, source.i, key, refParam);
2369
- else createArrayAssign(ctx, source.i, kind === 2 ? key : "\"" + key + "\"", refParam);
2381
+ createKeyAssign(ctx, source.i, kind, key, refParam);
2370
2382
  return "";
2371
2383
  }
2372
2384
  if (key !== "__proto__") return (kind === 0 ? "\"" + key + "\"" : key) + ":" + serialize$1(ctx, val);
@@ -2769,19 +2781,17 @@ function serializeCross(ctx, tree) {
2769
2781
  if (id == null) return result;
2770
2782
  const patches = resolvePatches(ctx.base);
2771
2783
  const ref = getRefParam(ctx, id);
2772
- const scopeId = ctx.state.scopeId;
2773
- const params = scopeId == null ? "" : "$R";
2774
2784
  let body = result;
2775
2785
  if (patches) {
2776
2786
  if (!ctx.base.assignAll && !result.startsWith(ref + "=")) body = ref + "=" + result;
2777
2787
  body = "(" + body + "," + patches + ref + ")";
2778
2788
  }
2779
- if (params === "") {
2789
+ const scopeId = ctx.state.scopeId;
2790
+ if (scopeId == null) {
2780
2791
  if (tree.t === 10 && !patches) return "(" + body + ")";
2781
2792
  return body;
2782
2793
  }
2783
- const args = scopeId == null ? "()" : "($R[\"" + serializeString(scopeId) + "\"])";
2784
- return "(" + createFunction([params], body) + ")" + args;
2794
+ return "(" + createFunction(["$R"], body) + ")($R[\"" + serializeString(scopeId) + "\"])";
2785
2795
  }
2786
2796
  function wrapSerializationError(error) {
2787
2797
  return error instanceof SerovalSerializationError ? error : new SerovalSerializationError(error);
@@ -3143,7 +3153,7 @@ function consumeLiveStream(ctx, depth, id, current) {
3143
3153
  });
3144
3154
  }
3145
3155
  function wrapPromiseResult(ctx, type, id, node) {
3146
- return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx.base, type === 23 ? 2 : 3), node], void 0, void 0, void 0, void 0);
3156
+ return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx.base, type === 23 ? 2 : 3), node]);
3147
3157
  }
3148
3158
  function handlePromise(type, id, depth, data) {
3149
3159
  if (this.state.alive) parseEvent(this, depth, data, wrapPromiseResult.bind(null, this, type, id), void 0);
@@ -3205,6 +3215,7 @@ function createStreamParserState(options) {
3205
3215
  writing: false,
3206
3216
  inFlight: void 0,
3207
3217
  reason: void 0,
3218
+ starting: false,
3208
3219
  onParse: options.onParse,
3209
3220
  onError: options.onError,
3210
3221
  onDone: options.onDone,
@@ -3333,25 +3344,30 @@ function startStreamParse(ctx, current) {
3333
3344
  const state = ctx.state;
3334
3345
  let parsed;
3335
3346
  let failure;
3336
- state.parsing++;
3347
+ state.starting = true;
3337
3348
  try {
3338
- parsed = parseSOS(ctx, 0, current);
3339
- } catch (error) {
3340
- failure = { value: error };
3349
+ state.parsing++;
3350
+ try {
3351
+ parsed = parseSOS(ctx, 0, current);
3352
+ } catch (error) {
3353
+ failure = { value: error };
3354
+ } finally {
3355
+ state.parsing--;
3356
+ if (!state.alive) releaseParserValues(ctx);
3357
+ }
3358
+ if (failure) {
3359
+ if (state.alive) stopStreamParse(ctx, 1, failure.value);
3360
+ else throw failure.value;
3361
+ } else if (parsed && state.alive) {
3362
+ state.queue.unshift({
3363
+ node: parsed,
3364
+ initial: true,
3365
+ accept: void 0
3366
+ });
3367
+ drainOutput(ctx);
3368
+ }
3341
3369
  } finally {
3342
- state.parsing--;
3343
- if (!state.alive) releaseParserValues(ctx);
3344
- }
3345
- if (failure) {
3346
- if (state.alive) stopStreamParse(ctx, 1, failure.value);
3347
- else throw failure.value;
3348
- } else if (parsed && state.alive) {
3349
- state.queue.unshift({
3350
- node: parsed,
3351
- initial: true,
3352
- accept: void 0
3353
- });
3354
- drainOutput(ctx);
3370
+ state.starting = false;
3355
3371
  }
3356
3372
  }
3357
3373
  function runCleanups(state) {
@@ -3402,7 +3418,7 @@ function stopStreamParse(ctx, mode, reason) {
3402
3418
  try {
3403
3419
  if (mode === 1) {
3404
3420
  if (onError) onError(reason);
3405
- else throw reason;
3421
+ else if (state.starting) throw reason;
3406
3422
  } else if (mode === 0) onDone?.();
3407
3423
  } catch (error) {
3408
3424
  failure = { value: error };
@@ -3569,6 +3585,7 @@ var Serializer = class {
3569
3585
  if (this.alive && !this.flushed && options) {
3570
3586
  this.pending++;
3571
3587
  this.keys.add(key);
3588
+ let starting = true;
3572
3589
  const cleanup = crossSerializeStream(value, {
3573
3590
  plugins: this.plugins,
3574
3591
  scopeId: options.scopeId,
@@ -3580,7 +3597,7 @@ var Serializer = class {
3580
3597
  let failure;
3581
3598
  try {
3582
3599
  if (options.onError) options.onError(error);
3583
- else throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3600
+ else if (starting) throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3584
3601
  } catch (handlerError) {
3585
3602
  failure = { value: handlerError };
3586
3603
  }
@@ -3599,6 +3616,7 @@ var Serializer = class {
3599
3616
  this.finishWrite();
3600
3617
  }
3601
3618
  });
3619
+ starting = false;
3602
3620
  if (this.alive) this.cleanups.push(cleanup);
3603
3621
  else cleanup();
3604
3622
  }
@@ -7,7 +7,8 @@
7
7
  export declare enum Feature {
8
8
  AggregateError = 1,
9
9
  /**
10
- * @deprecated Has no effect. Output always uses arrow functions.
10
+ * @deprecated Has no effect. Output follows the syntax of the build seroval
11
+ * runs in: arrow functions, or `function` expressions in an ES5 build.
11
12
  */
12
13
  ArrowFunction = 2,
13
14
  ErrorPrototypeStack = 4,
@@ -7,7 +7,8 @@
7
7
  export declare enum Feature {
8
8
  AggregateError = 1,
9
9
  /**
10
- * @deprecated Has no effect. Output always uses arrow functions.
10
+ * @deprecated Has no effect. Output follows the syntax of the build seroval
11
+ * runs in: arrow functions, or `function` expressions in an ES5 build.
11
12
  */
12
13
  ArrowFunction = 2,
13
14
  ErrorPrototypeStack = 4,
package/dist/dev/index.js CHANGED
@@ -7,7 +7,8 @@
7
7
  let Feature = /* @__PURE__ */ function(Feature) {
8
8
  Feature[Feature["AggregateError"] = 1] = "AggregateError";
9
9
  /**
10
- * @deprecated Has no effect. Output always uses arrow functions.
10
+ * @deprecated Has no effect. Output follows the syntax of the build seroval
11
+ * runs in: arrow functions, or `function` expressions in an ES5 build.
11
12
  */
12
13
  Feature[Feature["ArrowFunction"] = 2] = "ArrowFunction";
13
14
  Feature[Feature["ErrorPrototypeStack"] = 4] = "ErrorPrototypeStack";
@@ -165,7 +166,8 @@ function escapeCharCode(code) {
165
166
  default: return;
166
167
  }
167
168
  }
168
- function serializeString(str) {
169
+ function serializeString(input) {
170
+ const str = "" + input;
169
171
  if (!NEEDS_ESCAPE.test(str)) return str;
170
172
  if (str.length >= MIN_JSON_STRINGIFY_LENGTH && !JSON_ESCAPE_DIFFERENCES.test(str)) {
171
173
  let result = JSON.stringify(str).slice(1, -1);
@@ -1456,7 +1458,8 @@ async function parsePlainObject$1(ctx, depth, id, current, empty) {
1456
1458
  return createObjectNode(id, current, empty, await parseProperties$1(ctx, depth, current));
1457
1459
  }
1458
1460
  async function parseBoxed$1(ctx, depth, id, current) {
1459
- return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
1461
+ const node = parseValueAsync(ctx, depth, current.valueOf());
1462
+ return createBoxedNode(id, node instanceof Promise ? await node : node);
1460
1463
  }
1461
1464
  async function parseView$1(ctx, depth, type, id, current) {
1462
1465
  const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
@@ -1565,14 +1568,13 @@ async function parseAsyncIterable$1(ctx, depth, current) {
1565
1568
  const sequence = [];
1566
1569
  for (;;) {
1567
1570
  let result;
1571
+ markParserRef(ctx.base, id);
1568
1572
  try {
1569
1573
  result = await iterator.next();
1570
1574
  } catch (error) {
1571
- markParserRef(ctx.base, id);
1572
1575
  sequence.push(createStreamEventNode(33, id, await parseAsync(ctx, depth, error)));
1573
1576
  break;
1574
1577
  }
1575
- markParserRef(ctx.base, id);
1576
1578
  let parsed;
1577
1579
  try {
1578
1580
  parsed = await parseAsync(ctx, depth, result.value);
@@ -1700,6 +1702,8 @@ function applyObjectFlag(obj, flag) {
1700
1702
  }
1701
1703
  }
1702
1704
  const DEFAULT_DEPTH_LIMIT = 1e3;
1705
+ const MAX_REGEXP_PROPERTY_ESCAPES = 32;
1706
+ const REGEXP_PROPERTY_ESCAPE = /\\[pP]\{/g;
1703
1707
  function createBaseDeserializerContext(options) {
1704
1708
  const maxBase64Length = options.maxBase64Length ?? DEFAULT_MAX_BASE64_LENGTH;
1705
1709
  if (!Number.isSafeInteger(maxBase64Length) || maxBase64Length < 0) throw new RangeError("maxBase64Length must be a non-negative safe integer");
@@ -1710,7 +1714,8 @@ function createBaseDeserializerContext(options) {
1710
1714
  refs,
1711
1715
  features: options.features ?? 127 ^ (options.disabledFeatures ?? 0),
1712
1716
  depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT,
1713
- maxBase64Length
1717
+ maxBase64Length,
1718
+ regexpPropertyEscapes: MAX_REGEXP_PROPERTY_ESCAPES
1714
1719
  };
1715
1720
  }
1716
1721
  function createVanillaDeserializerContext(options) {
@@ -1822,6 +1827,13 @@ function deserializeRegExp(ctx, node) {
1822
1827
  if (ctx.base.features & 32) {
1823
1828
  const source = deserializeString(node.c);
1824
1829
  if (source.length > MAX_REGEXP_SOURCE_LENGTH) throw new SerovalMalformedNodeError(node);
1830
+ if (/[uv]/.test(String(node.m))) {
1831
+ const escapes = source.match(REGEXP_PROPERTY_ESCAPE);
1832
+ if (escapes) {
1833
+ ctx.base.regexpPropertyEscapes -= escapes.length;
1834
+ if (ctx.base.regexpPropertyEscapes < 0) throw new SerovalMalformedNodeError(node);
1835
+ }
1836
+ }
1825
1837
  return assignIndexedValue$1(ctx, node.i, new RegExp(source, node.m));
1826
1838
  }
1827
1839
  throw new SerovalUnsupportedNodeError(node);
@@ -2004,6 +2016,7 @@ function deserialize$1(ctx, depth, node) {
2004
2016
  }
2005
2017
  }
2006
2018
  function deserializeTop(ctx, node) {
2019
+ ctx.base.regexpPropertyEscapes = MAX_REGEXP_PROPERTY_ESCAPES;
2007
2020
  try {
2008
2021
  return deserialize$1(ctx, 0, node);
2009
2022
  } catch (error) {
@@ -2273,10 +2286,6 @@ function createObjectAssign(ctx, ref, key, value) {
2273
2286
  function createSequenceAssign(ctx, ref, index, value) {
2274
2287
  createAssignment(ctx.base, getRefParam(ctx, ref) + ".v[" + index + "]", value);
2275
2288
  }
2276
- /**
2277
- * Checks if the value is in the stack. Stack here is a reference
2278
- * structure to know if a object is to be accessed in a TDZ.
2279
- */
2280
2289
  function getKeyKind(key) {
2281
2290
  const first = key.charCodeAt(0);
2282
2291
  if (first >= 48 && first <= 57) {
@@ -2286,6 +2295,10 @@ function getKeyKind(key) {
2286
2295
  if (key === "Infinity") return 2;
2287
2296
  return isValidIdentifier(key) ? 1 : 0;
2288
2297
  }
2298
+ /**
2299
+ * Checks if the value is in the stack. Stack here is a reference
2300
+ * structure to know if a object is to be accessed in a TDZ.
2301
+ */
2289
2302
  function isIndexedValueInStack(ctx, node) {
2290
2303
  return node.t === 4 && ctx.stack.includes(node.i);
2291
2304
  }
@@ -2364,8 +2377,7 @@ function serializeProperty(ctx, source, key, val) {
2364
2377
  if (isIndexedValueInStack(ctx.base, val)) {
2365
2378
  const refParam = getRefParam(ctx, val.i);
2366
2379
  markSerializerRef(ctx.base, source.i);
2367
- if (kind === 1) createObjectAssign(ctx, source.i, key, refParam);
2368
- else createArrayAssign(ctx, source.i, kind === 2 ? key : "\"" + key + "\"", refParam);
2380
+ createKeyAssign(ctx, source.i, kind, key, refParam);
2369
2381
  return "";
2370
2382
  }
2371
2383
  if (key !== "__proto__") return (kind === 0 ? "\"" + key + "\"" : key) + ":" + serialize$1(ctx, val);
@@ -2768,19 +2780,17 @@ function serializeCross(ctx, tree) {
2768
2780
  if (id == null) return result;
2769
2781
  const patches = resolvePatches(ctx.base);
2770
2782
  const ref = getRefParam(ctx, id);
2771
- const scopeId = ctx.state.scopeId;
2772
- const params = scopeId == null ? "" : "$R";
2773
2783
  let body = result;
2774
2784
  if (patches) {
2775
2785
  if (!ctx.base.assignAll && !result.startsWith(ref + "=")) body = ref + "=" + result;
2776
2786
  body = "(" + body + "," + patches + ref + ")";
2777
2787
  }
2778
- if (params === "") {
2788
+ const scopeId = ctx.state.scopeId;
2789
+ if (scopeId == null) {
2779
2790
  if (tree.t === 10 && !patches) return "(" + body + ")";
2780
2791
  return body;
2781
2792
  }
2782
- const args = scopeId == null ? "()" : "($R[\"" + serializeString(scopeId) + "\"])";
2783
- return "(" + createFunction([params], body) + ")" + args;
2793
+ return "(" + createFunction(["$R"], body) + ")($R[\"" + serializeString(scopeId) + "\"])";
2784
2794
  }
2785
2795
  function wrapSerializationError(error) {
2786
2796
  return error instanceof SerovalSerializationError ? error : new SerovalSerializationError(error);
@@ -3142,7 +3152,7 @@ function consumeLiveStream(ctx, depth, id, current) {
3142
3152
  });
3143
3153
  }
3144
3154
  function wrapPromiseResult(ctx, type, id, node) {
3145
- return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx.base, type === 23 ? 2 : 3), node], void 0, void 0, void 0, void 0);
3155
+ return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx.base, type === 23 ? 2 : 3), node]);
3146
3156
  }
3147
3157
  function handlePromise(type, id, depth, data) {
3148
3158
  if (this.state.alive) parseEvent(this, depth, data, wrapPromiseResult.bind(null, this, type, id), void 0);
@@ -3204,6 +3214,7 @@ function createStreamParserState(options) {
3204
3214
  writing: false,
3205
3215
  inFlight: void 0,
3206
3216
  reason: void 0,
3217
+ starting: false,
3207
3218
  onParse: options.onParse,
3208
3219
  onError: options.onError,
3209
3220
  onDone: options.onDone,
@@ -3332,25 +3343,30 @@ function startStreamParse(ctx, current) {
3332
3343
  const state = ctx.state;
3333
3344
  let parsed;
3334
3345
  let failure;
3335
- state.parsing++;
3346
+ state.starting = true;
3336
3347
  try {
3337
- parsed = parseSOS(ctx, 0, current);
3338
- } catch (error) {
3339
- failure = { value: error };
3348
+ state.parsing++;
3349
+ try {
3350
+ parsed = parseSOS(ctx, 0, current);
3351
+ } catch (error) {
3352
+ failure = { value: error };
3353
+ } finally {
3354
+ state.parsing--;
3355
+ if (!state.alive) releaseParserValues(ctx);
3356
+ }
3357
+ if (failure) {
3358
+ if (state.alive) stopStreamParse(ctx, 1, failure.value);
3359
+ else throw failure.value;
3360
+ } else if (parsed && state.alive) {
3361
+ state.queue.unshift({
3362
+ node: parsed,
3363
+ initial: true,
3364
+ accept: void 0
3365
+ });
3366
+ drainOutput(ctx);
3367
+ }
3340
3368
  } finally {
3341
- state.parsing--;
3342
- if (!state.alive) releaseParserValues(ctx);
3343
- }
3344
- if (failure) {
3345
- if (state.alive) stopStreamParse(ctx, 1, failure.value);
3346
- else throw failure.value;
3347
- } else if (parsed && state.alive) {
3348
- state.queue.unshift({
3349
- node: parsed,
3350
- initial: true,
3351
- accept: void 0
3352
- });
3353
- drainOutput(ctx);
3369
+ state.starting = false;
3354
3370
  }
3355
3371
  }
3356
3372
  function runCleanups(state) {
@@ -3401,7 +3417,7 @@ function stopStreamParse(ctx, mode, reason) {
3401
3417
  try {
3402
3418
  if (mode === 1) {
3403
3419
  if (onError) onError(reason);
3404
- else throw reason;
3420
+ else if (state.starting) throw reason;
3405
3421
  } else if (mode === 0) onDone?.();
3406
3422
  } catch (error) {
3407
3423
  failure = { value: error };
@@ -3568,6 +3584,7 @@ var Serializer = class {
3568
3584
  if (this.alive && !this.flushed && options) {
3569
3585
  this.pending++;
3570
3586
  this.keys.add(key);
3587
+ let starting = true;
3571
3588
  const cleanup = crossSerializeStream(value, {
3572
3589
  plugins: this.plugins,
3573
3590
  scopeId: options.scopeId,
@@ -3579,7 +3596,7 @@ var Serializer = class {
3579
3596
  let failure;
3580
3597
  try {
3581
3598
  if (options.onError) options.onError(error);
3582
- else throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3599
+ else if (starting) throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3583
3600
  } catch (handlerError) {
3584
3601
  failure = { value: handlerError };
3585
3602
  }
@@ -3598,6 +3615,7 @@ var Serializer = class {
3598
3615
  this.finishWrite();
3599
3616
  }
3600
3617
  });
3618
+ starting = false;
3601
3619
  if (this.alive) this.cleanups.push(cleanup);
3602
3620
  else cleanup();
3603
3621
  }
package/dist/index.cjs CHANGED
@@ -8,7 +8,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
8
8
  let Feature = /* @__PURE__ */ function(Feature) {
9
9
  Feature[Feature["AggregateError"] = 1] = "AggregateError";
10
10
  /**
11
- * @deprecated Has no effect. Output always uses arrow functions.
11
+ * @deprecated Has no effect. Output follows the syntax of the build seroval
12
+ * runs in: arrow functions, or `function` expressions in an ES5 build.
12
13
  */
13
14
  Feature[Feature["ArrowFunction"] = 2] = "ArrowFunction";
14
15
  Feature[Feature["ErrorPrototypeStack"] = 4] = "ErrorPrototypeStack";
@@ -166,7 +167,8 @@ function escapeCharCode(code) {
166
167
  default: return;
167
168
  }
168
169
  }
169
- function serializeString(str) {
170
+ function serializeString(input) {
171
+ const str = "" + input;
170
172
  if (!NEEDS_ESCAPE.test(str)) return str;
171
173
  if (str.length >= MIN_JSON_STRINGIFY_LENGTH && !JSON_ESCAPE_DIFFERENCES.test(str)) {
172
174
  let result = JSON.stringify(str).slice(1, -1);
@@ -1441,7 +1443,8 @@ async function parsePlainObject$1(ctx, depth, id, current, empty) {
1441
1443
  return createObjectNode(id, current, empty, await parseProperties$1(ctx, depth, current));
1442
1444
  }
1443
1445
  async function parseBoxed$1(ctx, depth, id, current) {
1444
- return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
1446
+ const node = parseValueAsync(ctx, depth, current.valueOf());
1447
+ return createBoxedNode(id, node instanceof Promise ? await node : node);
1445
1448
  }
1446
1449
  async function parseView$1(ctx, depth, type, id, current) {
1447
1450
  const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
@@ -1550,14 +1553,13 @@ async function parseAsyncIterable$1(ctx, depth, current) {
1550
1553
  const sequence = [];
1551
1554
  for (;;) {
1552
1555
  let result;
1556
+ markParserRef(ctx.base, id);
1553
1557
  try {
1554
1558
  result = await iterator.next();
1555
1559
  } catch (error) {
1556
- markParserRef(ctx.base, id);
1557
1560
  sequence.push(createStreamEventNode(33, id, await parseAsync(ctx, depth, error)));
1558
1561
  break;
1559
1562
  }
1560
- markParserRef(ctx.base, id);
1561
1563
  let parsed;
1562
1564
  try {
1563
1565
  parsed = await parseAsync(ctx, depth, result.value);
@@ -1685,6 +1687,8 @@ function applyObjectFlag(obj, flag) {
1685
1687
  }
1686
1688
  }
1687
1689
  const DEFAULT_DEPTH_LIMIT = 1e3;
1690
+ const MAX_REGEXP_PROPERTY_ESCAPES = 32;
1691
+ const REGEXP_PROPERTY_ESCAPE = /\\[pP]\{/g;
1688
1692
  function createBaseDeserializerContext(options) {
1689
1693
  const maxBase64Length = options.maxBase64Length ?? DEFAULT_MAX_BASE64_LENGTH;
1690
1694
  if (!Number.isSafeInteger(maxBase64Length) || maxBase64Length < 0) throw new RangeError("maxBase64Length must be a non-negative safe integer");
@@ -1695,7 +1699,8 @@ function createBaseDeserializerContext(options) {
1695
1699
  refs,
1696
1700
  features: options.features ?? 127 ^ (options.disabledFeatures ?? 0),
1697
1701
  depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT,
1698
- maxBase64Length
1702
+ maxBase64Length,
1703
+ regexpPropertyEscapes: MAX_REGEXP_PROPERTY_ESCAPES
1699
1704
  };
1700
1705
  }
1701
1706
  function createVanillaDeserializerContext(options) {
@@ -1807,6 +1812,13 @@ function deserializeRegExp(ctx, node) {
1807
1812
  if (ctx.base.features & 32) {
1808
1813
  const source = deserializeString(node.c);
1809
1814
  if (source.length > MAX_REGEXP_SOURCE_LENGTH) throw new SerovalMalformedNodeError(node);
1815
+ if (/[uv]/.test(String(node.m))) {
1816
+ const escapes = source.match(REGEXP_PROPERTY_ESCAPE);
1817
+ if (escapes) {
1818
+ ctx.base.regexpPropertyEscapes -= escapes.length;
1819
+ if (ctx.base.regexpPropertyEscapes < 0) throw new SerovalMalformedNodeError(node);
1820
+ }
1821
+ }
1810
1822
  return assignIndexedValue$1(ctx, node.i, new RegExp(source, node.m));
1811
1823
  }
1812
1824
  throw new SerovalUnsupportedNodeError(node);
@@ -1989,6 +2001,7 @@ function deserialize$1(ctx, depth, node) {
1989
2001
  }
1990
2002
  }
1991
2003
  function deserializeTop(ctx, node) {
2004
+ ctx.base.regexpPropertyEscapes = MAX_REGEXP_PROPERTY_ESCAPES;
1992
2005
  try {
1993
2006
  return deserialize$1(ctx, 0, node);
1994
2007
  } catch (error) {
@@ -2258,10 +2271,6 @@ function createObjectAssign(ctx, ref, key, value) {
2258
2271
  function createSequenceAssign(ctx, ref, index, value) {
2259
2272
  createAssignment(ctx.base, getRefParam(ctx, ref) + ".v[" + index + "]", value);
2260
2273
  }
2261
- /**
2262
- * Checks if the value is in the stack. Stack here is a reference
2263
- * structure to know if a object is to be accessed in a TDZ.
2264
- */
2265
2274
  function getKeyKind(key) {
2266
2275
  const first = key.charCodeAt(0);
2267
2276
  if (first >= 48 && first <= 57) {
@@ -2271,6 +2280,10 @@ function getKeyKind(key) {
2271
2280
  if (key === "Infinity") return 2;
2272
2281
  return isValidIdentifier(key) ? 1 : 0;
2273
2282
  }
2283
+ /**
2284
+ * Checks if the value is in the stack. Stack here is a reference
2285
+ * structure to know if a object is to be accessed in a TDZ.
2286
+ */
2274
2287
  function isIndexedValueInStack(ctx, node) {
2275
2288
  return node.t === 4 && ctx.stack.includes(node.i);
2276
2289
  }
@@ -2349,8 +2362,7 @@ function serializeProperty(ctx, source, key, val) {
2349
2362
  if (isIndexedValueInStack(ctx.base, val)) {
2350
2363
  const refParam = getRefParam(ctx, val.i);
2351
2364
  markSerializerRef(ctx.base, source.i);
2352
- if (kind === 1) createObjectAssign(ctx, source.i, key, refParam);
2353
- else createArrayAssign(ctx, source.i, kind === 2 ? key : "\"" + key + "\"", refParam);
2365
+ createKeyAssign(ctx, source.i, kind, key, refParam);
2354
2366
  return "";
2355
2367
  }
2356
2368
  if (key !== "__proto__") return (kind === 0 ? "\"" + key + "\"" : key) + ":" + serialize$1(ctx, val);
@@ -2753,19 +2765,17 @@ function serializeCross(ctx, tree) {
2753
2765
  if (id == null) return result;
2754
2766
  const patches = resolvePatches(ctx.base);
2755
2767
  const ref = getRefParam(ctx, id);
2756
- const scopeId = ctx.state.scopeId;
2757
- const params = scopeId == null ? "" : "$R";
2758
2768
  let body = result;
2759
2769
  if (patches) {
2760
2770
  if (!ctx.base.assignAll && !result.startsWith(ref + "=")) body = ref + "=" + result;
2761
2771
  body = "(" + body + "," + patches + ref + ")";
2762
2772
  }
2763
- if (params === "") {
2773
+ const scopeId = ctx.state.scopeId;
2774
+ if (scopeId == null) {
2764
2775
  if (tree.t === 10 && !patches) return "(" + body + ")";
2765
2776
  return body;
2766
2777
  }
2767
- const args = scopeId == null ? "()" : "($R[\"" + serializeString(scopeId) + "\"])";
2768
- return "(" + createFunction([params], body) + ")" + args;
2778
+ return "(" + createFunction(["$R"], body) + ")($R[\"" + serializeString(scopeId) + "\"])";
2769
2779
  }
2770
2780
  function wrapSerializationError(error) {
2771
2781
  return error instanceof SerovalSerializationError ? error : new SerovalSerializationError(error);
@@ -3127,7 +3137,7 @@ function consumeLiveStream(ctx, depth, id, current) {
3127
3137
  });
3128
3138
  }
3129
3139
  function wrapPromiseResult(ctx, type, id, node) {
3130
- return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx.base, type === 23 ? 2 : 3), node], void 0, void 0, void 0, void 0);
3140
+ return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx.base, type === 23 ? 2 : 3), node]);
3131
3141
  }
3132
3142
  function handlePromise(type, id, depth, data) {
3133
3143
  if (this.state.alive) parseEvent(this, depth, data, wrapPromiseResult.bind(null, this, type, id), void 0);
@@ -3189,6 +3199,7 @@ function createStreamParserState(options) {
3189
3199
  writing: false,
3190
3200
  inFlight: void 0,
3191
3201
  reason: void 0,
3202
+ starting: false,
3192
3203
  onParse: options.onParse,
3193
3204
  onError: options.onError,
3194
3205
  onDone: options.onDone,
@@ -3317,25 +3328,30 @@ function startStreamParse(ctx, current) {
3317
3328
  const state = ctx.state;
3318
3329
  let parsed;
3319
3330
  let failure;
3320
- state.parsing++;
3331
+ state.starting = true;
3321
3332
  try {
3322
- parsed = parseSOS(ctx, 0, current);
3323
- } catch (error) {
3324
- failure = { value: error };
3333
+ state.parsing++;
3334
+ try {
3335
+ parsed = parseSOS(ctx, 0, current);
3336
+ } catch (error) {
3337
+ failure = { value: error };
3338
+ } finally {
3339
+ state.parsing--;
3340
+ if (!state.alive) releaseParserValues(ctx);
3341
+ }
3342
+ if (failure) {
3343
+ if (state.alive) stopStreamParse(ctx, 1, failure.value);
3344
+ else throw failure.value;
3345
+ } else if (parsed && state.alive) {
3346
+ state.queue.unshift({
3347
+ node: parsed,
3348
+ initial: true,
3349
+ accept: void 0
3350
+ });
3351
+ drainOutput(ctx);
3352
+ }
3325
3353
  } finally {
3326
- state.parsing--;
3327
- if (!state.alive) releaseParserValues(ctx);
3328
- }
3329
- if (failure) {
3330
- if (state.alive) stopStreamParse(ctx, 1, failure.value);
3331
- else throw failure.value;
3332
- } else if (parsed && state.alive) {
3333
- state.queue.unshift({
3334
- node: parsed,
3335
- initial: true,
3336
- accept: void 0
3337
- });
3338
- drainOutput(ctx);
3354
+ state.starting = false;
3339
3355
  }
3340
3356
  }
3341
3357
  function runCleanups(state) {
@@ -3386,7 +3402,7 @@ function stopStreamParse(ctx, mode, reason) {
3386
3402
  try {
3387
3403
  if (mode === 1) {
3388
3404
  if (onError) onError(reason);
3389
- else throw reason;
3405
+ else if (state.starting) throw reason;
3390
3406
  } else if (mode === 0) onDone?.();
3391
3407
  } catch (error) {
3392
3408
  failure = { value: error };
@@ -3553,6 +3569,7 @@ var Serializer = class {
3553
3569
  if (this.alive && !this.flushed && options) {
3554
3570
  this.pending++;
3555
3571
  this.keys.add(key);
3572
+ let starting = true;
3556
3573
  const cleanup = crossSerializeStream(value, {
3557
3574
  plugins: this.plugins,
3558
3575
  scopeId: options.scopeId,
@@ -3564,7 +3581,7 @@ var Serializer = class {
3564
3581
  let failure;
3565
3582
  try {
3566
3583
  if (options.onError) options.onError(error);
3567
- else throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3584
+ else if (starting) throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3568
3585
  } catch (handlerError) {
3569
3586
  failure = { value: handlerError };
3570
3587
  }
@@ -3583,6 +3600,7 @@ var Serializer = class {
3583
3600
  this.finishWrite();
3584
3601
  }
3585
3602
  });
3603
+ starting = false;
3586
3604
  if (this.alive) this.cleanups.push(cleanup);
3587
3605
  else cleanup();
3588
3606
  }
package/dist/index.d.cts CHANGED
@@ -7,7 +7,8 @@
7
7
  export declare enum Feature {
8
8
  AggregateError = 1,
9
9
  /**
10
- * @deprecated Has no effect. Output always uses arrow functions.
10
+ * @deprecated Has no effect. Output follows the syntax of the build seroval
11
+ * runs in: arrow functions, or `function` expressions in an ES5 build.
11
12
  */
12
13
  ArrowFunction = 2,
13
14
  ErrorPrototypeStack = 4,
package/dist/index.d.ts CHANGED
@@ -7,7 +7,8 @@
7
7
  export declare enum Feature {
8
8
  AggregateError = 1,
9
9
  /**
10
- * @deprecated Has no effect. Output always uses arrow functions.
10
+ * @deprecated Has no effect. Output follows the syntax of the build seroval
11
+ * runs in: arrow functions, or `function` expressions in an ES5 build.
11
12
  */
12
13
  ArrowFunction = 2,
13
14
  ErrorPrototypeStack = 4,
package/dist/index.js CHANGED
@@ -7,7 +7,8 @@
7
7
  let Feature = /* @__PURE__ */ function(Feature) {
8
8
  Feature[Feature["AggregateError"] = 1] = "AggregateError";
9
9
  /**
10
- * @deprecated Has no effect. Output always uses arrow functions.
10
+ * @deprecated Has no effect. Output follows the syntax of the build seroval
11
+ * runs in: arrow functions, or `function` expressions in an ES5 build.
11
12
  */
12
13
  Feature[Feature["ArrowFunction"] = 2] = "ArrowFunction";
13
14
  Feature[Feature["ErrorPrototypeStack"] = 4] = "ErrorPrototypeStack";
@@ -165,7 +166,8 @@ function escapeCharCode(code) {
165
166
  default: return;
166
167
  }
167
168
  }
168
- function serializeString(str) {
169
+ function serializeString(input) {
170
+ const str = "" + input;
169
171
  if (!NEEDS_ESCAPE.test(str)) return str;
170
172
  if (str.length >= MIN_JSON_STRINGIFY_LENGTH && !JSON_ESCAPE_DIFFERENCES.test(str)) {
171
173
  let result = JSON.stringify(str).slice(1, -1);
@@ -1440,7 +1442,8 @@ async function parsePlainObject$1(ctx, depth, id, current, empty) {
1440
1442
  return createObjectNode(id, current, empty, await parseProperties$1(ctx, depth, current));
1441
1443
  }
1442
1444
  async function parseBoxed$1(ctx, depth, id, current) {
1443
- return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
1445
+ const node = parseValueAsync(ctx, depth, current.valueOf());
1446
+ return createBoxedNode(id, node instanceof Promise ? await node : node);
1444
1447
  }
1445
1448
  async function parseView$1(ctx, depth, type, id, current) {
1446
1449
  const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
@@ -1549,14 +1552,13 @@ async function parseAsyncIterable$1(ctx, depth, current) {
1549
1552
  const sequence = [];
1550
1553
  for (;;) {
1551
1554
  let result;
1555
+ markParserRef(ctx.base, id);
1552
1556
  try {
1553
1557
  result = await iterator.next();
1554
1558
  } catch (error) {
1555
- markParserRef(ctx.base, id);
1556
1559
  sequence.push(createStreamEventNode(33, id, await parseAsync(ctx, depth, error)));
1557
1560
  break;
1558
1561
  }
1559
- markParserRef(ctx.base, id);
1560
1562
  let parsed;
1561
1563
  try {
1562
1564
  parsed = await parseAsync(ctx, depth, result.value);
@@ -1684,6 +1686,8 @@ function applyObjectFlag(obj, flag) {
1684
1686
  }
1685
1687
  }
1686
1688
  const DEFAULT_DEPTH_LIMIT = 1e3;
1689
+ const MAX_REGEXP_PROPERTY_ESCAPES = 32;
1690
+ const REGEXP_PROPERTY_ESCAPE = /\\[pP]\{/g;
1687
1691
  function createBaseDeserializerContext(options) {
1688
1692
  const maxBase64Length = options.maxBase64Length ?? DEFAULT_MAX_BASE64_LENGTH;
1689
1693
  if (!Number.isSafeInteger(maxBase64Length) || maxBase64Length < 0) throw new RangeError("maxBase64Length must be a non-negative safe integer");
@@ -1694,7 +1698,8 @@ function createBaseDeserializerContext(options) {
1694
1698
  refs,
1695
1699
  features: options.features ?? 127 ^ (options.disabledFeatures ?? 0),
1696
1700
  depthLimit: options.depthLimit || DEFAULT_DEPTH_LIMIT,
1697
- maxBase64Length
1701
+ maxBase64Length,
1702
+ regexpPropertyEscapes: MAX_REGEXP_PROPERTY_ESCAPES
1698
1703
  };
1699
1704
  }
1700
1705
  function createVanillaDeserializerContext(options) {
@@ -1806,6 +1811,13 @@ function deserializeRegExp(ctx, node) {
1806
1811
  if (ctx.base.features & 32) {
1807
1812
  const source = deserializeString(node.c);
1808
1813
  if (source.length > MAX_REGEXP_SOURCE_LENGTH) throw new SerovalMalformedNodeError(node);
1814
+ if (/[uv]/.test(String(node.m))) {
1815
+ const escapes = source.match(REGEXP_PROPERTY_ESCAPE);
1816
+ if (escapes) {
1817
+ ctx.base.regexpPropertyEscapes -= escapes.length;
1818
+ if (ctx.base.regexpPropertyEscapes < 0) throw new SerovalMalformedNodeError(node);
1819
+ }
1820
+ }
1809
1821
  return assignIndexedValue$1(ctx, node.i, new RegExp(source, node.m));
1810
1822
  }
1811
1823
  throw new SerovalUnsupportedNodeError(node);
@@ -1988,6 +2000,7 @@ function deserialize$1(ctx, depth, node) {
1988
2000
  }
1989
2001
  }
1990
2002
  function deserializeTop(ctx, node) {
2003
+ ctx.base.regexpPropertyEscapes = MAX_REGEXP_PROPERTY_ESCAPES;
1991
2004
  try {
1992
2005
  return deserialize$1(ctx, 0, node);
1993
2006
  } catch (error) {
@@ -2257,10 +2270,6 @@ function createObjectAssign(ctx, ref, key, value) {
2257
2270
  function createSequenceAssign(ctx, ref, index, value) {
2258
2271
  createAssignment(ctx.base, getRefParam(ctx, ref) + ".v[" + index + "]", value);
2259
2272
  }
2260
- /**
2261
- * Checks if the value is in the stack. Stack here is a reference
2262
- * structure to know if a object is to be accessed in a TDZ.
2263
- */
2264
2273
  function getKeyKind(key) {
2265
2274
  const first = key.charCodeAt(0);
2266
2275
  if (first >= 48 && first <= 57) {
@@ -2270,6 +2279,10 @@ function getKeyKind(key) {
2270
2279
  if (key === "Infinity") return 2;
2271
2280
  return isValidIdentifier(key) ? 1 : 0;
2272
2281
  }
2282
+ /**
2283
+ * Checks if the value is in the stack. Stack here is a reference
2284
+ * structure to know if a object is to be accessed in a TDZ.
2285
+ */
2273
2286
  function isIndexedValueInStack(ctx, node) {
2274
2287
  return node.t === 4 && ctx.stack.includes(node.i);
2275
2288
  }
@@ -2348,8 +2361,7 @@ function serializeProperty(ctx, source, key, val) {
2348
2361
  if (isIndexedValueInStack(ctx.base, val)) {
2349
2362
  const refParam = getRefParam(ctx, val.i);
2350
2363
  markSerializerRef(ctx.base, source.i);
2351
- if (kind === 1) createObjectAssign(ctx, source.i, key, refParam);
2352
- else createArrayAssign(ctx, source.i, kind === 2 ? key : "\"" + key + "\"", refParam);
2364
+ createKeyAssign(ctx, source.i, kind, key, refParam);
2353
2365
  return "";
2354
2366
  }
2355
2367
  if (key !== "__proto__") return (kind === 0 ? "\"" + key + "\"" : key) + ":" + serialize$1(ctx, val);
@@ -2752,19 +2764,17 @@ function serializeCross(ctx, tree) {
2752
2764
  if (id == null) return result;
2753
2765
  const patches = resolvePatches(ctx.base);
2754
2766
  const ref = getRefParam(ctx, id);
2755
- const scopeId = ctx.state.scopeId;
2756
- const params = scopeId == null ? "" : "$R";
2757
2767
  let body = result;
2758
2768
  if (patches) {
2759
2769
  if (!ctx.base.assignAll && !result.startsWith(ref + "=")) body = ref + "=" + result;
2760
2770
  body = "(" + body + "," + patches + ref + ")";
2761
2771
  }
2762
- if (params === "") {
2772
+ const scopeId = ctx.state.scopeId;
2773
+ if (scopeId == null) {
2763
2774
  if (tree.t === 10 && !patches) return "(" + body + ")";
2764
2775
  return body;
2765
2776
  }
2766
- const args = scopeId == null ? "()" : "($R[\"" + serializeString(scopeId) + "\"])";
2767
- return "(" + createFunction([params], body) + ")" + args;
2777
+ return "(" + createFunction(["$R"], body) + ")($R[\"" + serializeString(scopeId) + "\"])";
2768
2778
  }
2769
2779
  function wrapSerializationError(error) {
2770
2780
  return error instanceof SerovalSerializationError ? error : new SerovalSerializationError(error);
@@ -3126,7 +3136,7 @@ function consumeLiveStream(ctx, depth, id, current) {
3126
3136
  });
3127
3137
  }
3128
3138
  function wrapPromiseResult(ctx, type, id, node) {
3129
- return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx.base, type === 23 ? 2 : 3), node], void 0, void 0, void 0, void 0);
3139
+ return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx.base, type === 23 ? 2 : 3), node]);
3130
3140
  }
3131
3141
  function handlePromise(type, id, depth, data) {
3132
3142
  if (this.state.alive) parseEvent(this, depth, data, wrapPromiseResult.bind(null, this, type, id), void 0);
@@ -3188,6 +3198,7 @@ function createStreamParserState(options) {
3188
3198
  writing: false,
3189
3199
  inFlight: void 0,
3190
3200
  reason: void 0,
3201
+ starting: false,
3191
3202
  onParse: options.onParse,
3192
3203
  onError: options.onError,
3193
3204
  onDone: options.onDone,
@@ -3316,25 +3327,30 @@ function startStreamParse(ctx, current) {
3316
3327
  const state = ctx.state;
3317
3328
  let parsed;
3318
3329
  let failure;
3319
- state.parsing++;
3330
+ state.starting = true;
3320
3331
  try {
3321
- parsed = parseSOS(ctx, 0, current);
3322
- } catch (error) {
3323
- failure = { value: error };
3332
+ state.parsing++;
3333
+ try {
3334
+ parsed = parseSOS(ctx, 0, current);
3335
+ } catch (error) {
3336
+ failure = { value: error };
3337
+ } finally {
3338
+ state.parsing--;
3339
+ if (!state.alive) releaseParserValues(ctx);
3340
+ }
3341
+ if (failure) {
3342
+ if (state.alive) stopStreamParse(ctx, 1, failure.value);
3343
+ else throw failure.value;
3344
+ } else if (parsed && state.alive) {
3345
+ state.queue.unshift({
3346
+ node: parsed,
3347
+ initial: true,
3348
+ accept: void 0
3349
+ });
3350
+ drainOutput(ctx);
3351
+ }
3324
3352
  } finally {
3325
- state.parsing--;
3326
- if (!state.alive) releaseParserValues(ctx);
3327
- }
3328
- if (failure) {
3329
- if (state.alive) stopStreamParse(ctx, 1, failure.value);
3330
- else throw failure.value;
3331
- } else if (parsed && state.alive) {
3332
- state.queue.unshift({
3333
- node: parsed,
3334
- initial: true,
3335
- accept: void 0
3336
- });
3337
- drainOutput(ctx);
3353
+ state.starting = false;
3338
3354
  }
3339
3355
  }
3340
3356
  function runCleanups(state) {
@@ -3385,7 +3401,7 @@ function stopStreamParse(ctx, mode, reason) {
3385
3401
  try {
3386
3402
  if (mode === 1) {
3387
3403
  if (onError) onError(reason);
3388
- else throw reason;
3404
+ else if (state.starting) throw reason;
3389
3405
  } else if (mode === 0) onDone?.();
3390
3406
  } catch (error) {
3391
3407
  failure = { value: error };
@@ -3552,6 +3568,7 @@ var Serializer = class {
3552
3568
  if (this.alive && !this.flushed && options) {
3553
3569
  this.pending++;
3554
3570
  this.keys.add(key);
3571
+ let starting = true;
3555
3572
  const cleanup = crossSerializeStream(value, {
3556
3573
  plugins: this.plugins,
3557
3574
  scopeId: options.scopeId,
@@ -3563,7 +3580,7 @@ var Serializer = class {
3563
3580
  let failure;
3564
3581
  try {
3565
3582
  if (options.onError) options.onError(error);
3566
- else throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3583
+ else if (starting) throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3567
3584
  } catch (handlerError) {
3568
3585
  failure = { value: handlerError };
3569
3586
  }
@@ -3582,6 +3599,7 @@ var Serializer = class {
3582
3599
  this.finishWrite();
3583
3600
  }
3584
3601
  });
3602
+ starting = false;
3585
3603
  if (this.alive) this.cleanups.push(cleanup);
3586
3604
  else cleanup();
3587
3605
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "seroval",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "1.7.7",
5
+ "version": "1.7.9",
6
6
  "files": [
7
7
  "dist"
8
8
  ],