totally thrashing at this point, but moving forward

This commit is contained in:
brianc 2010-09-28 23:18:46 -05:00
parent cf637b08b7
commit 18e6ec2121
4 changed files with 132 additions and 0 deletions

79
lib/index.js Normal file
View File

@ -0,0 +1,79 @@
var EventEmitter = require('events').EventEmitter;
var sys = require('sys');
var net = require('net');
var NUL = '\0';
var chars = Buffer('R','utf8');
var UTF8 = {
R: chars[0]
};
var Client = function(config) {
EventEmitter.call(this);
config = config || {};
this.user = config.user;
this.database = config.database;
this.port = config.port || 5432;
};
sys.inherits(Client, EventEmitter);
Client.prototype.connect = function() {
var con = net.createConnection(this.port);
var self = this;
con.on('connect', function() {
var data = ['user',self.user,'database', self.database,NUL].join(NUL);
var dataBuffer = Buffer(data);
var fullBuffer = Buffer(8 + dataBuffer.length);
fullBuffer[0] = fullBuffer.length >>> 24;
fullBuffer[1] = fullBuffer.length >>> 16;
fullBuffer[2] = fullBuffer.length >>> 8;
fullBuffer[3] = fullBuffer.length >>> 0;
fullBuffer[4] = 0;
fullBuffer[5] = 3;
fullBuffer[6] = 0;
fullBuffer[7] = 0;
fullBuffer.write(data,8);
console.log(fullBuffer);
con.write(fullBuffer);
});
con.on('data', function(data) {
console.log('data!');
console.log(data);
});
};
var Parser = {
parse: function(buffer, offset) {
offset = offset || 0;
switch(buffer[offset]) {
case UTF8.R:
return Parser.parseR(buffer, offset);
default:
throw "Unsupported message id"
}
},
parseR: function(buffer, offset) {
var type = buffer[offset++];
var length = ((buffer[offset++] << 24) +
(buffer[offset++] << 16) +
(buffer[offset++] << 8) +
buffer[offset++]);
if(length == 8) {
return {
name: 'AuthenticationOk',
id: 'R',
length: length
}
}
}
};
module.exports = {
Client: Client,
Parser: Parser
};

26
test/api-tests.js Normal file
View File

@ -0,0 +1,26 @@
require(__dirname+"/test-helper");
//defaults
var client = new Client();
assert.equal(client.user, null);
assert.equal(client.database, null);
assert.equal(client.port, 5432);
var client = new Client({
user: 'brian',
database: 'pgjstest',
port: 321
});
assert.equal(client.user, 'brian');
assert.equal(client.database, 'pgjstest');
assert.equal(client.port, 321);
client.port = 5432;
client.connect(function() {
console.log('connected');
client.query('select count(*) from items',function(result) {
console.log('ran query');
});
});

12
test/parser-tests.js Normal file
View File

@ -0,0 +1,12 @@
require(__dirname+'/test-helper');
test('Parser on single messages', function() {
test('parses AuthenticationOk message', function() {
var result = Parser.parse(Buffer([0x52, 00, 00, 00, 08, 00, 00, 00, 00]));
assert.equal(result.name, 'AuthenticationOk');
assert.equal(result.id, 'R');
assert.equal(result.length, 8);
});
});

15
test/test-helper.js Normal file
View File

@ -0,0 +1,15 @@
sys = require('sys');
assert = require('assert');
Client = require(__dirname+"/../lib/").Client;
Parser = require(__dirname+"/../lib/").Parser;
test = function(name, action) {
for(var i = 0; i < test.tabout; i++) {
name = ' ' + name;
}
test.tabout += 2;
console.log(name);
action();
test.tabout -= 2;
};
test.tabout = 0;