Skip to content

Commit b5eb764

Browse files
committed
fix: avoid synchronous calls where possible
Signed-off-by: Aarni Koskela <akx@iki.fi>
1 parent 585ac50 commit b5eb764

File tree

6 files changed

+46
-28
lines changed

6 files changed

+46
-28
lines changed

src/main.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'use strict';
44

55
import fs from 'node:fs';
6+
import fsp from 'node:fs/promises';
67
import path from 'path';
78
import { fileURLToPath } from 'url';
89
import axios from 'axios';
@@ -23,6 +24,7 @@ if (args.length >= 3 && args[2][0] !== '-') {
2324
}
2425

2526
import { program } from 'commander';
27+
import { existsP } from './promises.js';
2628
program
2729
.description('tileserver-gl startup options')
2830
.usage('tileserver-gl [mbtiles] [options]')
@@ -95,7 +97,7 @@ const startWithInputFile = async (inputFile) => {
9597
inputFile = path.resolve(process.cwd(), inputFile);
9698
inputFilePath = path.dirname(inputFile);
9799

98-
const inputFileStats = fs.statSync(inputFile);
100+
const inputFileStats = await fsp.stat(inputFile);
99101
if (!inputFileStats.isFile() || inputFileStats.size === 0) {
100102
console.log(`ERROR: Not a valid input file: `);
101103
process.exit(1);
@@ -140,11 +142,11 @@ const startWithInputFile = async (inputFile) => {
140142
};
141143
}
142144

143-
const styles = fs.readdirSync(path.resolve(styleDir, 'styles'));
145+
const styles = await fsp.readdir(path.resolve(styleDir, 'styles'));
144146
for (const styleName of styles) {
145147
const styleFileRel = styleName + '/style.json';
146148
const styleFile = path.resolve(styleDir, 'styles', styleFileRel);
147-
if (fs.existsSync(styleFile)) {
149+
if (await existsP(styleFile)) {
148150
config['styles'][styleName] = {
149151
style: styleFileRel,
150152
tilejson: {
@@ -189,7 +191,7 @@ const startWithInputFile = async (inputFile) => {
189191
process.exit(1);
190192
}
191193

192-
instance.getInfo((err, info) => {
194+
instance.getInfo(async (err, info) => {
193195
if (err || !info) {
194196
console.log('ERROR: Metadata missing in the MBTiles.');
195197
console.log(
@@ -207,11 +209,11 @@ const startWithInputFile = async (inputFile) => {
207209
mbtiles: path.basename(inputFile),
208210
};
209211

210-
const styles = fs.readdirSync(path.resolve(styleDir, 'styles'));
212+
const styles = await fsp.readdir(path.resolve(styleDir, 'styles'));
211213
for (const styleName of styles) {
212214
const styleFileRel = styleName + '/style.json';
213215
const styleFile = path.resolve(styleDir, 'styles', styleFileRel);
214-
if (fs.existsSync(styleFile)) {
216+
if (await existsP(styleFile)) {
215217
config['styles'][styleName] = {
216218
style: styleFileRel,
217219
tilejson: {
@@ -254,10 +256,10 @@ fs.stat(path.resolve(opts.config), async (err, stats) => {
254256
return startWithInputFile(inputFile);
255257
} else {
256258
// try to find in the cwd
257-
const files = fs.readdirSync(process.cwd());
259+
const files = await fsp.readdir(process.cwd());
258260
for (const filename of files) {
259261
if (filename.endsWith('.mbtiles') || filename.endsWith('.pmtiles')) {
260-
const inputFilesStats = fs.statSync(filename);
262+
const inputFilesStats = await fsp.stat(filename);
261263
if (inputFilesStats.isFile() && inputFilesStats.size > 0) {
262264
inputFile = filename;
263265
break;

src/promises.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import util from 'node:util';
2+
import fsp from 'node:fs/promises';
3+
import zlib from 'zlib';
4+
5+
export const gzipP = util.promisify(zlib.gzip);
6+
export const gunzipP = util.promisify(zlib.gunzip);
7+
export const existsP = async (path) => {
8+
try {
9+
await fsp.access(path); // Defaults to F_OK: indicating that the file is visible to the calling process
10+
return true;
11+
} catch (err) {
12+
return false;
13+
}
14+
};

src/serve_data.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
'use strict';
22

3-
import fs from 'node:fs';
3+
import fsp from 'node:fs/promises';
44
import path from 'path';
5-
import zlib from 'zlib';
65

76
import clone from 'clone';
87
import express from 'express';
98
import MBTiles from '@mapbox/mbtiles';
109
import Pbf from 'pbf';
1110
import { VectorTile } from '@mapbox/vector-tile';
1211

13-
import { getTileUrls, isValidHttpUrl, fixTileJSONCenter } from './utils.js';
12+
import { fixTileJSONCenter, getTileUrls, isValidHttpUrl } from './utils.js';
1413
import {
15-
openPMtiles,
1614
getPMtilesInfo,
1715
getPMtilesTile,
16+
openPMtiles,
1817
} from './pmtiles_adapter.js';
18+
import { gunzipP, gzipP } from './promises.js';
1919

2020
export const serve_data = {
2121
init: (options, repo) => {
@@ -89,12 +89,12 @@ export const serve_data = {
8989
headers['Content-Encoding'] = 'gzip';
9090
res.set(headers);
9191

92-
data = zlib.gzipSync(data);
92+
data = await gzipP(data);
9393

9494
return res.status(200).send(data);
9595
}
9696
} else if (item.sourceType === 'mbtiles') {
97-
item.source.getTile(z, x, y, (err, data, headers) => {
97+
item.source.getTile(z, x, y, async (err, data, headers) => {
9898
let isGzipped;
9999
if (err) {
100100
if (/does not exist/.test(err.message)) {
@@ -114,7 +114,7 @@ export const serve_data = {
114114
data.slice(0, 2).indexOf(Buffer.from([0x1f, 0x8b])) === 0;
115115
if (options.dataDecoratorFunc) {
116116
if (isGzipped) {
117-
data = zlib.unzipSync(data);
117+
data = await gunzipP(data);
118118
isGzipped = false;
119119
}
120120
data = options.dataDecoratorFunc(id, 'data', data, z, x, y);
@@ -126,7 +126,7 @@ export const serve_data = {
126126
headers['Content-Type'] = 'application/json';
127127

128128
if (isGzipped) {
129-
data = zlib.unzipSync(data);
129+
data = await gunzipP(data);
130130
isGzipped = false;
131131
}
132132

@@ -151,7 +151,7 @@ export const serve_data = {
151151
res.set(headers);
152152

153153
if (!isGzipped) {
154-
data = zlib.gzipSync(data);
154+
data = await gzipP(data);
155155
}
156156

157157
return res.status(200).send(data);
@@ -212,7 +212,7 @@ export const serve_data = {
212212
};
213213

214214
if (!isValidHttpUrl(inputFile)) {
215-
const inputFileStats = fs.statSync(inputFile);
215+
const inputFileStats = await fsp.stat(inputFile);
216216
if (!inputFileStats.isFile() || inputFileStats.size === 0) {
217217
throw Error(`Not valid input file: "${inputFile}"`);
218218
}

src/serve_rendered.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import fs from 'node:fs';
1717
import path from 'path';
1818
import url from 'url';
1919
import util from 'util';
20-
import zlib from 'zlib';
2120
import sharp from 'sharp';
2221
import clone from 'clone';
2322
import Color from 'color';
@@ -42,6 +41,8 @@ import {
4241
getPMtilesTile,
4342
} from './pmtiles_adapter.js';
4443
import { renderOverlay, renderWatermark, renderAttribution } from './render.js';
44+
import fsp from 'node:fs/promises';
45+
import { gunzipP } from './promises.js';
4546

4647
const FLOAT_PATTERN = '[+-]?(?:\\d+|\\d+.?\\d+)';
4748
const PATH_PATTERN =
@@ -943,7 +944,7 @@ export const serve_rendered = {
943944
callback(null, response);
944945
}
945946
} else if (sourceType === 'mbtiles') {
946-
source.getTile(z, x, y, (err, data, headers) => {
947+
source.getTile(z, x, y, async (err, data, headers) => {
947948
if (err) {
948949
if (options.verbose)
949950
console.log('MBTiles error, serving empty', err);
@@ -962,7 +963,7 @@ export const serve_rendered = {
962963

963964
if (format === 'pbf') {
964965
try {
965-
response.data = zlib.unzipSync(data);
966+
response.data = await gunzipP(data);
966967
} catch (err) {
967968
console.log(
968969
'Skipping incorrect header for tile mbtiles://%s/%s/%s/%s.pbf',
@@ -1039,7 +1040,7 @@ export const serve_rendered = {
10391040
const styleFile = params.style;
10401041
const styleJSONPath = path.resolve(options.paths.styles, styleFile);
10411042
try {
1042-
styleJSON = JSON.parse(fs.readFileSync(styleJSONPath));
1043+
styleJSON = JSON.parse(await fsp.readFile(styleJSONPath));
10431044
} catch (e) {
10441045
console.log('Error parsing style file');
10451046
return false;
@@ -1145,7 +1146,7 @@ export const serve_rendered = {
11451146
}
11461147

11471148
if (!isValidHttpUrl(inputFile)) {
1148-
const inputFileStats = fs.statSync(inputFile);
1149+
const inputFileStats = await fsp.stat(inputFile);
11491150
if (!inputFileStats.isFile() || inputFileStats.size === 0) {
11501151
throw Error(`Not valid PMTiles file: "${inputFile}"`);
11511152
}
@@ -1187,9 +1188,9 @@ export const serve_rendered = {
11871188
}
11881189
} else {
11891190
queue.push(
1190-
new Promise((resolve, reject) => {
1191+
new Promise(async (resolve, reject) => {
11911192
inputFile = path.resolve(options.paths.mbtiles, inputFile);
1192-
const inputFileStats = fs.statSync(inputFile);
1193+
const inputFileStats = await fsp.stat(inputFile);
11931194
if (!inputFileStats.isFile() || inputFileStats.size === 0) {
11941195
throw Error(`Not valid MBTiles file: "${inputFile}"`);
11951196
}

src/serve_style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const serve_style = {
8787

8888
let styleFileData;
8989
try {
90-
styleFileData = fs.readFileSync(styleFile);
90+
styleFileData = fs.readFileSync(styleFile); // TODO: could be made async if this function was
9191
} catch (e) {
9292
console.log('Error reading style file');
9393
return false;

src/utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import path from 'path';
44
import fsPromises from 'fs/promises';
5-
import fs, { existsSync } from 'node:fs';
5+
import fs from 'node:fs';
66
import clone from 'clone';
77
import { combine } from '@jsse/pbfont';
8+
import { existsP } from './promises.js';
89

910
/**
1011
* Restrict user input to an allowed set of options.
@@ -225,7 +226,7 @@ export const listFonts = async (fontPath) => {
225226
const stats = await fsPromises.stat(path.join(fontPath, file));
226227
if (
227228
stats.isDirectory() &&
228-
existsSync(path.join(fontPath, file, '0-255.pbf'))
229+
(await existsP(path.join(fontPath, file, '0-255.pbf')))
229230
) {
230231
existingFonts[path.basename(file)] = true;
231232
}

0 commit comments

Comments
 (0)