move buffer-writer to external module

This commit is contained in:
brianc 2013-03-16 11:51:26 -05:00
parent a5ee365116
commit cee5f1d8b3
4 changed files with 2 additions and 326 deletions

View File

@ -4,7 +4,7 @@ var EventEmitter = require('events').EventEmitter;
var util = require('util');
var utils = require(__dirname + '/utils');
var Writer = require(__dirname + '/writer');
var Writer = require('buffer-writer');
var Connection = function(config) {
EventEmitter.call(this);

View File

@ -1,128 +0,0 @@
//binary data writer tuned for creating
//postgres message packets as effeciently as possible by reusing the
//same buffer to avoid memcpy and limit memory allocations
var Writer = function(size) {
this.size = size || 1024;
this.buffer = Buffer(this.size + 5);
this.offset = 5;
this.headerPosition = 0;
};
//resizes internal buffer if not enough size left
Writer.prototype._ensure = function(size) {
var remaining = this.buffer.length - this.offset;
if(remaining < size) {
var oldBuffer = this.buffer;
this.buffer = new Buffer(oldBuffer.length + size);
oldBuffer.copy(this.buffer);
}
};
Writer.prototype.addInt32 = function(num) {
this._ensure(4);
this.buffer[this.offset++] = (num >>> 24 & 0xFF);
this.buffer[this.offset++] = (num >>> 16 & 0xFF);
this.buffer[this.offset++] = (num >>> 8 & 0xFF);
this.buffer[this.offset++] = (num >>> 0 & 0xFF);
return this;
};
Writer.prototype.addInt16 = function(num) {
this._ensure(2);
this.buffer[this.offset++] = (num >>> 8 & 0xFF);
this.buffer[this.offset++] = (num >>> 0 & 0xFF);
return this;
};
//for versions of node requiring 'length' as 3rd argument to buffer.write
var writeString = function(buffer, string, offset, len) {
buffer.write(string, offset, len);
};
//overwrite function for older versions of node
if(Buffer.prototype.write.length === 3) {
writeString = function(buffer, string, offset, len) {
buffer.write(string, offset);
};
}
Writer.prototype.addCString = function(string) {
//just write a 0 for empty or null strings
if(!string) {
this._ensure(1);
} else {
var len = Buffer.byteLength(string);
this._ensure(len + 1); //+1 for null terminator
writeString(this.buffer, string, this.offset, len);
this.offset += len;
}
this.buffer[this.offset++] = 0; // null terminator
return this;
};
Writer.prototype.addChar = function(c) {
this._ensure(1);
writeString(this.buffer, c, this.offset, 1);
this.offset++;
return this;
};
Writer.prototype.addString = function(string) {
string = string || "";
var len = Buffer.byteLength(string);
this._ensure(len);
this.buffer.write(string, this.offset);
this.offset += len;
return this;
};
Writer.prototype.getByteLength = function() {
return this.offset - 5;
};
Writer.prototype.add = function(otherBuffer) {
this._ensure(otherBuffer.length);
otherBuffer.copy(this.buffer, this.offset);
this.offset += otherBuffer.length;
return this;
};
Writer.prototype.clear = function() {
this.offset = 5;
this.headerPosition = 0;
this.lastEnd = 0;
};
//appends a header block to all the written data since the last
//subsequent header or to the beginning if there is only one data block
Writer.prototype.addHeader = function(code, last) {
var origOffset = this.offset;
this.offset = this.headerPosition;
this.buffer[this.offset++] = code;
//length is everything in this packet minus the code
this.addInt32(origOffset - (this.headerPosition+1));
//set next header position
this.headerPosition = origOffset;
//make space for next header
this.offset = origOffset;
if(!last) {
this._ensure(5);
this.offset += 5;
}
};
Writer.prototype.join = function(code) {
if(code) {
this.addHeader(code, true);
}
return this.buffer.slice(code ? 0 : 5, this.offset);
};
Writer.prototype.flush = function(code) {
var result = this.join(code);
this.clear();
return result;
};
module.exports = Writer;

View File

@ -19,7 +19,7 @@
"main": "./lib",
"dependencies": {
"generic-pool": "2.0.2",
"deprecate": "~0.1.0"
"buffer-writer": "1.0.0"
},
"devDependencies": {
"jshint": "git://github.com/jshint/jshint.git"

View File

@ -1,196 +0,0 @@
require(__dirname + "/test-helper");
var Writer = require(__dirname + "/../../lib/writer");
test('adding int32', function() {
var testAddingInt32 = function(int, expectedBuffer) {
test('writes ' + int, function() {
var subject = new Writer();
var result = subject.addInt32(int).join();
assert.equalBuffers(result, expectedBuffer);
})
}
testAddingInt32(0, [0, 0, 0, 0]);
testAddingInt32(1, [0, 0, 0, 1]);
testAddingInt32(256, [0, 0, 1, 0]);
test('writes largest int32', function() {
//todo need to find largest int32 when I have internet access
return false;
})
test('writing multiple int32s', function() {
var subject = new Writer();
var result = subject.addInt32(1).addInt32(10).addInt32(0).join();
assert.equalBuffers(result, [0, 0, 0, 1, 0, 0, 0, 0x0a, 0, 0, 0, 0]);
})
test('having to resize the buffer', function() {
test('after resize correct result returned', function() {
var subject = new Writer(10);
subject.addInt32(1).addInt32(1).addInt32(1)
assert.equalBuffers(subject.join(), [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1])
})
})
})
test('int16', function() {
test('writes 0', function() {
var subject = new Writer();
var result = subject.addInt16(0).join();
assert.equalBuffers(result, [0,0]);
})
test('writes 400', function() {
var subject = new Writer();
var result = subject.addInt16(400).join();
assert.equalBuffers(result, [1, 0x90])
})
test('writes many', function() {
var subject = new Writer();
var result = subject.addInt16(0).addInt16(1).addInt16(2).join();
assert.equalBuffers(result, [0, 0, 0, 1, 0, 2])
})
test('resizes if internal buffer fills up', function() {
var subject = new Writer(3);
var result = subject.addInt16(2).addInt16(3).join();
assert.equalBuffers(result, [0, 2, 0, 3])
})
})
test('cString', function() {
test('writes empty cstring', function() {
var subject = new Writer();
var result = subject.addCString().join();
assert.equalBuffers(result, [0])
})
test('writes two empty cstrings', function() {
var subject = new Writer();
var result = subject.addCString("").addCString("").join();
assert.equalBuffers(result, [0, 0])
})
test('writes non-empty cstring', function() {
var subject = new Writer();
var result = subject.addCString("!!!").join();
assert.equalBuffers(result, [33, 33, 33, 0]);
})
test('resizes if reached end', function() {
var subject = new Writer(3);
var result = subject.addCString("!!!").join();
assert.equalBuffers(result, [33, 33, 33, 0]);
})
test('writes multiple cstrings', function() {
var subject = new Writer();
var result = subject.addCString("!").addCString("!").join();
assert.equalBuffers(result, [33, 0, 33, 0]);
})
})
test('writes char', function() {
var subject = new Writer(2);
var result = subject.addChar('a').addChar('b').addChar('c').join();
assert.equalBuffers(result, [0x61, 0x62, 0x63])
})
test('gets correct byte length', function() {
var subject = new Writer(5);
assert.equal(subject.getByteLength(), 0)
subject.addInt32(0)
assert.equal(subject.getByteLength(), 4)
subject.addCString("!")
assert.equal(subject.getByteLength(), 6)
})
test('can add arbitrary buffer to the end', function() {
var subject = new Writer(4);
subject.addCString("!!!")
var result = subject.add(Buffer("@@@")).join();
assert.equalBuffers(result, [33, 33, 33, 0, 0x40, 0x40, 0x40]);
})
test('can write normal string', function() {
var subject = new Writer(4);
var result = subject.addString("!").join();
assert.equalBuffers(result, [33]);
test('can write cString too', function() {
var result = subject.addCString("!").join();
assert.equalBuffers(result, [33, 33, 0]);
test('can resize', function() {
var result = subject.addString("!!").join();
assert.equalBuffers(result, [33, 33, 0, 33, 33]);
})
})
})
test('clearing', function() {
var subject = new Writer();
subject.addCString("@!!#!#");
subject.addInt32(10401);
subject.clear();
assert.equalBuffers(subject.join(), []);
test('can keep writing', function() {
var joinedResult = subject.addCString("!").addInt32(9).addInt16(2).join();
assert.equalBuffers(joinedResult, [33, 0, 0, 0, 0, 9, 0, 2]);
test('flush', function() {
var flushedResult = subject.flush();
test('returns result', function() {
assert.equalBuffers(flushedResult, [33, 0, 0, 0, 0, 9, 0, 2])
})
test('clears the writer', function() {
assert.equalBuffers(subject.join(), [])
assert.equalBuffers(subject.flush(), [])
})
})
})
})
test("resizing to much larger", function() {
var subject = new Writer(2);
var string = "!!!!!!!!";
var result = subject.addCString(string).flush();
assert.equalBuffers(result, [33, 33, 33, 33, 33, 33, 33, 33, 0])
})
test("flush", function() {
test('added as a hex code to a full writer', function() {
var subject = new Writer(2);
var result = subject.addCString("!").flush(0x50)
assert.equalBuffers(result, [0x50, 0, 0, 0, 6, 33, 0]);
})
test('added as a hex code to a non-full writer', function() {
var subject = new Writer(10).addCString("!");
var joinedResult = subject.join(0x50);
var result = subject.flush(0x50);
assert.equalBuffers(result, [0x50, 0, 0, 0, 6, 33, 0]);
})
test('added as a hex code to a buffer which requires resizing', function() {
var result = new Writer(2).addCString("!!!!!!!!").flush(0x50);
assert.equalBuffers(result, [0x50, 0, 0, 0, 0x0D, 33, 33, 33, 33, 33, 33, 33, 33, 0]);
})
})
test("header", function() {
test('adding two packets with headers', function() {
var subject = new Writer(10).addCString("!");
subject.addHeader(0x50);
subject.addCString("!!");
subject.addHeader(0x40);
subject.addCString("!");
var result = subject.flush(0x10);
assert.equalBuffers(result, [0x50, 0, 0, 0, 6, 33, 0, 0x40, 0, 0, 0, 7, 33, 33, 0, 0x10, 0, 0, 0, 6, 33, 0 ]);
})
})