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/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);
@@ -405,6 +407,30 @@ function getTypedArrayName(value) {
405
407
  if (!typedArrayNameGetter) typedArrayNameGetter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Int8Array.prototype), Symbol.toStringTag).get;
406
408
  return typedArrayNameGetter.call(value);
407
409
  }
410
+ let typedArrayGetters;
411
+ let dataViewGetters;
412
+ function getAccessor(proto, key) {
413
+ return Object.getOwnPropertyDescriptor(proto, key).get;
414
+ }
415
+ function getViewGetters(proto, lengthKey) {
416
+ return {
417
+ buffer: getAccessor(proto, "buffer"),
418
+ byteOffset: getAccessor(proto, "byteOffset"),
419
+ byteLength: getAccessor(proto, "byteLength"),
420
+ length: getAccessor(proto, lengthKey)
421
+ };
422
+ }
423
+ function readArrayBufferView(value, isDataView) {
424
+ let getters;
425
+ if (isDataView) getters = dataViewGetters ?? (dataViewGetters = getViewGetters(DataView.prototype, "byteLength"));
426
+ else getters = typedArrayGetters ?? (typedArrayGetters = getViewGetters(Object.getPrototypeOf(Int8Array.prototype), "length"));
427
+ return {
428
+ buffer: getters.buffer.call(value),
429
+ byteOffset: getters.byteOffset.call(value),
430
+ byteLength: getters.byteLength.call(value),
431
+ length: getters.length.call(value)
432
+ };
433
+ }
408
434
  //#endregion
409
435
  //#region src/core/base-primitives.ts
410
436
  function createNumberNode(value) {
@@ -425,12 +451,14 @@ function createBigIntNode(current) {
425
451
  function createIndexedValueNode(id) {
426
452
  return createSerovalNode(4, id);
427
453
  }
454
+ const getDateTime = Date.prototype.getTime;
455
+ const getDateISOString = Date.prototype.toISOString;
428
456
  function createDateNode(id, current) {
429
- const timestamp = current.valueOf();
430
- return createSerovalNode(5, id, timestamp === timestamp ? current.toISOString() : "");
457
+ const timestamp = getDateTime.call(current);
458
+ return createSerovalNode(5, id, timestamp === timestamp ? getDateISOString.call(current) : "");
431
459
  }
432
- function createTemporalNode(id, type, current) {
433
- return createSerovalNode(36, id, current.toString(), type);
460
+ function createTemporalNode(id, type, temporalClass, current) {
461
+ return createSerovalNode(36, id, temporalClass.prototype.toString.call(current), type);
434
462
  }
435
463
  const getRegExpSource = Object.getOwnPropertyDescriptor(RegExp.prototype, "source").get;
436
464
  const getRegExpFlags = Object.getOwnPropertyDescriptor(RegExp.prototype, "flags").get;
@@ -453,11 +481,11 @@ function createBoxedNode(id, boxed) {
453
481
  return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed);
454
482
  }
455
483
  const MAX_TYPED_ARRAY_LENGTH = 1e6;
456
- function createViewNode(type, id, current, buffer) {
484
+ function createViewNode(type, id, current, view, buffer) {
457
485
  const isDataView = type === 20;
458
- const size = isDataView ? current.byteLength : current.length;
486
+ const size = view.length;
459
487
  if (size > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
460
- 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);
488
+ 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);
461
489
  }
462
490
  function createErrorNode(type, id, current, options) {
463
491
  return createSerovalNode(type, id, getErrorConstructor(current), void 0, serializeString(current.message), options);
@@ -479,8 +507,9 @@ function createSequenceNode(id, sequence, throwAt, doneAt) {
479
507
  }
480
508
  //#endregion
481
509
  //#region src/core/live-stream.ts
510
+ const BRAND = /* @__PURE__ */ Symbol("LiveStream");
482
511
  function isLiveStream(value) {
483
- return value instanceof LiveStream;
512
+ return value != null && value[BRAND] === true;
484
513
  }
485
514
  /**
486
515
  * A single-consumer stream for incremental serialization. Unlike
@@ -488,11 +517,12 @@ function isLiveStream(value) {
488
517
  * producer operation that sent it settles only when the consumer accepts it.
489
518
  *
490
519
  * An internal class rather than a tagged POJO: identity is checked with
491
- * `instanceof`, which untrusted input cannot forge. The class is exported
520
+ * `isLiveStream`, which untrusted input cannot forge. The class is exported
492
521
  * because bundlers cannot drop an unexported top-level class.
493
522
  */
494
523
  var LiveStream = class {
495
524
  constructor(options) {
525
+ this[BRAND] = true;
496
526
  this.consumed = false;
497
527
  this.closed = false;
498
528
  this.cancelled = false;
@@ -1294,12 +1324,12 @@ function getTemporalType(currentClass) {
1294
1324
  }
1295
1325
  }
1296
1326
  function getObjectClass(current) {
1297
- const currentClass = current.constructor;
1298
- if (typeof currentClass !== "function") {
1299
- const proto = Object.getPrototypeOf(current);
1300
- return proto === null ? void 0 : proto.constructor;
1301
- }
1302
- return currentClass;
1327
+ const proto = Object.getPrototypeOf(current);
1328
+ if (proto === null) return void 0;
1329
+ if (proto === Object.prototype) return Object;
1330
+ const currentClass = proto.constructor;
1331
+ if (typeof currentClass !== "function" || currentClass === Object || currentClass.prototype === proto) return currentClass;
1332
+ return proto;
1303
1333
  }
1304
1334
  /**
1305
1335
  * Decides how an object is parsed. Shared by the sync, stream and async
@@ -1363,10 +1393,14 @@ function createMapNode(id, sentinel, k, v) {
1363
1393
  function createPromiseConstructorNode(ctx, id, resolver) {
1364
1394
  return createSerovalNode(22, id, resolver, void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 1));
1365
1395
  }
1366
- function getArrayBufferView(ctx, current) {
1367
- if (!ctx.compactArrayBufferViews) return current;
1368
- const buffer = new Uint8Array(current.buffer, current.byteOffset, current.byteLength).slice().buffer;
1369
- return new (current instanceof DataView ? DataView : getTypedArrayConstructor(getTypedArrayName(current)))(buffer);
1396
+ function getArrayBufferView(ctx, view) {
1397
+ if (!ctx.compactArrayBufferViews) return view;
1398
+ return {
1399
+ buffer: new Uint8Array(view.buffer, view.byteOffset, view.byteLength).slice().buffer,
1400
+ byteOffset: 0,
1401
+ byteLength: view.byteLength,
1402
+ length: view.length
1403
+ };
1370
1404
  }
1371
1405
  function createArrayBufferNode(ctx, id, current) {
1372
1406
  return createSerovalNode(19, id, encodeArrayBuffer(current), void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 5));
@@ -1424,11 +1458,12 @@ async function parsePlainObject$1(ctx, depth, id, current, empty) {
1424
1458
  return createObjectNode(id, current, empty, await parseProperties$1(ctx, depth, current));
1425
1459
  }
1426
1460
  async function parseBoxed$1(ctx, depth, id, current) {
1427
- 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);
1428
1463
  }
1429
1464
  async function parseView$1(ctx, depth, type, id, current) {
1430
- current = getArrayBufferView(ctx.base, current);
1431
- return createViewNode(type, id, current, await parseAsync(ctx, depth, current.buffer));
1465
+ const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
1466
+ return createViewNode(type, id, current, view, await parseAsync(ctx, depth, view.buffer));
1432
1467
  }
1433
1468
  async function parseError$1(ctx, depth, type, id, current) {
1434
1469
  const options = getErrorOptions(current, ctx.base.features);
@@ -1533,14 +1568,13 @@ async function parseAsyncIterable$1(ctx, depth, current) {
1533
1568
  const sequence = [];
1534
1569
  for (;;) {
1535
1570
  let result;
1571
+ markParserRef(ctx.base, id);
1536
1572
  try {
1537
1573
  result = await iterator.next();
1538
1574
  } catch (error) {
1539
- markParserRef(ctx.base, id);
1540
1575
  sequence.push(createStreamEventNode(33, id, await parseAsync(ctx, depth, error)));
1541
1576
  break;
1542
1577
  }
1543
- markParserRef(ctx.base, id);
1544
1578
  let parsed;
1545
1579
  try {
1546
1580
  parsed = await parseAsync(ctx, depth, result.value);
@@ -1585,7 +1619,7 @@ async function parseObjectAsync(ctx, depth, id, current) {
1585
1619
  case 12: return parseSet$1(ctx, depth, id, current);
1586
1620
  case 13: return parsePromise$1(ctx, depth, id, current);
1587
1621
  case 14: return createRegExpNode(id, current);
1588
- case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
1622
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), currentClass, current);
1589
1623
  case 16: return parsePlainObject$1(ctx, depth, id, current, !!currentClass);
1590
1624
  default: throw new SerovalUnsupportedTypeError(current);
1591
1625
  }
@@ -2241,10 +2275,6 @@ function createObjectAssign(ctx, ref, key, value) {
2241
2275
  function createSequenceAssign(ctx, ref, index, value) {
2242
2276
  createAssignment(ctx.base, getRefParam(ctx, ref) + ".v[" + index + "]", value);
2243
2277
  }
2244
- /**
2245
- * Checks if the value is in the stack. Stack here is a reference
2246
- * structure to know if a object is to be accessed in a TDZ.
2247
- */
2248
2278
  function getKeyKind(key) {
2249
2279
  const first = key.charCodeAt(0);
2250
2280
  if (first >= 48 && first <= 57) {
@@ -2254,6 +2284,10 @@ function getKeyKind(key) {
2254
2284
  if (key === "Infinity") return 2;
2255
2285
  return isValidIdentifier(key) ? 1 : 0;
2256
2286
  }
2287
+ /**
2288
+ * Checks if the value is in the stack. Stack here is a reference
2289
+ * structure to know if a object is to be accessed in a TDZ.
2290
+ */
2257
2291
  function isIndexedValueInStack(ctx, node) {
2258
2292
  return node.t === 4 && ctx.stack.includes(node.i);
2259
2293
  }
@@ -2332,8 +2366,7 @@ function serializeProperty(ctx, source, key, val) {
2332
2366
  if (isIndexedValueInStack(ctx.base, val)) {
2333
2367
  const refParam = getRefParam(ctx, val.i);
2334
2368
  markSerializerRef(ctx.base, source.i);
2335
- if (kind === 1) createObjectAssign(ctx, source.i, key, refParam);
2336
- else createArrayAssign(ctx, source.i, kind === 2 ? key : "\"" + key + "\"", refParam);
2369
+ createKeyAssign(ctx, source.i, kind, key, refParam);
2337
2370
  return "";
2338
2371
  }
2339
2372
  if (key !== "__proto__") return (kind === 0 ? "\"" + key + "\"" : key) + ":" + serialize$1(ctx, val);
@@ -2421,10 +2454,10 @@ function serializeNullConstructor(ctx, node) {
2421
2454
  return serializeDictionary(ctx, node, NULL_CONSTRUCTOR);
2422
2455
  }
2423
2456
  function serializeDate(node) {
2424
- return "new Date(\"" + node.s + "\")";
2457
+ return "new Date(\"" + serializeString(node.s) + "\")";
2425
2458
  }
2426
2459
  function serializeTemporal(ctx, node) {
2427
- if (ctx.base.features & 64) return "Temporal." + TEMPORAL_TYPE_NAME[node.c] + ".from(\"" + node.s + "\")";
2460
+ if (ctx.base.features & 64) return "Temporal." + TEMPORAL_TYPE_NAME[node.c] + ".from(\"" + serializeString(node.s) + "\")";
2428
2461
  throw new SerovalUnsupportedNodeError(node);
2429
2462
  }
2430
2463
  function serializeRegExp(ctx, node) {
@@ -2538,7 +2571,7 @@ function serializeArrayBuffer(ctx, node) {
2538
2571
  return getConstructor(ctx, node.f) + "(\"" + node.s + "\")";
2539
2572
  }
2540
2573
  function serializeView(ctx, node) {
2541
- return "new " + (node.t === 20 ? "DataView" : node.c) + "(" + serialize$1(ctx, node.f) + "," + node.b + "," + node.l + ")";
2574
+ return "new " + (node.t === 20 ? "DataView" : node.c) + "(" + serialize$1(ctx, node.f) + "," + +node.b + "," + +node.l + ")";
2542
2575
  }
2543
2576
  function serializeAggregateError(ctx, node) {
2544
2577
  const id = node.i;
@@ -2657,7 +2690,7 @@ function serializeSequence(ctx, node) {
2657
2690
  result += (item && result && ",") + item;
2658
2691
  }
2659
2692
  ctx.base.stack.pop();
2660
- if (result) return "{__SEROVAL_SEQUENCE__:!0,v:[" + result + "],t:" + node.s + ",d:" + node.l + "}";
2693
+ if (result) return "{__SEROVAL_SEQUENCE__:!0,v:[" + result + "],t:" + +node.s + ",d:" + +node.l + "}";
2661
2694
  }
2662
2695
  return "{__SEROVAL_SEQUENCE__:!0,v:[],t:-1,d:0}";
2663
2696
  }
@@ -2736,19 +2769,17 @@ function serializeCross(ctx, tree) {
2736
2769
  if (id == null) return result;
2737
2770
  const patches = resolvePatches(ctx.base);
2738
2771
  const ref = getRefParam(ctx, id);
2739
- const scopeId = ctx.state.scopeId;
2740
- const params = scopeId == null ? "" : "$R";
2741
2772
  let body = result;
2742
2773
  if (patches) {
2743
2774
  if (!ctx.base.assignAll && !result.startsWith(ref + "=")) body = ref + "=" + result;
2744
2775
  body = "(" + body + "," + patches + ref + ")";
2745
2776
  }
2746
- if (params === "") {
2777
+ const scopeId = ctx.state.scopeId;
2778
+ if (scopeId == null) {
2747
2779
  if (tree.t === 10 && !patches) return "(" + body + ")";
2748
2780
  return body;
2749
2781
  }
2750
- const args = scopeId == null ? "()" : "($R[\"" + serializeString(scopeId) + "\"])";
2751
- return "(" + createFunction([params], body) + ")" + args;
2782
+ return "(" + createFunction(["$R"], body) + ")($R[\"" + serializeString(scopeId) + "\"])";
2752
2783
  }
2753
2784
  function wrapSerializationError(error) {
2754
2785
  return error instanceof SerovalSerializationError ? error : new SerovalSerializationError(error);
@@ -2830,8 +2861,8 @@ function parseBoxed(ctx, depth, id, current) {
2830
2861
  return createBoxedNode(id, parseSOS(ctx, depth, current.valueOf()));
2831
2862
  }
2832
2863
  function parseView(ctx, depth, type, id, current) {
2833
- current = getArrayBufferView(ctx.base, current);
2834
- return createViewNode(type, id, current, parseSOS(ctx, depth, current.buffer));
2864
+ const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
2865
+ return createViewNode(type, id, current, view, parseSOS(ctx, depth, view.buffer));
2835
2866
  }
2836
2867
  function parseError(ctx, depth, type, id, current) {
2837
2868
  const options = getErrorOptions(current, ctx.base.features);
@@ -2922,14 +2953,15 @@ function parseObjectPhase2(ctx, depth, id, current, currentClass) {
2922
2953
  case 12: return parseSet(ctx, depth, id, current);
2923
2954
  case 13: return parsePromise(ctx, depth, id, current);
2924
2955
  case 14: return createRegExpNode(id, current);
2925
- case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
2956
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), currentClass, current);
2926
2957
  case 16: return parsePlainObject(ctx, depth, id, current, !!currentClass);
2927
2958
  default: throw new SerovalUnsupportedTypeError(current);
2928
2959
  }
2929
2960
  }
2930
2961
  function parseObject(ctx, depth, id, current) {
2931
2962
  if (Array.isArray(current)) return parseArray(ctx, depth, id, current);
2932
- if (isStream(current)) return isLiveStream(current) ? parseLiveStream(ctx, depth, id, current) : parseStream(ctx, depth, id, current);
2963
+ if (isLiveStream(current)) return parseLiveStream(ctx, depth, id, current);
2964
+ if (current instanceof Stream) return parseStream(ctx, depth, id, current);
2933
2965
  if (isSequence(current)) return parseSequence(ctx, depth, id, current);
2934
2966
  const currentClass = getObjectClass(current);
2935
2967
  if (currentClass === OpaqueReference) return parseSOS(ctx, depth, current.replacement);
@@ -3109,7 +3141,7 @@ function consumeLiveStream(ctx, depth, id, current) {
3109
3141
  });
3110
3142
  }
3111
3143
  function wrapPromiseResult(ctx, type, id, node) {
3112
- 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);
3144
+ return createSerovalNode(type, id, void 0, void 0, void 0, void 0, void 0, [parseSpecialReference(ctx.base, type === 23 ? 2 : 3), node]);
3113
3145
  }
3114
3146
  function handlePromise(type, id, depth, data) {
3115
3147
  if (this.state.alive) parseEvent(this, depth, data, wrapPromiseResult.bind(null, this, type, id), void 0);
@@ -3171,6 +3203,7 @@ function createStreamParserState(options) {
3171
3203
  writing: false,
3172
3204
  inFlight: void 0,
3173
3205
  reason: void 0,
3206
+ starting: false,
3174
3207
  onParse: options.onParse,
3175
3208
  onError: options.onError,
3176
3209
  onDone: options.onDone,
@@ -3299,25 +3332,30 @@ function startStreamParse(ctx, current) {
3299
3332
  const state = ctx.state;
3300
3333
  let parsed;
3301
3334
  let failure;
3302
- state.parsing++;
3335
+ state.starting = true;
3303
3336
  try {
3304
- parsed = parseSOS(ctx, 0, current);
3305
- } catch (error) {
3306
- failure = { value: error };
3337
+ state.parsing++;
3338
+ try {
3339
+ parsed = parseSOS(ctx, 0, current);
3340
+ } catch (error) {
3341
+ failure = { value: error };
3342
+ } finally {
3343
+ state.parsing--;
3344
+ if (!state.alive) releaseParserValues(ctx);
3345
+ }
3346
+ if (failure) {
3347
+ if (state.alive) stopStreamParse(ctx, 1, failure.value);
3348
+ else throw failure.value;
3349
+ } else if (parsed && state.alive) {
3350
+ state.queue.unshift({
3351
+ node: parsed,
3352
+ initial: true,
3353
+ accept: void 0
3354
+ });
3355
+ drainOutput(ctx);
3356
+ }
3307
3357
  } finally {
3308
- state.parsing--;
3309
- if (!state.alive) releaseParserValues(ctx);
3310
- }
3311
- if (failure) {
3312
- if (state.alive) stopStreamParse(ctx, 1, failure.value);
3313
- else throw failure.value;
3314
- } else if (parsed && state.alive) {
3315
- state.queue.unshift({
3316
- node: parsed,
3317
- initial: true,
3318
- accept: void 0
3319
- });
3320
- drainOutput(ctx);
3358
+ state.starting = false;
3321
3359
  }
3322
3360
  }
3323
3361
  function runCleanups(state) {
@@ -3368,7 +3406,7 @@ function stopStreamParse(ctx, mode, reason) {
3368
3406
  try {
3369
3407
  if (mode === 1) {
3370
3408
  if (onError) onError(reason);
3371
- else throw reason;
3409
+ else if (state.starting) throw reason;
3372
3410
  } else if (mode === 0) onDone?.();
3373
3411
  } catch (error) {
3374
3412
  failure = { value: error };
@@ -3535,6 +3573,7 @@ var Serializer = class {
3535
3573
  if (this.alive && !this.flushed && options) {
3536
3574
  this.pending++;
3537
3575
  this.keys.add(key);
3576
+ let starting = true;
3538
3577
  const cleanup = crossSerializeStream(value, {
3539
3578
  plugins: this.plugins,
3540
3579
  scopeId: options.scopeId,
@@ -3546,7 +3585,7 @@ var Serializer = class {
3546
3585
  let failure;
3547
3586
  try {
3548
3587
  if (options.onError) options.onError(error);
3549
- else throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3588
+ else if (starting) throw error instanceof SerovalParserError ? error : new SerovalParserError(error);
3550
3589
  } catch (handlerError) {
3551
3590
  failure = { value: handlerError };
3552
3591
  }
@@ -3565,6 +3604,7 @@ var Serializer = class {
3565
3604
  this.finishWrite();
3566
3605
  }
3567
3606
  });
3607
+ starting = false;
3568
3608
  if (this.alive) this.cleanups.push(cleanup);
3569
3609
  else cleanup();
3570
3610
  }