@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.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
  */
@@ -42,6 +42,9 @@ export function base64ToUint8Array(b64) {
42
42
  const clean = b64.replace(/[\r\n\s]/g, '');
43
43
  if (clean.length === 0)
44
44
  return new Uint8Array(0);
45
+ if (clean.length % 4 !== 0) {
46
+ throw new Error('omnibuf: invalid base64 string length');
47
+ }
45
48
  const pad = clean.endsWith('==') ? 2 : clean.endsWith('=') ? 1 : 0;
46
49
  const byteLen = (clean.length * 3) / 4 - pad;
47
50
  const out = new Uint8Array(byteLen);
@@ -49,8 +52,13 @@ export function base64ToUint8Array(b64) {
49
52
  for (let i = 0; i < clean.length; i += 4) {
50
53
  const c0 = BASE64_CHARS.indexOf(clean[i]);
51
54
  const c1 = BASE64_CHARS.indexOf(clean[i + 1]);
52
- const c2 = clean[i + 2] === '=' ? 0 : BASE64_CHARS.indexOf(clean[i + 2]);
53
- const c3 = clean[i + 3] === '=' ? 0 : BASE64_CHARS.indexOf(clean[i + 3]);
55
+ const isPad2 = clean[i + 2] === '=' && i + 4 === clean.length;
56
+ const isPad3 = clean[i + 3] === '=' && i + 4 === clean.length;
57
+ const c2 = isPad2 ? 0 : BASE64_CHARS.indexOf(clean[i + 2]);
58
+ const c3 = isPad3 ? 0 : BASE64_CHARS.indexOf(clean[i + 3]);
59
+ if (c0 < 0 || c1 < 0 || c2 < 0 || c3 < 0 || (isPad2 && !isPad3)) {
60
+ throw new Error(`omnibuf: invalid base64 character at index ${i}`);
61
+ }
54
62
  const n = (c0 << 18) | (c1 << 12) | (c2 << 6) | c3;
55
63
  if (outIdx < byteLen)
56
64
  out[outIdx++] = (n >>> 16) & 0xff;
@@ -64,21 +72,22 @@ export function base64ToUint8Array(b64) {
64
72
  function writeSchemaMetaBody(enc, meta, deterministic) {
65
73
  if (!meta)
66
74
  return;
67
- // 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)
68
76
  if (meta.typeName && meta.typeName.length > 0) {
69
- enc.writeByte(0x27);
77
+ enc.writeByte(0x35);
70
78
  enc.writeRawStringPayload(meta.typeName);
71
79
  }
72
- // 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)
73
81
  if (meta.fields) {
74
82
  const keys = Object.keys(meta.fields).map(Number);
75
83
  if (keys.length > 0) {
76
84
  if (deterministic) {
77
85
  keys.sort((a, b) => a - b);
78
86
  }
79
- enc.writeByte(0x53);
87
+ enc.writeByte(0x5d);
80
88
  enc.beginContainer();
81
- enc.writeByte(mapHeaderByte(WireUint, WireString));
89
+ enc.writeByte(WireUint);
90
+ enc.writeByte(WireString);
82
91
  enc.writeRawVarint(keys.length);
83
92
  for (const k of keys) {
84
93
  const name = meta.fields[k] ?? '';
@@ -88,16 +97,17 @@ function writeSchemaMetaBody(enc, meta, deterministic) {
88
97
  enc.endContainer();
89
98
  }
90
99
  }
91
- // 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)
92
101
  if (meta.children) {
93
102
  const keys = Object.keys(meta.children).map(Number);
94
103
  if (keys.length > 0) {
95
104
  if (deterministic) {
96
105
  keys.sort((a, b) => a - b);
97
106
  }
98
- enc.writeByte(0x73);
107
+ enc.writeByte(0x7d);
99
108
  enc.beginContainer();
100
- enc.writeByte(mapHeaderByte(WireUint, WireSubMsg));
109
+ enc.writeByte(WireUint);
110
+ enc.writeByte(WireSubMsg);
101
111
  enc.writeRawVarint(keys.length);
102
112
  for (const k of keys) {
103
113
  const child = meta.children[k];
@@ -111,18 +121,18 @@ function writeSchemaMetaBody(enc, meta, deterministic) {
111
121
  }
112
122
  }
113
123
  /**
114
- * Writes Field #0 (`Tag = 0x0B`, `WireSubMsg = 5`) containing `meta` into `enc`.
124
+ * Writes Field #0 (`Tag = 0x19`, `WireSubMsg = 12`) containing `meta` into `enc`.
115
125
  */
116
126
  export function writeField0SchemaMeta(enc, meta, deterministic = false) {
117
127
  if (!meta)
118
128
  return;
119
- 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
120
130
  enc.beginContainer();
121
131
  writeSchemaMetaBody(enc, meta, deterministic);
122
132
  enc.endContainer();
123
133
  }
124
134
  /**
125
- * 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.
126
136
  */
127
137
  export function encodeField0SchemaMeta(meta, deterministic = true) {
128
138
  if (!meta)
@@ -182,8 +192,8 @@ export function decodeSchemaMeta(data, depth = 0, limit = MaxNestingDepth) {
182
192
  const n = data.length;
183
193
  while (pos < n) {
184
194
  const [tag, tn] = decodeVarintBigInt(data, pos);
185
- if (tn === 0) {
186
- 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}`);
187
197
  }
188
198
  pos += tn;
189
199
  const fnum = Number(tag >> 4n);
@@ -202,22 +212,18 @@ export function decodeSchemaMeta(data, depth = 0, limit = MaxNestingDepth) {
202
212
  if (wt !== WireMap) {
203
213
  throw new Error(`omnibuf: SchemaMeta field #2 (fields) must be WireMap, got ${wt}`);
204
214
  }
205
- const [body, nextPos] = readLenSlice(data, pos, 2);
215
+ const [body, nextPos] = readLenSlice(data, pos, 3);
206
216
  pos = nextPos;
207
- if (body[0] !== mapHeaderByte(WireUint, WireString)) {
208
- throw new Error(`omnibuf: SchemaMeta fields map must have MapType 0x03, got 0x${body[0].toString(16)}`);
209
- }
210
- const [count, cn] = decodeVarintBigInt(body, 1);
211
- if (cn === 0 || count * 2n > BigInt(body.length - (1 + cn))) {
212
- 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');
213
220
  }
214
221
  if (!meta.fields)
215
222
  meta.fields = {};
216
- let p = 1 + cn;
217
- const c = Number(count);
223
+ let p = startP;
218
224
  for (let i = 0; i < c; i++) {
219
225
  const [k64, kn] = decodeVarintBigInt(body, p);
220
- if (kn === 0 || k64 === 0n || k64 > 0xffffffffn) {
226
+ if (kn === 0 || k64 === 0n || k64 > 536870911n) {
221
227
  throw new Error(`omnibuf: invalid SchemaMeta field number ${k64} at ${p}`);
222
228
  }
223
229
  p += kn;
@@ -234,22 +240,18 @@ export function decodeSchemaMeta(data, depth = 0, limit = MaxNestingDepth) {
234
240
  if (wt !== WireMap) {
235
241
  throw new Error(`omnibuf: SchemaMeta field #3 (children) must be WireMap, got ${wt}`);
236
242
  }
237
- const [body, nextPos] = readLenSlice(data, pos, 2);
243
+ const [body, nextPos] = readLenSlice(data, pos, 3);
238
244
  pos = nextPos;
239
- if (body[0] !== mapHeaderByte(WireUint, WireSubMsg)) {
240
- throw new Error(`omnibuf: SchemaMeta children map must have MapType 0x05, got 0x${body[0].toString(16)}`);
241
- }
242
- const [count, cn] = decodeVarintBigInt(body, 1);
243
- if (cn === 0 || count * 2n > BigInt(body.length - (1 + cn))) {
244
- 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');
245
248
  }
246
249
  if (!meta.children)
247
250
  meta.children = {};
248
- let p = 1 + cn;
249
- const c = Number(count);
251
+ let p = startP;
250
252
  for (let i = 0; i < c; i++) {
251
253
  const [k64, kn] = decodeVarintBigInt(body, p);
252
- if (kn === 0 || k64 === 0n || k64 > 0xffffffffn) {
254
+ if (kn === 0 || k64 === 0n || k64 > 536870911n) {
253
255
  throw new Error(`omnibuf: invalid SchemaMeta child field number ${k64} at ${p}`);
254
256
  }
255
257
  p += kn;
@@ -315,32 +317,46 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
315
317
  return target ?? {};
316
318
  }
317
319
  let activeSchema = inherited ? mergeSchemaMeta(null, inherited) : null;
318
- let pos = 0;
319
320
  const out = target ?? {};
320
- // Wire names and map keys are arbitrary strings, including __proto__.
321
- const own = (key) => (Object.hasOwn(out, key) ? out[key] : undefined);
322
321
  const roundF32 = options.roundFloat32 !== false;
323
- while (pos < n) {
324
- const [tag, tn] = decodeVarintBigInt(data, pos);
325
- if (tn === 0 || tag >> 4n > 0xffffffffn) {
326
- throw new Error(`omnibuf: truncated tag at ${pos}`);
327
- }
328
- 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;
329
330
  const fieldNum = Number(tag >> 4n);
330
331
  const wt = Number(tag & 0x0fn);
331
332
  if (fieldNum === 0) {
332
333
  if (wt !== WireSubMsg) {
333
- 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}`);
334
335
  }
335
- const [f0Body, nextPos] = readLenSlice(data, pos, 0);
336
- pos = nextPos;
336
+ const [f0Body, nextPos] = readLenSlice(data, scanPos, 0);
337
+ scanPos = nextPos;
337
338
  const parsedMeta = decodeSchemaMeta(f0Body, depth + 1);
338
339
  activeSchema = mergeSchemaMeta(activeSchema, parsedMeta);
339
340
  continue;
340
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
+ }
341
357
  const name = activeSchema?.fields?.[fieldNum] || `field_${fieldNum}`;
342
358
  const childSchema = activeSchema?.children?.[fieldNum] ?? null;
343
- if (!Object.hasOwn(out, name))
359
+ if (!Object.hasOwn(out, name) && wt !== WireSubMsg)
344
360
  setOwn(out, name, undefined);
345
361
  switch (wt) {
346
362
  case WireUint: {
@@ -381,87 +397,28 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
381
397
  }
382
398
  case WireSubMsg: {
383
399
  const [subBytes, nextPos] = readLenSlice(data, pos, 0);
384
- const prev = own(name);
385
- const targetSub = prev && typeof prev === 'object' && !Array.isArray(prev) && !(prev instanceof Uint8Array)
386
- ? prev
387
- : undefined;
388
- out[name] = decodeBlindWithSchema(subBytes, childSchema, depth + 1, options, targetSub);
389
- pos = nextPos;
390
- break;
391
- }
392
- case WireStringArr: {
393
- const [body, nextPos] = readLenSlice(data, pos, 1);
394
- const prev = Array.isArray(out[name]) ? out[name] : [];
395
- const [arr, ok] = appendStringArrBody(prev, body);
396
- if (!ok) {
397
- throw new Error(`omnibuf: invalid WireStringArr at ${pos}`);
398
- }
399
- out[name] = arr;
400
- pos = nextPos;
401
- break;
402
- }
403
- case WireUintArr: {
404
- const [body, nextPos] = readLenSlice(data, pos, 1);
405
- const prev = Array.isArray(out[name]) ? out[name] : [];
406
- const [arr, ok] = appendUintArrAutoBody(prev, body);
407
- if (!ok) {
408
- throw new Error(`omnibuf: invalid WireUintArr at ${pos}`);
400
+ let chunks = subPackets.get(fieldNum);
401
+ if (!chunks) {
402
+ chunks = [];
403
+ subPackets.set(fieldNum, chunks);
409
404
  }
410
- out[name] = arr;
405
+ chunks.push(subBytes);
411
406
  pos = nextPos;
412
407
  break;
413
408
  }
414
- case WireSintArr: {
415
- const [body, nextPos] = readLenSlice(data, pos, 1);
416
- const prev = Array.isArray(out[name]) ? out[name] : [];
417
- const [arr, ok] = appendSintArrAutoBody(prev, body);
409
+ case WireArray: {
410
+ const [body, nextPos] = readLenSlice(data, pos, 2);
411
+ const [elemType, , , ok] = parseArrayHeader(body);
418
412
  if (!ok) {
419
- throw new Error(`omnibuf: invalid WireSintArr at ${pos}`);
413
+ throw new Error(`omnibuf: invalid WireArray header at ${pos}`);
420
414
  }
421
- out[name] = arr;
422
- pos = nextPos;
423
- break;
424
- }
425
- case WireFixedArr: {
426
- const [body, nextPos] = readLenSlice(data, pos, 1);
427
- const elemType = body[0];
428
- if (elemType === WireFixed32) {
429
- if ((body.length - 1) % 4 !== 0) {
430
- throw new Error(`omnibuf: invalid WireFixedArr(Fixed32) at ${pos}`);
431
- }
432
- const prev = Array.isArray(out[name]) ? out[name] : [];
433
- const c = (body.length - 1) / 4;
434
- for (let i = 0; i < c; i++) {
435
- const off = 1 + i * 4;
436
- const bits = (body[off] |
437
- (body[off + 1] << 8) |
438
- (body[off + 2] << 16) |
439
- (body[off + 3] << 24)) >>>
440
- 0;
441
- const f32 = float32FromBits(bits);
442
- prev.push(roundF32 ? roundFloat32Shortest(f32) : f32);
443
- }
444
- out[name] = prev;
445
- }
446
- else if (elemType === WireFixed64) {
447
- if ((body.length - 1) % 8 !== 0) {
448
- throw new Error(`omnibuf: invalid WireFixedArr(Fixed64) at ${pos}`);
449
- }
450
- const prev = Array.isArray(out[name]) ? out[name] : [];
451
- const c = (body.length - 1) / 8;
452
- for (let i = 0; i < c; i++) {
453
- const off = 1 + i * 8;
454
- let u64 = 0n;
455
- for (let b = 7; b >= 0; b--) {
456
- u64 = (u64 << 8n) | BigInt(body[off + b]);
457
- }
458
- prev.push(float64FromBits(u64));
459
- }
460
- out[name] = prev;
461
- }
462
- else {
463
- 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}`);
464
418
  }
419
+ arrayElemTypes.set(fieldNum, elemType);
420
+ const prev = Array.isArray(out[name]) ? out[name] : [];
421
+ out[name] = decodeArrayBodyBlind(prev, body, childSchema, depth, options);
465
422
  pos = nextPos;
466
423
  break;
467
424
  }
@@ -469,54 +426,13 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
469
426
  const [body] = readLenSlice(data, pos, 1);
470
427
  throw new Error(`omnibuf: unrecognized WireExtended subtype 0x${body[0].toString(16).padStart(2, '0')}`);
471
428
  }
472
- case WireSubMsgArr: {
473
- const [arrPayload, nextPos] = readLenSlice(data, pos, 0);
474
- const [elemCount, ok] = countSubMsgArrElems(arrPayload);
475
- if (!ok) {
476
- throw new Error(`omnibuf: corrupt WireSubMsgArr element framing at ${pos}`);
477
- }
478
- const elems = Array.isArray(out[name])
479
- ? out[name]
480
- : [];
481
- let p = 0;
482
- for (let i = 0; i < elemCount; i++) {
483
- const [el, en] = decodeVarintBigInt(arrPayload, p);
484
- p += en;
485
- const elNum = Number(el);
486
- const elemMap = decodeBlindWithSchema(arrPayload.subarray(p, p + elNum), childSchema, depth + 1, options);
487
- elems.push(elemMap);
488
- p += elNum;
489
- }
490
- out[name] = elems;
491
- pos = nextPos;
492
- break;
493
- }
494
429
  case WireMap: {
495
- const [body, nextPos] = readLenSlice(data, pos, 2);
496
- const mapType = body[0];
497
- const keyWT = mapType >>> 4;
498
- const valWT = mapType & 0x0f;
499
- validateMapTypes(keyWT, valWT);
500
- const [count, cn] = decodeVarintBigInt(body, 1);
501
- if (cn === 0 || count * 2n > BigInt(body.length - (1 + cn))) {
502
- throw new Error(`omnibuf: WireMap count ${count} exceeds body (${body.length} B)`);
503
- }
430
+ const [body, nextPos] = readLenSlice(data, pos, 3);
504
431
  const prev = out[name];
505
432
  const m = prev && typeof prev === 'object' && !Array.isArray(prev) && !(prev instanceof Uint8Array)
506
433
  ? prev
507
434
  : {};
508
- let p = 1 + cn;
509
- const c = Number(count);
510
- for (let i = 0; i < c; i++) {
511
- const [k, nextP] = decodeMapKeyBlind(body, p, keyWT);
512
- const [v, nextP2] = decodeMapValBlind(body, nextP, valWT, childSchema, depth, options);
513
- p = nextP2;
514
- setOwn(m, k, v);
515
- }
516
- if (p !== body.length) {
517
- throw new Error(`omnibuf: WireMap trailing bytes (parsed ${p}, body ${body.length})`);
518
- }
519
- out[name] = m;
435
+ out[name] = decodeMapBodyBlind(m, body, childSchema, depth, options);
520
436
  pos = nextPos;
521
437
  break;
522
438
  }
@@ -524,6 +440,25 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
524
440
  if (n - pos < 4) {
525
441
  throw new Error(`omnibuf: truncated WireFixed32 at ${pos}`);
526
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
+ }
527
462
  const bits = (data[pos] | (data[pos + 1] << 8) | (data[pos + 2] << 16) | (data[pos + 3] << 24)) >>>
528
463
  0;
529
464
  const f32 = float32FromBits(bits);
@@ -539,6 +474,31 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
539
474
  for (let b = 7; b >= 0; b--) {
540
475
  u64 = (u64 << 8n) | BigInt(data[pos + b]);
541
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
+ }
542
502
  out[name] = float64FromBits(u64);
543
503
  pos += 8;
544
504
  break;
@@ -547,8 +507,227 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
547
507
  throw new Error(`omnibuf: unsupported wire type ${wt}`);
548
508
  }
549
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
+ }
550
530
  return out;
551
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
+ }
552
731
  function decodeMapKeyBlind(body, p, keyWT) {
553
732
  switch (keyWT) {
554
733
  case WireString: {
@@ -576,6 +755,13 @@ function decodeMapKeyBlind(body, p, keyWT) {
576
755
  const u32 = (body[p] | (body[p + 1] << 8) | (body[p + 2] << 16) | (body[p + 3] << 24)) >>> 0;
577
756
  return [u32.toString(), p + 4];
578
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
+ }
579
765
  case WireFixed64: {
580
766
  if (body.length - p < 8) {
581
767
  throw new Error(`omnibuf: truncated WireMap fixed64 key at ${p}`);
@@ -586,7 +772,17 @@ function decodeMapKeyBlind(body, p, keyWT) {
586
772
  }
587
773
  return [u64.toString(), p + 8];
588
774
  }
589
- 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
+ }
590
786
  case WireBoolFalse: {
591
787
  if (body.length - p < 1) {
592
788
  throw new Error(`omnibuf: truncated WireMap bool key at ${p}`);
@@ -634,6 +830,20 @@ function decodeMapValBlind(body, p, valWT, childSchema, depth, options) {
634
830
  if (body.length - p < 4) {
635
831
  throw new Error(`omnibuf: truncated WireMap fixed32 value at ${p}`);
636
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
+ }
637
847
  const bits = (body[p] | (body[p + 1] << 8) | (body[p + 2] << 16) | (body[p + 3] << 24)) >>> 0;
638
848
  const f32 = float32FromBits(bits);
639
849
  return [options.roundFloat32 !== false ? roundFloat32Shortest(f32) : f32, p + 4];
@@ -646,9 +856,29 @@ function decodeMapValBlind(body, p, valWT, childSchema, depth, options) {
646
856
  for (let b = 7; b >= 0; b--) {
647
857
  u64 = (u64 << 8n) | BigInt(body[p + b]);
648
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
+ }
649
880
  return [float64FromBits(u64), p + 8];
650
881
  }
651
- case WireBoolTrue:
652
882
  case WireBoolFalse: {
653
883
  if (body.length - p < 1) {
654
884
  throw new Error(`omnibuf: truncated WireMap bool value at ${p}`);
@@ -659,6 +889,20 @@ function decodeMapValBlind(body, p, valWT, childSchema, depth, options) {
659
889
  }
660
890
  return [b === 1, p + 1];
661
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
+ }
662
906
  default:
663
907
  throw new Error(`omnibuf: invalid WireMap value wire type ${valWT}`);
664
908
  }