Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Nov 6, 2024
1 parent d247aba commit 2ccd78c
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib/interceptor/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 2ccd78c

Please sign in to comment.