Skip to content

Commit

Permalink
[fix] Use path.resolve by default and require.resolve as a fallback (s…
Browse files Browse the repository at this point in the history
…ocketio#2797)

Browserify doesn't support require.resolve, and as a consequence, makes nexe fail the compilation. This PR attempts to get the path of the socket.io-client file via path.resolve and falls back to the original require.resolve if this file cannot be found.
  • Loading branch information
a-lucas authored and darrachequesne committed Jan 22, 2017
1 parent 09ea21c commit 5ae65b5
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 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 @@ -96,16 +98,21 @@ Server.prototype.checkRequest = function(req, fn) {
Server.prototype.serveClient = function(v){
if (!arguments.length) return this._serveClient;
this._serveClient = v;

var resolvePath = function(file){
var filepath = path.resolve(__dirname, './../../', file);
if (exists(filepath)) {
return filepath;
}
return require.resolve(file);
};
if (v && !clientSource) {
clientSource = read(require.resolve('socket.io-client/dist/socket.io.min.js'), 'utf-8');
clientSource = read(resolvePath( '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');
clientSourceMap = read(resolvePath( 'socket.io-client/dist/socket.io.js.map'), 'utf-8');
} catch(err) {
debug('could not load sourcemap file');
}
}

return this;
};

Expand Down Expand Up @@ -378,7 +385,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 +399,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 5ae65b5

Please sign in to comment.