diff --git a/packages/core/integration-tests/test/hmr.js b/packages/core/integration-tests/test/hmr.js index 0fe734ce817..f36d2454466 100644 --- a/packages/core/integration-tests/test/hmr.js +++ b/packages/core/integration-tests/test/hmr.js @@ -48,14 +48,17 @@ describe('hmr', function() { b = bundler(path.join(__dirname, '/input/index.js'), { watch: true, - hmr: true + hmr: true, + hmrHostname: 'localhost' }); await b.bundle(); - ws = new WebSocket('ws://localhost:' + b.options.hmrPort); + ws = new WebSocket('ws://localhost:' + b.options.hmrPort, { + origin: 'http://localhost:1234' + }); const buildEnd = nextEvent(b, 'buildEnd'); - await sleep(100); + fs.writeFile( path.join(__dirname, '/input/local.js'), 'exports.a = 5;\nexports.b = 5;' @@ -79,6 +82,7 @@ describe('hmr', function() { b = bundler(path.join(__dirname, '/input/index.js'), { watch: true, hmr: true, + hmrHostname: 'localhost', target: 'node' }); await b.bundle(); @@ -103,7 +107,9 @@ describe('hmr', function() { }); await b.bundle(); - ws = new WebSocket('ws://localhost:' + b.options.hmrPort); + ws = new WebSocket('ws://localhost:' + b.options.hmrPort, { + origin: 'http://localhost:1234' + }); const buildEnd = nextEvent(b, 'buildEnd'); @@ -130,11 +136,14 @@ describe('hmr', function() { b = bundler(path.join(__dirname, '/input/index.js'), { watch: true, - hmr: true + hmr: true, + hmrHostname: 'localhost' }); await b.bundle(); - ws = new WebSocket('ws://localhost:' + b.options.hmrPort); + ws = new WebSocket('ws://localhost:' + b.options.hmrPort, { + origin: 'http://localhost:1234' + }); const buildEnd = nextEvent(b, 'buildEnd'); @@ -159,11 +168,14 @@ describe('hmr', function() { b = bundler(path.join(__dirname, '/input/index.js'), { watch: true, - hmr: true + hmr: true, + hmrHostname: 'localhost' }); await b.bundle(); - ws = new WebSocket('ws://localhost:' + b.options.hmrPort); + ws = new WebSocket('ws://localhost:' + b.options.hmrPort, { + origin: 'http://localhost:1234' + }); const buildEnd = nextEvent(b, 'buildEnd'); @@ -198,7 +210,8 @@ describe('hmr', function() { b = bundler(path.join(__dirname, '/input/index.js'), { watch: true, - hmr: true + hmr: true, + hmrHostname: 'localhost' }); await b.bundle(); @@ -210,7 +223,9 @@ describe('hmr', function() { await nextEvent(b, 'buildEnd'); await sleep(50); - ws = new WebSocket('ws://localhost:' + b.options.hmrPort); + ws = new WebSocket('ws://localhost:' + b.options.hmrPort, { + origin: 'http://localhost:1234' + }); let msg = JSON.parse(await nextEvent(ws, 'message')); assert.equal(msg.type, 'error'); }); @@ -223,11 +238,14 @@ describe('hmr', function() { b = bundler(path.join(__dirname, '/input/index.js'), { watch: true, - hmr: true + hmr: true, + hmrHostname: 'localhost' }); await b.bundle(); - ws = new WebSocket('ws://localhost:' + b.options.hmrPort); + ws = new WebSocket('ws://localhost:' + b.options.hmrPort, { + origin: 'http://localhost:1234' + }); const firstBuildEnd = nextEvent(b, 'buildEnd'); @@ -264,7 +282,8 @@ describe('hmr', function() { b = bundler(path.join(__dirname, '/input/index.js'), { watch: true, - hmr: true + hmr: true, + hmrHostname: 'localhost' }); let bundle = await b.bundle(); let outputs = []; @@ -326,7 +345,8 @@ describe('hmr', function() { b = bundler(path.join(__dirname, '/input/index.js'), { watch: true, - hmr: true + hmr: true, + hmrHostname: 'localhost' }); let bundle = await b.bundle(); let outputs = []; @@ -367,7 +387,8 @@ describe('hmr', function() { b = bundler(path.join(__dirname, '/input/index.js'), { watch: true, - hmr: true + hmr: true, + hmrHostname: 'localhost' }); let bundle = await b.bundle(); let outputs = []; @@ -439,7 +460,8 @@ describe('hmr', function() { b = bundler(path.join(__dirname, '/input/index.js'), { watch: true, - hmr: true + hmr: true, + hmrHostname: 'localhost' }); let bundle = await b.bundle(); @@ -480,7 +502,8 @@ describe('hmr', function() { b = bundler(path.join(__dirname, '/input/index.js'), { watch: true, - hmr: true + hmr: true, + hmrHostname: 'localhost' }); let bundle = await b.bundle(); @@ -538,11 +561,13 @@ describe('hmr', function() { b = bundler(path.join(__dirname, '/input/index.js'), { watch: true, hmr: true, + hmrHostname: 'localhost', https: true }); await b.bundle(); ws = new WebSocket('wss://localhost:' + b.options.hmrPort, { + origin: 'http://localhost:1234', rejectUnauthorized: false }); @@ -572,6 +597,7 @@ describe('hmr', function() { b = bundler(path.join(__dirname, '/input/index.js'), { watch: true, hmr: true, + hmrHostname: 'localhost', https: { key: path.join(__dirname, '/integration/https/private.pem'), cert: path.join(__dirname, '/integration/https/primary.crt') @@ -580,6 +606,7 @@ describe('hmr', function() { await b.bundle(); ws = new WebSocket('wss://localhost:' + b.options.hmrPort, { + origin: 'http://localhost:1234', rejectUnauthorized: false }); diff --git a/packages/core/parcel-bundler/src/HMRServer.js b/packages/core/parcel-bundler/src/HMRServer.js index 4ce9d7d25bf..a8b1281ee31 100644 --- a/packages/core/parcel-bundler/src/HMRServer.js +++ b/packages/core/parcel-bundler/src/HMRServer.js @@ -21,9 +21,10 @@ class HMRServer { }; if (options.hmrHostname) { - websocketOptions.origin = `${options.https ? 'https' : 'http'}://${ - options.hmrHostname - }`; + websocketOptions.verifyClient = info => { + const originator = new URL(info.origin); + return options.hmrHostname === originator.hostname; + }; } this.wss = new WebSocket.Server(websocketOptions);