seroval 1.7.6 → 1.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/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);
@@ -389,6 +391,30 @@ function getTypedArrayName(value) {
389
391
  if (!typedArrayNameGetter) typedArrayNameGetter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Int8Array.prototype), Symbol.toStringTag).get;
390
392
  return typedArrayNameGetter.call(value);
391
393
  }
394
+ let typedArrayGetters;
395
+ let dataViewGetters;
396
+ function getAccessor(proto, key) {
397
+ return Object.getOwnPropertyDescriptor(proto, key).get;
398
+ }
399
+ function getViewGetters(proto, lengthKey) {
400
+ return {
401
+ buffer: getAccessor(proto, "buffer"),
402
+ byteOffset: getAccessor(proto, "byteOffset"),
403
+ byteLength: getAccessor(proto, "byteLength"),
404
+ length: getAccessor(proto, lengthKey)
405
+ };
406
+ }
407
+ function readArrayBufferView(value, isDataView) {
408
+ let getters;
409
+ if (isDataView) getters = dataViewGetters ?? (dataViewGetters = getViewGetters(DataView.prototype, "byteLength"));
410
+ else getters = typedArrayGetters ?? (typedArrayGetters = getViewGetters(Object.getPrototypeOf(Int8Array.prototype), "length"));
411
+ return {
412
+ buffer: getters.buffer.call(value),
413
+ byteOffset: getters.byteOffset.call(value),
414
+ byteLength: getters.byteLength.call(value),
415
+ length: getters.length.call(value)
416
+ };
417
+ }
392
418
  //#endregion
393
419
  //#region src/core/base-primitives.ts
394
420
  function createNumberNode(value) {
@@ -409,12 +435,14 @@ function createBigIntNode(current) {
409
435
  function createIndexedValueNode(id) {
410
436
  return createSerovalNode(4, id);
411
437
  }
438
+ const getDateTime = Date.prototype.getTime;
439
+ const getDateISOString = Date.prototype.toISOString;
412
440
  function createDateNode(id, current) {
413
- const timestamp = current.valueOf();
414
- return createSerovalNode(5, id, timestamp === timestamp ? current.toISOString() : "");
441
+ const timestamp = getDateTime.call(current);
442
+ return createSerovalNode(5, id, timestamp === timestamp ? getDateISOString.call(current) : "");
415
443
  }
416
- function createTemporalNode(id, type, current) {
417
- return createSerovalNode(36, id, current.toString(), type);
444
+ function createTemporalNode(id, type, temporalClass, current) {
445
+ return createSerovalNode(36, id, temporalClass.prototype.toString.call(current), type);
418
446
  }
419
447
  const getRegExpSource = Object.getOwnPropertyDescriptor(RegExp.prototype, "source").get;
420
448
  const getRegExpFlags = Object.getOwnPropertyDescriptor(RegExp.prototype, "flags").get;
@@ -437,11 +465,11 @@ function createBoxedNode(id, boxed) {
437
465
  return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed);
438
466
  }
439
467
  const MAX_TYPED_ARRAY_LENGTH = 1e6;
440
- function createViewNode(type, id, current, buffer) {
468
+ function createViewNode(type, id, current, view, buffer) {
441
469
  const isDataView = type === 20;
442
- const size = isDataView ? current.byteLength : current.length;
470
+ const size = view.length;
443
471
  if (size > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
444
- return createSerovalNode(type, id, void 0, isDataView ? void 0 : getTypedArrayName(current), void 0, void 0, void 0, void 0, buffer, current.byteOffset, void 0, size);
472
+ return createSerovalNode(type, id, void 0, isDataView ? void 0 : getTypedArrayName(current), void 0, void 0, void 0, void 0, buffer, view.byteOffset, void 0, size);
445
473
  }
446
474
  function createErrorNode(type, id, current, options) {
447
475
  return createSerovalNode(type, id, getErrorConstructor(current), void 0, serializeString(current.message), options);
@@ -463,8 +491,9 @@ function createSequenceNode(id, sequence, throwAt, doneAt) {
463
491
  }
464
492
  //#endregion
465
493
  //#region src/core/live-stream.ts
494
+ const BRAND = /* @__PURE__ */ Symbol("LiveStream");
466
495
  function isLiveStream(value) {
467
- return value instanceof LiveStream;
496
+ return value != null && value[BRAND] === true;
468
497
  }
469
498
  /**
470
499
  * A single-consumer stream for incremental serialization. Unlike
@@ -472,11 +501,12 @@ function isLiveStream(value) {
472
501
  * producer operation that sent it settles only when the consumer accepts it.
473
502
  *
474
503
  * An internal class rather than a tagged POJO: identity is checked with
475
- * `instanceof`, which untrusted input cannot forge. The class is exported
504
+ * `isLiveStream`, which untrusted input cannot forge. The class is exported
476
505
  * because bundlers cannot drop an unexported top-level class.
477
506
  */
478
507
  var LiveStream = class {
479
508
  constructor(options) {
509
+ this[BRAND] = true;
480
510
  this.consumed = false;
481
511
  this.closed = false;
482
512
  this.cancelled = false;
@@ -1278,12 +1308,12 @@ function getTemporalType(currentClass) {
1278
1308
  }
1279
1309
  }
1280
1310
  function getObjectClass(current) {
1281
- const currentClass = current.constructor;
1282
- if (typeof currentClass !== "function") {
1283
- const proto = Object.getPrototypeOf(current);
1284
- return proto === null ? void 0 : proto.constructor;
1285
- }
1286
- return currentClass;
1311
+ const proto = Object.getPrototypeOf(current);
1312
+ if (proto === null) return void 0;
1313
+ if (proto === Object.prototype) return Object;
1314
+ const currentClass = proto.constructor;
1315
+ if (typeof currentClass !== "function" || currentClass === Object || currentClass.prototype === proto) return currentClass;
1316
+ return proto;
1287
1317
  }
1288
1318
  /**
1289
1319
  * Decides how an object is parsed. Shared by the sync, stream and async
@@ -1347,10 +1377,14 @@ function createMapNode(id, sentinel, k, v) {
1347
1377
  function createPromiseConstructorNode(ctx, id, resolver) {
1348
1378
  return createSerovalNode(22, id, resolver, void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 1));
1349
1379
  }
1350
- function getArrayBufferView(ctx, current) {
1351
- if (!ctx.compactArrayBufferViews) return current;
1352
- const buffer = new Uint8Array(current.buffer, current.byteOffset, current.byteLength).slice().buffer;
1353
- return new (current instanceof DataView ? DataView : getTypedArrayConstructor(getTypedArrayName(current)))(buffer);
1380
+ function getArrayBufferView(ctx, view) {
1381
+ if (!ctx.compactArrayBufferViews) return view;
1382
+ return {
1383
+ buffer: new Uint8Array(view.buffer, view.byteOffset, view.byteLength).slice().buffer,
1384
+ byteOffset: 0,
1385
+ byteLength: view.byteLength,
1386
+ length: view.length
1387
+ };
1354
1388
  }
1355
1389
  function createArrayBufferNode(ctx, id, current) {
1356
1390
  return createSerovalNode(19, id, encodeArrayBuffer(current), void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 5));
@@ -1408,11 +1442,12 @@ async function parsePlainObject$1(ctx, depth, id, current, empty) {
1408
1442
  return createObjectNode(id, current, empty, await parseProperties$1(ctx, depth, current));
1409
1443
  }
1410
1444
  async function parseBoxed$1(ctx, depth, id, current) {
1411
- 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);
1412
1447
  }
1413
1448
  async function parseView$1(ctx, depth, type, id, current) {
1414
- current = getArrayBufferView(ctx.base, current);
1415
- return createViewNode(type, id, current, await parseAsync(ctx, depth, current.buffer));
1449
+ const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
1450
+ return createViewNode(type, id, current, view, await parseAsync(ctx, depth, view.buffer));
1416
1451
  }
1417
1452
  async function parseError$1(ctx, depth, type, id, current) {
1418
1453
  const options = getErrorOptions(current, ctx.base.features);
@@ -1517,14 +1552,13 @@ async function parseAsyncIterable$1(ctx, depth, current) {
1517
1552
  const sequence = [];
1518
1553
  for (;;) {
1519
1554
  let result;
1555
+ markParserRef(ctx.base, id);
1520
1556
  try {
1521
1557
  result = await iterator.next();
1522
1558
  } catch (error) {
1523
- markParserRef(ctx.base, id);
1524
1559
  sequence.push(createStreamEventNode(33, id, await parseAsync(ctx, depth, error)));
1525
1560
  break;
1526
1561
  }
1527
- markParserRef(ctx.base, id);
1528
1562
  let parsed;
1529
1563
  try {
1530
1564
  parsed = await parseAsync(ctx, depth, result.value);
@@ -1569,7 +1603,7 @@ async function parseObjectAsync(ctx, depth, id, current) {
1569
1603
  case 12: return parseSet$1(ctx, depth, id, current);
1570
1604
  case 13: return parsePromise$1(ctx, depth, id, current);
1571
1605
  case 14: return createRegExpNode(id, current);
1572
- case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
1606
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), currentClass, current);
1573
1607
  case 16: return parsePlainObject$1(ctx, depth, id, current, !!currentClass);
1574
1608
  default: throw new SerovalUnsupportedTypeError(current);
1575
1609
  }
@@ -2225,10 +2259,6 @@ function createObjectAssign(ctx, ref, key, value) {
2225
2259
  function createSequenceAssign(ctx, ref, index, value) {
2226
2260
  createAssignment(ctx.base, getRefParam(ctx, ref) + ".v[" + index + "]", value);
2227
2261
  }
2228
- /**
2229
- * Checks if the value is in the stack. Stack here is a reference
2230
- * structure to know if a object is to be accessed in a TDZ.
2231
- */
2232
2262
  function getKeyKind(key) {
2233
2263
  const first = key.charCodeAt(0);
2234
2264
  if (first >= 48 && first <= 57) {
@@ -2238,6 +2268,10 @@ function getKeyKind(key) {
2238
2268
  if (key === "Infinity") return 2;
2239
2269
  return isValidIdentifier(key) ? 1 : 0;
2240
2270
  }
2271
+ /**
2272
+ * Checks if the value is in the stack. Stack here is a reference
2273
+ * structure to know if a object is to be accessed in a TDZ.
2274
+ */
2241
2275
  function isIndexedValueInStack(ctx, node) {
2242
2276
  return node.t === 4 && ctx.stack.includes(node.i);
2243
2277
  }
@@ -2316,8 +2350,7 @@ function serializeProperty(ctx, source, key, val) {
2316
2350
  if (isIndexedValueInStack(ctx.base, val)) {
2317
2351
  const refParam = getRefParam(ctx, val.i);
2318
2352
  markSerializerRef(ctx.base, source.i);
2319
- if (kind === 1) createObjectAssign(ctx, source.i, key, refParam);
2320
- else createArrayAssign(ctx, source.i, kind === 2 ? key : "\"" + key + "\"", refParam);
2353
+ createKeyAssign(ctx, source.i, kind, key, refParam);
2321
2354
  return "";
2322
2355
  }
2323
2356
  if (key !== "__proto__") return (kind === 0 ? "\"" + key + "\"" : key) + ":" + serialize$1(ctx, val);
@@ -2405,10 +2438,10 @@ function serializeNullConstructor(ctx, node) {
2405
2438
  return serializeDictionary(ctx, node, NULL_CONSTRUCTOR);
2406
2439
  }
2407
2440
  function serializeDate(node) {
2408
- return "new Date(\"" + node.s + "\")";
2441
+ return "new Date(\"" + serializeString(node.s) + "\")";
2409
2442
  }
2410
2443
  function serializeTemporal(ctx, node) {
2411
- if (ctx.base.features & 64) return "Temporal." + TEMPORAL_TYPE_NAME[node.c] + ".from(\"" + node.s + "\")";
2444
+ if (ctx.base.features & 64) return "Temporal." + TEMPORAL_TYPE_NAME[node.c] + ".from(\"" + serializeString(node.s) + "\")";
2412
2445
  throw new SerovalUnsupportedNodeError(node);
2413
2446
  }
2414
2447
  function serializeRegExp(ctx, node) {
@@ -2522,7 +2555,7 @@ function serializeArrayBuffer(ctx, node) {
2522
2555
  return getConstructor(ctx, node.f) + "(\"" + node.s + "\")";
2523
2556
  }
2524
2557
  function serializeView(ctx, node) {
2525
- return "new " + (node.t === 20 ? "DataView" : node.c) + "(" + serialize$1(ctx, node.f) + "," + node.b + "," + node.l + ")";
2558
+ return "new " + (node.t === 20 ? "DataView" : node.c) + "(" + serialize$1(ctx, node.f) + "," + +node.b + "," + +node.l + ")";
2526
2559
  }
2527
2560
  function serializeAggregateError(ctx, node) {
2528
2561
  const id = node.i;
@@ -2641,7 +2674,7 @@ function serializeSequence(ctx, node) {
2641
2674
  result += (item && result && ",") + item;
2642
2675
  }
2643
2676
  ctx.base.stack.pop();
2644
- if (result) return "{__SEROVAL_SEQUENCE__:!0,v:[" + result + "],t:" + node.s + ",d:" + node.l + "}";
2677
+ if (result) return "{__SEROVAL_SEQUENCE__:!0,v:[" + result + "],t:" + +node.s + ",d:" + +node.l + "}";
2645
2678
  }
2646
2679
  return "{__SEROVAL_SEQUENCE__:!0,v:[],t:-1,d:0}";
2647
2680
  }
@@ -2720,19 +2753,17 @@ function serializeCross(ctx, tree) {
2720
2753
  if (id == null) return result;
2721
2754
  const patches = resolvePatches(ctx.base);
2722
2755
  const ref = getRefParam(ctx, id);
2723
- const scopeId = ctx.state.scopeId;
2724
- const params = scopeId == null ? "" : "$R";
2725
2756
  let body = result;
2726
2757
  if (patches) {
2727
2758
  if (!ctx.base.assignAll && !result.startsWith(ref + "=")) body = ref + "=" + result;
2728
2759
  body = "(" + body + "," + patches + ref + ")";
2729
2760
  }
2730
- if (params === "") {
2761
+ const scopeId = ctx.state.scopeId;
2762
+ if (scopeId == null) {
2731
2763
  if (tree.t === 10 && !patches) return "(" + body + ")";
2732
2764
  return body;
2733
2765
  }
2734
- const args = scopeId == null ? "()" : "($R[\"" + serializeString(scopeId) + "\"])";
2735
- return "(" + createFunction([params], body) + ")" + args;
2766
+ return "(" + createFunction(["$R"], body) + ")($R[\"" + serializeString(scopeId) + "\"])";
2736
2767
  }
2737
2768
  function wrapSerializationError(error) {
2738
2769
  return error instanceof SerovalSerializationError ? error : new SerovalSerializationError(error);
@@ -2814,8 +2845,8 @@ function parseBoxed(ctx, depth, id, current) {
2814
2845
  return createBoxedNode(id, parseSOS(ctx, depth, current.valueOf()));
2815
2846
  }
2816
2847
  function parseView(ctx, depth, type, id, current) {
2817
- current = getArrayBufferView(ctx.base, current);
2818
- return createViewNode(type, id, current, parseSOS(ctx, depth, current.buffer));
2848
+ const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
2849
+ return createViewNode(type, id, current, view, parseSOS(ctx, depth, view.buffer));
2819
2850
  }
2820
2851
  function parseError(ctx, depth, type, id, current) {
2821
2852
  const options = getErrorOptions(current, ctx.base.features);
@@ -2906,14 +2937,15 @@ function parseObjectPhase2(ctx, depth, id, current, currentClass) {
2906
2937
  case 12: return parseSet(ctx, depth, id, current);
2907
2938
  case 13: return parsePromise(ctx, depth, id, current);
2908
2939
  case 14: return createRegExpNode(id, current);
2909
- case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
2940
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), currentClass, current);
2910
2941
  case 16: return parsePlainObject(ctx, depth, id, current, !!currentClass);
2911
2942
  default: throw new SerovalUnsupportedTypeError(current);
2912
2943
  }
2913
2944
  }
2914
2945
  function parseObject(ctx, depth, id, current) {
2915
2946
  if (Array.isArray(current)) return parseArray(ctx, depth, id, current);
2916
- if (isStream(current)) return isLiveStream(current) ? parseLiveStream(ctx, depth, id, current) : parseStream(ctx, depth, id, current);
2947
+ if (isLiveStream(current)) return parseLiveStream(ctx, depth, id, current);
2948
+ if (current instanceof Stream) return parseStream(ctx, depth, id, current);
2917
2949
  if (isSequence(current)) return parseSequence(ctx, depth, id, current);
2918
2950
  const currentClass = getObjectClass(current);
2919
2951
  if (currentClass === OpaqueReference) return parseSOS(ctx, depth, current.replacement);
@@ -3093,7 +3125,7 @@ function consumeLiveStream(ctx, depth, id, current) {
3093
3125
  });
3094
3126
  }
3095
3127
  function wrapPromiseResult(ctx, type, id, node) {
3096
- 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);
3128
+ return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx.base, type === 23 ? 2 : 3), node]);
3097
3129
  }
3098
3130
  function handlePromise(type, id, depth, data) {
3099
3131
  if (this.state.alive) parseEvent(this, depth, data, wrapPromiseResult.bind(null, this, type, id), void 0);
@@ -3155,6 +3187,7 @@ function createStreamParserState(options) {
3155
3187
  writing: false,
3156
3188
  inFlight: void 0,
3157
3189
  reason: void 0,
3190
+ starting: false,
3158
3191
  onParse: options.onParse,
3159
3192
  onError: options.onError,
3160
3193
  onDone: options.onDone,
@@ -3283,25 +3316,30 @@ function startStreamParse(ctx, current) {
3283
3316
  const state = ctx.state;
3284
3317
  let parsed;
3285
3318
  let failure;
3286
- state.parsing++;
3319
+ state.starting = true;
3287
3320
  try {
3288
- parsed = parseSOS(ctx, 0, current);
3289
- } catch (error) {
3290
- failure = { value: error };
3321
+ state.parsing++;
3322
+ try {
3323
+ parsed = parseSOS(ctx, 0, current);
3324
+ } catch (error) {
3325
+ failure = { value: error };
3326
+ } finally {
3327
+ state.parsing--;
3328
+ if (!state.alive) releaseParserValues(ctx);
3329
+ }
3330
+ if (failure) {
3331
+ if (state.alive) stopStreamParse(ctx, 1, failure.value);
3332
+ else throw failure.value;
3333
+ } else if (parsed && state.alive) {
3334
+ state.queue.unshift({
3335
+ node: parsed,
3336
+ initial: true,
3337
+ accept: void 0
3338
+ });
3339
+ drainOutput(ctx);
3340
+ }
3291
3341
  } finally {
3292
- state.parsing--;
3293
- if (!state.alive) releaseParserValues(ctx);
3294
- }
3295
- if (failure) {
3296
- if (state.alive) stopStreamParse(ctx, 1, failure.value);
3297
- else throw failure.value;
3298
- } else if (parsed && state.alive) {
3299
- state.queue.unshift({
3300
- node: parsed,
3301
- initial: true,
3302
- accept: void 0
3303
- });
3304
- drainOutput(ctx);
3342
+ state.starting = false;
3305
3343
  }
3306
3344
  }
3307
3345
  function runCleanups(state) {
@@ -3352,7 +3390,7 @@ function stopStreamParse(ctx, mode, reason) {
3352
3390
  try {
3353
3391
  if (mode === 1) {
3354
3392
  if (onError) onError(reason);
3355
- else throw reason;
3393
+ else if (state.starting) throw reason;
3356
3394
  } else if (mode === 0) onDone?.();
3357
3395
  } catch (error) {
3358
3396
  failure = { value: error };
@@ -3519,6 +3557,7 @@ var Serializer = class {
3519
3557
  if (this.alive && !this.flushed && options) {
3520
3558
  this.pending++;
3521
3559
  this.keys.add(key);
3560
+ let starting = true;
3522
3561
  const cleanup = crossSerializeStream(value, {
3523
3562
  plugins: this.plugins,
3524
3563
  scopeId: options.scopeId,
@@ -3530,7 +3569,7 @@ var Serializer = class {
3530
3569
  let failure;
3531
3570
  try {
3532
3571
  if (options.onError) options.onError(error);
3533
- else throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3572
+ else if (starting) throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3534
3573
  } catch (handlerError) {
3535
3574
  failure = { value: handlerError };
3536
3575
  }
@@ -3549,6 +3588,7 @@ var Serializer = class {
3549
3588
  this.finishWrite();
3550
3589
  }
3551
3590
  });
3591
+ starting = false;
3552
3592
  if (this.alive) this.cleanups.push(cleanup);
3553
3593
  else cleanup();
3554
3594
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "seroval",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "1.7.6",
5
+ "version": "1.7.8",
6
6
  "files": [
7
7
  "dist"
8
8
  ],