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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,16 +1,15 @@
'use strict';
const path = require('path');
const fs = require('fs');
import path from 'path';
import fs from 'fs';
const clone = require('clone');
const glyphCompose = require('@mapbox/glyph-pbf-composite');
import clone from 'clone';
import glyphCompose from '@mapbox/glyph-pbf-composite';
module.exports.getPublicUrl = (publicUrl, req) => publicUrl || `${req.protocol}://${req.headers.host}/`;
module.exports.getTileUrls = (req, domains, path, format, publicUrl, aliases) => {
export const getPublicUrl = (publicUrl, req) => publicUrl || `${req.protocol}://${req.headers.host}/`;
export const getTileUrls = (req, domains, path, format, publicUrl, aliases) => {
if (domains) {
if (domains.constructor === String && domains.length > 0) {
domains = domains.split(',');
@ -37,7 +36,6 @@ module.exports.getTileUrls = (req, domains, path, format, publicUrl, aliases) =>
domains = [req.headers.host];
}
const key = req.query.key;
const queryParams = [];
if (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}`);
}
} else {
uris.push(`${publicUrl}${path}/{z}/{x}/{y}.${format}${query}`)
uris.push(`${publicUrl}${path}/{z}/{x}/{y}.${format}${query}`);
}
return uris;
};
module.exports.fixTileJSONCenter = tileJSON => {
export const fixTileJSONCenter = (tileJSON) => {
if (tileJSON.bounds && !tileJSON.center) {
const fitWidth = 1024;
const tiles = fitWidth / 256;
@ -71,9 +69,9 @@ module.exports.fixTileJSONCenter = tileJSON => {
(tileJSON.bounds[0] + tileJSON.bounds[2]) / 2,
(tileJSON.bounds[1] + tileJSON.bounds[3]) / 2,
Math.round(
-Math.log((tileJSON.bounds[2] - tileJSON.bounds[0]) / 360 / tiles) /
Math.LN2
)
-Math.log((tileJSON.bounds[2] - tileJSON.bounds[0]) / 360 / tiles) /
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 queue = [];
for (const font of fonts) {
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));
};