Skip to content

Commit

Permalink
Fix port detection lag on OS X with Firewall enabled (#319)
Browse files Browse the repository at this point in the history
This fixes #302 by temporarily forking `detect-port`.
Hopefully this fix will be merged into the original project, and we can refer to it again.
  • Loading branch information
gaearon authored Aug 1, 2016
1 parent 6e1887f commit f7011e0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"chalk": "1.1.3",
"cross-spawn": "4.0.0",
"css-loader": "0.23.1",
"detect-port": "0.1.4",
"eslint": "3.1.1",
"eslint-loader": "1.4.1",
"eslint-plugin-flowtype": "2.4.0",
Expand Down
1 change: 1 addition & 0 deletions scripts/eject.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ prompt(
path.join('scripts', 'build.js'),
path.join('scripts', 'start.js'),
path.join('scripts', 'utils', 'chrome.applescript'),
path.join('scripts', 'utils', 'detectPort.js'),
path.join('scripts', 'utils', 'prompt.js')
];

Expand Down
2 changes: 1 addition & 1 deletion scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var execSync = require('child_process').execSync;
var opn = require('opn');
var detect = require('detect-port');
var detect = require('./utils/detectPort');
var prompt = require('./utils/prompt');
var config = require('../config/webpack.config.dev');

Expand Down
86 changes: 86 additions & 0 deletions scripts/utils/detectPort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* ================================================================
* detect-port by xdf(xudafeng[at]126.com)
*
* first created at : Tue Mar 17 2015 00:16:10 GMT+0800 (CST)
*
* ================================================================
* Copyright xdf
*
* Licensed under the MIT License
* You may not use this file except in compliance with the License.
*
* ================================================================ */

// We are forking this temporarily to resolve
// https://github.com/facebookincubator/create-react-app/issues/302.

// We can replace this fork with `detect-port` package when this is merged:
// https://github.com/xudafeng/detect-port/pull/4.

'use strict';

var net = require('net');

var inject = function(port) {
var options = global.__detect ? global.__detect.options : {};

if (options.verbose) {
console.log('port %d was occupied', port);
}
};

function detect(port, fn) {

var _detect = function(port) {
return new Promise(function(resolve, reject) {
var socket = new net.Socket();
socket.once('error', function() {
socket.removeAllListeners('connect');
socket.removeAllListeners('error');
socket.end();
socket.destroy();
socket.unref();
var server = new net.Server();
server.on('error', function() {
inject(port);
port++;
resolve(_detect(port));
});

server.listen(port, function() {
server.once('close', function() {
resolve(port);
});
server.close();
});
});
socket.once('connect', function() {
inject(port);
port++;
resolve(_detect(port));
socket.removeAllListeners('connect');
socket.removeAllListeners('error');
socket.end();
socket.destroy();
socket.unref();
});
socket.connect({
port: port
});
});
}

var _detect_with_cb = function(_fn) {
_detect(port)
.then(function(result) {
_fn(null, result);
})
.catch(function(e) {
_fn(e);
});
};

return fn ? _detect_with_cb(fn) : _detect(port);
}

module.exports = detect;

0 comments on commit f7011e0

Please sign in to comment.