Skip to content

Commit 2cddf2a

Browse files
committed
fix for changes to Buffer.prototype.write signature change between node version. closes gh#66
1 parent 106490f commit 2cddf2a

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

lib/writer.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//same buffer to avoid memcpy and limit memory allocations
44
var Writer = function(size) {
55
this.size = size || 1024;
6-
this.buffer = new Buffer(this.size + 5);
6+
this.buffer = Buffer(this.size + 5);
77
this.offset = 5;
88
this.headerPosition = 0;
99
};
@@ -36,14 +36,26 @@ p.addInt16 = function(num) {
3636
return this;
3737
}
3838

39+
//for versions of node requiring 'length' as 3rd argument to buffer.write
40+
var writeString = function(buffer, string, offset, len) {
41+
buffer.write(string, offset, len);
42+
}
43+
44+
//overwrite function for older versions of node
45+
if(Buffer.prototype.write.length === 3) {
46+
writeString = function(buffer, string, offset, len) {
47+
buffer.write(string, offset);
48+
}
49+
}
50+
3951
p.addCString = function(string) {
4052
//just write a 0 for empty or null strings
4153
if(!string) {
4254
this._ensure(1);
4355
} else {
4456
var len = Buffer.byteLength(string);
4557
this._ensure(len + 1); //+1 for null terminator
46-
this.buffer.write(string, this.offset, len);
58+
writeString(this.buffer, string, this.offset, len);
4759
this.offset += len;
4860
}
4961

@@ -53,7 +65,7 @@ p.addCString = function(string) {
5365

5466
p.addChar = function(char) {
5567
this._ensure(1);
56-
this.buffer.write(char, this.offset, 1);
68+
writeString(this.buffer, char, this.offset, 1);
5769
this.offset++;
5870
return this;
5971
}

0 commit comments

Comments
 (0)