30 lines
1.4 KiB
JavaScript
30 lines
1.4 KiB
JavaScript
|
|
import { getRequestHost, getRequestProtocol } from "h3";
|
||
|
|
import { withoutProtocol, withTrailingSlash } from "ufo";
|
||
|
|
export function useNitroOrigin(e) {
|
||
|
|
const cert = process.env.NITRO_SSL_CERT;
|
||
|
|
const key = process.env.NITRO_SSL_KEY;
|
||
|
|
let host = process.env.NITRO_HOST || process.env.HOST || false;
|
||
|
|
let port = false;
|
||
|
|
if (process.dev)
|
||
|
|
port = process.env.NITRO_PORT || process.env.PORT || "3000";
|
||
|
|
let protocol = cert && key || !process.dev ? "https" : "http";
|
||
|
|
if ((process.dev || process.env.prerender) && process.env.__NUXT_DEV__) {
|
||
|
|
const origin = JSON.parse(process.env.__NUXT_DEV__).proxy.url;
|
||
|
|
host = withoutProtocol(origin);
|
||
|
|
protocol = origin.includes("https") ? "https" : "http";
|
||
|
|
} else if ((process.dev || process.env.prerender) && process.env.NUXT_VITE_NODE_OPTIONS) {
|
||
|
|
const origin = JSON.parse(process.env.NUXT_VITE_NODE_OPTIONS).baseURL.replace("/__nuxt_vite_node__", "");
|
||
|
|
host = withoutProtocol(origin);
|
||
|
|
protocol = origin.includes("https") ? "https" : "http";
|
||
|
|
} else {
|
||
|
|
host = getRequestHost(e, { xForwardedHost: true }) || host;
|
||
|
|
protocol = getRequestProtocol(e, { xForwardedProto: true }) || protocol;
|
||
|
|
}
|
||
|
|
if (typeof host === "string" && host.includes(":")) {
|
||
|
|
port = host.split(":").pop();
|
||
|
|
host = host.split(":")[0];
|
||
|
|
}
|
||
|
|
port = port ? `:${port}` : "";
|
||
|
|
return withTrailingSlash(`${protocol}://${host}${port}`);
|
||
|
|
}
|