-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev-middleware: Redefine "serverBaseUrl" as server-relative, '/json/l…
…ist' by requestor (#47628) Summary: Pull Request resolved: #47628 `serverBaseUrl` is currently documented as: > The base URL to the dev server, as addressible from the local developer machine This is problematic in general because `dev-middleware` on a server doesn't necessarily know about where clients might be reaching it from, how tunnels or port-forwards are set up, etc., and this can change over the lifetime of the server and vary between clients. Indeed, our own use of `serverBaseUrl` from both `community-cli-plugin` and internally simply sets it to the host and port the dev server is listening on - ie it's the address of the dev server accessible *from the server*. This PR changes the docs, redefining `serverBaseUrl`, to match the way we currently specify it. One usage where we *do* want the previously documented behaviour is in responses to `/json/list` (`getPageDescriptions`) where the URLs in the response should be reachable by a browser requesting `/json/list`. Here, we use the request (host header, etc.) to attempt to get working base URL. History: It should be mentioned that this is the latest in a series of changes like this: - #39394 - #39456 Learning from those: - This change does *not* break Android emulators, which routes `10.0.2.2` to localhost, or other routed devices, because `/open-debugger` still uses server-relative URLs, and now formally delegates to `BrowserLauncher` to decide what to do with those URLs (internally, VSCode / `xdg-open` handles port forwarding) - Middleware configuration is no longer required to specify how it is reachable from clients. This sets up some subsequent changes for more robust handling of tunnelled connections. Changelog: [General][Breaking] dev-middleware: Frameworks should specify `serverBaseUrl` relative to the middleware host. Reviewed By: huntie Differential Revision: D65974487 fbshipit-source-id: 1face8fc7715df387f75b329e80932d8543ee419
- Loading branch information
1 parent
ca9c563
commit acf384a
Showing
8 changed files
with
117 additions
and
20 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
packages/dev-middleware/src/__tests__/getBaseUrlFromRequest-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
import getBaseUrlFromRequest from '../utils/getBaseUrlFromRequest'; | ||
|
||
test('returns a base url based on req.headers.host', () => { | ||
expect( | ||
getBaseUrlFromRequest(makeRequest('localhost:8081', false))?.href, | ||
).toEqual('http://localhost:8081/'); | ||
}); | ||
|
||
test('identifies https using socket.encrypted', () => { | ||
expect( | ||
getBaseUrlFromRequest(makeRequest('secure.net:8443', true))?.href, | ||
).toEqual('https://secure.net:8443/'); | ||
}); | ||
|
||
test('works with ipv6 hosts', () => { | ||
expect(getBaseUrlFromRequest(makeRequest('[::1]:8081', false))?.href).toEqual( | ||
'http://[::1]:8081/', | ||
); | ||
}); | ||
|
||
test('returns null on an invalid host header', () => { | ||
expect(getBaseUrlFromRequest(makeRequest('local[]host', false))).toBeNull(); | ||
}); | ||
|
||
test('returns null on an empty host header', () => { | ||
expect(getBaseUrlFromRequest(makeRequest(null, false))).toBeNull(); | ||
}); | ||
|
||
function makeRequest( | ||
host: ?string, | ||
encrypted: boolean, | ||
): http$IncomingMessage<> | http$IncomingMessage<tls$TLSSocket> { | ||
// $FlowIgnore[incompatible-return] Partial mock of request | ||
return { | ||
socket: encrypted ? {encrypted: true} : {}, | ||
headers: host != null ? {host} : {}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/dev-middleware/src/utils/getBaseUrlFromRequest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
// Determine the base URL (scheme and host) used by a client to reach this | ||
// server. | ||
// | ||
// TODO: Support X-Forwarded-Host, etc. for trusted proxies | ||
export default function getBaseUrlFromRequest( | ||
req: http$IncomingMessage<tls$TLSSocket> | http$IncomingMessage<net$Socket>, | ||
): ?URL { | ||
const hostHeader = req.headers.host; | ||
if (hostHeader == null) { | ||
return null; | ||
} | ||
// `encrypted` is always true for TLS sockets and undefined for net | ||
// https://github.com/nodejs/node/issues/41863#issuecomment-1030709186 | ||
const scheme = req.socket.encrypted === true ? 'https' : 'http'; | ||
const url = `${scheme}://${req.headers.host}`; | ||
return URL.canParse(url) ? new URL(url) : null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters