Skip to content

Commit

Permalink
feat: support bonjour options (#3202)
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv authored Apr 20, 2021
1 parent 4b487f8 commit 5534583
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 29 deletions.
9 changes: 8 additions & 1 deletion lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@
},
"properties": {
"bonjour": {
"type": "boolean",
"anyOf": [
{
"type": "boolean"
},
{
"type": "object"
}
],
"description": "Broadcasts the server via ZeroConf networking on start. https://webpack.js.org/configuration/dev-server/#devserverbonjour"
},
"client": {
Expand Down
7 changes: 4 additions & 3 deletions lib/utils/runBonjour.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

function runBonjour({ port }) {
function runBonjour(options) {
const bonjour = require('bonjour')();
const os = require('os');

const { https, port } = options;
bonjour.publish({
name: `Webpack Dev Server ${os.hostname()}:${port}`,
port,
type: 'http',
type: https ? 'https' : 'http',
subtypes: ['webpack'],
...options.bonjour,
});

process.on('exit', () => {
Expand Down
9 changes: 7 additions & 2 deletions test/__snapshots__/validate-options.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

exports[`options validate should throw an error on the "bonjour" option with '' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.bonjour should be a boolean.
-> Broadcasts the server via ZeroConf networking on start. https://webpack.js.org/configuration/dev-server/#devserverbonjour"
- configuration.bonjour should be one of these:
boolean | object { … }
-> Broadcasts the server via ZeroConf networking on start. https://webpack.js.org/configuration/dev-server/#devserverbonjour
Details:
* configuration.bonjour should be a boolean.
* configuration.bonjour should be an object:
object { … }"
`;

exports[`options validate should throw an error on the "client" option with '{"host":true,"path":"","port":8080}' value 1`] = `
Expand Down
9 changes: 7 additions & 2 deletions test/__snapshots__/validate-options.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

exports[`options validate should throw an error on the "bonjour" option with '' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.bonjour should be a boolean.
-> Broadcasts the server via ZeroConf networking on start. https://webpack.js.org/configuration/dev-server/#devserverbonjour"
- configuration.bonjour should be one of these:
boolean | object { … }
-> Broadcasts the server via ZeroConf networking on start. https://webpack.js.org/configuration/dev-server/#devserverbonjour
Details:
* configuration.bonjour should be a boolean.
* configuration.bonjour should be an object:
object { … }"
`;

exports[`options validate should throw an error on the "client" option with '{"host":true,"path":"","port":8080}' value 1`] = `
Expand Down
106 changes: 86 additions & 20 deletions test/server/bonjour-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,97 @@ describe('bonjour option', () => {
});
});

beforeEach((done) => {
server = testServer.start(
config,
{
bonjour: true,
afterEach(() => {
mockPublish.mockReset();
mockUnpublishAll.mockReset();
});

describe('http', () => {
beforeEach((done) => {
server = testServer.start(
config,
{
bonjour: true,
port,
},
done
);
});

afterEach((done) => {
server.close(done);
});

it('should call bonjour with correct params', () => {
expect(mockPublish).toHaveBeenCalledTimes(1);
expect(mockPublish).toHaveBeenCalledWith({
name: `Webpack Dev Server ${os.hostname()}:${port}`,
port,
},
done
);
type: 'http',
subtypes: ['webpack'],
});
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
});
});

afterEach((done) => {
mockPublish.mockReset();
mockUnpublishAll.mockReset();
server.close(done);
describe('https option', () => {
beforeEach((done) => {
server = testServer.start(
config,
{
bonjour: true,
port,
https: true,
},
done
);
});

afterEach((done) => {
server.close(done);
});

it('bonjour should use https when passed in option', () => {
expect(mockPublish).toHaveBeenCalledTimes(1);
expect(mockPublish).toHaveBeenCalledWith({
name: `Webpack Dev Server ${os.hostname()}:${port}`,
port,
type: 'https',
subtypes: ['webpack'],
});
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
});
});

it('should call bonjour with correct params', () => {
expect(mockPublish).toHaveBeenCalledTimes(1);
expect(mockPublish).toHaveBeenCalledWith({
name: `Webpack Dev Server ${os.hostname()}:${port}`,
port,
type: 'http',
subtypes: ['webpack'],
describe('bonjour object', () => {
beforeEach((done) => {
server = testServer.start(
config,
{
bonjour: {
type: 'https',
protocol: 'udp',
},
port,
},
done
);
});

afterEach((done) => {
server.close(done);
});

it('applies bonjour options', () => {
expect(mockPublish).toHaveBeenCalledTimes(1);
expect(mockPublish).toHaveBeenCalledWith({
name: `Webpack Dev Server ${os.hostname()}:${port}`,
port,
type: 'https',
protocol: 'udp',
subtypes: ['webpack'],
});
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
});
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
});
});
2 changes: 1 addition & 1 deletion test/validate-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const tests = {
failure: [false],
},
bonjour: {
success: [false, true],
success: [false, true, { type: 'https' }],
failure: [''],
},
client: {
Expand Down

0 comments on commit 5534583

Please sign in to comment.