From a062d92582ea06554018c9f8f19ee60d103256cb Mon Sep 17 00:00:00 2001 From: Yoel Ridgway-Lopez Date: Mon, 10 Feb 2025 16:20:08 +0100 Subject: [PATCH] made the serve_style.add and addStyle functions async and return promises --- src/serve_style.js | 15 +++++++++++---- src/server.js | 11 ++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/serve_style.js b/src/serve_style.js index 51b03d3..ba6d0c8 100644 --- a/src/serve_style.js +++ b/src/serve_style.js @@ -198,9 +198,9 @@ export const serve_style = { * @param {object} programOpts - An object containing the program options * @param {Function} reportTiles Function for reporting tile sources. * @param {Function} reportFont Function for reporting font usage - * @returns {boolean} true if add is succesful + * @returns {Promise} true if add is successful */ - add: function ( + add: async function ( options, repo, params, @@ -214,13 +214,20 @@ export const serve_style = { let styleFileData; try { - styleFileData = fs.readFileSync(styleFile); // TODO: could be made async if this function was + styleFileData = await fs.promises.readFile(styleFile); } catch (e) { console.log(`Error reading style file "${params.style}"`); return false; } - const styleJSON = JSON.parse(styleFileData); + let styleJSON; + try { + styleJSON = JSON.parse(styleFileData); + } catch (e) { + console.log(`Error parsing style JSON from "${params.style}"`); + return false; + } + const validationErrors = validateStyleMin(styleJSON); if (validationErrors.length > 0) { console.log(`The file "${params.style}" is not a valid style file:`); diff --git a/src/server.js b/src/server.js index 9a3f8a8..b619e0c 100644 --- a/src/server.js +++ b/src/server.js @@ -178,12 +178,12 @@ async function start(opts) { * @param {object} item - The style configuration object. * @param {boolean} allowMoreData - Whether to allow adding more data sources. * @param {boolean} reportFonts - Whether to report fonts. - * @returns {void} + * @returns {Promise} */ - function addStyle(id, item, allowMoreData, reportFonts) { + async function addStyle(id, item, allowMoreData, reportFonts) { let success = true; if (item.serve_data !== false) { - success = serve_style.add( + success = await serve_style.add( options, serving.styles, item, @@ -271,6 +271,7 @@ async function start(opts) { item.serve_rendered = false; } } + return success; } for (const id of Object.keys(config.styles || {})) { @@ -279,8 +280,8 @@ async function start(opts) { console.log(`Missing "style" property for ${id}`); continue; } - - addStyle(id, item, true, true); + + startupPromises.push(addStyle(id, item, true, true)); } startupPromises.push( serve_font(options, serving.fonts, opts).then((sub) => {