feat: handle the X-Forwarded-Path header
This commit is contained in:
parent
366f71ca44
commit
549e175164
1 changed files with 67 additions and 66 deletions
133
src/utils.js
133
src/utils.js
|
@ -59,13 +59,13 @@ export const getPublicUrl = (publicUrl, req) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getTileUrls = (
|
export const getTileUrls = (
|
||||||
req,
|
req,
|
||||||
domains,
|
domains,
|
||||||
path,
|
path,
|
||||||
tileSize,
|
tileSize,
|
||||||
format,
|
format,
|
||||||
publicUrl,
|
publicUrl,
|
||||||
aliases,
|
aliases,
|
||||||
) => {
|
) => {
|
||||||
const urlObject = getUrlObject(req);
|
const urlObject = getUrlObject(req);
|
||||||
if (domains) {
|
if (domains) {
|
||||||
|
@ -74,8 +74,8 @@ export const getTileUrls = (
|
||||||
}
|
}
|
||||||
const hostParts = urlObject.host.split('.');
|
const hostParts = urlObject.host.split('.');
|
||||||
const relativeSubdomainsUsable =
|
const relativeSubdomainsUsable =
|
||||||
hostParts.length > 1 &&
|
hostParts.length > 1 &&
|
||||||
!/^([0-9]{1,3}\.){3}[0-9]{1,3}(\:[0-9]+)?$/.test(urlObject.host);
|
!/^([0-9]{1,3}\.){3}[0-9]{1,3}(\:[0-9]+)?$/.test(urlObject.host);
|
||||||
const newDomains = [];
|
const newDomains = [];
|
||||||
for (const domain of domains) {
|
for (const domain of domains) {
|
||||||
if (domain.indexOf('*') !== -1) {
|
if (domain.indexOf('*') !== -1) {
|
||||||
|
@ -114,9 +114,10 @@ export const getTileUrls = (
|
||||||
|
|
||||||
const uris = [];
|
const uris = [];
|
||||||
if (!publicUrl) {
|
if (!publicUrl) {
|
||||||
|
let xForwardedPath = `${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}/${path}/${tileParams}.${format}${query}`,
|
`${req.protocol}://${domain}${xForwardedPath}/${path}/${tileParams}.${format}${query}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -134,7 +135,7 @@ export const fixTileJSONCenter = (tileJSON) => {
|
||||||
(tileJSON.bounds[0] + tileJSON.bounds[2]) / 2,
|
(tileJSON.bounds[0] + tileJSON.bounds[2]) / 2,
|
||||||
(tileJSON.bounds[1] + tileJSON.bounds[3]) / 2,
|
(tileJSON.bounds[1] + tileJSON.bounds[3]) / 2,
|
||||||
Math.round(
|
Math.round(
|
||||||
-Math.log((tileJSON.bounds[2] - tileJSON.bounds[0]) / 360 / tiles) /
|
-Math.log((tileJSON.bounds[2] - tileJSON.bounds[0]) / 360 / tiles) /
|
||||||
Math.LN2,
|
Math.LN2,
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
@ -142,67 +143,67 @@ export const fixTileJSONCenter = (tileJSON) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const getFontPbf = (allowedFonts, fontPath, name, range, fallbacks) =>
|
const getFontPbf = (allowedFonts, fontPath, name, range, fallbacks) =>
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
if (!allowedFonts || (allowedFonts[name] && fallbacks)) {
|
if (!allowedFonts || (allowedFonts[name] && fallbacks)) {
|
||||||
const filename = path.join(fontPath, name, `${range}.pbf`);
|
const filename = path.join(fontPath, name, `${range}.pbf`);
|
||||||
if (!fallbacks) {
|
if (!fallbacks) {
|
||||||
fallbacks = clone(allowedFonts || {});
|
fallbacks = clone(allowedFonts || {});
|
||||||
}
|
|
||||||
delete fallbacks[name];
|
|
||||||
fs.readFile(filename, (err, data) => {
|
|
||||||
if (err) {
|
|
||||||
console.error(`ERROR: Font not found: ${name}`);
|
|
||||||
if (fallbacks && Object.keys(fallbacks).length) {
|
|
||||||
let fallbackName;
|
|
||||||
|
|
||||||
let fontStyle = name.split(' ').pop();
|
|
||||||
if (['Regular', 'Bold', 'Italic'].indexOf(fontStyle) < 0) {
|
|
||||||
fontStyle = 'Regular';
|
|
||||||
}
|
|
||||||
fallbackName = `Noto Sans ${fontStyle}`;
|
|
||||||
if (!fallbacks[fallbackName]) {
|
|
||||||
fallbackName = `Open Sans ${fontStyle}`;
|
|
||||||
if (!fallbacks[fallbackName]) {
|
|
||||||
fallbackName = Object.keys(fallbacks)[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.error(`ERROR: Trying to use ${fallbackName} as a fallback`);
|
|
||||||
delete fallbacks[fallbackName];
|
|
||||||
getFontPbf(null, fontPath, fallbackName, range, fallbacks).then(
|
|
||||||
resolve,
|
|
||||||
reject,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
reject(`Font load error: ${name}`);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
resolve(data);
|
|
||||||
}
|
}
|
||||||
});
|
delete fallbacks[name];
|
||||||
} else {
|
fs.readFile(filename, (err, data) => {
|
||||||
reject(`Font not allowed: ${name}`);
|
if (err) {
|
||||||
}
|
console.error(`ERROR: Font not found: ${name}`);
|
||||||
});
|
if (fallbacks && Object.keys(fallbacks).length) {
|
||||||
|
let fallbackName;
|
||||||
|
|
||||||
|
let fontStyle = name.split(' ').pop();
|
||||||
|
if (['Regular', 'Bold', 'Italic'].indexOf(fontStyle) < 0) {
|
||||||
|
fontStyle = 'Regular';
|
||||||
|
}
|
||||||
|
fallbackName = `Noto Sans ${fontStyle}`;
|
||||||
|
if (!fallbacks[fallbackName]) {
|
||||||
|
fallbackName = `Open Sans ${fontStyle}`;
|
||||||
|
if (!fallbacks[fallbackName]) {
|
||||||
|
fallbackName = Object.keys(fallbacks)[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error(`ERROR: Trying to use ${fallbackName} as a fallback`);
|
||||||
|
delete fallbacks[fallbackName];
|
||||||
|
getFontPbf(null, fontPath, fallbackName, range, fallbacks).then(
|
||||||
|
resolve,
|
||||||
|
reject,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
reject(`Font load error: ${name}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resolve(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
reject(`Font not allowed: ${name}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export const getFontsPbf = async (
|
export const getFontsPbf = async (
|
||||||
allowedFonts,
|
allowedFonts,
|
||||||
fontPath,
|
fontPath,
|
||||||
names,
|
names,
|
||||||
range,
|
range,
|
||||||
fallbacks,
|
fallbacks,
|
||||||
) => {
|
) => {
|
||||||
const fonts = names.split(',');
|
const fonts = names.split(',');
|
||||||
const queue = [];
|
const queue = [];
|
||||||
for (const font of fonts) {
|
for (const font of fonts) {
|
||||||
queue.push(
|
queue.push(
|
||||||
getFontPbf(
|
getFontPbf(
|
||||||
allowedFonts,
|
allowedFonts,
|
||||||
fontPath,
|
fontPath,
|
||||||
font,
|
font,
|
||||||
range,
|
range,
|
||||||
clone(allowedFonts || fallbacks),
|
clone(allowedFonts || fallbacks),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,8 +218,8 @@ export const listFonts = async (fontPath) => {
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const stats = await fsPromises.stat(path.join(fontPath, file));
|
const stats = await fsPromises.stat(path.join(fontPath, file));
|
||||||
if (
|
if (
|
||||||
stats.isDirectory() &&
|
stats.isDirectory() &&
|
||||||
existsSync(path.join(fontPath, file, '0-255.pbf'))
|
existsSync(path.join(fontPath, file, '0-255.pbf'))
|
||||||
) {
|
) {
|
||||||
existingFonts[path.basename(file)] = true;
|
existingFonts[path.basename(file)] = true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue