@elaraai/east 1.0.45 → 1.0.47

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.
Files changed (90) hide show
  1. package/dist/src/containers/sortedmap.d.ts +22 -0
  2. package/dist/src/containers/sortedmap.d.ts.map +1 -1
  3. package/dist/src/containers/sortedmap.js +31 -2
  4. package/dist/src/containers/sortedmap.js.map +1 -1
  5. package/dist/src/containers/sortedset.d.ts +22 -0
  6. package/dist/src/containers/sortedset.d.ts.map +1 -1
  7. package/dist/src/containers/sortedset.js +31 -2
  8. package/dist/src/containers/sortedset.js.map +1 -1
  9. package/dist/src/expr/blob.d.ts +6 -1
  10. package/dist/src/expr/blob.d.ts.map +1 -1
  11. package/dist/src/expr/blob.js +6 -1
  12. package/dist/src/expr/blob.js.map +1 -1
  13. package/dist/src/expr/libs/blob.d.ts +10 -1
  14. package/dist/src/expr/libs/blob.d.ts.map +1 -1
  15. package/dist/src/expr/libs/blob.js +10 -1
  16. package/dist/src/expr/libs/blob.js.map +1 -1
  17. package/dist/src/fuzz.d.ts.map +1 -1
  18. package/dist/src/fuzz.js +11 -2
  19. package/dist/src/fuzz.js.map +1 -1
  20. package/dist/src/serialization/beast2/index.d.ts +76 -22
  21. package/dist/src/serialization/beast2/index.d.ts.map +1 -1
  22. package/dist/src/serialization/beast2/index.js +87 -720
  23. package/dist/src/serialization/beast2/index.js.map +1 -1
  24. package/dist/src/serialization/beast2/index.spec.js +147 -4
  25. package/dist/src/serialization/beast2/index.spec.js.map +1 -1
  26. package/dist/src/serialization/beast2/shared.d.ts +79 -0
  27. package/dist/src/serialization/beast2/shared.d.ts.map +1 -0
  28. package/dist/src/serialization/beast2/shared.js +143 -0
  29. package/dist/src/serialization/beast2/shared.js.map +1 -0
  30. package/dist/src/serialization/beast2/v4/container.d.ts +67 -0
  31. package/dist/src/serialization/beast2/v4/container.d.ts.map +1 -0
  32. package/dist/src/serialization/beast2/v4/container.js +727 -0
  33. package/dist/src/serialization/beast2/v4/container.js.map +1 -0
  34. package/dist/src/serialization/beast2/{sourcemap-table.d.ts → v4/sourcemap-table.d.ts} +2 -2
  35. package/dist/src/serialization/beast2/v4/sourcemap-table.d.ts.map +1 -0
  36. package/dist/src/serialization/beast2/{sourcemap-table.js → v4/sourcemap-table.js} +2 -2
  37. package/dist/src/serialization/beast2/v4/sourcemap-table.js.map +1 -0
  38. package/dist/src/serialization/beast2/{string-table.d.ts → v4/string-table.d.ts} +1 -1
  39. package/dist/src/serialization/beast2/v4/string-table.d.ts.map +1 -0
  40. package/dist/src/serialization/beast2/{string-table.js → v4/string-table.js} +1 -1
  41. package/dist/src/serialization/beast2/v4/string-table.js.map +1 -0
  42. package/dist/src/serialization/beast2/{type-table.d.ts → v4/type-table.d.ts} +6 -4
  43. package/dist/src/serialization/beast2/v4/type-table.d.ts.map +1 -0
  44. package/dist/src/serialization/beast2/{type-table.js → v4/type-table.js} +103 -10
  45. package/dist/src/serialization/beast2/v4/type-table.js.map +1 -0
  46. package/dist/src/serialization/beast2/{value-table.d.ts → v4/value-table.d.ts} +2 -2
  47. package/dist/src/serialization/beast2/v4/value-table.d.ts.map +1 -0
  48. package/dist/src/serialization/beast2/{value-table.js → v4/value-table.js} +3 -3
  49. package/dist/src/serialization/beast2/v4/value-table.js.map +1 -0
  50. package/dist/src/serialization/beast2/v5/codec.d.ts +213 -0
  51. package/dist/src/serialization/beast2/v5/codec.d.ts.map +1 -0
  52. package/dist/src/serialization/beast2/v5/codec.js +954 -0
  53. package/dist/src/serialization/beast2/v5/codec.js.map +1 -0
  54. package/dist/src/serialization/beast2/v5/deflate.d.ts +16 -0
  55. package/dist/src/serialization/beast2/v5/deflate.d.ts.map +1 -0
  56. package/dist/src/serialization/beast2/v5/deflate.js +172 -0
  57. package/dist/src/serialization/beast2/v5/deflate.js.map +1 -0
  58. package/dist/src/serialization/beast2/v5/frames.d.ts +150 -0
  59. package/dist/src/serialization/beast2/v5/frames.d.ts.map +1 -0
  60. package/dist/src/serialization/beast2/v5/frames.js +284 -0
  61. package/dist/src/serialization/beast2/v5/frames.js.map +1 -0
  62. package/dist/src/serialization/beast2/v5/index.spec.d.ts +6 -0
  63. package/dist/src/serialization/beast2/v5/index.spec.d.ts.map +1 -0
  64. package/dist/src/serialization/beast2/v5/index.spec.js +484 -0
  65. package/dist/src/serialization/beast2/v5/index.spec.js.map +1 -0
  66. package/dist/src/serialization/beast2/v5/stream.d.ts +177 -0
  67. package/dist/src/serialization/beast2/v5/stream.d.ts.map +1 -0
  68. package/dist/src/serialization/beast2/v5/stream.js +447 -0
  69. package/dist/src/serialization/beast2/v5/stream.js.map +1 -0
  70. package/dist/src/serialization/beast2/v5/type-section.d.ts +81 -0
  71. package/dist/src/serialization/beast2/v5/type-section.d.ts.map +1 -0
  72. package/dist/src/serialization/beast2/v5/type-section.js +211 -0
  73. package/dist/src/serialization/beast2/v5/type-section.js.map +1 -0
  74. package/dist/src/serialization/beast2/version.d.ts +40 -0
  75. package/dist/src/serialization/beast2/version.d.ts.map +1 -0
  76. package/dist/src/serialization/beast2/version.js +23 -0
  77. package/dist/src/serialization/beast2/version.js.map +1 -0
  78. package/dist/src/serialization/index.d.ts +1 -1
  79. package/dist/src/serialization/index.d.ts.map +1 -1
  80. package/dist/src/serialization/index.js +1 -1
  81. package/dist/src/serialization/index.js.map +1 -1
  82. package/package.json +1 -1
  83. package/dist/src/serialization/beast2/sourcemap-table.d.ts.map +0 -1
  84. package/dist/src/serialization/beast2/sourcemap-table.js.map +0 -1
  85. package/dist/src/serialization/beast2/string-table.d.ts.map +0 -1
  86. package/dist/src/serialization/beast2/string-table.js.map +0 -1
  87. package/dist/src/serialization/beast2/type-table.d.ts.map +0 -1
  88. package/dist/src/serialization/beast2/type-table.js.map +0 -1
  89. package/dist/src/serialization/beast2/value-table.d.ts.map +0 -1
  90. package/dist/src/serialization/beast2/value-table.js.map +0 -1
@@ -0,0 +1,177 @@
1
+ /**
2
+ * Copyright (c) 2025 Elara AI Pty Ltd
3
+ * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
4
+ */
5
+ /**
6
+ * Beast2 v5 streaming and paging APIs.
7
+ *
8
+ * - {@link Beast2Writer} — append-only streaming writer: each `write(batch)`
9
+ * becomes one root segment (one frame), so writer memory is O(batch), never
10
+ * the whole collection.
11
+ * - {@link encodeBeast2SegmentsFor} — in-memory convenience over the writer.
12
+ * - {@link iterBeast2SegmentsFor} — sequential segment iterator: yields one
13
+ * decoded collection per root segment with O(segment) decoded memory.
14
+ * - {@link openBeast2PagesFor} — random access over an indexed blob: O(1)
15
+ * `elementCount`, per-segment decode via footer + index seeks.
16
+ *
17
+ * Streaming roots are collections (Array/Set/Dict). Writers default to
18
+ * self-contained segments (aliasing scoped per segment) so their output is
19
+ * pageable and parallel-decodable; pass `selfContained: false` to keep
20
+ * whole-stream aliasing at the cost of random access.
21
+ */
22
+ import { type EastTypeValue } from "../../../type_of_type.js";
23
+ import type { EastType, ValueTypeOf } from "../../../types.js";
24
+ import { SourceMap } from "../../../location.js";
25
+ import { type Beast2DecodeOptions } from "../shared.js";
26
+ import { type Beast2Codec } from "./frames.js";
27
+ /** Options accepted by {@link Beast2Writer} and {@link encodeBeast2SegmentsFor}. */
28
+ export type Beast2WriterOptions = {
29
+ /** Per-frame codec. Defaults to `"deflate"`. */
30
+ codec?: Beast2Codec;
31
+ /** Scope aliasing per segment so the output is pageable and segments decode
32
+ * independently. Defaults to `true`. */
33
+ selfContained?: boolean;
34
+ /** Write the trailing index + footer at {@link Beast2Writer.finish}.
35
+ * Defaults to `true`. */
36
+ index?: boolean;
37
+ /** Source map for function values in the stream, written to the header. */
38
+ sourceMap?: SourceMap | null;
39
+ };
40
+ /**
41
+ * Append-only streaming writer for a v5 collection blob.
42
+ *
43
+ * Each {@link write} encodes one batch — a value of the declared collection
44
+ * type — as one root segment, so peak writer memory is one batch plus its
45
+ * aliased containers, never the whole collection. Output bytes are pushed to
46
+ * the `sink` as they are produced; the header is written at construction and
47
+ * {@link finish} appends the terminator (plus index and footer by default),
48
+ * so the byte stream is append-only end to end.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * const chunks: Uint8Array[] = [];
53
+ * const writer = new Beast2Writer(ArrayType(StringType), (b) => chunks.push(b));
54
+ * writer.write(["a", "b"]);
55
+ * writer.write(["c"]);
56
+ * writer.finish();
57
+ * const blob = Buffer.concat(chunks);
58
+ * decodeBeast2For(ArrayType(StringType))(blob); // ["a", "b", "c"]
59
+ * ```
60
+ */
61
+ export declare class Beast2Writer<T extends EastType = EastType> {
62
+ /** Number of segments written so far. */
63
+ segments: number;
64
+ private readonly kind;
65
+ private readonly sink;
66
+ private readonly codec;
67
+ private readonly selfContained;
68
+ private readonly withIndex;
69
+ private readonly ctx;
70
+ private readonly encodeElems;
71
+ private readonly index;
72
+ private bytesWritten;
73
+ private finished;
74
+ /**
75
+ * @param type - the collection type this stream holds (Array/Set/Dict)
76
+ * @param sink - receives output bytes as they are produced
77
+ * @param options - codec, self-containment, index, and source map options
78
+ * @throws {TypeError} When `type` is not an Array, Set or Dict type.
79
+ */
80
+ constructor(type: T | EastTypeValue, sink: (bytes: Uint8Array) => void, options?: Beast2WriterOptions);
81
+ /**
82
+ * Encodes one batch as one root segment.
83
+ *
84
+ * Empty batches are skipped — a segment count is never zero, so the stream
85
+ * terminator stays unambiguous.
86
+ *
87
+ * @param batch - a value of the declared collection type
88
+ * @throws {Error} When called after {@link finish}.
89
+ */
90
+ write(batch: ValueTypeOf<T>): void;
91
+ /**
92
+ * Terminates the stream: writes the terminator frame and, unless disabled,
93
+ * the index and footer. Idempotent.
94
+ */
95
+ finish(): void;
96
+ private emit;
97
+ }
98
+ /**
99
+ * Builds a curried batch encoder: `encode(batches)` returns one v5 blob with
100
+ * one segment per non-empty batch. The in-memory convenience form of
101
+ * {@link Beast2Writer} — use the writer to stream to a file or socket.
102
+ *
103
+ * @param type - the collection type (Array/Set/Dict)
104
+ * @param options - writer options
105
+ * @returns a function encoding an iterable of batches to v5 bytes
106
+ * @throws {TypeError} When `type` is not an Array, Set or Dict type.
107
+ */
108
+ export declare function encodeBeast2SegmentsFor<T extends EastType>(type: T | EastTypeValue, options?: Beast2WriterOptions): (batches: Iterable<ValueTypeOf<T>>) => Uint8Array;
109
+ /**
110
+ * Builds a curried segment iterator: `segments(data)` yields one decoded
111
+ * collection per root segment, in stream order, with O(segment) decoded
112
+ * memory. The caller merges (or processes each batch and drops it).
113
+ *
114
+ * @param type - the collection type (Array/Set/Dict)
115
+ * @param options - decode options (platform functions for decoded functions)
116
+ * @returns a function returning a generator over decoded segments
117
+ * @throws {TypeError} When `type` is not an Array, Set or Dict type.
118
+ */
119
+ export declare function iterBeast2SegmentsFor<T extends EastType>(type: T | EastTypeValue, options?: Beast2DecodeOptions): (data: Uint8Array) => Generator<ValueTypeOf<T>>;
120
+ /**
121
+ * Random access over an indexed, self-contained v5 collection blob.
122
+ *
123
+ * Reads the footer + index once; `elementCount` is O(1) from the index, and
124
+ * {@link segment} seeks to and decodes exactly one segment. Requires the blob
125
+ * to carry an index (written by default by {@link Beast2Writer}); random
126
+ * access additionally requires self-contained segments.
127
+ */
128
+ export declare class Beast2Pages<T extends EastType = EastType> {
129
+ /** Per-segment element counts from the index (pairs for Dict roots). */
130
+ readonly counts: readonly number[];
131
+ /** Sum of all segment counts. For Array roots this is the exact element
132
+ * count; for Set/Dict roots it is an upper bound (cross-segment duplicate
133
+ * keys collapse on merge). */
134
+ readonly elementCount: number;
135
+ /** Whether segments are independently decodable. */
136
+ readonly selfContained: boolean;
137
+ private readonly data;
138
+ private readonly indexData;
139
+ private readonly kind;
140
+ private readonly sourceMap;
141
+ private readonly decodeSegment;
142
+ private readonly platform;
143
+ private readonly cumulative;
144
+ /** @internal Use {@link openBeast2PagesFor}. */
145
+ constructor(data: Uint8Array, typeValue: EastTypeValue, options?: Beast2DecodeOptions);
146
+ /** Number of segments in the blob. */
147
+ get segmentCount(): number;
148
+ /**
149
+ * Decodes one segment by index.
150
+ *
151
+ * @param i - zero-based segment index
152
+ * @returns the segment's decoded collection
153
+ * @throws {Error} When the blob is not self-contained (segments cannot be
154
+ * decoded independently) or `i` is out of range.
155
+ */
156
+ segment(i: number): ValueTypeOf<T>;
157
+ /**
158
+ * Reads one element by row index (Array roots only): binary-searches the
159
+ * index, decodes that single segment, and returns the row.
160
+ *
161
+ * @param row - zero-based element index across the whole collection
162
+ * @returns the decoded element
163
+ * @throws {Error} When the root is not an Array or `row` is out of range.
164
+ */
165
+ element(row: number): ValueTypeOf<T> extends (infer E)[] ? E : never;
166
+ }
167
+ /**
168
+ * Builds a curried pages opener: `open(data)` parses the header, footer and
169
+ * index once and returns a {@link Beast2Pages} for random access.
170
+ *
171
+ * @param type - the collection type (Array/Set/Dict)
172
+ * @param options - decode options (platform functions for decoded functions)
173
+ * @returns a function opening a blob for paged reads
174
+ * @throws {TypeError} When `type` is not an Array, Set or Dict type.
175
+ */
176
+ export declare function openBeast2PagesFor<T extends EastType>(type: T | EastTypeValue, options?: Beast2DecodeOptions): (data: Uint8Array) => Beast2Pages<T>;
177
+ //# sourceMappingURL=stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../../../../src/serialization/beast2/v5/stream.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAE/D,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,KAAK,mBAAmB,EAAwB,MAAM,cAAc,CAAC;AAE9E,OAAO,EAAE,KAAK,WAAW,EAA2B,MAAM,aAAa,CAAC;AA4BxE,oFAAoF;AACpF,MAAM,MAAM,mBAAmB,GAAG;IAChC,gDAAgD;IAChD,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB;6C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AC,CAAC,SAAS,QAAQ,EAAE,IAAI,EAAE,CAAC,GAAG,aAAa,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,CAAC,IAAI,EAAE,UAAU,KAAK,WAAW,CAAC,CAAC,CAAC,CAInJ"}
@@ -0,0 +1,447 @@
1
+ /**
2
+ * Copyright (c) 2025 Elara AI Pty Ltd
3
+ * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
4
+ */
5
+ /**
6
+ * Beast2 v5 streaming and paging APIs.
7
+ *
8
+ * - {@link Beast2Writer} — append-only streaming writer: each `write(batch)`
9
+ * becomes one root segment (one frame), so writer memory is O(batch), never
10
+ * the whole collection.
11
+ * - {@link encodeBeast2SegmentsFor} — in-memory convenience over the writer.
12
+ * - {@link iterBeast2SegmentsFor} — sequential segment iterator: yields one
13
+ * decoded collection per root segment with O(segment) decoded memory.
14
+ * - {@link openBeast2PagesFor} — random access over an indexed blob: O(1)
15
+ * `elementCount`, per-segment decode via footer + index seeks.
16
+ *
17
+ * Streaming roots are collections (Array/Set/Dict). Writers default to
18
+ * self-contained segments (aliasing scoped per segment) so their output is
19
+ * pageable and parallel-decodable; pass `selfContained: false` to keep
20
+ * whole-stream aliasing at the cost of random access.
21
+ */
22
+ import {} from "../../../type_of_type.js";
23
+ import { BufferWriter, BufferReader } from "../../binary-utils.js";
24
+ import { SourceMap } from "../../../location.js";
25
+ import { buildPlatformContext } from "../shared.js";
26
+ import { writeTypeSection, readTypeSection, asTypeValue } from "./type-section.js";
27
+ import { FrameReader, writeFrame } from "./frames.js";
28
+ import { MAGIC_BYTES_V5, TAG_NEW, createV5EncodeContext, buildV5Encoder, buildV5Decoder, writeSourceMapSectionV5, readSourceMapSectionV5, writeIndexAndFooter, readIndex, isSegmentedRoot, } from "./codec.js";
29
+ /** Validates that a root type is streamable and returns its kind. */
30
+ function checkSegmented(typeValue) {
31
+ if (!isSegmentedRoot(typeValue)) {
32
+ throw new TypeError(`beast2 v5 streams hold Array, Set or Dict values, not ${typeValue.type}`);
33
+ }
34
+ return typeValue.type;
35
+ }
36
+ // =============================================================================
37
+ // Streaming writer
38
+ // =============================================================================
39
+ /**
40
+ * Append-only streaming writer for a v5 collection blob.
41
+ *
42
+ * Each {@link write} encodes one batch — a value of the declared collection
43
+ * type — as one root segment, so peak writer memory is one batch plus its
44
+ * aliased containers, never the whole collection. Output bytes are pushed to
45
+ * the `sink` as they are produced; the header is written at construction and
46
+ * {@link finish} appends the terminator (plus index and footer by default),
47
+ * so the byte stream is append-only end to end.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * const chunks: Uint8Array[] = [];
52
+ * const writer = new Beast2Writer(ArrayType(StringType), (b) => chunks.push(b));
53
+ * writer.write(["a", "b"]);
54
+ * writer.write(["c"]);
55
+ * writer.finish();
56
+ * const blob = Buffer.concat(chunks);
57
+ * decodeBeast2For(ArrayType(StringType))(blob); // ["a", "b", "c"]
58
+ * ```
59
+ */
60
+ export class Beast2Writer {
61
+ /** Number of segments written so far. */
62
+ segments = 0;
63
+ kind;
64
+ sink;
65
+ codec;
66
+ selfContained;
67
+ withIndex;
68
+ ctx;
69
+ encodeElems;
70
+ index = [];
71
+ bytesWritten = 0;
72
+ finished = false;
73
+ /**
74
+ * @param type - the collection type this stream holds (Array/Set/Dict)
75
+ * @param sink - receives output bytes as they are produced
76
+ * @param options - codec, self-containment, index, and source map options
77
+ * @throws {TypeError} When `type` is not an Array, Set or Dict type.
78
+ */
79
+ constructor(type, sink, options) {
80
+ const typeValue = asTypeValue(type);
81
+ this.kind = checkSegmented(typeValue);
82
+ this.sink = sink;
83
+ this.codec = options?.codec ?? "deflate";
84
+ this.selfContained = options?.selfContained ?? true;
85
+ this.withIndex = options?.index ?? true;
86
+ const sourceMap = options?.sourceMap ?? null;
87
+ this.ctx = createV5EncodeContext(sourceMap, this.selfContained);
88
+ const typeCtx = new Map();
89
+ if (this.kind === "Dict") {
90
+ const key = buildV5Encoder(typeValue.value.key, typeCtx);
91
+ const val = buildV5Encoder(typeValue.value.value, typeCtx);
92
+ this.encodeElems = (value, logical) => {
93
+ for (const [k, v] of value) {
94
+ key(k, logical, this.ctx);
95
+ val(v, logical, this.ctx);
96
+ }
97
+ };
98
+ }
99
+ else {
100
+ const elem = buildV5Encoder(typeValue.value, typeCtx);
101
+ this.encodeElems = (value, logical) => {
102
+ for (const item of value)
103
+ elem(item, logical, this.ctx);
104
+ };
105
+ }
106
+ // Header: magic + type section + source map section, then the root tag
107
+ // as its own frame so every indexed segment frame is pure.
108
+ const head = new BufferWriter();
109
+ head.writeBytes(MAGIC_BYTES_V5);
110
+ writeTypeSection(typeValue, head);
111
+ writeSourceMapSectionV5(sourceMap, head);
112
+ writeFrame(head, new Uint8Array([TAG_NEW]), "none");
113
+ // The root container consumes definition 0 (no root object exists on the
114
+ // encode side — batches are independent values, so nothing can alias it);
115
+ // segments scope from definition 1 to match the decoder's numbering.
116
+ this.ctx.containerCount = 1;
117
+ this.ctx.segmentBaseDef = 1;
118
+ this.emit(head.toUint8Array());
119
+ }
120
+ /**
121
+ * Encodes one batch as one root segment.
122
+ *
123
+ * Empty batches are skipped — a segment count is never zero, so the stream
124
+ * terminator stays unambiguous.
125
+ *
126
+ * @param batch - a value of the declared collection type
127
+ * @throws {Error} When called after {@link finish}.
128
+ */
129
+ write(batch) {
130
+ if (this.finished)
131
+ throw new Error("write() after finish()");
132
+ const count = this.kind === "Array" ? batch.length : batch.size;
133
+ if (count === 0)
134
+ return;
135
+ if (this.selfContained) {
136
+ this.ctx.containerIndex.clear();
137
+ this.ctx.segmentBaseDef = this.ctx.containerCount;
138
+ }
139
+ const logical = new BufferWriter();
140
+ logical.writeVarint(count);
141
+ this.encodeElems(batch, logical);
142
+ const frame = new BufferWriter();
143
+ writeFrame(frame, logical.toUint8Array(), this.codec);
144
+ this.index.push({ offset: this.bytesWritten, count });
145
+ this.segments++;
146
+ this.emit(frame.toUint8Array());
147
+ }
148
+ /**
149
+ * Terminates the stream: writes the terminator frame and, unless disabled,
150
+ * the index and footer. Idempotent.
151
+ */
152
+ finish() {
153
+ if (this.finished)
154
+ return;
155
+ this.finished = true;
156
+ const tail = new BufferWriter();
157
+ writeFrame(tail, new Uint8Array([0x00]), "none");
158
+ if (this.withIndex) {
159
+ // writeIndexAndFooter derives the index offset from the writer's size,
160
+ // so seed it with the bytes already emitted.
161
+ const before = tail.size;
162
+ writeIndexAndFooterAt(tail, this.bytesWritten + before, this.index, this.selfContained && !this.ctx.crossSegmentRef);
163
+ }
164
+ this.emit(tail.toUint8Array());
165
+ }
166
+ emit(bytes) {
167
+ this.bytesWritten += bytes.length;
168
+ this.sink(bytes);
169
+ }
170
+ }
171
+ /** Writes the index + footer where the index offset is `absoluteOffset`
172
+ * rather than the writer's own size (the writer streams to a sink). */
173
+ function writeIndexAndFooterAt(writer, absoluteOffset, segments, selfContained) {
174
+ const local = new BufferWriter();
175
+ writeIndexAndFooter(local, segments, selfContained);
176
+ // writeIndexAndFooter stamped its own size as the index offset (0 here, as
177
+ // `local` starts empty at the index) — restamp the footer's u64 with the
178
+ // absolute offset.
179
+ const bytes = local.toUint8Array();
180
+ let v = BigInt(absoluteOffset);
181
+ for (let i = 0; i < 8; i++) {
182
+ bytes[bytes.length - 16 + i] = Number(v & 0xffn);
183
+ v >>= 8n;
184
+ }
185
+ writer.writeBytes(bytes);
186
+ }
187
+ // =============================================================================
188
+ // In-memory convenience encode
189
+ // =============================================================================
190
+ /**
191
+ * Builds a curried batch encoder: `encode(batches)` returns one v5 blob with
192
+ * one segment per non-empty batch. The in-memory convenience form of
193
+ * {@link Beast2Writer} — use the writer to stream to a file or socket.
194
+ *
195
+ * @param type - the collection type (Array/Set/Dict)
196
+ * @param options - writer options
197
+ * @returns a function encoding an iterable of batches to v5 bytes
198
+ * @throws {TypeError} When `type` is not an Array, Set or Dict type.
199
+ */
200
+ export function encodeBeast2SegmentsFor(type, options) {
201
+ checkSegmented(asTypeValue(type));
202
+ return (batches) => {
203
+ const chunks = [];
204
+ const writer = new Beast2Writer(type, (b) => chunks.push(b), options);
205
+ for (const batch of batches)
206
+ writer.write(batch);
207
+ writer.finish();
208
+ let total = 0;
209
+ for (const c of chunks)
210
+ total += c.length;
211
+ const out = new Uint8Array(total);
212
+ let pos = 0;
213
+ for (const c of chunks) {
214
+ out.set(c, pos);
215
+ pos += c.length;
216
+ }
217
+ return out;
218
+ };
219
+ }
220
+ // =============================================================================
221
+ // Segment iterator
222
+ // =============================================================================
223
+ /** Verifies the v5 magic without dispatching (stream APIs are v5-only). */
224
+ function verifyV5Magic(data) {
225
+ if (data.length < 8) {
226
+ throw new Error(`Data too short for Beast2 format: ${data.length} bytes`);
227
+ }
228
+ for (let i = 0; i < 8; i++) {
229
+ if (data[i] !== MAGIC_BYTES_V5[i]) {
230
+ if (i === 7 && data[i] === 0x04) {
231
+ throw new Error(`beast2 v5: segment APIs need a v5 blob; this is a v4 container (re-encode with version 5)`);
232
+ }
233
+ throw new Error(`Invalid Beast2 v5 magic at offset ${i}: expected 0x${MAGIC_BYTES_V5[i].toString(16)}, got 0x${data[i].toString(16)}`);
234
+ }
235
+ }
236
+ }
237
+ /** Parses the header of a v5 collection blob for the segment-level readers. */
238
+ function openSegmented(data, typeValue) {
239
+ verifyV5Magic(data);
240
+ const kind = checkSegmented(typeValue);
241
+ const reader = new BufferReader(data, MAGIC_BYTES_V5.length);
242
+ readTypeSection(reader);
243
+ const sourceMap = readSourceMapSectionV5(reader);
244
+ return { kind, sourceMap, frameOffset: reader.offset };
245
+ }
246
+ /** Builds the per-segment decode closure shared by the iterator and pages. */
247
+ function buildSegmentDecoder(typeValue, kind) {
248
+ const typeCtx = new Map();
249
+ if (kind === "Dict") {
250
+ const key = buildV5Decoder(typeValue.value.key, typeCtx);
251
+ const val = buildV5Decoder(typeValue.value.value, typeCtx);
252
+ return (reader, ctx, n) => {
253
+ const map = new Map();
254
+ for (let i = 0; i < n; i++) {
255
+ const k = key(reader, ctx);
256
+ const v = val(reader, ctx);
257
+ map.set(k, v);
258
+ }
259
+ return map;
260
+ };
261
+ }
262
+ const elem = buildV5Decoder(typeValue.value, typeCtx);
263
+ if (kind === "Set") {
264
+ return (reader, ctx, n) => {
265
+ const set = new Set();
266
+ for (let i = 0; i < n; i++)
267
+ set.add(elem(reader, ctx));
268
+ return set;
269
+ };
270
+ }
271
+ return (reader, ctx, n) => {
272
+ const arr = new Array(n);
273
+ for (let i = 0; i < n; i++)
274
+ arr[i] = elem(reader, ctx);
275
+ return arr;
276
+ };
277
+ }
278
+ /**
279
+ * Builds a curried segment iterator: `segments(data)` yields one decoded
280
+ * collection per root segment, in stream order, with O(segment) decoded
281
+ * memory. The caller merges (or processes each batch and drops it).
282
+ *
283
+ * @param type - the collection type (Array/Set/Dict)
284
+ * @param options - decode options (platform functions for decoded functions)
285
+ * @returns a function returning a generator over decoded segments
286
+ * @throws {TypeError} When `type` is not an Array, Set or Dict type.
287
+ */
288
+ export function iterBeast2SegmentsFor(type, options) {
289
+ const typeValue = asTypeValue(type);
290
+ checkSegmented(typeValue);
291
+ return function* (data) {
292
+ const { kind, sourceMap, frameOffset } = openSegmented(data, typeValue);
293
+ const decodeSegment = buildSegmentDecoder(typeValue, kind);
294
+ const ctx = { containers: [], sourceMap, ...buildPlatformContext(options) };
295
+ const cursor = new FrameReader(data, frameOffset);
296
+ let reader = cursor.next();
297
+ const tag = reader.readUint8();
298
+ if (tag !== TAG_NEW) {
299
+ throw new Error(`beast2 v5: root container must be NEW (tag 0x${tag.toString(16)})`);
300
+ }
301
+ // The root container is definition 0 — segments never alias it, but the
302
+ // definition numbering must match the writer's.
303
+ ctx.containers.push(kind === "Array" ? [] : kind === "Set" ? new Set() : new Map());
304
+ for (;;) {
305
+ if (reader.offset === reader.buffer.length)
306
+ reader = cursor.next();
307
+ const n = reader.readVarint();
308
+ if (n === 0)
309
+ break;
310
+ yield decodeSegment(reader, ctx, n);
311
+ }
312
+ if (reader.offset !== reader.buffer.length) {
313
+ throw new Error(`beast2 v5: ${reader.buffer.length - reader.offset} logical bytes after the root terminator`);
314
+ }
315
+ // Trailing bytes must be nothing or a well-formed index + footer.
316
+ if (cursor.wireOffset !== data.length && readIndex(data) === null) {
317
+ throw new Error(`beast2 v5: ${data.length - cursor.wireOffset} trailing bytes at offset ${cursor.wireOffset} (no footer)`);
318
+ }
319
+ };
320
+ }
321
+ // =============================================================================
322
+ // Paging reader
323
+ // =============================================================================
324
+ /**
325
+ * Random access over an indexed, self-contained v5 collection blob.
326
+ *
327
+ * Reads the footer + index once; `elementCount` is O(1) from the index, and
328
+ * {@link segment} seeks to and decodes exactly one segment. Requires the blob
329
+ * to carry an index (written by default by {@link Beast2Writer}); random
330
+ * access additionally requires self-contained segments.
331
+ */
332
+ export class Beast2Pages {
333
+ /** Per-segment element counts from the index (pairs for Dict roots). */
334
+ counts;
335
+ /** Sum of all segment counts. For Array roots this is the exact element
336
+ * count; for Set/Dict roots it is an upper bound (cross-segment duplicate
337
+ * keys collapse on merge). */
338
+ elementCount;
339
+ /** Whether segments are independently decodable. */
340
+ selfContained;
341
+ data;
342
+ indexData;
343
+ kind;
344
+ sourceMap;
345
+ decodeSegment;
346
+ platform;
347
+ cumulative;
348
+ /** @internal Use {@link openBeast2PagesFor}. */
349
+ constructor(data, typeValue, options) {
350
+ const { kind, sourceMap, frameOffset } = openSegmented(data, typeValue);
351
+ void frameOffset;
352
+ const index = readIndex(data);
353
+ if (!index) {
354
+ throw new Error(`beast2 v5: blob has no index/footer — paging needs a writer with index enabled`);
355
+ }
356
+ this.data = data;
357
+ this.indexData = index;
358
+ this.kind = kind;
359
+ this.sourceMap = sourceMap;
360
+ this.decodeSegment = buildSegmentDecoder(typeValue, kind);
361
+ this.platform = options;
362
+ this.counts = index.counts;
363
+ this.elementCount = index.totalCount;
364
+ this.selfContained = index.selfContained;
365
+ this.cumulative = new Array(index.counts.length);
366
+ let sum = 0;
367
+ for (let i = 0; i < index.counts.length; i++) {
368
+ sum += index.counts[i];
369
+ this.cumulative[i] = sum;
370
+ }
371
+ }
372
+ /** Number of segments in the blob. */
373
+ get segmentCount() {
374
+ return this.indexData.offsets.length;
375
+ }
376
+ /**
377
+ * Decodes one segment by index.
378
+ *
379
+ * @param i - zero-based segment index
380
+ * @returns the segment's decoded collection
381
+ * @throws {Error} When the blob is not self-contained (segments cannot be
382
+ * decoded independently) or `i` is out of range.
383
+ */
384
+ segment(i) {
385
+ if (!this.selfContained) {
386
+ throw new Error(`beast2 v5: blob has cross-segment aliasing — random access needs self-contained segments`);
387
+ }
388
+ if (i < 0 || i >= this.indexData.offsets.length) {
389
+ throw new Error(`beast2 v5: segment ${i} out of range (${this.indexData.offsets.length} segments)`);
390
+ }
391
+ const cursor = new FrameReader(this.data, this.indexData.offsets[i]);
392
+ const reader = cursor.next();
393
+ const n = reader.readVarint();
394
+ if (n !== this.indexData.counts[i]) {
395
+ throw new Error(`beast2 v5: segment ${i} declares ${n} elements, index says ${this.indexData.counts[i]}`);
396
+ }
397
+ const ctx = { containers: [], sourceMap: this.sourceMap, ...buildPlatformContext(this.platform) };
398
+ const value = this.decodeSegment(reader, ctx, n);
399
+ if (reader.offset !== reader.buffer.length) {
400
+ throw new Error(`beast2 v5: ${reader.buffer.length - reader.offset} logical bytes after segment ${i}`);
401
+ }
402
+ return value;
403
+ }
404
+ /**
405
+ * Reads one element by row index (Array roots only): binary-searches the
406
+ * index, decodes that single segment, and returns the row.
407
+ *
408
+ * @param row - zero-based element index across the whole collection
409
+ * @returns the decoded element
410
+ * @throws {Error} When the root is not an Array or `row` is out of range.
411
+ */
412
+ element(row) {
413
+ if (this.kind !== "Array") {
414
+ throw new Error(`beast2 v5: element() addresses Array roots; this blob holds ${this.kind}`);
415
+ }
416
+ if (row < 0 || row >= this.elementCount) {
417
+ throw new Error(`beast2 v5: element ${row} out of range (${this.elementCount} elements)`);
418
+ }
419
+ // Binary search the cumulative counts for the owning segment.
420
+ let lo = 0, hi = this.cumulative.length - 1;
421
+ while (lo < hi) {
422
+ const mid = (lo + hi) >> 1;
423
+ if (this.cumulative[mid] <= row)
424
+ lo = mid + 1;
425
+ else
426
+ hi = mid;
427
+ }
428
+ const base = lo === 0 ? 0 : this.cumulative[lo - 1];
429
+ const seg = this.segment(lo);
430
+ return seg[row - base];
431
+ }
432
+ }
433
+ /**
434
+ * Builds a curried pages opener: `open(data)` parses the header, footer and
435
+ * index once and returns a {@link Beast2Pages} for random access.
436
+ *
437
+ * @param type - the collection type (Array/Set/Dict)
438
+ * @param options - decode options (platform functions for decoded functions)
439
+ * @returns a function opening a blob for paged reads
440
+ * @throws {TypeError} When `type` is not an Array, Set or Dict type.
441
+ */
442
+ export function openBeast2PagesFor(type, options) {
443
+ const typeValue = asTypeValue(type);
444
+ checkSegmented(typeValue);
445
+ return (data) => new Beast2Pages(data, typeValue, options);
446
+ }
447
+ //# sourceMappingURL=stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.js","sourceRoot":"","sources":["../../../../../src/serialization/beast2/v5/stream.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAsB,MAAM,0BAA0B,CAAC;AAE9D,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAA4B,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAC9E,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACnF,OAAO,EAAoB,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,EACL,cAAc,EACd,OAAO,EAGP,qBAAqB,EACrB,cAAc,EACd,cAAc,EACd,uBAAuB,EACvB,sBAAsB,EACtB,mBAAmB,EACnB,SAAS,EACT,eAAe,GAEhB,MAAM,YAAY,CAAC;AAKpB,qEAAqE;AACrE,SAAS,cAAc,CAAC,SAAwB;IAC9C,IAAI,CAAC,eAAe,CAAC,SAAS,CA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