UNPKG

protobufjs

Version:

Protocol Buffers for JavaScript & TypeScript.

1,709 lines (1,513 loc) 103 kB
/*! * protobuf.js v8.7.0 (c) 2016, daniel wirtz * compiled mon, 06 jul 2026 13:50:00 utc * licensed under the bsd-3-clause license * see: https://github.com/dcodeio/protobuf.js for details */ (function(undefined){"use strict";(function prelude(modules, cache, entries) { // This is the prelude used to bundle protobuf.js for the browser. Wraps up the CommonJS // sources through a conflict-free require shim and is again wrapped within an iife that // provides a minification-friendly `undefined` var plus a global "use strict" directive // so that minification can remove the directives of each module. function $require(name) { var $module = cache[name]; if (!$module) modules[name][0].call($module = cache[name] = { exports: {} }, $require, $module, $module.exports); return $module.exports; } var protobuf = $require(entries[0]); // Expose globally protobuf.util.global.protobuf = protobuf; // Be nice to AMD if (typeof define === "function" && define.amd) define(["long"], function(Long) { if (Long && Long.isLong) { protobuf.util.Long = Long; protobuf.configure(); } return protobuf; }); // Be nice to CommonJS if (typeof module === "object" && module && module.exports) module.exports = protobuf; })/* end of prelude */({1:[function(require,module,exports){ "use strict"; /** * Build type, one of `"full"`, `"light"` or `"minimal"`. * @name build * @type {string} * @const */ exports.build = "minimal"; // Serialization exports.Writer = require(15); exports.BufferWriter = require(16); exports.Reader = require(2); exports.BufferReader = require(3); // Utility exports.util = require(12); exports.rpc = require(5); exports.roots = require(4); exports.configure = configure; /* istanbul ignore next */ /** * Reconfigures the library according to the environment. * @returns {undefined} */ function configure() { exports.Writer._configure(exports.BufferWriter); exports.Reader._configure(exports.BufferReader); } // Set up buffer utility according to the environment configure(); },{"12":12,"15":15,"16":16,"2":2,"3":3,"4":4,"5":5}],2:[function(require,module,exports){ "use strict"; module.exports = Reader; var util = require(12); var BufferReader; // cyclic var LongBits = util.LongBits, utf8 = util.utf8; /* istanbul ignore next */ function indexOutOfRange(reader, writeLength) { return RangeError("index out of range: " + reader.pos + " + " + (writeLength || 1) + " > " + reader.len); } /** * Constructs a new reader instance using the specified buffer. * @classdesc Wire format reader using `Uint8Array`. * @constructor * @param {Uint8Array} buffer Buffer to read from */ function Reader(buffer) { /** * Read buffer. * @type {Uint8Array} */ this.buf = buffer; /** * Read buffer position. * @type {number} */ this.pos = 0; /** * Read buffer length. * @type {number} */ this.len = buffer.length; /** * Cached DataView for packed reads. * @type {DataView|null} */ this.view = null; /** * Whether to discard unknown fields while decoding. * @type {boolean} */ this.discardUnknown = Reader.discardUnknown; } function create_array(buffer) { // TODO: Remove plain array reader support in the next major release. if (Array.isArray(buffer)) buffer = new Uint8Array(buffer); if (buffer instanceof Uint8Array) return new Reader(buffer); throw Error("illegal buffer"); } var create = function create() { return util.Buffer ? function create_buffer_setup(buffer) { return (Reader.create = function create_buffer(buffer) { return util.Buffer.isBuffer(buffer) ? new BufferReader(buffer) /* istanbul ignore next */ : create_array(buffer); })(buffer); } /* istanbul ignore next */ : create_array; }; /** * Creates a new reader using the specified buffer. * @function * @param {Uint8Array|Buffer} buffer Buffer to read from * @returns {Reader|BufferReader} A {@link BufferReader} if `buffer` is a Buffer, otherwise a {@link Reader} * @throws {Error} If `buffer` is not a valid buffer */ Reader.create = create(); /** * Returns raw bytes from the backing buffer without advancing the reader. * @param {number} start Start offset * @param {number} end End offset * @returns {Uint8Array} Raw bytes */ Reader.prototype.raw = function read_raw(start, end) { return this.buf.subarray(start, end); }; /** * Reads a varint as an unsigned 32 bit value. * @function * @returns {number} Value read */ Reader.prototype.uint32 = function read_uint32() { var buf = this.buf, pos = this.pos, value = (buf[pos] & 127) >>> 0; if (buf[pos++] < 128) { this.pos = pos; return value; } value = (value | (buf[pos] & 127) << 7) >>> 0; if (buf[pos++] < 128) { this.pos = pos; return value; } value = (value | (buf[pos] & 127) << 14) >>> 0; if (buf[pos++] < 128) { this.pos = pos; return value; } value = (value | (buf[pos] & 127) << 21) >>> 0; if (buf[pos++] < 128) { this.pos = pos; return value; } value = (value | (buf[pos] & 15) << 28) >>> 0; if (buf[pos++] < 128) { this.pos = pos; return value; } for (var i = 0; i < 5; ++i) { /* istanbul ignore if */ if (pos >= this.len) { this.pos = pos; throw indexOutOfRange(this); } if (buf[pos++] < 128) { this.pos = pos; return value; } } /* istanbul ignore next */ this.pos = pos; throw Error("invalid varint encoding"); }; /** * Reads a field tag. * @function * @returns {number} Tag read */ Reader.prototype.tag = function read_tag() { var buf = this.buf, pos = this.pos, value = (buf[pos] & 127) >>> 0; if (buf[pos++] < 128) { this.pos = pos; return value; } value = (value | (buf[pos] & 127) << 7) >>> 0; if (buf[pos++] < 128) { this.pos = pos; return value; } value = (value | (buf[pos] & 127) << 14) >>> 0; if (buf[pos++] < 128) { this.pos = pos; return value; } value = (value | (buf[pos] & 127) << 21) >>> 0; if (buf[pos++] < 128) { this.pos = pos; return value; } value = (value | (buf[pos] & 15) << 28) >>> 0; if (buf[pos] < 128 && (buf[pos] & 112) === 0) { this.pos = pos + 1; return value; } this.pos = pos + 1; throw Error("invalid tag encoding"); }; /** * Reads a varint as a signed 32 bit value. * @returns {number} Value read */ Reader.prototype.int32 = function read_int32() { return this.uint32() | 0; }; /** * Reads a zig-zag encoded varint as a signed 32 bit value. * @returns {number} Value read */ Reader.prototype.sint32 = function read_sint32() { var value = this.uint32(); return value >>> 1 ^ -(value & 1) | 0; }; /* eslint-disable no-invalid-this */ function readLongVarint() { // tends to deopt with local vars for octet etc. var bits = new LongBits(0, 0); var i = 0; if (this.len - this.pos > 4) { // fast route (lo) for (; i < 4; ++i) { // 1st..4th bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0; if (this.buf[this.pos++] < 128) return bits; } // 5th bits.lo = (bits.lo | (this.buf[this.pos] & 127) << 28) >>> 0; bits.hi = (bits.hi | (this.buf[this.pos] & 127) >> 4) >>> 0; if (this.buf[this.pos++] < 128) return bits; i = 0; } else { for (; i < 4; ++i) { /* istanbul ignore if */ if (this.pos >= this.len) throw indexOutOfRange(this); // 1st..4th bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0; if (this.buf[this.pos++] < 128) return bits; } throw indexOutOfRange(this); } if (this.len - this.pos > 4) { // fast route (hi) for (; i < 5; ++i) { // 6th..10th bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0; if (this.buf[this.pos++] < 128) return bits; } } else { for (; i < 5; ++i) { /* istanbul ignore if */ if (this.pos >= this.len) throw indexOutOfRange(this); // 6th..10th bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0; if (this.buf[this.pos++] < 128) return bits; } } /* istanbul ignore next */ throw Error("invalid varint encoding"); } /* eslint-enable no-invalid-this */ /** * Reads a varint as a signed 64 bit value. * @name Reader#int64 * @function * @returns {Long} Value read */ /** * Reads a varint as an unsigned 64 bit value. * @name Reader#uint64 * @function * @returns {Long} Value read */ /** * Reads a zig-zag encoded varint as a signed 64 bit value. * @name Reader#sint64 * @function * @returns {Long} Value read */ /** * Reads a varint as a boolean. * @returns {boolean} Value read */ Reader.prototype.bool = function read_bool() { var value = false, b; for (var i = 0; i < 10; ++i) { /* istanbul ignore if */ if (this.pos >= this.len) throw indexOutOfRange(this); b = this.buf[this.pos++]; if (b & 127) value = true; if (b < 128) return value; } /* istanbul ignore next */ throw Error("invalid varint encoding"); }; function readFixed32_end(buf, end) { // note that this uses `end`, not `pos` return (buf[end - 4] | buf[end - 3] << 8 | buf[end - 2] << 16 | buf[end - 1] << 24) >>> 0; } /** * Reads fixed 32 bits as an unsigned 32 bit integer. * @returns {number} Value read */ Reader.prototype.fixed32 = function read_fixed32() { /* istanbul ignore if */ if (this.pos + 4 > this.len) throw indexOutOfRange(this, 4); return readFixed32_end(this.buf, this.pos += 4); }; /** * Reads fixed 32 bits as a signed 32 bit integer. * @returns {number} Value read */ Reader.prototype.sfixed32 = function read_sfixed32() { /* istanbul ignore if */ if (this.pos + 4 > this.len) throw indexOutOfRange(this, 4); return readFixed32_end(this.buf, this.pos += 4) | 0; }; /* eslint-disable no-invalid-this */ function readFixed64(/* this: Reader */) { /* istanbul ignore if */ if (this.pos + 8 > this.len) throw indexOutOfRange(this, 8); return new LongBits(readFixed32_end(this.buf, this.pos += 4), readFixed32_end(this.buf, this.pos += 4)); } /* eslint-enable no-invalid-this */ /** * Reads fixed 64 bits. * @name Reader#fixed64 * @function * @returns {Long} Value read */ /** * Reads zig-zag encoded fixed 64 bits. * @name Reader#sfixed64 * @function * @returns {Long} Value read */ /** * Reads a float (32 bit) as a number. * @function * @returns {number} Value read */ Reader.prototype.float = function read_float() { /* istanbul ignore if */ if (this.pos + 4 > this.len) throw indexOutOfRange(this, 4); var value = util.float.readFloatLE(this.buf, this.pos); this.pos += 4; return value; }; /** * Reads a double (64 bit float) as a number. * @function * @returns {number} Value read */ Reader.prototype.double = function read_double() { /* istanbul ignore if */ if (this.pos + 8 > this.len) throw indexOutOfRange(this, 4); var value = util.float.readDoubleLE(this.buf, this.pos); this.pos += 8; return value; }; /** * Reads a packed repeated field of unsigned 32 bit varints. * @param {number[]} [array] Array to read into; a new one is created if omitted * @returns {number[]} Array read into */ Reader.prototype.uint32s = function read_uint32s(array) { if (array === undefined) array = []; var end = this.uint32() + this.pos; while (this.pos < end) array.push(this.uint32()); return array; }; /** * Reads a packed repeated field of signed 32 bit varints. * @param {number[]} [array] Array to read into; a new one is created if omitted * @returns {number[]} Array read into */ Reader.prototype.int32s = function read_int32s(array) { if (array === undefined) array = []; var end = this.uint32() + this.pos; while (this.pos < end) array.push(this.int32()); return array; }; /** * Reads a packed repeated field of zig-zag encoded signed 32 bit varints. * @param {number[]} [array] Array to read into; a new one is created if omitted * @returns {number[]} Array read into */ Reader.prototype.sint32s = function read_sint32s(array) { if (array === undefined) array = []; var end = this.uint32() + this.pos; while (this.pos < end) array.push(this.sint32()); return array; }; /** * Reads a packed repeated field of booleans. * @param {boolean[]} [array] Array to read into; a new one is created if omitted * @returns {boolean[]} Array read into */ Reader.prototype.bools = function read_bools(array) { if (array === undefined) array = []; var end = this.uint32() + this.pos; while (this.pos < end) array.push(this.bool()); return array; }; // The view allocation only pays off when amortized over enough reads var VIEW_THRESHOLD_FLOAT = 8, VIEW_THRESHOLD_INT = 128; function getLazyView(reader, count, threshold) { var view = reader.view; if (view || count < threshold) return view; var buf = reader.buf; return reader.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength); } /** * Reads a packed repeated field of unsigned 32 bit fixed values. * @param {number[]} [array] Array to read into; a new one is created if omitted * @returns {number[]} Array read into */ Reader.prototype.fixed32s = function read_fixed32s(array) { if (array === undefined) array = []; var len = this.uint32(), end = this.pos + len; /* istanbul ignore if */ if (end > this.len) throw indexOutOfRange(this, len); var count = len >>> 2, i = array.length, pos = this.pos; array.length = i + count; var dv = getLazyView(this, count, VIEW_THRESHOLD_INT); if (dv) for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getUint32(pos, true); else { var buf = this.buf; for (var j = 0; j < count; ++j, pos += 4) array[i++] = readFixed32_end(buf, pos + 4); } this.pos = pos; if (pos !== end) throw indexOutOfRange(this, 4); return array; }; /** * Reads a packed repeated field of signed 32 bit fixed values. * @param {number[]} [array] Array to read into; a new one is created if omitted * @returns {number[]} Array read into */ Reader.prototype.sfixed32s = function read_sfixed32s(array) { if (array === undefined) array = []; var len = this.uint32(), end = this.pos + len; /* istanbul ignore if */ if (end > this.len) throw indexOutOfRange(this, len); var count = len >>> 2, i = array.length, pos = this.pos; array.length = i + count; var dv = getLazyView(this, count, VIEW_THRESHOLD_INT); if (dv) for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getInt32(pos, true); else { var buf = this.buf; for (var j = 0; j < count; ++j, pos += 4) array[i++] = readFixed32_end(buf, pos + 4) | 0; } this.pos = pos; if (pos !== end) throw indexOutOfRange(this, 4); return array; }; /** * Reads a packed repeated field of floats (32 bit). * @param {number[]} [array] Array to read into; a new one is created if omitted * @returns {number[]} Array read into */ Reader.prototype.floats = function read_floats(array) { if (array === undefined) array = []; var len = this.uint32(), end = this.pos + len; /* istanbul ignore if */ if (end > this.len) throw indexOutOfRange(this, len); var count = len >>> 2, i = array.length, pos = this.pos; array.length = i + count; var dv = getLazyView(this, count, VIEW_THRESHOLD_FLOAT); if (dv) for (var k = 0; k < count; ++k, pos += 4) array[i++] = dv.getFloat32(pos, true); else { var buf = this.buf; for (var j = 0; j < count; ++j, pos += 4) array[i++] = util.float.readFloatLE(buf, pos); } this.pos = pos; if (pos !== end) throw indexOutOfRange(this, 4); return array; }; /** * Reads a packed repeated field of doubles (64 bit float). * @param {number[]} [array] Array to read into; a new one is created if omitted * @returns {number[]} Array read into */ Reader.prototype.doubles = function read_doubles(array) { if (array === undefined) array = []; var len = this.uint32(), end = this.pos + len; /* istanbul ignore if */ if (end > this.len) throw indexOutOfRange(this, len); var count = len >>> 3, i = array.length, pos = this.pos; array.length = i + count; var dv = getLazyView(this, count, VIEW_THRESHOLD_FLOAT); if (dv) for (var k = 0; k < count; ++k, pos += 8) array[i++] = dv.getFloat64(pos, true); else { var buf = this.buf; for (var j = 0; j < count; ++j, pos += 8) array[i++] = util.float.readDoubleLE(buf, pos); } this.pos = pos; if (pos !== end) throw indexOutOfRange(this, 8); return array; }; /** * Reads a packed repeated field of unsigned 64 bit varints. * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted * @returns {Array.<Long|number>} Array read into */ Reader.prototype.uint64s = function read_uint64s(array) { if (array === undefined) array = []; var end = this.uint32() + this.pos; while (this.pos < end) array.push(this.uint64()); return array; }; /** * Reads a packed repeated field of signed 64 bit varints. * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted * @returns {Array.<Long|number>} Array read into */ Reader.prototype.int64s = function read_int64s(array) { if (array === undefined) array = []; var end = this.uint32() + this.pos; while (this.pos < end) array.push(this.int64()); return array; }; /** * Reads a packed repeated field of zig-zag encoded signed 64 bit varints. * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted * @returns {Array.<Long|number>} Array read into */ Reader.prototype.sint64s = function read_sint64s(array) { if (array === undefined) array = []; var end = this.uint32() + this.pos; while (this.pos < end) array.push(this.sint64()); return array; }; /** * Reads a packed repeated field of unsigned 64 bit fixed values. * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted * @returns {Array.<Long|number>} Array read into */ Reader.prototype.fixed64s = function read_fixed64s(array) { if (array === undefined) array = []; var len = this.uint32(), end = this.pos + len, i = array.length; /* istanbul ignore if */ if (end > this.len) throw indexOutOfRange(this, len); var count = len >>> 3; array.length = i + count; // 8 bytes per value, count is known for (var j = 0; j < count; ++j) array[i++] = this.fixed64(); if (this.pos !== end) throw indexOutOfRange(this, 8); return array; }; /** * Reads a packed repeated field of signed 64 bit fixed values. * @param {Array.<Long|number>} [array] Array to read into; a new one is created if omitted * @returns {Array.<Long|number>} Array read into */ Reader.prototype.sfixed64s = function read_sfixed64s(array) { if (array === undefined) array = []; var len = this.uint32(), end = this.pos + len, i = array.length; /* istanbul ignore if */ if (end > this.len) throw indexOutOfRange(this, len); var count = len >>> 3; array.length = i + count; // 8 bytes per value, count is known for (var j = 0; j < count; ++j) array[i++] = this.sfixed64(); if (this.pos !== end) throw indexOutOfRange(this, 8); return array; }; /** * Reads a sequence of bytes preceeded by its length as a varint. * @returns {Uint8Array} Value read */ Reader.prototype.bytes = function read_bytes() { var length = this.uint32(), start = this.pos, end = this.pos + length; /* istanbul ignore if */ if (end > this.len) throw indexOutOfRange(this, length); this.pos = end; return this.raw(start, end); }; /** * Reads a string preceeded by its byte length as a varint. * @returns {string} Value read */ Reader.prototype.string = function read_string() { var length = this.uint32(), start = this.pos, end = this.pos + length; /* istanbul ignore if */ if (end > this.len) throw indexOutOfRange(this, length); this.pos = end; return utf8.read(this.buf, start, end); }; /** * Reads a string preceeded by its byte length as a varint, rejecting invalid UTF8. * @returns {string} Value read */ Reader.prototype.stringVerify = function read_string_verify() { var length = this.uint32(), start = this.pos, end = this.pos + length; /* istanbul ignore if */ if (end > this.len) throw indexOutOfRange(this, length); this.pos = end; return utf8.readStrict(this.buf, start, end); }; /** * Skips the specified number of bytes if specified, otherwise skips a varint. * @param {number} [length] Length if known, otherwise a varint is assumed * @returns {Reader} `this` */ Reader.prototype.skip = function skip(length) { if (typeof length === "number") { /* istanbul ignore if */ if (this.pos + length > this.len) throw indexOutOfRange(this, length); this.pos += length; } else { do { /* istanbul ignore if */ if (this.pos >= this.len) throw indexOutOfRange(this); } while (this.buf[this.pos++] & 128); } return this; }; /** * Recursion limit. * @type {number} */ Reader.recursionLimit = util.recursionLimit; /** * Whether readers discard unknown fields while decoding. * @type {boolean} */ Reader.discardUnknown = true; /** * Skips the next element of the specified wire type. * @param {number} wireType Wire type received * @param {number} [depth] Depth of recursion to control nested calls; 0 if omitted * @param {number} [fieldNumber] Field number for validating group end tags * @returns {Reader} `this` */ Reader.prototype.skipType = function(wireType, depth, fieldNumber) { if (depth === undefined) depth = 0; if (depth > Reader.recursionLimit) throw Error("max depth exceeded"); if (fieldNumber === 0) throw Error("illegal tag: field number 0"); switch (wireType) { case 0: this.skip(); break; case 1: this.skip(8); break; case 2: this.skip(this.uint32()); break; case 3: while (true) { var tag = this.tag(); var nestedField = tag >>> 3; wireType = tag & 7; if (!nestedField) throw Error("illegal tag: field number 0"); if (wireType === 4) { if (fieldNumber !== undefined && nestedField !== fieldNumber) throw Error("invalid end group tag"); break; } this.skipType(wireType, depth + 1, nestedField); } break; case 5: this.skip(4); break; /* istanbul ignore next */ default: throw Error("invalid wire type " + wireType + " at offset " + this.pos); } return this; }; Reader._configure = function(BufferReader_) { BufferReader = BufferReader_; Reader.create = create(); BufferReader._configure(); var fn = util.Long ? "toLong" : /* istanbul ignore next */ "toNumber"; util.merge(Reader.prototype, { int64: function read_int64() { return readLongVarint.call(this)[fn](false); }, uint64: function read_uint64() { return readLongVarint.call(this)[fn](true); }, sint64: function read_sint64() { return readLongVarint.call(this).zzDecode()[fn](false); }, fixed64: function read_fixed64() { return readFixed64.call(this)[fn](true); }, sfixed64: function read_sfixed64() { return readFixed64.call(this)[fn](false); } }); }; },{"12":12}],3:[function(require,module,exports){ "use strict"; module.exports = BufferReader; // extends Reader var Reader = require(2); BufferReader.prototype = Object.create(Reader.prototype, { constructor: { value: BufferReader, writable: true, enumerable: false, configurable: true } }); var util = require(12); /** * Constructs a new buffer reader instance. * @classdesc Wire format reader using node buffers. * @extends Reader * @constructor * @param {Buffer} buffer Buffer to read from */ function BufferReader(buffer) { Reader.call(this, buffer); /** * Read buffer. * @name BufferReader#buf * @type {Buffer} */ } BufferReader._configure = function () { /* istanbul ignore else */ if (util.Buffer) BufferReader.prototype._slice = util.Buffer.prototype.slice; }; /** * Returns raw bytes from the backing buffer without advancing the reader. * @name BufferReader#raw * @function * @param {number} start Start offset * @param {number} end End offset * @returns {Buffer} Raw bytes */ BufferReader.prototype.raw = function read_raw_buffer(start, end) { return this._slice.call(this.buf, start, end); }; /** * @override */ BufferReader.prototype.string = function read_string_buffer() { var len = this.uint32(), // modifies pos start = this.pos, end = this.pos + len; /* istanbul ignore if */ if (end > this.len) throw RangeError("index out of range: " + this.pos + " + " + len + " > " + this.len); this.pos = end; return this.buf.utf8Slice ? this.buf.utf8Slice(start, end) : this.buf.toString("utf-8", start, end); }; /** * Reads a sequence of bytes preceeded by its length as a varint. * @name BufferReader#bytes * @function * @returns {Buffer} Value read */ BufferReader._configure(); },{"12":12,"2":2}],4:[function(require,module,exports){ "use strict"; module.exports = Object.create(null); /** * Named roots. * This is where pbjs stores generated structures (the option `-r, --root` specifies a name). * Can also be used manually to make roots available across modules. * @name roots * @type {Object.<string,Root>} * @example * // pbjs -r myroot -o compiled.js ... * * // in another module: * require("./compiled.js"); * * // in any subsequent module: * var root = protobuf.roots["myroot"]; */ },{}],5:[function(require,module,exports){ "use strict"; /** * Streaming RPC helpers. * @namespace */ var rpc = exports; /** * RPC implementation passed to {@link Service#create} performing a service request on network level, i.e. by utilizing http requests or websockets. * @typedef RPCImpl * @type {function} * @param {Method|rpc.ServiceMethod<Message<{}>,Message<{}>>} method Reflected or static method being called * @param {Uint8Array} requestData Request data * @param {RPCImplCallback} callback Callback function * @returns {undefined} * @example * function rpcImpl(method, requestData, callback) { * if (protobuf.util.lcFirst(method.name) !== "myMethod") // compatible with static code * throw Error("no such method"); * asynchronouslyObtainAResponse(requestData, function(err, responseData) { * callback(err, responseData); * }); * } */ /** * Node-style callback as used by {@link RPCImpl}. * @typedef RPCImplCallback * @type {function} * @param {Error|null} error Error, if any, otherwise `null` * @param {Uint8Array|null} [response] Response data or `null` to signal end of stream, if there hasn't been an error * @returns {undefined} */ rpc.Service = require(6); },{"6":6}],6:[function(require,module,exports){ "use strict"; module.exports = Service; var util = require(12); // Extends EventEmitter Service.prototype = Object.create(util.EventEmitter.prototype, { constructor: { value: Service, writable: true, enumerable: false, configurable: true } }); /** * A service method callback as used by {@link rpc.ServiceMethod|ServiceMethod}. * * Differs from {@link RPCImplCallback} in that it is an actual callback of a service method which may not return `response = null`. * @typedef rpc.ServiceMethodCallback * @template TRes extends Message<TRes> * @type {function} * @param {Error|null} error Error, if any * @param {TRes} [response] Response message * @returns {undefined} */ /** * A service method part of a {@link rpc.Service} as created by {@link Service.create}. * @typedef rpc.ServiceMethod * @template TReq extends Message<TReq> * @template TRes extends Message<TRes> * @type {{ * (request: TReq|Properties<TReq>, callback: rpc.ServiceMethodCallback<TRes>): void; * (request: TReq|Properties<TReq>): Promise<TRes>; * readonly name: string; * readonly path: string; * readonly requestType: string; * readonly responseType: string; * readonly requestStream: true|undefined; * readonly responseStream: true|undefined; * }} */ /** * Constructs a new RPC service instance. * @classdesc An RPC service as returned by {@link Service#create}. * @exports rpc.Service * @extends util.EventEmitter * @constructor * @param {RPCImpl} rpcImpl RPC implementation * @param {boolean} [requestDelimited=false] Whether requests are length-delimited * @param {boolean} [responseDelimited=false] Whether responses are length-delimited */ function Service(rpcImpl, requestDelimited, responseDelimited) { if (typeof rpcImpl !== "function") throw TypeError("rpcImpl must be a function"); util.EventEmitter.call(this); /** * RPC implementation. Becomes `null` once the service is ended. * @type {RPCImpl|null} */ this.rpcImpl = rpcImpl; /** * Whether requests are length-delimited. * @type {boolean} */ this.requestDelimited = Boolean(requestDelimited); /** * Whether responses are length-delimited. * @type {boolean} */ this.responseDelimited = Boolean(responseDelimited); } /** * Calls a service method through {@link rpc.Service#rpcImpl|rpcImpl}. * @param {Method|rpc.ServiceMethod<TReq,TRes>} method Reflected or static method * @param {Constructor<TReq>} requestCtor Request constructor * @param {Constructor<TRes>} responseCtor Response constructor * @param {TReq|Properties<TReq>} request Request message or plain object * @param {rpc.ServiceMethodCallback<TRes>} callback Service callback * @returns {undefined} * @template TReq extends Message<TReq> * @template TRes extends Message<TRes> */ Service.prototype.rpcCall = function rpcCall(method, requestCtor, responseCtor, request, callback) { if (!request) throw TypeError("request must be specified"); var self = this; if (!callback) return util.asPromise(rpcCall, self, method, requestCtor, responseCtor, request); if (!self.rpcImpl) { setTimeout(function() { callback(Error("already ended")); }, 0); return undefined; } try { return self.rpcImpl( method, requestCtor[self.requestDelimited ? "encodeDelimited" : "encode"](request).finish(), function rpcCallback(err, response) { if (err) { self.emit("error", err, method); return callback(err); } if (response === null) { self.end(/* endedByRPC */ true); return undefined; } if (!(response instanceof responseCtor)) { try { response = responseCtor[self.responseDelimited ? "decodeDelimited" : "decode"](response); } catch (err) { self.emit("error", err, method); return callback(err); } } self.emit("data", response, method); return callback(null, response); } ); } catch (err) { self.emit("error", err, method); setTimeout(function() { callback(err); }, 0); return undefined; } }; /** * Ends this service and emits the `end` event. * @param {boolean} [endedByRPC=false] Whether the service has been ended by the RPC implementation. * @returns {rpc.Service} `this` */ Service.prototype.end = function end(endedByRPC) { if (this.rpcImpl) { if (!endedByRPC) // signal end to rpcImpl this.rpcImpl(null, null, null); this.rpcImpl = null; this.emit("end").off(); } return this; }; },{"12":12}],7:[function(require,module,exports){ "use strict"; module.exports = asPromise; /** * Callback as used by {@link util.asPromise}. * @typedef asPromiseCallback * @type {function} * @param {Error|null} error Error, if any * @param {...*} params Additional arguments * @returns {undefined} */ /** * Returns a promise from a node-style callback function. * @memberof util * @param {asPromiseCallback} fn Function to call * @param {*} ctx Function context * @param {...*} params Function arguments * @returns {Promise<*>} Promisified function */ function asPromise(fn, ctx/*, varargs */) { var params = new Array(arguments.length - 1), offset = 0, index = 2, pending = true; while (index < arguments.length) params[offset++] = arguments[index++]; return new Promise(function executor(resolve, reject) { params[offset] = function callback(err/*, varargs */) { if (pending) { pending = false; if (err) reject(err); else { var params = new Array(arguments.length - 1), offset = 0; while (offset < params.length) params[offset++] = arguments[offset]; resolve.apply(null, params); } } }; try { fn.apply(ctx || null, params); } catch (err) { if (pending) { pending = false; reject(err); } } }); } },{}],8:[function(require,module,exports){ "use strict"; /** * A minimal base64 implementation for number arrays. * @memberof util * @namespace */ var base64 = exports; /** * Calculates the byte length of a base64 encoded string. * @param {string} string Base64 encoded string * @returns {number} Byte length */ base64.length = function length(string) { var p = string.length; if (!p) return 0; while (p > 0 && string.charAt(p - 1) === "=") --p; return Math.floor(p * 3 / 4); }; // Base64 encoding table var b64 = new Array(64); // Base64 decoding table var s64 = new Array(123); // 65..90, 97..122, 48..57, 43, 47 for (var i = 0; i < 64;) s64[b64[i] = i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i - 59 | 43] = i++; s64[45] = 62; // - -> + s64[95] = 63; // _ -> / /** * Encodes a buffer to a base64 encoded string. * @param {Uint8Array} buffer Source buffer * @param {number} start Source start * @param {number} end Source end * @returns {string} Base64 encoded string */ base64.encode = function encode(buffer, start, end) { var parts = null, chunk = []; var i = 0, // output index j = 0, // goto index t; // temporary while (start < end) { var b = buffer[start++]; switch (j) { case 0: chunk[i++] = b64[b >> 2]; t = (b & 3) << 4; j = 1; break; case 1: chunk[i++] = b64[t | b >> 4]; t = (b & 15) << 2; j = 2; break; case 2: chunk[i++] = b64[t | b >> 6]; chunk[i++] = b64[b & 63]; j = 0; break; } if (i > 8191) { (parts || (parts = [])).push(String.fromCharCode.apply(String, chunk)); i = 0; } } if (j) { chunk[i++] = b64[t]; chunk[i++] = 61; if (j === 1) chunk[i++] = 61; } if (parts) { if (i) parts.push(String.fromCharCode.apply(String, chunk.slice(0, i))); return parts.join(""); } return String.fromCharCode.apply(String, chunk.slice(0, i)); }; var invalidEncoding = "invalid encoding"; /** * Decodes a base64 encoded string to a buffer. * @param {string} string Source string * @param {Uint8Array} buffer Destination buffer * @param {number} offset Destination offset * @returns {number} Number of bytes written * @throws {Error} If encoding is invalid */ base64.decode = function decode(string, buffer, offset) { var start = offset; var j = 0, // goto index t; // temporary for (var i = 0; i < string.length;) { var c = string.charCodeAt(i++); if (c === 61 && j > 1) break; if ((c = s64[c]) === undefined) throw Error(invalidEncoding); switch (j) { case 0: t = c; j = 1; break; case 1: buffer[offset++] = t << 2 | (c & 48) >> 4; t = c; j = 2; break; case 2: buffer[offset++] = (t & 15) << 4 | (c & 60) >> 2; t = c; j = 3; break; case 3: buffer[offset++] = (t & 3) << 6 | c; j = 0; break; } } if (j === 1) throw Error(invalidEncoding); return offset - start; }; var base64Re = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/, base64UrlRe = /[-_]/, base64UrlNoPaddingRe = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2}(?:==)?|[A-Za-z0-9_-]{3}=?)?$/; /** * Tests if the specified string appears to be base64 encoded. * @param {string} string String to test * @returns {boolean} `true` if probably base64 encoded, otherwise false */ base64.test = function test(string) { return base64Re.test(string) || base64UrlRe.test(string) && base64UrlNoPaddingRe.test(string); }; },{}],9:[function(require,module,exports){ "use strict"; module.exports = EventEmitter; /** * Constructs a new event emitter instance. * @classdesc A minimal event emitter. * @memberof util * @constructor */ function EventEmitter() { /** * Registered listeners. * @type {Object.<string,*>} * @private */ this._listeners = Object.create(null); } /** * Event listener as used by {@link util.EventEmitter}. * @typedef EventEmitterListener * @type {function} * @param {...*} args Arguments * @returns {undefined} */ /** * Registers an event listener. * @param {string} evt Event name * @param {EventEmitterListener} fn Listener * @param {*} [ctx] Listener context * @returns {this} `this` */ EventEmitter.prototype.on = function on(evt, fn, ctx) { (this._listeners[evt] || (this._listeners[evt] = [])).push({ fn : fn, ctx : ctx || this }); return this; }; /** * Removes an event listener or any matching listeners if arguments are omitted. * @param {string} [evt] Event name. Removes all listeners if omitted. * @param {EventEmitterListener} [fn] Listener to remove. Removes all listeners of `evt` if omitted. * @returns {this} `this` */ EventEmitter.prototype.off = function off(evt, fn) { if (evt === undefined) this._listeners = Object.create(null); else { if (fn === undefined) this._listeners[evt] = []; else { var listeners = this._listeners[evt]; if (!listeners) return this; for (var i = 0; i < listeners.length;) if (listeners[i].fn === fn) listeners.splice(i, 1); else ++i; } } return this; }; /** * Emits an event by calling its listeners with the specified arguments. * @param {string} evt Event name * @param {...*} args Arguments * @returns {this} `this` */ EventEmitter.prototype.emit = function emit(evt) { var listeners = this._listeners[evt]; if (listeners) { var args = [], i = 1; for (; i < arguments.length;) args.push(arguments[i++]); for (i = 0; i < listeners.length;) listeners[i].fn.apply(listeners[i++].ctx, args); } return this; }; },{}],10:[function(require,module,exports){ "use strict"; module.exports = factory(factory); /** * Reads / writes floats / doubles from / to buffers. * @name util.float * @namespace */ /** * Writes a 32 bit float to a buffer using little endian byte order. * @name util.float.writeFloatLE * @function * @param {number} val Value to write * @param {Uint8Array} buf Target buffer * @param {number} pos Target buffer offset * @returns {undefined} */ /** * Writes a 32 bit float to a buffer using big endian byte order. * @name util.float.writeFloatBE * @function * @param {number} val Value to write * @param {Uint8Array} buf Target buffer * @param {number} pos Target buffer offset * @returns {undefined} */ /** * Reads a 32 bit float from a buffer using little endian byte order. * @name util.float.readFloatLE * @function * @param {Uint8Array} buf Source buffer * @param {number} pos Source buffer offset * @returns {number} Value read */ /** * Reads a 32 bit float from a buffer using big endian byte order. * @name util.float.readFloatBE * @function * @param {Uint8Array} buf Source buffer * @param {number} pos Source buffer offset * @returns {number} Value read */ /** * Writes a 64 bit double to a buffer using little endian byte order. * @name util.float.writeDoubleLE * @function * @param {number} val Value to write * @param {Uint8Array} buf Target buffer * @param {number} pos Target buffer offset * @returns {undefined} */ /** * Writes a 64 bit double to a buffer using big endian byte order. * @name util.float.writeDoubleBE * @function * @param {number} val Value to write * @param {Uint8Array} buf Target buffer * @param {number} pos Target buffer offset * @returns {undefined} */ /** * Reads a 64 bit double from a buffer using little endian byte order. * @name util.float.readDoubleLE * @function * @param {Uint8Array} buf Source buffer * @param {number} pos Source buffer offset * @returns {number} Value read */ /** * Reads a 64 bit double from a buffer using big endian byte order. * @name util.float.readDoubleBE * @function * @param {Uint8Array} buf Source buffer * @param {number} pos Source buffer offset * @returns {number} Value read */ // Factory function for the purpose of node-based testing in modified global environments function factory(exports) { // float: typed array if (typeof Float32Array !== "undefined") (function() { var f32 = new Float32Array([ -0 ]), f8b = new Uint8Array(f32.buffer), le = f8b[3] === 128; function writeFloat_f32_cpy(val, buf, pos) { f32[0] = val; buf[pos ] = f8b[0]; buf[pos + 1] = f8b[1]; buf[pos + 2] = f8b[2]; buf[pos + 3] = f8b[3]; } function writeFloat_f32_rev(val, buf, pos) { f32[0] = val; buf[pos ] = f8b[3]; buf[pos + 1] = f8b[2]; buf[pos + 2] = f8b[1]; buf[pos + 3] = f8b[0]; } /* istanbul ignore next */ exports.writeFloatLE = le ? writeFloat_f32_cpy : writeFloat_f32_rev; /* istanbul ignore next */ exports.writeFloatBE = le ? writeFloat_f32_rev : writeFloat_f32_cpy; function readFloat_f32_cpy(buf, pos) { f8b[0] = buf[pos ]; f8b[1] = buf[pos + 1]; f8b[2] = buf[pos + 2]; f8b[3] = buf[pos + 3]; return f32[0]; } function readFloat_f32_rev(buf, pos) { f8b[3] = buf[pos ]; f8b[2] = buf[pos + 1]; f8b[1] = buf[pos + 2]; f8b[0] = buf[pos + 3]; return f32[0]; } /* istanbul ignore next */ exports.readFloatLE = le ? readFloat_f32_cpy : readFloat_f32_rev; /* istanbul ignore next */ exports.readFloatBE = le ? readFloat_f32_rev : readFloat_f32_cpy; // float: ieee754 })(); else (function() { function writeFloat_ieee754(writeUint, val, buf, pos) { var sign = val < 0 ? 1 : 0; if (sign) val = -val; if (val === 0) writeUint(1 / val > 0 ? /* positive */ 0 : /* negative 0 */ 2147483648, buf, pos); else if (isNaN(val)) writeUint(2143289344, buf, pos); else if (val > 3.4028234663852886e+38) // +-Infinity writeUint((sign << 31 | 2139095040) >>> 0, buf, pos); else if (val < 1.1754943508222875e-38) // denormal writeUint((sign << 31 | Math.round(val / 1.401298464324817e-45)) >>> 0, buf, pos); else { var exponent = Math.floor(Math.log(val) / Math.LN2), mantissa = Math.round(val * Math.pow(2, -exponent) * 8388608) & 8388607; writeUint((sign << 31 | exponent + 127 << 23 | mantissa) >>> 0, buf, pos); } } exports.writeFloatLE = writeFloat_ieee754.bind(null, writeUintLE); exports.writeFloatBE = writeFloat_ieee754.bind(null, writeUintBE); function readFloat_ieee754(readUint, buf, pos) { var uint = readUint(buf, pos), sign = (uint >> 31) * 2 + 1, exponent = uint >>> 23 & 255, mantissa = uint & 8388607; return exponent === 255 ? mantissa ? NaN : sign * Infinity : exponent === 0 // denormal ? sign * 1.401298464324817e-45 * mantissa : sign * Math.pow(2, exponent - 150) * (mantissa + 8388608); } exports.readFloatLE = readFloat_ieee754.bind(null, readUintLE); exports.readFloatBE = readFloat_ieee754.bind(null, readUintBE); })(); // double: typed array if (typeof Float64Array !== "undefined") (function() { var f64 = new Float64Array([-0]), f8b = new Uint8Array(f64.buffer), le = f8b[7] === 128; function writeDouble_f64_cpy(val, buf, pos) { f64[0] = val; buf[pos ] = f8b[0]; buf[pos + 1] = f8b[1]; buf[pos + 2] = f8b[2]; buf[pos + 3] = f8b[3]; buf[pos + 4] = f8b[4]; buf[pos + 5] = f8b[5]; buf[pos + 6] = f8b[6]; buf[pos + 7] = f8b[7]; } function writeDouble_f64_rev(val, buf, pos) { f64[0] = val; buf[pos ] = f8b[7]; buf[pos + 1] = f8b[6]; buf[pos + 2] = f8b[5]; buf[pos + 3] = f8b[4]; buf[pos + 4] = f8b[3]; buf[pos + 5] = f8b[2]; buf[pos + 6] = f8b[1]; buf[pos + 7] = f8b[0]; } /* istanbul ignore next */ exports.writeDoubleLE = le ? writeDouble_f64_cpy : writeDouble_f64_rev; /* istanbul ignore next */ exports.writeDoubleBE = le ? writeDouble_f64_rev : writeDouble_f64_cpy; function readDouble_f64_cpy(buf, pos) { f8b[0] = buf[pos ]; f8b[1] = buf[pos + 1]; f8b[2] = buf[pos + 2]; f8b[3] = buf[pos + 3]; f8b[4] = buf[pos + 4]; f8b[5] = buf[pos + 5]; f8b[6] = buf[pos + 6]; f8b[7] = buf[pos + 7]; return f64[0]; } function readDouble_f64_rev(buf, pos) { f8b[7] = buf[pos ]; f8b[6] = buf[pos + 1]; f8b[5] = buf[pos + 2]; f8b[4] = buf[pos + 3]; f8b[3] = buf[pos + 4]; f8b[2] = buf[pos + 5]; f8b[1] = buf[pos + 6]; f8b[0] = buf[pos + 7]; return f64[0]; } /* istanbul ignore next */ exports.readDoubleLE = le ? readDouble_f64_cpy : readDouble_f64_rev; /* istanbul ignore next */ exports.readDoubleBE = le ? readDouble_f64_rev : readDouble_f64_cpy; // double: ieee754 })(); else (function() { function writeDouble_ieee754(writeUint, off0, off1, val, buf, pos) { var sign = val < 0 ? 1 : 0; if (sign) val = -val; if (val === 0) { writeUint(0, buf, pos + off0); writeUint(1 / val > 0 ? /* positive */ 0 : /* negative 0 */ 2147483648, buf, pos + off1); } else if (isNaN(val)) {