switch to esm module

This commit is contained in:
acalcutt 2022-09-16 15:59:42 -04:00
parent 49fae1e739
commit 1d289c5a55
9 changed files with 398 additions and 384 deletions

View file

@ -4,13 +4,14 @@
"description": "Map tile server for JSON GL styles - vector and server side generated raster tiles", "description": "Map tile server for JSON GL styles - vector and server side generated raster tiles",
"main": "src/main.js", "main": "src/main.js",
"bin": "src/main.js", "bin": "src/main.js",
"type": "module",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/maptiler/tileserver-gl.git" "url": "git+https://github.com/maptiler/tileserver-gl.git"
}, },
"license": "BSD-2-Clause", "license": "BSD-2-Clause",
"engines": { "engines": {
"node": ">=10 <17" "node": ">=14.13.0"
}, },
"scripts": { "scripts": {
"test": "mocha test/**.js --timeout 10000", "test": "mocha test/**.js --timeout 10000",
@ -25,25 +26,24 @@
"@mapbox/vector-tile": "1.3.1", "@mapbox/vector-tile": "1.3.1",
"advanced-pool": "0.3.3", "advanced-pool": "0.3.3",
"canvas": "2.9.3", "canvas": "2.9.3",
"chokidar": "3.3.1", "chokidar": "3.5.3",
"clone": "2.1.2", "clone": "2.1.2",
"color": "3.1.2", "color": "4.2.3",
"commander": "4.1.1", "commander": "9.4.0",
"cors": "2.8.5", "cors": "2.8.5",
"esm": "3.2.25", "express": "4.18.1",
"express": "4.17.1", "handlebars": "4.7.7",
"handlebars": "4.7.3",
"http-shutdown": "1.2.2", "http-shutdown": "1.2.2",
"morgan": "1.9.1", "morgan": "1.10.0",
"pbf": "3.2.1", "pbf": "3.2.1",
"proj4": "2.6.0", "proj4": "2.8.0",
"request": "2.88.2", "request": "2.88.2",
"sharp": "0.26.2", "sharp": "0.30.7",
"tileserver-gl-styles": "2.0.0" "tileserver-gl-styles": "2.0.0"
}, },
"devDependencies": { "devDependencies": {
"mocha": "^7.1.0", "mocha": "^10.0.0",
"should": "^13.2.3", "chai": "4.3.6",
"supertest": "^4.0.2" "supertest": "^6.2.4"
} }
} }

View file

@ -2,22 +2,25 @@
'use strict'; 'use strict';
require = require('esm')(module); import fs from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';
import request from 'request';
import {server} from './server.js';
const fs = require('fs'); import MBTiles from '@mapbox/mbtiles';
const path = require('path');
const request = require('request');
const MBTiles = require('@mapbox/mbtiles'); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const packageJson = require('../package'); const packageJson = JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8'));
const args = process.argv; const args = process.argv;
if (args.length >= 3 && args[2][0] !== '-') { if (args.length >= 3 && args[2][0] !== '-') {
args.splice(2, 0, '--mbtiles'); args.splice(2, 0, '--mbtiles');
} }
const opts = require('commander') import { program } from 'commander';
program
.description('tileserver-gl startup options') .description('tileserver-gl startup options')
.usage('tileserver-gl [mbtiles] [options]') .usage('tileserver-gl [mbtiles] [options]')
.option( .option(
@ -68,8 +71,8 @@ const opts = require('commander')
packageJson.version, packageJson.version,
'-v, --version' '-v, --version'
) )
.parse(args); program.parse(process.argv);
const opts = program.opts();
console.log(`Starting ${packageJson.name} v${packageJson.version}`); console.log(`Starting ${packageJson.name} v${packageJson.version}`);
const startServer = (configPath, config) => { const startServer = (configPath, config) => {
@ -77,7 +80,7 @@ const startServer = (configPath, config) => {
if (publicUrl && publicUrl.lastIndexOf('/') !== publicUrl.length - 1) { if (publicUrl && publicUrl.lastIndexOf('/') !== publicUrl.length - 1) {
publicUrl += '/'; publicUrl += '/';
} }
return require('./server')({ return server({
configPath: configPath, configPath: configPath,
config: config, config: config,
bind: opts.bind, bind: opts.bind,
@ -87,7 +90,7 @@ const startServer = (configPath, config) => {
silent: opts.silent, silent: opts.silent,
logFile: opts.log_file, logFile: opts.log_file,
logFormat: opts.log_format, logFormat: opts.log_format,
publicUrl: publicUrl publicUrl: publicUrl,
}); });
}; };
@ -118,49 +121,48 @@ const startWithMBTiles = (mbtilesFile) => {
} }
const bounds = info.bounds; const bounds = info.bounds;
const styleDir = path.resolve(__dirname, "../node_modules/tileserver-gl-styles/"); const styleDir = path.resolve(__dirname, '../node_modules/tileserver-gl-styles/');
const config = { const config = {
"options": { 'options': {
"paths": { 'paths': {
"root": styleDir, 'root': styleDir,
"fonts": "fonts", 'fonts': 'fonts',
"styles": "styles", 'styles': 'styles',
"mbtiles": path.dirname(mbtilesFile) 'mbtiles': path.dirname(mbtilesFile),
} },
}, },
"styles": {}, 'styles': {},
"data": {} 'data': {},
}; };
if (info.format === 'pbf' && if (info.format === 'pbf' &&
info.name.toLowerCase().indexOf('openmaptiles') > -1) { info.name.toLowerCase().indexOf('openmaptiles') > -1) {
config['data'][`v3`] = { config['data'][`v3`] = {
"mbtiles": path.basename(mbtilesFile) 'mbtiles': path.basename(mbtilesFile),
}; };
const styles = fs.readdirSync(path.resolve(styleDir, 'styles')); const styles = fs.readdirSync(path.resolve(styleDir, 'styles'));
for (let styleName of styles) { for (const styleName of styles) {
const styleFileRel = styleName + '/style.json'; const styleFileRel = styleName + '/style.json';
const styleFile = path.resolve(styleDir, 'styles', styleFileRel); const styleFile = path.resolve(styleDir, 'styles', styleFileRel);
if (fs.existsSync(styleFile)) { if (fs.existsSync(styleFile)) {
config['styles'][styleName] = { config['styles'][styleName] = {
"style": styleFileRel, 'style': styleFileRel,
"tilejson": { 'tilejson': {
"bounds": bounds 'bounds': bounds,
} },
}; };
} }
} }
} else { } else {
console.log(`WARN: MBTiles not in "openmaptiles" format. Serving raw data only...`); console.log(`WARN: MBTiles not in "openmaptiles" format. Serving raw data only...`);
config['data'][(info.id || 'mbtiles') config['data'][(info.id || 'mbtiles')
.replace(/\//g, '_') .replace(/\//g, '_')
.replace(/:/g, '_') .replace(/:/g, '_')
.replace(/\?/g, '_')] = { .replace(/\?/g, '_')] = {
"mbtiles": path.basename(mbtilesFile) 'mbtiles': path.basename(mbtilesFile),
}; };
} }
@ -181,7 +183,7 @@ fs.stat(path.resolve(opts.config), (err, stats) => {
if (!mbtiles) { if (!mbtiles) {
// try to find in the cwd // try to find in the cwd
const files = fs.readdirSync(process.cwd()); const files = fs.readdirSync(process.cwd());
for (let filename of files) { for (const filename of files) {
if (filename.endsWith('.mbtiles')) { if (filename.endsWith('.mbtiles')) {
const mbTilesStats = fs.statSync(filename); const mbTilesStats = fs.statSync(filename);
if (mbTilesStats.isFile() && mbTilesStats.size > 0) { if (mbTilesStats.isFile() && mbTilesStats.size > 0) {

View file

@ -1,18 +1,18 @@
'use strict'; 'use strict';
const fs = require('fs'); import fs from 'fs';
const path = require('path'); import path from 'path';
const zlib = require('zlib'); import zlib from 'zlib';
const clone = require('clone'); import clone from 'clone';
const express = require('express'); import express from 'express';
const MBTiles = require('@mapbox/mbtiles'); import MBTiles from '@mapbox/mbtiles';
const Pbf = require('pbf'); import Pbf from 'pbf';
const VectorTile = require('@mapbox/vector-tile').VectorTile; import VectorTile from '@mapbox/vector-tile';
const utils = require('./utils'); import {getTileUrls, fixTileJSONCenter} from './utils.js';
module.exports = { export const serve_data = {
init: (options, repo) => { init: (options, repo) => {
const app = express().disable('x-powered-by'); const app = express().disable('x-powered-by');
@ -21,7 +21,7 @@ module.exports = {
if (!item) { if (!item) {
return res.sendStatus(404); return res.sendStatus(404);
} }
let tileJSONFormat = item.tileJSON.format; const tileJSONFormat = item.tileJSON.format;
const z = req.params.z | 0; const z = req.params.z | 0;
const x = req.params.x | 0; const x = req.params.x | 0;
const y = req.params.y | 0; const y = req.params.y | 0;
@ -52,7 +52,7 @@ module.exports = {
} else { } else {
if (tileJSONFormat === 'pbf') { if (tileJSONFormat === 'pbf') {
isGzipped = data.slice(0, 2).indexOf( isGzipped = data.slice(0, 2).indexOf(
Buffer.from([0x1f, 0x8b])) === 0; Buffer.from([0x1f, 0x8b])) === 0;
if (options.dataDecoratorFunc) { if (options.dataDecoratorFunc) {
if (isGzipped) { if (isGzipped) {
data = zlib.unzipSync(data); data = zlib.unzipSync(data);
@ -73,10 +73,10 @@ module.exports = {
const tile = new VectorTile(new Pbf(data)); const tile = new VectorTile(new Pbf(data));
const geojson = { const geojson = {
"type": "FeatureCollection", 'type': 'FeatureCollection',
"features": [] 'features': [],
}; };
for (let layerName in tile.layers) { for (const layerName in tile.layers) {
const layer = tile.layers[layerName]; const layer = tile.layers[layerName];
for (let i = 0; i < layer.length; i++) { for (let i = 0; i < layer.length; i++) {
const feature = layer.feature(i); const feature = layer.feature(i);
@ -108,10 +108,10 @@ module.exports = {
return res.sendStatus(404); return res.sendStatus(404);
} }
const info = clone(item.tileJSON); const info = clone(item.tileJSON);
info.tiles = utils.getTileUrls(req, info.tiles, info.tiles = getTileUrls(req, info.tiles,
`data/${req.params.id}`, info.format, item.publicUrl, { `data/${req.params.id}`, info.format, item.publicUrl, {
'pbf': options.pbfAlias 'pbf': options.pbfAlias,
}); });
return res.send(info); return res.send(info);
}); });
@ -120,7 +120,7 @@ module.exports = {
add: (options, repo, params, id, publicUrl) => { add: (options, repo, params, id, publicUrl) => {
const mbtilesFile = path.resolve(options.paths.mbtiles, params.mbtiles); const mbtilesFile = path.resolve(options.paths.mbtiles, params.mbtiles);
let tileJSON = { let tileJSON = {
'tiles': params.domains || options.domains 'tiles': params.domains || options.domains,
}; };
const mbtilesFileStats = fs.statSync(mbtilesFile); const mbtilesFileStats = fs.statSync(mbtilesFile);
@ -145,12 +145,13 @@ module.exports = {
Object.assign(tileJSON, info); Object.assign(tileJSON, info);
tileJSON['tilejson'] = '2.0.0'; tileJSON['tilejson'] = '2.0.0';
tileJSON['id'] = id;
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 || {});
utils.fixTileJSONCenter(tileJSON); fixTileJSONCenter(tileJSON);
if (options.dataDecoratorFunc) { if (options.dataDecoratorFunc) {
tileJSON = options.dataDecoratorFunc(id, 'tilejson', tileJSON); tileJSON = options.dataDecoratorFunc(id, 'tilejson', tileJSON);
@ -164,8 +165,8 @@ module.exports = {
repo[id] = { repo[id] = {
tileJSON, tileJSON,
publicUrl, publicUrl,
source source,
} };
}); });
} },
}; };

View file

@ -1,12 +1,12 @@
'use strict'; 'use strict';
const express = require('express'); import express from 'express';
const fs = require('fs'); import fs from 'fs';
const path = require('path'); import path from 'path';
const utils = require('./utils'); import {getFontsPbf} from './utils.js';
module.exports = (options, allowedFonts) => { export const serve_font = (options, allowedFonts) => {
const app = express().disable('x-powered-by'); const app = express().disable('x-powered-by');
const lastModified = new Date().toUTCString(); const lastModified = new Date().toUTCString();
@ -40,19 +40,19 @@ module.exports = (options, allowedFonts) => {
const fontstack = decodeURI(req.params.fontstack); const fontstack = decodeURI(req.params.fontstack);
const range = req.params.range; const range = req.params.range;
utils.getFontsPbf(options.serveAllFonts ? null : allowedFonts, getFontsPbf(options.serveAllFonts ? null : allowedFonts,
fontPath, fontstack, range, existingFonts).then(concated => { fontPath, fontstack, range, existingFonts).then((concated) => {
res.header('Content-type', 'application/x-protobuf'); res.header('Content-type', 'application/x-protobuf');
res.header('Last-Modified', lastModified); res.header('Last-Modified', lastModified);
return res.send(concated); return res.send(concated);
}, err => res.status(400).send(err) }, (err) => res.status(400).send(err),
); );
}); });
app.get('/fonts.json', (req, res, next) => { app.get('/fonts.json', (req, res, next) => {
res.header('Content-type', 'application/json'); res.header('Content-type', 'application/json');
return res.send( return res.send(
Object.keys(options.serveAllFonts ? existingFonts : allowedFonts).sort() Object.keys(options.serveAllFonts ? existingFonts : allowedFonts).sort(),
); );
}); });

10
src/serve_light.js Normal file
View file

@ -0,0 +1,10 @@
'use strict';
export const serve_rendered = {
init: (options, repo) => {
},
add: (options, repo, params, id, publicUrl, dataResolver) => {
},
remove: (repo, id) => {
},
};

View file

@ -1,37 +1,33 @@
'use strict'; 'use strict';
const advancedPool = require('advanced-pool'); import advancedPool from 'advanced-pool';
const fs = require('fs'); import fs from 'fs';
const path = require('path'); import path from 'path';
const url = require('url'); import url from 'url';
const util = require('util'); import util from 'util';
const zlib = require('zlib'); import zlib from 'zlib';
import sharp from 'sharp'; // sharp has to be required before node-canvas. see https://github.com/lovell/sharp/issues/371
// sharp has to be required before node-canvas import pkg from 'canvas';
// see https://github.com/lovell/sharp/issues/371 import clone from 'clone';
const sharp = require('sharp'); import Color from 'color';
import express from 'express';
const { createCanvas } = require('canvas'); import SphericalMercator from '@mapbox/sphericalmercator';
import mlgl from '@acalcutt/maplibre-gl-native';
const clone = require('clone'); import MBTiles from '@mapbox/mbtiles';
const Color = require('color'); import proj4 from 'proj4';
const express = require('express'); import request from 'request';
const mercator = new (require('@mapbox/sphericalmercator'))(); import {getFontsPbf, getTileUrls, fixTileJSONCenter} from './utils.js';
const mbgl = require('@maplibre/maplibre-gl-native');
const MBTiles = require('@mapbox/mbtiles');
const proj4 = require('proj4');
const request = require('request');
const utils = require('./utils');
const FLOAT_PATTERN = '[+-]?(?:\\d+|\\d+\.?\\d+)'; const FLOAT_PATTERN = '[+-]?(?:\\d+|\\d+\.?\\d+)';
const httpTester = /^(http(s)?:)?\/\//; const httpTester = /^(http(s)?:)?\/\//;
const getScale = scale => (scale || '@1x').slice(1, 2) | 0; const {createCanvas} = pkg;
const mercator = new SphericalMercator();
const getScale = (scale) => (scale || '@1x').slice(1, 2) | 0;
mbgl.on('message', e => { mlgl.on('message', (e) => {
if (e.severity === 'WARNING' || e.severity === 'ERROR') { if (e.severity === 'WARNING' || e.severity === 'ERROR') {
console.log('mbgl:', e); console.log('mlgl:', e);
} }
}); });
@ -42,7 +38,7 @@ const extensionToFormat = {
'.jpg': 'jpeg', '.jpg': 'jpeg',
'.jpeg': 'jpeg', '.jpeg': 'jpeg',
'.png': 'png', '.png': 'png',
'.webp': 'webp' '.webp': 'webp',
}; };
/** /**
@ -50,18 +46,18 @@ const extensionToFormat = {
* string is for unknown or unsupported formats. * string is for unknown or unsupported formats.
*/ */
const cachedEmptyResponses = { const cachedEmptyResponses = {
'': Buffer.alloc(0) '': Buffer.alloc(0),
}; };
/** /**
* Create an appropriate mbgl response for http errors. * Create an appropriate mlgl response for http errors.
* @param {string} format The format (a sharp format or 'pbf'). * @param {string} format The format (a sharp format or 'pbf').
* @param {string} color The background color (or empty string for transparent). * @param {string} color The background color (or empty string for transparent).
* @param {Function} callback The mbgl callback. * @param {Function} callback The mlgl callback.
*/ */
function createEmptyResponse(format, color, callback) { function createEmptyResponse(format, color, callback) {
if (!format || format === 'pbf') { if (!format || format === 'pbf') {
callback(null, { data: cachedEmptyResponses[''] }); callback(null, {data: cachedEmptyResponses['']});
return; return;
} }
@ -75,7 +71,7 @@ function createEmptyResponse(format, color, callback) {
const cacheKey = `${format},${color}`; const cacheKey = `${format},${color}`;
const data = cachedEmptyResponses[cacheKey]; const data = cachedEmptyResponses[cacheKey];
if (data) { if (data) {
callback(null, { data: data }); callback(null, {data: data});
return; return;
} }
@ -87,13 +83,13 @@ function createEmptyResponse(format, color, callback) {
raw: { raw: {
width: 1, width: 1,
height: 1, height: 1,
channels: channels channels: channels,
} },
}).toFormat(format).toBuffer((err, buffer, info) => { }).toFormat(format).toBuffer((err, buffer, info) => {
if (!err) { if (!err) {
cachedEmptyResponses[cacheKey] = buffer; cachedEmptyResponses[cacheKey] = buffer;
} }
callback(null, { data: buffer }); callback(null, {data: buffer});
}); });
} }
@ -119,7 +115,7 @@ const extractPathFromQuery = (query, transformer) => {
}; };
const renderOverlay = (z, x, y, bearing, pitch, w, h, scale, const renderOverlay = (z, x, y, bearing, pitch, w, h, scale,
path, query) => { path, query) => {
if (!path || path.length < 2) { if (!path || path.length < 2) {
return null; return null;
} }
@ -179,14 +175,14 @@ const calcZForBBox = (bbox, w, h, query) => {
const padding = query.padding !== undefined ? const padding = query.padding !== undefined ?
parseFloat(query.padding) : 0.1; parseFloat(query.padding) : 0.1;
const minCorner = mercator.px([bbox[0], bbox[3]], z), const minCorner = mercator.px([bbox[0], bbox[3]], z);
maxCorner = mercator.px([bbox[2], bbox[1]], z); const maxCorner = mercator.px([bbox[2], bbox[1]], z);
const w_ = w / (1 + 2 * padding); const w_ = w / (1 + 2 * padding);
const h_ = h / (1 + 2 * padding); const h_ = h / (1 + 2 * padding);
z -= Math.max( z -= Math.max(
Math.log((maxCorner[0] - minCorner[0]) / w_), Math.log((maxCorner[0] - minCorner[0]) / w_),
Math.log((maxCorner[1] - minCorner[1]) / h_) Math.log((maxCorner[1] - minCorner[1]) / h_),
) / Math.LN2; ) / Math.LN2;
z = Math.max(Math.log(Math.max(w, h) / 256) / Math.LN2, Math.min(25, z)); z = Math.max(Math.log(Math.max(w, h) / 256) / Math.LN2, Math.min(25, z));
@ -197,7 +193,7 @@ const calcZForBBox = (bbox, w, h, query) => {
const existingFonts = {}; const existingFonts = {};
let maxScaleFactor = 2; let maxScaleFactor = 2;
module.exports = { export const serve_rendered = {
init: (options, repo) => { init: (options, repo) => {
const fontListingPromise = new Promise((resolve, reject) => { const fontListingPromise = new Promise((resolve, reject) => {
fs.readdir(options.paths.fonts, (err, files) => { fs.readdir(options.paths.fonts, (err, files) => {
@ -230,8 +226,8 @@ module.exports = {
const app = express().disable('x-powered-by'); const app = express().disable('x-powered-by');
const respondImage = (item, z, lon, lat, bearing, pitch, const respondImage = (item, z, lon, lat, bearing, pitch,
width, height, scale, format, res, next, width, height, scale, format, res, next,
opt_overlay) => { opt_overlay) => {
if (Math.abs(lon) > 180 || Math.abs(lat) > 85.06 || if (Math.abs(lon) > 180 || Math.abs(lat) > 85.06 ||
lon !== lon || lat !== lat) { lon !== lon || lat !== lat) {
return res.status(400).send('Invalid center'); return res.status(400).send('Invalid center');
@ -250,14 +246,14 @@ module.exports = {
const pool = item.map.renderers[scale]; const pool = item.map.renderers[scale];
pool.acquire((err, renderer) => { pool.acquire((err, renderer) => {
const mbglZ = Math.max(0, z - 1); const mlglZ = Math.max(0, z - 1);
const params = { const params = {
zoom: mbglZ, zoom: mlglZ,
center: [lon, lat], center: [lon, lat],
bearing: bearing, bearing: bearing,
pitch: pitch, pitch: pitch,
width: width, width: width,
height: height height: height,
}; };
if (z === 0) { if (z === 0) {
params.width *= 2; params.width *= 2;
@ -279,9 +275,9 @@ module.exports = {
// Fix semi-transparent outlines on raw, premultiplied input // Fix semi-transparent outlines on raw, premultiplied input
// https://github.com/maptiler/tileserver-gl/issues/350#issuecomment-477857040 // https://github.com/maptiler/tileserver-gl/issues/350#issuecomment-477857040
for (var i = 0; i < data.length; i += 4) { for (let i = 0; i < data.length; i += 4) {
var alpha = data[i + 3]; const alpha = data[i + 3];
var norm = alpha / 255; const norm = alpha / 255;
if (alpha === 0) { if (alpha === 0) {
data[i] = 0; data[i] = 0;
data[i + 1] = 0; data[i + 1] = 0;
@ -297,16 +293,18 @@ module.exports = {
raw: { raw: {
width: params.width * scale, width: params.width * scale,
height: params.height * scale, height: params.height * scale,
channels: 4 channels: 4,
} },
}); });
if (z > 2 && tileMargin > 0) { if (z > 2 && tileMargin > 0) {
const [_, y] = mercator.px(params.center, z);
let yoffset = Math.max(Math.min(0, y - 128 - tileMargin), y + 128 + tileMargin - Math.pow(2, z + 8));
image.extract({ image.extract({
left: tileMargin * scale, left: tileMargin * scale,
top: tileMargin * scale, top: (tileMargin + yoffset) * scale,
width: width * scale, width: width * scale,
height: height * scale height: height * scale,
}); });
} }
@ -316,7 +314,7 @@ module.exports = {
} }
if (opt_overlay) { if (opt_overlay) {
image.composite([{ input: opt_overlay }]); image.composite([{input: opt_overlay}]);
} }
if (item.watermark) { if (item.watermark) {
const canvas = createCanvas(scale * width, scale * height); const canvas = createCanvas(scale * width, scale * height);
@ -329,17 +327,17 @@ module.exports = {
ctx.fillStyle = 'rgba(0,0,0,.4)'; ctx.fillStyle = 'rgba(0,0,0,.4)';
ctx.fillText(item.watermark, 5, height - 5); ctx.fillText(item.watermark, 5, height - 5);
image.composite([{ input: canvas.toBuffer() }]); image.composite([{input: canvas.toBuffer()}]);
} }
const formatQuality = (options.formatQuality || {})[format]; const formatQuality = (options.formatQuality || {})[format];
if (format === 'png') { if (format === 'png') {
image.png({ adaptiveFiltering: false }); image.png({adaptiveFiltering: false});
} else if (format === 'jpeg') { } else if (format === 'jpeg') {
image.jpeg({ quality: formatQuality || 80 }); image.jpeg({quality: formatQuality || 80});
} else if (format === 'webp') { } else if (format === 'webp') {
image.webp({ quality: formatQuality || 90 }); image.webp({quality: formatQuality || 90});
} }
image.toBuffer((err, buffer, info) => { image.toBuffer((err, buffer, info) => {
if (!buffer) { if (!buffer) {
@ -348,7 +346,7 @@ module.exports = {
res.set({ res.set({
'Last-Modified': item.lastModified, 'Last-Modified': item.lastModified,
'Content-Type': `image/${format}` 'Content-Type': `image/${format}`,
}); });
return res.status(200).send(buffer); return res.status(200).send(buffer);
}); });
@ -362,18 +360,18 @@ module.exports = {
return res.sendStatus(404); return res.sendStatus(404);
} }
const modifiedSince = req.get('if-modified-since'), cc = req.get('cache-control'); const modifiedSince = req.get('if-modified-since'); const cc = req.get('cache-control');
if (modifiedSince && (!cc || cc.indexOf('no-cache') === -1)) { if (modifiedSince && (!cc || cc.indexOf('no-cache') === -1)) {
if (new Date(item.lastModified) <= new Date(modifiedSince)) { if (new Date(item.lastModified) <= new Date(modifiedSince)) {
return res.sendStatus(304); return res.sendStatus(304);
} }
} }
const z = req.params.z | 0, const z = req.params.z | 0;
x = req.params.x | 0, const x = req.params.x | 0;
y = req.params.y | 0, const y = req.params.y | 0;
scale = getScale(req.params.scale), const scale = getScale(req.params.scale);
format = req.params.format; const format = req.params.format;
if (z < 0 || x < 0 || y < 0 || if (z < 0 || x < 0 || y < 0 ||
z > 22 || x >= Math.pow(2, z) || y >= Math.pow(2, z)) { z > 22 || x >= Math.pow(2, z) || y >= Math.pow(2, z)) {
return res.status(404).send('Out of bounds'); return res.status(404).send('Out of bounds');
@ -381,10 +379,10 @@ module.exports = {
const tileSize = 256; const tileSize = 256;
const tileCenter = mercator.ll([ const tileCenter = mercator.ll([
((x + 0.5) / (1 << z)) * (256 << z), ((x + 0.5) / (1 << z)) * (256 << z),
((y + 0.5) / (1 << z)) * (256 << z) ((y + 0.5) / (1 << z)) * (256 << z),
], z); ], z);
return respondImage(item, z, tileCenter[0], tileCenter[1], 0, 0, return respondImage(item, z, tileCenter[0], tileCenter[1], 0, 0,
tileSize, tileSize, scale, format, res, next); tileSize, tileSize, scale, format, res, next);
}); });
if (options.serveStaticMaps !== false) { if (options.serveStaticMaps !== false) {
@ -393,8 +391,8 @@ module.exports = {
const centerPattern = const centerPattern =
util.format(':x(%s),:y(%s),:z(%s)(@:bearing(%s)(,:pitch(%s))?)?', util.format(':x(%s),:y(%s),:z(%s)(@:bearing(%s)(,:pitch(%s))?)?',
FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN,
FLOAT_PATTERN, FLOAT_PATTERN); FLOAT_PATTERN, FLOAT_PATTERN);
app.get(util.format(staticPattern, centerPattern), (req, res, next) => { app.get(util.format(staticPattern, centerPattern), (req, res, next) => {
const item = repo[req.params.id]; const item = repo[req.params.id];
@ -402,15 +400,15 @@ module.exports = {
return res.sendStatus(404); return res.sendStatus(404);
} }
const raw = req.params.raw; const raw = req.params.raw;
let z = +req.params.z, const z = +req.params.z;
x = +req.params.x, let x = +req.params.x;
y = +req.params.y, let y = +req.params.y;
bearing = +(req.params.bearing || '0'), const bearing = +(req.params.bearing || '0');
pitch = +(req.params.pitch || '0'), const pitch = +(req.params.pitch || '0');
w = req.params.width | 0, const w = req.params.width | 0;
h = req.params.height | 0, const h = req.params.height | 0;
scale = getScale(req.params.scale), const scale = getScale(req.params.scale);
format = req.params.format; const format = req.params.format;
if (z < 0) { if (z < 0) {
return res.status(404).send('Invalid zoom'); return res.status(404).send('Invalid zoom');
@ -427,10 +425,10 @@ module.exports = {
const path = extractPathFromQuery(req.query, transformer); const path = extractPathFromQuery(req.query, transformer);
const overlay = renderOverlay(z, x, y, bearing, pitch, w, h, scale, const overlay = renderOverlay(z, x, y, bearing, pitch, w, h, scale,
path, req.query); path, req.query);
return respondImage(item, z, x, y, bearing, pitch, w, h, scale, format, return respondImage(item, z, x, y, bearing, pitch, w, h, scale, format,
res, next, overlay); res, next, overlay);
}); });
const serveBounds = (req, res, next) => { const serveBounds = (req, res, next) => {
@ -440,7 +438,7 @@ module.exports = {
} }
const raw = req.params.raw; const raw = req.params.raw;
const bbox = [+req.params.minx, +req.params.miny, const bbox = [+req.params.minx, +req.params.miny,
+req.params.maxx, +req.params.maxy]; +req.params.maxx, +req.params.maxy];
let center = [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2]; let center = [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2];
const transformer = raw ? const transformer = raw ?
@ -456,32 +454,32 @@ module.exports = {
center = transformer(center); center = transformer(center);
} }
const w = req.params.width | 0, const w = req.params.width | 0;
h = req.params.height | 0, const h = req.params.height | 0;
scale = getScale(req.params.scale), const scale = getScale(req.params.scale);
format = req.params.format; const format = req.params.format;
const z = calcZForBBox(bbox, w, h, req.query), const z = calcZForBBox(bbox, w, h, req.query);
x = center[0], const x = center[0];
y = center[1], const y = center[1];
bearing = 0, const bearing = 0;
pitch = 0; const pitch = 0;
const path = extractPathFromQuery(req.query, transformer); const path = extractPathFromQuery(req.query, transformer);
const overlay = renderOverlay(z, x, y, bearing, pitch, w, h, scale, const overlay = renderOverlay(z, x, y, bearing, pitch, w, h, scale,
path, req.query); path, req.query);
return respondImage(item, z, x, y, bearing, pitch, w, h, scale, format, return respondImage(item, z, x, y, bearing, pitch, w, h, scale, format,
res, next, overlay); res, next, overlay);
}; };
const boundsPattern = const boundsPattern =
util.format(':minx(%s),:miny(%s),:maxx(%s),:maxy(%s)', util.format(':minx(%s),:miny(%s),:maxx(%s),:maxy(%s)',
FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN); FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN);
app.get(util.format(staticPattern, boundsPattern), serveBounds); app.get(util.format(staticPattern, boundsPattern), serveBounds);
app.get('/:id/static/', (req, res, next) => { app.get('/:id/static/', (req, res, next) => {
for (let key in req.query) { for (const key in req.query) {
req.query[key.toLowerCase()] = req.query[key]; req.query[key.toLowerCase()] = req.query[key];
} }
req.params.raw = true; req.params.raw = true;
@ -510,12 +508,12 @@ module.exports = {
return res.sendStatus(404); return res.sendStatus(404);
} }
const raw = req.params.raw; const raw = req.params.raw;
const w = req.params.width | 0, const w = req.params.width | 0;
h = req.params.height | 0, const h = req.params.height | 0;
bearing = 0, const bearing = 0;
pitch = 0, const pitch = 0;
scale = getScale(req.params.scale), const scale = getScale(req.params.scale);
format = req.params.format; const format = req.params.format;
const transformer = raw ? const transformer = raw ?
mercator.inverse.bind(mercator) : item.dataProjWGStoInternalWGS; mercator.inverse.bind(mercator) : item.dataProjWGStoInternalWGS;
@ -535,18 +533,18 @@ module.exports = {
const bbox_ = mercator.convert(bbox, '900913'); const bbox_ = mercator.convert(bbox, '900913');
const center = mercator.inverse( const center = mercator.inverse(
[(bbox_[0] + bbox_[2]) / 2, (bbox_[1] + bbox_[3]) / 2] [(bbox_[0] + bbox_[2]) / 2, (bbox_[1] + bbox_[3]) / 2],
); );
const z = calcZForBBox(bbox, w, h, req.query), const z = calcZForBBox(bbox, w, h, req.query);
x = center[0], const x = center[0];
y = center[1]; const y = center[1];
const overlay = renderOverlay(z, x, y, bearing, pitch, w, h, scale, const overlay = renderOverlay(z, x, y, bearing, pitch, w, h, scale,
path, req.query); path, req.query);
return respondImage(item, z, x, y, bearing, pitch, w, h, scale, format, return respondImage(item, z, x, y, bearing, pitch, w, h, scale, format,
res, next, overlay); res, next, overlay);
}); });
} }
@ -556,8 +554,8 @@ module.exports = {
return res.sendStatus(404); return res.sendStatus(404);
} }
const info = clone(item.tileJSON); const info = clone(item.tileJSON);
info.tiles = utils.getTileUrls(req, info.tiles, info.tiles = getTileUrls(req, info.tiles,
`styles/${req.params.id}`, info.format, item.publicUrl); `styles/${req.params.id}`, info.format, item.publicUrl);
return res.send(info); return res.send(info);
}); });
@ -566,44 +564,44 @@ module.exports = {
add: (options, repo, params, id, publicUrl, dataResolver) => { add: (options, repo, params, id, publicUrl, dataResolver) => {
const map = { const map = {
renderers: [], renderers: [],
sources: {} sources: {},
}; };
let styleJSON; let styleJSON;
const createPool = (ratio, min, max) => { const createPool = (ratio, min, max) => {
const createRenderer = (ratio, createCallback) => { const createRenderer = (ratio, createCallback) => {
const renderer = new mbgl.Map({ const renderer = new mlgl.Map({
mode: "tile", mode: 'tile',
ratio: ratio, ratio: ratio,
request: (req, callback) => { request: (req, callback) => {
const protocol = req.url.split(':')[0]; const protocol = req.url.split(':')[0];
//console.log('Handling request:', req); // console.log('Handling request:', req);
if (protocol === 'sprites') { if (protocol === 'sprites') {
const dir = options.paths[protocol]; const dir = options.paths[protocol];
const file = unescape(req.url).substring(protocol.length + 3); const file = unescape(req.url).substring(protocol.length + 3);
fs.readFile(path.join(dir, file), (err, data) => { fs.readFile(path.join(dir, file), (err, data) => {
callback(err, { data: data }); callback(err, {data: data});
}); });
} else if (protocol === 'fonts') { } else if (protocol === 'fonts') {
const parts = req.url.split('/'); const parts = req.url.split('/');
const fontstack = unescape(parts[2]); const fontstack = unescape(parts[2]);
const range = parts[3].split('.')[0]; const range = parts[3].split('.')[0];
utils.getFontsPbf( getFontsPbf(
null, options.paths[protocol], fontstack, range, existingFonts null, options.paths[protocol], fontstack, range, existingFonts,
).then(concated => { ).then((concated) => {
callback(null, { data: concated }); callback(null, {data: concated});
}, err => { }, (err) => {
callback(err, { data: null }); callback(err, {data: null});
}); });
} else if (protocol === 'mbtiles') { } else if (protocol === 'mbtiles') {
const parts = req.url.split('/'); const parts = req.url.split('/');
const sourceId = parts[2]; const sourceId = parts[2];
const source = map.sources[sourceId]; const source = map.sources[sourceId];
const sourceInfo = styleJSON.sources[sourceId]; const sourceInfo = styleJSON.sources[sourceId];
const z = parts[3] | 0, const z = parts[3] | 0;
x = parts[4] | 0, const x = parts[4] | 0;
y = parts[5].split('.')[0] | 0, const y = parts[5].split('.')[0] | 0;
format = parts[5].split('.')[1]; const format = parts[5].split('.')[1];
source.getTile(z, x, y, (err, data, headers) => { source.getTile(z, x, y, (err, data, headers) => {
if (err) { if (err) {
if (options.verbose) console.log('MBTiles error, serving empty', err); if (options.verbose) console.log('MBTiles error, serving empty', err);
@ -620,11 +618,11 @@ module.exports = {
try { try {
response.data = zlib.unzipSync(data); response.data = zlib.unzipSync(data);
} catch (err) { } catch (err) {
console.log("Skipping incorrect header for tile mbtiles://%s/%s/%s/%s.pbf", id, z, x, y); console.log('Skipping incorrect header for tile mbtiles://%s/%s/%s/%s.pbf', id, z, x, y);
} }
if (options.dataDecoratorFunc) { if (options.dataDecoratorFunc) {
response.data = options.dataDecoratorFunc( response.data = options.dataDecoratorFunc(
sourceId, 'data', response.data, z, x, y); sourceId, 'data', response.data, z, x, y);
} }
} else { } else {
response.data = data; response.data = data;
@ -636,7 +634,7 @@ module.exports = {
request({ request({
url: req.url, url: req.url,
encoding: null, encoding: null,
gzip: true gzip: true,
}, (err, res, body) => { }, (err, res, body) => {
const parts = url.parse(req.url); const parts = url.parse(req.url);
const extension = path.extname(parts.pathname).toLowerCase(); const extension = path.extname(parts.pathname).toLowerCase();
@ -662,7 +660,7 @@ module.exports = {
callback(null, response); callback(null, response);
}); });
} }
} },
}); });
renderer.load(styleJSON); renderer.load(styleJSON);
createCallback(null, renderer); createCallback(null, renderer);
@ -671,9 +669,9 @@ module.exports = {
min: min, min: min,
max: max, max: max,
create: createRenderer.bind(null, ratio), create: createRenderer.bind(null, ratio),
destroy: renderer => { destroy: (renderer) => {
renderer.release(); renderer.release();
} },
}); });
}; };
@ -689,8 +687,8 @@ module.exports = {
if (styleJSON.sprite && !httpTester.test(styleJSON.sprite)) { if (styleJSON.sprite && !httpTester.test(styleJSON.sprite)) {
styleJSON.sprite = 'sprites://' + styleJSON.sprite = 'sprites://' +
styleJSON.sprite styleJSON.sprite
.replace('{style}', path.basename(styleFile, '.json')) .replace('{style}', path.basename(styleFile, '.json'))
.replace('{styleJsonFolder}', path.relative(options.paths.sprites, path.dirname(styleJSONPath))); .replace('{styleJsonFolder}', path.relative(options.paths.sprites, path.dirname(styleJSONPath)));
} }
if (styleJSON.glyphs && !httpTester.test(styleJSON.glyphs)) { if (styleJSON.glyphs && !httpTester.test(styleJSON.glyphs)) {
styleJSON.glyphs = `fonts://${styleJSON.glyphs}`; styleJSON.glyphs = `fonts://${styleJSON.glyphs}`;
@ -711,26 +709,28 @@ module.exports = {
const tileJSON = { const tileJSON = {
'tilejson': '2.0.0', 'tilejson': '2.0.0',
'name': styleJSON.name, 'name': styleJSON.name,
'id': id,
'attribution': '', 'attribution': '',
'minzoom': 0, 'minzoom': 0,
'maxzoom': 20, 'maxzoom': 20,
'bounds': [-180, -85.0511, 180, 85.0511], 'bounds': [-180, -85.0511, 180, 85.0511],
'format': 'png', 'format': 'png',
'type': 'baselayer' 'type': 'baselayer',
}; };
const attributionOverride = params.tilejson && params.tilejson.attribution; const attributionOverride = params.tilejson && params.tilejson.attribution;
Object.assign(tileJSON, params.tilejson || {}); Object.assign(tileJSON, params.tilejson || {});
tileJSON.tiles = params.domains || options.domains; tileJSON.tiles = params.domains || options.domains;
utils.fixTileJSONCenter(tileJSON); fixTileJSONCenter(tileJSON);
repo[id] = { const repoobj = {
tileJSON, tileJSON,
publicUrl, publicUrl,
map, map,
dataProjWGStoInternalWGS: null, dataProjWGStoInternalWGS: null,
lastModified: new Date().toUTCString(), lastModified: new Date().toUTCString(),
watermark: params.watermark || options.watermark watermark: params.watermark || options.watermark,
}; };
repo[id] = repoobj;
const queue = []; const queue = [];
for (const name of Object.keys(styleJSON.sources)) { for (const name of Object.keys(styleJSON.sources)) {
@ -764,18 +764,18 @@ module.exports = {
if (!mbtilesFileStats.isFile() || mbtilesFileStats.size === 0) { if (!mbtilesFileStats.isFile() || mbtilesFileStats.size === 0) {
throw Error(`Not valid MBTiles file: ${mbtilesFile}`); throw Error(`Not valid MBTiles file: ${mbtilesFile}`);
} }
map.sources[name] = new MBTiles(mbtilesFile, err => { map.sources[name] = new MBTiles(mbtilesFile + '?mode=ro', err => {
map.sources[name].getInfo((err, info) => { map.sources[name].getInfo((err, info) => {
if (err) { if (err) {
console.error(err); console.error(err);
return; return;
} }
if (!repo[id].dataProjWGStoInternalWGS && info.proj4) { if (!repoobj.dataProjWGStoInternalWGS && info.proj4) {
// how to do this for multiple sources with different proj4 defs? // how to do this for multiple sources with different proj4 defs?
const to3857 = proj4('EPSG:3857'); const to3857 = proj4('EPSG:3857');
const toDataProj = proj4(info.proj4); const toDataProj = proj4(info.proj4);
repo[id].dataProjWGStoInternalWGS = xy => to3857.inverse(toDataProj.forward(xy)); repoobj.dataProjWGStoInternalWGS = (xy) => to3857.inverse(toDataProj.forward(xy));
} }
const type = source.type; const type = source.type;
@ -783,7 +783,7 @@ module.exports = {
source.type = type; source.type = type;
source.tiles = [ source.tiles = [
// meta url which will be detected when requested // meta url which will be detected when requested
`mbtiles://${name}/{z}/{x}/{y}.${info.format || 'pbf'}` `mbtiles://${name}/{z}/{x}/{y}.${info.format || 'pbf'}`,
]; ];
delete source.scheme; delete source.scheme;
@ -793,10 +793,12 @@ module.exports = {
if (!attributionOverride && if (!attributionOverride &&
source.attribution && source.attribution.length > 0) { source.attribution && source.attribution.length > 0) {
if (tileJSON.attribution.length > 0) { if (!tileJSON.attribution.includes(source.attribution)) {
tileJSON.attribution += '; '; if (tileJSON.attribution.length > 0) {
tileJSON.attribution += ' | ';
}
tileJSON.attribution += source.attribution;
} }
tileJSON.attribution += source.attribution;
} }
resolve(); resolve();
}); });
@ -821,9 +823,9 @@ module.exports = {
return Promise.all([renderersReadyPromise]); return Promise.all([renderersReadyPromise]);
}, },
remove: (repo, id) => { remove: (repo, id) => {
let item = repo[id]; const item = repo[id];
if (item) { if (item) {
item.map.renderers.forEach(pool => { item.map.renderers.forEach((pool) => {
pool.close(); pool.close();
}); });
} }

View file

@ -1,13 +1,13 @@
'use strict'; 'use strict';
const path = require('path'); import path from 'path';
const fs = require('fs'); import fs from 'fs';
const clone = require('clone'); import clone from 'clone';
const express = require('express'); import express from 'express';
import {validate} from '@maplibre/maplibre-gl-style-spec'; import {validate} from '@maplibre/maplibre-gl-style-spec';
const utils = require('./utils'); import {getPublicUrl} from './utils.js';
const httpTester = /^(http(s)?:)?\/\//; const httpTester = /^(http(s)?:)?\/\//;
@ -24,10 +24,10 @@ const fixUrl = (req, url, publicUrl, opt_nokey) => {
query = `?${queryParams.join('&')}`; query = `?${queryParams.join('&')}`;
} }
return url.replace( return url.replace(
'local://', utils.getPublicUrl(publicUrl, req)) + query; 'local://', getPublicUrl(publicUrl, req)) + query;
}; };
module.exports = { export const serve_style = {
init: (options, repo) => { init: (options, repo) => {
const app = express().disable('x-powered-by'); const app = express().disable('x-powered-by');
@ -56,8 +56,8 @@ module.exports = {
if (!item || !item.spritePath) { if (!item || !item.spritePath) {
return res.sendStatus(404); return res.sendStatus(404);
} }
const scale = req.params.scale, const scale = req.params.scale;
format = req.params.format; const format = req.params.format;
const filename = `${item.spritePath + (scale || '')}.${format}`; const filename = `${item.spritePath + (scale || '')}.${format}`;
return fs.readFile(filename, (err, data) => { return fs.readFile(filename, (err, data) => {
if (err) { if (err) {
@ -87,7 +87,7 @@ module.exports = {
return false; return false;
} }
let validationErrors = validate(styleFileData); const validationErrors = validate(styleFileData);
if (validationErrors.length > 0) { if (validationErrors.length > 0) {
console.log(`The file "${params.style}" is not valid a valid style file:`); console.log(`The file "${params.style}" is not valid a valid style file:`);
for (const err of validationErrors) { for (const err of validationErrors) {
@ -95,7 +95,7 @@ module.exports = {
} }
return false; return false;
} }
let styleJSON = JSON.parse(styleFileData); const styleJSON = JSON.parse(styleFileData);
for (const name of Object.keys(styleJSON.sources)) { for (const name of Object.keys(styleJSON.sources)) {
const source = styleJSON.sources[name]; const source = styleJSON.sources[name];
@ -120,7 +120,7 @@ module.exports = {
} }
} }
for (let obj of styleJSON.layers) { for (const obj of styleJSON.layers) {
if (obj['type'] === 'symbol') { if (obj['type'] === 'symbol') {
const fonts = (obj['layout'] || {})['text-font']; const fonts = (obj['layout'] || {})['text-font'];
if (fonts && fonts.length) { if (fonts && fonts.length) {
@ -136,9 +136,9 @@ module.exports = {
if (styleJSON.sprite && !httpTester.test(styleJSON.sprite)) { if (styleJSON.sprite && !httpTester.test(styleJSON.sprite)) {
spritePath = path.join(options.paths.sprites, spritePath = path.join(options.paths.sprites,
styleJSON.sprite styleJSON.sprite
.replace('{style}', path.basename(styleFile, '.json')) .replace('{style}', path.basename(styleFile, '.json'))
.replace('{styleJsonFolder}', path.relative(options.paths.sprites, path.dirname(styleFile))) .replace('{styleJsonFolder}', path.relative(options.paths.sprites, path.dirname(styleFile))),
); );
styleJSON.sprite = `local://styles/${id}/sprite`; styleJSON.sprite = `local://styles/${id}/sprite`;
} }
@ -150,9 +150,9 @@ module.exports = {
styleJSON, styleJSON,
spritePath, spritePath,
publicUrl, publicUrl,
name: styleJSON.name name: styleJSON.name,
}; };
return true; return true;
} },
}; };

View file

@ -1,44 +1,45 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict'; 'use strict';
import os from 'os';
process.env.UV_THREADPOOL_SIZE = process.env.UV_THREADPOOL_SIZE =
Math.ceil(Math.max(4, require('os').cpus().length * 1.5)); Math.ceil(Math.max(4, os.cpus().length * 1.5));
const fs = require('fs'); import fs from 'fs';
const path = require('path'); import path from 'path';
const chokidar = require('chokidar'); import chokidar from 'chokidar';
const clone = require('clone'); import clone from 'clone';
const cors = require('cors'); import cors from 'cors';
const enableShutdown = require('http-shutdown'); import enableShutdown from 'http-shutdown';
const express = require('express'); import express from 'express';
const handlebars = require('handlebars'); import handlebars from 'handlebars';
const mercator = new (require('@mapbox/sphericalmercator'))(); import SphericalMercator from '@mapbox/sphericalmercator';
const morgan = require('morgan'); const mercator = new SphericalMercator();
import morgan from 'morgan';
import {serve_data} from './serve_data.js';
import {serve_style} from './serve_style.js';
import {serve_font} from './serve_font.js';
import {getTileUrls, getPublicUrl} from './utils.js';
const packageJson = require('../package'); import {fileURLToPath} from 'url';
const serve_font = require('./serve_font'); const __filename = fileURLToPath(import.meta.url);
const serve_style = require('./serve_style'); const __dirname = path.dirname(__filename);
const serve_data = require('./serve_data'); const packageJson = JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8'));
const utils = require('./utils');
let serve_rendered = null;
const isLight = packageJson.name.slice(-6) === '-light'; const isLight = packageJson.name.slice(-6) === '-light';
if (!isLight) { const serve_rendered = (await import(`${!isLight ? `./serve_rendered.js` : `./serve_light.js`}`)).serve_rendered;
// do not require `serve_rendered` in the light package
serve_rendered = require('./serve_rendered');
}
function start(opts) { export function server(opts) {
console.log('Starting server'); console.log('Starting server');
const app = express().disable('x-powered-by'), const app = express().disable('x-powered-by');
serving = { const serving = {
styles: {}, styles: {},
rendered: {}, rendered: {},
data: {}, data: {},
fonts: {} fonts: {},
}; };
app.enable('trust proxy'); app.enable('trust proxy');
@ -46,8 +47,8 @@ function start(opts) {
const defaultLogFormat = process.env.NODE_ENV === 'production' ? 'tiny' : 'dev'; const defaultLogFormat = process.env.NODE_ENV === 'production' ? 'tiny' : 'dev';
const logFormat = opts.logFormat || defaultLogFormat; const logFormat = opts.logFormat || defaultLogFormat;
app.use(morgan(logFormat, { app.use(morgan(logFormat, {
stream: opts.logFile ? fs.createWriteStream(opts.logFile, { flags: 'a' }) : process.stdout, stream: opts.logFile ? fs.createWriteStream(opts.logFile, {flags: 'a'}) : process.stdout,
skip: (req, res) => opts.silent && (res.statusCode === 200 || res.statusCode === 304) skip: (req, res) => opts.silent && (res.statusCode === 200 || res.statusCode === 304),
})); }));
} }
@ -56,7 +57,7 @@ function start(opts) {
if (opts.configPath) { if (opts.configPath) {
configPath = path.resolve(opts.configPath); configPath = path.resolve(opts.configPath);
try { try {
config = clone(require(configPath)); config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
} catch (e) { } catch (e) {
console.log('ERROR: Config file not found or invalid!'); console.log('ERROR: Config file not found or invalid!');
console.log(' See README.md for instructions and sample data.'); console.log(' See README.md for instructions and sample data.');
@ -81,7 +82,7 @@ function start(opts) {
const startupPromises = []; const startupPromises = [];
const checkPath = type => { const checkPath = (type) => {
if (!fs.existsSync(paths[type])) { if (!fs.existsSync(paths[type])) {
console.error(`The specified path for "${type}" does not exist (${paths[type]}).`); console.error(`The specified path for "${type}" does not exist (${paths[type]}).`);
process.exit(1); process.exit(1);
@ -106,65 +107,65 @@ function start(opts) {
app.use('/data/', serve_data.init(options, serving.data)); app.use('/data/', serve_data.init(options, serving.data));
app.use('/styles/', serve_style.init(options, serving.styles)); app.use('/styles/', serve_style.init(options, serving.styles));
if (serve_rendered) { if (!isLight) {
startupPromises.push( startupPromises.push(
serve_rendered.init(options, serving.rendered) serve_rendered.init(options, serving.rendered)
.then(sub => { .then((sub) => {
app.use('/styles/', sub); app.use('/styles/', sub);
}) }),
); );
} }
let addStyle = (id, item, allowMoreData, reportFonts) => { const addStyle = (id, item, allowMoreData, reportFonts) => {
let success = true; let success = true;
if (item.serve_data !== false) { if (item.serve_data !== false) {
success = serve_style.add(options, serving.styles, item, id, opts.publicUrl, success = serve_style.add(options, serving.styles, item, id, opts.publicUrl,
(mbtiles, fromData) => { (mbtiles, fromData) => {
let dataItemId; let dataItemId;
for (const id of Object.keys(data)) { for (const id of Object.keys(data)) {
if (fromData) { if (fromData) {
if (id === mbtiles) { if (id === mbtiles) {
dataItemId = id; dataItemId = id;
} }
} else { } else {
if (data[id].mbtiles === mbtiles) { if (data[id].mbtiles === mbtiles) {
dataItemId = id; dataItemId = id;
}
} }
} }
} if (dataItemId) { // mbtiles exist in the data config
if (dataItemId) { // mbtiles exist in the data config return dataItemId;
return dataItemId;
} else {
if (fromData || !allowMoreData) {
console.log(`ERROR: style "${item.style}" using unknown mbtiles "${mbtiles}"! Skipping...`);
return undefined;
} else { } else {
let id = mbtiles.substr(0, mbtiles.lastIndexOf('.')) || mbtiles; if (fromData || !allowMoreData) {
while (data[id]) id += '_'; console.log(`ERROR: style "${item.style}" using unknown mbtiles "${mbtiles}"! Skipping...`);
data[id] = { return undefined;
'mbtiles': mbtiles } else {
}; let id = mbtiles.substr(0, mbtiles.lastIndexOf('.')) || mbtiles;
return id; while (data[id]) id += '_';
data[id] = {
'mbtiles': mbtiles,
};
return id;
}
} }
} }, (font) => {
}, font => { if (reportFonts) {
if (reportFonts) { serving.fonts[font] = true;
serving.fonts[font] = true; }
} });
});
} }
if (success && item.serve_rendered !== false) { if (success && item.serve_rendered !== false) {
if (serve_rendered) { if (!isLight) {
startupPromises.push(serve_rendered.add(options, serving.rendered, item, id, opts.publicUrl, startupPromises.push(serve_rendered.add(options, serving.rendered, item, id, opts.publicUrl,
mbtiles => { (mbtiles) => {
let mbtilesFile; let mbtilesFile;
for (const id of Object.keys(data)) { for (const id of Object.keys(data)) {
if (id === mbtiles) { if (id === mbtiles) {
mbtilesFile = data[id].mbtiles; mbtilesFile = data[id].mbtiles;
}
} }
} return mbtilesFile;
return mbtilesFile; },
}
)); ));
} else { } else {
item.serve_rendered = false; item.serve_rendered = false;
@ -183,9 +184,9 @@ function start(opts) {
} }
startupPromises.push( startupPromises.push(
serve_font(options, serving.fonts).then(sub => { serve_font(options, serving.fonts).then((sub) => {
app.use('/', sub); app.use('/', sub);
}) }),
); );
for (const id of Object.keys(data)) { for (const id of Object.keys(data)) {
@ -196,7 +197,7 @@ function start(opts) {
} }
startupPromises.push( startupPromises.push(
serve_data.add(options, serving.data, item, id, opts.publicUrl) serve_data.add(options, serving.data, item, id, opts.publicUrl),
); );
} }
@ -208,9 +209,9 @@ function start(opts) {
for (const file of files) { for (const file of files) {
if (file.isFile() && if (file.isFile() &&
path.extname(file.name).toLowerCase() == '.json') { path.extname(file.name).toLowerCase() == '.json') {
let id = path.basename(file.name, '.json'); const id = path.basename(file.name, '.json');
let item = { const item = {
style: file.name style: file.name,
}; };
addStyle(id, item, false, false); addStyle(id, item, false, false);
} }
@ -218,27 +219,27 @@ function start(opts) {
}); });
const watcher = chokidar.watch(path.join(options.paths.styles, '*.json'), const watcher = chokidar.watch(path.join(options.paths.styles, '*.json'),
{ {
}); });
watcher.on('all', watcher.on('all',
(eventType, filename) => { (eventType, filename) => {
if (filename) { if (filename) {
let id = path.basename(filename, '.json'); const id = path.basename(filename, '.json');
console.log(`Style "${id}" changed, updating...`); console.log(`Style "${id}" changed, updating...`);
serve_style.remove(serving.styles, id); serve_style.remove(serving.styles, id);
if (serve_rendered) { if (!isLight) {
serve_rendered.remove(serving.rendered, id); serve_rendered.remove(serving.rendered, id);
} }
if (eventType == "add" || eventType == "change") { if (eventType == 'add' || eventType == 'change') {
let item = { const item = {
style: filename style: filename,
}; };
addStyle(id, item, false, false); addStyle(id, item, false, false);
}
} }
} });
});
} }
app.get('/styles.json', (req, res, next) => { app.get('/styles.json', (req, res, next) => {
@ -250,7 +251,7 @@ function start(opts) {
version: styleJSON.version, version: styleJSON.version,
name: styleJSON.name, name: styleJSON.name,
id: id, id: id,
url: `${utils.getPublicUrl(opts.publicUrl, req)}styles/${id}/style.json${query}` url: `${getPublicUrl(opts.publicUrl, req)}styles/${id}/style.json${query}`,
}); });
} }
res.send(result); res.send(result);
@ -265,8 +266,8 @@ function start(opts) {
} else { } else {
path = `${type}/${id}`; path = `${type}/${id}`;
} }
info.tiles = utils.getTileUrls(req, info.tiles, path, info.format, opts.publicUrl, { info.tiles = getTileUrls(req, info.tiles, path, info.format, opts.publicUrl, {
'pbf': options.pbfAlias 'pbf': options.pbfAlias,
}); });
arr.push(info); arr.push(info);
} }
@ -283,7 +284,7 @@ function start(opts) {
res.send(addTileJSONs(addTileJSONs([], req, 'rendered'), req, 'data')); res.send(addTileJSONs(addTileJSONs([], req, 'rendered'), req, 'data'));
}); });
//------------------------------------ // ------------------------------------
// serve web presentations // serve web presentations
app.use('/', express.static(path.join(__dirname, '../public/resources'))); app.use('/', express.static(path.join(__dirname, '../public/resources')));
@ -329,7 +330,7 @@ function start(opts) {
})); }));
}; };
serveTemplate('/$', 'index', req => { serveTemplate('/$', 'index', (req) => {
const styles = clone(serving.styles || {}); const styles = clone(serving.styles || {});
for (const id of Object.keys(styles)) { for (const id of Object.keys(styles)) {
const style = styles[id]; const style = styles[id];
@ -345,9 +346,9 @@ function start(opts) {
style.thumbnail = `${center[2]}/${Math.floor(centerPx[0] / 256)}/${Math.floor(centerPx[1] / 256)}.png`; style.thumbnail = `${center[2]}/${Math.floor(centerPx[0] / 256)}/${Math.floor(centerPx[1] / 256)}.png`;
} }
style.xyz_link = utils.getTileUrls( style.xyz_link = getTileUrls(
req, style.serving_rendered.tileJSON.tiles, req, style.serving_rendered.tileJSON.tiles,
`styles/${id}`, style.serving_rendered.tileJSON.format, opts.publicUrl)[0]; `styles/${id}`, style.serving_rendered.tileJSON.format, opts.publicUrl)[0];
} }
} }
const data = clone(serving.data || {}); const data = clone(serving.data || {});
@ -365,10 +366,10 @@ function start(opts) {
data_.thumbnail = `${center[2]}/${Math.floor(centerPx[0] / 256)}/${Math.floor(centerPx[1] / 256)}.${data_.tileJSON.format}`; data_.thumbnail = `${center[2]}/${Math.floor(centerPx[0] / 256)}/${Math.floor(centerPx[1] / 256)}.${data_.tileJSON.format}`;
} }
data_.xyz_link = utils.getTileUrls( data_.xyz_link = getTileUrls(
req, tilejson.tiles, `data/${id}`, tilejson.format, opts.publicUrl, { req, tilejson.tiles, `data/${id}`, tilejson.format, opts.publicUrl, {
'pbf': options.pbfAlias 'pbf': options.pbfAlias,
})[0]; })[0];
} }
if (data_.filesize) { if (data_.filesize) {
let suffix = 'kB'; let suffix = 'kB';
@ -386,11 +387,11 @@ function start(opts) {
} }
return { return {
styles: Object.keys(styles).length ? styles : null, styles: Object.keys(styles).length ? styles : null,
data: Object.keys(data).length ? data : null data: Object.keys(data).length ? data : null,
}; };
}); });
serveTemplate('/styles/:id/$', 'viewer', req => { serveTemplate('/styles/:id/$', 'viewer', (req) => {
const id = req.params.id; const id = req.params.id;
const style = clone(((serving.styles || {})[id] || {}).styleJSON); const style = clone(((serving.styles || {})[id] || {}).styleJSON);
if (!style) { if (!style) {
@ -408,13 +409,13 @@ function start(opts) {
return res.redirect(301, '/styles/' + req.params.id + '/'); return res.redirect(301, '/styles/' + req.params.id + '/');
}); });
*/ */
serveTemplate('/styles/:id/wmts.xml', 'wmts', req => { serveTemplate('/styles/:id/wmts.xml', 'wmts', (req) => {
const id = req.params.id; const id = req.params.id;
const wmts = clone((serving.styles || {})[id]); const wmts = clone((serving.styles || {})[id]);
if (!wmts) { if (!wmts) {
return null; return null;
} }
if (wmts.hasOwnProperty("serve_rendered") && !wmts.serve_rendered) { if (wmts.hasOwnProperty('serve_rendered') && !wmts.serve_rendered) {
return null; return null;
} }
wmts.id = id; wmts.id = id;
@ -423,7 +424,7 @@ function start(opts) {
return wmts; return wmts;
}); });
serveTemplate('/data/:id/$', 'data', req => { serveTemplate('/data/:id/$', 'data', (req) => {
const id = req.params.id; const id = req.params.id;
const data = clone(serving.data[id]); const data = clone(serving.data[id]);
if (!data) { if (!data) {
@ -447,7 +448,7 @@ function start(opts) {
} }
}); });
const server = app.listen(process.env.PORT || opts.port, process.env.BIND || opts.bind, function () { const server = app.listen(process.env.PORT || opts.port, process.env.BIND || opts.bind, function() {
let address = this.address().address; let address = this.address().address;
if (address.indexOf('::') === 0) { if (address.indexOf('::') === 0) {
address = `[${address}]`; // literal IPv6 address address = `[${address}]`; // literal IPv6 address
@ -461,14 +462,14 @@ function start(opts) {
return { return {
app: app, app: app,
server: server, server: server,
startupPromise: startupPromise startupPromise: startupPromise,
}; };
} }
module.exports = opts => { export const exports = (opts) => {
const running = start(opts); const running = start(opts);
running.startupPromise.catch(err => { running.startupPromise.catch((err) => {
console.error(err.message); console.error(err.message);
process.exit(1); process.exit(1);
}); });

View file

@ -1,16 +1,15 @@
'use strict'; 'use strict';
const path = require('path'); import path from 'path';
const fs = require('fs'); import fs from 'fs';
const clone = require('clone'); import clone from 'clone';
const glyphCompose = require('@mapbox/glyph-pbf-composite'); import glyphCompose from '@mapbox/glyph-pbf-composite';
module.exports.getPublicUrl = (publicUrl, req) => publicUrl || `${req.protocol}://${req.headers.host}/`; export const getPublicUrl = (publicUrl, req) => publicUrl || `${req.protocol}://${req.headers.host}/`;
module.exports.getTileUrls = (req, domains, path, format, publicUrl, aliases) => {
export const getTileUrls = (req, domains, path, format, publicUrl, aliases) => {
if (domains) { if (domains) {
if (domains.constructor === String && domains.length > 0) { if (domains.constructor === String && domains.length > 0) {
domains = domains.split(','); domains = domains.split(',');
@ -37,7 +36,6 @@ module.exports.getTileUrls = (req, domains, path, format, publicUrl, aliases) =>
domains = [req.headers.host]; domains = [req.headers.host];
} }
const key = req.query.key;
const queryParams = []; const queryParams = [];
if (req.query.key) { if (req.query.key) {
queryParams.push(`key=${encodeURIComponent(req.query.key)}`); queryParams.push(`key=${encodeURIComponent(req.query.key)}`);
@ -57,13 +55,13 @@ module.exports.getTileUrls = (req, domains, path, format, publicUrl, aliases) =>
uris.push(`${req.protocol}://${domain}/${path}/{z}/{x}/{y}.${format}${query}`); uris.push(`${req.protocol}://${domain}/${path}/{z}/{x}/{y}.${format}${query}`);
} }
} else { } else {
uris.push(`${publicUrl}${path}/{z}/{x}/{y}.${format}${query}`) uris.push(`${publicUrl}${path}/{z}/{x}/{y}.${format}${query}`);
} }
return uris; return uris;
}; };
module.exports.fixTileJSONCenter = tileJSON => { export const fixTileJSONCenter = (tileJSON) => {
if (tileJSON.bounds && !tileJSON.center) { if (tileJSON.bounds && !tileJSON.center) {
const fitWidth = 1024; const fitWidth = 1024;
const tiles = fitWidth / 256; const tiles = fitWidth / 256;
@ -71,9 +69,9 @@ module.exports.fixTileJSONCenter = tileJSON => {
(tileJSON.bounds[0] + tileJSON.bounds[2]) / 2, (tileJSON.bounds[0] + tileJSON.bounds[2]) / 2,
(tileJSON.bounds[1] + tileJSON.bounds[3]) / 2, (tileJSON.bounds[1] + tileJSON.bounds[3]) / 2,
Math.round( Math.round(
-Math.log((tileJSON.bounds[2] - tileJSON.bounds[0]) / 360 / tiles) / -Math.log((tileJSON.bounds[2] - tileJSON.bounds[0]) / 360 / tiles) /
Math.LN2 Math.LN2,
) ),
]; ];
} }
}; };
@ -118,14 +116,14 @@ const getFontPbf = (allowedFonts, fontPath, name, range, fallbacks) => new Promi
} }
}); });
module.exports.getFontsPbf = (allowedFonts, fontPath, names, range, fallbacks) => { export const getFontsPbf = (allowedFonts, fontPath, names, range, fallbacks) => {
const fonts = names.split(','); const fonts = names.split(',');
const queue = []; const queue = [];
for (const font of fonts) { for (const font of fonts) {
queue.push( queue.push(
getFontPbf(allowedFonts, fontPath, font, range, clone(allowedFonts || fallbacks)) getFontPbf(allowedFonts, fontPath, font, range, clone(allowedFonts || fallbacks)),
); );
} }
return Promise.all(queue).then(values => glyphCompose.combine(values)); return Promise.all(queue).then((values) => glyphCompose.combine(values));
}; };