Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"color": "4.2.3",
"commander": "12.1.0",
"cors": "2.8.5",
"crypto": "^1.0.1",
"express": "4.19.2",
"handlebars": "4.7.8",
"http-shutdown": "1.2.2",
Expand All @@ -42,7 +43,7 @@
"pmtiles": "3.0.6",
"proj4": "2.11.0",
"sanitize-filename": "1.6.3",
"sharp": "0.33.4",
"sharp": "^0.33.4",
"tileserver-gl-styles": "2.0.0"
},
"devDependencies": {
Expand Down
8 changes: 6 additions & 2 deletions src/serve_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import MBTiles from '@mapbox/mbtiles';
import Pbf from 'pbf';
import { VectorTile } from '@mapbox/vector-tile';

import { getTileUrls, isValidHttpUrl, fixTileJSONCenter } from './utils.js';
import { getTileUrls, isValidHttpUrl, fixTileJSONCenter, getFileHash } from './utils.js';
import {
openPMtiles,
getPMtilesInfo,
Expand Down Expand Up @@ -179,6 +179,7 @@ export const serve_data = {
{
pbf: options.pbfAlias,
},
info.cacheId
);
return res.send(info);
});
Expand Down Expand Up @@ -230,10 +231,11 @@ export const serve_data = {
Object.assign(tileJSON, metadata);

tileJSON['tilejson'] = '2.0.0';
tileJSON['cacheId']= btoa(tileJSON['mtime']);
delete tileJSON['filesize'];
delete tileJSON['mtime'];
delete tileJSON['scheme'];

Object.assign(tileJSON, params.tilejson || {});
fixTileJSONCenter(tileJSON);

Expand All @@ -242,6 +244,8 @@ export const serve_data = {
}
} else if (inputType === 'mbtiles') {
sourceType = 'mbtiles';
tileJSON['cacheId']= await getFileHash(inputFile)

Check notice

Code scanning / CodeQL

Semicolon insertion

Avoid automated semicolon insertion (96% of all statements in [the enclosing function](1) have an explicit semicolon).

const sourceInfoPromise = new Promise((resolve, reject) => {
source = new MBTiles(inputFile + '?mode=ro', (err) => {
if (err) {
Expand Down
2 changes: 2 additions & 0 deletions src/serve_rendered.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,8 @@ export const serve_rendered = {
tileSize,
info.format,
item.publicUrl,
null,
info.cacheId
);
return res.send(info);
});
Expand Down
6 changes: 5 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ function start(opts) {
{
pbf: options.pbfAlias,
},
info.cacheId
);
arr.push(info);
}
Expand Down Expand Up @@ -479,6 +480,8 @@ function start(opts) {
tileSize,
style.serving_rendered.tileJSON.format,
opts.publicUrl,
null,
style.serving_rendered.tileJSON.cacheId
)[0];
}

Expand Down Expand Up @@ -517,6 +520,7 @@ function start(opts) {
{
pbf: options.pbfAlias,
},
tileJSON.cacheId
)[0];

if (data.filesize) {
Expand Down Expand Up @@ -598,7 +602,7 @@ function start(opts) {
serveTemplate('/data/:id/$', 'data', (req) => {
const { id } = req.params;
const data = serving.data[id];

debugger
if (!data) {
return null;
}
Expand Down
18 changes: 17 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fsPromises from 'fs/promises';
import fs, { existsSync } from 'node:fs';
import clone from 'clone';
import { combine } from '@jsse/pbfont';

import * as crypto from 'crypto';
/**
* Restrict user input to an allowed set of options.
* @param opts
Expand Down Expand Up @@ -72,6 +72,7 @@ export const getTileUrls = (
format,
publicUrl,
aliases,
cacheId
) => {
const urlObject = getUrlObject(req);
if (domains) {
Expand Down Expand Up @@ -107,6 +108,11 @@ export const getTileUrls = (
if (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('&')}` : '';

if (aliases && aliases[format]) {
Expand All @@ -130,6 +136,7 @@ export const getTileUrls = (
uris.push(`${publicUrl}${path}/${tileParams}.${format}${query}`);
}


return uris;
};

Expand Down Expand Up @@ -245,3 +252,12 @@ export const isValidHttpUrl = (string) => {

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')));
})

Check notice

Code scanning / CodeQL

Semicolon insertion

Avoid automated semicolon insertion (90% of all statements in [the enclosing script](1) have an explicit semicolon).