fix for strang \0 buffer encoding issue in node v0.3.0

This commit is contained in:
brianc 2010-11-01 22:11:40 -04:00
parent 2ce2277bcf
commit 0c777fafec
4 changed files with 18 additions and 12 deletions

View File

@ -74,7 +74,7 @@ p.startup = function(config) {
p.password = function(password) { p.password = function(password) {
//0x70 = 'p' //0x70 = 'p'
this.send(0x70, Buffer(password + '\0', this.encoding)); this.send(0x70, new Writer().addCString(password).join());
}; };
p.send = function(code, bodyBuffer) { p.send = function(code, bodyBuffer) {
@ -97,7 +97,7 @@ p.end = function() {
p.query = function(text) { p.query = function(text) {
//0x51 = Q //0x51 = Q
this.send(0x51, new Buffer(text + '\0', this.encoding)); this.send(0x51, new Writer().addCString(text).join());
}; };
p.parse = function(query) { p.parse = function(query) {
@ -181,9 +181,7 @@ p.end = function() {
}; };
p.describe = function(msg) { p.describe = function(msg) {
var str = msg.type + (msg.name || "" ) + '\0'; this.send(0x44, new Writer().addCString(msg.type + (msg.name || '')).join());
var buffer = Buffer(str, this.encoding);
this.send(0x44, buffer);
}; };
//parsing methods //parsing methods

View File

@ -32,7 +32,11 @@ p.addInt32 = function(val, first) {
}; };
p.addCString = function(val) { p.addCString = function(val) {
return this.add(Buffer(val + '\0','utf8')); var len = Buffer.byteLength(val);
var buffer = new Buffer(len+1);
buffer.write(val);
buffer[len] = 0;
return this.add(buffer);
}; };
p.addChar = function(char, first) { p.addChar = function(char, first) {

View File

@ -27,8 +27,12 @@ p.addInt32 = function(val, first) {
]),first); ]),first);
}; };
p.addCString = function(val) { p.addCString = function(val, front) {
return this.add(Buffer(val + '\0','utf8')); var len = Buffer.byteLength(val);
var buffer = new Buffer(len+1);
buffer.write(val);
buffer[len] = 0;
return this.add(buffer, front);
}; };
p.addChar = function(char, first) { p.addChar = function(char, first) {

View File

@ -57,7 +57,7 @@ var oneFieldBuf = new BufferList()
.addCString('test') .addCString('test')
.join(true, 'D'); .join(true, 'D');
var oneFieldBuf = buffers.dataRow(['test\0']); var oneFieldBuf = buffers.dataRow(['test']);
var expectedAuthenticationOkayMessage = { var expectedAuthenticationOkayMessage = {
@ -67,9 +67,9 @@ var expectedAuthenticationOkayMessage = {
var expectedParameterStatusMessage = { var expectedParameterStatusMessage = {
name: 'parameterStatus', name: 'parameterStatus',
length: 25,
parameterName: 'client_encoding', parameterName: 'client_encoding',
parameterValue: 'UTF8' parameterValue: 'UTF8',
length: 25
}; };
var expectedBackendKeyDataMessage = { var expectedBackendKeyDataMessage = {
@ -246,7 +246,7 @@ test('Connection', function() {
}); });
test('field is correct', function() { test('field is correct', function() {
assert.equal(message.fields[0],'test\0'); assert.equal(message.fields[0],'test');
}); });
}); });