got
hangs on revalidated cached response. That is, a response gets revalidated because max-age
is reached, while the cached body is still valid (e.g. matching etag
).
Actually, it seems like there's already a test, currently marked as failing, exactly covering this case:
Line 476 in 3822412
I've tracked the issue down to
Line 879 in 3822412
Line 842 in 3822412
I think the issue here is that the response (coming from cache, thus no actual response) never finishes, so getStreamAsBuffer
waits indefinitely.
At first, I thought a fix could be as simple as
diff --git a/source/core/index.ts b/source/core/index.ts
index 36e5cf8..75b4dad 100644
--- a/source/core/index.ts
+++ b/source/core/index.ts
@@ -834,9 +834,13 @@ export default class Request extends Duplex implements RequestEvents<Request> {
response.pause();
});
- response.once('end', () => {
- this.push(null);
- });
+ if (this._isFromCache) {
+ this.push(null);
+ } else {
+ response.once('end', () => {
+ this.push(null);
+ });
+ }
if (this._noPipe) {
const success = await this._setRawBody();
While that allows the promise to be resolved, it caused other test cases to fail.
I'm afraid I'm too unfamiliar with the code base to come up with a suitable solution, but happy to help if pointed in the right direction.
Hangs indefinitely, respectively crashes the process if used in a top-level await.
Should return revalidated cached response
import got from 'got';
class SimpleCache {
cached;
get() {
return this.cached;
}
set(_, value) {
const entry = JSON.parse(value);
// To force immediate revalidation
entry.value.cachePolicy.rescc['max-age'] = '0';
this.cached = JSON.stringify(entry);
}
}
const cache = new SimpleCache();
// npm registry is a good showcase
const url = 'https://registry.npmjs.org/got';
let response = await got(url, { cache });
console.log(response.isFromCache);
response = await got(url, { cache });
console.log(response.isFromCache);
Run with:
node test.js; echo $?
Because it's a top-level await, Node.js correctly detects that the promise will never get resolved (exit code 13):
false
13
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too