Skip to content

Commit

Permalink
Merge pull request socketio#596 from einaros/nodenext
Browse files Browse the repository at this point in the history
Node v0.5+ compatibility
  • Loading branch information
rauchg committed Oct 28, 2011
2 parents abd0326 + a125fcb commit 8339c96
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 241 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ run-tests:
@./node_modules/.bin/expresso \
-t 3000 \
-I support \
-I lib \
--serial \
$(TESTFLAGS) \
$(TESTS)

test:
@$(MAKE) TESTS="$(ALL_TESTS)" run-tests
@$(MAKE) NODE_PATH=lib TESTS="$(ALL_TESTS)" run-tests

test-cov:
@TESTFLAGS=--cov $(MAKE) test
Expand Down
9 changes: 1 addition & 8 deletions examples/chat/app.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@

/**
* Bootstrap app.
*/

require.paths.unshift(__dirname + '/../../lib/');

/**
* Module dependencies.
*/

var express = require('express')
, stylus = require('stylus')
, nib = require('nib')
, sio = require('socket.io');
, sio = require('../../lib/socket.io');

/**
* App.
Expand Down
8 changes: 4 additions & 4 deletions examples/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
, "description": "example chat application with socket.io"
, "version": "0.0.1"
, "dependencies": {
"express": "2.3.11"
, "jade": "0.12.1"
, "stylus": "0.13.3"
, "nib": "0.0.8"
"express": "2.5.0"
, "jade": "0.16.4"
, "stylus": "0.19.0"
, "nib": "0.2.0"
}
}
9 changes: 1 addition & 8 deletions examples/irc-output/app.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@

/**
* Bootstrap app.
*/

require.paths.unshift(__dirname + '/../../lib/');

/**
* Module dependencies.
*/

var express = require('express')
, stylus = require('stylus')
, nib = require('nib')
, sio = require('socket.io')
, sio = require('../../lib/socket.io')
, irc = require('./irc');

/**
Expand Down
2 changes: 1 addition & 1 deletion examples/irc-output/irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* From https://github.com/felixge/nodelog/
*/

var sys = require('sys');
var sys = require('util');
var tcp = require('net');
var irc = exports;

Expand Down
8 changes: 4 additions & 4 deletions examples/irc-output/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "socket.io-irc"
, "version": "0.0.1"
, "dependencies": {
"express": "2.3.11"
, "jade": "0.12.1"
, "stylus": "0.13.3"
, "nib": "0.0.8"
"express": "2.5.0"
, "jade": "0.16.4"
, "stylus": "0.19.0"
, "nib": "0.2.0"
}
}
5 changes: 5 additions & 0 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ function Manager (server, options) {

var self = this;

// default error handler
server.on('error', function(err) {
self.log.warn('error raised: ' + err);
});

this.initStore();

this.on('set:store', function() {
Expand Down
16 changes: 10 additions & 6 deletions lib/transports/flashsocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ FlashSocket.prototype.name = 'flashsocket';
* @api private
*/

var server;

FlashSocket.init = function (manager) {
var server;
function create () {
server = require('policyfile').createServer({
log: function(msg){
Expand Down Expand Up @@ -80,10 +80,15 @@ FlashSocket.init = function (manager) {
// destory the server and create a new server
manager.on('set:flash policy port', function (value, key) {
var transports = manager.get('transports');

if (server && server.port !== value && ~transports.indexOf('flashsocket')) {
// destroy the server and rebuild it on a new port
server.close();
if (~transports.indexOf('flashsocket')) {
if (server) {
if (server.port === value) return;
// destroy the server and rebuild it on a new port
try {
server.close();
}
catch (e) { /* ignore exception. could e.g. be that the server isn't started yet */ }
}
create();
}
});
Expand All @@ -94,7 +99,6 @@ FlashSocket.init = function (manager) {
create();
}
});

// check if we need to initialize at start
if (~manager.get('transports').indexOf('flashsocket')){
create();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
, "redis": "0.6.7"
}
, "devDependencies": {
"expresso": "0.7.7"
"expresso": "0.9.2"
, "should": "0.0.4"
, "assertvanish": "0.0.3-1"
, "benchmark": "0.2.2"
Expand Down
45 changes: 28 additions & 17 deletions support/node-websocket-client/lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var events = require('events');
var http = require('http');
var net = require('net');
var urllib = require('url');
var sys = require('sys');
var sys = require('util');

var FRAME_NO = 0;
var FRAME_LO = 1;
Expand Down Expand Up @@ -347,7 +347,6 @@ var WebSocket = function(url, proto, opts) {
// that we've closed.
var finishClose = self.finishClose = function() {
readyState = CLOSED;

if (stream) {
stream.end();
stream.destroy();
Expand Down Expand Up @@ -469,29 +468,45 @@ var WebSocket = function(url, proto, opts) {
// that http.Client passes its constructor arguments through,
// un-inspected to net.Stream.connect(). The latter accepts a
// string as its first argument to connect to a UNIX socket.
var httpClient = undefined;
var opt = {};
var agent = null;
switch (getUrlScheme(url)) {
case 'ws':
var u = urllib.parse(url);
httpClient = http.createClient(u.port || 80, u.hostname);
httpPath = (u.pathname || '/') + (u.search || '');
httpHeaders.Host = u.hostname + (u.port ? (":" + u.port) : "");
agent = new http.Agent({
host: u.hostname,
port: u.port || 80
});
opt.agent = agent;
opt.host = u.hostname;
opt.port = u.port || 80;
opt.path = (u.pathname || '/') + (u.search || '');
opt.headers = httpHeaders;
break;

case 'ws+unix':
var sockPath = url.substring('ws+unix://'.length, url.length);
httpClient = http.createClient(sockPath);
httpHeaders.Host = 'localhost';
var u = urllib.parse(url);
agent = new http.Agent({
host: 'localhost',
port: sockPath
});
opt.agent = agent;
opt.host = 'localhost';
opt.path = sockPath;
opt.headers = httpHeaders;
break;

default:
throw new Error('Invalid URL scheme \'' + urlScheme + '\' specified.');
}

httpClient.on('upgrade', (function() {
var httpReq = http.request(opt, function() { });
var upgradeHandler = (function() {
var data = undefined;

return function(req, s, head) {
req.socket.setNoDelay(true);
stream = s;

if (readyState == CLOSED) {
Expand Down Expand Up @@ -554,7 +569,7 @@ var WebSocket = function(url, proto, opts) {
//
// XXX: This is lame. We should only remove the listeners
// that we added.
httpClient.removeAllListeners('upgrade');
httpReq.removeAllListeners('upgrade');
stream.removeAllListeners('data');
stream.on('data', dataListener);

Expand Down Expand Up @@ -582,13 +597,9 @@ var WebSocket = function(url, proto, opts) {

stream.emit('data', head);
};
})());
httpClient.on('error', function(e) {
httpClient.end();
errorListener(e);
});

var httpReq = httpClient.request(httpPath, httpHeaders);
})();
agent.on('upgrade', upgradeHandler); // node v0.4
httpReq.on('upgrade', upgradeHandler); // node v0.5+

httpReq.write(challenge, 'binary');
httpReq.end();
Expand Down
17 changes: 15 additions & 2 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,21 @@ HTTPClient.prototype.request = function (path, opts, fn) {
*/

HTTPClient.prototype.end = function () {
this.agent.sockets.forEach(function (socket) {
socket.end();
// node <v0.5 compat
if (this.agent.sockets.forEach) {
this.agent.sockets.forEach(function (socket) {
if (socket.end) socket.end();
});
return;
}
// node >=v0.5 compat
var self = this;
Object.keys(this.agent.sockets).forEach(function (socket) {
for (var i = 0, l = self.agent.sockets[socket].length; i < l; ++i) {
if (self.agent.sockets[socket][i]._handle) {
self.agent.sockets[socket][i]._handle.socket.end();
}
}
});
};

Expand Down
6 changes: 3 additions & 3 deletions test/hybi-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ mask = function(buf, maskString) {
getHybiLengthAsHexString = function(len, masked) {
if (len < 126) {
var buf = new Buffer(1);
buf[0] = (masked ? 0x80 : 0) | len;
buf[0] = (masked ? 0x80 : 0) | len;
}
else if (len < 65536) {
var buf = new Buffer(3);
buf[0] = (masked ? 0x80 : 0) | 126;
buf[0] = (masked ? 0x80 : 0) | 126;
getBufferFromHexString(pack(4, len)).copy(buf, 1);
}
else {
var buf = new Buffer(9);
buf[0] = (masked ? 0x80 : 0) | 127;
buf[0] = (masked ? 0x80 : 0) | 127;
getBufferFromHexString(pack(16, len)).copy(buf, 1);
}
return getHexStringFromBuffer(buf);
Expand Down
2 changes: 1 addition & 1 deletion test/io.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ module.exports = {
io.server.close();
done();
});
done();
} catch (e) {
e.should.match(/EACCES/);
done();
}
}

};
30 changes: 19 additions & 11 deletions test/manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ module.exports = {
io.disable('foo');

calls.should.eql(3);

done();
},

Expand Down Expand Up @@ -426,8 +427,8 @@ module.exports = {

'test disabling heartbeats': function (done) {
var port = ++ports
, io = sio.listen(port)
, cl = client(port)
, io = create(cl)
, messages = 0
, beat = false
, ws;
Expand All @@ -446,9 +447,8 @@ module.exports = {

socket.on('disconnect', function (reason) {
beat.should.be.false;

cl.end();
ws.finishClose();
cl.end();
io.server.close();
done();
});
Expand Down Expand Up @@ -505,8 +505,10 @@ module.exports = {
io.rooms.foo.length.should.equal(2);
io.rooms.bar.length.should.equal(2);

io.server.close();
done();
process.nextTick(function() {
io.server.close();
done();
});
},

'test passing options directly to the Manager through listen': function (done) {
Expand All @@ -515,8 +517,10 @@ module.exports = {

io.get('resource').should.equal('/my resource');
io.get('custom').should.equal('opt');
io.server.close();
done();
process.nextTick(function() {
io.server.close();
done();
});
},

'test disabling the log': function (done) {
Expand All @@ -535,8 +539,10 @@ module.exports = {
console.log = _console;
calls.should.equal(0);

io.server.close();
done();
process.nextTick(function() {
io.server.close();
done();
});
},

'test disabling logging with colors': function (done) {
Expand All @@ -558,7 +564,9 @@ module.exports = {
console.log = _console;
calls.should.equal(2);

io.server.close();
done();
process.nextTick(function() {
io.server.close();
done();
});
}
};
Loading

0 comments on commit 8339c96

Please sign in to comment.