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/ext/textformat.d.ts
CHANGED
|
@@ -10,10 +10,10 @@ declare module ".." {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
interface Type {
|
|
13
|
-
/** Parses this type from protobuf text format. */
|
|
13
|
+
/** Installed by `textformat.install()`. Parses this type from protobuf text format. */
|
|
14
14
|
fromText(text: string): $protobuf.Message<{}>;
|
|
15
15
|
|
|
16
|
-
/** Formats a message of this type as protobuf text format. */
|
|
16
|
+
/** Installed by `textformat.install()`. Formats a message of this type as protobuf text format. */
|
|
17
17
|
toText(message: ($protobuf.Message<{}>|{ [k: string]: any }), options?: ITextFormatOptions): string;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// DO NOT EDIT! This is a generated file. Edit the source file instead and regenerate.
|
|
2
2
|
|
|
3
|
+
import * as $protobuf from "..";
|
|
4
|
+
|
|
3
5
|
/** Maximum recursion depth for formatting length-delimited unknown fields. */
|
|
4
6
|
export let unknownRecursionLimit: number;
|
|
5
7
|
|
|
@@ -9,3 +11,23 @@ export interface ITextFormatOptions {
|
|
|
9
11
|
/** Also includes and formats unknown fields. */
|
|
10
12
|
unknowns?: boolean;
|
|
11
13
|
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Parses a message from protobuf text format using the specified reflected type.
|
|
17
|
+
* @param type Reflected message type
|
|
18
|
+
* @param text Text format input
|
|
19
|
+
* @returns Message instance
|
|
20
|
+
*/
|
|
21
|
+
export function fromText(type: $protobuf.Type, text: string): $protobuf.Message<{}>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Formats a message as protobuf text format using the specified reflected type.
|
|
25
|
+
* @param type Reflected message type
|
|
26
|
+
* @param message Message instance or plain object
|
|
27
|
+
* @param [options] Text format options
|
|
28
|
+
* @returns Text format output
|
|
29
|
+
*/
|
|
30
|
+
export function toText(type: $protobuf.Type, message: ($protobuf.Message<{}>|{ [k: string]: any }), options?: ITextFormatOptions): string;
|
|
31
|
+
|
|
32
|
+
/** Installs reflected {@link Type} convenience methods. */
|
|
33
|
+
export function install(): void;
|
package/ext/textformat.js
CHANGED
|
@@ -68,40 +68,61 @@ function parseText(type, text) {
|
|
|
68
68
|
* @property {boolean} [unknowns=false] Also includes and formats unknown fields.
|
|
69
69
|
*/
|
|
70
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Parses a message from protobuf text format using the specified reflected type.
|
|
73
|
+
* @function fromText
|
|
74
|
+
* @name fromText
|
|
75
|
+
* @param {$protobuf.Type} type Reflected message type
|
|
76
|
+
* @param {string} text Text format input
|
|
77
|
+
* @returns {$protobuf.Message<{}>} Message instance
|
|
78
|
+
*/
|
|
79
|
+
textformat.fromText = function fromText(type, text) {
|
|
80
|
+
return parseText(type, text);
|
|
81
|
+
};
|
|
82
|
+
|
|
71
83
|
/**
|
|
72
84
|
* Formats a message as protobuf text format using the specified reflected type.
|
|
73
|
-
* @
|
|
74
|
-
* @
|
|
85
|
+
* @function toText
|
|
86
|
+
* @name toText
|
|
87
|
+
* @param {$protobuf.Type} type Reflected message type
|
|
88
|
+
* @param {$protobuf.Message<{}>|Object.<string,*>} message Message instance or plain object
|
|
75
89
|
* @param {ITextFormatOptions} [options] Text format options
|
|
76
90
|
* @returns {string} Text format output
|
|
77
|
-
* @private
|
|
78
91
|
*/
|
|
79
|
-
function
|
|
92
|
+
textformat.toText = function toText(type, message, options) {
|
|
80
93
|
if (!(type instanceof Type))
|
|
81
94
|
throw TypeError("type must be a Type");
|
|
82
95
|
type.root.resolveAll();
|
|
83
96
|
var lines = [];
|
|
84
97
|
writeMessage(type, message, lines, 0, options || {}, 0);
|
|
85
98
|
return lines.join("\n");
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Parses this type from protobuf text format.
|
|
90
|
-
* @param {string} text Text format input
|
|
91
|
-
* @returns {Message<{}>} Message instance
|
|
92
|
-
*/
|
|
93
|
-
Type.prototype.fromText = function fromText(text) {
|
|
94
|
-
return parseText(this, text);
|
|
95
99
|
};
|
|
96
100
|
|
|
97
101
|
/**
|
|
98
|
-
*
|
|
99
|
-
* @
|
|
100
|
-
* @
|
|
101
|
-
* @returns {
|
|
102
|
+
* Installs reflected {@link Type} convenience methods.
|
|
103
|
+
* @function install
|
|
104
|
+
* @name install
|
|
105
|
+
* @returns {undefined}
|
|
102
106
|
*/
|
|
103
|
-
|
|
104
|
-
|
|
107
|
+
textformat.install = function install() {
|
|
108
|
+
/**
|
|
109
|
+
* Parses a message of this type from protobuf text format. Convenience for {@link textformat.fromText}.
|
|
110
|
+
* @param {string} text Text format input
|
|
111
|
+
* @returns {Message<{}>} Message instance
|
|
112
|
+
*/
|
|
113
|
+
Type.prototype.fromText = function fromText(text) {
|
|
114
|
+
return textformat.fromText(this, text);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Formats a message of this type as protobuf text format. Convenience for {@link textformat.toText}.
|
|
119
|
+
* @param {Message<{}>|Object.<string,*>} message Message instance or plain object
|
|
120
|
+
* @param {ITextFormatOptions} [options] Text format options
|
|
121
|
+
* @returns {string} Text format output
|
|
122
|
+
*/
|
|
123
|
+
Type.prototype.toText = function toText(message, options) {
|
|
124
|
+
return textformat.toText(this, message, options);
|
|
125
|
+
};
|
|
105
126
|
};
|
|
106
127
|
|
|
107
128
|
function Tokenizer(source) {
|
package/index.d.ts
CHANGED
|
@@ -313,6 +313,18 @@ export class Field extends FieldBase {
|
|
|
313
313
|
/** Determines whether this field tracks presence. */
|
|
314
314
|
readonly hasPresence: boolean;
|
|
315
315
|
|
|
316
|
+
/**
|
|
317
|
+
* The field name as declared in the .proto source (snake_case). Populated on resolve,
|
|
318
|
+
* falling back to `name`. Mirrors `FieldDescriptorProto.name`.
|
|
319
|
+
*/
|
|
320
|
+
readonly protoName: string;
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* The JSON name of this field (lowerCamelCase per protoc's `ToJsonName`, or an
|
|
324
|
+
* explicit `[json_name]`). Populated on resolve. This is the key used on ProtoJSON output.
|
|
325
|
+
*/
|
|
326
|
+
readonly jsonName: string;
|
|
327
|
+
|
|
316
328
|
/**
|
|
317
329
|
* Field decorator (TypeScript).
|
|
318
330
|
* @param fieldId Field id
|
|
@@ -388,6 +400,12 @@ export class FieldBase extends ReflectionObject {
|
|
|
388
400
|
/** Comment for this field. */
|
|
389
401
|
comment: (string|null);
|
|
390
402
|
|
|
403
|
+
/** Field name as declared in the .proto source, if different from `name`. */
|
|
404
|
+
protoName?: string;
|
|
405
|
+
|
|
406
|
+
/** JSON name, if different from the derived default. */
|
|
407
|
+
jsonName?: string;
|
|
408
|
+
|
|
391
409
|
/**
|
|
392
410
|
* Converts this field to a field descriptor.
|
|
393
411
|
* @param [toJSONOptions] JSON conversion options
|
|
@@ -666,13 +684,16 @@ export class Method extends ReflectionObject {
|
|
|
666
684
|
requestType: string;
|
|
667
685
|
|
|
668
686
|
/** Whether requests are streamed or not. */
|
|
669
|
-
requestStream?:
|
|
687
|
+
requestStream?: true;
|
|
670
688
|
|
|
671
689
|
/** Response type. */
|
|
672
690
|
responseType: string;
|
|
673
691
|
|
|
674
692
|
/** Whether responses are streamed or not. */
|
|
675
|
-
responseStream?:
|
|
693
|
+
responseStream?: true;
|
|
694
|
+
|
|
695
|
+
/** gRPC-style method path. */
|
|
696
|
+
path: string;
|
|
676
697
|
|
|
677
698
|
/** Resolved request type. */
|
|
678
699
|
resolvedRequestType: (Type|null);
|
|
@@ -1204,6 +1225,9 @@ export class Reader {
|
|
|
1204
1225
|
/** Read buffer length. */
|
|
1205
1226
|
len: number;
|
|
1206
1227
|
|
|
1228
|
+
/** Whether to discard unknown fields while decoding. */
|
|
1229
|
+
discardUnknown: boolean;
|
|
1230
|
+
|
|
1207
1231
|
/**
|
|
1208
1232
|
* Creates a new reader using the specified buffer.
|
|
1209
1233
|
* @param buffer Buffer to read from
|
|
@@ -1326,6 +1350,9 @@ export class Reader {
|
|
|
1326
1350
|
/** Recursion limit. */
|
|
1327
1351
|
static recursionLimit: number;
|
|
1328
1352
|
|
|
1353
|
+
/** Whether readers discard unknown fields while decoding. */
|
|
1354
|
+
static discardUnknown: boolean;
|
|
1355
|
+
|
|
1329
1356
|
/**
|
|
1330
1357
|
* Skips the next element of the specified wire type.
|
|
1331
1358
|
* @param wireType Wire type received
|
|
@@ -1453,13 +1480,17 @@ export namespace rpc {
|
|
|
1453
1480
|
*/
|
|
1454
1481
|
type ServiceMethodCallback<TRes extends Message<TRes>> = (error: (Error|null), response?: TRes) => void;
|
|
1455
1482
|
|
|
1456
|
-
/**
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1483
|
+
/** A service method part of a {@link rpc.Service} as created by {@link Service.create}. */
|
|
1484
|
+
type ServiceMethod<TReq extends Message<TReq>, TRes extends Message<TRes>> = {
|
|
1485
|
+
(request: TReq|Properties<TReq>, callback: rpc.ServiceMethodCallback<TRes>): void;
|
|
1486
|
+
(request: TReq|Properties<TReq>): Promise<TRes>;
|
|
1487
|
+
readonly name: string;
|
|
1488
|
+
readonly path: string;
|
|
1489
|
+
readonly requestType: string;
|
|
1490
|
+
readonly responseType: string;
|
|
1491
|
+
readonly requestStream: true|undefined;
|
|
1492
|
+
readonly responseStream: true|undefined;
|
|
1493
|
+
};
|
|
1463
1494
|
|
|
1464
1495
|
/** An RPC service as returned by {@link Service#create}. */
|
|
1465
1496
|
class Service extends util.EventEmitter {
|
|
@@ -2662,9 +2693,17 @@ export namespace util {
|
|
|
2662
2693
|
* Converts a string to camel case.
|
|
2663
2694
|
* @param str String to convert
|
|
2664
2695
|
* @returns Converted string
|
|
2696
|
+
* @deprecated Use {@link util.jsonName} for protobuf field JSON names.
|
|
2665
2697
|
*/
|
|
2666
2698
|
function camelCase(str: string): string;
|
|
2667
2699
|
|
|
2700
|
+
/**
|
|
2701
|
+
* Converts a proto field name to its protoc-compatible JSON name.
|
|
2702
|
+
* @param str Proto field name
|
|
2703
|
+
* @returns JSON name
|
|
2704
|
+
*/
|
|
2705
|
+
function jsonName(str: string): string;
|
|
2706
|
+
|
|
2668
2707
|
/**
|
|
2669
2708
|
* Compares reflected fields by id.
|
|
2670
2709
|
* @param a First field
|
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "protobufjs",
|
|
3
|
-
"version": "8.
|
|
4
|
-
"versionScheme": "~",
|
|
3
|
+
"version": "8.6.0",
|
|
5
4
|
"description": "Protocol Buffers for JavaScript & TypeScript.",
|
|
6
5
|
"author": "Daniel Wirtz <dcode+protobufjs@dcode.io>",
|
|
7
6
|
"license": "BSD-3-Clause",
|
|
@@ -26,7 +25,8 @@
|
|
|
26
25
|
"bench": "node bench",
|
|
27
26
|
"build": "npm run build:bundle && npm run build:types",
|
|
28
27
|
"build:bundle": "gulp --gulpfile scripts/gulpfile.js",
|
|
29
|
-
"build:
|
|
28
|
+
"build:tests": "node ./scripts/gentests.js",
|
|
29
|
+
"build:types": "node cli/bin/pbts --main --global protobuf --out index.d.ts src/ && node cli/bin/pbts --main --import \"\\$protobuf=..\" --out ext/descriptor.generated.d.ts ext/descriptor.js && node cli/bin/pbts --main --import \"\\$protobuf=..\" --out ext/textformat.generated.d.ts ext/textformat.js && node cli/bin/pbts --main --import \"\\$protobuf=..\" --out ext/protojson.generated.d.ts ext/protojson.js",
|
|
30
30
|
"coverage": "npm run coverage:test && npm run coverage:report",
|
|
31
31
|
"coverage:test": "nyc --silent tape -r ./lib/tape-adapter tests/*.js tests/node/*.js",
|
|
32
32
|
"coverage:report": "nyc report --reporter=lcov --reporter=text",
|
|
@@ -38,12 +38,11 @@
|
|
|
38
38
|
"lint:types": "tslint \"**/*.d.ts\" -e \"**/node_modules/**\" -t stylish -c config/tslint.json",
|
|
39
39
|
"prepublish": "cd cli && npm install && cd .. && npm run build",
|
|
40
40
|
"prepublishOnly": "cd cli && npm install && cd .. && npm run build",
|
|
41
|
-
"postinstall": "node scripts/postinstall",
|
|
42
41
|
"prof": "node bench/prof",
|
|
43
42
|
"test": "npm run test:sources && npm run test:types",
|
|
44
43
|
"test:sources": "tape -r ./lib/tape-adapter tests/*.js tests/node/*.js",
|
|
45
44
|
"test:types": "tsc tests/comp_typescript.ts --target es2019 --module commonjs --types node --lib es2019 --esModuleInterop --strictNullChecks --experimentalDecorators --emitDecoratorMetadata && tsc -p tsconfig.test-types.json",
|
|
46
|
-
"make": "npm run lint:sources && npm run build && npm run lint:types &&
|
|
45
|
+
"make": "npm run lint:sources && npm run build && npm run lint:types && npm run build:tests && npm test"
|
|
47
46
|
},
|
|
48
47
|
"dependencies": {
|
|
49
48
|
"long": "^5.3.2"
|
|
@@ -73,7 +72,6 @@
|
|
|
73
72
|
"tape": "^5.0.0",
|
|
74
73
|
"tslint": "^6.0.0",
|
|
75
74
|
"typescript": "^3.7.5",
|
|
76
|
-
"uglify-js": "^3.7.7",
|
|
77
75
|
"vinyl-buffer": "^1.0.1",
|
|
78
76
|
"vinyl-fs": "^4.0.0",
|
|
79
77
|
"vinyl-source-stream": "^2.0.0"
|
|
@@ -87,7 +85,6 @@
|
|
|
87
85
|
"minimal.js",
|
|
88
86
|
"package-lock.json",
|
|
89
87
|
"tsconfig.json",
|
|
90
|
-
"scripts/postinstall.js",
|
|
91
88
|
"dist/**",
|
|
92
89
|
"ext/**",
|
|
93
90
|
"google/**",
|
package/src/converter.js
CHANGED
|
@@ -42,7 +42,7 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
|
|
|
42
42
|
} gen
|
|
43
43
|
("}");
|
|
44
44
|
} else gen
|
|
45
|
-
("if(
|
|
45
|
+
("if(!util.isObject(d%s))", prop)
|
|
46
46
|
("throw TypeError(%j)", field.fullName + ": object expected")
|
|
47
47
|
("m%s=types[%i].fromObject(d%s,q+1)", prop, fieldIndex, prop);
|
|
48
48
|
} else {
|
|
@@ -106,9 +106,11 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
|
|
|
106
106
|
converter.fromObject = function fromObject(mtype) {
|
|
107
107
|
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
|
|
108
108
|
var fields = mtype.fieldsArray;
|
|
109
|
-
var gen = util.codegen(["d", "q"]
|
|
109
|
+
var gen = util.codegen(["d", "q"])
|
|
110
110
|
("if(d instanceof C)")
|
|
111
111
|
("return d")
|
|
112
|
+
("if(!util.isObject(d))")
|
|
113
|
+
("throw TypeError(%j)", mtype.fullName + ": object expected")
|
|
112
114
|
("if(q===undefined)q=0")
|
|
113
115
|
("if(q>util.recursionLimit)")
|
|
114
116
|
("throw Error(\"max depth exceeded\")");
|
|
@@ -125,7 +127,7 @@ converter.fromObject = function fromObject(mtype) {
|
|
|
125
127
|
// Map fields
|
|
126
128
|
if (field.map) { gen
|
|
127
129
|
("if(d%s){", prop)
|
|
128
|
-
("if(
|
|
130
|
+
("if(!util.isObject(d%s))", prop)
|
|
129
131
|
("throw TypeError(%j)", field.fullName + ": object expected")
|
|
130
132
|
("m%s={}", prop)
|
|
131
133
|
("for(var ks=Object.keys(d%s),i=0;i<ks.length;++i){", prop);
|
|
@@ -238,7 +240,7 @@ converter.toObject = function toObject(mtype) {
|
|
|
238
240
|
var fields = mtype.fieldsArray.slice().sort(util.compareFieldsById);
|
|
239
241
|
if (!fields.length)
|
|
240
242
|
return util.codegen()("return {}");
|
|
241
|
-
var gen = util.codegen(["m", "o", "q"]
|
|
243
|
+
var gen = util.codegen(["m", "o", "q"])
|
|
242
244
|
("if(!o)")
|
|
243
245
|
("o={}")
|
|
244
246
|
("if(q===undefined)q=0")
|
|
@@ -327,7 +329,7 @@ converter.toObject = function toObject(mtype) {
|
|
|
327
329
|
genValuePartial_toObject(gen, field, /* sorted */ index, prop + "[j]")
|
|
328
330
|
("}");
|
|
329
331
|
} else { gen
|
|
330
|
-
("if(m%s!=null&&
|
|
332
|
+
("if(m%s!=null&&Object.hasOwnProperty.call(m,%j)){", prop, field.name); // !== undefined && !== null
|
|
331
333
|
genValuePartial_toObject(gen, field, /* sorted */ index, prop);
|
|
332
334
|
if (field.partOf && !field.partOf.isProto3Optional) gen
|
|
333
335
|
("if(o.oneofs)")
|
package/src/decoder.js
CHANGED
|
@@ -26,7 +26,7 @@ function decoder(mtype) {
|
|
|
26
26
|
if (!pfield.repeated && !pfield.map && !pfield.hasPresence)
|
|
27
27
|
hasImplicitPresenceField = true;
|
|
28
28
|
}
|
|
29
|
-
var gen = util.codegen(["r", "l", "z", "q", "g"]
|
|
29
|
+
var gen = util.codegen(["r", "l", "z", "q", "g"])
|
|
30
30
|
("if(!(r instanceof Reader))")
|
|
31
31
|
("r=Reader.create(r)")
|
|
32
32
|
("if(q===undefined)q=0")
|
|
@@ -186,8 +186,10 @@ function decoder(mtype) {
|
|
|
186
186
|
// Unknown fields
|
|
187
187
|
gen
|
|
188
188
|
("r.skipType(%s,q,t)", i ? "u" : "t&7")
|
|
189
|
-
("
|
|
190
|
-
|
|
189
|
+
("if(!r.discardUnknown){")
|
|
190
|
+
("util.makeProp(m,\"$unknowns\",false);")
|
|
191
|
+
("(m.$unknowns||(m.$unknowns=[])).push(r.raw(s,r.pos))")
|
|
192
|
+
("}")
|
|
191
193
|
("}")
|
|
192
194
|
("if(z!==undefined)")
|
|
193
195
|
("throw Error(\"missing end group\")");
|
|
@@ -196,7 +198,7 @@ function decoder(mtype) {
|
|
|
196
198
|
for (i = 0; i < mtype._fieldsArray.length; ++i) {
|
|
197
199
|
var rfield = mtype._fieldsArray[i];
|
|
198
200
|
if (rfield.required) gen
|
|
199
|
-
("if(!
|
|
201
|
+
("if(!Object.hasOwnProperty.call(m,%j))", rfield.name)
|
|
200
202
|
("throw util.ProtocolError(%j,{instance:m})", missing(rfield));
|
|
201
203
|
}
|
|
202
204
|
|
package/src/encoder.js
CHANGED
|
@@ -27,7 +27,7 @@ function genTypePartial(gen, field, fieldIndex, ref) {
|
|
|
27
27
|
*/
|
|
28
28
|
function encoder(mtype) {
|
|
29
29
|
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
|
|
30
|
-
var gen = util.codegen(["m", "w", "q"]
|
|
30
|
+
var gen = util.codegen(["m", "w", "q"])
|
|
31
31
|
("if(!w)")
|
|
32
32
|
("w=Writer.create()")
|
|
33
33
|
("if(q===undefined)q=0")
|
package/src/field.js
CHANGED
|
@@ -38,6 +38,12 @@ Field.fromJSON = function fromJSON(name, json) {
|
|
|
38
38
|
var field = new Field(name, json.id, json.type, json.rule, json.extend, json.options, json.comment);
|
|
39
39
|
if (json.edition)
|
|
40
40
|
field._edition = json.edition;
|
|
41
|
+
if (json.protoName)
|
|
42
|
+
field.protoName = json.protoName;
|
|
43
|
+
if (json.jsonName !== undefined)
|
|
44
|
+
field.jsonName = json.jsonName;
|
|
45
|
+
else if (json.options && json.options.json_name !== undefined)
|
|
46
|
+
field.jsonName = json.options.json_name;
|
|
41
47
|
field._defaultEdition = "proto3"; // For backwards-compatibility.
|
|
42
48
|
return field;
|
|
43
49
|
};
|
|
@@ -177,6 +183,18 @@ function Field(name, id, type, rule, extend, options, comment) {
|
|
|
177
183
|
* @type {string|null}
|
|
178
184
|
*/
|
|
179
185
|
this.comment = comment;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Field name as declared in the .proto source, if different from `name`.
|
|
189
|
+
* @type {string|undefined}
|
|
190
|
+
*/
|
|
191
|
+
this.protoName = undefined;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* JSON name, if different from the derived default.
|
|
195
|
+
* @type {string|undefined}
|
|
196
|
+
*/
|
|
197
|
+
this.jsonName = undefined;
|
|
180
198
|
}
|
|
181
199
|
|
|
182
200
|
/**
|
|
@@ -246,6 +264,22 @@ Object.defineProperty(Field.prototype, "hasPresence", {
|
|
|
246
264
|
}
|
|
247
265
|
});
|
|
248
266
|
|
|
267
|
+
/**
|
|
268
|
+
* The field name as declared in the .proto source (snake_case). Populated on resolve,
|
|
269
|
+
* falling back to `name`. Mirrors `FieldDescriptorProto.name`.
|
|
270
|
+
* @name Field#protoName
|
|
271
|
+
* @type {string}
|
|
272
|
+
* @readonly
|
|
273
|
+
*/
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* The JSON name of this field (lowerCamelCase per protoc's `ToJsonName`, or an
|
|
277
|
+
* explicit `[json_name]`). Populated on resolve. This is the key used on ProtoJSON output.
|
|
278
|
+
* @name Field#jsonName
|
|
279
|
+
* @type {string}
|
|
280
|
+
* @readonly
|
|
281
|
+
*/
|
|
282
|
+
|
|
249
283
|
/**
|
|
250
284
|
* @override
|
|
251
285
|
*/
|
|
@@ -279,13 +313,15 @@ Field.prototype.setOption = function setOption(name, value, ifNotSet) {
|
|
|
279
313
|
Field.prototype.toJSON = function toJSON(toJSONOptions) {
|
|
280
314
|
var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
|
|
281
315
|
return util.toObject([
|
|
282
|
-
"edition"
|
|
283
|
-
"rule"
|
|
284
|
-
"type"
|
|
285
|
-
"id"
|
|
286
|
-
"extend"
|
|
287
|
-
"
|
|
288
|
-
"
|
|
316
|
+
"edition" , this._editionToJSON(),
|
|
317
|
+
"rule" , this.rule !== "optional" && this.rule || undefined,
|
|
318
|
+
"type" , this.type,
|
|
319
|
+
"id" , this.id,
|
|
320
|
+
"extend" , this.extend,
|
|
321
|
+
"protoName" , this.protoName !== this.name ? this.protoName : undefined,
|
|
322
|
+
"jsonName" , this.jsonName !== util.jsonName(this.protoName || this.name) ? this.jsonName : undefined,
|
|
323
|
+
"options" , this.options,
|
|
324
|
+
"comment" , keepComments ? this.comment : undefined
|
|
289
325
|
]);
|
|
290
326
|
};
|
|
291
327
|
|
|
@@ -354,6 +390,12 @@ Field.prototype.resolve = function resolve() {
|
|
|
354
390
|
if (this.parent instanceof Type && this.parent._ctor)
|
|
355
391
|
this.parent._ctor.prototype[this.name] = this.defaultValue;
|
|
356
392
|
|
|
393
|
+
// derive the proto/JSON names
|
|
394
|
+
if (this.protoName === undefined)
|
|
395
|
+
this.protoName = this.name;
|
|
396
|
+
if (this.jsonName === undefined)
|
|
397
|
+
this.jsonName = util.jsonName(this.protoName);
|
|
398
|
+
|
|
357
399
|
return ReflectionObject.prototype.resolve.call(this);
|
|
358
400
|
};
|
|
359
401
|
|
package/src/mapfield.js
CHANGED
|
@@ -65,7 +65,14 @@ function MapField(name, id, keyType, type, options, comment) {
|
|
|
65
65
|
* @throws {TypeError} If arguments are invalid
|
|
66
66
|
*/
|
|
67
67
|
MapField.fromJSON = function fromJSON(name, json) {
|
|
68
|
-
|
|
68
|
+
var field = new MapField(name, json.id, json.keyType, json.type, json.options, json.comment);
|
|
69
|
+
if (json.protoName)
|
|
70
|
+
field.protoName = json.protoName;
|
|
71
|
+
if (json.jsonName !== undefined)
|
|
72
|
+
field.jsonName = json.jsonName;
|
|
73
|
+
else if (json.options && json.options.json_name !== undefined)
|
|
74
|
+
field.jsonName = json.options.json_name;
|
|
75
|
+
return field;
|
|
69
76
|
};
|
|
70
77
|
|
|
71
78
|
/**
|
|
@@ -76,12 +83,14 @@ MapField.fromJSON = function fromJSON(name, json) {
|
|
|
76
83
|
MapField.prototype.toJSON = function toJSON(toJSONOptions) {
|
|
77
84
|
var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
|
|
78
85
|
return util.toObject([
|
|
79
|
-
"keyType"
|
|
80
|
-
"type"
|
|
81
|
-
"id"
|
|
82
|
-
"extend"
|
|
83
|
-
"
|
|
84
|
-
"
|
|
86
|
+
"keyType" , this.keyType,
|
|
87
|
+
"type" , this.type,
|
|
88
|
+
"id" , this.id,
|
|
89
|
+
"extend" , this.extend,
|
|
90
|
+
"protoName" , this.protoName !== this.name ? this.protoName : undefined,
|
|
91
|
+
"jsonName" , this.jsonName !== util.jsonName(this.protoName || this.name) ? this.jsonName : undefined,
|
|
92
|
+
"options" , this.options,
|
|
93
|
+
"comment" , keepComments ? this.comment : undefined
|
|
85
94
|
]);
|
|
86
95
|
};
|
|
87
96
|
|
package/src/method.js
CHANGED
|
@@ -61,7 +61,7 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
|
|
|
61
61
|
|
|
62
62
|
/**
|
|
63
63
|
* Whether requests are streamed or not.
|
|
64
|
-
* @type {
|
|
64
|
+
* @type {true|undefined}
|
|
65
65
|
*/
|
|
66
66
|
this.requestStream = requestStream ? true : undefined; // toJSON
|
|
67
67
|
|
|
@@ -73,10 +73,16 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
|
|
|
73
73
|
|
|
74
74
|
/**
|
|
75
75
|
* Whether responses are streamed or not.
|
|
76
|
-
* @type {
|
|
76
|
+
* @type {true|undefined}
|
|
77
77
|
*/
|
|
78
78
|
this.responseStream = responseStream ? true : undefined; // toJSON
|
|
79
79
|
|
|
80
|
+
/**
|
|
81
|
+
* gRPC-style method path.
|
|
82
|
+
* @type {string}
|
|
83
|
+
*/
|
|
84
|
+
this.path = "/" + this.name;
|
|
85
|
+
|
|
80
86
|
/**
|
|
81
87
|
* Resolved request type.
|
|
82
88
|
* @type {Type|null}
|
|
@@ -154,6 +160,14 @@ Method.prototype.resolve = function resolve() {
|
|
|
154
160
|
if (this.resolved)
|
|
155
161
|
return this;
|
|
156
162
|
|
|
163
|
+
if (this.parent) {
|
|
164
|
+
var serviceName = this.parent.fullName;
|
|
165
|
+
if (serviceName.charAt(0) === ".")
|
|
166
|
+
serviceName = serviceName.substring(1);
|
|
167
|
+
this.path = "/" + serviceName + "/" + this.name;
|
|
168
|
+
} else
|
|
169
|
+
this.path = "/" + this.name;
|
|
170
|
+
|
|
157
171
|
this.resolvedRequestType = this.parent.lookupType(this.requestType);
|
|
158
172
|
this.resolvedResponseType = this.parent.lookupType(this.responseType);
|
|
159
173
|
|
package/src/namespace.js
CHANGED
|
@@ -426,14 +426,14 @@ Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChe
|
|
|
426
426
|
if (path[0] === "")
|
|
427
427
|
return this.root.lookup(path.slice(1), filterTypes);
|
|
428
428
|
|
|
429
|
-
//
|
|
430
|
-
var found = this.
|
|
429
|
+
// Lookup at this namespace and below
|
|
430
|
+
var found = this._lookupImpl(path, flatPath);
|
|
431
431
|
if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
|
|
432
432
|
return found;
|
|
433
433
|
}
|
|
434
434
|
|
|
435
|
-
//
|
|
436
|
-
found = this.
|
|
435
|
+
// Fall back to respective absolute path once relative scope has been checked (non-standard)
|
|
436
|
+
found = this.root._fullyQualifiedObjects && this.root._fullyQualifiedObjects["." + flatPath];
|
|
437
437
|
if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
|
|
438
438
|
return found;
|
|
439
439
|
}
|
package/src/parse.js
CHANGED
|
@@ -482,10 +482,13 @@ function parse(source, root, options) {
|
|
|
482
482
|
if (!nameRe.test(name))
|
|
483
483
|
throw illegal(name, "name");
|
|
484
484
|
|
|
485
|
+
var protoName = name;
|
|
485
486
|
name = applyCase(name);
|
|
486
487
|
skip("=");
|
|
487
488
|
|
|
488
489
|
var field = new Field(name, parseId(next()), type, rule === "proto3_optional" ? "optional" : rule, extend);
|
|
490
|
+
if (protoName !== name)
|
|
491
|
+
field.protoName = protoName;
|
|
489
492
|
|
|
490
493
|
ifBlock(field, function parseField_block(token) {
|
|
491
494
|
|
|
@@ -627,7 +630,11 @@ function parse(source, root, options) {
|
|
|
627
630
|
throw illegal(name, "name");
|
|
628
631
|
|
|
629
632
|
skip("=");
|
|
630
|
-
var
|
|
633
|
+
var protoName = name;
|
|
634
|
+
name = applyCase(name);
|
|
635
|
+
var field = new MapField(name, parseId(next()), keyType, valueType);
|
|
636
|
+
if (protoName !== name)
|
|
637
|
+
field.protoName = protoName;
|
|
631
638
|
ifBlock(field, function parseMapField_block(token) {
|
|
632
639
|
|
|
633
640
|
/* istanbul ignore else */
|
|
@@ -839,11 +846,18 @@ function parse(source, root, options) {
|
|
|
839
846
|
topLevelOptions[name] = value;
|
|
840
847
|
return;
|
|
841
848
|
}
|
|
849
|
+
// lift json_name onto Field
|
|
850
|
+
if (name === "json_name" && parent instanceof Field) {
|
|
851
|
+
parent.jsonName = value;
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
842
854
|
if (parent.setOption)
|
|
843
855
|
parent.setOption(name, value);
|
|
844
856
|
}
|
|
845
857
|
|
|
846
858
|
function setParsedOption(parent, name, value, propName) {
|
|
859
|
+
if (name === "json_name" && parent instanceof Field)
|
|
860
|
+
return; // lifted onto Field#jsonName above
|
|
847
861
|
if (parent.setParsedOption)
|
|
848
862
|
parent.setParsedOption(name, value, propName);
|
|
849
863
|
}
|
package/src/reader.js
CHANGED
|
@@ -38,6 +38,12 @@ function Reader(buffer) {
|
|
|
38
38
|
* @type {number}
|
|
39
39
|
*/
|
|
40
40
|
this.len = buffer.length;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Whether to discard unknown fields while decoding.
|
|
44
|
+
* @type {boolean}
|
|
45
|
+
*/
|
|
46
|
+
this.discardUnknown = Reader.discardUnknown;
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
var create_array = typeof Uint8Array !== "undefined"
|
|
@@ -450,6 +456,12 @@ Reader.prototype.skip = function skip(length) {
|
|
|
450
456
|
*/
|
|
451
457
|
Reader.recursionLimit = util.recursionLimit;
|
|
452
458
|
|
|
459
|
+
/**
|
|
460
|
+
* Whether readers discard unknown fields while decoding.
|
|
461
|
+
* @type {boolean}
|
|
462
|
+
*/
|
|
463
|
+
Reader.discardUnknown = false;
|
|
464
|
+
|
|
453
465
|
/**
|
|
454
466
|
* Skips the next element of the specified wire type.
|
|
455
467
|
* @param {number} wireType Wire type received
|
package/src/roots.js
CHANGED