seroval 1.7.5 → 1.7.7

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.
@@ -406,6 +406,30 @@ function getTypedArrayName(value) {
406
406
  if (!typedArrayNameGetter) typedArrayNameGetter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Int8Array.prototype), Symbol.toStringTag).get;
407
407
  return typedArrayNameGetter.call(value);
408
408
  }
409
+ let typedArrayGetters;
410
+ let dataViewGetters;
411
+ function getAccessor(proto, key) {
412
+ return Object.getOwnPropertyDescriptor(proto, key).get;
413
+ }
414
+ function getViewGetters(proto, lengthKey) {
415
+ return {
416
+ buffer: getAccessor(proto, "buffer"),
417
+ byteOffset: getAccessor(proto, "byteOffset"),
418
+ byteLength: getAccessor(proto, "byteLength"),
419
+ length: getAccessor(proto, lengthKey)
420
+ };
421
+ }
422
+ function readArrayBufferView(value, isDataView) {
423
+ let getters;
424
+ if (isDataView) getters = dataViewGetters ?? (dataViewGetters = getViewGetters(DataView.prototype, "byteLength"));
425
+ else getters = typedArrayGetters ?? (typedArrayGetters = getViewGetters(Object.getPrototypeOf(Int8Array.prototype), "length"));
426
+ return {
427
+ buffer: getters.buffer.call(value),
428
+ byteOffset: getters.byteOffset.call(value),
429
+ byteLength: getters.byteLength.call(value),
430
+ length: getters.length.call(value)
431
+ };
432
+ }
409
433
  //#endregion
410
434
  //#region src/core/base-primitives.ts
411
435
  function createNumberNode(value) {
@@ -426,15 +450,19 @@ function createBigIntNode(current) {
426
450
  function createIndexedValueNode(id) {
427
451
  return createSerovalNode(4, id);
428
452
  }
453
+ const getDateTime = Date.prototype.getTime;
454
+ const getDateISOString = Date.prototype.toISOString;
429
455
  function createDateNode(id, current) {
430
- const timestamp = current.valueOf();
431
- return createSerovalNode(5, id, timestamp === timestamp ? current.toISOString() : "");
456
+ const timestamp = getDateTime.call(current);
457
+ return createSerovalNode(5, id, timestamp === timestamp ? getDateISOString.call(current) : "");
432
458
  }
433
- function createTemporalNode(id, type, current) {
434
- return createSerovalNode(36, id, current.toString(), type);
459
+ function createTemporalNode(id, type, temporalClass, current) {
460
+ return createSerovalNode(36, id, temporalClass.prototype.toString.call(current), type);
435
461
  }
462
+ const getRegExpSource = Object.getOwnPropertyDescriptor(RegExp.prototype, "source").get;
463
+ const getRegExpFlags = Object.getOwnPropertyDescriptor(RegExp.prototype, "flags").get;
436
464
  function createRegExpNode(id, current) {
437
- return createSerovalNode(6, id, void 0, serializeString(current.source), current.flags);
465
+ return createSerovalNode(6, id, void 0, serializeString(getRegExpSource.call(current)), getRegExpFlags.call(current));
438
466
  }
439
467
  function createWKSymbolNode(id, current) {
440
468
  return createSerovalNode(17, id, INV_SYMBOL_REF[current]);
@@ -452,11 +480,11 @@ function createBoxedNode(id, boxed) {
452
480
  return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed);
453
481
  }
454
482
  const MAX_TYPED_ARRAY_LENGTH = 1e6;
455
- function createViewNode(type, id, current, buffer) {
483
+ function createViewNode(type, id, current, view, buffer) {
456
484
  const isDataView = type === 20;
457
- const size = isDataView ? current.byteLength : current.length;
485
+ const size = view.length;
458
486
  if (size > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
459
- 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);
487
+ 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);
460
488
  }
461
489
  function createErrorNode(type, id, current, options) {
462
490
  return createSerovalNode(type, id, getErrorConstructor(current), void 0, serializeString(current.message), options);
@@ -478,8 +506,9 @@ function createSequenceNode(id, sequence, throwAt, doneAt) {
478
506
  }
479
507
  //#endregion
480
508
  //#region src/core/live-stream.ts
509
+ const BRAND = /* @__PURE__ */ Symbol("LiveStream");
481
510
  function isLiveStream(value) {
482
- return value instanceof LiveStream;
511
+ return value != null && value[BRAND] === true;
483
512
  }
484
513
  /**
485
514
  * A single-consumer stream for incremental serialization. Unlike
@@ -487,11 +516,12 @@ function isLiveStream(value) {
487
516
  * producer operation that sent it settles only when the consumer accepts it.
488
517
  *
489
518
  * An internal class rather than a tagged POJO: identity is checked with
490
- * `instanceof`, which untrusted input cannot forge. The class is exported
519
+ * `isLiveStream`, which untrusted input cannot forge. The class is exported
491
520
  * because bundlers cannot drop an unexported top-level class.
492
521
  */
493
522
  var LiveStream = class {
494
523
  constructor(options) {
524
+ this[BRAND] = true;
495
525
  this.consumed = false;
496
526
  this.closed = false;
497
527
  this.cancelled = false;
@@ -1293,12 +1323,12 @@ function getTemporalType(currentClass) {
1293
1323
  }
1294
1324
  }
1295
1325
  function getObjectClass(current) {
1296
- const currentClass = current.constructor;
1297
- if (typeof currentClass !== "function") {
1298
- const proto = Object.getPrototypeOf(current);
1299
- return proto === null ? void 0 : proto.constructor;
1300
- }
1301
- return currentClass;
1326
+ const proto = Object.getPrototypeOf(current);
1327
+ if (proto === null) return void 0;
1328
+ if (proto === Object.prototype) return Object;
1329
+ const currentClass = proto.constructor;
1330
+ if (typeof currentClass !== "function" || currentClass === Object || currentClass.prototype === proto) return currentClass;
1331
+ return proto;
1302
1332
  }
1303
1333
  /**
1304
1334
  * Decides how an object is parsed. Shared by the sync, stream and async
@@ -1362,10 +1392,14 @@ function createMapNode(id, sentinel, k, v) {
1362
1392
  function createPromiseConstructorNode(ctx, id, resolver) {
1363
1393
  return createSerovalNode(22, id, resolver, void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 1));
1364
1394
  }
1365
- function getArrayBufferView(ctx, current) {
1366
- if (!ctx.compactArrayBufferViews) return current;
1367
- const buffer = new Uint8Array(current.buffer, current.byteOffset, current.byteLength).slice().buffer;
1368
- return new (current instanceof DataView ? DataView : getTypedArrayConstructor(getTypedArrayName(current)))(buffer);
1395
+ function getArrayBufferView(ctx, view) {
1396
+ if (!ctx.compactArrayBufferViews) return view;
1397
+ return {
1398
+ buffer: new Uint8Array(view.buffer, view.byteOffset, view.byteLength).slice().buffer,
1399
+ byteOffset: 0,
1400
+ byteLength: view.byteLength,
1401
+ length: view.length
1402
+ };
1369
1403
  }
1370
1404
  function createArrayBufferNode(ctx, id, current) {
1371
1405
  return createSerovalNode(19, id, encodeArrayBuffer(current), void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 5));
@@ -1426,8 +1460,8 @@ async function parseBoxed$1(ctx, depth, id, current) {
1426
1460
  return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
1427
1461
  }
1428
1462
  async function parseView$1(ctx, depth, type, id, current) {
1429
- current = getArrayBufferView(ctx.base, current);
1430
- return createViewNode(type, id, current, await parseAsync(ctx, depth, current.buffer));
1463
+ const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
1464
+ return createViewNode(type, id, current, view, await parseAsync(ctx, depth, view.buffer));
1431
1465
  }
1432
1466
  async function parseError$1(ctx, depth, type, id, current) {
1433
1467
  const options = getErrorOptions(current, ctx.base.features);
@@ -1584,7 +1618,7 @@ async function parseObjectAsync(ctx, depth, id, current) {
1584
1618
  case 12: return parseSet$1(ctx, depth, id, current);
1585
1619
  case 13: return parsePromise$1(ctx, depth, id, current);
1586
1620
  case 14: return createRegExpNode(id, current);
1587
- case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
1621
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), currentClass, current);
1588
1622
  case 16: return parsePlainObject$1(ctx, depth, id, current, !!currentClass);
1589
1623
  default: throw new SerovalUnsupportedTypeError(current);
1590
1624
  }
@@ -2420,10 +2454,10 @@ function serializeNullConstructor(ctx, node) {
2420
2454
  return serializeDictionary(ctx, node, NULL_CONSTRUCTOR);
2421
2455
  }
2422
2456
  function serializeDate(node) {
2423
- return "new Date(\"" + node.s + "\")";
2457
+ return "new Date(\"" + serializeString(node.s) + "\")";
2424
2458
  }
2425
2459
  function serializeTemporal(ctx, node) {
2426
- 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) + "\")";
2427
2461
  throw new SerovalUnsupportedNodeError(node);
2428
2462
  }
2429
2463
  function serializeRegExp(ctx, node) {
@@ -2537,7 +2571,7 @@ function serializeArrayBuffer(ctx, node) {
2537
2571
  return getConstructor(ctx, node.f) + "(\"" + node.s + "\")";
2538
2572
  }
2539
2573
  function serializeView(ctx, node) {
2540
- 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 + ")";
2541
2575
  }
2542
2576
  function serializeAggregateError(ctx, node) {
2543
2577
  const id = node.i;
@@ -2656,7 +2690,7 @@ function serializeSequence(ctx, node) {
2656
2690
  result += (item && result && ",") + item;
2657
2691
  }
2658
2692
  ctx.base.stack.pop();
2659
- 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 + "}";
2660
2694
  }
2661
2695
  return "{__SEROVAL_SEQUENCE__:!0,v:[],t:-1,d:0}";
2662
2696
  }
@@ -2829,8 +2863,8 @@ function parseBoxed(ctx, depth, id, current) {
2829
2863
  return createBoxedNode(id, parseSOS(ctx, depth, current.valueOf()));
2830
2864
  }
2831
2865
  function parseView(ctx, depth, type, id, current) {
2832
- current = getArrayBufferView(ctx.base, current);
2833
- return createViewNode(type, id, current, parseSOS(ctx, depth, current.buffer));
2866
+ const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
2867
+ return createViewNode(type, id, current, view, parseSOS(ctx, depth, view.buffer));
2834
2868
  }
2835
2869
  function parseError(ctx, depth, type, id, current) {
2836
2870
  const options = getErrorOptions(current, ctx.base.features);
@@ -2921,14 +2955,15 @@ function parseObjectPhase2(ctx, depth, id, current, currentClass) {
2921
2955
  case 12: return parseSet(ctx, depth, id, current);
2922
2956
  case 13: return parsePromise(ctx, depth, id, current);
2923
2957
  case 14: return createRegExpNode(id, current);
2924
- case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
2958
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), currentClass, current);
2925
2959
  case 16: return parsePlainObject(ctx, depth, id, current, !!currentClass);
2926
2960
  default: throw new SerovalUnsupportedTypeError(current);
2927
2961
  }
2928
2962
  }
2929
2963
  function parseObject(ctx, depth, id, current) {
2930
2964
  if (Array.isArray(current)) return parseArray(ctx, depth, id, current);
2931
- if (isStream(current)) return isLiveStream(current) ? parseLiveStream(ctx, depth, id, current) : parseStream(ctx, depth, id, current);
2965
+ if (isLiveStream(current)) return parseLiveStream(ctx, depth, id, current);
2966
+ if (current instanceof Stream) return parseStream(ctx, depth, id, current);
2932
2967
  if (isSequence(current)) return parseSequence(ctx, depth, id, current);
2933
2968
  const currentClass = getObjectClass(current);
2934
2969
  if (currentClass === OpaqueReference) return parseSOS(ctx, depth, current.replacement);
@@ -483,6 +483,7 @@ interface LiveStreamSink<T> {
483
483
  interface LiveStreamOptions {
484
484
  onCancel?: (reason: unknown) => void;
485
485
  }
486
+ declare const BRAND: unique symbol;
486
487
  export declare function isLiveStream<T>(value: unknown): value is LiveStream<T>;
487
488
  /**
488
489
  * A single-consumer stream for incremental serialization. Unlike
@@ -490,10 +491,11 @@ export declare function isLiveStream<T>(value: unknown): value is LiveStream<T>;
490
491
  * producer operation that sent it settles only when the consumer accepts it.
491
492
  *
492
493
  * An internal class rather than a tagged POJO: identity is checked with
493
- * `instanceof`, which untrusted input cannot forge. The class is exported
494
+ * `isLiveStream`, which untrusted input cannot forge. The class is exported
494
495
  * because bundlers cannot drop an unexported top-level class.
495
496
  */
496
497
  export declare class LiveStream<T> {
498
+ private readonly [BRAND];
497
499
  private pending;
498
500
  private waiting;
499
501
  private consumed;
@@ -483,6 +483,7 @@ interface LiveStreamSink<T> {
483
483
  interface LiveStreamOptions {
484
484
  onCancel?: (reason: unknown) => void;
485
485
  }
486
+ declare const BRAND: unique symbol;
486
487
  export declare function isLiveStream<T>(value: unknown): value is LiveStream<T>;
487
488
  /**
488
489
  * A single-consumer stream for incremental serialization. Unlike
@@ -490,10 +491,11 @@ export declare function isLiveStream<T>(value: unknown): value is LiveStream<T>;
490
491
  * producer operation that sent it settles only when the consumer accepts it.
491
492
  *
492
493
  * An internal class rather than a tagged POJO: identity is checked with
493
- * `instanceof`, which untrusted input cannot forge. The class is exported
494
+ * `isLiveStream`, which untrusted input cannot forge. The class is exported
494
495
  * because bundlers cannot drop an unexported top-level class.
495
496
  */
496
497
  export declare class LiveStream<T> {
498
+ private readonly [BRAND];
497
499
  private pending;
498
500
  private waiting;
499
501
  private consumed;
package/dist/dev/index.js CHANGED
@@ -405,6 +405,30 @@ function getTypedArrayName(value) {
405
405
  if (!typedArrayNameGetter) typedArrayNameGetter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Int8Array.prototype), Symbol.toStringTag).get;
406
406
  return typedArrayNameGetter.call(value);
407
407
  }
408
+ let typedArrayGetters;
409
+ let dataViewGetters;
410
+ function getAccessor(proto, key) {
411
+ return Object.getOwnPropertyDescriptor(proto, key).get;
412
+ }
413
+ function getViewGetters(proto, lengthKey) {
414
+ return {
415
+ buffer: getAccessor(proto, "buffer"),
416
+ byteOffset: getAccessor(proto, "byteOffset"),
417
+ byteLength: getAccessor(proto, "byteLength"),
418
+ length: getAccessor(proto, lengthKey)
419
+ };
420
+ }
421
+ function readArrayBufferView(value, isDataView) {
422
+ let getters;
423
+ if (isDataView) getters = dataViewGetters ?? (dataViewGetters = getViewGetters(DataView.prototype, "byteLength"));
424
+ else getters = typedArrayGetters ?? (typedArrayGetters = getViewGetters(Object.getPrototypeOf(Int8Array.prototype), "length"));
425
+ return {
426
+ buffer: getters.buffer.call(value),
427
+ byteOffset: getters.byteOffset.call(value),
428
+ byteLength: getters.byteLength.call(value),
429
+ length: getters.length.call(value)
430
+ };
431
+ }
408
432
  //#endregion
409
433
  //#region src/core/base-primitives.ts
410
434
  function createNumberNode(value) {
@@ -425,15 +449,19 @@ function createBigIntNode(current) {
425
449
  function createIndexedValueNode(id) {
426
450
  return createSerovalNode(4, id);
427
451
  }
452
+ const getDateTime = Date.prototype.getTime;
453
+ const getDateISOString = Date.prototype.toISOString;
428
454
  function createDateNode(id, current) {
429
- const timestamp = current.valueOf();
430
- return createSerovalNode(5, id, timestamp === timestamp ? current.toISOString() : "");
455
+ const timestamp = getDateTime.call(current);
456
+ return createSerovalNode(5, id, timestamp === timestamp ? getDateISOString.call(current) : "");
431
457
  }
432
- function createTemporalNode(id, type, current) {
433
- return createSerovalNode(36, id, current.toString(), type);
458
+ function createTemporalNode(id, type, temporalClass, current) {
459
+ return createSerovalNode(36, id, temporalClass.prototype.toString.call(current), type);
434
460
  }
461
+ const getRegExpSource = Object.getOwnPropertyDescriptor(RegExp.prototype, "source").get;
462
+ const getRegExpFlags = Object.getOwnPropertyDescriptor(RegExp.prototype, "flags").get;
435
463
  function createRegExpNode(id, current) {
436
- return createSerovalNode(6, id, void 0, serializeString(current.source), current.flags);
464
+ return createSerovalNode(6, id, void 0, serializeString(getRegExpSource.call(current)), getRegExpFlags.call(current));
437
465
  }
438
466
  function createWKSymbolNode(id, current) {
439
467
  return createSerovalNode(17, id, INV_SYMBOL_REF[current]);
@@ -451,11 +479,11 @@ function createBoxedNode(id, boxed) {
451
479
  return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed);
452
480
  }
453
481
  const MAX_TYPED_ARRAY_LENGTH = 1e6;
454
- function createViewNode(type, id, current, buffer) {
482
+ function createViewNode(type, id, current, view, buffer) {
455
483
  const isDataView = type === 20;
456
- const size = isDataView ? current.byteLength : current.length;
484
+ const size = view.length;
457
485
  if (size > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
458
- 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);
486
+ 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);
459
487
  }
460
488
  function createErrorNode(type, id, current, options) {
461
489
  return createSerovalNode(type, id, getErrorConstructor(current), void 0, serializeString(current.message), options);
@@ -477,8 +505,9 @@ function createSequenceNode(id, sequence, throwAt, doneAt) {
477
505
  }
478
506
  //#endregion
479
507
  //#region src/core/live-stream.ts
508
+ const BRAND = /* @__PURE__ */ Symbol("LiveStream");
480
509
  function isLiveStream(value) {
481
- return value instanceof LiveStream;
510
+ return value != null && value[BRAND] === true;
482
511
  }
483
512
  /**
484
513
  * A single-consumer stream for incremental serialization. Unlike
@@ -486,11 +515,12 @@ function isLiveStream(value) {
486
515
  * producer operation that sent it settles only when the consumer accepts it.
487
516
  *
488
517
  * An internal class rather than a tagged POJO: identity is checked with
489
- * `instanceof`, which untrusted input cannot forge. The class is exported
518
+ * `isLiveStream`, which untrusted input cannot forge. The class is exported
490
519
  * because bundlers cannot drop an unexported top-level class.
491
520
  */
492
521
  var LiveStream = class {
493
522
  constructor(options) {
523
+ this[BRAND] = true;
494
524
  this.consumed = false;
495
525
  this.closed = false;
496
526
  this.cancelled = false;
@@ -1292,12 +1322,12 @@ function getTemporalType(currentClass) {
1292
1322
  }
1293
1323
  }
1294
1324
  function getObjectClass(current) {
1295
- const currentClass = current.constructor;
1296
- if (typeof currentClass !== "function") {
1297
- const proto = Object.getPrototypeOf(current);
1298
- return proto === null ? void 0 : proto.constructor;
1299
- }
1300
- return currentClass;
1325
+ const proto = Object.getPrototypeOf(current);
1326
+ if (proto === null) return void 0;
1327
+ if (proto === Object.prototype) return Object;
1328
+ const currentClass = proto.constructor;
1329
+ if (typeof currentClass !== "function" || currentClass === Object || currentClass.prototype === proto) return currentClass;
1330
+ return proto;
1301
1331
  }
1302
1332
  /**
1303
1333
  * Decides how an object is parsed. Shared by the sync, stream and async
@@ -1361,10 +1391,14 @@ function createMapNode(id, sentinel, k, v) {
1361
1391
  function createPromiseConstructorNode(ctx, id, resolver) {
1362
1392
  return createSerovalNode(22, id, resolver, void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 1));
1363
1393
  }
1364
- function getArrayBufferView(ctx, current) {
1365
- if (!ctx.compactArrayBufferViews) return current;
1366
- const buffer = new Uint8Array(current.buffer, current.byteOffset, current.byteLength).slice().buffer;
1367
- return new (current instanceof DataView ? DataView : getTypedArrayConstructor(getTypedArrayName(current)))(buffer);
1394
+ function getArrayBufferView(ctx, view) {
1395
+ if (!ctx.compactArrayBufferViews) return view;
1396
+ return {
1397
+ buffer: new Uint8Array(view.buffer, view.byteOffset, view.byteLength).slice().buffer,
1398
+ byteOffset: 0,
1399
+ byteLength: view.byteLength,
1400
+ length: view.length
1401
+ };
1368
1402
  }
1369
1403
  function createArrayBufferNode(ctx, id, current) {
1370
1404
  return createSerovalNode(19, id, encodeArrayBuffer(current), void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 5));
@@ -1425,8 +1459,8 @@ async function parseBoxed$1(ctx, depth, id, current) {
1425
1459
  return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
1426
1460
  }
1427
1461
  async function parseView$1(ctx, depth, type, id, current) {
1428
- current = getArrayBufferView(ctx.base, current);
1429
- return createViewNode(type, id, current, await parseAsync(ctx, depth, current.buffer));
1462
+ const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
1463
+ return createViewNode(type, id, current, view, await parseAsync(ctx, depth, view.buffer));
1430
1464
  }
1431
1465
  async function parseError$1(ctx, depth, type, id, current) {
1432
1466
  const options = getErrorOptions(current, ctx.base.features);
@@ -1583,7 +1617,7 @@ async function parseObjectAsync(ctx, depth, id, current) {
1583
1617
  case 12: return parseSet$1(ctx, depth, id, current);
1584
1618
  case 13: return parsePromise$1(ctx, depth, id, current);
1585
1619
  case 14: return createRegExpNode(id, current);
1586
- case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
1620
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), currentClass, current);
1587
1621
  case 16: return parsePlainObject$1(ctx, depth, id, current, !!currentClass);
1588
1622
  default: throw new SerovalUnsupportedTypeError(current);
1589
1623
  }
@@ -2419,10 +2453,10 @@ function serializeNullConstructor(ctx, node) {
2419
2453
  return serializeDictionary(ctx, node, NULL_CONSTRUCTOR);
2420
2454
  }
2421
2455
  function serializeDate(node) {
2422
- return "new Date(\"" + node.s + "\")";
2456
+ return "new Date(\"" + serializeString(node.s) + "\")";
2423
2457
  }
2424
2458
  function serializeTemporal(ctx, node) {
2425
- if (ctx.base.features & 64) return "Temporal." + TEMPORAL_TYPE_NAME[node.c] + ".from(\"" + node.s + "\")";
2459
+ if (ctx.base.features & 64) return "Temporal." + TEMPORAL_TYPE_NAME[node.c] + ".from(\"" + serializeString(node.s) + "\")";
2426
2460
  throw new SerovalUnsupportedNodeError(node);
2427
2461
  }
2428
2462
  function serializeRegExp(ctx, node) {
@@ -2536,7 +2570,7 @@ function serializeArrayBuffer(ctx, node) {
2536
2570
  return getConstructor(ctx, node.f) + "(\"" + node.s + "\")";
2537
2571
  }
2538
2572
  function serializeView(ctx, node) {
2539
- return "new " + (node.t === 20 ? "DataView" : node.c) + "(" + serialize$1(ctx, node.f) + "," + node.b + "," + node.l + ")";
2573
+ return "new " + (node.t === 20 ? "DataView" : node.c) + "(" + serialize$1(ctx, node.f) + "," + +node.b + "," + +node.l + ")";
2540
2574
  }
2541
2575
  function serializeAggregateError(ctx, node) {
2542
2576
  const id = node.i;
@@ -2655,7 +2689,7 @@ function serializeSequence(ctx, node) {
2655
2689
  result += (item && result && ",") + item;
2656
2690
  }
2657
2691
  ctx.base.stack.pop();
2658
- if (result) return "{__SEROVAL_SEQUENCE__:!0,v:[" + result + "],t:" + node.s + ",d:" + node.l + "}";
2692
+ if (result) return "{__SEROVAL_SEQUENCE__:!0,v:[" + result + "],t:" + +node.s + ",d:" + +node.l + "}";
2659
2693
  }
2660
2694
  return "{__SEROVAL_SEQUENCE__:!0,v:[],t:-1,d:0}";
2661
2695
  }
@@ -2828,8 +2862,8 @@ function parseBoxed(ctx, depth, id, current) {
2828
2862
  return createBoxedNode(id, parseSOS(ctx, depth, current.valueOf()));
2829
2863
  }
2830
2864
  function parseView(ctx, depth, type, id, current) {
2831
- current = getArrayBufferView(ctx.base, current);
2832
- return createViewNode(type, id, current, parseSOS(ctx, depth, current.buffer));
2865
+ const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
2866
+ return createViewNode(type, id, current, view, parseSOS(ctx, depth, view.buffer));
2833
2867
  }
2834
2868
  function parseError(ctx, depth, type, id, current) {
2835
2869
  const options = getErrorOptions(current, ctx.base.features);
@@ -2920,14 +2954,15 @@ function parseObjectPhase2(ctx, depth, id, current, currentClass) {
2920
2954
  case 12: return parseSet(ctx, depth, id, current);
2921
2955
  case 13: return parsePromise(ctx, depth, id, current);
2922
2956
  case 14: return createRegExpNode(id, current);
2923
- case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
2957
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), currentClass, current);
2924
2958
  case 16: return parsePlainObject(ctx, depth, id, current, !!currentClass);
2925
2959
  default: throw new SerovalUnsupportedTypeError(current);
2926
2960
  }
2927
2961
  }
2928
2962
  function parseObject(ctx, depth, id, current) {
2929
2963
  if (Array.isArray(current)) return parseArray(ctx, depth, id, current);
2930
- if (isStream(current)) return isLiveStream(current) ? parseLiveStream(ctx, depth, id, current) : parseStream(ctx, depth, id, current);
2964
+ if (isLiveStream(current)) return parseLiveStream(ctx, depth, id, current);
2965
+ if (current instanceof Stream) return parseStream(ctx, depth, id, current);
2931
2966
  if (isSequence(current)) return parseSequence(ctx, depth, id, current);
2932
2967
  const currentClass = getObjectClass(current);
2933
2968
  if (currentClass === OpaqueReference) return parseSOS(ctx, depth, current.replacement);
package/dist/index.cjs CHANGED
@@ -390,6 +390,30 @@ function getTypedArrayName(value) {
390
390
  if (!typedArrayNameGetter) typedArrayNameGetter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Int8Array.prototype), Symbol.toStringTag).get;
391
391
  return typedArrayNameGetter.call(value);
392
392
  }
393
+ let typedArrayGetters;
394
+ let dataViewGetters;
395
+ function getAccessor(proto, key) {
396
+ return Object.getOwnPropertyDescriptor(proto, key).get;
397
+ }
398
+ function getViewGetters(proto, lengthKey) {
399
+ return {
400
+ buffer: getAccessor(proto, "buffer"),
401
+ byteOffset: getAccessor(proto, "byteOffset"),
402
+ byteLength: getAccessor(proto, "byteLength"),
403
+ length: getAccessor(proto, lengthKey)
404
+ };
405
+ }
406
+ function readArrayBufferView(value, isDataView) {
407
+ let getters;
408
+ if (isDataView) getters = dataViewGetters ?? (dataViewGetters = getViewGetters(DataView.prototype, "byteLength"));
409
+ else getters = typedArrayGetters ?? (typedArrayGetters = getViewGetters(Object.getPrototypeOf(Int8Array.prototype), "length"));
410
+ return {
411
+ buffer: getters.buffer.call(value),
412
+ byteOffset: getters.byteOffset.call(value),
413
+ byteLength: getters.byteLength.call(value),
414
+ length: getters.length.call(value)
415
+ };
416
+ }
393
417
  //#endregion
394
418
  //#region src/core/base-primitives.ts
395
419
  function createNumberNode(value) {
@@ -410,15 +434,19 @@ function createBigIntNode(current) {
410
434
  function createIndexedValueNode(id) {
411
435
  return createSerovalNode(4, id);
412
436
  }
437
+ const getDateTime = Date.prototype.getTime;
438
+ const getDateISOString = Date.prototype.toISOString;
413
439
  function createDateNode(id, current) {
414
- const timestamp = current.valueOf();
415
- return createSerovalNode(5, id, timestamp === timestamp ? current.toISOString() : "");
440
+ const timestamp = getDateTime.call(current);
441
+ return createSerovalNode(5, id, timestamp === timestamp ? getDateISOString.call(current) : "");
416
442
  }
417
- function createTemporalNode(id, type, current) {
418
- return createSerovalNode(36, id, current.toString(), type);
443
+ function createTemporalNode(id, type, temporalClass, current) {
444
+ return createSerovalNode(36, id, temporalClass.prototype.toString.call(current), type);
419
445
  }
446
+ const getRegExpSource = Object.getOwnPropertyDescriptor(RegExp.prototype, "source").get;
447
+ const getRegExpFlags = Object.getOwnPropertyDescriptor(RegExp.prototype, "flags").get;
420
448
  function createRegExpNode(id, current) {
421
- return createSerovalNode(6, id, void 0, serializeString(current.source), current.flags);
449
+ return createSerovalNode(6, id, void 0, serializeString(getRegExpSource.call(current)), getRegExpFlags.call(current));
422
450
  }
423
451
  function createWKSymbolNode(id, current) {
424
452
  return createSerovalNode(17, id, INV_SYMBOL_REF[current]);
@@ -436,11 +464,11 @@ function createBoxedNode(id, boxed) {
436
464
  return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed);
437
465
  }
438
466
  const MAX_TYPED_ARRAY_LENGTH = 1e6;
439
- function createViewNode(type, id, current, buffer) {
467
+ function createViewNode(type, id, current, view, buffer) {
440
468
  const isDataView = type === 20;
441
- const size = isDataView ? current.byteLength : current.length;
469
+ const size = view.length;
442
470
  if (size > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
443
- 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);
471
+ 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);
444
472
  }
445
473
  function createErrorNode(type, id, current, options) {
446
474
  return createSerovalNode(type, id, getErrorConstructor(current), void 0, serializeString(current.message), options);
@@ -462,8 +490,9 @@ function createSequenceNode(id, sequence, throwAt, doneAt) {
462
490
  }
463
491
  //#endregion
464
492
  //#region src/core/live-stream.ts
493
+ const BRAND = /* @__PURE__ */ Symbol("LiveStream");
465
494
  function isLiveStream(value) {
466
- return value instanceof LiveStream;
495
+ return value != null && value[BRAND] === true;
467
496
  }
468
497
  /**
469
498
  * A single-consumer stream for incremental serialization. Unlike
@@ -471,11 +500,12 @@ function isLiveStream(value) {
471
500
  * producer operation that sent it settles only when the consumer accepts it.
472
501
  *
473
502
  * An internal class rather than a tagged POJO: identity is checked with
474
- * `instanceof`, which untrusted input cannot forge. The class is exported
503
+ * `isLiveStream`, which untrusted input cannot forge. The class is exported
475
504
  * because bundlers cannot drop an unexported top-level class.
476
505
  */
477
506
  var LiveStream = class {
478
507
  constructor(options) {
508
+ this[BRAND] = true;
479
509
  this.consumed = false;
480
510
  this.closed = false;
481
511
  this.cancelled = false;
@@ -1277,12 +1307,12 @@ function getTemporalType(currentClass) {
1277
1307
  }
1278
1308
  }
1279
1309
  function getObjectClass(current) {
1280
- const currentClass = current.constructor;
1281
- if (typeof currentClass !== "function") {
1282
- const proto = Object.getPrototypeOf(current);
1283
- return proto === null ? void 0 : proto.constructor;
1284
- }
1285
- return currentClass;
1310
+ const proto = Object.getPrototypeOf(current);
1311
+ if (proto === null) return void 0;
1312
+ if (proto === Object.prototype) return Object;
1313
+ const currentClass = proto.constructor;
1314
+ if (typeof currentClass !== "function" || currentClass === Object || currentClass.prototype === proto) return currentClass;
1315
+ return proto;
1286
1316
  }
1287
1317
  /**
1288
1318
  * Decides how an object is parsed. Shared by the sync, stream and async
@@ -1346,10 +1376,14 @@ function createMapNode(id, sentinel, k, v) {
1346
1376
  function createPromiseConstructorNode(ctx, id, resolver) {
1347
1377
  return createSerovalNode(22, id, resolver, void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 1));
1348
1378
  }
1349
- function getArrayBufferView(ctx, current) {
1350
- if (!ctx.compactArrayBufferViews) return current;
1351
- const buffer = new Uint8Array(current.buffer, current.byteOffset, current.byteLength).slice().buffer;
1352
- return new (current instanceof DataView ? DataView : getTypedArrayConstructor(getTypedArrayName(current)))(buffer);
1379
+ function getArrayBufferView(ctx, view) {
1380
+ if (!ctx.compactArrayBufferViews) return view;
1381
+ return {
1382
+ buffer: new Uint8Array(view.buffer, view.byteOffset, view.byteLength).slice().buffer,
1383
+ byteOffset: 0,
1384
+ byteLength: view.byteLength,
1385
+ length: view.length
1386
+ };
1353
1387
  }
1354
1388
  function createArrayBufferNode(ctx, id, current) {
1355
1389
  return createSerovalNode(19, id, encodeArrayBuffer(current), void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 5));
@@ -1410,8 +1444,8 @@ async function parseBoxed$1(ctx, depth, id, current) {
1410
1444
  return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
1411
1445
  }
1412
1446
  async function parseView$1(ctx, depth, type, id, current) {
1413
- current = getArrayBufferView(ctx.base, current);
1414
- return createViewNode(type, id, current, await parseAsync(ctx, depth, current.buffer));
1447
+ const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
1448
+ return createViewNode(type, id, current, view, await parseAsync(ctx, depth, view.buffer));
1415
1449
  }
1416
1450
  async function parseError$1(ctx, depth, type, id, current) {
1417
1451
  const options = getErrorOptions(current, ctx.base.features);
@@ -1568,7 +1602,7 @@ async function parseObjectAsync(ctx, depth, id, current) {
1568
1602
  case 12: return parseSet$1(ctx, depth, id, current);
1569
1603
  case 13: return parsePromise$1(ctx, depth, id, current);
1570
1604
  case 14: return createRegExpNode(id, current);
1571
- case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
1605
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), currentClass, current);
1572
1606
  case 16: return parsePlainObject$1(ctx, depth, id, current, !!currentClass);
1573
1607
  default: throw new SerovalUnsupportedTypeError(current);
1574
1608
  }
@@ -2404,10 +2438,10 @@ function serializeNullConstructor(ctx, node) {
2404
2438
  return serializeDictionary(ctx, node, NULL_CONSTRUCTOR);
2405
2439
  }
2406
2440
  function serializeDate(node) {
2407
- return "new Date(\"" + node.s + "\")";
2441
+ return "new Date(\"" + serializeString(node.s) + "\")";
2408
2442
  }
2409
2443
  function serializeTemporal(ctx, node) {
2410
- 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) + "\")";
2411
2445
  throw new SerovalUnsupportedNodeError(node);
2412
2446
  }
2413
2447
  function serializeRegExp(ctx, node) {
@@ -2521,7 +2555,7 @@ function serializeArrayBuffer(ctx, node) {
2521
2555
  return getConstructor(ctx, node.f) + "(\"" + node.s + "\")";
2522
2556
  }
2523
2557
  function serializeView(ctx, node) {
2524
- 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 + ")";
2525
2559
  }
2526
2560
  function serializeAggregateError(ctx, node) {
2527
2561
  const id = node.i;
@@ -2640,7 +2674,7 @@ function serializeSequence(ctx, node) {
2640
2674
  result += (item && result && ",") + item;
2641
2675
  }
2642
2676
  ctx.base.stack.pop();
2643
- 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 + "}";
2644
2678
  }
2645
2679
  return "{__SEROVAL_SEQUENCE__:!0,v:[],t:-1,d:0}";
2646
2680
  }
@@ -2813,8 +2847,8 @@ function parseBoxed(ctx, depth, id, current) {
2813
2847
  return createBoxedNode(id, parseSOS(ctx, depth, current.valueOf()));
2814
2848
  }
2815
2849
  function parseView(ctx, depth, type, id, current) {
2816
- current = getArrayBufferView(ctx.base, current);
2817
- return createViewNode(type, id, current, parseSOS(ctx, depth, current.buffer));
2850
+ const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
2851
+ return createViewNode(type, id, current, view, parseSOS(ctx, depth, view.buffer));
2818
2852
  }
2819
2853
  function parseError(ctx, depth, type, id, current) {
2820
2854
  const options = getErrorOptions(current, ctx.base.features);
@@ -2905,14 +2939,15 @@ function parseObjectPhase2(ctx, depth, id, current, currentClass) {
2905
2939
  case 12: return parseSet(ctx, depth, id, current);
2906
2940
  case 13: return parsePromise(ctx, depth, id, current);
2907
2941
  case 14: return createRegExpNode(id, current);
2908
- case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
2942
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), currentClass, current);
2909
2943
  case 16: return parsePlainObject(ctx, depth, id, current, !!currentClass);
2910
2944
  default: throw new SerovalUnsupportedTypeError(current);
2911
2945
  }
2912
2946
  }
2913
2947
  function parseObject(ctx, depth, id, current) {
2914
2948
  if (Array.isArray(current)) return parseArray(ctx, depth, id, current);
2915
- if (isStream(current)) return isLiveStream(current) ? parseLiveStream(ctx, depth, id, current) : parseStream(ctx, depth, id, current);
2949
+ if (isLiveStream(current)) return parseLiveStream(ctx, depth, id, current);
2950
+ if (current instanceof Stream) return parseStream(ctx, depth, id, current);
2916
2951
  if (isSequence(current)) return parseSequence(ctx, depth, id, current);
2917
2952
  const currentClass = getObjectClass(current);
2918
2953
  if (currentClass === OpaqueReference) return parseSOS(ctx, depth, current.replacement);
package/dist/index.d.cts CHANGED
@@ -483,6 +483,7 @@ interface LiveStreamSink<T> {
483
483
  interface LiveStreamOptions {
484
484
  onCancel?: (reason: unknown) => void;
485
485
  }
486
+ declare const BRAND: unique symbol;
486
487
  export declare function isLiveStream<T>(value: unknown): value is LiveStream<T>;
487
488
  /**
488
489
  * A single-consumer stream for incremental serialization. Unlike
@@ -490,10 +491,11 @@ export declare function isLiveStream<T>(value: unknown): value is LiveStream<T>;
490
491
  * producer operation that sent it settles only when the consumer accepts it.
491
492
  *
492
493
  * An internal class rather than a tagged POJO: identity is checked with
493
- * `instanceof`, which untrusted input cannot forge. The class is exported
494
+ * `isLiveStream`, which untrusted input cannot forge. The class is exported
494
495
  * because bundlers cannot drop an unexported top-level class.
495
496
  */
496
497
  export declare class LiveStream<T> {
498
+ private readonly [BRAND];
497
499
  private pending;
498
500
  private waiting;
499
501
  private consumed;
package/dist/index.d.ts CHANGED
@@ -483,6 +483,7 @@ interface LiveStreamSink<T> {
483
483
  interface LiveStreamOptions {
484
484
  onCancel?: (reason: unknown) => void;
485
485
  }
486
+ declare const BRAND: unique symbol;
486
487
  export declare function isLiveStream<T>(value: unknown): value is LiveStream<T>;
487
488
  /**
488
489
  * A single-consumer stream for incremental serialization. Unlike
@@ -490,10 +491,11 @@ export declare function isLiveStream<T>(value: unknown): value is LiveStream<T>;
490
491
  * producer operation that sent it settles only when the consumer accepts it.
491
492
  *
492
493
  * An internal class rather than a tagged POJO: identity is checked with
493
- * `instanceof`, which untrusted input cannot forge. The class is exported
494
+ * `isLiveStream`, which untrusted input cannot forge. The class is exported
494
495
  * because bundlers cannot drop an unexported top-level class.
495
496
  */
496
497
  export declare class LiveStream<T> {
498
+ private readonly [BRAND];
497
499
  private pending;
498
500
  private waiting;
499
501
  private consumed;
package/dist/index.js CHANGED
@@ -389,6 +389,30 @@ function getTypedArrayName(value) {
389
389
  if (!typedArrayNameGetter) typedArrayNameGetter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Int8Array.prototype), Symbol.toStringTag).get;
390
390
  return typedArrayNameGetter.call(value);
391
391
  }
392
+ let typedArrayGetters;
393
+ let dataViewGetters;
394
+ function getAccessor(proto, key) {
395
+ return Object.getOwnPropertyDescriptor(proto, key).get;
396
+ }
397
+ function getViewGetters(proto, lengthKey) {
398
+ return {
399
+ buffer: getAccessor(proto, "buffer"),
400
+ byteOffset: getAccessor(proto, "byteOffset"),
401
+ byteLength: getAccessor(proto, "byteLength"),
402
+ length: getAccessor(proto, lengthKey)
403
+ };
404
+ }
405
+ function readArrayBufferView(value, isDataView) {
406
+ let getters;
407
+ if (isDataView) getters = dataViewGetters ?? (dataViewGetters = getViewGetters(DataView.prototype, "byteLength"));
408
+ else getters = typedArrayGetters ?? (typedArrayGetters = getViewGetters(Object.getPrototypeOf(Int8Array.prototype), "length"));
409
+ return {
410
+ buffer: getters.buffer.call(value),
411
+ byteOffset: getters.byteOffset.call(value),
412
+ byteLength: getters.byteLength.call(value),
413
+ length: getters.length.call(value)
414
+ };
415
+ }
392
416
  //#endregion
393
417
  //#region src/core/base-primitives.ts
394
418
  function createNumberNode(value) {
@@ -409,15 +433,19 @@ function createBigIntNode(current) {
409
433
  function createIndexedValueNode(id) {
410
434
  return createSerovalNode(4, id);
411
435
  }
436
+ const getDateTime = Date.prototype.getTime;
437
+ const getDateISOString = Date.prototype.toISOString;
412
438
  function createDateNode(id, current) {
413
- const timestamp = current.valueOf();
414
- return createSerovalNode(5, id, timestamp === timestamp ? current.toISOString() : "");
439
+ const timestamp = getDateTime.call(current);
440
+ return createSerovalNode(5, id, timestamp === timestamp ? getDateISOString.call(current) : "");
415
441
  }
416
- function createTemporalNode(id, type, current) {
417
- return createSerovalNode(36, id, current.toString(), type);
442
+ function createTemporalNode(id, type, temporalClass, current) {
443
+ return createSerovalNode(36, id, temporalClass.prototype.toString.call(current), type);
418
444
  }
445
+ const getRegExpSource = Object.getOwnPropertyDescriptor(RegExp.prototype, "source").get;
446
+ const getRegExpFlags = Object.getOwnPropertyDescriptor(RegExp.prototype, "flags").get;
419
447
  function createRegExpNode(id, current) {
420
- return createSerovalNode(6, id, void 0, serializeString(current.source), current.flags);
448
+ return createSerovalNode(6, id, void 0, serializeString(getRegExpSource.call(current)), getRegExpFlags.call(current));
421
449
  }
422
450
  function createWKSymbolNode(id, current) {
423
451
  return createSerovalNode(17, id, INV_SYMBOL_REF[current]);
@@ -435,11 +463,11 @@ function createBoxedNode(id, boxed) {
435
463
  return createSerovalNode(21, id, void 0, void 0, void 0, void 0, void 0, void 0, boxed);
436
464
  }
437
465
  const MAX_TYPED_ARRAY_LENGTH = 1e6;
438
- function createViewNode(type, id, current, buffer) {
466
+ function createViewNode(type, id, current, view, buffer) {
439
467
  const isDataView = type === 20;
440
- const size = isDataView ? current.byteLength : current.length;
468
+ const size = view.length;
441
469
  if (size > MAX_TYPED_ARRAY_LENGTH) throw new SerovalUnsupportedTypeError(current);
442
- 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);
470
+ 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);
443
471
  }
444
472
  function createErrorNode(type, id, current, options) {
445
473
  return createSerovalNode(type, id, getErrorConstructor(current), void 0, serializeString(current.message), options);
@@ -461,8 +489,9 @@ function createSequenceNode(id, sequence, throwAt, doneAt) {
461
489
  }
462
490
  //#endregion
463
491
  //#region src/core/live-stream.ts
492
+ const BRAND = /* @__PURE__ */ Symbol("LiveStream");
464
493
  function isLiveStream(value) {
465
- return value instanceof LiveStream;
494
+ return value != null && value[BRAND] === true;
466
495
  }
467
496
  /**
468
497
  * A single-consumer stream for incremental serialization. Unlike
@@ -470,11 +499,12 @@ function isLiveStream(value) {
470
499
  * producer operation that sent it settles only when the consumer accepts it.
471
500
  *
472
501
  * An internal class rather than a tagged POJO: identity is checked with
473
- * `instanceof`, which untrusted input cannot forge. The class is exported
502
+ * `isLiveStream`, which untrusted input cannot forge. The class is exported
474
503
  * because bundlers cannot drop an unexported top-level class.
475
504
  */
476
505
  var LiveStream = class {
477
506
  constructor(options) {
507
+ this[BRAND] = true;
478
508
  this.consumed = false;
479
509
  this.closed = false;
480
510
  this.cancelled = false;
@@ -1276,12 +1306,12 @@ function getTemporalType(currentClass) {
1276
1306
  }
1277
1307
  }
1278
1308
  function getObjectClass(current) {
1279
- const currentClass = current.constructor;
1280
- if (typeof currentClass !== "function") {
1281
- const proto = Object.getPrototypeOf(current);
1282
- return proto === null ? void 0 : proto.constructor;
1283
- }
1284
- return currentClass;
1309
+ const proto = Object.getPrototypeOf(current);
1310
+ if (proto === null) return void 0;
1311
+ if (proto === Object.prototype) return Object;
1312
+ const currentClass = proto.constructor;
1313
+ if (typeof currentClass !== "function" || currentClass === Object || currentClass.prototype === proto) return currentClass;
1314
+ return proto;
1285
1315
  }
1286
1316
  /**
1287
1317
  * Decides how an object is parsed. Shared by the sync, stream and async
@@ -1345,10 +1375,14 @@ function createMapNode(id, sentinel, k, v) {
1345
1375
  function createPromiseConstructorNode(ctx, id, resolver) {
1346
1376
  return createSerovalNode(22, id, resolver, void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 1));
1347
1377
  }
1348
- function getArrayBufferView(ctx, current) {
1349
- if (!ctx.compactArrayBufferViews) return current;
1350
- const buffer = new Uint8Array(current.buffer, current.byteOffset, current.byteLength).slice().buffer;
1351
- return new (current instanceof DataView ? DataView : getTypedArrayConstructor(getTypedArrayName(current)))(buffer);
1378
+ function getArrayBufferView(ctx, view) {
1379
+ if (!ctx.compactArrayBufferViews) return view;
1380
+ return {
1381
+ buffer: new Uint8Array(view.buffer, view.byteOffset, view.byteLength).slice().buffer,
1382
+ byteOffset: 0,
1383
+ byteLength: view.byteLength,
1384
+ length: view.length
1385
+ };
1352
1386
  }
1353
1387
  function createArrayBufferNode(ctx, id, current) {
1354
1388
  return createSerovalNode(19, id, encodeArrayBuffer(current), void 0, void 0, void 0, void 0, void 0, parseSpecialReference(ctx, 5));
@@ -1409,8 +1443,8 @@ async function parseBoxed$1(ctx, depth, id, current) {
1409
1443
  return createBoxedNode(id, await parseAsync(ctx, depth, current.valueOf()));
1410
1444
  }
1411
1445
  async function parseView$1(ctx, depth, type, id, current) {
1412
- current = getArrayBufferView(ctx.base, current);
1413
- return createViewNode(type, id, current, await parseAsync(ctx, depth, current.buffer));
1446
+ const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
1447
+ return createViewNode(type, id, current, view, await parseAsync(ctx, depth, view.buffer));
1414
1448
  }
1415
1449
  async function parseError$1(ctx, depth, type, id, current) {
1416
1450
  const options = getErrorOptions(current, ctx.base.features);
@@ -1567,7 +1601,7 @@ async function parseObjectAsync(ctx, depth, id, current) {
1567
1601
  case 12: return parseSet$1(ctx, depth, id, current);
1568
1602
  case 13: return parsePromise$1(ctx, depth, id, current);
1569
1603
  case 14: return createRegExpNode(id, current);
1570
- case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
1604
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), currentClass, current);
1571
1605
  case 16: return parsePlainObject$1(ctx, depth, id, current, !!currentClass);
1572
1606
  default: throw new SerovalUnsupportedTypeError(current);
1573
1607
  }
@@ -2403,10 +2437,10 @@ function serializeNullConstructor(ctx, node) {
2403
2437
  return serializeDictionary(ctx, node, NULL_CONSTRUCTOR);
2404
2438
  }
2405
2439
  function serializeDate(node) {
2406
- return "new Date(\"" + node.s + "\")";
2440
+ return "new Date(\"" + serializeString(node.s) + "\")";
2407
2441
  }
2408
2442
  function serializeTemporal(ctx, node) {
2409
- if (ctx.base.features & 64) return "Temporal." + TEMPORAL_TYPE_NAME[node.c] + ".from(\"" + node.s + "\")";
2443
+ if (ctx.base.features & 64) return "Temporal." + TEMPORAL_TYPE_NAME[node.c] + ".from(\"" + serializeString(node.s) + "\")";
2410
2444
  throw new SerovalUnsupportedNodeError(node);
2411
2445
  }
2412
2446
  function serializeRegExp(ctx, node) {
@@ -2520,7 +2554,7 @@ function serializeArrayBuffer(ctx, node) {
2520
2554
  return getConstructor(ctx, node.f) + "(\"" + node.s + "\")";
2521
2555
  }
2522
2556
  function serializeView(ctx, node) {
2523
- return "new " + (node.t === 20 ? "DataView" : node.c) + "(" + serialize$1(ctx, node.f) + "," + node.b + "," + node.l + ")";
2557
+ return "new " + (node.t === 20 ? "DataView" : node.c) + "(" + serialize$1(ctx, node.f) + "," + +node.b + "," + +node.l + ")";
2524
2558
  }
2525
2559
  function serializeAggregateError(ctx, node) {
2526
2560
  const id = node.i;
@@ -2639,7 +2673,7 @@ function serializeSequence(ctx, node) {
2639
2673
  result += (item && result && ",") + item;
2640
2674
  }
2641
2675
  ctx.base.stack.pop();
2642
- if (result) return "{__SEROVAL_SEQUENCE__:!0,v:[" + result + "],t:" + node.s + ",d:" + node.l + "}";
2676
+ if (result) return "{__SEROVAL_SEQUENCE__:!0,v:[" + result + "],t:" + +node.s + ",d:" + +node.l + "}";
2643
2677
  }
2644
2678
  return "{__SEROVAL_SEQUENCE__:!0,v:[],t:-1,d:0}";
2645
2679
  }
@@ -2812,8 +2846,8 @@ function parseBoxed(ctx, depth, id, current) {
2812
2846
  return createBoxedNode(id, parseSOS(ctx, depth, current.valueOf()));
2813
2847
  }
2814
2848
  function parseView(ctx, depth, type, id, current) {
2815
- current = getArrayBufferView(ctx.base, current);
2816
- return createViewNode(type, id, current, parseSOS(ctx, depth, current.buffer));
2849
+ const view = getArrayBufferView(ctx.base, readArrayBufferView(current, type === 20));
2850
+ return createViewNode(type, id, current, view, parseSOS(ctx, depth, view.buffer));
2817
2851
  }
2818
2852
  function parseError(ctx, depth, type, id, current) {
2819
2853
  const options = getErrorOptions(current, ctx.base.features);
@@ -2904,14 +2938,15 @@ function parseObjectPhase2(ctx, depth, id, current, currentClass) {
2904
2938
  case 12: return parseSet(ctx, depth, id, current);
2905
2939
  case 13: return parsePromise(ctx, depth, id, current);
2906
2940
  case 14: return createRegExpNode(id, current);
2907
- case 15: return createTemporalNode(id, getTemporalType(currentClass), current);
2941
+ case 15: return createTemporalNode(id, getTemporalType(currentClass), currentClass, current);
2908
2942
  case 16: return parsePlainObject(ctx, depth, id, current, !!currentClass);
2909
2943
  default: throw new SerovalUnsupportedTypeError(current);
2910
2944
  }
2911
2945
  }
2912
2946
  function parseObject(ctx, depth, id, current) {
2913
2947
  if (Array.isArray(current)) return parseArray(ctx, depth, id, current);
2914
- if (isStream(current)) return isLiveStream(current) ? parseLiveStream(ctx, depth, id, current) : parseStream(ctx, depth, id, current);
2948
+ if (isLiveStream(current)) return parseLiveStream(ctx, depth, id, current);
2949
+ if (current instanceof Stream) return parseStream(ctx, depth, id, current);
2915
2950
  if (isSequence(current)) return parseSequence(ctx, depth, id, current);
2916
2951
  const currentClass = getObjectClass(current);
2917
2952
  if (currentClass === OpaqueReference) return parseSOS(ctx, depth, current.replacement);
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "seroval",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "1.7.5",
5
+ "version": "1.7.7",
6
6
  "files": [
7
7
  "dist"
8
8
  ],
@@ -20,6 +20,7 @@
20
20
  "@types/node": "^26.6.5",
21
21
  "@vitest/ui": "^5.0.5",
22
22
  "esbuild": "^0.28.2",
23
+ "fast-check": "^4.10.2",
23
24
  "temporal-polyfill": "^1.0.6",
24
25
  "ts-prune": "^0.10.3",
25
26
  "tsdown": "^0.23.1",
@@ -72,6 +73,7 @@
72
73
  "watch": "tsdown --watch",
73
74
  "test": "vitest",
74
75
  "test:ui": "vitest --ui",
75
- "prune": "ts-prune"
76
+ "prune": "ts-prune",
77
+ "fuzz": "vitest run --config vitest.fuzz.config.ts"
76
78
  }
77
79
  }