Skip to content

fix listen-socket #1869

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

Merged
merged 1 commit into from
May 14, 2019
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
14 changes: 13 additions & 1 deletion bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,19 @@ function startDevServer(config, options) {
}
});

runServer();
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 if (options.port) {
runServer();
} else {
Expand Down
46 changes: 16 additions & 30 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,21 @@ class Server {
return false;
}

showStatus() {
const suffix =
this.options.inline !== false || this.options.lazy === true
? '/'
: '/webpack-dev-server/';
const uri = `${createDomain(this.options, this.listeningApp)}${suffix}`;

status(
uri,
this.options,
this.log,
this.options.stats && this.options.stats.colors
);
}

// delegate listen call and init sockjs
listen(port, hostname, fn) {
this.hostname = hostname;
Expand Down Expand Up @@ -816,36 +831,7 @@ class Server {
runBonjour(this.options);
}

const showStatus = () => {
const suffix =
this.options.inline !== false || this.options.lazy === true
? '/'
: '/webpack-dev-server/';

const uri = `${createDomain(this.options, this.listeningApp)}${suffix}`;

status(
uri,
this.options,
this.log,
this.options.stats && this.options.stats.colors
);
};

if (this.options.socket) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Put this lines(L835 - L845) back to /bin.

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

fs.chmod(this.options.socket, READ_WRITE, (err) => {
if (err) {
throw err;
}

showStatus();
});
} else {
showStatus();
}
this.showStatus();

if (fn) {
fn.call(this.listeningApp, err);
Expand Down
56 changes: 37 additions & 19 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@
/* eslint-disable
array-bracket-spacing,
*/
const path = require('path');
const { unlink } = require('fs');
const { join, resolve } = require('path');
const execa = require('execa');
const runDevServer = require('./helpers/run-webpack-dev-server');

const httpsCertificateDirectory = path.join(
__dirname,
'fixtures/https-certificate'
);
const caPath = path.join(httpsCertificateDirectory, 'ca.pem');
const pfxPath = path.join(httpsCertificateDirectory, 'server.pfx');
const keyPath = path.join(httpsCertificateDirectory, 'server.key');
const certPath = path.join(httpsCertificateDirectory, 'server.crt');
const httpsCertificateDirectory = join(__dirname, 'fixtures/https-certificate');
const caPath = join(httpsCertificateDirectory, 'ca.pem');
const pfxPath = join(httpsCertificateDirectory, 'server.pfx');
const keyPath = join(httpsCertificateDirectory, 'server.key');
const certPath = join(httpsCertificateDirectory, 'server.crt');

describe('CLI', () => {
it('--progress', (done) => {
runDevServer('--progress')
.then((output) => {
expect(output.code).toEqual(0);
expect(output.stderr.indexOf('0% compiling') >= 0).toBe(true);
expect(output.stderr.includes('0% compiling')).toBe(true);
done();
})
.catch(done);
Expand All @@ -31,7 +29,7 @@ describe('CLI', () => {
runDevServer('--bonjour')
.then((output) => {
expect(output.code).toEqual(0);
expect(output.stdout.indexOf('Bonjour') >= 0).toBe(true);
expect(output.stdout.includes('Bonjour')).toBe(true);
done();
})
.catch(done);
Expand All @@ -41,7 +39,7 @@ describe('CLI', () => {
runDevServer('--https')
.then((output) => {
expect(output.code).toEqual(0);
expect(output.stdout.indexOf('Project is running at') >= 0).toBe(true);
expect(output.stdout.includes('Project is running at')).toBe(true);
done();
})
.catch(done);
Expand All @@ -53,7 +51,7 @@ describe('CLI', () => {
)
.then((output) => {
expect(output.code).toEqual(0);
expect(output.stdout.indexOf('Project is running at') >= 0).toBe(true);
expect(output.stdout.includes('Project is running at')).toBe(true);
done();
})
.catch(done);
Expand Down Expand Up @@ -82,9 +80,29 @@ describe('CLI', () => {
.catch(done);
});

// The Unix socket to listen to (instead of a host).
it('--socket', (done) => {
const socketPath = join('.', 'webpack.sock');

runDevServer(`--socket ${socketPath}`)
.then((output) => {
expect(output.code).toEqual(0);

if (process.platform === 'win32') {
done();
} else {
expect(output.stdout.includes(socketPath)).toBe(true);
unlink(socketPath, () => {
done();
});
}
})
.catch(done);
});

it('should exit the process when SIGINT is detected', (done) => {
const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js');
const examplePath = path.resolve(__dirname, '../examples/cli/public');
const cliPath = resolve(__dirname, '../bin/webpack-dev-server.js');
const examplePath = resolve(__dirname, '../examples/cli/public');
const cp = execa('node', [cliPath], { cwd: examplePath });

cp.stdout.on('data', (data) => {
Expand All @@ -101,8 +119,8 @@ describe('CLI', () => {
});

it('should exit the process when SIGINT is detected, even before the compilation is done', (done) => {
const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js');
const examplePath = path.resolve(__dirname, '../examples/cli/public');
const cliPath = resolve(__dirname, '../bin/webpack-dev-server.js');
const examplePath = resolve(__dirname, '../examples/cli/public');
const cp = execa('node', [cliPath], { cwd: examplePath });
let killed = false;

Expand All @@ -120,8 +138,8 @@ describe('CLI', () => {
});

it('should use different random port when multiple instances are started on different processes', (done) => {
const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js');
const examplePath = path.resolve(__dirname, '../examples/cli/public');
const cliPath = resolve(__dirname, '../bin/webpack-dev-server.js');
const examplePath = resolve(__dirname, '../examples/cli/public');

const cp = execa('node', [cliPath], { cwd: examplePath });
const cp2 = execa('node', [cliPath], { cwd: examplePath });
Expand Down