2011-05-02 13:32:30 +08:00
|
|
|
//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
|
2011-01-02 01:30:04 +08:00
|
|
|
var Writer = function(size) {
|
2011-01-02 01:27:52 +08:00
|
|
|
this.size = size || 1024;
|
2011-11-02 12:02:59 +08:00
|
|
|
this.buffer = Buffer(this.size + 5);
|
2011-01-15 05:19:10 +08:00
|
|
|
this.offset = 5;
|
2011-04-17 00:42:23 +08:00
|
|
|
this.headerPosition = 0;
|
2010-11-01 07:21:37 +08:00
|
|
|
};
|
|
|
|
|
2011-01-02 01:30:04 +08:00
|
|
|
var p = Writer.prototype;
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
//resizes internal buffer if not enough size left
|
|
|
|
p._ensure = function(size) {
|
2011-01-15 05:19:10 +08:00
|
|
|
var remaining = this.buffer.length - this.offset;
|
|
|
|
if(remaining < size) {
|
2011-01-15 04:52:16 +08:00
|
|
|
var oldBuffer = this.buffer;
|
2011-10-31 04:20:21 +08:00
|
|
|
this.buffer = new Buffer(oldBuffer.length + size);
|
2011-01-15 04:52:16 +08:00
|
|
|
oldBuffer.copy(this.buffer);
|
2010-12-30 09:04:33 +08:00
|
|
|
}
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p.addInt32 = function(num) {
|
2013-01-21 21:08:49 +08:00
|
|
|
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);
|
2011-01-02 01:27:52 +08:00
|
|
|
return this;
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p.addInt16 = function(num) {
|
2013-01-21 21:08:49 +08:00
|
|
|
this._ensure(2);
|
|
|
|
this.buffer[this.offset++] = (num >>> 8 & 0xFF);
|
|
|
|
this.buffer[this.offset++] = (num >>> 0 & 0xFF);
|
2011-01-02 01:27:52 +08:00
|
|
|
return this;
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-11-02 12:02:59 +08:00
|
|
|
//for versions of node requiring 'length' as 3rd argument to buffer.write
|
|
|
|
var writeString = function(buffer, string, offset, len) {
|
|
|
|
buffer.write(string, offset, len);
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2011-11-02 12:02:59 +08:00
|
|
|
|
|
|
|
//overwrite function for older versions of node
|
|
|
|
if(Buffer.prototype.write.length === 3) {
|
|
|
|
writeString = function(buffer, string, offset, len) {
|
|
|
|
buffer.write(string, offset);
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2011-11-02 12:02:59 +08:00
|
|
|
}
|
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p.addCString = function(string) {
|
2011-07-21 04:32:47 +08:00
|
|
|
//just write a 0 for empty or null strings
|
|
|
|
if(!string) {
|
|
|
|
this._ensure(1);
|
2011-10-31 04:20:21 +08:00
|
|
|
} else {
|
|
|
|
var len = Buffer.byteLength(string);
|
|
|
|
this._ensure(len + 1); //+1 for null terminator
|
2011-11-02 12:02:59 +08:00
|
|
|
writeString(this.buffer, string, this.offset, len);
|
2011-10-31 04:20:21 +08:00
|
|
|
this.offset += len;
|
2011-07-21 04:32:47 +08:00
|
|
|
}
|
2011-10-31 04:20:21 +08:00
|
|
|
|
|
|
|
this.buffer[this.offset++] = 0; // null terminator
|
2011-01-15 04:52:16 +08:00
|
|
|
return this;
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2011-01-02 01:27:52 +08:00
|
|
|
|
2013-01-24 08:23:01 +08:00
|
|
|
p.addChar = function(c) {
|
2011-01-02 01:27:52 +08:00
|
|
|
this._ensure(1);
|
2013-01-24 08:23:01 +08:00
|
|
|
writeString(this.buffer, c, this.offset, 1);
|
2011-01-02 01:27:52 +08:00
|
|
|
this.offset++;
|
|
|
|
return this;
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 02:36:26 +08:00
|
|
|
p.addString = function(string) {
|
2013-01-21 21:08:49 +08:00
|
|
|
string = string || "";
|
2011-01-02 02:36:26 +08:00
|
|
|
var len = Buffer.byteLength(string);
|
|
|
|
this._ensure(len);
|
|
|
|
this.buffer.write(string, this.offset);
|
|
|
|
this.offset += len;
|
|
|
|
return this;
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2011-01-02 02:36:26 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p.getByteLength = function() {
|
2011-01-15 05:19:10 +08:00
|
|
|
return this.offset - 5;
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2011-01-02 01:27:52 +08:00
|
|
|
|
|
|
|
p.add = function(otherBuffer) {
|
|
|
|
this._ensure(otherBuffer.length);
|
|
|
|
otherBuffer.copy(this.buffer, this.offset);
|
|
|
|
this.offset += otherBuffer.length;
|
|
|
|
return this;
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:33:50 +08:00
|
|
|
p.clear = function() {
|
2011-04-17 00:42:23 +08:00
|
|
|
this.offset = 5;
|
|
|
|
this.headerPosition = 0;
|
|
|
|
this.lastEnd = 0;
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2011-04-17 00:42:23 +08:00
|
|
|
|
|
|
|
//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
|
|
|
|
p.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
|
2013-01-21 21:08:49 +08:00
|
|
|
this.addInt32(origOffset - (this.headerPosition+1));
|
2011-04-17 00:42:23 +08:00
|
|
|
//set next header position
|
|
|
|
this.headerPosition = origOffset;
|
|
|
|
//make space for next header
|
|
|
|
this.offset = origOffset;
|
|
|
|
if(!last) {
|
|
|
|
this._ensure(5);
|
|
|
|
this.offset += 5;
|
|
|
|
}
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2011-01-15 05:19:10 +08:00
|
|
|
|
|
|
|
p.join = function(code) {
|
|
|
|
if(code) {
|
2011-04-17 00:42:23 +08:00
|
|
|
this.addHeader(code, true);
|
2011-01-15 05:19:10 +08:00
|
|
|
}
|
|
|
|
return this.buffer.slice(code ? 0 : 5, this.offset);
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2011-01-02 01:33:50 +08:00
|
|
|
|
2011-01-15 05:19:10 +08:00
|
|
|
p.flush = function(code) {
|
|
|
|
var result = this.join(code);
|
2011-01-02 01:51:33 +08:00
|
|
|
this.clear();
|
|
|
|
return result;
|
2013-01-21 21:08:49 +08:00
|
|
|
};
|
2011-01-02 01:51:33 +08:00
|
|
|
|
2011-01-02 01:30:04 +08:00
|
|
|
module.exports = Writer;
|