@omnibuf/core 0.0.8 → 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/blind.d.ts +3 -3
- package/dist/blind.d.ts.map +1 -1
- package/dist/blind.js +191 -31
- package/dist/blind.js.map +1 -1
- package/dist/encoder.d.ts +5 -5
- package/dist/encoder.d.ts.map +1 -1
- package/dist/encoder.js +25 -4
- package/dist/encoder.js.map +1 -1
- package/dist/protobuf-es.d.ts +2 -0
- package/dist/protobuf-es.d.ts.map +1 -1
- package/dist/protobuf-es.js +40 -8
- package/dist/protobuf-es.js.map +1 -1
- package/dist/readers.d.ts +4 -3
- package/dist/readers.d.ts.map +1 -1
- package/dist/readers.js +405 -120
- package/dist/readers.js.map +1 -1
- package/dist/schema.d.ts +9 -4
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +202 -79
- package/dist/schema.js.map +1 -1
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +16 -14
- package/dist/validation.js.map +1 -1
- package/dist/wire.d.ts +16 -3
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +303 -23
- package/dist/wire.js.map +1 -1
- package/package.json +1 -1
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,
|
|
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([
|
|
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
|
|
330
|
-
obj
|
|
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
|
|
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
|
-
|
|
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 (!
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
459
|
-
|
|
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
|
|
466
|
-
(
|
|
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
|
|
486
|
-
? init
|
|
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
|
-
?
|
|
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
|
|
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
|
|
617
|
+
setFieldOnObj(msg, f, undefined);
|
|
543
618
|
return;
|
|
544
619
|
}
|
|
545
|
-
msg
|
|
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
|
|
639
|
+
setFieldOnObj(out, f, val === undefined ? undefined : cloneFieldValue(f, val));
|
|
565
640
|
}
|
|
566
641
|
else {
|
|
567
|
-
out
|
|
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 &&
|
|
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 &&
|
|
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.
|
|
859
|
+
return this.getCachedField0Det().slice();
|
|
778
860
|
}
|
|
779
|
-
return
|
|
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
|
-
|
|
888
|
+
const selfDescribing = Boolean(options?.selfDescribing);
|
|
889
|
+
if (selfDescribing) {
|
|
807
890
|
if (deterministic && !this.isCyclicOrDeep()) {
|
|
808
|
-
enc.writeRaw(this.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
1020
|
+
setFieldOnObj(out, f, undefined);
|
|
925
1021
|
}
|
|
926
1022
|
else {
|
|
927
|
-
out
|
|
1023
|
+
setFieldOnObj(out, f, defaultFieldValue(f, nextCreating));
|
|
928
1024
|
}
|
|
929
1025
|
}
|
|
930
|
-
|
|
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
|
-
|
|
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] ??
|
|
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] =
|
|
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] =
|
|
1724
|
+
out[key] = decodeZigZag32(v);
|
|
1602
1725
|
return pos + vn;
|
|
1603
1726
|
}
|
|
1604
1727
|
case 'sint64':
|
|
@@ -1871,7 +1994,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1871
1994
|
const [el, en] = decodeVarintBigInt(arrPayload, p);
|
|
1872
1995
|
p += en;
|
|
1873
1996
|
const elNum = Number(el);
|
|
1874
|
-
const elemObj = subSchema.decode(arrPayload.subarray(p, p + elNum), subSchema.create(), depth +
|
|
1997
|
+
const elemObj = subSchema.decode(arrPayload.subarray(p, p + elNum), subSchema.create(), depth + 2, options);
|
|
1875
1998
|
elems.push(elemObj);
|
|
1876
1999
|
p += elNum;
|
|
1877
2000
|
}
|
|
@@ -1880,7 +2003,7 @@ function decodeCompiledFieldValue(data, pos, wt, f, out, alreadySeen, depth, opt
|
|
|
1880
2003
|
}
|
|
1881
2004
|
case 'map': {
|
|
1882
2005
|
const [body, nextPos] = readLenSlice(data, pos, 3);
|
|
1883
|
-
decodeMapFieldBody(f, out, body, alreadySeen, depth, options);
|
|
2006
|
+
decodeMapFieldBody(f, out, body, alreadySeen, depth + 1, options);
|
|
1884
2007
|
return nextPos;
|
|
1885
2008
|
}
|
|
1886
2009
|
}
|
|
@@ -2069,7 +2192,7 @@ export function omnibufField(fieldNum, options = {}) {
|
|
|
2069
2192
|
if (contextOrPropKey.metadata) {
|
|
2070
2193
|
const metaObj = contextOrPropKey.metadata;
|
|
2071
2194
|
if (!metaObj[PENDING_FIELDS_SYMBOL]) {
|
|
2072
|
-
metaObj[PENDING_FIELDS_SYMBOL] =
|
|
2195
|
+
metaObj[PENDING_FIELDS_SYMBOL] = Object.create(null);
|
|
2073
2196
|
}
|
|
2074
2197
|
metaObj[PENDING_FIELDS_SYMBOL][propKey] = spec;
|
|
2075
2198
|
}
|
|
@@ -2080,7 +2203,7 @@ export function omnibufField(fieldNum, options = {}) {
|
|
|
2080
2203
|
if (ctor) {
|
|
2081
2204
|
let map = classPendingFields.get(ctor);
|
|
2082
2205
|
if (!map) {
|
|
2083
|
-
map =
|
|
2206
|
+
map = Object.create(null);
|
|
2084
2207
|
classPendingFields.set(ctor, map);
|
|
2085
2208
|
}
|
|
2086
2209
|
map[propKey] = spec;
|
|
@@ -2095,7 +2218,7 @@ export function omnibufField(fieldNum, options = {}) {
|
|
|
2095
2218
|
if (ctor) {
|
|
2096
2219
|
let map = classPendingFields.get(ctor);
|
|
2097
2220
|
if (!map) {
|
|
2098
|
-
map =
|
|
2221
|
+
map = Object.create(null);
|
|
2099
2222
|
classPendingFields.set(ctor, map);
|
|
2100
2223
|
}
|
|
2101
2224
|
map[propKey] = spec;
|
|
@@ -2117,7 +2240,7 @@ export function omnibufMessage(typeName) {
|
|
|
2117
2240
|
if (metadataFields) {
|
|
2118
2241
|
let map = classPendingFields.get(target);
|
|
2119
2242
|
if (!map) {
|
|
2120
|
-
map =
|
|
2243
|
+
map = Object.create(null);
|
|
2121
2244
|
classPendingFields.set(target, map);
|
|
2122
2245
|
}
|
|
2123
2246
|
Object.assign(map, metadataFields);
|