UNPKG

flatbuffers

Version:

Memory Efficient Serialization Library

253 lines (252 loc) 9.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ByteBuffer = void 0; var constants_js_1 = require("./constants.js"); var utils_js_1 = require("./utils.js"); var encoding_js_1 = require("./encoding.js"); var ByteBuffer = /** @class */ (function () { /** * Create a new ByteBuffer with a given array of bytes (`Uint8Array`) */ function ByteBuffer(bytes_) { this.bytes_ = bytes_; this.position_ = 0; this.text_decoder_ = new TextDecoder(); } /** * Create and allocate a new ByteBuffer with a given size. */ ByteBuffer.allocate = function (byte_size) { return new ByteBuffer(new Uint8Array(byte_size)); }; ByteBuffer.prototype.clear = function () { this.position_ = 0; }; /** * Get the underlying `Uint8Array`. */ ByteBuffer.prototype.bytes = function () { return this.bytes_; }; /** * Get the buffer's position. */ ByteBuffer.prototype.position = function () { return this.position_; }; /** * Set the buffer's position. */ ByteBuffer.prototype.setPosition = function (position) { this.position_ = position; }; /** * Get the buffer's capacity. */ ByteBuffer.prototype.capacity = function () { return this.bytes_.length; }; ByteBuffer.prototype.readInt8 = function (offset) { return this.readUint8(offset) << 24 >> 24; }; ByteBuffer.prototype.readUint8 = function (offset) { return this.bytes_[offset]; }; ByteBuffer.prototype.readInt16 = function (offset) { return this.readUint16(offset) << 16 >> 16; }; ByteBuffer.prototype.readUint16 = function (offset) { return this.bytes_[offset] | this.bytes_[offset + 1] << 8; }; ByteBuffer.prototype.readInt32 = function (offset) { return this.bytes_[offset] | this.bytes_[offset + 1] << 8 | this.bytes_[offset + 2] << 16 | this.bytes_[offset + 3] << 24; }; ByteBuffer.prototype.readUint32 = function (offset) { return this.readInt32(offset) >>> 0; }; ByteBuffer.prototype.readInt64 = function (offset) { return BigInt.asIntN(64, BigInt(this.readUint32(offset)) + (BigInt(this.readUint32(offset + 4)) << BigInt(32))); }; ByteBuffer.prototype.readUint64 = function (offset) { return BigInt.asUintN(64, BigInt(this.readUint32(offset)) + (BigInt(this.readUint32(offset + 4)) << BigInt(32))); }; ByteBuffer.prototype.readFloat32 = function (offset) { utils_js_1.int32[0] = this.readInt32(offset); return utils_js_1.float32[0]; }; ByteBuffer.prototype.readFloat64 = function (offset) { utils_js_1.int32[utils_js_1.isLittleEndian ? 0 : 1] = this.readInt32(offset); utils_js_1.int32[utils_js_1.isLittleEndian ? 1 : 0] = this.readInt32(offset + 4); return utils_js_1.float64[0]; }; ByteBuffer.prototype.writeInt8 = function (offset, value) { this.bytes_[offset] = value; }; ByteBuffer.prototype.writeUint8 = function (offset, value) { this.bytes_[offset] = value; }; ByteBuffer.prototype.writeInt16 = function (offset, value) { this.bytes_[offset] = value; this.bytes_[offset + 1] = value >> 8; }; ByteBuffer.prototype.writeUint16 = function (offset, value) { this.bytes_[offset] = value; this.bytes_[offset + 1] = value >> 8; }; ByteBuffer.prototype.writeInt32 = function (offset, value) { this.bytes_[offset] = value; this.bytes_[offset + 1] = value >> 8; this.bytes_[offset + 2] = value >> 16; this.bytes_[offset + 3] = value >> 24; }; ByteBuffer.prototype.writeUint32 = function (offset, value) { this.bytes_[offset] = value; this.bytes_[offset + 1] = value >> 8; this.bytes_[offset + 2] = value >> 16; this.bytes_[offset + 3] = value >> 24; }; ByteBuffer.prototype.writeInt64 = function (offset, value) { this.writeInt32(offset, Number(BigInt.asIntN(32, value))); this.writeInt32(offset + 4, Number(BigInt.asIntN(32, value >> BigInt(32)))); }; ByteBuffer.prototype.writeUint64 = function (offset, value) { this.writeUint32(offset, Number(BigInt.asUintN(32, value))); this.writeUint32(offset + 4, Number(BigInt.asUintN(32, value >> BigInt(32)))); }; ByteBuffer.prototype.writeFloat32 = function (offset, value) { utils_js_1.float32[0] = value; this.writeInt32(offset, utils_js_1.int32[0]); }; ByteBuffer.prototype.writeFloat64 = function (offset, value) { utils_js_1.float64[0] = value; this.writeInt32(offset, utils_js_1.int32[utils_js_1.isLittleEndian ? 0 : 1]); this.writeInt32(offset + 4, utils_js_1.int32[utils_js_1.isLittleEndian ? 1 : 0]); }; /** * Return the file identifier. Behavior is undefined for FlatBuffers whose * schema does not include a file_identifier (likely points at padding or the * start of a the root vtable). */ ByteBuffer.prototype.getBufferIdentifier = function () { if (this.bytes_.length < this.position_ + constants_js_1.SIZEOF_INT + constants_js_1.FILE_IDENTIFIER_LENGTH) { throw new Error('FlatBuffers: ByteBuffer is too short to contain an identifier.'); } var result = ""; for (var i = 0; i < constants_js_1.FILE_IDENTIFIER_LENGTH; i++) { result += String.fromCharCode(this.readInt8(this.position_ + constants_js_1.SIZEOF_INT + i)); } return result; }; /** * Look up a field in the vtable, return an offset into the object, or 0 if the * field is not present. */ ByteBuffer.prototype.__offset = function (bb_pos, vtable_offset) { var vtable = bb_pos - this.readInt32(bb_pos); return vtable_offset < this.readInt16(vtable) ? this.readInt16(vtable + vtable_offset) : 0; }; /** * Initialize any Table-derived type to point to the union at the given offset. */ ByteBuffer.prototype.__union = function (t, offset) { t.bb_pos = offset + this.readInt32(offset); t.bb = this; return t; }; /** * Create a JavaScript string from UTF-8 data stored inside the FlatBuffer. * This allocates a new string and converts to wide chars upon each access. * * To avoid the conversion to string, pass Encoding.UTF8_BYTES as the * "optionalEncoding" argument. This is useful for avoiding conversion when * the data will just be packaged back up in another FlatBuffer later on. * * @param offset * @param opt_encoding Defaults to UTF16_STRING */ ByteBuffer.prototype.__string = function (offset, opt_encoding) { offset += this.readInt32(offset); var length = this.readInt32(offset); offset += constants_js_1.SIZEOF_INT; var utf8bytes = this.bytes_.subarray(offset, offset + length); if (opt_encoding === encoding_js_1.Encoding.UTF8_BYTES) return utf8bytes; else return this.text_decoder_.decode(utf8bytes); }; /** * Handle unions that can contain string as its member, if a Table-derived type then initialize it, * if a string then return a new one * * WARNING: strings are immutable in JS so we can't change the string that the user gave us, this * makes the behaviour of __union_with_string different compared to __union */ ByteBuffer.prototype.__union_with_string = function (o, offset) { if (typeof o === 'string') { return this.__string(offset); } return this.__union(o, offset); }; /** * Retrieve the relative offset stored at "offset" */ ByteBuffer.prototype.__indirect = function (offset) { return offset + this.readInt32(offset); }; /** * Get the start of data of a vector whose offset is stored at "offset" in this object. */ ByteBuffer.prototype.__vector = function (offset) { return offset + this.readInt32(offset) + constants_js_1.SIZEOF_INT; // data starts after the length }; /** * Get the length of a vector whose offset is stored at "offset" in this object. */ ByteBuffer.prototype.__vector_len = function (offset) { return this.readInt32(offset + this.readInt32(offset)); }; ByteBuffer.prototype.__has_identifier = function (ident) { if (ident.length != constants_js_1.FILE_IDENTIFIER_LENGTH) { throw new Error('FlatBuffers: file identifier must be length ' + constants_js_1.FILE_IDENTIFIER_LENGTH); } for (var i = 0; i < constants_js_1.FILE_IDENTIFIER_LENGTH; i++) { if (ident.charCodeAt(i) != this.readInt8(this.position() + constants_js_1.SIZEOF_INT + i)) { return false; } } return true; }; /** * A helper function for generating list for obj api */ ByteBuffer.prototype.createScalarList = function (listAccessor, listLength) { var ret = []; for (var i = 0; i < listLength; ++i) { var val = listAccessor(i); if (val !== null) { ret.push(val); } } return ret; }; /** * A helper function for generating list for obj api * @param listAccessor function that accepts an index and return data at that index * @param listLength listLength * @param res result list */ ByteBuffer.prototype.createObjList = function (listAccessor, listLength) { var ret = []; for (var i = 0; i < listLength; ++i) { var val = listAccessor(i); if (val !== null) { ret.push(val.unpack()); } } return ret; }; return ByteBuffer; }()); exports.ByteBuffer = ByteBuffer;