(xpath)
| 38 | // Wrapper on readCompressedJsonFileFallback that initially only checks |
| 39 | // if the file exists but doesn't read the content till you call it. |
| 40 | export function readCompressedJsonFileFallbackLazily(xpath) { |
| 41 | const cache = new Map() |
| 42 | // This will throw if the file isn't accessible at all, e.g. ENOENT |
| 43 | // But, the file might have been replaced by one called `SAMENAME.json.br` |
| 44 | // because in staging, we ship these files compressed to make the |
| 45 | // deployment faster. So, in our file-presence check, we need to |
| 46 | // account for that. |
| 47 | try { |
| 48 | fs.accessSync(xpath) |
| 49 | } catch (err) { |
| 50 | if (err.code === 'ENOENT') { |
| 51 | try { |
| 52 | fs.accessSync(xpath + '.br') |
| 53 | } catch (err) { |
| 54 | if (err.code === 'ENOENT') { |
| 55 | throw new Error(`Neither ${xpath} nor ${xpath}.br is accessible`) |
| 56 | } |
| 57 | throw err |
| 58 | } |
| 59 | } else { |
| 60 | throw err |
| 61 | } |
| 62 | } |
| 63 | return () => { |
| 64 | if (!cache.has(xpath)) { |
| 65 | cache.set(xpath, readCompressedJsonFileFallback(xpath)) |
| 66 | if (globalCacheCounter[xpath]) { |
| 67 | console.warn( |
| 68 | "If this happens it's because the readCompressedJsonFileFallbackLazily " + |
| 69 | 'function has been called non-globally. Only use ' + |
| 70 | 'readCompressedJsonFileFallback once at module-level.' |
| 71 | ) |
| 72 | throw new Error(`Globally reading the same file more than once (${xpath})`) |
| 73 | } |
| 74 | globalCacheCounter[xpath] = 1 |
| 75 | } |
| 76 | return cache.get(xpath) |
| 77 | } |
| 78 | } |
no test coverage detected