Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix port detection lag on OS X with Firewall enabled #319

Merged
merged 1 commit into from
Aug 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;