UNPKG

zigbee-herdsman-zigate

Version:

An open source ZigBee gateway solution with node.js.

144 lines 4.54 kB
"use strict"; // fork of original project: https://github.com/villadora/node-buffer-reader (BSD License) // simply added a 'isMore' function var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const assert_1 = __importDefault(require("assert")); const outError = 'Out of Original Buffer\'s Boundary'; /** * @method nextUInt16BE */ class BufferReader { constructor(buffer) { buffer = buffer || Buffer.alloc(0); assert_1.default(Buffer.isBuffer(buffer), 'A Buffer must be provided'); this.buf = buffer; this.offset = 0; } append(buffer) { assert_1.default(Buffer.isBuffer(buffer), 'A Buffer must be provided'); this.buf = Buffer.concat([this.buf, buffer]); return this; } isMore() { return this.offset < this.buf.length; } tell() { return this.offset; } seek(pos) { assert_1.default(pos >= 0 && pos <= this.buf.length, 'Position is Invalid'); this.offset = pos; return this; } move(diff) { assert_1.default(this.offset + diff >= 0 && this.offset + diff <= this.buf.length, 'Difference is Invalid'); this.offset += diff; return this; } nextAll() { const remain = this.buf.length - this.offset; assert_1.default(remain >= 0, 'Buffer is not in normal state: offset > totalLength'); const buf = Buffer.alloc(remain); this.buf.copy(buf, 0, this.offset); this.offset = this.buf.length; return buf; } restAll() { return this.nextAll(); } nextBuffer(length) { assert_1.default(length >= 0, 'Length must be no negative'); assert_1.default(this.offset + length <= this.buf.length, outError); const buf = Buffer.alloc(length); this.buf.copy(buf, 0, this.offset, this.offset + length); this.offset += length; return buf; } nextString(length, encoding) { assert_1.default(length >= 0, 'Length must be no negative'); assert_1.default(this.offset + length <= this.buf.length, outError); this.offset += length; return this.buf.toString(encoding, this.offset - length, this.offset); } nextStringZero(encoding) { // Find null by end of buffer let length; for (length = 0; length + this.offset < this.buf.length && this.buf[this.offset + length] !== 0x00; length++) ; assert_1.default(length <= this.buf.length && this.buf[this.offset + length] === 0x00, outError); this.offset += length + 1; return this.buf.toString(encoding, this.offset - length - 1, this.offset - 1); } nextInt8() { return this.reader('Int8', 1); } nextUInt8() { return this.reader('UInt8', 1); } nextUInt16LE() { return this.reader('UInt16LE', 2); } nextUInt16BE() { return this.reader('UInt16BE', 2); } nextInt16LE() { return this.reader('Int16LE', 2); } nextInt16BE() { return this.reader('Int16BE', 2); } nextUInt32LE() { return this.reader('UInt32LE', 4); } nextUInt32BE() { return this.reader('UInt32BE', 4); } nextInt32LE() { return this.reader('Int32LE', 4); } nextInt32BE() { return this.reader('Int32BE', 4); } nextFloatLE() { return this.reader('FloatLE', 4); } nextFloatBE() { return this.reader('FloatBE', 4); } nextDoubleLE() { return this.reader('DoubleLE', 8); } nextDoubleBE() { return this.reader('DoubleBE', 8); } nextBigInt64LE() { return this.reader('BigInt64LE', 8); } nextBigInt64BE() { return this.reader('BigInt64BE', 8); } nextBigUInt64LE() { return this.reader('BigUInt64LE', 8); } nextBigUInt64BE() { return this.reader('BigUInt64BE', 8); } reader(valueName, size) { return 0; } } BufferReader.prototype['reader'] = function (valueName, size) { valueName = cap(valueName); assert_1.default(this.offset + size <= this.buf.length, outError); const val = this.buf['read' + valueName](this.offset); this.offset += size; return val; }; function cap(str) { return str.charAt(0).toUpperCase() + str.slice(1); } exports.default = BufferReader; //# sourceMappingURL=buffer-reader.js.map