Backend half
This commit is contained in:
+142
@@ -0,0 +1,142 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (c) 2015-2024 MariaDB Corporation Ab
|
||||
|
||||
'use strict';
|
||||
|
||||
const ZLib = require('zlib');
|
||||
const Utils = require('../misc/utils');
|
||||
|
||||
/**
|
||||
* MySQL packet parser
|
||||
* see : https://mariadb.com/kb/en/library/0-packet/
|
||||
*/
|
||||
class CompressionInputStream {
|
||||
constructor(reader, receiveQueue, opts, info) {
|
||||
this.reader = reader;
|
||||
this.receiveQueue = receiveQueue;
|
||||
this.info = info;
|
||||
this.opts = opts;
|
||||
this.header = Buffer.allocUnsafe(7);
|
||||
this.headerLen = 0;
|
||||
this.compressPacketLen = null;
|
||||
this.packetLen = null;
|
||||
this.remainingLen = null;
|
||||
|
||||
this.parts = null;
|
||||
this.partsTotalLen = 0;
|
||||
}
|
||||
|
||||
receivePacket(chunk) {
|
||||
let cmd = this.currentCmd();
|
||||
if (this.opts.debugCompress) {
|
||||
this.opts.logger.network(
|
||||
`<== conn:${this.info.threadId ? this.info.threadId : -1} ${
|
||||
cmd
|
||||
? cmd.onPacketReceive
|
||||
? cmd.constructor.name + '.' + cmd.onPacketReceive.name
|
||||
: cmd.constructor.name
|
||||
: 'no command'
|
||||
} (compress)\n${Utils.log(this.opts, chunk, 0, chunk.length, this.header)}`
|
||||
);
|
||||
}
|
||||
if (cmd) cmd.compressSequenceNo = this.header[3];
|
||||
const unCompressLen = this.header[4] | (this.header[5] << 8) | (this.header[6] << 16);
|
||||
if (unCompressLen === 0) {
|
||||
this.reader.onData(chunk);
|
||||
} else {
|
||||
//use synchronous inflating, to ensure FIFO packet order
|
||||
const unCompressChunk = ZLib.inflateSync(chunk);
|
||||
this.reader.onData(unCompressChunk);
|
||||
}
|
||||
}
|
||||
|
||||
currentCmd() {
|
||||
let cmd;
|
||||
while ((cmd = this.receiveQueue.peek())) {
|
||||
if (cmd.onPacketReceive) return cmd;
|
||||
this.receiveQueue.shift();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
resetHeader() {
|
||||
this.remainingLen = null;
|
||||
this.headerLen = 0;
|
||||
}
|
||||
|
||||
onData(chunk) {
|
||||
let pos = 0;
|
||||
let length;
|
||||
const chunkLen = chunk.length;
|
||||
|
||||
do {
|
||||
if (this.remainingLen) {
|
||||
length = this.remainingLen;
|
||||
} else if (this.headerLen === 0 && chunkLen - pos >= 7) {
|
||||
this.header[0] = chunk[pos];
|
||||
this.header[1] = chunk[pos + 1];
|
||||
this.header[2] = chunk[pos + 2];
|
||||
this.header[3] = chunk[pos + 3];
|
||||
this.header[4] = chunk[pos + 4];
|
||||
this.header[5] = chunk[pos + 5];
|
||||
this.header[6] = chunk[pos + 6];
|
||||
this.headerLen = 7;
|
||||
pos += 7;
|
||||
this.compressPacketLen = this.header[0] + (this.header[1] << 8) + (this.header[2] << 16);
|
||||
this.packetLen = this.header[4] | (this.header[5] << 8) | (this.header[6] << 16);
|
||||
if (this.packetLen === 0) this.packetLen = this.compressPacketLen;
|
||||
length = this.compressPacketLen;
|
||||
} else {
|
||||
length = null;
|
||||
while (chunkLen - pos > 0) {
|
||||
this.header[this.headerLen++] = chunk[pos++];
|
||||
if (this.headerLen === 7) {
|
||||
this.compressPacketLen = this.header[0] + (this.header[1] << 8) + (this.header[2] << 16);
|
||||
this.packetLen = this.header[4] | (this.header[5] << 8) | (this.header[6] << 16);
|
||||
if (this.packetLen === 0) this.packetLen = this.compressPacketLen;
|
||||
length = this.compressPacketLen;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (length) {
|
||||
if (chunkLen - pos >= length) {
|
||||
const buf = chunk.subarray(pos, pos + length);
|
||||
pos += length;
|
||||
if (this.parts) {
|
||||
this.parts.push(buf);
|
||||
this.partsTotalLen += length;
|
||||
|
||||
if (this.compressPacketLen < 0xffffff) {
|
||||
let buf = Buffer.concat(this.parts, this.partsTotalLen);
|
||||
this.parts = null;
|
||||
this.receivePacket(buf);
|
||||
}
|
||||
} else {
|
||||
if (this.compressPacketLen < 0xffffff) {
|
||||
this.receivePacket(buf);
|
||||
} else {
|
||||
this.parts = [buf];
|
||||
this.partsTotalLen = length;
|
||||
}
|
||||
}
|
||||
this.resetHeader();
|
||||
} else {
|
||||
const buf = chunk.subarray(pos, chunkLen);
|
||||
if (!this.parts) {
|
||||
this.parts = [buf];
|
||||
this.partsTotalLen = chunkLen - pos;
|
||||
} else {
|
||||
this.parts.push(buf);
|
||||
this.partsTotalLen += chunkLen - pos;
|
||||
}
|
||||
this.remainingLen = length - (chunkLen - pos);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} while (pos < chunkLen);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = CompressionInputStream;
|
||||
+174
@@ -0,0 +1,174 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (c) 2015-2024 MariaDB Corporation Ab
|
||||
|
||||
'use strict';
|
||||
|
||||
const Utils = require('../misc/utils');
|
||||
const ZLib = require('zlib');
|
||||
|
||||
//increase by level to avoid buffer copy.
|
||||
const SMALL_BUFFER_SIZE = 2048;
|
||||
const MEDIUM_BUFFER_SIZE = 131072; //128k
|
||||
const LARGE_BUFFER_SIZE = 1048576; //1M
|
||||
const MAX_BUFFER_SIZE = 16777222; //16M + 7
|
||||
|
||||
/**
|
||||
/**
|
||||
* MySQL compression filter.
|
||||
* see https://mariadb.com/kb/en/library/0-packet/#compressed-packet
|
||||
*/
|
||||
class CompressionOutputStream {
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param socket current socket
|
||||
* @param opts current connection options
|
||||
* @param info current connection information
|
||||
* @constructor
|
||||
*/
|
||||
constructor(socket, opts, info) {
|
||||
this.info = info;
|
||||
this.opts = opts;
|
||||
this.pos = 7;
|
||||
this.header = Buffer.allocUnsafe(7);
|
||||
this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
this.writer = (buffer) => {
|
||||
socket.write(buffer);
|
||||
};
|
||||
}
|
||||
|
||||
growBuffer(len) {
|
||||
let newCapacity;
|
||||
if (len + this.pos < MEDIUM_BUFFER_SIZE) {
|
||||
newCapacity = MEDIUM_BUFFER_SIZE;
|
||||
} else if (len + this.pos < LARGE_BUFFER_SIZE) {
|
||||
newCapacity = LARGE_BUFFER_SIZE;
|
||||
} else newCapacity = MAX_BUFFER_SIZE;
|
||||
|
||||
let newBuf = Buffer.allocUnsafe(newCapacity);
|
||||
this.buf.copy(newBuf, 0, 0, this.pos);
|
||||
this.buf = newBuf;
|
||||
}
|
||||
|
||||
writeBuf(arr, cmd) {
|
||||
let off = 0,
|
||||
len = arr.length;
|
||||
if (arr instanceof Uint8Array) {
|
||||
arr = Buffer.from(arr);
|
||||
}
|
||||
if (len > this.buf.length - this.pos) {
|
||||
if (this.buf.length !== MAX_BUFFER_SIZE) {
|
||||
this.growBuffer(len);
|
||||
}
|
||||
|
||||
//max buffer size
|
||||
if (len > this.buf.length - this.pos) {
|
||||
//not enough space in buffer, will stream :
|
||||
// fill buffer and flush until all data are snd
|
||||
let remainingLen = len;
|
||||
|
||||
while (true) {
|
||||
//filling buffer
|
||||
let lenToFillBuffer = Math.min(MAX_BUFFER_SIZE - this.pos, remainingLen);
|
||||
arr.copy(this.buf, this.pos, off, off + lenToFillBuffer);
|
||||
remainingLen -= lenToFillBuffer;
|
||||
off += lenToFillBuffer;
|
||||
this.pos += lenToFillBuffer;
|
||||
|
||||
if (remainingLen === 0) return;
|
||||
this.flush(false, cmd, remainingLen);
|
||||
}
|
||||
}
|
||||
}
|
||||
arr.copy(this.buf, this.pos, off, off + len);
|
||||
this.pos += len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush the internal buffer.
|
||||
*/
|
||||
flush(cmdEnd, cmd, remainingLen) {
|
||||
if (this.pos < 1536) {
|
||||
//*******************************************************************************
|
||||
// small packet, no compression
|
||||
//*******************************************************************************
|
||||
|
||||
this.buf[0] = this.pos - 7;
|
||||
this.buf[1] = (this.pos - 7) >>> 8;
|
||||
this.buf[2] = (this.pos - 7) >>> 16;
|
||||
this.buf[3] = ++cmd.compressSequenceNo;
|
||||
this.buf[4] = 0;
|
||||
this.buf[5] = 0;
|
||||
this.buf[6] = 0;
|
||||
|
||||
if (this.opts.debugCompress) {
|
||||
this.opts.logger.network(
|
||||
`==> conn:${this.info.threadId ? this.info.threadId : -1} ${
|
||||
cmd ? cmd.constructor.name + '(0,' + this.pos + ')' : 'unknown'
|
||||
} (compress)\n${Utils.log(this.opts, this.buf, 0, this.pos)}`
|
||||
);
|
||||
}
|
||||
|
||||
this.writer(this.buf.subarray(0, this.pos));
|
||||
} else {
|
||||
//*******************************************************************************
|
||||
// compressing packet
|
||||
//*******************************************************************************
|
||||
//use synchronous inflating, to ensure FIFO packet order
|
||||
const compressChunk = ZLib.deflateSync(this.buf.subarray(7, this.pos));
|
||||
const compressChunkLen = compressChunk.length;
|
||||
|
||||
this.header[0] = compressChunkLen;
|
||||
this.header[1] = compressChunkLen >>> 8;
|
||||
this.header[2] = compressChunkLen >>> 16;
|
||||
this.header[3] = ++cmd.compressSequenceNo;
|
||||
this.header[4] = this.pos - 7;
|
||||
this.header[5] = (this.pos - 7) >>> 8;
|
||||
this.header[6] = (this.pos - 7) >>> 16;
|
||||
|
||||
if (this.opts.debugCompress) {
|
||||
this.opts.logger.network(
|
||||
`==> conn:${this.info.threadId ? this.info.threadId : -1} ${
|
||||
cmd ? cmd.constructor.name + '(0,' + this.pos + '=>' + compressChunkLen + ')' : 'unknown'
|
||||
} (compress)\n${Utils.log(this.opts, compressChunk, 0, compressChunkLen, this.header)}`
|
||||
);
|
||||
}
|
||||
|
||||
this.writer(this.header);
|
||||
this.writer(compressChunk);
|
||||
if (cmdEnd && compressChunkLen === MAX_BUFFER_SIZE) this.writeEmptyPacket(cmd);
|
||||
this.header = Buffer.allocUnsafe(7);
|
||||
}
|
||||
this.buf = remainingLen
|
||||
? CompressionOutputStream.allocateBuffer(remainingLen)
|
||||
: Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
this.pos = 7;
|
||||
}
|
||||
|
||||
static allocateBuffer(len) {
|
||||
if (len + 4 < SMALL_BUFFER_SIZE) {
|
||||
return Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
} else if (len + 4 < MEDIUM_BUFFER_SIZE) {
|
||||
return Buffer.allocUnsafe(MEDIUM_BUFFER_SIZE);
|
||||
} else if (len + 4 < LARGE_BUFFER_SIZE) {
|
||||
return Buffer.allocUnsafe(LARGE_BUFFER_SIZE);
|
||||
}
|
||||
return Buffer.allocUnsafe(MAX_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
writeEmptyPacket(cmd) {
|
||||
const emptyBuf = Buffer.from([0x00, 0x00, 0x00, cmd.compressSequenceNo, 0x00, 0x00, 0x00]);
|
||||
|
||||
if (this.opts.debugCompress) {
|
||||
this.opts.logger.network(
|
||||
`==> conn:${this.info.threadId ? this.info.threadId : -1} ${
|
||||
cmd ? cmd.constructor.name + '(0,' + this.pos + ')' : 'unknown'
|
||||
} (compress)\n${Utils.log(this.opts, emptyBuf, 0, 7)}`
|
||||
);
|
||||
}
|
||||
|
||||
this.writer(emptyBuf);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = CompressionOutputStream;
|
||||
+204
@@ -0,0 +1,204 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (c) 2015-2025 MariaDB Corporation Ab
|
||||
|
||||
'use strict';
|
||||
|
||||
const PacketNodeEncoded = require('./packet-node-encoded');
|
||||
const PacketIconvEncoded = require('./packet-node-iconv');
|
||||
const Collations = require('../const/collations');
|
||||
const Utils = require('../misc/utils');
|
||||
|
||||
/**
|
||||
* MySQL packet parser
|
||||
* see : https://mariadb.com/kb/en/library/0-packet/
|
||||
*/
|
||||
class PacketInputStream {
|
||||
constructor(unexpectedPacket, receiveQueue, out, opts, info) {
|
||||
this.unexpectedPacket = unexpectedPacket;
|
||||
this.opts = opts;
|
||||
this.receiveQueue = receiveQueue;
|
||||
this.info = info;
|
||||
this.out = out;
|
||||
|
||||
//in case packet is not complete
|
||||
this.header = Buffer.allocUnsafe(4);
|
||||
this.headerLen = 0;
|
||||
this.packetLen = null;
|
||||
this.remainingLen = null;
|
||||
|
||||
this.parts = null;
|
||||
this.partsTotalLen = 0;
|
||||
this.changeEncoding(this.opts.collation ? this.opts.collation : Collations.fromIndex(224));
|
||||
this.changeDebug(this.opts.debug);
|
||||
this.opts.on('collation', this.changeEncoding.bind(this));
|
||||
this.opts.on('debug', this.changeDebug.bind(this));
|
||||
}
|
||||
|
||||
changeEncoding(collation) {
|
||||
this.encoding = collation.charset;
|
||||
this.packet = Buffer.isEncoding(this.encoding)
|
||||
? new PacketNodeEncoded(this.encoding)
|
||||
: new PacketIconvEncoded(this.encoding);
|
||||
}
|
||||
|
||||
changeDebug(debug) {
|
||||
this.receivePacket = debug ? this.receivePacketDebug : this.receivePacketBasic;
|
||||
}
|
||||
|
||||
receivePacketDebug(packet) {
|
||||
let cmd = this.currentCmd();
|
||||
this.header[0] = this.packetLen;
|
||||
this.header[1] = this.packetLen >> 8;
|
||||
this.header[2] = this.packetLen >> 16;
|
||||
this.header[3] = this.sequenceNo;
|
||||
if (packet) {
|
||||
this.opts.logger.network(
|
||||
`<== conn:${this.info.threadId ? this.info.threadId : -1} ${
|
||||
cmd
|
||||
? cmd.onPacketReceive
|
||||
? cmd.constructor.name + '.' + cmd.onPacketReceive.name
|
||||
: cmd.constructor.name
|
||||
: 'no command'
|
||||
} (${packet.pos},${packet.end})\n${Utils.log(this.opts, packet.buf, packet.pos, packet.end, this.header)}`
|
||||
);
|
||||
}
|
||||
|
||||
if (!cmd) {
|
||||
this.unexpectedPacket(packet);
|
||||
return;
|
||||
}
|
||||
|
||||
cmd.sequenceNo = this.sequenceNo;
|
||||
cmd.onPacketReceive(packet, this.out, this.opts, this.info);
|
||||
if (!cmd.onPacketReceive) {
|
||||
this.receiveQueue.shift();
|
||||
}
|
||||
}
|
||||
|
||||
receivePacketBasic(packet) {
|
||||
let cmd = this.currentCmd();
|
||||
if (!cmd) {
|
||||
this.unexpectedPacket(packet);
|
||||
return;
|
||||
}
|
||||
cmd.sequenceNo = this.sequenceNo;
|
||||
cmd.onPacketReceive(packet, this.out, this.opts, this.info);
|
||||
if (!cmd.onPacketReceive) this.receiveQueue.shift();
|
||||
}
|
||||
|
||||
resetHeader() {
|
||||
this.remainingLen = null;
|
||||
this.headerLen = 0;
|
||||
}
|
||||
|
||||
currentCmd() {
|
||||
let cmd;
|
||||
while ((cmd = this.receiveQueue.peek())) {
|
||||
if (cmd.onPacketReceive) return cmd;
|
||||
this.receiveQueue.shift();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
onData(chunk) {
|
||||
let pos = 0;
|
||||
let length;
|
||||
const chunkLen = chunk.length;
|
||||
|
||||
do {
|
||||
//read header
|
||||
if (this.remainingLen) {
|
||||
length = this.remainingLen;
|
||||
} else if (this.headerLen === 0 && chunkLen - pos >= 4) {
|
||||
this.packetLen = chunk[pos] + (chunk[pos + 1] << 8) + (chunk[pos + 2] << 16);
|
||||
this.sequenceNo = chunk[pos + 3];
|
||||
pos += 4;
|
||||
length = this.packetLen;
|
||||
} else {
|
||||
length = null;
|
||||
while (chunkLen - pos > 0) {
|
||||
this.header[this.headerLen++] = chunk[pos++];
|
||||
if (this.headerLen === 4) {
|
||||
this.packetLen = this.header[0] + (this.header[1] << 8) + (this.header[2] << 16);
|
||||
this.sequenceNo = this.header[3];
|
||||
length = this.packetLen;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (length) {
|
||||
if (chunkLen - pos >= length) {
|
||||
pos += length;
|
||||
if (!this.parts) {
|
||||
if (this.packetLen < 0xffffff) {
|
||||
this.receivePacket(this.packet.update(chunk, pos - length, pos));
|
||||
// fast path, knowing there is no parts
|
||||
// loop can be simplified until reaching the end of the packet.
|
||||
while (pos + 4 < chunkLen) {
|
||||
this.packetLen = chunk[pos] + (chunk[pos + 1] << 8) + (chunk[pos + 2] << 16);
|
||||
this.sequenceNo = chunk[pos + 3];
|
||||
pos += 4;
|
||||
if (chunkLen - pos >= this.packetLen) {
|
||||
pos += this.packetLen;
|
||||
if (this.packetLen < 0xffffff) {
|
||||
this.receivePacket(this.packet.update(chunk, pos - this.packetLen, pos));
|
||||
} else {
|
||||
this.parts = [chunk.subarray(pos - this.packetLen, pos)];
|
||||
this.partsTotalLen = this.packetLen;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
const buf = chunk.subarray(pos, chunkLen);
|
||||
if (!this.parts) {
|
||||
this.parts = [buf];
|
||||
this.partsTotalLen = chunkLen - pos;
|
||||
} else {
|
||||
this.parts.push(buf);
|
||||
this.partsTotalLen += chunkLen - pos;
|
||||
}
|
||||
this.remainingLen = this.packetLen - (chunkLen - pos);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.parts = [chunk.subarray(pos - length, pos)];
|
||||
this.partsTotalLen = length;
|
||||
}
|
||||
} else {
|
||||
this.parts.push(chunk.subarray(pos - length, pos));
|
||||
this.partsTotalLen += length;
|
||||
|
||||
if (this.packetLen < 0xffffff) {
|
||||
let buf = Buffer.concat(this.parts, this.partsTotalLen);
|
||||
this.parts = null;
|
||||
this.receivePacket(this.packet.update(buf, 0, this.partsTotalLen));
|
||||
}
|
||||
}
|
||||
this.resetHeader();
|
||||
} else {
|
||||
const buf = chunk.subarray(pos, chunkLen);
|
||||
if (!this.parts) {
|
||||
this.parts = [buf];
|
||||
this.partsTotalLen = chunkLen - pos;
|
||||
} else {
|
||||
this.parts.push(buf);
|
||||
this.partsTotalLen += chunkLen - pos;
|
||||
}
|
||||
this.remainingLen = length - (chunkLen - pos);
|
||||
return;
|
||||
}
|
||||
} else if (length === 0 && this.parts) {
|
||||
// ending empty packet
|
||||
this.parts.push(chunk.subarray(pos - length, pos));
|
||||
this.partsTotalLen += length;
|
||||
let buf = Buffer.concat(this.parts, this.partsTotalLen);
|
||||
this.parts = null;
|
||||
this.receivePacket(this.packet.update(buf, 0, this.partsTotalLen));
|
||||
this.resetHeader();
|
||||
}
|
||||
} while (pos < chunkLen);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PacketInputStream;
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (c) 2015-2024 MariaDB Corporation Ab
|
||||
|
||||
'use strict';
|
||||
|
||||
const Packet = require('./packet');
|
||||
|
||||
class PacketNodeEncoded extends Packet {
|
||||
constructor(encoding) {
|
||||
super();
|
||||
// using undefined for utf8 permit to avoid node.js searching
|
||||
// for charset, using directly utf8 default one.
|
||||
this.encoding = encoding === 'utf8' ? undefined : encoding;
|
||||
}
|
||||
|
||||
readStringLengthEncoded() {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) return null;
|
||||
|
||||
this.pos += len;
|
||||
return this.buf.toString(this.encoding, this.pos - len, this.pos);
|
||||
}
|
||||
|
||||
static readString(encoding, buf, beg, len) {
|
||||
return buf.toString(encoding, beg, beg + len);
|
||||
}
|
||||
|
||||
subPacketLengthEncoded(len) {
|
||||
this.skip(len);
|
||||
return new PacketNodeEncoded(this.encoding).update(this.buf, this.pos - len, this.pos);
|
||||
}
|
||||
|
||||
readStringRemaining() {
|
||||
const str = this.buf.toString(this.encoding, this.pos, this.end);
|
||||
this.pos = this.end;
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PacketNodeEncoded;
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (c) 2015-2024 MariaDB Corporation Ab
|
||||
|
||||
'use strict';
|
||||
|
||||
const Packet = require('./packet');
|
||||
const Iconv = require('iconv-lite');
|
||||
|
||||
class PacketIconvEncoded extends Packet {
|
||||
constructor(encoding) {
|
||||
super();
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
readStringLengthEncoded() {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) return null;
|
||||
|
||||
this.pos += len;
|
||||
return Iconv.decode(this.buf.subarray(this.pos - len, this.pos), this.encoding);
|
||||
}
|
||||
|
||||
static readString(encoding, buf, beg, len) {
|
||||
return Iconv.decode(buf.subarray(beg, beg + len), encoding);
|
||||
}
|
||||
|
||||
subPacketLengthEncoded(len) {
|
||||
this.skip(len);
|
||||
return new PacketIconvEncoded(this.encoding).update(this.buf, this.pos - len, this.pos);
|
||||
}
|
||||
|
||||
readStringRemaining() {
|
||||
const str = Iconv.decode(this.buf.subarray(this.pos, this.end), this.encoding);
|
||||
this.pos = this.end;
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PacketIconvEncoded;
|
||||
+770
@@ -0,0 +1,770 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (c) 2015-2025 MariaDB Corporation Ab
|
||||
|
||||
'use strict';
|
||||
|
||||
const Iconv = require('iconv-lite');
|
||||
const Utils = require('../misc/utils');
|
||||
const Errors = require('../misc/errors');
|
||||
const Collations = require('../const/collations');
|
||||
|
||||
const QUOTE = 0x27;
|
||||
const DBL_QUOTE = 0x22;
|
||||
const ZERO_BYTE = 0x00;
|
||||
const SLASH = 0x5c;
|
||||
|
||||
//increase by level to avoid buffer copy.
|
||||
const SMALL_BUFFER_SIZE = 256;
|
||||
const MEDIUM_BUFFER_SIZE = 16384; //16k
|
||||
const LARGE_BUFFER_SIZE = 131072; //128k
|
||||
const BIG_BUFFER_SIZE = 1048576; //1M
|
||||
const MAX_BUFFER_SIZE = 16777219; //16M + 4
|
||||
const CHARS_GLOBAL_REGEXP = /[\000\032"'\\\n\r\t]/g;
|
||||
|
||||
/**
|
||||
* MySQL packet builder.
|
||||
*
|
||||
* @param opts options
|
||||
* @param info connection info
|
||||
* @constructor
|
||||
*/
|
||||
class PacketOutputStream {
|
||||
constructor(opts, info) {
|
||||
this.opts = opts;
|
||||
this.info = info;
|
||||
this.pos = 4;
|
||||
this.markPos = -1;
|
||||
this.bufContainDataAfterMark = false;
|
||||
this.cmdLength = 0;
|
||||
this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
this.maxAllowedPacket = opts.maxAllowedPacket || 16777216;
|
||||
this.maxPacketLength = Math.min(MAX_BUFFER_SIZE, this.maxAllowedPacket + 4);
|
||||
|
||||
this.changeEncoding(this.opts.collation ? this.opts.collation : Collations.fromIndex(224));
|
||||
this.changeDebug(this.opts.debug);
|
||||
|
||||
this.opts.on('collation', this.changeEncoding.bind(this));
|
||||
this.opts.on('debug', this.changeDebug.bind(this));
|
||||
}
|
||||
|
||||
changeEncoding(collation) {
|
||||
this.encoding = collation.charset;
|
||||
if (this.encoding === 'utf8') {
|
||||
this.writeString = this.writeDefaultBufferString;
|
||||
this.encodeString = this.encodeNodeString;
|
||||
this.writeLengthEncodedString = this.writeDefaultBufferLengthEncodedString;
|
||||
this.writeStringEscapeQuote = this.writeUtf8StringEscapeQuote;
|
||||
} else if (Buffer.isEncoding(this.encoding)) {
|
||||
this.writeString = this.writeDefaultBufferString;
|
||||
this.encodeString = this.encodeNodeString;
|
||||
this.writeLengthEncodedString = this.writeDefaultBufferLengthEncodedString;
|
||||
this.writeStringEscapeQuote = this.writeDefaultStringEscapeQuote;
|
||||
} else {
|
||||
this.writeString = this.writeDefaultIconvString;
|
||||
this.encodeString = this.encodeIconvString;
|
||||
this.writeLengthEncodedString = this.writeDefaultIconvLengthEncodedString;
|
||||
this.writeStringEscapeQuote = this.writeDefaultStringEscapeQuote;
|
||||
}
|
||||
}
|
||||
|
||||
changeDebug(debug) {
|
||||
this.debug = debug;
|
||||
this.flushBuffer = debug ? this.flushBufferDebug : this.flushBufferBasic;
|
||||
this.fastFlush = debug ? this.fastFlushDebug : this.fastFlushBasic;
|
||||
}
|
||||
|
||||
setStream(stream) {
|
||||
this.stream = stream;
|
||||
}
|
||||
|
||||
growBuffer(len) {
|
||||
let newCapacity;
|
||||
if (len + this.pos < MEDIUM_BUFFER_SIZE) {
|
||||
newCapacity = MEDIUM_BUFFER_SIZE;
|
||||
} else if (len + this.pos < LARGE_BUFFER_SIZE) {
|
||||
newCapacity = LARGE_BUFFER_SIZE;
|
||||
} else if (len + this.pos < BIG_BUFFER_SIZE) {
|
||||
newCapacity = BIG_BUFFER_SIZE;
|
||||
} else if (this.bufContainDataAfterMark) {
|
||||
// special case, for bulk, when a bunch of parameter doesn't fit in 16Mb packet
|
||||
// this save bunch of encoded parameter, sending parameter until mark, then resending data after mark
|
||||
newCapacity = len + this.pos;
|
||||
} else {
|
||||
newCapacity = MAX_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
if (len + this.pos > newCapacity) {
|
||||
if (this.markPos !== -1) {
|
||||
// buf is > 16M with mark.
|
||||
// flush until mark, reset pos at beginning
|
||||
this.flushBufferStopAtMark();
|
||||
|
||||
if (len + this.pos <= this.buf.length) {
|
||||
return;
|
||||
}
|
||||
return this.growBuffer(len);
|
||||
}
|
||||
}
|
||||
|
||||
let newBuf = Buffer.allocUnsafe(newCapacity);
|
||||
this.buf.copy(newBuf, 0, 0, this.pos);
|
||||
this.buf = newBuf;
|
||||
}
|
||||
|
||||
mark() {
|
||||
this.markPos = this.pos;
|
||||
}
|
||||
|
||||
isMarked() {
|
||||
return this.markPos !== -1;
|
||||
}
|
||||
|
||||
hasFlushed() {
|
||||
return this.cmd.sequenceNo !== -1;
|
||||
}
|
||||
|
||||
hasDataAfterMark() {
|
||||
return this.bufContainDataAfterMark;
|
||||
}
|
||||
|
||||
bufIsAfterMaxPacketLength() {
|
||||
return this.pos > this.maxPacketLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset mark flag and send bytes after mark flag.
|
||||
*
|
||||
* @return buffer after mark flag
|
||||
*/
|
||||
resetMark() {
|
||||
this.pos = this.markPos;
|
||||
this.markPos = -1;
|
||||
if (this.bufContainDataAfterMark) {
|
||||
const data = Buffer.allocUnsafe(this.pos - 4);
|
||||
this.buf.copy(data, 0, 4, this.pos);
|
||||
this.cmd.sequenceNo = -1;
|
||||
this.cmd.compressSequenceNo = -1;
|
||||
this.bufContainDataAfterMark = false;
|
||||
return data;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send packet to socket.
|
||||
*
|
||||
* @throws IOException if socket error occur.
|
||||
*/
|
||||
flush() {
|
||||
this.flushBuffer(true, 0);
|
||||
this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
this.cmd.sequenceNo = -1;
|
||||
this.cmd.compressSequenceNo = -1;
|
||||
this.cmdLength = 0;
|
||||
this.markPos = -1;
|
||||
}
|
||||
|
||||
flushPacket() {
|
||||
this.flushBuffer(false, 0);
|
||||
this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
this.cmdLength = 0;
|
||||
this.markPos = -1;
|
||||
}
|
||||
|
||||
startPacket(cmd) {
|
||||
this.cmd = cmd;
|
||||
this.pos = 4;
|
||||
}
|
||||
|
||||
writeInt8(value) {
|
||||
if (this.pos + 1 >= this.buf.length) {
|
||||
let b = Buffer.allocUnsafe(1);
|
||||
b[0] = value;
|
||||
this.writeBuffer(b, 0, 1);
|
||||
return;
|
||||
}
|
||||
this.buf[this.pos++] = value;
|
||||
}
|
||||
|
||||
writeInt16(value) {
|
||||
if (this.pos + 2 >= this.buf.length) {
|
||||
let b = Buffer.allocUnsafe(2);
|
||||
b[0] = value;
|
||||
b[1] = value >>> 8;
|
||||
this.writeBuffer(b, 0, 2);
|
||||
return;
|
||||
}
|
||||
this.buf[this.pos] = value;
|
||||
this.buf[this.pos + 1] = value >> 8;
|
||||
this.pos += 2;
|
||||
}
|
||||
|
||||
writeInt16AtPos(initPos) {
|
||||
this.buf[initPos] = this.pos - initPos - 2;
|
||||
this.buf[initPos + 1] = (this.pos - initPos - 2) >> 8;
|
||||
}
|
||||
|
||||
writeInt24(value) {
|
||||
if (this.pos + 3 >= this.buf.length) {
|
||||
//not enough space remaining
|
||||
let arr = Buffer.allocUnsafe(3);
|
||||
arr[0] = value;
|
||||
arr[1] = value >> 8;
|
||||
arr[2] = value >> 16;
|
||||
this.writeBuffer(arr, 0, 3);
|
||||
return;
|
||||
}
|
||||
|
||||
this.buf[this.pos] = value;
|
||||
this.buf[this.pos + 1] = value >> 8;
|
||||
this.buf[this.pos + 2] = value >> 16;
|
||||
this.pos += 3;
|
||||
}
|
||||
|
||||
writeInt32(value) {
|
||||
if (this.pos + 4 >= this.buf.length) {
|
||||
//not enough space remaining
|
||||
let arr = Buffer.allocUnsafe(4);
|
||||
arr.writeInt32LE(value, 0);
|
||||
this.writeBuffer(arr, 0, 4);
|
||||
return;
|
||||
}
|
||||
|
||||
this.buf[this.pos] = value;
|
||||
this.buf[this.pos + 1] = value >> 8;
|
||||
this.buf[this.pos + 2] = value >> 16;
|
||||
this.buf[this.pos + 3] = value >> 24;
|
||||
this.pos += 4;
|
||||
}
|
||||
|
||||
writeBigInt(value) {
|
||||
if (this.pos + 8 >= this.buf.length) {
|
||||
//not enough space remaining
|
||||
let arr = Buffer.allocUnsafe(8);
|
||||
arr.writeBigInt64LE(value, 0);
|
||||
this.writeBuffer(arr, 0, 8);
|
||||
return;
|
||||
}
|
||||
this.buf.writeBigInt64LE(value, this.pos);
|
||||
this.pos += 8;
|
||||
}
|
||||
|
||||
writeDouble(value) {
|
||||
if (this.pos + 8 >= this.buf.length) {
|
||||
//not enough space remaining
|
||||
let arr = Buffer.allocUnsafe(8);
|
||||
arr.writeDoubleLE(value, 0);
|
||||
this.writeBuffer(arr, 0, 8);
|
||||
return;
|
||||
}
|
||||
this.buf.writeDoubleLE(value, this.pos);
|
||||
this.pos += 8;
|
||||
}
|
||||
|
||||
writeLengthCoded(len) {
|
||||
if (len < 0xfb) {
|
||||
this.writeInt8(len);
|
||||
return;
|
||||
}
|
||||
|
||||
if (len < 65536) {
|
||||
//max length is len < 0xffff
|
||||
this.writeInt8(0xfc);
|
||||
this.writeInt16(len);
|
||||
} else if (len < 16777216) {
|
||||
this.writeInt8(0xfd);
|
||||
this.writeInt24(len);
|
||||
} else {
|
||||
this.writeInt8(0xfe);
|
||||
this.writeBigInt(BigInt(len));
|
||||
}
|
||||
}
|
||||
|
||||
writeBuffer(arr, off, len) {
|
||||
if (len > this.buf.length - this.pos) {
|
||||
if (this.buf.length !== MAX_BUFFER_SIZE) {
|
||||
this.growBuffer(len);
|
||||
}
|
||||
|
||||
//max buffer size
|
||||
if (len > this.buf.length - this.pos) {
|
||||
if (this.markPos !== -1) {
|
||||
this.growBuffer(len);
|
||||
if (this.markPos !== -1) {
|
||||
this.flushBufferStopAtMark();
|
||||
}
|
||||
}
|
||||
|
||||
if (len > this.buf.length - this.pos) {
|
||||
//not enough space in buffer, will stream :
|
||||
// fill buffer and flush until all data are snd
|
||||
let remainingLen = len;
|
||||
|
||||
while (true) {
|
||||
//filling buffer
|
||||
let lenToFillBuffer = Math.min(MAX_BUFFER_SIZE - this.pos, remainingLen);
|
||||
arr.copy(this.buf, this.pos, off, off + lenToFillBuffer);
|
||||
remainingLen -= lenToFillBuffer;
|
||||
off += lenToFillBuffer;
|
||||
this.pos += lenToFillBuffer;
|
||||
|
||||
if (remainingLen === 0) return;
|
||||
this.flushBuffer(false, remainingLen);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// node.js copy is fast only when copying big buffer.
|
||||
// quick array copy is multiple time faster for small copy
|
||||
if (len > 50) {
|
||||
arr.copy(this.buf, this.pos, off, off + len);
|
||||
this.pos += len;
|
||||
} else {
|
||||
for (let i = 0; i < len; ) {
|
||||
this.buf[this.pos++] = arr[off + i++];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write ascii string to socket (no escaping)
|
||||
*
|
||||
* @param str string
|
||||
*/
|
||||
writeStringAscii(str) {
|
||||
let len = str.length;
|
||||
|
||||
//not enough space remaining
|
||||
if (len >= this.buf.length - this.pos) {
|
||||
let strBuf = Buffer.from(str, 'ascii');
|
||||
this.writeBuffer(strBuf, 0, strBuf.length);
|
||||
return;
|
||||
}
|
||||
|
||||
for (let off = 0; off < len; ) {
|
||||
this.buf[this.pos++] = str.charCodeAt(off++);
|
||||
}
|
||||
}
|
||||
|
||||
writeLengthEncodedBuffer(buffer) {
|
||||
const len = buffer.length;
|
||||
this.writeLengthCoded(len);
|
||||
this.writeBuffer(buffer, 0, len);
|
||||
}
|
||||
|
||||
writeUtf8StringEscapeQuote(str) {
|
||||
const charsLength = str.length;
|
||||
|
||||
//not enough space remaining
|
||||
if (charsLength * 3 + 2 >= this.buf.length - this.pos) {
|
||||
const arr = Buffer.from(str, 'utf8');
|
||||
this.writeInt8(QUOTE);
|
||||
this.writeBufferEscape(arr);
|
||||
this.writeInt8(QUOTE);
|
||||
return;
|
||||
}
|
||||
|
||||
//create UTF-8 byte array
|
||||
//since javascript char are internally using UTF-16 using surrogate's pattern, 4 bytes unicode characters will
|
||||
//represent 2 characters : example "\uD83C\uDFA4" = 🎤 unicode 8 "no microphones"
|
||||
//so max size is 3 * charLength
|
||||
//(escape characters are 1 byte encoded, so length might only be 2 when escaped)
|
||||
// + 2 for the quotes for text protocol
|
||||
let charsOffset = 0;
|
||||
let currChar;
|
||||
this.buf[this.pos++] = QUOTE;
|
||||
//quick loop if only ASCII chars for faster escape
|
||||
for (; charsOffset < charsLength && (currChar = str.charCodeAt(charsOffset)) < 0x80; charsOffset++) {
|
||||
if (currChar === SLASH || currChar === QUOTE || currChar === ZERO_BYTE || currChar === DBL_QUOTE) {
|
||||
this.buf[this.pos++] = SLASH;
|
||||
}
|
||||
this.buf[this.pos++] = currChar;
|
||||
}
|
||||
|
||||
//if quick loop not finished
|
||||
while (charsOffset < charsLength) {
|
||||
currChar = str.charCodeAt(charsOffset++);
|
||||
if (currChar < 0x80) {
|
||||
if (currChar === SLASH || currChar === QUOTE || currChar === ZERO_BYTE || currChar === DBL_QUOTE) {
|
||||
this.buf[this.pos++] = SLASH;
|
||||
}
|
||||
this.buf[this.pos++] = currChar;
|
||||
} else if (currChar < 0x800) {
|
||||
this.buf[this.pos++] = 0xc0 | (currChar >> 6);
|
||||
this.buf[this.pos++] = 0x80 | (currChar & 0x3f);
|
||||
} else if (currChar >= 0xd800 && currChar < 0xe000) {
|
||||
//reserved for surrogate - see https://en.wikipedia.org/wiki/UTF-16
|
||||
if (currChar < 0xdc00) {
|
||||
//is high surrogate
|
||||
if (charsOffset + 1 > charsLength) {
|
||||
this.buf[this.pos++] = 0x3f;
|
||||
} else {
|
||||
const nextChar = str.charCodeAt(charsOffset);
|
||||
if (nextChar >= 0xdc00 && nextChar < 0xe000) {
|
||||
//is low surrogate
|
||||
const surrogatePairs = (currChar << 10) + nextChar + (0x010000 - (0xd800 << 10) - 0xdc00);
|
||||
this.buf[this.pos++] = 0xf0 | (surrogatePairs >> 18);
|
||||
this.buf[this.pos++] = 0x80 | ((surrogatePairs >> 12) & 0x3f);
|
||||
this.buf[this.pos++] = 0x80 | ((surrogatePairs >> 6) & 0x3f);
|
||||
this.buf[this.pos++] = 0x80 | (surrogatePairs & 0x3f);
|
||||
charsOffset++;
|
||||
} else {
|
||||
//must have low surrogate
|
||||
this.buf[this.pos++] = 0x3f;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//low surrogate without high surrogate before
|
||||
this.buf[this.pos++] = 0x3f;
|
||||
}
|
||||
} else {
|
||||
this.buf[this.pos++] = 0xe0 | (currChar >> 12);
|
||||
this.buf[this.pos++] = 0x80 | ((currChar >> 6) & 0x3f);
|
||||
this.buf[this.pos++] = 0x80 | (currChar & 0x3f);
|
||||
}
|
||||
}
|
||||
this.buf[this.pos++] = QUOTE;
|
||||
}
|
||||
|
||||
encodeIconvString(str) {
|
||||
return Iconv.encode(str, this.encoding);
|
||||
}
|
||||
|
||||
encodeNodeString(str) {
|
||||
return Buffer.from(str, this.encoding);
|
||||
}
|
||||
|
||||
writeDefaultBufferString(str) {
|
||||
//javascript use UCS-2 or UTF-16 string internal representation
|
||||
//that means that string to byte will be a maximum of * 3
|
||||
// (4 bytes utf-8 are represented on 2 UTF-16 characters)
|
||||
if (str.length * 3 < this.buf.length - this.pos) {
|
||||
this.pos += this.buf.write(str, this.pos, this.encoding);
|
||||
return;
|
||||
}
|
||||
|
||||
//checking real length
|
||||
let byteLength = Buffer.byteLength(str, this.encoding);
|
||||
if (byteLength > this.buf.length - this.pos) {
|
||||
if (this.buf.length < MAX_BUFFER_SIZE) {
|
||||
this.growBuffer(byteLength);
|
||||
}
|
||||
if (byteLength > this.buf.length - this.pos) {
|
||||
//not enough space in buffer, will stream :
|
||||
let strBuf = Buffer.from(str, this.encoding);
|
||||
this.writeBuffer(strBuf, 0, strBuf.length);
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.pos += this.buf.write(str, this.pos, this.encoding);
|
||||
}
|
||||
|
||||
writeDefaultBufferLengthEncodedString(str) {
|
||||
//javascript use UCS-2 or UTF-16 string internal representation
|
||||
//that means that string to byte will be a maximum of * 3
|
||||
// (4 bytes utf-8 are represented on 2 UTF-16 characters)
|
||||
//checking real length
|
||||
let byteLength = Buffer.byteLength(str, this.encoding);
|
||||
this.writeLengthCoded(byteLength);
|
||||
|
||||
if (byteLength > this.buf.length - this.pos) {
|
||||
if (this.buf.length < MAX_BUFFER_SIZE) {
|
||||
this.growBuffer(byteLength);
|
||||
}
|
||||
if (byteLength > this.buf.length - this.pos) {
|
||||
//not enough space in buffer, will stream :
|
||||
let strBuf = Buffer.from(str, this.encoding);
|
||||
this.writeBuffer(strBuf, 0, strBuf.length);
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.pos += this.buf.write(str, this.pos, this.encoding);
|
||||
}
|
||||
|
||||
writeDefaultIconvString(str) {
|
||||
let buf = Iconv.encode(str, this.encoding);
|
||||
this.writeBuffer(buf, 0, buf.length);
|
||||
}
|
||||
|
||||
writeDefaultIconvLengthEncodedString(str) {
|
||||
let buf = Iconv.encode(str, this.encoding);
|
||||
this.writeLengthCoded(buf.length);
|
||||
this.writeBuffer(buf, 0, buf.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameters need to be properly escaped :
|
||||
* following characters are to be escaped by "\" :
|
||||
* - \0
|
||||
* - \\
|
||||
* - \'
|
||||
* - \"
|
||||
* - \032
|
||||
* regex split part of string writing part, and escaping special char.
|
||||
* Those chars are <= 7f meaning that this will work even with multibyte encoding
|
||||
*
|
||||
* @param str string to escape.
|
||||
*/
|
||||
writeDefaultStringEscapeQuote(str) {
|
||||
this.writeInt8(QUOTE);
|
||||
let match;
|
||||
let lastIndex = 0;
|
||||
while ((match = CHARS_GLOBAL_REGEXP.exec(str)) !== null) {
|
||||
this.writeString(str.slice(lastIndex, match.index));
|
||||
this.writeInt8(SLASH);
|
||||
this.writeInt8(match[0].charCodeAt(0));
|
||||
lastIndex = CHARS_GLOBAL_REGEXP.lastIndex;
|
||||
}
|
||||
|
||||
if (lastIndex === 0) {
|
||||
// Nothing was escaped
|
||||
this.writeString(str);
|
||||
this.writeInt8(QUOTE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (lastIndex < str.length) {
|
||||
this.writeString(str.slice(lastIndex));
|
||||
}
|
||||
this.writeInt8(QUOTE);
|
||||
}
|
||||
|
||||
writeBinaryDate(date) {
|
||||
const year = date.getFullYear();
|
||||
const mon = date.getMonth() + 1;
|
||||
const day = date.getDate();
|
||||
const hour = date.getHours();
|
||||
const min = date.getMinutes();
|
||||
const sec = date.getSeconds();
|
||||
const ms = date.getMilliseconds();
|
||||
|
||||
let len = ms === 0 ? 7 : 11;
|
||||
//not enough space remaining
|
||||
if (len + 1 > this.buf.length - this.pos) {
|
||||
let tmpBuf = Buffer.allocUnsafe(len + 1);
|
||||
|
||||
tmpBuf[0] = len;
|
||||
tmpBuf[1] = year;
|
||||
tmpBuf[2] = year >>> 8;
|
||||
tmpBuf[3] = mon;
|
||||
tmpBuf[4] = day;
|
||||
tmpBuf[5] = hour;
|
||||
tmpBuf[6] = min;
|
||||
tmpBuf[7] = sec;
|
||||
if (ms !== 0) {
|
||||
const micro = ms * 1000;
|
||||
tmpBuf[8] = micro;
|
||||
tmpBuf[9] = micro >>> 8;
|
||||
tmpBuf[10] = micro >>> 16;
|
||||
tmpBuf[11] = micro >>> 24;
|
||||
}
|
||||
|
||||
this.writeBuffer(tmpBuf, 0, len + 1);
|
||||
return;
|
||||
}
|
||||
|
||||
this.buf[this.pos] = len;
|
||||
this.buf[this.pos + 1] = year;
|
||||
this.buf[this.pos + 2] = year >>> 8;
|
||||
this.buf[this.pos + 3] = mon;
|
||||
this.buf[this.pos + 4] = day;
|
||||
this.buf[this.pos + 5] = hour;
|
||||
this.buf[this.pos + 6] = min;
|
||||
this.buf[this.pos + 7] = sec;
|
||||
|
||||
if (ms !== 0) {
|
||||
const micro = ms * 1000;
|
||||
this.buf[this.pos + 8] = micro;
|
||||
this.buf[this.pos + 9] = micro >>> 8;
|
||||
this.buf[this.pos + 10] = micro >>> 16;
|
||||
this.buf[this.pos + 11] = micro >>> 24;
|
||||
}
|
||||
this.pos += len + 1;
|
||||
}
|
||||
|
||||
writeBufferEscape(val) {
|
||||
let valLen = val.length;
|
||||
if (valLen * 2 > this.buf.length - this.pos) {
|
||||
//makes buffer bigger (up to 16M)
|
||||
if (this.buf.length !== MAX_BUFFER_SIZE) this.growBuffer(valLen * 2);
|
||||
|
||||
//data may still be bigger than buffer.
|
||||
//must flush buffer when full (and reset position to 4)
|
||||
if (valLen * 2 > this.buf.length - this.pos) {
|
||||
//not enough space in buffer, will fill buffer
|
||||
for (let i = 0; i < valLen; i++) {
|
||||
switch (val[i]) {
|
||||
case QUOTE:
|
||||
case SLASH:
|
||||
case DBL_QUOTE:
|
||||
case ZERO_BYTE:
|
||||
if (this.pos >= this.buf.length) this.flushBuffer(false, (valLen - i) * 2);
|
||||
this.buf[this.pos++] = SLASH; //add escape slash
|
||||
}
|
||||
if (this.pos >= this.buf.length) this.flushBuffer(false, (valLen - i) * 2);
|
||||
this.buf[this.pos++] = val[i];
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//sure to have enough place to use buffer directly
|
||||
for (let i = 0; i < valLen; i++) {
|
||||
switch (val[i]) {
|
||||
case QUOTE:
|
||||
case SLASH:
|
||||
case DBL_QUOTE:
|
||||
case ZERO_BYTE:
|
||||
this.buf[this.pos++] = SLASH; //add escape slash
|
||||
}
|
||||
this.buf[this.pos++] = val[i];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Count query size. If query size is greater than max_allowed_packet and nothing has been already
|
||||
* send, throw an exception to avoid having the connection closed.
|
||||
*
|
||||
* @param length additional length to query size
|
||||
* @param info current connection information
|
||||
* @throws Error if query has not to be sent.
|
||||
*/
|
||||
checkMaxAllowedLength(length, info) {
|
||||
if (this.opts.maxAllowedPacket && this.cmdLength + length >= this.maxAllowedPacket) {
|
||||
// launch exception only if no packet has been sent.
|
||||
return Errors.createError(
|
||||
`query size (${this.cmdLength + length}) is >= to max_allowed_packet (${this.maxAllowedPacket})`,
|
||||
Errors.ER_MAX_ALLOWED_PACKET,
|
||||
info
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate if buffer contain any data.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isEmpty() {
|
||||
return this.pos <= 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush the internal buffer.
|
||||
*/
|
||||
flushBufferDebug(commandEnd, remainingLen) {
|
||||
if (this.pos > 4) {
|
||||
this.buf[0] = this.pos - 4;
|
||||
this.buf[1] = (this.pos - 4) >>> 8;
|
||||
this.buf[2] = (this.pos - 4) >>> 16;
|
||||
this.buf[3] = ++this.cmd.sequenceNo;
|
||||
this.stream.writeBuf(this.buf.subarray(0, this.pos), this.cmd);
|
||||
this.stream.flush(true, this.cmd);
|
||||
this.cmdLength += this.pos - 4;
|
||||
|
||||
this.opts.logger.network(
|
||||
`==> conn:${this.info.threadId ? this.info.threadId : -1} ${
|
||||
this.cmd.constructor.name + '(0,' + this.pos + ')'
|
||||
}\n${Utils.log(this.opts, this.buf, 0, this.pos)}`
|
||||
);
|
||||
|
||||
if (commandEnd && this.pos === MAX_BUFFER_SIZE) {
|
||||
//if last packet fill the max size, must send an empty com to indicate that command end.
|
||||
this.writeEmptyPacket();
|
||||
}
|
||||
this.buf = this.createBufferWithMinSize(remainingLen);
|
||||
this.pos = 4;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush to last mark.
|
||||
*/
|
||||
flushBufferStopAtMark() {
|
||||
const end = this.pos;
|
||||
this.pos = this.markPos;
|
||||
const tmpBuf = Buffer.allocUnsafe(Math.max(SMALL_BUFFER_SIZE, end + 4 - this.pos));
|
||||
this.buf.copy(tmpBuf, 4, this.markPos, end);
|
||||
this.flushBuffer(true, end - this.pos);
|
||||
this.cmdLength = 0;
|
||||
this.buf = tmpBuf;
|
||||
this.pos = 4 + end - this.markPos;
|
||||
this.markPos = -1;
|
||||
this.bufContainDataAfterMark = true;
|
||||
}
|
||||
|
||||
flushBufferBasic(commandEnd, remainingLen) {
|
||||
this.buf[0] = this.pos - 4;
|
||||
this.buf[1] = (this.pos - 4) >>> 8;
|
||||
this.buf[2] = (this.pos - 4) >>> 16;
|
||||
this.buf[3] = ++this.cmd.sequenceNo;
|
||||
this.stream.writeBuf(this.buf.subarray(0, this.pos), this.cmd);
|
||||
this.stream.flush(true, this.cmd);
|
||||
this.cmdLength += this.pos - 4;
|
||||
if (commandEnd && this.pos === MAX_BUFFER_SIZE) {
|
||||
//if last packet fill the max size, must send an empty com to indicate that command end.
|
||||
this.writeEmptyPacket();
|
||||
}
|
||||
this.buf = this.createBufferWithMinSize(remainingLen);
|
||||
this.pos = 4;
|
||||
}
|
||||
|
||||
createBufferWithMinSize(remainingLen) {
|
||||
let newCapacity;
|
||||
if (remainingLen + 4 < SMALL_BUFFER_SIZE) {
|
||||
newCapacity = SMALL_BUFFER_SIZE;
|
||||
} else if (remainingLen + 4 < MEDIUM_BUFFER_SIZE) {
|
||||
newCapacity = MEDIUM_BUFFER_SIZE;
|
||||
} else if (remainingLen + 4 < LARGE_BUFFER_SIZE) {
|
||||
newCapacity = LARGE_BUFFER_SIZE;
|
||||
} else if (remainingLen + 4 < BIG_BUFFER_SIZE) {
|
||||
newCapacity = BIG_BUFFER_SIZE;
|
||||
} else {
|
||||
newCapacity = MAX_BUFFER_SIZE;
|
||||
}
|
||||
return Buffer.allocUnsafe(newCapacity);
|
||||
}
|
||||
|
||||
fastFlushDebug(cmd, packet) {
|
||||
this.stream.writeBuf(packet, cmd);
|
||||
this.stream.flush(true, cmd);
|
||||
this.cmdLength += packet.length;
|
||||
|
||||
this.opts.logger.network(
|
||||
`==> conn:${this.info.threadId ? this.info.threadId : -1} ${
|
||||
cmd.constructor.name + '(0,' + packet.length + ')'
|
||||
}\n${Utils.log(this.opts, packet, 0, packet.length)}`
|
||||
);
|
||||
this.cmdLength = 0;
|
||||
this.markPos = -1;
|
||||
}
|
||||
|
||||
fastFlushBasic(cmd, packet) {
|
||||
this.stream.writeBuf(packet, cmd);
|
||||
this.stream.flush(true, cmd);
|
||||
this.cmdLength = 0;
|
||||
this.markPos = -1;
|
||||
}
|
||||
|
||||
writeEmptyPacket() {
|
||||
const emptyBuf = Buffer.from([0x00, 0x00, 0x00, ++this.cmd.sequenceNo]);
|
||||
|
||||
if (this.debug) {
|
||||
this.opts.logger.network(
|
||||
`==> conn:${this.info.threadId ? this.info.threadId : -1} ${this.cmd.constructor.name}(0,4)\n${Utils.log(
|
||||
this.opts,
|
||||
emptyBuf,
|
||||
0,
|
||||
4
|
||||
)}`
|
||||
);
|
||||
}
|
||||
|
||||
this.stream.writeBuf(emptyBuf, this.cmd);
|
||||
this.stream.flush(true, this.cmd);
|
||||
this.cmdLength = 0;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PacketOutputStream;
|
||||
+600
@@ -0,0 +1,600 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (c) 2015-2024 MariaDB Corporation Ab
|
||||
|
||||
'use strict';
|
||||
|
||||
const Errors = require('../misc/errors');
|
||||
|
||||
/**
|
||||
* Object to easily parse buffer.
|
||||
* Packet are MUTABLE (buffer are changed, to avoid massive packet object creation).
|
||||
* Use clone() in case immutability is required
|
||||
*
|
||||
*/
|
||||
class Packet {
|
||||
update(buf, pos, end) {
|
||||
this.buf = buf;
|
||||
this.pos = pos;
|
||||
this.end = end;
|
||||
return this;
|
||||
}
|
||||
|
||||
skip(n) {
|
||||
this.pos += n;
|
||||
}
|
||||
|
||||
readGeometry(defaultVal) {
|
||||
const geoBuf = this.readBufferLengthEncoded();
|
||||
if (geoBuf === null || geoBuf.length === 0) {
|
||||
return defaultVal;
|
||||
}
|
||||
let geoPos = 4;
|
||||
return readGeometryObject(false);
|
||||
|
||||
function parseCoordinates(byteOrder) {
|
||||
geoPos += 16;
|
||||
const x = byteOrder ? geoBuf.readDoubleLE(geoPos - 16) : geoBuf.readDoubleBE(geoPos - 16);
|
||||
const y = byteOrder ? geoBuf.readDoubleLE(geoPos - 8) : geoBuf.readDoubleBE(geoPos - 8);
|
||||
return [x, y];
|
||||
}
|
||||
|
||||
function readGeometryObject(inner) {
|
||||
const byteOrder = geoBuf[geoPos++];
|
||||
const wkbType = byteOrder ? geoBuf.readInt32LE(geoPos) : geoBuf.readInt32BE(geoPos);
|
||||
geoPos += 4;
|
||||
switch (wkbType) {
|
||||
case 1: //wkbPoint
|
||||
const coords = parseCoordinates(byteOrder);
|
||||
|
||||
if (inner) return coords;
|
||||
return {
|
||||
type: 'Point',
|
||||
coordinates: coords
|
||||
};
|
||||
|
||||
case 2: //wkbLineString
|
||||
const pointNumber = byteOrder ? geoBuf.readInt32LE(geoPos) : geoBuf.readInt32BE(geoPos);
|
||||
geoPos += 4;
|
||||
let coordinates = [];
|
||||
for (let i = 0; i < pointNumber; i++) {
|
||||
coordinates.push(parseCoordinates(byteOrder));
|
||||
}
|
||||
if (inner) return coordinates;
|
||||
return {
|
||||
type: 'LineString',
|
||||
coordinates: coordinates
|
||||
};
|
||||
|
||||
case 3: //wkbPolygon
|
||||
let polygonCoordinates = [];
|
||||
const numRings = byteOrder ? geoBuf.readInt32LE(geoPos) : geoBuf.readInt32BE(geoPos);
|
||||
geoPos += 4;
|
||||
for (let ring = 0; ring < numRings; ring++) {
|
||||
const pointNumber = byteOrder ? geoBuf.readInt32LE(geoPos) : geoBuf.readInt32BE(geoPos);
|
||||
geoPos += 4;
|
||||
let linesCoordinates = [];
|
||||
for (let i = 0; i < pointNumber; i++) {
|
||||
linesCoordinates.push(parseCoordinates(byteOrder));
|
||||
}
|
||||
polygonCoordinates.push(linesCoordinates);
|
||||
}
|
||||
|
||||
if (inner) return polygonCoordinates;
|
||||
return {
|
||||
type: 'Polygon',
|
||||
coordinates: polygonCoordinates
|
||||
};
|
||||
|
||||
case 4: //wkbMultiPoint
|
||||
return {
|
||||
type: 'MultiPoint',
|
||||
coordinates: parseGeomArray(byteOrder, true)
|
||||
};
|
||||
|
||||
case 5: //wkbMultiLineString
|
||||
return {
|
||||
type: 'MultiLineString',
|
||||
coordinates: parseGeomArray(byteOrder, true)
|
||||
};
|
||||
case 6: //wkbMultiPolygon
|
||||
return {
|
||||
type: 'MultiPolygon',
|
||||
coordinates: parseGeomArray(byteOrder, true)
|
||||
};
|
||||
case 7: //wkbGeometryCollection
|
||||
return {
|
||||
type: 'GeometryCollection',
|
||||
geometries: parseGeomArray(byteOrder, false)
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseGeomArray(byteOrder, inner) {
|
||||
let coordinates = [];
|
||||
const number = byteOrder ? geoBuf.readInt32LE(geoPos) : geoBuf.readInt32BE(geoPos);
|
||||
geoPos += 4;
|
||||
for (let i = 0; i < number; i++) {
|
||||
coordinates.push(readGeometryObject(inner));
|
||||
}
|
||||
return coordinates;
|
||||
}
|
||||
}
|
||||
|
||||
peek() {
|
||||
return this.buf[this.pos];
|
||||
}
|
||||
|
||||
remaining() {
|
||||
return this.end - this.pos > 0;
|
||||
}
|
||||
|
||||
readInt8() {
|
||||
const val = this.buf[this.pos++];
|
||||
return val | ((val & (2 ** 7)) * 0x1fffffe);
|
||||
}
|
||||
|
||||
readUInt8() {
|
||||
return this.buf[this.pos++];
|
||||
}
|
||||
|
||||
readInt16() {
|
||||
this.pos += 2;
|
||||
const first = this.buf[this.pos - 2];
|
||||
const last = this.buf[this.pos - 1];
|
||||
const val = first + last * 2 ** 8;
|
||||
return val | ((val & (2 ** 15)) * 0x1fffe);
|
||||
}
|
||||
|
||||
readUInt16() {
|
||||
this.pos += 2;
|
||||
return this.buf[this.pos - 2] + this.buf[this.pos - 1] * 2 ** 8;
|
||||
}
|
||||
|
||||
readInt24() {
|
||||
const first = this.buf[this.pos];
|
||||
const last = this.buf[this.pos + 2];
|
||||
const val = first + this.buf[this.pos + 1] * 2 ** 8 + last * 2 ** 16;
|
||||
this.pos += 3;
|
||||
return val | ((val & (2 ** 23)) * 0x1fe);
|
||||
}
|
||||
|
||||
readUInt24() {
|
||||
this.pos += 3;
|
||||
return this.buf[this.pos - 3] + this.buf[this.pos - 2] * 2 ** 8 + this.buf[this.pos - 1] * 2 ** 16;
|
||||
}
|
||||
|
||||
readUInt32() {
|
||||
this.pos += 4;
|
||||
return (
|
||||
this.buf[this.pos - 4] +
|
||||
this.buf[this.pos - 3] * 2 ** 8 +
|
||||
this.buf[this.pos - 2] * 2 ** 16 +
|
||||
this.buf[this.pos - 1] * 2 ** 24
|
||||
);
|
||||
}
|
||||
|
||||
readInt32() {
|
||||
this.pos += 4;
|
||||
return (
|
||||
this.buf[this.pos - 4] +
|
||||
this.buf[this.pos - 3] * 2 ** 8 +
|
||||
this.buf[this.pos - 2] * 2 ** 16 +
|
||||
(this.buf[this.pos - 1] << 24)
|
||||
);
|
||||
}
|
||||
|
||||
readBigInt64() {
|
||||
const val = this.buf.readBigInt64LE(this.pos);
|
||||
this.pos += 8;
|
||||
return val;
|
||||
}
|
||||
|
||||
readBigUInt64() {
|
||||
const val = this.buf.readBigUInt64LE(this.pos);
|
||||
this.pos += 8;
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Metadata are length encoded, but cannot have length > 256, so simplified readUnsignedLength
|
||||
* @returns {number}
|
||||
*/
|
||||
readMetadataLength() {
|
||||
const type = this.buf[this.pos++];
|
||||
if (type < 0xfb) return type;
|
||||
return this.readUInt16();
|
||||
}
|
||||
|
||||
readUnsignedLength() {
|
||||
const type = this.buf[this.pos++];
|
||||
if (type < 0xfb) return type;
|
||||
switch (type) {
|
||||
case 0xfb:
|
||||
return null;
|
||||
case 0xfc:
|
||||
//readUInt16();
|
||||
this.pos += 2;
|
||||
return this.buf[this.pos - 2] + this.buf[this.pos - 1] * 2 ** 8;
|
||||
case 0xfd:
|
||||
//readUInt24();
|
||||
this.pos += 3;
|
||||
return this.buf[this.pos - 3] + this.buf[this.pos - 2] * 2 ** 8 + this.buf[this.pos - 1] * 2 ** 16;
|
||||
case 0xfe:
|
||||
// limitation to BigInt signed value
|
||||
return Number(this.readBigInt64());
|
||||
}
|
||||
}
|
||||
|
||||
readBuffer(len) {
|
||||
this.pos += len;
|
||||
return this.buf.subarray(this.pos - len, this.pos);
|
||||
}
|
||||
|
||||
readBufferRemaining() {
|
||||
let b = this.buf.subarray(this.pos, this.end);
|
||||
this.pos = this.end;
|
||||
return b;
|
||||
}
|
||||
|
||||
readBufferLengthEncoded() {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) return null;
|
||||
this.pos += len;
|
||||
return this.buf.subarray(this.pos - len, this.pos);
|
||||
}
|
||||
|
||||
readStringNullEnded() {
|
||||
let initialPosition = this.pos;
|
||||
let cnt = 0;
|
||||
while (this.remaining() > 0 && this.buf[this.pos++] !== 0) {
|
||||
cnt++;
|
||||
}
|
||||
return this.buf.toString(undefined, initialPosition, initialPosition + cnt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return unsigned Bigint.
|
||||
*
|
||||
* Could be used for reading other kinds of value than InsertId, if reading possible null value
|
||||
* @returns {bigint}
|
||||
*/
|
||||
readInsertId() {
|
||||
const type = this.buf[this.pos++];
|
||||
if (type < 0xfb) return BigInt(type);
|
||||
switch (type) {
|
||||
case 0xfc:
|
||||
this.pos += 2;
|
||||
return BigInt(this.buf[this.pos - 2] + this.buf[this.pos - 1] * 2 ** 8);
|
||||
case 0xfd:
|
||||
this.pos += 3;
|
||||
return BigInt(this.buf[this.pos - 3] + this.buf[this.pos - 2] * 2 ** 8 + this.buf[this.pos - 1] * 2 ** 16);
|
||||
case 0xfe:
|
||||
return this.readBigInt64();
|
||||
}
|
||||
}
|
||||
|
||||
readAsciiStringLengthEncoded() {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) return null;
|
||||
this.pos += len;
|
||||
return this.buf.toString('ascii', this.pos - len, this.pos);
|
||||
}
|
||||
|
||||
readStringLengthEncoded() {
|
||||
throw new Error('code is normally superseded by Node encoder or Iconv depending on charset used');
|
||||
}
|
||||
|
||||
readBigIntLengthEncoded() {
|
||||
const len = this.buf[this.pos++];
|
||||
|
||||
// fast-path: if length encoded is < to 16, value is in safe integer range, using atoi
|
||||
if (len < 16) {
|
||||
return BigInt(this._atoi(len));
|
||||
}
|
||||
|
||||
if (len === 0xfb) return null;
|
||||
|
||||
return this.readBigIntFromLen(len);
|
||||
}
|
||||
|
||||
readBigIntFromLen(len) {
|
||||
// atoll
|
||||
let result = 0n;
|
||||
let negate = false;
|
||||
let begin = this.pos;
|
||||
|
||||
if (len > 0 && this.buf[begin] === 45) {
|
||||
//minus sign
|
||||
negate = true;
|
||||
begin++;
|
||||
}
|
||||
for (; begin < this.pos + len; begin++) {
|
||||
result = result * 10n + BigInt(this.buf[begin] - 48);
|
||||
}
|
||||
this.pos += len;
|
||||
return negate ? -1n * result : result;
|
||||
}
|
||||
|
||||
readDecimalLengthEncoded() {
|
||||
const len = this.buf[this.pos++];
|
||||
if (len === 0xfb) return null;
|
||||
this.pos += len;
|
||||
return this.buf.toString('ascii', this.pos - len, this.pos);
|
||||
}
|
||||
|
||||
readDate() {
|
||||
const len = this.buf[this.pos++];
|
||||
if (len === 0xfb) return null;
|
||||
let res = [];
|
||||
let value = 0;
|
||||
let initPos = this.pos;
|
||||
this.pos += len;
|
||||
while (initPos < this.pos) {
|
||||
const char = this.buf[initPos++];
|
||||
if (char === 45) {
|
||||
//minus separator
|
||||
res.push(value);
|
||||
value = 0;
|
||||
} else {
|
||||
value = value * 10 + char - 48;
|
||||
}
|
||||
}
|
||||
res.push(value);
|
||||
|
||||
//handle zero-date as null
|
||||
if (res[0] === 0 && res[1] === 0 && res[2] === 0) return null;
|
||||
|
||||
return new Date(res[0], res[1] - 1, res[2]);
|
||||
}
|
||||
|
||||
readBinaryDate(opts) {
|
||||
const len = this.buf[this.pos++];
|
||||
let year = 0;
|
||||
let month = 0;
|
||||
let day = 0;
|
||||
if (len > 0) {
|
||||
year = this.readInt16();
|
||||
if (len > 2) {
|
||||
month = this.readUInt8() - 1;
|
||||
if (len > 3) {
|
||||
day = this.readUInt8();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (year === 0 && month === 0 && day === 0) return opts.dateStrings ? '0000-00-00' : null;
|
||||
if (opts.dateStrings) {
|
||||
return `${appendZero(year, 4)}-${appendZero(month + 1, 2)}-${appendZero(day, 2)}`;
|
||||
}
|
||||
//handle zero-date as null
|
||||
return new Date(year, month, day);
|
||||
}
|
||||
|
||||
readDateTime() {
|
||||
const len = this.buf[this.pos++];
|
||||
if (len === 0xfb) return null;
|
||||
this.pos += len;
|
||||
const str = this.buf.toString('ascii', this.pos - len, this.pos);
|
||||
if (str.startsWith('0000-00-00 00:00:00')) return null;
|
||||
return new Date(str);
|
||||
}
|
||||
|
||||
readBinaryDateTime() {
|
||||
const len = this.buf[this.pos++];
|
||||
let year = 0;
|
||||
let month = 0;
|
||||
let day = 0;
|
||||
let hour = 0;
|
||||
let min = 0;
|
||||
let sec = 0;
|
||||
let microSec = 0;
|
||||
|
||||
if (len > 0) {
|
||||
year = this.readInt16();
|
||||
if (len > 2) {
|
||||
month = this.readUInt8();
|
||||
if (len > 3) {
|
||||
day = this.readUInt8();
|
||||
if (len > 4) {
|
||||
hour = this.readUInt8();
|
||||
min = this.readUInt8();
|
||||
sec = this.readUInt8();
|
||||
if (len > 7) {
|
||||
microSec = this.readUInt32();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//handle zero-date as null
|
||||
if (year === 0 && month === 0 && day === 0 && hour === 0 && min === 0 && sec === 0 && microSec === 0) return null;
|
||||
return new Date(year, month - 1, day, hour, min, sec, microSec / 1000);
|
||||
}
|
||||
|
||||
readBinaryDateTimeAsString(scale) {
|
||||
const len = this.buf[this.pos++];
|
||||
let year = 0;
|
||||
let month = 0;
|
||||
let day = 0;
|
||||
let hour = 0;
|
||||
let min = 0;
|
||||
let sec = 0;
|
||||
let microSec = 0;
|
||||
|
||||
if (len > 0) {
|
||||
year = this.readInt16();
|
||||
if (len > 2) {
|
||||
month = this.readUInt8();
|
||||
if (len > 3) {
|
||||
day = this.readUInt8();
|
||||
if (len > 4) {
|
||||
hour = this.readUInt8();
|
||||
min = this.readUInt8();
|
||||
sec = this.readUInt8();
|
||||
if (len > 7) {
|
||||
microSec = this.readUInt32();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//handle zero-date as null
|
||||
if (year === 0 && month === 0 && day === 0 && hour === 0 && min === 0 && sec === 0 && microSec === 0)
|
||||
return '0000-00-00 00:00:00' + (scale > 0 ? '.000000'.substring(0, scale + 1) : '');
|
||||
|
||||
return (
|
||||
appendZero(year, 4) +
|
||||
'-' +
|
||||
appendZero(month, 2) +
|
||||
'-' +
|
||||
appendZero(day, 2) +
|
||||
' ' +
|
||||
appendZero(hour, 2) +
|
||||
':' +
|
||||
appendZero(min, 2) +
|
||||
':' +
|
||||
appendZero(sec, 2) +
|
||||
(microSec > 0
|
||||
? scale > 0
|
||||
? '.' + appendZero(microSec, 6).substring(0, scale)
|
||||
: '.' + appendZero(microSec, 6)
|
||||
: scale > 0
|
||||
? '.' + appendZero(microSec, 6).substring(0, scale)
|
||||
: '')
|
||||
);
|
||||
}
|
||||
|
||||
readBinaryTime() {
|
||||
const len = this.buf[this.pos++];
|
||||
let negate = false;
|
||||
let hour = 0;
|
||||
let min = 0;
|
||||
let sec = 0;
|
||||
let microSec = 0;
|
||||
|
||||
if (len > 0) {
|
||||
negate = this.buf[this.pos++] === 1;
|
||||
hour = this.readUInt32() * 24 + this.readUInt8();
|
||||
min = this.readUInt8();
|
||||
sec = this.readUInt8();
|
||||
if (len > 8) {
|
||||
microSec = this.readUInt32();
|
||||
}
|
||||
}
|
||||
let val = appendZero(hour, 2) + ':' + appendZero(min, 2) + ':' + appendZero(sec, 2);
|
||||
if (microSec > 0) {
|
||||
val += '.' + appendZero(microSec, 6);
|
||||
}
|
||||
if (negate) return '-' + val;
|
||||
return val;
|
||||
}
|
||||
|
||||
readFloat() {
|
||||
const val = this.buf.readFloatLE(this.pos);
|
||||
this.pos += 4;
|
||||
return val;
|
||||
}
|
||||
|
||||
readDouble() {
|
||||
const val = this.buf.readDoubleLE(this.pos);
|
||||
this.pos += 8;
|
||||
return val;
|
||||
}
|
||||
|
||||
readIntLengthEncoded() {
|
||||
const len = this.buf[this.pos++];
|
||||
if (len === 0xfb) return null;
|
||||
return this._atoi(len);
|
||||
}
|
||||
|
||||
_atoi(len) {
|
||||
let result = 0;
|
||||
let negate = false;
|
||||
let begin = this.pos;
|
||||
|
||||
if (len > 0 && this.buf[begin] === 45) {
|
||||
//minus sign
|
||||
negate = true;
|
||||
begin++;
|
||||
}
|
||||
for (; begin < this.pos + len; begin++) {
|
||||
result = result * 10 + (this.buf[begin] - 48);
|
||||
}
|
||||
this.pos += len;
|
||||
return negate ? -1 * result : result;
|
||||
}
|
||||
|
||||
readFloatLengthCoded() {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) return null;
|
||||
this.pos += len;
|
||||
return +this.buf.toString('ascii', this.pos - len, this.pos);
|
||||
}
|
||||
|
||||
skipLengthCodedNumber() {
|
||||
const type = this.buf[this.pos++];
|
||||
switch (type) {
|
||||
case 251:
|
||||
return;
|
||||
case 252:
|
||||
this.pos += 2 + (0xffff & (this.buf[this.pos] + (this.buf[this.pos + 1] << 8)));
|
||||
return;
|
||||
case 253:
|
||||
this.pos +=
|
||||
3 + (0xffffff & (this.buf[this.pos] + (this.buf[this.pos + 1] << 8) + (this.buf[this.pos + 2] << 16)));
|
||||
return;
|
||||
case 254:
|
||||
this.pos += 8 + Number(this.buf.readBigUInt64LE(this.pos));
|
||||
return;
|
||||
default:
|
||||
this.pos += type;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
length() {
|
||||
return this.end - this.pos;
|
||||
}
|
||||
|
||||
subPacketLengthEncoded(len) {}
|
||||
|
||||
/**
|
||||
* Parse ERR_Packet : https://mariadb.com/kb/en/library/err_packet/
|
||||
*
|
||||
* @param info current connection info
|
||||
* @param sql command sql
|
||||
* @param stack additional stack trace
|
||||
* @returns {Error}
|
||||
*/
|
||||
readError(info, sql, stack) {
|
||||
this.skip(1);
|
||||
let errno = this.readUInt16();
|
||||
let sqlState;
|
||||
let msg;
|
||||
// check '#'
|
||||
if (this.peek() === 0x23) {
|
||||
// skip '#'
|
||||
this.skip(6);
|
||||
sqlState = this.buf.toString(undefined, this.pos - 5, this.pos);
|
||||
msg = this.readStringNullEnded();
|
||||
} else {
|
||||
// pre 4.1 format
|
||||
sqlState = 'HY000';
|
||||
msg = this.buf.toString(undefined, this.pos, this.end);
|
||||
}
|
||||
let fatal = sqlState.startsWith('08') || sqlState === '70100';
|
||||
return Errors.createError(msg, errno, info, sqlState, sql, fatal, stack);
|
||||
}
|
||||
}
|
||||
|
||||
const appendZero = (val, len) => {
|
||||
let st = val.toString();
|
||||
while (st.length < len) {
|
||||
st = '0' + st;
|
||||
}
|
||||
return st;
|
||||
};
|
||||
|
||||
module.exports = Packet;
|
||||
Reference in New Issue
Block a user