@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/blind.js CHANGED
@@ -2,9 +2,9 @@
2
2
  // Field #0 (omnibuf.v1.SchemaMeta) & Schema-Less Inspection (DecodeBlind)
3
3
  // ============================================================================
4
4
  import { validateMapTypes, validateValue } from './validation.js';
5
- import { Encoder, mapHeaderByte } from './encoder.js';
6
- import { appendSintArrAutoBody, appendStringArrBody, appendUintArrAutoBody, countSubMsgArrElems, } from './readers.js';
7
- import { MaxNestingDepth, WireBoolFalse, WireBoolTrue, WireBytes, WireExtended, WireFixed32, WireFixed64, WireFixedArr, WireMap, WireSint, WireSintArr, WireString, WireStringArr, WireSubMsg, WireSubMsgArr, WireUint, WireUintArr, decodeUTF8, decodeVarintAuto, decodeVarintBigInt, decodeZigZagAuto, float32FromBits, float64FromBits, nextContainerField, readLenSlice, roundFloat32Shortest, } from './wire.js';
5
+ import { Encoder } from './encoder.js';
6
+ import { appendBoolArrBody, appendSintArrAutoBody, appendStringArrBody, appendUintArrAutoBody, countSubMsgArrElems, } from './readers.js';
7
+ import { MaxNestingDepth, WireArray, WireBoolFalse, WireBoolTrue, WireBytes, WireExtended, WireFixed32, WireFixed64, WireFloat32, WireFloat64, WireMap, WireSfixed32, WireSfixed64, WireSint, WireString, WireSubMsg, WireUint, decodeUTF8, decodeVarintAuto, decodeVarintBigInt, decodeZigZagAuto, float32FromBits, float64FromBits, nextContainerField, parseArrayHeader, parseMapHeader, readLenSlice, roundFloat32Shortest, skipValue, } from './wire.js';
8
8
  /**
9
9
  * Base64 encoder for `Uint8Array` (works in both Browser and Node.js without Buffer dependency).
10
10
  */
@@ -72,21 +72,22 @@ export function base64ToUint8Array(b64) {
72
72
  function writeSchemaMetaBody(enc, meta, deterministic) {
73
73
  if (!meta)
74
74
  return;
75
- // Field #1: type_name (WireString = 3 -> Tag (1 << 4) | 3 = 19 -> 0x27)
75
+ // Field #1: type_name (WireString = 10 -> Tag (1 << 4) | 10 = 26 -> 0x35)
76
76
  if (meta.typeName && meta.typeName.length > 0) {
77
- enc.writeByte(0x27);
77
+ enc.writeByte(0x35);
78
78
  enc.writeRawStringPayload(meta.typeName);
79
79
  }
80
- // Field #2: fields map<uint32, string> (WireMap = 9 -> Tag (2 << 4) | 9 = 41 -> 0x53)
80
+ // Field #2: fields map<uint32, string> (WireMap = 14 -> Tag (2 << 4) | 14 = 46 -> 0x5D)
81
81
  if (meta.fields) {
82
82
  const keys = Object.keys(meta.fields).map(Number);
83
83
  if (keys.length > 0) {
84
84
  if (deterministic) {
85
85
  keys.sort((a, b) => a - b);
86
86
  }
87
- enc.writeByte(0x53);
87
+ enc.writeByte(0x5d);
88
88
  enc.beginContainer();
89
- enc.writeByte(mapHeaderByte(WireUint, WireString));
89
+ enc.writeByte(WireUint);
90
+ enc.writeByte(WireString);
90
91
  enc.writeRawVarint(keys.length);
91
92
  for (const k of keys) {
92
93
  const name = meta.fields[k] ?? '';
@@ -96,16 +97,17 @@ function writeSchemaMetaBody(enc, meta, deterministic) {
96
97
  enc.endContainer();
97
98
  }
98
99
  }
99
- // Field #3: children map<uint32, SchemaMeta> (WireMap = 9 -> Tag (3 << 4) | 9 = 57 -> 0x73)
100
+ // Field #3: children map<uint32, SchemaMeta> (WireMap = 14 -> Tag (3 << 4) | 14 = 62 -> 0x7D)
100
101
  if (meta.children) {
101
102
  const keys = Object.keys(meta.children).map(Number);
102
103
  if (keys.length > 0) {
103
104
  if (deterministic) {
104
105
  keys.sort((a, b) => a - b);
105
106
  }
106
- enc.writeByte(0x73);
107
+ enc.writeByte(0x7d);
107
108
  enc.beginContainer();
108
- enc.writeByte(mapHeaderByte(WireUint, WireSubMsg));
109
+ enc.writeByte(WireUint);
110
+ enc.writeByte(WireSubMsg);
109
111
  enc.writeRawVarint(keys.length);
110
112
  for (const k of keys) {
111
113
  const child = meta.children[k];
@@ -119,18 +121,18 @@ function writeSchemaMetaBody(enc, meta, deterministic) {
119
121
  }
120
122
  }
121
123
  /**
122
- * Writes Field #0 (`Tag = 0x0B`, `WireSubMsg = 5`) containing `meta` into `enc`.
124
+ * Writes Field #0 (`Tag = 0x19`, `WireSubMsg = 12`) containing `meta` into `enc`.
123
125
  */
124
126
  export function writeField0SchemaMeta(enc, meta, deterministic = false) {
125
127
  if (!meta)
126
128
  return;
127
- enc.writeByte(0x0b); // Tag = (0 << 4) | WireSubMsg(5) = 5 -> (5 << 1) | 1 = 0x0B
129
+ enc.writeByte(0x19); // Tag = (0 << 4) | WireSubMsg(12) = 12 -> (12 << 1) | 1 = 0x19
128
130
  enc.beginContainer();
129
131
  writeSchemaMetaBody(enc, meta, deterministic);
130
132
  enc.endContainer();
131
133
  }
132
134
  /**
133
- * Encodes Field #0 (`Tag = 0x0B`, `WireSubMsg = 5`) containing `meta` as a Uint8Array.
135
+ * Encodes Field #0 (`Tag = 0x19`, `WireSubMsg = 12`) containing `meta` as a Uint8Array.
134
136
  */
135
137
  export function encodeField0SchemaMeta(meta, deterministic = true) {
136
138
  if (!meta)
@@ -190,8 +192,8 @@ export function decodeSchemaMeta(data, depth = 0, limit = MaxNestingDepth) {
190
192
  const n = data.length;
191
193
  while (pos < n) {
192
194
  const [tag, tn] = decodeVarintBigInt(data, pos);
193
- if (tn === 0) {
194
- throw new Error(`omnibuf: truncated tag in Field #0 SchemaMeta at ${pos}`);
195
+ if (tn === 0 || tag >> 4n > 536870911n) {
196
+ throw new Error(`omnibuf: truncated or invalid tag in Field #0 SchemaMeta at ${pos}`);
195
197
  }
196
198
  pos += tn;
197
199
  const fnum = Number(tag >> 4n);
@@ -210,22 +212,18 @@ export function decodeSchemaMeta(data, depth = 0, limit = MaxNestingDepth) {
210
212
  if (wt !== WireMap) {
211
213
  throw new Error(`omnibuf: SchemaMeta field #2 (fields) must be WireMap, got ${wt}`);
212
214
  }
213
- const [body, nextPos] = readLenSlice(data, pos, 2);
215
+ const [body, nextPos] = readLenSlice(data, pos, 3);
214
216
  pos = nextPos;
215
- if (body[0] !== mapHeaderByte(WireUint, WireString)) {
216
- throw new Error(`omnibuf: SchemaMeta fields map must have MapType 0x03, got 0x${body[0].toString(16)}`);
217
- }
218
- const [count, cn] = decodeVarintBigInt(body, 1);
219
- if (cn === 0 || count * 2n > BigInt(body.length - (1 + cn))) {
220
- throw new Error(`omnibuf: SchemaMeta fields count ${count} exceeds body`);
217
+ const [keyWT, valWT, c, startP, ok] = parseMapHeader(body);
218
+ if (!ok || keyWT !== WireUint || valWT !== WireString) {
219
+ throw new Error('omnibuf: invalid SchemaMeta fields map header');
221
220
  }
222
221
  if (!meta.fields)
223
222
  meta.fields = {};
224
- let p = 1 + cn;
225
- const c = Number(count);
223
+ let p = startP;
226
224
  for (let i = 0; i < c; i++) {
227
225
  const [k64, kn] = decodeVarintBigInt(body, p);
228
- if (kn === 0 || k64 === 0n || k64 > 0xffffffffn) {
226
+ if (kn === 0 || k64 === 0n || k64 > 536870911n) {
229
227
  throw new Error(`omnibuf: invalid SchemaMeta field number ${k64} at ${p}`);
230
228
  }
231
229
  p += kn;
@@ -242,22 +240,18 @@ export function decodeSchemaMeta(data, depth = 0, limit = MaxNestingDepth) {
242
240
  if (wt !== WireMap) {
243
241
  throw new Error(`omnibuf: SchemaMeta field #3 (children) must be WireMap, got ${wt}`);
244
242
  }
245
- const [body, nextPos] = readLenSlice(data, pos, 2);
243
+ const [body, nextPos] = readLenSlice(data, pos, 3);
246
244
  pos = nextPos;
247
- if (body[0] !== mapHeaderByte(WireUint, WireSubMsg)) {
248
- throw new Error(`omnibuf: SchemaMeta children map must have MapType 0x05, got 0x${body[0].toString(16)}`);
249
- }
250
- const [count, cn] = decodeVarintBigInt(body, 1);
251
- if (cn === 0 || count * 2n > BigInt(body.length - (1 + cn))) {
252
- throw new Error(`omnibuf: SchemaMeta children count ${count} exceeds body`);
245
+ const [keyWT, valWT, c, startP, ok] = parseMapHeader(body);
246
+ if (!ok || keyWT !== WireUint || valWT !== WireSubMsg) {
247
+ throw new Error('omnibuf: invalid SchemaMeta children map header');
253
248
  }
254
249
  if (!meta.children)
255
250
  meta.children = {};
256
- let p = 1 + cn;
257
- const c = Number(count);
251
+ let p = startP;
258
252
  for (let i = 0; i < c; i++) {
259
253
  const [k64, kn] = decodeVarintBigInt(body, p);
260
- if (kn === 0 || k64 === 0n || k64 > 0xffffffffn) {
254
+ if (kn === 0 || k64 === 0n || k64 > 536870911n) {
261
255
  throw new Error(`omnibuf: invalid SchemaMeta child field number ${k64} at ${p}`);
262
256
  }
263
257
  p += kn;
@@ -323,32 +317,46 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
323
317
  return target ?? {};
324
318
  }
325
319
  let activeSchema = inherited ? mergeSchemaMeta(null, inherited) : null;
326
- let pos = 0;
327
320
  const out = target ?? {};
328
- // Wire names and map keys are arbitrary strings, including __proto__.
329
- const own = (key) => (Object.hasOwn(out, key) ? out[key] : undefined);
330
321
  const roundF32 = options.roundFloat32 !== false;
331
- while (pos < n) {
332
- const [tag, tn] = decodeVarintBigInt(data, pos);
333
- if (tn === 0 || tag >> 4n > 0xffffffffn) {
334
- throw new Error(`omnibuf: truncated tag at ${pos}`);
335
- }
336
- pos += tn;
322
+ // Pre-scan all Field #0 (SchemaMeta) blocks in the message stream before resolving field names.
323
+ let scanPos = 0;
324
+ while (scanPos < n) {
325
+ const [tag, tn] = decodeVarintBigInt(data, scanPos);
326
+ if (tn === 0 || tag >> 4n > 536870911n) {
327
+ throw new Error(`omnibuf: truncated or invalid tag at ${scanPos}`);
328
+ }
329
+ scanPos += tn;
337
330
  const fieldNum = Number(tag >> 4n);
338
331
  const wt = Number(tag & 0x0fn);
339
332
  if (fieldNum === 0) {
340
333
  if (wt !== WireSubMsg) {
341
- throw new Error(`omnibuf: Field #0 must have WireSubMsg wire type (5), got ${wt}`);
334
+ throw new Error(`omnibuf: Field #0 must have WireSubMsg wire type (12), got ${wt}`);
342
335
  }
343
- const [f0Body, nextPos] = readLenSlice(data, pos, 0);
344
- pos = nextPos;
336
+ const [f0Body, nextPos] = readLenSlice(data, scanPos, 0);
337
+ scanPos = nextPos;
345
338
  const parsedMeta = decodeSchemaMeta(f0Body, depth + 1);
346
339
  activeSchema = mergeSchemaMeta(activeSchema, parsedMeta);
347
340
  continue;
348
341
  }
342
+ scanPos = skipValue(data, scanPos, wt);
343
+ }
344
+ const subPackets = new Map();
345
+ const arrayElemTypes = new Map();
346
+ let pos = 0;
347
+ while (pos < n) {
348
+ const [tag, tn] = decodeVarintBigInt(data, pos);
349
+ pos += tn;
350
+ const fieldNum = Number(tag >> 4n);
351
+ const wt = Number(tag & 0x0fn);
352
+ if (fieldNum === 0) {
353
+ const [, nextPos] = readLenSlice(data, pos, 0);
354
+ pos = nextPos;
355
+ continue;
356
+ }
349
357
  const name = activeSchema?.fields?.[fieldNum] || `field_${fieldNum}`;
350
358
  const childSchema = activeSchema?.children?.[fieldNum] ?? null;
351
- if (!Object.hasOwn(out, name))
359
+ if (!Object.hasOwn(out, name) && wt !== WireSubMsg)
352
360
  setOwn(out, name, undefined);
353
361
  switch (wt) {
354
362
  case WireUint: {
@@ -389,87 +397,28 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
389
397
  }
390
398
  case WireSubMsg: {
391
399
  const [subBytes, nextPos] = readLenSlice(data, pos, 0);
392
- const prev = own(name);
393
- const targetSub = prev && typeof prev === 'object' && !Array.isArray(prev) && !(prev instanceof Uint8Array)
394
- ? prev
395
- : undefined;
396
- out[name] = decodeBlindWithSchema(subBytes, childSchema, depth + 1, options, targetSub);
397
- pos = nextPos;
398
- break;
399
- }
400
- case WireStringArr: {
401
- const [body, nextPos] = readLenSlice(data, pos, 1);
402
- const prev = Array.isArray(out[name]) ? out[name] : [];
403
- const [arr, ok] = appendStringArrBody(prev, body);
404
- if (!ok) {
405
- throw new Error(`omnibuf: invalid WireStringArr at ${pos}`);
400
+ let chunks = subPackets.get(fieldNum);
401
+ if (!chunks) {
402
+ chunks = [];
403
+ subPackets.set(fieldNum, chunks);
406
404
  }
407
- out[name] = arr;
405
+ chunks.push(subBytes);
408
406
  pos = nextPos;
409
407
  break;
410
408
  }
411
- case WireUintArr: {
412
- const [body, nextPos] = readLenSlice(data, pos, 1);
413
- const prev = Array.isArray(out[name]) ? out[name] : [];
414
- const [arr, ok] = appendUintArrAutoBody(prev, body);
409
+ case WireArray: {
410
+ const [body, nextPos] = readLenSlice(data, pos, 2);
411
+ const [elemType, , , ok] = parseArrayHeader(body);
415
412
  if (!ok) {
416
- throw new Error(`omnibuf: invalid WireUintArr at ${pos}`);
413
+ throw new Error(`omnibuf: invalid WireArray header at ${pos}`);
417
414
  }
418
- out[name] = arr;
419
- pos = nextPos;
420
- break;
421
- }
422
- case WireSintArr: {
423
- const [body, nextPos] = readLenSlice(data, pos, 1);
424
- const prev = Array.isArray(out[name]) ? out[name] : [];
425
- const [arr, ok] = appendSintArrAutoBody(prev, body);
426
- if (!ok) {
427
- throw new Error(`omnibuf: invalid WireSintArr at ${pos}`);
428
- }
429
- out[name] = arr;
430
- pos = nextPos;
431
- break;
432
- }
433
- case WireFixedArr: {
434
- const [body, nextPos] = readLenSlice(data, pos, 1);
435
- const elemType = body[0];
436
- if (elemType === WireFixed32) {
437
- if ((body.length - 1) % 4 !== 0) {
438
- throw new Error(`omnibuf: invalid WireFixedArr(Fixed32) at ${pos}`);
439
- }
440
- const prev = Array.isArray(out[name]) ? out[name] : [];
441
- const c = (body.length - 1) / 4;
442
- for (let i = 0; i < c; i++) {
443
- const off = 1 + i * 4;
444
- const bits = (body[off] |
445
- (body[off + 1] << 8) |
446
- (body[off + 2] << 16) |
447
- (body[off + 3] << 24)) >>>
448
- 0;
449
- const f32 = float32FromBits(bits);
450
- prev.push(roundF32 ? roundFloat32Shortest(f32) : f32);
451
- }
452
- out[name] = prev;
453
- }
454
- else if (elemType === WireFixed64) {
455
- if ((body.length - 1) % 8 !== 0) {
456
- throw new Error(`omnibuf: invalid WireFixedArr(Fixed64) at ${pos}`);
457
- }
458
- const prev = Array.isArray(out[name]) ? out[name] : [];
459
- const c = (body.length - 1) / 8;
460
- for (let i = 0; i < c; i++) {
461
- const off = 1 + i * 8;
462
- let u64 = 0n;
463
- for (let b = 7; b >= 0; b--) {
464
- u64 = (u64 << 8n) | BigInt(body[off + b]);
465
- }
466
- prev.push(float64FromBits(u64));
467
- }
468
- out[name] = prev;
469
- }
470
- else {
471
- throw new Error(`omnibuf: invalid WireFixedArr ElemType ${elemType}`);
415
+ const prevElemType = arrayElemTypes.get(fieldNum);
416
+ if (prevElemType !== undefined && prevElemType !== elemType) {
417
+ throw new Error(`omnibuf: conflicting WireArray element types for field ${fieldNum}`);
472
418
  }
419
+ arrayElemTypes.set(fieldNum, elemType);
420
+ const prev = Array.isArray(out[name]) ? out[name] : [];
421
+ out[name] = decodeArrayBodyBlind(prev, body, childSchema, depth, options);
473
422
  pos = nextPos;
474
423
  break;
475
424
  }
@@ -477,54 +426,13 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
477
426
  const [body] = readLenSlice(data, pos, 1);
478
427
  throw new Error(`omnibuf: unrecognized WireExtended subtype 0x${body[0].toString(16).padStart(2, '0')}`);
479
428
  }
480
- case WireSubMsgArr: {
481
- const [arrPayload, nextPos] = readLenSlice(data, pos, 0);
482
- const [elemCount, ok] = countSubMsgArrElems(arrPayload);
483
- if (!ok) {
484
- throw new Error(`omnibuf: corrupt WireSubMsgArr element framing at ${pos}`);
485
- }
486
- const elems = Array.isArray(out[name])
487
- ? out[name]
488
- : [];
489
- let p = 0;
490
- for (let i = 0; i < elemCount; i++) {
491
- const [el, en] = decodeVarintBigInt(arrPayload, p);
492
- p += en;
493
- const elNum = Number(el);
494
- const elemMap = decodeBlindWithSchema(arrPayload.subarray(p, p + elNum), childSchema, depth + 1, options);
495
- elems.push(elemMap);
496
- p += elNum;
497
- }
498
- out[name] = elems;
499
- pos = nextPos;
500
- break;
501
- }
502
429
  case WireMap: {
503
- const [body, nextPos] = readLenSlice(data, pos, 2);
504
- const mapType = body[0];
505
- const keyWT = mapType >>> 4;
506
- const valWT = mapType & 0x0f;
507
- validateMapTypes(keyWT, valWT);
508
- const [count, cn] = decodeVarintBigInt(body, 1);
509
- if (cn === 0 || count * 2n > BigInt(body.length - (1 + cn))) {
510
- throw new Error(`omnibuf: WireMap count ${count} exceeds body (${body.length} B)`);
511
- }
430
+ const [body, nextPos] = readLenSlice(data, pos, 3);
512
431
  const prev = out[name];
513
432
  const m = prev && typeof prev === 'object' && !Array.isArray(prev) && !(prev instanceof Uint8Array)
514
433
  ? prev
515
434
  : {};
516
- let p = 1 + cn;
517
- const c = Number(count);
518
- for (let i = 0; i < c; i++) {
519
- const [k, nextP] = decodeMapKeyBlind(body, p, keyWT);
520
- const [v, nextP2] = decodeMapValBlind(body, nextP, valWT, childSchema, depth, options);
521
- p = nextP2;
522
- setOwn(m, k, v);
523
- }
524
- if (p !== body.length) {
525
- throw new Error(`omnibuf: WireMap trailing bytes (parsed ${p}, body ${body.length})`);
526
- }
527
- out[name] = m;
435
+ out[name] = decodeMapBodyBlind(m, body, childSchema, depth, options);
528
436
  pos = nextPos;
529
437
  break;
530
438
  }
@@ -532,6 +440,25 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
532
440
  if (n - pos < 4) {
533
441
  throw new Error(`omnibuf: truncated WireFixed32 at ${pos}`);
534
442
  }
443
+ const u32 = (data[pos] | (data[pos + 1] << 8) | (data[pos + 2] << 16) | (data[pos + 3] << 24)) >>>
444
+ 0;
445
+ out[name] = u32;
446
+ pos += 4;
447
+ break;
448
+ }
449
+ case WireSfixed32: {
450
+ if (n - pos < 4) {
451
+ throw new Error(`omnibuf: truncated WireSfixed32 at ${pos}`);
452
+ }
453
+ const i32 = data[pos] | (data[pos + 1] << 8) | (data[pos + 2] << 16) | (data[pos + 3] << 24) | 0;
454
+ out[name] = i32;
455
+ pos += 4;
456
+ break;
457
+ }
458
+ case WireFloat32: {
459
+ if (n - pos < 4) {
460
+ throw new Error(`omnibuf: truncated WireFloat32 at ${pos}`);
461
+ }
535
462
  const bits = (data[pos] | (data[pos + 1] << 8) | (data[pos + 2] << 16) | (data[pos + 3] << 24)) >>>
536
463
  0;
537
464
  const f32 = float32FromBits(bits);
@@ -547,6 +474,31 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
547
474
  for (let b = 7; b >= 0; b--) {
548
475
  u64 = (u64 << 8n) | BigInt(data[pos + b]);
549
476
  }
477
+ out[name] = u64 <= 9007199254740991n ? Number(u64) : u64;
478
+ pos += 8;
479
+ break;
480
+ }
481
+ case WireSfixed64: {
482
+ if (n - pos < 8) {
483
+ throw new Error(`omnibuf: truncated WireSfixed64 at ${pos}`);
484
+ }
485
+ let u64 = 0n;
486
+ for (let b = 7; b >= 0; b--) {
487
+ u64 = (u64 << 8n) | BigInt(data[pos + b]);
488
+ }
489
+ const s64 = BigInt.asIntN(64, u64);
490
+ out[name] = s64 >= -9007199254740991n && s64 <= 9007199254740991n ? Number(s64) : s64;
491
+ pos += 8;
492
+ break;
493
+ }
494
+ case WireFloat64: {
495
+ if (n - pos < 8) {
496
+ throw new Error(`omnibuf: truncated WireFloat64 at ${pos}`);
497
+ }
498
+ let u64 = 0n;
499
+ for (let b = 7; b >= 0; b--) {
500
+ u64 = (u64 << 8n) | BigInt(data[pos + b]);
501
+ }
550
502
  out[name] = float64FromBits(u64);
551
503
  pos += 8;
552
504
  break;
@@ -555,8 +507,227 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
555
507
  throw new Error(`omnibuf: unsupported wire type ${wt}`);
556
508
  }
557
509
  }
510
+ for (const [fieldNum, chunks] of subPackets.entries()) {
511
+ const name = activeSchema?.fields?.[fieldNum] || `field_${fieldNum}`;
512
+ const childSchema = activeSchema?.children?.[fieldNum] ?? null;
513
+ let combined;
514
+ if (chunks.length === 1) {
515
+ combined = chunks[0];
516
+ }
517
+ else {
518
+ let totalLen = 0;
519
+ for (const c of chunks)
520
+ totalLen += c.length;
521
+ combined = new Uint8Array(totalLen);
522
+ let off = 0;
523
+ for (const c of chunks) {
524
+ combined.set(c, off);
525
+ off += c.length;
526
+ }
527
+ }
528
+ setOwn(out, name, decodeBlindWithSchema(combined, childSchema, depth + 1, options));
529
+ }
558
530
  return out;
559
531
  }
532
+ function decodeArrayBodyBlind(prev, body, childSchema, depth, options) {
533
+ if (depth > MaxNestingDepth) {
534
+ throw new Error(`omnibuf: nesting depth ${depth} exceeds MaxNestingDepth (${MaxNestingDepth})`);
535
+ }
536
+ const [elemType, count, startP, ok] = parseArrayHeader(body);
537
+ if (!ok) {
538
+ throw new Error('omnibuf: invalid WireArray header');
539
+ }
540
+ const roundF32 = options.roundFloat32 !== false;
541
+ switch (elemType) {
542
+ case WireBoolFalse: {
543
+ const [arr, valid] = appendBoolArrBody(prev, body);
544
+ if (!valid)
545
+ throw new Error('omnibuf: invalid WireArray(bool)');
546
+ return arr;
547
+ }
548
+ case WireUint: {
549
+ const [arr, valid] = appendUintArrAutoBody(prev, body);
550
+ if (!valid)
551
+ throw new Error('omnibuf: invalid WireArray(uint)');
552
+ return arr;
553
+ }
554
+ case WireSint: {
555
+ const [arr, valid] = appendSintArrAutoBody(prev, body);
556
+ if (!valid)
557
+ throw new Error('omnibuf: invalid WireArray(sint)');
558
+ return arr;
559
+ }
560
+ case WireString: {
561
+ const [arr, valid] = appendStringArrBody(prev, body);
562
+ if (!valid)
563
+ throw new Error('omnibuf: invalid WireArray(string)');
564
+ return arr;
565
+ }
566
+ case WireBytes: {
567
+ let p = startP;
568
+ for (let i = 0; i < count; i++) {
569
+ const [b, nextP] = readLenSlice(body, p, 0);
570
+ prev.push(options.bytesAsBase64 ? uint8ArrayToBase64(b) : b.slice());
571
+ p = nextP;
572
+ }
573
+ if (p !== body.length)
574
+ throw new Error('omnibuf: WireArray(bytes) trailing bytes');
575
+ return prev;
576
+ }
577
+ case WireFixed32: {
578
+ if (body.length - startP !== count * 4) {
579
+ throw new Error('omnibuf: invalid WireArray(fixed32) length');
580
+ }
581
+ for (let i = 0; i < count; i++) {
582
+ const off = startP + i * 4;
583
+ const u32 = (body[off] | (body[off + 1] << 8) | (body[off + 2] << 16) | (body[off + 3] << 24)) >>>
584
+ 0;
585
+ prev.push(u32);
586
+ }
587
+ return prev;
588
+ }
589
+ case WireSfixed32: {
590
+ if (body.length - startP !== count * 4) {
591
+ throw new Error('omnibuf: invalid WireArray(sfixed32) length');
592
+ }
593
+ for (let i = 0; i < count; i++) {
594
+ const off = startP + i * 4;
595
+ const i32 = body[off] | (body[off + 1] << 8) | (body[off + 2] << 16) | (body[off + 3] << 24) | 0;
596
+ prev.push(i32);
597
+ }
598
+ return prev;
599
+ }
600
+ case WireFloat32: {
601
+ if (body.length - startP !== count * 4) {
602
+ throw new Error('omnibuf: invalid WireArray(float32) length');
603
+ }
604
+ for (let i = 0; i < count; i++) {
605
+ const off = startP + i * 4;
606
+ const bits = (body[off] | (body[off + 1] << 8) | (body[off + 2] << 16) | (body[off + 3] << 24)) >>>
607
+ 0;
608
+ const f32 = float32FromBits(bits);
609
+ prev.push(roundF32 ? roundFloat32Shortest(f32) : f32);
610
+ }
611
+ return prev;
612
+ }
613
+ case WireFixed64: {
614
+ if (body.length - startP !== count * 8) {
615
+ throw new Error('omnibuf: invalid WireArray(fixed64) length');
616
+ }
617
+ for (let i = 0; i < count; i++) {
618
+ const off = startP + i * 8;
619
+ let u64 = 0n;
620
+ for (let b = 7; b >= 0; b--) {
621
+ u64 = (u64 << 8n) | BigInt(body[off + b]);
622
+ }
623
+ prev.push(u64 <= 9007199254740991n ? Number(u64) : u64);
624
+ }
625
+ return prev;
626
+ }
627
+ case WireSfixed64: {
628
+ if (body.length - startP !== count * 8) {
629
+ throw new Error('omnibuf: invalid WireArray(sfixed64) length');
630
+ }
631
+ for (let i = 0; i < count; i++) {
632
+ const off = startP + i * 8;
633
+ let u64 = 0n;
634
+ for (let b = 7; b >= 0; b--) {
635
+ u64 = (u64 << 8n) | BigInt(body[off + b]);
636
+ }
637
+ const s64 = BigInt.asIntN(64, u64);
638
+ prev.push(s64 >= -9007199254740991n && s64 <= 9007199254740991n ? Number(s64) : s64);
639
+ }
640
+ return prev;
641
+ }
642
+ case WireFloat64: {
643
+ if (body.length - startP !== count * 8) {
644
+ throw new Error('omnibuf: invalid WireArray(float64) length');
645
+ }
646
+ for (let i = 0; i < count; i++) {
647
+ const off = startP + i * 8;
648
+ let u64 = 0n;
649
+ for (let b = 7; b >= 0; b--) {
650
+ u64 = (u64 << 8n) | BigInt(body[off + b]);
651
+ }
652
+ prev.push(float64FromBits(u64));
653
+ }
654
+ return prev;
655
+ }
656
+ case WireSubMsg: {
657
+ const [elemCount, valid] = countSubMsgArrElems(body);
658
+ if (!valid)
659
+ throw new Error('omnibuf: corrupt WireArray(submsg) framing');
660
+ let p = startP;
661
+ for (let i = 0; i < elemCount; i++) {
662
+ const [el, en] = decodeVarintBigInt(body, p);
663
+ p += en;
664
+ const elNum = Number(el);
665
+ const elemMap = decodeBlindWithSchema(body.subarray(p, p + elNum), childSchema, depth + 1, options);
666
+ prev.push(elemMap);
667
+ p += elNum;
668
+ }
669
+ return prev;
670
+ }
671
+ case WireArray: {
672
+ let p = startP;
673
+ for (let i = 0; i < count; i++) {
674
+ const [nestedBody, nextP] = readLenSlice(body, p, 2);
675
+ prev.push(decodeArrayBodyBlind([], nestedBody, childSchema, depth + 1, options));
676
+ p = nextP;
677
+ }
678
+ if (p !== body.length)
679
+ throw new Error('omnibuf: WireArray(array) trailing bytes');
680
+ return prev;
681
+ }
682
+ case WireMap: {
683
+ let p = startP;
684
+ for (let i = 0; i < count; i++) {
685
+ const [nestedBody, nextP] = readLenSlice(body, p, 3);
686
+ prev.push(decodeMapBodyBlind({}, nestedBody, childSchema, depth + 1, options));
687
+ p = nextP;
688
+ }
689
+ if (p !== body.length)
690
+ throw new Error('omnibuf: WireArray(map) trailing bytes');
691
+ return prev;
692
+ }
693
+ case WireExtended: {
694
+ const p = startP;
695
+ if (count > 0) {
696
+ const [extBody] = readLenSlice(body, p, 1);
697
+ throw new Error(`omnibuf: unrecognized WireExtended subtype 0x${extBody[0].toString(16).padStart(2, '0')}`);
698
+ }
699
+ if (p !== body.length)
700
+ throw new Error('omnibuf: WireArray(extended) trailing bytes');
701
+ return prev;
702
+ }
703
+ default:
704
+ throw new Error(`omnibuf: unsupported WireArray element type ${elemType}`);
705
+ }
706
+ }
707
+ function decodeMapBodyBlind(m, body, childSchema, depth, options) {
708
+ if (depth > MaxNestingDepth) {
709
+ throw new Error(`omnibuf: nesting depth ${depth} exceeds MaxNestingDepth (${MaxNestingDepth})`);
710
+ }
711
+ const keyWT = body[0];
712
+ const valWT = body[1];
713
+ validateMapTypes(keyWT, valWT);
714
+ const [count, cn] = decodeVarintBigInt(body, 2);
715
+ if (cn === 0 || count > BigInt(Math.floor((body.length - (2 + cn)) / 2))) {
716
+ throw new Error(`omnibuf: WireMap count ${count} exceeds body (${body.length} B)`);
717
+ }
718
+ let p = 2 + cn;
719
+ const c = Number(count);
720
+ for (let i = 0; i < c; i++) {
721
+ const [k, nextP] = decodeMapKeyBlind(body, p, keyWT);
722
+ const [v, nextP2] = decodeMapValBlind(body, nextP, valWT, childSchema, depth, options);
723
+ p = nextP2;
724
+ setOwn(m, k, v);
725
+ }
726
+ if (p !== body.length) {
727
+ throw new Error(`omnibuf: WireMap trailing bytes (parsed ${p}, body ${body.length})`);
728
+ }
729
+ return m;
730
+ }
560
731
  function decodeMapKeyBlind(body, p, keyWT) {
561
732
  switch (keyWT) {
562
733
  case WireString: {
@@ -584,6 +755,13 @@ function decodeMapKeyBlind(body, p, keyWT) {
584
755
  const u32 = (body[p] | (body[p + 1] << 8) | (body[p + 2] << 16) | (body[p + 3] << 24)) >>> 0;
585
756
  return [u32.toString(), p + 4];
586
757
  }
758
+ case WireSfixed32: {
759
+ if (body.length - p < 4) {
760
+ throw new Error(`omnibuf: truncated WireMap sfixed32 key at ${p}`);
761
+ }
762
+ const i32 = body[p] | (body[p + 1] << 8) | (body[p + 2] << 16) | (body[p + 3] << 24) | 0;
763
+ return [i32.toString(), p + 4];
764
+ }
587
765
  case WireFixed64: {
588
766
  if (body.length - p < 8) {
589
767
  throw new Error(`omnibuf: truncated WireMap fixed64 key at ${p}`);
@@ -594,7 +772,17 @@ function decodeMapKeyBlind(body, p, keyWT) {
594
772
  }
595
773
  return [u64.toString(), p + 8];
596
774
  }
597
- case WireBoolTrue:
775
+ case WireSfixed64: {
776
+ if (body.length - p < 8) {
777
+ throw new Error(`omnibuf: truncated WireMap sfixed64 key at ${p}`);
778
+ }
779
+ let u64 = 0n;
780
+ for (let b = 7; b >= 0; b--) {
781
+ u64 = (u64 << 8n) | BigInt(body[p + b]);
782
+ }
783
+ const s64 = BigInt.asIntN(64, u64);
784
+ return [s64.toString(), p + 8];
785
+ }
598
786
  case WireBoolFalse: {
599
787
  if (body.length - p < 1) {
600
788
  throw new Error(`omnibuf: truncated WireMap bool key at ${p}`);
@@ -642,6 +830,20 @@ function decodeMapValBlind(body, p, valWT, childSchema, depth, options) {
642
830
  if (body.length - p < 4) {
643
831
  throw new Error(`omnibuf: truncated WireMap fixed32 value at ${p}`);
644
832
  }
833
+ const u32 = (body[p] | (body[p + 1] << 8) | (body[p + 2] << 16) | (body[p + 3] << 24)) >>> 0;
834
+ return [u32, p + 4];
835
+ }
836
+ case WireSfixed32: {
837
+ if (body.length - p < 4) {
838
+ throw new Error(`omnibuf: truncated WireMap sfixed32 value at ${p}`);
839
+ }
840
+ const i32 = body[p] | (body[p + 1] << 8) | (body[p + 2] << 16) | (body[p + 3] << 24) | 0;
841
+ return [i32, p + 4];
842
+ }
843
+ case WireFloat32: {
844
+ if (body.length - p < 4) {
845
+ throw new Error(`omnibuf: truncated WireMap float32 value at ${p}`);
846
+ }
645
847
  const bits = (body[p] | (body[p + 1] << 8) | (body[p + 2] << 16) | (body[p + 3] << 24)) >>> 0;
646
848
  const f32 = float32FromBits(bits);
647
849
  return [options.roundFloat32 !== false ? roundFloat32Shortest(f32) : f32, p + 4];
@@ -654,9 +856,29 @@ function decodeMapValBlind(body, p, valWT, childSchema, depth, options) {
654
856
  for (let b = 7; b >= 0; b--) {
655
857
  u64 = (u64 << 8n) | BigInt(body[p + b]);
656
858
  }
859
+ return [u64 <= 9007199254740991n ? Number(u64) : u64, p + 8];
860
+ }
861
+ case WireSfixed64: {
862
+ if (body.length - p < 8) {
863
+ throw new Error(`omnibuf: truncated WireMap sfixed64 value at ${p}`);
864
+ }
865
+ let u64 = 0n;
866
+ for (let b = 7; b >= 0; b--) {
867
+ u64 = (u64 << 8n) | BigInt(body[p + b]);
868
+ }
869
+ const s64 = BigInt.asIntN(64, u64);
870
+ return [s64 >= -9007199254740991n && s64 <= 9007199254740991n ? Number(s64) : s64, p + 8];
871
+ }
872
+ case WireFloat64: {
873
+ if (body.length - p < 8) {
874
+ throw new Error(`omnibuf: truncated WireMap float64 value at ${p}`);
875
+ }
876
+ let u64 = 0n;
877
+ for (let b = 7; b >= 0; b--) {
878
+ u64 = (u64 << 8n) | BigInt(body[p + b]);
879
+ }
657
880
  return [float64FromBits(u64), p + 8];
658
881
  }
659
- case WireBoolTrue:
660
882
  case WireBoolFalse: {
661
883
  if (body.length - p < 1) {
662
884
  throw new Error(`omnibuf: truncated WireMap bool value at ${p}`);
@@ -667,6 +889,20 @@ function decodeMapValBlind(body, p, valWT, childSchema, depth, options) {
667
889
  }
668
890
  return [b === 1, p + 1];
669
891
  }
892
+ case WireArray: {
893
+ const [arrBody, nextP] = readLenSlice(body, p, 2);
894
+ const arr = decodeArrayBodyBlind([], arrBody, childSchema, depth + 1, options);
895
+ return [arr, nextP];
896
+ }
897
+ case WireMap: {
898
+ const [mapBody, nextP] = readLenSlice(body, p, 3);
899
+ const mapObj = decodeMapBodyBlind({}, mapBody, childSchema, depth + 1, options);
900
+ return [mapObj, nextP];
901
+ }
902
+ case WireExtended: {
903
+ const [extBody] = readLenSlice(body, p, 1);
904
+ throw new Error(`omnibuf: unrecognized WireExtended subtype 0x${extBody[0].toString(16).padStart(2, '0')}`);
905
+ }
670
906
  default:
671
907
  throw new Error(`omnibuf: invalid WireMap value wire type ${valWT}`);
672
908
  }