tileserver-gl 4.1.1 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ import * as http from 'http';
2
+ var options = {
3
+ timeout: 2000,
4
+ };
5
+ var url = 'http://localhost:8080/health';
6
+ var request = http.request(url, options, (res) => {
7
+ console.log(`STATUS: ${res.statusCode}`);
8
+ if (res.statusCode == 200) {
9
+ process.exit(0);
10
+ } else {
11
+ process.exit(1);
12
+ }
13
+ });
14
+ request.on('error', function (err) {
15
+ console.log('ERROR');
16
+ process.exit(1);
17
+ });
18
+ request.end();
package/src/main.js CHANGED
@@ -4,73 +4,52 @@
4
4
 
5
5
  import fs from 'node:fs';
6
6
  import path from 'path';
7
- import {fileURLToPath} from 'url';
7
+ import { fileURLToPath } from 'url';
8
8
  import request from 'request';
9
- import {server} from './server.js';
9
+ import { server } from './server.js';
10
10
 
11
11
  import MBTiles from '@mapbox/mbtiles';
12
12
 
13
13
  const __filename = fileURLToPath(import.meta.url);
14
14
  const __dirname = path.dirname(__filename);
15
- const packageJson = JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8'));
15
+ const packageJson = JSON.parse(
16
+ fs.readFileSync(__dirname + '/../package.json', 'utf8'),
17
+ );
16
18
 
17
19
  const args = process.argv;
18
20
  if (args.length >= 3 && args[2][0] !== '-') {
19
21
  args.splice(2, 0, '--mbtiles');
20
22
  }
21
23
 
22
- import {program} from 'commander';
24
+ import { program } from 'commander';
23
25
  program
24
26
  .description('tileserver-gl startup options')
25
27
  .usage('tileserver-gl [mbtiles] [options]')
26
28
  .option(
27
29
  '--mbtiles <file>',
28
30
  'MBTiles file (uses demo configuration);\n' +
29
- '\t ignored if the configuration file is also specified'
31
+ '\t ignored if the configuration file is also specified',
30
32
  )
31
33
  .option(
32
34
  '-c, --config <file>',
33
35
  'Configuration file [config.json]',
34
- 'config.json'
35
- )
36
- .option(
37
- '-b, --bind <address>',
38
- 'Bind address'
39
- )
40
- .option(
41
- '-p, --port <port>',
42
- 'Port [8080]',
43
- 8080,
44
- parseInt
45
- )
46
- .option(
47
- '-C|--no-cors',
48
- 'Disable Cross-origin resource sharing headers'
36
+ 'config.json',
49
37
  )
38
+ .option('-b, --bind <address>', 'Bind address')
39
+ .option('-p, --port <port>', 'Port [8080]', 8080, parseInt)
40
+ .option('-C|--no-cors', 'Disable Cross-origin resource sharing headers')
50
41
  .option(
51
42
  '-u|--public_url <url>',
52
- 'Enable exposing the server on subpaths, not necessarily the root of the domain'
53
- )
54
- .option(
55
- '-V, --verbose',
56
- 'More verbose output'
57
- )
58
- .option(
59
- '-s, --silent',
60
- 'Less verbose output'
61
- )
62
- .option(
63
- '-l|--log_file <file>',
64
- 'output log file (defaults to standard out)'
43
+ 'Enable exposing the server on subpaths, not necessarily the root of the domain',
65
44
  )
45
+ .option('-V, --verbose', 'More verbose output')
46
+ .option('-s, --silent', 'Less verbose output')
47
+ .option('-l|--log_file <file>', 'output log file (defaults to standard out)')
66
48
  .option(
67
49
  '-f|--log_format <format>',
68
- 'define the log format: https://github.com/expressjs/morgan#morganformat-options'
69
- )
70
- .version(
71
- packageJson.version,
72
- '-v, --version'
50
+ 'define the log format: https://github.com/expressjs/morgan#morganformat-options',
73
51
  )
52
+ .version(packageJson.version, '-v, --version');
74
53
  program.parse(process.argv);
75
54
  const opts = program.opts();
76
55
 
@@ -91,14 +70,16 @@ const startServer = (configPath, config) => {
91
70
  silent: opts.silent,
92
71
  logFile: opts.log_file,
93
72
  logFormat: opts.log_format,
94
- publicUrl: publicUrl
73
+ publicUrl: publicUrl,
95
74
  });
96
75
  };
97
76
 
98
77
  const startWithMBTiles = (mbtilesFile) => {
99
78
  console.log(`[INFO] Automatically creating config file for ${mbtilesFile}`);
100
79
  console.log(`[INFO] Only a basic preview style will be used.`);
101
- console.log(`[INFO] See documentation to learn how to create config.json file.`);
80
+ console.log(
81
+ `[INFO] See documentation to learn how to create config.json file.`,
82
+ );
102
83
 
103
84
  mbtilesFile = path.resolve(process.cwd(), mbtilesFile);
104
85
 
@@ -110,60 +91,70 @@ const startWithMBTiles = (mbtilesFile) => {
110
91
  const instance = new MBTiles(mbtilesFile + '?mode=ro', (err) => {
111
92
  if (err) {
112
93
  console.log('ERROR: Unable to open MBTiles.');
113
- console.log(` Make sure ${path.basename(mbtilesFile)} is valid MBTiles.`);
94
+ console.log(`Make sure ${path.basename(mbtilesFile)} is valid MBTiles.`);
114
95
  process.exit(1);
115
96
  }
116
97
 
117
98
  instance.getInfo((err, info) => {
118
99
  if (err || !info) {
119
100
  console.log('ERROR: Metadata missing in the MBTiles.');
120
- console.log(` Make sure ${path.basename(mbtilesFile)} is valid MBTiles.`);
101
+ console.log(
102
+ `Make sure ${path.basename(mbtilesFile)} is valid MBTiles.`,
103
+ );
121
104
  process.exit(1);
122
105
  }
123
106
  const bounds = info.bounds;
124
107
 
125
- const styleDir = path.resolve(__dirname, '../node_modules/tileserver-gl-styles/');
108
+ const styleDir = path.resolve(
109
+ __dirname,
110
+ '../node_modules/tileserver-gl-styles/',
111
+ );
126
112
 
127
113
  const config = {
128
- 'options': {
129
- 'paths': {
130
- 'root': styleDir,
131
- 'fonts': 'fonts',
132
- 'styles': 'styles',
133
- 'mbtiles': path.dirname(mbtilesFile)
134
- }
114
+ options: {
115
+ paths: {
116
+ root: styleDir,
117
+ fonts: 'fonts',
118
+ styles: 'styles',
119
+ mbtiles: path.dirname(mbtilesFile),
120
+ },
135
121
  },
136
- 'styles': {},
137
- 'data': {}
122
+ styles: {},
123
+ data: {},
138
124
  };
139
125
 
140
- if (info.format === 'pbf' &&
141
- info.name.toLowerCase().indexOf('openmaptiles') > -1) {
126
+ if (
127
+ info.format === 'pbf' &&
128
+ info.name.toLowerCase().indexOf('openmaptiles') > -1
129
+ ) {
142
130
  config['data'][`v3`] = {
143
- 'mbtiles': path.basename(mbtilesFile)
131
+ mbtiles: path.basename(mbtilesFile),
144
132
  };
145
133
 
146
-
147
134
  const styles = fs.readdirSync(path.resolve(styleDir, 'styles'));
148
135
  for (const styleName of styles) {
149
136
  const styleFileRel = styleName + '/style.json';
150
137
  const styleFile = path.resolve(styleDir, 'styles', styleFileRel);
151
138
  if (fs.existsSync(styleFile)) {
152
139
  config['styles'][styleName] = {
153
- 'style': styleFileRel,
154
- 'tilejson': {
155
- 'bounds': bounds
156
- }
140
+ style: styleFileRel,
141
+ tilejson: {
142
+ bounds: bounds,
143
+ },
157
144
  };
158
145
  }
159
146
  }
160
147
  } else {
161
- console.log(`WARN: MBTiles not in "openmaptiles" format. Serving raw data only...`);
162
- config['data'][(info.id || 'mbtiles')
148
+ console.log(
149
+ `WARN: MBTiles not in "openmaptiles" format. Serving raw data only...`,
150
+ );
151
+ config['data'][
152
+ (info.id || 'mbtiles')
163
153
  .replace(/\//g, '_')
164
154
  .replace(/:/g, '_')
165
- .replace(/\?/g, '_')] = {
166
- 'mbtiles': path.basename(mbtilesFile)
155
+ .replace(/\?/g, '_')
156
+ ] = {
157
+ mbtiles: path.basename(mbtilesFile),
167
158
  };
168
159
  }
169
160
 
@@ -197,7 +188,8 @@ fs.stat(path.resolve(opts.config), (err, stats) => {
197
188
  console.log(`No MBTiles specified, using ${mbtiles}`);
198
189
  return startWithMBTiles(mbtiles);
199
190
  } else {
200
- const url = 'https://github.com/maptiler/tileserver-gl/releases/download/v1.3.0/zurich_switzerland.mbtiles';
191
+ const url =
192
+ 'https://github.com/maptiler/tileserver-gl/releases/download/v1.3.0/zurich_switzerland.mbtiles';
201
193
  const filename = 'zurich_switzerland.mbtiles';
202
194
  const stream = fs.createWriteStream(filename);
203
195
  console.log(`No MBTiles found`);
package/src/serve_data.js CHANGED
@@ -10,97 +10,108 @@ import MBTiles from '@mapbox/mbtiles';
10
10
  import Pbf from 'pbf';
11
11
  import VectorTile from '@mapbox/vector-tile';
12
12
 
13
- import {getTileUrls, fixTileJSONCenter} from './utils.js';
13
+ import { getTileUrls, fixTileJSONCenter } from './utils.js';
14
14
 
15
15
  export const serve_data = {
16
16
  init: (options, repo) => {
17
17
  const app = express().disable('x-powered-by');
18
18
 
19
- app.get('/:id/:z(\\d+)/:x(\\d+)/:y(\\d+).:format([\\w.]+)', (req, res, next) => {
20
- const item = repo[req.params.id];
21
- if (!item) {
22
- return res.sendStatus(404);
23
- }
24
- const tileJSONFormat = item.tileJSON.format;
25
- const z = req.params.z | 0;
26
- const x = req.params.x | 0;
27
- const y = req.params.y | 0;
28
- let format = req.params.format;
29
- if (format === options.pbfAlias) {
30
- format = 'pbf';
31
- }
32
- if (format !== tileJSONFormat &&
33
- !(format === 'geojson' && tileJSONFormat === 'pbf')) {
34
- return res.status(404).send('Invalid format');
35
- }
36
- if (z < item.tileJSON.minzoom || 0 || x < 0 || y < 0 ||
19
+ app.get(
20
+ '/:id/:z(\\d+)/:x(\\d+)/:y(\\d+).:format([\\w.]+)',
21
+ (req, res, next) => {
22
+ const item = repo[req.params.id];
23
+ if (!item) {
24
+ return res.sendStatus(404);
25
+ }
26
+ const tileJSONFormat = item.tileJSON.format;
27
+ const z = req.params.z | 0;
28
+ const x = req.params.x | 0;
29
+ const y = req.params.y | 0;
30
+ let format = req.params.format;
31
+ if (format === options.pbfAlias) {
32
+ format = 'pbf';
33
+ }
34
+ if (
35
+ format !== tileJSONFormat &&
36
+ !(format === 'geojson' && tileJSONFormat === 'pbf')
37
+ ) {
38
+ return res.status(404).send('Invalid format');
39
+ }
40
+ if (
41
+ z < item.tileJSON.minzoom ||
42
+ 0 ||
43
+ x < 0 ||
44
+ y < 0 ||
37
45
  z > item.tileJSON.maxzoom ||
38
- x >= Math.pow(2, z) || y >= Math.pow(2, z)) {
39
- return res.status(404).send('Out of bounds');
40
- }
41
- item.source.getTile(z, x, y, (err, data, headers) => {
42
- let isGzipped;
43
- if (err) {
44
- if (/does not exist/.test(err.message)) {
45
- return res.status(204).send();
46
- } else {
47
- return res.status(500).send(err.message);
48
- }
49
- } else {
50
- if (data == null) {
51
- return res.status(404).send('Not found');
46
+ x >= Math.pow(2, z) ||
47
+ y >= Math.pow(2, z)
48
+ ) {
49
+ return res.status(404).send('Out of bounds');
50
+ }
51
+ item.source.getTile(z, x, y, (err, data, headers) => {
52
+ let isGzipped;
53
+ if (err) {
54
+ if (/does not exist/.test(err.message)) {
55
+ return res.status(204).send();
56
+ } else {
57
+ return res.status(500).send(err.message);
58
+ }
52
59
  } else {
53
- if (tileJSONFormat === 'pbf') {
54
- isGzipped = data.slice(0, 2).indexOf(
55
- Buffer.from([0x1f, 0x8b])) === 0;
56
- if (options.dataDecoratorFunc) {
60
+ if (data == null) {
61
+ return res.status(404).send('Not found');
62
+ } else {
63
+ if (tileJSONFormat === 'pbf') {
64
+ isGzipped =
65
+ data.slice(0, 2).indexOf(Buffer.from([0x1f, 0x8b])) === 0;
66
+ if (options.dataDecoratorFunc) {
67
+ if (isGzipped) {
68
+ data = zlib.unzipSync(data);
69
+ isGzipped = false;
70
+ }
71
+ data = options.dataDecoratorFunc(id, 'data', data, z, x, y);
72
+ }
73
+ }
74
+ if (format === 'pbf') {
75
+ headers['Content-Type'] = 'application/x-protobuf';
76
+ } else if (format === 'geojson') {
77
+ headers['Content-Type'] = 'application/json';
78
+
57
79
  if (isGzipped) {
58
80
  data = zlib.unzipSync(data);
59
81
  isGzipped = false;
60
82
  }
61
- data = options.dataDecoratorFunc(id, 'data', data, z, x, y);
62
- }
63
- }
64
- if (format === 'pbf') {
65
- headers['Content-Type'] = 'application/x-protobuf';
66
- } else if (format === 'geojson') {
67
- headers['Content-Type'] = 'application/json';
68
-
69
- if (isGzipped) {
70
- data = zlib.unzipSync(data);
71
- isGzipped = false;
72
- }
73
83
 
74
- const tile = new VectorTile(new Pbf(data));
75
- const geojson = {
76
- 'type': 'FeatureCollection',
77
- 'features': []
78
- };
79
- for (const layerName in tile.layers) {
80
- const layer = tile.layers[layerName];
81
- for (let i = 0; i < layer.length; i++) {
82
- const feature = layer.feature(i);
83
- const featureGeoJSON = feature.toGeoJSON(x, y, z);
84
- featureGeoJSON.properties.layer = layerName;
85
- geojson.features.push(featureGeoJSON);
84
+ const tile = new VectorTile(new Pbf(data));
85
+ const geojson = {
86
+ type: 'FeatureCollection',
87
+ features: [],
88
+ };
89
+ for (const layerName in tile.layers) {
90
+ const layer = tile.layers[layerName];
91
+ for (let i = 0; i < layer.length; i++) {
92
+ const feature = layer.feature(i);
93
+ const featureGeoJSON = feature.toGeoJSON(x, y, z);
94
+ featureGeoJSON.properties.layer = layerName;
95
+ geojson.features.push(featureGeoJSON);
96
+ }
86
97
  }
98
+ data = JSON.stringify(geojson);
87
99
  }
88
- data = JSON.stringify(geojson);
89
- }
90
- delete headers['ETag']; // do not trust the tile ETag -- regenerate
91
- headers['Content-Encoding'] = 'gzip';
92
- res.set(headers);
100
+ delete headers['ETag']; // do not trust the tile ETag -- regenerate
101
+ headers['Content-Encoding'] = 'gzip';
102
+ res.set(headers);
93
103
 
94
- if (!isGzipped) {
95
- data = zlib.gzipSync(data);
96
- isGzipped = true;
97
- }
104
+ if (!isGzipped) {
105
+ data = zlib.gzipSync(data);
106
+ isGzipped = true;
107
+ }
98
108
 
99
- return res.status(200).send(data);
109
+ return res.status(200).send(data);
110
+ }
100
111
  }
101
- }
102
- });
103
- });
112
+ });
113
+ },
114
+ );
104
115
 
105
116
  app.get('/:id.json', (req, res, next) => {
106
117
  const item = repo[req.params.id];
@@ -108,10 +119,16 @@ export const serve_data = {
108
119
  return res.sendStatus(404);
109
120
  }
110
121
  const info = clone(item.tileJSON);
111
- info.tiles = getTileUrls(req, info.tiles,
112
- `data/${req.params.id}`, info.format, item.publicUrl, {
113
- 'pbf': options.pbfAlias
114
- });
122
+ info.tiles = getTileUrls(
123
+ req,
124
+ info.tiles,
125
+ `data/${req.params.id}`,
126
+ info.format,
127
+ item.publicUrl,
128
+ {
129
+ pbf: options.pbfAlias,
130
+ },
131
+ );
115
132
  return res.send(info);
116
133
  });
117
134
 
@@ -120,7 +137,7 @@ export const serve_data = {
120
137
  add: (options, repo, params, id, publicUrl) => {
121
138
  const mbtilesFile = path.resolve(options.paths.mbtiles, params.mbtiles);
122
139
  let tileJSON = {
123
- 'tiles': params.domains || options.domains
140
+ tiles: params.domains || options.domains,
124
141
  };
125
142
 
126
143
  const mbtilesFileStats = fs.statSync(mbtilesFile);
@@ -129,7 +146,7 @@ export const serve_data = {
129
146
  }
130
147
  let source;
131
148
  const sourceInfoPromise = new Promise((resolve, reject) => {
132
- source = new MBTiles(mbtilesFile + '?mode=ro', err => {
149
+ source = new MBTiles(mbtilesFile + '?mode=ro', (err) => {
133
150
  if (err) {
134
151
  reject(err);
135
152
  return;
@@ -164,8 +181,8 @@ export const serve_data = {
164
181
  repo[id] = {
165
182
  tileJSON,
166
183
  publicUrl,
167
- source
184
+ source,
168
185
  };
169
186
  });
170
- }
187
+ },
171
188
  };
package/src/serve_font.js CHANGED
@@ -4,7 +4,7 @@ import express from 'express';
4
4
  import fs from 'node:fs';
5
5
  import path from 'path';
6
6
 
7
- import {getFontsPbf} from './utils.js';
7
+ import { getFontsPbf } from './utils.js';
8
8
 
9
9
  export const serve_font = (options, allowedFonts) => {
10
10
  const app = express().disable('x-powered-by');
@@ -26,8 +26,10 @@ export const serve_font = (options, allowedFonts) => {
26
26
  reject(err);
27
27
  return;
28
28
  }
29
- if (stats.isDirectory() &&
30
- fs.existsSync(path.join(fontPath, file, '0-255.pbf'))) {
29
+ if (
30
+ stats.isDirectory() &&
31
+ fs.existsSync(path.join(fontPath, file, '0-255.pbf'))
32
+ ) {
31
33
  existingFonts[path.basename(file)] = true;
32
34
  }
33
35
  });
@@ -40,19 +42,26 @@ export const serve_font = (options, allowedFonts) => {
40
42
  const fontstack = decodeURI(req.params.fontstack);
41
43
  const range = req.params.range;
42
44
 
43
- getFontsPbf(options.serveAllFonts ? null : allowedFonts,
44
- fontPath, fontstack, range, existingFonts).then((concated) => {
45
- res.header('Content-type', 'application/x-protobuf');
46
- res.header('Last-Modified', lastModified);
47
- return res.send(concated);
48
- }, (err) => res.status(400).send(err)
45
+ getFontsPbf(
46
+ options.serveAllFonts ? null : allowedFonts,
47
+ fontPath,
48
+ fontstack,
49
+ range,
50
+ existingFonts,
51
+ ).then(
52
+ (concated) => {
53
+ res.header('Content-type', 'application/x-protobuf');
54
+ res.header('Last-Modified', lastModified);
55
+ return res.send(concated);
56
+ },
57
+ (err) => res.status(400).send(err),
49
58
  );
50
59
  });
51
60
 
52
61
  app.get('/fonts.json', (req, res, next) => {
53
62
  res.header('Content-type', 'application/json');
54
63
  return res.send(
55
- Object.keys(options.serveAllFonts ? existingFonts : allowedFonts).sort()
64
+ Object.keys(options.serveAllFonts ? existingFonts : allowedFonts).sort(),
56
65
  );
57
66
  });
58
67
 
@@ -1,10 +1,9 @@
1
+ /* eslint-disable @typescript-eslint/no-empty-function */
2
+ /* eslint-disable @typescript-eslint/no-unused-vars */
1
3
  'use strict';
2
4
 
3
5
  export const serve_rendered = {
4
- init: (options, repo) => {
5
- },
6
- add: (options, repo, params, id, publicUrl, dataResolver) => {
7
- },
8
- remove: (repo, id) => {
9
- }
6
+ init: (options, repo) => {},
7
+ add: (options, repo, params, id, publicUrl, dataResolver) => {},
8
+ remove: (repo, id) => {},
10
9
  };