diff --git a/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js b/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js index b2d83c053d3..2f5eab91061 100644 --- a/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js +++ b/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js @@ -192,7 +192,7 @@ describe.each(['HTTP', 'HTTPS'])( } }); - describe.each(['10.0.2.2', '10.0.3.2'])( + describe.each(['10.0.2.2', '10.0.3.2', '127.0.0.1'])( '%s aliasing to and from localhost', sourceHost => { test('in source map fetching during Debugger.scriptParsed', async () => { diff --git a/packages/dev-middleware/src/inspector-proxy/Device.js b/packages/dev-middleware/src/inspector-proxy/Device.js index b9481dac910..1ecfd66f653 100644 --- a/packages/dev-middleware/src/inspector-proxy/Device.js +++ b/packages/dev-middleware/src/inspector-proxy/Device.js @@ -37,8 +37,21 @@ const debug = require('debug')('Metro:InspectorProxy'); const PAGES_POLLING_INTERVAL = 1000; -// Android's stock emulator and other emulators such as genymotion use a standard localhost alias. -const EMULATOR_LOCALHOST_ADDRESSES: Array = ['10.0.2.2', '10.0.3.2']; +// Replace hosts appearing in the `url` and `sourceMapURL` fields of +// `Debugger.scriptParsed`, and back again in messages from the debugger, +// to account for device/debugger/proxy running on different networks. +const REWRITE_HOSTS_TO_LOCALHOST: Array = [ + // A device may retrieve a bundle through 127.0.0.1 via a (SSH) tunnel, but + // the (remote) Metro server may be on a host without an IPv4 loopback, so + // 127.0.0.1 may not be addressible locally for (e.g., for source map + // fetching). Replacing with the more general 'localhost' should always be + // safe while also more compatible with IPv6-only setups. + '127.0.0.1', + // Android's stock emulator and other emulators such as genymotion use a + // standard localhost alias. + '10.0.2.2', + '10.0.3.2', +]; // Prefix for script URLs that are alphanumeric IDs. See comment in #processMessageFromDeviceLegacy method for // more details. @@ -621,15 +634,14 @@ export default class Device { ) { const params = payload.params; if ('sourceMapURL' in params) { - for (let i = 0; i < EMULATOR_LOCALHOST_ADDRESSES.length; ++i) { - const address = EMULATOR_LOCALHOST_ADDRESSES[i]; - if (params.sourceMapURL.includes(address)) { + for (const hostToRewrite of REWRITE_HOSTS_TO_LOCALHOST) { + if (params.sourceMapURL.includes(hostToRewrite)) { // $FlowFixMe[cannot-write] payload.params.sourceMapURL = params.sourceMapURL.replace( - address, + hostToRewrite, 'localhost', ); - debuggerInfo.originalSourceURLAddress = address; + debuggerInfo.originalSourceURLAddress = hostToRewrite; } } @@ -654,12 +666,11 @@ export default class Device { } } if ('url' in params) { - for (let i = 0; i < EMULATOR_LOCALHOST_ADDRESSES.length; ++i) { - const address = EMULATOR_LOCALHOST_ADDRESSES[i]; - if (params.url.indexOf(address) >= 0) { + for (const hostToRewrite of REWRITE_HOSTS_TO_LOCALHOST) { + if (params.url.includes(hostToRewrite)) { // $FlowFixMe[cannot-write] - payload.params.url = params.url.replace(address, 'localhost'); - debuggerInfo.originalSourceURLAddress = address; + payload.params.url = params.url.replace(hostToRewrite, 'localhost'); + debuggerInfo.originalSourceURLAddress = hostToRewrite; } }