Remove URL.canParse, fix older Node compatibility

Summary:
`URL.canParse` was added to Node.js [in v18.17.0](9586cd0cfd).

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
This commit is contained in:
Rob Hogan 2024-11-19 01:50:49 -08:00 committed by Facebook GitHub Bot
parent 29a0d7c3b2
commit 99767d43b0

View File

@ -24,5 +24,9 @@ export default function getBaseUrlFromRequest(
// 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;
try {
return new URL(url);
} catch {
return null;
}
}