Skip to content

feat/refactor(server): move CLI server start into API listen method #2028

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

Closed
wants to merge 12 commits into from
Closed
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
60 changes: 5 additions & 55 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

/* eslint-disable no-shadow, no-console */

const fs = require('fs');
const net = require('net');
const debug = require('debug')('webpack-dev-server');
const importLocal = require('import-local');
const yargs = require('yargs');
Expand All @@ -15,7 +13,6 @@ const setupExitSignals = require('../lib/utils/setupExitSignals');
const colors = require('../lib/utils/colors');
const processOptions = require('../lib/utils/processOptions');
const createLogger = require('../lib/utils/createLogger');
const findPort = require('../lib/utils/findPort');
const getVersions = require('../lib/utils/getVersions');
const options = require('./options');

Expand Down Expand Up @@ -115,58 +112,11 @@ function startDevServer(config, options) {
throw err;
}

if (options.socket) {
server.listeningApp.on('error', (e) => {
if (e.code === 'EADDRINUSE') {
const clientSocket = new net.Socket();

clientSocket.on('error', (err) => {
if (err.code === 'ECONNREFUSED') {
// No other server listening on this socket so it can be safely removed
fs.unlinkSync(options.socket);

server.listen(options.socket, options.host, (error) => {
if (error) {
throw error;
}
});
}
});

clientSocket.connect({ path: options.socket }, () => {
throw new Error('This socket is already used');
});
}
});
Copy link
Member

Choose a reason for hiding this comment

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

I think we can solve this without new method, it is very inconsistently with node api for servers, we can move this code without new method, just listen error event and handle this inside server class

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do you mean handle it inside the listen(port, hostname, fn) method, rather than making a new method?


server.listen(options.socket, options.host, (err) => {
if (err) {
throw err;
}

// chmod 666 (rw rw rw)
const READ_WRITE = 438;

fs.chmod(options.socket, READ_WRITE, (err) => {
if (err) {
throw err;
}
});
});
} else {
findPort(options.port)
.then((port) => {
options.port = port;
server.listen(options.port, options.host, (err) => {
if (err) {
throw err;
}
});
})
.catch((err) => {
throw err;
});
}
server.listen(options.socket || options.port, options.host, (err) => {
if (err) {
throw err;
}
});
}

processOptions(config, argv, (config, options) => {
Expand Down
97 changes: 94 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
func-names
*/
const fs = require('fs');
const net = require('net');
const path = require('path');
const tls = require('tls');
const url = require('url');
Expand All @@ -31,6 +32,8 @@ const createDomain = require('./utils/createDomain');
const runBonjour = require('./utils/runBonjour');
const routes = require('./utils/routes');
const getSocketServerImplementation = require('./utils/getSocketServerImplementation');
const findPort = require('./utils/findPort');
const tryParseInt = require('./utils/tryParseInt');
const schema = require('./options.json');

// Workaround for node ^8.6.0, ^9.0.0
Expand Down Expand Up @@ -741,23 +744,111 @@ class Server {
listen(port, hostname, fn) {
this.hostname = hostname;

return this.listeningApp.listen(port, hostname, (err) => {
const setupCallback = () => {
this.createSocketServer();

if (this.options.bonjour) {
runBonjour(this.options);
}

this.showStatus();
};

if (fn) {
// between setupCallback and userCallback should be any other needed handling,
// specifically so that things are done in the right order to prevent
// backwards compatability issues
let userCallbackCalled = false;
const userCallback = (err) => {
if (fn && !userCallbackCalled) {
userCallbackCalled = true;
fn.call(this.listeningApp, err);
}
};

const onListeningCallback = () => {
if (typeof this.options.onListening === 'function') {
this.options.onListening(this);
}
});
};

const fullCallback = (err) => {
setupCallback();
userCallback(err);
onListeningCallback();
};

// try to follow the Node standard in terms of deciding
// whether this is a socket or a port that we will listen on:
// https://github.com/nodejs/node/blob/64219741218aa87e259cf8257596073b8e747f0a/lib/net.js#L196
if (typeof port === 'string' && tryParseInt(port) === null) {
// in this case the "port" argument is actually a socket path
const socket = port;
// set this so that status helper can identify how the project is being run correctly
this.options.socket = socket;

const chmodSocket = (done) => {
// chmod 666 (rw rw rw) - octal
const READ_WRITE = 438;
fs.chmod(socket, READ_WRITE, done);
};

const startSocket = () => {
this.listeningApp.on('error', (err) => {
userCallback(err);
});

this.listeningApp.listen(socket, hostname, (err) => {
if (err) {
fullCallback(err);
} else {
chmodSocket(fullCallback);
}
});
};

fs.access(socket, fs.constants.F_OK, (err) => {
if (err) {
// file does not exist
startSocket();
} else {
// file exists

const clientSocket = new net.Socket();

clientSocket.on('error', (err) => {
if (err.code === 'ECONNREFUSED' || err.code === 'ENOTSOCK') {
// No other server listening on this socket so it can be safely removed
fs.unlinkSync(socket);

startSocket();
}
});

clientSocket.connect({ path: socket }, () => {
// if a client socket successfully connects to the given socket path,
// it means that the socket is in use
const err = new Error('This socket is already used');
clientSocket.destroy();
// do not call onListening or the setup method, since the server
// cannot start listening on a used socket
userCallback(err);
});
}
});
} else {
findPort(port)
.then((resPort) => {
// we need to set this because runBonjour uses this.options.port
this.options.port = resPort;
this.listeningApp.listen(resPort, hostname, fullCallback);
})
.catch((err) => {
// if there is no available port, the server can't start
userCallback(err);
});
}

return this.listeningApp;
}

close(cb) {
Expand Down
Loading