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/mapbox-gl-native": "3.4.4",
|
||||||
"@mapbox/sphericalmercator": "1.0.5",
|
"@mapbox/sphericalmercator": "1.0.5",
|
||||||
"advanced-pool": "0.3.2",
|
"advanced-pool": "0.3.2",
|
||||||
"async": "2.4.0",
|
|
||||||
"base64url": "2.0.0",
|
"base64url": "2.0.0",
|
||||||
"canvas": "1.6.5",
|
"canvas": "1.6.5",
|
||||||
"clone": "2.1.1",
|
"clone": "2.1.1",
|
||||||
|
|
|
@ -36,19 +36,15 @@ module.exports = function(options, allowedFonts) {
|
||||||
var fontstack = decodeURI(req.params.fontstack);
|
var fontstack = decodeURI(req.params.fontstack);
|
||||||
var range = req.params.range;
|
var range = req.params.range;
|
||||||
|
|
||||||
return utils.getFontsPbf(options.serveAllFonts ? null : allowedFonts,
|
utils.getFontsPbf(options.serveAllFonts ? null : allowedFonts,
|
||||||
fontPath, fontstack, range, existingFonts,
|
fontPath, fontstack, range, existingFonts).then(function(concated) {
|
||||||
function(err, concated) {
|
|
||||||
if (err || concated.length === 0) {
|
|
||||||
console.log(err);
|
|
||||||
console.log(concated.length);
|
|
||||||
return res.status(400).send('');
|
|
||||||
} else {
|
|
||||||
res.header('Content-type', 'application/x-protobuf');
|
res.header('Content-type', 'application/x-protobuf');
|
||||||
res.header('Last-Modified', lastModified);
|
res.header('Last-Modified', lastModified);
|
||||||
return res.send(concated);
|
return res.send(concated);
|
||||||
|
}, function(err) {
|
||||||
|
return res.status(400).send(err);
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/fonts.json', function(req, res, next) {
|
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 parts = req.url.split('/');
|
||||||
var fontstack = unescape(parts[2]);
|
var fontstack = unescape(parts[2]);
|
||||||
var range = parts[3].split('.')[0];
|
var range = parts[3].split('.')[0];
|
||||||
utils.getFontsPbf(null, options.paths[protocol], fontstack, range, existingFonts,
|
utils.getFontsPbf(
|
||||||
function(err, concated) {
|
null, options.paths[protocol], fontstack, range, existingFonts
|
||||||
callback(err, {data: concated});
|
).then(function(concated) {
|
||||||
|
callback(null, {data: concated});
|
||||||
|
}, function(err) {
|
||||||
|
callback(err, {data: null});
|
||||||
});
|
});
|
||||||
} else if (protocol == 'mbtiles') {
|
} else if (protocol == 'mbtiles') {
|
||||||
var parts = req.url.split('/');
|
var parts = req.url.split('/');
|
||||||
|
|
|
@ -117,7 +117,5 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
return Promise.resolve(app);
|
||||||
resolve(app);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
49
src/utils.js
49
src/utils.js
|
@ -1,7 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async'),
|
var path = require('path'),
|
||||||
path = require('path'),
|
|
||||||
fs = require('fs');
|
fs = require('fs');
|
||||||
|
|
||||||
var clone = require('clone'),
|
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, fontPath, name, range, fallbacks) {
|
||||||
var getFontPbf = function(allowedFonts, name, range, callback, fallbacks) {
|
return new Promise(function(resolve, reject) {
|
||||||
if (!allowedFonts || (allowedFonts[name] && fallbacks)) {
|
if (!allowedFonts || (allowedFonts[name] && fallbacks)) {
|
||||||
var filename = path.join(fontPath, name, range + '.pbf');
|
var filename = path.join(fontPath, name, range + '.pbf');
|
||||||
if (!fallbacks) {
|
if (!fallbacks) {
|
||||||
fallbacks = clone(allowedFonts || {});
|
fallbacks = clone(allowedFonts || {});
|
||||||
}
|
}
|
||||||
delete fallbacks[name];
|
delete fallbacks[name];
|
||||||
return fs.readFile(filename, function(err, data) {
|
fs.readFile(filename, function(err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('ERROR: Font not found:', name);
|
console.error('ERROR: Font not found:', name);
|
||||||
if (fallbacks && Object.keys(fallbacks).length) {
|
if (fallbacks && Object.keys(fallbacks).length) {
|
||||||
var fallbackName = Object.keys(fallbacks)[0];
|
var fallbackName = Object.keys(fallbacks)[0];
|
||||||
console.error('ERROR: Trying to use', fallbackName, 'as a fallback');
|
console.error('ERROR: Trying to use', fallbackName, 'as a fallback');
|
||||||
delete fallbacks[fallbackName];
|
delete fallbacks[fallbackName];
|
||||||
return getFontPbf(null, fallbackName, range, callback, fallbacks);
|
getFontPbf(null, fontPath, fallbackName, range, fallbacks).then(resolve, reject);
|
||||||
} else {
|
} else {
|
||||||
return callback(new Error('Font load error: ' + name));
|
reject('Font load error: ' + name);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return callback(null, data);
|
resolve(data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
return callback(new Error('Font not allowed: ' + name));
|
reject('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));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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