Skip to content

Commit

Permalink
fix: listFonts was broken, missing fonts could not fallback (#1076)
Browse files Browse the repository at this point in the history
Promise code never worked: listFonts did not wait for fs.stat() to resolve().
This was not evident because late results were still used.

A recent PR made it worse: late results are now ignored.
This manifested for styles with missing fonts, no fallback could be used.

Signed-off-by: Martin d'Allens <martin.dallens@liberty-rider.com>
  • Loading branch information
Caerbannog authored Nov 24, 2023
1 parent b25a642 commit c9aa26a
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

import path from 'path';
import fs from 'node:fs';
import fsPromises from 'fs/promises';
import fs, { existsSync } from 'node:fs';
import clone from 'clone';
import glyphCompose from '@mapbox/glyph-pbf-composite';

Expand Down Expand Up @@ -165,30 +166,18 @@ export const getFontsPbf = (

export const listFonts = async (fontPath) => {
const existingFonts = {};
const fontListingPromise = new Promise((resolve, reject) => {
fs.readdir(fontPath, (err, files) => {
if (err) {
reject(err);
return;
}
for (const file of files) {
fs.stat(path.join(fontPath, file), (err, stats) => {
if (err) {
reject(err);
return;
}
if (
stats.isDirectory() &&
fs.existsSync(path.join(fontPath, file, '0-255.pbf'))
) {
existingFonts[path.basename(file)] = true;
}
});
}
resolve();
});
});
await fontListingPromise;

const files = await fsPromises.readdir(fontPath);
for (const file of files) {
const stats = await fsPromises.stat(path.join(fontPath, file));
if (
stats.isDirectory() &&
existsSync(path.join(fontPath, file, '0-255.pbf'))
) {
existingFonts[path.basename(file)] = true;
}
}

return existingFonts;
};

Expand Down

0 comments on commit c9aa26a

Please sign in to comment.