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
  */
@@ -2240,12 +2240,18 @@ Field.prototype._inferLegacyProtoFeatures = function _inferLegacyProtoFeatures(e
2240
2240
  }
2241
2241
 
2242
2242
  var features = {};
2243
- this.resolve();
2243
+
2244
2244
  if (this.rule === "required") {
2245
2245
  features.field_presence = "LEGACY_REQUIRED";
2246
2246
  }
2247
- if (this.resolvedType instanceof Type && this.resolvedType.group) {
2248
- features.message_encoding = "DELIMITED";
2247
+ if (this.parent && types.defaults[this.type] === undefined) {
2248
+ // We can't use resolvedType because types may not have been resolved yet. However,
2249
+ // legacy groups are always in the same scope as the field so we don't have to do a
2250
+ // full scan of the tree.
2251
+ var type = this.parent.get(this.type.split(".").pop());
2252
+ if (type && type instanceof Type && type.group) {
2253
+ features.message_encoding = "DELIMITED";
2254
+ }
2249
2255
  }
2250
2256
  if (this.getOption("packed") === true) {
2251
2257
  features.repeated_field_encoding = "PACKED";
@@ -3001,10 +3007,40 @@ function Namespace(name, options) {
3001
3007
  * @private
3002
3008
  */
3003
3009
  this._nestedArray = null;
3010
+
3011
+ /**
3012
+ * Cache lookup calls for any objects contains anywhere under this namespace.
3013
+ * This drastically speeds up resolve for large cross-linked protos where the same
3014
+ * types are looked up repeatedly.
3015
+ * @type {Object.<string,ReflectionObject|null>}
3016
+ * @private
3017
+ */
3018
+ this._lookupCache = {};
3019
+
3020
+ /**
3021
+ * Whether or not objects contained in this namespace need feature resolution.
3022
+ * @type {boolean}
3023
+ * @protected
3024
+ */
3025
+ this._needsRecursiveFeatureResolution = true;
3026
+
3027
+ /**
3028
+ * Whether or not objects contained in this namespace need a resolve.
3029
+ * @type {boolean}
3030
+ * @protected
3031
+ */
3032
+ this._needsRecursiveResolve = true;
3004
3033
  }
3005
3034
 
3006
3035
  function clearCache(namespace) {
3007
3036
  namespace._nestedArray = null;
3037
+ namespace._lookupCache = {};
3038
+
3039
+ // Also clear parent caches, since they include nested lookups.
3040
+ var parent = namespace;
3041
+ while(parent = parent.parent) {
3042
+ parent._lookupCache = {};
3043
+ }
3008
3044
  return namespace;
3009
3045
  }
3010
3046
 
@@ -3142,6 +3178,16 @@ Namespace.prototype.add = function add(object) {
3142
3178
  }
3143
3179
  }
3144
3180
 
3181
+ this._needsRecursiveFeatureResolution = true;
3182
+ this._needsRecursiveResolve = true;
3183
+
3184
+ // Also clear parent caches, since they need to recurse down.
3185
+ var parent = this;
3186
+ while(parent = parent.parent) {
3187
+ parent._needsRecursiveFeatureResolution = true;
3188
+ parent._needsRecursiveResolve = true;
3189
+ }
3190
+
3145
3191
  object.onAdd(this);
3146
3192
  return clearCache(this);
3147
3193
  };
@@ -3203,6 +3249,8 @@ Namespace.prototype.define = function define(path, json) {
3203
3249
  * @returns {Namespace} `this`
3204
3250
  */
3205
3251
  Namespace.prototype.resolveAll = function resolveAll() {
3252
+ if (!this._needsRecursiveResolve) return this;
3253
+
3206
3254
  var nested = this.nestedArray, i = 0;
3207
3255
  this.resolve();
3208
3256
  while (i < nested.length)
@@ -3210,6 +3258,7 @@ Namespace.prototype.resolveAll = function resolveAll() {
3210
3258
  nested[i++].resolveAll();
3211
3259
  else
3212
3260
  nested[i++].resolve();
3261
+ this._needsRecursiveResolve = false;
3213
3262
  return this;
3214
3263
  };
3215
3264
 
@@ -3217,6 +3266,9 @@ Namespace.prototype.resolveAll = function resolveAll() {
3217
3266
  * @override
3218
3267
  */
3219
3268
  Namespace.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
3269
+ if (!this._needsRecursiveFeatureResolution) return this;
3270
+ this._needsRecursiveFeatureResolution = false;
3271
+
3220
3272
  edition = this._edition || edition;
3221
3273
 
3222
3274
  ReflectionObject.prototype._resolveFeaturesRecursive.call(this, edition);
@@ -3234,7 +3286,6 @@ Namespace.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursi
3234
3286
  * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
3235
3287
  */
3236
3288
  Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChecked) {
3237
-
3238
3289
  /* istanbul ignore next */
3239
3290
  if (typeof filterTypes === "boolean") {
3240
3291
  parentAlreadyChecked = filterTypes;
@@ -3249,29 +3300,72 @@ Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChe
3249
3300
  } else if (!path.length)
3250
3301
  return this;
3251
3302
 
3303
+ var flatPath = path.join(".");
3304
+
3252
3305
  // Start at root if path is absolute
3253
3306
  if (path[0] === "")
3254
3307
  return this.root.lookup(path.slice(1), filterTypes);
3255
3308
 
3309
+ // Early bailout for objects with matching absolute paths
3310
+ var found = this.root._fullyQualifiedObjects["." + flatPath];
3311
+ if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
3312
+ return found;
3313
+ }
3314
+
3315
+ // Do a regular lookup at this namespace and below
3316
+ found = this._lookupImpl(path, flatPath);
3317
+ if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
3318
+ return found;
3319
+ }
3320
+
3321
+ if (parentAlreadyChecked)
3322
+ return null;
3323
+
3324
+ // If there hasn't been a match, walk up the tree and look more broadly
3325
+ var current = this;
3326
+ while (current.parent) {
3327
+ found = current.parent._lookupImpl(path, flatPath);
3328
+ if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
3329
+ return found;
3330
+ }
3331
+ current = current.parent;
3332
+ }
3333
+ return null;
3334
+ };
3335
+
3336
+ /**
3337
+ * Internal helper for lookup that handles searching just at this namespace and below along with caching.
3338
+ * @param {string[]} path Path to look up
3339
+ * @param {string} flatPath Flattened version of the path to use as a cache key
3340
+ * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
3341
+ * @private
3342
+ */
3343
+ Namespace.prototype._lookupImpl = function lookup(path, flatPath) {
3344
+ if(Object.prototype.hasOwnProperty.call(this._lookupCache, flatPath)) {
3345
+ return this._lookupCache[flatPath];
3346
+ }
3347
+
3256
3348
  // Test if the first part matches any nested object, and if so, traverse if path contains more
3257
3349
  var found = this.get(path[0]);
3350
+ var exact = null;
3258
3351
  if (found) {
3259
3352
  if (path.length === 1) {
3260
- if (!filterTypes || filterTypes.indexOf(found.constructor) > -1)
3261
- return found;
3262
- } else if (found instanceof Namespace && (found = found.lookup(path.slice(1), filterTypes, true)))
3263
- return found;
3353
+ exact = found;
3354
+ } else if (found instanceof Namespace) {
3355
+ path = path.slice(1);
3356
+ exact = found._lookupImpl(path, path.join("."));
3357
+ }
3264
3358
 
3265
3359
  // Otherwise try each nested namespace
3266
- } else
3360
+ } else {
3267
3361
  for (var i = 0; i < this.nestedArray.length; ++i)
3268
- if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i].lookup(path, filterTypes, true)))
3269
- return found;
3362
+ if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i]._lookupImpl(path, flatPath)))
3363
+ exact = found;
3364
+ }
3270
3365
 
3271
- // If there hasn't been a match, try again at the parent
3272
- if (this.parent === null || parentAlreadyChecked)
3273
- return null;
3274
- return this.parent.lookup(path, filterTypes);
3366
+ // Set this even when null, so that when we walk up the tree we can quickly bail on repeated checks back down.
3367
+ this._lookupCache[flatPath] = exact;
3368
+ return exact;
3275
3369
  };
3276
3370
 
3277
3371
  /**
@@ -3402,6 +3496,7 @@ function ReflectionObject(name, options) {
3402
3496
  /**
3403
3497
  * The edition specified for this object. Only relevant for top-level objects.
3404
3498
  * @type {string}
3499
+ * @private
3405
3500
  */
3406
3501
  this._edition = null;
3407
3502
 
@@ -3409,15 +3504,24 @@ function ReflectionObject(name, options) {
3409
3504
  * The default edition to use for this object if none is specified. For legacy reasons,
3410
3505
  * this is proto2 except in the JSON parsing case where it was proto3.
3411
3506
  * @type {string}
3507
+ * @private
3412
3508
  */
3413
3509
  this._defaultEdition = "proto2";
3414
3510
 
3415
3511
  /**
3416
3512
  * Resolved Features.
3417
3513
  * @type {object}
3514
+ * @private
3418
3515
  */
3419
3516
  this._features = {};
3420
3517
 
3518
+ /**
3519
+ * Whether or not features have been resolved.
3520
+ * @type {boolean}
3521
+ * @private
3522
+ */
3523
+ this._featuresResolved = false;
3524
+
3421
3525
  /**
3422
3526
  * Parent namespace.
3423
3527
  * @type {Namespace|null}
@@ -3523,10 +3627,8 @@ ReflectionObject.prototype.onRemove = function onRemove(parent) {
3523
3627
  ReflectionObject.prototype.resolve = function resolve() {
3524
3628
  if (this.resolved)
3525
3629
  return this;
3526
- if (this instanceof Root) {
3527
- this._resolveFeaturesRecursive(this._edition);
3528
- this.resolved = true;
3529
- }
3630
+ if (this.root instanceof Root)
3631
+ this.resolved = true; // only if part of a root
3530
3632
  return this;
3531
3633
  };
3532
3634
 
@@ -3545,6 +3647,10 @@ ReflectionObject.prototype._resolveFeaturesRecursive = function _resolveFeatures
3545
3647
  * @returns {undefined}
3546
3648
  */
3547
3649
  ReflectionObject.prototype._resolveFeatures = function _resolveFeatures(edition) {
3650
+ if (this._featuresResolved) {
3651
+ return;
3652
+ }
3653
+
3548
3654
  var defaults = {};
3549
3655
 
3550
3656
  /* istanbul ignore if */
@@ -3568,6 +3674,7 @@ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures(edition)
3568
3674
  throw new Error("Unknown edition: " + edition);
3569
3675
  }
3570
3676
  this._features = Object.assign(defaults, protoFeatures || {});
3677
+ this._featuresResolved = true;
3571
3678
  return;
3572
3679
  }
3573
3680
 
@@ -3589,6 +3696,7 @@ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures(edition)
3589
3696
  // Sister fields should have the same features as their extensions.
3590
3697
  this.extensionField._features = this._features;
3591
3698
  }
3699
+ this._featuresResolved = true;
3592
3700
  };
3593
3701
 
3594
3702
  /**
@@ -4448,8 +4556,19 @@ function Root(options) {
4448
4556
  */
4449
4557
  this.files = [];
4450
4558
 
4451
- // Default to proto2 if unspecified.
4559
+ /**
4560
+ * Edition, defaults to proto2 if unspecified.
4561
+ * @type {string}
4562
+ * @private
4563
+ */
4452
4564
  this._edition = "proto2";
4565
+
4566
+ /**
4567
+ * Global lookup cache of fully qualified names.
4568
+ * @type {Object.<string,ReflectionObject>}
4569
+ * @private
4570
+ */
4571
+ this._fullyQualifiedObjects = {};
4453
4572
  }
4454
4573
 
4455
4574
  /**
@@ -4511,6 +4630,9 @@ Root.prototype.load = function load(filename, options, callback) {
4511
4630
 
4512
4631
  // Finishes loading by calling the callback (exactly once)
4513
4632
  function finish(err, root) {
4633
+ if (root) {
4634
+ root.resolveAll();
4635
+ }
4514
4636
  /* istanbul ignore if */
4515
4637
  if (!callback) {
4516
4638
  return;
@@ -4520,9 +4642,6 @@ Root.prototype.load = function load(filename, options, callback) {
4520
4642
  }
4521
4643
  var cb = callback;
4522
4644
  callback = null;
4523
- if (root) {
4524
- root.resolveAll();
4525
- }
4526
4645
  cb(err, root);
4527
4646
  }
4528
4647
 
@@ -4630,8 +4749,8 @@ Root.prototype.load = function load(filename, options, callback) {
4630
4749
  for (var i = 0, resolved; i < filename.length; ++i)
4631
4750
  if (resolved = self.resolvePath("", filename[i]))
4632
4751
  fetch(resolved);
4633
- self.resolveAll();
4634
4752
  if (sync) {
4753
+ self.resolveAll();
4635
4754
  return self;
4636
4755
  }
4637
4756
  if (!queued) {
@@ -4680,10 +4799,13 @@ Root.prototype.loadSync = function loadSync(filename, options) {
4680
4799
  * @override
4681
4800
  */
4682
4801
  Root.prototype.resolveAll = function resolveAll() {
4802
+ if (!this._needsRecursiveResolve) return this;
4803
+
4683
4804
  if (this.deferred.length)
4684
4805
  throw Error("unresolvable extensions: " + this.deferred.map(function(field) {
4685
4806
  return "'extend " + field.extend + "' in " + field.parent.fullName;
4686
4807
  }).join(", "));
4808
+ this._resolveFeaturesRecursive(this._edition);
4687
4809
  return Namespace.prototype.resolveAll.call(this);
4688
4810
  };
4689
4811
 
@@ -4746,6 +4868,11 @@ Root.prototype._handleAdd = function _handleAdd(object) {
4746
4868
  object.parent[object.name] = object; // expose namespace as property of its parent
4747
4869
  }
4748
4870
 
4871
+ if (object instanceof Type || object instanceof Enum || object instanceof Field) {
4872
+ // Only store types and enums for quick lookup during resolve.
4873
+ this._fullyQualifiedObjects[object.fullName] = object;
4874
+ }
4875
+
4749
4876
  // The above also adds uppercased (and thus conflict-free) nested types, services and enums as
4750
4877
  // properties of namespaces just like static code does. This allows using a .d.ts generated for
4751
4878
  // a static module with reflection-based solutions where the condition is met.
@@ -4786,6 +4913,8 @@ Root.prototype._handleRemove = function _handleRemove(object) {
4786
4913
  delete object.parent[object.name]; // unexpose namespaces
4787
4914
 
4788
4915
  }
4916
+
4917
+ delete this._fullyQualifiedObjects[object.fullName];
4789
4918
  };
4790
4919
 
4791
4920
  // Sets up cyclic dependencies (called in index-light)
@@ -5110,6 +5239,8 @@ Service.prototype.get = function get(name) {
5110
5239
  * @override
5111
5240
  */
5112
5241
  Service.prototype.resolveAll = function resolveAll() {
5242
+ if (!this._needsRecursiveResolve) return this;
5243
+
5113
5244
  Namespace.prototype.resolve.call(this);
5114
5245
  var methods = this.methodsArray;
5115
5246
  for (var i = 0; i < methods.length; ++i)
@@ -5121,6 +5252,8 @@ Service.prototype.resolveAll = function resolveAll() {
5121
5252
  * @override
5122
5253
  */
5123
5254
  Service.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
5255
+ if (!this._needsRecursiveFeatureResolution) return this;
5256
+
5124
5257
  edition = this._edition || edition;
5125
5258
 
5126
5259
  Namespace.prototype._resolveFeaturesRecursive.call(this, edition);
@@ -5490,6 +5623,8 @@ Type.prototype.toJSON = function toJSON(toJSONOptions) {
5490
5623
  * @override
5491
5624
  */
5492
5625
  Type.prototype.resolveAll = function resolveAll() {
5626
+ if (!this._needsRecursiveResolve) return this;
5627
+
5493
5628
  Namespace.prototype.resolveAll.call(this);
5494
5629
  var oneofs = this.oneofsArray; i = 0;
5495
5630
  while (i < oneofs.length)
@@ -5504,6 +5639,8 @@ Type.prototype.resolveAll = function resolveAll() {
5504
5639
  * @override
5505
5640
  */
5506
5641
  Type.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
5642
+ if (!this._needsRecursiveFeatureResolution) return this;
5643
+
5507
5644
  edition = this._edition || edition;
5508
5645
 
5509
5646
  Namespace.prototype._resolveFeaturesRecursive.call(this, edition);