refactoring

This commit is contained in:
brianc 2010-10-20 00:34:16 -05:00
parent c923ae03a5
commit 21879edc24

View File

@ -14,6 +14,11 @@ var Client = function(config) {
this.stream = config.stream || new net.Stream();
this.queryQueue = [];
this.password = config.password || '';
this.lastBuffer = false;
this.lastOffset = 0;
this.buffer = null;
this.offset = null;
this.encoding = 'utf8';
};
sys.inherits(Client, EventEmitter);
@ -27,29 +32,24 @@ p.connect = function() {
var self = this;
this.stream.on('connect', function() {
var data = ['user',self.user,'database', self.database, '\0'].join('\0');
var dataBuffer = Buffer(data,'utf8');
var dataBuffer = Buffer(data, self.encoding);
var fullBuffer = Buffer(4 + dataBuffer.length);
fullBuffer[0] = 0;
fullBuffer[1] = 3;
fullBuffer[2] = 0;
fullBuffer[3] = 0;
self.writeInt32(fullBuffer, 0, 0x03);//write protocol version
dataBuffer.copy(fullBuffer, 4, 0);
self.send(null, fullBuffer);
});
this.stream.on('data', function(buffer) {
self.setBuffer(buffer);
var msg = self.parseMessage();
while(msg) {
var msg;
while(msg = self.parseMessage()) {
self.emit('message', msg);
self.emit(msg.name, msg);
msg = self.parseMessage();
}
});
this.on('authenticationCleartextPassword', function() {
var stringBuffer = new Buffer(self.password + '\0', 'utf8');
self.send('p', stringBuffer);
self.send('p', new Buffer(self.password + '\0', self.encoding));
});
this.on('readyForQuery', function() {
@ -63,16 +63,20 @@ p.send = function(code, bodyBuffer) {
var buffer = Buffer(length + (code ? 1 : 0));
var offset = 0;
if(code) {
buffer[offset++] = Buffer(code,'utf8') [0];
buffer[offset++] = Buffer(code, this.encoding) [0];
}
buffer[offset++] = length >>> 24 & 0xFF;
buffer[offset++] = length >>> 16 & 0xFF;
buffer[offset++] = length >>> 8 & 0xFF;
buffer[offset++] = length >>> 0 & 0xFF;
bodyBuffer.copy(buffer, offset, 0);
this.writeInt32(buffer, offset, length);
bodyBuffer.copy(buffer, offset+4, 0);
return this.stream.write(buffer);
};
p.writeInt32 = function(buffer, offset, value) {
buffer[offset++] = value >>> 24 & 0xFF;
buffer[offset++] = value >>> 16 & 0xFF;
buffer[offset++] = value >>> 8 & 0xFF;
buffer[offset++] = value >>> 0 & 0xFF;
};
p.disconnect = function() {
var terminationBuffer = new Buffer([88,0,0,0,4]);
this.stream.write(terminationBuffer);
@ -86,7 +90,7 @@ p.query = function(text) {
return query;
};
p.pulseQueryQueue = function() {
p.pulseQueryQueue = function(ready) {
if(!this.readyForQuery) {
return;
};
@ -257,7 +261,7 @@ p.parseE = function(msg) {
};
p.readChar = function() {
return Buffer([this.buffer[this.offset++]]).toString('utf8');
return Buffer([this.buffer[this.offset++]]).toString(this.encoding);
};
p.parseInt32 = function() {
@ -282,13 +286,13 @@ p.parseInt16 = function() {
};
p.readString = function(length) {
return this.buffer.toString('utf8', this.offset, (this.offset += length));
return this.buffer.toString(this.encoding, this.offset, (this.offset += length));
};
p.parseCString = function() {
var start = this.offset;
while(this.buffer[this.offset++]) { };
return this.buffer.toString('utf8',start, this.offset - 1);
return this.buffer.toString(this.encoding, start, this.offset - 1);
};
//end parsing methods
module.exports = Client;