UNPKG

p5-analysis

Version:

API to find, create, and analyze p5.js sketch files.

67 lines (66 loc) 2.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.cachedFetch = void 0; const crypto_1 = __importDefault(require("crypto")); const fs_1 = __importDefault(require("fs")); const node_fetch_1 = __importDefault(require("node-fetch")); const CACHE_DIR = '/tmp/node-cdn-fetch-cache'; /** Wrapper for node-fetch that looks in a cache directory before fetching from * the network. If the cache is missing, it will be created. If the file is * missing, it will be fetched from the network and saved to the cache. If the * file is present, it will be read from the cache. * * Usage: const fetch = require('node-fetch-cache'); const res = await * fetch('https://example.com/file.json'); console.log(res.text()); * * const fetch = require('node-fetch-cache')('/path/to/cache/dir'); const res = * await fetch('https://example.com/file.json'); console.log(res.text()); */ async function cachedFetch(url) { const hash = crypto_1.default.createHash('md5').update(url).digest('hex'); const cacheDir = `${CACHE_DIR}/${hash.slice(0, 2)}`; const cachePath = `${cacheDir}/${hash.slice(2)}`; const cacheDataPath = `${cachePath}.data`; const cacheMetaPath = `${cachePath}.meta`; if (!fs_1.default.existsSync(cacheDir)) { fs_1.default.mkdirSync(cacheDir, { recursive: true }); } // if the file is more than a day old, fetch the URL from the network if (!fs_1.default.existsSync(cacheMetaPath) || fs_1.default.statSync(cacheMetaPath).mtime.getTime() < Date.now() - 86400000) { if (process.env.DEBUG_NODE_CDN_CACHE) { // eslint-disable-next-line no-console console.debug(`cache miss: ${url}`); } const res = await (0, node_fetch_1.default)(url); if (res.ok) { const text = await res.text(); fs_1.default.writeFileSync(cacheDataPath, text); } else { // fs.unlinkSync(cacheDataPath); } const meta = { headers: Object.fromEntries(res.headers.entries()), ok: res.ok, redirected: res.redirected, status: res.status, statusText: res.statusText, type: res.type, }; fs_1.default.writeFileSync(cacheMetaPath, JSON.stringify(meta)); } // Read from the cache we just wrote. In a development context, this has no // noticeable efficiency impact; and, it reduces the number of code paths. const meta = JSON.parse(fs_1.default.readFileSync(cacheMetaPath, 'utf-8')); const text = meta.ok ? fs_1.default.readFileSync(cacheDataPath, 'utf-8') : undefined; return { ...meta, text: () => (meta.ok ? Promise.resolve(text) : Promise.reject(meta.statusText)), url, }; } exports.cachedFetch = cachedFetch;