support Request object

This commit is contained in:
Randall Schmidt 2021-07-09 13:41:18 -04:00
parent dae57d2604
commit 847b330489
2 changed files with 30 additions and 14 deletions

@ -1,4 +1,4 @@
import fetch from 'node-fetch';
import fetch, { Request } from 'node-fetch';
import fs from 'fs';
import { URLSearchParams } from 'url';
import crypto from 'crypto';
@ -47,26 +47,42 @@ function getBodyCacheKeyJson(body) {
return body.path;
} if (body.toString && body.toString() === '[object FormData]') {
return getFormDataCacheKey(body);
} if (body instanceof Buffer) {
return body.toString();
}
throw new Error('Unsupported body type. Supported body types are: string, number, undefined, null, url.URLSearchParams, fs.ReadStream, FormData');
}
function getRequestCacheKey(req) {
return {
cache: req.cache,
credentials: req.credentials,
destination: req.destination,
headers: req.headers,
integrity: req.integrity,
method: req.method,
redirect: req.redirect,
referrer: req.referrer,
referrerPolicy: req.referrerPolicy,
url: req.url,
body: getBodyCacheKeyJson(req.body),
};
}
function getCacheKey(requestArguments) {
const resource = requestArguments[0];
const init = requestArguments[1] || {};
if (typeof resource !== 'string') {
throw new Error('The first argument must be a string (fetch.Request is not supported).');
}
const resourceCacheKeyJson = resource instanceof Request
? getRequestCacheKey(resource)
: { url: resource };
const resourceCacheKeyJson = { url: resource };
const initCacheKeyJson = { ...init };
resourceCacheKeyJson.body = getBodyCacheKeyJson(resourceCacheKeyJson.body);
initCacheKeyJson.body = getBodyCacheKeyJson(initCacheKeyJson.body);
delete resourceCacheKeyJson.agent;
delete initCacheKeyJson.agent;
return md5(JSON.stringify([resourceCacheKeyJson, initCacheKeyJson, CACHE_VERSION]));

@ -316,14 +316,14 @@ describe('Cache tests', function() {
}).timeout(10000);
describe('Data tests', function() {
it('Does not support Request objects', async function() {
try {
const request = new standardFetch.Request('https://google.com');
await cachedFetch(request);
throw new Error('The above line should have thrown.');
} catch (err) {
assert(err.message.includes('The first argument must be a string (fetch.Request is not supported).'));
}
it('Supports request objects', async function() {
let request = new standardFetch.Request('https://google.com', { body: 'test', method: 'POST' });
res = await cachedFetch(request);
assert.strictEqual(res.fromCache, false);
request = new standardFetch.Request('https://google.com', { body: 'test', method: 'POST' });
res = await cachedFetch(request);
assert.strictEqual(res.fromCache, true);
});
it('Refuses to consume body twice', async function() {