From f5256cf80ad97a9512a75a941a2cd9e57730035c Mon Sep 17 00:00:00 2001 From: Lukas Martinelli Date: Tue, 10 Jan 2017 14:05:36 +0100 Subject: [PATCH] Add missing metadata lib --- src/libs/metadata.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/libs/metadata.js diff --git a/src/libs/metadata.js b/src/libs/metadata.js new file mode 100644 index 0000000..861ba15 --- /dev/null +++ b/src/libs/metadata.js @@ -0,0 +1,32 @@ +import request from 'request' + +function loadJSON(url, defaultValue, cb) { + request({ + url: url, + withCredentials: false, + }, (error, response, body) => { + if (!error && body && response.statusCode == 200) { + try { + cb(JSON.parse(body)) + } catch(err) { + console.error(err) + cb(defaultValue) + } + } else { + console.warn('Can not metadata for ' + url) + cb(defaultValue) + } + }) +} + +export function downloadGlyphsMetadata(urlTemplate, cb) { + if(!urlTemplate) return cb([]) + const url = urlTemplate.replace('{fontstack}/{range}.pbf', 'fontstacks.json') + loadJSON(url, [], cb) +} + +export function downloadSpriteMetadata(baseUrl, cb) { + if(!baseUrl) return cb([]) + const url = baseUrl + '.json' + loadJSON(url, {}, glyphs => cb(Object.keys(glyphs))) +}