protobufjs 8.4.0 → 8.4.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.
@@ -25,6 +25,18 @@ util.pool = require("./pool");
25
25
  // utility to work with the low and high bits of a 64 bit value
26
26
  util.LongBits = require("./longbits");
27
27
 
28
+ /**
29
+ * Tests if the specified key can affect object prototypes.
30
+ * @memberof util
31
+ * @param {string} key Key to test
32
+ * @returns {boolean} `true` if the key is unsafe
33
+ */
34
+ function isUnsafeProperty(key) {
35
+ return key === "__proto__" || key === "prototype" || key === "constructor";
36
+ }
37
+
38
+ util.isUnsafeProperty = isUnsafeProperty;
39
+
28
40
  /**
29
41
  * Whether running within node or not.
30
42
  * @memberof util
@@ -259,15 +271,21 @@ util.boolFromKey = function boolFromKey(key) {
259
271
  * Merges the properties of the source object into the destination object.
260
272
  * @memberof util
261
273
  * @param {Object.<string,*>} dst Destination object
262
- * @param {Object.<string,*>} src Source object
263
- * @param {boolean} [ifNotSet=false] Merges only if the key is not already set
274
+ * @param {...(Object.<string,*>|boolean)} src Source objects, optionally followed by an `ifNotSet` flag
264
275
  * @returns {Object.<string,*>} Destination object
265
276
  */
266
- function merge(dst, src, ifNotSet) { // used by converters
267
- for (var keys = Object.keys(src), i = 0; i < keys.length; ++i)
268
- if (dst[keys[i]] === undefined || !ifNotSet)
269
- if (keys[i] !== "__proto__")
277
+ function merge(dst) { // used by converters
278
+ var ifNotSet = typeof arguments[arguments.length - 1] === "boolean",
279
+ limit = ifNotSet ? arguments.length - 1 : arguments.length;
280
+ ifNotSet = ifNotSet && arguments[arguments.length - 1];
281
+ for (var a = 1; a < limit; ++a) {
282
+ var src = arguments[a];
283
+ if (!src)
284
+ continue;
285
+ for (var keys = Object.keys(src), i = 0; i < keys.length; ++i)
286
+ if (!isUnsafeProperty(keys[i]) && (dst[keys[i]] === undefined || !ifNotSet))
270
287
  dst[keys[i]] = src[keys[i]];
288
+ }
271
289
  return dst;
272
290
  }
273
291
 
@@ -5,4 +5,3 @@ var patterns = exports;
5
5
  patterns.numberRe = /^(?![eE])[0-9]*(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?$/;
6
6
  patterns.typeRefRe = /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*$/;
7
7
  patterns.reservedRe = /^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$/;
8
- patterns.unsafePropertyRe = /^(?:__proto__|prototype|constructor)$/;
package/src/util.js CHANGED
@@ -16,8 +16,7 @@ util.fetch = require("./util/fetch");
16
16
  util.path = require("./util/path");
17
17
  util.patterns = require("./util/patterns");
18
18
 
19
- var reservedRe = util.patterns.reservedRe,
20
- unsafePropertyRe = util.patterns.unsafePropertyRe;
19
+ var reservedRe = util.patterns.reservedRe;
21
20
 
22
21
  /**
23
22
  * Node's fs module if available.
@@ -180,7 +179,7 @@ util.decorateEnum = function decorateEnum(object) {
180
179
  util.setProperty = function setProperty(dst, path, value, ifNotSet) {
181
180
  function setProp(dst, path, value) {
182
181
  var part = path.shift();
183
- if (unsafePropertyRe.test(part))
182
+ if (util.isUnsafeProperty(part))
184
183
  return dst;
185
184
  if (path.length > 0) {
186
185
  dst[part] = setProp(dst[part] || {}, path, value);
package/src/wrappers.js CHANGED
@@ -7,7 +7,8 @@
7
7
  */
8
8
  var wrappers = exports;
9
9
 
10
- var Message = require("./message");
10
+ var Message = require("./message"),
11
+ util = require("./util/minimal");
11
12
 
12
13
  /**
13
14
  * From object converter part of an {@link IWrapper}.
@@ -54,10 +55,9 @@ wrappers[".google.protobuf.Any"] = {
54
55
  if (type_url.indexOf("/") === -1) {
55
56
  type_url = "/" + type_url;
56
57
  }
57
- var nextDepth = depth === undefined ? 1 : depth + 1;
58
58
  return this.create({
59
59
  type_url: type_url,
60
- value: type.encode(type.fromObject(object, nextDepth)).finish()
60
+ value: type.encode(type.fromObject(object, depth === undefined ? 1 : depth + 1)).finish()
61
61
  });
62
62
  }
63
63
  }
@@ -65,13 +65,16 @@ wrappers[".google.protobuf.Any"] = {
65
65
  return this.fromObject(object, depth);
66
66
  },
67
67
 
68
- toObject: function(message, options) {
68
+ toObject: function(message, options, depth) {
69
+ if (depth === undefined)
70
+ depth = 0;
71
+ if (depth > util.recursionLimit)
72
+ throw Error("max depth exceeded");
69
73
 
70
74
  // Default prefix
71
75
  var googleApi = "type.googleapis.com/";
72
76
  var prefix = "";
73
77
  var name = "";
74
-
75
78
  // decode value if requested and unmapped
76
79
  if (options && options.json && message.type_url && message.value) {
77
80
  // Only use fully qualified type name after the last '/'
@@ -81,12 +84,12 @@ wrappers[".google.protobuf.Any"] = {
81
84
  var type = this.lookup(name);
82
85
  /* istanbul ignore else */
83
86
  if (type)
84
- message = type.decode(message.value);
87
+ message = type.decode(message.value, undefined, undefined, depth + 1);
85
88
  }
86
89
 
87
90
  // wrap value if unmapped
88
91
  if (!(message instanceof this.ctor) && message instanceof Message) {
89
- var object = message.$type.toObject(message, options);
92
+ var object = message.$type.toObject(message, options, depth + 1);
90
93
  var messageName = message.$type.fullName[0] === "." ?
91
94
  message.$type.fullName.slice(1) : message.$type.fullName;
92
95
  // Default to type.googleapis.com prefix if no prefix is used
@@ -98,6 +101,6 @@ wrappers[".google.protobuf.Any"] = {
98
101
  return object;
99
102
  }
100
103
 
101
- return this.toObject(message, options);
104
+ return this.toObject(message, options, depth);
102
105
  }
103
106
  };
package/src/writer.js CHANGED
@@ -230,7 +230,7 @@ Writer.prototype.uint32 = function write_uint32(value) {
230
230
  * @returns {Writer} `this`
231
231
  */
232
232
  Writer.prototype.int32 = function write_int32(value) {
233
- return value < 0
233
+ return (value |= 0) < 0
234
234
  ? this._push(writeVarint64, 10, LongBits.fromNumber(value)) // 10 bytes per spec
235
235
  : this.uint32(value);
236
236
  };
@@ -245,16 +245,18 @@ Writer.prototype.sint32 = function write_sint32(value) {
245
245
  };
246
246
 
247
247
  function writeVarint64(val, buf, pos) {
248
- while (val.hi) {
249
- buf[pos++] = val.lo & 127 | 128;
250
- val.lo = (val.lo >>> 7 | val.hi << 25) >>> 0;
251
- val.hi >>>= 7;
248
+ var lo = val.lo,
249
+ hi = val.hi;
250
+ while (hi) {
251
+ buf[pos++] = lo & 127 | 128;
252
+ lo = (lo >>> 7 | hi << 25) >>> 0;
253
+ hi >>>= 7;
252
254
  }
253
- while (val.lo > 127) {
254
- buf[pos++] = val.lo & 127 | 128;
255
- val.lo = val.lo >>> 7;
255
+ while (lo > 127) {
256
+ buf[pos++] = lo & 127 | 128;
257
+ lo = lo >>> 7;
256
258
  }
257
- buf[pos++] = val.lo;
259
+ buf[pos++] = lo;
258
260
  }
259
261
 
260
262
  /**