Comparing version 0.1.1 to 0.1.2
@@ -17,3 +17,2 @@ var common = require('../test/common'); | ||
connection.query('INSERT INTO ' + table + ' SET title="' + text + '"', function(err, result) { | ||
debugger; | ||
if (err) throw err; | ||
@@ -20,0 +19,0 @@ if (numLeft > 1) |
@@ -42,24 +42,6 @@ var _ = require('underscore'); | ||
var connection = this; | ||
var cnt = 0; | ||
//var startTime = process.hrtime(); | ||
var handlePacket = function(packet) { | ||
if (packet) { | ||
connection._cmdBytesReceived += packet.payload.length + 4; | ||
} | ||
//if (packet) | ||
// console.log('PACKET:', packet.payload, packet.payload.length); | ||
var done = connection.commands[0].execute(packet, connection); | ||
if (done) { | ||
connection.commands.shift(); | ||
if (connection.commands.length !== 0) | ||
handlePacket(); // TODO: process.nextTick ? | ||
} | ||
}; | ||
this.packetParser = new PacketParser(handlePacket); | ||
//this.stream.pipe(this.packetParser); | ||
this.stream.resume(); | ||
var cnt1 = 0; | ||
this.packetParser = new PacketParser(this.handlePacket.bind(this)); | ||
// TODO: check stream instanceof net.Stream, fallback if not | ||
this.stream.ondata = function(data, start, end) { | ||
connection.packetParser._write(data.slice(start, end)); | ||
connection.packetParser.execute(data, start, end); | ||
}; | ||
@@ -71,8 +53,15 @@ this.addCommand(new Commands.Handshake()); | ||
packet.writeHeader(); | ||
var data = packet.payload; | ||
this.stream.write(data); | ||
//console.log('<<< ', packet.payload); | ||
//console.log('<<< ', [packet.payload.toString()]); | ||
this.stream.write(packet.buffer); | ||
}; | ||
Connection.prototype.handlePacket = function(packet) { | ||
var commands = this.commands; | ||
var done = commands[0].execute(packet, this); | ||
if (done) { | ||
commands.shift(); | ||
if (commands.length !== 0) | ||
this.handlePacket(); // TODO: process.nextTick ? | ||
} | ||
}; | ||
Connection.prototype.addCommand = function(cmd) { | ||
@@ -79,0 +68,0 @@ this.commands.push(cmd); |
@@ -1,2 +0,1 @@ | ||
var Writable = require('stream').Writable || require('readable-stream').Writable; | ||
var Packet = require('./packets/packet'); | ||
@@ -6,6 +5,4 @@ | ||
// onPayload(sequenceId, payload) is called for each mysql packet | ||
function PacketParser(onPayload) | ||
function PacketParser(onPacket) | ||
{ | ||
Writable.call(this); | ||
// array of last payload chunks | ||
@@ -24,34 +21,22 @@ // only used when corrent payload is not complete | ||
this.sequenceId = 0; | ||
this.onPayload = onPayload; | ||
this.onPacket = onPacket; | ||
} | ||
PacketParser.prototype = Object.create( | ||
Writable.prototype, { constructor: { value: PacketParser }}); | ||
PacketParser.prototype._write = function(chunk, encoding, callback) { | ||
//console.log('>>> ', chunk); | ||
//console.log('>>> ', [chunk.toString()]); | ||
//var p = new Packet(0, chunk); | ||
//console.log('==== WRITE ===', chunk.length, this.state); | ||
//console.log(chunk); | ||
var chunkOffset = 0; | ||
while (chunkOffset < chunk.length) { | ||
//console.log("===================", chunk.length, chunkOffset, this.state); | ||
//console.log(chunk.slice(chunkOffset)); | ||
if (this.state == 'head0' && (chunk.length - chunkOffset) > 3) { | ||
this.length = chunk[chunkOffset] + (chunk[chunkOffset+1] << 8) + (chunk[chunkOffset+2] << 16); | ||
this.sequenceId = chunk[chunkOffset+3]; | ||
PacketParser.prototype.execute = function(chunk, start, end) { | ||
//var length = end - start; | ||
//if (length == 0) | ||
// return; | ||
while (start < chunk.length) { | ||
if (this.state == 'head0' && (end - start) > 3) { | ||
this.length = chunk[start] + (chunk[start+1] << 8) + (chunk[start+2] << 16); | ||
this.sequenceId = chunk[start+3]; | ||
var packetLength = this.length + 4; | ||
// TODO: second case seems to be most important | ||
// for example, short select with one row result in 6 packets in one chunk | ||
// worth to swap first nd second | ||
if (chunk.length - chunkOffset == packetLength) { | ||
this.onPayload(new Packet(this.sequenceId, chunk.slice(chunkOffset + 4))); | ||
return; | ||
} else if (chunk.length - chunkOffset > packetLength) { // more than one packet in chunk | ||
this.onPayload(new Packet(this.sequenceId, chunk.slice(chunkOffset + 4, chunkOffset + packetLength))); | ||
chunkOffset += packetLength; | ||
if (end - start > packetLength) { // more than one packet in chunk | ||
this.onPacket(new Packet(this.sequenceId, chunk, start + 4, start + packetLength)); | ||
start += packetLength; | ||
} else if (end - start == packetLength) { | ||
return this.onPacket(new Packet(this.sequenceId, chunk, start + 4, start + packetLength)); | ||
} else { // payload is incomplete | ||
this.buffer = [chunk.slice(chunkOffset + 4)]; | ||
this.bufferLength = chunk.length - chunkOffset - 4; | ||
this.buffer = [chunk.slice(start + 4, end)]; | ||
this.bufferLength = end - start - 4; | ||
this.state = 'payload'; | ||
@@ -62,3 +47,3 @@ return; | ||
var remainingPayload = this.length - this.bufferLength; | ||
if (chunk.length - chunkOffset >= remainingPayload) { // last chunk for payload | ||
if (end - start >= remainingPayload) { // last chunk for payload | ||
var payload = new Buffer(this.length); | ||
@@ -70,22 +55,22 @@ var offset = 0; | ||
} | ||
chunk.copy(payload, offset, chunkOffset, chunkOffset + remainingPayload); | ||
this.onPayload(new Packet(this.sequenceId, payload)); | ||
chunk.copy(payload, offset, start, start + remainingPayload); | ||
this.onPacket(new Packet(this.sequenceId, payload, 0, this.length)); | ||
this.buffer = []; | ||
this.bufferLength = 0; | ||
chunkOffset += remainingPayload; | ||
start += remainingPayload; | ||
this.state = 'head0'; | ||
} else { | ||
this.buffer.push(chunk.slice(chunkOffset)); | ||
this.bufferLength += chunk.length - chunkOffset; | ||
this.buffer.push(chunk.slice(start, end)); | ||
this.bufferLength += end - start; | ||
return; | ||
} | ||
} else { // length < 4 or state != header0 | ||
if (chunk.length - chunkOffset + this.headerLen < 4) { | ||
chunk.copy(this.headerBuff, this.headerLen, chunkOffset); | ||
this.headerLen += chunk.length - chunkOffset; | ||
if (end - start + this.headerLen < 4) { | ||
chunk.copy(this.headerBuff, this.headerLen, start, end); | ||
this.headerLen += end - start; | ||
this.state = 'head_'; | ||
return; | ||
} | ||
chunk.copy(this.headerBuff, this.headerLen, chunkOffset, chunkOffset + 4 - this.headerLen); | ||
chunkOffset += 4 - this.headerLen; | ||
chunk.copy(this.headerBuff, this.headerLen, start, start + 4 - this.headerLen); | ||
start += 4 - this.headerLen; | ||
this.length = this.headerBuff[0] + (this.headerBuff[1] << 8) + (this.headerBuff[2] << 16); | ||
@@ -92,0 +77,0 @@ this.headerLen = 0; |
//var BigNumber = require("bignumber.js"); | ||
function Packet(id, payload) | ||
function Packet(id, buffer, start, end) | ||
{ | ||
this.sequenceId = id; | ||
this.payload = payload; | ||
this.offset = 0; | ||
this.buffer = buffer; | ||
this.offset = start; | ||
this.start = start; | ||
this.end = end; | ||
} | ||
@@ -14,4 +16,12 @@ | ||
Packet.prototype.reset = function() { | ||
this.offset = this.start; | ||
}; | ||
Packet.prototype.length = function() { | ||
return this.end - this.start; | ||
}; | ||
Packet.prototype.haveMoreData = function() { | ||
return this.payload.length > this.offset; | ||
return this.end > this.offset; | ||
}; | ||
@@ -27,3 +37,3 @@ | ||
{ | ||
return this.payload[this.offset++]; | ||
return this.buffer[this.offset++]; | ||
}; | ||
@@ -34,3 +44,3 @@ | ||
this.offset += 2; | ||
return this.payload.readUInt16LE(this.offset - 2); | ||
return this.buffer.readUInt16LE(this.offset - 2); | ||
}; | ||
@@ -41,8 +51,7 @@ | ||
this.offset += 4; | ||
return this.payload.readUInt32LE(this.offset - 4); | ||
return this.buffer.readUInt32LE(this.offset - 4); | ||
}; | ||
Packet.prototype.isEOF = function() { | ||
// TODO: fix when payload replaced with buffer | ||
return this.payload[0] == 0xfe && this.payload.length < 9; | ||
return this.buffer[this.offset] == 0xfe && this.length() < 9; | ||
}; | ||
@@ -62,3 +71,4 @@ | ||
console.trace(); | ||
throw "Implement 8bytes BigNumber"; | ||
debugger; | ||
//throw "Implement 8bytes BigNumber"; | ||
} | ||
@@ -72,3 +82,3 @@ if (byte1 == 0xfb) | ||
this.offset += len; | ||
return this.payload.slice(this.offset - len, this.offset); | ||
return this.buffer.slice(this.offset - len, this.offset); | ||
}; | ||
@@ -82,7 +92,3 @@ | ||
this.offset += len; | ||
var sb = this.payload.parent; | ||
var offset = this.payload.offset + this.offset; | ||
return sb.utf8Slice(offset - len, offset); | ||
// safe, but slow. TODO: benchmark numbers here | ||
// return this.payload.slice(this.offset - len, this.offset).toString('utf-8'); | ||
return this.buffer.utf8Slice(this.offset - len, this.offset); | ||
}; | ||
@@ -93,6 +99,6 @@ | ||
var end = this.offset; | ||
while (this.payload[end]) | ||
while (this.buffer[end]) | ||
end = end + 1; // TODO: handle OOB check | ||
this.offset = end + 1; | ||
return this.payload.toString('utf8', start, end); // TODO: encoding? | ||
return this.buffer.toString('utf8', start, end); // TODO: encoding? | ||
}; | ||
@@ -102,3 +108,3 @@ | ||
// TODO: will be changed to buffer[4] | ||
return this.payload[0] == 0xff; | ||
return this.buffer[0] == 0xff; | ||
}; | ||
@@ -110,3 +116,3 @@ | ||
var sqlState = this.readBuffer(7); | ||
var message = this.payload.slice(this.offset); | ||
var message = this.buffer.slice(this.offset); | ||
console.log('SqlState', sqlState.toString(), 'message', message.toString()); | ||
@@ -131,3 +137,3 @@ var err = new Error(message.toString()); | ||
Packet.prototype.writeInt32 = function(n) { | ||
this.payload.writeUInt32LE(n, this.offset); | ||
this.buffer.writeUInt32LE(n, this.offset); | ||
this.offset += 4; | ||
@@ -142,3 +148,3 @@ }; | ||
Packet.prototype.writeInt16 = function(n) { | ||
this.payload.writeUInt16LE(n, this.offset); | ||
this.buffer.writeUInt16LE(n, this.offset); | ||
this.offset += 2; | ||
@@ -148,3 +154,3 @@ }; | ||
Packet.prototype.writeInt8 = function(n) { | ||
this.payload[this.offset] = n; | ||
this.buffer[this.offset] = n; | ||
this.offset++; | ||
@@ -154,3 +160,3 @@ }; | ||
Packet.prototype.writeBuffer = function(b) { | ||
b.copy(this.payload, this.offset); | ||
b.copy(this.buffer, this.offset); | ||
this.offset += b.length; | ||
@@ -161,3 +167,3 @@ }; | ||
Packet.prototype.writeNullTerminatedString = function(s) { | ||
this.payload.write(s, this.offset); | ||
this.buffer.write(s, this.offset); | ||
this.offset += s.length; | ||
@@ -169,3 +175,3 @@ this.writeInt8(0); | ||
var bytes = Buffer.byteLength(s, 'utf8'); | ||
this.payload.write(s, this.offset, bytes, 'utf8'); | ||
this.buffer.write(s, this.offset, bytes, 'utf8'); | ||
this.offset += bytes; | ||
@@ -176,3 +182,3 @@ }; | ||
this.writeLengthCodedNumber(s.length); | ||
this.payload.write(s, this.offset); | ||
this.buffer.write(s, this.offset); | ||
this.offset += s.length; | ||
@@ -200,3 +206,3 @@ }; | ||
this.offset = 0; | ||
this.writeInt24(this.payload.length - 4); | ||
this.writeInt24(this.buffer.length - 4); | ||
this.writeInt8(this.sequenceId); | ||
@@ -203,0 +209,0 @@ this.offset = offset; |
@@ -10,3 +10,3 @@ //var constants = require('../constants'); | ||
TextRow.fromPacket = function(packet) { | ||
packet.offset = 0; | ||
//packet.reset(); // set offset to starting point? | ||
var columns = []; | ||
@@ -13,0 +13,0 @@ while(packet.haveMoreData()) { |
{ | ||
"name": "mysql2", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "fast mysql driver", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34559
997