From d8af2d925182ab746be5db36d4aeb1d17795a350 Mon Sep 17 00:00:00 2001 From: roblan Date: Mon, 14 Oct 2019 13:50:02 +0200 Subject: [PATCH] fix: add hostname and port to bonjour name to prevent name collisions (#2276) --- lib/utils/runBonjour.js | 3 ++- ...r.test.js.snap => runBonjour.test.js.snap} | 2 +- ... runBonjour.test.js => runBonjour.test.js} | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) rename test/server/utils/__snapshots__/{ runBonjour.test.js.snap => runBonjour.test.js.snap} (80%) rename test/server/utils/{ runBonjour.test.js => runBonjour.test.js} (63%) diff --git a/lib/utils/runBonjour.js b/lib/utils/runBonjour.js index 8085eb8215..3add365d53 100644 --- a/lib/utils/runBonjour.js +++ b/lib/utils/runBonjour.js @@ -2,9 +2,10 @@ function runBonjour({ port }) { const bonjour = require('bonjour')(); + const os = require('os'); bonjour.publish({ - name: 'Webpack Dev Server', + name: `Webpack Dev Server ${os.hostname()}:${port}`, port, type: 'http', subtypes: ['webpack'], diff --git a/test/server/utils/__snapshots__/ runBonjour.test.js.snap b/test/server/utils/__snapshots__/runBonjour.test.js.snap similarity index 80% rename from test/server/utils/__snapshots__/ runBonjour.test.js.snap rename to test/server/utils/__snapshots__/runBonjour.test.js.snap index e0dab41696..ce580fbb82 100644 --- a/test/server/utils/__snapshots__/ runBonjour.test.js.snap +++ b/test/server/utils/__snapshots__/runBonjour.test.js.snap @@ -2,7 +2,7 @@ exports[`runBonjour should call bonjour.publish 1`] = ` Object { - "name": "Webpack Dev Server", + "name": "Webpack Dev Server hostname:1111", "port": 1111, "subtypes": Array [ "webpack", diff --git a/test/server/utils/ runBonjour.test.js b/test/server/utils/runBonjour.test.js similarity index 63% rename from test/server/utils/ runBonjour.test.js rename to test/server/utils/runBonjour.test.js index 66730dc326..e416a7fa62 100644 --- a/test/server/utils/ runBonjour.test.js +++ b/test/server/utils/runBonjour.test.js @@ -2,6 +2,12 @@ const runBonjour = require('../../../lib/utils/runBonjour'); +jest.mock('os', () => { + return { + hostname: () => 'hostname', + }; +}); + describe('runBonjour', () => { let mock; let publish = jest.fn(); @@ -32,4 +38,17 @@ describe('runBonjour', () => { expect(publish.mock.calls[0][0]).toMatchSnapshot(); }); + + it('should call bonjour.publish with different name for different ports', () => { + runBonjour({ + port: 1111, + }); + runBonjour({ + port: 2222, + }); + + const calls = publish.mock.calls; + + expect(calls[0][0].name).not.toEqual(calls[1][0].name); + }); });