diff --git a/keyserver/src/utils/import-json.js b/keyserver/src/utils/import-json.js index 6719b10b6..fb2318a45 100644 --- a/keyserver/src/utils/import-json.js +++ b/keyserver/src/utils/import-json.js @@ -1,23 +1,26 @@ // @flow const cachedJSON = new Map(); async function importJSON(path: string): Promise { const cached = cachedJSON.get(path); if (cached !== undefined) { return cached; } + const json = await getJSON(path); + if (!cachedJSON.has(path)) { + cachedJSON.set(path, json); + } + return cachedJSON.get(path); +} + +async function getJSON(path: string): Promise { try { // $FlowFixMe const importedJSON = await import(`../../${path}`); - if (!cachedJSON.has(path)) { - cachedJSON.set(path, importedJSON.default); - } + return importedJSON.default; } catch { - if (!cachedJSON.has(path)) { - cachedJSON.set(path, null); - } + return null; } - return cachedJSON.get(path); } export { importJSON };