node-postgres/lib/index.js

112 lines
2.5 KiB
JavaScript
Raw Normal View History

var EventEmitter = require('events').EventEmitter;
var sys = require('sys');
var net = require('net');
var NUL = '\0';
2010-09-29 13:08:53 +08:00
var chars = Buffer('RS','utf8');
var UTF8 = {
2010-09-29 13:08:53 +08:00
R: chars[0],
S: chars[1]
};
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);
});
};
2010-09-29 13:30:35 +08:00
var Parser = function(buffer) {
2010-09-29 13:20:10 +08:00
this.offset = 0;
2010-09-29 13:30:35 +08:00
this.buffer = buffer;
2010-09-29 13:12:04 +08:00
};
2010-09-29 13:12:04 +08:00
var p = Parser.prototype;
2010-09-29 13:20:10 +08:00
p.parse = function(buffer) {
2010-09-29 13:30:35 +08:00
switch(this.buffer[this.offset]) {
2010-09-29 13:12:04 +08:00
case UTF8.R:
2010-09-29 13:30:35 +08:00
return this.parseR();
2010-09-29 13:12:04 +08:00
case UTF8.S:
2010-09-29 13:30:35 +08:00
return this.parseS();
2010-09-29 13:12:04 +08:00
default:
throw new Error("Unsupported message ID");
}
};
2010-09-29 13:30:35 +08:00
p.parseR = function() {
var type = this.buffer[this.offset++];
var length = this.parseLength();
2010-09-29 13:12:04 +08:00
if(length == 8) {
2010-09-29 13:08:53 +08:00
return {
2010-09-29 13:12:04 +08:00
name: 'AuthenticationOk',
id: 'R',
length: length
2010-09-29 13:08:53 +08:00
}
}
2010-09-29 13:12:04 +08:00
throw new Error("Unknown AuthenticatinOk message type");
};
2010-09-29 13:20:10 +08:00
p.parseS = function(buffer) {
2010-09-29 13:30:35 +08:00
var type = this.buffer[this.offset++];
var length = this.parseLength(this.buffer);
var parameterName = this.parseCString();
var parameterValue = this.parseCString();
2010-09-29 13:12:04 +08:00
return {
name: 'ParameterStatus',
id: 'S',
length: length,
parameterName: parameterName,
parameterValue: parameterValue
}
};
2010-09-29 13:30:35 +08:00
p.parseLength = function() {
var buffer = this.buffer;
return ((buffer[this.offset++] << 24) +
(buffer[this.offset++] << 16) +
(buffer[this.offset++] << 8) +
buffer[this.offset++]);
};
2010-09-29 13:20:10 +08:00
p.parseCString = function(buffer) {
var start = this.offset;
2010-09-29 13:30:35 +08:00
while(this.buffer[this.offset++]) { };
return this.buffer.toString('utf8',start, this.offset - 1);
2010-09-29 13:20:10 +08:00
};
module.exports = {
Client: Client,
Parser: Parser
};