@omnibuf/core 0.0.3 → 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/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, mapHeaderByte } from './encoder.js';
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, WireFixedArr, WireMap, WireSint, WireSintArr, WireString, WireStringArr, WireSubMsg, WireSubMsgArr, WireUint, WireUintArr, decodeUTF8, decodeVarint, decodeVarintAuto, decodeVarintBigInt, decodeZigZag, decodeZigZagAuto, decodeZigZagBigInt, encodeZigZag, encodeZigZagNumber, float32FromBits, float64FromBits, readFloat64At, readLenSlice, roundFloat32Shortest, } from './wire.js';
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[]' ? 'float32[]' : 'fixed32[]';
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[]' ? 'float64[]' : 'fixed64[]';
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' ? 'float32' : 'fixed32';
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' ? 'float64' : 'fixed64';
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 'float64':
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 WireSubMsgArr;
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 > 0xffffffff) {
200
- throw new Error(`omnibuf: field number ${spec.fieldNum} on ${typeName}.${propKey} out of range (1..4294967295)`);
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 = mk === 'bool' ? WireUint : scalarWireType(mk);
242
- cf.mapValWT = mv === 'bool' ? WireUint : scalarWireType(mv);
275
+ cf.mapKeyWT = scalarWireType(mk);
276
+ cf.mapValWT = scalarWireType(mv);
243
277
  validateMapTypes(cf.mapKeyWT, cf.mapValWT);
244
278
  }
245
279
  if (oneofLocal) {
@@ -896,7 +930,7 @@ export class MessageSchema {
896
930
  }
897
931
  else {
898
932
  const [t64, tn] = decodeVarintBigInt(data, pos);
899
- if (tn === 0 || t64 >> 4n > 0xffffffffn) {
933
+ if (tn === 0 || t64 >> 4n > 536870911n) {
900
934
  throw new Error(`omnibuf: truncated or invalid multi-byte tag at ${pos}`);
901
935
  }
902
936
  tag = Number(t64);
@@ -1272,7 +1306,6 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
1272
1306
  const fnum = f.fieldNum;
1273
1307
  switch (f.effectiveType) {
1274
1308
  case 'uint32':
1275
- case 'enum':
1276
1309
  enc.writeUint(fnum, Number(rawVal ?? 0) >>> 0);
1277
1310
  break;
1278
1311
  case 'uint64': {
@@ -1282,6 +1315,7 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
1282
1315
  }
1283
1316
  case 'sint32':
1284
1317
  case 'int32':
1318
+ case 'enum':
1285
1319
  enc.writeInt(fnum, Number(rawVal ?? 0) | 0);
1286
1320
  break;
1287
1321
  case 'sint64':
@@ -1305,16 +1339,20 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
1305
1339
  enc.writeFloat32(fnum, Number(rawVal ?? 0));
1306
1340
  break;
1307
1341
  case 'fixed32':
1308
- case 'sfixed32':
1309
1342
  enc.writeFixed32(fnum, Number(rawVal ?? 0) >>> 0);
1310
1343
  break;
1344
+ case 'sfixed32':
1345
+ enc.writeSfixed32(fnum, Number(rawVal ?? 0) | 0);
1346
+ break;
1311
1347
  case 'float64':
1312
1348
  enc.writeFloat64(fnum, Number(rawVal ?? 0));
1313
1349
  break;
1314
1350
  case 'fixed64':
1315
- case 'sfixed64':
1316
1351
  enc.writeFixed64(fnum, rawVal ?? 0);
1317
1352
  break;
1353
+ case 'sfixed64':
1354
+ enc.writeSfixed64(fnum, rawVal ?? 0);
1355
+ break;
1318
1356
  case 'string[]':
1319
1357
  enc.writeStringArr(fnum, Array.isArray(rawVal) ? rawVal : []);
1320
1358
  break;
@@ -1325,7 +1363,6 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
1325
1363
  enc.writeBoolArr(fnum, Array.isArray(rawVal) ? rawVal : []);
1326
1364
  break;
1327
1365
  case 'uint32[]':
1328
- case 'enum[]':
1329
1366
  enc.writeUint32Arr(fnum, rawVal ?? []);
1330
1367
  break;
1331
1368
  case 'uint64[]':
@@ -1333,6 +1370,7 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
1333
1370
  break;
1334
1371
  case 'sint32[]':
1335
1372
  case 'int32[]':
1373
+ case 'enum[]':
1336
1374
  case 'sint64[]':
1337
1375
  case 'int64[]':
1338
1376
  enc.writeSintArr(fnum, rawVal ?? []);
@@ -1341,16 +1379,20 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
1341
1379
  enc.writeFloat32Arr(fnum, rawVal ?? []);
1342
1380
  break;
1343
1381
  case 'fixed32[]':
1344
- case 'sfixed32[]':
1345
1382
  enc.writeFixed32Arr(fnum, rawVal ?? []);
1346
1383
  break;
1384
+ case 'sfixed32[]':
1385
+ enc.writeSfixed32Arr(fnum, rawVal ?? []);
1386
+ break;
1347
1387
  case 'float64[]':
1348
1388
  enc.writeFloat64Arr(fnum, rawVal ?? []);
1349
1389
  break;
1350
1390
  case 'fixed64[]':
1351
- case 'sfixed64[]':
1352
1391
  enc.writeFixed64Arr(fnum, rawVal ?? []);
1353
1392
  break;
1393
+ case 'sfixed64[]':
1394
+ enc.writeSfixed64Arr(fnum, rawVal ?? []);
1395
+ break;
1354
1396
  case 'submsg[]': {
1355
1397
  const subSchema = f.resolveSubSchema?.();
1356
1398
  if (!subSchema)
@@ -1386,7 +1428,6 @@ function encodeMapScalarEntry(enc, t, val) {
1386
1428
  : new Uint8Array(0));
1387
1429
  break;
1388
1430
  case 'uint32':
1389
- case 'enum':
1390
1431
  enc.writeRawVarint(Number(val ?? 0) >>> 0);
1391
1432
  break;
1392
1433
  case 'uint64': {
@@ -1402,6 +1443,7 @@ function encodeMapScalarEntry(enc, t, val) {
1402
1443
  }
1403
1444
  case 'sint32':
1404
1445
  case 'int32':
1446
+ case 'enum':
1405
1447
  enc.writeRawVarint(encodeZigZagNumber(Number(val ?? 0) | 0));
1406
1448
  break;
1407
1449
  case 'sint64':
@@ -1428,7 +1470,7 @@ function encodeMapScalarEntry(enc, t, val) {
1428
1470
  }
1429
1471
  case 'bool': {
1430
1472
  const b = val === true || val === 'true' || val === 1;
1431
- enc.writeRawVarint(b ? 1 : 0);
1473
+ enc.writeByte(b ? 1 : 0);
1432
1474
  break;
1433
1475
  }
1434
1476
  }
@@ -1465,7 +1507,8 @@ function encodeMapField(enc, f, rawVal, deterministic, writeUnknown = false) {
1465
1507
  : [];
1466
1508
  enc.writeTag(f.fieldNum, WireMap);
1467
1509
  enc.beginContainer();
1468
- enc.writeByte(mapHeaderByte(keyWT, valWT));
1510
+ enc.writeByte(keyWT);
1511
+ enc.writeByte(valWT);
1469
1512
  enc.writeRawVarint(entries.length);
1470
1513
  if (deterministic && entries.length > 1) {
1471
1514
  entries.sort((a, b) => compareMapKeys(keyType, a[0], b[0]));
@@ -1504,20 +1547,6 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1504
1547
  out[key] = Number(v & 0xffffffffn) >>> 0;
1505
1548
  return pos + vn;
1506
1549
  }
1507
- case 'enum': {
1508
- if (pos < n && (data[pos] & 7) !== 0) {
1509
- const [v, vn] = decodeVarint(data, pos);
1510
- if (vn === 0)
1511
- throw new Error(`omnibuf: truncated enum at ${pos}`);
1512
- out[key] = v | 0;
1513
- return pos + vn;
1514
- }
1515
- const [v, vn] = decodeVarintBigInt(data, pos);
1516
- if (vn === 0)
1517
- throw new Error(`omnibuf: truncated enum at ${pos}`);
1518
- out[key] = Number(v & 0xffffffffn) | 0;
1519
- return pos + vn;
1520
- }
1521
1550
  case 'uint64': {
1522
1551
  const [v, vn] = f.useBigInt ? decodeVarintBigInt(data, pos) : decodeVarintAuto(data, pos);
1523
1552
  if (vn === 0)
@@ -1526,7 +1555,8 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1526
1555
  return pos + vn;
1527
1556
  }
1528
1557
  case 'sint32':
1529
- case 'int32': {
1558
+ case 'int32':
1559
+ case 'enum': {
1530
1560
  if (pos < n && (data[pos] & 7) !== 0) {
1531
1561
  const [v, vn] = decodeVarint(data, pos);
1532
1562
  if (vn === 0)
@@ -1564,7 +1594,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1564
1594
  }
1565
1595
  case 'float32': {
1566
1596
  if (n - pos < 4)
1567
- throw new Error(`omnibuf: truncated fixed32 at ${pos}`);
1597
+ throw new Error(`omnibuf: truncated float32 at ${pos}`);
1568
1598
  const bits = (data[pos] | (data[pos + 1] << 8) | (data[pos + 2] << 16) | (data[pos + 3] << 24)) >>>
1569
1599
  0;
1570
1600
  out[key] = roundFloat32Shortest(float32FromBits(bits));
@@ -1587,7 +1617,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1587
1617
  }
1588
1618
  case 'float64': {
1589
1619
  if (n - pos < 8)
1590
- throw new Error(`omnibuf: truncated fixed64 at ${pos}`);
1620
+ throw new Error(`omnibuf: truncated float64 at ${pos}`);
1591
1621
  out[key] = readFloat64At(data, pos);
1592
1622
  return pos + 8;
1593
1623
  }
@@ -1614,7 +1644,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1614
1644
  return pos + 8;
1615
1645
  }
1616
1646
  case 'string[]': {
1617
- const [body, nextPos] = readLenSlice(data, pos, 1);
1647
+ const [body, nextPos] = readLenSlice(data, pos, 2);
1618
1648
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1619
1649
  const [arr, ok] = appendStringArrBody(prev, body);
1620
1650
  if (!ok)
@@ -1623,7 +1653,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1623
1653
  return nextPos;
1624
1654
  }
1625
1655
  case 'bytes[]': {
1626
- const [body, nextPos] = readLenSlice(data, pos, 1);
1656
+ const [body, nextPos] = readLenSlice(data, pos, 2);
1627
1657
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1628
1658
  const [arr, ok] = appendBytesArrBody(prev, body, false);
1629
1659
  if (!ok)
@@ -1632,7 +1662,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1632
1662
  return nextPos;
1633
1663
  }
1634
1664
  case 'bool[]': {
1635
- const [body, nextPos] = readLenSlice(data, pos, 1);
1665
+ const [body, nextPos] = readLenSlice(data, pos, 2);
1636
1666
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1637
1667
  const [arr, ok] = appendBoolArrBody(prev, body);
1638
1668
  if (!ok)
@@ -1641,7 +1671,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1641
1671
  return nextPos;
1642
1672
  }
1643
1673
  case 'uint32[]': {
1644
- const [body, nextPos] = readLenSlice(data, pos, 1);
1674
+ const [body, nextPos] = readLenSlice(data, pos, 2);
1645
1675
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1646
1676
  const [arr, ok] = appendUint32ArrBody(prev, body);
1647
1677
  if (!ok)
@@ -1650,7 +1680,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1650
1680
  return nextPos;
1651
1681
  }
1652
1682
  case 'enum[]': {
1653
- const [body, nextPos] = readLenSlice(data, pos, 1);
1683
+ const [body, nextPos] = readLenSlice(data, pos, 2);
1654
1684
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1655
1685
  const [arr, ok] = appendEnumArrBody(prev, body);
1656
1686
  if (!ok)
@@ -1659,7 +1689,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1659
1689
  return nextPos;
1660
1690
  }
1661
1691
  case 'uint64[]': {
1662
- const [body, nextPos] = readLenSlice(data, pos, 1);
1692
+ const [body, nextPos] = readLenSlice(data, pos, 2);
1663
1693
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1664
1694
  const [arr, ok] = appendUintArrAutoBody(prev, body);
1665
1695
  if (!ok)
@@ -1671,7 +1701,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1671
1701
  case 'int32[]':
1672
1702
  case 'sint64[]':
1673
1703
  case 'int64[]': {
1674
- const [body, nextPos] = readLenSlice(data, pos, 1);
1704
+ const [body, nextPos] = readLenSlice(data, pos, 2);
1675
1705
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1676
1706
  const [arr, ok] = appendSintArrAutoBody(prev, body);
1677
1707
  if (!ok)
@@ -1681,7 +1711,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1681
1711
  return nextPos;
1682
1712
  }
1683
1713
  case 'float32[]': {
1684
- const [body, nextPos] = readLenSlice(data, pos, 1);
1714
+ const [body, nextPos] = readLenSlice(data, pos, 2);
1685
1715
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1686
1716
  const [arr, ok] = appendFloat32ArrBody(prev, body);
1687
1717
  if (!ok)
@@ -1690,14 +1720,14 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1690
1720
  return nextPos;
1691
1721
  }
1692
1722
  case 'fixed32[]': {
1693
- const [body, nextPos] = readLenSlice(data, pos, 1);
1694
- if (body.length < 1 || body[0] !== WireFixed32 || (body.length - 1) % 4 !== 0) {
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) {
1695
1726
  throw new Error(`omnibuf: invalid fixed32 array at ${pos}`);
1696
1727
  }
1697
1728
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1698
- const c = (body.length - 1) / 4;
1699
1729
  for (let i = 0; i < c; i++) {
1700
- const off = 1 + i * 4;
1730
+ const off = startP + i * 4;
1701
1731
  prev.push((body[off] | (body[off + 1] << 8) | (body[off + 2] << 16) | (body[off + 3] << 24)) >>>
1702
1732
  0);
1703
1733
  }
@@ -1705,21 +1735,21 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1705
1735
  return nextPos;
1706
1736
  }
1707
1737
  case 'sfixed32[]': {
1708
- const [body, nextPos] = readLenSlice(data, pos, 1);
1709
- if (body.length < 1 || body[0] !== WireFixed32 || (body.length - 1) % 4 !== 0) {
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) {
1710
1741
  throw new Error(`omnibuf: invalid sfixed32 array at ${pos}`);
1711
1742
  }
1712
1743
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1713
- const c = (body.length - 1) / 4;
1714
1744
  for (let i = 0; i < c; i++) {
1715
- const off = 1 + i * 4;
1745
+ const off = startP + i * 4;
1716
1746
  prev.push(body[off] | (body[off + 1] << 8) | (body[off + 2] << 16) | (body[off + 3] << 24) | 0);
1717
1747
  }
1718
1748
  out[key] = prev;
1719
1749
  return nextPos;
1720
1750
  }
1721
1751
  case 'float64[]': {
1722
- const [body, nextPos] = readLenSlice(data, pos, 1);
1752
+ const [body, nextPos] = readLenSlice(data, pos, 2);
1723
1753
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1724
1754
  const [arr, ok] = appendFloat64ArrBody(prev, body);
1725
1755
  if (!ok)
@@ -1729,15 +1759,16 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1729
1759
  }
1730
1760
  case 'fixed64[]':
1731
1761
  case 'sfixed64[]': {
1732
- const [body, nextPos] = readLenSlice(data, pos, 1);
1733
- if (body.length < 1 || body[0] !== WireFixed64 || (body.length - 1) % 8 !== 0) {
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) {
1734
1767
  throw new Error(`omnibuf: invalid fixed64 array at ${pos}`);
1735
1768
  }
1736
1769
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1737
- const c = (body.length - 1) / 8;
1738
- const isSigned = f.effectiveType === 'sfixed64[]';
1739
1770
  for (let i = 0; i < c; i++) {
1740
- const off = 1 + i * 8;
1771
+ const off = startP + i * 8;
1741
1772
  let u64 = 0n;
1742
1773
  for (let b = 7; b >= 0; b--) {
1743
1774
  u64 = (u64 << 8n) | BigInt(body[off + b]);
@@ -1771,7 +1802,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1771
1802
  return nextPos;
1772
1803
  }
1773
1804
  case 'submsg[]': {
1774
- const [arrPayload, nextPos] = readLenSlice(data, pos, 0);
1805
+ const [arrPayload, nextPos] = readLenSlice(data, pos, 2);
1775
1806
  const [elemCount, ok] = countSubMsgArrElems(arrPayload);
1776
1807
  if (!ok) {
1777
1808
  throw new Error(`omnibuf: corrupt submsg array at ${pos}`);
@@ -1781,7 +1812,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1781
1812
  throw new Error(`omnibuf: missing subSchema on ${key}`);
1782
1813
  }
1783
1814
  const elems = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1784
- let p = 0;
1815
+ let [, , p] = parseArrayHeader(arrPayload);
1785
1816
  for (let i = 0; i < elemCount; i++) {
1786
1817
  const [el, en] = decodeVarintBigInt(arrPayload, p);
1787
1818
  p += en;
@@ -1794,7 +1825,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1794
1825
  return nextPos;
1795
1826
  }
1796
1827
  case 'map': {
1797
- const [body, nextPos] = readLenSlice(data, pos, 2);
1828
+ const [body, nextPos] = readLenSlice(data, pos, 3);
1798
1829
  decodeMapFieldBody(f, out, body, alreadySeen, depth, options);
1799
1830
  return nextPos;
1800
1831
  }
@@ -1822,24 +1853,30 @@ function decodeMapScalarEntry(body, p, wt, subSchema, depth, options) {
1822
1853
  throw new Error(`omnibuf: truncated WireMap sint at ${p}`);
1823
1854
  return [decodeZigZagAuto(v), p + vn];
1824
1855
  }
1825
- case WireFixed32: {
1856
+ case WireFixed32:
1857
+ case WireSfixed32:
1858
+ case WireFloat32: {
1826
1859
  if (body.length - p < 4) {
1827
- throw new Error(`omnibuf: truncated WireMap fixed32 at ${p}`);
1860
+ throw new Error(`omnibuf: truncated WireMap 32-bit entry at ${p}`);
1828
1861
  }
1829
1862
  const bits = (body[p] | (body[p + 1] << 8) | (body[p + 2] << 16) | (body[p + 3] << 24)) >>> 0;
1830
1863
  return [bits, p + 4];
1831
1864
  }
1832
- case WireFixed64: {
1865
+ case WireFixed64:
1866
+ case WireSfixed64:
1867
+ case WireFloat64: {
1833
1868
  if (body.length - p < 8) {
1834
- throw new Error(`omnibuf: truncated WireMap fixed64 at ${p}`);
1869
+ throw new Error(`omnibuf: truncated WireMap 64-bit entry at ${p}`);
1835
1870
  }
1836
1871
  let u64 = 0n;
1837
1872
  for (let b = 7; b >= 0; b--) {
1838
1873
  u64 = (u64 << 8n) | BigInt(body[p + b]);
1839
1874
  }
1875
+ if (wt === WireFloat64) {
1876
+ return [u64, p + 8];
1877
+ }
1840
1878
  return [u64 <= 9007199254740991n ? Number(u64) : u64, p + 8];
1841
1879
  }
1842
- case WireBoolTrue:
1843
1880
  case WireBoolFalse: {
1844
1881
  if (body.length - p < 1) {
1845
1882
  throw new Error(`omnibuf: truncated WireMap bool at ${p}`);
@@ -1863,12 +1900,13 @@ function decodeMapScalarEntry(body, p, wt, subSchema, depth, options) {
1863
1900
  }
1864
1901
  }
1865
1902
  function decodeMapFieldBody(f, out, body, alreadySeen, depth, options) {
1866
- const expHeader = mapHeaderByte(f.mapKeyWT ?? WireString, f.mapValWT ?? WireUint);
1867
- if (body.length < 2 || body[0] !== expHeader) {
1868
- throw new Error(`omnibuf: WireMap header mismatch (got 0x${body[0]?.toString(16)}, want 0x${expHeader.toString(16)})`);
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})`);
1869
1907
  }
1870
- const [count, cn] = decodeVarintBigInt(body, 1);
1871
- if (cn === 0 || count * 2n > BigInt(body.length - (1 + cn))) {
1908
+ const [count, cn] = decodeVarintBigInt(body, 2);
1909
+ if (cn === 0 || count > BigInt(Math.floor((body.length - (2 + cn)) / 2))) {
1872
1910
  throw new Error(`omnibuf: invalid WireMap count ${count}`);
1873
1911
  }
1874
1912
  const subSchema = f.resolveSubSchema?.();
@@ -1890,14 +1928,14 @@ function decodeMapFieldBody(f, out, body, alreadySeen, depth, options) {
1890
1928
  f.mapValType === 'int64' ||
1891
1929
  f.mapValType === 'fixed64' ||
1892
1930
  f.mapValType === 'sfixed64');
1893
- let p = 1 + cn;
1931
+ let p = 2 + cn;
1894
1932
  const c = Number(count);
1895
1933
  for (let i = 0; i < c; i++) {
1896
1934
  const [kv, nextP] = decodeMapScalarEntry(body, p, f.mapKeyWT ?? WireString, undefined, depth, options);
1897
1935
  const [rawVv, nextP2] = decodeMapScalarEntry(body, nextP, f.mapValWT ?? WireUint, subSchema, depth, options);
1898
1936
  const convert = (v, type) => {
1899
1937
  if (type === 'bool')
1900
- return v !== 0 && v !== 0n;
1938
+ return Boolean(v);
1901
1939
  if (type === 'float32')
1902
1940
  return roundFloat32Shortest(float32FromBits(Number(v)));
1903
1941
  if (type === 'float64')