mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: `URL.canParse` was added to Node.js [in v18.17.0](https://github.com/nodejs/node/commit/9586cd0cfd50bb83722af4edeb54cd113b68e20a). Internally, we have call sites using earlier versions of Node.js. Changelog: [General][Fixed]: dev-middleware: Remove URL.canParse, restore compat with Node < 18.17 Reviewed By: rshest Differential Revision: D66158204 fbshipit-source-id: e7a846b536e76672cea9afd5bdc5041d50a8b587
33 lines
952 B
JavaScript
33 lines
952 B
JavaScript
/**
|
|
* 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}`;
|
|
try {
|
|
return new URL(url);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|