@omnibuf/core 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/blind.d.ts +3 -3
- package/dist/blind.d.ts.map +1 -1
- package/dist/blind.js +414 -170
- package/dist/blind.js.map +1 -1
- package/dist/encoder.d.ts +48 -30
- package/dist/encoder.d.ts.map +1 -1
- package/dist/encoder.js +161 -60
- package/dist/encoder.js.map +1 -1
- package/dist/protobuf-es.d.ts.map +1 -1
- package/dist/protobuf-es.js +3 -1
- package/dist/protobuf-es.js.map +1 -1
- package/dist/readers.d.ts +2 -2
- package/dist/readers.d.ts.map +1 -1
- package/dist/readers.js +235 -217
- package/dist/readers.js.map +1 -1
- package/dist/schema.d.ts +1 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +147 -103
- package/dist/schema.js.map +1 -1
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +79 -29
- package/dist/validation.js.map +1 -1
- package/dist/wire.d.ts +33 -23
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +318 -66
- package/dist/wire.js.map +1 -1
- package/package.json +1 -1
package/dist/schema.js
CHANGED
|
@@ -3,9 +3,9 @@ import { validateValue, validateMapTypes } from './validation.js';
|
|
|
3
3
|
// Tier 3: Annotated Classes & Declarative Type Mapping for Plain Objects/Classes
|
|
4
4
|
// ============================================================================
|
|
5
5
|
import { base64ToUint8Array, decodeSchemaMeta, encodeField0SchemaMeta, mergeSchemaMeta, writeField0SchemaMeta, } from './blind.js';
|
|
6
|
-
import { Encoder
|
|
6
|
+
import { Encoder } from './encoder.js';
|
|
7
7
|
import { appendBoolArrBody, appendBytesArrBody, appendEnumArrBody, appendFloat32ArrBody, appendFloat64ArrBody, appendSintArrAutoBody, appendStringArrBody, appendUint32ArrBody, appendUintArrAutoBody, countSubMsgArrElems, } from './readers.js';
|
|
8
|
-
import { MaxNestingDepth, WireBoolFalse, WireBoolTrue, WireBytes, WireFixed32, WireFixed64,
|
|
8
|
+
import { MaxNestingDepth, WireArray, WireBoolFalse, WireBoolTrue, WireBytes, WireFixed32, WireFixed64, WireFloat32, WireFloat64, WireMap, WireSfixed32, WireSfixed64, WireSint, WireString, WireSubMsg, WireUint, decodeUTF8, decodeVarint, decodeVarintAuto, decodeVarintBigInt, decodeZigZag, decodeZigZagAuto, decodeZigZagBigInt, encodeZigZag, encodeZigZagNumber, float32FromBits, float64FromBits, parseArrayHeader, readFloat64At, readLenSlice, roundFloat32Shortest, } from './wire.js';
|
|
9
9
|
/**
|
|
10
10
|
* Converts `snake_case` field names to `protoCamelCase` (matching `protoc` and `@bufbuild/protobuf` v2).
|
|
11
11
|
*/
|
|
@@ -75,9 +75,25 @@ function resolveEffectiveType(type, wire) {
|
|
|
75
75
|
? 'sint64[]'
|
|
76
76
|
: 'sint32[]';
|
|
77
77
|
case 'fixed32':
|
|
78
|
-
return type === 'float32[]'
|
|
78
|
+
return type === 'float32[]'
|
|
79
|
+
? 'float32[]'
|
|
80
|
+
: type === 'int32[]' || type === 'sint32[]' || type === 'sfixed32[]'
|
|
81
|
+
? 'sfixed32[]'
|
|
82
|
+
: 'fixed32[]';
|
|
83
|
+
case 'sfixed32':
|
|
84
|
+
return 'sfixed32[]';
|
|
85
|
+
case 'float32':
|
|
86
|
+
return 'float32[]';
|
|
79
87
|
case 'fixed64':
|
|
80
|
-
return type === 'float64[]'
|
|
88
|
+
return type === 'float64[]'
|
|
89
|
+
? 'float64[]'
|
|
90
|
+
: type === 'int64[]' || type === 'sint64[]' || type === 'sfixed64[]'
|
|
91
|
+
? 'sfixed64[]'
|
|
92
|
+
: 'fixed64[]';
|
|
93
|
+
case 'sfixed64':
|
|
94
|
+
return 'sfixed64[]';
|
|
95
|
+
case 'float64':
|
|
96
|
+
return 'float64[]';
|
|
81
97
|
case 'string':
|
|
82
98
|
return 'string[]';
|
|
83
99
|
case 'bytes':
|
|
@@ -92,9 +108,25 @@ function resolveEffectiveType(type, wire) {
|
|
|
92
108
|
case 'sint':
|
|
93
109
|
return type === 'uint64' || type === 'int64' || type === 'sint64' ? 'sint64' : 'sint32';
|
|
94
110
|
case 'fixed32':
|
|
95
|
-
return type === 'float32'
|
|
111
|
+
return type === 'float32'
|
|
112
|
+
? 'float32'
|
|
113
|
+
: type === 'int32' || type === 'sint32' || type === 'sfixed32'
|
|
114
|
+
? 'sfixed32'
|
|
115
|
+
: 'fixed32';
|
|
116
|
+
case 'sfixed32':
|
|
117
|
+
return 'sfixed32';
|
|
118
|
+
case 'float32':
|
|
119
|
+
return 'float32';
|
|
96
120
|
case 'fixed64':
|
|
97
|
-
return type === 'float64'
|
|
121
|
+
return type === 'float64'
|
|
122
|
+
? 'float64'
|
|
123
|
+
: type === 'int64' || type === 'sint64' || type === 'sfixed64'
|
|
124
|
+
? 'sfixed64'
|
|
125
|
+
: 'fixed64';
|
|
126
|
+
case 'sfixed64':
|
|
127
|
+
return 'sfixed64';
|
|
128
|
+
case 'float64':
|
|
129
|
+
return 'float64';
|
|
98
130
|
case 'string':
|
|
99
131
|
return 'string';
|
|
100
132
|
case 'bytes':
|
|
@@ -107,12 +139,12 @@ function wireTypeForEffectiveType(t) {
|
|
|
107
139
|
switch (t) {
|
|
108
140
|
case 'uint32':
|
|
109
141
|
case 'uint64':
|
|
110
|
-
case 'enum':
|
|
111
142
|
return WireUint;
|
|
112
143
|
case 'sint32':
|
|
113
144
|
case 'sint64':
|
|
114
145
|
case 'int32':
|
|
115
146
|
case 'int64':
|
|
147
|
+
case 'enum':
|
|
116
148
|
return WireSint;
|
|
117
149
|
case 'bool':
|
|
118
150
|
return WireBoolTrue;
|
|
@@ -122,36 +154,36 @@ function wireTypeForEffectiveType(t) {
|
|
|
122
154
|
return WireBytes;
|
|
123
155
|
case 'submsg':
|
|
124
156
|
return WireSubMsg;
|
|
125
|
-
case 'float32':
|
|
126
157
|
case 'fixed32':
|
|
127
|
-
case 'sfixed32':
|
|
128
158
|
return WireFixed32;
|
|
129
|
-
case '
|
|
159
|
+
case 'sfixed32':
|
|
160
|
+
return WireSfixed32;
|
|
161
|
+
case 'float32':
|
|
162
|
+
return WireFloat32;
|
|
130
163
|
case 'fixed64':
|
|
131
|
-
case 'sfixed64':
|
|
132
164
|
return WireFixed64;
|
|
165
|
+
case 'sfixed64':
|
|
166
|
+
return WireSfixed64;
|
|
167
|
+
case 'float64':
|
|
168
|
+
return WireFloat64;
|
|
133
169
|
case 'string[]':
|
|
134
170
|
case 'bytes[]':
|
|
135
|
-
return WireStringArr;
|
|
136
171
|
case 'uint32[]':
|
|
137
172
|
case 'uint64[]':
|
|
138
173
|
case 'enum[]':
|
|
139
174
|
case 'bool[]':
|
|
140
|
-
return WireUintArr;
|
|
141
175
|
case 'sint32[]':
|
|
142
176
|
case 'sint64[]':
|
|
143
177
|
case 'int32[]':
|
|
144
178
|
case 'int64[]':
|
|
145
|
-
return WireSintArr;
|
|
146
179
|
case 'float32[]':
|
|
147
180
|
case 'float64[]':
|
|
148
181
|
case 'fixed32[]':
|
|
149
182
|
case 'fixed64[]':
|
|
150
183
|
case 'sfixed32[]':
|
|
151
184
|
case 'sfixed64[]':
|
|
152
|
-
return WireFixedArr;
|
|
153
185
|
case 'submsg[]':
|
|
154
|
-
return
|
|
186
|
+
return WireArray;
|
|
155
187
|
case 'map':
|
|
156
188
|
return WireMap;
|
|
157
189
|
}
|
|
@@ -159,6 +191,8 @@ function wireTypeForEffectiveType(t) {
|
|
|
159
191
|
function scalarWireType(t) {
|
|
160
192
|
if (t === 'submsg')
|
|
161
193
|
return WireSubMsg;
|
|
194
|
+
if (t === 'bool')
|
|
195
|
+
return WireBoolFalse;
|
|
162
196
|
return wireTypeForEffectiveType(t);
|
|
163
197
|
}
|
|
164
198
|
const sharedEncoder = new Encoder(8192);
|
|
@@ -196,8 +230,8 @@ export class MessageSchema {
|
|
|
196
230
|
const compiled = [];
|
|
197
231
|
const oneofMap = new Map();
|
|
198
232
|
for (const [propKey, spec] of Object.entries(specs)) {
|
|
199
|
-
if (!Number.isInteger(spec.fieldNum) || spec.fieldNum < 1 || spec.fieldNum >
|
|
200
|
-
throw new Error(`omnibuf: field number ${spec.fieldNum} on ${typeName}.${propKey} out of range (1..
|
|
233
|
+
if (!Number.isInteger(spec.fieldNum) || spec.fieldNum < 1 || spec.fieldNum > 536870911) {
|
|
234
|
+
throw new Error(`omnibuf: field number ${spec.fieldNum} on ${typeName}.${propKey} out of range (1..536870911)`);
|
|
201
235
|
}
|
|
202
236
|
if (this.byNum.has(spec.fieldNum)) {
|
|
203
237
|
throw new Error(`omnibuf: duplicate field number ${spec.fieldNum} on ${typeName}`);
|
|
@@ -238,8 +272,8 @@ export class MessageSchema {
|
|
|
238
272
|
const mv = spec.mapVal ?? spec.mapValType ?? 'uint64';
|
|
239
273
|
cf.mapKeyType = mk;
|
|
240
274
|
cf.mapValType = mv;
|
|
241
|
-
cf.mapKeyWT =
|
|
242
|
-
cf.mapValWT =
|
|
275
|
+
cf.mapKeyWT = scalarWireType(mk);
|
|
276
|
+
cf.mapValWT = scalarWireType(mv);
|
|
243
277
|
validateMapTypes(cf.mapKeyWT, cf.mapValWT);
|
|
244
278
|
}
|
|
245
279
|
if (oneofLocal) {
|
|
@@ -315,7 +349,7 @@ export class MessageSchema {
|
|
|
315
349
|
const caseName = f.localName;
|
|
316
350
|
const altCaseName = f.name;
|
|
317
351
|
const propDesc = Object.getOwnPropertyDescriptor(obj, f.propKey);
|
|
318
|
-
if (!
|
|
352
|
+
if (!(f.propKey in obj) || (propDesc && 'value' in propDesc)) {
|
|
319
353
|
const initialPlainVal = propDesc?.value;
|
|
320
354
|
Object.defineProperty(obj, f.propKey, {
|
|
321
355
|
get() {
|
|
@@ -398,29 +432,32 @@ export class MessageSchema {
|
|
|
398
432
|
if (init !== undefined && isMessage(init, this)) {
|
|
399
433
|
return init;
|
|
400
434
|
}
|
|
401
|
-
const
|
|
435
|
+
const hasGenCreate = Boolean(this.generatedHooks?.create);
|
|
436
|
+
const obj = hasGenCreate
|
|
402
437
|
? this.generatedHooks.create()
|
|
403
438
|
: this.factory
|
|
404
439
|
? this.factory()
|
|
405
440
|
: {};
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
if (
|
|
418
|
-
|
|
441
|
+
if (!hasGenCreate) {
|
|
442
|
+
this.ensureAccessors(obj);
|
|
443
|
+
let nextCreating;
|
|
444
|
+
if (this.isCyclicOrDeep()) {
|
|
445
|
+
nextCreating = new Set(creatingAncestors);
|
|
446
|
+
nextCreating.add(this);
|
|
447
|
+
}
|
|
448
|
+
for (let i = 0; i < this.fields.length; i++) {
|
|
449
|
+
const f = this.fields[i];
|
|
450
|
+
if (f.oneof)
|
|
451
|
+
continue;
|
|
452
|
+
if (f.explicitPresence) {
|
|
453
|
+
if (!(f.propKey in obj)) {
|
|
454
|
+
obj[f.propKey] = undefined;
|
|
455
|
+
}
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
if (obj[f.propKey] === undefined) {
|
|
459
|
+
obj[f.propKey] = defaultFieldValue(f, nextCreating);
|
|
419
460
|
}
|
|
420
|
-
continue;
|
|
421
|
-
}
|
|
422
|
-
if (obj[f.propKey] === undefined) {
|
|
423
|
-
obj[f.propKey] = defaultFieldValue(f, nextCreating);
|
|
424
461
|
}
|
|
425
462
|
}
|
|
426
463
|
if (init) {
|
|
@@ -893,7 +930,7 @@ export class MessageSchema {
|
|
|
893
930
|
}
|
|
894
931
|
else {
|
|
895
932
|
const [t64, tn] = decodeVarintBigInt(data, pos);
|
|
896
|
-
if (tn === 0 || t64 >> 4n >
|
|
933
|
+
if (tn === 0 || t64 >> 4n > 536870911n) {
|
|
897
934
|
throw new Error(`omnibuf: truncated or invalid multi-byte tag at ${pos}`);
|
|
898
935
|
}
|
|
899
936
|
tag = Number(t64);
|
|
@@ -1269,7 +1306,6 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
|
|
|
1269
1306
|
const fnum = f.fieldNum;
|
|
1270
1307
|
switch (f.effectiveType) {
|
|
1271
1308
|
case 'uint32':
|
|
1272
|
-
case 'enum':
|
|
1273
1309
|
enc.writeUint(fnum, Number(rawVal ?? 0) >>> 0);
|
|
1274
1310
|
break;
|
|
1275
1311
|
case 'uint64': {
|
|
@@ -1279,6 +1315,7 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
|
|
|
1279
1315
|
}
|
|
1280
1316
|
case 'sint32':
|
|
1281
1317
|
case 'int32':
|
|
1318
|
+
case 'enum':
|
|
1282
1319
|
enc.writeInt(fnum, Number(rawVal ?? 0) | 0);
|
|
1283
1320
|
break;
|
|
1284
1321
|
case 'sint64':
|
|
@@ -1302,16 +1339,20 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
|
|
|
1302
1339
|
enc.writeFloat32(fnum, Number(rawVal ?? 0));
|
|
1303
1340
|
break;
|
|
1304
1341
|
case 'fixed32':
|
|
1305
|
-
case 'sfixed32':
|
|
1306
1342
|
enc.writeFixed32(fnum, Number(rawVal ?? 0) >>> 0);
|
|
1307
1343
|
break;
|
|
1344
|
+
case 'sfixed32':
|
|
1345
|
+
enc.writeSfixed32(fnum, Number(rawVal ?? 0) | 0);
|
|
1346
|
+
break;
|
|
1308
1347
|
case 'float64':
|
|
1309
1348
|
enc.writeFloat64(fnum, Number(rawVal ?? 0));
|
|
1310
1349
|
break;
|
|
1311
1350
|
case 'fixed64':
|
|
1312
|
-
case 'sfixed64':
|
|
1313
1351
|
enc.writeFixed64(fnum, rawVal ?? 0);
|
|
1314
1352
|
break;
|
|
1353
|
+
case 'sfixed64':
|
|
1354
|
+
enc.writeSfixed64(fnum, rawVal ?? 0);
|
|
1355
|
+
break;
|
|
1315
1356
|
case 'string[]':
|
|
1316
1357
|
enc.writeStringArr(fnum, Array.isArray(rawVal) ? rawVal : []);
|
|
1317
1358
|
break;
|
|
@@ -1322,7 +1363,6 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
|
|
|
1322
1363
|
enc.writeBoolArr(fnum, Array.isArray(rawVal) ? rawVal : []);
|
|
1323
1364
|
break;
|
|
1324
1365
|
case 'uint32[]':
|
|
1325
|
-
case 'enum[]':
|
|
1326
1366
|
enc.writeUint32Arr(fnum, rawVal ?? []);
|
|
1327
1367
|
break;
|
|
1328
1368
|
case 'uint64[]':
|
|
@@ -1330,6 +1370,7 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
|
|
|
1330
1370
|
break;
|
|
1331
1371
|
case 'sint32[]':
|
|
1332
1372
|
case 'int32[]':
|
|
1373
|
+
case 'enum[]':
|
|
1333
1374
|
case 'sint64[]':
|
|
1334
1375
|
case 'int64[]':
|
|
1335
1376
|
enc.writeSintArr(fnum, rawVal ?? []);
|
|
@@ -1338,16 +1379,20 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
|
|
|
1338
1379
|
enc.writeFloat32Arr(fnum, rawVal ?? []);
|
|
1339
1380
|
break;
|
|
1340
1381
|
case 'fixed32[]':
|
|
1341
|
-
case 'sfixed32[]':
|
|
1342
1382
|
enc.writeFixed32Arr(fnum, rawVal ?? []);
|
|
1343
1383
|
break;
|
|
1384
|
+
case 'sfixed32[]':
|
|
1385
|
+
enc.writeSfixed32Arr(fnum, rawVal ?? []);
|
|
1386
|
+
break;
|
|
1344
1387
|
case 'float64[]':
|
|
1345
1388
|
enc.writeFloat64Arr(fnum, rawVal ?? []);
|
|
1346
1389
|
break;
|
|
1347
1390
|
case 'fixed64[]':
|
|
1348
|
-
case 'sfixed64[]':
|
|
1349
1391
|
enc.writeFixed64Arr(fnum, rawVal ?? []);
|
|
1350
1392
|
break;
|
|
1393
|
+
case 'sfixed64[]':
|
|
1394
|
+
enc.writeSfixed64Arr(fnum, rawVal ?? []);
|
|
1395
|
+
break;
|
|
1351
1396
|
case 'submsg[]': {
|
|
1352
1397
|
const subSchema = f.resolveSubSchema?.();
|
|
1353
1398
|
if (!subSchema)
|
|
@@ -1383,7 +1428,6 @@ function encodeMapScalarEntry(enc, t, val) {
|
|
|
1383
1428
|
: new Uint8Array(0));
|
|
1384
1429
|
break;
|
|
1385
1430
|
case 'uint32':
|
|
1386
|
-
case 'enum':
|
|
1387
1431
|
enc.writeRawVarint(Number(val ?? 0) >>> 0);
|
|
1388
1432
|
break;
|
|
1389
1433
|
case 'uint64': {
|
|
@@ -1399,6 +1443,7 @@ function encodeMapScalarEntry(enc, t, val) {
|
|
|
1399
1443
|
}
|
|
1400
1444
|
case 'sint32':
|
|
1401
1445
|
case 'int32':
|
|
1446
|
+
case 'enum':
|
|
1402
1447
|
enc.writeRawVarint(encodeZigZagNumber(Number(val ?? 0) | 0));
|
|
1403
1448
|
break;
|
|
1404
1449
|
case 'sint64':
|
|
@@ -1425,7 +1470,7 @@ function encodeMapScalarEntry(enc, t, val) {
|
|
|
1425
1470
|
}
|
|
1426
1471
|
case 'bool': {
|
|
1427
1472
|
const b = val === true || val === 'true' || val === 1;
|
|
1428
|
-
enc.
|
|
1473
|
+
enc.writeByte(b ? 1 : 0);
|
|
1429
1474
|
break;
|
|
1430
1475
|
}
|
|
1431
1476
|
}
|
|
@@ -1462,7 +1507,8 @@ function encodeMapField(enc, f, rawVal, deterministic, writeUnknown = false) {
|
|
|
1462
1507
|
: [];
|
|
1463
1508
|
enc.writeTag(f.fieldNum, WireMap);
|
|
1464
1509
|
enc.beginContainer();
|
|
1465
|
-
enc.writeByte(
|
|
1510
|
+
enc.writeByte(keyWT);
|
|
1511
|
+
enc.writeByte(valWT);
|
|
1466
1512
|
enc.writeRawVarint(entries.length);
|
|
1467
1513
|
if (deterministic && entries.length > 1) {
|
|
1468
1514
|
entries.sort((a, b) => compareMapKeys(keyType, a[0], b[0]));
|
|
@@ -1501,20 +1547,6 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1501
1547
|
out[key] = Number(v & 0xffffffffn) >>> 0;
|
|
1502
1548
|
return pos + vn;
|
|
1503
1549
|
}
|
|
1504
|
-
case 'enum': {
|
|
1505
|
-
if (pos < n && (data[pos] & 7) !== 0) {
|
|
1506
|
-
const [v, vn] = decodeVarint(data, pos);
|
|
1507
|
-
if (vn === 0)
|
|
1508
|
-
throw new Error(`omnibuf: truncated enum at ${pos}`);
|
|
1509
|
-
out[key] = v | 0;
|
|
1510
|
-
return pos + vn;
|
|
1511
|
-
}
|
|
1512
|
-
const [v, vn] = decodeVarintBigInt(data, pos);
|
|
1513
|
-
if (vn === 0)
|
|
1514
|
-
throw new Error(`omnibuf: truncated enum at ${pos}`);
|
|
1515
|
-
out[key] = Number(v & 0xffffffffn) | 0;
|
|
1516
|
-
return pos + vn;
|
|
1517
|
-
}
|
|
1518
1550
|
case 'uint64': {
|
|
1519
1551
|
const [v, vn] = f.useBigInt ? decodeVarintBigInt(data, pos) : decodeVarintAuto(data, pos);
|
|
1520
1552
|
if (vn === 0)
|
|
@@ -1523,7 +1555,8 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1523
1555
|
return pos + vn;
|
|
1524
1556
|
}
|
|
1525
1557
|
case 'sint32':
|
|
1526
|
-
case 'int32':
|
|
1558
|
+
case 'int32':
|
|
1559
|
+
case 'enum': {
|
|
1527
1560
|
if (pos < n && (data[pos] & 7) !== 0) {
|
|
1528
1561
|
const [v, vn] = decodeVarint(data, pos);
|
|
1529
1562
|
if (vn === 0)
|
|
@@ -1561,7 +1594,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1561
1594
|
}
|
|
1562
1595
|
case 'float32': {
|
|
1563
1596
|
if (n - pos < 4)
|
|
1564
|
-
throw new Error(`omnibuf: truncated
|
|
1597
|
+
throw new Error(`omnibuf: truncated float32 at ${pos}`);
|
|
1565
1598
|
const bits = (data[pos] | (data[pos + 1] << 8) | (data[pos + 2] << 16) | (data[pos + 3] << 24)) >>>
|
|
1566
1599
|
0;
|
|
1567
1600
|
out[key] = roundFloat32Shortest(float32FromBits(bits));
|
|
@@ -1584,7 +1617,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1584
1617
|
}
|
|
1585
1618
|
case 'float64': {
|
|
1586
1619
|
if (n - pos < 8)
|
|
1587
|
-
throw new Error(`omnibuf: truncated
|
|
1620
|
+
throw new Error(`omnibuf: truncated float64 at ${pos}`);
|
|
1588
1621
|
out[key] = readFloat64At(data, pos);
|
|
1589
1622
|
return pos + 8;
|
|
1590
1623
|
}
|
|
@@ -1611,7 +1644,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1611
1644
|
return pos + 8;
|
|
1612
1645
|
}
|
|
1613
1646
|
case 'string[]': {
|
|
1614
|
-
const [body, nextPos] = readLenSlice(data, pos,
|
|
1647
|
+
const [body, nextPos] = readLenSlice(data, pos, 2);
|
|
1615
1648
|
const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
|
|
1616
1649
|
const [arr, ok] = appendStringArrBody(prev, body);
|
|
1617
1650
|
if (!ok)
|
|
@@ -1620,7 +1653,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1620
1653
|
return nextPos;
|
|
1621
1654
|
}
|
|
1622
1655
|
case 'bytes[]': {
|
|
1623
|
-
const [body, nextPos] = readLenSlice(data, pos,
|
|
1656
|
+
const [body, nextPos] = readLenSlice(data, pos, 2);
|
|
1624
1657
|
const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
|
|
1625
1658
|
const [arr, ok] = appendBytesArrBody(prev, body, false);
|
|
1626
1659
|
if (!ok)
|
|
@@ -1629,7 +1662,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1629
1662
|
return nextPos;
|
|
1630
1663
|
}
|
|
1631
1664
|
case 'bool[]': {
|
|
1632
|
-
const [body, nextPos] = readLenSlice(data, pos,
|
|
1665
|
+
const [body, nextPos] = readLenSlice(data, pos, 2);
|
|
1633
1666
|
const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
|
|
1634
1667
|
const [arr, ok] = appendBoolArrBody(prev, body);
|
|
1635
1668
|
if (!ok)
|
|
@@ -1638,7 +1671,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1638
1671
|
return nextPos;
|
|
1639
1672
|
}
|
|
1640
1673
|
case 'uint32[]': {
|
|
1641
|
-
const [body, nextPos] = readLenSlice(data, pos,
|
|
1674
|
+
const [body, nextPos] = readLenSlice(data, pos, 2);
|
|
1642
1675
|
const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
|
|
1643
1676
|
const [arr, ok] = appendUint32ArrBody(prev, body);
|
|
1644
1677
|
if (!ok)
|
|
@@ -1647,7 +1680,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1647
1680
|
return nextPos;
|
|
1648
1681
|
}
|
|
1649
1682
|
case 'enum[]': {
|
|
1650
|
-
const [body, nextPos] = readLenSlice(data, pos,
|
|
1683
|
+
const [body, nextPos] = readLenSlice(data, pos, 2);
|
|
1651
1684
|
const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
|
|
1652
1685
|
const [arr, ok] = appendEnumArrBody(prev, body);
|
|
1653
1686
|
if (!ok)
|
|
@@ -1656,7 +1689,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1656
1689
|
return nextPos;
|
|
1657
1690
|
}
|
|
1658
1691
|
case 'uint64[]': {
|
|
1659
|
-
const [body, nextPos] = readLenSlice(data, pos,
|
|
1692
|
+
const [body, nextPos] = readLenSlice(data, pos, 2);
|
|
1660
1693
|
const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
|
|
1661
1694
|
const [arr, ok] = appendUintArrAutoBody(prev, body);
|
|
1662
1695
|
if (!ok)
|
|
@@ -1668,7 +1701,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1668
1701
|
case 'int32[]':
|
|
1669
1702
|
case 'sint64[]':
|
|
1670
1703
|
case 'int64[]': {
|
|
1671
|
-
const [body, nextPos] = readLenSlice(data, pos,
|
|
1704
|
+
const [body, nextPos] = readLenSlice(data, pos, 2);
|
|
1672
1705
|
const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
|
|
1673
1706
|
const [arr, ok] = appendSintArrAutoBody(prev, body);
|
|
1674
1707
|
if (!ok)
|
|
@@ -1678,7 +1711,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1678
1711
|
return nextPos;
|
|
1679
1712
|
}
|
|
1680
1713
|
case 'float32[]': {
|
|
1681
|
-
const [body, nextPos] = readLenSlice(data, pos,
|
|
1714
|
+
const [body, nextPos] = readLenSlice(data, pos, 2);
|
|
1682
1715
|
const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
|
|
1683
1716
|
const [arr, ok] = appendFloat32ArrBody(prev, body);
|
|
1684
1717
|
if (!ok)
|
|
@@ -1687,14 +1720,14 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1687
1720
|
return nextPos;
|
|
1688
1721
|
}
|
|
1689
1722
|
case 'fixed32[]': {
|
|
1690
|
-
const [body, nextPos] = readLenSlice(data, pos,
|
|
1691
|
-
|
|
1723
|
+
const [body, nextPos] = readLenSlice(data, pos, 2);
|
|
1724
|
+
const [elemType, c, startP, ok] = parseArrayHeader(body);
|
|
1725
|
+
if (!ok || elemType !== WireFixed32 || body.length - startP !== c * 4) {
|
|
1692
1726
|
throw new Error(`omnibuf: invalid fixed32 array at ${pos}`);
|
|
1693
1727
|
}
|
|
1694
1728
|
const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
|
|
1695
|
-
const c = (body.length - 1) / 4;
|
|
1696
1729
|
for (let i = 0; i < c; i++) {
|
|
1697
|
-
const off =
|
|
1730
|
+
const off = startP + i * 4;
|
|
1698
1731
|
prev.push((body[off] | (body[off + 1] << 8) | (body[off + 2] << 16) | (body[off + 3] << 24)) >>>
|
|
1699
1732
|
0);
|
|
1700
1733
|
}
|
|
@@ -1702,21 +1735,21 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1702
1735
|
return nextPos;
|
|
1703
1736
|
}
|
|
1704
1737
|
case 'sfixed32[]': {
|
|
1705
|
-
const [body, nextPos] = readLenSlice(data, pos,
|
|
1706
|
-
|
|
1738
|
+
const [body, nextPos] = readLenSlice(data, pos, 2);
|
|
1739
|
+
const [elemType, c, startP, ok] = parseArrayHeader(body);
|
|
1740
|
+
if (!ok || elemType !== WireSfixed32 || body.length - startP !== c * 4) {
|
|
1707
1741
|
throw new Error(`omnibuf: invalid sfixed32 array at ${pos}`);
|
|
1708
1742
|
}
|
|
1709
1743
|
const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
|
|
1710
|
-
const c = (body.length - 1) / 4;
|
|
1711
1744
|
for (let i = 0; i < c; i++) {
|
|
1712
|
-
const off =
|
|
1745
|
+
const off = startP + i * 4;
|
|
1713
1746
|
prev.push(body[off] | (body[off + 1] << 8) | (body[off + 2] << 16) | (body[off + 3] << 24) | 0);
|
|
1714
1747
|
}
|
|
1715
1748
|
out[key] = prev;
|
|
1716
1749
|
return nextPos;
|
|
1717
1750
|
}
|
|
1718
1751
|
case 'float64[]': {
|
|
1719
|
-
const [body, nextPos] = readLenSlice(data, pos,
|
|
1752
|
+
const [body, nextPos] = readLenSlice(data, pos, 2);
|
|
1720
1753
|
const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
|
|
1721
1754
|
const [arr, ok] = appendFloat64ArrBody(prev, body);
|
|
1722
1755
|
if (!ok)
|
|
@@ -1726,15 +1759,16 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1726
1759
|
}
|
|
1727
1760
|
case 'fixed64[]':
|
|
1728
1761
|
case 'sfixed64[]': {
|
|
1729
|
-
const [body, nextPos] = readLenSlice(data, pos,
|
|
1730
|
-
|
|
1762
|
+
const [body, nextPos] = readLenSlice(data, pos, 2);
|
|
1763
|
+
const isSigned = f.effectiveType === 'sfixed64[]';
|
|
1764
|
+
const expElemType = isSigned ? WireSfixed64 : WireFixed64;
|
|
1765
|
+
const [elemType, c, startP, ok] = parseArrayHeader(body);
|
|
1766
|
+
if (!ok || elemType !== expElemType || body.length - startP !== c * 8) {
|
|
1731
1767
|
throw new Error(`omnibuf: invalid fixed64 array at ${pos}`);
|
|
1732
1768
|
}
|
|
1733
1769
|
const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
|
|
1734
|
-
const c = (body.length - 1) / 8;
|
|
1735
|
-
const isSigned = f.effectiveType === 'sfixed64[]';
|
|
1736
1770
|
for (let i = 0; i < c; i++) {
|
|
1737
|
-
const off =
|
|
1771
|
+
const off = startP + i * 8;
|
|
1738
1772
|
let u64 = 0n;
|
|
1739
1773
|
for (let b = 7; b >= 0; b--) {
|
|
1740
1774
|
u64 = (u64 << 8n) | BigInt(body[off + b]);
|
|
@@ -1768,7 +1802,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1768
1802
|
return nextPos;
|
|
1769
1803
|
}
|
|
1770
1804
|
case 'submsg[]': {
|
|
1771
|
-
const [arrPayload, nextPos] = readLenSlice(data, pos,
|
|
1805
|
+
const [arrPayload, nextPos] = readLenSlice(data, pos, 2);
|
|
1772
1806
|
const [elemCount, ok] = countSubMsgArrElems(arrPayload);
|
|
1773
1807
|
if (!ok) {
|
|
1774
1808
|
throw new Error(`omnibuf: corrupt submsg array at ${pos}`);
|
|
@@ -1778,7 +1812,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1778
1812
|
throw new Error(`omnibuf: missing subSchema on ${key}`);
|
|
1779
1813
|
}
|
|
1780
1814
|
const elems = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
|
|
1781
|
-
let p =
|
|
1815
|
+
let [, , p] = parseArrayHeader(arrPayload);
|
|
1782
1816
|
for (let i = 0; i < elemCount; i++) {
|
|
1783
1817
|
const [el, en] = decodeVarintBigInt(arrPayload, p);
|
|
1784
1818
|
p += en;
|
|
@@ -1791,7 +1825,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1791
1825
|
return nextPos;
|
|
1792
1826
|
}
|
|
1793
1827
|
case 'map': {
|
|
1794
|
-
const [body, nextPos] = readLenSlice(data, pos,
|
|
1828
|
+
const [body, nextPos] = readLenSlice(data, pos, 3);
|
|
1795
1829
|
decodeMapFieldBody(f, out, body, alreadySeen, depth, options);
|
|
1796
1830
|
return nextPos;
|
|
1797
1831
|
}
|
|
@@ -1819,24 +1853,30 @@ function decodeMapScalarEntry(body, p, wt, subSchema, depth, options) {
|
|
|
1819
1853
|
throw new Error(`omnibuf: truncated WireMap sint at ${p}`);
|
|
1820
1854
|
return [decodeZigZagAuto(v), p + vn];
|
|
1821
1855
|
}
|
|
1822
|
-
case WireFixed32:
|
|
1856
|
+
case WireFixed32:
|
|
1857
|
+
case WireSfixed32:
|
|
1858
|
+
case WireFloat32: {
|
|
1823
1859
|
if (body.length - p < 4) {
|
|
1824
|
-
throw new Error(`omnibuf: truncated WireMap
|
|
1860
|
+
throw new Error(`omnibuf: truncated WireMap 32-bit entry at ${p}`);
|
|
1825
1861
|
}
|
|
1826
1862
|
const bits = (body[p] | (body[p + 1] << 8) | (body[p + 2] << 16) | (body[p + 3] << 24)) >>> 0;
|
|
1827
1863
|
return [bits, p + 4];
|
|
1828
1864
|
}
|
|
1829
|
-
case WireFixed64:
|
|
1865
|
+
case WireFixed64:
|
|
1866
|
+
case WireSfixed64:
|
|
1867
|
+
case WireFloat64: {
|
|
1830
1868
|
if (body.length - p < 8) {
|
|
1831
|
-
throw new Error(`omnibuf: truncated WireMap
|
|
1869
|
+
throw new Error(`omnibuf: truncated WireMap 64-bit entry at ${p}`);
|
|
1832
1870
|
}
|
|
1833
1871
|
let u64 = 0n;
|
|
1834
1872
|
for (let b = 7; b >= 0; b--) {
|
|
1835
1873
|
u64 = (u64 << 8n) | BigInt(body[p + b]);
|
|
1836
1874
|
}
|
|
1875
|
+
if (wt === WireFloat64) {
|
|
1876
|
+
return [u64, p + 8];
|
|
1877
|
+
}
|
|
1837
1878
|
return [u64 <= 9007199254740991n ? Number(u64) : u64, p + 8];
|
|
1838
1879
|
}
|
|
1839
|
-
case WireBoolTrue:
|
|
1840
1880
|
case WireBoolFalse: {
|
|
1841
1881
|
if (body.length - p < 1) {
|
|
1842
1882
|
throw new Error(`omnibuf: truncated WireMap bool at ${p}`);
|
|
@@ -1860,12 +1900,13 @@ function decodeMapScalarEntry(body, p, wt, subSchema, depth, options) {
|
|
|
1860
1900
|
}
|
|
1861
1901
|
}
|
|
1862
1902
|
function decodeMapFieldBody(f, out, body, alreadySeen, depth, options) {
|
|
1863
|
-
const
|
|
1864
|
-
|
|
1865
|
-
|
|
1903
|
+
const expKeyWT = f.mapKeyWT ?? WireString;
|
|
1904
|
+
const expValWT = f.mapValWT ?? WireUint;
|
|
1905
|
+
if (body.length < 3 || body[0] !== expKeyWT || body[1] !== expValWT) {
|
|
1906
|
+
throw new Error(`omnibuf: WireMap header mismatch (got ${body[0]}/${body[1]}, want ${expKeyWT}/${expValWT})`);
|
|
1866
1907
|
}
|
|
1867
|
-
const [count, cn] = decodeVarintBigInt(body,
|
|
1868
|
-
if (cn === 0 || count
|
|
1908
|
+
const [count, cn] = decodeVarintBigInt(body, 2);
|
|
1909
|
+
if (cn === 0 || count > BigInt(Math.floor((body.length - (2 + cn)) / 2))) {
|
|
1869
1910
|
throw new Error(`omnibuf: invalid WireMap count ${count}`);
|
|
1870
1911
|
}
|
|
1871
1912
|
const subSchema = f.resolveSubSchema?.();
|
|
@@ -1887,14 +1928,14 @@ function decodeMapFieldBody(f, out, body, alreadySeen, depth, options) {
|
|
|
1887
1928
|
f.mapValType === 'int64' ||
|
|
1888
1929
|
f.mapValType === 'fixed64' ||
|
|
1889
1930
|
f.mapValType === 'sfixed64');
|
|
1890
|
-
let p =
|
|
1931
|
+
let p = 2 + cn;
|
|
1891
1932
|
const c = Number(count);
|
|
1892
1933
|
for (let i = 0; i < c; i++) {
|
|
1893
1934
|
const [kv, nextP] = decodeMapScalarEntry(body, p, f.mapKeyWT ?? WireString, undefined, depth, options);
|
|
1894
1935
|
const [rawVv, nextP2] = decodeMapScalarEntry(body, nextP, f.mapValWT ?? WireUint, subSchema, depth, options);
|
|
1895
1936
|
const convert = (v, type) => {
|
|
1896
1937
|
if (type === 'bool')
|
|
1897
|
-
return v
|
|
1938
|
+
return Boolean(v);
|
|
1898
1939
|
if (type === 'float32')
|
|
1899
1940
|
return roundFloat32Shortest(float32FromBits(Number(v)));
|
|
1900
1941
|
if (type === 'float64')
|
|
@@ -1942,6 +1983,9 @@ export function defineMessage(typeName, fields, factory, generatedHooks) {
|
|
|
1942
1983
|
// ============================================================================
|
|
1943
1984
|
const SCHEMA_SYMBOL = Symbol.for('omnibuf.schema');
|
|
1944
1985
|
const PENDING_FIELDS_SYMBOL = Symbol.for('omnibuf.pendingFields');
|
|
1986
|
+
if (!Symbol.metadata) {
|
|
1987
|
+
Symbol.metadata = Symbol.for('Symbol.metadata');
|
|
1988
|
+
}
|
|
1945
1989
|
const classPendingFields = new WeakMap();
|
|
1946
1990
|
const classSchemaRegistry = new WeakMap();
|
|
1947
1991
|
/**
|