API inicial
This commit is contained in:
590
node_modules/mariadb/lib/io/bulk-packet.js
generated
vendored
Normal file
590
node_modules/mariadb/lib/io/bulk-packet.js
generated
vendored
Normal file
@@ -0,0 +1,590 @@
|
||||
'use strict';
|
||||
|
||||
const moment = require('moment-timezone');
|
||||
const Iconv = require('iconv-lite');
|
||||
const SMALL_BUFFER_SIZE = 1024;
|
||||
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
|
||||
|
||||
/**
|
||||
* Packet splitter.
|
||||
*
|
||||
* The servers have a limit max_allowed_packet which limits the size of the data sent, to avoid saturating the server in memory.
|
||||
*
|
||||
* The following implementation has a workaround that will rewrite the command and separate the send according to this value.
|
||||
* This implies that this command can send multiple commands, with some tricks for sequencing packets.
|
||||
*
|
||||
*/
|
||||
class BulkPacket {
|
||||
constructor(opts, out, row) {
|
||||
this.out = out;
|
||||
this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
this.pos = 4;
|
||||
this.datatypes = [];
|
||||
this.encoding = out.encoding;
|
||||
this.statementId = -1;
|
||||
this.waitingResponseNo = 1;
|
||||
this.singleQuery = false;
|
||||
this.haveErrorResponse = false;
|
||||
this.writeBinaryDate = opts.tz
|
||||
? opts.tz === 'Etc/UTC'
|
||||
? this.writeBinaryUtcDate
|
||||
: this.writeBinaryTimezoneDate
|
||||
: this.writeBinaryLocalDate;
|
||||
if (this.encoding === 'utf8') {
|
||||
this.writeLengthEncodedString = this.writeDefaultLengthEncodedString;
|
||||
} else if (Buffer.isEncoding(this.encoding)) {
|
||||
this.writeLengthEncodedString = this.writeDefaultLengthEncodedString;
|
||||
} else {
|
||||
this.writeLengthEncodedString = this.writeIconvLengthEncodedString;
|
||||
}
|
||||
this.maxAllowedPacket = opts.maxAllowedPacket;
|
||||
this.maxPacketSize = opts.maxAllowedPacket
|
||||
? Math.min(MAX_BUFFER_SIZE, opts.maxAllowedPacket)
|
||||
: 4194304;
|
||||
this.writeHeader(row);
|
||||
}
|
||||
|
||||
datatypeChanged(row) {
|
||||
if (this.datatypes.length !== row.length) return true;
|
||||
for (let r = 0; r < row.length; r++) {
|
||||
if (row[r] !== null) {
|
||||
switch (typeof row[r]) {
|
||||
case 'boolean':
|
||||
if (this.datatypes[r] !== 0x01) return true;
|
||||
break;
|
||||
case 'number':
|
||||
case 'bigint':
|
||||
if (this.datatypes[r] !== 0x0f) return true;
|
||||
break;
|
||||
case 'object':
|
||||
if (Object.prototype.toString.call(row[r]) === '[object Date]') {
|
||||
if (this.datatypes[r] !== 0x0c) return true;
|
||||
} else if (Buffer.isBuffer(row[r])) {
|
||||
if (this.datatypes[r] !== 0xfb) return true;
|
||||
} else if (
|
||||
row[r].type != null &&
|
||||
[
|
||||
'Point',
|
||||
'LineString',
|
||||
'Polygon',
|
||||
'MultiPoint',
|
||||
'MultiLineString',
|
||||
'MultiPolygon',
|
||||
'GeometryCollection'
|
||||
].includes(row[r].type)
|
||||
) {
|
||||
if (this.datatypes[r] !== 0xfb) return true;
|
||||
} else {
|
||||
if (this.datatypes[r] !== 0x0f) return true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (this.datatypes[r] !== 0x0f) return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
writeHeader(row) {
|
||||
this.buf[this.pos++] = 0xfa;
|
||||
|
||||
//use last prepare command
|
||||
this.buf[this.pos++] = this.statementId;
|
||||
this.buf[this.pos++] = this.statementId >> 8;
|
||||
this.buf[this.pos++] = this.statementId >> 16;
|
||||
this.buf[this.pos++] = this.statementId >> 24;
|
||||
//set bulk flags to Send types to server
|
||||
this.buf[this.pos++] = 0x80;
|
||||
this.buf[this.pos++] = 0x00;
|
||||
|
||||
//send data type (strings)
|
||||
this.datatypes = [];
|
||||
if (row) {
|
||||
for (let r = 0; r < row.length; r++) {
|
||||
if (row[r] === null) {
|
||||
this.buf[this.pos++] = 0x0f;
|
||||
} else {
|
||||
switch (typeof row[r]) {
|
||||
case 'boolean':
|
||||
this.buf[this.pos++] = 0x01;
|
||||
break;
|
||||
case 'bigint':
|
||||
case 'number':
|
||||
this.buf[this.pos++] = 0x0f;
|
||||
break;
|
||||
case 'object':
|
||||
if (Object.prototype.toString.call(row[r]) === '[object Date]') {
|
||||
this.buf[this.pos++] = 0x0c;
|
||||
} else if (Buffer.isBuffer(row[r])) {
|
||||
this.buf[this.pos++] = 0xfb;
|
||||
} else if (
|
||||
row[r].type != null &&
|
||||
[
|
||||
'Point',
|
||||
'LineString',
|
||||
'Polygon',
|
||||
'MultiPoint',
|
||||
'MultiLineString',
|
||||
'MultiPolygon',
|
||||
'GeometryCollection'
|
||||
].includes(row[r].type)
|
||||
) {
|
||||
this.buf[this.pos++] = 0xfb;
|
||||
} else {
|
||||
this.buf[this.pos++] = 0x0f;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
this.buf[this.pos++] = 0x0f;
|
||||
}
|
||||
}
|
||||
this.datatypes[r] = this.buf[this.pos - 1];
|
||||
this.buf[this.pos++] = 0x00;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 newCapacity = MAX_BUFFER_SIZE;
|
||||
|
||||
if (newCapacity > this.maxPacketSize && this.markPos) {
|
||||
this.flush(false, len);
|
||||
return true;
|
||||
} else {
|
||||
let newBuf = Buffer.allocUnsafe(Math.min(newCapacity));
|
||||
this.buf.copy(newBuf, 0, 0, this.pos);
|
||||
this.buf = newBuf;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
writeLengthStringAscii(val) {
|
||||
let len = val.length;
|
||||
//not enough space remaining
|
||||
if (len >= this.buf.length - this.pos) {
|
||||
let strBuf = Buffer.from(val, 'ascii');
|
||||
return this.writeLengthEncodedBuffer(strBuf);
|
||||
}
|
||||
|
||||
this.writeLength(len);
|
||||
for (let off = 0; off < len; ) {
|
||||
this.buf[this.pos++] = val.charCodeAt(off++);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
writeLength(len) {
|
||||
if (len < 0xfb) {
|
||||
return this.writeInt8(len);
|
||||
} else if (len < 65536) {
|
||||
let flushed = this.writeInt8(0xfc);
|
||||
return this.writeInt16(len) || flushed;
|
||||
} else if (len < 16777216) {
|
||||
let flushed = this.writeInt8(0xfd);
|
||||
return this.writeInt24(len) || flushed;
|
||||
} else {
|
||||
//4 last bytes are filled with 0, packet limitation size is 32 bit integer
|
||||
if (this.pos + 9 >= this.buf.length) {
|
||||
const tmpBuf = Buffer.allocUnsafe(9);
|
||||
tmpBuf[0] = 0xfe;
|
||||
tmpBuf[1] = len;
|
||||
tmpBuf[2] = len >>> 8;
|
||||
tmpBuf[3] = len >>> 16;
|
||||
tmpBuf[4] = len >>> 24;
|
||||
tmpBuf[5] = 0;
|
||||
tmpBuf[6] = 0;
|
||||
tmpBuf[7] = 0;
|
||||
tmpBuf[8] = 0;
|
||||
return this.writeBuffer(tmpBuf);
|
||||
}
|
||||
this.buf[this.pos++] = 0xfe;
|
||||
this.buf[this.pos++] = len;
|
||||
this.buf[this.pos++] = len >>> 8;
|
||||
this.buf[this.pos++] = len >>> 16;
|
||||
this.buf[this.pos++] = len >>> 24;
|
||||
this.buf[this.pos++] = 0;
|
||||
this.buf[this.pos++] = 0;
|
||||
this.buf[this.pos++] = 0;
|
||||
this.buf[this.pos++] = 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
writeLengthEncodedBuffer(val) {
|
||||
let valLen = val.length;
|
||||
let flushed = this.writeLength(valLen);
|
||||
return this.writeBuffer(val) || flushed;
|
||||
}
|
||||
|
||||
writeBuffer(val) {
|
||||
let flushed = false;
|
||||
let valLen = val.length;
|
||||
if (valLen > this.buf.length - this.pos) {
|
||||
//makes buffer bigger (up to 16M)
|
||||
if (this.buf.length < MAX_BUFFER_SIZE) flushed = this.growBuffer(valLen * 2);
|
||||
|
||||
//data may still be bigger than buffer.
|
||||
//must flush buffer when full (and reset position to 4)
|
||||
if (valLen > this.buf.length - this.pos) {
|
||||
let tmpPos = this.buf.length - this.pos;
|
||||
val.copy(this.buf, this.pos, 0, tmpPos);
|
||||
this.pos += tmpPos;
|
||||
this.flush(false, valLen - tmpPos);
|
||||
|
||||
while (tmpPos < valLen) {
|
||||
if (this.buf.length - this.pos < valLen - tmpPos) this.growBuffer(valLen - tmpPos);
|
||||
const toWrite = Math.min(valLen - tmpPos, this.buf.length - this.pos);
|
||||
val.copy(this.buf, this.pos, tmpPos, tmpPos + toWrite);
|
||||
tmpPos += toWrite;
|
||||
this.pos += toWrite;
|
||||
if (valLen - tmpPos > 0) this.flush(false, valLen - tmpPos);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
//sure to have enough place to use buffer directly
|
||||
val.copy(this.buf, this.pos, 0, valLen);
|
||||
this.pos += valLen;
|
||||
return flushed;
|
||||
}
|
||||
|
||||
writeInt8(value) {
|
||||
let flushed = false;
|
||||
if (this.pos + 1 > this.buf.length) {
|
||||
if (this.buf.length < MAX_BUFFER_SIZE) {
|
||||
flushed = this.growBuffer(1);
|
||||
} else {
|
||||
this.flush(false, 1);
|
||||
this.buf[this.pos++] = value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
this.buf[this.pos++] = value;
|
||||
return flushed;
|
||||
}
|
||||
|
||||
writeInt16(value) {
|
||||
let flushed = false;
|
||||
if (this.pos + 2 > this.buf.length) {
|
||||
if (this.buf.length < this.maxPacketSize) flushed = this.growBuffer(2);
|
||||
if (this.pos + 2 > this.buf.length) {
|
||||
const tmpBuf = Buffer.allocUnsafe(2);
|
||||
tmpBuf[0] = value;
|
||||
tmpBuf[1] = value >>> 8;
|
||||
this.writeBuffer(tmpBuf);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
this.buf[this.pos++] = value;
|
||||
this.buf[this.pos++] = value >>> 8;
|
||||
return flushed;
|
||||
}
|
||||
|
||||
writeInt24(value) {
|
||||
let flushed = false;
|
||||
if (this.pos + 3 > this.buf.length) {
|
||||
if (this.buf.length < this.maxPacketSize) flushed = this.growBuffer(3);
|
||||
if (this.pos + 3 > this.buf.length) {
|
||||
const tmpBuf = Buffer.allocUnsafe(3);
|
||||
tmpBuf[0] = value;
|
||||
tmpBuf[1] = value >>> 8;
|
||||
tmpBuf[2] = value >>> 16;
|
||||
this.writeBuffer(tmpBuf);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
this.buf[this.pos++] = value;
|
||||
this.buf[this.pos++] = value >>> 8;
|
||||
this.buf[this.pos++] = value >>> 16;
|
||||
return flushed;
|
||||
}
|
||||
|
||||
writeIconvLengthEncodedString(str) {
|
||||
let buf = Iconv.encode(str, this.encoding);
|
||||
return this.writeLengthEncodedBuffer(buf, 0, buf.length);
|
||||
}
|
||||
|
||||
writeDefaultLengthEncodedString(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 + 10 < this.buf.length - this.pos) {
|
||||
//reserve position for length indicator
|
||||
const maxLen = str.length * 3;
|
||||
let lengthPos;
|
||||
if (maxLen < 0xfb) {
|
||||
lengthPos = this.pos;
|
||||
this.pos++;
|
||||
} else if (maxLen < 65536) {
|
||||
this.buf[this.pos++] = 0xfc;
|
||||
lengthPos = this.pos;
|
||||
this.pos += 2;
|
||||
} else {
|
||||
//if len was > 16M, would have been > to buffer length
|
||||
this.buf[this.pos++] = 0xfd;
|
||||
lengthPos = this.pos;
|
||||
this.pos += 3;
|
||||
}
|
||||
const prevPos = this.pos;
|
||||
this.pos += this.buf.write(str, this.pos, this.encoding);
|
||||
//write real data length
|
||||
const realLen = this.pos - prevPos;
|
||||
if (maxLen < 0xfb) {
|
||||
this.buf[lengthPos] = realLen;
|
||||
} else if (maxLen < 65536) {
|
||||
this.buf[lengthPos] = realLen;
|
||||
this.buf[lengthPos + 1] = realLen >>> 8;
|
||||
} else {
|
||||
this.buf[lengthPos] = realLen;
|
||||
this.buf[lengthPos + 1] = realLen >>> 8;
|
||||
this.buf[lengthPos + 2] = realLen >>> 16;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//checking real length
|
||||
let flushed = false;
|
||||
let byteLength = Buffer.byteLength(str, this.encoding);
|
||||
if (byteLength + 9 > this.buf.length - this.pos) {
|
||||
if (this.buf.length < MAX_BUFFER_SIZE) flushed = this.growBuffer(byteLength + 9);
|
||||
|
||||
if (byteLength > this.buf.length - this.pos) {
|
||||
//not enough space in buffer, will stream :
|
||||
let strBuf = Buffer.from(str, this.encoding);
|
||||
return this.writeLengthEncodedBuffer(strBuf) || flushed;
|
||||
}
|
||||
}
|
||||
this.writeLength(byteLength);
|
||||
this.pos += this.buf.write(str, this.pos, this.encoding);
|
||||
return flushed;
|
||||
}
|
||||
|
||||
writeBinaryLocalDate(date, opts) {
|
||||
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();
|
||||
return this._writeBinaryDate(year, mon, day, hour, min, sec, ms);
|
||||
}
|
||||
|
||||
writeBinaryUtcDate(date, opts) {
|
||||
const year = date.getUTCFullYear();
|
||||
const mon = date.getUTCMonth() + 1;
|
||||
const day = date.getUTCDate();
|
||||
const hour = date.getUTCHours();
|
||||
const min = date.getUTCMinutes();
|
||||
const sec = date.getUTCSeconds();
|
||||
const ms = date.getUTCMilliseconds();
|
||||
return this._writeBinaryDate(year, mon, day, hour, min, sec, ms);
|
||||
}
|
||||
|
||||
_writeBinaryDate(year, mon, day, hour, min, sec, ms) {
|
||||
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;
|
||||
}
|
||||
|
||||
return this.writeBuffer(tmpBuf);
|
||||
}
|
||||
|
||||
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;
|
||||
return false;
|
||||
}
|
||||
|
||||
writeBinaryTimezoneDate(date, opts) {
|
||||
const dateZoned = new Date(
|
||||
moment.tz(date, opts.localTz).tz(opts.tz).format('YYYY-MM-DD HH:mm:ss.SSSSSS')
|
||||
);
|
||||
const year = dateZoned.getFullYear();
|
||||
const mon = dateZoned.getMonth() + 1;
|
||||
const day = dateZoned.getDate();
|
||||
const hour = dateZoned.getHours();
|
||||
const min = dateZoned.getMinutes();
|
||||
const sec = dateZoned.getSeconds();
|
||||
const ms = dateZoned.getMilliseconds();
|
||||
return this._writeBinaryDate(year, mon, day, hour, min, sec, ms);
|
||||
}
|
||||
|
||||
mark(isLast, nextRow) {
|
||||
let flushed = false;
|
||||
this.nextRow = nextRow;
|
||||
if (this.singleQuery) {
|
||||
//end of big query that is more than 16M
|
||||
//write single one
|
||||
if (!this.haveErrorResponse) {
|
||||
const packetSendSize =
|
||||
this.pos +
|
||||
(this.singleQuerySequenceNo !== undefined
|
||||
? (this.singleQuerySequenceNo + 1) * MAX_BUFFER_SIZE
|
||||
: 0);
|
||||
if (this.maxAllowedPacket && packetSendSize > this.maxAllowedPacket) {
|
||||
console.log(
|
||||
"will send a packet to db server with size > connection option 'maxAllowedPacket' (size send is " +
|
||||
packetSendSize +
|
||||
') connection might be reset by server'
|
||||
);
|
||||
}
|
||||
this.copyAndFlush(true);
|
||||
flushed = true;
|
||||
this.markPos = undefined;
|
||||
}
|
||||
|
||||
this.singleQuerySequenceNo = undefined;
|
||||
this.singleQueryCompressSequenceNo = undefined;
|
||||
this.singleQuery = false;
|
||||
this.writeHeader(nextRow);
|
||||
this.markPos = undefined;
|
||||
} else {
|
||||
if (!isLast && this.datatypeChanged(nextRow)) {
|
||||
this.markPos = this.pos;
|
||||
this.flushMark();
|
||||
flushed = true;
|
||||
} else if (this.markPos && this.pos > this.maxPacketSize) {
|
||||
//not enough room for current query , flush mark.
|
||||
this.flushMark();
|
||||
flushed = true;
|
||||
} else {
|
||||
//just mark ending query
|
||||
this.markPos = this.pos;
|
||||
if (isLast) {
|
||||
this.flushMark();
|
||||
flushed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return flushed;
|
||||
}
|
||||
|
||||
flush(end, remainingLen) {
|
||||
if (this.markPos && !this.singleQuery) {
|
||||
this.flushMark();
|
||||
} else {
|
||||
//one insert is more than 16M, will continue to mono insert, hoping
|
||||
//that max_allowed_packet is sized accordingly to query.
|
||||
if (this.buf.length < MAX_BUFFER_SIZE) {
|
||||
//in this case, connector has default to 4M packet, and a single query size
|
||||
//is > to 4mb. growing buffer to 16M
|
||||
let newBuf = Buffer.allocUnsafe(MAX_BUFFER_SIZE);
|
||||
this.buf.copy(newBuf, 0, 0, this.pos);
|
||||
this.buf = newBuf;
|
||||
} else {
|
||||
if (!this.haveErrorResponse) {
|
||||
if (this.maxAllowedPacket && this.buf.length > this.maxAllowedPacket) {
|
||||
console.log(
|
||||
"will send a packet to server with size > connection option 'maxAllowedPacket' (size send is " +
|
||||
this.pos +
|
||||
') connection might be reset by server'
|
||||
);
|
||||
}
|
||||
this.copyAndFlush(false);
|
||||
|
||||
this.markPos = undefined;
|
||||
if (!this.singleQuery) this.waitingResponseNo++;
|
||||
this.singleQuery = true;
|
||||
this.singleQuerySequenceNo = this.out.cmd.sequenceNo;
|
||||
this.singleQueryCompressSequenceNo = this.out.cmd.compressSequenceNo;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
flushMark() {
|
||||
let afterMark;
|
||||
if (this.pos !== this.markPos) {
|
||||
afterMark = Buffer.allocUnsafe(this.pos - this.markPos);
|
||||
this.buf.copy(afterMark, 0, this.markPos, this.pos);
|
||||
}
|
||||
|
||||
this.pos = this.markPos;
|
||||
|
||||
if (!this.haveErrorResponse) {
|
||||
this.copyAndFlush(true);
|
||||
this.waitingResponseNo++;
|
||||
}
|
||||
|
||||
this.pos = 4;
|
||||
this.markPos = undefined;
|
||||
if (this.nextRow) this.writeHeader(this.nextRow);
|
||||
if (afterMark) {
|
||||
if (this.buf.length - this.pos < afterMark.length)
|
||||
this.growBuffer(afterMark.length - (this.buf.length - this.pos));
|
||||
afterMark.copy(this.buf, this.pos, 0, afterMark.length);
|
||||
this.pos += afterMark.length;
|
||||
}
|
||||
this.singleQuery = false;
|
||||
this.singleQuerySequenceNo = undefined;
|
||||
this.singleQueryCompressSequenceNo = undefined;
|
||||
}
|
||||
|
||||
copyAndFlush(ended) {
|
||||
this.out.buf = this.buf;
|
||||
this.out.pos = this.pos;
|
||||
if (this.singleQuerySequenceNo !== undefined) {
|
||||
this.out.cmd.sequenceNo = this.singleQuerySequenceNo;
|
||||
this.out.cmd.compressSequenceNo = this.singleQueryCompressSequenceNo;
|
||||
} else {
|
||||
this.out.cmd.sequenceNo = -1;
|
||||
this.out.cmd.compressSequenceNo = -1;
|
||||
}
|
||||
this.out.flushBuffer(ended);
|
||||
if (this.singleQuerySequenceNo !== undefined) {
|
||||
this.singleQuerySequenceNo = this.out.cmd.sequenceNo;
|
||||
this.singleQueryCompressSequenceNo = this.out.cmd.compressSequenceNo;
|
||||
}
|
||||
this.pos = 4;
|
||||
this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
endedWithError() {
|
||||
this.haveErrorResponse = true;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BulkPacket;
|
||||
141
node_modules/mariadb/lib/io/compression-input-stream.js
generated
vendored
Normal file
141
node_modules/mariadb/lib/io/compression-input-stream.js
generated
vendored
Normal file
@@ -0,0 +1,141 @@
|
||||
'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) {
|
||||
console.log(
|
||||
'<== conn:%d %s (compress)\n%s',
|
||||
this.info.threadId ? this.info.threadId : -1,
|
||||
cmd
|
||||
? cmd.onPacketReceive
|
||||
? cmd.constructor.name + '.' + cmd.onPacketReceive.name
|
||||
: cmd.constructor.name
|
||||
: 'no command',
|
||||
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.slice(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.slice(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;
|
||||
171
node_modules/mariadb/lib/io/compression-output-stream.js
generated
vendored
Normal file
171
node_modules/mariadb/lib/io/compression-output-stream.js
generated
vendored
Normal file
@@ -0,0 +1,171 @@
|
||||
'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 (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) {
|
||||
console.log(
|
||||
'==> conn:%d %s (compress)\n%s',
|
||||
this.info.threadId ? this.info.threadId : -1,
|
||||
cmd ? cmd.constructor.name + '(0,' + this.pos + ')' : 'unknown',
|
||||
Utils.log(this.opts, this.buf, 0, this.pos)
|
||||
);
|
||||
}
|
||||
|
||||
this.writer(this.buf.slice(0, this.pos));
|
||||
} else {
|
||||
//*******************************************************************************
|
||||
// compressing packet
|
||||
//*******************************************************************************
|
||||
//use synchronous inflating, to ensure FIFO packet order
|
||||
const compressChunk = ZLib.deflateSync(this.buf.slice(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) {
|
||||
console.log(
|
||||
'==> conn:%d %s (compress)\n%s',
|
||||
this.info.threadId ? this.info.threadId : -1,
|
||||
cmd ? cmd.constructor.name + '(0,' + this.pos + '=>' + compressChunkLen + ')' : 'unknown',
|
||||
Utils.log(this.opts, compressChunk, 0, compressChunkLen, this.header)
|
||||
);
|
||||
}
|
||||
|
||||
this.writer(this.header);
|
||||
this.writer(compressChunk);
|
||||
if (cmdEnd && this.pos === 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) {
|
||||
console.log(
|
||||
'==> conn:%d %s (compress)\n%s',
|
||||
this.info.threadId ? this.info.threadId : -1,
|
||||
cmd ? cmd.constructor.name + '(0,' + this.pos + ')' : 'unknown',
|
||||
Utils.log(this.opts, emptyBuf, 0, 7)
|
||||
);
|
||||
}
|
||||
|
||||
this.writer(emptyBuf);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = CompressionOutputStream;
|
||||
193
node_modules/mariadb/lib/io/packet-input-stream.js
generated
vendored
Normal file
193
node_modules/mariadb/lib/io/packet-input-stream.js
generated
vendored
Normal file
@@ -0,0 +1,193 @@
|
||||
'use strict';
|
||||
|
||||
const PacketNodeEncoded = require('./packet-node-encoded');
|
||||
const PacketIconvEncoded = require('./packet-node-iconv');
|
||||
|
||||
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.changeDebug(this.opts.logPackets, 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.packetConstructor = Buffer.isEncoding(this.encoding)
|
||||
? PacketNodeEncoded
|
||||
: PacketIconvEncoded;
|
||||
}
|
||||
|
||||
changeDebug(logPackets, debug) {
|
||||
this.logPackets = logPackets;
|
||||
this.debug = debug;
|
||||
this.receivePacket =
|
||||
this.logPackets || this.debug ? this.receivePacketDebug : this.receivePacketBasic;
|
||||
}
|
||||
|
||||
receivePacketDebug(packet) {
|
||||
let cmd = this.currentCmd();
|
||||
|
||||
if (packet) {
|
||||
const packetStr = Utils.log(this.opts, packet.buf, packet.pos, packet.end, this.header);
|
||||
if (this.opts.logPackets) {
|
||||
this.info.addPacket(
|
||||
'<== 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' +
|
||||
packetStr
|
||||
);
|
||||
}
|
||||
if (this.opts.debug) {
|
||||
console.log(
|
||||
'<== conn:%d %s (%d,%d)\n%s',
|
||||
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,
|
||||
packetStr
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!cmd) {
|
||||
this.unexpectedPacket(packet);
|
||||
return;
|
||||
}
|
||||
|
||||
cmd.sequenceNo = this.header[3];
|
||||
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.header[3];
|
||||
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.header[0] = chunk[pos];
|
||||
this.header[1] = chunk[pos + 1];
|
||||
this.header[2] = chunk[pos + 2];
|
||||
this.header[3] = chunk[pos + 3];
|
||||
pos += 4;
|
||||
this.headerLen = 4;
|
||||
this.packetLen = this.header[0] + (this.header[1] << 8) + (this.header[2] << 16);
|
||||
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);
|
||||
length = this.packetLen;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (length) {
|
||||
if (chunkLen - pos >= length) {
|
||||
const buf = chunk.slice(pos, pos + length);
|
||||
pos += length;
|
||||
if (this.parts) {
|
||||
this.parts.push(buf);
|
||||
this.partsTotalLen += length;
|
||||
|
||||
if (this.packetLen < 0xffffff) {
|
||||
let buf = Buffer.concat(this.parts, this.partsTotalLen);
|
||||
this.parts = null;
|
||||
const packet = new this.packetConstructor(buf, 0, this.partsTotalLen, this.encoding);
|
||||
this.receivePacket(packet);
|
||||
}
|
||||
} else {
|
||||
if (this.packetLen < 0xffffff) {
|
||||
const packet = new this.packetConstructor(buf, 0, length, this.encoding);
|
||||
this.receivePacket(packet);
|
||||
} else {
|
||||
this.parts = [buf];
|
||||
this.partsTotalLen = length;
|
||||
}
|
||||
}
|
||||
this.resetHeader();
|
||||
} else {
|
||||
const buf = chunk.slice(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 = PacketInputStream;
|
||||
36
node_modules/mariadb/lib/io/packet-node-encoded.js
generated
vendored
Normal file
36
node_modules/mariadb/lib/io/packet-node-encoded.js
generated
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
'use strict';
|
||||
|
||||
const Packet = require('./packet');
|
||||
|
||||
class PacketNodeEncoded extends Packet {
|
||||
constructor(buf, pos, end, encoding) {
|
||||
super(buf, pos, end);
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
readStringLength() {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) return null;
|
||||
|
||||
this.pos += len;
|
||||
return this.buf.toString(this.encoding, this.pos - len, this.pos);
|
||||
}
|
||||
|
||||
readString(beg, len) {
|
||||
return this.buf.toString(this.encoding, beg, beg + len);
|
||||
}
|
||||
|
||||
subPacketLengthEncoded() {
|
||||
const len = this.readUnsignedLength();
|
||||
this.skip(len);
|
||||
return new PacketNodeEncoded(this.buf, this.pos - len, this.pos, this.encoding);
|
||||
}
|
||||
|
||||
readStringRemaining() {
|
||||
const str = this.buf.toString(this.encoding, this.pos, this.end);
|
||||
this.pos = this.end;
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PacketNodeEncoded;
|
||||
37
node_modules/mariadb/lib/io/packet-node-iconv.js
generated
vendored
Normal file
37
node_modules/mariadb/lib/io/packet-node-iconv.js
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
const Packet = require('./packet');
|
||||
const Iconv = require('iconv-lite');
|
||||
|
||||
class PacketIconvEncoded extends Packet {
|
||||
constructor(buf, pos, end, encoding) {
|
||||
super(buf, pos, end);
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
readStringLength() {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) return null;
|
||||
|
||||
this.pos += len;
|
||||
return Iconv.decode(this.buf.slice(this.pos - len, this.pos), this.encoding);
|
||||
}
|
||||
|
||||
readString(beg, len) {
|
||||
return Iconv.decode(this.buf.slice(beg, beg + len), this.encoding);
|
||||
}
|
||||
|
||||
subPacketLengthEncoded() {
|
||||
const len = this.readUnsignedLength();
|
||||
this.skip(len);
|
||||
return new PacketIconvEncoded(this.buf, this.pos - len, this.pos, this.encoding);
|
||||
}
|
||||
|
||||
readStringRemaining() {
|
||||
const str = Iconv.decode(this.buf.slice(this.pos, this.end), this.encoding);
|
||||
this.pos = this.end;
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PacketIconvEncoded;
|
||||
502
node_modules/mariadb/lib/io/packet-output-stream.js
generated
vendored
Normal file
502
node_modules/mariadb/lib/io/packet-output-stream.js
generated
vendored
Normal file
@@ -0,0 +1,502 @@
|
||||
'use strict';
|
||||
|
||||
const Iconv = require('iconv-lite');
|
||||
const Utils = require('../misc/utils');
|
||||
|
||||
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 = 1024;
|
||||
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 = /[\0\"\'\\\b\n\r\t\u001A]/g; // eslint-disable-line no-control-regex
|
||||
|
||||
/**
|
||||
* 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.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
this.changeEncoding(this.opts.collation);
|
||||
this.changeDebug(this.opts.logPackets, 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.writeStringEscapeQuote = this.writeUtf8StringEscapeQuote;
|
||||
} else if (Buffer.isEncoding(this.encoding)) {
|
||||
this.writeString = this.writeDefaultBufferString;
|
||||
this.writeStringEscapeQuote = this.writeDefaultStringEscapeQuote;
|
||||
} else {
|
||||
this.writeString = this.writeDefaultIconvString;
|
||||
this.writeStringEscapeQuote = this.writeDefaultStringEscapeQuote;
|
||||
}
|
||||
}
|
||||
|
||||
changeDebug(logPackets, debug) {
|
||||
this.logPackets = logPackets;
|
||||
this.debug = debug;
|
||||
this.flushBuffer =
|
||||
this.logPackets || this.debug ? this.flushBufferDebug : this.flushBufferBasic;
|
||||
}
|
||||
|
||||
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 newCapacity = MAX_BUFFER_SIZE;
|
||||
|
||||
let newBuf = Buffer.allocUnsafe(newCapacity);
|
||||
this.buf.copy(newBuf, 0, 0, this.pos);
|
||||
this.buf = newBuf;
|
||||
}
|
||||
|
||||
startPacket(cmd) {
|
||||
this.cmd = cmd;
|
||||
this.pos = 4;
|
||||
}
|
||||
|
||||
writeInt8(value) {
|
||||
if (this.pos + 1 >= this.buf.length) {
|
||||
if (this.pos >= MAX_BUFFER_SIZE) {
|
||||
//buffer is more than a Packet, must flushBuffer()
|
||||
this.flushBuffer(false, 1);
|
||||
} else this.growBuffer(1);
|
||||
}
|
||||
this.buf[this.pos++] = value;
|
||||
}
|
||||
|
||||
writeInt16(value) {
|
||||
if (this.pos + 2 >= this.buf.length) {
|
||||
let b = Buffer.allocUnsafe(2);
|
||||
b.writeUInt16LE(value, 0);
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
writeLengthCoded(len) {
|
||||
//length encoded can be null(0xfb) or bigger than 65k, but only if using binary protocol
|
||||
//so not implemented for now
|
||||
if (len < 0xfb) {
|
||||
this.writeInt8(len);
|
||||
return;
|
||||
}
|
||||
|
||||
//max length is len < 0xffff
|
||||
this.writeInt8(0xfc);
|
||||
this.writeInt16(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) {
|
||||
//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);
|
||||
}
|
||||
}
|
||||
}
|
||||
arr.copy(this.buf, this.pos, off, off + len);
|
||||
this.pos += len;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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++);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
writeDefaultIconvString(str) {
|
||||
let buf = Iconv.encode(str, this.encoding);
|
||||
this.writeBuffer(buf, 0, buf.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameters need to be properly escaped :
|
||||
* following characters are to be escaped by "\" :
|
||||
* - \0
|
||||
* - \\
|
||||
* - \'
|
||||
* - \"
|
||||
* regex split part of string writing part, and escaping special char.
|
||||
* Those chars are <= 7f meaning that this will work even with multi-byte 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);
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate if buffer contain any data.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isEmpty() {
|
||||
return this.pos <= 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush the internal buffer.
|
||||
*/
|
||||
flushBufferDebug(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.slice(0, this.pos), this.cmd);
|
||||
|
||||
const packet = Utils.log(this.opts, this.buf, 0, this.pos);
|
||||
if (this.opts.logPackets) {
|
||||
this.info.addPacket(
|
||||
'==> conn:' +
|
||||
(this.info.threadId ? this.info.threadId : -1) +
|
||||
' ' +
|
||||
this.cmd.constructor.name +
|
||||
'(0,' +
|
||||
this.pos +
|
||||
')\n' +
|
||||
packet
|
||||
);
|
||||
}
|
||||
|
||||
if (this.opts.debug) {
|
||||
console.log(
|
||||
'==> conn:%d %s\n%s',
|
||||
this.info.threadId ? this.info.threadId : -1,
|
||||
this.cmd.constructor.name + '(0,' + this.pos + ')',
|
||||
Utils.log(this.opts, this.buf, 0, this.pos)
|
||||
);
|
||||
}
|
||||
|
||||
if (commandEnd) {
|
||||
//if last packet fill the max size, must send an empty com to indicate that command end.
|
||||
if (this.pos === MAX_BUFFER_SIZE) {
|
||||
this.writeEmptyPacket();
|
||||
} else {
|
||||
this.stream.flush(true, this.cmd);
|
||||
this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
}
|
||||
} else {
|
||||
this.buf = allocateBuffer(remainingLen + 4);
|
||||
this.pos = 4;
|
||||
}
|
||||
}
|
||||
|
||||
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.slice(0, this.pos), this.cmd);
|
||||
if (commandEnd) {
|
||||
//if last packet fill the max size, must send an empty com to indicate that command end.
|
||||
if (this.pos === MAX_BUFFER_SIZE) {
|
||||
this.writeEmptyPacket();
|
||||
} else {
|
||||
this.stream.flush(true, this.cmd);
|
||||
this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
}
|
||||
} else {
|
||||
this.buf = allocateBuffer(remainingLen + 4);
|
||||
this.pos = 4;
|
||||
}
|
||||
}
|
||||
|
||||
writeEmptyPacket() {
|
||||
const emptyBuf = Buffer.from([0x00, 0x00, 0x00, ++this.cmd.sequenceNo]);
|
||||
|
||||
if (this.opts.logPackets || this.opts.debug) {
|
||||
const packet = Utils.log(this.opts, emptyBuf, 0, 4);
|
||||
if (this.opts.logPackets) {
|
||||
this.info.addPacket(
|
||||
'==> conn:' +
|
||||
(this.info.threadId ? this.info.threadId : -1) +
|
||||
' ' +
|
||||
this.cmd.constructor.name +
|
||||
'(0,4)\n' +
|
||||
packet
|
||||
);
|
||||
}
|
||||
if (this.opts.debug) {
|
||||
console.log(
|
||||
'==> conn:%d %s\n%s',
|
||||
this.info.threadId ? this.info.threadId : -1,
|
||||
this.cmd.constructor.name + '(0,4)',
|
||||
packet
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
this.stream.writeBuf(emptyBuf, this.cmd);
|
||||
this.stream.flush(true, this.cmd);
|
||||
}
|
||||
}
|
||||
|
||||
function allocateBuffer(len) {
|
||||
if (len < SMALL_BUFFER_SIZE) {
|
||||
return Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
} else if (len < MEDIUM_BUFFER_SIZE) {
|
||||
return Buffer.allocUnsafe(MEDIUM_BUFFER_SIZE);
|
||||
} else if (len < LARGE_BUFFER_SIZE) {
|
||||
return Buffer.allocUnsafe(LARGE_BUFFER_SIZE);
|
||||
} else if (len < BIG_BUFFER_SIZE) {
|
||||
return Buffer.allocUnsafe(BIG_BUFFER_SIZE);
|
||||
}
|
||||
return Buffer.allocUnsafe(MAX_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
module.exports = PacketOutputStream;
|
||||
509
node_modules/mariadb/lib/io/packet.js
generated
vendored
Normal file
509
node_modules/mariadb/lib/io/packet.js
generated
vendored
Normal file
@@ -0,0 +1,509 @@
|
||||
'use strict';
|
||||
|
||||
const Errors = require('../misc/errors');
|
||||
const Iconv = require('iconv-lite');
|
||||
const Long = require('long');
|
||||
const moment = require('moment-timezone');
|
||||
|
||||
/**
|
||||
* Object to easily parse buffer.
|
||||
*
|
||||
*/
|
||||
class Packet {
|
||||
constructor(buf, pos, end) {
|
||||
this.buf = buf;
|
||||
this.pos = pos;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
skip(n) {
|
||||
this.pos += n;
|
||||
}
|
||||
|
||||
readGeometry(dataTypeName) {
|
||||
const geoBuf = this.readBufferLengthEncoded();
|
||||
if (geoBuf === null || geoBuf.length === 0) {
|
||||
if (dataTypeName) {
|
||||
switch (dataTypeName) {
|
||||
case 'point':
|
||||
return { type: 'Point' };
|
||||
case 'linestring':
|
||||
return { type: 'LineString' };
|
||||
case 'polygon':
|
||||
return { type: 'Polygon' };
|
||||
case 'multipoint':
|
||||
return { type: 'MultiPoint' };
|
||||
case 'multilinestring':
|
||||
return { type: 'MultiLineString' };
|
||||
case 'multipolygon':
|
||||
return { type: 'MultiPolygon' };
|
||||
default:
|
||||
return { type: dataTypeName };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
readUInt8() {
|
||||
return this.buf[this.pos++];
|
||||
}
|
||||
|
||||
readUInt16() {
|
||||
return this.buf[this.pos++] + (this.buf[this.pos++] << 8);
|
||||
}
|
||||
|
||||
readUInt24() {
|
||||
return this.buf[this.pos++] + (this.buf[this.pos++] << 8) + (this.buf[this.pos++] << 16);
|
||||
}
|
||||
|
||||
readUInt32() {
|
||||
return (
|
||||
this.buf[this.pos++] +
|
||||
(this.buf[this.pos++] << 8) +
|
||||
(this.buf[this.pos++] << 16) +
|
||||
this.buf[this.pos++] * 0x1000000
|
||||
);
|
||||
}
|
||||
|
||||
readInt32() {
|
||||
return (
|
||||
this.buf[this.pos++] +
|
||||
(this.buf[this.pos++] << 8) +
|
||||
(this.buf[this.pos++] << 16) +
|
||||
(this.buf[this.pos++] << 24)
|
||||
);
|
||||
}
|
||||
|
||||
readInt32LE() {
|
||||
return (
|
||||
(this.buf[this.pos++] << 24) +
|
||||
(this.buf[this.pos++] << 16) +
|
||||
(this.buf[this.pos++] << 8) +
|
||||
this.buf[this.pos++]
|
||||
);
|
||||
}
|
||||
|
||||
readInt64() {
|
||||
// could use readBigInt64LE when support would be 10.20+
|
||||
const val =
|
||||
this.buf[this.pos + 4] +
|
||||
this.buf[this.pos + 5] * 2 ** 8 +
|
||||
this.buf[this.pos + 6] * 2 ** 16 +
|
||||
(this.buf[this.pos + 7] << 24);
|
||||
const vv =
|
||||
(BigInt(val) << BigInt(32)) +
|
||||
BigInt(
|
||||
this.buf[this.pos] +
|
||||
this.buf[this.pos + 1] * 2 ** 8 +
|
||||
this.buf[this.pos + 2] * 2 ** 16 +
|
||||
this.buf[this.pos + 3] * 2 ** 24
|
||||
);
|
||||
this.pos += 8;
|
||||
return vv;
|
||||
}
|
||||
|
||||
readUnsignedLength() {
|
||||
const type = this.buf[this.pos++] & 0xff;
|
||||
switch (type) {
|
||||
case 0xfb:
|
||||
return null;
|
||||
case 0xfc:
|
||||
return this.readUInt16();
|
||||
case 0xfd:
|
||||
return this.readUInt24();
|
||||
case 0xfe:
|
||||
// limitation to BigInt signed value
|
||||
return Number(this.readInt64());
|
||||
default:
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
readBuffer(len) {
|
||||
this.pos += len;
|
||||
return this.buf.slice(this.pos - len, this.pos);
|
||||
}
|
||||
|
||||
readBufferRemaining() {
|
||||
let b = this.buf.slice(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.slice(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('utf8', initialPosition, initialPosition + cnt);
|
||||
}
|
||||
|
||||
readSignedLength() {
|
||||
const type = this.buf[this.pos++];
|
||||
switch (type) {
|
||||
case 0xfb:
|
||||
return null;
|
||||
case 0xfc:
|
||||
return this.readUInt16();
|
||||
case 0xfd:
|
||||
return this.readUInt24();
|
||||
case 0xfe:
|
||||
return Number(this.readInt64());
|
||||
default:
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
readSignedLengthBigInt() {
|
||||
const type = this.buf[this.pos++];
|
||||
switch (type) {
|
||||
case 0xfb:
|
||||
return null;
|
||||
case 0xfc:
|
||||
return BigInt(this.readUInt16());
|
||||
case 0xfd:
|
||||
return BigInt(this.readUInt24());
|
||||
case 0xfe:
|
||||
return this.readInt64();
|
||||
default:
|
||||
return BigInt(type);
|
||||
}
|
||||
}
|
||||
|
||||
readAsciiStringLengthEncoded() {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) return null;
|
||||
this.pos += len;
|
||||
return this.buf.toString('ascii', this.pos - len, this.pos);
|
||||
}
|
||||
|
||||
readStringLengthEncoded(encoding) {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) return null;
|
||||
|
||||
this.pos += len;
|
||||
if (Buffer.isEncoding(encoding)) {
|
||||
return this.buf.toString(encoding, this.pos - len, this.pos);
|
||||
}
|
||||
return Iconv.decode(this.buf.slice(this.pos - len, this.pos), encoding);
|
||||
}
|
||||
|
||||
readLongLengthEncoded(supportBigInt, supportBigNumbers, bigNumberStrings, unsigned) {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) return null;
|
||||
|
||||
if (supportBigInt) {
|
||||
const str = this.buf.toString('ascii', this.pos, this.pos + len);
|
||||
this.pos += len;
|
||||
return BigInt(str);
|
||||
}
|
||||
|
||||
let result = 0;
|
||||
let negate = false;
|
||||
let begin = this.pos;
|
||||
|
||||
//minus sign
|
||||
if (len > 0 && this.buf[begin] === 45) {
|
||||
negate = true;
|
||||
begin++;
|
||||
}
|
||||
for (; begin < this.pos + len; begin++) {
|
||||
result = result * 10 + (this.buf[begin] - 48);
|
||||
}
|
||||
|
||||
let val = negate ? -1 * result : result;
|
||||
this.pos += len;
|
||||
|
||||
if (!Number.isSafeInteger(val)) {
|
||||
const str = this.buf.toString('ascii', this.pos - len, this.pos);
|
||||
if (bigNumberStrings) return str;
|
||||
if (supportBigNumbers) {
|
||||
return Long.fromString(str, unsigned, 10);
|
||||
}
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
readDecimalLengthEncoded(bigNumberStrings) {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) return null;
|
||||
|
||||
this.pos += len;
|
||||
let str = this.buf.toString('ascii', this.pos - len, this.pos);
|
||||
return bigNumberStrings ? str : +str;
|
||||
}
|
||||
|
||||
readDate() {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) 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]);
|
||||
}
|
||||
|
||||
readDateTime(opts) {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) 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;
|
||||
|
||||
if (opts.tz) {
|
||||
return new Date(
|
||||
moment.tz(str, opts.tz).clone().tz(opts.localTz).format('YYYY-MM-DD HH:mm:ss.SSSSSS')
|
||||
);
|
||||
}
|
||||
return new Date(str);
|
||||
}
|
||||
|
||||
readIntLengthEncoded() {
|
||||
const len = this.readUnsignedLength();
|
||||
if (len === null) return null;
|
||||
|
||||
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++] & 0xff;
|
||||
switch (type) {
|
||||
case 251:
|
||||
return;
|
||||
case 252:
|
||||
this.pos +=
|
||||
2 + (0xffff & ((this.buf[this.pos] & 0xff) + ((this.buf[this.pos + 1] & 0xff) << 8)));
|
||||
return;
|
||||
case 253:
|
||||
this.pos +=
|
||||
3 +
|
||||
(0xffffff &
|
||||
((this.buf[this.pos] & 0xff) +
|
||||
((this.buf[this.pos + 1] & 0xff) << 8) +
|
||||
((this.buf[this.pos + 2] & 0xff) << 16)));
|
||||
return;
|
||||
case 254:
|
||||
this.pos +=
|
||||
8 +
|
||||
((this.buf[this.pos] & 0xff) +
|
||||
((this.buf[this.pos + 1] & 0xff) << 8) +
|
||||
((this.buf[this.pos + 2] & 0xff) << 16) +
|
||||
((this.buf[this.pos + 3] & 0xff) << 24) +
|
||||
((this.buf[this.pos + 4] & 0xff) << 32) +
|
||||
((this.buf[this.pos + 5] & 0xff) << 40) +
|
||||
((this.buf[this.pos + 6] & 0xff) << 48) +
|
||||
((this.buf[this.pos + 7] & 0xff) << 56));
|
||||
return;
|
||||
default:
|
||||
this.pos += type;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
positionFromEnd(num) {
|
||||
this.pos = this.end - num;
|
||||
}
|
||||
|
||||
/**
|
||||
* For testing purpose only
|
||||
*/
|
||||
_toBuf() {
|
||||
return this.buf.slice(this.pos, this.end);
|
||||
}
|
||||
|
||||
forceOffset(off) {
|
||||
this.pos = off;
|
||||
}
|
||||
|
||||
length() {
|
||||
return this.end - this.pos;
|
||||
}
|
||||
|
||||
subPacketLengthEncoded() {
|
||||
const len = this.readUnsignedLength();
|
||||
this.skip(len);
|
||||
return new Packet(this.buf, this.pos - len, this.pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 errorCode = this.readUInt16();
|
||||
let sqlState = '';
|
||||
|
||||
if (this.peek() === 0x23) {
|
||||
this.skip(6);
|
||||
sqlState = this.buf.toString('utf8', this.pos - 5, this.pos);
|
||||
}
|
||||
|
||||
let msg = this.buf.toString('utf8', this.pos, this.end);
|
||||
if (sql) msg += '\n' + sql;
|
||||
let fatal = sqlState.startsWith('08') || sqlState === '70100';
|
||||
if (fatal) {
|
||||
const packetMsgs = info.getLastPackets();
|
||||
if (packetMsgs !== '')
|
||||
return Errors.createError(
|
||||
msg + '\nlast received packets:\n' + packetMsgs,
|
||||
fatal,
|
||||
info,
|
||||
sqlState,
|
||||
errorCode,
|
||||
stack
|
||||
);
|
||||
}
|
||||
return Errors.createError(msg, fatal, info, sqlState, errorCode, stack);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Packet;
|
||||
481
node_modules/mariadb/lib/io/rewrite-packet.js
generated
vendored
Normal file
481
node_modules/mariadb/lib/io/rewrite-packet.js
generated
vendored
Normal file
@@ -0,0 +1,481 @@
|
||||
'use strict';
|
||||
|
||||
const Iconv = require('iconv-lite');
|
||||
|
||||
const QUOTE = 0x27;
|
||||
const DBL_QUOTE = 0x22;
|
||||
const ZERO_BYTE = 0x00;
|
||||
const SLASH = 0x5c;
|
||||
|
||||
const SMALL_BUFFER_SIZE = 1024;
|
||||
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 = /[\0\"\'\\]/g; // eslint-disable-line no-control-regex
|
||||
|
||||
/**
|
||||
* Packet splitter.
|
||||
*
|
||||
* The servers have a limit max_allowed_packet which limits the size of the data sent, to avoid saturating the server in memory.
|
||||
*
|
||||
* The following implementation has a workaround that will rewrite the command and separate the send according to this value.
|
||||
* This implies that this command can send multiple commands, with some tricks for sequencing packets.
|
||||
*
|
||||
*/
|
||||
class ReWritePacket {
|
||||
constructor(maxAllowedPacket, out, initString, endString) {
|
||||
this.out = out;
|
||||
this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
this.pos = 4;
|
||||
this.initStr = initString;
|
||||
this.endStr = endString;
|
||||
this.encoding = out.encoding;
|
||||
this.endStrLength = Buffer.byteLength(this.endStr, this.encoding);
|
||||
this.waitingResponseNo = 0;
|
||||
this.singleQuery = false;
|
||||
this.haveErrorResponse = false;
|
||||
|
||||
if (this.encoding === 'utf8') {
|
||||
this.writeString = this.writeDefaultBufferString;
|
||||
this.writeStringEscapeQuote = this.writeUtf8StringEscapeQuote;
|
||||
} else if (Buffer.isEncoding(this.encoding)) {
|
||||
this.writeString = this.writeDefaultBufferString;
|
||||
this.writeStringEscapeQuote = this.writeDefaultStringEscapeQuote;
|
||||
} else {
|
||||
this.writeString = this.writeDefaultIconvString;
|
||||
this.writeStringEscapeQuote = this.writeDefaultStringEscapeQuote;
|
||||
}
|
||||
this.maxAllowedPacket = maxAllowedPacket;
|
||||
if (maxAllowedPacket) {
|
||||
this.maxPacketSize = Math.min(MAX_BUFFER_SIZE, maxAllowedPacket) - this.endStrLength;
|
||||
} else this.maxPacketSize = 4194304 - this.endStrLength;
|
||||
|
||||
this.buf[this.pos++] = 0x03;
|
||||
this.writeString(this.initStr);
|
||||
}
|
||||
|
||||
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 newCapacity = MAX_BUFFER_SIZE;
|
||||
|
||||
if (newCapacity > this.maxPacketSize && this.markPos) {
|
||||
this.flush(false, len);
|
||||
return true;
|
||||
}
|
||||
let newBuf = Buffer.allocUnsafe(Math.min(newCapacity));
|
||||
this.buf.copy(newBuf, 0, 0, this.pos);
|
||||
this.buf = newBuf;
|
||||
return false;
|
||||
}
|
||||
|
||||
writeInt8(value) {
|
||||
let flushed = false;
|
||||
if (this.pos + 1 + this.endStrLength >= this.buf.length) {
|
||||
if (this.buf.length < MAX_BUFFER_SIZE) {
|
||||
flushed = this.growBuffer(1);
|
||||
} else {
|
||||
this.flush(false, 1);
|
||||
this.buf[this.pos++] = value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
this.buf[this.pos++] = value;
|
||||
return flushed;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 + this.endStrLength)) {
|
||||
let strBuf = Buffer.from(str, 'ascii');
|
||||
return this.writeBuffer(strBuf, 0, strBuf.length);
|
||||
}
|
||||
|
||||
for (let off = 0; off < len; ) {
|
||||
this.buf[this.pos++] = str.charCodeAt(off++);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
writeUtf8StringEscapeQuote(str) {
|
||||
const charsLength = str.length;
|
||||
|
||||
//not enough space remaining
|
||||
if (charsLength * 3 + 2 >= this.buf.length - (this.pos + this.endStrLength)) {
|
||||
let flushed;
|
||||
const arr = Buffer.from(str, 'utf8');
|
||||
flushed = this.writeInt8(QUOTE);
|
||||
flushed = this.writeBufferEscape(arr) || flushed;
|
||||
flushed = this.writeInt8(QUOTE) || flushed;
|
||||
return flushed;
|
||||
}
|
||||
|
||||
//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;
|
||||
return false;
|
||||
}
|
||||
|
||||
writeDefaultIconvString(str) {
|
||||
let buf = Iconv.encode(str, this.encoding);
|
||||
return this.writeBuffer(buf, 0, buf.length);
|
||||
}
|
||||
|
||||
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.endStrLength)) {
|
||||
this.pos += this.buf.write(str, this.pos, this.encoding);
|
||||
return false;
|
||||
}
|
||||
|
||||
//checking real length
|
||||
let flushed = false;
|
||||
let byteLength = Buffer.byteLength(str, this.encoding);
|
||||
if (byteLength > this.buf.length - (this.pos + this.endStrLength)) {
|
||||
if (this.buf.length < MAX_BUFFER_SIZE) {
|
||||
flushed = this.growBuffer(byteLength);
|
||||
}
|
||||
if (byteLength > this.buf.length - (this.pos + this.endStrLength)) {
|
||||
//not enough space in buffer, will stream :
|
||||
let strBuf = Buffer.from(str, this.encoding);
|
||||
flushed = this.writeBuffer(strBuf, 0, strBuf.length) || flushed;
|
||||
return flushed;
|
||||
}
|
||||
}
|
||||
this.pos += this.buf.write(str, this.pos, this.encoding);
|
||||
return flushed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameters need to be properly escaped :
|
||||
* following characters are to be escaped by "\" :
|
||||
* - \0
|
||||
* - \\
|
||||
* - \'
|
||||
* - \"
|
||||
* regex split part of string writing part, and escaping special char.
|
||||
* Those chars are <= 7f meaning that this will work even with multi-byte encoding
|
||||
*
|
||||
* @param str string to escape.
|
||||
*/
|
||||
writeDefaultStringEscapeQuote(str) {
|
||||
let flushed = this.writeInt8(QUOTE);
|
||||
let match;
|
||||
let lastIndex = 0;
|
||||
while ((match = CHARS_GLOBAL_REGEXP.exec(str)) !== null) {
|
||||
flushed = this.writeString(str.slice(lastIndex, match.index)) || flushed;
|
||||
flushed = this.writeInt8(SLASH) || flushed;
|
||||
flushed = this.writeInt8(match[0].charCodeAt(0)) || flushed;
|
||||
lastIndex = CHARS_GLOBAL_REGEXP.lastIndex;
|
||||
}
|
||||
|
||||
if (lastIndex === 0) {
|
||||
// Nothing was escaped
|
||||
flushed = this.writeString(str) || flushed;
|
||||
flushed = this.writeInt8(QUOTE) || flushed;
|
||||
return flushed;
|
||||
}
|
||||
|
||||
if (lastIndex < str.length) {
|
||||
flushed = this.writeString(str.slice(lastIndex)) || flushed;
|
||||
}
|
||||
flushed = this.writeInt8(QUOTE) || flushed;
|
||||
return flushed;
|
||||
}
|
||||
|
||||
writeBufferEscape(val) {
|
||||
let flushed = false;
|
||||
let valLen = val.length;
|
||||
if (valLen * 2 > this.buf.length - (this.pos + this.endStrLength)) {
|
||||
//makes buffer bigger (up to 16M)
|
||||
if (this.buf.length < MAX_BUFFER_SIZE) flushed = 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 + this.endStrLength)) {
|
||||
//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.flush(false, (valLen - i) * 2);
|
||||
this.buf[this.pos++] = SLASH; //add escape slash
|
||||
}
|
||||
if (this.pos >= this.buf.length) this.flush(false, (valLen - i) * 2);
|
||||
this.buf[this.pos++] = val[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
//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];
|
||||
}
|
||||
return flushed;
|
||||
}
|
||||
|
||||
writeBuffer(arr, off, len) {
|
||||
let flushed = false;
|
||||
if (len > this.buf.length - (this.pos + this.endStrLength)) {
|
||||
if (this.buf.length < MAX_BUFFER_SIZE) flushed = this.growBuffer(len);
|
||||
|
||||
//max buffer size
|
||||
if (len > this.buf.length - (this.pos + this.endStrLength)) {
|
||||
//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 flushed;
|
||||
this.flush(false, remainingLen);
|
||||
flushed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
arr.copy(this.buf, this.pos, off, off + len);
|
||||
this.pos += len;
|
||||
return flushed;
|
||||
}
|
||||
|
||||
mark(isLast) {
|
||||
let flushed = false;
|
||||
if (this.singleQuery) {
|
||||
//end of big query that is more than 16M
|
||||
//write single one
|
||||
flushed = this.writeString(this.endStr);
|
||||
|
||||
if (!this.haveErrorResponse) {
|
||||
const packetSendSize =
|
||||
this.pos +
|
||||
(this.singleQuerySequenceNo != undefined
|
||||
? (this.singleQuerySequenceNo + 1) * MAX_BUFFER_SIZE
|
||||
: 0);
|
||||
if (this.maxAllowedPacket && packetSendSize > this.maxAllowedPacket) {
|
||||
console.log(
|
||||
"will send a packet to db server with size > connection option 'maxAllowedPacket' (size send is " +
|
||||
packetSendSize +
|
||||
') connection might be reset by server'
|
||||
);
|
||||
}
|
||||
this.copyAndFlush(true);
|
||||
flushed = true;
|
||||
this.markPos = undefined;
|
||||
}
|
||||
|
||||
this.singleQuerySequenceNo = undefined;
|
||||
this.singleQueryCompressSequenceNo = undefined;
|
||||
this.singleQuery = false;
|
||||
this.buf[this.pos++] = 0x03;
|
||||
this.writeString(this.initStr);
|
||||
this.markPos = undefined;
|
||||
} else {
|
||||
if (this.markPos && this.pos + this.endStrLength > this.maxPacketSize) {
|
||||
//not enough room for current query, flush mark.
|
||||
this.flushMark();
|
||||
flushed = true;
|
||||
}
|
||||
//just mark ending query
|
||||
this.markPos = this.pos;
|
||||
if (isLast) {
|
||||
this.flushMark();
|
||||
flushed = true;
|
||||
}
|
||||
if (!isLast) flushed = this.writeStringAscii(',') || flushed;
|
||||
}
|
||||
return flushed;
|
||||
}
|
||||
|
||||
flush(end, remainingLen) {
|
||||
if (this.markPos && !this.singleQuery) {
|
||||
this.flushMark();
|
||||
} else {
|
||||
//one insert is more than 16M, will continue to mono insert, hoping
|
||||
//that max_allowed_packet is sized accordingly to query.
|
||||
if (this.buf.length < MAX_BUFFER_SIZE) {
|
||||
//in this case, connector has default to 4M packet, and a single query size
|
||||
//is > to 4mb. growing buffer to 16M
|
||||
let newBuf = Buffer.allocUnsafe(MAX_BUFFER_SIZE);
|
||||
this.buf.copy(newBuf, 0, 0, this.pos);
|
||||
this.buf = newBuf;
|
||||
} else {
|
||||
if (!this.haveErrorResponse) {
|
||||
if (this.maxAllowedPacket && this.buf.length > this.maxAllowedPacket) {
|
||||
console.log(
|
||||
"will send a packet to server with size > connection option 'maxAllowedPacket' (size send is " +
|
||||
this.pos +
|
||||
') connection might be reset by server'
|
||||
);
|
||||
}
|
||||
this.copyAndFlush(false);
|
||||
|
||||
this.markPos = undefined;
|
||||
if (!this.singleQuery) this.waitingResponseNo++;
|
||||
this.singleQuery = true;
|
||||
this.singleQuerySequenceNo = this.out.cmd.sequenceNo;
|
||||
this.singleQueryCompressSequenceNo = this.out.cmd.compressSequenceNo;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
flushMark() {
|
||||
let afterMark;
|
||||
if (this.pos !== this.markPos) {
|
||||
//remove "," character
|
||||
afterMark = Buffer.allocUnsafe(this.pos - this.markPos - 1);
|
||||
this.buf.copy(afterMark, 0, this.markPos + 1, this.pos);
|
||||
}
|
||||
|
||||
this.pos = this.markPos;
|
||||
this.writeString(this.endStr);
|
||||
|
||||
if (!this.haveErrorResponse) {
|
||||
this.copyAndFlush(true);
|
||||
this.waitingResponseNo++;
|
||||
}
|
||||
|
||||
this.pos = 4;
|
||||
this.buf[this.pos++] = 0x03;
|
||||
this.writeString(this.initStr);
|
||||
this.markPos = undefined;
|
||||
if (afterMark) {
|
||||
if (this.buf.length - this.pos < afterMark.length)
|
||||
this.growBuffer(afterMark.length - (this.buf.length - this.pos));
|
||||
afterMark.copy(this.buf, this.pos, 0, afterMark.length);
|
||||
this.pos += afterMark.length;
|
||||
}
|
||||
this.singleQuery = false;
|
||||
this.singleQuerySequenceNo = undefined;
|
||||
this.singleQueryCompressSequenceNo = undefined;
|
||||
}
|
||||
|
||||
copyAndFlush(ended) {
|
||||
this.out.buf = this.buf;
|
||||
this.out.pos = this.pos;
|
||||
if (this.singleQuerySequenceNo != undefined) {
|
||||
this.out.cmd.sequenceNo = this.singleQuerySequenceNo;
|
||||
this.out.cmd.compressSequenceNo = this.singleQueryCompressSequenceNo;
|
||||
} else {
|
||||
this.out.cmd.sequenceNo = -1;
|
||||
this.out.cmd.compressSequenceNo = -1;
|
||||
}
|
||||
this.out.flushBuffer(ended);
|
||||
if (this.singleQuerySequenceNo != undefined) {
|
||||
this.singleQuerySequenceNo = this.out.cmd.sequenceNo;
|
||||
this.singleQueryCompressSequenceNo = this.out.cmd.compressSequenceNo;
|
||||
}
|
||||
this.pos = 4;
|
||||
this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
endedWithError() {
|
||||
this.haveErrorResponse = true;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ReWritePacket;
|
||||
Reference in New Issue
Block a user