protobufjs 7.4.0 → 7.5.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/src/root.js CHANGED
@@ -35,11 +35,14 @@ function Root(options) {
35
35
  * @type {string[]}
36
36
  */
37
37
  this.files = [];
38
+
39
+ // Default to proto2 if unspecified.
40
+ this._edition = "proto2";
38
41
  }
39
42
 
40
43
  /**
41
44
  * Loads a namespace descriptor into a root namespace.
42
- * @param {INamespace} json Nameespace descriptor
45
+ * @param {INamespace} json Namespace descriptor
43
46
  * @param {Root} [root] Root namespace, defaults to create a new one if omitted
44
47
  * @returns {Root} Root namespace
45
48
  */
@@ -48,7 +51,7 @@ Root.fromJSON = function fromJSON(json, root) {
48
51
  root = new Root();
49
52
  if (json.options)
50
53
  root.setOptions(json.options);
51
- return root.addJSON(json.nested);
54
+ return root.addJSON(json.nested)._resolveFeaturesRecursive();
52
55
  };
53
56
 
54
57
  /**
@@ -88,18 +91,24 @@ Root.prototype.load = function load(filename, options, callback) {
88
91
  options = undefined;
89
92
  }
90
93
  var self = this;
91
- if (!callback)
94
+ if (!callback) {
92
95
  return util.asPromise(load, self, filename, options);
96
+ }
93
97
 
94
98
  var sync = callback === SYNC; // undocumented
95
99
 
96
100
  // Finishes loading by calling the callback (exactly once)
97
101
  function finish(err, root) {
102
+ if (root) {
103
+ root._resolveFeaturesRecursive();
104
+ }
98
105
  /* istanbul ignore if */
99
- if (!callback)
106
+ if (!callback) {
100
107
  return;
101
- if (sync)
108
+ }
109
+ if (sync) {
102
110
  throw err;
111
+ }
103
112
  var cb = callback;
104
113
  callback = null;
105
114
  cb(err, root);
@@ -139,8 +148,9 @@ Root.prototype.load = function load(filename, options, callback) {
139
148
  } catch (err) {
140
149
  finish(err);
141
150
  }
142
- if (!sync && !queued)
151
+ if (!sync && !queued) {
143
152
  finish(null, self); // only once anyway
153
+ }
144
154
  }
145
155
 
146
156
  // Fetches a single file
@@ -148,15 +158,16 @@ Root.prototype.load = function load(filename, options, callback) {
148
158
  filename = getBundledFileName(filename) || filename;
149
159
 
150
160
  // Skip if already loaded / attempted
151
- if (self.files.indexOf(filename) > -1)
161
+ if (self.files.indexOf(filename) > -1) {
152
162
  return;
163
+ }
153
164
  self.files.push(filename);
154
165
 
155
166
  // Shortcut bundled definitions
156
167
  if (filename in common) {
157
- if (sync)
168
+ if (sync) {
158
169
  process(filename, common[filename]);
159
- else {
170
+ } else {
160
171
  ++queued;
161
172
  setTimeout(function() {
162
173
  --queued;
@@ -182,8 +193,9 @@ Root.prototype.load = function load(filename, options, callback) {
182
193
  self.fetch(filename, function(err, source) {
183
194
  --queued;
184
195
  /* istanbul ignore if */
185
- if (!callback)
196
+ if (!callback) {
186
197
  return; // terminated meanwhile
198
+ }
187
199
  if (err) {
188
200
  /* istanbul ignore else */
189
201
  if (!weak)
@@ -200,17 +212,21 @@ Root.prototype.load = function load(filename, options, callback) {
200
212
 
201
213
  // Assembling the root namespace doesn't require working type
202
214
  // references anymore, so we can load everything in parallel
203
- if (util.isString(filename))
215
+ if (util.isString(filename)) {
204
216
  filename = [ filename ];
217
+ }
205
218
  for (var i = 0, resolved; i < filename.length; ++i)
206
219
  if (resolved = self.resolvePath("", filename[i]))
207
220
  fetch(resolved);
208
-
209
- if (sync)
221
+ if (sync) {
222
+ self._resolveFeaturesRecursive();
210
223
  return self;
211
- if (!queued)
224
+ }
225
+ if (!queued) {
212
226
  finish(null, self);
213
- return undefined;
227
+ }
228
+
229
+ return self;
214
230
  };
215
231
  // function load(filename:string, options:IParseOptions, callback:LoadCallback):undefined
216
232
 
@@ -256,6 +272,7 @@ Root.prototype.resolveAll = function resolveAll() {
256
272
  throw Error("unresolvable extensions: " + this.deferred.map(function(field) {
257
273
  return "'extend " + field.extend + "' in " + field.parent.fullName;
258
274
  }).join(", "));
275
+ this._resolveFeaturesRecursive(this._edition);
259
276
  return Namespace.prototype.resolveAll.call(this);
260
277
  };
261
278
 
package/src/service.js CHANGED
@@ -57,7 +57,10 @@ Service.fromJSON = function fromJSON(name, json) {
57
57
  service.add(Method.fromJSON(names[i], json.methods[names[i]]));
58
58
  if (json.nested)
59
59
  service.addJSON(json.nested);
60
+ if (json.edition)
61
+ service._edition = json.edition;
60
62
  service.comment = json.comment;
63
+ service._defaultEdition = "proto3"; // For backwards-compatibility.
61
64
  return service;
62
65
  };
63
66
 
@@ -70,6 +73,7 @@ Service.prototype.toJSON = function toJSON(toJSONOptions) {
70
73
  var inherited = Namespace.prototype.toJSON.call(this, toJSONOptions);
71
74
  var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
72
75
  return util.toObject([
76
+ "edition" , this._editionToJSON(),
73
77
  "options" , inherited && inherited.options || undefined,
74
78
  "methods" , Namespace.arrayToJSON(this.methodsArray, toJSONOptions) || /* istanbul ignore next */ {},
75
79
  "nested" , inherited && inherited.nested || undefined,
@@ -106,10 +110,26 @@ Service.prototype.get = function get(name) {
106
110
  * @override
107
111
  */
108
112
  Service.prototype.resolveAll = function resolveAll() {
113
+ Namespace.prototype.resolve.call(this);
109
114
  var methods = this.methodsArray;
110
115
  for (var i = 0; i < methods.length; ++i)
111
116
  methods[i].resolve();
112
- return Namespace.prototype.resolve.call(this);
117
+ return this;
118
+ };
119
+
120
+ /**
121
+ * @override
122
+ */
123
+ Service.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
124
+ if (!this._needsRecursiveFeatureResolution) return this;
125
+
126
+ edition = this._edition || edition;
127
+
128
+ Namespace.prototype._resolveFeaturesRecursive.call(this, edition);
129
+ this.methodsArray.forEach(method => {
130
+ method._resolveFeaturesRecursive(edition);
131
+ });
132
+ return this;
113
133
  };
114
134
 
115
135
  /**
package/src/type.js CHANGED
@@ -272,6 +272,9 @@ Type.fromJSON = function fromJSON(name, json) {
272
272
  type.group = true;
273
273
  if (json.comment)
274
274
  type.comment = json.comment;
275
+ if (json.edition)
276
+ type._edition = json.edition;
277
+ type._defaultEdition = "proto3"; // For backwards-compatibility.
275
278
  return type;
276
279
  };
277
280
 
@@ -284,6 +287,7 @@ Type.prototype.toJSON = function toJSON(toJSONOptions) {
284
287
  var inherited = Namespace.prototype.toJSON.call(this, toJSONOptions);
285
288
  var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
286
289
  return util.toObject([
290
+ "edition" , this._editionToJSON(),
287
291
  "options" , inherited && inherited.options || undefined,
288
292
  "oneofs" , Namespace.arrayToJSON(this.oneofsArray, toJSONOptions),
289
293
  "fields" , Namespace.arrayToJSON(this.fieldsArray.filter(function(obj) { return !obj.declaringField; }), toJSONOptions) || {},
@@ -299,13 +303,32 @@ Type.prototype.toJSON = function toJSON(toJSONOptions) {
299
303
  * @override
300
304
  */
301
305
  Type.prototype.resolveAll = function resolveAll() {
302
- var fields = this.fieldsArray, i = 0;
303
- while (i < fields.length)
304
- fields[i++].resolve();
306
+ Namespace.prototype.resolveAll.call(this);
305
307
  var oneofs = this.oneofsArray; i = 0;
306
308
  while (i < oneofs.length)
307
309
  oneofs[i++].resolve();
308
- return Namespace.prototype.resolveAll.call(this);
310
+ var fields = this.fieldsArray, i = 0;
311
+ while (i < fields.length)
312
+ fields[i++].resolve();
313
+ return this;
314
+ };
315
+
316
+ /**
317
+ * @override
318
+ */
319
+ Type.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
320
+ if (!this._needsRecursiveFeatureResolution) return this;
321
+
322
+ edition = this._edition || edition;
323
+
324
+ Namespace.prototype._resolveFeaturesRecursive.call(this, edition);
325
+ this.oneofsArray.forEach(oneof => {
326
+ oneof._resolveFeatures(edition);
327
+ });
328
+ this.fieldsArray.forEach(field => {
329
+ field._resolveFeatures(edition);
330
+ });
331
+ return this;
309
332
  };
310
333
 
311
334
  /**
package/src/util.js CHANGED
@@ -171,9 +171,10 @@ util.decorateEnum = function decorateEnum(object) {
171
171
  * @param {Object.<string,*>} dst Destination object
172
172
  * @param {string} path dot '.' delimited path of the property to set
173
173
  * @param {Object} value the value to set
174
+ * @param {boolean|undefined} [ifNotSet] Sets the option only if it isn't currently set
174
175
  * @returns {Object.<string,*>} Destination object
175
176
  */
176
- util.setProperty = function setProperty(dst, path, value) {
177
+ util.setProperty = function setProperty(dst, path, value, ifNotSet) {
177
178
  function setProp(dst, path, value) {
178
179
  var part = path.shift();
179
180
  if (part === "__proto__" || part === "prototype") {
@@ -183,6 +184,8 @@ util.setProperty = function setProperty(dst, path, value) {
183
184
  dst[part] = setProp(dst[part] || {}, path, value);
184
185
  } else {
185
186
  var prevValue = dst[part];
187
+ if (prevValue && ifNotSet)
188
+ return dst;
186
189
  if (prevValue)
187
190
  value = [].concat(prevValue).concat(value);
188
191
  dst[part] = value;