pojoc 0.7.0 → 0.8.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/README.md +17 -4
- package/dist/access.d.ts +30 -0
- package/dist/access.js +174 -0
- package/dist/codec.d.ts +12 -0
- package/dist/codec.js +127 -0
- package/dist/compiled.d.ts +43 -0
- package/dist/compiled.js +290 -0
- package/dist/compiler.d.ts +8 -0
- package/dist/compiler.js +8 -0
- package/dist/field_access.d.ts +16 -0
- package/dist/field_access.js +56 -0
- package/dist/index.d.ts +2 -42
- package/dist/index.js +1 -1117
- package/dist/legacy.d.ts +21 -0
- package/dist/legacy.js +28 -0
- package/dist/legacy_scalar.d.ts +4 -0
- package/dist/legacy_scalar.js +35 -0
- package/dist/legacy_types.d.ts +35 -0
- package/dist/legacy_types.js +1 -0
- package/dist/payload.d.ts +11 -0
- package/dist/payload.js +438 -0
- package/dist/scalar_writes.d.ts +17 -0
- package/dist/scalar_writes.js +114 -0
- package/dist/scalars.d.ts +24 -0
- package/dist/scalars.js +291 -0
- package/dist/schema.d.ts +23 -0
- package/dist/schema.js +47 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.js +1 -0
- package/dist/versions.d.ts +5 -0
- package/dist/versions.js +171 -0
- package/dist/wire.d.ts +31 -0
- package/dist/wire.js +279 -0
- package/dist/writer.d.ts +28 -0
- package/dist/writer.js +180 -0
- package/package.json +5 -1
package/dist/index.js
CHANGED
|
@@ -1,1117 +1 @@
|
|
|
1
|
-
|
|
2
|
-
u8: Uint8Array, i8: Int8Array, u16: Uint16Array, i16: Int16Array,
|
|
3
|
-
u32: Uint32Array, i32: Int32Array, u64: BigUint64Array, i64: BigInt64Array,
|
|
4
|
-
f32: Float32Array, f64: Float64Array,
|
|
5
|
-
};
|
|
6
|
-
const littleEndian = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
7
|
-
const textEncoder = new TextEncoder();
|
|
8
|
-
const textDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
9
|
-
const loneSurrogate = /[\uD800-\uDFFF]/u;
|
|
10
|
-
const maxU64 = 0xffffffffffffffffn;
|
|
11
|
-
function invalid(reason) {
|
|
12
|
-
throw new RangeError(reason);
|
|
13
|
-
}
|
|
14
|
-
function checkedVarint32(value) {
|
|
15
|
-
if (typeof value !== "number" || (value >>> 0) !== value) {
|
|
16
|
-
invalid("Integer out of range for varint32");
|
|
17
|
-
}
|
|
18
|
-
return value;
|
|
19
|
-
}
|
|
20
|
-
function varint32Size(value) {
|
|
21
|
-
if (value < 0x80)
|
|
22
|
-
return 1;
|
|
23
|
-
if (value < 0x4000)
|
|
24
|
-
return 2;
|
|
25
|
-
if (value < 0x20_0000)
|
|
26
|
-
return 3;
|
|
27
|
-
if (value < 0x1000_0000)
|
|
28
|
-
return 4;
|
|
29
|
-
return 5;
|
|
30
|
-
}
|
|
31
|
-
function writeVarint32(data, pos, value) {
|
|
32
|
-
while (value >= 128) {
|
|
33
|
-
data[pos++] = (value & 127) | 128;
|
|
34
|
-
value >>>= 7;
|
|
35
|
-
}
|
|
36
|
-
data[pos++] = value;
|
|
37
|
-
return pos;
|
|
38
|
-
}
|
|
39
|
-
class Reader {
|
|
40
|
-
data;
|
|
41
|
-
view;
|
|
42
|
-
pos = 0;
|
|
43
|
-
constructor(data) {
|
|
44
|
-
this.data = data;
|
|
45
|
-
this.view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
46
|
-
}
|
|
47
|
-
take(count) {
|
|
48
|
-
if (!Number.isSafeInteger(count) || count < 0 || count > this.data.length - this.pos) {
|
|
49
|
-
invalid("Unexpected end of Pojoc payload");
|
|
50
|
-
}
|
|
51
|
-
const start = this.pos;
|
|
52
|
-
this.pos += count;
|
|
53
|
-
return start;
|
|
54
|
-
}
|
|
55
|
-
bytes(count) {
|
|
56
|
-
const start = this.take(count);
|
|
57
|
-
return this.data.subarray(start, this.pos);
|
|
58
|
-
}
|
|
59
|
-
byte() { return this.view.getUint8(this.take(1)); }
|
|
60
|
-
uint(width) {
|
|
61
|
-
const start = this.take(width);
|
|
62
|
-
if (width === 1)
|
|
63
|
-
return this.view.getUint8(start);
|
|
64
|
-
if (width === 2)
|
|
65
|
-
return this.view.getUint16(start, true);
|
|
66
|
-
return this.view.getUint32(start, true);
|
|
67
|
-
}
|
|
68
|
-
varint() {
|
|
69
|
-
let byte = this.byte();
|
|
70
|
-
if (byte < 128)
|
|
71
|
-
return BigInt(byte);
|
|
72
|
-
let value = BigInt(byte & 127);
|
|
73
|
-
for (let shift = 7n; shift <= 63n; shift += 7n) {
|
|
74
|
-
byte = this.byte();
|
|
75
|
-
if (shift === 63n && byte > 1)
|
|
76
|
-
invalid("Pojoc varint overflow");
|
|
77
|
-
value |= BigInt(byte & 127) << shift;
|
|
78
|
-
if (byte < 128)
|
|
79
|
-
return value;
|
|
80
|
-
}
|
|
81
|
-
return invalid("Pojoc varint overflow");
|
|
82
|
-
}
|
|
83
|
-
varint32(extendedLength = false) {
|
|
84
|
-
const data = this.data;
|
|
85
|
-
let pos = this.pos;
|
|
86
|
-
if (pos >= data.length)
|
|
87
|
-
invalid("Unexpected end of Pojoc payload");
|
|
88
|
-
const first = data[pos++];
|
|
89
|
-
if (first < 128) {
|
|
90
|
-
this.pos = pos;
|
|
91
|
-
return first;
|
|
92
|
-
}
|
|
93
|
-
if (data.length - pos < 4)
|
|
94
|
-
return this.varint32Slow(extendedLength);
|
|
95
|
-
let value = first & 127;
|
|
96
|
-
let byte = data[pos++];
|
|
97
|
-
value |= (byte & 127) << 7;
|
|
98
|
-
if (byte >= 128) {
|
|
99
|
-
byte = data[pos++];
|
|
100
|
-
value |= (byte & 127) << 14;
|
|
101
|
-
if (byte >= 128) {
|
|
102
|
-
byte = data[pos++];
|
|
103
|
-
value |= (byte & 127) << 21;
|
|
104
|
-
if (byte >= 128) {
|
|
105
|
-
byte = data[pos++];
|
|
106
|
-
if (byte > 15) {
|
|
107
|
-
if (extendedLength)
|
|
108
|
-
return this.varint32Slow(true);
|
|
109
|
-
invalid("Pojoc varint32 overflow");
|
|
110
|
-
}
|
|
111
|
-
value |= byte << 28;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
this.pos = pos;
|
|
116
|
-
return value >>> 0;
|
|
117
|
-
}
|
|
118
|
-
varint32Slow(extendedLength) {
|
|
119
|
-
const first = this.byte();
|
|
120
|
-
let value = first & 127;
|
|
121
|
-
for (let shift = 7; shift <= 28; shift += 7) {
|
|
122
|
-
const byte = this.byte();
|
|
123
|
-
const bits = byte & 127;
|
|
124
|
-
if (shift === 28 && bits > 15)
|
|
125
|
-
invalid("Pojoc varint32 overflow");
|
|
126
|
-
value |= bits << shift;
|
|
127
|
-
if (byte < 128)
|
|
128
|
-
return value >>> 0;
|
|
129
|
-
}
|
|
130
|
-
// Lengths are encoded as u64 on the wire, but this target limits them to u32.
|
|
131
|
-
if (extendedLength) {
|
|
132
|
-
for (let i = 5; i < 10; i++) {
|
|
133
|
-
const byte = this.byte();
|
|
134
|
-
if (byte === 0)
|
|
135
|
-
return value >>> 0;
|
|
136
|
-
if (byte !== 128)
|
|
137
|
-
break;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return invalid("Pojoc varint32 overflow");
|
|
141
|
-
}
|
|
142
|
-
length() { return this.varint32(true); }
|
|
143
|
-
varint32Array(count) {
|
|
144
|
-
if (count > this.data.length - this.pos)
|
|
145
|
-
invalid("Unexpected end of Pojoc payload");
|
|
146
|
-
const result = [];
|
|
147
|
-
for (let i = 0; i < count; i++)
|
|
148
|
-
result.push(this.varint32());
|
|
149
|
-
return result;
|
|
150
|
-
}
|
|
151
|
-
string() { return textDecoder.decode(this.bytes(this.length())); }
|
|
152
|
-
}
|
|
153
|
-
class Writer {
|
|
154
|
-
data = new Uint8Array(128);
|
|
155
|
-
view = new DataView(this.data.buffer);
|
|
156
|
-
pos = 0;
|
|
157
|
-
reserve(end) {
|
|
158
|
-
if (end > 0xffff_ffff + 16)
|
|
159
|
-
invalid("Pojoc payload too large");
|
|
160
|
-
if (end > this.data.length) {
|
|
161
|
-
const next = new Uint8Array(Math.min(0xffff_ffff + 16, Math.max(end, this.data.length * 2)));
|
|
162
|
-
next.set(this.data);
|
|
163
|
-
this.data = next;
|
|
164
|
-
this.view = new DataView(next.buffer);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
take(count) {
|
|
168
|
-
const start = this.pos;
|
|
169
|
-
const end = start + count;
|
|
170
|
-
this.reserve(end);
|
|
171
|
-
this.pos = end;
|
|
172
|
-
return start;
|
|
173
|
-
}
|
|
174
|
-
bytes(value) {
|
|
175
|
-
const start = this.take(value.length);
|
|
176
|
-
this.data.set(value, start);
|
|
177
|
-
}
|
|
178
|
-
byte(value) {
|
|
179
|
-
const start = this.take(1);
|
|
180
|
-
this.view.setUint8(start, value);
|
|
181
|
-
}
|
|
182
|
-
uint(width, value) {
|
|
183
|
-
const start = this.take(width);
|
|
184
|
-
if (width === 1)
|
|
185
|
-
this.view.setUint8(start, value);
|
|
186
|
-
else if (width === 2)
|
|
187
|
-
this.view.setUint16(start, value, true);
|
|
188
|
-
else
|
|
189
|
-
this.view.setUint32(start, value, true);
|
|
190
|
-
}
|
|
191
|
-
varint(value) {
|
|
192
|
-
if (value < 0n || value > maxU64)
|
|
193
|
-
invalid("Expected unsigned 64-bit varint");
|
|
194
|
-
while (value >= 128n) {
|
|
195
|
-
this.byte(Number(value & 127n) | 128);
|
|
196
|
-
value >>= 7n;
|
|
197
|
-
}
|
|
198
|
-
this.byte(Number(value));
|
|
199
|
-
}
|
|
200
|
-
length(value) {
|
|
201
|
-
if (!Number.isSafeInteger(value) || value < 0 || value > 0xffff_ffff)
|
|
202
|
-
invalid("Invalid Pojoc length");
|
|
203
|
-
this.varint32(value);
|
|
204
|
-
}
|
|
205
|
-
varint32(value) {
|
|
206
|
-
const start = this.take(varint32Size(value));
|
|
207
|
-
writeVarint32(this.data, start, value);
|
|
208
|
-
}
|
|
209
|
-
varint32Array(items) {
|
|
210
|
-
const count = items.length;
|
|
211
|
-
let pos = this.pos;
|
|
212
|
-
this.reserve(pos + count);
|
|
213
|
-
let data = this.data;
|
|
214
|
-
for (let i = 0; i < count; i++) {
|
|
215
|
-
const value = checkedVarint32(items[i]);
|
|
216
|
-
if (data.length - pos < 5) {
|
|
217
|
-
this.reserve(pos + varint32Size(value));
|
|
218
|
-
data = this.data;
|
|
219
|
-
}
|
|
220
|
-
pos = writeVarint32(data, pos, value);
|
|
221
|
-
}
|
|
222
|
-
this.pos = pos;
|
|
223
|
-
}
|
|
224
|
-
string(value) {
|
|
225
|
-
if (typeof value !== "string")
|
|
226
|
-
throw new TypeError("Expected a Pojoc string");
|
|
227
|
-
if (loneSurrogate.test(value))
|
|
228
|
-
throw new TypeError("Invalid Unicode string");
|
|
229
|
-
const bytes = textEncoder.encode(value);
|
|
230
|
-
this.length(bytes.length);
|
|
231
|
-
this.bytes(bytes);
|
|
232
|
-
}
|
|
233
|
-
finish() { return this.data.subarray(0, this.pos); }
|
|
234
|
-
}
|
|
235
|
-
function arrayOf(ty) {
|
|
236
|
-
return typeof ty === "string" ? packed[ty] : undefined;
|
|
237
|
-
}
|
|
238
|
-
function readPacked(r, ctor, count) {
|
|
239
|
-
const width = ctor.BYTES_PER_ELEMENT;
|
|
240
|
-
const bytes = r.bytes(count * width);
|
|
241
|
-
if ((littleEndian || width === 1) && bytes.byteOffset % width === 0) {
|
|
242
|
-
return new ctor(bytes.buffer, bytes.byteOffset, count);
|
|
243
|
-
}
|
|
244
|
-
const array = new ctor(count);
|
|
245
|
-
const copy = new Uint8Array(array.buffer);
|
|
246
|
-
copy.set(bytes);
|
|
247
|
-
if (!littleEndian && width > 1) {
|
|
248
|
-
for (let i = 0; i < copy.length; i += width)
|
|
249
|
-
copy.subarray(i, i + width).reverse();
|
|
250
|
-
}
|
|
251
|
-
return array;
|
|
252
|
-
}
|
|
253
|
-
function integerKind(ty) {
|
|
254
|
-
if (ty === "varint32")
|
|
255
|
-
return [32, false];
|
|
256
|
-
if (ty === "varint64")
|
|
257
|
-
return [64, false];
|
|
258
|
-
if (typeof ty !== "string" || (ty[0] !== "u" && ty[0] !== "i"))
|
|
259
|
-
return;
|
|
260
|
-
if (ty === "intern")
|
|
261
|
-
return;
|
|
262
|
-
return [Number(ty.slice(1)), ty[0] === "i"];
|
|
263
|
-
}
|
|
264
|
-
function integer(ty, value) {
|
|
265
|
-
const [bits, signed] = integerKind(ty);
|
|
266
|
-
if (bits === 64 ? typeof value !== "bigint" : typeof value !== "number" || !Number.isInteger(value)) {
|
|
267
|
-
throw new TypeError(`Expected ${bits === 64 ? "bigint" : "integer"} for ${ty}`);
|
|
268
|
-
}
|
|
269
|
-
const n = BigInt(value);
|
|
270
|
-
const wrapped = signed ? BigInt.asIntN(bits, n) : BigInt.asUintN(bits, n);
|
|
271
|
-
if (n !== wrapped)
|
|
272
|
-
invalid(`Integer out of range for ${ty}`);
|
|
273
|
-
return n;
|
|
274
|
-
}
|
|
275
|
-
function readScalar(r, ty, table) {
|
|
276
|
-
switch (ty) {
|
|
277
|
-
case "u8": return r.byte();
|
|
278
|
-
case "i8": return r.view.getInt8(r.take(1));
|
|
279
|
-
case "u16": return r.uint(2);
|
|
280
|
-
case "i16": return r.view.getInt16(r.take(2), true);
|
|
281
|
-
case "u32": return r.uint(4);
|
|
282
|
-
case "i32": return r.view.getInt32(r.take(4), true);
|
|
283
|
-
case "u64": return r.view.getBigUint64(r.take(8), true);
|
|
284
|
-
case "i64": return r.view.getBigInt64(r.take(8), true);
|
|
285
|
-
case "f32": return r.view.getFloat32(r.take(4), true);
|
|
286
|
-
case "f64": return r.view.getFloat64(r.take(8), true);
|
|
287
|
-
case "bool": return r.byte() !== 0;
|
|
288
|
-
case "string": return r.string();
|
|
289
|
-
case "varint32": return r.varint32();
|
|
290
|
-
case "varint64": return r.varint();
|
|
291
|
-
case "intern": {
|
|
292
|
-
const index = r.length();
|
|
293
|
-
if (index >= table.length)
|
|
294
|
-
invalid("Invalid Pojoc intern index");
|
|
295
|
-
return table[index];
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
function writeScalar(w, ty, value, table) {
|
|
300
|
-
switch (ty) {
|
|
301
|
-
case "string":
|
|
302
|
-
w.string(value);
|
|
303
|
-
return;
|
|
304
|
-
case "varint32":
|
|
305
|
-
w.varint32(checkedVarint32(value));
|
|
306
|
-
return;
|
|
307
|
-
case "intern": {
|
|
308
|
-
if (typeof value !== "string")
|
|
309
|
-
throw new TypeError("Expected an interned string");
|
|
310
|
-
let index = table.get(value);
|
|
311
|
-
if (index === undefined) {
|
|
312
|
-
index = table.size;
|
|
313
|
-
table.set(value, index);
|
|
314
|
-
}
|
|
315
|
-
w.length(index);
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
case "bool": {
|
|
319
|
-
if (typeof value !== "boolean")
|
|
320
|
-
throw new TypeError("Expected a Pojoc boolean");
|
|
321
|
-
w.byte(Number(value));
|
|
322
|
-
return;
|
|
323
|
-
}
|
|
324
|
-
case "f32":
|
|
325
|
-
case "f64": {
|
|
326
|
-
if (typeof value !== "number")
|
|
327
|
-
throw new TypeError("Expected a Pojoc number");
|
|
328
|
-
const start = w.take(ty === "f32" ? 4 : 8);
|
|
329
|
-
if (ty === "f32")
|
|
330
|
-
w.view.setFloat32(start, value, true);
|
|
331
|
-
else
|
|
332
|
-
w.view.setFloat64(start, value, true);
|
|
333
|
-
return;
|
|
334
|
-
}
|
|
335
|
-
default: {
|
|
336
|
-
const [bits, signed] = integerKind(ty);
|
|
337
|
-
if (bits < 64) {
|
|
338
|
-
const min = signed ? -(2 ** (bits - 1)) : 0;
|
|
339
|
-
const max = (2 ** (signed ? bits - 1 : bits)) - 1;
|
|
340
|
-
if (typeof value !== "number" || !Number.isInteger(value) || value < min || value > max) {
|
|
341
|
-
invalid(`Integer out of range for ${ty}`);
|
|
342
|
-
}
|
|
343
|
-
w.uint(bits / 8, value);
|
|
344
|
-
return;
|
|
345
|
-
}
|
|
346
|
-
const n = integer(ty, value);
|
|
347
|
-
if (ty === "varint64")
|
|
348
|
-
w.varint(n);
|
|
349
|
-
else {
|
|
350
|
-
const start = w.take(8);
|
|
351
|
-
w.view.setBigUint64(start, n, true);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
function sequence(value) {
|
|
357
|
-
if (Array.isArray(value) || (ArrayBuffer.isView(value) && !(value instanceof DataView))) {
|
|
358
|
-
return value;
|
|
359
|
-
}
|
|
360
|
-
throw new TypeError("Expected a Pojoc array");
|
|
361
|
-
}
|
|
362
|
-
function typedValues(ty, items) {
|
|
363
|
-
const ctor = arrayOf(ty);
|
|
364
|
-
if (!ctor)
|
|
365
|
-
return items;
|
|
366
|
-
const target = new ctor(items.length);
|
|
367
|
-
const writable = target;
|
|
368
|
-
items.forEach((item, i) => { writable[i] = item; });
|
|
369
|
-
return target;
|
|
370
|
-
}
|
|
371
|
-
function unsignedBytes(value, width) {
|
|
372
|
-
const bytes = new Uint8Array(width);
|
|
373
|
-
for (let n = 0; n < width; n++, value >>= 8n)
|
|
374
|
-
bytes[n] = Number(value & 255n);
|
|
375
|
-
return bytes;
|
|
376
|
-
}
|
|
377
|
-
function bytesAsInt(bytes) {
|
|
378
|
-
let value = 0n;
|
|
379
|
-
for (let i = Math.min(bytes.length, 16) - 1; i >= 0; i--)
|
|
380
|
-
value = (value << 8n) | BigInt(bytes[i]);
|
|
381
|
-
return value;
|
|
382
|
-
}
|
|
383
|
-
function limitCollection(count) {
|
|
384
|
-
// Variable-size values can consume zero bytes, so byte bounds alone cannot
|
|
385
|
-
// rule out a huge number of objects in an otherwise tiny envelope.
|
|
386
|
-
if (count > 16_777_216)
|
|
387
|
-
invalid("Pojoc collection exceeds the object decode limit");
|
|
388
|
-
}
|
|
389
|
-
function signedVarint(r) {
|
|
390
|
-
const value = r.varint();
|
|
391
|
-
return (value >> 1n) ^ -(value & 1n);
|
|
392
|
-
}
|
|
393
|
-
function signedDelta32(r) {
|
|
394
|
-
if (r.pos >= r.data.length)
|
|
395
|
-
invalid("Unexpected end of Pojoc payload");
|
|
396
|
-
const byte = r.data[r.pos];
|
|
397
|
-
if (byte < 128) {
|
|
398
|
-
r.pos++;
|
|
399
|
-
return (byte >>> 1) ^ -(byte & 1);
|
|
400
|
-
}
|
|
401
|
-
// Only the low 32 bits can affect the reconstructed element.
|
|
402
|
-
return Number(BigInt.asIntN(32, signedVarint(r)));
|
|
403
|
-
}
|
|
404
|
-
function writeSignedVarint(w, value) {
|
|
405
|
-
const wrapped = BigInt.asIntN(64, value);
|
|
406
|
-
w.varint((wrapped << 1n) ^ (wrapped >> 63n));
|
|
407
|
-
}
|
|
408
|
-
function decodeDelta(r, ty, count) {
|
|
409
|
-
limitCollection(count);
|
|
410
|
-
if (count > r.data.length - r.pos)
|
|
411
|
-
invalid("Truncated Pojoc delta array");
|
|
412
|
-
const [bits, signed] = integerKind(ty);
|
|
413
|
-
if (bits < 64) {
|
|
414
|
-
const items = new (arrayOf(ty))(count);
|
|
415
|
-
if (count === 0)
|
|
416
|
-
return items;
|
|
417
|
-
let previous = signed ? signedDelta32(r) : r.varint32();
|
|
418
|
-
items[0] = previous;
|
|
419
|
-
for (let i = 1; i < count; i++) {
|
|
420
|
-
previous = (previous + signedDelta32(r)) | 0;
|
|
421
|
-
items[i] = previous;
|
|
422
|
-
}
|
|
423
|
-
return items;
|
|
424
|
-
}
|
|
425
|
-
const wrap = signed ? BigInt.asIntN : BigInt.asUintN;
|
|
426
|
-
const items = new (signed ? BigInt64Array : BigUint64Array)(count);
|
|
427
|
-
let previous = 0n;
|
|
428
|
-
for (let i = 0; i < count; i++) {
|
|
429
|
-
const next = i ? previous + signedVarint(r) : signed ? signedVarint(r) : r.varint();
|
|
430
|
-
previous = wrap(64, next);
|
|
431
|
-
items[i] = previous;
|
|
432
|
-
}
|
|
433
|
-
return items;
|
|
434
|
-
}
|
|
435
|
-
function encodeDelta(w, ty, value) {
|
|
436
|
-
const [bits, signed] = integerKind(ty);
|
|
437
|
-
if (bits < 64) {
|
|
438
|
-
const bound = 2 ** (signed ? bits - 1 : bits);
|
|
439
|
-
const min = signed ? -bound : 0;
|
|
440
|
-
let previous = 0;
|
|
441
|
-
for (let i = 0; i < value.length; i++) {
|
|
442
|
-
const next = value[i];
|
|
443
|
-
if (typeof next !== "number" || !Number.isInteger(next))
|
|
444
|
-
throw new TypeError(`Expected integer for ${ty}`);
|
|
445
|
-
if (next < min || next >= bound)
|
|
446
|
-
invalid(`Integer out of range for ${ty}`);
|
|
447
|
-
const delta = next - previous;
|
|
448
|
-
const zigzag = delta < 0 ? -2 * delta - 1 : 2 * delta;
|
|
449
|
-
const encoded = i === 0 && !signed ? next : zigzag;
|
|
450
|
-
// A jump across the full 32-bit range can need 33 ZigZag bits.
|
|
451
|
-
if (encoded <= 0xffff_ffff)
|
|
452
|
-
w.varint32(encoded);
|
|
453
|
-
else
|
|
454
|
-
w.varint(BigInt(encoded));
|
|
455
|
-
previous = next;
|
|
456
|
-
}
|
|
457
|
-
return;
|
|
458
|
-
}
|
|
459
|
-
let previous = 0n;
|
|
460
|
-
for (let i = 0; i < value.length; i++) {
|
|
461
|
-
const next = integer(ty, value[i]);
|
|
462
|
-
if (i || signed)
|
|
463
|
-
writeSignedVarint(w, next - previous);
|
|
464
|
-
else
|
|
465
|
-
w.varint(next);
|
|
466
|
-
previous = next;
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
function resized(bytes, width) {
|
|
470
|
-
if (bytes.length === width)
|
|
471
|
-
return bytes;
|
|
472
|
-
const result = new Uint8Array(width);
|
|
473
|
-
result.set(bytes.subarray(0, width));
|
|
474
|
-
return result;
|
|
475
|
-
}
|
|
476
|
-
function object(value) {
|
|
477
|
-
if (typeof value !== "object" || value === null)
|
|
478
|
-
throw new TypeError("Expected a Pojoc record");
|
|
479
|
-
return value;
|
|
480
|
-
}
|
|
481
|
-
function setField(target, name, value) {
|
|
482
|
-
if (name === "__proto__") {
|
|
483
|
-
Object.defineProperty(target, name, { value, enumerable: true, writable: true, configurable: true });
|
|
484
|
-
}
|
|
485
|
-
else
|
|
486
|
-
target[name] = value;
|
|
487
|
-
}
|
|
488
|
-
function isOptional(ty) {
|
|
489
|
-
return typeof ty !== "string" && ty[0] === "optional";
|
|
490
|
-
}
|
|
491
|
-
function headerLength(fields) {
|
|
492
|
-
let count = 0;
|
|
493
|
-
for (const field of fields)
|
|
494
|
-
if (isOptional(field.ty))
|
|
495
|
-
count++;
|
|
496
|
-
return Math.ceil(count / 8);
|
|
497
|
-
}
|
|
498
|
-
function readValue(r, ty, schemas, scope, table, historical = false) {
|
|
499
|
-
if (typeof ty === "string")
|
|
500
|
-
return readScalar(r, ty, table);
|
|
501
|
-
const schema = schemas[scope];
|
|
502
|
-
switch (ty[0]) {
|
|
503
|
-
case "bytes": return r.bytes(ty[1]);
|
|
504
|
-
case "bits": return resized(r.bytes(ty[1]), ty[2]);
|
|
505
|
-
case "enum": {
|
|
506
|
-
const value = r.uint(ty[1]);
|
|
507
|
-
if (value !== 0 && !ty[2].includes(value))
|
|
508
|
-
invalid("Invalid Pojoc enum discriminant");
|
|
509
|
-
return value;
|
|
510
|
-
}
|
|
511
|
-
case "union": {
|
|
512
|
-
const discriminant = r.uint(ty[1]);
|
|
513
|
-
const payload = r.bytes(r.length());
|
|
514
|
-
const variant = ty[2].find(v => v[0] === discriminant);
|
|
515
|
-
if (!variant)
|
|
516
|
-
return { kind: "Unknown", discriminant, data: payload };
|
|
517
|
-
const inner = new Reader(payload);
|
|
518
|
-
const value = readValue(inner, variant[2], schemas, scope, table);
|
|
519
|
-
if (inner.pos !== payload.length)
|
|
520
|
-
invalid("Invalid Pojoc union payload length");
|
|
521
|
-
validate(variant[2], value, variant[1]);
|
|
522
|
-
return { kind: variant[1], value };
|
|
523
|
-
}
|
|
524
|
-
case "record": {
|
|
525
|
-
const fields = schema.records[historical ? ty[1] : ty[2]];
|
|
526
|
-
return readFields(r, fields, schemas, scope, table, !historical);
|
|
527
|
-
}
|
|
528
|
-
case "import": return readPayload(r, schemas, schema.imports[ty[1]], ty[2]);
|
|
529
|
-
case "optional": return r.byte() ? readValue(r, ty[1], schemas, scope, table, historical) : null;
|
|
530
|
-
case "constraint":
|
|
531
|
-
case "box": return readValue(r, ty[1], schemas, scope, table, historical);
|
|
532
|
-
case "vfloat": return Math.fround(ty[1] + r.uint(ty[3]) * ty[2]);
|
|
533
|
-
case "tuple": return ty[1].map(item => readValue(r, item, schemas, scope, table));
|
|
534
|
-
case "delta": return decodeDelta(r, ty[1], ty[2] ?? r.length());
|
|
535
|
-
case "array": {
|
|
536
|
-
const count = ty[2] ?? r.length();
|
|
537
|
-
const ctor = arrayOf(ty[1]);
|
|
538
|
-
if (ctor)
|
|
539
|
-
return readPacked(r, ctor, count);
|
|
540
|
-
limitCollection(count);
|
|
541
|
-
if (ty[1] === "varint32")
|
|
542
|
-
return r.varint32Array(count);
|
|
543
|
-
const result = [];
|
|
544
|
-
for (let i = 0; i < count; i++)
|
|
545
|
-
result.push(readValue(r, ty[1], schemas, scope, table));
|
|
546
|
-
return result;
|
|
547
|
-
}
|
|
548
|
-
case "map": {
|
|
549
|
-
const count = ty[3] ?? r.length();
|
|
550
|
-
limitCollection(count);
|
|
551
|
-
const entries = [];
|
|
552
|
-
for (let i = 0; i < count; i++) {
|
|
553
|
-
const key = readValue(r, ty[1], schemas, scope, table);
|
|
554
|
-
const value = readValue(r, ty[2], schemas, scope, table);
|
|
555
|
-
entries.push([key, value]);
|
|
556
|
-
}
|
|
557
|
-
return ty[3] === null ? new Map(entries) : entries;
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
function writeValue(w, ty, value, schemas, scope, table, historical = false) {
|
|
562
|
-
if (typeof ty === "string")
|
|
563
|
-
return writeScalar(w, ty, value, table);
|
|
564
|
-
const schema = schemas[scope];
|
|
565
|
-
switch (ty[0]) {
|
|
566
|
-
case "bytes":
|
|
567
|
-
if (!(value instanceof Uint8Array) || value.length !== ty[1])
|
|
568
|
-
invalid("Invalid fixed bytes length");
|
|
569
|
-
w.bytes(value);
|
|
570
|
-
return;
|
|
571
|
-
case "bits":
|
|
572
|
-
if (!(value instanceof Uint8Array) || value.length !== ty[2])
|
|
573
|
-
invalid("Invalid bitset width");
|
|
574
|
-
w.bytes(resized(value, ty[1]));
|
|
575
|
-
return;
|
|
576
|
-
case "enum":
|
|
577
|
-
if (typeof value !== "number" || (value !== 0 && !ty[2].includes(value)))
|
|
578
|
-
invalid("Invalid enum value");
|
|
579
|
-
w.uint(ty[1], value <= ty[3] ? value : 0);
|
|
580
|
-
return;
|
|
581
|
-
case "union": {
|
|
582
|
-
const item = object(value);
|
|
583
|
-
const variant = ty[3].find(v => v[1] === item.kind);
|
|
584
|
-
const discriminant = (item.kind === "Unknown" ? item.discriminant : variant?.[0]);
|
|
585
|
-
if (!Number.isInteger(discriminant) || discriminant < 0)
|
|
586
|
-
invalid("Invalid Pojoc union kind");
|
|
587
|
-
if (discriminant >= 2 ** (ty[1] * 8))
|
|
588
|
-
invalid("Pojoc union discriminant overflow");
|
|
589
|
-
let payload;
|
|
590
|
-
if (variant) {
|
|
591
|
-
validate(variant[2], item.value, variant[1]);
|
|
592
|
-
const nested = new Writer();
|
|
593
|
-
writeValue(nested, variant[2], item.value, schemas, scope, table);
|
|
594
|
-
payload = nested.finish();
|
|
595
|
-
}
|
|
596
|
-
else {
|
|
597
|
-
if (!(item.data instanceof Uint8Array))
|
|
598
|
-
throw new TypeError("Expected bytes for unknown union variant");
|
|
599
|
-
payload = item.data;
|
|
600
|
-
}
|
|
601
|
-
w.uint(ty[1], discriminant);
|
|
602
|
-
w.length(payload.length);
|
|
603
|
-
w.bytes(payload);
|
|
604
|
-
return;
|
|
605
|
-
}
|
|
606
|
-
case "record": {
|
|
607
|
-
const fields = schema.records[historical ? ty[1] : ty[2]];
|
|
608
|
-
writeFields(w, fields, object(value), schemas, scope, table, !historical);
|
|
609
|
-
return;
|
|
610
|
-
}
|
|
611
|
-
case "import":
|
|
612
|
-
writePayload(w, schemas, schema.imports[ty[1]], ty[2], object(value));
|
|
613
|
-
return;
|
|
614
|
-
case "optional":
|
|
615
|
-
w.byte(value == null ? 0 : 1);
|
|
616
|
-
if (value != null)
|
|
617
|
-
writeValue(w, ty[1], value, schemas, scope, table, historical);
|
|
618
|
-
return;
|
|
619
|
-
case "constraint":
|
|
620
|
-
case "box": return writeValue(w, ty[1], value, schemas, scope, table, historical);
|
|
621
|
-
case "vfloat": {
|
|
622
|
-
if (typeof value !== "number")
|
|
623
|
-
throw new TypeError("Expected a Pojoc vfloat");
|
|
624
|
-
const steps = Math.round((Math.fround(value) - ty[1]) / ty[2]);
|
|
625
|
-
w.uint(ty[3], Math.min(2 ** (ty[3] * 8) - 1, Math.max(0, steps || 0)));
|
|
626
|
-
return;
|
|
627
|
-
}
|
|
628
|
-
case "tuple": {
|
|
629
|
-
const items = sequence(value);
|
|
630
|
-
if (items.length !== ty[1].length)
|
|
631
|
-
invalid("Invalid Pojoc tuple length");
|
|
632
|
-
ty[1].forEach((kind, i) => writeValue(w, kind, items[i], schemas, scope, table));
|
|
633
|
-
return;
|
|
634
|
-
}
|
|
635
|
-
case "array":
|
|
636
|
-
case "delta": {
|
|
637
|
-
const items = sequence(value);
|
|
638
|
-
if (ty[2] === null)
|
|
639
|
-
w.length(items.length);
|
|
640
|
-
else if (items.length !== ty[2])
|
|
641
|
-
invalid("Invalid fixed array length");
|
|
642
|
-
if (ty[0] === "delta")
|
|
643
|
-
return encodeDelta(w, ty[1], items);
|
|
644
|
-
if (ty[1] === "varint32")
|
|
645
|
-
return w.varint32Array(items);
|
|
646
|
-
const ctor = arrayOf(ty[1]);
|
|
647
|
-
if (littleEndian && ctor && value instanceof ctor) {
|
|
648
|
-
w.bytes(new Uint8Array(value.buffer, value.byteOffset, value.byteLength));
|
|
649
|
-
}
|
|
650
|
-
else {
|
|
651
|
-
for (let i = 0; i < items.length; i++)
|
|
652
|
-
writeValue(w, ty[1], items[i], schemas, scope, table);
|
|
653
|
-
}
|
|
654
|
-
return;
|
|
655
|
-
}
|
|
656
|
-
case "map": {
|
|
657
|
-
let entries;
|
|
658
|
-
if (ty[3] === null) {
|
|
659
|
-
if (!(value instanceof Map))
|
|
660
|
-
throw new TypeError("Expected a Pojoc Map");
|
|
661
|
-
w.length(value.size);
|
|
662
|
-
entries = value;
|
|
663
|
-
}
|
|
664
|
-
else {
|
|
665
|
-
if (!Array.isArray(value) || value.length !== ty[3])
|
|
666
|
-
invalid("Invalid fixed map length");
|
|
667
|
-
entries = value;
|
|
668
|
-
}
|
|
669
|
-
for (const [key, item] of entries) {
|
|
670
|
-
writeValue(w, ty[1], key, schemas, scope, table);
|
|
671
|
-
writeValue(w, ty[2], item, schemas, scope, table);
|
|
672
|
-
}
|
|
673
|
-
return;
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
}
|
|
677
|
-
function validate(ty, value, field) {
|
|
678
|
-
if (typeof ty === "string")
|
|
679
|
-
return;
|
|
680
|
-
if (ty[0] === "optional" && value != null)
|
|
681
|
-
return validate(ty[1], value, field);
|
|
682
|
-
if (ty[0] !== "constraint")
|
|
683
|
-
return;
|
|
684
|
-
const [, , min, max] = ty;
|
|
685
|
-
const n = typeof value === "string" ? textEncoder.encode(value).length
|
|
686
|
-
: value instanceof Map ? value.size
|
|
687
|
-
: Array.isArray(value) || ArrayBuffer.isView(value) ? sequence(value).length
|
|
688
|
-
: Number(value);
|
|
689
|
-
if ((min !== null && n < min) || (max !== null && n > max))
|
|
690
|
-
invalid(`Constraint violation in ${field}`);
|
|
691
|
-
}
|
|
692
|
-
function readFields(r, fields, schemas, scope, table, checks) {
|
|
693
|
-
const header = r.bytes(headerLength(fields));
|
|
694
|
-
let bit = 0;
|
|
695
|
-
const result = {};
|
|
696
|
-
for (const field of fields) {
|
|
697
|
-
let value;
|
|
698
|
-
if (isOptional(field.ty)) {
|
|
699
|
-
const present = header[bit >> 3] & 1 << (bit & 7);
|
|
700
|
-
bit++;
|
|
701
|
-
value = present ? readValue(r, field.ty[1], schemas, scope, table, field.cast) : null;
|
|
702
|
-
}
|
|
703
|
-
else {
|
|
704
|
-
value = readValue(r, field.ty, schemas, scope, table, field.cast);
|
|
705
|
-
}
|
|
706
|
-
if (checks)
|
|
707
|
-
validate(field.ty, value, field.name);
|
|
708
|
-
setField(result, field.name, value);
|
|
709
|
-
}
|
|
710
|
-
return result;
|
|
711
|
-
}
|
|
712
|
-
function writeFields(w, fields, value, schemas, scope, table, checks) {
|
|
713
|
-
const header = new Uint8Array(headerLength(fields));
|
|
714
|
-
let bit = 0;
|
|
715
|
-
for (const field of fields) {
|
|
716
|
-
if (isOptional(field.ty)) {
|
|
717
|
-
if (value[field.name] != null)
|
|
718
|
-
header[bit >> 3] |= 1 << (bit & 7);
|
|
719
|
-
bit++;
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
w.bytes(header);
|
|
723
|
-
for (const field of fields) {
|
|
724
|
-
const item = value[field.name];
|
|
725
|
-
if (checks)
|
|
726
|
-
validate(field.ty, item, field.name);
|
|
727
|
-
if (isOptional(field.ty)) {
|
|
728
|
-
if (item != null)
|
|
729
|
-
writeValue(w, field.ty[1], item, schemas, scope, table, field.cast);
|
|
730
|
-
}
|
|
731
|
-
else {
|
|
732
|
-
writeValue(w, field.ty, item, schemas, scope, table, field.cast);
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
function defaultValue(ty, supplied, schemas, scope) {
|
|
737
|
-
const schema = schemas[scope];
|
|
738
|
-
const plan = Array.isArray(supplied) ? supplied : [];
|
|
739
|
-
if (typeof ty === "string") {
|
|
740
|
-
if (ty === "string" || ty === "intern")
|
|
741
|
-
return supplied ?? "";
|
|
742
|
-
if (ty === "bool")
|
|
743
|
-
return supplied ?? false;
|
|
744
|
-
const value = plan[0] === "bytes" ? bytesAsInt(Uint8Array.from(plan[1])) : plan[0] === "integer" ? plan[1] : supplied ?? 0;
|
|
745
|
-
if (integerKind(ty)?.[0] === 64)
|
|
746
|
-
return BigInt(value);
|
|
747
|
-
return ty === "f32" ? Math.fround(Number(value)) : Number(value);
|
|
748
|
-
}
|
|
749
|
-
switch (ty[0]) {
|
|
750
|
-
case "optional": return supplied == null ? null : defaultValue(ty[1], supplied, schemas, scope);
|
|
751
|
-
case "box":
|
|
752
|
-
case "constraint": return defaultValue(ty[1], supplied, schemas, scope);
|
|
753
|
-
case "bytes":
|
|
754
|
-
case "bits": {
|
|
755
|
-
const width = ty[0] === "bits" ? ty[2] : ty[1];
|
|
756
|
-
if (plan[0] === "bytes")
|
|
757
|
-
return resized(Uint8Array.from(plan[1]), width);
|
|
758
|
-
if (plan[0] === "integer")
|
|
759
|
-
return unsignedBytes(BigInt(plan[1]), width);
|
|
760
|
-
return new Uint8Array(width);
|
|
761
|
-
}
|
|
762
|
-
case "enum":
|
|
763
|
-
case "vfloat": return Number(plan[0] === "integer" ? plan[1] : supplied ?? 0);
|
|
764
|
-
case "union": {
|
|
765
|
-
const first = ty[3][0];
|
|
766
|
-
if (first === undefined)
|
|
767
|
-
return { kind: "Unknown", discriminant: 0, data: new Uint8Array() };
|
|
768
|
-
return { kind: first[1], value: defaultValue(first[2], null, schemas, scope) };
|
|
769
|
-
}
|
|
770
|
-
case "record": return defaults(schema.records[ty[2]], {}, schemas, scope);
|
|
771
|
-
case "import": {
|
|
772
|
-
const imported = schema.imports[ty[1]];
|
|
773
|
-
const target = schemas[imported];
|
|
774
|
-
return defaults(target.roots[target.latest].fields, {}, schemas, imported);
|
|
775
|
-
}
|
|
776
|
-
case "tuple": return ty[1].map((item, i) => defaultValue(item, plan[1]?.[i], schemas, scope));
|
|
777
|
-
case "array":
|
|
778
|
-
case "delta": {
|
|
779
|
-
const items = plan[0] === "items" ? plan[1] : [];
|
|
780
|
-
const result = Array.from({ length: ty[2] ?? items.length }, (_, i) => defaultValue(ty[1], plan[0] === "repeat" ? plan[1] : items[i], schemas, scope));
|
|
781
|
-
return typedValues(ty[1], result);
|
|
782
|
-
}
|
|
783
|
-
case "map": {
|
|
784
|
-
const items = (plan[0] === "pairs" ? plan[1] : []);
|
|
785
|
-
const result = items.map(([key, value]) => [
|
|
786
|
-
defaultValue(ty[1], key, schemas, scope), defaultValue(ty[2], value, schemas, scope),
|
|
787
|
-
]);
|
|
788
|
-
if (ty[3] === null)
|
|
789
|
-
return new Map(result);
|
|
790
|
-
while (result.length < ty[3]) {
|
|
791
|
-
result.push([defaultValue(ty[1], null, schemas, scope), defaultValue(ty[2], null, schemas, scope)]);
|
|
792
|
-
}
|
|
793
|
-
return result.slice(0, ty[3]);
|
|
794
|
-
}
|
|
795
|
-
}
|
|
796
|
-
}
|
|
797
|
-
function defaults(fields, value, schemas, scope) {
|
|
798
|
-
const result = {};
|
|
799
|
-
for (const field of fields) {
|
|
800
|
-
setField(result, field.name, Object.prototype.hasOwnProperty.call(value, field.name)
|
|
801
|
-
? value[field.name] : defaultValue(field.ty, field.default, schemas, scope));
|
|
802
|
-
}
|
|
803
|
-
return result;
|
|
804
|
-
}
|
|
805
|
-
function castScalar(value, from, to) {
|
|
806
|
-
if (to === "bool")
|
|
807
|
-
return Boolean(value);
|
|
808
|
-
if (to === "f32")
|
|
809
|
-
return Math.fround(Number(value));
|
|
810
|
-
if (to === "f64")
|
|
811
|
-
return Number(value);
|
|
812
|
-
const target = integerKind(to);
|
|
813
|
-
if (!target)
|
|
814
|
-
return value;
|
|
815
|
-
const [bits, signed] = target;
|
|
816
|
-
let n;
|
|
817
|
-
if (from === "f32" || from === "f64" || (typeof from !== "string" && from[0] === "vfloat")) {
|
|
818
|
-
const minimum = signed ? -(1n << BigInt(bits - 1)) : 0n;
|
|
819
|
-
const maximum = (1n << BigInt(signed ? bits - 1 : bits)) - 1n;
|
|
820
|
-
const number = Number(value);
|
|
821
|
-
if (Number.isNaN(number))
|
|
822
|
-
n = 0n;
|
|
823
|
-
else if (number <= Number(minimum))
|
|
824
|
-
n = minimum;
|
|
825
|
-
else if (number >= Number(maximum))
|
|
826
|
-
n = maximum;
|
|
827
|
-
else
|
|
828
|
-
n = BigInt(Math.trunc(number));
|
|
829
|
-
}
|
|
830
|
-
else {
|
|
831
|
-
n = BigInt(value);
|
|
832
|
-
n = signed ? BigInt.asIntN(bits, n) : BigInt.asUintN(bits, n);
|
|
833
|
-
}
|
|
834
|
-
return bits === 64 ? n : Number(n);
|
|
835
|
-
}
|
|
836
|
-
function adapt(value, from, to, schemas, scope, down) {
|
|
837
|
-
if (value == null)
|
|
838
|
-
return defaultValue(to, null, schemas, scope);
|
|
839
|
-
if (typeof from !== "string" && (from[0] === "optional" || from[0] === "box" || from[0] === "constraint")) {
|
|
840
|
-
return adapt(value, from[1], to, schemas, scope, down);
|
|
841
|
-
}
|
|
842
|
-
if (typeof to !== "string" && (to[0] === "optional" || to[0] === "box" || to[0] === "constraint")) {
|
|
843
|
-
return adapt(value, from, to[1], schemas, scope, down);
|
|
844
|
-
}
|
|
845
|
-
if (typeof from === "string" && typeof to === "string" && from !== to)
|
|
846
|
-
return castScalar(value, from, to);
|
|
847
|
-
if (typeof from !== "string" && from[0] === "bits" && typeof to === "string") {
|
|
848
|
-
return castScalar(bytesAsInt(value), "u64", to);
|
|
849
|
-
}
|
|
850
|
-
if (typeof to !== "string" && to[0] === "bits" && typeof from === "string") {
|
|
851
|
-
return unsignedBytes(BigInt(value), to[2]);
|
|
852
|
-
}
|
|
853
|
-
if (typeof from === "string" || typeof to === "string")
|
|
854
|
-
return value;
|
|
855
|
-
if (from[0] === "record" && to[0] === "record") {
|
|
856
|
-
const records = schemas[scope].records;
|
|
857
|
-
return project(object(value), records[down ? from[2] : from[1]], records[down ? to[1] : to[2]], schemas, scope, down);
|
|
858
|
-
}
|
|
859
|
-
if (to[0] === "bytes")
|
|
860
|
-
return resized(value, to[1]);
|
|
861
|
-
if (to[0] === "bits")
|
|
862
|
-
return resized(value, to[2]);
|
|
863
|
-
if (to[0] === "array" || to[0] === "delta") {
|
|
864
|
-
const items = from[0] === "map" ? Array.from(value.values()) : sequence(value);
|
|
865
|
-
const result = Array.from({ length: to[2] ?? items.length }, (_, i) => i < items.length ? items[i] : defaultValue(to[1], null, schemas, scope));
|
|
866
|
-
return typedValues(to[1], result);
|
|
867
|
-
}
|
|
868
|
-
if (to[0] === "map" && from[0] === "array") {
|
|
869
|
-
const items = sequence(value);
|
|
870
|
-
return new Map(Array.from({ length: items.length }, (_, i) => [i, items[i]]));
|
|
871
|
-
}
|
|
872
|
-
if (to[0] === "map" && to[3] !== null) {
|
|
873
|
-
const items = value.slice(0, to[3]);
|
|
874
|
-
if (down) {
|
|
875
|
-
while (items.length < to[3]) {
|
|
876
|
-
items.push([defaultValue(to[1], null, schemas, scope), defaultValue(to[2], null, schemas, scope)]);
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
return items;
|
|
880
|
-
}
|
|
881
|
-
return value;
|
|
882
|
-
}
|
|
883
|
-
function project(value, from, to, schemas, scope, down) {
|
|
884
|
-
const source = new Map(from.map(field => [field.id, field]));
|
|
885
|
-
const result = {};
|
|
886
|
-
for (const field of to) {
|
|
887
|
-
const old = source.get(field.id);
|
|
888
|
-
const prior = old && value[old.name];
|
|
889
|
-
const cast = down ? field.cast : old?.cast;
|
|
890
|
-
let current;
|
|
891
|
-
if (!old)
|
|
892
|
-
current = defaultValue(field.ty, down ? null : field.default, schemas, scope);
|
|
893
|
-
else if (cast && (down ? old.lazy : field.lazy)) {
|
|
894
|
-
// Rust drops a value when a historical cast crosses into a lazy target.
|
|
895
|
-
current = defaultValue(field.ty, null, schemas, scope);
|
|
896
|
-
}
|
|
897
|
-
else if (cast || JSON.stringify(old.ty) !== JSON.stringify(field.ty)) {
|
|
898
|
-
current = adapt(prior, old.ty, field.ty, schemas, scope, down);
|
|
899
|
-
}
|
|
900
|
-
else
|
|
901
|
-
current = prior;
|
|
902
|
-
setField(result, field.name, current);
|
|
903
|
-
}
|
|
904
|
-
return result;
|
|
905
|
-
}
|
|
906
|
-
function versionLayout(schema, version) {
|
|
907
|
-
const root = schema.roots[version];
|
|
908
|
-
if (!root)
|
|
909
|
-
invalid(`Unsupported Pojoc version ${version}`);
|
|
910
|
-
return root;
|
|
911
|
-
}
|
|
912
|
-
function readTable(r) {
|
|
913
|
-
const count = r.length();
|
|
914
|
-
if (count > r.data.length - r.pos)
|
|
915
|
-
invalid("Invalid Pojoc intern table length");
|
|
916
|
-
limitCollection(count);
|
|
917
|
-
const table = [];
|
|
918
|
-
for (let i = 0; i < count; i++)
|
|
919
|
-
table.push(r.string());
|
|
920
|
-
return table;
|
|
921
|
-
}
|
|
922
|
-
function readPayload(r, schemas, scope, version) {
|
|
923
|
-
const schema = schemas[scope];
|
|
924
|
-
const source = versionLayout(schema, version);
|
|
925
|
-
const latest = versionLayout(schema, schema.latest).fields;
|
|
926
|
-
const value = readFields(r, source.fields, schemas, scope, source.intern ? readTable(r) : [], version === schema.latest);
|
|
927
|
-
if (version === schema.latest)
|
|
928
|
-
return value;
|
|
929
|
-
const result = project(value, source.fields, latest, schemas, scope, false);
|
|
930
|
-
for (const field of latest)
|
|
931
|
-
validate(field.ty, result[field.name], field.name);
|
|
932
|
-
return result;
|
|
933
|
-
}
|
|
934
|
-
function writePayload(w, schemas, scope, version, value) {
|
|
935
|
-
const schema = schemas[scope];
|
|
936
|
-
const target = versionLayout(schema, version);
|
|
937
|
-
const latest = versionLayout(schema, schema.latest).fields;
|
|
938
|
-
const table = new Map();
|
|
939
|
-
let wire = value;
|
|
940
|
-
if (version !== schema.latest) {
|
|
941
|
-
for (const field of latest)
|
|
942
|
-
validate(field.ty, value[field.name], field.name);
|
|
943
|
-
wire = project(value, latest, target.fields, schemas, scope, true);
|
|
944
|
-
}
|
|
945
|
-
if (target.intern) {
|
|
946
|
-
const body = new Writer();
|
|
947
|
-
writeFields(body, target.fields, wire, schemas, scope, table, version === schema.latest);
|
|
948
|
-
w.length(table.size);
|
|
949
|
-
for (const text of table.keys())
|
|
950
|
-
w.string(text);
|
|
951
|
-
w.bytes(body.finish());
|
|
952
|
-
}
|
|
953
|
-
else
|
|
954
|
-
writeFields(w, target.fields, wire, schemas, scope, table, version === schema.latest);
|
|
955
|
-
}
|
|
956
|
-
function openEnvelope(input, offset) {
|
|
957
|
-
const bytes = input instanceof Uint8Array ? input : new Uint8Array(input);
|
|
958
|
-
if (!Number.isSafeInteger(offset) || offset < 0 || offset > bytes.length)
|
|
959
|
-
invalid("Invalid Pojoc offset");
|
|
960
|
-
const envelope = new Reader(bytes.subarray(offset));
|
|
961
|
-
const version = envelope.varint();
|
|
962
|
-
const reader = new Reader(envelope.bytes(envelope.uint(4)));
|
|
963
|
-
return { version, reader, offset: offset + envelope.pos };
|
|
964
|
-
}
|
|
965
|
-
function skipValue(r, ty, schemas, scope, table) {
|
|
966
|
-
if (typeof ty === "string") {
|
|
967
|
-
if (ty === "string")
|
|
968
|
-
r.take(r.length());
|
|
969
|
-
else if (ty === "varint32")
|
|
970
|
-
r.varint32();
|
|
971
|
-
else if (ty === "varint64" || ty === "intern")
|
|
972
|
-
r.varint();
|
|
973
|
-
else
|
|
974
|
-
r.take(arrayOf(ty)?.BYTES_PER_ELEMENT ?? 1);
|
|
975
|
-
return;
|
|
976
|
-
}
|
|
977
|
-
switch (ty[0]) {
|
|
978
|
-
case "bytes":
|
|
979
|
-
case "bits":
|
|
980
|
-
case "enum":
|
|
981
|
-
r.take(ty[1]);
|
|
982
|
-
return;
|
|
983
|
-
case "union":
|
|
984
|
-
r.take(ty[1]);
|
|
985
|
-
r.take(r.length());
|
|
986
|
-
return;
|
|
987
|
-
case "vfloat":
|
|
988
|
-
r.take(ty[3]);
|
|
989
|
-
return;
|
|
990
|
-
case "optional":
|
|
991
|
-
if (r.byte())
|
|
992
|
-
skipValue(r, ty[1], schemas, scope, table);
|
|
993
|
-
return;
|
|
994
|
-
case "constraint":
|
|
995
|
-
case "box": return skipValue(r, ty[1], schemas, scope, table);
|
|
996
|
-
case "record":
|
|
997
|
-
positionField(r, schemas[scope].records[ty[2]], schemas, scope, table);
|
|
998
|
-
return;
|
|
999
|
-
case "import":
|
|
1000
|
-
readPayload(r, schemas, schemas[scope].imports[ty[1]], ty[2]);
|
|
1001
|
-
return;
|
|
1002
|
-
case "tuple":
|
|
1003
|
-
for (const item of ty[1])
|
|
1004
|
-
skipValue(r, item, schemas, scope, table);
|
|
1005
|
-
return;
|
|
1006
|
-
case "array":
|
|
1007
|
-
case "delta": {
|
|
1008
|
-
const count = ty[2] ?? r.length();
|
|
1009
|
-
const ctor = ty[0] === "array" && arrayOf(ty[1]);
|
|
1010
|
-
if (ctor) {
|
|
1011
|
-
r.take(count * ctor.BYTES_PER_ELEMENT);
|
|
1012
|
-
return;
|
|
1013
|
-
}
|
|
1014
|
-
limitCollection(count);
|
|
1015
|
-
for (let i = 0; i < count; i++) {
|
|
1016
|
-
if (ty[0] === "delta")
|
|
1017
|
-
r.varint();
|
|
1018
|
-
else
|
|
1019
|
-
skipValue(r, ty[1], schemas, scope, table);
|
|
1020
|
-
}
|
|
1021
|
-
return;
|
|
1022
|
-
}
|
|
1023
|
-
case "map": {
|
|
1024
|
-
const count = ty[3] ?? r.length();
|
|
1025
|
-
limitCollection(count);
|
|
1026
|
-
for (let i = 0; i < count; i++) {
|
|
1027
|
-
skipValue(r, ty[1], schemas, scope, table);
|
|
1028
|
-
skipValue(r, ty[2], schemas, scope, table);
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
}
|
|
1033
|
-
function positionField(r, fields, schemas, scope, table, wanted) {
|
|
1034
|
-
const header = r.bytes(headerLength(fields));
|
|
1035
|
-
let bit = 0;
|
|
1036
|
-
for (const field of fields) {
|
|
1037
|
-
let ty = field.ty;
|
|
1038
|
-
if (isOptional(field.ty)) {
|
|
1039
|
-
const present = header[bit >> 3] & 1 << (bit & 7);
|
|
1040
|
-
bit++;
|
|
1041
|
-
ty = present ? field.ty[1] : null;
|
|
1042
|
-
}
|
|
1043
|
-
if (field === wanted)
|
|
1044
|
-
return ty;
|
|
1045
|
-
if (ty !== null)
|
|
1046
|
-
skipValue(r, ty, schemas, scope, table);
|
|
1047
|
-
}
|
|
1048
|
-
return null;
|
|
1049
|
-
}
|
|
1050
|
-
function makeCodec(schemas, scope) {
|
|
1051
|
-
const schema = schemas[scope];
|
|
1052
|
-
const current = BigInt(schema.latest);
|
|
1053
|
-
const latest = versionLayout(schema, schema.latest);
|
|
1054
|
-
function encodeForVersion(value, version) {
|
|
1055
|
-
if (typeof version === "number" && !Number.isSafeInteger(version))
|
|
1056
|
-
invalid("Use bigint for a large Pojoc version");
|
|
1057
|
-
const n = BigInt(version);
|
|
1058
|
-
const writer = new Writer();
|
|
1059
|
-
writer.varint(n);
|
|
1060
|
-
const length = writer.take(4);
|
|
1061
|
-
writePayload(writer, schemas, scope, n.toString(), object(value));
|
|
1062
|
-
const bytes = writer.pos - length - 4;
|
|
1063
|
-
if (bytes > 0xffff_ffff)
|
|
1064
|
-
invalid("Pojoc payload exceeds its envelope");
|
|
1065
|
-
writer.view.setUint32(length, bytes, true);
|
|
1066
|
-
return writer.finish();
|
|
1067
|
-
}
|
|
1068
|
-
function decodeFrom(input, offset = 0) {
|
|
1069
|
-
const envelope = openEnvelope(input, offset);
|
|
1070
|
-
const value = readPayload(envelope.reader, schemas, scope, envelope.version.toString());
|
|
1071
|
-
if (envelope.reader.pos !== envelope.reader.data.length)
|
|
1072
|
-
invalid("Unexpected trailing Pojoc payload");
|
|
1073
|
-
return { value, version: envelope.version, offset: envelope.offset };
|
|
1074
|
-
}
|
|
1075
|
-
function seek(input, path) {
|
|
1076
|
-
const names = path.split(".");
|
|
1077
|
-
let fields = latest.fields;
|
|
1078
|
-
const hops = [];
|
|
1079
|
-
for (let i = 0; i < names.length; i++) {
|
|
1080
|
-
const field = fields.find(item => item.name === names[i]);
|
|
1081
|
-
if (!field?.seek)
|
|
1082
|
-
invalid(`Unknown seekable Pojoc path ${path}`);
|
|
1083
|
-
hops.push({ fields, field });
|
|
1084
|
-
if (i + 1 < names.length) {
|
|
1085
|
-
if (field.lazy || typeof field.ty === "string" || field.ty[0] !== "record")
|
|
1086
|
-
invalid(`Invalid nested path ${path}`);
|
|
1087
|
-
fields = schema.records[field.ty[2]];
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
const envelope = openEnvelope(input, 0);
|
|
1091
|
-
if (envelope.version !== current) {
|
|
1092
|
-
let value = decodeFrom(input).value;
|
|
1093
|
-
for (const name of names)
|
|
1094
|
-
value = object(value)[name];
|
|
1095
|
-
return value;
|
|
1096
|
-
}
|
|
1097
|
-
const r = envelope.reader;
|
|
1098
|
-
const table = latest.intern ? readTable(r) : [];
|
|
1099
|
-
let selected = null;
|
|
1100
|
-
for (const hop of hops)
|
|
1101
|
-
selected = positionField(r, hop.fields, schemas, scope, table, hop.field);
|
|
1102
|
-
const leaf = hops[hops.length - 1].field;
|
|
1103
|
-
const value = selected === null ? null : readValue(r, selected, schemas, scope, table);
|
|
1104
|
-
validate(leaf.ty, value, leaf.name);
|
|
1105
|
-
return value;
|
|
1106
|
-
}
|
|
1107
|
-
return {
|
|
1108
|
-
version: current,
|
|
1109
|
-
encode: (value) => encodeForVersion(value, current),
|
|
1110
|
-
encodeForVersion,
|
|
1111
|
-
decode: (input) => decodeFrom(input).value,
|
|
1112
|
-
decodeFrom,
|
|
1113
|
-
create: (value = {}) => defaults(latest.fields, object(value), schemas, scope),
|
|
1114
|
-
seek,
|
|
1115
|
-
};
|
|
1116
|
-
}
|
|
1117
|
-
export { makeCodec };
|
|
1
|
+
export { makeCodec } from "./legacy.js";
|