diff --git a/lib/client.js b/lib/client.js index 4dfe893c..7f9b74e4 100644 --- a/lib/client.js +++ b/lib/client.js @@ -5,6 +5,7 @@ var EventEmitter = require('events').EventEmitter; var utils = require(__dirname + '/utils'); var BufferList = require(__dirname + '/buffer-list'); +var Connection = require(__dirname + '/connection'); var Client = function(config) { EventEmitter.call(this); @@ -14,6 +15,7 @@ var Client = function(config) { this.port = config.port || 5432; this.host = config.host; this.queryQueue = []; + this.connection = new Connection(); this.stream = config.stream || new net.Stream(); this.queryQueue = []; this.password = config.password || ''; @@ -29,33 +31,10 @@ sys.inherits(Client, EventEmitter); var p = Client.prototype; p.connect = function() { - if(this.stream.readyState === 'closed'){ - this.stream.connect(this.port, this.host); - } var self = this; - this.stream.on('connect', function() { - var data = ['user',self.user,'database', self.database, '\0'].join('\0'); - var byteLength = Buffer.byteLength(data); - var fullBuffer = new Buffer(byteLength + 4); - fullBuffer[0] = 0; - fullBuffer[1] = 3; - fullBuffer[2] = 0; - fullBuffer[3] = 0; - fullBuffer.write(data, 4); - self.send(null, fullBuffer); - }); - - this.stream.on('data', function(buffer) { - self.setBuffer(buffer); - var msg; - while(msg = self.parseMessage()) { - self.emit('message', msg); - self.emit(msg.name, msg); - } - }); - - this.on('authenticationCleartextPassword', function() { - self.send('p', new Buffer(self.password + '\0', self.encoding)); + var con = this.connection; + con.on('authenticationCleartextPassword', function() { + con.passwordMessage(this.password); }); this.on('authenticationMD5Password', function(msg) { diff --git a/lib/connection.js b/lib/connection.js index 7201a980..b161080f 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -56,6 +56,10 @@ p.startupMessage = function(config) { this.send(false, buffer.join()); }; +p.passwordMessage = function(password) { + this.send('p', Buffer(password + '\0', this.encoding)); +}; + p.send = function(code, bodyBuffer) { var length = bodyBuffer.length + 4; var buffer = Buffer(length + (code ? 1 : 0)); diff --git a/test/unit/client/authentication-tests.js b/test/unit/client/authentication-tests.js index 80ba98b4..85e43bd8 100644 --- a/test/unit/client/authentication-tests.js +++ b/test/unit/client/authentication-tests.js @@ -6,20 +6,7 @@ test('password authentication', function(){ var client = createClient(); client.password = "!"; - var clearTextPasswordBuffer = Buffer([0x52, 0, 0, 0, 8, 0, 0, 0, 3]); - - var raised = false; - - client.on('authenticationCleartextPassword', function() { - raised = true; - }); - - client.stream.emit('data', clearTextPasswordBuffer); - - test('raises event', function() { - assert.ok(raised); - }); - + client.connection.emit('authenticationCleartextPassword'); test('responds with password', function() { assert.length(client.stream.packets, 1); var packet = client.stream.packets[0]; diff --git a/test/unit/connection/outbound-sending-tests.js b/test/unit/connection/outbound-sending-tests.js index 7b142ef1..b6c1503f 100644 --- a/test/unit/connection/outbound-sending-tests.js +++ b/test/unit/connection/outbound-sending-tests.js @@ -25,6 +25,11 @@ test("sends startup message", function() { .addCString('').join(true)) }); +test('sends passwordMessage', function() { + con.passwordMessage("!"); + assert.recieved(stream, new BufferList().addCString("!").join(true,'p')); +}); + test('sends query message', function() { var txt = 'select * from boom'; con.query(txt);