@omnibuf/core 0.1.1 → 0.1.3

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
@@ -2,10 +2,10 @@ import { validateValue, validateMapTypes } from './validation.js';
2
2
  // ============================================================================
3
3
  // Tier 3: Annotated Classes & Declarative Type Mapping for Plain Objects/Classes
4
4
  // ============================================================================
5
- import { base64ToUint8Array, decodeSchemaMeta, encodeField0SchemaMeta, mergeSchemaMeta, writeField0SchemaMeta, } from './blind.js';
5
+ import { base64ToUint8Array, encodeField0SchemaMeta, mergeSchemaMeta, writeField0SchemaMeta, } from './blind.js';
6
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, WireArray, WireBoolFalse, WireBoolTrue, WireBytes, WireFixed32, WireFixed64, WireFloat32, WireFloat64, WireMap, WireSfixed32, WireSfixed64, WireSint, WireString, WireSubMsg, WireUint, decodeUTF8, decodeVarint, decodeVarintAuto, decodeVarintBigInt, decodeZigZag32, decodeZigZagAuto, decodeZigZagBigInt, encodeZigZag, encodeZigZagNumber, float32FromBits, float64FromBits, parseArrayHeader, readFloat64At, readLenSlice, roundFloat32Shortest, } from './wire.js';
8
+ import { MaxNestingDepth, WireArray, WireBoolFalse, WireBoolTrue, WireBytes, WireFixed32, WireFixed64, WireFloat32, WireFloat64, WireMap, WireSfixed32, WireSfixed64, WireSint, WireString, WireSubMsg, WireUint, compareUTF8Strings, decodeUTF8, decodeVarint, decodeVarintAuto, decodeVarintBigInt, decodeZigZag32, decodeZigZagAuto, decodeZigZagBigInt, encodeZigZag, encodeZigZagNumber, float32FromBits, float64FromBits, parseArrayHeader, readFloat64At, readLenSlice, toBigInt64Input, validateField0At, } from './wire.js';
9
9
  /**
10
10
  * Converts `snake_case` field names to `protoCamelCase` (matching `protoc` and `@bufbuild/protobuf` v2).
11
11
  */
@@ -243,10 +243,13 @@ export class MessageSchema {
243
243
  shortName;
244
244
  fields;
245
245
  oneofs;
246
+ nonOneofFields;
247
+ denseByNum;
246
248
  byNum = new Map();
247
249
  byKey = new Map();
248
250
  oneofByName = new Map();
249
251
  factory;
252
+ precomputedDescMap;
250
253
  generatedHooks;
251
254
  typeNameDesc;
252
255
  cachedMeta = null;
@@ -343,7 +346,107 @@ export class MessageSchema {
343
346
  // Sort fields by ascending fieldNum for deterministic output
344
347
  compiled.sort((a, b) => a.fieldNum - b.fieldNum);
345
348
  this.fields = compiled;
349
+ this.nonOneofFields = compiled.filter((f) => !f.oneof);
346
350
  this.oneofs = Array.from(oneofMap.values());
351
+ const dense = new Array(65);
352
+ for (const cf of compiled) {
353
+ if (cf.fieldNum <= 64) {
354
+ dense[cf.fieldNum] = cf;
355
+ }
356
+ }
357
+ this.denseByNum = dense;
358
+ const canUseProtoFastPath = !factory &&
359
+ !compiled.some((f) => f.isProtoKey || f.inObjectProto || f.altKey === '__proto__') &&
360
+ !this.oneofs.some((o) => o.localName === '__proto__' ||
361
+ o.protoName === '__proto__' ||
362
+ o.localName in Object.prototype);
363
+ if (canUseProtoFastPath) {
364
+ this.precomputedDescMap = this.buildDescriptorMap();
365
+ }
366
+ else {
367
+ this.precomputedDescMap = null;
368
+ }
369
+ }
370
+ buildDescriptorMap() {
371
+ const descMap = {
372
+ $typeName: this.typeNameDesc,
373
+ };
374
+ for (const oneof of this.oneofs) {
375
+ if (oneof.protoName !== oneof.localName) {
376
+ const ln = oneof.localName;
377
+ descMap[oneof.protoName] = {
378
+ get() {
379
+ return this[ln];
380
+ },
381
+ set(v) {
382
+ this[ln] = v;
383
+ },
384
+ enumerable: false,
385
+ configurable: true,
386
+ };
387
+ }
388
+ }
389
+ for (const f of this.fields) {
390
+ if (f.oneof) {
391
+ const oneofKey = f.oneof;
392
+ const caseName = f.localName;
393
+ const altCaseName = f.name;
394
+ const propKey = f.propKey;
395
+ descMap[propKey] = {
396
+ get() {
397
+ const g = this[oneofKey];
398
+ if (g &&
399
+ typeof g === 'object' &&
400
+ (g.case === caseName || g.case === altCaseName || g.case === propKey)) {
401
+ return g.value;
402
+ }
403
+ return undefined;
404
+ },
405
+ set(v) {
406
+ if (v === undefined || v === null) {
407
+ const g = this[oneofKey];
408
+ if (g &&
409
+ typeof g === 'object' &&
410
+ (g.case === caseName || g.case === altCaseName || g.case === propKey)) {
411
+ this[oneofKey] = { case: undefined };
412
+ }
413
+ }
414
+ else {
415
+ this[oneofKey] = { case: caseName, value: v };
416
+ }
417
+ },
418
+ enumerable: false,
419
+ configurable: true,
420
+ };
421
+ if (f.altKey) {
422
+ descMap[f.altKey] = {
423
+ get() {
424
+ return this[propKey];
425
+ },
426
+ set(v) {
427
+ this[propKey] = v;
428
+ },
429
+ enumerable: false,
430
+ configurable: true,
431
+ };
432
+ }
433
+ continue;
434
+ }
435
+ if (f.altKey) {
436
+ const pk = f.propKey;
437
+ descMap[f.altKey] = {
438
+ get() {
439
+ return this[pk];
440
+ },
441
+ set(v) {
442
+ this[pk] = v;
443
+ },
444
+ enumerable: false,
445
+ configurable: true,
446
+ };
447
+ }
448
+ }
449
+ return descMap;
347
450
  }
348
451
  /**
349
452
  * Look up a compiled field by number, `localName` (`camelCase`), `.proto` `name` (`snake_case`),
@@ -495,6 +598,26 @@ export class MessageSchema {
495
598
  return init;
496
599
  }
497
600
  const hasGenCreate = Boolean(this.generatedHooks?.create);
601
+ if (!hasGenCreate && this.precomputedDescMap !== null) {
602
+ const obj = {};
603
+ let nextCreating;
604
+ if (this.isCyclicOrDeep()) {
605
+ nextCreating = new Set(creatingAncestors);
606
+ nextCreating.add(this);
607
+ }
608
+ for (let i = 0; i < this.oneofs.length; i++) {
609
+ obj[this.oneofs[i].localName] = { case: undefined };
610
+ }
611
+ for (let i = 0; i < this.nonOneofFields.length; i++) {
612
+ const f = this.nonOneofFields[i];
613
+ obj[f.propKey] = f.explicitPresence ? undefined : defaultFieldValue(f, nextCreating);
614
+ }
615
+ Object.defineProperties(obj, this.precomputedDescMap);
616
+ if (!init) {
617
+ return obj;
618
+ }
619
+ return this.applyInitToInstance(obj, init);
620
+ }
498
621
  const obj = hasGenCreate
499
622
  ? this.generatedHooks.create()
500
623
  : this.factory
@@ -524,69 +647,71 @@ export class MessageSchema {
524
647
  }
525
648
  }
526
649
  if (init) {
527
- for (const oneof of this.oneofs) {
528
- const localVal = oneof.localName in Object.prototype
529
- ? getOwnProp(init, oneof.localName)
530
- : init[oneof.localName];
531
- const protoVal = oneof.protoName !== oneof.localName
532
- ? oneof.protoName in Object.prototype
533
- ? getOwnProp(init, oneof.protoName)
534
- : init[oneof.protoName]
535
- : undefined;
536
- const initOneof = localVal ?? protoVal;
537
- if (initOneof && typeof initOneof === 'object' && 'case' in initOneof) {
538
- if (initOneof.case === undefined) {
539
- obj[oneof.localName] = { case: undefined };
540
- }
541
- else {
542
- const matchingField = oneof.fields.find((f) => f.localName === initOneof.case ||
543
- f.name === initOneof.case ||
544
- f.propKey === initOneof.case);
545
- if (matchingField) {
546
- obj[oneof.localName] = {
547
- case: matchingField.localName,
548
- value: initFieldValue(matchingField, initOneof.value),
549
- };
550
- }
551
- }
650
+ return this.applyInitToInstance(obj, init);
651
+ }
652
+ return obj;
653
+ }
654
+ applyInitToInstance(obj, init) {
655
+ for (const oneof of this.oneofs) {
656
+ const localVal = oneof.localName in Object.prototype
657
+ ? getOwnProp(init, oneof.localName)
658
+ : init[oneof.localName];
659
+ const protoVal = oneof.protoName !== oneof.localName
660
+ ? oneof.protoName in Object.prototype
661
+ ? getOwnProp(init, oneof.protoName)
662
+ : init[oneof.protoName]
663
+ : undefined;
664
+ const initOneof = localVal ?? protoVal;
665
+ if (initOneof && typeof initOneof === 'object' && 'case' in initOneof) {
666
+ if (initOneof.case === undefined) {
667
+ obj[oneof.localName] = { case: undefined };
552
668
  }
553
- }
554
- for (const f of this.fields) {
555
- const pVal = f.inObjectProto
556
- ? getOwnProp(init, f.propKey)
557
- : init[f.propKey];
558
- const initVal = pVal !== undefined
559
- ? pVal
560
- : f.altKey !== undefined
561
- ? f.altInObjectProto
562
- ? getOwnProp(init, f.altKey)
563
- : init[f.altKey]
564
- : undefined;
565
- if (initVal !== undefined) {
566
- if (f.oneof) {
567
- obj[f.oneof] = {
568
- case: f.localName,
569
- value: initFieldValue(f, initVal),
669
+ else {
670
+ const matchingField = oneof.fields.find((f) => f.localName === initOneof.case ||
671
+ f.name === initOneof.case ||
672
+ f.propKey === initOneof.case);
673
+ if (matchingField) {
674
+ obj[oneof.localName] = {
675
+ case: matchingField.localName,
676
+ value: initFieldValue(matchingField, initOneof.value),
570
677
  };
571
678
  }
572
- else {
573
- setFieldOnObj(obj, f, initFieldValue(f, initVal));
574
- }
575
679
  }
576
680
  }
577
- if (Array.isArray(init.$unknown) && init.$unknown.length > 0) {
578
- Object.defineProperty(obj, '$unknown', {
579
- value: init.$unknown.map((u) => ({
580
- no: u.no,
581
- wireType: u.wireType,
582
- data: u.data.slice(),
583
- })),
584
- enumerable: false,
585
- writable: true,
586
- configurable: true,
587
- });
681
+ }
682
+ for (const f of this.fields) {
683
+ const pVal = f.inObjectProto ? getOwnProp(init, f.propKey) : init[f.propKey];
684
+ const initVal = pVal !== undefined
685
+ ? pVal
686
+ : f.altKey !== undefined
687
+ ? f.altInObjectProto
688
+ ? getOwnProp(init, f.altKey)
689
+ : init[f.altKey]
690
+ : undefined;
691
+ if (initVal !== undefined) {
692
+ if (f.oneof) {
693
+ obj[f.oneof] = {
694
+ case: f.localName,
695
+ value: initFieldValue(f, initVal),
696
+ };
697
+ }
698
+ else {
699
+ setFieldOnObj(obj, f, initFieldValue(f, initVal));
700
+ }
588
701
  }
589
702
  }
703
+ if (Array.isArray(init.$unknown) && init.$unknown.length > 0) {
704
+ Object.defineProperty(obj, '$unknown', {
705
+ value: init.$unknown.map((u) => ({
706
+ no: u.no,
707
+ wireType: u.wireType,
708
+ data: u.data.slice(),
709
+ })),
710
+ enumerable: false,
711
+ writable: true,
712
+ configurable: true,
713
+ });
714
+ }
590
715
  return obj;
591
716
  }
592
717
  /**
@@ -887,7 +1012,7 @@ export class MessageSchema {
887
1012
  const writeUnknown = options?.writeUnknownFields ?? false;
888
1013
  const selfDescribing = Boolean(options?.selfDescribing);
889
1014
  if (selfDescribing) {
890
- if (deterministic && !this.isCyclicOrDeep()) {
1015
+ if (!this.isCyclicOrDeep()) {
891
1016
  enc.writeRaw(this.getCachedField0Det());
892
1017
  }
893
1018
  else {
@@ -981,8 +1106,8 @@ export class MessageSchema {
981
1106
  /**
982
1107
  * Decodes `data` into `target` (or a newly created instance if omitted).
983
1108
  */
984
- decode(data, target, depth = 0, options) {
985
- if (this.generatedHooks?.decode) {
1109
+ decode(data, target, depth = 0, options, startOffset = 0, endOffset = data.length) {
1110
+ if (this.generatedHooks?.decode && startOffset === 0 && endOffset === data.length) {
986
1111
  return this.generatedHooks.decode(data, target, depth, options);
987
1112
  }
988
1113
  const maxDepth = options?.recursionLimit ?? MaxNestingDepth;
@@ -1028,8 +1153,8 @@ export class MessageSchema {
1028
1153
  }
1029
1154
  }
1030
1155
  }
1031
- const n = data.length;
1032
- let pos = 0;
1156
+ const n = endOffset;
1157
+ let pos = startOffset;
1033
1158
  let seenMask = 0;
1034
1159
  let seenSet = null;
1035
1160
  let unknownFields = Array.isArray(out.$unknown) ? out.$unknown : null;
@@ -1048,7 +1173,7 @@ export class MessageSchema {
1048
1173
  pos += 2;
1049
1174
  }
1050
1175
  else {
1051
- const [t64, tn] = decodeVarintBigInt(data, pos);
1176
+ const [t64, tn] = decodeVarintBigInt(data, pos, n);
1052
1177
  if (tn === 0 || t64 >> 4n > 536870911n) {
1053
1178
  throw new Error(`omnibuf: truncated or invalid multi-byte tag at ${pos}`);
1054
1179
  }
@@ -1058,20 +1183,20 @@ export class MessageSchema {
1058
1183
  const fieldNum = tag <= 0x7fffffff ? tag >>> 4 : Math.floor(tag / 16);
1059
1184
  const wt = tag & 0x0f;
1060
1185
  if (fieldNum === 0) {
1061
- if (wt !== WireSubMsg) {
1062
- throw new Error(`omnibuf: invalid Field #0 wire type ${wt}`);
1186
+ const nextPos = validateField0At(data, pos, wt, depth, maxDepth, n);
1187
+ if (nextPos < 0) {
1188
+ throw new Error(`omnibuf: invalid Field #0 at ${pos}`);
1063
1189
  }
1064
- const [meta, nextPos] = readLenSlice(data, pos, 0);
1065
- decodeSchemaMeta(meta, depth + 1, maxDepth);
1066
1190
  pos = nextPos;
1067
1191
  continue;
1068
1192
  }
1069
- const f = this.byNum.get(fieldNum);
1193
+ const f = fieldNum <= 64 ? this.denseByNum[fieldNum] : this.byNum.get(fieldNum);
1070
1194
  if (!f ||
1071
1195
  (f.effectiveType !== 'bool' && f.wireType !== wt) ||
1072
1196
  (f.effectiveType === 'bool' && wt !== WireBoolFalse && wt !== WireBoolTrue)) {
1073
1197
  const valStart = pos;
1074
- pos = validateValue(data, pos, wt, depth, maxDepth);
1198
+ const valBuf = n === data.length ? data : data.subarray(0, n);
1199
+ pos = validateValue(valBuf, pos, wt, depth, maxDepth);
1075
1200
  if (readUnknown) {
1076
1201
  if (!unknownFields)
1077
1202
  unknownFields = [];
@@ -1103,11 +1228,11 @@ export class MessageSchema {
1103
1228
  if (cur !== undefined) {
1104
1229
  box[f.propKey] = cur;
1105
1230
  }
1106
- pos = decodeCompiledFieldValue(data, pos, wt, f, box, alreadySeen, depth, maxDepth, options);
1231
+ pos = decodeCompiledFieldValue(data, pos, wt, f, box, alreadySeen, depth, maxDepth, options, n);
1107
1232
  setFieldOnObj(out, f, box[f.propKey]);
1108
1233
  }
1109
1234
  else {
1110
- pos = decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, maxDepth, options);
1235
+ pos = decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, maxDepth, options, n);
1111
1236
  }
1112
1237
  }
1113
1238
  if (unknownFields && unknownFields.length > 0 && !('$unknown' in out)) {
@@ -1147,14 +1272,30 @@ function getFieldFromMsg(msg, f) {
1147
1272
  return undefined;
1148
1273
  }
1149
1274
  function coerce64(val, useBigInt, unsigned) {
1150
- if (useBigInt) {
1151
- const b = typeof val === 'bigint' ? val : BigInt(val ?? 0);
1152
- return unsigned ? BigInt.asUintN(64, b) : BigInt.asIntN(64, b);
1275
+ if (useBigInt || typeof val === 'string') {
1276
+ const b = toBigInt64Input(val ?? 0);
1277
+ const clamped = unsigned ? BigInt.asUintN(64, b) : BigInt.asIntN(64, b);
1278
+ if (useBigInt)
1279
+ return clamped;
1280
+ return unsigned
1281
+ ? clamped <= 9007199254740991n
1282
+ ? Number(clamped)
1283
+ : clamped
1284
+ : clamped >= -9007199254740991n && clamped <= 9007199254740991n
1285
+ ? Number(clamped)
1286
+ : clamped;
1153
1287
  }
1154
1288
  if (typeof val === 'bigint') {
1155
- return val >= -9007199254740991n && val <= 9007199254740991n ? Number(val) : val;
1289
+ const clamped = unsigned ? BigInt.asUintN(64, val) : BigInt.asIntN(64, val);
1290
+ return clamped >= -9007199254740991n && clamped <= 9007199254740991n
1291
+ ? Number(clamped)
1292
+ : clamped;
1156
1293
  }
1157
- return Number(val ?? 0);
1294
+ const n = Number(val ?? 0);
1295
+ if (!Number.isFinite(n)) {
1296
+ throw new Error(`omnibuf: non-finite integer value ${val}`);
1297
+ }
1298
+ return Math.trunc(n);
1158
1299
  }
1159
1300
  function initFieldValue(f, val) {
1160
1301
  if (val === null || val === undefined) {
@@ -1205,12 +1346,18 @@ function initFieldValue(f, val) {
1205
1346
  }
1206
1347
  const out = {};
1207
1348
  for (const [k, v] of Object.entries(val)) {
1208
- Object.defineProperty(out, k, {
1209
- value: isSubMsgVal ? sub.create(v) : v,
1210
- writable: true,
1211
- enumerable: true,
1212
- configurable: true,
1213
- });
1349
+ const mapped = isSubMsgVal ? sub.create(v) : v;
1350
+ if (k === '__proto__') {
1351
+ Object.defineProperty(out, k, {
1352
+ value: mapped,
1353
+ writable: true,
1354
+ enumerable: true,
1355
+ configurable: true,
1356
+ });
1357
+ }
1358
+ else {
1359
+ out[k] = mapped;
1360
+ }
1214
1361
  }
1215
1362
  return out;
1216
1363
  }
@@ -1269,12 +1416,18 @@ function cloneFieldValue(f, val) {
1269
1416
  }
1270
1417
  const out = {};
1271
1418
  for (const [k, v] of Object.entries(val)) {
1272
- Object.defineProperty(out, k, {
1273
- value: isSubMsgVal ? sub.clone(v) : v,
1274
- writable: true,
1275
- enumerable: true,
1276
- configurable: true,
1277
- });
1419
+ const cloned = isSubMsgVal ? sub.clone(v) : v;
1420
+ if (k === '__proto__') {
1421
+ Object.defineProperty(out, k, {
1422
+ value: cloned,
1423
+ writable: true,
1424
+ enumerable: true,
1425
+ configurable: true,
1426
+ });
1427
+ }
1428
+ else {
1429
+ out[k] = cloned;
1430
+ }
1278
1431
  }
1279
1432
  return out;
1280
1433
  }
@@ -1418,8 +1571,14 @@ function defaultFieldValue(f, creatingAncestors) {
1418
1571
  return '';
1419
1572
  case 'bytes':
1420
1573
  return new Uint8Array(0);
1421
- default:
1574
+ case 'uint64':
1575
+ case 'int64':
1576
+ case 'sint64':
1577
+ case 'fixed64':
1578
+ case 'sfixed64':
1422
1579
  return f.useBigInt ? 0n : 0;
1580
+ default:
1581
+ return 0;
1423
1582
  }
1424
1583
  }
1425
1584
  function isFieldValueEmpty(f, val) {
@@ -1461,7 +1620,12 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false, s
1461
1620
  break;
1462
1621
  case 'uint64': {
1463
1622
  const v = rawVal ?? 0;
1464
- enc.writeUint(fnum, typeof v === 'bigint' ? BigInt.asUintN(64, v) : v < 0 ? BigInt.asUintN(64, BigInt(v)) : v);
1623
+ if (typeof v === 'number' && v >= 0 && v <= 9007199254740991) {
1624
+ enc.writeUint(fnum, v);
1625
+ }
1626
+ else {
1627
+ enc.writeUint(fnum, BigInt.asUintN(64, toBigInt64Input(v)));
1628
+ }
1465
1629
  break;
1466
1630
  }
1467
1631
  case 'sint32':
@@ -1582,13 +1746,9 @@ function encodeMapScalarEntry(enc, t, val) {
1582
1746
  enc.writeRawVarint(Number(val ?? 0) >>> 0);
1583
1747
  break;
1584
1748
  case 'uint64': {
1585
- const v = typeof val === 'bigint'
1586
- ? BigInt.asUintN(64, val)
1587
- : typeof val === 'string'
1588
- ? BigInt.asUintN(64, BigInt(val))
1589
- : val < 0
1590
- ? BigInt.asUintN(64, BigInt(val))
1591
- : Number(val ?? 0);
1749
+ const v = typeof val === 'number' && val >= 0 && val <= 9007199254740991
1750
+ ? val
1751
+ : BigInt.asUintN(64, toBigInt64Input(val ?? 0));
1592
1752
  enc.writeRawVarint(v);
1593
1753
  break;
1594
1754
  }
@@ -1599,8 +1759,7 @@ function encodeMapScalarEntry(enc, t, val) {
1599
1759
  break;
1600
1760
  case 'sint64':
1601
1761
  case 'int64': {
1602
- const v = typeof val === 'bigint' ? val : typeof val === 'string' ? BigInt(val) : Number(val ?? 0);
1603
- enc.writeRawVarint(encodeZigZag(v));
1762
+ enc.writeRawVarint(encodeZigZag(val ?? 0));
1604
1763
  break;
1605
1764
  }
1606
1765
  case 'float32':
@@ -1615,8 +1774,7 @@ function encodeMapScalarEntry(enc, t, val) {
1615
1774
  break;
1616
1775
  case 'fixed64':
1617
1776
  case 'sfixed64': {
1618
- const v = typeof val === 'bigint' ? val : typeof val === 'string' ? BigInt(val) : Number(val ?? 0);
1619
- enc.writeRawFixed64(v);
1777
+ enc.writeRawFixed64(val ?? 0);
1620
1778
  break;
1621
1779
  }
1622
1780
  case 'bool': {
@@ -1629,9 +1787,7 @@ function encodeMapScalarEntry(enc, t, val) {
1629
1787
  function compareMapKeys(keyType, a, b) {
1630
1788
  switch (keyType) {
1631
1789
  case 'string': {
1632
- const sa = String(a);
1633
- const sb = String(b);
1634
- return sa < sb ? -1 : sa > sb ? 1 : 0;
1790
+ return compareUTF8Strings(String(a), String(b));
1635
1791
  }
1636
1792
  case 'bool': {
1637
1793
  const ba = a === true || a === 'true' ? 1 : 0;
@@ -1680,29 +1836,31 @@ function encodeMapField(enc, f, rawVal, deterministic, writeUnknown = false, sel
1680
1836
  }
1681
1837
  enc.endContainer();
1682
1838
  }
1683
- function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, maxDepth, options) {
1839
+ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, maxDepth, options, endLimit = data.length) {
1684
1840
  const key = f.propKey;
1685
- const n = data.length;
1841
+ const n = endLimit;
1686
1842
  if ((wt === WireArray || wt === WireMap) && depth + 1 > maxDepth) {
1687
1843
  throw new Error(`omnibuf: nesting depth ${depth + 1} exceeds MaxNestingDepth (${maxDepth})`);
1688
1844
  }
1689
1845
  switch (f.effectiveType) {
1690
1846
  case 'uint32': {
1691
1847
  if (pos < n && (data[pos] & 7) !== 0) {
1692
- const [v, vn] = decodeVarint(data, pos);
1848
+ const [v, vn] = decodeVarint(data, pos, n);
1693
1849
  if (vn === 0)
1694
1850
  throw new Error(`omnibuf: truncated uint32 at ${pos}`);
1695
1851
  out[key] = v >>> 0;
1696
1852
  return pos + vn;
1697
1853
  }
1698
- const [v, vn] = decodeVarintBigInt(data, pos);
1854
+ const [v, vn] = decodeVarintBigInt(data, pos, n);
1699
1855
  if (vn === 0)
1700
1856
  throw new Error(`omnibuf: truncated uint32 at ${pos}`);
1701
1857
  out[key] = Number(v & 0xffffffffn) >>> 0;
1702
1858
  return pos + vn;
1703
1859
  }
1704
1860
  case 'uint64': {
1705
- const [v, vn] = f.useBigInt ? decodeVarintBigInt(data, pos) : decodeVarintAuto(data, pos);
1861
+ const [v, vn] = f.useBigInt
1862
+ ? decodeVarintBigInt(data, pos, n)
1863
+ : decodeVarintAuto(data, pos, n);
1706
1864
  if (vn === 0)
1707
1865
  throw new Error(`omnibuf: truncated uint64 at ${pos}`);
1708
1866
  out[key] = v;
@@ -1712,13 +1870,13 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1712
1870
  case 'int32':
1713
1871
  case 'enum': {
1714
1872
  if (pos < n && (data[pos] & 7) !== 0) {
1715
- const [v, vn] = decodeVarint(data, pos);
1873
+ const [v, vn] = decodeVarint(data, pos, n);
1716
1874
  if (vn === 0)
1717
1875
  throw new Error(`omnibuf: truncated int32 at ${pos}`);
1718
1876
  out[key] = decodeZigZag32(v);
1719
1877
  return pos + vn;
1720
1878
  }
1721
- const [v, vn] = decodeVarintBigInt(data, pos);
1879
+ const [v, vn] = decodeVarintBigInt(data, pos, n);
1722
1880
  if (vn === 0)
1723
1881
  throw new Error(`omnibuf: truncated int32 at ${pos}`);
1724
1882
  out[key] = decodeZigZag32(v);
@@ -1726,7 +1884,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1726
1884
  }
1727
1885
  case 'sint64':
1728
1886
  case 'int64': {
1729
- const [v, vn] = decodeVarintBigInt(data, pos);
1887
+ const [v, vn] = decodeVarintBigInt(data, pos, n);
1730
1888
  if (vn === 0)
1731
1889
  throw new Error(`omnibuf: truncated int64 at ${pos}`);
1732
1890
  out[key] = f.useBigInt ? decodeZigZagBigInt(v) : decodeZigZagAuto(v);
@@ -1737,13 +1895,22 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1737
1895
  return pos;
1738
1896
  }
1739
1897
  case 'string': {
1740
- const [s, nextPos] = readLenSlice(data, pos, 0);
1898
+ if (pos < n && (data[pos] & 1) !== 0) {
1899
+ const sLen = data[pos] >>> 1;
1900
+ const sStart = pos + 1;
1901
+ const sEnd = sStart + sLen;
1902
+ if (sEnd <= n) {
1903
+ out[key] = decodeUTF8(data, sStart, sEnd);
1904
+ return sEnd;
1905
+ }
1906
+ }
1907
+ const [s, nextPos] = readLenSlice(data, pos, 0, n);
1741
1908
  out[key] = decodeUTF8(s);
1742
1909
  return nextPos;
1743
1910
  }
1744
1911
  case 'bytes': {
1745
- const [b, nextPos] = readLenSlice(data, pos, 0);
1746
- out[key] = b.slice();
1912
+ const [b, nextPos] = readLenSlice(data, pos, 0, n);
1913
+ out[key] = options?.zeroCopy ? b : b.slice();
1747
1914
  return nextPos;
1748
1915
  }
1749
1916
  case 'float32': {
@@ -1751,7 +1918,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1751
1918
  throw new Error(`omnibuf: truncated float32 at ${pos}`);
1752
1919
  const bits = (data[pos] | (data[pos + 1] << 8) | (data[pos + 2] << 16) | (data[pos + 3] << 24)) >>>
1753
1920
  0;
1754
- out[key] = roundFloat32Shortest(float32FromBits(bits));
1921
+ out[key] = float32FromBits(bits);
1755
1922
  return pos + 4;
1756
1923
  }
1757
1924
  case 'fixed32': {
@@ -1798,7 +1965,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1798
1965
  return pos + 8;
1799
1966
  }
1800
1967
  case 'string[]': {
1801
- const [body, nextPos] = readLenSlice(data, pos, 2);
1968
+ const [body, nextPos] = readLenSlice(data, pos, 2, n);
1802
1969
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1803
1970
  const [arr, ok] = appendStringArrBody(prev, body);
1804
1971
  if (!ok)
@@ -1807,16 +1974,16 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1807
1974
  return nextPos;
1808
1975
  }
1809
1976
  case 'bytes[]': {
1810
- const [body, nextPos] = readLenSlice(data, pos, 2);
1977
+ const [body, nextPos] = readLenSlice(data, pos, 2, n);
1811
1978
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1812
- const [arr, ok] = appendBytesArrBody(prev, body, false);
1979
+ const [arr, ok] = appendBytesArrBody(prev, body, Boolean(options?.zeroCopy));
1813
1980
  if (!ok)
1814
1981
  throw new Error(`omnibuf: invalid bytes array at ${pos}`);
1815
1982
  out[key] = arr;
1816
1983
  return nextPos;
1817
1984
  }
1818
1985
  case 'bool[]': {
1819
- const [body, nextPos] = readLenSlice(data, pos, 2);
1986
+ const [body, nextPos] = readLenSlice(data, pos, 2, n);
1820
1987
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1821
1988
  const [arr, ok] = appendBoolArrBody(prev, body);
1822
1989
  if (!ok)
@@ -1825,7 +1992,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1825
1992
  return nextPos;
1826
1993
  }
1827
1994
  case 'uint32[]': {
1828
- const [body, nextPos] = readLenSlice(data, pos, 2);
1995
+ const [body, nextPos] = readLenSlice(data, pos, 2, n);
1829
1996
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1830
1997
  const [arr, ok] = appendUint32ArrBody(prev, body);
1831
1998
  if (!ok)
@@ -1834,7 +2001,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1834
2001
  return nextPos;
1835
2002
  }
1836
2003
  case 'enum[]': {
1837
- const [body, nextPos] = readLenSlice(data, pos, 2);
2004
+ const [body, nextPos] = readLenSlice(data, pos, 2, n);
1838
2005
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1839
2006
  const [arr, ok] = appendEnumArrBody(prev, body);
1840
2007
  if (!ok)
@@ -1843,7 +2010,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1843
2010
  return nextPos;
1844
2011
  }
1845
2012
  case 'uint64[]': {
1846
- const [body, nextPos] = readLenSlice(data, pos, 2);
2013
+ const [body, nextPos] = readLenSlice(data, pos, 2, n);
1847
2014
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1848
2015
  const startLen = prev.length;
1849
2016
  const [arr, ok] = appendUintArrAutoBody(prev, body);
@@ -1859,7 +2026,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1859
2026
  }
1860
2027
  case 'sint32[]':
1861
2028
  case 'int32[]': {
1862
- const [body, nextPos] = readLenSlice(data, pos, 2);
2029
+ const [body, nextPos] = readLenSlice(data, pos, 2, n);
1863
2030
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1864
2031
  const [arr, ok] = appendEnumArrBody(prev, body);
1865
2032
  if (!ok)
@@ -1869,7 +2036,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1869
2036
  }
1870
2037
  case 'sint64[]':
1871
2038
  case 'int64[]': {
1872
- const [body, nextPos] = readLenSlice(data, pos, 2);
2039
+ const [body, nextPos] = readLenSlice(data, pos, 2, n);
1873
2040
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1874
2041
  const startLen = prev.length;
1875
2042
  const [arr, ok] = appendSintArrAutoBody(prev, body);
@@ -1884,20 +2051,16 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1884
2051
  return nextPos;
1885
2052
  }
1886
2053
  case 'float32[]': {
1887
- const [body, nextPos] = readLenSlice(data, pos, 2);
2054
+ const [body, nextPos] = readLenSlice(data, pos, 2, n);
1888
2055
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1889
- const startLen = prev.length;
1890
2056
  const [arr, ok] = appendFloat32ArrBody(prev, body);
1891
2057
  if (!ok)
1892
2058
  throw new Error(`omnibuf: invalid float32 array at ${pos}`);
1893
- for (let i = startLen; i < arr.length; i++) {
1894
- arr[i] = roundFloat32Shortest(arr[i]);
1895
- }
1896
2059
  out[key] = arr;
1897
2060
  return nextPos;
1898
2061
  }
1899
2062
  case 'fixed32[]': {
1900
- const [body, nextPos] = readLenSlice(data, pos, 2);
2063
+ const [body, nextPos] = readLenSlice(data, pos, 2, n);
1901
2064
  const [elemType, c, startP, ok] = parseArrayHeader(body);
1902
2065
  if (!ok || elemType !== WireFixed32 || body.length - startP !== c * 4) {
1903
2066
  throw new Error(`omnibuf: invalid fixed32 array at ${pos}`);
@@ -1912,7 +2075,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1912
2075
  return nextPos;
1913
2076
  }
1914
2077
  case 'sfixed32[]': {
1915
- const [body, nextPos] = readLenSlice(data, pos, 2);
2078
+ const [body, nextPos] = readLenSlice(data, pos, 2, n);
1916
2079
  const [elemType, c, startP, ok] = parseArrayHeader(body);
1917
2080
  if (!ok || elemType !== WireSfixed32 || body.length - startP !== c * 4) {
1918
2081
  throw new Error(`omnibuf: invalid sfixed32 array at ${pos}`);
@@ -1926,7 +2089,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1926
2089
  return nextPos;
1927
2090
  }
1928
2091
  case 'float64[]': {
1929
- const [body, nextPos] = readLenSlice(data, pos, 2);
2092
+ const [body, nextPos] = readLenSlice(data, pos, 2, n);
1930
2093
  const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1931
2094
  const [arr, ok] = appendFloat64ArrBody(prev, body);
1932
2095
  if (!ok)
@@ -1936,7 +2099,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1936
2099
  }
1937
2100
  case 'fixed64[]':
1938
2101
  case 'sfixed64[]': {
1939
- const [body, nextPos] = readLenSlice(data, pos, 2);
2102
+ const [body, nextPos] = readLenSlice(data, pos, 2, n);
1940
2103
  const isSigned = f.effectiveType === 'sfixed64[]';
1941
2104
  const expElemType = isSigned ? WireSfixed64 : WireFixed64;
1942
2105
  const [elemType, c, startP, ok] = parseArrayHeader(body);
@@ -1964,7 +2127,6 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1964
2127
  return nextPos;
1965
2128
  }
1966
2129
  case 'submsg': {
1967
- const [subBytes, nextPos] = readLenSlice(data, pos, 0);
1968
2130
  const subSchema = f.resolveSubSchema?.();
1969
2131
  if (!subSchema) {
1970
2132
  throw new Error(`omnibuf: missing subSchema on ${key}`);
@@ -1972,29 +2134,120 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
1972
2134
  const existing = out[key];
1973
2135
  const hasExisting = existing !== undefined && existing !== null && typeof existing === 'object';
1974
2136
  const targetSub = hasExisting ? existing : subSchema.create();
1975
- out[key] = subSchema.decode(subBytes, targetSub, depth + 1, {
1976
- ...options,
1977
- merge: f.oneof ? alreadySeen && hasExisting : alreadySeen,
1978
- });
2137
+ const shouldMerge = f.oneof ? alreadySeen && hasExisting : alreadySeen;
2138
+ const subOpts = shouldMerge
2139
+ ? { ...options, merge: true }
2140
+ : options?.merge
2141
+ ? { ...options, merge: false }
2142
+ : options;
2143
+ if (pos < n) {
2144
+ const lb = data[pos];
2145
+ if ((lb & 1) !== 0) {
2146
+ const subLen = lb >>> 1;
2147
+ const sStart = pos + 1;
2148
+ const sEnd = sStart + subLen;
2149
+ if (sEnd <= n) {
2150
+ out[key] = subSchema.decode(data, targetSub, depth + 1, subOpts, sStart, sEnd);
2151
+ return sEnd;
2152
+ }
2153
+ }
2154
+ else if ((lb & 2) !== 0 && pos + 2 <= n) {
2155
+ const subLen = (lb | (data[pos + 1] << 8)) >>> 2;
2156
+ const sStart = pos + 2;
2157
+ const sEnd = sStart + subLen;
2158
+ if (sEnd <= n) {
2159
+ out[key] = subSchema.decode(data, targetSub, depth + 1, subOpts, sStart, sEnd);
2160
+ return sEnd;
2161
+ }
2162
+ }
2163
+ }
2164
+ const [subBytes, nextPos] = readLenSlice(data, pos, 0, n);
2165
+ out[key] = subSchema.decode(subBytes, targetSub, depth + 1, subOpts);
1979
2166
  return nextPos;
1980
2167
  }
1981
2168
  case 'submsg[]': {
1982
- const [arrPayload, nextPos] = readLenSlice(data, pos, 2);
1983
- const [elemCount, ok] = countSubMsgArrElems(arrPayload);
1984
- if (!ok) {
1985
- throw new Error(`omnibuf: corrupt submsg array at ${pos}`);
1986
- }
1987
2169
  const subSchema = f.resolveSubSchema?.();
1988
2170
  if (!subSchema) {
1989
2171
  throw new Error(`omnibuf: missing subSchema on ${key}`);
1990
2172
  }
2173
+ if (pos < n) {
2174
+ let arrLen = -1;
2175
+ let arrStart = pos;
2176
+ const lb = data[pos];
2177
+ if ((lb & 1) !== 0) {
2178
+ arrLen = lb >>> 1;
2179
+ arrStart = pos + 1;
2180
+ }
2181
+ else if ((lb & 2) !== 0 && pos + 2 <= n) {
2182
+ arrLen = (lb | (data[pos + 1] << 8)) >>> 2;
2183
+ arrStart = pos + 2;
2184
+ }
2185
+ const arrEnd = arrStart + arrLen;
2186
+ if (arrLen >= 2 &&
2187
+ arrEnd <= n &&
2188
+ data[arrStart] === WireSubMsg &&
2189
+ (data[arrStart + 1] & 1) !== 0) {
2190
+ const elemCount = data[arrStart + 1] >>> 1;
2191
+ let p = arrStart + 2;
2192
+ if (elemCount <= arrEnd - p && (elemCount > 0 || p === arrEnd)) {
2193
+ const elems = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
2194
+ const origLen = elems.length;
2195
+ for (let i = 0; i < elemCount; i++) {
2196
+ if (p >= arrEnd) {
2197
+ elems.length = origLen;
2198
+ throw new Error(`omnibuf: corrupt submsg array at ${pos}`);
2199
+ }
2200
+ let elNum = 0;
2201
+ const eb0 = data[p];
2202
+ if ((eb0 & 1) !== 0) {
2203
+ elNum = eb0 >>> 1;
2204
+ p += 1;
2205
+ }
2206
+ else if ((eb0 & 2) !== 0) {
2207
+ if (p + 2 > arrEnd) {
2208
+ elems.length = origLen;
2209
+ throw new Error(`omnibuf: corrupt submsg array at ${pos}`);
2210
+ }
2211
+ elNum = (eb0 | (data[p + 1] << 8)) >>> 2;
2212
+ p += 2;
2213
+ }
2214
+ else {
2215
+ const [el, en] = decodeVarintBigInt(data, p, arrEnd);
2216
+ if (en === 0 || el > BigInt(arrEnd - (p + en))) {
2217
+ elems.length = origLen;
2218
+ throw new Error(`omnibuf: corrupt submsg array at ${pos}`);
2219
+ }
2220
+ p += en;
2221
+ elNum = Number(el);
2222
+ }
2223
+ if (p + elNum > arrEnd) {
2224
+ elems.length = origLen;
2225
+ throw new Error(`omnibuf: corrupt submsg array at ${pos}`);
2226
+ }
2227
+ elems.push(subSchema.decode(data, subSchema.create(), depth + 2, options, p, p + elNum));
2228
+ p += elNum;
2229
+ }
2230
+ if (p !== arrEnd) {
2231
+ elems.length = origLen;
2232
+ throw new Error(`omnibuf: corrupt submsg array at ${pos}`);
2233
+ }
2234
+ out[key] = elems;
2235
+ return arrEnd;
2236
+ }
2237
+ }
2238
+ }
2239
+ const [arrPayload, nextPos] = readLenSlice(data, pos, 2, n);
2240
+ const [elemCount, ok] = countSubMsgArrElems(arrPayload);
2241
+ if (!ok) {
2242
+ throw new Error(`omnibuf: corrupt submsg array at ${pos}`);
2243
+ }
1991
2244
  const elems = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1992
2245
  let [, , p] = parseArrayHeader(arrPayload);
1993
2246
  for (let i = 0; i < elemCount; i++) {
1994
2247
  const [el, en] = decodeVarintBigInt(arrPayload, p);
1995
2248
  p += en;
1996
2249
  const elNum = Number(el);
1997
- const elemObj = subSchema.decode(arrPayload.subarray(p, p + elNum), subSchema.create(), depth + 2, options);
2250
+ const elemObj = subSchema.decode(arrPayload, subSchema.create(), depth + 2, options, p, p + elNum);
1998
2251
  elems.push(elemObj);
1999
2252
  p += elNum;
2000
2253
  }
@@ -2002,7 +2255,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
2002
2255
  return nextPos;
2003
2256
  }
2004
2257
  case 'map': {
2005
- const [body, nextPos] = readLenSlice(data, pos, 3);
2258
+ const [body, nextPos] = readLenSlice(data, pos, 3, n);
2006
2259
  decodeMapFieldBody(f, out, body, alreadySeen, depth + 1, options);
2007
2260
  return nextPos;
2008
2261
  }
@@ -2011,12 +2264,20 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, max
2011
2264
  function decodeMapScalarEntry(body, p, wt, subSchema, depth, options) {
2012
2265
  switch (wt) {
2013
2266
  case WireString: {
2267
+ if (p < body.length && (body[p] & 1) !== 0) {
2268
+ const sLen = body[p] >>> 1;
2269
+ const sStart = p + 1;
2270
+ const sEnd = sStart + sLen;
2271
+ if (sEnd <= body.length) {
2272
+ return [decodeUTF8(body, sStart, sEnd), sEnd];
2273
+ }
2274
+ }
2014
2275
  const [sBytes, nextP] = readLenSlice(body, p, 0);
2015
2276
  return [decodeUTF8(sBytes), nextP];
2016
2277
  }
2017
2278
  case WireBytes: {
2018
2279
  const [bBytes, nextP] = readLenSlice(body, p, 0);
2019
- return [bBytes.slice(), nextP];
2280
+ return [options?.zeroCopy ? bBytes : bBytes.slice(), nextP];
2020
2281
  }
2021
2282
  case WireUint: {
2022
2283
  const [v, vn] = decodeVarintAuto(body, p);
@@ -2076,6 +2337,31 @@ function decodeMapScalarEntry(body, p, wt, subSchema, depth, options) {
2076
2337
  throw new Error(`omnibuf: invalid WireMap entry wire type ${wt}`);
2077
2338
  }
2078
2339
  }
2340
+ function convertMapScalarValue(v, type, useBigInt) {
2341
+ switch (type) {
2342
+ case 'bool':
2343
+ return Boolean(v);
2344
+ case 'float32':
2345
+ return float32FromBits(Number(v));
2346
+ case 'float64':
2347
+ return float64FromBits(BigInt(v));
2348
+ case 'uint32':
2349
+ return typeof v === 'number' ? v >>> 0 : Number(BigInt(v) & 0xffffffffn) >>> 0;
2350
+ case 'sfixed32':
2351
+ case 'int32':
2352
+ case 'sint32':
2353
+ case 'enum':
2354
+ return typeof v === 'number' ? v | 0 : Number(BigInt(v) & 0xffffffffn) | 0;
2355
+ case 'sfixed64': {
2356
+ const signed = BigInt.asIntN(64, BigInt(v));
2357
+ return useBigInt || signed < -9007199254740991n || signed > 9007199254740991n
2358
+ ? signed
2359
+ : Number(signed);
2360
+ }
2361
+ default:
2362
+ return v;
2363
+ }
2364
+ }
2079
2365
  function decodeMapFieldBody(f, out, body, alreadySeen, depth, options) {
2080
2366
  const expKeyWT = f.mapKeyWT ?? WireString;
2081
2367
  const expValWT = f.mapValWT ?? WireUint;
@@ -2114,41 +2400,27 @@ function decodeMapFieldBody(f, out, body, alreadySeen, depth, options) {
2114
2400
  let p = 2 + cn;
2115
2401
  const c = Number(count);
2116
2402
  for (let i = 0; i < c; i++) {
2117
- const [kv, nextP] = decodeMapScalarEntry(body, p, f.mapKeyWT ?? WireString, undefined, depth, options);
2118
- const [rawVv, nextP2] = decodeMapScalarEntry(body, nextP, f.mapValWT ?? WireUint, subSchema, depth, options);
2119
- const convert = (v, type) => {
2120
- if (type === 'bool')
2121
- return Boolean(v);
2122
- if (type === 'float32')
2123
- return roundFloat32Shortest(float32FromBits(Number(v)));
2124
- if (type === 'float64')
2125
- return float64FromBits(BigInt(v));
2126
- if (type === 'uint32')
2127
- return Number(BigInt(v) & 0xffffffffn) >>> 0;
2128
- if (type === 'sfixed32' || type === 'int32' || type === 'sint32' || type === 'enum') {
2129
- return Number(BigInt(v) & 0xffffffffn) | 0;
2130
- }
2131
- if (type === 'sfixed64') {
2132
- const signed = BigInt.asIntN(64, BigInt(v));
2133
- return f.useBigInt || signed < -9007199254740991n || signed > 9007199254740991n
2134
- ? signed
2135
- : Number(signed);
2136
- }
2137
- return v;
2138
- };
2139
- const vv = convert(isVal64 ? BigInt(rawVv) : rawVv, f.mapValType);
2140
- const key = convert(isKey64 ? BigInt(kv) : kv, f.mapKeyType);
2403
+ const [kv, nextP] = decodeMapScalarEntry(body, p, expKeyWT, undefined, depth, options);
2404
+ const [rawVv, nextP2] = decodeMapScalarEntry(body, nextP, expValWT, subSchema, depth, options);
2405
+ const vv = convertMapScalarValue(isVal64 ? BigInt(rawVv) : rawVv, f.mapValType, f.useBigInt);
2406
+ const key = convertMapScalarValue(isKey64 ? BigInt(kv) : kv, f.mapKeyType, f.useBigInt);
2141
2407
  p = nextP2;
2142
2408
  if (targetMap instanceof Map) {
2143
2409
  targetMap.set(key, vv);
2144
2410
  }
2145
2411
  else {
2146
- Object.defineProperty(targetMap, String(key), {
2147
- value: vv,
2148
- enumerable: true,
2149
- configurable: true,
2150
- writable: true,
2151
- });
2412
+ const kStr = String(key);
2413
+ if (kStr === '__proto__') {
2414
+ Object.defineProperty(targetMap, kStr, {
2415
+ value: vv,
2416
+ enumerable: true,
2417
+ configurable: true,
2418
+ writable: true,
2419
+ });
2420
+ }
2421
+ else {
2422
+ targetMap[kStr] = vv;
2423
+ }
2152
2424
  }
2153
2425
  }
2154
2426
  if (p !== body.length) {
@@ -2191,8 +2463,13 @@ export function omnibufField(fieldNum, options = {}) {
2191
2463
  const propKey = String(contextOrPropKey.name);
2192
2464
  if (contextOrPropKey.metadata) {
2193
2465
  const metaObj = contextOrPropKey.metadata;
2194
- if (!metaObj[PENDING_FIELDS_SYMBOL]) {
2195
- metaObj[PENDING_FIELDS_SYMBOL] = Object.create(null);
2466
+ if (!Object.hasOwn(metaObj, PENDING_FIELDS_SYMBOL)) {
2467
+ const inherited = metaObj[PENDING_FIELDS_SYMBOL];
2468
+ const own = Object.create(null);
2469
+ if (inherited) {
2470
+ Object.assign(own, inherited);
2471
+ }
2472
+ metaObj[PENDING_FIELDS_SYMBOL] = own;
2196
2473
  }
2197
2474
  metaObj[PENDING_FIELDS_SYMBOL][propKey] = spec;
2198
2475
  }
@@ -2263,7 +2540,8 @@ export function getClassSchema(ctorOrInstance) {
2263
2540
  const ctor = typeof ctorOrInstance === 'function' ? ctorOrInstance : ctorOrInstance?.constructor;
2264
2541
  if (!ctor || ctor === Object)
2265
2542
  return null;
2266
- const existing = classSchemaRegistry.get(ctor) ?? ctor[SCHEMA_SYMBOL];
2543
+ const existing = classSchemaRegistry.get(ctor) ??
2544
+ (Object.hasOwn(ctor, SCHEMA_SYMBOL) ? ctor[SCHEMA_SYMBOL] : undefined);
2267
2545
  if (existing)
2268
2546
  return existing;
2269
2547
  // Trigger a dummy instantiation if Stage 3 addInitializer hasn't run yet
@@ -2280,7 +2558,9 @@ export function getClassSchema(ctorOrInstance) {
2280
2558
  if (!pending || Object.keys(pending).length === 0) {
2281
2559
  return null;
2282
2560
  }
2283
- const name = ctor.__omnibufTypeName || ctor.name || 'Message';
2561
+ const name = (Object.hasOwn(ctor, '__omnibufTypeName') ? ctor.__omnibufTypeName : '') ||
2562
+ ctor.name ||
2563
+ 'Message';
2284
2564
  const schema = new MessageSchema(name, pending, () => new ctor());
2285
2565
  classSchemaRegistry.set(ctor, schema);
2286
2566
  ctor[SCHEMA_SYMBOL] = schema;