2023-03-15 12:17:43 +00:00
|
|
|
import { IncomingMessage } from "node:http";
|
2023-02-13 12:41:30 +00:00
|
|
|
|
|
|
|
|
function absoluteUrl(
|
|
|
|
|
req?: IncomingMessage,
|
|
|
|
|
localhostAddress = "localhost:3000"
|
|
|
|
|
) {
|
|
|
|
|
let host =
|
|
|
|
|
(req?.headers ? req.headers.host : window.location.host) ||
|
|
|
|
|
localhostAddress;
|
|
|
|
|
let protocol = /^localhost(:\d+)?$/.test(host) ? "http:" : "https:";
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
req &&
|
|
|
|
|
req.headers["x-forwarded-host"] &&
|
|
|
|
|
typeof req.headers["x-forwarded-host"] === "string"
|
|
|
|
|
) {
|
|
|
|
|
host = req.headers["x-forwarded-host"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
req &&
|
|
|
|
|
req.headers["x-forwarded-proto"] &&
|
|
|
|
|
typeof req.headers["x-forwarded-proto"] === "string"
|
|
|
|
|
) {
|
|
|
|
|
protocol = `${req.headers["x-forwarded-proto"]}:`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
protocol,
|
|
|
|
|
host,
|
|
|
|
|
origin: protocol + "//" + host,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default absoluteUrl;
|