protobufjs 8.4.2 → 8.6.0

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.
@@ -23,10 +23,16 @@ var util = require("../util/minimal");
23
23
  * @typedef rpc.ServiceMethod
24
24
  * @template TReq extends Message<TReq>
25
25
  * @template TRes extends Message<TRes>
26
- * @type {function}
27
- * @param {TReq|Properties<TReq>} request Request message or plain object
28
- * @param {rpc.ServiceMethodCallback<TRes>} [callback] Node-style callback called with the error, if any, and the response message
29
- * @returns {Promise<Message<TRes>>} Promise if `callback` has been omitted, otherwise `undefined`
26
+ * @type {{
27
+ * (request: TReq|Properties<TReq>, callback: rpc.ServiceMethodCallback<TRes>): void;
28
+ * (request: TReq|Properties<TReq>): Promise<TRes>;
29
+ * readonly name: string;
30
+ * readonly path: string;
31
+ * readonly requestType: string;
32
+ * readonly responseType: string;
33
+ * readonly requestStream: true|undefined;
34
+ * readonly responseStream: true|undefined;
35
+ * }}
30
36
  */
31
37
 
32
38
  /**
package/src/service.js CHANGED
@@ -9,8 +9,6 @@ var Method = require("./method"),
9
9
  util = require("./util"),
10
10
  rpc = require("./rpc");
11
11
 
12
- var reservedRe = util.patterns.reservedRe;
13
-
14
12
  /**
15
13
  * Constructs a new service instance.
16
14
  * @classdesc Reflected service.
@@ -190,11 +188,11 @@ Service.prototype.create = function create(rpcImpl, requestDelimited, responseDe
190
188
  var rpcService = new rpc.Service(rpcImpl, requestDelimited, responseDelimited);
191
189
  for (var i = 0, method; i < /* initializes */ this.methodsArray.length; ++i) {
192
190
  var methodName = util.lcFirst((method = this._methodsArray[i]).resolve().name).replace(/[^$\w_]/g, "");
193
- rpcService[methodName] = util.codegen(["r","c"], reservedRe.test(methodName) ? methodName + "_" : methodName)("return this.rpcCall(m,q,s,r,c)")({
194
- m: method,
195
- q: method.resolvedRequestType.ctor,
196
- s: method.resolvedResponseType.ctor
197
- });
191
+ rpcService[methodName] = (function(method, requestType, responseType) {
192
+ return function rpcMethod(request, callback) {
193
+ return rpc.Service.prototype.rpcCall.call(this, method, requestType, responseType, request, callback);
194
+ };
195
+ })(method, method.resolvedRequestType.ctor, method.resolvedResponseType.ctor);
198
196
  }
199
197
  return rpcService;
200
198
  };
package/src/type.js CHANGED
@@ -89,6 +89,13 @@ function Type(name, options) {
89
89
  * @private
90
90
  */
91
91
  this._ctor = null;
92
+
93
+ /**
94
+ * Cached fields by JSON name.
95
+ * @type {Object.<string,Field>|null}
96
+ * @private
97
+ */
98
+ this._fieldsByJsonName = null; // used by ext/protojson
92
99
  }
93
100
 
94
101
  Object.defineProperties(Type.prototype, {
@@ -201,7 +208,7 @@ Object.defineProperties(Type.prototype, {
201
208
  */
202
209
  Type.generateConstructor = function generateConstructor(mtype) {
203
210
  /* eslint-disable no-unexpected-multiline */
204
- var gen = util.codegen(["p"], mtype.name);
211
+ var gen = util.codegen(["p"]);
205
212
  // explicitly initialize mutable object/array fields so that these aren't just inherited from the prototype
206
213
  for (var i = 0, field; i < mtype.fieldsArray.length; ++i)
207
214
  if ((field = mtype._fieldsArray[i]).map) gen
@@ -215,7 +222,7 @@ Type.generateConstructor = function generateConstructor(mtype) {
215
222
  };
216
223
 
217
224
  function clearCache(type) {
218
- type._fieldsById = type._fieldsArray = type._oneofsArray = null;
225
+ type._fieldsById = type._fieldsArray = type._oneofsArray = type._fieldsByJsonName = null;
219
226
  delete type.encode;
220
227
  delete type.decode;
221
228
  delete type.verify;
@@ -379,7 +386,7 @@ Type.prototype.add = function add(object) {
379
386
  throw Error("duplicate id " + object.id + " in " + this);
380
387
  if (this.isReservedId(object.id))
381
388
  throw Error("id " + object.id + " is reserved in " + this);
382
- if (this.isReservedName(object.name))
389
+ if (this.isReservedName(object.name) || object.name.charAt(0) === "$")
383
390
  throw Error("name '" + object.name + "' is reserved in " + this);
384
391
  if (object.name === "__proto__")
385
392
  return this;
@@ -392,6 +399,8 @@ Type.prototype.add = function add(object) {
392
399
  return clearCache(this);
393
400
  }
394
401
  if (object instanceof OneOf) {
402
+ if (object.name.charAt(0) === "$")
403
+ throw Error("name '" + object.name + "' is reserved in " + this);
395
404
  if (object.name === "__proto__")
396
405
  return this;
397
406
  if (!this.oneofs)
@@ -119,7 +119,7 @@ util.isset =
119
119
  */
120
120
  util.isSet = function isSet(obj, prop) {
121
121
  var value = obj[prop];
122
- if (value != null && obj.hasOwnProperty(prop)) // eslint-disable-line eqeqeq, no-prototype-builtins
122
+ if (value != null && Object.hasOwnProperty.call(obj, prop)) // eslint-disable-line eqeqeq
123
123
  return typeof value !== "object" || (Array.isArray(value) ? value.length : Object.keys(value).length) > 0;
124
124
  return false;
125
125
  };
package/src/util.js CHANGED
@@ -93,6 +93,7 @@ var camelCaseRe = /_([a-z])/g;
93
93
  * Converts a string to camel case.
94
94
  * @param {string} str String to convert
95
95
  * @returns {string} Converted string
96
+ * @deprecated Use {@link util.jsonName} for protobuf field JSON names.
96
97
  */
97
98
  util.camelCase = function camelCase(str) {
98
99
  return str.substring(0, 1)
@@ -100,6 +101,28 @@ util.camelCase = function camelCase(str) {
100
101
  .replace(camelCaseRe, function($0, $1) { return $1.toUpperCase(); });
101
102
  };
102
103
 
104
+ /**
105
+ * Converts a proto field name to its protoc-compatible JSON name.
106
+ * @param {string} str Proto field name
107
+ * @returns {string} JSON name
108
+ */
109
+ util.jsonName = function jsonName(str) {
110
+ var result = "",
111
+ upperNext = false,
112
+ i = 0;
113
+ for (; i < str.length; ++i) {
114
+ var ch = str.charAt(i);
115
+ if (ch === "_")
116
+ upperNext = true;
117
+ else if (upperNext) {
118
+ result += ch.toUpperCase();
119
+ upperNext = false;
120
+ } else
121
+ result += ch;
122
+ }
123
+ return result;
124
+ };
125
+
103
126
  /**
104
127
  * Compares reflected fields by id.
105
128
  * @param {Field} a First field
package/src/verifier.js CHANGED
@@ -122,7 +122,7 @@ function genVerifyKey(gen, field, ref) {
122
122
  function verifier(mtype) {
123
123
  /* eslint-disable no-unexpected-multiline */
124
124
 
125
- var gen = util.codegen(["m", "q"], mtype.name + "$verify")
125
+ var gen = util.codegen(["m", "q"])
126
126
  ("if(typeof m!==\"object\"||m===null)")
127
127
  ("return%j", "object expected")
128
128
  ("if(q===undefined)q=0")
@@ -138,7 +138,7 @@ function verifier(mtype) {
138
138
  ref = "m" + util.safeProp(field.name);
139
139
 
140
140
  if (field.optional) gen
141
- ("if(%s!=null&&m.hasOwnProperty(%j)){", ref, field.name); // !== undefined && !== null
141
+ ("if(%s!=null&&Object.hasOwnProperty.call(m,%j)){", ref, field.name); // !== undefined && !== null
142
142
 
143
143
  // map fields
144
144
  if (field.map) { gen
@@ -1,32 +0,0 @@
1
- "use strict";
2
-
3
- var path = require("path"),
4
- fs = require("fs"),
5
- pkg = require(path.join(__dirname, "..", "package.json"));
6
-
7
- // check version scheme used by dependents
8
- if (!pkg.versionScheme)
9
- return;
10
-
11
- var warn = process.stderr.isTTY
12
- ? "\x1b[30m\x1b[43mWARN\x1b[0m \x1b[35m" + path.basename(process.argv[1], ".js") + "\x1b[0m"
13
- : "WARN " + path.basename(process.argv[1], ".js");
14
-
15
- var basePkg;
16
- try {
17
- basePkg = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json")));
18
- } catch (e) {
19
- return;
20
- }
21
-
22
- [
23
- "dependencies",
24
- "devDependencies",
25
- "optionalDependencies",
26
- "peerDependencies"
27
- ]
28
- .forEach(function(check) {
29
- var version = basePkg && basePkg[check] && basePkg[check][pkg.name];
30
- if (typeof version === "string" && version.charAt(0) !== pkg.versionScheme)
31
- process.stderr.write(pkg.name + " " + warn + " " + pkg.name + "@" + version + " is configured as a dependency of " + basePkg.name + ". use " + pkg.name + "@" + pkg.versionScheme + version.substring(1) + " instead for API compatibility.\n");
32
- });