2020-04-17 20:31:22 +00:00
|
|
|
const fetch = require('node-fetch');
|
|
|
|
const fs = require('fs');
|
2021-06-12 23:26:05 +00:00
|
|
|
const { URLSearchParams } = require('url');
|
2020-04-17 20:31:22 +00:00
|
|
|
const crypto = require('crypto');
|
2021-06-12 23:26:05 +00:00
|
|
|
const Response = require('./classes/response.js');
|
|
|
|
const MemoryCache = require('./classes/caching/memory_cache.js');
|
|
|
|
const FileSystemCache = require('./classes/caching/file_system_cache.js');
|
|
|
|
|
|
|
|
const CACHE_VERSION = 2;
|
2020-04-17 20:31:22 +00:00
|
|
|
|
|
|
|
function md5(str) {
|
|
|
|
return crypto.createHash('md5').update(str).digest('hex');
|
|
|
|
}
|
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
// Since the bounday in FormData is random,
|
|
|
|
// we ignore it for purposes of calculating
|
|
|
|
// the cache key.
|
|
|
|
function getFormDataCacheKey(formData) {
|
|
|
|
const cacheKey = { ...formData };
|
|
|
|
const boundary = formData.getBoundary();
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
delete cacheKey._boundary;
|
2020-11-27 23:01:16 +00:00
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
const boundaryReplaceRegex = new RegExp(boundary, 'g');
|
2020-04-17 20:31:22 +00:00
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
cacheKey._streams = cacheKey._streams.map((s) => {
|
|
|
|
if (typeof s === 'string') {
|
|
|
|
return s.replace(boundaryReplaceRegex, '');
|
2020-04-17 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
return s;
|
|
|
|
});
|
|
|
|
|
|
|
|
return cacheKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBodyCacheKeyJson(body) {
|
|
|
|
if (!body) {
|
|
|
|
return body;
|
|
|
|
} if (typeof body === 'string') {
|
2020-04-17 20:31:22 +00:00
|
|
|
return body;
|
2021-06-12 23:26:05 +00:00
|
|
|
} if (body instanceof URLSearchParams) {
|
|
|
|
return body.toString();
|
|
|
|
} if (body instanceof fs.ReadStream) {
|
|
|
|
return body.path;
|
|
|
|
} if (body.toString && body.toString() === '[object FormData]') {
|
|
|
|
return getFormDataCacheKey(body);
|
2020-04-17 20:31:22 +00:00
|
|
|
}
|
2021-06-12 23:26:05 +00:00
|
|
|
|
|
|
|
throw new Error('Unsupported body type. Supported body types are: string, number, undefined, null, url.URLSearchParams, fs.ReadStream, FormData');
|
2020-04-17 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
function getCacheKey(requestArguments) {
|
|
|
|
const resource = requestArguments[0];
|
|
|
|
const init = requestArguments[1] || {};
|
2020-04-17 20:31:22 +00:00
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
if (typeof resource !== 'string') {
|
|
|
|
throw new Error('The first argument must be a string (fetch.Request is not supported).');
|
2020-04-17 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
const resourceCacheKeyJson = { url: resource };
|
|
|
|
const initCacheKeyJson = { ...init };
|
2020-04-17 20:31:22 +00:00
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
resourceCacheKeyJson.body = getBodyCacheKeyJson(resourceCacheKeyJson.body);
|
|
|
|
initCacheKeyJson.body = getBodyCacheKeyJson(initCacheKeyJson.body);
|
2020-04-17 20:31:22 +00:00
|
|
|
|
2021-07-03 14:20:32 +00:00
|
|
|
delete resourceCacheKeyJson.agent;
|
|
|
|
delete initCacheKeyJson.agent;
|
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
return md5(JSON.stringify([resourceCacheKeyJson, initCacheKeyJson, CACHE_VERSION]));
|
2020-04-17 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
async function createRawResponse(fetchRes) {
|
|
|
|
const buffer = await fetchRes.buffer();
|
2020-04-17 20:31:22 +00:00
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
return {
|
|
|
|
status: fetchRes.status,
|
|
|
|
statusText: fetchRes.statusText,
|
|
|
|
type: fetchRes.type,
|
|
|
|
url: fetchRes.url,
|
|
|
|
ok: fetchRes.ok,
|
|
|
|
headers: fetchRes.headers.raw(),
|
|
|
|
redirected: fetchRes.redirected,
|
|
|
|
bodyBuffer: buffer,
|
|
|
|
};
|
|
|
|
}
|
2020-04-17 20:31:22 +00:00
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
async function getResponse(cache, requestArguments) {
|
|
|
|
const cacheKey = getCacheKey(requestArguments);
|
|
|
|
const cachedValue = await cache.get(cacheKey);
|
2020-04-17 20:31:22 +00:00
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
const ejectSelfFromCache = () => cache.remove(cacheKey);
|
|
|
|
|
|
|
|
if (cachedValue) {
|
|
|
|
return new Response(cachedValue, ejectSelfFromCache, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
const fetchResponse = await fetch(...requestArguments);
|
|
|
|
const rawResponse = await createRawResponse(fetchResponse);
|
|
|
|
await cache.set(cacheKey, rawResponse);
|
|
|
|
return new Response(rawResponse, ejectSelfFromCache, false);
|
2020-04-17 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 23:26:05 +00:00
|
|
|
function createFetchWithCache(cache) {
|
|
|
|
const fetchCache = (...args) => getResponse(cache, args);
|
|
|
|
fetchCache.withCache = createFetchWithCache;
|
|
|
|
|
|
|
|
return fetchCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultFetch = createFetchWithCache(new MemoryCache());
|
|
|
|
|
|
|
|
module.exports = defaultFetch;
|
|
|
|
module.exports.fetchBuilder = defaultFetch;
|
|
|
|
module.exports.MemoryCache = MemoryCache;
|
|
|
|
module.exports.FileSystemCache = FileSystemCache;
|