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

Temporarily fork detect-port #2147

Merged
merged 4 commits into from
May 14, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
66 changes: 66 additions & 0 deletions packages/react-dev-utils/detectPort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have the original license, not FB's.

Unfortunately this makes our situation more complicated as I'd like to avoid mixing licenses in one package.

Ideally we should either use original package, or use a fork from either of our personal repos (so that we don't bring a different license here).

While we're looking at it, can we check what Zeit's serve uses? Maybe we can just use that (since I think it worked with 0.0.0.0 fine).

Copy link
Contributor Author

@Timer Timer May 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was going to ask you about the licensing before merging. It's MIT.

Serve uses detect-port, meaning it has the same issue we do.

* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

const debug = require('debug')('detect-port');
const net = require('net');

module.exports = (port, host, callback) => {
if (typeof port === 'function') {
callback = port;
port = null;
}
port = parseInt(port) || 0;
let maxPort = port + 10;
if (maxPort > 65535) {
maxPort = 65535;
}
debug('detect free port between [%s, %s)', port, maxPort);
if (typeof callback === 'function') {
return tryListen(host, port, maxPort, callback);
}
// promise
return new Promise(resolve => {
tryListen(host, port, maxPort, (_, realPort) => {
resolve(realPort);
});
});
};

function tryListen(host, port, maxPort, callback) {
const server = new net.Server();

server.on('error', err => {
debug('listen %s error: %s', port, err);
if (port === 0) {
return callback(err);
}

port++;
if (port >= maxPort) {
debug(
'port: %s >= maxPort: %s, give up and use random port',
port,
maxPort
);
port = 0;
maxPort = 0;
}
server.close();
return tryListen(host, port, maxPort, callback);
});

server.listen({ port, host }, () => {
port = server.address().port;
server.close();
debug('get free port: %s', port);
callback(null, port);
});
}
1 change: 0 additions & 1 deletion packages/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"connect-history-api-fallback": "1.3.0",
"cross-spawn": "4.0.2",
"css-loader": "0.28.0",
"detect-port": "1.1.1",
"dotenv": "2.0.0",
"eslint": "3.16.1",
"eslint-config-react-app": "^0.6.1",
Expand Down
12 changes: 6 additions & 6 deletions packages/react-scripts/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require('../config/env');

const fs = require('fs');
const chalk = require('chalk');
const detect = require('detect-port');
const detect = require('react-dev-utils/detectPort');
const WebpackDevServer = require('webpack-dev-server');
const clearConsole = require('react-dev-utils/clearConsole');
const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
Expand All @@ -48,10 +48,10 @@ if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {

// Tools like Cloud9 rely on this.
const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
const HOST = process.env.HOST || '0.0.0.0';

function run(port) {
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
const host = process.env.HOST || '0.0.0.0';

// Create a webpack compiler that is configured with custom messages.
const compiler = createWebpackCompiler(
Expand All @@ -63,7 +63,7 @@ function run(port) {
console.log();
console.log('The app is running at:');
console.log();
console.log(` ${chalk.cyan(`${protocol}://${host}:${port}/`)}`);
console.log(` ${chalk.cyan(`${protocol}://${HOST}:${port}/`)}`);
console.log();
console.log('Note that the development build is not optimized.');
console.log(
Expand All @@ -80,7 +80,7 @@ function run(port) {
addWebpackMiddleware(devServer)
.then(() => {
// Launch WebpackDevServer.
devServer.listen(port, host, err => {
devServer.listen(port, HOST, err => {
if (err) {
return console.log(err);
}
Expand All @@ -91,7 +91,7 @@ function run(port) {
console.log(chalk.cyan('Starting the development server...'));
console.log();

openBrowser(`${protocol}://${host}:${port}/`);
openBrowser(`${protocol}://${HOST}:${port}/`);
});
})
.catch(e => {
Expand All @@ -105,7 +105,7 @@ function run(port) {

// We attempt to use the default port but if it is busy, we offer the user to
// run on a different port. `detect()` Promise resolves to the next free port.
detect(DEFAULT_PORT).then(port => {
detect(DEFAULT_PORT, HOST).then(port => {
if (port === DEFAULT_PORT) {
run(port);
return;
Expand Down