@omnibuf/core 0.0.7 → 0.1.1

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
@@ -5,7 +5,7 @@ import { validateValue, validateMapTypes } from './validation.js';
5
5
  import { base64ToUint8Array, decodeSchemaMeta, 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, decodeZigZag, 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, decodeUTF8, decodeVarint, decodeVarintAuto, decodeVarintBigInt, decodeZigZag32, decodeZigZagAuto, decodeZigZagBigInt, encodeZigZag, encodeZigZagNumber, float32FromBits, float64FromBits, parseArrayHeader, readFloat64At, readLenSlice, roundFloat32Shortest, } from './wire.js';
9
9
  /**
10
10
  * Converts `snake_case` field names to `protoCamelCase` (matching `protoc` and `@bufbuild/protobuf` v2).
11
11
  */
@@ -48,7 +48,44 @@ export function protoCamelCase(snakeCase) {
48
48
  export function protoSnakeCase(lowerCamelCase) {
49
49
  return lowerCamelCase.replace(/[A-Z]/g, (letter) => '_' + letter.toLowerCase());
50
50
  }
51
- const RESERVED_OBJECT_PROPERTIES = new Set(['constructor', 'toString', 'toJSON', 'valueOf']);
51
+ const RESERVED_OBJECT_PROPERTIES = new Set([
52
+ '__proto__',
53
+ 'constructor',
54
+ 'hasOwnProperty',
55
+ 'isPrototypeOf',
56
+ 'propertyIsEnumerable',
57
+ 'toLocaleString',
58
+ 'toString',
59
+ 'toJSON',
60
+ 'valueOf',
61
+ ]);
62
+ function getOwnProp(obj, key) {
63
+ const desc = Object.getOwnPropertyDescriptor(obj, key);
64
+ if (!desc)
65
+ return undefined;
66
+ return desc.get ? desc.get.call(obj) : desc.value;
67
+ }
68
+ function setOwnProp(obj, key, value) {
69
+ const desc = Object.getOwnPropertyDescriptor(obj, key);
70
+ if (desc?.set) {
71
+ desc.set.call(obj, value);
72
+ return;
73
+ }
74
+ Object.defineProperty(obj, key, {
75
+ value,
76
+ writable: true,
77
+ enumerable: true,
78
+ configurable: true,
79
+ });
80
+ }
81
+ function setFieldOnObj(obj, f, value) {
82
+ if (f.isProtoKey) {
83
+ setOwnProp(obj, f.propKey, value);
84
+ }
85
+ else {
86
+ obj[f.propKey] = value;
87
+ }
88
+ }
52
89
  /**
53
90
  * Escapes field names that collide with built-in JavaScript `Object.prototype` properties.
54
91
  */
@@ -233,6 +270,9 @@ export class MessageSchema {
233
270
  if (!Number.isInteger(spec.fieldNum) || spec.fieldNum < 1 || spec.fieldNum > 536870911) {
234
271
  throw new Error(`omnibuf: field number ${spec.fieldNum} on ${typeName}.${propKey} out of range (1..536870911)`);
235
272
  }
273
+ if (spec.fieldNum >= 19000 && spec.fieldNum <= 19999) {
274
+ throw new Error(`omnibuf: field number ${spec.fieldNum} on ${typeName}.${propKey} is in Protobuf reserved range (19000..19999)`);
275
+ }
236
276
  if (this.byNum.has(spec.fieldNum)) {
237
277
  throw new Error(`omnibuf: duplicate field number ${spec.fieldNum} on ${typeName}`);
238
278
  }
@@ -265,6 +305,10 @@ export class MessageSchema {
265
305
  oneof: oneofLocal,
266
306
  oneofProtoName: oneofProto,
267
307
  useBigInt: spec.useBigInt ?? false,
308
+ isProtoKey: propKey === '__proto__',
309
+ inObjectProto: propKey in Object.prototype,
310
+ altInObjectProto: altKey !== undefined && altKey in Object.prototype,
311
+ oneofProtoInObjectProto: oneofProto !== undefined && oneofProto in Object.prototype,
268
312
  resolveSubSchema,
269
313
  };
270
314
  if (effectiveType === 'map') {
@@ -326,17 +370,23 @@ export class MessageSchema {
326
370
  });
327
371
  }
328
372
  for (const oneof of this.oneofs) {
329
- if (!(oneof.localName in obj) || obj[oneof.localName] === undefined) {
330
- obj[oneof.localName] = { case: undefined };
373
+ if (!Object.hasOwn(obj, oneof.localName) || getOwnProp(obj, oneof.localName) === undefined) {
374
+ setOwnProp(obj, oneof.localName, { case: undefined });
331
375
  }
332
- if (oneof.protoName !== oneof.localName && !(oneof.protoName in obj)) {
376
+ if (oneof.protoName !== oneof.localName && !Object.hasOwn(obj, oneof.protoName)) {
333
377
  const ln = oneof.localName;
378
+ const isProtoLn = ln === '__proto__';
334
379
  Object.defineProperty(obj, oneof.protoName, {
335
380
  get() {
336
- return this[ln];
381
+ return isProtoLn ? getOwnProp(this, ln) : this[ln];
337
382
  },
338
383
  set(v) {
339
- this[ln] = v;
384
+ if (isProtoLn) {
385
+ setOwnProp(this, ln, v);
386
+ }
387
+ else {
388
+ this[ln] = v;
389
+ }
340
390
  },
341
391
  enumerable: false,
342
392
  configurable: true,
@@ -349,7 +399,7 @@ export class MessageSchema {
349
399
  const caseName = f.localName;
350
400
  const altCaseName = f.name;
351
401
  const propDesc = Object.getOwnPropertyDescriptor(obj, f.propKey);
352
- if (!(f.propKey in obj) || (propDesc && 'value' in propDesc)) {
402
+ if (!propDesc || 'value' in propDesc) {
353
403
  const initialPlainVal = propDesc?.value;
354
404
  Object.defineProperty(obj, f.propKey, {
355
405
  get() {
@@ -381,14 +431,20 @@ export class MessageSchema {
381
431
  obj[oneofKey] = { case: caseName, value: initialPlainVal };
382
432
  }
383
433
  }
384
- if (f.altKey && !(f.altKey in obj)) {
434
+ if (f.altKey && !Object.hasOwn(obj, f.altKey)) {
385
435
  const pk = f.propKey;
436
+ const isProtoPk = Boolean(f.isProtoKey);
386
437
  Object.defineProperty(obj, f.altKey, {
387
438
  get() {
388
- return this[pk];
439
+ return isProtoPk ? getOwnProp(this, pk) : this[pk];
389
440
  },
390
441
  set(v) {
391
- this[pk] = v;
442
+ if (isProtoPk) {
443
+ setOwnProp(this, pk, v);
444
+ }
445
+ else {
446
+ this[pk] = v;
447
+ }
392
448
  },
393
449
  enumerable: false,
394
450
  configurable: true,
@@ -396,14 +452,20 @@ export class MessageSchema {
396
452
  }
397
453
  continue;
398
454
  }
399
- if (f.altKey && !(f.altKey in obj)) {
455
+ if (f.altKey && !Object.hasOwn(obj, f.altKey)) {
400
456
  const pk = f.propKey;
457
+ const isProtoPk = Boolean(f.isProtoKey);
401
458
  Object.defineProperty(obj, f.altKey, {
402
459
  get() {
403
- return this[pk];
460
+ return isProtoPk ? getOwnProp(this, pk) : this[pk];
404
461
  },
405
462
  set(v) {
406
- this[pk] = v;
463
+ if (isProtoPk) {
464
+ setOwnProp(this, pk, v);
465
+ }
466
+ else {
467
+ this[pk] = v;
468
+ }
407
469
  },
408
470
  enumerable: false,
409
471
  configurable: true,
@@ -450,20 +512,28 @@ export class MessageSchema {
450
512
  if (f.oneof)
451
513
  continue;
452
514
  if (f.explicitPresence) {
453
- if (!(f.propKey in obj)) {
454
- obj[f.propKey] = undefined;
515
+ if (f.inObjectProto ? !Object.hasOwn(obj, f.propKey) : !(f.propKey in obj)) {
516
+ setFieldOnObj(obj, f, undefined);
455
517
  }
456
518
  continue;
457
519
  }
458
- if (obj[f.propKey] === undefined) {
459
- obj[f.propKey] = defaultFieldValue(f, nextCreating);
520
+ const cur = f.inObjectProto ? getOwnProp(obj, f.propKey) : obj[f.propKey];
521
+ if (cur === undefined) {
522
+ setFieldOnObj(obj, f, defaultFieldValue(f, nextCreating));
460
523
  }
461
524
  }
462
525
  }
463
526
  if (init) {
464
527
  for (const oneof of this.oneofs) {
465
- const initOneof = init[oneof.localName] ??
466
- (oneof.protoName !== oneof.localName ? init[oneof.protoName] : undefined);
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;
467
537
  if (initOneof && typeof initOneof === 'object' && 'case' in initOneof) {
468
538
  if (initOneof.case === undefined) {
469
539
  obj[oneof.localName] = { case: undefined };
@@ -482,10 +552,15 @@ export class MessageSchema {
482
552
  }
483
553
  }
484
554
  for (const f of this.fields) {
485
- const initVal = init[f.propKey] !== undefined
486
- ? init[f.propKey]
555
+ const pVal = f.inObjectProto
556
+ ? getOwnProp(init, f.propKey)
557
+ : init[f.propKey];
558
+ const initVal = pVal !== undefined
559
+ ? pVal
487
560
  : f.altKey !== undefined
488
- ? init[f.altKey]
561
+ ? f.altInObjectProto
562
+ ? getOwnProp(init, f.altKey)
563
+ : init[f.altKey]
489
564
  : undefined;
490
565
  if (initVal !== undefined) {
491
566
  if (f.oneof) {
@@ -495,7 +570,7 @@ export class MessageSchema {
495
570
  };
496
571
  }
497
572
  else {
498
- obj[f.propKey] = initFieldValue(f, initVal);
573
+ setFieldOnObj(obj, f, initFieldValue(f, initVal));
499
574
  }
500
575
  }
501
576
  }
@@ -539,10 +614,10 @@ export class MessageSchema {
539
614
  return;
540
615
  }
541
616
  if (f.explicitPresence) {
542
- msg[f.propKey] = undefined;
617
+ setFieldOnObj(msg, f, undefined);
543
618
  return;
544
619
  }
545
- msg[f.propKey] = defaultFieldValue(f);
620
+ setFieldOnObj(msg, f, defaultFieldValue(f));
546
621
  }
547
622
  /**
548
623
  * Deep-clones `msg`, including nested messages, arrays, maps, `oneof` groups, `Uint8Array` buffers, and `$unknown` fields.
@@ -561,10 +636,10 @@ export class MessageSchema {
561
636
  }
562
637
  }
563
638
  else if (f.explicitPresence) {
564
- out[f.propKey] = val === undefined ? undefined : cloneFieldValue(f, val);
639
+ setFieldOnObj(out, f, val === undefined ? undefined : cloneFieldValue(f, val));
565
640
  }
566
641
  else {
567
- out[f.propKey] = cloneFieldValue(f, val);
642
+ setFieldOnObj(out, f, cloneFieldValue(f, val));
568
643
  }
569
644
  }
570
645
  if (Array.isArray(src.$unknown) && src.$unknown.length > 0) {
@@ -650,7 +725,7 @@ export class MessageSchema {
650
725
  const visiting = new Set();
651
726
  const visited = new Set();
652
727
  const dfs = (node, depth) => {
653
- if (depth > MaxNestingDepth)
728
+ if (1 + depth * 2 > MaxNestingDepth)
654
729
  return true;
655
730
  if (visiting.has(node))
656
731
  return true;
@@ -677,11 +752,15 @@ export class MessageSchema {
677
752
  * For recursive schemas, follows the actual populated structure of `msg` when provided,
678
753
  * and terminates static cycle edges cleanly without exponential expansion.
679
754
  */
680
- getSchemaMeta(depth = 0, msg, ancestors) {
681
- if (depth === 0 && !msg && this.cachedMeta) {
755
+ getSchemaMeta(depth = 0, msg, ancestors, baseContainerDepth = 1) {
756
+ if (depth === 0 && baseContainerDepth === 1 && !msg && this.cachedMeta) {
682
757
  return this.cachedMeta;
683
758
  }
684
- if (depth === 0 && msg && !this.isCyclicOrDeep() && this.cachedMeta) {
759
+ if (depth === 0 &&
760
+ baseContainerDepth === 1 &&
761
+ msg &&
762
+ !this.isCyclicOrDeep() &&
763
+ this.cachedMeta) {
685
764
  return this.cachedMeta;
686
765
  }
687
766
  const fieldsMap = {};
@@ -689,9 +768,10 @@ export class MessageSchema {
689
768
  const pathAncestors = ancestors ?? new Set();
690
769
  const nextAncestors = new Set(pathAncestors);
691
770
  nextAncestors.add(this);
771
+ const schemaDepth = baseContainerDepth + depth * 2;
692
772
  for (const f of this.fields) {
693
773
  fieldsMap[f.fieldNum] = f.name;
694
- if (f.resolveSubSchema && depth < MaxNestingDepth) {
774
+ if (f.resolveSubSchema && schemaDepth + 3 <= MaxNestingDepth) {
695
775
  const sub = f.resolveSubSchema();
696
776
  if (!sub)
697
777
  continue;
@@ -699,10 +779,10 @@ export class MessageSchema {
699
779
  const rawVal = getFieldFromMsg(msg, f);
700
780
  if (f.effectiveType === 'submsg') {
701
781
  if (rawVal && typeof rawVal === 'object') {
702
- childrenMap[f.fieldNum] = sub.getSchemaMeta(depth + 1, rawVal, nextAncestors);
782
+ childrenMap[f.fieldNum] = sub.getSchemaMeta(depth + 1, rawVal, nextAncestors, baseContainerDepth);
703
783
  }
704
784
  else if (!pathAncestors.has(sub)) {
705
- childrenMap[f.fieldNum] = sub.getSchemaMeta(depth + 1, undefined, nextAncestors);
785
+ childrenMap[f.fieldNum] = sub.getSchemaMeta(depth + 1, undefined, nextAncestors, baseContainerDepth);
706
786
  }
707
787
  }
708
788
  else if (f.effectiveType === 'submsg[]') {
@@ -711,7 +791,7 @@ export class MessageSchema {
711
791
  for (let i = 0; i < rawVal.length; i++) {
712
792
  const elem = rawVal[i];
713
793
  if (elem && typeof elem === 'object') {
714
- merged = mergeSchemaMeta(merged, sub.getSchemaMeta(depth + 1, elem, nextAncestors));
794
+ merged = mergeSchemaMeta(merged, sub.getSchemaMeta(depth + 1, elem, nextAncestors, baseContainerDepth));
715
795
  }
716
796
  }
717
797
  if (merged) {
@@ -719,7 +799,7 @@ export class MessageSchema {
719
799
  }
720
800
  }
721
801
  else if (!pathAncestors.has(sub)) {
722
- childrenMap[f.fieldNum] = sub.getSchemaMeta(depth + 1, undefined, nextAncestors);
802
+ childrenMap[f.fieldNum] = sub.getSchemaMeta(depth + 1, undefined, nextAncestors, baseContainerDepth);
723
803
  }
724
804
  }
725
805
  else if (f.effectiveType === 'map' && f.mapValType === 'submsg') {
@@ -733,7 +813,7 @@ export class MessageSchema {
733
813
  for (let i = 0; i < vals.length; i++) {
734
814
  const v = vals[i];
735
815
  if (v && typeof v === 'object') {
736
- merged = mergeSchemaMeta(merged, sub.getSchemaMeta(depth + 1, v, nextAncestors));
816
+ merged = mergeSchemaMeta(merged, sub.getSchemaMeta(depth + 1, v, nextAncestors, baseContainerDepth));
737
817
  }
738
818
  }
739
819
  if (merged) {
@@ -741,12 +821,12 @@ export class MessageSchema {
741
821
  }
742
822
  }
743
823
  else if (!pathAncestors.has(sub)) {
744
- childrenMap[f.fieldNum] = sub.getSchemaMeta(depth + 1, undefined, nextAncestors);
824
+ childrenMap[f.fieldNum] = sub.getSchemaMeta(depth + 1, undefined, nextAncestors, baseContainerDepth);
745
825
  }
746
826
  }
747
827
  }
748
828
  else if (!pathAncestors.has(sub)) {
749
- childrenMap[f.fieldNum] = sub.getSchemaMeta(depth + 1, undefined, nextAncestors);
829
+ childrenMap[f.fieldNum] = sub.getSchemaMeta(depth + 1, undefined, nextAncestors, baseContainerDepth);
750
830
  }
751
831
  else {
752
832
  childrenMap[f.fieldNum] = sub.getShallowSchemaMeta();
@@ -760,23 +840,25 @@ export class MessageSchema {
760
840
  if (Object.keys(childrenMap).length > 0) {
761
841
  meta.children = childrenMap;
762
842
  }
763
- if (depth === 0 && !msg) {
843
+ if (depth === 0 && baseContainerDepth === 1 && !msg) {
764
844
  this.cachedMeta = meta;
765
845
  }
766
846
  return meta;
767
847
  }
848
+ getCachedField0Det() {
849
+ if (!this.cachedField0Det) {
850
+ this.cachedField0Det = encodeField0SchemaMeta(this.getSchemaMeta(0), true);
851
+ }
852
+ return this.cachedField0Det;
853
+ }
768
854
  /**
769
855
  * Returns pre-encoded deterministic Field #0 bytes.
770
856
  */
771
857
  getField0Bytes(deterministic = true) {
772
- if (deterministic && this.cachedField0Det) {
773
- return this.cachedField0Det;
774
- }
775
- const bytes = encodeField0SchemaMeta(this.getSchemaMeta(0), deterministic);
776
858
  if (deterministic) {
777
- this.cachedField0Det = bytes;
859
+ return this.getCachedField0Det().slice();
778
860
  }
779
- return bytes;
861
+ return encodeField0SchemaMeta(this.getSchemaMeta(0), false);
780
862
  }
781
863
  /**
782
864
  * Encodes `msg` into a new `Uint8Array`.
@@ -803,25 +885,33 @@ export class MessageSchema {
803
885
  encodeInto(enc, msg, options) {
804
886
  const deterministic = options?.deterministic ?? false;
805
887
  const writeUnknown = options?.writeUnknownFields ?? false;
806
- if (options?.selfDescribing) {
888
+ const selfDescribing = Boolean(options?.selfDescribing);
889
+ if (selfDescribing) {
807
890
  if (deterministic && !this.isCyclicOrDeep()) {
808
- enc.writeRaw(this.getField0Bytes(true));
891
+ enc.writeRaw(this.getCachedField0Det());
809
892
  }
810
893
  else {
811
- writeField0SchemaMeta(enc, this.getSchemaMeta(0, msg), deterministic);
894
+ writeField0SchemaMeta(enc, this.getSchemaMeta(0, msg, undefined, 1), deterministic);
812
895
  }
813
896
  }
814
- if (this.generatedHooks?.encode) {
897
+ if (this.generatedHooks?.encode && (!selfDescribing || !this.isCyclicOrDeep())) {
815
898
  this.generatedHooks.encode(enc, msg, options);
816
899
  return;
817
900
  }
818
- this.encodeBody(enc, msg, deterministic, writeUnknown);
901
+ const rootCoveredHops = Math.floor((MaxNestingDepth - 2) / 2);
902
+ this.encodeBody(enc, msg, deterministic, writeUnknown, selfDescribing, 0, rootCoveredHops);
819
903
  }
820
904
  /**
821
905
  * Internal single-pass body encoder (used for root and nested sub-messages).
822
906
  */
823
- encodeBody(enc, msg, deterministic, writeUnknown = false) {
824
- if (this.generatedHooks?.encode) {
907
+ encodeBody(enc, msg, deterministic, writeUnknown = false, selfDescribing = false, msgDepth = 0, coveredHops = 0) {
908
+ let activeCovered = coveredHops;
909
+ if (selfDescribing && activeCovered < 0 && msgDepth + 2 <= MaxNestingDepth) {
910
+ const baseDepth = msgDepth + 1;
911
+ writeField0SchemaMeta(enc, this.getSchemaMeta(0, msg, undefined, baseDepth), deterministic);
912
+ activeCovered = Math.floor((MaxNestingDepth - baseDepth - 1) / 2);
913
+ }
914
+ if (this.generatedHooks?.encode && (!selfDescribing || !this.isCyclicOrDeep())) {
825
915
  this.generatedHooks.encode(enc, msg, {
826
916
  deterministic,
827
917
  writeUnknownFields: writeUnknown,
@@ -850,10 +940,10 @@ export class MessageSchema {
850
940
  }
851
941
  enc.beginSubMsg(f.fieldNum);
852
942
  if (rawVal) {
853
- subSchema.encodeBody(enc, rawVal, deterministic, writeUnknown);
943
+ subSchema.encodeBody(enc, rawVal, deterministic, writeUnknown, selfDescribing, msgDepth + 1, activeCovered - 1);
854
944
  }
855
945
  else if (!f.optional) {
856
- subSchema.encodeBody(enc, subSchema.create(), deterministic, writeUnknown);
946
+ subSchema.encodeBody(enc, subSchema.create(), deterministic, writeUnknown, selfDescribing, msgDepth + 1, activeCovered - 1);
857
947
  }
858
948
  enc.endSubMsg();
859
949
  continue;
@@ -861,7 +951,7 @@ export class MessageSchema {
861
951
  if (f.omitEmpty && !f.oneof && !f.explicitPresence && isFieldValueEmpty(f, rawVal)) {
862
952
  continue;
863
953
  }
864
- encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown);
954
+ encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown, selfDescribing, msgDepth, activeCovered);
865
955
  }
866
956
  if (writeUnknown && msg && Array.isArray(msg.$unknown)) {
867
957
  const unknown = msg.$unknown;
@@ -918,16 +1008,24 @@ export class MessageSchema {
918
1008
  }
919
1009
  for (let i = 0; i < this.fields.length; i++) {
920
1010
  const f = this.fields[i];
1011
+ if (f.altKey && !f.altInObjectProto && Object.hasOwn(out, f.altKey)) {
1012
+ const desc = Object.getOwnPropertyDescriptor(out, f.altKey);
1013
+ if (desc && 'value' in desc) {
1014
+ delete out[f.altKey];
1015
+ }
1016
+ }
921
1017
  if (f.oneof)
922
1018
  continue;
923
1019
  if (f.explicitPresence) {
924
- out[f.propKey] = undefined;
1020
+ setFieldOnObj(out, f, undefined);
925
1021
  }
926
1022
  else {
927
- out[f.propKey] = defaultFieldValue(f, nextCreating);
1023
+ setFieldOnObj(out, f, defaultFieldValue(f, nextCreating));
928
1024
  }
929
1025
  }
930
- delete out.$unknown;
1026
+ if (Object.hasOwn(out, '$unknown')) {
1027
+ delete out.$unknown;
1028
+ }
931
1029
  }
932
1030
  }
933
1031
  const n = data.length;
@@ -999,7 +1097,18 @@ export class MessageSchema {
999
1097
  alreadySeen = true;
1000
1098
  seenSet.add(fieldNum);
1001
1099
  }
1002
- pos = decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, options);
1100
+ if (f.inObjectProto) {
1101
+ const box = Object.create(null);
1102
+ const cur = getOwnProp(out, f.propKey);
1103
+ if (cur !== undefined) {
1104
+ box[f.propKey] = cur;
1105
+ }
1106
+ pos = decodeCompiledFieldValue(data, pos, wt, f, box, alreadySeen, depth, maxDepth, options);
1107
+ setFieldOnObj(out, f, box[f.propKey]);
1108
+ }
1109
+ else {
1110
+ pos = decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, maxDepth, options);
1111
+ }
1003
1112
  }
1004
1113
  if (unknownFields && unknownFields.length > 0 && !('$unknown' in out)) {
1005
1114
  Object.defineProperty(out, '$unknown', {
@@ -1016,7 +1125,12 @@ function getFieldFromMsg(msg, f) {
1016
1125
  if (!msg)
1017
1126
  return undefined;
1018
1127
  if (f.oneof) {
1019
- const group = msg[f.oneof] ?? (f.oneofProtoName ? msg[f.oneofProtoName] : undefined);
1128
+ const group = msg[f.oneof] ??
1129
+ (f.oneofProtoName
1130
+ ? f.oneofProtoInObjectProto
1131
+ ? getOwnProp(msg, f.oneofProtoName)
1132
+ : msg[f.oneofProtoName]
1133
+ : undefined);
1020
1134
  if (group && typeof group === 'object' && 'case' in group) {
1021
1135
  if (group.case === f.localName || group.case === f.name || group.case === f.propKey) {
1022
1136
  return group.value;
@@ -1024,11 +1138,12 @@ function getFieldFromMsg(msg, f) {
1024
1138
  return undefined;
1025
1139
  }
1026
1140
  }
1027
- const v = msg[f.propKey];
1141
+ const v = f.inObjectProto ? getOwnProp(msg, f.propKey) : msg[f.propKey];
1028
1142
  if (v !== undefined)
1029
1143
  return v;
1030
- if (f.altKey !== undefined)
1031
- return msg[f.altKey];
1144
+ if (f.altKey !== undefined) {
1145
+ return f.altInObjectProto ? getOwnProp(msg, f.altKey) : msg[f.altKey];
1146
+ }
1032
1147
  return undefined;
1033
1148
  }
1034
1149
  function coerce64(val, useBigInt, unsigned) {
@@ -1177,6 +1292,8 @@ function bytesEqual(a, b) {
1177
1292
  function scalarValuesEqual(a, b) {
1178
1293
  if (a === b)
1179
1294
  return true;
1295
+ if (Number.isNaN(a) && Number.isNaN(b))
1296
+ return true;
1180
1297
  if ((typeof a === 'number' || typeof a === 'bigint') &&
1181
1298
  (typeof b === 'number' || typeof b === 'bigint')) {
1182
1299
  try {
@@ -1329,11 +1446,14 @@ function isFieldValueEmpty(f, val) {
1329
1446
  const sub = f.resolveSubSchema?.();
1330
1447
  return sub ? sub.isEmptyInstance(val) : true;
1331
1448
  }
1449
+ case 'float32':
1450
+ case 'float64':
1451
+ return Object.is(val, 0);
1332
1452
  default:
1333
1453
  return val === 0 || val === 0n;
1334
1454
  }
1335
1455
  }
1336
- function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
1456
+ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false, selfDescribing = false, msgDepth = 0, coveredHops = 0) {
1337
1457
  const fnum = f.fieldNum;
1338
1458
  switch (f.effectiveType) {
1339
1459
  case 'uint32':
@@ -1433,7 +1553,7 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
1433
1553
  for (let i = 0; i < items.length; i++) {
1434
1554
  enc.beginSubMsgElem();
1435
1555
  if (items[i] !== null && items[i] !== undefined) {
1436
- subSchema.encodeBody(enc, items[i], deterministic, writeUnknown);
1556
+ subSchema.encodeBody(enc, items[i], deterministic, writeUnknown, selfDescribing, msgDepth + 2, coveredHops - 1);
1437
1557
  }
1438
1558
  enc.endSubMsgElem();
1439
1559
  }
@@ -1441,7 +1561,7 @@ function encodeFieldValue(enc, f, rawVal, deterministic, writeUnknown = false) {
1441
1561
  break;
1442
1562
  }
1443
1563
  case 'map': {
1444
- encodeMapField(enc, f, rawVal, deterministic, writeUnknown);
1564
+ encodeMapField(enc, f, rawVal, deterministic, writeUnknown, selfDescribing, msgDepth, coveredHops);
1445
1565
  break;
1446
1566
  }
1447
1567
  }
@@ -1525,7 +1645,7 @@ function compareMapKeys(keyType, a, b) {
1525
1645
  }
1526
1646
  }
1527
1647
  }
1528
- function encodeMapField(enc, f, rawVal, deterministic, writeUnknown = false) {
1648
+ function encodeMapField(enc, f, rawVal, deterministic, writeUnknown = false, selfDescribing = false, msgDepth = 0, coveredHops = 0) {
1529
1649
  const keyWT = f.mapKeyWT ?? WireString;
1530
1650
  const valWT = f.mapValWT ?? WireUint;
1531
1651
  const keyType = f.mapKeyType ?? 'string';
@@ -1550,7 +1670,7 @@ function encodeMapField(enc, f, rawVal, deterministic, writeUnknown = false) {
1550
1670
  if (valType === 'submsg') {
1551
1671
  enc.beginContainer();
1552
1672
  if (v && subSchema) {
1553
- subSchema.encodeBody(enc, v, deterministic, writeUnknown);
1673
+ subSchema.encodeBody(enc, v, deterministic, writeUnknown, selfDescribing, msgDepth + 2, coveredHops - 1);
1554
1674
  }
1555
1675
  enc.endContainer();
1556
1676
  }
@@ -1560,9 +1680,12 @@ function encodeMapField(enc, f, rawVal, deterministic, writeUnknown = false) {
1560
1680
  }
1561
1681
  enc.endContainer();
1562
1682
  }
1563
- function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, options) {
1683
+ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, maxDepth, options) {
1564
1684
  const key = f.propKey;
1565
1685
  const n = data.length;
1686
+ if ((wt === WireArray || wt === WireMap) && depth + 1 > maxDepth) {
1687
+ throw new Error(`omnibuf: nesting depth ${depth + 1} exceeds MaxNestingDepth (${maxDepth})`);
1688
+ }
1566
1689
  switch (f.effectiveType) {
1567
1690
  case 'uint32': {
1568
1691
  if (pos < n && (data[pos] & 7) !== 0) {
@@ -1592,13 +1715,13 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1592
1715
  const [v, vn] = decodeVarint(data, pos);
1593
1716
  if (vn === 0)
1594
1717
  throw new Error(`omnibuf: truncated int32 at ${pos}`);
1595
- out[key] = decodeZigZag(v) | 0;
1718
+ out[key] = decodeZigZag32(v);
1596
1719
  return pos + vn;
1597
1720
  }
1598
1721
  const [v, vn] = decodeVarintBigInt(data, pos);
1599
1722
  if (vn === 0)
1600
1723
  throw new Error(`omnibuf: truncated int32 at ${pos}`);
1601
- out[key] = decodeZigZag(v) | 0;
1724
+ out[key] = decodeZigZag32(v);
1602
1725
  return pos + vn;
1603
1726
  }
1604
1727
  case 'sint64':
@@ -1735,7 +1858,15 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1735
1858
  return nextPos;
1736
1859
  }
1737
1860
  case 'sint32[]':
1738
- case 'int32[]':
1861
+ case 'int32[]': {
1862
+ const [body, nextPos] = readLenSlice(data, pos, 2);
1863
+ const prev = alreadySeen && Array.isArray(out[key]) ? out[key] : [];
1864
+ const [arr, ok] = appendEnumArrBody(prev, body);
1865
+ if (!ok)
1866
+ throw new Error(`omnibuf: invalid sint32 array at ${pos}`);
1867
+ out[key] = arr;
1868
+ return nextPos;
1869
+ }
1739
1870
  case 'sint64[]':
1740
1871
  case 'int64[]': {
1741
1872
  const [body, nextPos] = readLenSlice(data, pos, 2);
@@ -1744,8 +1875,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1744
1875
  const [arr, ok] = appendSintArrAutoBody(prev, body);
1745
1876
  if (!ok)
1746
1877
  throw new Error(`omnibuf: invalid sint array at ${pos}`);
1747
- const is64 = f.effectiveType === 'sint64[]' || f.effectiveType === 'int64[]';
1748
- if (f.useBigInt && is64) {
1878
+ if (f.useBigInt) {
1749
1879
  for (let i = startLen; i < arr.length; i++) {
1750
1880
  arr[i] = BigInt(arr[i]);
1751
1881
  }
@@ -1864,7 +1994,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1864
1994
  const [el, en] = decodeVarintBigInt(arrPayload, p);
1865
1995
  p += en;
1866
1996
  const elNum = Number(el);
1867
- const elemObj = subSchema.decode(arrPayload.subarray(p, p + elNum), subSchema.create(), depth + 1, options);
1997
+ const elemObj = subSchema.decode(arrPayload.subarray(p, p + elNum), subSchema.create(), depth + 2, options);
1868
1998
  elems.push(elemObj);
1869
1999
  p += elNum;
1870
2000
  }
@@ -1873,7 +2003,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
1873
2003
  }
1874
2004
  case 'map': {
1875
2005
  const [body, nextPos] = readLenSlice(data, pos, 3);
1876
- decodeMapFieldBody(f, out, body, alreadySeen, depth, options);
2006
+ decodeMapFieldBody(f, out, body, alreadySeen, depth + 1, options);
1877
2007
  return nextPos;
1878
2008
  }
1879
2009
  }
@@ -1969,6 +2099,12 @@ function decodeMapFieldBody(f, out, body, alreadySeen, depth, options) {
1969
2099
  targetMap = alreadySeen && existing && typeof existing === 'object' ? existing : {};
1970
2100
  out[f.propKey] = targetMap;
1971
2101
  }
2102
+ const isKey64 = f.useBigInt &&
2103
+ (f.mapKeyType === 'uint64' ||
2104
+ f.mapKeyType === 'sint64' ||
2105
+ f.mapKeyType === 'int64' ||
2106
+ f.mapKeyType === 'fixed64' ||
2107
+ f.mapKeyType === 'sfixed64');
1972
2108
  const isVal64 = f.useBigInt &&
1973
2109
  (f.mapValType === 'uint64' ||
1974
2110
  f.mapValType === 'sint64' ||
@@ -2001,7 +2137,7 @@ function decodeMapFieldBody(f, out, body, alreadySeen, depth, options) {
2001
2137
  return v;
2002
2138
  };
2003
2139
  const vv = convert(isVal64 ? BigInt(rawVv) : rawVv, f.mapValType);
2004
- const key = convert(kv, f.mapKeyType);
2140
+ const key = convert(isKey64 ? BigInt(kv) : kv, f.mapKeyType);
2005
2141
  p = nextP2;
2006
2142
  if (targetMap instanceof Map) {
2007
2143
  targetMap.set(key, vv);
@@ -2056,7 +2192,7 @@ export function omnibufField(fieldNum, options = {}) {
2056
2192
  if (contextOrPropKey.metadata) {
2057
2193
  const metaObj = contextOrPropKey.metadata;
2058
2194
  if (!metaObj[PENDING_FIELDS_SYMBOL]) {
2059
- metaObj[PENDING_FIELDS_SYMBOL] = {};
2195
+ metaObj[PENDING_FIELDS_SYMBOL] = Object.create(null);
2060
2196
  }
2061
2197
  metaObj[PENDING_FIELDS_SYMBOL][propKey] = spec;
2062
2198
  }
@@ -2067,7 +2203,7 @@ export function omnibufField(fieldNum, options = {}) {
2067
2203
  if (ctor) {
2068
2204
  let map = classPendingFields.get(ctor);
2069
2205
  if (!map) {
2070
- map = {};
2206
+ map = Object.create(null);
2071
2207
  classPendingFields.set(ctor, map);
2072
2208
  }
2073
2209
  map[propKey] = spec;
@@ -2082,7 +2218,7 @@ export function omnibufField(fieldNum, options = {}) {
2082
2218
  if (ctor) {
2083
2219
  let map = classPendingFields.get(ctor);
2084
2220
  if (!map) {
2085
- map = {};
2221
+ map = Object.create(null);
2086
2222
  classPendingFields.set(ctor, map);
2087
2223
  }
2088
2224
  map[propKey] = spec;
@@ -2104,7 +2240,7 @@ export function omnibufMessage(typeName) {
2104
2240
  if (metadataFields) {
2105
2241
  let map = classPendingFields.get(target);
2106
2242
  if (!map) {
2107
- map = {};
2243
+ map = Object.create(null);
2108
2244
  classPendingFields.set(target, map);
2109
2245
  }
2110
2246
  Object.assign(map, metadataFields);