Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle the X-Forwarded-Path header #1197

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 63 additions & 62 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@
};

export const getTileUrls = (
req,
domains,
path,
tileSize,
format,
publicUrl,
aliases,
req,
domains,
path,
tileSize,
format,
publicUrl,
aliases,
) => {
const urlObject = getUrlObject(req);
if (domains) {
Expand All @@ -74,8 +74,8 @@
}
const hostParts = urlObject.host.split('.');
const relativeSubdomainsUsable =
hostParts.length > 1 &&
!/^([0-9]{1,3}\.){3}[0-9]{1,3}(\:[0-9]+)?$/.test(urlObject.host);
hostParts.length > 1 &&
!/^([0-9]{1,3}\.){3}[0-9]{1,3}(\:[0-9]+)?$/.test(urlObject.host);
const newDomains = [];
for (const domain of domains) {
if (domain.indexOf('*') !== -1) {
Expand Down Expand Up @@ -114,9 +114,10 @@

const uris = [];
if (!publicUrl) {
let xForwardedPath = `${req.get('X-Forwarded-Path') ? "/" + req.get('X-Forwarded-Path') : ""}`
Fixed Show fixed Hide fixed
for (const domain of domains) {
uris.push(
`${req.protocol}://${domain}/${path}/${tileParams}.${format}${query}`,
`${req.protocol}://${domain}${xForwardedPath}/${path}/${tileParams}.${format}${query}`,
);
}
} else {
Expand All @@ -134,75 +135,75 @@
(tileJSON.bounds[0] + tileJSON.bounds[2]) / 2,
(tileJSON.bounds[1] + tileJSON.bounds[3]) / 2,
Math.round(
-Math.log((tileJSON.bounds[2] - tileJSON.bounds[0]) / 360 / tiles) /
-Math.log((tileJSON.bounds[2] - tileJSON.bounds[0]) / 360 / tiles) /
Math.LN2,
),
];
}
};

const getFontPbf = (allowedFonts, fontPath, name, range, fallbacks) =>
new Promise((resolve, reject) => {
if (!allowedFonts || (allowedFonts[name] && fallbacks)) {
const filename = path.join(fontPath, name, `${range}.pbf`);
if (!fallbacks) {
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}`;
new Promise((resolve, reject) => {
if (!allowedFonts || (allowedFonts[name] && fallbacks)) {
const filename = path.join(fontPath, name, `${range}.pbf`);
if (!fallbacks) {
fallbacks = clone(allowedFonts || {});
}
delete fallbacks[name];
Fixed Show fixed Hide fixed
fs.readFile(filename, (err, data) => {
Fixed Show fixed Hide fixed
if (err) {
console.error(`ERROR: Font not found: ${name}`);
Fixed Show fixed Hide fixed
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 = Object.keys(fallbacks)[0];
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,
);
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 {
reject(`Font load error: ${name}`);
resolve(data);
}
} else {
resolve(data);
}
});
} else {
reject(`Font not allowed: ${name}`);
}
});
});
} else {
reject(`Font not allowed: ${name}`);
}
});

export const getFontsPbf = async (
allowedFonts,
fontPath,
names,
range,
fallbacks,
allowedFonts,
fontPath,
names,
range,
fallbacks,
) => {
const fonts = names.split(',');
const queue = [];
for (const font of fonts) {
queue.push(
getFontPbf(
allowedFonts,
fontPath,
font,
range,
clone(allowedFonts || fallbacks),
),
getFontPbf(
allowedFonts,
fontPath,
font,
range,
clone(allowedFonts || fallbacks),
),
);
}

Expand All @@ -217,8 +218,8 @@
for (const file of files) {
const stats = await fsPromises.stat(path.join(fontPath, file));
if (
stats.isDirectory() &&
existsSync(path.join(fontPath, file, '0-255.pbf'))
stats.isDirectory() &&
existsSync(path.join(fontPath, file, '0-255.pbf'))
) {
existingFonts[path.basename(file)] = true;
}
Expand Down
Loading