@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/root.js
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = Root;
|
|
3
|
+
|
|
4
|
+
// extends Namespace
|
|
5
|
+
var Namespace = require("./namespace");
|
|
6
|
+
((Root.prototype = Object.create(Namespace.prototype)).constructor = Root).className = "Root";
|
|
7
|
+
|
|
8
|
+
var Field = require("./field"),
|
|
9
|
+
Enum = require("./enum"),
|
|
10
|
+
OneOf = require("./oneof"),
|
|
11
|
+
util = require("./util");
|
|
12
|
+
|
|
13
|
+
var Type, // cyclic
|
|
14
|
+
parse, // might be excluded
|
|
15
|
+
common; // "
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Constructs a new root namespace instance.
|
|
19
|
+
* @classdesc Root namespace wrapping all types, enums, services, sub-namespaces etc. that belong together.
|
|
20
|
+
* @extends NamespaceBase
|
|
21
|
+
* @constructor
|
|
22
|
+
* @param {Object.<string,*>} [options] Top level options
|
|
23
|
+
*/
|
|
24
|
+
function Root(options) {
|
|
25
|
+
Namespace.call(this, "", options);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Deferred extension fields.
|
|
29
|
+
* @type {Field[]}
|
|
30
|
+
*/
|
|
31
|
+
this.deferred = [];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Resolved file names of loaded files.
|
|
35
|
+
* @type {string[]}
|
|
36
|
+
*/
|
|
37
|
+
this.files = [];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Loads a namespace descriptor into a root namespace.
|
|
42
|
+
* @param {INamespace} json Nameespace descriptor
|
|
43
|
+
* @param {Root} [root] Root namespace, defaults to create a new one if omitted
|
|
44
|
+
* @returns {Root} Root namespace
|
|
45
|
+
*/
|
|
46
|
+
Root.fromJSON = function fromJSON(json, root) {
|
|
47
|
+
if (!root)
|
|
48
|
+
root = new Root();
|
|
49
|
+
if (json.options)
|
|
50
|
+
root.setOptions(json.options);
|
|
51
|
+
return root.addJSON(json.nested);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Resolves the path of an imported file, relative to the importing origin.
|
|
56
|
+
* This method exists so you can override it with your own logic in case your imports are scattered over multiple directories.
|
|
57
|
+
* @function
|
|
58
|
+
* @param {string} origin The file name of the importing file
|
|
59
|
+
* @param {string} target The file name being imported
|
|
60
|
+
* @returns {string|null} Resolved path to `target` or `null` to skip the file
|
|
61
|
+
*/
|
|
62
|
+
Root.prototype.resolvePath = util.path.resolve;
|
|
63
|
+
|
|
64
|
+
// A symbol-like function to safely signal synchronous loading
|
|
65
|
+
/* istanbul ignore next */
|
|
66
|
+
function SYNC() {} // eslint-disable-line no-empty-function
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
|
|
70
|
+
* @param {string|string[]} filename Names of one or multiple files to load
|
|
71
|
+
* @param {IParseOptions} options Parse options
|
|
72
|
+
* @param {LoadCallback} callback Callback function
|
|
73
|
+
* @returns {undefined}
|
|
74
|
+
*/
|
|
75
|
+
Root.prototype.load = function load(filename, options, callback) {
|
|
76
|
+
if (typeof options === "function") {
|
|
77
|
+
callback = options;
|
|
78
|
+
options = undefined;
|
|
79
|
+
}
|
|
80
|
+
var self = this;
|
|
81
|
+
if (!callback)
|
|
82
|
+
return util.asPromise(load, self, filename, options);
|
|
83
|
+
|
|
84
|
+
var sync = callback === SYNC; // undocumented
|
|
85
|
+
|
|
86
|
+
// Finishes loading by calling the callback (exactly once)
|
|
87
|
+
function finish(err, root) {
|
|
88
|
+
/* istanbul ignore if */
|
|
89
|
+
if (!callback)
|
|
90
|
+
return;
|
|
91
|
+
var cb = callback;
|
|
92
|
+
callback = null;
|
|
93
|
+
if (sync)
|
|
94
|
+
throw err;
|
|
95
|
+
cb(err, root);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Bundled definition existence checking
|
|
99
|
+
function getBundledFileName(filename) {
|
|
100
|
+
var idx = filename.lastIndexOf("google/protobuf/");
|
|
101
|
+
if (idx > -1) {
|
|
102
|
+
var altname = filename.substring(idx);
|
|
103
|
+
if (altname in common) return altname;
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Processes a single file
|
|
109
|
+
function process(filename, source) {
|
|
110
|
+
try {
|
|
111
|
+
if (util.isString(source) && source.charAt(0) === "{")
|
|
112
|
+
source = JSON.parse(source);
|
|
113
|
+
if (!util.isString(source))
|
|
114
|
+
self.setOptions(source.options).addJSON(source.nested);
|
|
115
|
+
else {
|
|
116
|
+
parse.filename = filename;
|
|
117
|
+
var parsed = parse(source, self, options),
|
|
118
|
+
resolved,
|
|
119
|
+
i = 0;
|
|
120
|
+
if (parsed.imports)
|
|
121
|
+
for (; i < parsed.imports.length; ++i)
|
|
122
|
+
if (resolved = (getBundledFileName(parsed.imports[i]) || self.resolvePath(filename, parsed.imports[i])))
|
|
123
|
+
fetch(resolved);
|
|
124
|
+
if (parsed.weakImports)
|
|
125
|
+
for (i = 0; i < parsed.weakImports.length; ++i)
|
|
126
|
+
if (resolved = (getBundledFileName(parsed.weakImports[i]) || self.resolvePath(filename, parsed.weakImports[i])))
|
|
127
|
+
fetch(resolved, true);
|
|
128
|
+
}
|
|
129
|
+
} catch (err) {
|
|
130
|
+
finish(err);
|
|
131
|
+
}
|
|
132
|
+
if (!sync && !queued)
|
|
133
|
+
finish(null, self); // only once anyway
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Fetches a single file
|
|
137
|
+
function fetch(filename, weak) {
|
|
138
|
+
|
|
139
|
+
// Skip if already loaded / attempted
|
|
140
|
+
if (self.files.indexOf(filename) > -1)
|
|
141
|
+
return;
|
|
142
|
+
self.files.push(filename);
|
|
143
|
+
|
|
144
|
+
// Shortcut bundled definitions
|
|
145
|
+
if (filename in common) {
|
|
146
|
+
if (sync)
|
|
147
|
+
process(filename, common[filename]);
|
|
148
|
+
else {
|
|
149
|
+
++queued;
|
|
150
|
+
setTimeout(function() {
|
|
151
|
+
--queued;
|
|
152
|
+
process(filename, common[filename]);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Otherwise fetch from disk or network
|
|
159
|
+
if (sync) {
|
|
160
|
+
var source;
|
|
161
|
+
try {
|
|
162
|
+
source = util.fs.readFileSync(filename).toString("utf8");
|
|
163
|
+
} catch (err) {
|
|
164
|
+
if (!weak)
|
|
165
|
+
finish(err);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
process(filename, source);
|
|
169
|
+
} else {
|
|
170
|
+
++queued;
|
|
171
|
+
util.fetch(filename, function(err, source) {
|
|
172
|
+
--queued;
|
|
173
|
+
/* istanbul ignore if */
|
|
174
|
+
if (!callback)
|
|
175
|
+
return; // terminated meanwhile
|
|
176
|
+
if (err) {
|
|
177
|
+
/* istanbul ignore else */
|
|
178
|
+
if (!weak)
|
|
179
|
+
finish(err);
|
|
180
|
+
else if (!queued) // can't be covered reliably
|
|
181
|
+
finish(null, self);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
process(filename, source);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
var queued = 0;
|
|
189
|
+
|
|
190
|
+
// Assembling the root namespace doesn't require working type
|
|
191
|
+
// references anymore, so we can load everything in parallel
|
|
192
|
+
if (util.isString(filename))
|
|
193
|
+
filename = [ filename ];
|
|
194
|
+
for (var i = 0, resolved; i < filename.length; ++i)
|
|
195
|
+
if (resolved = self.resolvePath("", filename[i]))
|
|
196
|
+
fetch(resolved);
|
|
197
|
+
|
|
198
|
+
if (sync)
|
|
199
|
+
return self;
|
|
200
|
+
if (!queued)
|
|
201
|
+
finish(null, self);
|
|
202
|
+
return undefined;
|
|
203
|
+
};
|
|
204
|
+
// function load(filename:string, options:IParseOptions, callback:LoadCallback):undefined
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
|
|
208
|
+
* @function Root#load
|
|
209
|
+
* @param {string|string[]} filename Names of one or multiple files to load
|
|
210
|
+
* @param {LoadCallback} callback Callback function
|
|
211
|
+
* @returns {undefined}
|
|
212
|
+
* @variation 2
|
|
213
|
+
*/
|
|
214
|
+
// function load(filename:string, callback:LoadCallback):undefined
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Loads one or multiple .proto or preprocessed .json files into this root namespace and returns a promise.
|
|
218
|
+
* @function Root#load
|
|
219
|
+
* @param {string|string[]} filename Names of one or multiple files to load
|
|
220
|
+
* @param {IParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
|
|
221
|
+
* @returns {Promise<Root>} Promise
|
|
222
|
+
* @variation 3
|
|
223
|
+
*/
|
|
224
|
+
// function load(filename:string, [options:IParseOptions]):Promise<Root>
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Synchronously loads one or multiple .proto or preprocessed .json files into this root namespace (node only).
|
|
228
|
+
* @function Root#loadSync
|
|
229
|
+
* @param {string|string[]} filename Names of one or multiple files to load
|
|
230
|
+
* @param {IParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
|
|
231
|
+
* @returns {Root} Root namespace
|
|
232
|
+
* @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
|
|
233
|
+
*/
|
|
234
|
+
Root.prototype.loadSync = function loadSync(filename, options) {
|
|
235
|
+
if (!util.isNode)
|
|
236
|
+
throw Error("not supported");
|
|
237
|
+
return this.load(filename, options, SYNC);
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* @override
|
|
242
|
+
*/
|
|
243
|
+
Root.prototype.resolveAll = function resolveAll() {
|
|
244
|
+
if (this.deferred.length)
|
|
245
|
+
throw Error("unresolvable extensions: " + this.deferred.map(function(field) {
|
|
246
|
+
return "'extend " + field.extend + "' in " + field.parent.fullName;
|
|
247
|
+
}).join(", "));
|
|
248
|
+
return Namespace.prototype.resolveAll.call(this);
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
// only uppercased (and thus conflict-free) children are exposed, see below
|
|
252
|
+
var exposeRe = /^[A-Z]/;
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Handles a deferred declaring extension field by creating a sister field to represent it within its extended type.
|
|
256
|
+
* @param {Root} root Root instance
|
|
257
|
+
* @param {Field} field Declaring extension field witin the declaring type
|
|
258
|
+
* @returns {boolean} `true` if successfully added to the extended type, `false` otherwise
|
|
259
|
+
* @inner
|
|
260
|
+
* @ignore
|
|
261
|
+
*/
|
|
262
|
+
function tryHandleExtension(root, field) {
|
|
263
|
+
var extendedType = field.parent.lookup(field.extend);
|
|
264
|
+
if (extendedType) {
|
|
265
|
+
var sisterField = new Field(field.fullName, field.id, field.type, field.rule, undefined, field.options);
|
|
266
|
+
sisterField.declaringField = field;
|
|
267
|
+
field.extensionField = sisterField;
|
|
268
|
+
extendedType.add(sisterField);
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Called when any object is added to this root or its sub-namespaces.
|
|
276
|
+
* @param {ReflectionObject} object Object added
|
|
277
|
+
* @returns {undefined}
|
|
278
|
+
* @private
|
|
279
|
+
*/
|
|
280
|
+
Root.prototype._handleAdd = function _handleAdd(object) {
|
|
281
|
+
if (object instanceof Field) {
|
|
282
|
+
|
|
283
|
+
if (/* an extension field (implies not part of a oneof) */ object.extend !== undefined && /* not already handled */ !object.extensionField)
|
|
284
|
+
if (!tryHandleExtension(this, object))
|
|
285
|
+
this.deferred.push(object);
|
|
286
|
+
|
|
287
|
+
} else if (object instanceof Enum) {
|
|
288
|
+
|
|
289
|
+
if (exposeRe.test(object.name))
|
|
290
|
+
object.parent[object.name] = object.values; // expose enum values as property of its parent
|
|
291
|
+
|
|
292
|
+
} else if (!(object instanceof OneOf)) /* everything else is a namespace */ {
|
|
293
|
+
|
|
294
|
+
if (object instanceof Type) // Try to handle any deferred extensions
|
|
295
|
+
for (var i = 0; i < this.deferred.length;)
|
|
296
|
+
if (tryHandleExtension(this, this.deferred[i]))
|
|
297
|
+
this.deferred.splice(i, 1);
|
|
298
|
+
else
|
|
299
|
+
++i;
|
|
300
|
+
for (var j = 0; j < /* initializes */ object.nestedArray.length; ++j) // recurse into the namespace
|
|
301
|
+
this._handleAdd(object._nestedArray[j]);
|
|
302
|
+
if (exposeRe.test(object.name))
|
|
303
|
+
object.parent[object.name] = object; // expose namespace as property of its parent
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// The above also adds uppercased (and thus conflict-free) nested types, services and enums as
|
|
307
|
+
// properties of namespaces just like static code does. This allows using a .d.ts generated for
|
|
308
|
+
// a static module with reflection-based solutions where the condition is met.
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Called when any object is removed from this root or its sub-namespaces.
|
|
313
|
+
* @param {ReflectionObject} object Object removed
|
|
314
|
+
* @returns {undefined}
|
|
315
|
+
* @private
|
|
316
|
+
*/
|
|
317
|
+
Root.prototype._handleRemove = function _handleRemove(object) {
|
|
318
|
+
if (object instanceof Field) {
|
|
319
|
+
|
|
320
|
+
if (/* an extension field */ object.extend !== undefined) {
|
|
321
|
+
if (/* already handled */ object.extensionField) { // remove its sister field
|
|
322
|
+
object.extensionField.parent.remove(object.extensionField);
|
|
323
|
+
object.extensionField = null;
|
|
324
|
+
} else { // cancel the extension
|
|
325
|
+
var index = this.deferred.indexOf(object);
|
|
326
|
+
/* istanbul ignore else */
|
|
327
|
+
if (index > -1)
|
|
328
|
+
this.deferred.splice(index, 1);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
} else if (object instanceof Enum) {
|
|
333
|
+
|
|
334
|
+
if (exposeRe.test(object.name))
|
|
335
|
+
delete object.parent[object.name]; // unexpose enum values
|
|
336
|
+
|
|
337
|
+
} else if (object instanceof Namespace) {
|
|
338
|
+
|
|
339
|
+
for (var i = 0; i < /* initializes */ object.nestedArray.length; ++i) // recurse into the namespace
|
|
340
|
+
this._handleRemove(object._nestedArray[i]);
|
|
341
|
+
|
|
342
|
+
if (exposeRe.test(object.name))
|
|
343
|
+
delete object.parent[object.name]; // unexpose namespaces
|
|
344
|
+
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
// Sets up cyclic dependencies (called in index-light)
|
|
349
|
+
Root._configure = function(Type_, parse_, common_) {
|
|
350
|
+
Type = Type_;
|
|
351
|
+
parse = parse_;
|
|
352
|
+
common = common_;
|
|
353
|
+
};
|
package/src/roots.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = {};
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Named roots.
|
|
6
|
+
* This is where pbjs stores generated structures (the option `-r, --root` specifies a name).
|
|
7
|
+
* Can also be used manually to make roots available accross modules.
|
|
8
|
+
* @name roots
|
|
9
|
+
* @type {Object.<string,Root>}
|
|
10
|
+
* @example
|
|
11
|
+
* // pbjs -r myroot -o compiled.js ...
|
|
12
|
+
*
|
|
13
|
+
* // in another module:
|
|
14
|
+
* require("./compiled.js");
|
|
15
|
+
*
|
|
16
|
+
* // in any subsequent module:
|
|
17
|
+
* var root = protobuf.roots["myroot"];
|
|
18
|
+
*/
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = Service;
|
|
3
|
+
|
|
4
|
+
var util = require("../util/minimal");
|
|
5
|
+
|
|
6
|
+
// Extends EventEmitter
|
|
7
|
+
(Service.prototype = Object.create(util.EventEmitter.prototype)).constructor = Service;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A service method callback as used by {@link rpc.ServiceMethod|ServiceMethod}.
|
|
11
|
+
*
|
|
12
|
+
* Differs from {@link RPCImplCallback} in that it is an actual callback of a service method which may not return `response = null`.
|
|
13
|
+
* @typedef rpc.ServiceMethodCallback
|
|
14
|
+
* @template TRes extends Message<TRes>
|
|
15
|
+
* @type {function}
|
|
16
|
+
* @param {Error|null} error Error, if any
|
|
17
|
+
* @param {TRes} [response] Response message
|
|
18
|
+
* @returns {undefined}
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A service method part of a {@link rpc.Service} as created by {@link Service.create}.
|
|
23
|
+
* @typedef rpc.ServiceMethod
|
|
24
|
+
* @template TReq extends Message<TReq>
|
|
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`
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Constructs a new RPC service instance.
|
|
34
|
+
* @classdesc An RPC service as returned by {@link Service#create}.
|
|
35
|
+
* @exports rpc.Service
|
|
36
|
+
* @extends util.EventEmitter
|
|
37
|
+
* @constructor
|
|
38
|
+
* @param {RPCImpl} rpcImpl RPC implementation
|
|
39
|
+
* @param {boolean} [requestDelimited=false] Whether requests are length-delimited
|
|
40
|
+
* @param {boolean} [responseDelimited=false] Whether responses are length-delimited
|
|
41
|
+
*/
|
|
42
|
+
function Service(rpcImpl, requestDelimited, responseDelimited) {
|
|
43
|
+
|
|
44
|
+
if (typeof rpcImpl !== "function")
|
|
45
|
+
throw TypeError("rpcImpl must be a function");
|
|
46
|
+
|
|
47
|
+
util.EventEmitter.call(this);
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* RPC implementation. Becomes `null` once the service is ended.
|
|
51
|
+
* @type {RPCImpl|null}
|
|
52
|
+
*/
|
|
53
|
+
this.rpcImpl = rpcImpl;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Whether requests are length-delimited.
|
|
57
|
+
* @type {boolean}
|
|
58
|
+
*/
|
|
59
|
+
this.requestDelimited = Boolean(requestDelimited);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Whether responses are length-delimited.
|
|
63
|
+
* @type {boolean}
|
|
64
|
+
*/
|
|
65
|
+
this.responseDelimited = Boolean(responseDelimited);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Calls a service method through {@link rpc.Service#rpcImpl|rpcImpl}.
|
|
70
|
+
* @param {Method|rpc.ServiceMethod<TReq,TRes>} method Reflected or static method
|
|
71
|
+
* @param {Constructor<TReq>} requestCtor Request constructor
|
|
72
|
+
* @param {Constructor<TRes>} responseCtor Response constructor
|
|
73
|
+
* @param {TReq|Properties<TReq>} request Request message or plain object
|
|
74
|
+
* @param {rpc.ServiceMethodCallback<TRes>} callback Service callback
|
|
75
|
+
* @returns {undefined}
|
|
76
|
+
* @template TReq extends Message<TReq>
|
|
77
|
+
* @template TRes extends Message<TRes>
|
|
78
|
+
*/
|
|
79
|
+
Service.prototype.rpcCall = function rpcCall(method, requestCtor, responseCtor, request, callback) {
|
|
80
|
+
|
|
81
|
+
if (!request)
|
|
82
|
+
throw TypeError("request must be specified");
|
|
83
|
+
|
|
84
|
+
var self = this;
|
|
85
|
+
if (!callback)
|
|
86
|
+
return util.asPromise(rpcCall, self, method, requestCtor, responseCtor, request);
|
|
87
|
+
|
|
88
|
+
if (!self.rpcImpl) {
|
|
89
|
+
setTimeout(function() { callback(Error("already ended")); }, 0);
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
return self.rpcImpl(
|
|
95
|
+
method,
|
|
96
|
+
requestCtor[self.requestDelimited ? "encodeDelimited" : "encode"](request).finish(),
|
|
97
|
+
function rpcCallback(err, response) {
|
|
98
|
+
|
|
99
|
+
if (err) {
|
|
100
|
+
self.emit("error", err, method);
|
|
101
|
+
return callback(err);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (response === null) {
|
|
105
|
+
self.end(/* endedByRPC */ true);
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (!(response instanceof responseCtor)) {
|
|
110
|
+
try {
|
|
111
|
+
response = responseCtor[self.responseDelimited ? "decodeDelimited" : "decode"](response);
|
|
112
|
+
} catch (err) {
|
|
113
|
+
self.emit("error", err, method);
|
|
114
|
+
return callback(err);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
self.emit("data", response, method);
|
|
119
|
+
return callback(null, response);
|
|
120
|
+
}
|
|
121
|
+
);
|
|
122
|
+
} catch (err) {
|
|
123
|
+
self.emit("error", err, method);
|
|
124
|
+
setTimeout(function() { callback(err); }, 0);
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Ends this service and emits the `end` event.
|
|
131
|
+
* @param {boolean} [endedByRPC=false] Whether the service has been ended by the RPC implementation.
|
|
132
|
+
* @returns {rpc.Service} `this`
|
|
133
|
+
*/
|
|
134
|
+
Service.prototype.end = function end(endedByRPC) {
|
|
135
|
+
if (this.rpcImpl) {
|
|
136
|
+
if (!endedByRPC) // signal end to rpcImpl
|
|
137
|
+
this.rpcImpl(null, null, null);
|
|
138
|
+
this.rpcImpl = null;
|
|
139
|
+
this.emit("end").off();
|
|
140
|
+
}
|
|
141
|
+
return this;
|
|
142
|
+
};
|
package/src/rpc.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Streaming RPC helpers.
|
|
5
|
+
* @namespace
|
|
6
|
+
*/
|
|
7
|
+
var rpc = exports;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* RPC implementation passed to {@link Service#create} performing a service request on network level, i.e. by utilizing http requests or websockets.
|
|
11
|
+
* @typedef RPCImpl
|
|
12
|
+
* @type {function}
|
|
13
|
+
* @param {Method|rpc.ServiceMethod<Message<{}>,Message<{}>>} method Reflected or static method being called
|
|
14
|
+
* @param {Uint8Array} requestData Request data
|
|
15
|
+
* @param {RPCImplCallback} callback Callback function
|
|
16
|
+
* @returns {undefined}
|
|
17
|
+
* @example
|
|
18
|
+
* function rpcImpl(method, requestData, callback) {
|
|
19
|
+
* if (protobuf.util.lcFirst(method.name) !== "myMethod") // compatible with static code
|
|
20
|
+
* throw Error("no such method");
|
|
21
|
+
* asynchronouslyObtainAResponse(requestData, function(err, responseData) {
|
|
22
|
+
* callback(err, responseData);
|
|
23
|
+
* });
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Node-style callback as used by {@link RPCImpl}.
|
|
29
|
+
* @typedef RPCImplCallback
|
|
30
|
+
* @type {function}
|
|
31
|
+
* @param {Error|null} error Error, if any, otherwise `null`
|
|
32
|
+
* @param {Uint8Array|null} [response] Response data or `null` to signal end of stream, if there hasn't been an error
|
|
33
|
+
* @returns {undefined}
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
rpc.Service = require("./rpc/service");
|