2020-04-17 20:31:22 +00:00
|
|
|
const fetch = require('node-fetch');
|
|
|
|
const fs = require('fs');
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
function md5(str) {
|
|
|
|
return crypto.createHash('md5').update(str).digest('hex');
|
|
|
|
}
|
|
|
|
|
2020-11-28 03:26:31 +00:00
|
|
|
class Headers {
|
|
|
|
constructor(rawHeaders) {
|
|
|
|
this.rawHeaders = rawHeaders;
|
|
|
|
}
|
|
|
|
|
|
|
|
* entries() {
|
|
|
|
for (let entry of Object.entries(this.rawHeaders)) {
|
|
|
|
yield entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
* keys() {
|
|
|
|
for (let key of Object.keys(this.rawHeaders)) {
|
|
|
|
yield key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
* values() {
|
|
|
|
for (let value of Object.values(this.rawHeaders)) {
|
|
|
|
yield value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get(name) {
|
|
|
|
return this.rawHeaders[name.toLowerCase()] || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
has(name) {
|
|
|
|
return !!this.get(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 02:46:49 +00:00
|
|
|
class Response {
|
2020-11-28 03:26:31 +00:00
|
|
|
constructor(raw, cacheFilePath, fromCache) {
|
2020-11-28 02:46:49 +00:00
|
|
|
Object.assign(this, raw);
|
|
|
|
this.cacheFilePath = cacheFilePath;
|
2020-11-28 03:26:31 +00:00
|
|
|
this.headers = new Headers(raw.headers);
|
|
|
|
this.fromCache = fromCache;
|
|
|
|
|
|
|
|
if (this.bodyBuffer.type === 'Buffer') {
|
|
|
|
this.bodyBuffer = Buffer.from(this.bodyBuffer);
|
|
|
|
}
|
2020-04-17 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
text() {
|
2020-11-28 02:46:49 +00:00
|
|
|
return this.bodyBuffer.toString();
|
2020-04-17 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
json() {
|
2020-11-28 02:46:49 +00:00
|
|
|
return JSON.parse(this.bodyBuffer.toString());
|
2020-04-17 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer() {
|
2020-11-28 02:46:49 +00:00
|
|
|
return this.bodyBuffer;
|
2020-04-17 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 02:46:49 +00:00
|
|
|
ejectFromCache() {
|
|
|
|
return fs.promises.unlink(this.cacheFilePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createRawResponse(fetchRes) {
|
|
|
|
const buffer = await fetchRes.buffer();
|
|
|
|
|
2020-11-28 03:26:31 +00:00
|
|
|
const rawHeaders = Array.from(fetchRes.headers.entries())
|
|
|
|
.reduce((aggregate, entry) => ({ ...aggregate, [entry[0]]: entry[1] }), {});
|
|
|
|
|
2020-11-28 02:46:49 +00:00
|
|
|
return {
|
|
|
|
status: fetchRes.status,
|
|
|
|
statusText: fetchRes.statusText,
|
|
|
|
type: fetchRes.type,
|
|
|
|
url: fetchRes.url,
|
|
|
|
ok: fetchRes.ok,
|
2020-11-28 03:26:31 +00:00
|
|
|
headers: rawHeaders,
|
2020-11-28 02:46:49 +00:00
|
|
|
redirected: fetchRes.redirected,
|
|
|
|
bodyBuffer: buffer,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getResponse(cacheDirPath, requestArguments) {
|
|
|
|
const [url, requestInit, ...rest] = requestArguments;
|
|
|
|
const requestParams = requestInit.body
|
|
|
|
? ({ ...requestInit, body: typeof requestInit.body === 'object' ? requestInit.body.toString() : requestInit.body })
|
|
|
|
: requestInit;
|
|
|
|
|
2020-11-28 03:26:31 +00:00
|
|
|
const cacheHash = md5(JSON.stringify([url, requestParams, ...rest]));
|
2020-11-28 02:46:49 +00:00
|
|
|
const cachedFilePath = path.join(cacheDirPath, `${cacheHash}.json`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const rawResponse = JSON.parse(await fs.promises.readFile(cachedFilePath));
|
2020-11-28 03:26:31 +00:00
|
|
|
return new Response(rawResponse, cachedFilePath, true);
|
2020-11-28 02:46:49 +00:00
|
|
|
} catch (err) {
|
|
|
|
const fetchResponse = await fetch(...requestArguments);
|
2020-11-28 03:26:31 +00:00
|
|
|
const rawResponse = await createRawResponse(fetchResponse);
|
2020-11-28 02:46:49 +00:00
|
|
|
await fs.promises.writeFile(cachedFilePath, JSON.stringify(rawResponse));
|
2020-11-28 03:26:31 +00:00
|
|
|
return new Response(rawResponse, cachedFilePath, false);
|
2020-04-17 20:31:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createFetch(cacheDirPath) {
|
|
|
|
let madeDir = false;
|
|
|
|
|
|
|
|
return async (...args) => {
|
|
|
|
if (!madeDir) {
|
2020-11-28 02:46:49 +00:00
|
|
|
await fs.promises.mkdir(cacheDirPath, { recursive: true });
|
2020-04-17 20:31:22 +00:00
|
|
|
madeDir = true;
|
|
|
|
}
|
|
|
|
|
2020-11-28 02:46:49 +00:00
|
|
|
return getResponse(cacheDirPath, args);
|
2020-04-17 20:31:22 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = createFetch;
|