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.
- package/README.md +65 -58
- package/dist/light/protobuf.js +167 -50
- package/dist/light/protobuf.js.map +1 -1
- package/dist/light/protobuf.min.js +3 -3
- package/dist/light/protobuf.min.js.map +1 -1
- package/dist/minimal/protobuf.js +26 -8
- package/dist/minimal/protobuf.js.map +1 -1
- package/dist/minimal/protobuf.min.js +3 -3
- package/dist/minimal/protobuf.min.js.map +1 -1
- package/dist/protobuf.js +182 -51
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/ext/README.md +38 -49
- package/ext/descriptor.generated.d.ts +2 -2
- package/ext/descriptor.js +18 -3
- package/ext/protojson.LICENSE +201 -0
- package/ext/protojson.d.ts +20 -0
- package/ext/protojson.generated.d.ts +49 -0
- package/ext/protojson.js +903 -0
- package/ext/textformat.d.ts +2 -2
- package/ext/textformat.generated.d.ts +22 -0
- package/ext/textformat.js +40 -19
- package/index.d.ts +48 -9
- package/package.json +4 -7
- package/src/converter.js +7 -5
- package/src/decoder.js +6 -4
- package/src/encoder.js +1 -1
- package/src/field.js +49 -7
- package/src/mapfield.js +16 -7
- package/src/method.js +16 -2
- package/src/namespace.js +4 -4
- package/src/parse.js +15 -1
- package/src/reader.js +12 -0
- package/src/roots.js +1 -1
- package/src/rpc/service.js +10 -4
- package/src/service.js +5 -7
- package/src/type.js +12 -3
- package/src/util/minimal.js +1 -1
- package/src/util.js +23 -0
- package/src/verifier.js +2 -2
- package/scripts/postinstall.js +0 -32
package/src/rpc/service.js
CHANGED
|
@@ -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 {
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
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] =
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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"]
|
|
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)
|
package/src/util/minimal.js
CHANGED
|
@@ -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 &&
|
|
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"]
|
|
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&&
|
|
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
|
package/scripts/postinstall.js
DELETED
|
@@ -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
|
-
});
|