chore: extract duplicated font listing

Signed-off-by: Martin d'Allens <martin.dallens@liberty-rider.com>
This commit is contained in:
Martin d'Allens 2023-11-19 23:08:16 +01:00 committed by Andrew Calcutt
parent b0157f3206
commit 5399f964dd
4 changed files with 41 additions and 51 deletions

View file

@ -14,7 +14,7 @@
"lint:eslint:fix": "eslint --fix \"{,!(node_modules|dist|static|public)/**/}*.{js,ts,cjs,mjs}\" --ignore-path .gitignore", "lint:eslint:fix": "eslint --fix \"{,!(node_modules|dist|static|public)/**/}*.{js,ts,cjs,mjs}\" --ignore-path .gitignore",
"lint:prettier": "prettier --check \"{,!(node_modules|dist|static|public)/**/}*.{js,ts,cjs,mjs,json}\" --ignore-path .gitignore", "lint:prettier": "prettier --check \"{,!(node_modules|dist|static|public)/**/}*.{js,ts,cjs,mjs,json}\" --ignore-path .gitignore",
"lint:prettier:fix": "prettier --write \"{,!(node_modules|dist|static|public)/**/}*.{js,ts,cjs,mjs,json}\" --ignore-path .gitignore", "lint:prettier:fix": "prettier --write \"{,!(node_modules|dist|static|public)/**/}*.{js,ts,cjs,mjs,json}\" --ignore-path .gitignore",
"docker": "docker build -f Dockerfile . && docker run --rm -i -p 8080:8080 $(docker build -q .)", "docker": "docker build . && docker run --rm -i -p 8080:8080 $(docker build -q .)",
"prepare": "node -e \"if (process.env.NODE_ENV !== 'production'){ process.exit(1) } \" || husky install" "prepare": "node -e \"if (process.env.NODE_ENV !== 'production'){ process.exit(1) } \" || husky install"
}, },
"dependencies": { "dependencies": {

View file

@ -1,10 +1,8 @@
'use strict'; 'use strict';
import express from 'express'; import express from 'express';
import fs from 'node:fs';
import path from 'path';
import { getFontsPbf } from './utils.js'; import { getFontsPbf, listFonts } from './utils.js';
export const serve_font = (options, allowedFonts) => { export const serve_font = (options, allowedFonts) => {
const app = express().disable('x-powered-by'); const app = express().disable('x-powered-by');
@ -14,29 +12,6 @@ export const serve_font = (options, allowedFonts) => {
const fontPath = options.paths.fonts; const fontPath = options.paths.fonts;
const existingFonts = {}; const existingFonts = {};
const fontListingPromise = new Promise((resolve, reject) => {
fs.readdir(options.paths.fonts, (err, files) => {
if (err) {
reject(err);
return;
}
for (const file of files) {
fs.stat(path.join(fontPath, file), (err, stats) => {
if (err) {
reject(err);
return;
}
if (
stats.isDirectory() &&
fs.existsSync(path.join(fontPath, file, '0-255.pbf'))
) {
existingFonts[path.basename(file)] = true;
}
});
}
resolve();
});
});
app.get('/fonts/:fontstack/:range([\\d]+-[\\d]+).pbf', (req, res, next) => { app.get('/fonts/:fontstack/:range([\\d]+-[\\d]+).pbf', (req, res, next) => {
const fontstack = decodeURI(req.params.fontstack); const fontstack = decodeURI(req.params.fontstack);
@ -65,5 +40,8 @@ export const serve_font = (options, allowedFonts) => {
); );
}); });
return fontListingPromise.then(() => app); return listFonts(options.paths.fonts).then((fonts) => {
Object.assign(existingFonts, fonts);
return app;
});
}; };

View file

@ -20,6 +20,7 @@ import proj4 from 'proj4';
import axios from 'axios'; import axios from 'axios';
import { import {
getFontsPbf, getFontsPbf,
listFonts,
getTileUrls, getTileUrls,
isValidHttpUrl, isValidHttpUrl,
fixTileJSONCenter, fixTileJSONCenter,
@ -613,27 +614,6 @@ let maxScaleFactor = 2;
export const serve_rendered = { export const serve_rendered = {
init: (options, repo) => { init: (options, repo) => {
const fontListingPromise = new Promise((resolve, reject) => {
fs.readdir(options.paths.fonts, (err, files) => {
if (err) {
reject(err);
return;
}
for (const file of files) {
fs.stat(path.join(options.paths.fonts, file), (err, stats) => {
if (err) {
reject(err);
return;
}
if (stats.isDirectory()) {
existingFonts[path.basename(file)] = true;
}
});
}
resolve();
});
});
maxScaleFactor = Math.min(Math.floor(options.maxScaleFactor || 3), 9); maxScaleFactor = Math.min(Math.floor(options.maxScaleFactor || 3), 9);
let scalePattern = ''; let scalePattern = '';
for (let i = 2; i <= maxScaleFactor; i++) { for (let i = 2; i <= maxScaleFactor; i++) {
@ -1215,7 +1195,10 @@ export const serve_rendered = {
return res.send(info); return res.send(info);
}); });
return Promise.all([fontListingPromise]).then(() => app); return listFonts(options.paths.fonts).then((fonts) => {
Object.assign(existingFonts, fonts);
return app;
});
}, },
add: async (options, repo, params, id, publicUrl, dataResolver) => { add: async (options, repo, params, id, publicUrl, dataResolver) => {
const map = { const map = {
@ -1618,7 +1601,7 @@ export const serve_rendered = {
} }
}); });
return Promise.all([renderersReadyPromise]); return renderersReadyPromise;
}, },
remove: (repo, id) => { remove: (repo, id) => {
const item = repo[id]; const item = repo[id];

View file

@ -163,6 +163,35 @@ export const getFontsPbf = (
return Promise.all(queue).then((values) => glyphCompose.combine(values)); return Promise.all(queue).then((values) => glyphCompose.combine(values));
}; };
export const listFonts = async (fontPath) => {
const existingFonts = {};
const fontListingPromise = new Promise((resolve, reject) => {
fs.readdir(fontPath, (err, files) => {
if (err) {
reject(err);
return;
}
for (const file of files) {
fs.stat(path.join(fontPath, file), (err, stats) => {
if (err) {
reject(err);
return;
}
if (
stats.isDirectory() &&
fs.existsSync(path.join(fontPath, file, '0-255.pbf'))
) {
existingFonts[path.basename(file)] = true;
}
});
}
resolve();
});
});
await fontListingPromise;
return existingFonts;
};
export const isValidHttpUrl = (string) => { export const isValidHttpUrl = (string) => {
let url; let url;