Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cache #3804

Merged
merged 17 commits into from
Nov 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP
  • Loading branch information
ronag committed Nov 6, 2024
commit 0d01767000d55d686a888dc63cb4e503e909a7e5
46 changes: 20 additions & 26 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,18 @@ 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)
}
})
.on('close', function () {
if (!this.errored && typeof handler.onComplete === 'function') {
handler.onComplete(value.rawTrailers ?? [])
}
})

if (typeof handler.onConnect === 'function') {
handler.onConnect((err) => {
stream.destroy(err)
Expand All @@ -73,36 +84,19 @@ module.exports = (opts = {}) => {

value.rawHeaders.push(AGE_HEADER, Buffer.from(`${age}`))

handler.onHeaders(value.statusCode, value.rawHeaders, () => stream.resume(), value.statusMessage)
if (stream.destroyed) {
return
if (handler.onHeaders(value.statusCode, value.rawHeaders, () => stream.resume(), value.statusMessage) === false) {
stream.pause()
}
}

if (opts.method === 'HEAD') {
if (typeof handler.onComplete === 'function') {
handler.onComplete(null)
}
stream
.on('error', noop)
.destroy()
stream.destroy()
} else {
stream
.on('data', chunk => {
if (typeof handler.onData === 'function' && !handler.onData(chunk)) {
stream.pause()
}
})
.on('end', () => {
if (typeof handler.onComplete === 'function') {
handler.onComplete(value.rawTrailers ?? [])
}
})
.on('error', (err) => {
if (!stream.readableEnded && typeof handler.onError === 'function') {
handler.onError(err)
}
})
stream.on('data', function (chunk) {
if (typeof handler.onData === 'function' && !handler.onData(chunk)) {
stream.pause()
}
})
}
} catch (err) {
stream.destroy(err)
Expand Down
Loading