From 524308aa61d967a29f93c82acb32c35c7fd6d50e Mon Sep 17 00:00:00 2001 From: Randall Schmidt Date: Sat, 3 Jul 2021 10:20:32 -0400 Subject: [PATCH] fix error with custom agent with circular reference --- index.js | 3 +++ test/tests.js | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/index.js b/index.js index 923577b..1713a73 100644 --- a/index.js +++ b/index.js @@ -66,6 +66,9 @@ function getCacheKey(requestArguments) { resourceCacheKeyJson.body = getBodyCacheKeyJson(resourceCacheKeyJson.body); initCacheKeyJson.body = getBodyCacheKeyJson(initCacheKeyJson.body); + delete resourceCacheKeyJson.agent; + delete initCacheKeyJson.agent; + return md5(JSON.stringify([resourceCacheKeyJson, initCacheKeyJson, CACHE_VERSION])); } diff --git a/test/tests.js b/test/tests.js index 8ce10f6..b131f33 100644 --- a/test/tests.js +++ b/test/tests.js @@ -6,6 +6,7 @@ const path = require('path'); const { URLSearchParams } = require('url'); const standardFetch = require('node-fetch'); const FetchCache = require('../index.js'); +const { Agent } = require('http'); const CACHE_PATH = path.join(__dirname, '..', '.cache'); const expectedPngBuffer = fs.readFileSync(path.join(__dirname, 'expected_png.png')); @@ -299,6 +300,13 @@ describe('Cache tests', function() { res = await cachedFetch(TWO_HUNDRED_URL, post(data2)); assert.strictEqual(res.fromCache, true); }); + + it('Does not error with custom agent with circular properties', async function() { + const agent = new Agent(); + agent.agent = agent; + + await cachedFetch('http://httpbin.org/status/200', { agent }); + }) }).timeout(10000); describe('Data tests', function() {