@depup/apollo__protobufjs 1.2.7-depup.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/LICENSE +39 -0
- package/README.md +32 -0
- package/bin/pbjs +6 -0
- package/bin/pbts +6 -0
- package/changes.json +14 -0
- package/cli/LICENSE +33 -0
- package/cli/README.md +11 -0
- package/cli/bin/pbjs +6 -0
- package/cli/bin/pbts +6 -0
- package/cli/index.d.ts +3 -0
- package/cli/index.js +3 -0
- package/cli/lib/tsd-jsdoc/LICENSE +21 -0
- package/cli/lib/tsd-jsdoc/README.md +23 -0
- package/cli/lib/tsd-jsdoc/plugin.js +21 -0
- package/cli/lib/tsd-jsdoc/publish.js +693 -0
- package/cli/lib/tsd-jsdoc.json +18 -0
- package/cli/package.json +7 -0
- package/cli/package.standalone.json +32 -0
- package/cli/pbjs.d.ts +9 -0
- package/cli/pbjs.js +331 -0
- package/cli/pbts.d.ts +9 -0
- package/cli/pbts.js +198 -0
- package/cli/targets/json-module.js +38 -0
- package/cli/targets/json.js +8 -0
- package/cli/targets/proto.js +326 -0
- package/cli/targets/proto2.js +10 -0
- package/cli/targets/proto3.js +10 -0
- package/cli/targets/static-module.js +29 -0
- package/cli/targets/static.js +709 -0
- package/cli/util.js +183 -0
- package/cli/wrappers/amd.js +7 -0
- package/cli/wrappers/closure.js +7 -0
- package/cli/wrappers/commonjs.js +7 -0
- package/cli/wrappers/default.js +15 -0
- package/cli/wrappers/es6.js +5 -0
- package/dist/README.md +31 -0
- package/dist/light/README.md +31 -0
- package/dist/light/protobuf.js +7198 -0
- package/dist/light/protobuf.js.map +1 -0
- package/dist/light/protobuf.min.js +7 -0
- package/dist/light/protobuf.min.js.map +1 -0
- package/dist/minimal/README.md +31 -0
- package/dist/minimal/protobuf.js +2675 -0
- package/dist/minimal/protobuf.js.map +1 -0
- package/dist/minimal/protobuf.min.js +7 -0
- package/dist/minimal/protobuf.min.js.map +1 -0
- package/dist/protobuf.js +8775 -0
- package/dist/protobuf.js.map +1 -0
- package/dist/protobuf.min.js +7 -0
- package/dist/protobuf.min.js.map +1 -0
- package/ext/debug/README.md +4 -0
- package/ext/debug/index.js +71 -0
- package/ext/descriptor/README.md +72 -0
- package/ext/descriptor/index.d.ts +191 -0
- package/ext/descriptor/index.js +1052 -0
- package/ext/descriptor/test.js +54 -0
- package/google/LICENSE +27 -0
- package/google/README.md +1 -0
- package/google/api/annotations.json +83 -0
- package/google/api/annotations.proto +11 -0
- package/google/api/http.json +86 -0
- package/google/api/http.proto +31 -0
- package/google/protobuf/api.json +118 -0
- package/google/protobuf/api.proto +34 -0
- package/google/protobuf/descriptor.json +739 -0
- package/google/protobuf/descriptor.proto +286 -0
- package/google/protobuf/source_context.json +20 -0
- package/google/protobuf/source_context.proto +7 -0
- package/google/protobuf/type.json +202 -0
- package/google/protobuf/type.proto +89 -0
- package/index.d.ts +2628 -0
- package/index.js +4 -0
- package/light.d.ts +2 -0
- package/light.js +4 -0
- package/minimal.d.ts +2 -0
- package/minimal.js +4 -0
- package/package.json +147 -0
- package/scripts/postinstall.js +35 -0
- package/src/common.js +399 -0
- package/src/converter.js +304 -0
- package/src/decoder.js +106 -0
- package/src/encoder.js +119 -0
- package/src/enum.js +181 -0
- package/src/field.js +379 -0
- package/src/index-light.js +104 -0
- package/src/index-minimal.js +36 -0
- package/src/index.js +12 -0
- package/src/mapfield.js +126 -0
- package/src/message.js +139 -0
- package/src/method.js +151 -0
- package/src/namespace.js +433 -0
- package/src/object.js +200 -0
- package/src/oneof.js +203 -0
- package/src/parse.js +761 -0
- package/src/reader.js +405 -0
- package/src/reader_buffer.js +44 -0
- package/src/root.js +353 -0
- package/src/roots.js +18 -0
- package/src/rpc/service.js +142 -0
- package/src/rpc.js +36 -0
- package/src/service.js +167 -0
- package/src/tokenize.js +397 -0
- package/src/type.js +589 -0
- package/src/types.js +196 -0
- package/src/typescript.jsdoc +22 -0
- package/src/util/longbits.js +200 -0
- package/src/util/minimal.js +406 -0
- package/src/util.js +178 -0
- package/src/verifier.js +191 -0
- package/src/wrappers.js +83 -0
- package/src/writer.js +459 -0
- package/src/writer_buffer.js +81 -0
- package/tsconfig.json +7 -0
package/src/enum.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = Enum;
|
|
3
|
+
|
|
4
|
+
// extends ReflectionObject
|
|
5
|
+
var ReflectionObject = require("./object");
|
|
6
|
+
((Enum.prototype = Object.create(ReflectionObject.prototype)).constructor = Enum).className = "Enum";
|
|
7
|
+
|
|
8
|
+
var Namespace = require("./namespace"),
|
|
9
|
+
util = require("./util");
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Constructs a new enum instance.
|
|
13
|
+
* @classdesc Reflected enum.
|
|
14
|
+
* @extends ReflectionObject
|
|
15
|
+
* @constructor
|
|
16
|
+
* @param {string} name Unique name within its namespace
|
|
17
|
+
* @param {Object.<string,number>} [values] Enum values as an object, by name
|
|
18
|
+
* @param {Object.<string,*>} [options] Declared options
|
|
19
|
+
* @param {string} [comment] The comment for this enum
|
|
20
|
+
* @param {Object.<string,string>} [comments] The value comments for this enum
|
|
21
|
+
*/
|
|
22
|
+
function Enum(name, values, options, comment, comments) {
|
|
23
|
+
ReflectionObject.call(this, name, options);
|
|
24
|
+
|
|
25
|
+
if (values && typeof values !== "object")
|
|
26
|
+
throw TypeError("values must be an object");
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Enum values by id.
|
|
30
|
+
* @type {Object.<number,string>}
|
|
31
|
+
*/
|
|
32
|
+
this.valuesById = {};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Enum values by name.
|
|
36
|
+
* @type {Object.<string,number>}
|
|
37
|
+
*/
|
|
38
|
+
this.values = Object.create(this.valuesById); // toJSON, marker
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Enum comment text.
|
|
42
|
+
* @type {string|null}
|
|
43
|
+
*/
|
|
44
|
+
this.comment = comment;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Value comment texts, if any.
|
|
48
|
+
* @type {Object.<string,string>}
|
|
49
|
+
*/
|
|
50
|
+
this.comments = comments || {};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Reserved ranges, if any.
|
|
54
|
+
* @type {Array.<number[]|string>}
|
|
55
|
+
*/
|
|
56
|
+
this.reserved = undefined; // toJSON
|
|
57
|
+
|
|
58
|
+
// Note that values inherit valuesById on their prototype which makes them a TypeScript-
|
|
59
|
+
// compatible enum. This is used by pbts to write actual enum definitions that work for
|
|
60
|
+
// static and reflection code alike instead of emitting generic object definitions.
|
|
61
|
+
|
|
62
|
+
if (values)
|
|
63
|
+
for (var keys = Object.keys(values), i = 0; i < keys.length; ++i)
|
|
64
|
+
if (typeof values[keys[i]] === "number") // use forward entries only
|
|
65
|
+
this.valuesById[ this.values[keys[i]] = values[keys[i]] ] = keys[i];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Enum descriptor.
|
|
70
|
+
* @interface IEnum
|
|
71
|
+
* @property {Object.<string,number>} values Enum values
|
|
72
|
+
* @property {Object.<string,*>} [options] Enum options
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Constructs an enum from an enum descriptor.
|
|
77
|
+
* @param {string} name Enum name
|
|
78
|
+
* @param {IEnum} json Enum descriptor
|
|
79
|
+
* @returns {Enum} Created enum
|
|
80
|
+
* @throws {TypeError} If arguments are invalid
|
|
81
|
+
*/
|
|
82
|
+
Enum.fromJSON = function fromJSON(name, json) {
|
|
83
|
+
var enm = new Enum(name, json.values, json.options, json.comment, json.comments);
|
|
84
|
+
enm.reserved = json.reserved;
|
|
85
|
+
return enm;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Converts this enum to an enum descriptor.
|
|
90
|
+
* @param {IToJSONOptions} [toJSONOptions] JSON conversion options
|
|
91
|
+
* @returns {IEnum} Enum descriptor
|
|
92
|
+
*/
|
|
93
|
+
Enum.prototype.toJSON = function toJSON(toJSONOptions) {
|
|
94
|
+
var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
|
|
95
|
+
return util.toObject([
|
|
96
|
+
"options" , this.options,
|
|
97
|
+
"values" , this.values,
|
|
98
|
+
"reserved" , this.reserved && this.reserved.length ? this.reserved : undefined,
|
|
99
|
+
"comment" , keepComments ? this.comment : undefined,
|
|
100
|
+
"comments" , keepComments ? this.comments : undefined
|
|
101
|
+
]);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Adds a value to this enum.
|
|
106
|
+
* @param {string} name Value name
|
|
107
|
+
* @param {number} id Value id
|
|
108
|
+
* @param {string} [comment] Comment, if any
|
|
109
|
+
* @returns {Enum} `this`
|
|
110
|
+
* @throws {TypeError} If arguments are invalid
|
|
111
|
+
* @throws {Error} If there is already a value with this name or id
|
|
112
|
+
*/
|
|
113
|
+
Enum.prototype.add = function add(name, id, comment) {
|
|
114
|
+
// utilized by the parser but not by .fromJSON
|
|
115
|
+
|
|
116
|
+
if (!util.isString(name))
|
|
117
|
+
throw TypeError("name must be a string");
|
|
118
|
+
|
|
119
|
+
if (!util.isInteger(id))
|
|
120
|
+
throw TypeError("id must be an integer");
|
|
121
|
+
|
|
122
|
+
if (this.values[name] !== undefined)
|
|
123
|
+
throw Error("duplicate name '" + name + "' in " + this);
|
|
124
|
+
|
|
125
|
+
if (this.isReservedId(id))
|
|
126
|
+
throw Error("id " + id + " is reserved in " + this);
|
|
127
|
+
|
|
128
|
+
if (this.isReservedName(name))
|
|
129
|
+
throw Error("name '" + name + "' is reserved in " + this);
|
|
130
|
+
|
|
131
|
+
if (this.valuesById[id] !== undefined) {
|
|
132
|
+
if (!(this.options && this.options.allow_alias))
|
|
133
|
+
throw Error("duplicate id " + id + " in " + this);
|
|
134
|
+
this.values[name] = id;
|
|
135
|
+
} else
|
|
136
|
+
this.valuesById[this.values[name] = id] = name;
|
|
137
|
+
|
|
138
|
+
this.comments[name] = comment || null;
|
|
139
|
+
return this;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Removes a value from this enum
|
|
144
|
+
* @param {string} name Value name
|
|
145
|
+
* @returns {Enum} `this`
|
|
146
|
+
* @throws {TypeError} If arguments are invalid
|
|
147
|
+
* @throws {Error} If `name` is not a name of this enum
|
|
148
|
+
*/
|
|
149
|
+
Enum.prototype.remove = function remove(name) {
|
|
150
|
+
|
|
151
|
+
if (!util.isString(name))
|
|
152
|
+
throw TypeError("name must be a string");
|
|
153
|
+
|
|
154
|
+
var val = this.values[name];
|
|
155
|
+
if (val == null)
|
|
156
|
+
throw Error("name '" + name + "' does not exist in " + this);
|
|
157
|
+
|
|
158
|
+
delete this.valuesById[val];
|
|
159
|
+
delete this.values[name];
|
|
160
|
+
delete this.comments[name];
|
|
161
|
+
|
|
162
|
+
return this;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Tests if the specified id is reserved.
|
|
167
|
+
* @param {number} id Id to test
|
|
168
|
+
* @returns {boolean} `true` if reserved, otherwise `false`
|
|
169
|
+
*/
|
|
170
|
+
Enum.prototype.isReservedId = function isReservedId(id) {
|
|
171
|
+
return Namespace.isReservedId(this.reserved, id);
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Tests if the specified name is reserved.
|
|
176
|
+
* @param {string} name Name to test
|
|
177
|
+
* @returns {boolean} `true` if reserved, otherwise `false`
|
|
178
|
+
*/
|
|
179
|
+
Enum.prototype.isReservedName = function isReservedName(name) {
|
|
180
|
+
return Namespace.isReservedName(this.reserved, name);
|
|
181
|
+
};
|
package/src/field.js
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = Field;
|
|
3
|
+
|
|
4
|
+
// extends ReflectionObject
|
|
5
|
+
var ReflectionObject = require("./object");
|
|
6
|
+
((Field.prototype = Object.create(ReflectionObject.prototype)).constructor = Field).className = "Field";
|
|
7
|
+
|
|
8
|
+
var Enum = require("./enum"),
|
|
9
|
+
types = require("./types"),
|
|
10
|
+
util = require("./util");
|
|
11
|
+
|
|
12
|
+
var Type; // cyclic
|
|
13
|
+
|
|
14
|
+
var ruleRe = /^required|optional|repeated$/;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Constructs a new message field instance. Note that {@link MapField|map fields} have their own class.
|
|
18
|
+
* @name Field
|
|
19
|
+
* @classdesc Reflected message field.
|
|
20
|
+
* @extends FieldBase
|
|
21
|
+
* @constructor
|
|
22
|
+
* @param {string} name Unique name within its namespace
|
|
23
|
+
* @param {number} id Unique id within its namespace
|
|
24
|
+
* @param {string} type Value type
|
|
25
|
+
* @param {string|Object.<string,*>} [rule="optional"] Field rule
|
|
26
|
+
* @param {string|Object.<string,*>} [extend] Extended type if different from parent
|
|
27
|
+
* @param {Object.<string,*>} [options] Declared options
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Constructs a field from a field descriptor.
|
|
32
|
+
* @param {string} name Field name
|
|
33
|
+
* @param {IField} json Field descriptor
|
|
34
|
+
* @returns {Field} Created field
|
|
35
|
+
* @throws {TypeError} If arguments are invalid
|
|
36
|
+
*/
|
|
37
|
+
Field.fromJSON = function fromJSON(name, json) {
|
|
38
|
+
return new Field(name, json.id, json.type, json.rule, json.extend, json.options, json.comment);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Not an actual constructor. Use {@link Field} instead.
|
|
43
|
+
* @classdesc Base class of all reflected message fields. This is not an actual class but here for the sake of having consistent type definitions.
|
|
44
|
+
* @exports FieldBase
|
|
45
|
+
* @extends ReflectionObject
|
|
46
|
+
* @constructor
|
|
47
|
+
* @param {string} name Unique name within its namespace
|
|
48
|
+
* @param {number} id Unique id within its namespace
|
|
49
|
+
* @param {string} type Value type
|
|
50
|
+
* @param {string|Object.<string,*>} [rule="optional"] Field rule
|
|
51
|
+
* @param {string|Object.<string,*>} [extend] Extended type if different from parent
|
|
52
|
+
* @param {Object.<string,*>} [options] Declared options
|
|
53
|
+
* @param {string} [comment] Comment associated with this field
|
|
54
|
+
*/
|
|
55
|
+
function Field(name, id, type, rule, extend, options, comment) {
|
|
56
|
+
|
|
57
|
+
if (util.isObject(rule)) {
|
|
58
|
+
comment = extend;
|
|
59
|
+
options = rule;
|
|
60
|
+
rule = extend = undefined;
|
|
61
|
+
} else if (util.isObject(extend)) {
|
|
62
|
+
comment = options;
|
|
63
|
+
options = extend;
|
|
64
|
+
extend = undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
ReflectionObject.call(this, name, options);
|
|
68
|
+
|
|
69
|
+
if (!util.isInteger(id) || id < 0)
|
|
70
|
+
throw TypeError("id must be a non-negative integer");
|
|
71
|
+
|
|
72
|
+
if (!util.isString(type))
|
|
73
|
+
throw TypeError("type must be a string");
|
|
74
|
+
|
|
75
|
+
if (rule !== undefined && !ruleRe.test(rule = rule.toString().toLowerCase()))
|
|
76
|
+
throw TypeError("rule must be a string rule");
|
|
77
|
+
|
|
78
|
+
if (extend !== undefined && !util.isString(extend))
|
|
79
|
+
throw TypeError("extend must be a string");
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Field rule, if any.
|
|
83
|
+
* @type {string|undefined}
|
|
84
|
+
*/
|
|
85
|
+
this.rule = rule && rule !== "optional" ? rule : undefined; // toJSON
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Field type.
|
|
89
|
+
* @type {string}
|
|
90
|
+
*/
|
|
91
|
+
this.type = type; // toJSON
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Unique field id.
|
|
95
|
+
* @type {number}
|
|
96
|
+
*/
|
|
97
|
+
this.id = id; // toJSON, marker
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Extended type if different from parent.
|
|
101
|
+
* @type {string|undefined}
|
|
102
|
+
*/
|
|
103
|
+
this.extend = extend || undefined; // toJSON
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Whether this field is required.
|
|
107
|
+
* @type {boolean}
|
|
108
|
+
*/
|
|
109
|
+
this.required = rule === "required";
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Whether this field is optional.
|
|
113
|
+
* @type {boolean}
|
|
114
|
+
*/
|
|
115
|
+
this.optional = !this.required;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Whether this field is repeated.
|
|
119
|
+
* @type {boolean}
|
|
120
|
+
*/
|
|
121
|
+
this.repeated = rule === "repeated";
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Whether this field is a map or not.
|
|
125
|
+
* @type {boolean}
|
|
126
|
+
*/
|
|
127
|
+
this.map = false;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Message this field belongs to.
|
|
131
|
+
* @type {Type|null}
|
|
132
|
+
*/
|
|
133
|
+
this.message = null;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* OneOf this field belongs to, if any,
|
|
137
|
+
* @type {OneOf|null}
|
|
138
|
+
*/
|
|
139
|
+
this.partOf = null;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The field type's default value.
|
|
143
|
+
* @type {*}
|
|
144
|
+
*/
|
|
145
|
+
this.typeDefault = null;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The field's default value on prototypes.
|
|
149
|
+
* @type {*}
|
|
150
|
+
*/
|
|
151
|
+
this.defaultValue = null;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Whether this field's value should be treated as a long.
|
|
155
|
+
* @type {boolean}
|
|
156
|
+
*/
|
|
157
|
+
this.long = util.Long ? types.long[type] !== undefined : /* istanbul ignore next */ false;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Whether this field's value is a buffer.
|
|
161
|
+
* @type {boolean}
|
|
162
|
+
*/
|
|
163
|
+
this.bytes = type === "bytes";
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Resolved type if not a basic type.
|
|
167
|
+
* @type {Type|Enum|null}
|
|
168
|
+
*/
|
|
169
|
+
this.resolvedType = null;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Sister-field within the extended type if a declaring extension field.
|
|
173
|
+
* @type {Field|null}
|
|
174
|
+
*/
|
|
175
|
+
this.extensionField = null;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Sister-field within the declaring namespace if an extended field.
|
|
179
|
+
* @type {Field|null}
|
|
180
|
+
*/
|
|
181
|
+
this.declaringField = null;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Internally remembers whether this field is packed.
|
|
185
|
+
* @type {boolean|null}
|
|
186
|
+
* @private
|
|
187
|
+
*/
|
|
188
|
+
this._packed = null;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Comment for this field.
|
|
192
|
+
* @type {string|null}
|
|
193
|
+
*/
|
|
194
|
+
this.comment = comment;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Determines whether this field is packed. Only relevant when repeated and working with proto2.
|
|
199
|
+
* @name Field#packed
|
|
200
|
+
* @type {boolean}
|
|
201
|
+
* @readonly
|
|
202
|
+
*/
|
|
203
|
+
Object.defineProperty(Field.prototype, "packed", {
|
|
204
|
+
get: function() {
|
|
205
|
+
// defaults to packed=true if not explicity set to false
|
|
206
|
+
if (this._packed === null)
|
|
207
|
+
this._packed = this.getOption("packed") !== false;
|
|
208
|
+
return this._packed;
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* @override
|
|
214
|
+
*/
|
|
215
|
+
Field.prototype.setOption = function setOption(name, value, ifNotSet) {
|
|
216
|
+
if (name === "packed") // clear cached before setting
|
|
217
|
+
this._packed = null;
|
|
218
|
+
return ReflectionObject.prototype.setOption.call(this, name, value, ifNotSet);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Field descriptor.
|
|
223
|
+
* @interface IField
|
|
224
|
+
* @property {string} [rule="optional"] Field rule
|
|
225
|
+
* @property {string} type Field type
|
|
226
|
+
* @property {number} id Field id
|
|
227
|
+
* @property {Object.<string,*>} [options] Field options
|
|
228
|
+
*/
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Extension field descriptor.
|
|
232
|
+
* @interface IExtensionField
|
|
233
|
+
* @extends IField
|
|
234
|
+
* @property {string} extend Extended type
|
|
235
|
+
*/
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Converts this field to a field descriptor.
|
|
239
|
+
* @param {IToJSONOptions} [toJSONOptions] JSON conversion options
|
|
240
|
+
* @returns {IField} Field descriptor
|
|
241
|
+
*/
|
|
242
|
+
Field.prototype.toJSON = function toJSON(toJSONOptions) {
|
|
243
|
+
var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
|
|
244
|
+
return util.toObject([
|
|
245
|
+
"rule" , this.rule !== "optional" && this.rule || undefined,
|
|
246
|
+
"type" , this.type,
|
|
247
|
+
"id" , this.id,
|
|
248
|
+
"extend" , this.extend,
|
|
249
|
+
"options" , this.options,
|
|
250
|
+
"comment" , keepComments ? this.comment : undefined
|
|
251
|
+
]);
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Resolves this field's type references.
|
|
256
|
+
* @returns {Field} `this`
|
|
257
|
+
* @throws {Error} If any reference cannot be resolved
|
|
258
|
+
*/
|
|
259
|
+
Field.prototype.resolve = function resolve() {
|
|
260
|
+
|
|
261
|
+
if (this.resolved)
|
|
262
|
+
return this;
|
|
263
|
+
|
|
264
|
+
if ((this.typeDefault = types.defaults[this.type]) === undefined) { // if not a basic type, resolve it
|
|
265
|
+
this.resolvedType = (this.declaringField ? this.declaringField.parent : this.parent).lookupTypeOrEnum(this.type);
|
|
266
|
+
if (this.resolvedType instanceof Type)
|
|
267
|
+
this.typeDefault = null;
|
|
268
|
+
else // instanceof Enum
|
|
269
|
+
this.typeDefault = this.resolvedType.values[Object.keys(this.resolvedType.values)[0]]; // first defined
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// use explicitly set default value if present
|
|
273
|
+
if (this.options && this.options["default"] != null) {
|
|
274
|
+
this.typeDefault = this.options["default"];
|
|
275
|
+
if (this.resolvedType instanceof Enum && typeof this.typeDefault === "string")
|
|
276
|
+
this.typeDefault = this.resolvedType.values[this.typeDefault];
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// remove unnecessary options
|
|
280
|
+
if (this.options) {
|
|
281
|
+
if (this.options.packed === true || this.options.packed !== undefined && this.resolvedType && !(this.resolvedType instanceof Enum))
|
|
282
|
+
delete this.options.packed;
|
|
283
|
+
if (!Object.keys(this.options).length)
|
|
284
|
+
this.options = undefined;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// convert to internal data type if necesssary
|
|
288
|
+
if (this.long) {
|
|
289
|
+
this.typeDefault = util.Long.fromNumber(this.typeDefault, this.type.charAt(0) === "u");
|
|
290
|
+
|
|
291
|
+
/* istanbul ignore else */
|
|
292
|
+
if (Object.freeze)
|
|
293
|
+
Object.freeze(this.typeDefault); // long instances are meant to be immutable anyway (i.e. use small int cache that even requires it)
|
|
294
|
+
|
|
295
|
+
} else if (this.bytes && typeof this.typeDefault === "string") {
|
|
296
|
+
var buf;
|
|
297
|
+
if (util.base64.test(this.typeDefault))
|
|
298
|
+
util.base64.decode(this.typeDefault, buf = util.newBuffer(util.base64.length(this.typeDefault)), 0);
|
|
299
|
+
else
|
|
300
|
+
util.utf8.write(this.typeDefault, buf = util.newBuffer(util.utf8.length(this.typeDefault)), 0);
|
|
301
|
+
this.typeDefault = buf;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// take special care of maps and repeated fields
|
|
305
|
+
if (this.map)
|
|
306
|
+
this.defaultValue = util.emptyObject;
|
|
307
|
+
else if (this.repeated)
|
|
308
|
+
this.defaultValue = util.emptyArray;
|
|
309
|
+
else
|
|
310
|
+
this.defaultValue = this.typeDefault;
|
|
311
|
+
|
|
312
|
+
// ensure proper value on prototype
|
|
313
|
+
if (this.parent instanceof Type)
|
|
314
|
+
this.parent.ctor.prototype[this.name] = this.defaultValue;
|
|
315
|
+
|
|
316
|
+
return ReflectionObject.prototype.resolve.call(this);
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
Field.prototype.useToArray = function useToArray() {
|
|
320
|
+
return !!this.getOption("(js_use_toArray)");
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
Field.prototype.preEncoded = function preEncoded() {
|
|
324
|
+
return !!this.getOption("(js_preEncoded)");
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Decorator function as returned by {@link Field.d} and {@link MapField.d} (TypeScript).
|
|
329
|
+
* @typedef FieldDecorator
|
|
330
|
+
* @type {function}
|
|
331
|
+
* @param {Object} prototype Target prototype
|
|
332
|
+
* @param {string} fieldName Field name
|
|
333
|
+
* @returns {undefined}
|
|
334
|
+
*/
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Field decorator (TypeScript).
|
|
338
|
+
* @name Field.d
|
|
339
|
+
* @function
|
|
340
|
+
* @param {number} fieldId Field id
|
|
341
|
+
* @param {"double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"string"|"bool"|"bytes"|Object} fieldType Field type
|
|
342
|
+
* @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
|
|
343
|
+
* @param {T} [defaultValue] Default value
|
|
344
|
+
* @returns {FieldDecorator} Decorator function
|
|
345
|
+
* @template T extends number | number[] | string | string[] | boolean | boolean[] | Uint8Array | Uint8Array[] | Buffer | Buffer[]
|
|
346
|
+
*/
|
|
347
|
+
Field.d = function decorateField(fieldId, fieldType, fieldRule, defaultValue) {
|
|
348
|
+
|
|
349
|
+
// submessage: decorate the submessage and use its name as the type
|
|
350
|
+
if (typeof fieldType === "function")
|
|
351
|
+
fieldType = util.decorateType(fieldType).name;
|
|
352
|
+
|
|
353
|
+
// enum reference: create a reflected copy of the enum and keep reuseing it
|
|
354
|
+
else if (fieldType && typeof fieldType === "object")
|
|
355
|
+
fieldType = util.decorateEnum(fieldType).name;
|
|
356
|
+
|
|
357
|
+
return function fieldDecorator(prototype, fieldName) {
|
|
358
|
+
util.decorateType(prototype.constructor)
|
|
359
|
+
.add(new Field(fieldName, fieldId, fieldType, fieldRule, { "default": defaultValue }));
|
|
360
|
+
};
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Field decorator (TypeScript).
|
|
365
|
+
* @name Field.d
|
|
366
|
+
* @function
|
|
367
|
+
* @param {number} fieldId Field id
|
|
368
|
+
* @param {Constructor<T>|string} fieldType Field type
|
|
369
|
+
* @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
|
|
370
|
+
* @returns {FieldDecorator} Decorator function
|
|
371
|
+
* @template T extends Message<T>
|
|
372
|
+
* @variation 2
|
|
373
|
+
*/
|
|
374
|
+
// like Field.d but without a default value
|
|
375
|
+
|
|
376
|
+
// Sets up cyclic dependencies (called in index-light)
|
|
377
|
+
Field._configure = function configure(Type_) {
|
|
378
|
+
Type = Type_;
|
|
379
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var protobuf = module.exports = require("./index-minimal");
|
|
3
|
+
|
|
4
|
+
protobuf.build = "light";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A node-style callback as used by {@link load} and {@link Root#load}.
|
|
8
|
+
* @typedef LoadCallback
|
|
9
|
+
* @type {function}
|
|
10
|
+
* @param {Error|null} error Error, if any, otherwise `null`
|
|
11
|
+
* @param {Root} [root] Root, if there hasn't been an error
|
|
12
|
+
* @returns {undefined}
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Loads one or multiple .proto or preprocessed .json files into a common root namespace and calls the callback.
|
|
17
|
+
* @param {string|string[]} filename One or multiple files to load
|
|
18
|
+
* @param {Root} root Root namespace, defaults to create a new one if omitted.
|
|
19
|
+
* @param {LoadCallback} callback Callback function
|
|
20
|
+
* @returns {undefined}
|
|
21
|
+
* @see {@link Root#load}
|
|
22
|
+
*/
|
|
23
|
+
function load(filename, root, callback) {
|
|
24
|
+
if (typeof root === "function") {
|
|
25
|
+
callback = root;
|
|
26
|
+
root = new protobuf.Root();
|
|
27
|
+
} else if (!root)
|
|
28
|
+
root = new protobuf.Root();
|
|
29
|
+
return root.load(filename, callback);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Loads one or multiple .proto or preprocessed .json files into a common root namespace and calls the callback.
|
|
34
|
+
* @name load
|
|
35
|
+
* @function
|
|
36
|
+
* @param {string|string[]} filename One or multiple files to load
|
|
37
|
+
* @param {LoadCallback} callback Callback function
|
|
38
|
+
* @returns {undefined}
|
|
39
|
+
* @see {@link Root#load}
|
|
40
|
+
* @variation 2
|
|
41
|
+
*/
|
|
42
|
+
// function load(filename:string, callback:LoadCallback):undefined
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Loads one or multiple .proto or preprocessed .json files into a common root namespace and returns a promise.
|
|
46
|
+
* @name load
|
|
47
|
+
* @function
|
|
48
|
+
* @param {string|string[]} filename One or multiple files to load
|
|
49
|
+
* @param {Root} [root] Root namespace, defaults to create a new one if omitted.
|
|
50
|
+
* @returns {Promise<Root>} Promise
|
|
51
|
+
* @see {@link Root#load}
|
|
52
|
+
* @variation 3
|
|
53
|
+
*/
|
|
54
|
+
// function load(filename:string, [root:Root]):Promise<Root>
|
|
55
|
+
|
|
56
|
+
protobuf.load = load;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Synchronously loads one or multiple .proto or preprocessed .json files into a common root namespace (node only).
|
|
60
|
+
* @param {string|string[]} filename One or multiple files to load
|
|
61
|
+
* @param {Root} [root] Root namespace, defaults to create a new one if omitted.
|
|
62
|
+
* @returns {Root} Root namespace
|
|
63
|
+
* @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
|
|
64
|
+
* @see {@link Root#loadSync}
|
|
65
|
+
*/
|
|
66
|
+
function loadSync(filename, root) {
|
|
67
|
+
if (!root)
|
|
68
|
+
root = new protobuf.Root();
|
|
69
|
+
return root.loadSync(filename);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
protobuf.loadSync = loadSync;
|
|
73
|
+
|
|
74
|
+
// Serialization
|
|
75
|
+
protobuf.encoder = require("./encoder");
|
|
76
|
+
protobuf.decoder = require("./decoder");
|
|
77
|
+
protobuf.verifier = require("./verifier");
|
|
78
|
+
protobuf.converter = require("./converter");
|
|
79
|
+
|
|
80
|
+
// Reflection
|
|
81
|
+
protobuf.ReflectionObject = require("./object");
|
|
82
|
+
protobuf.Namespace = require("./namespace");
|
|
83
|
+
protobuf.Root = require("./root");
|
|
84
|
+
protobuf.Enum = require("./enum");
|
|
85
|
+
protobuf.Type = require("./type");
|
|
86
|
+
protobuf.Field = require("./field");
|
|
87
|
+
protobuf.OneOf = require("./oneof");
|
|
88
|
+
protobuf.MapField = require("./mapfield");
|
|
89
|
+
protobuf.Service = require("./service");
|
|
90
|
+
protobuf.Method = require("./method");
|
|
91
|
+
|
|
92
|
+
// Runtime
|
|
93
|
+
protobuf.Message = require("./message");
|
|
94
|
+
protobuf.wrappers = require("./wrappers");
|
|
95
|
+
|
|
96
|
+
// Utility
|
|
97
|
+
protobuf.types = require("./types");
|
|
98
|
+
protobuf.util = require("./util");
|
|
99
|
+
|
|
100
|
+
// Set up possibly cyclic reflection dependencies
|
|
101
|
+
protobuf.ReflectionObject._configure(protobuf.Root);
|
|
102
|
+
protobuf.Namespace._configure(protobuf.Type, protobuf.Service, protobuf.Enum);
|
|
103
|
+
protobuf.Root._configure(protobuf.Type);
|
|
104
|
+
protobuf.Field._configure(protobuf.Type);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var protobuf = exports;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Build type, one of `"full"`, `"light"` or `"minimal"`.
|
|
6
|
+
* @name build
|
|
7
|
+
* @type {string}
|
|
8
|
+
* @const
|
|
9
|
+
*/
|
|
10
|
+
protobuf.build = "minimal";
|
|
11
|
+
|
|
12
|
+
// Serialization
|
|
13
|
+
protobuf.Writer = require("./writer");
|
|
14
|
+
protobuf.BufferWriter = require("./writer_buffer");
|
|
15
|
+
protobuf.Reader = require("./reader");
|
|
16
|
+
protobuf.BufferReader = require("./reader_buffer");
|
|
17
|
+
|
|
18
|
+
// Utility
|
|
19
|
+
protobuf.util = require("./util/minimal");
|
|
20
|
+
protobuf.rpc = require("./rpc");
|
|
21
|
+
protobuf.roots = require("./roots");
|
|
22
|
+
protobuf.configure = configure;
|
|
23
|
+
|
|
24
|
+
/* istanbul ignore next */
|
|
25
|
+
/**
|
|
26
|
+
* Reconfigures the library according to the environment.
|
|
27
|
+
* @returns {undefined}
|
|
28
|
+
*/
|
|
29
|
+
function configure() {
|
|
30
|
+
protobuf.Reader._configure(protobuf.BufferReader);
|
|
31
|
+
protobuf.util._configure();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Set up buffer utility according to the environment
|
|
35
|
+
protobuf.Writer._configure(protobuf.BufferWriter);
|
|
36
|
+
configure();
|