Skip to content

Commit

Permalink
direct fetch: when cancelling due to redirect, read body fully to avo…
Browse files Browse the repository at this point in the history
…id possible exception due to encoding

fixes #687
  • Loading branch information
ikreymer committed Sep 17, 2024
1 parent da44257 commit eeef8d9
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/util/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1449,14 +1449,6 @@ class AsyncFetcher {

const headers = reqresp.getRequestHeadersDict();

let signal = null;
let abort = null;

if (this.filter) {
abort = new AbortController();
signal = abort.signal;
}

const dispatcher = getGlobalDispatcher().compose((dispatch) => {
return (opts, handler) => {
if (opts.headers) {
Expand All @@ -1470,13 +1462,18 @@ class AsyncFetcher {
method,
headers,
body: reqresp.postData || undefined,
signal,
redirect: this.manualRedirect ? "manual" : "follow",
dispatcher,
});

if (this.filter && !this.filter(resp) && abort) {
abort.abort();
if (this.filter && !this.filter(resp)) {
// if redirect and cancelled, read whole buffer to avoid possible node error event
if (resp.status >= 300 && resp.status < 400) {
await resp.arrayBuffer();
} else {
// otherwise, just cancel
resp.body?.cancel().catch(() => {});
}
throw new Error("response-filtered-out");
}

Expand Down

0 comments on commit eeef8d9

Please sign in to comment.