diff --git a/src/main.js b/src/main.js index 5bf4e0a..f3bb71b 100644 --- a/src/main.js +++ b/src/main.js @@ -241,7 +241,7 @@ const StartWithInputFile = async (inputFile) => { } }; -fs.stat(path.resolve(opts.config), (err, stats) => { +fs.stat(path.resolve(opts.config), async (err, stats) => { if (err || !stats.isFile() || stats.size === 0) { let inputFile; if (opts.file) { @@ -274,21 +274,22 @@ fs.stat(path.resolve(opts.config), (err, stats) => { const writer = fs.createWriteStream(filename); console.log(`No input file found`); console.log(`[DEMO] Downloading sample data (${filename}) from ${url}`); - axios({ - url, - method: 'GET', - responseType: 'stream', - }) - .then((response) => { - response.data.pipe(writer); - writer.on('finish', () => StartWithInputFile(filename)); - writer.on('error', (err) => - console.error(`Error writing file: ${err}`), - ); - }) - .catch((error) => { - console.error(`Error downloading file: ${error}`); + + try { + const response = await axios({ + url, + method: 'GET', + responseType: 'stream', }); + + response.data.pipe(writer); + writer.on('finish', () => StartWithInputFile(filename)); + writer.on('error', (err) => + console.error(`Error writing file: ${err}`), + ); + } catch (error) { + console.error(`Error downloading file: ${error}`); + } } } } else { diff --git a/src/serve_font.js b/src/serve_font.js index e21bf22..02f46dc 100644 --- a/src/serve_font.js +++ b/src/serve_font.js @@ -4,7 +4,7 @@ import express from 'express'; import { getFontsPbf, listFonts } from './utils.js'; -export const serve_font = (options, allowedFonts) => { +export const serve_font = async (options, allowedFonts) => { const app = express().disable('x-powered-by'); const lastModified = new Date().toUTCString(); @@ -13,25 +13,29 @@ export const serve_font = (options, allowedFonts) => { const existingFonts = {}; - app.get('/fonts/:fontstack/:range([\\d]+-[\\d]+).pbf', (req, res, next) => { - const fontstack = decodeURI(req.params.fontstack); - const range = req.params.range; + app.get( + '/fonts/:fontstack/:range([\\d]+-[\\d]+).pbf', + async (req, res, next) => { + const fontstack = decodeURI(req.params.fontstack); + const range = req.params.range; + + try { + const concatenated = await getFontsPbf( + options.serveAllFonts ? null : allowedFonts, + fontPath, + fontstack, + range, + existingFonts, + ); - getFontsPbf( - options.serveAllFonts ? null : allowedFonts, - fontPath, - fontstack, - range, - existingFonts, - ).then( - (concated) => { res.header('Content-type', 'application/x-protobuf'); res.header('Last-Modified', lastModified); - return res.send(concated); - }, - (err) => res.status(400).header('Content-Type', 'text/plain').send(err), - ); - }); + return res.send(concatenated); + } catch (err) { + res.status(400).header('Content-Type', 'text/plain').send(err); + } + }, + ); app.get('/fonts.json', (req, res, next) => { res.header('Content-type', 'application/json'); @@ -40,8 +44,7 @@ export const serve_font = (options, allowedFonts) => { ); }); - return listFonts(options.paths.fonts).then((fonts) => { - Object.assign(existingFonts, fonts); - return app; - }); + const fonts = await listFonts(options.paths.fonts); + Object.assign(existingFonts, fonts); + return app; }; diff --git a/src/serve_rendered.js b/src/serve_rendered.js index fbdd1f0..bb0006c 100644 --- a/src/serve_rendered.js +++ b/src/serve_rendered.js @@ -524,7 +524,7 @@ const existingFonts = {}; let maxScaleFactor = 2; export const serve_rendered = { - init: (options, repo) => { + init: async (options, repo) => { maxScaleFactor = Math.min(Math.floor(options.maxScaleFactor || 3), 9); let scalePattern = ''; for (let i = 2; i <= maxScaleFactor; i++) { @@ -909,10 +909,9 @@ export const serve_rendered = { return res.send(info); }); - return listFonts(options.paths.fonts).then((fonts) => { - Object.assign(existingFonts, fonts); - return app; - }); + const fonts = await listFonts(options.paths.fonts); + Object.assign(existingFonts, fonts); + return app; }, add: async (options, repo, params, id, publicUrl, dataResolver) => { const map = { @@ -941,20 +940,19 @@ export const serve_rendered = { const parts = req.url.split('/'); const fontstack = unescape(parts[2]); const range = parts[3].split('.')[0]; - getFontsPbf( - null, - options.paths[protocol], - fontstack, - range, - existingFonts, - ).then( - (concated) => { - callback(null, { data: concated }); - }, - (err) => { - callback(err, { data: null }); - }, - ); + + try { + const concatenated = await getFontsPbf( + null, + options.paths[protocol], + fontstack, + range, + existingFonts, + ); + callback(null, { data: concatenated }); + } catch (err) { + callback(err, { data: null }); + } } else if (protocol === 'mbtiles' || protocol === 'pmtiles') { const parts = req.url.split('/'); const sourceId = parts[2]; @@ -1296,26 +1294,24 @@ export const serve_rendered = { } } - const renderersReadyPromise = Promise.all(queue).then(() => { - // standard and @2x tiles are much more usual -> default to larger pools - const minPoolSizes = options.minRendererPoolSizes || [8, 4, 2]; - const maxPoolSizes = options.maxRendererPoolSizes || [16, 8, 4]; - for (let s = 1; s <= maxScaleFactor; s++) { - const i = Math.min(minPoolSizes.length - 1, s - 1); - const j = Math.min(maxPoolSizes.length - 1, s - 1); - const minPoolSize = minPoolSizes[i]; - const maxPoolSize = Math.max(minPoolSize, maxPoolSizes[j]); - map.renderers[s] = createPool(s, 'tile', minPoolSize, maxPoolSize); - map.renderers_static[s] = createPool( - s, - 'static', - minPoolSize, - maxPoolSize, - ); - } - }); + await Promise.all(queue); - return renderersReadyPromise; + // standard and @2x tiles are much more usual -> default to larger pools + const minPoolSizes = options.minRendererPoolSizes || [8, 4, 2]; + const maxPoolSizes = options.maxRendererPoolSizes || [16, 8, 4]; + for (let s = 1; s <= maxScaleFactor; s++) { + const i = Math.min(minPoolSizes.length - 1, s - 1); + const j = Math.min(maxPoolSizes.length - 1, s - 1); + const minPoolSize = minPoolSizes[i]; + const maxPoolSize = Math.max(minPoolSize, maxPoolSizes[j]); + map.renderers[s] = createPool(s, 'tile', minPoolSize, maxPoolSize); + map.renderers_static[s] = createPool( + s, + 'static', + minPoolSize, + maxPoolSize, + ); + } }, remove: (repo, id) => { const item = repo[id]; diff --git a/src/server.js b/src/server.js index 1e570eb..a4a3760 100644 --- a/src/server.js +++ b/src/server.js @@ -141,11 +141,8 @@ function start(opts) { // Load all available icons into a settings object startupPromises.push( - new Promise((resolve) => { - getFiles(paths.icons).then((files) => { - paths.availableIcons = files; - resolve(); - }); + getFiles(paths.icons).then((files) => { + paths.availableIcons = files; }), ); diff --git a/src/utils.js b/src/utils.js index 3f5cb0d..8ed2d9c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -139,7 +139,7 @@ const getFontPbf = (allowedFonts, fontPath, name, range, fallbacks) => } }); -export const getFontsPbf = ( +export const getFontsPbf = async ( allowedFonts, fontPath, names, @@ -160,7 +160,8 @@ export const getFontsPbf = ( ); } - return Promise.all(queue).then((values) => glyphCompose.combine(values)); + const values = await Promise.all(queue); + return glyphCompose.combine(values); }; export const listFonts = async (fontPath) => {