| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _jsbi = _interopRequireDefault(require("jsbi"));
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- const SHIFT_LEFT_32 = (1 << 16) * (1 << 16);
- const SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32;
- const UNKNOWN_PLP_LEN = Buffer.from([0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
- const ZERO_LENGTH_BUFFER = Buffer.alloc(0);
- /**
- A Buffer-like class that tracks position.
- As values are written, the position advances by the size of the written data.
- When writing, automatically allocates new buffers if there's not enough space.
- */
- class WritableTrackingBuffer {
- constructor(initialSize, encoding, doubleSizeGrowth) {
- this.initialSize = initialSize;
- this.encoding = encoding || 'ucs2';
- this.doubleSizeGrowth = doubleSizeGrowth || false;
- this.buffer = Buffer.alloc(this.initialSize, 0);
- this.compositeBuffer = ZERO_LENGTH_BUFFER;
- this.position = 0;
- }
- get data() {
- this.newBuffer(0);
- return this.compositeBuffer;
- }
- copyFrom(buffer) {
- const length = buffer.length;
- this.makeRoomFor(length);
- buffer.copy(this.buffer, this.position);
- this.position += length;
- }
- makeRoomFor(requiredLength) {
- if (this.buffer.length - this.position < requiredLength) {
- if (this.doubleSizeGrowth) {
- let size = Math.max(128, this.buffer.length * 2);
- while (size < requiredLength) {
- size *= 2;
- }
- this.newBuffer(size);
- } else {
- this.newBuffer(requiredLength);
- }
- }
- }
- newBuffer(size) {
- const buffer = this.buffer.slice(0, this.position);
- this.compositeBuffer = Buffer.concat([this.compositeBuffer, buffer]);
- this.buffer = size === 0 ? ZERO_LENGTH_BUFFER : Buffer.alloc(size, 0);
- this.position = 0;
- }
- writeUInt8(value) {
- const length = 1;
- this.makeRoomFor(length);
- this.buffer.writeUInt8(value, this.position);
- this.position += length;
- }
- writeUInt16LE(value) {
- const length = 2;
- this.makeRoomFor(length);
- this.buffer.writeUInt16LE(value, this.position);
- this.position += length;
- }
- writeUShort(value) {
- this.writeUInt16LE(value);
- }
- writeUInt16BE(value) {
- const length = 2;
- this.makeRoomFor(length);
- this.buffer.writeUInt16BE(value, this.position);
- this.position += length;
- }
- writeUInt24LE(value) {
- const length = 3;
- this.makeRoomFor(length);
- this.buffer[this.position + 2] = value >>> 16 & 0xff;
- this.buffer[this.position + 1] = value >>> 8 & 0xff;
- this.buffer[this.position] = value & 0xff;
- this.position += length;
- }
- writeUInt32LE(value) {
- const length = 4;
- this.makeRoomFor(length);
- this.buffer.writeUInt32LE(value, this.position);
- this.position += length;
- }
- writeBigInt64LE(value) {
- this.writeBigU_Int64LE(value);
- }
- writeBigU_Int64LE(value) {
- this.makeRoomFor(8);
- let lo = _jsbi.default.toNumber(_jsbi.default.bitwiseAnd(value, _jsbi.default.BigInt(0xffffffff)));
- this.buffer[this.position++] = lo;
- lo = lo >> 8;
- this.buffer[this.position++] = lo;
- lo = lo >> 8;
- this.buffer[this.position++] = lo;
- lo = lo >> 8;
- this.buffer[this.position++] = lo;
- let hi = _jsbi.default.toNumber(_jsbi.default.bitwiseAnd(_jsbi.default.signedRightShift(value, _jsbi.default.BigInt(32)), _jsbi.default.BigInt(0xffffffff)));
- this.buffer[this.position++] = hi;
- hi = hi >> 8;
- this.buffer[this.position++] = hi;
- hi = hi >> 8;
- this.buffer[this.position++] = hi;
- hi = hi >> 8;
- this.buffer[this.position++] = hi;
- }
- writeInt64LE(value) {
- this.writeBigInt64LE(_jsbi.default.BigInt(value));
- }
- writeUInt32BE(value) {
- const length = 4;
- this.makeRoomFor(length);
- this.buffer.writeUInt32BE(value, this.position);
- this.position += length;
- }
- writeUInt40LE(value) {
- // inspired by https://github.com/dpw/node-buffer-more-ints
- this.writeInt32LE(value & -1);
- this.writeUInt8(Math.floor(value * SHIFT_RIGHT_32));
- }
- writeUInt64LE(value) {
- this.writeBigUInt64LE(_jsbi.default.BigInt(value));
- }
- writeBigUInt64LE(value) {
- this.writeBigU_Int64LE(value);
- }
- writeInt8(value) {
- const length = 1;
- this.makeRoomFor(length);
- this.buffer.writeInt8(value, this.position);
- this.position += length;
- }
- writeInt16LE(value) {
- const length = 2;
- this.makeRoomFor(length);
- this.buffer.writeInt16LE(value, this.position);
- this.position += length;
- }
- writeInt16BE(value) {
- const length = 2;
- this.makeRoomFor(length);
- this.buffer.writeInt16BE(value, this.position);
- this.position += length;
- }
- writeInt32LE(value) {
- const length = 4;
- this.makeRoomFor(length);
- this.buffer.writeInt32LE(value, this.position);
- this.position += length;
- }
- writeInt32BE(value) {
- const length = 4;
- this.makeRoomFor(length);
- this.buffer.writeInt32BE(value, this.position);
- this.position += length;
- }
- writeFloatLE(value) {
- const length = 4;
- this.makeRoomFor(length);
- this.buffer.writeFloatLE(value, this.position);
- this.position += length;
- }
- writeDoubleLE(value) {
- const length = 8;
- this.makeRoomFor(length);
- this.buffer.writeDoubleLE(value, this.position);
- this.position += length;
- }
- writeString(value, encoding) {
- if (encoding == null) {
- encoding = this.encoding;
- }
- const length = Buffer.byteLength(value, encoding);
- this.makeRoomFor(length); // $FlowFixMe https://github.com/facebook/flow/pull/5398
- this.buffer.write(value, this.position, encoding);
- this.position += length;
- }
- writeBVarchar(value, encoding) {
- this.writeUInt8(value.length);
- this.writeString(value, encoding);
- }
- writeUsVarchar(value, encoding) {
- this.writeUInt16LE(value.length);
- this.writeString(value, encoding);
- } // TODO: Figure out what types are passed in other than `Buffer`
- writeUsVarbyte(value, encoding) {
- if (encoding == null) {
- encoding = this.encoding;
- }
- let length;
- if (value instanceof Buffer) {
- length = value.length;
- } else {
- value = value.toString();
- length = Buffer.byteLength(value, encoding);
- }
- this.writeUInt16LE(length);
- if (value instanceof Buffer) {
- this.writeBuffer(value);
- } else {
- this.makeRoomFor(length); // $FlowFixMe https://github.com/facebook/flow/pull/5398
- this.buffer.write(value, this.position, encoding);
- this.position += length;
- }
- }
- writePLPBody(value, encoding) {
- if (encoding == null) {
- encoding = this.encoding;
- }
- let length;
- if (value instanceof Buffer) {
- length = value.length;
- } else {
- value = value.toString();
- length = Buffer.byteLength(value, encoding);
- } // Length of all chunks.
- // this.writeUInt64LE(length);
- // unknown seems to work better here - might revisit later.
- this.writeBuffer(UNKNOWN_PLP_LEN); // In the UNKNOWN_PLP_LEN case, the data is represented as a series of zero or more chunks.
- if (length > 0) {
- // One chunk.
- this.writeUInt32LE(length);
- if (value instanceof Buffer) {
- this.writeBuffer(value);
- } else {
- this.makeRoomFor(length);
- this.buffer.write(value, this.position, encoding);
- this.position += length;
- }
- } // PLP_TERMINATOR (no more chunks).
- this.writeUInt32LE(0);
- }
- writeBuffer(value) {
- const length = value.length;
- this.makeRoomFor(length);
- value.copy(this.buffer, this.position);
- this.position += length;
- }
- writeMoney(value) {
- this.writeInt32LE(Math.floor(value * SHIFT_RIGHT_32));
- this.writeInt32LE(value & -1);
- }
- }
- var _default = WritableTrackingBuffer;
- exports.default = _default;
- module.exports = WritableTrackingBuffer;
|