protobufjs 8.1.6-experimental → 8.2.1

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.
Files changed (76) hide show
  1. package/README.md +225 -570
  2. package/dist/light/protobuf.js +2041 -1482
  3. package/dist/light/protobuf.js.map +1 -1
  4. package/dist/light/protobuf.min.js +3 -3
  5. package/dist/light/protobuf.min.js.map +1 -1
  6. package/dist/minimal/protobuf.js +1167 -863
  7. package/dist/minimal/protobuf.js.map +1 -1
  8. package/dist/minimal/protobuf.min.js +3 -3
  9. package/dist/minimal/protobuf.min.js.map +1 -1
  10. package/dist/protobuf.js +2173 -1527
  11. package/dist/protobuf.js.map +1 -1
  12. package/dist/protobuf.min.js +3 -3
  13. package/dist/protobuf.min.js.map +1 -1
  14. package/ext/README.md +81 -0
  15. package/ext/descriptor/README.md +3 -70
  16. package/ext/descriptor/index.d.ts +1 -190
  17. package/ext/descriptor/index.js +1 -1161
  18. package/ext/descriptor.d.ts +309 -0
  19. package/ext/descriptor.js +1241 -0
  20. package/ext/textformat.d.ts +24 -0
  21. package/ext/textformat.js +1227 -0
  22. package/google/protobuf/compiler/plugin.json +126 -0
  23. package/google/protobuf/compiler/plugin.proto +47 -0
  24. package/google/protobuf/descriptor.json +2 -2
  25. package/google/protobuf/descriptor.proto +2 -1
  26. package/index.d.ts +585 -476
  27. package/package.json +22 -40
  28. package/src/converter.js +63 -27
  29. package/src/decoder.js +126 -49
  30. package/src/encoder.js +10 -2
  31. package/src/enum.js +4 -1
  32. package/src/field.js +10 -7
  33. package/src/mapfield.js +1 -0
  34. package/src/message.js +7 -6
  35. package/src/method.js +4 -3
  36. package/src/namespace.js +31 -12
  37. package/src/object.js +24 -19
  38. package/src/oneof.js +2 -0
  39. package/src/parse.js +128 -46
  40. package/src/reader.js +145 -30
  41. package/src/reader_buffer.js +24 -3
  42. package/src/root.js +10 -4
  43. package/src/service.js +15 -6
  44. package/src/tokenize.js +6 -1
  45. package/src/type.js +57 -27
  46. package/src/types.js +1 -1
  47. package/src/util/aspromise.d.ts +13 -0
  48. package/src/util/aspromise.js +52 -0
  49. package/src/util/base64.d.ts +32 -0
  50. package/src/util/base64.js +146 -0
  51. package/src/util/codegen.d.ts +31 -0
  52. package/src/util/codegen.js +113 -0
  53. package/src/util/eventemitter.d.ts +45 -0
  54. package/src/util/eventemitter.js +84 -0
  55. package/src/util/fetch.d.ts +56 -0
  56. package/src/util/fetch.js +112 -0
  57. package/src/util/float.d.ts +83 -0
  58. package/src/util/float.js +335 -0
  59. package/src/util/fs.js +11 -0
  60. package/src/util/inquire.d.ts +10 -0
  61. package/src/util/inquire.js +38 -0
  62. package/src/util/minimal.js +74 -12
  63. package/src/util/path.d.ts +22 -0
  64. package/src/util/path.js +72 -0
  65. package/src/util/patterns.js +8 -0
  66. package/src/util/pool.d.ts +32 -0
  67. package/src/util/pool.js +48 -0
  68. package/src/util/utf8.d.ts +24 -0
  69. package/src/util/utf8.js +130 -0
  70. package/src/util.js +18 -13
  71. package/src/verifier.js +7 -4
  72. package/src/wrappers.js +4 -3
  73. package/src/writer.js +33 -5
  74. package/src/writer_buffer.js +18 -1
  75. package/tsconfig.json +2 -2
  76. package/ext/descriptor/test.js +0 -54
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ module.exports = codegen;
3
+
4
+ var patterns = require("./patterns");
5
+ var reservedRe = patterns.reservedRe;
6
+
7
+ /**
8
+ * Begins generating a function.
9
+ * @memberof util
10
+ * @param {string[]} functionParams Function parameter names
11
+ * @param {string} [functionName] Function name if not anonymous
12
+ * @returns {Codegen} Appender that appends code to the function's body
13
+ */
14
+ function codegen(functionParams, functionName) {
15
+
16
+ /* istanbul ignore if */
17
+ if (typeof functionParams === "string") {
18
+ functionName = functionParams;
19
+ functionParams = undefined;
20
+ }
21
+
22
+ var body = [];
23
+
24
+ /**
25
+ * Appends code to the function's body or finishes generation.
26
+ * @typedef Codegen
27
+ * @type {function}
28
+ * @param {string|Object.<string,*>} [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any
29
+ * @param {...*} [formatParams] Format parameters
30
+ * @returns {Codegen|Function} Itself or the generated function if finished
31
+ * @throws {Error} If format parameter counts do not match
32
+ */
33
+
34
+ function Codegen(formatStringOrScope) {
35
+ // note that explicit array handling below makes this ~50% faster
36
+
37
+ // finish the function
38
+ if (typeof formatStringOrScope !== "string") {
39
+ var source = toString();
40
+ if (codegen.verbose)
41
+ console.log("codegen: " + source); // eslint-disable-line no-console
42
+ source = "return " + source;
43
+ if (formatStringOrScope) {
44
+ var scopeKeys = Object.keys(formatStringOrScope),
45
+ scopeParams = new Array(scopeKeys.length + 1),
46
+ scopeValues = new Array(scopeKeys.length),
47
+ scopeOffset = 0;
48
+ while (scopeOffset < scopeKeys.length) {
49
+ scopeParams[scopeOffset] = scopeKeys[scopeOffset];
50
+ scopeValues[scopeOffset] = formatStringOrScope[scopeKeys[scopeOffset++]];
51
+ }
52
+ scopeParams[scopeOffset] = source;
53
+ return Function.apply(null, scopeParams).apply(null, scopeValues); // eslint-disable-line no-new-func
54
+ }
55
+ return Function(source)(); // eslint-disable-line no-new-func
56
+ }
57
+
58
+ // otherwise append to body
59
+ var formatParams = new Array(arguments.length - 1),
60
+ formatOffset = 0;
61
+ while (formatOffset < formatParams.length)
62
+ formatParams[formatOffset] = arguments[++formatOffset];
63
+ formatOffset = 0;
64
+ formatStringOrScope = formatStringOrScope.replace(/%([%dfijs])/g, function replace($0, $1) {
65
+ var value = formatParams[formatOffset++];
66
+ switch ($1) {
67
+ case "d": case "f": return String(Number(value));
68
+ case "i": return String(Math.floor(value));
69
+ case "j": return JSON.stringify(value);
70
+ case "s": return String(value);
71
+ }
72
+ return "%";
73
+ });
74
+ if (formatOffset !== formatParams.length)
75
+ throw Error("parameter count mismatch");
76
+ body.push(formatStringOrScope);
77
+ return Codegen;
78
+ }
79
+
80
+ function toString(functionNameOverride) {
81
+ return "function " + safeFunctionName(functionNameOverride || functionName) + "(" + (functionParams && functionParams.join(",") || "") + "){\n " + body.join("\n ") + "\n}";
82
+ }
83
+
84
+ Codegen.toString = toString;
85
+ return Codegen;
86
+ }
87
+
88
+ /**
89
+ * Begins generating a function.
90
+ * @memberof util
91
+ * @function codegen
92
+ * @param {string} [functionName] Function name if not anonymous
93
+ * @returns {Codegen} Appender that appends code to the function's body
94
+ * @variation 2
95
+ */
96
+
97
+ /**
98
+ * When set to `true`, codegen will log generated code to console. Useful for debugging.
99
+ * @name util.codegen.verbose
100
+ * @type {boolean}
101
+ */
102
+ codegen.verbose = false;
103
+
104
+ function safeFunctionName(name) {
105
+ if (!name)
106
+ return "";
107
+ name = String(name).replace(/[^\w$]/g, "");
108
+ if (!name)
109
+ return "";
110
+ if (/^\d/.test(name))
111
+ name = "_" + name;
112
+ return reservedRe.test(name) ? name + "_" : name;
113
+ }
@@ -0,0 +1,45 @@
1
+ export = EventEmitter;
2
+
3
+ type EventEmitterListener = (...args: any[]) => {};
4
+
5
+ /**
6
+ * Constructs a new event emitter instance.
7
+ * @classdesc A minimal event emitter.
8
+ * @memberof util
9
+ * @constructor
10
+ */
11
+ declare class EventEmitter {
12
+
13
+ /**
14
+ * Constructs a new event emitter instance.
15
+ * @classdesc A minimal event emitter.
16
+ * @memberof util
17
+ * @constructor
18
+ */
19
+ constructor();
20
+
21
+ /**
22
+ * Registers an event listener.
23
+ * @param {string} evt Event name
24
+ * @param {EventEmitterListener} fn Listener
25
+ * @param {*} [ctx] Listener context
26
+ * @returns {this} `this`
27
+ */
28
+ public on(evt: string, fn: EventEmitterListener, ctx?: any): EventEmitter;
29
+
30
+ /**
31
+ * Removes an event listener or any matching listeners if arguments are omitted.
32
+ * @param {string} [evt] Event name. Removes all listeners if omitted.
33
+ * @param {EventEmitterListener} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
34
+ * @returns {this} `this`
35
+ */
36
+ public off(evt?: string, fn?: EventEmitterListener): EventEmitter;
37
+
38
+ /**
39
+ * Emits an event by calling its listeners with the specified arguments.
40
+ * @param {string} evt Event name
41
+ * @param {...*} args Arguments
42
+ * @returns {this} `this`
43
+ */
44
+ public emit(evt: string, ...args: any[]): EventEmitter;
45
+ }
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ module.exports = EventEmitter;
3
+
4
+ /**
5
+ * Constructs a new event emitter instance.
6
+ * @classdesc A minimal event emitter.
7
+ * @memberof util
8
+ * @constructor
9
+ */
10
+ function EventEmitter() {
11
+
12
+ /**
13
+ * Registered listeners.
14
+ * @type {Object.<string,*>}
15
+ * @private
16
+ */
17
+ this._listeners = {};
18
+ }
19
+
20
+ /**
21
+ * Event listener as used by {@link util.EventEmitter}.
22
+ * @typedef EventEmitterListener
23
+ * @type {function}
24
+ * @param {...*} args Arguments
25
+ * @returns {undefined}
26
+ */
27
+
28
+ /**
29
+ * Registers an event listener.
30
+ * @param {string} evt Event name
31
+ * @param {EventEmitterListener} fn Listener
32
+ * @param {*} [ctx] Listener context
33
+ * @returns {this} `this`
34
+ */
35
+ EventEmitter.prototype.on = function on(evt, fn, ctx) {
36
+ (this._listeners[evt] || (this._listeners[evt] = [])).push({
37
+ fn : fn,
38
+ ctx : ctx || this
39
+ });
40
+ return this;
41
+ };
42
+
43
+ /**
44
+ * Removes an event listener or any matching listeners if arguments are omitted.
45
+ * @param {string} [evt] Event name. Removes all listeners if omitted.
46
+ * @param {EventEmitterListener} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
47
+ * @returns {this} `this`
48
+ */
49
+ EventEmitter.prototype.off = function off(evt, fn) {
50
+ if (evt === undefined)
51
+ this._listeners = {};
52
+ else {
53
+ if (fn === undefined)
54
+ this._listeners[evt] = [];
55
+ else {
56
+ var listeners = this._listeners[evt];
57
+ for (var i = 0; i < listeners.length;)
58
+ if (listeners[i].fn === fn)
59
+ listeners.splice(i, 1);
60
+ else
61
+ ++i;
62
+ }
63
+ }
64
+ return this;
65
+ };
66
+
67
+ /**
68
+ * Emits an event by calling its listeners with the specified arguments.
69
+ * @param {string} evt Event name
70
+ * @param {...*} args Arguments
71
+ * @returns {this} `this`
72
+ */
73
+ EventEmitter.prototype.emit = function emit(evt) {
74
+ var listeners = this._listeners[evt];
75
+ if (listeners) {
76
+ var args = [],
77
+ i = 1;
78
+ for (; i < arguments.length;)
79
+ args.push(arguments[i++]);
80
+ for (i = 0; i < listeners.length;)
81
+ listeners[i].fn.apply(listeners[i++].ctx, args);
82
+ }
83
+ return this;
84
+ };
@@ -0,0 +1,56 @@
1
+ export = fetch;
2
+
3
+ /**
4
+ * Node-style callback as used by {@link util.fetch}.
5
+ * @typedef FetchCallback
6
+ * @type {function}
7
+ * @param {?Error} error Error, if any, otherwise `null`
8
+ * @param {string} [contents] File contents, if there hasn't been an error
9
+ * @returns {undefined}
10
+ */
11
+ type FetchCallback = (error: Error, contents?: string) => void;
12
+
13
+ /**
14
+ * Options as used by {@link util.fetch}.
15
+ * @typedef IFetchOptions
16
+ * @type {Object}
17
+ * @property {boolean} [binary=false] Whether expecting a binary response
18
+ * @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest
19
+ */
20
+
21
+ interface IFetchOptions {
22
+ binary?: boolean;
23
+ xhr?: boolean;
24
+ }
25
+
26
+ /**
27
+ * Fetches the contents of a file.
28
+ * @memberof util
29
+ * @param {string} filename File path or url
30
+ * @param {IFetchOptions} options Fetch options
31
+ * @param {FetchCallback} callback Callback function
32
+ * @returns {undefined}
33
+ */
34
+ declare function fetch(filename: string, options: IFetchOptions, callback: FetchCallback): void;
35
+
36
+ /**
37
+ * Fetches the contents of a file.
38
+ * @name util.fetch
39
+ * @function
40
+ * @param {string} path File path or url
41
+ * @param {FetchCallback} callback Callback function
42
+ * @returns {undefined}
43
+ * @variation 2
44
+ */
45
+ declare function fetch(path: string, callback: FetchCallback): void;
46
+
47
+ /**
48
+ * Fetches the contents of a file.
49
+ * @name util.fetch
50
+ * @function
51
+ * @param {string} path File path or url
52
+ * @param {IFetchOptions} [options] Fetch options
53
+ * @returns {Promise<string|Uint8Array>} Promise
54
+ * @variation 3
55
+ */
56
+ declare function fetch(path: string, options?: IFetchOptions): Promise<(string|Uint8Array)>;
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ module.exports = fetch;
3
+
4
+ var asPromise = require("./aspromise"),
5
+ fs = require("./fs");
6
+
7
+ /**
8
+ * Node-style callback as used by {@link util.fetch}.
9
+ * @typedef FetchCallback
10
+ * @type {function}
11
+ * @param {?Error} error Error, if any, otherwise `null`
12
+ * @param {string} [contents] File contents, if there hasn't been an error
13
+ * @returns {undefined}
14
+ */
15
+
16
+ /**
17
+ * Options as used by {@link util.fetch}.
18
+ * @interface IFetchOptions
19
+ * @property {boolean} [binary=false] Whether expecting a binary response
20
+ * @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest
21
+ */
22
+
23
+ /**
24
+ * Fetches the contents of a file.
25
+ * @memberof util
26
+ * @param {string} filename File path or url
27
+ * @param {IFetchOptions} options Fetch options
28
+ * @param {FetchCallback} callback Callback function
29
+ * @returns {undefined}
30
+ */
31
+ function fetch(filename, options, callback) {
32
+ if (typeof options === "function") {
33
+ callback = options;
34
+ options = {};
35
+ } else if (!options)
36
+ options = {};
37
+
38
+ if (!callback)
39
+ return asPromise(fetch, this, filename, options); // eslint-disable-line no-invalid-this
40
+
41
+ // if a node-like filesystem is present, try it first but fall back to XHR if nothing is found.
42
+ if (!options.xhr && fs && fs.readFile)
43
+ return fs.readFile(filename, function fetchReadFileCallback(err, contents) {
44
+ return err && typeof XMLHttpRequest !== "undefined"
45
+ ? fetch.xhr(filename, options, callback)
46
+ : err
47
+ ? callback(err)
48
+ : callback(null, options.binary ? contents : contents.toString("utf8"));
49
+ });
50
+
51
+ // use the XHR version otherwise.
52
+ return fetch.xhr(filename, options, callback);
53
+ }
54
+
55
+ /**
56
+ * Fetches the contents of a file.
57
+ * @name util.fetch
58
+ * @function
59
+ * @param {string} path File path or url
60
+ * @param {FetchCallback} callback Callback function
61
+ * @returns {undefined}
62
+ * @variation 2
63
+ */
64
+
65
+ /**
66
+ * Fetches the contents of a file.
67
+ * @name util.fetch
68
+ * @function
69
+ * @param {string} path File path or url
70
+ * @param {IFetchOptions} [options] Fetch options
71
+ * @returns {Promise<string|Uint8Array>} Promise
72
+ * @variation 3
73
+ */
74
+
75
+ /**/
76
+ fetch.xhr = function fetch_xhr(filename, options, callback) {
77
+ var xhr = new XMLHttpRequest();
78
+ xhr.onreadystatechange /* works everywhere */ = function fetchOnReadyStateChange() {
79
+
80
+ if (xhr.readyState !== 4)
81
+ return undefined;
82
+
83
+ // local cors security errors return status 0 / empty string, too. afaik this cannot be
84
+ // reliably distinguished from an actually empty file for security reasons. feel free
85
+ // to send a pull request if you are aware of a solution.
86
+ if (xhr.status !== 0 && xhr.status !== 200)
87
+ return callback(Error("status " + xhr.status));
88
+
89
+ // if binary data is expected, make sure that some sort of array is returned, even if
90
+ // ArrayBuffers are not supported. the binary string fallback, however, is unsafe.
91
+ if (options.binary) {
92
+ var buffer = xhr.response;
93
+ if (!buffer) {
94
+ buffer = [];
95
+ for (var i = 0; i < xhr.responseText.length; ++i)
96
+ buffer.push(xhr.responseText.charCodeAt(i) & 255);
97
+ }
98
+ return callback(null, typeof Uint8Array !== "undefined" ? new Uint8Array(buffer) : buffer);
99
+ }
100
+ return callback(null, xhr.responseText);
101
+ };
102
+
103
+ if (options.binary) {
104
+ // ref: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data#Receiving_binary_data_in_older_browsers
105
+ if ("overrideMimeType" in xhr)
106
+ xhr.overrideMimeType("text/plain; charset=x-user-defined");
107
+ xhr.responseType = "arraybuffer";
108
+ }
109
+
110
+ xhr.open("GET", filename);
111
+ xhr.send();
112
+ };
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Writes a 32 bit float to a buffer using little endian byte order.
3
+ * @name writeFloatLE
4
+ * @function
5
+ * @param {number} val Value to write
6
+ * @param {Uint8Array} buf Target buffer
7
+ * @param {number} pos Target buffer offset
8
+ * @returns {undefined}
9
+ */
10
+ export function writeFloatLE(val: number, buf: Uint8Array, pos: number): void;
11
+
12
+ /**
13
+ * Writes a 32 bit float to a buffer using big endian byte order.
14
+ * @name writeFloatBE
15
+ * @function
16
+ * @param {number} val Value to write
17
+ * @param {Uint8Array} buf Target buffer
18
+ * @param {number} pos Target buffer offset
19
+ * @returns {undefined}
20
+ */
21
+ export function writeFloatBE(val: number, buf: Uint8Array, pos: number): void;
22
+
23
+ /**
24
+ * Reads a 32 bit float from a buffer using little endian byte order.
25
+ * @name readFloatLE
26
+ * @function
27
+ * @param {Uint8Array} buf Source buffer
28
+ * @param {number} pos Source buffer offset
29
+ * @returns {number} Value read
30
+ */
31
+ export function readFloatLE(buf: Uint8Array, pos: number): number;
32
+
33
+ /**
34
+ * Reads a 32 bit float from a buffer using big endian byte order.
35
+ * @name readFloatBE
36
+ * @function
37
+ * @param {Uint8Array} buf Source buffer
38
+ * @param {number} pos Source buffer offset
39
+ * @returns {number} Value read
40
+ */
41
+ export function readFloatBE(buf: Uint8Array, pos: number): number;
42
+
43
+ /**
44
+ * Writes a 64 bit double to a buffer using little endian byte order.
45
+ * @name writeDoubleLE
46
+ * @function
47
+ * @param {number} val Value to write
48
+ * @param {Uint8Array} buf Target buffer
49
+ * @param {number} pos Target buffer offset
50
+ * @returns {undefined}
51
+ */
52
+ export function writeDoubleLE(val: number, buf: Uint8Array, pos: number): void;
53
+
54
+ /**
55
+ * Writes a 64 bit double to a buffer using big endian byte order.
56
+ * @name writeDoubleBE
57
+ * @function
58
+ * @param {number} val Value to write
59
+ * @param {Uint8Array} buf Target buffer
60
+ * @param {number} pos Target buffer offset
61
+ * @returns {undefined}
62
+ */
63
+ export function writeDoubleBE(val: number, buf: Uint8Array, pos: number): void;
64
+
65
+ /**
66
+ * Reads a 64 bit double from a buffer using little endian byte order.
67
+ * @name readDoubleLE
68
+ * @function
69
+ * @param {Uint8Array} buf Source buffer
70
+ * @param {number} pos Source buffer offset
71
+ * @returns {number} Value read
72
+ */
73
+ export function readDoubleLE(buf: Uint8Array, pos: number): number;
74
+
75
+ /**
76
+ * Reads a 64 bit double from a buffer using big endian byte order.
77
+ * @name readDoubleBE
78
+ * @function
79
+ * @param {Uint8Array} buf Source buffer
80
+ * @param {number} pos Source buffer offset
81
+ * @returns {number} Value read
82
+ */
83
+ export function readDoubleBE(buf: Uint8Array, pos: number): number;