clean up prototype shorthand

For some reason a few years ago I thought it would be neat to use a shorthand version of prototype to save myself some keystrokes.  That was a cosmetic mistake.  It also breaks ctags.

Also, normalized some whitespace.
This commit is contained in:
brianc 2013-03-06 08:48:52 -06:00
parent 8ce808d0da
commit b58ae9e7f7
9 changed files with 147 additions and 122 deletions

View File

@ -6,18 +6,6 @@
'src/binding.cc'
],
'conditions' : [
['OS=="mac"', {
'include_dirs': ['<!@(pg_config --includedir)'],
'libraries' : ['-lpq -L<!@(pg_config --libdir)']
}],
['OS=="linux"', {
'include_dirs': ['<!@(pg_config --includedir)'],
'libraries' : ['-lpq -L<!@(pg_config --libdir)']
}],
['OS=="solaris"', {
'include_dirs': ['<!@(pg_config --includedir)'],
'libraries' : ['-lpq -L<!@(pg_config --libdir)']
}],
['OS=="win"', {
'include_dirs': ['<!@(pg_config --includedir)'],
'libraries' : ['libpq.lib'],
@ -28,6 +16,9 @@
]
},
}
}, { # OS!="win"
'include_dirs': ['<!@(pg_config --includedir)'],
'libraries' : ['-lpq -L<!@(pg_config --libdir)']
}]
]
}

View File

@ -36,9 +36,7 @@ var Client = function(config) {
util.inherits(Client, EventEmitter);
var p = Client.prototype;
p.connect = function(callback) {
Client.prototype.connect = function(callback) {
var self = this;
var con = this.connection;
if(this.host && this.host.indexOf('/') === 0) {
@ -170,7 +168,7 @@ p.connect = function(callback) {
};
p.cancel = function(client, query) {
Client.prototype.cancel = function(client, query) {
if (client.activeQuery == query) {
var con = this.connection;
@ -190,7 +188,7 @@ p.cancel = function(client, query) {
}
};
p._pulseQueryQueue = function() {
Client.prototype._pulseQueryQueue = function() {
if(this.readyForQuery===true) {
this.activeQuery = this.queryQueue.shift();
if(this.activeQuery) {
@ -204,7 +202,8 @@ p._pulseQueryQueue = function() {
}
}
};
p._copy = function (text, stream) {
Client.prototype._copy = function (text, stream) {
var config = {},
query;
config.text = text;
@ -222,13 +221,16 @@ p._copy = function (text, stream) {
return config.stream;
};
p.copyFrom = function (text) {
Client.prototype.copyFrom = function (text) {
return this._copy(text, new CopyFromStream());
};
p.copyTo = function (text) {
Client.prototype.copyTo = function (text) {
return this._copy(text, new CopyToStream());
};
p.query = function(config, values, callback) {
Client.prototype.query = function(config, values, callback) {
//can take in strings, config object or query object
var query = (config instanceof Query) ? config :
new Query(config, values, callback);
@ -243,19 +245,19 @@ p.query = function(config, values, callback) {
//prevents client from otherwise emitting 'drain' event until 'resumeDrain' is
//called
p.pauseDrain = function() {
Client.prototype.pauseDrain = function() {
this._drainPaused = 1;
};
//resume raising 'drain' event
p.resumeDrain = function() {
Client.prototype.resumeDrain = function() {
if(this._drainPaused > 1) {
this.emit('drain');
}
this._drainPaused = 0;
};
p.end = function() {
Client.prototype.end = function() {
this.connection.end();
};

View File

@ -22,9 +22,7 @@ var Connection = function(config) {
util.inherits(Connection, EventEmitter);
var p = Connection.prototype;
p.connect = function(port, host) {
Connection.prototype.connect = function(port, host) {
if (this.stream.readyState === 'closed') {
this.stream.connect(port, host);
@ -79,7 +77,7 @@ p.connect = function(port, host) {
}
};
p.attachListeners = function(stream) {
Connection.prototype.attachListeners = function(stream) {
var self = this;
stream.on('data', function(buffer) {
self.setBuffer(buffer);
@ -92,7 +90,7 @@ p.attachListeners = function(stream) {
});
};
p.requestSsl = function(config) {
Connection.prototype.requestSsl = function(config) {
this.checkSslResponse = true;
var bodyBuffer = this.writer
@ -108,7 +106,7 @@ p.requestSsl = function(config) {
this.stream.write(buffer);
};
p.startup = function(config) {
Connection.prototype.startup = function(config) {
var bodyBuffer = this.writer
.addInt16(3)
.addInt16(0)
@ -130,7 +128,7 @@ p.startup = function(config) {
this.stream.write(buffer);
};
p.cancel = function(processID, secretKey) {
Connection.prototype.cancel = function(processID, secretKey) {
var bodyBuffer = this.writer
.addInt16(1234)
.addInt16(5678)
@ -147,12 +145,12 @@ p.cancel = function(processID, secretKey) {
this.stream.write(buffer);
};
p.password = function(password) {
Connection.prototype.password = function(password) {
//0x70 = 'p'
this._send(0x70, this.writer.addCString(password));
};
p._send = function(code, more) {
Connection.prototype._send = function(code, more) {
if(!this.stream.writable) { return false; }
if(more === true) {
this.writer.addHeader(code);
@ -161,14 +159,14 @@ p._send = function(code, more) {
}
};
p.query = function(text) {
Connection.prototype.query = function(text) {
//0x51 = Q
this.stream.write(this.writer.addCString(text).flush(0x51));
};
//send parse message
//"more" === true to buffer the message until flush() is called
p.parse = function(query, more) {
Connection.prototype.parse = function(query, more) {
//expect something like this:
// { name: 'queryName',
// text: 'select * from blah',
@ -193,7 +191,7 @@ p.parse = function(query, more) {
//send bind message
//"more" === true to buffer the message until flush() is called
p.bind = function(config, more) {
Connection.prototype.bind = function(config, more) {
//normalize config
config = config || {};
config.portal = config.portal || '';
@ -229,7 +227,7 @@ p.bind = function(config, more) {
//send execute message
//"more" === true to buffer the message until flush() is called
p.execute = function(config, more) {
Connection.prototype.execute = function(config, more) {
config = config || {};
config.portal = config.portal || '';
config.rows = config.rows || '';
@ -243,13 +241,13 @@ p.execute = function(config, more) {
var emptyBuffer = Buffer(0);
p.flush = function() {
Connection.prototype.flush = function() {
//0x48 = 'H'
this.writer.add(emptyBuffer);
this._send(0x48);
};
p.sync = function() {
Connection.prototype.sync = function() {
//clear out any pending data in the writer
this.writer.flush(0);
@ -257,29 +255,33 @@ p.sync = function() {
this._send(0x53);
};
p.end = function() {
Connection.prototype.end = function() {
//0x58 = 'X'
this.writer.add(emptyBuffer);
this._send(0x58);
};
p.describe = function(msg, more) {
Connection.prototype.describe = function(msg, more) {
this.writer.addCString(msg.type + (msg.name || ''));
this._send(0x44, more);
};
p.sendCopyFromChunk = function (chunk) {
Connection.prototype.sendCopyFromChunk = function (chunk) {
this.stream.write(this.writer.add(chunk).flush(0x64));
};
p.endCopyFrom = function () {
Connection.prototype.endCopyFrom = function () {
this.stream.write(this.writer.add(emptyBuffer).flush(0x63));
};
p.sendCopyFail = function (msg) {
Connection.prototype.sendCopyFail = function (msg) {
//this.stream.write(this.writer.add(emptyBuffer).flush(0x66));
this.writer.addCString(msg);
this._send(0x66);
};
//parsing methods
p.setBuffer = function(buffer) {
Connection.prototype.setBuffer = function(buffer) {
if(this.lastBuffer) { //we have unfinished biznaz
//need to combine last two buffers
var remaining = this.lastBuffer.length - this.lastOffset;
@ -292,7 +294,7 @@ p.setBuffer = function(buffer) {
this.offset = 0;
};
p.readSslResponse = function() {
Connection.prototype.readSslResponse = function() {
var remaining = this.buffer.length - (this.offset);
if(remaining < 1) {
this.lastBuffer = this.buffer;
@ -302,7 +304,7 @@ p.readSslResponse = function() {
return { name: 'sslresponse', text: this.buffer[this.offset++] };
};
p.parseMessage = function() {
Connection.prototype.parseMessage = function() {
var remaining = this.buffer.length - (this.offset);
if(remaining < 5) {
//cannot read id + length without at least 5 bytes
@ -410,7 +412,7 @@ p.parseMessage = function() {
}
};
p.parseR = function(msg) {
Connection.prototype.parseR = function(msg) {
var code = 0;
if(msg.length === 8) {
code = this.parseInt32();
@ -432,29 +434,29 @@ p.parseR = function(msg) {
throw new Error("Unknown authenticatinOk message type" + util.inspect(msg));
};
p.parseS = function(msg) {
Connection.prototype.parseS = function(msg) {
msg.parameterName = this.parseCString();
msg.parameterValue = this.parseCString();
return msg;
};
p.parseK = function(msg) {
Connection.prototype.parseK = function(msg) {
msg.processID = this.parseInt32();
msg.secretKey = this.parseInt32();
return msg;
};
p.parseC = function(msg) {
Connection.prototype.parseC = function(msg) {
msg.text = this.parseCString();
return msg;
};
p.parseZ = function(msg) {
Connection.prototype.parseZ = function(msg) {
msg.status = this.readChar();
return msg;
};
p.parseT = function(msg) {
Connection.prototype.parseT = function(msg) {
msg.fieldCount = this.parseInt16();
var fields = [];
for(var i = 0; i < msg.fieldCount; i++){
@ -464,7 +466,7 @@ p.parseT = function(msg) {
return msg;
};
p.parseField = function() {
Connection.prototype.parseField = function() {
var field = {
name: this.parseCString(),
tableID: this.parseInt32(),
@ -477,7 +479,7 @@ p.parseField = function() {
return field;
};
p.parseD = function(msg) {
Connection.prototype.parseD = function(msg) {
var fieldCount = this.parseInt16();
var fields = [];
for(var i = 0; i < fieldCount; i++) {
@ -490,7 +492,7 @@ p.parseD = function(msg) {
};
//parses error
p.parseE = function(input) {
Connection.prototype.parseE = function(input) {
var fields = {};
var msg, item;
var fieldType = this.readString(1);
@ -527,15 +529,16 @@ p.parseE = function(input) {
};
//same thing, different name
p.parseN = p.parseE;
Connection.prototype.parseN = Connection.prototype.parseE;
p.parseA = function(msg) {
Connection.prototype.parseA = function(msg) {
msg.processId = this.parseInt32();
msg.channel = this.parseCString();
msg.payload = this.parseCString();
return msg;
};
p.parseGH = function (msg) {
Connection.prototype.parseGH = function (msg) {
msg.binary = Boolean(this.parseInt8());
var columnCount = this.parseInt16();
msg.columnTypes = [];
@ -544,22 +547,24 @@ p.parseGH = function (msg) {
}
return msg;
};
p.parseInt8 = function () {
Connection.prototype.parseInt8 = function () {
var value = Number(this.buffer[this.offset]);
this.offset++;
return value;
};
p.readChar = function() {
Connection.prototype.readChar = function() {
return Buffer([this.buffer[this.offset++]]).toString(this.encoding);
};
p.parseInt32 = function() {
Connection.prototype.parseInt32 = function() {
var value = this.peekInt32();
this.offset += 4;
return value;
};
p.peekInt32 = function(offset) {
Connection.prototype.peekInt32 = function(offset) {
offset = offset || this.offset;
var buffer = this.buffer;
return ((buffer[offset++] << 24) +
@ -569,26 +574,27 @@ p.peekInt32 = function(offset) {
};
p.parseInt16 = function() {
Connection.prototype.parseInt16 = function() {
return ((this.buffer[this.offset++] << 8) +
(this.buffer[this.offset++] << 0));
};
p.readString = function(length) {
Connection.prototype.readString = function(length) {
return this.buffer.toString(this.encoding, this.offset,
(this.offset += length));
};
p.readBytes = function(length) {
Connection.prototype.readBytes = function(length) {
return this.buffer.slice(this.offset, this.offset += length);
};
p.parseCString = function() {
Connection.prototype.parseCString = function() {
var start = this.offset;
while(this.buffer[this.offset++]) { }
return this.buffer.toString(this.encoding, start, this.offset - 1);
};
p.parsed = function (msg) {
Connection.prototype.parsed = function (msg) {
//exclude length field
msg.chunk = this.readBytes(msg.length - 4);
return msg;

View File

@ -11,10 +11,13 @@ var CopyFromStream = function () {
this._dataBuffered = false;
this.__defineGetter__("writable", this._writable.bind(this));
};
util.inherits(CopyFromStream, Stream);
CopyFromStream.prototype._writable = function () {
return !(this._finished || this._error);
};
CopyFromStream.prototype.startStreamingToConnection = function (connection) {
if (this._error) {
return;
@ -23,6 +26,7 @@ CopyFromStream.prototype.startStreamingToConnection = function (connection) {
this._sendIfConnectionReady();
this._endIfNeedAndPossible();
};
CopyFromStream.prototype._handleChunk = function (string, encoding) {
var dataChunk,
tmpBuffer;
@ -46,6 +50,7 @@ CopyFromStream.prototype._handleChunk = function (string, encoding) {
return this._sendIfConnectionReady();
};
CopyFromStream.prototype._sendIfConnectionReady = function () {
var dataSent = false;
if (this._connection) {
@ -60,18 +65,21 @@ CopyFromStream.prototype._sendIfConnectionReady = function () {
}
return dataSent;
};
CopyFromStream.prototype._endIfNeedAndPossible = function () {
if (this._connection && this._finished && !this._finishedSent) {
this._finishedSent = true;
this._connection.endCopyFrom();
}
};
CopyFromStream.prototype.write = function (string, encoding) {
if (this._error || this._finished) {
return false;
}
return this._handleChunk.apply(this, arguments);
};
CopyFromStream.prototype.end = function (string, encondig) {
if (this._error || this._finished) {
return false;
@ -82,6 +90,7 @@ CopyFromStream.prototype.end = function (string, encondig) {
}
this._endIfNeedAndPossible();
};
CopyFromStream.prototype.error = function (error) {
if (this._error || this._closed) {
return false;
@ -89,6 +98,7 @@ CopyFromStream.prototype.error = function (error) {
this._error = true;
this.emit('error', error);
};
CopyFromStream.prototype.close = function () {
if (this._error || this._closed) {
return false;
@ -98,6 +108,7 @@ CopyFromStream.prototype.close = function () {
}
this.emit("close");
};
var CopyToStream = function () {
Stream.apply(this, arguments);
this._error = false;
@ -107,7 +118,9 @@ var CopyToStream = function () {
this._encoding = undefined;
this.__defineGetter__('readable', this._readable.bind(this));
};
util.inherits(CopyToStream, Stream);
CopyToStream.prototype._outputDataChunk = function () {
if (this._paused) {
return;
@ -121,9 +134,11 @@ CopyToStream.prototype._outputDataChunk = function () {
this.buffer = new Buffer(0);
}
};
CopyToStream.prototype._readable = function () {
return !this._finished && !this._error;
};
CopyToStream.prototype.error = function (error) {
if (!this.readable) {
return false;
@ -133,6 +148,7 @@ CopyToStream.prototype.error = function (error) {
this.emit('error', error);
}
};
CopyToStream.prototype.close = function () {
if (!this.readable) {
return false;
@ -142,6 +158,7 @@ CopyToStream.prototype.close = function () {
this.emit("end");
}
};
CopyToStream.prototype.handleChunk = function (chunk) {
var tmpBuffer;
if (!this.readable) {
@ -157,12 +174,14 @@ CopyToStream.prototype.handleChunk = function (chunk) {
}
this._outputDataChunk();
};
CopyToStream.prototype.pause = function () {
if (!this.readable) {
return false;
}
this._paused = true;
};
CopyToStream.prototype.resume = function () {
if (!this._paused) {
return false;
@ -176,6 +195,7 @@ CopyToStream.prototype.resume = function () {
return this.emit('end');
}
};
CopyToStream.prototype.setEncoding = function (encoding) {
this._encoding = encoding;
};

View File

@ -8,6 +8,7 @@ var CopyToStream = require(__dirname + '/../copystream').CopyToStream;
var binding;
//TODO remove on v1.0.0
try {
//v0.5.x
binding = require(__dirname + '/../../build/Release/binding.node');
@ -20,15 +21,13 @@ var Connection = binding.Connection;
var types = require(__dirname + "/../types");
var NativeQuery = require(__dirname + '/query');
var EventEmitter = require('events').EventEmitter;
var p = Connection.prototype;
for(var k in EventEmitter.prototype) {
p[k] = EventEmitter.prototype[k];
Connection.prototype[k] = EventEmitter.prototype[k];
}
var nativeConnect = p.connect;
var nativeConnect = Connection.prototype.connect;
p.connect = function(cb) {
Connection.prototype.connect = function(cb) {
var self = this;
this.connectionParameters.getLibpqConnectionString(function(err, conString) {
if(err) {
@ -52,7 +51,8 @@ p.connect = function(cb) {
nativeConnect.call(self, conString);
});
};
p._copy = function (text, stream) {
Connection.prototype._copy = function (text, stream) {
var q = new NativeQuery(text, function (error) {
if (error) {
q.stream.error(error);
@ -65,19 +65,24 @@ p._copy = function (text, stream) {
this._pulseQueryQueue();
return q.stream;
};
p.copyFrom = function (text) {
Connection.prototype.copyFrom = function (text) {
return this._copy(text, new CopyFromStream());
};
p.copyTo = function (text) {
Connection.prototype.copyTo = function (text) {
return this._copy(text, new CopyToStream());
};
p.sendCopyFromChunk = function (chunk) {
Connection.prototype.sendCopyFromChunk = function (chunk) {
this._sendCopyFromChunk(chunk);
};
p.endCopyFrom = function (msg) {
Connection.prototype.endCopyFrom = function (msg) {
this._endCopyFrom(msg);
};
p.query = function(config, values, callback) {
Connection.prototype.query = function(config, values, callback) {
var query = (config instanceof NativeQuery) ? config :
new NativeQuery(config, values, callback);
this._queryQueue.push(query);
@ -85,16 +90,16 @@ p.query = function(config, values, callback) {
return query;
};
var nativeCancel = p.cancel;
var nativeCancel = Connection.prototype.cancel;
p.cancel = function(client, query) {
Connection.prototype.cancel = function(client, query) {
if (client._activeQuery == query)
this.connect(nativeCancel.bind(client));
else if (client._queryQueue.indexOf(query) != -1)
client._queryQueue.splice(client._queryQueue.indexOf(query), 1);
};
p._pulseQueryQueue = function(initialConnection) {
Connection.prototype._pulseQueryQueue = function(initialConnection) {
if(!this._connected) {
return;
}
@ -131,19 +136,21 @@ p._pulseQueryQueue = function(initialConnection) {
}
};
p.pauseDrain = function() {
Connection.prototype.pauseDrain = function() {
this._drainPaused = 1;
};
p.resumeDrain = function() {
Connection.prototype.resumeDrain = function() {
if(this._drainPaused > 1) {
this.emit('drain');
}
this._drainPaused = 0;
};
p.sendCopyFail = function(msg) {
Connection.prototype.sendCopyFail = function(msg) {
this.endCopyFrom(msg);
};
var clientBuilder = function(config) {
config = config || {};
var connection = new Connection();

View File

@ -32,7 +32,6 @@ var NativeQuery = function(config, values, callback) {
};
util.inherits(NativeQuery, EventEmitter);
var p = NativeQuery.prototype;
//maps from native rowdata into api compatible row object
var mapRowData = function(row) {
@ -45,7 +44,7 @@ var mapRowData = function(row) {
return result;
};
p.handleRow = function(rowData) {
NativeQuery.prototype.handleRow = function(rowData) {
var row = mapRowData(rowData);
if(this.callback) {
this._result.addRow(row);
@ -53,7 +52,7 @@ p.handleRow = function(rowData) {
this.emit('row', row, this._result);
};
p.handleError = function(error) {
NativeQuery.prototype.handleError = function(error) {
if (this._canceledDueToError) {
error = this._canceledDueToError;
this._canceledDueToError = false;
@ -66,7 +65,7 @@ p.handleError = function(error) {
}
};
p.handleReadyForQuery = function(meta) {
NativeQuery.prototype.handleReadyForQuery = function(meta) {
if (this._canceledDueToError) {
return this.handleError(this._canceledDueToError);
}
@ -78,11 +77,13 @@ p.handleReadyForQuery = function(meta) {
}
this.emit('end', this._result);
};
p.streamData = function (connection) {
NativeQuery.prototype.streamData = function (connection) {
if ( this.stream ) this.stream.startStreamingToConnection(connection);
else connection.sendCopyFail('No source stream defined');
};
p.handleCopyFromChunk = function (chunk) {
NativeQuery.prototype.handleCopyFromChunk = function (chunk) {
if ( this.stream ) {
this.stream.handleChunk(chunk);
}
@ -90,4 +91,5 @@ p.handleCopyFromChunk = function (chunk) {
//query method instead of copyTo) error will be handled
//on copyOutResponse event, so silently ignore this error here
};
module.exports = NativeQuery;

View File

@ -30,9 +30,8 @@ var Query = function(config, values, callback) {
};
util.inherits(Query, EventEmitter);
var p = Query.prototype;
p.requiresPreparation = function() {
Query.prototype.requiresPreparation = function() {
//named queries must always be prepared
if(this.name) { return true; }
//always prepare if there are max number of rows expected per
@ -55,7 +54,7 @@ var noParse = function(val) {
//associates row metadata from the supplied
//message with this query object
//metadata used when parsing row results
p.handleRowDescription = function(msg) {
Query.prototype.handleRowDescription = function(msg) {
this._fieldNames = [];
this._fieldConverters = [];
var len = msg.fields.length;
@ -67,7 +66,7 @@ p.handleRowDescription = function(msg) {
}
};
p.handleDataRow = function(msg) {
Query.prototype.handleDataRow = function(msg) {
var self = this;
var row = {};
for(var i = 0; i < msg.fields.length; i++) {
@ -88,11 +87,11 @@ p.handleDataRow = function(msg) {
}
};
p.handleCommandComplete = function(msg) {
Query.prototype.handleCommandComplete = function(msg) {
this._result.addCommandComplete(msg);
};
p.handleReadyForQuery = function() {
Query.prototype.handleReadyForQuery = function() {
if (this._canceledDueToError) {
return this.handleError(this._canceledDueToError);
}
@ -102,7 +101,7 @@ p.handleReadyForQuery = function() {
this.emit('end', this._result);
};
p.handleError = function(err) {
Query.prototype.handleError = function(err) {
if (this._canceledDueToError) {
err = this._canceledDueToError;
this._canceledDueToError = false;
@ -117,7 +116,7 @@ p.handleError = function(err) {
this.emit('end');
};
p.submit = function(connection) {
Query.prototype.submit = function(connection) {
var self = this;
if(this.requiresPreparation()) {
this.prepare(connection);
@ -126,11 +125,11 @@ p.submit = function(connection) {
}
};
p.hasBeenParsed = function(connection) {
Query.prototype.hasBeenParsed = function(connection) {
return this.name && connection.parsedStatements[this.name];
};
p.getRows = function(connection) {
Query.prototype.getRows = function(connection) {
connection.execute({
portal: this.portalName,
rows: this.rows
@ -138,7 +137,7 @@ p.getRows = function(connection) {
connection.flush();
};
p.prepare = function(connection) {
Query.prototype.prepare = function(connection) {
var self = this;
//prepared statements need sync to be called after each command
//complete or when an error is encountered
@ -177,11 +176,13 @@ p.prepare = function(connection) {
this.getRows(connection);
};
p.streamData = function (connection) {
Query.prototype.streamData = function (connection) {
if ( this.stream ) this.stream.startStreamingToConnection(connection);
else connection.sendCopyFail('No source stream defined');
};
p.handleCopyFromChunk = function (chunk) {
Query.prototype.handleCopyFromChunk = function (chunk) {
if ( this.stream ) {
this.stream.handleChunk(chunk);
}

View File

@ -8,12 +8,10 @@ var Result = function() {
this.rows = [];
};
var p = Result.prototype;
var matchRegexp = /([A-Za-z]+) (\d+ )?(\d+)?/;
//adds a command complete message
p.addCommandComplete = function(msg) {
Result.prototype.addCommandComplete = function(msg) {
var match;
if(msg.text) {
//pure javascript
@ -35,7 +33,7 @@ p.addCommandComplete = function(msg) {
}
};
p.addRow = function(row) {
Result.prototype.addRow = function(row) {
this.rows.push(row);
};

View File

@ -8,10 +8,8 @@ var Writer = function(size) {
this.headerPosition = 0;
};
var p = Writer.prototype;
//resizes internal buffer if not enough size left
p._ensure = function(size) {
Writer.prototype._ensure = function(size) {
var remaining = this.buffer.length - this.offset;
if(remaining < size) {
var oldBuffer = this.buffer;
@ -20,7 +18,7 @@ p._ensure = function(size) {
}
};
p.addInt32 = function(num) {
Writer.prototype.addInt32 = function(num) {
this._ensure(4);
this.buffer[this.offset++] = (num >>> 24 & 0xFF);
this.buffer[this.offset++] = (num >>> 16 & 0xFF);
@ -29,7 +27,7 @@ p.addInt32 = function(num) {
return this;
};
p.addInt16 = function(num) {
Writer.prototype.addInt16 = function(num) {
this._ensure(2);
this.buffer[this.offset++] = (num >>> 8 & 0xFF);
this.buffer[this.offset++] = (num >>> 0 & 0xFF);
@ -48,7 +46,7 @@ if(Buffer.prototype.write.length === 3) {
};
}
p.addCString = function(string) {
Writer.prototype.addCString = function(string) {
//just write a 0 for empty or null strings
if(!string) {
this._ensure(1);
@ -63,14 +61,14 @@ p.addCString = function(string) {
return this;
};
p.addChar = function(c) {
Writer.prototype.addChar = function(c) {
this._ensure(1);
writeString(this.buffer, c, this.offset, 1);
this.offset++;
return this;
};
p.addString = function(string) {
Writer.prototype.addString = function(string) {
string = string || "";
var len = Buffer.byteLength(string);
this._ensure(len);
@ -79,18 +77,18 @@ p.addString = function(string) {
return this;
};
p.getByteLength = function() {
Writer.prototype.getByteLength = function() {
return this.offset - 5;
};
p.add = function(otherBuffer) {
Writer.prototype.add = function(otherBuffer) {
this._ensure(otherBuffer.length);
otherBuffer.copy(this.buffer, this.offset);
this.offset += otherBuffer.length;
return this;
};
p.clear = function() {
Writer.prototype.clear = function() {
this.offset = 5;
this.headerPosition = 0;
this.lastEnd = 0;
@ -98,7 +96,7 @@ p.clear = function() {
//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) {
Writer.prototype.addHeader = function(code, last) {
var origOffset = this.offset;
this.offset = this.headerPosition;
this.buffer[this.offset++] = code;
@ -114,14 +112,14 @@ p.addHeader = function(code, last) {
}
};
p.join = function(code) {
Writer.prototype.join = function(code) {
if(code) {
this.addHeader(code, true);
}
return this.buffer.slice(code ? 0 : 5, this.offset);
};
p.flush = function(code) {
Writer.prototype.flush = function(code) {
var result = this.join(code);
this.clear();
return result;