biner
Version:
Declarative binary data encoder / decoder.
103 lines • 2.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const errors_1 = require("./errors");
class Transaction {
constructor(stream) {
this.stream = stream;
this.index = 0;
}
append(buf) {
this.stream.append(buf);
}
commit() {
this.stream.consume(this.index);
}
get(i = 0) {
return this.stream.get(this.index + i);
}
get length() {
return this.stream.length;
}
slice(start, end) {
return this.stream.slice(start, end);
}
toString(encoding, start, end) {
return this.stream.toString(encoding, start, end);
}
readBuffer(size) {
assertSize(this.index + size, this.length);
const buf = this.stream.slice(this.index, this.index + size);
this.index += size;
return buf;
}
indexOf(byte, offset = 0) {
return this.stream.indexOf(byte, this.index + offset) - this.index;
}
doRead(method, size) {
assertSize(this.index + size, this.length);
const value = this.stream.buffer[method](this.index, size);
this.index += size;
return value;
}
readDoubleBE() {
return this.doRead('readDoubleBE', 8);
}
readDoubleLE() {
return this.doRead('readDoubleLE', 8);
}
readFloatBE() {
return this.doRead('readFloatBE', 4);
}
readFloatLE() {
return this.doRead('readFloatLE', 4);
}
readInt16BE() {
return this.doRead('readInt16BE', 2);
}
readInt16LE() {
return this.doRead('readInt16LE', 2);
}
readInt32BE() {
return this.doRead('readInt32BE', 4);
}
readInt32LE() {
return this.doRead('readInt32LE', 4);
}
readInt8() {
return this.doRead('readInt8', 1);
}
readIntBE(byteLength) {
return this.doRead('readIntBE', byteLength);
}
readIntLE(byteLength) {
return this.doRead('readIntLE', byteLength);
}
readUInt16BE() {
return this.doRead('readUInt16BE', 2);
}
readUInt16LE() {
return this.doRead('readUInt16LE', 2);
}
readUInt32BE() {
return this.doRead('readUInt32BE', 4);
}
readUInt32LE() {
return this.doRead('readUInt32LE', 4);
}
readUInt8() {
return this.doRead('readUInt8', 1);
}
readUIntBE(byteLength) {
return this.doRead('readUIntBE', byteLength);
}
readUIntLE(byteLength) {
return this.doRead('readUIntLE', byteLength);
}
}
exports.Transaction = Transaction;
function assertSize(size, length) {
if (size > length) {
throw new errors_1.NotEnoughDataError(size, length);
}
}
//# sourceMappingURL=transaction.js.map