diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 00000000..1e8ee731 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,14 @@ +{ + "extends": "standard", + "parser": "babel-eslint", + "env": { + // Note: mocha env is defined inside test/.eslintrc.json + "node": true + }, + "rules": { + "yoda": 0, + "semi": [2, "always"], + "no-extra-semi": 2, + "semi-spacing": [2, { "before": false, "after": true }] + } +} diff --git a/.zuul.yml b/.zuul.yml deleted file mode 100644 index f4e5a977..00000000 --- a/.zuul.yml +++ /dev/null @@ -1,8 +0,0 @@ -ui: mocha-bdd -server: ./test/support/server.js -browserify: - - exclude: ws -tunnel: - type: ngrok - authtoken: 6Aw8vTgcG5EvXdQywVvbh_3fMxvd4Q7dcL2caAHAFjV - proto: tcp diff --git a/Makefile b/Makefile index 54fc2ff8..f4f139c9 100644 --- a/Makefile +++ b/Makefile @@ -4,34 +4,18 @@ REPORTER = dot build: socket.io.js socket.io.js: lib/*.js package.json - @./support/browserify.sh > socket.io.js + @./node_modules/.bin/gulp test: - @if [ "x$(BROWSER_NAME)" = "x" ]; then make test-node; else make test-zuul; fi + @./node_modules/.bin/gulp test test-node: - @./node_modules/.bin/mocha \ - --reporter $(REPORTER) \ - --require test/support/server.js \ - test/index.js + @./node_modules/.bin/gulp test-node test-zuul: - @if [ "x$(BROWSER_PLATFORM)" = "x" ]; then \ - ./node_modules/zuul/bin/zuul \ - --browser-name $(BROWSER_NAME) \ - --browser-version $(BROWSER_VERSION) \ - test/index.js; \ - else \ - ./node_modules/zuul/bin/zuul \ - --browser-name $(BROWSER_NAME) \ - --browser-version $(BROWSER_VERSION) \ - --browser-platform "$(BROWSER_PLATFORM)" \ - test/index.js; \ - fi + @./node_modules/.bin/gulp test-zuul test-cov: - @./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- \ - --reporter $(REPORTER) \ - test/ + @./node_modules/.bin/gulp test-cov .PHONY: test diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 00000000..6aaa94d8 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,114 @@ +const gulp = require('gulp'); +const mocha = require('gulp-mocha'); +const istanbul = require('gulp-istanbul'); +const webpack = require('webpack-stream'); +const child = require('child_process'); +const help = require('gulp-task-listing'); +const eslint = require('gulp-eslint'); + +gulp.task('help', help); + +gulp.task('default', ['build']); + +// ////////////////////////////////////// +// BUILDING +// ////////////////////////////////////// + +const BUILD_TARGET_DIR = './'; + +gulp.task('build', function () { + return gulp.src('lib/*.js') + .pipe(webpack(require('./support/webpack.config.js'))) + .pipe(gulp.dest(BUILD_TARGET_DIR)); +}); + +// ////////////////////////////////////// +// TESTING +// ////////////////////////////////////// + +const REPORTER = 'dot'; +const TEST_FILE = './test/index.js'; +const TEST_SUPPORT_SERVER_FILE = './test/support/server.js'; + +gulp.task('test', ['lint'], function () { + if (process.env.hasOwnProperty('BROWSER_NAME')) { + return testZuul(); + } else { + return testNode(); + } +}); + +gulp.task('test-node', testNode); +gulp.task('test-zuul', testZuul); + +gulp.task('lint', function () { + return gulp.src([ + '**/*.js', + '!node_modules/**', + '!coverage/**', + '!socket.io.js' + ]) + .pipe(eslint()) + .pipe(eslint.format()) + .pipe(eslint.failAfterError()); +}); + +// runs zuul through shell process +function testZuul () { + const ZUUL_CMD = './node_modules/zuul/bin/zuul'; + const args = [ + '--browser-name', + process.env.BROWSER_NAME, + '--browser-version', + process.env.BROWSER_VERSION + ]; + if (process.env.hasOwnProperty('BROWSER_PLATFORM')) { + args.push('--browser-platform'); + args.push(process.env.BROWSER_PLATFORM); + } + args.push(TEST_FILE); + const zuulChild = child.spawn(ZUUL_CMD, args, { stdio: 'inherit' }); + zuulChild.on('exit', function (code) { process.exit(code); }); + return zuulChild; +} + +function testNode () { + const MOCHA_OPTS = { + reporter: REPORTER, + require: [TEST_SUPPORT_SERVER_FILE], + bail: true + }; + return gulp.src(TEST_FILE, { read: false }) + .pipe(mocha(MOCHA_OPTS)) + // following lines to fix gulp-mocha not terminating (see gulp-mocha webpage) + .once('error', function (err) { + console.error(err.stack); + process.exit(1); + }) + .once('end', function () { + process.exit(); + }); +} + +gulp.task('istanbul-pre-test', function () { + return gulp.src(['lib/**/*.js']) + // Covering files + .pipe(istanbul()) + // Force `require` to return covered files + .pipe(istanbul.hookRequire()); +}); + +gulp.task('test-cov', ['istanbul-pre-test'], function () { + gulp.src(['test/*.js', 'test/support/*.js']) + .pipe(mocha({ + reporter: REPORTER + })) + .pipe(istanbul.writeReports()) + .once('error', function (err) { + console.error(err); + process.exit(1); + }) + .once('end', function () { + process.exit(); + }); +}); diff --git a/lib/index.js b/lib/index.js index e5d93849..7be6dbf5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -33,8 +33,8 @@ var cache = exports.managers = {}; * @api public */ -function lookup(uri, opts) { - if (typeof uri == 'object') { +function lookup (uri, opts) { + if (typeof uri === 'object') { opts = uri; uri = undefined; } @@ -63,21 +63,25 @@ function lookup(uri, opts) { } if (parsed.query && !opts.query) { opts.query = parsed.query; - } else if (opts && 'object' == typeof opts.query) { - - function encodeQueryString(obj){ - var str = []; - for(var p in obj) - if (obj.hasOwnProperty(p)) { - str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); - } - return str.join("&"); - } + } else if (opts && 'object' === typeof opts.query) { opts.query = encodeQueryString(opts.query); } return io.socket(parsed.path, opts); } - +/** + * Helper method to parse query objects to string. + * @param {object} query + * @returns {string} + */ +function encodeQueryString (obj) { + var str = []; + for (var p in obj) { + if (obj.hasOwnProperty(p)) { + str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p])); + } + } + return str.join('&'); +} /** * Protocol version. * diff --git a/lib/manager.js b/lib/manager.js index e89c3ded..1ad3ead3 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -33,9 +33,9 @@ module.exports = Manager; * @api public */ -function Manager(uri, opts){ +function Manager (uri, opts) { if (!(this instanceof Manager)) return new Manager(uri, opts); - if (uri && ('object' == typeof uri)) { + if (uri && ('object' === typeof uri)) { opts = uri; uri = undefined; } @@ -74,7 +74,7 @@ function Manager(uri, opts){ * @api private */ -Manager.prototype.emitAll = function() { +Manager.prototype.emitAll = function () { this.emit.apply(this, arguments); for (var nsp in this.nsps) { if (has.call(this.nsps, nsp)) { @@ -89,7 +89,7 @@ Manager.prototype.emitAll = function() { * @api private */ -Manager.prototype.updateSocketIds = function(){ +Manager.prototype.updateSocketIds = function () { for (var nsp in this.nsps) { if (has.call(this.nsps, nsp)) { this.nsps[nsp].id = this.engine.id; @@ -111,7 +111,7 @@ Emitter(Manager.prototype); * @api public */ -Manager.prototype.reconnection = function(v){ +Manager.prototype.reconnection = function (v) { if (!arguments.length) return this._reconnection; this._reconnection = !!v; return this; @@ -125,7 +125,7 @@ Manager.prototype.reconnection = function(v){ * @api public */ -Manager.prototype.reconnectionAttempts = function(v){ +Manager.prototype.reconnectionAttempts = function (v) { if (!arguments.length) return this._reconnectionAttempts; this._reconnectionAttempts = v; return this; @@ -139,14 +139,14 @@ Manager.prototype.reconnectionAttempts = function(v){ * @api public */ -Manager.prototype.reconnectionDelay = function(v){ +Manager.prototype.reconnectionDelay = function (v) { if (!arguments.length) return this._reconnectionDelay; this._reconnectionDelay = v; this.backoff && this.backoff.setMin(v); return this; }; -Manager.prototype.randomizationFactor = function(v){ +Manager.prototype.randomizationFactor = function (v) { if (!arguments.length) return this._randomizationFactor; this._randomizationFactor = v; this.backoff && this.backoff.setJitter(v); @@ -161,7 +161,7 @@ Manager.prototype.randomizationFactor = function(v){ * @api public */ -Manager.prototype.reconnectionDelayMax = function(v){ +Manager.prototype.reconnectionDelayMax = function (v) { if (!arguments.length) return this._reconnectionDelayMax; this._reconnectionDelayMax = v; this.backoff && this.backoff.setMax(v); @@ -175,7 +175,7 @@ Manager.prototype.reconnectionDelayMax = function(v){ * @api public */ -Manager.prototype.timeout = function(v){ +Manager.prototype.timeout = function (v) { if (!arguments.length) return this._timeout; this._timeout = v; return this; @@ -188,7 +188,7 @@ Manager.prototype.timeout = function(v){ * @api private */ -Manager.prototype.maybeReconnectOnOpen = function() { +Manager.prototype.maybeReconnectOnOpen = function () { // Only try to reconnect if it's the first time we're connecting if (!this.reconnecting && this._reconnection && this.backoff.attempts === 0) { // keeps reconnection from firing twice for the same reconnection loop @@ -196,7 +196,6 @@ Manager.prototype.maybeReconnectOnOpen = function() { } }; - /** * Sets the current transport `socket`. * @@ -206,7 +205,7 @@ Manager.prototype.maybeReconnectOnOpen = function() { */ Manager.prototype.open = -Manager.prototype.connect = function(fn, opts){ +Manager.prototype.connect = function (fn, opts) { debug('readyState %s', this.readyState); if (~this.readyState.indexOf('open')) return this; @@ -218,13 +217,13 @@ Manager.prototype.connect = function(fn, opts){ this.skipReconnect = false; // emit `open` - var openSub = on(socket, 'open', function() { + var openSub = on(socket, 'open', function () { self.onopen(); fn && fn(); }); // emit `connect_error` - var errorSub = on(socket, 'error', function(data){ + var errorSub = on(socket, 'error', function (data) { debug('connect_error'); self.cleanup(); self.readyState = 'closed'; @@ -245,7 +244,7 @@ Manager.prototype.connect = function(fn, opts){ debug('connect attempt will timeout after %d', timeout); // set timer - var timer = setTimeout(function(){ + var timer = setTimeout(function () { debug('connect attempt timed out after %d', timeout); openSub.destroy(); socket.close(); @@ -254,7 +253,7 @@ Manager.prototype.connect = function(fn, opts){ }, timeout); this.subs.push({ - destroy: function(){ + destroy: function () { clearTimeout(timer); } }); @@ -272,7 +271,7 @@ Manager.prototype.connect = function(fn, opts){ * @api private */ -Manager.prototype.onopen = function(){ +Manager.prototype.onopen = function () { debug('open'); // clear old subs @@ -298,8 +297,8 @@ Manager.prototype.onopen = function(){ * @api private */ -Manager.prototype.onping = function(){ - this.lastPing = new Date; +Manager.prototype.onping = function () { + this.lastPing = new Date(); this.emitAll('ping'); }; @@ -309,8 +308,8 @@ Manager.prototype.onping = function(){ * @api private */ -Manager.prototype.onpong = function(){ - this.emitAll('pong', new Date - this.lastPing); +Manager.prototype.onpong = function () { + this.emitAll('pong', new Date() - this.lastPing); }; /** @@ -319,7 +318,7 @@ Manager.prototype.onpong = function(){ * @api private */ -Manager.prototype.ondata = function(data){ +Manager.prototype.ondata = function (data) { this.decoder.add(data); }; @@ -329,7 +328,7 @@ Manager.prototype.ondata = function(data){ * @api private */ -Manager.prototype.ondecoded = function(packet) { +Manager.prototype.ondecoded = function (packet) { this.emit('packet', packet); }; @@ -339,7 +338,7 @@ Manager.prototype.ondecoded = function(packet) { * @api private */ -Manager.prototype.onerror = function(err){ +Manager.prototype.onerror = function (err) { debug('error', err); this.emitAll('error', err); }; @@ -351,14 +350,14 @@ Manager.prototype.onerror = function(err){ * @api public */ -Manager.prototype.socket = function(nsp, opts){ +Manager.prototype.socket = function (nsp, opts) { var socket = this.nsps[nsp]; if (!socket) { socket = new Socket(this, nsp, opts); this.nsps[nsp] = socket; var self = this; socket.on('connecting', onConnecting); - socket.on('connect', function(){ + socket.on('connect', function () { socket.id = self.engine.id; }); @@ -368,7 +367,7 @@ Manager.prototype.socket = function(nsp, opts){ } } - function onConnecting() { + function onConnecting () { if (!~indexOf(self.connecting, socket)) { self.connecting.push(socket); } @@ -383,7 +382,7 @@ Manager.prototype.socket = function(nsp, opts){ * @param {Socket} socket */ -Manager.prototype.destroy = function(socket){ +Manager.prototype.destroy = function (socket) { var index = indexOf(this.connecting, socket); if (~index) this.connecting.splice(index, 1); if (this.connecting.length) return; @@ -398,15 +397,15 @@ Manager.prototype.destroy = function(socket){ * @api private */ -Manager.prototype.packet = function(packet){ +Manager.prototype.packet = function (packet) { debug('writing packet %j', packet); var self = this; - if (packet.query && packet.type == 0) packet.nsp += '?' + packet.query; + if (packet.query && packet.type === 0) packet.nsp += '?' + packet.query; if (!self.encoding) { // encode, then write to engine with result self.encoding = true; - this.encoder.encode(packet, function(encodedPackets) { + this.encoder.encode(packet, function (encodedPackets) { for (var i = 0; i < encodedPackets.length; i++) { self.engine.write(encodedPackets[i], packet.options); } @@ -425,7 +424,7 @@ Manager.prototype.packet = function(packet){ * @api private */ -Manager.prototype.processPacketQueue = function() { +Manager.prototype.processPacketQueue = function () { if (this.packetBuffer.length > 0 && !this.encoding) { var pack = this.packetBuffer.shift(); this.packet(pack); @@ -438,11 +437,14 @@ Manager.prototype.processPacketQueue = function() { * @api private */ -Manager.prototype.cleanup = function(){ +Manager.prototype.cleanup = function () { debug('cleanup'); - var sub; - while (sub = this.subs.shift()) sub.destroy(); + var subsLength = this.subs.length; + for (var i = 0; i < subsLength; i++) { + var sub = this.subs.shift(); + sub.destroy(); + } this.packetBuffer = []; this.encoding = false; @@ -458,11 +460,11 @@ Manager.prototype.cleanup = function(){ */ Manager.prototype.close = -Manager.prototype.disconnect = function(){ +Manager.prototype.disconnect = function () { debug('disconnect'); this.skipReconnect = true; this.reconnecting = false; - if ('opening' == this.readyState) { + if ('opening' === this.readyState) { // `onclose` will not fire because // an open event never happened this.cleanup(); @@ -478,7 +480,7 @@ Manager.prototype.disconnect = function(){ * @api private */ -Manager.prototype.onclose = function(reason){ +Manager.prototype.onclose = function (reason) { debug('onclose'); this.cleanup(); @@ -497,7 +499,7 @@ Manager.prototype.onclose = function(reason){ * @api private */ -Manager.prototype.reconnect = function(){ +Manager.prototype.reconnect = function () { if (this.reconnecting || this.skipReconnect) return this; var self = this; @@ -512,7 +514,7 @@ Manager.prototype.reconnect = function(){ debug('will wait %dms before reconnect attempt', delay); this.reconnecting = true; - var timer = setTimeout(function(){ + var timer = setTimeout(function () { if (self.skipReconnect) return; debug('attempting reconnect'); @@ -522,7 +524,7 @@ Manager.prototype.reconnect = function(){ // check again for the case socket closed in above events if (self.skipReconnect) return; - self.open(function(err){ + self.open(function (err) { if (err) { debug('reconnect attempt error'); self.reconnecting = false; @@ -536,7 +538,7 @@ Manager.prototype.reconnect = function(){ }, delay); this.subs.push({ - destroy: function(){ + destroy: function () { clearTimeout(timer); } }); @@ -549,7 +551,7 @@ Manager.prototype.reconnect = function(){ * @api private */ -Manager.prototype.onreconnect = function(){ +Manager.prototype.onreconnect = function () { var attempt = this.backoff.attempts; this.reconnecting = false; this.backoff.reset(); diff --git a/lib/on.js b/lib/on.js index 6be286d5..fad92644 100644 --- a/lib/on.js +++ b/lib/on.js @@ -14,10 +14,10 @@ module.exports = on; * @api public */ -function on(obj, ev, fn) { +function on (obj, ev, fn) { obj.on(ev, fn); return { - destroy: function(){ + destroy: function () { obj.removeListener(ev, fn); } }; diff --git a/lib/socket.js b/lib/socket.js index ec186229..c7e4d91c 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -52,7 +52,7 @@ var emit = Emitter.prototype.emit; * @api public */ -function Socket(io, nsp, opts){ +function Socket (io, nsp, opts) { this.io = io; this.nsp = nsp; this.json = this; // compat @@ -62,7 +62,7 @@ function Socket(io, nsp, opts){ this.sendBuffer = []; this.connected = false; this.disconnected = true; - if(opts && opts.query){ + if (opts && opts.query) { this.query = opts.query; } if (this.io.autoConnect) this.open(); @@ -80,7 +80,7 @@ Emitter(Socket.prototype); * @api private */ -Socket.prototype.subEvents = function() { +Socket.prototype.subEvents = function () { if (this.subs) return; var io = this.io; @@ -98,12 +98,12 @@ Socket.prototype.subEvents = function() { */ Socket.prototype.open = -Socket.prototype.connect = function(){ +Socket.prototype.connect = function () { if (this.connected) return this; this.subEvents(); this.io.open(); // ensure open - if ('open' == this.io.readyState) this.onopen(); + if ('open' === this.io.readyState) this.onopen(); this.emit('connecting'); return this; }; @@ -115,7 +115,7 @@ Socket.prototype.connect = function(){ * @api public */ -Socket.prototype.send = function(){ +Socket.prototype.send = function () { var args = toArray(arguments); args.unshift('message'); this.emit.apply(this, args); @@ -131,7 +131,7 @@ Socket.prototype.send = function(){ * @api public */ -Socket.prototype.emit = function(ev){ +Socket.prototype.emit = function (ev) { if (events.hasOwnProperty(ev)) { emit.apply(this, arguments); return this; @@ -146,7 +146,7 @@ Socket.prototype.emit = function(ev){ packet.options.compress = !this.flags || false !== this.flags.compress; // event ack callback - if ('function' == typeof args[args.length - 1]) { + if ('function' === typeof args[args.length - 1]) { debug('emitting packet with ack id %d', this.ids); this.acks[this.ids] = args.pop(); packet.id = this.ids++; @@ -170,7 +170,7 @@ Socket.prototype.emit = function(ev){ * @api private */ -Socket.prototype.packet = function(packet){ +Socket.prototype.packet = function (packet) { packet.nsp = this.nsp; this.io.packet(packet); }; @@ -181,12 +181,13 @@ Socket.prototype.packet = function(packet){ * @api private */ -Socket.prototype.onopen = function(){ +Socket.prototype.onopen = function () { debug('transport is open - connecting'); + // write connect packet if necessary - if ('/' != this.nsp) { + if ('/' !== this.nsp) { if (this.query) { - this.packet({ type: parser.CONNECT, query: this.query}); + this.packet({type: parser.CONNECT, query: this.query}); } else { this.packet({type: parser.CONNECT}); } @@ -200,7 +201,7 @@ Socket.prototype.onopen = function(){ * @api private */ -Socket.prototype.onclose = function(reason){ +Socket.prototype.onclose = function (reason) { debug('close (%s)', reason); this.connected = false; this.disconnected = true; @@ -215,8 +216,8 @@ Socket.prototype.onclose = function(reason){ * @api private */ -Socket.prototype.onpacket = function(packet){ - if (packet.nsp != this.nsp) return; +Socket.prototype.onpacket = function (packet) { + if (packet.nsp !== this.nsp) return; switch (packet.type) { case parser.CONNECT: @@ -256,7 +257,7 @@ Socket.prototype.onpacket = function(packet){ * @api private */ -Socket.prototype.onevent = function(packet){ +Socket.prototype.onevent = function (packet) { var args = packet.data || []; debug('emitting event %j', args); @@ -278,10 +279,10 @@ Socket.prototype.onevent = function(packet){ * @api private */ -Socket.prototype.ack = function(id){ +Socket.prototype.ack = function (id) { var self = this; var sent = false; - return function(){ + return function () { // prevent double callbacks if (sent) return; sent = true; @@ -304,9 +305,9 @@ Socket.prototype.ack = function(id){ * @api private */ -Socket.prototype.onack = function(packet){ +Socket.prototype.onack = function (packet) { var ack = this.acks[packet.id]; - if ('function' == typeof ack) { + if ('function' === typeof ack) { debug('calling ack %s with %j', packet.id, packet.data); ack.apply(this, packet.data); delete this.acks[packet.id]; @@ -321,7 +322,7 @@ Socket.prototype.onack = function(packet){ * @api private */ -Socket.prototype.onconnect = function(){ +Socket.prototype.onconnect = function () { this.connected = true; this.disconnected = false; this.emit('connect'); @@ -334,7 +335,7 @@ Socket.prototype.onconnect = function(){ * @api private */ -Socket.prototype.emitBuffered = function(){ +Socket.prototype.emitBuffered = function () { var i; for (i = 0; i < this.receiveBuffer.length; i++) { emit.apply(this, this.receiveBuffer[i]); @@ -353,7 +354,7 @@ Socket.prototype.emitBuffered = function(){ * @api private */ -Socket.prototype.ondisconnect = function(){ +Socket.prototype.ondisconnect = function () { debug('server disconnect (%s)', this.nsp); this.destroy(); this.onclose('io server disconnect'); @@ -367,7 +368,7 @@ Socket.prototype.ondisconnect = function(){ * @api private. */ -Socket.prototype.destroy = function(){ +Socket.prototype.destroy = function () { if (this.subs) { // clean subscriptions to avoid reconnections for (var i = 0; i < this.subs.length; i++) { @@ -387,7 +388,7 @@ Socket.prototype.destroy = function(){ */ Socket.prototype.close = -Socket.prototype.disconnect = function(){ +Socket.prototype.disconnect = function () { if (this.connected) { debug('performing disconnect (%s)', this.nsp); this.packet({ type: parser.DISCONNECT }); @@ -411,7 +412,7 @@ Socket.prototype.disconnect = function(){ * @api public */ -Socket.prototype.compress = function(compress){ +Socket.prototype.compress = function (compress) { this.flags = this.flags || {}; this.flags.compress = compress; return this; diff --git a/lib/url.js b/lib/url.js index dd3982bd..8bac5429 100644 --- a/lib/url.js +++ b/lib/url.js @@ -21,17 +21,17 @@ module.exports = url; * @api public */ -function url(uri, loc){ +function url (uri, loc) { var obj = uri; // default to window.location - var loc = loc || global.location; + loc = loc || global.location; if (null == uri) uri = loc.protocol + '//' + loc.host; // relative path support - if ('string' == typeof uri) { - if ('/' == uri.charAt(0)) { - if ('/' == uri.charAt(1)) { + if ('string' === typeof uri) { + if ('/' === uri.charAt(0)) { + if ('/' === uri.charAt(1)) { uri = loc.protocol + uri; } else { uri = loc.host + uri; @@ -40,7 +40,7 @@ function url(uri, loc){ if (!/^(https?|wss?):\/\//.test(uri)) { debug('protocol-less url %s', uri); - if ('undefined' != typeof loc) { + if ('undefined' !== typeof loc) { uri = loc.protocol + '//' + uri; } else { uri = 'https://' + uri; @@ -56,8 +56,7 @@ function url(uri, loc){ if (!obj.port) { if (/^(http|ws)$/.test(obj.protocol)) { obj.port = '80'; - } - else if (/^(http|ws)s$/.test(obj.protocol)) { + } else if (/^(http|ws)s$/.test(obj.protocol)) { obj.port = '443'; } } @@ -70,7 +69,7 @@ function url(uri, loc){ // define unique id obj.id = obj.protocol + '://' + host + ':' + obj.port; // define href - obj.href = obj.protocol + '://' + host + (loc && loc.port == obj.port ? '' : (':' + obj.port)); + obj.href = obj.protocol + '://' + host + (loc && loc.port === obj.port ? '' : (':' + obj.port)); return obj; } diff --git a/package.json b/package.json index 1c04e6f3..9d600017 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,6 @@ "client" ], "main": "./lib/index", - "jspm": { - "main": "socket.io.js" - }, "dependencies": { "debug": "2.2.0", "engine.io-client": "1.6.8", @@ -27,22 +24,35 @@ "backo2": "1.0.2" }, "devDependencies": { + "babel-core": "6.4.5", + "babel-eslint": "4.1.7", + "babel-loader": "6.2.1", + "babel-preset-es2015": "6.3.13", "base64-arraybuffer": "0.1.5", - "browserify": "13.0.0", "concat-stream": "1.5.1", "derequire": "2.0.3", + "eslint-config-standard": "4.4.0", + "eslint-plugin-standard": "1.3.1", "expect.js": "0.3.1", + "gulp": "3.9.0", + "gulp-eslint": "1.1.1", + "gulp-file": "0.2.0", + "gulp-istanbul": "0.10.3", + "gulp-mocha": "2.2.0", + "gulp-task-listing": "1.0.1", "has-cors": "1.1.0", "istanbul": "0.4.2", "mocha": "2.3.4", "socket.io": "1.4.5", "text-blob-builder": "0.0.1", "uglify-js": "2.6.1", - "zuul": "3.9.0", + "webpack-stream": "3.1.0", + "zuul": "3.10.1", + "zuul-builder-webpack": "1.1.0", "zuul-ngrok": "3.2.0" }, "scripts": { - "test": "make test" + "test": "gulp test" }, "contributors": [ { diff --git a/support/browserify.js b/support/browserify.js deleted file mode 100644 index ec9005b2..00000000 --- a/support/browserify.js +++ /dev/null @@ -1,53 +0,0 @@ - -/** - * Module dependencies. - */ - -var browserify = require('browserify'); -var concat = require('concat-stream'); -var derequire = require('derequire'); -var path = require.resolve('../'); - -/** - * Module exports. - */ - -module.exports = build; - -/** - * Make the build. - * - * @api public - */ - - -function build(fn){ - var bundle = browserify({ - builtins: false, - entries: [ path ], - insertGlobalVars: { global: glob }, - standalone: 'io' - }) - .exclude('ws') - .bundle(); - - bundle.on('error', function (err) { - fn(err); - }); - - bundle.pipe(concat({ encoding: 'string' }, function (out) { - fn(null, derequire(out)); - })); -} - -/** - * Populates `global`. - * - * @api private - */ - -function glob(){ - return 'typeof self !== "undefined" ? self : ' - + 'typeof window !== "undefined" ? window : ' - + 'typeof global !== "undefined" ? global : {}'; -} diff --git a/support/browserify.sh b/support/browserify.sh deleted file mode 100755 index 0ea6a6ad..00000000 --- a/support/browserify.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env node - -require('./browserify')(function(err, out){ - if (err) throw err; - console.log(out); -}); diff --git a/support/webpack.config.js b/support/webpack.config.js new file mode 100644 index 00000000..4cd2bfc2 --- /dev/null +++ b/support/webpack.config.js @@ -0,0 +1,34 @@ + +module.exports = { + entry: './lib/index.js', + output: { + library: 'io', + libraryTarget: 'umd', + filename: 'socket.io.js' + }, + externals: { + global: glob() + }, + module: { + loaders: [{ + test: /\.js$/, + exclude: /(node_modules|bower_components)/, + loader: 'babel', // 'babel-loader' is also a legal name to reference + query: { + presets: ['es2015'] + } + }] + } +}; + +/** + * Populates `global`. + * + * @api private + */ + +function glob () { + return 'typeof self !== "undefined" ? self : ' + + 'typeof window !== "undefined" ? window : ' + + 'typeof global !== "undefined" ? global : {}'; +} diff --git a/test/.eslintrc.json b/test/.eslintrc.json new file mode 100644 index 00000000..8a30ebae --- /dev/null +++ b/test/.eslintrc.json @@ -0,0 +1,6 @@ +{ + "env": { + "node": true, + "mocha": true + } +} diff --git a/test/connection.js b/test/connection.js index 2e4df3cc..be49f21b 100644 --- a/test/connection.js +++ b/test/connection.js @@ -4,25 +4,25 @@ var hasCORS = require('has-cors'); var textBlobBuilder = require('text-blob-builder'); var env = require('./support/env'); -describe('connection', function() { +describe('connection', function () { this.timeout(70000); - it('should connect to localhost', function(done){ + it('should connect to localhost', function (done) { var socket = io({ forceNew: true }); socket.emit('hi'); - socket.on('hi', function(data){ + socket.on('hi', function (data) { socket.disconnect(); done(); }); }); - it('should not connect when autoConnect option set to false', function() { + it('should not connect when autoConnect option set to false', function () { var socket = io({ forceNew: true, autoConnect: false }); expect(socket.io.engine).to.not.be.ok(); socket.disconnect(); }); - it('should start two connections with same path', function(){ + it('should start two connections with same path', function () { var s1 = io('/'); var s2 = io('/'); @@ -31,7 +31,7 @@ describe('connection', function() { s2.disconnect(); }); - it('should start two connections with same path and different querystrings', function(){ + it('should start two connections with same path and different querystrings', function () { var s1 = io('/?woot'); var s2 = io('/'); @@ -40,38 +40,38 @@ describe('connection', function() { s2.disconnect(); }); - it('should work with acks', function(done){ + it('should work with acks', function (done) { var socket = io({ forceNew: true }); socket.emit('ack'); - socket.on('ack', function(fn){ + socket.on('ack', function (fn) { fn(5, { test: true }); }); - socket.on('got it', function(){ + socket.on('got it', function () { socket.disconnect(); done(); }); }); - it('should receive date with ack', function(done){ + it('should receive date with ack', function (done) { var socket = io({ forceNew: true }); - socket.emit('getAckDate', { test: true }, function(data){ - expect(data).to.be.a('string'); - socket.disconnect(); - done(); + socket.emit('getAckDate', { test: true }, function (data) { + expect(data).to.be.a('string'); + socket.disconnect(); + done(); }); }); - it('should work with false', function(done){ + it('should work with false', function (done) { var socket = io({ forceNew: true }); socket.emit('false'); - socket.on('false', function(f){ + socket.on('false', function (f) { expect(f).to.be(false); socket.disconnect(); done(); }); }); - it('should receive utf8 multibyte characters', function(done) { + it('should receive utf8 multibyte characters', function (done) { var correct = [ 'てすと', 'Я Б Г Д Ж Й', @@ -82,10 +82,10 @@ describe('connection', function() { var socket = io({ forceNew: true }); var i = 0; - socket.on('takeUtf8', function(data) { + socket.on('takeUtf8', function (data) { expect(data).to.be(correct[i]); i++; - if (i == correct.length) { + if (i === correct.length) { socket.disconnect(); done(); } @@ -93,12 +93,12 @@ describe('connection', function() { socket.emit('getUtf8'); }); - it('should connect to a namespace after connection established', function(done) { + it('should connect to a namespace after connection established', function (done) { var manager = io.Manager(); var socket = manager.socket('/'); - socket.on('connect', function(){ + socket.on('connect', function () { var foo = manager.socket('/foo'); - foo.on('connect', function(){ + foo.on('connect', function () { foo.close(); socket.close(); manager.close(); @@ -107,14 +107,14 @@ describe('connection', function() { }); }); - it('should open a new namespace after connection gets closed', function(done){ + it('should open a new namespace after connection gets closed', function (done) { var manager = io.Manager(); var socket = manager.socket('/'); - socket.on('connect', function() { + socket.on('connect', function () { socket.disconnect(); - }).on('disconnect', function() { + }).on('disconnect', function () { var foo = manager.socket('/foo'); - foo.on('connect', function() { + foo.on('connect', function () { foo.disconnect(); manager.close(); done(); @@ -122,24 +122,24 @@ describe('connection', function() { }); }); - it('should reconnect by default', function(done){ + it('should reconnect by default', function (done) { var socket = io({ forceNew: true }); - socket.io.on('reconnect', function() { + socket.io.on('reconnect', function () { socket.disconnect(); done(); }); - setTimeout(function() { + setTimeout(function () { socket.io.engine.close(); }, 500); }); - it('should reconnect manually', function(done) { + it('should reconnect manually', function (done) { var socket = io({ forceNew: true }); - socket.once('connect', function() { + socket.once('connect', function () { socket.disconnect(); - }).once('disconnect', function() { - socket.once('connect', function() { + }).once('disconnect', function () { + socket.once('connect', function () { socket.disconnect(); done(); }); @@ -147,33 +147,33 @@ describe('connection', function() { }); }); - it('should reconnect automatically after reconnecting manually', function(done){ + it('should reconnect automatically after reconnecting manually', function (done) { var socket = io({ forceNew: true }); - socket.once('connect', function() { + socket.once('connect', function () { socket.disconnect(); - }).once('disconnect', function() { - socket.on('reconnect', function() { + }).once('disconnect', function () { + socket.on('reconnect', function () { socket.disconnect(); done(); }); socket.connect(); - setTimeout(function() { + setTimeout(function () { socket.io.engine.close(); }, 500); }); }); - it('should attempt reconnects after a failed reconnect', function(done){ + it('should attempt reconnects after a failed reconnect', function (done) { var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 }); var socket = manager.socket('/timeout'); - socket.once('reconnect_failed', function() { + socket.once('reconnect_failed', function () { var reconnects = 0; - var reconnectCb = function() { + var reconnectCb = function () { reconnects++; }; manager.on('reconnect_attempt', reconnectCb); - manager.on('reconnect_failed', function failed() { + manager.on('reconnect_failed', function failed () { expect(reconnects).to.be(2); socket.close(); manager.close(); @@ -183,15 +183,18 @@ describe('connection', function() { }); }); - it('reconnect delay should increase every time', function(done){ + it('reconnect delay should increase every time', function (done) { var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 3, reconnectionDelay: 100, randomizationFactor: 0.2 }); var socket = manager.socket('/timeout'); - var reconnects = 0, increasingDelay = true, startTime, prevDelay = 0; - - socket.on('connect_error', function() { + var reconnects = 0; + var increasingDelay = true; + var startTime; + var prevDelay = 0; + + socket.on('connect_error', function () { startTime = new Date().getTime(); }); - socket.on('reconnect_attempt', function() { + socket.on('reconnect_attempt', function () { reconnects++; var currentTime = new Date().getTime(); var delay = currentTime - startTime; @@ -201,7 +204,7 @@ describe('connection', function() { prevDelay = delay; }); - socket.on('reconnect_failed', function failed() { + socket.on('reconnect_failed', function failed () { expect(reconnects).to.be(3); expect(increasingDelay).to.be.ok(); socket.close(); @@ -210,51 +213,51 @@ describe('connection', function() { }); }); - it('reconnect event should fire in socket', function(done){ + it('reconnect event should fire in socket', function (done) { var socket = io({ forceNew: true }); - socket.on('reconnect', function() { + socket.on('reconnect', function () { socket.disconnect(); done(); }); - setTimeout(function() { + setTimeout(function () { socket.io.engine.close(); }, 500); }); - it('should not reconnect when force closed', function(done){ + it('should not reconnect when force closed', function (done) { var socket = io('/invalid', { forceNew: true, timeout: 0, reconnectionDelay: 10 }); - socket.on('connect_error', function() { - socket.on('reconnect_attempt', function() { + socket.on('connect_error', function () { + socket.on('reconnect_attempt', function () { expect().fail(); }); socket.disconnect(); // set a timeout to let reconnection possibly fire - setTimeout(function() { + setTimeout(function () { done(); }, 500); }); }); - it('should stop reconnecting when force closed', function(done){ + it('should stop reconnecting when force closed', function (done) { var socket = io('/invalid', { forceNew: true, timeout: 0, reconnectionDelay: 10 }); - socket.once('reconnect_attempt', function() { - socket.on('reconnect_attempt', function() { + socket.once('reconnect_attempt', function () { + socket.on('reconnect_attempt', function () { expect().fail(); }); socket.disconnect(); // set a timeout to let reconnection possibly fire - setTimeout(function() { + setTimeout(function () { done(); }, 500); }); }); - it('should reconnect after stopping reconnection', function(done){ + it('should reconnect after stopping reconnection', function (done) { var socket = io('/invalid', { forceNew: true, timeout: 0, reconnectionDelay: 10 }); - socket.once('reconnect_attempt', function() { - socket.on('reconnect_attempt', function() { + socket.once('reconnect_attempt', function () { + socket.on('reconnect_attempt', function () { socket.disconnect(); done(); }); @@ -263,17 +266,17 @@ describe('connection', function() { }); }); - it('should stop reconnecting on a socket and keep to reconnect on another', function(done){ + it('should stop reconnecting on a socket and keep to reconnect on another', function (done) { var manager = io.Manager(); var socket1 = manager.socket('/'); var socket2 = manager.socket('/asd'); - manager.on('reconnect_attempt', function() { - socket1.on('connect', function() { + manager.on('reconnect_attempt', function () { + socket1.on('connect', function () { expect().fail(); }); - socket2.on('connect', function() { - setTimeout(function() { + socket2.on('connect', function () { + setTimeout(function () { socket2.disconnect(); manager.disconnect(); done(); @@ -282,22 +285,22 @@ describe('connection', function() { socket1.disconnect(); }); - setTimeout(function() { + setTimeout(function () { manager.engine.close(); }, 1000); }); - it('should try to reconnect twice and fail when requested two attempts with immediate timeout and reconnect enabled', function(done) { + it('should try to reconnect twice and fail when requested two attempts with immediate timeout and reconnect enabled', function (done) { var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 }); var socket; var reconnects = 0; - var reconnectCb = function() { + var reconnectCb = function () { reconnects++; }; manager.on('reconnect_attempt', reconnectCb); - manager.on('reconnect_failed', function failed() { + manager.on('reconnect_failed', function failed () { expect(reconnects).to.be(2); socket.close(); manager.close(); @@ -307,18 +310,18 @@ describe('connection', function() { socket = manager.socket('/timeout'); }); - it('should fire reconnect_* events on socket', function(done) { + it('should fire reconnect_* events on socket', function (done) { var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 }); var socket = manager.socket('/timeout_socket'); var reconnects = 0; - var reconnectCb = function(attempts) { + var reconnectCb = function (attempts) { reconnects++; expect(attempts).to.be(reconnects); }; socket.on('reconnect_attempt', reconnectCb); - socket.on('reconnect_failed', function failed() { + socket.on('reconnect_failed', function failed () { expect(reconnects).to.be(2); socket.close(); manager.close(); @@ -326,34 +329,34 @@ describe('connection', function() { }); }); - it('should fire error on socket', function(done) { + it('should fire error on socket', function (done) { var manager = io.Manager({ reconnection: true }); var socket = manager.socket('/timeout_socket'); - socket.on('error', function(data) { + socket.on('error', function (data) { expect(data.code).to.be('test'); socket.close(); manager.close(); done(); }); - socket.on('connect', function() { + socket.on('connect', function () { manager.engine.onPacket({ type: 'error', data: 'test' }); }); }); - it('should fire reconnecting (on socket) with attempts number when reconnecting twice', function(done) { + it('should fire reconnecting (on socket) with attempts number when reconnecting twice', function (done) { var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 }); var socket = manager.socket('/timeout_socket'); var reconnects = 0; - var reconnectCb = function(attempts) { + var reconnectCb = function (attempts) { reconnects++; expect(attempts).to.be(reconnects); }; socket.on('reconnecting', reconnectCb); - socket.on('reconnect_failed', function failed() { + socket.on('reconnect_failed', function failed () { expect(reconnects).to.be(2); socket.close(); manager.close(); @@ -361,18 +364,18 @@ describe('connection', function() { }); }); - it('should not try to reconnect and should form a connection when connecting to correct port with default timeout', function(done) { + it('should not try to reconnect and should form a connection when connecting to correct port with default timeout', function (done) { var manager = io.Manager({ reconnection: true, reconnectionDelay: 10 }); - var cb = function() { + var cb = function () { socket.close(); expect().fail(); }; manager.on('reconnect_attempt', cb); var socket = manager.socket('/valid'); - socket.on('connect', function(){ + socket.on('connect', function () { // set a timeout to let reconnection possibly fire - setTimeout(function() { + setTimeout(function () { socket.close(); manager.close(); done(); @@ -380,10 +383,10 @@ describe('connection', function() { }); }); - it('should connect while disconnecting another socket', function(done) { + it('should connect while disconnecting another socket', function (done) { var manager = io.Manager(); var socket1 = manager.socket('/foo'); - socket1.on('connect', function() { + socket1.on('connect', function () { var socket2 = manager.socket('/asd'); socket2.on('connect', done); socket1.disconnect(); @@ -393,17 +396,17 @@ describe('connection', function() { // Ignore incorrect connection test for old IE due to no support for // `script.onerror` (see: http://requirejs.org/docs/api.html#ieloadfail) if (!global.document || hasCORS) { - it('should try to reconnect twice and fail when requested two attempts with incorrect address and reconnect enabled', function(done) { + it('should try to reconnect twice and fail when requested two attempts with incorrect address and reconnect enabled', function (done) { var manager = io.Manager('http://localhost:3940', { reconnection: true, reconnectionAttempts: 2, reconnectionDelay: 10 }); var socket = manager.socket('/asd'); var reconnects = 0; - var cb = function() { + var cb = function () { reconnects++; }; manager.on('reconnect_attempt', cb); - manager.on('reconnect_failed', function() { + manager.on('reconnect_failed', function () { expect(reconnects).to.be(2); socket.disconnect(); manager.close(); @@ -411,17 +414,17 @@ describe('connection', function() { }); }); - it('should not try to reconnect with incorrect port when reconnection disabled', function(done) { + it('should not try to reconnect with incorrect port when reconnection disabled', function (done) { var manager = io.Manager('http://localhost:9823', { reconnection: false }); - var cb = function() { + var cb = function () { socket.close(); expect().fail(); }; manager.on('reconnect_attempt', cb); - manager.on('connect_error', function(){ + manager.on('connect_error', function () { // set a timeout to let reconnection possibly fire - setTimeout(function() { + setTimeout(function () { socket.disconnect(); manager.close(); done(); @@ -432,9 +435,9 @@ describe('connection', function() { }); } - it('should emit date as string', function(done){ + it('should emit date as string', function (done) { var socket = io({ forceNew: true }); - socket.on('takeDate', function(data) { + socket.on('takeDate', function (data) { socket.close(); expect(data).to.be.a('string'); done(); @@ -442,9 +445,9 @@ describe('connection', function() { socket.emit('getDate'); }); - it('should emit date in object', function(done){ + it('should emit date in object', function (done) { var socket = io({ forceNew: true }); - socket.on('takeDateObj', function(data) { + socket.on('takeDateObj', function (data) { socket.close(); expect(data).to.be.an('object'); expect(data.date).to.be.a('string'); @@ -454,9 +457,9 @@ describe('connection', function() { }); if (!global.Blob && !global.ArrayBuffer) { - it('should get base64 data as a last resort', function(done) { + it('should get base64 data as a last resort', function (done) { var socket = io({ forceNew: true }); - socket.on('takebin', function(a) { + socket.on('takebin', function (a) { socket.disconnect(); expect(a.base64).to.be(true); expect(a.data).to.eql('YXNkZmFzZGY='); @@ -469,22 +472,22 @@ describe('connection', function() { if (global.ArrayBuffer) { var base64 = require('base64-arraybuffer'); - it('should get binary data (as an ArrayBuffer)', function(done){ + it('should get binary data (as an ArrayBuffer)', function (done) { var socket = io({ forceNew: true }); if (env.node) { socket.io.engine.binaryType = 'arraybuffer'; } socket.emit('doge'); - socket.on('doge', function(buffer){ + socket.on('doge', function (buffer) { expect(buffer instanceof ArrayBuffer).to.be(true); socket.disconnect(); done(); }); }); - it('should send binary data (as an ArrayBuffer)', function(done){ + it('should send binary data (as an ArrayBuffer)', function (done) { var socket = io({ forceNew: true }); - socket.on('buffack', function(){ + socket.on('buffack', function () { socket.disconnect(); done(); }); @@ -492,9 +495,9 @@ describe('connection', function() { socket.emit('buffa', buf); }); - it('should send binary data (as an ArrayBuffer) mixed with json', function(done) { + it('should send binary data (as an ArrayBuffer) mixed with json', function (done) { var socket = io({ forceNew: true }); - socket.on('jsonbuff-ack', function() { + socket.on('jsonbuff-ack', function () { socket.disconnect(); done(); }); @@ -502,9 +505,9 @@ describe('connection', function() { socket.emit('jsonbuff', {hello: 'lol', message: buf, goodbye: 'gotcha'}); }); - it('should send events with ArrayBuffers in the correct order', function(done) { + it('should send events with ArrayBuffers in the correct order', function (done) { var socket = io({ forceNew: true }); - socket.on('abuff2-ack', function() { + socket.on('abuff2-ack', function () { socket.disconnect(); done(); }); @@ -515,9 +518,9 @@ describe('connection', function() { } if (global.Blob && null != textBlobBuilder('xxx')) { - it('should send binary data (as a Blob)', function(done){ + it('should send binary data (as a Blob)', function (done) { var socket = io({ forceNew: true }); - socket.on('back', function(){ + socket.on('back', function () { socket.disconnect(); done(); }); @@ -525,9 +528,9 @@ describe('connection', function() { socket.emit('blob', blob); }); - it('should send binary data (as a Blob) mixed with json', function(done) { + it('should send binary data (as a Blob) mixed with json', function (done) { var socket = io({ forceNew: true }); - socket.on('jsonblob-ack', function() { + socket.on('jsonblob-ack', function () { socket.disconnect(); done(); }); @@ -535,9 +538,9 @@ describe('connection', function() { socket.emit('jsonblob', {hello: 'lol', message: blob, goodbye: 'gotcha'}); }); - it('should send events with Blobs in the correct order', function(done) { + it('should send events with Blobs in the correct order', function (done) { var socket = io({ forceNew: true }); - socket.on('blob3-ack', function() { + socket.on('blob3-ack', function () { socket.disconnect(); done(); }); diff --git a/test/socket.js b/test/socket.js index 6b8b2053..1fa0b6c5 100644 --- a/test/socket.js +++ b/test/socket.js @@ -1,12 +1,12 @@ var expect = require('expect.js'); var io = require('../'); -describe('socket', function(){ +describe('socket', function () { this.timeout(70000); - it('should have an accessible socket id equal to the engine.io socket id', function(done) { + it('should have an accessible socket id equal to the engine.io socket id', function (done) { var socket = io({ forceNew: true }); - socket.on('connect', function(){ + socket.on('connect', function () { expect(socket.id).to.be.ok(); expect(socket.id).to.eql(socket.io.engine.id); socket.disconnect(); @@ -14,10 +14,10 @@ describe('socket', function(){ }); }); - it('clears socket.id upon disconnection', function(done){ + it('clears socket.id upon disconnection', function (done) { var socket = io({ forceNew: true }); - socket.on('connect', function(){ - socket.on('disconnect', function(){ + socket.on('connect', function () { + socket.on('disconnect', function () { expect(socket.id).to.not.be.ok(); done(); }); @@ -26,25 +26,25 @@ describe('socket', function(){ }); }); - it('doesn\'t fire a connect_error if we force disconnect in opening state', function(done){ + it('doesn\'t fire a connect_error if we force disconnect in opening state', function (done) { var socket = io({ forceNew: true, timeout: 100 }); socket.disconnect(); - socket.on('connect_error', function(){ + socket.on('connect_error', function () { throw new Error('Unexpected'); }); - setTimeout(function(){ + setTimeout(function () { done(); }, 300); }); - it('should ping and pong with latency', function(done){ + it('should ping and pong with latency', function (done) { var socket = io({ forceNew: true }); - socket.on('connect', function(){ + socket.on('connect', function () { var pinged; - socket.once('ping', function(){ + socket.once('ping', function () { pinged = true; }); - socket.once('pong', function(ms){ + socket.once('pong', function (ms) { expect(pinged).to.be(true); expect(ms).to.be.a('number'); socket.disconnect(); @@ -53,16 +53,16 @@ describe('socket', function(){ }); }); - it('should change socket.id upon reconnection', function(done){ + it('should change socket.id upon reconnection', function (done) { var socket = io({ forceNew: true }); - socket.on('connect', function(){ + socket.on('connect', function () { var id = socket.id; - socket.on('reconnect_attempt', function(){ + socket.on('reconnect_attempt', function () { expect(socket.id).to.not.be.ok(); }); - socket.on('reconnect', function() { + socket.on('reconnect', function () { expect(socket.id).to.not.eql(id); socket.disconnect(); done(); @@ -72,10 +72,10 @@ describe('socket', function(){ }); }); - it('should enable compression by default', function(done){ + it('should enable compression by default', function (done) { var socket = io({ forceNew: true }); - socket.on('connect', function(){ - socket.io.engine.once('packetCreate', function(packet){ + socket.on('connect', function () { + socket.io.engine.once('packetCreate', function (packet) { expect(packet.options.compress).to.be(true); socket.disconnect(); done(); @@ -84,10 +84,10 @@ describe('socket', function(){ }); }); - it('should disable compression', function(done){ + it('should disable compression', function (done) { var socket = io({ forceNew: true }); - socket.on('connect', function(){ - socket.io.engine.once('packetCreate', function(packet){ + socket.on('connect', function () { + socket.io.engine.once('packetCreate', function (packet) { expect(packet.options.compress).to.be(false); socket.disconnect(); done(); @@ -96,10 +96,10 @@ describe('socket', function(){ }); }); - it('should store query string as a property', function(done){ - var socket = io('/abc', {query: {a:'b'}}); //passes in as a query obj - var socket2 = io('/abcd?b=c&d=e'); //passes in as a query string - var socket3 = io('/abc', {query: {'&a':'&=?a'}}); //checks that it encodes a string. + it('should store query string as a property', function (done) { + var socket = io('/abc', {query: {a: 'b'}}); // passes in as a query obj + var socket2 = io('/abcd?b=c&d=e'); // passes in as a query string + var socket3 = io('/abc', {query: {'&a': '&=?a'}}); // checks that it encodes a string expect(socket.query).to.be('a=b'); expect(socket2.query).to.be('b=c&d=e'); expect(socket3.query).to.be('%26a=%26%3D%3Fa'); diff --git a/test/support/server.js b/test/support/server.js index 08f55d03..bc871b0c 100644 --- a/test/support/server.js +++ b/test/support/server.js @@ -5,50 +5,50 @@ var io = require('socket.io'); var server = io(process.env.ZUUL_PORT || 3210, { pingInterval: 2000 }); var expect = require('expect.js'); -server.of('/foo').on('connection', function(){ +server.of('/foo').on('connection', function () { // register namespace }); -server.of('/timeout_socket').on('connection', function(){ +server.of('/timeout_socket').on('connection', function () { // register namespace }); -server.of('/valid').on('connection', function(){ +server.of('/valid').on('connection', function () { // register namespace }); -server.of('/asd').on('connection', function(){ +server.of('/asd').on('connection', function () { // register namespace }); -server.on('connection', function(socket){ +server.on('connection', function (socket) { // simple test - socket.on('hi', function(){ + socket.on('hi', function () { socket.emit('hi'); }); // ack tests - socket.on('ack', function(){ - socket.emit('ack', function(a, b){ - if (a == 5 && b.test) { + socket.on('ack', function () { + socket.emit('ack', function (a, b) { + if (a === 5 && b.test) { socket.emit('got it'); } }); }); - socket.on('getAckDate', function(data, cb){ + socket.on('getAckDate', function (data, cb) { cb(new Date()); }); - socket.on('getDate', function(){ + socket.on('getDate', function () { socket.emit('takeDate', new Date()); }); - socket.on('getDateObj', function(){ + socket.on('getDateObj', function () { socket.emit('takeDateObj', { date: new Date() }); }); - socket.on('getUtf8', function() { + socket.on('getUtf8', function () { socket.emit('takeUtf8', 'てすと'); socket.emit('takeUtf8', 'Я Б Г Д Ж Й'); socket.emit('takeUtf8', 'Ä ä Ü ü ß'); @@ -57,23 +57,23 @@ server.on('connection', function(socket){ }); // false test - socket.on('false', function(){ + socket.on('false', function () { socket.emit('false', false); }); // binary test - socket.on('doge', function(){ + socket.on('doge', function () { var buf = new Buffer('asdfasdf', 'utf8'); socket.emit('doge', buf); }); // expect receiving binary to be buffer - socket.on('buffa', function(a){ + socket.on('buffa', function (a) { if (Buffer.isBuffer(a)) socket.emit('buffack'); }); // expect receiving binary with mixed JSON - socket.on('jsonbuff', function(a) { + socket.on('jsonbuff', function (a) { expect(a.hello).to.eql('lol'); expect(Buffer.isBuffer(a.message)).to.be(true); expect(a.goodbye).to.eql('gotcha'); @@ -82,22 +82,22 @@ server.on('connection', function(socket){ // expect receiving buffers in order var receivedAbuff1 = false; - socket.on('abuff1', function(a) { + socket.on('abuff1', function (a) { expect(Buffer.isBuffer(a)).to.be(true); receivedAbuff1 = true; }); - socket.on('abuff2', function(a) { + socket.on('abuff2', function (a) { expect(receivedAbuff1).to.be(true); socket.emit('abuff2-ack'); }); // expect sent blob to be buffer - socket.on('blob', function(a){ + socket.on('blob', function (a) { if (Buffer.isBuffer(a)) socket.emit('back'); }); // expect sent blob mixed with json to be buffer - socket.on('jsonblob', function(a) { + socket.on('jsonblob', function (a) { expect(a.hello).to.eql('lol'); expect(Buffer.isBuffer(a.message)).to.be(true); expect(a.goodbye).to.eql('gotcha'); @@ -107,16 +107,16 @@ server.on('connection', function(socket){ // expect blobs sent in order to arrive in correct order var receivedblob1 = false; var receivedblob2 = false; - socket.on('blob1', function(a) { + socket.on('blob1', function (a) { expect(Buffer.isBuffer(a)).to.be(true); receivedblob1 = true; }); - socket.on('blob2', function(a) { + socket.on('blob2', function (a) { expect(receivedblob1).to.be(true); expect(a).to.eql('second'); receivedblob2 = true; }); - socket.on('blob3', function(a) { + socket.on('blob3', function (a) { expect(Buffer.isBuffer(a)).to.be(true); expect(receivedblob1).to.be(true); expect(receivedblob2).to.be(true); @@ -124,7 +124,7 @@ server.on('connection', function(socket){ }); // emit buffer to base64 receiving browsers - socket.on('getbin', function() { + socket.on('getbin', function () { var buf = new Buffer('asdfasdf', 'utf8'); socket.emit('takebin', buf); }); diff --git a/test/url.js b/test/url.js index cf27ccb8..ce3dfc6d 100644 --- a/test/url.js +++ b/test/url.js @@ -3,9 +3,8 @@ var loc = {}; var url = require('../lib/url'); var expect = require('expect.js'); -describe('url', function(){ - - it('works with undefined', function(){ +describe('url', function () { + it('works with undefined', function () { loc.hostname = 'woot.com'; loc.protocol = 'https:'; loc.port = 4005; @@ -16,7 +15,7 @@ describe('url', function(){ expect(parsed.port).to.be('4005'); }); - it('works with relative paths', function(){ + it('works with relative paths', function () { loc.hostname = 'woot.com'; loc.protocol = 'https:'; loc.port = 3000; @@ -27,7 +26,7 @@ describe('url', function(){ expect(parsed.port).to.be('3000'); }); - it('works with no protocol', function(){ + it('works with no protocol', function () { loc.protocol = 'http:'; var parsed = url('localhost:3000', loc); expect(parsed.host).to.be('localhost'); @@ -35,7 +34,7 @@ describe('url', function(){ expect(parsed.protocol).to.be('http'); }); - it('works with no schema', function(){ + it('works with no schema', function () { loc.protocol = 'http:'; var parsed = url('//localhost:3000', loc); expect(parsed.host).to.be('localhost'); @@ -43,7 +42,7 @@ describe('url', function(){ expect(parsed.protocol).to.be('http'); }); - it('forces ports for unique url ids', function(){ + it('forces ports for unique url ids', function () { var id1 = url('http://google.com:80/'); var id2 = url('http://google.com/'); var id3 = url('https://google.com/'); @@ -52,7 +51,7 @@ describe('url', function(){ expect(id2.id).to.not.be(id3.id); }); - it('identifies the namespace', function(){ + it('identifies the namespace', function () { loc.protocol = 'http:'; loc.hostname = 'woot.com'; @@ -61,7 +60,7 @@ describe('url', function(){ expect(url('http://google.com/').path).to.be('/'); }); - it('works with ipv6', function(){ + it('works with ipv6', function () { var parsed = url('http://[::1]'); expect(parsed.protocol).to.be('http'); expect(parsed.host).to.be('::1'); @@ -69,7 +68,7 @@ describe('url', function(){ expect(parsed.id).to.be('http://[::1]:80'); }); - it('works with ipv6 location', function(){ + it('works with ipv6 location', function () { loc.protocol = 'http:'; loc.hostname = '[::1]'; loc.port = ''; diff --git a/zuul.config.js b/zuul.config.js new file mode 100644 index 00000000..df894211 --- /dev/null +++ b/zuul.config.js @@ -0,0 +1,12 @@ + +module.exports = { + ui: 'mocha-bdd', + server: './test/support/server.js', + tunnel: { + type: 'ngrok', + authtoken: '6Aw8vTgcG5EvXdQywVvbh_3fMxvd4Q7dcL2caAHAFjV', + proto: 'tcp' + }, + builder: 'zuul-builder-webpack', + webpack: require('./support/webpack.config.js') +};