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,171 @@
1
+ import { castScalar } from "./compiled.js";
2
+ import { bytesAsInt, integerKind, resized, sequence, typedValues, unsignedBytes, } from "./scalars.js";
3
+ import { object, setField } from "./schema.js";
4
+ function defaultValue(ty, supplied, schemas, scope) {
5
+ const schema = schemas[scope];
6
+ const plan = Array.isArray(supplied) ? supplied : [];
7
+ if (typeof ty === "string") {
8
+ if (ty === "string" || ty === "intern")
9
+ return supplied ?? "";
10
+ if (ty === "bool")
11
+ return supplied ?? false;
12
+ const value = plan[0] === "bytes"
13
+ ? bytesAsInt(Uint8Array.from(plan[1]))
14
+ : plan[0] === "integer"
15
+ ? plan[1]
16
+ : (supplied ?? 0);
17
+ if (integerKind(ty)?.[0] === 64)
18
+ return BigInt(value);
19
+ return ty === "f32" ? Math.fround(Number(value)) : Number(value);
20
+ }
21
+ switch (ty[0]) {
22
+ case "optional":
23
+ return supplied == null
24
+ ? null
25
+ : defaultValue(ty[1], supplied, schemas, scope);
26
+ case "box":
27
+ case "constraint":
28
+ return defaultValue(ty[1], supplied, schemas, scope);
29
+ case "bytes":
30
+ case "bits": {
31
+ const width = ty[0] === "bits" ? ty[2] : ty[1];
32
+ if (plan[0] === "bytes")
33
+ return resized(Uint8Array.from(plan[1]), width);
34
+ if (plan[0] === "integer")
35
+ return unsignedBytes(BigInt(plan[1]), width);
36
+ return new Uint8Array(width);
37
+ }
38
+ case "enum":
39
+ case "vfloat":
40
+ return Number(plan[0] === "integer" ? plan[1] : (supplied ?? 0));
41
+ case "union": {
42
+ const first = ty[3][0];
43
+ if (first === undefined)
44
+ return { kind: "Unknown", discriminant: 0, data: new Uint8Array() };
45
+ return {
46
+ kind: first[1],
47
+ value: defaultValue(first[2], null, schemas, scope),
48
+ };
49
+ }
50
+ case "record":
51
+ return defaults(schema.records[ty[2]], {}, schemas, scope);
52
+ case "import": {
53
+ const imported = schema.imports[ty[1]];
54
+ const target = schemas[imported];
55
+ return defaults(target.roots[target.latest].fields, {}, schemas, imported);
56
+ }
57
+ case "tuple":
58
+ return ty[1].map((item, i) => defaultValue(item, plan[1]?.[i], schemas, scope));
59
+ case "array":
60
+ case "delta": {
61
+ const items = plan[0] === "items" ? plan[1] : [];
62
+ const result = Array.from({ length: ty[2] ?? items.length }, (_, i) => defaultValue(ty[1], plan[0] === "repeat" ? plan[1] : items[i], schemas, scope));
63
+ return typedValues(ty[1], result);
64
+ }
65
+ case "map": {
66
+ const items = (plan[0] === "pairs" ? plan[1] : []);
67
+ const result = items.map(([key, value]) => [
68
+ defaultValue(ty[1], key, schemas, scope),
69
+ defaultValue(ty[2], value, schemas, scope),
70
+ ]);
71
+ if (ty[3] === null)
72
+ return new Map(result);
73
+ while (result.length < ty[3]) {
74
+ result.push([
75
+ defaultValue(ty[1], null, schemas, scope),
76
+ defaultValue(ty[2], null, schemas, scope),
77
+ ]);
78
+ }
79
+ return result.slice(0, ty[3]);
80
+ }
81
+ }
82
+ }
83
+ function defaults(fields, value, schemas, scope) {
84
+ const result = {};
85
+ for (const field of fields) {
86
+ setField(result, field.name, Object.prototype.hasOwnProperty.call(value, field.name)
87
+ ? value[field.name]
88
+ : defaultValue(field.ty, field.default, schemas, scope));
89
+ }
90
+ return result;
91
+ }
92
+ function adapt(value, from, to, schemas, scope, down) {
93
+ if (value == null)
94
+ return defaultValue(to, null, schemas, scope);
95
+ if (typeof from !== "string" &&
96
+ (from[0] === "optional" || from[0] === "box" || from[0] === "constraint")) {
97
+ return adapt(value, from[1], to, schemas, scope, down);
98
+ }
99
+ if (typeof to !== "string" &&
100
+ (to[0] === "optional" || to[0] === "box" || to[0] === "constraint")) {
101
+ return adapt(value, from, to[1], schemas, scope, down);
102
+ }
103
+ if (typeof from === "string" && typeof to === "string" && from !== to)
104
+ return castScalar(value, from === "f32" || from === "f64", to);
105
+ if (typeof from !== "string" &&
106
+ from[0] === "bits" &&
107
+ typeof to === "string") {
108
+ return castScalar(bytesAsInt(value), false, to);
109
+ }
110
+ if (typeof to !== "string" && to[0] === "bits" && typeof from === "string") {
111
+ return unsignedBytes(BigInt(value), to[2]);
112
+ }
113
+ if (typeof from === "string" || typeof to === "string")
114
+ return value;
115
+ if (from[0] === "record" && to[0] === "record") {
116
+ const records = schemas[scope].records;
117
+ return project(object(value), records[down ? from[2] : from[1]], records[down ? to[1] : to[2]], schemas, scope, down);
118
+ }
119
+ if (to[0] === "bytes")
120
+ return resized(value, to[1]);
121
+ if (to[0] === "bits")
122
+ return resized(value, to[2]);
123
+ if (to[0] === "array" || to[0] === "delta") {
124
+ const items = from[0] === "map"
125
+ ? Array.from(value.values())
126
+ : sequence(value);
127
+ const result = Array.from({ length: to[2] ?? items.length }, (_, i) => i < items.length ? items[i] : defaultValue(to[1], null, schemas, scope));
128
+ return typedValues(to[1], result);
129
+ }
130
+ if (to[0] === "map" && from[0] === "array") {
131
+ const items = sequence(value);
132
+ return new Map(Array.from({ length: items.length }, (_, i) => [i, items[i]]));
133
+ }
134
+ if (to[0] === "map" && to[3] !== null) {
135
+ const items = value.slice(0, to[3]);
136
+ if (down) {
137
+ while (items.length < to[3]) {
138
+ items.push([
139
+ defaultValue(to[1], null, schemas, scope),
140
+ defaultValue(to[2], null, schemas, scope),
141
+ ]);
142
+ }
143
+ }
144
+ return items;
145
+ }
146
+ return value;
147
+ }
148
+ function project(value, from, to, schemas, scope, down) {
149
+ const source = new Map(from.map((field) => [field.id, field]));
150
+ const result = {};
151
+ for (const field of to) {
152
+ const old = source.get(field.id);
153
+ const prior = old && value[old.name];
154
+ const cast = down ? field.cast : old?.cast;
155
+ let current;
156
+ if (!old)
157
+ current = defaultValue(field.ty, down ? null : field.default, schemas, scope);
158
+ else if (cast && (down ? old.lazy : field.lazy)) {
159
+ // Rust drops a value when a historical cast crosses into a lazy target.
160
+ current = defaultValue(field.ty, null, schemas, scope);
161
+ }
162
+ else if (cast || JSON.stringify(old.ty) !== JSON.stringify(field.ty)) {
163
+ current = adapt(prior, old.ty, field.ty, schemas, scope, down);
164
+ }
165
+ else
166
+ current = prior;
167
+ setField(result, field.name, current);
168
+ }
169
+ return result;
170
+ }
171
+ export { defaults, project };
package/dist/wire.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ import type { Cursor, CursorOps, Input, ReadTable } from "./types.js";
2
+ declare const textEncoder: TextEncoder;
3
+ declare function invalid(reason: string): never;
4
+ declare function checkedVarint32(value: unknown): number;
5
+ declare function varint32Size(value: number): number;
6
+ declare function writeVarint32(data: Uint8Array, pos: number, value: number): number;
7
+ declare function cursorView(r: Cursor): DataView;
8
+ declare function takeBytes(r: Cursor, count: number): number;
9
+ declare function readBytes(r: Cursor, count: number): Uint8Array;
10
+ declare function readByte(r: Cursor): number;
11
+ declare function readUint(r: Cursor, width: number): number;
12
+ declare function readVarint(r: Cursor): bigint;
13
+ declare function readVarint32(r: Cursor, extendedLength?: boolean): number;
14
+ declare function readLength(r: Cursor): number;
15
+ declare function readVarint32Array(r: Cursor, count: number): number[];
16
+ declare function readString(r: Cursor): string;
17
+ declare function readIntern(r: Cursor, table: ReadTable): string;
18
+ declare const cursorReads: CursorOps;
19
+ declare function limitCollection(count: number): void;
20
+ declare function tableLength(r: Cursor): number;
21
+ declare function readTable(r: Cursor): ReadTable;
22
+ declare function readTableOffsets(r: Cursor): ReadTable;
23
+ declare const emptyTable: ReadTable;
24
+ type Envelope = Cursor & {
25
+ version: bigint;
26
+ };
27
+ declare function openEnvelope(input: Input, offset: number, current: bigint, shortVersion: number): Envelope;
28
+ declare function openSchemaEnvelope(input: Input, offset: number, current: bigint, shortVersion: number, currentSize: number | undefined, fixedSize: (version: bigint) => number | undefined): Envelope;
29
+ export type { Envelope };
30
+ export { textEncoder, invalid, checkedVarint32, varint32Size, writeVarint32, cursorView, takeBytes, readBytes, readByte, readUint, readVarint, readVarint32, readLength, readVarint32Array, readString, readIntern, cursorReads, limitCollection, tableLength, readTable, readTableOffsets, emptyTable, openEnvelope, openSchemaEnvelope, };
package/dist/wire.js ADDED
@@ -0,0 +1,276 @@
1
+ const textEncoder = new TextEncoder();
2
+ const textDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
3
+ function invalid(reason) {
4
+ throw new RangeError(reason);
5
+ }
6
+ function checkedVarint32(value) {
7
+ if (typeof value !== "number" || value >>> 0 !== value) {
8
+ invalid("Integer out of range for varint32");
9
+ }
10
+ return value;
11
+ }
12
+ function varint32Size(value) {
13
+ if (value < 0x80)
14
+ return 1;
15
+ if (value < 0x4000)
16
+ return 2;
17
+ if (value < 0x20_0000)
18
+ return 3;
19
+ if (value < 0x1000_0000)
20
+ return 4;
21
+ return 5;
22
+ }
23
+ function writeVarint32(data, pos, value) {
24
+ while (value >= 128) {
25
+ data[pos++] = (value & 127) | 128;
26
+ value >>>= 7;
27
+ }
28
+ data[pos++] = value;
29
+ return pos;
30
+ }
31
+ function decodeText(data, start, end) {
32
+ if (end - start <= 32) {
33
+ let value = "";
34
+ for (let i = start; i < end; i++) {
35
+ const byte = data[i];
36
+ if (byte >= 0x80)
37
+ return textDecoder.decode(data.subarray(start, end));
38
+ value += String.fromCharCode(byte);
39
+ }
40
+ return value;
41
+ }
42
+ return textDecoder.decode(data.subarray(start, end));
43
+ }
44
+ function cursorView(r) {
45
+ const data = r.data;
46
+ return (r.view ??= new DataView(data.buffer, data.byteOffset, data.byteLength));
47
+ }
48
+ function takeBytes(r, count) {
49
+ const start = r.pos;
50
+ if (!Number.isSafeInteger(count) || count < 0 || count > r.end - start) {
51
+ invalid("Unexpected end of Pojoc payload");
52
+ }
53
+ r.pos = start + count;
54
+ return start;
55
+ }
56
+ function readBytes(r, count) {
57
+ const start = takeBytes(r, count);
58
+ return r.data.subarray(start, r.pos);
59
+ }
60
+ function readByte(r) {
61
+ return r.data[takeBytes(r, 1)];
62
+ }
63
+ function readUint(r, width) {
64
+ const start = takeBytes(r, width);
65
+ if (width === 1)
66
+ return r.data[start];
67
+ const data = r.data;
68
+ const low = data[start] | (data[start + 1] << 8);
69
+ if (width === 2)
70
+ return low;
71
+ return (low | (data[start + 2] << 16) | (data[start + 3] << 24)) >>> 0;
72
+ }
73
+ function readVarint(r) {
74
+ let byte = readByte(r);
75
+ if (byte < 128)
76
+ return BigInt(byte);
77
+ let value = BigInt(byte & 127);
78
+ for (let shift = 7n; shift <= 63n; shift += 7n) {
79
+ byte = readByte(r);
80
+ if (shift === 63n && byte > 1)
81
+ invalid("Pojoc varint overflow");
82
+ value |= BigInt(byte & 127) << shift;
83
+ if (byte < 128)
84
+ return value;
85
+ }
86
+ return invalid("Pojoc varint overflow");
87
+ }
88
+ function readVarint32(r, extendedLength = false) {
89
+ const { data, end } = r;
90
+ let pos = r.pos;
91
+ if (pos >= end)
92
+ invalid("Unexpected end of Pojoc payload");
93
+ const first = data[pos++];
94
+ if (first < 128) {
95
+ r.pos = pos;
96
+ return first;
97
+ }
98
+ if (end - pos < 4)
99
+ return readVarint32Slow(r, extendedLength);
100
+ let value = first & 127;
101
+ let byte = data[pos++];
102
+ value |= (byte & 127) << 7;
103
+ if (byte >= 128) {
104
+ byte = data[pos++];
105
+ value |= (byte & 127) << 14;
106
+ if (byte >= 128) {
107
+ byte = data[pos++];
108
+ value |= (byte & 127) << 21;
109
+ if (byte >= 128) {
110
+ byte = data[pos++];
111
+ if (byte > 15) {
112
+ if (extendedLength)
113
+ return readVarint32Slow(r, true);
114
+ invalid("Pojoc varint32 overflow");
115
+ }
116
+ value |= byte << 28;
117
+ }
118
+ }
119
+ }
120
+ r.pos = pos;
121
+ return value >>> 0;
122
+ }
123
+ function readVarint32Slow(r, extendedLength) {
124
+ const first = readByte(r);
125
+ let value = first & 127;
126
+ for (let shift = 7; shift <= 28; shift += 7) {
127
+ const byte = readByte(r);
128
+ const bits = byte & 127;
129
+ if (shift === 28 && bits > 15)
130
+ invalid("Pojoc varint32 overflow");
131
+ value |= bits << shift;
132
+ if (byte < 128)
133
+ return value >>> 0;
134
+ }
135
+ // Lengths are encoded as u64 on the wire, but this target limits them to u32.
136
+ if (extendedLength) {
137
+ for (let i = 5; i < 10; i++) {
138
+ const byte = readByte(r);
139
+ if (byte === 0)
140
+ return value >>> 0;
141
+ if (byte !== 128)
142
+ break;
143
+ }
144
+ }
145
+ return invalid("Pojoc varint32 overflow");
146
+ }
147
+ function readLength(r) {
148
+ return readVarint32(r, true);
149
+ }
150
+ function readVarint32Array(r, count) {
151
+ if (count > r.end - r.pos)
152
+ invalid("Unexpected end of Pojoc payload");
153
+ const result = [];
154
+ for (let i = 0; i < count; i++)
155
+ result.push(readVarint32(r));
156
+ return result;
157
+ }
158
+ function readString(r) {
159
+ const count = readLength(r);
160
+ const start = r.pos;
161
+ const end = start + count;
162
+ if (end > r.end)
163
+ invalid("Unexpected end of Pojoc payload");
164
+ r.pos = end;
165
+ return decodeText(r.data, start, end);
166
+ }
167
+ function readIntern(r, table) {
168
+ const index = readLength(r);
169
+ if (index >= table.length)
170
+ invalid("Invalid Pojoc intern index");
171
+ const entry = table[index];
172
+ return typeof entry === "string"
173
+ ? entry
174
+ : resolveIntern(r, table, index, entry);
175
+ }
176
+ function resolveIntern(r, table, index, entry) {
177
+ const pos = r.pos;
178
+ r.pos = entry;
179
+ try {
180
+ const value = readString(r);
181
+ table[index] = value;
182
+ return value;
183
+ }
184
+ finally {
185
+ r.pos = pos;
186
+ }
187
+ }
188
+ const cursorReads = {
189
+ take: takeBytes,
190
+ view: cursorView,
191
+ varint32: readVarint32,
192
+ varint: readVarint,
193
+ string: readString,
194
+ intern: readIntern,
195
+ };
196
+ function limitCollection(count) {
197
+ // Variable-size values can consume zero bytes, so byte bounds alone cannot
198
+ // rule out a huge number of objects in an otherwise tiny envelope.
199
+ if (count > 16_777_216)
200
+ invalid("Pojoc collection exceeds the object decode limit");
201
+ }
202
+ function tableLength(r) {
203
+ const count = readLength(r);
204
+ if (count > r.end - r.pos)
205
+ invalid("Invalid Pojoc intern table length");
206
+ limitCollection(count);
207
+ return count;
208
+ }
209
+ function readTable(r) {
210
+ const count = tableLength(r);
211
+ const table = [];
212
+ for (let i = 0; i < count; i++)
213
+ table.push(readString(r));
214
+ return table;
215
+ }
216
+ function readTableOffsets(r) {
217
+ const count = tableLength(r);
218
+ if (count === 0)
219
+ return emptyTable;
220
+ const table = [];
221
+ for (let i = 0; i < count; i++) {
222
+ table.push(r.pos);
223
+ takeBytes(r, readLength(r));
224
+ }
225
+ return table;
226
+ }
227
+ const emptyTable = [];
228
+ function openEnvelope(input, offset, current, shortVersion) {
229
+ const data = input instanceof Uint8Array ? input : new Uint8Array(input);
230
+ if (!Number.isSafeInteger(offset) || offset < 0 || offset > data.length)
231
+ invalid("Invalid Pojoc offset");
232
+ const currentVersion = data[offset] === shortVersion;
233
+ const firstLength = data[offset + 1];
234
+ if (currentVersion && firstLength < 128) {
235
+ const pos = offset + 2;
236
+ const size = firstLength;
237
+ if (size > data.length - pos)
238
+ invalid("Unexpected end of Pojoc payload");
239
+ return { data, view: undefined, pos, end: pos + size, version: current };
240
+ }
241
+ const envelope = {
242
+ data,
243
+ view: undefined,
244
+ pos: currentVersion ? offset + 1 : offset,
245
+ end: data.length,
246
+ version: current,
247
+ };
248
+ if (!currentVersion)
249
+ envelope.version = readVarint(envelope);
250
+ const size = readVarint32(envelope);
251
+ if (size > envelope.end - envelope.pos)
252
+ invalid("Unexpected end of Pojoc payload");
253
+ envelope.end = envelope.pos + size;
254
+ return envelope;
255
+ }
256
+ function openSchemaEnvelope(input, offset, current, shortVersion, currentSize, fixedSize) {
257
+ const data = input instanceof Uint8Array ? input : new Uint8Array(input);
258
+ if (!Number.isSafeInteger(offset) || offset < 0 || offset > data.length)
259
+ invalid("Invalid Pojoc offset");
260
+ const latest = data[offset] === shortVersion;
261
+ const r = {
262
+ data,
263
+ view: undefined,
264
+ pos: latest ? offset + 1 : offset,
265
+ end: data.length,
266
+ version: current,
267
+ };
268
+ if (!latest)
269
+ r.version = readVarint(r);
270
+ const size = (latest ? currentSize : fixedSize(r.version)) ?? readUint(r, 4);
271
+ if (size > r.end - r.pos)
272
+ invalid("Unexpected end of Pojoc payload");
273
+ r.end = r.pos + size;
274
+ return r;
275
+ }
276
+ export { textEncoder, invalid, checkedVarint32, varint32Size, writeVarint32, cursorView, takeBytes, readBytes, readByte, readUint, readVarint, readVarint32, readLength, readVarint32Array, readString, readIntern, cursorReads, limitCollection, tableLength, readTable, readTableOffsets, emptyTable, openEnvelope, openSchemaEnvelope, };
@@ -0,0 +1,28 @@
1
+ type WriteCursor = {
2
+ data: Uint8Array<ArrayBuffer>;
3
+ pos: number;
4
+ view: DataView | undefined;
5
+ readonly fillPresenceInPlace: boolean;
6
+ };
7
+ type WriteInterner = {
8
+ first: string | undefined;
9
+ second: string | undefined;
10
+ entries: Map<string, number> | undefined;
11
+ };
12
+ declare function beginWrite(fillPresenceInPlace: boolean): WriteCursor;
13
+ declare function writeView(w: WriteCursor): DataView;
14
+ declare function writeU32At(data: Uint8Array, pos: number, value: number): void;
15
+ declare function takeWrite(w: WriteCursor, count: number): number;
16
+ declare function writeBytes(w: WriteCursor, value: Uint8Array): void;
17
+ declare function writeByte(w: WriteCursor, value: number): void;
18
+ declare function writeUint(w: WriteCursor, width: number, value: number): void;
19
+ declare function writeVarint(w: WriteCursor, value: bigint): void;
20
+ declare function writeLength(w: WriteCursor, value: number): void;
21
+ declare function writeU32Varint(w: WriteCursor, value: number): void;
22
+ declare function writeVarint32Array(w: WriteCursor, items: ArrayLike<unknown>): void;
23
+ declare function writeString(w: WriteCursor, value: unknown): void;
24
+ declare function finishWrite(w: WriteCursor): Uint8Array;
25
+ declare function finishEnvelope(writer: WriteCursor, length: number): Uint8Array<ArrayBuffer>;
26
+ declare function prependInternTable(w: WriteCursor, bodyStart: number, table: Map<string, number> | WriteInterner): void;
27
+ export type { WriteCursor, WriteInterner };
28
+ export { beginWrite, writeView, writeU32At, takeWrite, writeBytes, writeByte, writeUint, writeVarint, writeLength, writeU32Varint, writeVarint32Array, writeString, finishWrite, finishEnvelope, prependInternTable, };
package/dist/writer.js ADDED
@@ -0,0 +1,180 @@
1
+ import { checkedVarint32, invalid, textEncoder, varint32Size, writeVarint32, } from "./wire.js";
2
+ const loneSurrogate = /[\uD800-\uDFFF]/u;
3
+ const maxU64 = 0xffffffffffffffffn;
4
+ // Bytes past pos remain zero until an append uses them, including bitmap slots.
5
+ function beginWrite(fillPresenceInPlace) {
6
+ return {
7
+ data: new Uint8Array(128),
8
+ pos: 0,
9
+ view: undefined,
10
+ fillPresenceInPlace,
11
+ };
12
+ }
13
+ function writeView(w) {
14
+ return (w.view ??= new DataView(w.data.buffer));
15
+ }
16
+ function writeU32At(data, pos, value) {
17
+ data[pos] = value;
18
+ data[pos + 1] = value >>> 8;
19
+ data[pos + 2] = value >>> 16;
20
+ data[pos + 3] = value >>> 24;
21
+ }
22
+ function reserveWrite(w, end) {
23
+ if (end > w.data.length) {
24
+ if (end > 0xffff_ffff + 16)
25
+ invalid("Pojoc payload too large");
26
+ const next = new Uint8Array(Math.min(0xffff_ffff + 16, Math.max(end, w.data.length * 2)));
27
+ next.set(w.data);
28
+ w.data = next;
29
+ w.view = undefined;
30
+ }
31
+ }
32
+ function takeWrite(w, count) {
33
+ const start = w.pos;
34
+ const end = start + count;
35
+ reserveWrite(w, end);
36
+ w.pos = end;
37
+ return start;
38
+ }
39
+ function writeBytes(w, value) {
40
+ const start = takeWrite(w, value.length);
41
+ w.data.set(value, start);
42
+ }
43
+ function writeByte(w, value) {
44
+ const start = takeWrite(w, 1);
45
+ w.data[start] = value;
46
+ }
47
+ function writeUint(w, width, value) {
48
+ const start = takeWrite(w, width);
49
+ if (width === 1)
50
+ w.data[start] = value;
51
+ else if (width === 2) {
52
+ w.data[start] = value;
53
+ w.data[start + 1] = value >>> 8;
54
+ }
55
+ else
56
+ writeU32At(w.data, start, value);
57
+ }
58
+ function writeVarint(w, value) {
59
+ if (value < 0n || value > maxU64)
60
+ invalid("Expected unsigned 64-bit varint");
61
+ while (value >= 128n) {
62
+ writeByte(w, Number(value & 127n) | 128);
63
+ value >>= 7n;
64
+ }
65
+ writeByte(w, Number(value));
66
+ }
67
+ function writeLength(w, value) {
68
+ if (typeof value !== "number" || value !== value >>> 0)
69
+ invalid("Invalid Pojoc length");
70
+ writeU32Varint(w, value);
71
+ }
72
+ function writeU32Varint(w, value) {
73
+ if (value < 0x80 && w.pos < w.data.length) {
74
+ w.data[w.pos++] = value;
75
+ return;
76
+ }
77
+ const start = takeWrite(w, varint32Size(value));
78
+ writeVarint32(w.data, start, value);
79
+ }
80
+ function writeVarint32Array(w, items) {
81
+ const count = items.length;
82
+ let pos = w.pos;
83
+ reserveWrite(w, pos + count);
84
+ let data = w.data;
85
+ for (let i = 0; i < count; i++) {
86
+ const value = checkedVarint32(items[i]);
87
+ if (data.length - pos < 5) {
88
+ reserveWrite(w, pos + varint32Size(value));
89
+ data = w.data;
90
+ }
91
+ pos = writeVarint32(data, pos, value);
92
+ }
93
+ w.pos = pos;
94
+ }
95
+ function writeString(w, value) {
96
+ if (typeof value !== "string")
97
+ throw new TypeError("Expected a Pojoc string");
98
+ if (value.length < 0x80) {
99
+ const start = w.pos;
100
+ const end = start + 1 + value.length;
101
+ reserveWrite(w, end);
102
+ const data = w.data;
103
+ let at = start + 1;
104
+ for (let i = 0; i < value.length; i++) {
105
+ const byte = value.charCodeAt(i);
106
+ if (byte >= 0x80)
107
+ break;
108
+ data[at++] = byte;
109
+ }
110
+ if (at === end) {
111
+ data[start] = value.length;
112
+ w.pos = end;
113
+ return;
114
+ }
115
+ // Bound the short UTF-8 write, then fill in its actual length.
116
+ if (loneSurrogate.test(value))
117
+ throw new TypeError("Invalid Unicode string");
118
+ reserveWrite(w, start + 2 + value.length * 3);
119
+ const written = textEncoder.encodeInto(value, w.data.subarray(start + 1)).written;
120
+ if (written >= 0x80)
121
+ w.data.copyWithin(start + 2, start + 1, start + 1 + written);
122
+ w.pos = writeVarint32(w.data, start, written) + written;
123
+ return;
124
+ }
125
+ // Native encoding is faster than sizing long strings in JavaScript.
126
+ if (loneSurrogate.test(value))
127
+ throw new TypeError("Invalid Unicode string");
128
+ const bytes = textEncoder.encode(value);
129
+ writeLength(w, bytes.length);
130
+ writeBytes(w, bytes);
131
+ }
132
+ function finishWrite(w) {
133
+ return w.data.subarray(0, w.pos);
134
+ }
135
+ function finishEnvelope(writer, length) {
136
+ const bytes = writer.pos - length - 5;
137
+ if (bytes > 0xffff_ffff)
138
+ invalid("Pojoc payload exceeds its envelope");
139
+ // Place the header next to the payload inside the reserved space.
140
+ const start = 5 - varint32Size(bytes);
141
+ const data = writer.data;
142
+ if (start !== 0) {
143
+ if (length === 1)
144
+ data[start] = data[0];
145
+ else
146
+ data.copyWithin(start, 0, length);
147
+ }
148
+ writeVarint32(data, start + length, bytes);
149
+ return data.subarray(start, writer.pos);
150
+ }
151
+ function prependInternTable(w, bodyStart, table) {
152
+ const bodyEnd = w.pos;
153
+ writeInternTable(w, table);
154
+ if (bodyStart === bodyEnd)
155
+ return;
156
+ // The table length is known after encoding the body. Move the body in place.
157
+ const prefix = w.data.slice(bodyEnd, w.pos);
158
+ w.data.copyWithin(bodyStart + prefix.length, bodyStart, bodyEnd);
159
+ w.data.set(prefix, bodyStart);
160
+ }
161
+ function writeInternTable(w, table) {
162
+ if (!(table instanceof Map) && !table.entries) {
163
+ const { first, second } = table;
164
+ if (first === undefined) {
165
+ writeByte(w, 0);
166
+ }
167
+ else {
168
+ writeByte(w, second === undefined ? 1 : 2);
169
+ writeString(w, first);
170
+ if (second !== undefined)
171
+ writeString(w, second);
172
+ }
173
+ return;
174
+ }
175
+ const entries = table instanceof Map ? table : table.entries;
176
+ writeLength(w, entries.size);
177
+ for (const text of entries.keys())
178
+ writeString(w, text);
179
+ }
180
+ export { beginWrite, writeView, writeU32At, takeWrite, writeBytes, writeByte, writeUint, writeVarint, writeLength, writeU32Varint, writeVarint32Array, writeString, finishWrite, finishEnvelope, prependInternTable, };