Skip to content

Commit

Permalink
Use fs.readFileSync instead of require.resolve and then fallback to r…
Browse files Browse the repository at this point in the history
…equire.resolve if this fails.
  • Loading branch information
a-lucas committed Dec 30, 2016
1 parent 1f59e45 commit 5d3f51a
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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
*/

Expand Down

0 comments on commit 5d3f51a

Please sign in to comment.