move allowed functions to utils.js
This commit is contained in:
parent
097c0e1455
commit
941e283b81
4 changed files with 56 additions and 53 deletions
|
@ -27,13 +27,14 @@ import polyline from '@mapbox/polyline';
|
|||
import proj4 from 'proj4';
|
||||
import axios from 'axios';
|
||||
import {
|
||||
allowedScales,
|
||||
allowedTileSizes,
|
||||
getFontsPbf,
|
||||
listFonts,
|
||||
getTileUrls,
|
||||
isValidHttpUrl,
|
||||
fixTileJSONCenter,
|
||||
fetchTileData,
|
||||
allowedOptions,
|
||||
readFile,
|
||||
} from './utils.js';
|
||||
import { openPMtiles, getPMtilesInfo } from './pmtiles_adapter.js';
|
||||
|
@ -66,26 +67,6 @@ const httpTester = /^https?:\/\//i;
|
|||
|
||||
const mercator = new SphericalMercator();
|
||||
|
||||
/**
|
||||
* Parses a scale string to a number.
|
||||
* @param {string} scale The scale string (e.g., '2x', '4x').
|
||||
* @param {number} maxScaleDigit Maximum allowed scale digit.
|
||||
* @returns {number|null} The parsed scale as a number or null if invalid.
|
||||
*/
|
||||
function parseScale(scale, maxScaleDigit = 9) {
|
||||
if (scale === undefined) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line security/detect-non-literal-regexp
|
||||
const regex = new RegExp(`^[2-${maxScaleDigit}]x$`);
|
||||
if (!regex.test(scale)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return parseInt(scale.slice(0, -1), 10);
|
||||
}
|
||||
|
||||
mlgl.on('message', (e) => {
|
||||
if (e.severity === 'WARNING' || e.severity === 'ERROR') {
|
||||
console.log('mlgl:', e);
|
||||
|
@ -676,13 +657,10 @@ async function handleTileRequest(
|
|||
const z = parseFloat(zParam) | 0;
|
||||
const x = parseFloat(xParam) | 0;
|
||||
const y = parseFloat(yParam) | 0;
|
||||
const scale = parseScale(scaleParam, maxScaleFactor);
|
||||
const scale = allowedScales(scaleParam, maxScaleFactor);
|
||||
|
||||
let parsedTileSize = parseInt(defailtTileSize, 10);
|
||||
if (tileSize) {
|
||||
const allowedTileSizes = allowedOptions(['256', '512'], {
|
||||
defaultValue: null,
|
||||
});
|
||||
parsedTileSize = parseInt(allowedTileSizes(tileSize), 10);
|
||||
|
||||
if (parsedTileSize == null) {
|
||||
|
@ -778,7 +756,7 @@ async function handleStaticRequest(
|
|||
.send('Invalid width or height provided in size parameter');
|
||||
}
|
||||
|
||||
const scale = parseScale(scaleParam, maxScaleFactor);
|
||||
const scale = allowedScales(scaleParam, maxScaleFactor);
|
||||
let isRaw = raw === 'raw';
|
||||
|
||||
const staticTypeMatch = staticType.match(staticTypeRegex);
|
||||
|
|
|
@ -7,31 +7,15 @@ import clone from 'clone';
|
|||
import express from 'express';
|
||||
import { validateStyleMin } from '@maplibre/maplibre-gl-style-spec';
|
||||
|
||||
import { fixUrl, allowedOptions, readFile } from './utils.js';
|
||||
import {
|
||||
allowedSpriteScales,
|
||||
allowedSpriteFormats,
|
||||
fixUrl,
|
||||
readFile,
|
||||
} from './utils.js';
|
||||
|
||||
const httpTester = /^https?:\/\//i;
|
||||
const allowedSpriteFormats = allowedOptions(['png', 'json']);
|
||||
|
||||
/**
|
||||
* Checks if a string is a valid sprite scale and returns it if it is within the allowed range, and null if it does not conform.
|
||||
* @param {string} scale - The scale string to validate (e.g., '2x', '3x').
|
||||
* @param {number} [maxScale] - The maximum scale value. If no value is passed in, it defaults to a value of 3.
|
||||
* @returns {string|null} - The valid scale string or null if invalid.
|
||||
*/
|
||||
function allowedSpriteScales(scale, maxScale = 3) {
|
||||
if (!scale) {
|
||||
return '';
|
||||
}
|
||||
const match = scale?.match(/^([2-9]\d*)x$/);
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
const parsedScale = parseInt(match[1], 10);
|
||||
if (parsedScale <= maxScale) {
|
||||
return `@${parsedScale}x`;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
export const serve_style = {
|
||||
/**
|
||||
* Initializes the serve_style module.
|
||||
|
|
|
@ -17,10 +17,10 @@ import { serve_data } from './serve_data.js';
|
|||
import { serve_style } from './serve_style.js';
|
||||
import { serve_font } from './serve_font.js';
|
||||
import {
|
||||
allowedTileSizes,
|
||||
getTileUrls,
|
||||
getPublicUrl,
|
||||
isValidHttpUrl,
|
||||
allowedOptions,
|
||||
} from './utils.js';
|
||||
|
||||
import { fileURLToPath } from 'url';
|
||||
|
@ -104,10 +104,6 @@ async function start(opts) {
|
|||
? path.resolve(paths.root, paths.files)
|
||||
: path.resolve(__dirname, '../public/files');
|
||||
|
||||
const allowedTileSizes = allowedOptions(['256', '512'], {
|
||||
defaultValue: options.tileSize || 256,
|
||||
});
|
||||
|
||||
const startupPromises = [];
|
||||
|
||||
for (const type of Object.keys(paths)) {
|
||||
|
|
45
src/utils.js
45
src/utils.js
|
@ -8,6 +8,10 @@ import { combine } from '@jsse/pbfont';
|
|||
import { existsP } from './promises.js';
|
||||
import { getPMtilesTile } from './pmtiles_adapter.js';
|
||||
|
||||
export const allowedSpriteFormats = allowedOptions(['png', 'json']);
|
||||
|
||||
export const allowedTileSizes = allowedOptions(['256', '512']);
|
||||
|
||||
/**
|
||||
* Restrict user input to an allowed set of options.
|
||||
* @param {string[]} opts - An array of allowed option strings.
|
||||
|
@ -20,6 +24,47 @@ export function allowedOptions(opts, { defaultValue } = {}) {
|
|||
return (value) => values[value] || defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a scale string to a number.
|
||||
* @param {string} scale The scale string (e.g., '2x', '4x').
|
||||
* @param {number} maxScale Maximum allowed scale digit.
|
||||
* @returns {number|null} The parsed scale as a number or null if invalid.
|
||||
*/
|
||||
export function allowedScales(scale, maxScale = 9) {
|
||||
if (scale === undefined) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line security/detect-non-literal-regexp
|
||||
const regex = new RegExp(`^[2-${maxScale}]x$`);
|
||||
if (!regex.test(scale)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return parseInt(scale.slice(0, -1), 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a string is a valid sprite scale and returns it if it is within the allowed range, and null if it does not conform.
|
||||
* @param {string} scale - The scale string to validate (e.g., '2x', '3x').
|
||||
* @param {number} [maxScale] - The maximum scale value. If no value is passed in, it defaults to a value of 3.
|
||||
* @returns {string|null} - The valid scale string or null if invalid.
|
||||
*/
|
||||
export function allowedSpriteScales(scale, maxScale = 3) {
|
||||
if (!scale) {
|
||||
return '';
|
||||
}
|
||||
const match = scale?.match(/^([2-9]\d*)x$/);
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
const parsedScale = parseInt(match[1], 10);
|
||||
if (parsedScale <= maxScale) {
|
||||
return `@${parsedScale}x`;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces local:// URLs with public http(s):// URLs.
|
||||
* @param {object} req - Express request object.
|
||||
|
|
Loading…
Reference in a new issue