pojoc 0.7.0 → 0.8.0

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.
@@ -0,0 +1,21 @@
1
+ import type { Input, Version } from "./types.js";
2
+ import type { SchemaSpec } from "./legacy_types.js";
3
+ declare function makeCodec<T>(schemas: SchemaSpec[], scope: number): {
4
+ version: bigint;
5
+ encode: (value: T) => Uint8Array<ArrayBufferLike>;
6
+ encodeForVersion: (value: T, target: Version) => Uint8Array<ArrayBufferLike>;
7
+ decode: (input: Input) => T;
8
+ decodeFrom: (input: Input, offset?: number) => {
9
+ value: T;
10
+ version: bigint;
11
+ offset: number;
12
+ };
13
+ open: (input: Input, offset?: number) => Readonly<T>;
14
+ select: <K extends keyof T & string>(keys: readonly K[]) => (input: Input, offset?: number) => Pick<T, K>;
15
+ create: (value?: Partial<T>) => T;
16
+ seek: {
17
+ <K extends keyof T & string>(input: Input, path: K): T[K];
18
+ (input: Input, path: string): unknown;
19
+ };
20
+ };
21
+ export { makeCodec };
package/dist/legacy.js ADDED
@@ -0,0 +1,28 @@
1
+ import { openEnvelope } from "./wire.js";
2
+ import { createPlanned, decodePlanned, encodePlanned, openPlanned, selectPlanned, seekPlanned, } from "./codec.js";
3
+ // Released v1 artifacts call this entry point. New artifacts emit these exports directly.
4
+ function makeCodec(schemas, scope) {
5
+ const version = BigInt(schemas[scope].latest);
6
+ const shortVersion = version < 128n ? Number(version) : -1;
7
+ const openFrame = (input, offset = 0) => openEnvelope(input, offset, version, shortVersion);
8
+ const decodeFrame = (r) => decodePlanned(r, version, schemas, scope);
9
+ const encodeForVersion = (value, target) => encodePlanned(value, target, schemas, scope);
10
+ function seek(input, path) {
11
+ return seekPlanned(input, path, version, schemas, scope, openFrame, decodeFrame);
12
+ }
13
+ return {
14
+ version,
15
+ encode: (value) => encodeForVersion(value, version),
16
+ encodeForVersion,
17
+ decode: (input) => decodeFrame(openFrame(input)),
18
+ decodeFrom: (input, offset = 0) => {
19
+ const r = openFrame(input, offset);
20
+ return { value: decodeFrame(r), version: r.version, offset: r.end };
21
+ },
22
+ open: (input, offset = 0) => openPlanned(openFrame(input, offset), version, schemas, scope, decodeFrame),
23
+ select: (keys) => selectPlanned(keys, version, schemas, scope, openFrame, decodeFrame),
24
+ create: (value = {}) => createPlanned(value, schemas, scope),
25
+ seek,
26
+ };
27
+ }
28
+ export { makeCodec };
@@ -0,0 +1,4 @@
1
+ import type { Primitive } from "./types.js";
2
+ import { type WriteCursor } from "./writer.js";
3
+ declare function writeScalar(w: WriteCursor, ty: Primitive, value: unknown, table: Map<string, number> | undefined): void;
4
+ export { writeScalar };
@@ -0,0 +1,35 @@
1
+ import { invalid } from "./wire.js";
2
+ import { writeString, writeU32Varint } from "./writer.js";
3
+ import { writeU8, writeI8, writeU16, writeI16, writeU32, writeI32, writeU64, writeI64, writeF32, writeF64, writeBool, writeVarint32Value, writeVarint64Value, writeNewIntern, } from "./scalar_writes.js";
4
+ function writeLegacyIntern(w, value, table) {
5
+ if (!table)
6
+ invalid("Missing Pojoc intern table");
7
+ if (typeof value !== "string")
8
+ throw new TypeError("Expected an interned string");
9
+ const index = table.get(value);
10
+ if (index === undefined)
11
+ writeNewIntern(w, value, table);
12
+ else
13
+ writeU32Varint(w, index);
14
+ }
15
+ const scalarWriters = {
16
+ u8: writeU8,
17
+ i8: writeI8,
18
+ u16: writeU16,
19
+ i16: writeI16,
20
+ u32: writeU32,
21
+ i32: writeI32,
22
+ u64: writeU64,
23
+ i64: writeI64,
24
+ f32: writeF32,
25
+ f64: writeF64,
26
+ bool: writeBool,
27
+ varint32: writeVarint32Value,
28
+ varint64: writeVarint64Value,
29
+ string: writeString,
30
+ intern: writeLegacyIntern,
31
+ };
32
+ function writeScalar(w, ty, value, table) {
33
+ scalarWriters[ty](w, value, table);
34
+ }
35
+ export { writeScalar };
@@ -0,0 +1,35 @@
1
+ import type { Primitive, Cursor, CursorOps, Obj, ReadTable } from "./types.js";
2
+ type Variant = [number, string, WireType];
3
+ type WireType = Primitive | ["record", string, string] | ["enum", number, number[], number] | ["bits", number, number] | ["union", number, Variant[], Variant[]] | ["array" | "delta", WireType, number | null] | ["map", WireType, WireType, number | null] | ["tuple", WireType[]] | ["optional" | "box", WireType] | ["constraint", WireType, number | null, number | null] | ["vfloat", number, number, number] | ["bytes", number] | ["import", string, string];
4
+ type Field = {
5
+ id: string;
6
+ name: string;
7
+ ty: WireType;
8
+ default: unknown;
9
+ seek: boolean;
10
+ lazy: boolean;
11
+ cast: boolean;
12
+ };
13
+ type BoundLayout = (r: Cursor, table: ReadTable) => Obj;
14
+ type CursorReads = CursorOps & {
15
+ field: (r: Cursor, table: ReadTable, index: number) => unknown;
16
+ };
17
+ type LayoutReader = (r: Cursor, table: ReadTable, read: CursorReads) => Obj;
18
+ type FixedLayout = {
19
+ width: number;
20
+ read: (r: Cursor, pos: number, read: CursorOps) => Obj;
21
+ };
22
+ type RootSpec = {
23
+ fields: Field[];
24
+ intern: boolean;
25
+ decodeCursor?: LayoutReader;
26
+ };
27
+ type SchemaSpec = {
28
+ latest: string;
29
+ roots: Record<string, RootSpec>;
30
+ records: Record<string, Field[]>;
31
+ cursorRecordReaders?: Record<string, LayoutReader | FixedLayout>;
32
+ runtime?: "bun" | "node";
33
+ imports: Record<string, number>;
34
+ };
35
+ export type { WireType, Field, BoundLayout, CursorReads, LayoutReader, FixedLayout, RootSpec, SchemaSpec, };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ import { type WriteCursor } from "./writer.js";
2
+ import type { Cursor, CursorOps, Obj, ReadTable } from "./types.js";
3
+ import type { BoundLayout, Field, FixedLayout, LayoutReader, SchemaSpec, WireType } from "./legacy_types.js";
4
+ declare function readValue(r: Cursor, ty: WireType, schemas: SchemaSpec[], scope: number, table: ReadTable, historical?: boolean): unknown;
5
+ declare function validate(ty: WireType, value: unknown, field: string): void;
6
+ declare function readFixedRecords(r: Cursor, count: number, width: number, read: FixedLayout["read"]): Obj[];
7
+ declare function readRecordArray(r: Cursor, count: number, table: ReadTable, read: (r: Cursor, table: ReadTable, ops: CursorOps) => Obj): Obj[];
8
+ declare function bindLayout(decode: LayoutReader, fields: Field[], schemas: SchemaSpec[], scope: number): BoundLayout;
9
+ declare function readPayload(r: Cursor, schemas: SchemaSpec[], scope: number, version: string): Obj;
10
+ declare function writePayload(w: WriteCursor, schemas: SchemaSpec[], scope: number, version: string, value: Obj): void;
11
+ export { readValue, validate, bindLayout, readPayload, writePayload, readFixedRecords, readRecordArray, };
@@ -0,0 +1,438 @@
1
+ import { writeScalar } from "./legacy_scalar.js";
2
+ import { validateRange } from "./compiled.js";
3
+ import { beginWrite, finishWrite, prependInternTable, takeWrite, writeByte, writeBytes, writeLength, writeUint, writeVarint32Array, } from "./writer.js";
4
+ import { cursorReads, cursorView, emptyTable, invalid, limitCollection, readByte, readBytes, readLength, readTable, readUint, readVarint32Array, takeBytes, } from "./wire.js";
5
+ import { arrayOf, decodeDelta, encodeDelta, fixedWidths, isFixedScalar, littleEndian, readFixedScalar, readPacked, readScalar, resized, sequence, } from "./scalars.js";
6
+ import { accessLayout, isOptional, object, setField, versionLayout, } from "./schema.js";
7
+ import { project } from "./versions.js";
8
+ function readValue(r, ty, schemas, scope, table, historical = false) {
9
+ if (typeof ty === "string")
10
+ return readScalar(r, ty, table);
11
+ const schema = schemas[scope];
12
+ switch (ty[0]) {
13
+ case "bytes":
14
+ return readBytes(r, ty[1]);
15
+ case "bits":
16
+ return resized(readBytes(r, ty[1]), ty[2]);
17
+ case "enum": {
18
+ const value = readUint(r, ty[1]);
19
+ if (value !== 0 && !ty[2].includes(value))
20
+ invalid("Invalid Pojoc enum discriminant");
21
+ return value;
22
+ }
23
+ case "union": {
24
+ const discriminant = readUint(r, ty[1]);
25
+ const size = readLength(r);
26
+ const variant = ty[2].find((v) => v[0] === discriminant);
27
+ if (!variant)
28
+ return { kind: "Unknown", discriminant, data: readBytes(r, size) };
29
+ if (size > r.end - r.pos)
30
+ invalid("Unexpected end of Pojoc payload");
31
+ const outerEnd = r.end;
32
+ r.end = r.pos + size;
33
+ try {
34
+ const value = readValue(r, variant[2], schemas, scope, table);
35
+ if (r.pos !== r.end)
36
+ invalid("Invalid Pojoc union payload length");
37
+ validate(variant[2], value, variant[1]);
38
+ return { kind: variant[1], value };
39
+ }
40
+ finally {
41
+ r.end = outerEnd;
42
+ }
43
+ }
44
+ case "record": {
45
+ const name = historical ? ty[1] : ty[2];
46
+ const fields = schema.records[name];
47
+ const decode = !historical && schema.cursorRecordReaders?.[name];
48
+ if (decode && typeof decode !== "function") {
49
+ return decode.read(r, takeBytes(r, decode.width), cursorReads);
50
+ }
51
+ return decode
52
+ ? bindLayout(decode, fields, schemas, scope)(r, table)
53
+ : readFields(r, fields, schemas, scope, table, !historical);
54
+ }
55
+ case "import":
56
+ return readPayload(r, schemas, schema.imports[ty[1]], ty[2]);
57
+ case "optional":
58
+ return readByte(r)
59
+ ? readValue(r, ty[1], schemas, scope, table, historical)
60
+ : null;
61
+ case "constraint":
62
+ case "box":
63
+ return readValue(r, ty[1], schemas, scope, table, historical);
64
+ case "vfloat":
65
+ return Math.fround(ty[1] + readUint(r, ty[3]) * ty[2]);
66
+ case "tuple":
67
+ return ty[1].map((item) => readValue(r, item, schemas, scope, table));
68
+ case "delta":
69
+ return decodeDelta(r, ty[1], ty[2] ?? readLength(r));
70
+ case "array": {
71
+ const count = ty[2] ?? readLength(r);
72
+ const ctor = arrayOf(ty[1]);
73
+ if (ctor)
74
+ return readPacked(r, ctor, count);
75
+ const element = ty[1];
76
+ if (typeof element !== "string" && element[0] === "record") {
77
+ const decode = schema.cursorRecordReaders?.[element[2]];
78
+ if (decode && typeof decode !== "function")
79
+ return readFixedRecords(r, count, decode.width, decode.read);
80
+ }
81
+ limitCollection(count);
82
+ if (element === "varint32")
83
+ return readVarint32Array(r, count);
84
+ const result = [];
85
+ for (let i = 0; i < count; i++)
86
+ result.push(readValue(r, element, schemas, scope, table));
87
+ return result;
88
+ }
89
+ case "map": {
90
+ const count = ty[3] ?? readLength(r);
91
+ limitCollection(count);
92
+ const entries = [];
93
+ for (let i = 0; i < count; i++) {
94
+ const key = readValue(r, ty[1], schemas, scope, table);
95
+ const value = readValue(r, ty[2], schemas, scope, table);
96
+ entries.push([key, value]);
97
+ }
98
+ return ty[3] === null ? new Map(entries) : entries;
99
+ }
100
+ }
101
+ }
102
+ function writeValue(w, ty, value, schemas, scope, table, historical = false) {
103
+ if (typeof ty === "string")
104
+ return writeScalar(w, ty, value, table);
105
+ const schema = schemas[scope];
106
+ switch (ty[0]) {
107
+ case "bytes":
108
+ if (!(value instanceof Uint8Array) || value.length !== ty[1])
109
+ invalid("Invalid fixed bytes length");
110
+ writeBytes(w, value);
111
+ return;
112
+ case "bits":
113
+ if (!(value instanceof Uint8Array) || value.length !== ty[2])
114
+ invalid("Invalid bitset width");
115
+ writeBytes(w, resized(value, ty[1]));
116
+ return;
117
+ case "enum":
118
+ if (typeof value !== "number" || (value !== 0 && !ty[2].includes(value)))
119
+ invalid("Invalid enum value");
120
+ writeUint(w, ty[1], value <= ty[3] ? value : 0);
121
+ return;
122
+ case "union": {
123
+ const item = object(value);
124
+ const variant = ty[3].find((v) => v[1] === item.kind);
125
+ const discriminant = (item.kind === "Unknown" ? item.discriminant : variant?.[0]);
126
+ if (!Number.isInteger(discriminant) || discriminant < 0)
127
+ invalid("Invalid Pojoc union kind");
128
+ if (discriminant >= 2 ** (ty[1] * 8))
129
+ invalid("Pojoc union discriminant overflow");
130
+ let payload;
131
+ if (variant) {
132
+ validate(variant[2], item.value, variant[1]);
133
+ const nested = beginWrite(w.fillPresenceInPlace);
134
+ writeValue(nested, variant[2], item.value, schemas, scope, table);
135
+ payload = finishWrite(nested);
136
+ }
137
+ else {
138
+ if (!(item.data instanceof Uint8Array))
139
+ throw new TypeError("Expected bytes for unknown union variant");
140
+ payload = item.data;
141
+ }
142
+ writeUint(w, ty[1], discriminant);
143
+ writeLength(w, payload.length);
144
+ writeBytes(w, payload);
145
+ return;
146
+ }
147
+ case "record": {
148
+ const fields = schema.records[historical ? ty[1] : ty[2]];
149
+ writeFields(w, fields, object(value), schemas, scope, table, !historical);
150
+ return;
151
+ }
152
+ case "import":
153
+ writePayload(w, schemas, schema.imports[ty[1]], ty[2], object(value));
154
+ return;
155
+ case "optional":
156
+ writeByte(w, value == null ? 0 : 1);
157
+ if (value != null)
158
+ writeValue(w, ty[1], value, schemas, scope, table, historical);
159
+ return;
160
+ case "constraint":
161
+ case "box":
162
+ return writeValue(w, ty[1], value, schemas, scope, table, historical);
163
+ case "vfloat": {
164
+ if (typeof value !== "number")
165
+ throw new TypeError("Expected a Pojoc vfloat");
166
+ const steps = Math.round((Math.fround(value) - ty[1]) / ty[2]);
167
+ writeUint(w, ty[3], Math.min(2 ** (ty[3] * 8) - 1, Math.max(0, steps || 0)));
168
+ return;
169
+ }
170
+ case "tuple": {
171
+ const items = sequence(value);
172
+ if (items.length !== ty[1].length)
173
+ invalid("Invalid Pojoc tuple length");
174
+ ty[1].forEach((kind, i) => writeValue(w, kind, items[i], schemas, scope, table));
175
+ return;
176
+ }
177
+ case "array":
178
+ case "delta": {
179
+ const items = sequence(value);
180
+ if (ty[2] === null)
181
+ writeLength(w, items.length);
182
+ else if (items.length !== ty[2])
183
+ invalid("Invalid fixed array length");
184
+ if (ty[0] === "delta")
185
+ return encodeDelta(w, ty[1], items);
186
+ if (ty[1] === "varint32")
187
+ return writeVarint32Array(w, items);
188
+ const ctor = arrayOf(ty[1]);
189
+ if (littleEndian && ctor && value instanceof ctor) {
190
+ writeBytes(w, new Uint8Array(value.buffer, value.byteOffset, value.byteLength));
191
+ }
192
+ else {
193
+ for (let i = 0; i < items.length; i++)
194
+ writeValue(w, ty[1], items[i], schemas, scope, table);
195
+ }
196
+ return;
197
+ }
198
+ case "map": {
199
+ let entries;
200
+ if (ty[3] === null) {
201
+ if (!(value instanceof Map))
202
+ throw new TypeError("Expected a Pojoc Map");
203
+ writeLength(w, value.size);
204
+ entries = value;
205
+ }
206
+ else {
207
+ if (!Array.isArray(value) || value.length !== ty[3])
208
+ invalid("Invalid fixed map length");
209
+ entries = value;
210
+ }
211
+ for (const [key, item] of entries) {
212
+ writeValue(w, ty[1], key, schemas, scope, table);
213
+ writeValue(w, ty[2], item, schemas, scope, table);
214
+ }
215
+ return;
216
+ }
217
+ }
218
+ }
219
+ function validate(ty, value, field) {
220
+ if (typeof ty === "string")
221
+ return;
222
+ if (ty[0] === "optional" && value != null)
223
+ return validate(ty[1], value, field);
224
+ if (ty[0] !== "constraint")
225
+ return;
226
+ const [, , min, max] = ty;
227
+ validateRange(value, min, max, field);
228
+ }
229
+ const fieldReadPlans = new WeakMap();
230
+ function needsValidation(ty) {
231
+ while (isOptional(ty))
232
+ ty = ty[1];
233
+ return typeof ty !== "string" && ty[0] === "constraint";
234
+ }
235
+ function fieldReadPlan(fields) {
236
+ let plan = fieldReadPlans.get(fields);
237
+ if (plan !== undefined)
238
+ return plan;
239
+ const layout = accessLayout(fields);
240
+ const steps = [];
241
+ let run;
242
+ for (const { field, ty, bit } of layout.fields) {
243
+ if (bit < 0 && isFixedScalar(ty)) {
244
+ if (!run) {
245
+ run = { width: 0, scalars: [] };
246
+ steps.push(run);
247
+ }
248
+ run.scalars.push({ name: field.name, ty, offset: run.width });
249
+ run.width += fixedWidths[ty];
250
+ }
251
+ else {
252
+ run = undefined;
253
+ steps.push({
254
+ name: field.name,
255
+ ty,
256
+ cast: field.cast,
257
+ presenceBit: bit,
258
+ constrained: needsValidation(ty),
259
+ });
260
+ }
261
+ }
262
+ plan = { headerBytes: layout.bitmapBytes, steps };
263
+ fieldReadPlans.set(fields, plan);
264
+ return plan;
265
+ }
266
+ function readFields(r, fields, schemas, scope, table, checks) {
267
+ const { headerBytes, steps } = fieldReadPlan(fields);
268
+ const header = headerBytes === 0 ? r.pos : takeBytes(r, headerBytes);
269
+ const { data } = r;
270
+ const result = {};
271
+ for (const step of steps) {
272
+ if ("scalars" in step) {
273
+ // The run fits in this payload view before any scalar read uses its precomputed offset.
274
+ const start = takeBytes(r, step.width);
275
+ const view = cursorView(r);
276
+ for (const { name, ty, offset } of step.scalars) {
277
+ setField(result, name, readFixedScalar(view, start + offset, ty));
278
+ }
279
+ continue;
280
+ }
281
+ const { name, ty, cast, presenceBit, constrained } = step;
282
+ if (presenceBit >= 0 &&
283
+ (data[header + (presenceBit >> 3)] & (1 << (presenceBit & 7))) === 0) {
284
+ setField(result, name, null);
285
+ continue;
286
+ }
287
+ const value = readValue(r, ty, schemas, scope, table, cast);
288
+ if (checks && constrained)
289
+ validate(ty, value, name);
290
+ setField(result, name, value);
291
+ }
292
+ return result;
293
+ }
294
+ function readFixedRecords(r, count, width, read) {
295
+ limitCollection(count);
296
+ // Reserve the entire block before any generated reader uses its offsets.
297
+ let pos = takeBytes(r, count * width);
298
+ const rows = [];
299
+ for (let i = 0; i < count; i++, pos += width)
300
+ rows.push(read(r, pos, cursorReads));
301
+ return rows;
302
+ }
303
+ function readRecordArray(r, count, table, read) {
304
+ limitCollection(count);
305
+ const rows = [];
306
+ for (let i = 0; i < count; i++)
307
+ rows.push(read(r, table, cursorReads));
308
+ return rows;
309
+ }
310
+ const boundLayouts = new WeakMap();
311
+ function bindField(field, schemas, scope) {
312
+ const ty = isOptional(field.ty) ? field.ty[1] : field.ty;
313
+ const schema = schemas[scope];
314
+ if (typeof ty !== "string" && ty[0] === "array") {
315
+ const [, element, fixedCount] = ty;
316
+ if (typeof element !== "string" && element[0] === "record") {
317
+ const key = element[2];
318
+ const decode = schema.cursorRecordReaders?.[key];
319
+ if (decode && typeof decode !== "function") {
320
+ return (r) => readFixedRecords(r, fixedCount ?? readLength(r), decode.width, decode.read);
321
+ }
322
+ if (decode) {
323
+ let read;
324
+ return (r, table) => {
325
+ const count = fixedCount ?? readLength(r);
326
+ if (count === 0)
327
+ return [];
328
+ // Resolve on first use so self-referential layouts can bind safely.
329
+ read ??= bindLayout(decode, schema.records[key], schemas, scope);
330
+ return readRecordArray(r, count, table, read);
331
+ };
332
+ }
333
+ }
334
+ }
335
+ return (r, table) => {
336
+ const value = readValue(r, ty, schemas, scope, table, field.cast);
337
+ validate(ty, value, field.name);
338
+ return value;
339
+ };
340
+ }
341
+ function bindLayout(decode, fields, schemas, scope) {
342
+ let bound = boundLayouts.get(decode);
343
+ if (bound !== undefined)
344
+ return bound;
345
+ const fieldsByIndex = fields.map((field) => bindField(field, schemas, scope));
346
+ const reads = {
347
+ ...cursorReads,
348
+ field: (r, table, index) => fieldsByIndex[index](r, table),
349
+ };
350
+ bound = (r, table) => decode(r, table, reads);
351
+ boundLayouts.set(decode, bound);
352
+ return bound;
353
+ }
354
+ function writeFieldsSinglePass(w, fields, value, schemas, scope, table, checks) {
355
+ const { bitmapBytes: headerBytes } = accessLayout(fields);
356
+ const header = w.pos;
357
+ if (headerBytes !== 0) {
358
+ takeWrite(w, headerBytes);
359
+ w.data.fill(0, header, header + headerBytes);
360
+ }
361
+ let bit = 0;
362
+ for (const field of fields) {
363
+ const item = value[field.name];
364
+ if (checks)
365
+ validate(field.ty, item, field.name);
366
+ if (isOptional(field.ty)) {
367
+ if (item != null) {
368
+ // The writer can grow while encoding an earlier field.
369
+ w.data[header + (bit >> 3)] |= 1 << (bit & 7);
370
+ writeValue(w, field.ty[1], item, schemas, scope, table, field.cast);
371
+ }
372
+ bit++;
373
+ }
374
+ else {
375
+ writeValue(w, field.ty, item, schemas, scope, table, field.cast);
376
+ }
377
+ }
378
+ }
379
+ function writeFields(w, fields, value, schemas, scope, table, checks) {
380
+ if (w.fillPresenceInPlace) {
381
+ writeFieldsSinglePass(w, fields, value, schemas, scope, table, checks);
382
+ return;
383
+ }
384
+ const header = new Uint8Array(accessLayout(fields).bitmapBytes);
385
+ let bit = 0;
386
+ for (const field of fields) {
387
+ if (isOptional(field.ty)) {
388
+ if (value[field.name] != null)
389
+ header[bit >> 3] |= 1 << (bit & 7);
390
+ bit++;
391
+ }
392
+ }
393
+ writeBytes(w, header);
394
+ for (const field of fields) {
395
+ const item = value[field.name];
396
+ if (checks)
397
+ validate(field.ty, item, field.name);
398
+ if (isOptional(field.ty)) {
399
+ if (item != null)
400
+ writeValue(w, field.ty[1], item, schemas, scope, table, field.cast);
401
+ }
402
+ else {
403
+ writeValue(w, field.ty, item, schemas, scope, table, field.cast);
404
+ }
405
+ }
406
+ }
407
+ function readPayload(r, schemas, scope, version) {
408
+ const schema = schemas[scope];
409
+ const source = versionLayout(schema, version);
410
+ const table = source.intern ? readTable(r) : emptyTable;
411
+ const value = source.decodeCursor
412
+ ? bindLayout(source.decodeCursor, source.fields, schemas, scope)(r, table)
413
+ : readFields(r, source.fields, schemas, scope, table, version === schema.latest);
414
+ if (version === schema.latest)
415
+ return value;
416
+ const latest = versionLayout(schema, schema.latest).fields;
417
+ const result = project(value, source.fields, latest, schemas, scope, false);
418
+ for (const field of latest)
419
+ validate(field.ty, result[field.name], field.name);
420
+ return result;
421
+ }
422
+ function writePayload(w, schemas, scope, version, value) {
423
+ const schema = schemas[scope];
424
+ const target = versionLayout(schema, version);
425
+ const latest = versionLayout(schema, schema.latest).fields;
426
+ const table = new Map();
427
+ let wire = value;
428
+ if (version !== schema.latest) {
429
+ for (const field of latest)
430
+ validate(field.ty, value[field.name], field.name);
431
+ wire = project(value, latest, target.fields, schemas, scope, true);
432
+ }
433
+ const start = w.pos;
434
+ writeFields(w, target.fields, wire, schemas, scope, table, version === schema.latest);
435
+ if (target.intern)
436
+ prependInternTable(w, start, table);
437
+ }
438
+ export { readValue, validate, bindLayout, readPayload, writePayload, readFixedRecords, readRecordArray, };
@@ -0,0 +1,17 @@
1
+ import { type WriteCursor, type WriteInterner } from "./writer.js";
2
+ declare function writeU8(w: WriteCursor, value: unknown): void;
3
+ declare function writeI8(w: WriteCursor, value: unknown): void;
4
+ declare function writeU16(w: WriteCursor, value: unknown): void;
5
+ declare function writeI16(w: WriteCursor, value: unknown): void;
6
+ declare function writeU32(w: WriteCursor, value: unknown): void;
7
+ declare function writeI32(w: WriteCursor, value: unknown): void;
8
+ declare function writeU64(w: WriteCursor, value: unknown): void;
9
+ declare function writeI64(w: WriteCursor, value: unknown): void;
10
+ declare function writeF32(w: WriteCursor, value: unknown): void;
11
+ declare function writeF64(w: WriteCursor, value: unknown): void;
12
+ declare function writeBool(w: WriteCursor, value: unknown): void;
13
+ declare function writeVarint32Value(w: WriteCursor, value: unknown): void;
14
+ declare function writeVarint64Value(w: WriteCursor, value: unknown): void;
15
+ declare function writeNewIntern(w: WriteCursor, value: string, entries: Map<string, number>): void;
16
+ declare function writeIntern(w: WriteCursor, value: unknown, table: WriteInterner | undefined): void;
17
+ export { writeU8, writeI8, writeU16, writeI16, writeU32, writeI32, writeU64, writeI64, writeF32, writeF64, writeBool, writeVarint32Value, writeVarint64Value, writeNewIntern, writeIntern, };