fix for changes to Buffer.prototype.write signature change between node version. closes gh#66

This commit is contained in:
brianc 2011-11-01 23:02:59 -05:00
parent 106490f0a8
commit 2cddf2a112

View File

@ -3,7 +3,7 @@
//same buffer to avoid memcpy and limit memory allocations
var Writer = function(size) {
this.size = size || 1024;
this.buffer = new Buffer(this.size + 5);
this.buffer = Buffer(this.size + 5);
this.offset = 5;
this.headerPosition = 0;
};
@ -36,6 +36,18 @@ p.addInt16 = function(num) {
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);
}
}
p.addCString = function(string) {
//just write a 0 for empty or null strings
if(!string) {
@ -43,7 +55,7 @@ p.addCString = function(string) {
} else {
var len = Buffer.byteLength(string);
this._ensure(len + 1); //+1 for null terminator
this.buffer.write(string, this.offset, len);
writeString(this.buffer, string, this.offset, len);
this.offset += len;
}
@ -53,7 +65,7 @@ p.addCString = function(string) {
p.addChar = function(char) {
this._ensure(1);
this.buffer.write(char, this.offset, 1);
writeString(this.buffer, char, this.offset, 1);
this.offset++;
return this;
}