protobufjs 7.5.0 → 7.5.2

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.
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * protobuf.js v7.5.0 (c) 2016, daniel wirtz
3
- * compiled tue, 15 apr 2025 17:05:18 utc
2
+ * protobuf.js v7.5.2 (c) 2016, daniel wirtz
3
+ * compiled wed, 14 may 2025 19:25:30 utc
4
4
  * licensed under the bsd-3-clause license
5
5
  * see: https://github.com/dcodeio/protobuf.js for details
6
6
  */
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * protobuf.js v7.5.0 (c) 2016, daniel wirtz
3
- * compiled tue, 15 apr 2025 17:05:18 utc
2
+ * protobuf.js v7.5.2 (c) 2016, daniel wirtz
3
+ * compiled wed, 14 may 2025 19:25:30 utc
4
4
  * licensed under the bsd-3-clause license
5
5
  * see: https://github.com/dcodeio/protobuf.js for details
6
6
  */
package/dist/protobuf.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * protobuf.js v7.5.0 (c) 2016, daniel wirtz
3
- * compiled tue, 15 apr 2025 17:05:18 utc
2
+ * protobuf.js v7.5.2 (c) 2016, daniel wirtz
3
+ * compiled wed, 14 may 2025 19:25:30 utc
4
4
  * licensed under the bsd-3-clause license
5
5
  * see: https://github.com/dcodeio/protobuf.js for details
6
6
  */
@@ -2641,12 +2641,18 @@ Field.prototype._inferLegacyProtoFeatures = function _inferLegacyProtoFeatures(e
2641
2641
  }
2642
2642
 
2643
2643
  var features = {};
2644
- this.resolve();
2644
+
2645
2645
  if (this.rule === "required") {
2646
2646
  features.field_presence = "LEGACY_REQUIRED";
2647
2647
  }
2648
- if (this.resolvedType instanceof Type && this.resolvedType.group) {
2649
- features.message_encoding = "DELIMITED";
2648
+ if (this.parent && types.defaults[this.type] === undefined) {
2649
+ // We can't use resolvedType because types may not have been resolved yet. However,
2650
+ // legacy groups are always in the same scope as the field so we don't have to do a
2651
+ // full scan of the tree.
2652
+ var type = this.parent.get(this.type.split(".").pop());
2653
+ if (type && type instanceof Type && type.group) {
2654
+ features.message_encoding = "DELIMITED";
2655
+ }
2650
2656
  }
2651
2657
  if (this.getOption("packed") === true) {
2652
2658
  features.repeated_field_encoding = "PACKED";
@@ -3416,10 +3422,40 @@ function Namespace(name, options) {
3416
3422
  * @private
3417
3423
  */
3418
3424
  this._nestedArray = null;
3425
+
3426
+ /**
3427
+ * Cache lookup calls for any objects contains anywhere under this namespace.
3428
+ * This drastically speeds up resolve for large cross-linked protos where the same
3429
+ * types are looked up repeatedly.
3430
+ * @type {Object.<string,ReflectionObject|null>}
3431
+ * @private
3432
+ */
3433
+ this._lookupCache = {};
3434
+
3435
+ /**
3436
+ * Whether or not objects contained in this namespace need feature resolution.
3437
+ * @type {boolean}
3438
+ * @protected
3439
+ */
3440
+ this._needsRecursiveFeatureResolution = true;
3441
+
3442
+ /**
3443
+ * Whether or not objects contained in this namespace need a resolve.
3444
+ * @type {boolean}
3445
+ * @protected
3446
+ */
3447
+ this._needsRecursiveResolve = true;
3419
3448
  }
3420
3449
 
3421
3450
  function clearCache(namespace) {
3422
3451
  namespace._nestedArray = null;
3452
+ namespace._lookupCache = {};
3453
+
3454
+ // Also clear parent caches, since they include nested lookups.
3455
+ var parent = namespace;
3456
+ while(parent = parent.parent) {
3457
+ parent._lookupCache = {};
3458
+ }
3423
3459
  return namespace;
3424
3460
  }
3425
3461
 
@@ -3557,6 +3593,16 @@ Namespace.prototype.add = function add(object) {
3557
3593
  }
3558
3594
  }
3559
3595
 
3596
+ this._needsRecursiveFeatureResolution = true;
3597
+ this._needsRecursiveResolve = true;
3598
+
3599
+ // Also clear parent caches, since they need to recurse down.
3600
+ var parent = this;
3601
+ while(parent = parent.parent) {
3602
+ parent._needsRecursiveFeatureResolution = true;
3603
+ parent._needsRecursiveResolve = true;
3604
+ }
3605
+
3560
3606
  object.onAdd(this);
3561
3607
  return clearCache(this);
3562
3608
  };
@@ -3618,6 +3664,8 @@ Namespace.prototype.define = function define(path, json) {
3618
3664
  * @returns {Namespace} `this`
3619
3665
  */
3620
3666
  Namespace.prototype.resolveAll = function resolveAll() {
3667
+ if (!this._needsRecursiveResolve) return this;
3668
+
3621
3669
  var nested = this.nestedArray, i = 0;
3622
3670
  this.resolve();
3623
3671
  while (i < nested.length)
@@ -3625,6 +3673,7 @@ Namespace.prototype.resolveAll = function resolveAll() {
3625
3673
  nested[i++].resolveAll();
3626
3674
  else
3627
3675
  nested[i++].resolve();
3676
+ this._needsRecursiveResolve = false;
3628
3677
  return this;
3629
3678
  };
3630
3679
 
@@ -3632,6 +3681,9 @@ Namespace.prototype.resolveAll = function resolveAll() {
3632
3681
  * @override
3633
3682
  */
3634
3683
  Namespace.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
3684
+ if (!this._needsRecursiveFeatureResolution) return this;
3685
+ this._needsRecursiveFeatureResolution = false;
3686
+
3635
3687
  edition = this._edition || edition;
3636
3688
 
3637
3689
  ReflectionObject.prototype._resolveFeaturesRecursive.call(this, edition);
@@ -3649,7 +3701,6 @@ Namespace.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursi
3649
3701
  * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
3650
3702
  */
3651
3703
  Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChecked) {
3652
-
3653
3704
  /* istanbul ignore next */
3654
3705
  if (typeof filterTypes === "boolean") {
3655
3706
  parentAlreadyChecked = filterTypes;
@@ -3664,29 +3715,72 @@ Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChe
3664
3715
  } else if (!path.length)
3665
3716
  return this;
3666
3717
 
3718
+ var flatPath = path.join(".");
3719
+
3667
3720
  // Start at root if path is absolute
3668
3721
  if (path[0] === "")
3669
3722
  return this.root.lookup(path.slice(1), filterTypes);
3670
3723
 
3724
+ // Early bailout for objects with matching absolute paths
3725
+ var found = this.root._fullyQualifiedObjects["." + flatPath];
3726
+ if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
3727
+ return found;
3728
+ }
3729
+
3730
+ // Do a regular lookup at this namespace and below
3731
+ found = this._lookupImpl(path, flatPath);
3732
+ if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
3733
+ return found;
3734
+ }
3735
+
3736
+ if (parentAlreadyChecked)
3737
+ return null;
3738
+
3739
+ // If there hasn't been a match, walk up the tree and look more broadly
3740
+ var current = this;
3741
+ while (current.parent) {
3742
+ found = current.parent._lookupImpl(path, flatPath);
3743
+ if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
3744
+ return found;
3745
+ }
3746
+ current = current.parent;
3747
+ }
3748
+ return null;
3749
+ };
3750
+
3751
+ /**
3752
+ * Internal helper for lookup that handles searching just at this namespace and below along with caching.
3753
+ * @param {string[]} path Path to look up
3754
+ * @param {string} flatPath Flattened version of the path to use as a cache key
3755
+ * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
3756
+ * @private
3757
+ */
3758
+ Namespace.prototype._lookupImpl = function lookup(path, flatPath) {
3759
+ if(Object.prototype.hasOwnProperty.call(this._lookupCache, flatPath)) {
3760
+ return this._lookupCache[flatPath];
3761
+ }
3762
+
3671
3763
  // Test if the first part matches any nested object, and if so, traverse if path contains more
3672
3764
  var found = this.get(path[0]);
3765
+ var exact = null;
3673
3766
  if (found) {
3674
3767
  if (path.length === 1) {
3675
- if (!filterTypes || filterTypes.indexOf(found.constructor) > -1)
3676
- return found;
3677
- } else if (found instanceof Namespace && (found = found.lookup(path.slice(1), filterTypes, true)))
3678
- return found;
3768
+ exact = found;
3769
+ } else if (found instanceof Namespace) {
3770
+ path = path.slice(1);
3771
+ exact = found._lookupImpl(path, path.join("."));
3772
+ }
3679
3773
 
3680
3774
  // Otherwise try each nested namespace
3681
- } else
3775
+ } else {
3682
3776
  for (var i = 0; i < this.nestedArray.length; ++i)
3683
- if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i].lookup(path, filterTypes, true)))
3684
- return found;
3777
+ if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i]._lookupImpl(path, flatPath)))
3778
+ exact = found;
3779
+ }
3685
3780
 
3686
- // If there hasn't been a match, try again at the parent
3687
- if (this.parent === null || parentAlreadyChecked)
3688
- return null;
3689
- return this.parent.lookup(path, filterTypes);
3781
+ // Set this even when null, so that when we walk up the tree we can quickly bail on repeated checks back down.
3782
+ this._lookupCache[flatPath] = exact;
3783
+ return exact;
3690
3784
  };
3691
3785
 
3692
3786
  /**
@@ -3817,6 +3911,7 @@ function ReflectionObject(name, options) {
3817
3911
  /**
3818
3912
  * The edition specified for this object. Only relevant for top-level objects.
3819
3913
  * @type {string}
3914
+ * @private
3820
3915
  */
3821
3916
  this._edition = null;
3822
3917
 
@@ -3824,15 +3919,24 @@ function ReflectionObject(name, options) {
3824
3919
  * The default edition to use for this object if none is specified. For legacy reasons,
3825
3920
  * this is proto2 except in the JSON parsing case where it was proto3.
3826
3921
  * @type {string}
3922
+ * @private
3827
3923
  */
3828
3924
  this._defaultEdition = "proto2";
3829
3925
 
3830
3926
  /**
3831
3927
  * Resolved Features.
3832
3928
  * @type {object}
3929
+ * @private
3833
3930
  */
3834
3931
  this._features = {};
3835
3932
 
3933
+ /**
3934
+ * Whether or not features have been resolved.
3935
+ * @type {boolean}
3936
+ * @private
3937
+ */
3938
+ this._featuresResolved = false;
3939
+
3836
3940
  /**
3837
3941
  * Parent namespace.
3838
3942
  * @type {Namespace|null}
@@ -3938,10 +4042,8 @@ ReflectionObject.prototype.onRemove = function onRemove(parent) {
3938
4042
  ReflectionObject.prototype.resolve = function resolve() {
3939
4043
  if (this.resolved)
3940
4044
  return this;
3941
- if (this instanceof Root) {
3942
- this._resolveFeaturesRecursive(this._edition);
3943
- this.resolved = true;
3944
- }
4045
+ if (this.root instanceof Root)
4046
+ this.resolved = true; // only if part of a root
3945
4047
  return this;
3946
4048
  };
3947
4049
 
@@ -3960,6 +4062,10 @@ ReflectionObject.prototype._resolveFeaturesRecursive = function _resolveFeatures
3960
4062
  * @returns {undefined}
3961
4063
  */
3962
4064
  ReflectionObject.prototype._resolveFeatures = function _resolveFeatures(edition) {
4065
+ if (this._featuresResolved) {
4066
+ return;
4067
+ }
4068
+
3963
4069
  var defaults = {};
3964
4070
 
3965
4071
  /* istanbul ignore if */
@@ -3983,6 +4089,7 @@ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures(edition)
3983
4089
  throw new Error("Unknown edition: " + edition);
3984
4090
  }
3985
4091
  this._features = Object.assign(defaults, protoFeatures || {});
4092
+ this._featuresResolved = true;
3986
4093
  return;
3987
4094
  }
3988
4095
 
@@ -4004,6 +4111,7 @@ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures(edition)
4004
4111
  // Sister fields should have the same features as their extensions.
4005
4112
  this.extensionField._features = this._features;
4006
4113
  }
4114
+ this._featuresResolved = true;
4007
4115
  };
4008
4116
 
4009
4117
  /**
@@ -4879,6 +4987,10 @@ function parse(source, root, options) {
4879
4987
  parseEnum(type, token);
4880
4988
  break;
4881
4989
 
4990
+ case "reserved":
4991
+ readRanges(type.reserved || (type.reserved = []), true);
4992
+ break;
4993
+
4882
4994
  /* istanbul ignore next */
4883
4995
  default:
4884
4996
  throw illegal(token); // there are no groups with proto3 semantics
@@ -5830,8 +5942,19 @@ function Root(options) {
5830
5942
  */
5831
5943
  this.files = [];
5832
5944
 
5833
- // Default to proto2 if unspecified.
5945
+ /**
5946
+ * Edition, defaults to proto2 if unspecified.
5947
+ * @type {string}
5948
+ * @private
5949
+ */
5834
5950
  this._edition = "proto2";
5951
+
5952
+ /**
5953
+ * Global lookup cache of fully qualified names.
5954
+ * @type {Object.<string,ReflectionObject>}
5955
+ * @private
5956
+ */
5957
+ this._fullyQualifiedObjects = {};
5835
5958
  }
5836
5959
 
5837
5960
  /**
@@ -5893,6 +6016,9 @@ Root.prototype.load = function load(filename, options, callback) {
5893
6016
 
5894
6017
  // Finishes loading by calling the callback (exactly once)
5895
6018
  function finish(err, root) {
6019
+ if (root) {
6020
+ root.resolveAll();
6021
+ }
5896
6022
  /* istanbul ignore if */
5897
6023
  if (!callback) {
5898
6024
  return;
@@ -5902,9 +6028,6 @@ Root.prototype.load = function load(filename, options, callback) {
5902
6028
  }
5903
6029
  var cb = callback;
5904
6030
  callback = null;
5905
- if (root) {
5906
- root.resolveAll();
5907
- }
5908
6031
  cb(err, root);
5909
6032
  }
5910
6033
 
@@ -6012,8 +6135,8 @@ Root.prototype.load = function load(filename, options, callback) {
6012
6135
  for (var i = 0, resolved; i < filename.length; ++i)
6013
6136
  if (resolved = self.resolvePath("", filename[i]))
6014
6137
  fetch(resolved);
6015
- self.resolveAll();
6016
6138
  if (sync) {
6139
+ self.resolveAll();
6017
6140
  return self;
6018
6141
  }
6019
6142
  if (!queued) {
@@ -6062,10 +6185,13 @@ Root.prototype.loadSync = function loadSync(filename, options) {
6062
6185
  * @override
6063
6186
  */
6064
6187
  Root.prototype.resolveAll = function resolveAll() {
6188
+ if (!this._needsRecursiveResolve) return this;
6189
+
6065
6190
  if (this.deferred.length)
6066
6191
  throw Error("unresolvable extensions: " + this.deferred.map(function(field) {
6067
6192
  return "'extend " + field.extend + "' in " + field.parent.fullName;
6068
6193
  }).join(", "));
6194
+ this._resolveFeaturesRecursive(this._edition);
6069
6195
  return Namespace.prototype.resolveAll.call(this);
6070
6196
  };
6071
6197
 
@@ -6128,6 +6254,11 @@ Root.prototype._handleAdd = function _handleAdd(object) {
6128
6254
  object.parent[object.name] = object; // expose namespace as property of its parent
6129
6255
  }
6130
6256
 
6257
+ if (object instanceof Type || object instanceof Enum || object instanceof Field) {
6258
+ // Only store types and enums for quick lookup during resolve.
6259
+ this._fullyQualifiedObjects[object.fullName] = object;
6260
+ }
6261
+
6131
6262
  // The above also adds uppercased (and thus conflict-free) nested types, services and enums as
6132
6263
  // properties of namespaces just like static code does. This allows using a .d.ts generated for
6133
6264
  // a static module with reflection-based solutions where the condition is met.
@@ -6168,6 +6299,8 @@ Root.prototype._handleRemove = function _handleRemove(object) {
6168
6299
  delete object.parent[object.name]; // unexpose namespaces
6169
6300
 
6170
6301
  }
6302
+
6303
+ delete this._fullyQualifiedObjects[object.fullName];
6171
6304
  };
6172
6305
 
6173
6306
  // Sets up cyclic dependencies (called in index-light)
@@ -6492,6 +6625,8 @@ Service.prototype.get = function get(name) {
6492
6625
  * @override
6493
6626
  */
6494
6627
  Service.prototype.resolveAll = function resolveAll() {
6628
+ if (!this._needsRecursiveResolve) return this;
6629
+
6495
6630
  Namespace.prototype.resolve.call(this);
6496
6631
  var methods = this.methodsArray;
6497
6632
  for (var i = 0; i < methods.length; ++i)
@@ -6503,6 +6638,8 @@ Service.prototype.resolveAll = function resolveAll() {
6503
6638
  * @override
6504
6639
  */
6505
6640
  Service.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
6641
+ if (!this._needsRecursiveFeatureResolution) return this;
6642
+
6506
6643
  edition = this._edition || edition;
6507
6644
 
6508
6645
  Namespace.prototype._resolveFeaturesRecursive.call(this, edition);
@@ -7290,6 +7427,8 @@ Type.prototype.toJSON = function toJSON(toJSONOptions) {
7290
7427
  * @override
7291
7428
  */
7292
7429
  Type.prototype.resolveAll = function resolveAll() {
7430
+ if (!this._needsRecursiveResolve) return this;
7431
+
7293
7432
  Namespace.prototype.resolveAll.call(this);
7294
7433
  var oneofs = this.oneofsArray; i = 0;
7295
7434
  while (i < oneofs.length)
@@ -7304,6 +7443,8 @@ Type.prototype.resolveAll = function resolveAll() {
7304
7443
  * @override
7305
7444
  */
7306
7445
  Type.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
7446
+ if (!this._needsRecursiveFeatureResolution) return this;
7447
+
7307
7448
  edition = this._edition || edition;
7308
7449
 
7309
7450
  Namespace.prototype._resolveFeaturesRecursive.call(this, edition);