33 lines
889 B
TypeScript
33 lines
889 B
TypeScript
type CanvasRuntimeEndpointOptions = {
|
|
internalOrigin?: string;
|
|
port?: string;
|
|
};
|
|
|
|
function normalizeInternalOrigin(value: string) {
|
|
const url = new URL(value);
|
|
url.pathname = "/";
|
|
url.search = "";
|
|
url.hash = "";
|
|
return url.toString();
|
|
}
|
|
|
|
export function resolveCanvasRuntimeEndpoint(
|
|
path: string,
|
|
requestUrl: string,
|
|
options: CanvasRuntimeEndpointOptions = {}
|
|
) {
|
|
const configuredOrigin = String(
|
|
options.internalOrigin ?? process.env.CANVAS_RUNTIME_INTERNAL_ORIGIN ?? ""
|
|
).trim();
|
|
if (configuredOrigin) {
|
|
return new URL(path, normalizeInternalOrigin(configuredOrigin)).toString();
|
|
}
|
|
|
|
const port = String(options.port ?? process.env.PORT ?? "").trim();
|
|
if (/^\d{1,5}$/.test(port) && Number(port) > 0 && Number(port) <= 65_535) {
|
|
return new URL(path, `http://127.0.0.1:${port}`).toString();
|
|
}
|
|
|
|
return new URL(path, requestUrl).toString();
|
|
}
|