Use promises instead of async in font concatenation (remove async dependency)
This commit is contained in:
parent
5d93b1d4f9
commit
bdc3d20524
5 changed files with 36 additions and 41 deletions
|
@ -22,7 +22,6 @@
|
|||
"@mapbox/mapbox-gl-native": "3.4.4",
|
||||
"@mapbox/sphericalmercator": "1.0.5",
|
||||
"advanced-pool": "0.3.2",
|
||||
"async": "2.4.0",
|
||||
"base64url": "2.0.0",
|
||||
"canvas": "1.6.5",
|
||||
"clone": "2.1.1",
|
||||
|
|
|
@ -36,19 +36,15 @@ module.exports = function(options, allowedFonts) {
|
|||
var fontstack = decodeURI(req.params.fontstack);
|
||||
var range = req.params.range;
|
||||
|
||||
return utils.getFontsPbf(options.serveAllFonts ? null : allowedFonts,
|
||||
fontPath, fontstack, range, existingFonts,
|
||||
function(err, concated) {
|
||||
if (err || concated.length === 0) {
|
||||
console.log(err);
|
||||
console.log(concated.length);
|
||||
return res.status(400).send('');
|
||||
} else {
|
||||
utils.getFontsPbf(options.serveAllFonts ? null : allowedFonts,
|
||||
fontPath, fontstack, range, existingFonts).then(function(concated) {
|
||||
res.header('Content-type', 'application/x-protobuf');
|
||||
res.header('Last-Modified', lastModified);
|
||||
return res.send(concated);
|
||||
}, function(err) {
|
||||
return res.status(400).send(err);
|
||||
}
|
||||
});
|
||||
);
|
||||
});
|
||||
|
||||
app.get('/fonts.json', function(req, res, next) {
|
||||
|
|
|
@ -91,9 +91,12 @@ module.exports = function(options, repo, params, id, dataResolver) {
|
|||
var parts = req.url.split('/');
|
||||
var fontstack = unescape(parts[2]);
|
||||
var range = parts[3].split('.')[0];
|
||||
utils.getFontsPbf(null, options.paths[protocol], fontstack, range, existingFonts,
|
||||
function(err, concated) {
|
||||
callback(err, {data: concated});
|
||||
utils.getFontsPbf(
|
||||
null, options.paths[protocol], fontstack, range, existingFonts
|
||||
).then(function(concated) {
|
||||
callback(null, {data: concated});
|
||||
}, function(err) {
|
||||
callback(err, {data: null});
|
||||
});
|
||||
} else if (protocol == 'mbtiles') {
|
||||
var parts = req.url.split('/');
|
||||
|
|
|
@ -117,7 +117,5 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) {
|
|||
});
|
||||
});
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
resolve(app);
|
||||
});
|
||||
return Promise.resolve(app);
|
||||
};
|
||||
|
|
49
src/utils.js
49
src/utils.js
|
@ -1,7 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
var async = require('async'),
|
||||
path = require('path'),
|
||||
var path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
var clone = require('clone'),
|
||||
|
@ -73,47 +72,47 @@ module.exports.fixTileJSONCenter = function(tileJSON) {
|
|||
}
|
||||
};
|
||||
|
||||
module.exports.getFontsPbf = function(allowedFonts, fontPath, names, range, fallbacks, callback) {
|
||||
var getFontPbf = function(allowedFonts, name, range, callback, fallbacks) {
|
||||
var getFontPbf = function(allowedFonts, fontPath, name, range, fallbacks) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
if (!allowedFonts || (allowedFonts[name] && fallbacks)) {
|
||||
var filename = path.join(fontPath, name, range + '.pbf');
|
||||
if (!fallbacks) {
|
||||
fallbacks = clone(allowedFonts || {});
|
||||
}
|
||||
delete fallbacks[name];
|
||||
return fs.readFile(filename, function(err, data) {
|
||||
fs.readFile(filename, function(err, data) {
|
||||
if (err) {
|
||||
console.error('ERROR: Font not found:', name);
|
||||
if (fallbacks && Object.keys(fallbacks).length) {
|
||||
var fallbackName = Object.keys(fallbacks)[0];
|
||||
console.error('ERROR: Trying to use', fallbackName, 'as a fallback');
|
||||
delete fallbacks[fallbackName];
|
||||
return getFontPbf(null, fallbackName, range, callback, fallbacks);
|
||||
getFontPbf(null, fontPath, fallbackName, range, fallbacks).then(resolve, reject);
|
||||
} else {
|
||||
return callback(new Error('Font load error: ' + name));
|
||||
reject('Font load error: ' + name);
|
||||
}
|
||||
} else {
|
||||
return callback(null, data);
|
||||
resolve(data);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return callback(new Error('Font not allowed: ' + name));
|
||||
}
|
||||
};
|
||||
|
||||
var fonts = names.split(',');
|
||||
var queue = [];
|
||||
fonts.forEach(function(font) {
|
||||
queue.push(function(callback) {
|
||||
getFontPbf(allowedFonts, font, range, callback, clone(allowedFonts || fallbacks));
|
||||
});
|
||||
});
|
||||
|
||||
return async.parallel(queue, function(err, results) {
|
||||
if (err) {
|
||||
callback(err, new Buffer([]));
|
||||
} else {
|
||||
callback(err, glyphCompose.combine(results));
|
||||
reject('Font not allowed: ' + name);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.getFontsPbf = function(allowedFonts, fontPath, names, range, fallbacks) {
|
||||
var fonts = names.split(',');
|
||||
var queue = [];
|
||||
fonts.forEach(function(font) {
|
||||
queue.push(
|
||||
getFontPbf(allowedFonts, fontPath, font, range, clone(allowedFonts || fallbacks))
|
||||
);
|
||||
});
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
Promise.all(queue).then(function(values) {
|
||||
return resolve(glyphCompose.combine(values));
|
||||
}, reject);
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue