From c64c07790d32000b2063d90746d3712a388a3fed Mon Sep 17 00:00:00 2001 From: Andrew Calcutt Date: Sun, 15 Oct 2023 00:08:49 -0400 Subject: [PATCH] feat: allow multiple urls with same filename Signed-off-by: Andrew Calcutt --- package-lock.json | 12 ++++++++++++ package.json | 1 + src/server.js | 11 ++++++++--- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3bae350..db105bb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@mapbox/vector-tile": "1.3.1", "@maplibre/maplibre-gl-native": "5.2.0", "@maplibre/maplibre-gl-style-spec": "18.0.0", + "@sindresorhus/fnv1a": "3.0.0", "advanced-pool": "0.3.3", "canvas": "2.11.2", "chokidar": "3.5.3", @@ -986,6 +987,17 @@ "@octokit/openapi-types": "^12.11.0" } }, + "node_modules/@sindresorhus/fnv1a": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@sindresorhus/fnv1a/-/fnv1a-3.0.0.tgz", + "integrity": "sha512-M6pmbdZqAryzjZ4ELAzrdCMoMZk5lH/fshKrapfSeXdf2W+GDqZvPmfXaNTZp43//FVbSwkTPwpEMnehSyskkQ==", + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/@tootallnate/once": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz", diff --git a/package.json b/package.json index f9d569e..dc44a3e 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@mapbox/vector-tile": "1.3.1", "@maplibre/maplibre-gl-native": "5.2.0", "@maplibre/maplibre-gl-style-spec": "18.0.0", + "@sindresorhus/fnv1a": "3.0.0", "advanced-pool": "0.3.3", "canvas": "2.11.2", "chokidar": "3.5.3", diff --git a/src/server.js b/src/server.js index 87a476b..71c7c4f 100644 --- a/src/server.js +++ b/src/server.js @@ -6,7 +6,7 @@ process.env.UV_THREADPOOL_SIZE = Math.ceil(Math.max(4, os.cpus().length * 1.5)); import fs from 'node:fs'; import path from 'path'; - +import fnv1a from '@sindresorhus/fnv1a'; import chokidar from 'chokidar'; import clone from 'clone'; import cors from 'cors'; @@ -207,8 +207,13 @@ function start(opts) { ); return undefined; } else { - let id = StyleSourceId.replace(/^.*\/(.*)$/, '$1'); // Remove url path up to last backslash, if it exists - id = id.substr(0, StyleSourceId.lastIndexOf('.')) || id; // Remove extension, if it exists + let id = + StyleSourceId.substr(0, StyleSourceId.lastIndexOf('.')) || + StyleSourceId; + if (isValidHttpUrl(StyleSourceId)) { + id = + id.replace(/^.*\/(.*)$/, '$1') + '_' + fnv1a(StyleSourceId); + } while (data[id]) id += '_'; //if the data source id already exists, add a "_" untill it doesn't //Add the new data source to the data array. data[id] = {