protobufjs 8.1.1-experimental → 8.1.3-experimental

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 v8.1.1-experimental (c) 2016, daniel wirtz
3
- * compiled wed, 07 may 2025 22:28:18 utc
2
+ * protobuf.js v8.1.3-experimental (c) 2016, daniel wirtz
3
+ * compiled mon, 12 may 2025 03:50:28 utc
4
4
  * licensed under the bsd-3-clause license
5
5
  * see: https://github.com/dcodeio/protobuf.js for details
6
6
  */
@@ -3007,10 +3007,40 @@ function Namespace(name, options) {
3007
3007
  * @private
3008
3008
  */
3009
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;
3010
3033
  }
3011
3034
 
3012
3035
  function clearCache(namespace) {
3013
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
+ }
3014
3044
  return namespace;
3015
3045
  }
3016
3046
 
@@ -3148,6 +3178,16 @@ Namespace.prototype.add = function add(object) {
3148
3178
  }
3149
3179
  }
3150
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
+
3151
3191
  object.onAdd(this);
3152
3192
  return clearCache(this);
3153
3193
  };
@@ -3209,6 +3249,8 @@ Namespace.prototype.define = function define(path, json) {
3209
3249
  * @returns {Namespace} `this`
3210
3250
  */
3211
3251
  Namespace.prototype.resolveAll = function resolveAll() {
3252
+ if (!this._needsRecursiveResolve) return this;
3253
+
3212
3254
  var nested = this.nestedArray, i = 0;
3213
3255
  this.resolve();
3214
3256
  while (i < nested.length)
@@ -3216,6 +3258,7 @@ Namespace.prototype.resolveAll = function resolveAll() {
3216
3258
  nested[i++].resolveAll();
3217
3259
  else
3218
3260
  nested[i++].resolve();
3261
+ this._needsRecursiveResolve = false;
3219
3262
  return this;
3220
3263
  };
3221
3264
 
@@ -3223,6 +3266,9 @@ Namespace.prototype.resolveAll = function resolveAll() {
3223
3266
  * @override
3224
3267
  */
3225
3268
  Namespace.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
3269
+ if (!this._needsRecursiveFeatureResolution) return this;
3270
+ this._needsRecursiveFeatureResolution = false;
3271
+
3226
3272
  edition = this._edition || edition;
3227
3273
 
3228
3274
  ReflectionObject.prototype._resolveFeaturesRecursive.call(this, edition);
@@ -3240,7 +3286,6 @@ Namespace.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursi
3240
3286
  * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
3241
3287
  */
3242
3288
  Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChecked) {
3243
-
3244
3289
  /* istanbul ignore next */
3245
3290
  if (typeof filterTypes === "boolean") {
3246
3291
  parentAlreadyChecked = filterTypes;
@@ -3255,29 +3300,72 @@ Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChe
3255
3300
  } else if (!path.length)
3256
3301
  return this;
3257
3302
 
3303
+ var flatPath = path.join(".");
3304
+
3258
3305
  // Start at root if path is absolute
3259
3306
  if (path[0] === "")
3260
3307
  return this.root.lookup(path.slice(1), filterTypes);
3261
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
+
3262
3348
  // Test if the first part matches any nested object, and if so, traverse if path contains more
3263
3349
  var found = this.get(path[0]);
3350
+ var exact = null;
3264
3351
  if (found) {
3265
3352
  if (path.length === 1) {
3266
- if (!filterTypes || filterTypes.indexOf(found.constructor) > -1)
3267
- return found;
3268
- } else if (found instanceof Namespace && (found = found.lookup(path.slice(1), filterTypes, true)))
3269
- return found;
3353
+ exact = found;
3354
+ } else if (found instanceof Namespace) {
3355
+ path = path.slice(1);
3356
+ exact = found._lookupImpl(path, path.join("."));
3357
+ }
3270
3358
 
3271
3359
  // Otherwise try each nested namespace
3272
- } else
3360
+ } else {
3273
3361
  for (var i = 0; i < this.nestedArray.length; ++i)
3274
- if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i].lookup(path, filterTypes, true)))
3275
- return found;
3362
+ if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i]._lookupImpl(path, flatPath)))
3363
+ exact = found;
3364
+ }
3276
3365
 
3277
- // If there hasn't been a match, try again at the parent
3278
- if (this.parent === null || parentAlreadyChecked)
3279
- return null;
3280
- 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;
3281
3369
  };
3282
3370
 
3283
3371
  /**
@@ -3408,6 +3496,7 @@ function ReflectionObject(name, options) {
3408
3496
  /**
3409
3497
  * The edition specified for this object. Only relevant for top-level objects.
3410
3498
  * @type {string}
3499
+ * @private
3411
3500
  */
3412
3501
  this._edition = null;
3413
3502
 
@@ -3415,19 +3504,22 @@ function ReflectionObject(name, options) {
3415
3504
  * The default edition to use for this object if none is specified. For legacy reasons,
3416
3505
  * this is proto2 except in the JSON parsing case where it was proto3.
3417
3506
  * @type {string}
3507
+ * @private
3418
3508
  */
3419
3509
  this._defaultEdition = "proto2";
3420
3510
 
3421
3511
  /**
3422
3512
  * Resolved Features.
3423
3513
  * @type {object}
3514
+ * @private
3424
3515
  */
3425
3516
  this._features = {};
3426
3517
 
3427
3518
  /**
3428
3519
  * Whether or not features have been resolved.
3429
3520
  * @type {boolean}
3430
- * /
3521
+ * @private
3522
+ */
3431
3523
  this._featuresResolved = false;
3432
3524
 
3433
3525
  /**
@@ -3535,9 +3627,8 @@ ReflectionObject.prototype.onRemove = function onRemove(parent) {
3535
3627
  ReflectionObject.prototype.resolve = function resolve() {
3536
3628
  if (this.resolved)
3537
3629
  return this;
3538
- if (this instanceof Root) {
3539
- this.resolved = true;
3540
- }
3630
+ if (this.root instanceof Root)
3631
+ this.resolved = true; // only if part of a root
3541
3632
  return this;
3542
3633
  };
3543
3634
 
@@ -4465,8 +4556,19 @@ function Root(options) {
4465
4556
  */
4466
4557
  this.files = [];
4467
4558
 
4468
- // Default to proto2 if unspecified.
4559
+ /**
4560
+ * Edition, defaults to proto2 if unspecified.
4561
+ * @type {string}
4562
+ * @private
4563
+ */
4469
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 = {};
4470
4572
  }
4471
4573
 
4472
4574
  /**
@@ -4480,7 +4582,7 @@ Root.fromJSON = function fromJSON(json, root) {
4480
4582
  root = new Root();
4481
4583
  if (json.options)
4482
4584
  root.setOptions(json.options);
4483
- return root.addJSON(json.nested)._resolveFeaturesRecursive();
4585
+ return root.addJSON(json.nested).resolveAll();
4484
4586
  };
4485
4587
 
4486
4588
  /**
@@ -4529,7 +4631,7 @@ Root.prototype.load = function load(filename, options, callback) {
4529
4631
  // Finishes loading by calling the callback (exactly once)
4530
4632
  function finish(err, root) {
4531
4633
  if (root) {
4532
- root._resolveFeaturesRecursive();
4634
+ root.resolveAll();
4533
4635
  }
4534
4636
  /* istanbul ignore if */
4535
4637
  if (!callback) {
@@ -4648,7 +4750,7 @@ Root.prototype.load = function load(filename, options, callback) {
4648
4750
  if (resolved = self.resolvePath("", filename[i]))
4649
4751
  fetch(resolved);
4650
4752
  if (sync) {
4651
- self._resolveFeaturesRecursive();
4753
+ self.resolveAll();
4652
4754
  return self;
4653
4755
  }
4654
4756
  if (!queued) {
@@ -4697,6 +4799,8 @@ Root.prototype.loadSync = function loadSync(filename, options) {
4697
4799
  * @override
4698
4800
  */
4699
4801
  Root.prototype.resolveAll = function resolveAll() {
4802
+ if (!this._needsRecursiveResolve) return this;
4803
+
4700
4804
  if (this.deferred.length)
4701
4805
  throw Error("unresolvable extensions: " + this.deferred.map(function(field) {
4702
4806
  return "'extend " + field.extend + "' in " + field.parent.fullName;
@@ -4764,6 +4868,11 @@ Root.prototype._handleAdd = function _handleAdd(object) {
4764
4868
  object.parent[object.name] = object; // expose namespace as property of its parent
4765
4869
  }
4766
4870
 
4871
+ if (object instanceof Type || object instanceof Enum) {
4872
+ // Only store types and enums for quick lookup during resolve.
4873
+ this._fullyQualifiedObjects[object.fullName] = object;
4874
+ }
4875
+
4767
4876
  // The above also adds uppercased (and thus conflict-free) nested types, services and enums as
4768
4877
  // properties of namespaces just like static code does. This allows using a .d.ts generated for
4769
4878
  // a static module with reflection-based solutions where the condition is met.
@@ -4804,6 +4913,8 @@ Root.prototype._handleRemove = function _handleRemove(object) {
4804
4913
  delete object.parent[object.name]; // unexpose namespaces
4805
4914
 
4806
4915
  }
4916
+
4917
+ delete this._fullyQualifiedObjects[object.fullName];
4807
4918
  };
4808
4919
 
4809
4920
  // Sets up cyclic dependencies (called in index-light)
@@ -5128,6 +5239,8 @@ Service.prototype.get = function get(name) {
5128
5239
  * @override
5129
5240
  */
5130
5241
  Service.prototype.resolveAll = function resolveAll() {
5242
+ if (!this._needsRecursiveResolve) return this;
5243
+
5131
5244
  Namespace.prototype.resolve.call(this);
5132
5245
  var methods = this.methodsArray;
5133
5246
  for (var i = 0; i < methods.length; ++i)
@@ -5139,6 +5252,8 @@ Service.prototype.resolveAll = function resolveAll() {
5139
5252
  * @override
5140
5253
  */
5141
5254
  Service.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
5255
+ if (!this._needsRecursiveFeatureResolution) return this;
5256
+
5142
5257
  edition = this._edition || edition;
5143
5258
 
5144
5259
  Namespace.prototype._resolveFeaturesRecursive.call(this, edition);
@@ -5508,6 +5623,8 @@ Type.prototype.toJSON = function toJSON(toJSONOptions) {
5508
5623
  * @override
5509
5624
  */
5510
5625
  Type.prototype.resolveAll = function resolveAll() {
5626
+ if (!this._needsRecursiveResolve) return this;
5627
+
5511
5628
  Namespace.prototype.resolveAll.call(this);
5512
5629
  var oneofs = this.oneofsArray; i = 0;
5513
5630
  while (i < oneofs.length)
@@ -5522,6 +5639,8 @@ Type.prototype.resolveAll = function resolveAll() {
5522
5639
  * @override
5523
5640
  */
5524
5641
  Type.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
5642
+ if (!this._needsRecursiveFeatureResolution) return this;
5643
+
5525
5644
  edition = this._edition || edition;
5526
5645
 
5527
5646
  Namespace.prototype._resolveFeaturesRecursive.call(this, edition);