added fetching to addStyle() function and parsed it into serve_style and serve_rendered

This commit is contained in:
Yoel Ridgway-Lopez 2025-02-10 17:30:40 +01:00
parent a062d92582
commit 166130d142
3 changed files with 30 additions and 25 deletions

View file

@ -1028,10 +1028,11 @@ export const serve_rendered = {
* @param {object} params Parameters object.
* @param {string} id ID of the item.
* @param {object} programOpts - An object containing the program options
* @param {object} style pre-fetched/read StyleJSON object.
* @param {Function} dataResolver Function to resolve data.
* @returns {Promise<void>}
*/
add: async function (options, repo, params, id, programOpts, dataResolver) {
add: async function (options, repo, params, id, programOpts, style, dataResolver) {
const map = {
renderers: [],
renderersStatic: [],
@ -1041,7 +1042,7 @@ export const serve_rendered = {
const { publicUrl, verbose } = programOpts;
let styleJSON;
const styleJSON = clone(style);
/**
* Creates a pool of renderers.
* @param {number} ratio Pixel ratio
@ -1230,12 +1231,6 @@ export const serve_rendered = {
const styleFile = params.style;
const styleJSONPath = path.resolve(options.paths.styles, styleFile);
try {
styleJSON = JSON.parse(await fsp.readFile(styleJSONPath));
} catch (e) {
console.log('Error parsing style file');
return false;
}
if (styleJSON.sprite) {
if (!Array.isArray(styleJSON.sprite)) {

View file

@ -12,6 +12,7 @@ import {
allowedSpriteFormats,
fixUrl,
readFile,
isValidHttpUrl,
} from './utils.js';
const httpTester = /^https?:\/\//i;
@ -196,6 +197,7 @@ export const serve_style = {
* @param {object} params Parameters object containing style path
* @param {string} id ID of the style.
* @param {object} programOpts - An object containing the program options
* @param {object} style pre-fetched/read StyleJSON object.
* @param {Function} reportTiles Function for reporting tile sources.
* @param {Function} reportFont Function for reporting font usage
* @returns {Promise<boolean>} true if add is successful
@ -206,27 +208,13 @@ export const serve_style = {
params,
id,
programOpts,
style,
reportTiles,
reportFont,
) {
const { publicUrl } = programOpts;
const styleFile = path.resolve(options.paths.styles, params.style);
let styleFileData;
try {
styleFileData = await fs.promises.readFile(styleFile);
} catch (e) {
console.log(`Error reading style file "${params.style}"`);
return false;
}
let styleJSON;
try {
styleJSON = JSON.parse(styleFileData);
} catch (e) {
console.log(`Error parsing style JSON from "${params.style}"`);
return false;
}
const styleFile = path.resolve(options.paths.styles, params.style)
const styleJSON = clone(style);
const validationErrors = validateStyleMin(styleJSON);
if (validationErrors.length > 0) {

View file

@ -182,6 +182,26 @@ async function start(opts) {
*/
async function addStyle(id, item, allowMoreData, reportFonts) {
let success = true;
let styleJSON;
try {
if (isValidHttpUrl(item.style)){
const res = await fetch(item.style);
if (!res.ok) {
throw new Error(`fetch error ${res.status}`);
}
styleJSON = await res.json();
} else {
const styleFile = path.resolve(options.paths.styles, item.style);
const styleFileData = await fs.promises.readFile(styleFile);
styleJSON = JSON.parse(styleFileData);
}
} catch (e) {
console.log(`Error getting style file "${item.style}"`);
return false;
}
if (item.serve_data !== false) {
success = await serve_style.add(
options,
@ -189,6 +209,7 @@ async function start(opts) {
item,
id,
opts,
styleJSON,
(styleSourceId, protocol) => {
let dataItemId;
for (const id of Object.keys(data)) {
@ -246,6 +267,7 @@ async function start(opts) {
item,
id,
opts,
styleJSON,
function dataResolver(styleSourceId) {
let fileType;
let inputFile;