feat: Add support for relative public URL
This commit is contained in:
parent
f2b48acb61
commit
e33be9df5c
1 changed files with 11 additions and 6 deletions
17
src/utils.js
17
src/utils.js
|
@ -92,7 +92,7 @@ export function fixUrl(req, url, publicUrl) {
|
||||||
* @param {object} req - Express request object.
|
* @param {object} req - Express request object.
|
||||||
* @returns {URL} - URL object with correct host and optionally path.
|
* @returns {URL} - URL object with correct host and optionally path.
|
||||||
*/
|
*/
|
||||||
function getUrlObject(req) {
|
function getUrlObject(req, prefix) {
|
||||||
const urlObject = new URL(`${req.protocol}://${req.headers.host}/`);
|
const urlObject = new URL(`${req.protocol}://${req.headers.host}/`);
|
||||||
// support overriding hostname by sending X-Forwarded-Host http header
|
// support overriding hostname by sending X-Forwarded-Host http header
|
||||||
urlObject.hostname = req.hostname;
|
urlObject.hostname = req.hostname;
|
||||||
|
@ -104,7 +104,7 @@ function getUrlObject(req) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// support add url prefix by sending X-Forwarded-Path http header
|
// support add url prefix by sending X-Forwarded-Path http header
|
||||||
const xForwardedPath = req.get('X-Forwarded-Path');
|
const xForwardedPath = prefix || req.get('X-Forwarded-Path');
|
||||||
if (xForwardedPath) {
|
if (xForwardedPath) {
|
||||||
urlObject.pathname = path.posix.join(xForwardedPath, urlObject.pathname);
|
urlObject.pathname = path.posix.join(xForwardedPath, urlObject.pathname);
|
||||||
}
|
}
|
||||||
|
@ -119,8 +119,13 @@ function getUrlObject(req) {
|
||||||
*/
|
*/
|
||||||
export function getPublicUrl(publicUrl, req) {
|
export function getPublicUrl(publicUrl, req) {
|
||||||
if (publicUrl) {
|
if (publicUrl) {
|
||||||
return publicUrl;
|
if (/^https?:\/\//.test(publicUrl)) {
|
||||||
|
return publicUrl;
|
||||||
|
} else {
|
||||||
|
return getUrlObject(req, publicUrl).toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return getUrlObject(req).toString();
|
return getUrlObject(req).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,11 +201,11 @@ export function getTileUrls(
|
||||||
}
|
}
|
||||||
|
|
||||||
const uris = [];
|
const uris = [];
|
||||||
if (!publicUrl) {
|
if (!publicUrl || !/^https?:\/\//.test(publicUrl)) {
|
||||||
let xForwardedPath = `${req.get('X-Forwarded-Path') ? '/' + req.get('X-Forwarded-Path') : ''}`;
|
let xForwardedPath = publicUrl || `${req.get('X-Forwarded-Path') ? '/' + req.get('X-Forwarded-Path') : ''}`;
|
||||||
for (const domain of domains) {
|
for (const domain of domains) {
|
||||||
uris.push(
|
uris.push(
|
||||||
`${req.protocol}://${domain}${xForwardedPath}/${path}/${tileParams}${format}${query}`,
|
`${req.protocol}://${domain}${xForwardedPath.replace(/\/$/, "")}/${path}/${tileParams}${format}${query}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue