From 5d3f51a07bb8335ad285a9c9441dfeabf1329ce6 Mon Sep 17 00:00:00 2001 From: Antoine LUCAS Date: Fri, 30 Dec 2016 17:20:52 +1100 Subject: [PATCH 1/2] Use fs.readFileSync instead of require.resolve and then fallback to require.resolve if this fails. --- lib/index.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/index.js b/lib/index.js index c3687501d9..df7cd95fbc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,6 +5,8 @@ var http = require('http'); var read = require('fs').readFileSync; +var path = require('path'); +var exists = require('fs').existsSync; var engine = require('engine.io'); var client = require('socket.io-client'); var clientVersion = require('socket.io-client/package').version; @@ -98,11 +100,22 @@ Server.prototype.serveClient = function(v){ this._serveClient = v; if (v && !clientSource) { - clientSource = read(require.resolve('socket.io-client/dist/socket.io.min.js'), 'utf-8'); - try { - clientSourceMap = read(require.resolve('socket.io-client/dist/socket.io.js.map'), 'utf-8'); - } catch(err) { - debug('could not load sourcemap file'); + + var clientSourcePath = path.resolve(__dirname, './../../socket.io-client/dist/socket.io.min.js'); + var clientSourceMapPath = path.resolve(__dirname, './../../socket.io-client/dist/socket.io.min.js.map'); + if(!exists(clientSourcePath)) { + clientSource = read(require.resolve('socket.io-client/dist/socket.io.min.js'), 'utf-8'); + } else { + clientSource = read(clientSourcePath); + } + if(!exists(clientSourceMapPath)) { + try { + clientSourceMap = read(require.resolve('socket.io-client/dist/socket.io.js.map'), 'utf-8'); + } catch(err) { + debug('could not load sourcemap file'); + } + } else { + clientSourceMap = read(clientSourceMapPath); } } @@ -378,7 +391,7 @@ Server.prototype.onconnection = function(conn){ Server.prototype.of = function(name, fn){ if (String(name)[0] !== '/') name = '/' + name; - + var nsp = this.nsps[name]; if (!nsp) { debug('initializing namespace %s', name); @@ -392,7 +405,7 @@ Server.prototype.of = function(name, fn){ /** * Closes server connection * - * @param {Function} [fn] optional, called as `fn([err])` on error OR all conns closed + * @param {Function} [fn] optional, called as `fn([err])` on error OR all conns closed * @api public */ From ca325db8b04726b248c0746b7760200b32029527 Mon Sep 17 00:00:00 2001 From: Antoine LUCAS Date: Sun, 22 Jan 2017 08:23:43 +1100 Subject: [PATCH 2/2] - Implemented @darrachequesne's suggestion --- lib/index.js | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/lib/index.js b/lib/index.js index df7cd95fbc..c979e8b6c2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -98,27 +98,21 @@ Server.prototype.checkRequest = function(req, fn) { Server.prototype.serveClient = function(v){ if (!arguments.length) return this._serveClient; this._serveClient = v; - - if (v && !clientSource) { - - var clientSourcePath = path.resolve(__dirname, './../../socket.io-client/dist/socket.io.min.js'); - var clientSourceMapPath = path.resolve(__dirname, './../../socket.io-client/dist/socket.io.min.js.map'); - if(!exists(clientSourcePath)) { - clientSource = read(require.resolve('socket.io-client/dist/socket.io.min.js'), 'utf-8'); - } else { - clientSource = read(clientSourcePath); + var resolvePath = function(file){ + var filepath = path.resolve(__dirname, './../../', file); + if (exists(filepath)) { + return filepath; } - if(!exists(clientSourceMapPath)) { - try { - clientSourceMap = read(require.resolve('socket.io-client/dist/socket.io.js.map'), 'utf-8'); - } catch(err) { - debug('could not load sourcemap file'); - } - } else { - clientSourceMap = read(clientSourceMapPath); + return require.resolve(file); + }; + if (v && !clientSource) { + clientSource = read(resolvePath( 'socket.io-client/dist/socket.io.min.js'), 'utf-8'); + try { + clientSourceMap = read(resolvePath( 'socket.io-client/dist/socket.io.js.map'), 'utf-8'); + } catch(err) { + debug('could not load sourcemap file'); } } - return this; };