@creditkarma/thrift-server-core
Version:
Thrift core library in TypeScript
125 lines • 3.81 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BufferedTransport = void 0;
const binary = require("../binary");
const errors_1 = require("../errors");
const TTransport_1 = require("./TTransport");
const DEFAULT_READ_BUFFER_SIDE = 1024;
class BufferedTransport extends TTransport_1.TTransport {
constructor(buffer) {
super(buffer || Buffer.alloc(DEFAULT_READ_BUFFER_SIDE));
this.readCursor = 0;
this.writeCursor = buffer !== undefined ? buffer.length : 0;
this.outBuffers = [];
this.outCount = 0;
}
static receiver(data) {
const reader = new BufferedTransport(Buffer.alloc(data.length));
data.copy(reader.buffer, 0, 0);
return reader;
}
remaining() {
const remainingSize = this.writeCursor - this.readCursor;
const remainingBuffer = Buffer.alloc(remainingSize);
if (remainingSize > 0) {
this.buffer.copy(remainingBuffer, 0, this.readCursor, this.writeCursor);
}
return remainingBuffer;
}
commitPosition() {
const unreadSize = this.writeCursor - this.readCursor;
const bufSize = unreadSize * 2 > DEFAULT_READ_BUFFER_SIDE
? unreadSize * 2
: DEFAULT_READ_BUFFER_SIDE;
const buf = Buffer.alloc(bufSize);
if (unreadSize > 0) {
this.buffer.copy(buf, 0, this.readCursor, this.writeCursor);
}
this.readCursor = 0;
this.writeCursor = unreadSize;
this.buffer = buf;
}
rollbackPosition() {
this.readCursor = 0;
}
isOpen() {
return true;
}
open() {
return true;
}
close() {
return true;
}
read(len) {
this.ensureAvailable(len);
const buf = Buffer.alloc(len);
this.buffer.copy(buf, 0, this.readCursor, this.readCursor + len);
this.readCursor += len;
return buf;
}
readByte() {
this.ensureAvailable(1);
return binary.readByte(this.buffer[this.readCursor++]);
}
readI16() {
this.ensureAvailable(2);
const i16 = binary.readI16(this.buffer, this.readCursor);
this.readCursor += 2;
return i16;
}
readI32() {
this.ensureAvailable(4);
const i32 = binary.readI32(this.buffer, this.readCursor);
this.readCursor += 4;
return i32;
}
readDouble() {
this.ensureAvailable(8);
const d = binary.readDouble(this.buffer, this.readCursor);
this.readCursor += 8;
return d;
}
readString(len) {
this.ensureAvailable(len);
const str = this.buffer.toString('utf8', this.readCursor, this.readCursor + len);
this.readCursor += len;
return str;
}
readAll() {
return this.readString(this.buffer.length - this.readCursor);
}
consume(len) {
this.readCursor += len;
}
write(buf) {
if (buf instanceof Buffer) {
this.outBuffers.push(buf);
this.outCount += buf.length;
}
else {
throw new TypeError(`Expected buffer but found type ${typeof buf}`);
}
}
flush() {
if (this.outCount < 1) {
return Buffer.alloc(0);
}
const msg = Buffer.alloc(this.outCount);
let pos = 0;
this.outBuffers.forEach((buf) => {
buf.copy(msg, pos, 0);
pos += buf.length;
});
this.outBuffers = [];
this.outCount = 0;
return msg;
}
ensureAvailable(len) {
if (this.readCursor + len > this.buffer.length) {
throw new errors_1.InputBufferUnderrunError();
}
}
}
exports.BufferedTransport = BufferedTransport;
//# sourceMappingURL=BufferedTransport.js.map