Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jezzadebate committed Mar 1, 2012
1 parent 3309c63 commit e389c2e
Show file tree
Hide file tree
Showing 14 changed files with 1,651 additions and 1,554 deletions.
9 changes: 4 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/*!
* ws: a node.js websocket client
* Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
* wslite: simple, pure node.js websocket library
* Copyright 2012, Jeremy Debate
* Based on ws, copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
* MIT Licensed
*/

module.exports = require('./lib/WebSocket');
module.exports.Server = require('./lib/WebSocketServer');
module.exports.Sender = require('./lib/Sender');
module.exports.Receiver = require('./lib/Receiver');
module.exports.Server = require('./lib/WebSocketServer');
91 changes: 48 additions & 43 deletions lib/BufferPool.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,64 @@
/*!
* ws: a node.js websocket client
* Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
* wslite: simple, pure node.js websocket library
* Copyright 2012, Jeremy Debate
* Based on ws, copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
* MIT Licensed
*/

var util = require('util');

function BufferPool(initialSize, growStrategy, shrinkStrategy) {
if (typeof initialSize === 'function') {
shrinkStrategy = growStrategy;
growStrategy = initialSize;
initialSize = 0;
}
else if (typeof initialSize === 'undefined') {
initialSize = 0;
}
this._growStrategy = (growStrategy || function(db, size) {
return db.used + size;
}).bind(null, this);
this._shrinkStrategy = (shrinkStrategy || function(db) {
return initialSize;
}).bind(null, this);
this._buffer = new Buffer(initialSize);
this._offset = 0;
this._used = 0;
this._changeFactor = 0;
this.__defineGetter__('size', function(){
return this._buffer == null ? 0 : this._buffer.length;
});
this.__defineGetter__('used', function(){
return this._used;
});
if (typeof initialSize === 'function') {
shrinkStrategy = growStrategy;
growStrategy = initialSize;
initialSize = 0;
}
else if (typeof initialSize === 'undefined') {
initialSize = 0;
}
this._growStrategy = (growStrategy ||
function(db, size) {
return db.used + size;
}).bind(null, this);
this._shrinkStrategy = (shrinkStrategy ||
function(db) {
return initialSize;
}).bind(null, this);
this._buffer = new Buffer(initialSize);
this._offset = 0;
this._used = 0;
this._changeFactor = 0;
this.__defineGetter__('size',
function() {
return this._buffer == null ? 0: this._buffer.length;
});
this.__defineGetter__('used',
function() {
return this._used;
});
}

BufferPool.prototype.get = function(length) {
if (this._buffer == null || this._offset + length > this._buffer.length) {
var newBuf = new Buffer(this._growStrategy(length));
this._buffer = newBuf;
this._offset = 0;
}
this._used += length;
var buf = this._buffer.slice(this._offset, this._offset + length);
this._offset += length;
return buf;
if (this._buffer == null || this._offset + length > this._buffer.length) {
var newBuf = new Buffer(this._growStrategy(length));
this._buffer = newBuf;
this._offset = 0;
}
this._used += length;
var buf = this._buffer.slice(this._offset, this._offset + length);
this._offset += length;
return buf;
}

BufferPool.prototype.reset = function(forceNewBuffer) {
var len = this._shrinkStrategy();
if (len < this.size) this._changeFactor -= 1;
if (forceNewBuffer || this._changeFactor < -2) {
this._changeFactor = 0;
this._buffer = len ? new Buffer(len) : null;
}
this._offset = 0;
this._used = 0;
var len = this._shrinkStrategy();
if (len < this.size) this._changeFactor -= 1;
if (forceNewBuffer || this._changeFactor < -2) {
this._changeFactor = 0;
this._buffer = len ? new Buffer(len) : null;
}
this._offset = 0;
this._used = 0;
}

module.exports = BufferPool;
26 changes: 12 additions & 14 deletions lib/BufferUtil.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
/*!
* ws: a node.js websocket client
* Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
* wslite: simple, pure node.js websocket library
* Copyright 2012, Jeremy Debate
* Based on ws, copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
* MIT Licensed
*/

/**
* Node version 0.4 and 0.6 compatibility
*/

try {
module.exports = require('../build/Release/bufferutil');
} catch (e) { try {
module.exports = require('../build/default/bufferutil');
} catch (e) {
console.error('bufferutil.node has either not been built (run make),');
console.error('or your node install has changed architecture (run make clean && make).')
throw e;
}}
module.exports = require('../../../bin/bufferutil');
} catch(e) {
try {
module.exports = require('./BufferUtilNative.js');
} catch(e) {
console.error("Could not load binary or native buffer util!");
throw e;
}
}
40 changes: 40 additions & 0 deletions lib/BufferUtilNative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*!
* wslite: simple, pure node.js websocket library
* Copyright 2012, Jeremy Debate
* Based on ws, copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
* MIT Licensed
*/

module.exports.BufferUtil = {
merge: function(mergedBuffer, buffers) {
var offset = 0;
for (var i = 0, l = buffers.length; i < l; ++i) {
var buf = buffers[i];
buf.copy(mergedBuffer, offset);
offset += buf.length;
}
},
mask: function(source, mask, output, offset, length) {
var maskNum = mask.readUInt32LE(0, true);
var i = 0;
for (; i < length - 3; i += 4) {
output.writeUInt32LE(maskNum ^ source.readUInt32LE(i, true), offset + i, true);
}
switch (length % 4) {
case 3:
output[offset + i + 2] = source[i + 2] ^ mask[2];
case 2:
output[offset + i + 1] = source[i + 1] ^ mask[1];
case 1:
output[offset + i] = source[i] ^ mask[0];
case 0:
;
}
},
unmask: function(data, mask) {
var maskNum = mask.readUInt32LE(0, true);
for (var i = 0, ll = data.length; i < ll; i += 4) {
data.writeUInt32LE(maskNum ^ data.readUInt32LE(i, true), i, true);
}
}
}
39 changes: 0 additions & 39 deletions lib/BufferUtilWindows.js

This file was deleted.

35 changes: 18 additions & 17 deletions lib/ErrorCodes.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
/*!
* ws: a node.js websocket client
* Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
* wslite: simple, pure node.js websocket library
* Copyright 2012, Jeremy Debate
* Based on ws, copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
* MIT Licensed
*/

module.exports = {
isValidErrorCode: function(code) {
return (code >= 1000 && code <= 1010 && code != 1004 && code != 1005 && code != 1006) ||
(code >= 3000 && code <= 4999);
},
1000: 'normal',
1001: 'going away',
1002: 'protocol error',
1003: 'unsupported data',
1004: 'reserved',
1005: 'reserved for extensions',
1006: 'reserved for extensions',
1007: 'inconsistent or invalid data',
1008: 'policy violation',
1009: 'message too big',
1010: 'extension handshake missing',
isValidErrorCode: function(code) {
return (code >= 1000 && code <= 1010 && code != 1004 && code != 1005 && code != 1006) ||
(code >= 3000 && code <= 4999);
},
1000: 'normal',
1001: 'going away',
1002: 'protocol error',
1003: 'unsupported data',
1004: 'reserved',
1005: 'reserved for extensions',
1006: 'reserved for extensions',
1007: 'inconsistent or invalid data',
1008: 'policy violation',
1009: 'message too big',
1010: 'extension handshake missing',
};
Loading

0 comments on commit e389c2e

Please sign in to comment.