chore: extract duplicated font listing
Signed-off-by: Martin d'Allens <martin.dallens@liberty-rider.com>
This commit is contained in:
parent
b0157f3206
commit
5399f964dd
4 changed files with 41 additions and 51 deletions
|
|
@ -14,7 +14,7 @@
|
|||
"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: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"
|
||||
},
|
||||
"dependencies": {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
'use strict';
|
||||
|
||||
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) => {
|
||||
const app = express().disable('x-powered-by');
|
||||
|
|
@ -14,29 +12,6 @@ export const serve_font = (options, allowedFonts) => {
|
|||
const fontPath = options.paths.fonts;
|
||||
|
||||
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) => {
|
||||
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;
|
||||
});
|
||||
};
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import proj4 from 'proj4';
|
|||
import axios from 'axios';
|
||||
import {
|
||||
getFontsPbf,
|
||||
listFonts,
|
||||
getTileUrls,
|
||||
isValidHttpUrl,
|
||||
fixTileJSONCenter,
|
||||
|
|
@ -613,27 +614,6 @@ let maxScaleFactor = 2;
|
|||
|
||||
export const serve_rendered = {
|
||||
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);
|
||||
let scalePattern = '';
|
||||
for (let i = 2; i <= maxScaleFactor; i++) {
|
||||
|
|
@ -1215,7 +1195,10 @@ export const serve_rendered = {
|
|||
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) => {
|
||||
const map = {
|
||||
|
|
@ -1618,7 +1601,7 @@ export const serve_rendered = {
|
|||
}
|
||||
});
|
||||
|
||||
return Promise.all([renderersReadyPromise]);
|
||||
return renderersReadyPromise;
|
||||
},
|
||||
remove: (repo, id) => {
|
||||
const item = repo[id];
|
||||
|
|
|
|||
29
src/utils.js
29
src/utils.js
|
|
@ -163,6 +163,35 @@ export const getFontsPbf = (
|
|||
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) => {
|
||||
let url;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue