UNPKG

mustreams

Version:

Binary stream and buffer for mudb

261 lines 8.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var browser_string_1 = require("./browser-string"); function ceilLog2(v_) { var v = v_ - 1; var r = (v > 0xFFFF) ? 1 << 4 : 0; v >>>= r; var shift = (v > 0xFF) ? 1 << 3 : 0; v >>>= shift; r |= shift; shift = (v > 0xF) ? 1 << 2 : 0; v >>>= shift; r |= shift; shift = (v > 0x3) ? 1 << 1 : 0; v >>>= shift; r |= shift; return (r | (v >> 1)) + 1; } var MuBuffer = (function () { function MuBuffer(buffer) { this.buffer = buffer; this.dataView = new DataView(buffer); this.uint8 = new Uint8Array(buffer); } return MuBuffer; }()); exports.MuBuffer = MuBuffer; var bufferPool = new Array(32); for (var i = 0; i < 32; ++i) { bufferPool[i] = []; } function allocBuffer(size) { var b = ceilLog2(size); return bufferPool[b].pop() || new MuBuffer(new ArrayBuffer(1 << b)); } exports.allocBuffer = allocBuffer; function freeBuffer(buffer) { if (buffer.uint8.length > 0) { bufferPool[ceilLog2(buffer.uint8.length)].push(buffer); } } exports.freeBuffer = freeBuffer; function reallocBuffer(buffer, nsize) { if (buffer.uint8.length >= nsize) { return buffer; } var result = allocBuffer(nsize); result.uint8.set(buffer.uint8); freeBuffer(buffer); return result; } exports.reallocBuffer = reallocBuffer; var LITTLE_ENDIAN = true; var MuWriteStream = (function () { function MuWriteStream(capacity) { this.buffer = allocBuffer(capacity); this.offset = 0; } MuWriteStream.prototype.bytes = function () { return this.buffer.uint8.subarray(0, this.offset); }; MuWriteStream.prototype.destroy = function () { freeBuffer(this.buffer); }; MuWriteStream.prototype.grow = function (bytes) { this.buffer = reallocBuffer(this.buffer, this.offset + bytes); }; MuWriteStream.prototype.writeInt8 = function (x) { this.buffer.dataView.setInt8(this.offset++, x); }; MuWriteStream.prototype.writeInt16 = function (x) { this.buffer.dataView.setInt16(this.offset, x, LITTLE_ENDIAN); this.offset += 2; }; MuWriteStream.prototype.writeInt32 = function (x) { this.buffer.dataView.setInt32(this.offset, x, LITTLE_ENDIAN); this.offset += 4; }; MuWriteStream.prototype.writeUint8 = function (x) { this.buffer.dataView.setUint8(this.offset++, x); }; MuWriteStream.prototype.writeUint16 = function (x) { this.buffer.dataView.setUint16(this.offset, x, LITTLE_ENDIAN); this.offset += 2; }; MuWriteStream.prototype.writeUint32 = function (x) { this.buffer.dataView.setUint32(this.offset, x, LITTLE_ENDIAN); this.offset += 4; }; MuWriteStream.prototype.writeFloat32 = function (x) { this.buffer.dataView.setFloat32(this.offset, x, LITTLE_ENDIAN); this.offset += 4; }; MuWriteStream.prototype.writeFloat64 = function (x) { this.buffer.dataView.setFloat64(this.offset, x, LITTLE_ENDIAN); this.offset += 8; }; MuWriteStream.prototype.writeVarInt = function (x_) { var x = x_ >>> 0; var bytes = this.buffer.uint8; var offset = this.offset; if (x < (1 << 7)) { bytes[offset] = x; this.offset += 1; } else if (x < (1 << 14)) { bytes[offset] = 0x80 | (x & 0x7f); bytes[offset + 1] = x >>> 7; this.offset += 2; } else if (x < (1 << 21)) { bytes[offset] = 0x80 | (x & 0x7f); bytes[offset + 1] = 0x80 | ((x >> 7) & 0x7f); bytes[offset + 2] = x >>> 14; this.offset += 3; } else if (x < (1 << 28)) { bytes[offset] = 0x80 | (x & 0x7f); bytes[offset + 1] = 0x80 | ((x >> 7) & 0x7f); bytes[offset + 2] = 0x80 | ((x >> 14) & 0x7f); bytes[offset + 3] = x >>> 21; this.offset += 4; } else { bytes[offset] = 0x80 | (x & 0x7f); bytes[offset + 1] = 0x80 | ((x >> 7) & 0x7f); bytes[offset + 2] = 0x80 | ((x >> 14) & 0x7f); bytes[offset + 3] = 0x80 | ((x >> 21) & 0x7f); bytes[offset + 4] = x >>> 28; this.offset += 5; } }; MuWriteStream.prototype.writeASCIINoLength = function (str) { for (var i = 0; i < str.length; ++i) { this.writeUint8(str.charCodeAt(i)); } }; MuWriteStream.prototype.writeASCII = function (str) { this.writeUint32(str.length); this.writeASCIINoLength(str); }; MuWriteStream.prototype.writeString = function (str) { var bytes = browser_string_1.encodeString(str); this.writeUint32(bytes.length); this.buffer.uint8.set(bytes, this.offset); this.offset += bytes.length; }; MuWriteStream.prototype.writeUint8At = function (offset, x) { this.buffer.dataView.setUint8(offset, x); }; MuWriteStream.prototype.writeUint32At = function (offset, x) { this.buffer.dataView.setUint32(offset, x, LITTLE_ENDIAN); }; return MuWriteStream; }()); exports.MuWriteStream = MuWriteStream; var MuReadStream = (function () { function MuReadStream(data) { this.buffer = new MuBuffer(data.buffer); this.offset = data.byteOffset; this.length = data.byteLength + data.byteOffset; } MuReadStream.prototype.readInt8 = function () { return this.buffer.dataView.getInt8(this.offset++); }; MuReadStream.prototype.readInt16 = function () { var offset = this.offset; this.offset += 2; return this.buffer.dataView.getInt16(offset, LITTLE_ENDIAN); }; MuReadStream.prototype.readInt32 = function () { var offset = this.offset; this.offset += 4; return this.buffer.dataView.getInt32(offset, LITTLE_ENDIAN); }; MuReadStream.prototype.readUint8 = function () { return this.buffer.dataView.getUint8(this.offset++); }; MuReadStream.prototype.readUint16 = function () { var offset = this.offset; this.offset += 2; return this.buffer.dataView.getUint16(offset, LITTLE_ENDIAN); }; MuReadStream.prototype.readUint32 = function () { var offset = this.offset; this.offset += 4; return this.buffer.dataView.getUint32(offset, LITTLE_ENDIAN); }; MuReadStream.prototype.readFloat32 = function () { var offset = this.offset; this.offset += 4; return this.buffer.dataView.getFloat32(offset, LITTLE_ENDIAN); }; MuReadStream.prototype.readFloat64 = function () { var offset = this.offset; this.offset += 8; return this.buffer.dataView.getFloat64(offset, LITTLE_ENDIAN); }; MuReadStream.prototype.readVarInt = function () { var bytes = this.buffer.uint8; var offset = this.offset; var x0 = bytes[offset++]; if (x0 < 0x80) { this.offset = offset; return x0; } var x1 = bytes[offset++]; if (x1 < 0x80) { this.offset = offset; return (x0 & 0x7f) | (x1 << 7); } var x2 = bytes[offset++]; if (x2 < 0x80) { this.offset = offset; return (x0 & 0x7f) | ((x1 & 0x7f) << 7) | (x2 << 14); } var x3 = bytes[offset++]; if (x3 < 0x80) { this.offset = offset; return (x0 & 0x7f) | ((x1 & 0x7f) << 7) | ((x2 & 0x7f) << 14) | (x3 << 21); } var x4 = bytes[offset++]; this.offset = offset; return (x0 & 0x7f) + ((x1 & 0x7f) << 7) + ((x2 & 0x7f) << 14) + ((x3 & 0x7f) << 21) + (x4 * (1 << 28)); }; MuReadStream.prototype.readASCIIOf = function (length) { var head = this.offset; this.offset += length; var str = ''; for (var i = head; i < this.offset; ++i) { str += String.fromCharCode(this.buffer.uint8[i]); } return str; }; MuReadStream.prototype.readASCII = function () { var length = this.readUint32(); return this.readASCIIOf(length); }; MuReadStream.prototype.readString = function () { var byteLength = this.readUint32(); var bytes = this.buffer.uint8.subarray(this.offset, this.offset + byteLength); this.offset += byteLength; return browser_string_1.decodeString(bytes); }; MuReadStream.prototype.readUint8At = function (offset) { return this.buffer.dataView.getUint8(offset); }; return MuReadStream; }()); exports.MuReadStream = MuReadStream; //# sourceMappingURL=index.js.map