From 2ccd78c7fb8f76017e3c49bd922f10b74798f566 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Wed, 6 Nov 2024 10:07:10 +0000 Subject: [PATCH] WIP --- lib/interceptor/cache.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/interceptor/cache.js b/lib/interceptor/cache.js index c3264a1f0af..686b5941e1f 100644 --- a/lib/interceptor/cache.js +++ b/lib/interceptor/cache.js @@ -8,7 +8,6 @@ const CacheRevalidationHandler = require('../handler/cache-revalidation-handler' const { assertCacheStore, assertCacheMethods } = require('../util/cache.js') const AGE_HEADER = Buffer.from('age') -function noop () {} /** * @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions} [opts] @@ -57,6 +56,12 @@ module.exports = (opts = {}) => { assert(!stream.readableDidRead, 'stream should not be readableDidRead') try { + stream.on('error', function (err) { + if (!this.readableEnded && typeof handler.onError === 'function') { + handler.onError(err) + } + }) + if (typeof handler.onConnect === 'function') { handler.onConnect((err) => { stream.destroy(err) @@ -83,26 +88,19 @@ module.exports = (opts = {}) => { if (typeof handler.onComplete === 'function') { handler.onComplete(null) } - stream - .on('error', noop) - .destroy() + stream.destroy() } else { stream - .on('data', chunk => { + .on('data', function (chunk) { if (typeof handler.onData === 'function' && !handler.onData(chunk)) { stream.pause() } }) - .on('end', () => { + .on('end', function () { if (typeof handler.onComplete === 'function') { handler.onComplete(value.rawTrailers ?? []) } }) - .on('error', (err) => { - if (!stream.readableEnded && typeof handler.onError === 'function') { - handler.onError(err) - } - }) } } catch (err) { stream.destroy(err)