seroval 1.6.12 → 1.7.1

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.d.cts CHANGED
@@ -1,11 +1,14 @@
1
1
  //#region src/core/compat.d.ts
2
2
  /**
3
- * References
4
- * - https://compat-table.github.io/compat-table/es6/
5
- * - MDN
3
+ * Public feature bits for `disabledFeatures`. Stays a runtime `enum` so the
4
+ * published declaration is unchanged for consumers. Its values must match
5
+ * the internal bits (checked by test/feature.test.ts).
6
6
  */
7
- declare enum Feature {
7
+ export declare enum Feature {
8
8
  AggregateError = 1,
9
+ /**
10
+ * @deprecated Has no effect. Output always uses arrow functions.
11
+ */
9
12
  ArrowFunction = 2,
10
13
  ErrorPrototypeStack = 4,
11
14
  ObjectAssign = 8,
@@ -323,6 +326,7 @@ interface SerovalStreamConstructorNode extends SerovalBaseNode {
323
326
  i: number;
324
327
  a: SerovalNode[];
325
328
  f: SerovalNodeWithID;
329
+ l: 1 | undefined;
326
330
  }
327
331
  interface SerovalStreamNextNode extends SerovalBaseNode {
328
332
  t: SerovalNodeType.StreamNext;
@@ -362,185 +366,189 @@ interface BaseDeserializerContextOptions extends PluginAccessOptions {
362
366
  /** Maximum encoded characters per ArrayBuffer. Defaults to 1,000,000. */
363
367
  maxBase64Length?: number;
364
368
  }
365
- interface BaseDeserializerContext extends PluginAccessOptions {
366
- readonly mode: SerovalMode;
367
- /**
368
- * Mapping ids to values
369
- */
370
- refs: Map<number, unknown> & {
371
- types: Map<number, SerovalNodeType>;
372
- };
373
- features: number;
374
- depthLimit: number;
375
- maxBase64Length: number;
376
- }
377
369
  interface VanillaDeserializerContextOptions extends Omit<BaseDeserializerContextOptions, 'refs'> {
378
370
  markedRefs: number[] | Set<number>;
379
371
  }
380
- interface VanillaDeserializerState {
381
- marked: Set<number>;
382
- }
383
- interface VanillaDeserializerContext {
384
- mode: SerovalMode.Vanilla;
385
- base: BaseDeserializerContext;
386
- child: DeserializePluginContext | undefined;
387
- state: VanillaDeserializerState;
388
- }
389
- interface CrossDeserializerContext {
390
- mode: SerovalMode.Cross;
391
- base: BaseDeserializerContext;
392
- child: DeserializePluginContext | undefined;
393
- }
394
372
  type CrossDeserializerContextOptions = BaseDeserializerContextOptions;
395
- type DeserializerContext = VanillaDeserializerContext | CrossDeserializerContext;
396
- declare class DeserializePluginContext {
397
- private _p;
398
- private depth;
399
- constructor(_p: DeserializerContext, depth: number);
373
+ interface DeserializePluginContext {
400
374
  deserialize<T>(node: SerovalNode): T;
401
375
  }
402
376
  //#endregion
403
377
  //#region src/core/context/serializer.d.ts
404
- declare const enum AssignmentType {
405
- Index = 0,
406
- Add = 1,
407
- Set = 2,
408
- Delete = 3,
409
- Define = 4
410
- }
411
- interface IndexAssignment {
412
- t: AssignmentType.Index;
413
- s: string;
414
- k: undefined;
415
- v: string;
416
- }
417
- interface SetAssignment {
418
- t: AssignmentType.Set;
419
- s: string;
420
- k: string;
421
- v: string;
422
- }
423
- interface AddAssignment {
424
- t: AssignmentType.Add;
425
- s: string;
426
- k: undefined;
427
- v: string;
428
- }
429
- interface DeleteAssignment {
430
- t: AssignmentType.Delete;
431
- s: string;
432
- k: string;
433
- v: undefined;
434
- }
435
- interface DefineAssignment {
436
- t: AssignmentType.Define;
437
- s: string;
438
- k: string;
439
- v: undefined;
440
- }
441
- type Assignment = IndexAssignment | AddAssignment | SetAssignment | DeleteAssignment | DefineAssignment;
442
- interface FlaggedObject {
443
- type: SerovalObjectFlags;
444
- value: string;
445
- }
446
378
  interface BaseSerializerContextOptions extends PluginAccessOptions {
447
379
  features: number;
448
380
  markedRefs: number[] | Set<number>;
449
- }
450
- interface BaseSerializerContext extends PluginAccessOptions {
451
- readonly mode: SerovalMode;
452
- features: number;
453
- stack: number[];
454
- /**
455
- * Array of object mutations
456
- */
457
- flags: FlaggedObject[];
458
- /**
459
- * Array of assignments to be done (used for recursion)
460
- */
461
- assignments: Assignment[];
462
- /**
463
- * Refs that are...referenced
464
- */
465
- marked: Set<number>;
381
+ assignSharedRefsOnly?: boolean;
466
382
  }
467
383
  interface CrossContextOptions {
468
384
  scopeId?: string;
469
385
  }
470
- interface VanillaSerializerState {
471
- valid: Map<number, number>;
472
- vars: string[];
473
- }
474
- interface VanillaSerializerContext {
475
- mode: SerovalMode.Vanilla;
476
- base: BaseSerializerContext;
477
- state: VanillaSerializerState;
478
- child: SerializePluginContext | undefined;
479
- }
480
386
  type VanillaSerializerContextOptions = BaseSerializerContextOptions;
481
- interface CrossSerializerContext {
482
- mode: SerovalMode.Cross;
483
- base: BaseSerializerContext;
484
- state: CrossContextOptions;
485
- child: SerializePluginContext | undefined;
486
- }
487
387
  interface CrossSerializerContextOptions extends BaseSerializerContextOptions, CrossContextOptions {}
488
- type SerializerContext = VanillaSerializerContext | CrossSerializerContext;
489
- declare class SerializePluginContext {
490
- private _p;
491
- constructor(_p: SerializerContext);
388
+ interface SerializePluginContext {
492
389
  serialize(node: SerovalNode): string;
493
390
  }
494
391
  //#endregion
495
- //#region src/core/context/parser.d.ts
496
- interface BaseParserContextOptions extends PluginAccessOptions {
497
- disabledFeatures?: number;
498
- refs?: Map<unknown, number>;
499
- depthLimit?: number;
500
- /** Copy each typed array or DataView's visible bytes into its own buffer. */
501
- compactArrayBufferViews?: boolean;
392
+ //#region src/core/stream.d.ts
393
+ interface StreamListener<T> {
394
+ next(value: T): void;
395
+ throw(value: unknown): void;
396
+ return(value: T): void;
502
397
  }
503
- interface BaseParserContext extends PluginAccessOptions {
504
- readonly mode: SerovalMode;
505
- marked: Set<number>;
506
- refs: Map<unknown, number>;
507
- features: number;
508
- depthLimit: number;
509
- compactArrayBufferViews: boolean;
398
+ /**
399
+ * An internal class rather than a tagged POJO: identity is checked with
400
+ * `instanceof`, which untrusted input cannot forge. The class is exported
401
+ * because bundlers cannot drop an unexported top-level class.
402
+ *
403
+ * The behavior is intentionally duplicated from `STREAM_CONSTRUCTOR`. That
404
+ * constructor's source is embedded verbatim into the eval-based `deserialize`
405
+ * output, which has no access to this class, so the two cannot be shared. A
406
+ * stream read back through `deserialize` is therefore a plain POJO and, by
407
+ * design, is not treated as a genuine Stream on re-serialization. Keep the two
408
+ * implementations in sync.
409
+ */
410
+ export declare class Stream<T> {
411
+ private buffer;
412
+ private listeners;
413
+ private alive;
414
+ private success;
415
+ private count;
416
+ private live;
417
+ constructor(live?: number);
418
+ private flush;
419
+ private replay;
420
+ on(listener: StreamListener<T>): () => void;
421
+ /**
422
+ * Returns the events of a stream that has ended, without subscribing, so a
423
+ * live receiver is not consumed. The last value is the `return` or `throw`
424
+ * value. Returns `undefined` while the stream is open, or when a live
425
+ * receiver was already consumed and has no events left.
426
+ * @internal
427
+ */
428
+ endedEvents(): {
429
+ values: unknown[];
430
+ success: boolean;
431
+ } | undefined;
432
+ next(value: T): void;
433
+ throw(value: unknown): void;
434
+ return(value: T): void;
510
435
  }
436
+ export declare function isStream<T>(value: unknown): value is Stream<T> | LiveStream<T>;
437
+ export declare function createStream<T>(): Stream<T>;
438
+ //#endregion
439
+ //#region src/core/live-stream.d.ts
440
+ type LiveStreamEvent<T> = {
441
+ type: 'next';
442
+ value: T;
443
+ } | {
444
+ type: 'return';
445
+ value: T;
446
+ } | {
447
+ type: 'throw';
448
+ error: unknown;
449
+ };
450
+ interface LiveStreamDelivery<T> {
451
+ event: LiveStreamEvent<T>;
452
+ /**
453
+ * Release the event and resolve the producer operation that sent it.
454
+ * Call it only after the serialized record for the event has been
455
+ * accepted by its destination. Late or repeated calls are ignored.
456
+ */
457
+ accept(): void;
458
+ }
459
+ interface LiveStreamConsumer<T> {
460
+ read(): Promise<LiveStreamDelivery<T>>;
461
+ cancel(reason?: unknown): void;
462
+ }
463
+ interface LiveStreamProducer<T> {
464
+ write(value: T): Promise<void>;
465
+ close(value: T): Promise<void>;
466
+ fail(error: unknown): Promise<void>;
467
+ }
468
+ /**
469
+ * Receives events from `LiveStream.pump`. Each handler gets the event value
470
+ * and the accept function for it, and returns `undefined` to keep going or
471
+ * the error that stopped the sink. `error` runs if the stream is cancelled
472
+ * while a read is pending.
473
+ */
474
+ interface LiveStreamSink<T> {
475
+ next(value: T, accept: () => void): unknown;
476
+ throw(value: unknown, accept: () => void): unknown;
477
+ return(value: T, accept: () => void): unknown;
478
+ /** Runs once after the terminal event, or after a handler failed. */
479
+ done?(): void;
480
+ /** Runs if the stream is cancelled while a read is pending. */
481
+ error?(reason: unknown): void;
482
+ }
483
+ interface LiveStreamOptions {
484
+ onCancel?: (reason: unknown) => void;
485
+ }
486
+ export declare function isLiveStream<T>(value: unknown): value is LiveStream<T>;
487
+ /**
488
+ * A single-consumer stream for incremental serialization. Unlike
489
+ * `createStream`, it keeps no history: at most one event is held, and the
490
+ * producer operation that sent it settles only when the consumer accepts it.
491
+ *
492
+ * An internal class rather than a tagged POJO: identity is checked with
493
+ * `instanceof`, which untrusted input cannot forge. The class is exported
494
+ * because bundlers cannot drop an unexported top-level class.
495
+ */
496
+ export declare class LiveStream<T> {
497
+ private pending;
498
+ private waiting;
499
+ private consumed;
500
+ private closed;
501
+ private cancelled;
502
+ private cancelReason;
503
+ private options;
504
+ constructor(options?: LiveStreamOptions);
505
+ private deliver;
506
+ /** @internal */
507
+ push(event: LiveStreamEvent<T>): Promise<void>;
508
+ private cancel;
509
+ private read;
510
+ consume(): LiveStreamConsumer<T>;
511
+ /**
512
+ * Consumes the stream through a replay-style listener, accepting each
513
+ * event as soon as it has been delivered. Use `consume` or `pump` when
514
+ * acceptance must wait for processing.
515
+ */
516
+ on(listener: StreamListener<T>): () => void;
517
+ /**
518
+ * Consumes the stream into a sink that controls acceptance. Returns the
519
+ * cancel function.
520
+ * @internal
521
+ */
522
+ pump(sink: LiveStreamSink<T>): (reason?: unknown) => void;
523
+ }
524
+ /**
525
+ * Creates a live stream and the producer that writes to it.
526
+ */
527
+ export declare function createLiveStream<T>(options?: LiveStreamOptions): {
528
+ stream: LiveStream<T>;
529
+ producer: LiveStreamProducer<T>;
530
+ };
511
531
  //#endregion
512
532
  //#region src/core/context/sync-parser.d.ts
513
533
  type SyncParserContextOptions = BaseParserContextOptions;
514
- declare const enum ParserMode {
515
- Sync = 1,
516
- Stream = 2
517
- }
518
- interface SyncParserContext {
519
- type: ParserMode.Sync;
520
- base: BaseParserContext;
521
- child: SyncParsePluginContext | undefined;
522
- }
523
- declare class SyncParsePluginContext {
524
- private _p;
525
- private depth;
526
- constructor(_p: SyncParserContext, depth: number);
534
+ interface SyncParsePluginContext {
527
535
  parse<T>(current: T): SerovalNode;
528
536
  }
529
537
  interface StreamParserContextOptions extends SyncParserContextOptions {
530
- onParse: (node: SerovalNode, initial: boolean) => void;
538
+ /**
539
+ * Receives each parsed record. Returning a promise defers the next record,
540
+ * and the acceptance of the source event that produced it, until the
541
+ * promise settles.
542
+ */
543
+ onParse: (node: SerovalNode, initial: boolean) => void | PromiseLike<void>;
531
544
  onError?: (error: unknown) => void;
532
545
  onDone?: () => void;
533
546
  }
534
- interface StreamParserContext {
535
- type: ParserMode.Stream;
536
- base: BaseParserContext;
537
- state: StreamParserState;
538
- }
539
- declare class StreamParsePluginContext {
540
- private _p;
541
- private depth;
542
- constructor(_p: StreamParserContext, depth: number);
547
+ //#endregion
548
+ //#region src/core/context/stream-parser.d.ts
549
+ interface StreamParsePluginContext {
543
550
  parse<T>(current: T): SerovalNode;
551
+ parseStreamSource(current: AsyncIterable<unknown>): SerovalNode;
544
552
  parseWithError<T>(current: T): SerovalNode | undefined;
545
553
  isAlive(): boolean;
546
554
  pushPendingState(): void;
@@ -549,29 +557,19 @@ declare class StreamParsePluginContext {
549
557
  onError(error: unknown): void;
550
558
  addCleanup(callback: () => void): void;
551
559
  }
552
- interface StreamParserState {
553
- alive: boolean;
554
- pending: number;
555
- initial: boolean;
556
- buffer: SerovalNode[];
557
- onParse: (node: SerovalNode, initial: boolean) => void;
558
- onError?: (error: unknown) => void;
559
- onDone?: () => void;
560
- cleanups: (() => void)[];
561
- }
562
560
  //#endregion
563
561
  //#region src/core/plugin.d.ts
564
- declare const enum SerovalMode {
562
+ export declare const enum SerovalMode {
565
563
  Vanilla = 1,
566
564
  Cross = 2
567
565
  }
568
- interface PluginData {
566
+ export interface PluginData {
569
567
  id: number;
570
568
  }
571
- type PluginInfo = {
569
+ export type PluginInfo = {
572
570
  [key: string]: SerovalNode;
573
571
  };
574
- interface Plugin<Value, Info extends PluginInfo> {
572
+ export interface Plugin<Value, Info extends PluginInfo> {
575
573
  /**
576
574
  * A unique string that helps idenfity the plugin
577
575
  */
@@ -585,6 +583,13 @@ interface Plugin<Value, Info extends PluginInfo> {
585
583
  * @param value
586
584
  */
587
585
  test(value: unknown): boolean;
586
+ /**
587
+ * Set to `true` if `test` never returns `true` for a plain object, whose
588
+ * prototype is `Object.prototype`, such as an object literal. The parsers
589
+ * then skip this plugin for those objects. Leave it unset if unsure. A
590
+ * plugin that sets it is never applied to such objects.
591
+ */
592
+ skipPlainObjects?: boolean;
588
593
  /**
589
594
  * Parsing modes
590
595
  */
@@ -602,105 +607,149 @@ interface Plugin<Value, Info extends PluginInfo> {
602
607
  */
603
608
  deserialize(node: Info, ctx: DeserializePluginContext, data: PluginData): Value;
604
609
  }
605
- declare function createPlugin<Value, Info extends PluginInfo>(plugin: Plugin<Value, Info>): Plugin<Value, Info>;
606
- interface PluginAccessOptions {
610
+ export declare function createPlugin<Value, Info extends PluginInfo>(plugin: Plugin<Value, Info>): Plugin<Value, Info>;
611
+ export interface PluginAccessOptions {
607
612
  plugins?: Plugin<any, any>[];
608
613
  }
609
- declare function resolvePlugins(plugins?: Plugin<any, any>[]): Plugin<any, any>[] | undefined;
614
+ export declare function resolvePlugins(plugins?: Plugin<any, any>[]): Plugin<any, any>[] | undefined;
615
+ /**
616
+ * Returns the plugins that are tested for plain objects. It is the same array
617
+ * when no plugin sets `skipPlainObjects`.
618
+ */
619
+ export declare function getPlainObjectPlugins(plugins: Plugin<any, any>[] | undefined): Plugin<any, any>[] | undefined;
620
+ //#endregion
621
+ //#region src/core/context/parser.d.ts
622
+ interface BaseParserContextOptions extends PluginAccessOptions {
623
+ disabledFeatures?: number;
624
+ refs?: Map<unknown, number>;
625
+ depthLimit?: number;
626
+ /** Copy each typed array or DataView's visible bytes into its own buffer. */
627
+ compactArrayBufferViews?: boolean;
628
+ }
610
629
  //#endregion
611
630
  //#region src/core/context/async-parser.d.ts
612
631
  type AsyncParserContextOptions = BaseParserContextOptions;
613
- interface AsyncParserContext {
614
- base: BaseParserContext;
615
- child: AsyncParsePluginContext | undefined;
616
- }
617
- declare class AsyncParsePluginContext {
618
- private _p;
619
- private depth;
620
- constructor(_p: AsyncParserContext, depth: number);
632
+ interface AsyncParsePluginContext {
621
633
  parse<T>(current: T): Promise<SerovalNode>;
634
+ parseStreamSource(current: AsyncIterable<unknown>): Promise<SerovalNode>;
622
635
  }
623
636
  //#endregion
624
637
  //#region src/core/cross/index.d.ts
625
- interface CrossSerializeOptions extends SyncParserContextOptions, CrossContextOptions {}
626
- declare function crossSerialize<T>(source: T, options?: CrossSerializeOptions): string;
627
- interface CrossSerializeAsyncOptions extends AsyncParserContextOptions, CrossContextOptions {}
628
- declare function crossSerializeAsync<T>(source: T, options?: CrossSerializeAsyncOptions): Promise<string>;
629
- type ToCrossJSONOptions = SyncParserContextOptions;
630
- declare function toCrossJSON<T>(source: T, options?: ToCrossJSONOptions): SerovalNode;
631
- type ToCrossJSONAsyncOptions = AsyncParserContextOptions;
632
- declare function toCrossJSONAsync<T>(source: T, options?: ToCrossJSONAsyncOptions): Promise<SerovalNode>;
633
- interface CrossSerializeStreamOptions extends Omit<StreamParserContextOptions, 'onParse'>, CrossContextOptions {
634
- onSerialize: (data: string, initial: boolean) => void;
635
- }
636
- declare function crossSerializeStream<T>(source: T, options: CrossSerializeStreamOptions): () => void;
637
- type ToCrossJSONStreamOptions = StreamParserContextOptions;
638
- declare function toCrossJSONStream<T>(source: T, options: ToCrossJSONStreamOptions): () => void;
639
- type FromCrossJSONOptions = CrossDeserializerContextOptions;
640
- declare function fromCrossJSON<T>(source: SerovalNode, options: FromCrossJSONOptions): T;
638
+ export interface CrossAssignmentOptions {
639
+ /**
640
+ * Only assign `$R[n]` for values that the output reads more than once,
641
+ * such as shared references and cycles. The output is shorter, but later
642
+ * scripts cannot read the other values from `$R`. Only use it when no
643
+ * later output that shares the same references refers back to this one.
644
+ */
645
+ assignSharedRefsOnly?: boolean;
646
+ }
647
+ export interface CrossSerializeOptions extends SyncParserContextOptions, CrossContextOptions, CrossAssignmentOptions {}
648
+ export declare function crossSerialize<T>(source: T, options?: CrossSerializeOptions): string;
649
+ export interface CrossSerializeAsyncOptions extends AsyncParserContextOptions, CrossContextOptions, CrossAssignmentOptions {}
650
+ export declare function crossSerializeAsync<T>(source: T, options?: CrossSerializeAsyncOptions): Promise<string>;
651
+ export type ToCrossJSONOptions = SyncParserContextOptions;
652
+ export declare function toCrossJSON<T>(source: T, options?: ToCrossJSONOptions): SerovalNode;
653
+ export type ToCrossJSONAsyncOptions = AsyncParserContextOptions;
654
+ export declare function toCrossJSONAsync<T>(source: T, options?: ToCrossJSONAsyncOptions): Promise<SerovalNode>;
655
+ export interface CrossSerializeStreamOptions extends Omit<StreamParserContextOptions, 'onParse'>, CrossContextOptions {
656
+ /**
657
+ * Receives each serialized record. Returning a promise defers the next
658
+ * record, and the acceptance of the live stream event behind this one,
659
+ * until the promise settles.
660
+ */
661
+ onSerialize: (data: string, initial: boolean) => void | PromiseLike<void>;
662
+ }
663
+ export declare function crossSerializeStream<T>(source: T, options: CrossSerializeStreamOptions): (reason?: unknown) => void;
664
+ export type ToCrossJSONStreamOptions = StreamParserContextOptions;
665
+ export declare function toCrossJSONStream<T>(source: T, options: ToCrossJSONStreamOptions): (reason?: unknown) => void;
666
+ export type FromCrossJSONOptions = CrossDeserializerContextOptions;
667
+ export declare function fromCrossJSON<T>(source: SerovalNode, options: FromCrossJSONOptions): T;
668
+ export interface CrossDeserializer {
669
+ /** Deserializes one streamed record. Records share references. */
670
+ deserialize<T>(node: SerovalNode): T;
671
+ /** Deferred values (promises, streams) created but not yet settled. */
672
+ readonly pending: number;
673
+ /** Rejects pending promises and throws into open streams; idempotent. */
674
+ abort(reason: unknown): void;
675
+ }
676
+ export type CrossDeserializerOptions = FromCrossJSONOptions;
677
+ export declare function createCrossDeserializer(options: CrossDeserializerOptions): CrossDeserializer;
641
678
  //#endregion
642
679
  //#region src/core/errors.d.ts
643
- declare class SerovalError extends Error {
680
+ export declare class SerovalError extends Error {
644
681
  cause: unknown;
645
682
  constructor(type: string, cause: unknown);
646
683
  }
647
- declare class SerovalParserError extends SerovalError {
684
+ export declare class SerovalParserError extends SerovalError {
648
685
  constructor(cause: unknown);
649
686
  }
650
- declare class SerovalSerializationError extends SerovalError {
687
+ export declare class SerovalSerializationError extends SerovalError {
651
688
  constructor(cause: unknown);
652
689
  }
653
- declare class SerovalDeserializationError extends SerovalError {
690
+ export declare class SerovalDeserializationError extends SerovalError {
654
691
  constructor(cause: unknown);
655
692
  }
656
- declare class SerovalUnsupportedTypeError extends Error {
693
+ export declare class SerovalUnsupportedTypeError extends Error {
657
694
  value: unknown;
658
695
  constructor(value: unknown);
659
696
  }
660
- declare class SerovalUnsupportedNodeError extends Error {
697
+ export declare class SerovalUnsupportedNodeError extends Error {
661
698
  constructor(node: SerovalNode);
662
699
  }
663
- declare class SerovalMissingPluginError extends Error {
700
+ export declare class SerovalMissingPluginError extends Error {
664
701
  constructor(tag: string);
665
702
  }
666
- declare class SerovalMissingInstanceError extends Error {
703
+ export declare class SerovalMissingInstanceError extends Error {
667
704
  constructor(tag: string);
668
705
  }
669
- declare class SerovalMissingReferenceError extends Error {
706
+ /**
707
+ * @deprecated Not thrown by seroval. A missing `createReference` key throws
708
+ * {@link SerovalMissingReferenceForIdError} instead.
709
+ */
710
+ export declare class SerovalMissingReferenceError extends Error {
670
711
  value: unknown;
671
712
  constructor(value: unknown);
672
713
  }
673
- declare class SerovalMissingReferenceForIdError extends Error {
714
+ export declare class SerovalMissingReferenceForIdError extends Error {
674
715
  constructor(id: string);
675
716
  }
676
- declare class SerovalUnknownTypedArrayError extends Error {
717
+ export declare class SerovalUnknownTypedArrayError extends Error {
677
718
  constructor(name: string);
678
719
  }
679
- declare class SerovalMalformedNodeError extends Error {
720
+ export declare class SerovalMalformedNodeError extends Error {
680
721
  constructor(node: SerovalNode);
681
722
  }
682
- declare class SerovalConflictedNodeIdError extends Error {
723
+ export declare class SerovalConflictedNodeIdError extends Error {
683
724
  constructor(node: SerovalNode);
684
725
  }
685
- declare class SerovalDepthLimitError extends Error {
726
+ export declare class SerovalDepthLimitError extends Error {
686
727
  constructor(limit: number);
687
728
  }
729
+ export declare class SerovalAbortedError extends Error {
730
+ constructor();
731
+ }
732
+ export type LiveStreamErrorReason = 'consumed' | 'pending' | 'reading' | 'closed';
733
+ export declare class SerovalLiveStreamError extends Error {
734
+ reason: LiveStreamErrorReason;
735
+ constructor(reason: LiveStreamErrorReason);
736
+ }
688
737
  //#endregion
689
738
  //#region src/core/keys.d.ts
690
- declare function getCrossReferenceHeader(id?: string): string;
739
+ export declare function getCrossReferenceHeader(id?: string): string;
691
740
  //#endregion
692
741
  //#region src/core/opaque-reference.d.ts
693
742
  /**
694
743
  * An opaque reference allows hiding values from the serializer.
695
744
  */
696
- declare class OpaqueReference<V, R = undefined> {
745
+ export declare class OpaqueReference<V, R = undefined> {
697
746
  readonly value: V;
698
747
  readonly replacement?: R | undefined;
699
748
  constructor(value: V, replacement?: R | undefined);
700
749
  }
701
750
  //#endregion
702
751
  //#region src/core/reference.d.ts
703
- declare function createReference<T>(id: string, value: T): T;
752
+ export declare function createReference<T>(id: string, value: T): T;
704
753
  //#endregion
705
754
  //#region src/core/Serializer.d.ts
706
755
  interface SerializerOptions extends PluginAccessOptions {
@@ -708,79 +757,75 @@ interface SerializerOptions extends PluginAccessOptions {
708
757
  scopeId?: string;
709
758
  disabledFeatures?: number;
710
759
  compactArrayBufferViews?: boolean;
760
+ depthLimit?: number;
711
761
  onData: (result: string) => void;
712
762
  onError: (error: unknown) => void;
713
763
  onDone?: () => void;
714
764
  }
715
765
  declare class Serializer {
716
- private options;
717
766
  private alive;
718
767
  private flushed;
719
768
  private done;
720
769
  private pending;
721
770
  private cleanups;
722
- private refs;
771
+ private readonly refs;
723
772
  private plugins?;
773
+ private options;
724
774
  constructor(options: SerializerOptions);
725
775
  keys: Set<string>;
726
776
  write(key: string, value: unknown): void;
777
+ private finishWrite;
727
778
  ids: number;
728
779
  private getNextID;
729
780
  push(value: unknown): string;
730
781
  flush(): void;
731
782
  close(): void;
783
+ private finish;
732
784
  }
733
785
  //#endregion
734
- //#region src/core/stream.d.ts
735
- interface StreamListener<T> {
736
- next(value: T): void;
737
- throw(value: unknown): void;
738
- return(value: T): void;
739
- }
786
+ //#region src/core/sequence.d.ts
740
787
  /**
741
788
  * An internal class rather than a tagged POJO: identity is checked with
742
- * `instanceof`, which untrusted input cannot forge (the class is not exported).
743
- *
744
- * The behavior is intentionally duplicated from `STREAM_CONSTRUCTOR`. That
745
- * constructor's source is embedded verbatim into the eval-based `deserialize`
746
- * output, which has no access to this class, so the two cannot be shared. A
747
- * stream read back through `deserialize` is therefore a plain POJO and, by
748
- * design, is not treated as a genuine Stream on re-serialization. Keep the two
749
- * implementations in sync.
789
+ * `instanceof`, which untrusted input cannot forge. The class is exported
790
+ * because bundlers cannot drop an unexported top-level class.
791
+ * The eval-based `deserialize` path still rebuilds a `{__SEROVAL_SEQUENCE__…}`
792
+ * POJO from embedded source - it has no access to this class - so a value read
793
+ * back through `deserialize` is not an instance and, by design, is not treated
794
+ * as a genuine Sequence on re-serialization.
750
795
  */
751
- declare class Stream<T> {
752
- private buffer;
753
- private listeners;
754
- private alive;
755
- private success;
756
- private count;
757
- private flush;
758
- private replay;
759
- on(listener: StreamListener<T>): () => void;
760
- next(value: T): void;
761
- throw(value: unknown): void;
762
- return(value: T): void;
796
+ export declare class Sequence {
797
+ v: unknown[];
798
+ t: number;
799
+ d: number;
800
+ constructor(values: unknown[], throwAt: number, doneAt: number);
763
801
  }
764
- declare function isStream<T>(value: unknown): value is Stream<T>;
765
- declare function createStream<T>(): Stream<T>;
766
802
  //#endregion
767
803
  //#region src/core/tree/index.d.ts
768
- type SyncParserContextOptions$1 = Omit<BaseParserContextOptions, 'refs'>;
769
- type AsyncParserContextOptions$1 = Omit<BaseParserContextOptions, 'refs'>;
770
- declare function serialize<T>(source: T, options?: SyncParserContextOptions$1): string;
771
- declare function serializeAsync<T>(source: T, options?: AsyncParserContextOptions$1): Promise<string>;
772
- declare function deserialize<T>(source: string): T;
773
- interface SerovalJSON {
804
+ /**
805
+ * Options for `serialize`. The same as the parser options, without `refs`:
806
+ * the output is self-contained, so there is nothing to share between calls.
807
+ */
808
+ export type SerializeOptions = Omit<BaseParserContextOptions, 'refs'>;
809
+ /** Options for `serializeAsync`. */
810
+ export type SerializeAsyncOptions = Omit<BaseParserContextOptions, 'refs'>;
811
+ /** Options for `toJSON`. */
812
+ export type ToJSONOptions = SerializeOptions;
813
+ /** Options for `toJSONAsync`. */
814
+ export type ToJSONAsyncOptions = SerializeAsyncOptions;
815
+ export declare function serialize<T>(source: T, options?: SerializeOptions): string;
816
+ export declare function serializeAsync<T>(source: T, options?: SerializeAsyncOptions): Promise<string>;
817
+ export declare function deserialize<T>(source: string): T;
818
+ export interface SerovalJSON {
774
819
  t: SerovalNode;
775
820
  f: number;
776
821
  m: number[];
777
822
  }
778
- interface FromJSONOptions extends PluginAccessOptions, Pick<BaseDeserializerContextOptions, 'maxBase64Length'> {
823
+ export interface FromJSONOptions extends PluginAccessOptions, Pick<BaseDeserializerContextOptions, 'depthLimit' | 'maxBase64Length'> {
779
824
  disabledFeatures?: number;
780
825
  }
781
- declare function toJSON<T>(source: T, options?: SyncParserContextOptions$1): SerovalJSON;
782
- declare function toJSONAsync<T>(source: T, options?: AsyncParserContextOptions$1): Promise<SerovalJSON>;
783
- declare function compileJSON(source: SerovalJSON, options?: PluginAccessOptions): string;
784
- declare function fromJSON<T>(source: SerovalJSON, options?: FromJSONOptions): T;
826
+ export declare function toJSON<T>(source: T, options?: ToJSONOptions): SerovalJSON;
827
+ export declare function toJSONAsync<T>(source: T, options?: ToJSONAsyncOptions): Promise<SerovalJSON>;
828
+ export declare function compileJSON(source: SerovalJSON, options?: PluginAccessOptions): string;
829
+ export declare function fromJSON<T>(source: SerovalJSON, options?: FromJSONOptions): T;
785
830
  //#endregion
786
- export { type AsyncParsePluginContext, AsyncParserContextOptions, type BaseDeserializerContextOptions, type BaseParserContextOptions, type BaseSerializerContextOptions, type CrossContextOptions, type CrossDeserializerContextOptions, CrossSerializeAsyncOptions, CrossSerializeOptions, CrossSerializeStreamOptions, type CrossSerializerContextOptions, type DeserializePluginContext, Feature, FromCrossJSONOptions, FromJSONOptions, OpaqueReference, Plugin, PluginAccessOptions, PluginData, PluginInfo, type SerializePluginContext, Serializer, SerovalConflictedNodeIdError, SerovalDepthLimitError, SerovalDeserializationError, SerovalError, SerovalJSON, SerovalMalformedNodeError, SerovalMissingInstanceError, SerovalMissingPluginError, SerovalMissingReferenceError, SerovalMissingReferenceForIdError, SerovalMode, type SerovalNode, SerovalParserError, SerovalSerializationError, SerovalUnknownTypedArrayError, SerovalUnsupportedNodeError, SerovalUnsupportedTypeError, type Stream, type StreamListener, type StreamParsePluginContext, type StreamParserContextOptions, type SyncParsePluginContext, SyncParserContextOptions, ToCrossJSONAsyncOptions, ToCrossJSONOptions, ToCrossJSONStreamOptions, type VanillaDeserializerContextOptions, type VanillaSerializerContextOptions, compileJSON, createPlugin, createReference, createStream, crossSerialize, crossSerializeAsync, crossSerializeStream, deserialize, fromCrossJSON, fromJSON, getCrossReferenceHeader, isStream, resolvePlugins, serialize, serializeAsync, toCrossJSON, toCrossJSONAsync, toCrossJSONStream, toJSON, toJSONAsync };
831
+ export { type AsyncParsePluginContext, type AsyncParserContextOptions, type BaseDeserializerContextOptions, type BaseParserContextOptions, type BaseSerializerContextOptions, type CrossContextOptions, type CrossDeserializerContextOptions, type CrossSerializerContextOptions, type DeserializePluginContext, type LiveStreamConsumer, type LiveStreamDelivery, type LiveStreamEvent, type LiveStreamOptions, type LiveStreamProducer, type SerializePluginContext, Serializer, type SerovalNode, type StreamListener, type StreamParsePluginContext, type StreamParserContextOptions, type SyncParsePluginContext, type SyncParserContextOptions, type VanillaDeserializerContextOptions, type VanillaSerializerContextOptions };