added fetching to addStyle() function and parsed it into serve_style and serve_rendered
This commit is contained in:
parent
a062d92582
commit
166130d142
3 changed files with 30 additions and 25 deletions
|
@ -1028,10 +1028,11 @@ export const serve_rendered = {
|
||||||
* @param {object} params Parameters object.
|
* @param {object} params Parameters object.
|
||||||
* @param {string} id ID of the item.
|
* @param {string} id ID of the item.
|
||||||
* @param {object} programOpts - An object containing the program options
|
* @param {object} programOpts - An object containing the program options
|
||||||
|
* @param {object} style pre-fetched/read StyleJSON object.
|
||||||
* @param {Function} dataResolver Function to resolve data.
|
* @param {Function} dataResolver Function to resolve data.
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
add: async function (options, repo, params, id, programOpts, dataResolver) {
|
add: async function (options, repo, params, id, programOpts, style, dataResolver) {
|
||||||
const map = {
|
const map = {
|
||||||
renderers: [],
|
renderers: [],
|
||||||
renderersStatic: [],
|
renderersStatic: [],
|
||||||
|
@ -1041,7 +1042,7 @@ export const serve_rendered = {
|
||||||
|
|
||||||
const { publicUrl, verbose } = programOpts;
|
const { publicUrl, verbose } = programOpts;
|
||||||
|
|
||||||
let styleJSON;
|
const styleJSON = clone(style);
|
||||||
/**
|
/**
|
||||||
* Creates a pool of renderers.
|
* Creates a pool of renderers.
|
||||||
* @param {number} ratio Pixel ratio
|
* @param {number} ratio Pixel ratio
|
||||||
|
@ -1230,12 +1231,6 @@ export const serve_rendered = {
|
||||||
|
|
||||||
const styleFile = params.style;
|
const styleFile = params.style;
|
||||||
const styleJSONPath = path.resolve(options.paths.styles, styleFile);
|
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 (styleJSON.sprite) {
|
||||||
if (!Array.isArray(styleJSON.sprite)) {
|
if (!Array.isArray(styleJSON.sprite)) {
|
||||||
|
|
|
@ -12,6 +12,7 @@ import {
|
||||||
allowedSpriteFormats,
|
allowedSpriteFormats,
|
||||||
fixUrl,
|
fixUrl,
|
||||||
readFile,
|
readFile,
|
||||||
|
isValidHttpUrl,
|
||||||
} from './utils.js';
|
} from './utils.js';
|
||||||
|
|
||||||
const httpTester = /^https?:\/\//i;
|
const httpTester = /^https?:\/\//i;
|
||||||
|
@ -196,6 +197,7 @@ export const serve_style = {
|
||||||
* @param {object} params Parameters object containing style path
|
* @param {object} params Parameters object containing style path
|
||||||
* @param {string} id ID of the style.
|
* @param {string} id ID of the style.
|
||||||
* @param {object} programOpts - An object containing the program options
|
* @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} reportTiles Function for reporting tile sources.
|
||||||
* @param {Function} reportFont Function for reporting font usage
|
* @param {Function} reportFont Function for reporting font usage
|
||||||
* @returns {Promise<boolean>} true if add is successful
|
* @returns {Promise<boolean>} true if add is successful
|
||||||
|
@ -206,27 +208,13 @@ export const serve_style = {
|
||||||
params,
|
params,
|
||||||
id,
|
id,
|
||||||
programOpts,
|
programOpts,
|
||||||
|
style,
|
||||||
reportTiles,
|
reportTiles,
|
||||||
reportFont,
|
reportFont,
|
||||||
) {
|
) {
|
||||||
const { publicUrl } = programOpts;
|
const { publicUrl } = programOpts;
|
||||||
const styleFile = path.resolve(options.paths.styles, params.style);
|
const styleFile = path.resolve(options.paths.styles, params.style)
|
||||||
|
const styleJSON = clone(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 validationErrors = validateStyleMin(styleJSON);
|
const validationErrors = validateStyleMin(styleJSON);
|
||||||
if (validationErrors.length > 0) {
|
if (validationErrors.length > 0) {
|
||||||
|
|
|
@ -182,6 +182,26 @@ async function start(opts) {
|
||||||
*/
|
*/
|
||||||
async function addStyle(id, item, allowMoreData, reportFonts) {
|
async function addStyle(id, item, allowMoreData, reportFonts) {
|
||||||
let success = true;
|
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) {
|
if (item.serve_data !== false) {
|
||||||
success = await serve_style.add(
|
success = await serve_style.add(
|
||||||
options,
|
options,
|
||||||
|
@ -189,6 +209,7 @@ async function start(opts) {
|
||||||
item,
|
item,
|
||||||
id,
|
id,
|
||||||
opts,
|
opts,
|
||||||
|
styleJSON,
|
||||||
(styleSourceId, protocol) => {
|
(styleSourceId, protocol) => {
|
||||||
let dataItemId;
|
let dataItemId;
|
||||||
for (const id of Object.keys(data)) {
|
for (const id of Object.keys(data)) {
|
||||||
|
@ -246,6 +267,7 @@ async function start(opts) {
|
||||||
item,
|
item,
|
||||||
id,
|
id,
|
||||||
opts,
|
opts,
|
||||||
|
styleJSON,
|
||||||
function dataResolver(styleSourceId) {
|
function dataResolver(styleSourceId) {
|
||||||
let fileType;
|
let fileType;
|
||||||
let inputFile;
|
let inputFile;
|
||||||
|
|
Loading…
Reference in a new issue