2010-10-24 05:34:20 +08:00
|
|
|
var net = require('net');
|
|
|
|
var crypto = require('crypto');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2011-10-11 08:40:52 +08:00
|
|
|
var util = require('util');
|
2010-10-24 05:34:20 +08:00
|
|
|
|
|
|
|
var utils = require(__dirname + '/utils');
|
2013-03-17 00:51:26 +08:00
|
|
|
var Writer = require('buffer-writer');
|
2010-10-24 05:34:20 +08:00
|
|
|
|
|
|
|
var Connection = function(config) {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
config = config || {};
|
|
|
|
this.stream = config.stream || new net.Stream();
|
|
|
|
this.lastBuffer = false;
|
|
|
|
this.lastOffset = 0;
|
|
|
|
this.buffer = null;
|
|
|
|
this.offset = null;
|
|
|
|
this.encoding = 'utf8';
|
2010-10-29 08:09:40 +08:00
|
|
|
this.parsedStatements = {};
|
2011-01-02 02:40:45 +08:00
|
|
|
this.writer = new Writer();
|
2012-09-11 10:40:41 +08:00
|
|
|
this.ssl = config.ssl || false;
|
2013-03-29 22:38:49 +08:00
|
|
|
this._ending = false;
|
2010-10-24 05:34:20 +08:00
|
|
|
};
|
|
|
|
|
2011-10-11 08:40:52 +08:00
|
|
|
util.inherits(Connection, EventEmitter);
|
2010-10-24 05:34:20 +08:00
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.connect = function(port, host) {
|
2010-10-24 09:26:24 +08:00
|
|
|
|
2013-03-07 00:26:40 +08:00
|
|
|
if(this.stream.readyState === 'closed') {
|
2010-10-24 06:36:04 +08:00
|
|
|
this.stream.connect(port, host);
|
2013-03-07 00:26:40 +08:00
|
|
|
} else if(this.stream.readyState == 'open') {
|
2010-10-24 06:36:04 +08:00
|
|
|
this.emit('connect');
|
2010-10-24 05:34:20 +08:00
|
|
|
}
|
2010-10-24 09:26:24 +08:00
|
|
|
|
2010-10-24 05:34:20 +08:00
|
|
|
var self = this;
|
2010-10-24 06:36:04 +08:00
|
|
|
|
2010-10-24 05:34:20 +08:00
|
|
|
this.stream.on('connect', function() {
|
2010-10-24 06:36:04 +08:00
|
|
|
self.emit('connect');
|
2010-10-24 05:34:20 +08:00
|
|
|
});
|
2013-01-24 08:08:32 +08:00
|
|
|
|
2012-09-11 10:40:41 +08:00
|
|
|
this.stream.on('error', function(error) {
|
2013-03-29 22:38:49 +08:00
|
|
|
//don't raise ECONNRESET errors - they can & should be ignored
|
|
|
|
//during disconnect
|
|
|
|
if(self._ending && error.code == 'ECONNRESET') {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-11 10:40:41 +08:00
|
|
|
self.emit('error', error);
|
2012-08-07 19:51:37 +08:00
|
|
|
});
|
2010-10-24 06:36:04 +08:00
|
|
|
|
2013-03-29 02:24:33 +08:00
|
|
|
this.stream.on('end', function() {
|
|
|
|
self.emit('end');
|
|
|
|
});
|
|
|
|
|
2012-09-11 10:40:41 +08:00
|
|
|
if(this.ssl) {
|
|
|
|
this.stream.once('data', function(buffer) {
|
|
|
|
self.setBuffer(buffer);
|
|
|
|
var msg = self.readSslResponse();
|
|
|
|
self.emit('message', msg);
|
|
|
|
self.emit(msg.name, msg);
|
|
|
|
});
|
|
|
|
this.once('sslresponse', function(msg) {
|
2013-03-07 00:26:40 +08:00
|
|
|
if(msg.text == 0x53) {
|
2012-09-11 10:40:41 +08:00
|
|
|
var tls = require('tls');
|
|
|
|
self.stream.removeAllListeners();
|
2013-01-24 08:08:32 +08:00
|
|
|
self.stream = tls.connect({
|
|
|
|
socket: self.stream,
|
|
|
|
servername: host,
|
2012-12-05 04:18:19 +08:00
|
|
|
rejectUnauthorized: self.ssl.rejectUnauthorized,
|
|
|
|
ca: self.ssl.ca,
|
|
|
|
pfx: self.ssl.pfx,
|
|
|
|
key: self.ssl.key,
|
|
|
|
passphrase: self.ssl.passphrase,
|
|
|
|
cert: self.ssl.cert,
|
|
|
|
NPNProtocols: self.ssl.NPNProtocols
|
2012-09-11 10:40:41 +08:00
|
|
|
});
|
|
|
|
self.attachListeners(self.stream);
|
|
|
|
self.emit('sslconnect');
|
|
|
|
} else {
|
2013-01-24 08:08:32 +08:00
|
|
|
self.emit(
|
|
|
|
'error',
|
|
|
|
new Error("The server doesn't support SSL/TLS connections.")
|
|
|
|
);
|
2012-08-07 19:51:37 +08:00
|
|
|
}
|
2013-01-24 08:08:32 +08:00
|
|
|
});
|
2010-10-31 09:23:54 +08:00
|
|
|
|
2012-09-11 10:40:41 +08:00
|
|
|
} else {
|
|
|
|
this.attachListeners(this.stream);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.attachListeners = function(stream) {
|
2012-09-11 10:40:41 +08:00
|
|
|
var self = this;
|
|
|
|
stream.on('data', function(buffer) {
|
|
|
|
self.setBuffer(buffer);
|
2013-01-21 21:29:31 +08:00
|
|
|
var msg = self.parseMessage();
|
|
|
|
while(msg) {
|
2012-09-11 10:40:41 +08:00
|
|
|
self.emit('message', msg);
|
|
|
|
self.emit(msg.name, msg);
|
2013-01-21 21:29:31 +08:00
|
|
|
msg = self.parseMessage();
|
2012-09-11 10:40:41 +08:00
|
|
|
}
|
2010-10-31 09:23:54 +08:00
|
|
|
});
|
2010-10-24 06:36:04 +08:00
|
|
|
};
|
2010-10-24 05:34:20 +08:00
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.requestSsl = function(config) {
|
2012-08-07 19:51:37 +08:00
|
|
|
this.checkSslResponse = true;
|
2013-01-24 08:08:32 +08:00
|
|
|
|
2012-08-07 19:51:37 +08:00
|
|
|
var bodyBuffer = this.writer
|
|
|
|
.addInt16(0x04D2)
|
|
|
|
.addInt16(0x162F).flush();
|
2013-01-24 08:08:32 +08:00
|
|
|
|
2012-08-07 19:51:37 +08:00
|
|
|
var length = bodyBuffer.length + 4;
|
2013-01-24 08:08:32 +08:00
|
|
|
|
2012-08-07 19:51:37 +08:00
|
|
|
var buffer = new Writer()
|
|
|
|
.addInt32(length)
|
|
|
|
.add(bodyBuffer)
|
|
|
|
.join();
|
|
|
|
this.stream.write(buffer);
|
2013-01-21 21:29:31 +08:00
|
|
|
};
|
2012-08-07 19:51:37 +08:00
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.startup = function(config) {
|
2011-01-02 02:40:45 +08:00
|
|
|
var bodyBuffer = this.writer
|
2010-10-24 06:36:04 +08:00
|
|
|
.addInt16(3)
|
|
|
|
.addInt16(0)
|
|
|
|
.addCString('user')
|
|
|
|
.addCString(config.user)
|
|
|
|
.addCString('database')
|
|
|
|
.addCString(config.database)
|
2013-03-06 04:44:20 +08:00
|
|
|
.addCString('client_encoding')
|
|
|
|
.addCString("'utf-8'")
|
2011-01-02 02:40:45 +08:00
|
|
|
.addCString('').flush();
|
2010-11-01 07:36:35 +08:00
|
|
|
//this message is sent without a code
|
|
|
|
|
|
|
|
var length = bodyBuffer.length + 4;
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2010-11-01 07:36:35 +08:00
|
|
|
var buffer = new Writer()
|
|
|
|
.addInt32(length)
|
|
|
|
.add(bodyBuffer)
|
|
|
|
.join();
|
|
|
|
this.stream.write(buffer);
|
2010-10-24 05:34:20 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.cancel = function(processID, secretKey) {
|
2011-11-02 23:07:14 +08:00
|
|
|
var bodyBuffer = this.writer
|
|
|
|
.addInt16(1234)
|
|
|
|
.addInt16(5678)
|
|
|
|
.addInt32(processID)
|
|
|
|
.addInt32(secretKey)
|
|
|
|
.addCString('').flush();
|
|
|
|
|
|
|
|
var length = bodyBuffer.length + 4;
|
|
|
|
|
|
|
|
var buffer = new Writer()
|
2010-11-01 07:36:35 +08:00
|
|
|
.addInt32(length)
|
|
|
|
.add(bodyBuffer)
|
|
|
|
.join();
|
|
|
|
this.stream.write(buffer);
|
2010-10-24 05:34:20 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.password = function(password) {
|
2010-11-01 07:36:35 +08:00
|
|
|
//0x70 = 'p'
|
2011-01-27 23:40:07 +08:00
|
|
|
this._send(0x70, this.writer.addCString(password));
|
2010-10-24 08:02:13 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype._send = function(code, more) {
|
2013-01-21 21:29:31 +08:00
|
|
|
if(!this.stream.writable) { return false; }
|
2011-04-17 00:42:23 +08:00
|
|
|
if(more === true) {
|
|
|
|
this.writer.addHeader(code);
|
|
|
|
} else {
|
|
|
|
return this.stream.write(this.writer.flush(code));
|
|
|
|
}
|
2013-01-21 21:29:31 +08:00
|
|
|
};
|
2010-10-24 05:34:20 +08:00
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.query = function(text) {
|
2010-11-01 07:36:35 +08:00
|
|
|
//0x51 = Q
|
2011-01-27 23:40:07 +08:00
|
|
|
this.stream.write(this.writer.addCString(text).flush(0x51));
|
2010-10-24 05:34:20 +08:00
|
|
|
};
|
|
|
|
|
2011-04-17 00:42:23 +08:00
|
|
|
//send parse message
|
|
|
|
//"more" === true to buffer the message until flush() is called
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parse = function(query, more) {
|
2010-10-24 05:34:20 +08:00
|
|
|
//expect something like this:
|
|
|
|
// { name: 'queryName',
|
|
|
|
// text: 'select * from blah',
|
|
|
|
// types: ['int8', 'bool'] }
|
|
|
|
|
|
|
|
//normalize missing query names to allow for null
|
|
|
|
query.name = query.name || '';
|
|
|
|
//normalize null type array
|
|
|
|
query.types = query.types || [];
|
2010-10-24 13:18:48 +08:00
|
|
|
var len = query.types.length;
|
2011-01-02 02:40:45 +08:00
|
|
|
var buffer = this.writer
|
2010-10-24 05:34:20 +08:00
|
|
|
.addCString(query.name) //name of query
|
|
|
|
.addCString(query.text) //actual query text
|
2010-10-24 13:18:48 +08:00
|
|
|
.addInt16(len);
|
|
|
|
for(var i = 0; i < len; i++) {
|
|
|
|
buffer.addInt32(query.types[i]);
|
2010-10-24 05:34:20 +08:00
|
|
|
}
|
2010-10-28 13:27:08 +08:00
|
|
|
|
2011-04-17 00:42:23 +08:00
|
|
|
var code = 0x50;
|
|
|
|
this._send(code, more);
|
2010-10-24 05:34:20 +08:00
|
|
|
};
|
|
|
|
|
2011-04-17 00:42:23 +08:00
|
|
|
//send bind message
|
|
|
|
//"more" === true to buffer the message until flush() is called
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.bind = function(config, more) {
|
2010-10-24 05:34:20 +08:00
|
|
|
//normalize config
|
|
|
|
config = config || {};
|
2010-10-25 02:46:50 +08:00
|
|
|
config.portal = config.portal || '';
|
|
|
|
config.statement = config.statement || '';
|
2011-02-14 23:42:04 +08:00
|
|
|
config.binary = config.binary || false;
|
2010-10-25 02:46:50 +08:00
|
|
|
var values = config.values || [];
|
|
|
|
var len = values.length;
|
2011-01-02 02:40:45 +08:00
|
|
|
var buffer = this.writer
|
2010-10-25 02:46:50 +08:00
|
|
|
.addCString(config.portal)
|
|
|
|
.addCString(config.statement)
|
2010-10-24 05:34:20 +08:00
|
|
|
.addInt16(0) //always use default text format
|
2010-10-25 02:46:50 +08:00
|
|
|
.addInt16(len); //number of parameters
|
|
|
|
for(var i = 0; i < len; i++) {
|
|
|
|
var val = values[i];
|
2011-10-14 23:31:12 +08:00
|
|
|
if(val === null || typeof val === "undefined") {
|
2010-10-25 02:46:50 +08:00
|
|
|
buffer.addInt32(-1);
|
|
|
|
} else {
|
|
|
|
buffer.addInt32(Buffer.byteLength(val));
|
2011-01-02 02:40:45 +08:00
|
|
|
buffer.addString(val);
|
2010-10-25 02:46:50 +08:00
|
|
|
}
|
2010-10-24 05:34:20 +08:00
|
|
|
}
|
2011-02-14 23:42:04 +08:00
|
|
|
|
2013-03-07 00:26:40 +08:00
|
|
|
if(config.binary) {
|
2011-02-14 23:42:04 +08:00
|
|
|
buffer.addInt16(1); // format codes to use binary
|
|
|
|
buffer.addInt16(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer.addInt16(0); // format codes to use text
|
|
|
|
}
|
2010-11-01 07:36:35 +08:00
|
|
|
//0x42 = 'B'
|
2011-04-17 00:42:23 +08:00
|
|
|
this._send(0x42, more);
|
2010-10-24 05:34:20 +08:00
|
|
|
};
|
|
|
|
|
2011-04-17 00:42:23 +08:00
|
|
|
//send execute message
|
|
|
|
//"more" === true to buffer the message until flush() is called
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.execute = function(config, more) {
|
2010-10-25 02:46:50 +08:00
|
|
|
config = config || {};
|
|
|
|
config.portal = config.portal || '';
|
|
|
|
config.rows = config.rows || '';
|
2011-01-02 02:40:45 +08:00
|
|
|
var buffer = this.writer
|
2010-10-25 02:46:50 +08:00
|
|
|
.addCString(config.portal)
|
2011-01-27 23:40:07 +08:00
|
|
|
.addInt32(config.rows);
|
|
|
|
|
2010-11-01 07:36:35 +08:00
|
|
|
//0x45 = 'E'
|
2011-04-17 00:42:23 +08:00
|
|
|
this._send(0x45, more);
|
2010-10-24 05:34:20 +08:00
|
|
|
};
|
|
|
|
|
2011-01-27 23:40:07 +08:00
|
|
|
var emptyBuffer = Buffer(0);
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.flush = function() {
|
2010-11-01 07:36:35 +08:00
|
|
|
//0x48 = 'H'
|
2013-01-21 21:29:31 +08:00
|
|
|
this.writer.add(emptyBuffer);
|
2011-04-17 00:42:23 +08:00
|
|
|
this._send(0x48);
|
2013-01-21 21:29:31 +08:00
|
|
|
};
|
2010-10-24 05:34:20 +08:00
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.sync = function() {
|
2011-04-17 00:42:23 +08:00
|
|
|
//clear out any pending data in the writer
|
2013-01-21 21:29:31 +08:00
|
|
|
this.writer.flush(0);
|
2013-01-24 08:08:32 +08:00
|
|
|
|
2011-04-17 00:42:23 +08:00
|
|
|
this.writer.add(emptyBuffer);
|
|
|
|
this._send(0x53);
|
2010-10-24 05:34:20 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.end = function() {
|
2010-11-01 07:36:35 +08:00
|
|
|
//0x58 = 'X'
|
2011-04-17 00:42:23 +08:00
|
|
|
this.writer.add(emptyBuffer);
|
|
|
|
this._send(0x58);
|
2013-03-29 22:38:49 +08:00
|
|
|
this._ending = true;
|
2010-10-24 08:28:57 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.describe = function(msg, more) {
|
2011-04-17 00:42:23 +08:00
|
|
|
this.writer.addCString(msg.type + (msg.name || ''));
|
|
|
|
this._send(0x44, more);
|
2010-10-28 13:27:08 +08:00
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Connection.prototype.sendCopyFromChunk = function (chunk) {
|
2012-09-27 18:28:00 +08:00
|
|
|
this.stream.write(this.writer.add(chunk).flush(0x64));
|
2013-01-21 21:29:31 +08:00
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Connection.prototype.endCopyFrom = function () {
|
2012-09-27 18:28:00 +08:00
|
|
|
this.stream.write(this.writer.add(emptyBuffer).flush(0x63));
|
2013-01-21 21:29:31 +08:00
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Connection.prototype.sendCopyFail = function (msg) {
|
2013-01-16 19:40:59 +08:00
|
|
|
//this.stream.write(this.writer.add(emptyBuffer).flush(0x66));
|
|
|
|
this.writer.addCString(msg);
|
|
|
|
this._send(0x66);
|
2013-01-21 21:29:31 +08:00
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
2010-10-24 05:34:20 +08:00
|
|
|
//parsing methods
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.setBuffer = function(buffer) {
|
2010-10-24 05:34:20 +08:00
|
|
|
if(this.lastBuffer) { //we have unfinished biznaz
|
|
|
|
//need to combine last two buffers
|
|
|
|
var remaining = this.lastBuffer.length - this.lastOffset;
|
|
|
|
var combinedBuffer = new Buffer(buffer.length + remaining);
|
|
|
|
this.lastBuffer.copy(combinedBuffer, 0, this.lastOffset);
|
|
|
|
buffer.copy(combinedBuffer, remaining, 0);
|
|
|
|
buffer = combinedBuffer;
|
|
|
|
}
|
|
|
|
this.buffer = buffer;
|
|
|
|
this.offset = 0;
|
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.readSslResponse = function() {
|
2012-08-07 19:51:37 +08:00
|
|
|
var remaining = this.buffer.length - (this.offset);
|
|
|
|
if(remaining < 1) {
|
|
|
|
this.lastBuffer = this.buffer;
|
|
|
|
this.lastOffset = this.offset;
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-07 00:26:40 +08:00
|
|
|
return {
|
|
|
|
name: 'sslresponse',
|
|
|
|
text: this.buffer[this.offset++]
|
|
|
|
};
|
2012-08-07 19:51:37 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseMessage = function() {
|
2010-10-24 05:34:20 +08:00
|
|
|
var remaining = this.buffer.length - (this.offset);
|
|
|
|
if(remaining < 5) {
|
|
|
|
//cannot read id + length without at least 5 bytes
|
|
|
|
//just abort the read now
|
|
|
|
this.lastBuffer = this.buffer;
|
|
|
|
this.lastOffset = this.offset;
|
2010-11-01 06:46:33 +08:00
|
|
|
return false;
|
2010-10-24 05:34:20 +08:00
|
|
|
}
|
|
|
|
|
2010-11-01 06:58:32 +08:00
|
|
|
//read message id code
|
|
|
|
var id = this.buffer[this.offset++];
|
|
|
|
//read message length
|
|
|
|
var length = this.parseInt32();
|
2010-10-24 05:34:20 +08:00
|
|
|
|
2010-11-01 06:58:32 +08:00
|
|
|
if(remaining <= length) {
|
2010-10-24 05:34:20 +08:00
|
|
|
this.lastBuffer = this.buffer;
|
|
|
|
//rewind the last 5 bytes we read
|
|
|
|
this.lastOffset = this.offset-5;
|
|
|
|
return false;
|
|
|
|
}
|
2010-11-01 06:46:33 +08:00
|
|
|
|
2010-11-01 06:58:32 +08:00
|
|
|
var msg = {
|
|
|
|
length: length
|
|
|
|
};
|
|
|
|
switch(id)
|
2010-11-01 06:46:33 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
case 0x52: //R
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'authenticationOk';
|
|
|
|
return this.parseR(msg);
|
2010-11-01 06:46:33 +08:00
|
|
|
|
|
|
|
case 0x53: //S
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'parameterStatus';
|
|
|
|
return this.parseS(msg);
|
2010-11-01 06:46:33 +08:00
|
|
|
|
|
|
|
case 0x4b: //K
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'backendKeyData';
|
|
|
|
return this.parseK(msg);
|
2010-11-01 06:46:33 +08:00
|
|
|
|
|
|
|
case 0x43: //C
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'commandComplete';
|
|
|
|
return this.parseC(msg);
|
2010-11-01 06:46:33 +08:00
|
|
|
|
|
|
|
case 0x5a: //Z
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'readyForQuery';
|
|
|
|
return this.parseZ(msg);
|
2010-11-01 06:46:33 +08:00
|
|
|
|
|
|
|
case 0x54: //T
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'rowDescription';
|
|
|
|
return this.parseT(msg);
|
2010-11-01 06:46:33 +08:00
|
|
|
|
|
|
|
case 0x44: //D
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'dataRow';
|
|
|
|
return this.parseD(msg);
|
2010-11-01 06:46:33 +08:00
|
|
|
|
|
|
|
case 0x45: //E
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'error';
|
|
|
|
return this.parseE(msg);
|
2010-11-01 06:46:33 +08:00
|
|
|
|
|
|
|
case 0x4e: //N
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'notice';
|
|
|
|
return this.parseN(msg);
|
2010-11-01 06:46:33 +08:00
|
|
|
|
|
|
|
case 0x31: //1
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'parseComplete';
|
|
|
|
return msg;
|
2010-11-01 06:46:33 +08:00
|
|
|
|
|
|
|
case 0x32: //2
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'bindComplete';
|
|
|
|
return msg;
|
2010-11-01 06:46:33 +08:00
|
|
|
|
|
|
|
case 0x41: //A
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'notification';
|
|
|
|
return this.parseA(msg);
|
2010-11-01 06:46:33 +08:00
|
|
|
|
|
|
|
case 0x6e: //n
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'noData';
|
|
|
|
return msg;
|
2010-11-01 06:46:33 +08:00
|
|
|
|
|
|
|
case 0x49: //I
|
2010-11-01 06:58:32 +08:00
|
|
|
msg.name = 'emptyQuery';
|
|
|
|
return msg;
|
2010-11-01 06:46:33 +08:00
|
|
|
|
2010-11-15 07:44:36 +08:00
|
|
|
case 0x73: //s
|
|
|
|
msg.name = 'portalSuspended';
|
|
|
|
return msg;
|
|
|
|
|
2012-09-27 18:28:00 +08:00
|
|
|
case 0x47: //G
|
|
|
|
msg.name = 'copyInResponse';
|
|
|
|
return this.parseGH(msg);
|
|
|
|
|
|
|
|
case 0x48: //H
|
|
|
|
msg.name = 'copyOutResponse';
|
2013-01-24 08:08:32 +08:00
|
|
|
return this.parseGH(msg);
|
2012-09-27 18:28:00 +08:00
|
|
|
case 0x63: //c
|
|
|
|
msg.name = 'copyDone';
|
|
|
|
return msg;
|
|
|
|
|
|
|
|
case 0x64: //d
|
|
|
|
msg.name = 'copyData';
|
|
|
|
return this.parsed(msg);
|
|
|
|
|
2010-11-01 06:46:33 +08:00
|
|
|
default:
|
2010-11-01 06:58:32 +08:00
|
|
|
throw new Error("Unrecognized message code " + id);
|
2010-11-01 06:46:33 +08:00
|
|
|
}
|
2010-10-24 05:34:20 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseR = function(msg) {
|
2010-10-24 05:34:20 +08:00
|
|
|
var code = 0;
|
|
|
|
if(msg.length === 8) {
|
|
|
|
code = this.parseInt32();
|
|
|
|
if(code === 3) {
|
|
|
|
msg.name = 'authenticationCleartextPassword';
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
if(msg.length === 12) {
|
|
|
|
code = this.parseInt32();
|
|
|
|
if(code === 5) { //md5 required
|
|
|
|
msg.name = 'authenticationMD5Password';
|
|
|
|
msg.salt = new Buffer(4);
|
|
|
|
this.buffer.copy(msg.salt, 0, this.offset, this.offset + 4);
|
|
|
|
this.offset += 4;
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
}
|
2011-10-11 08:40:52 +08:00
|
|
|
throw new Error("Unknown authenticatinOk message type" + util.inspect(msg));
|
2010-10-24 05:34:20 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseS = function(msg) {
|
2010-10-24 05:34:20 +08:00
|
|
|
msg.parameterName = this.parseCString();
|
|
|
|
msg.parameterValue = this.parseCString();
|
|
|
|
return msg;
|
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseK = function(msg) {
|
2010-10-24 05:34:20 +08:00
|
|
|
msg.processID = this.parseInt32();
|
|
|
|
msg.secretKey = this.parseInt32();
|
|
|
|
return msg;
|
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseC = function(msg) {
|
2010-10-24 05:34:20 +08:00
|
|
|
msg.text = this.parseCString();
|
|
|
|
return msg;
|
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseZ = function(msg) {
|
2010-10-24 05:34:20 +08:00
|
|
|
msg.status = this.readChar();
|
|
|
|
return msg;
|
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseT = function(msg) {
|
2010-10-24 05:34:20 +08:00
|
|
|
msg.fieldCount = this.parseInt16();
|
|
|
|
var fields = [];
|
|
|
|
for(var i = 0; i < msg.fieldCount; i++){
|
|
|
|
fields[i] = this.parseField();
|
|
|
|
}
|
|
|
|
msg.fields = fields;
|
|
|
|
return msg;
|
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseField = function() {
|
2010-10-24 05:34:20 +08:00
|
|
|
var field = {
|
|
|
|
name: this.parseCString(),
|
|
|
|
tableID: this.parseInt32(),
|
|
|
|
columnID: this.parseInt16(),
|
|
|
|
dataTypeID: this.parseInt32(),
|
|
|
|
dataTypeSize: this.parseInt16(),
|
|
|
|
dataTypeModifier: this.parseInt32(),
|
|
|
|
format: this.parseInt16() === 0 ? 'text' : 'binary'
|
|
|
|
};
|
|
|
|
return field;
|
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseD = function(msg) {
|
2010-10-24 05:34:20 +08:00
|
|
|
var fieldCount = this.parseInt16();
|
|
|
|
var fields = [];
|
|
|
|
for(var i = 0; i < fieldCount; i++) {
|
|
|
|
var length = this.parseInt32();
|
2013-01-21 21:29:31 +08:00
|
|
|
fields[i] = (length === -1 ? null : this.readBytes(length));
|
|
|
|
}
|
2010-10-24 05:34:20 +08:00
|
|
|
msg.fieldCount = fieldCount;
|
|
|
|
msg.fields = fields;
|
|
|
|
return msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
//parses error
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseE = function(input) {
|
2010-10-24 05:34:20 +08:00
|
|
|
var fields = {};
|
2011-09-22 19:36:12 +08:00
|
|
|
var msg, item;
|
2010-10-24 05:34:20 +08:00
|
|
|
var fieldType = this.readString(1);
|
|
|
|
while(fieldType != '\0') {
|
|
|
|
fields[fieldType] = this.parseCString();
|
|
|
|
fieldType = this.readString(1);
|
|
|
|
}
|
2013-03-07 00:26:40 +08:00
|
|
|
if(input.name === 'error') {
|
2011-09-22 19:36:12 +08:00
|
|
|
// the msg is an Error instance
|
|
|
|
msg = new Error(fields.M);
|
|
|
|
for (item in input) {
|
|
|
|
// copy input properties to the error
|
2013-03-07 00:26:40 +08:00
|
|
|
if(input.hasOwnProperty(item)) {
|
2011-09-22 19:36:12 +08:00
|
|
|
msg[item] = input[item];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// the msg is an object literal
|
|
|
|
msg = input;
|
|
|
|
msg.message = fields.M;
|
|
|
|
}
|
2010-10-24 05:34:20 +08:00
|
|
|
msg.severity = fields.S;
|
|
|
|
msg.code = fields.C;
|
|
|
|
msg.detail = fields.D;
|
|
|
|
msg.hint = fields.H;
|
|
|
|
msg.position = fields.P;
|
|
|
|
msg.internalPosition = fields.p;
|
|
|
|
msg.internalQuery = fields.q;
|
|
|
|
msg.where = fields.W;
|
|
|
|
msg.file = fields.F;
|
|
|
|
msg.line = fields.L;
|
|
|
|
msg.routine = fields.R;
|
|
|
|
return msg;
|
|
|
|
};
|
|
|
|
|
2010-10-25 03:43:25 +08:00
|
|
|
//same thing, different name
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseN = Connection.prototype.parseE;
|
2010-10-25 03:43:25 +08:00
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseA = function(msg) {
|
2010-10-24 11:31:43 +08:00
|
|
|
msg.processId = this.parseInt32();
|
|
|
|
msg.channel = this.parseCString();
|
2010-10-24 11:45:03 +08:00
|
|
|
msg.payload = this.parseCString();
|
2010-10-24 11:31:43 +08:00
|
|
|
return msg;
|
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Connection.prototype.parseGH = function (msg) {
|
2012-09-27 18:28:00 +08:00
|
|
|
msg.binary = Boolean(this.parseInt8());
|
|
|
|
var columnCount = this.parseInt16();
|
|
|
|
msg.columnTypes = [];
|
|
|
|
for(var i = 0; i<columnCount; i++) {
|
|
|
|
msg.columnTypes[i] = this.parseInt16();
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Connection.prototype.parseInt8 = function () {
|
2013-01-24 08:08:32 +08:00
|
|
|
var value = Number(this.buffer[this.offset]);
|
2012-09-27 18:28:00 +08:00
|
|
|
this.offset++;
|
|
|
|
return value;
|
2013-01-21 21:29:31 +08:00
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Connection.prototype.readChar = function() {
|
2010-10-24 05:34:20 +08:00
|
|
|
return Buffer([this.buffer[this.offset++]]).toString(this.encoding);
|
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseInt32 = function() {
|
2010-10-24 05:34:20 +08:00
|
|
|
var value = this.peekInt32();
|
|
|
|
this.offset += 4;
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.peekInt32 = function(offset) {
|
2010-10-24 05:34:20 +08:00
|
|
|
offset = offset || this.offset;
|
|
|
|
var buffer = this.buffer;
|
|
|
|
return ((buffer[offset++] << 24) +
|
|
|
|
(buffer[offset++] << 16) +
|
|
|
|
(buffer[offset++] << 8) +
|
|
|
|
buffer[offset++]);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseInt16 = function() {
|
2010-10-24 05:34:20 +08:00
|
|
|
return ((this.buffer[this.offset++] << 8) +
|
|
|
|
(this.buffer[this.offset++] << 0));
|
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.readString = function(length) {
|
2013-01-24 08:08:32 +08:00
|
|
|
return this.buffer.toString(this.encoding, this.offset,
|
|
|
|
(this.offset += length));
|
2010-10-24 05:34:20 +08:00
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.readBytes = function(length) {
|
2011-01-27 22:10:45 +08:00
|
|
|
return this.buffer.slice(this.offset, this.offset += length);
|
|
|
|
};
|
|
|
|
|
2013-03-06 22:48:52 +08:00
|
|
|
Connection.prototype.parseCString = function() {
|
2010-10-24 05:34:20 +08:00
|
|
|
var start = this.offset;
|
2013-01-21 21:29:31 +08:00
|
|
|
while(this.buffer[this.offset++]) { }
|
2010-10-24 05:34:20 +08:00
|
|
|
return this.buffer.toString(this.encoding, start, this.offset - 1);
|
|
|
|
};
|
2013-03-06 22:48:52 +08:00
|
|
|
|
|
|
|
Connection.prototype.parsed = function (msg) {
|
2012-09-27 18:28:00 +08:00
|
|
|
//exclude length field
|
|
|
|
msg.chunk = this.readBytes(msg.length - 4);
|
2013-01-24 08:08:32 +08:00
|
|
|
return msg;
|
2013-01-21 21:29:31 +08:00
|
|
|
};
|
2010-10-24 05:34:20 +08:00
|
|
|
//end parsing methods
|
2010-10-24 06:36:04 +08:00
|
|
|
module.exports = Connection;
|