This commit is contained in:
Majid Hojati 2024-07-29 15:27:38 -06:00 committed by GitHub
commit 33d8f15cd5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 40 additions and 6 deletions

9
package-lock.json generated
View file

@ -25,6 +25,7 @@
"color": "4.2.3", "color": "4.2.3",
"commander": "12.1.0", "commander": "12.1.0",
"cors": "2.8.5", "cors": "2.8.5",
"crypto": "^1.0.1",
"express": "4.19.2", "express": "4.19.2",
"handlebars": "4.7.8", "handlebars": "4.7.8",
"http-shutdown": "1.2.2", "http-shutdown": "1.2.2",
@ -33,7 +34,7 @@
"pmtiles": "3.0.7", "pmtiles": "3.0.7",
"proj4": "2.11.0", "proj4": "2.11.0",
"sanitize-filename": "1.6.3", "sanitize-filename": "1.6.3",
"sharp": "0.33.4", "sharp": "^0.33.4",
"tileserver-gl-styles": "2.0.0" "tileserver-gl-styles": "2.0.0"
}, },
"bin": { "bin": {
@ -2740,6 +2741,12 @@
"node": ">= 8" "node": ">= 8"
} }
}, },
"node_modules/crypto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz",
"integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==",
"deprecated": "This package is no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in."
},
"node_modules/d3-queue": { "node_modules/d3-queue": {
"version": "3.0.7", "version": "3.0.7",
"resolved": "https://registry.npmjs.org/d3-queue/-/d3-queue-3.0.7.tgz", "resolved": "https://registry.npmjs.org/d3-queue/-/d3-queue-3.0.7.tgz",

View file

@ -34,6 +34,7 @@
"color": "4.2.3", "color": "4.2.3",
"commander": "12.1.0", "commander": "12.1.0",
"cors": "2.8.5", "cors": "2.8.5",
"crypto": "^1.0.1",
"express": "4.19.2", "express": "4.19.2",
"handlebars": "4.7.8", "handlebars": "4.7.8",
"http-shutdown": "1.2.2", "http-shutdown": "1.2.2",
@ -42,7 +43,7 @@
"pmtiles": "3.0.7", "pmtiles": "3.0.7",
"proj4": "2.11.0", "proj4": "2.11.0",
"sanitize-filename": "1.6.3", "sanitize-filename": "1.6.3",
"sharp": "0.33.4", "sharp": "^0.33.4",
"tileserver-gl-styles": "2.0.0" "tileserver-gl-styles": "2.0.0"
}, },
"devDependencies": { "devDependencies": {

View file

@ -10,7 +10,7 @@ import MBTiles from '@mapbox/mbtiles';
import Pbf from 'pbf'; import Pbf from 'pbf';
import { VectorTile } from '@mapbox/vector-tile'; import { VectorTile } from '@mapbox/vector-tile';
import { getTileUrls, isValidHttpUrl, fixTileJSONCenter } from './utils.js'; import { getTileUrls, isValidHttpUrl, fixTileJSONCenter, getFileHash } from './utils.js';
import { import {
openPMtiles, openPMtiles,
getPMtilesInfo, getPMtilesInfo,
@ -179,6 +179,7 @@ export const serve_data = {
{ {
pbf: options.pbfAlias, pbf: options.pbfAlias,
}, },
info.cacheId
); );
return res.send(info); return res.send(info);
}); });
@ -230,10 +231,11 @@ export const serve_data = {
Object.assign(tileJSON, metadata); Object.assign(tileJSON, metadata);
tileJSON['tilejson'] = '2.0.0'; tileJSON['tilejson'] = '2.0.0';
tileJSON['cacheId']= btoa(tileJSON['mtime']);
delete tileJSON['filesize']; delete tileJSON['filesize'];
delete tileJSON['mtime']; delete tileJSON['mtime'];
delete tileJSON['scheme']; delete tileJSON['scheme'];
Object.assign(tileJSON, params.tilejson || {}); Object.assign(tileJSON, params.tilejson || {});
fixTileJSONCenter(tileJSON); fixTileJSONCenter(tileJSON);
@ -242,6 +244,8 @@ export const serve_data = {
} }
} else if (inputType === 'mbtiles') { } else if (inputType === 'mbtiles') {
sourceType = 'mbtiles'; sourceType = 'mbtiles';
tileJSON['cacheId']= await getFileHash(inputFile)
const sourceInfoPromise = new Promise((resolve, reject) => { const sourceInfoPromise = new Promise((resolve, reject) => {
source = new MBTiles(inputFile + '?mode=ro', (err) => { source = new MBTiles(inputFile + '?mode=ro', (err) => {
if (err) { if (err) {

View file

@ -847,6 +847,8 @@ export const serve_rendered = {
tileSize, tileSize,
info.format, info.format,
item.publicUrl, item.publicUrl,
null,
info.cacheId
); );
return res.send(info); return res.send(info);
}); });

View file

@ -373,6 +373,7 @@ function start(opts) {
{ {
pbf: options.pbfAlias, pbf: options.pbfAlias,
}, },
info.cacheId
); );
arr.push(info); arr.push(info);
} }
@ -479,6 +480,8 @@ function start(opts) {
tileSize, tileSize,
style.serving_rendered.tileJSON.format, style.serving_rendered.tileJSON.format,
opts.publicUrl, opts.publicUrl,
null,
style.serving_rendered.tileJSON.cacheId
)[0]; )[0];
} }
@ -517,6 +520,7 @@ function start(opts) {
{ {
pbf: options.pbfAlias, pbf: options.pbfAlias,
}, },
tileJSON.cacheId
)[0]; )[0];
if (data.filesize) { if (data.filesize) {
@ -598,7 +602,7 @@ function start(opts) {
serveTemplate('/data/:id/$', 'data', (req) => { serveTemplate('/data/:id/$', 'data', (req) => {
const { id } = req.params; const { id } = req.params;
const data = serving.data[id]; const data = serving.data[id];
debugger
if (!data) { if (!data) {
return null; return null;
} }

View file

@ -5,7 +5,7 @@ import fsPromises from 'fs/promises';
import fs, { existsSync } from 'node:fs'; import fs, { existsSync } from 'node:fs';
import clone from 'clone'; import clone from 'clone';
import { combine } from '@jsse/pbfont'; import { combine } from '@jsse/pbfont';
import * as crypto from 'crypto';
/** /**
* Restrict user input to an allowed set of options. * Restrict user input to an allowed set of options.
* @param opts * @param opts
@ -72,6 +72,7 @@ export const getTileUrls = (
format, format,
publicUrl, publicUrl,
aliases, aliases,
cacheId
) => { ) => {
const urlObject = getUrlObject(req); const urlObject = getUrlObject(req);
if (domains) { if (domains) {
@ -107,6 +108,11 @@ export const getTileUrls = (
if (req.query.style) { if (req.query.style) {
queryParams.push(`style=${encodeURIComponent(req.query.style)}`); queryParams.push(`style=${encodeURIComponent(req.query.style)}`);
} }
if(cacheId){
queryParams.push(`cache-id=${encodeURIComponent(cacheId)}`);
}
const query = queryParams.length > 0 ? `?${queryParams.join('&')}` : ''; const query = queryParams.length > 0 ? `?${queryParams.join('&')}` : '';
if (aliases && aliases[format]) { if (aliases && aliases[format]) {
@ -130,6 +136,7 @@ export const getTileUrls = (
uris.push(`${publicUrl}${path}/${tileParams}.${format}${query}`); uris.push(`${publicUrl}${path}/${tileParams}.${format}${query}`);
} }
return uris; return uris;
}; };
@ -245,3 +252,12 @@ export const isValidHttpUrl = (string) => {
return url.protocol === 'http:' || url.protocol === 'https:'; return url.protocol === 'http:' || url.protocol === 'https:';
}; };
export const getFileHash = path => new Promise((resolve, reject) => {
const hash = crypto.createHash('sha1');
const rs = fs.createReadStream(path);
rs.on('error', reject);
rs.on('data', chunk => hash.update(chunk));
rs.on('end', () => resolve(hash.digest('base64')));
})