Skip to content

Commit 883927b

Browse files
Js-Brechtarcanis
authored andcommitted
Added hook to address 400 error returned during redirect (#329)
* Added redirect+response hook During tarball resolution, some redirects would return 400 error if authentication headers are present. These hooks will intercept a 400 error and remove the authorization header. * Removed redir caching. Force delete authentication header on redirect * Decreases the amount of ts-ignore
1 parent 1340738 commit 883927b

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

packages/berry-core/sources/httpUtils.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import HttpAgent, {HttpsAgent} from 'agentkeepalive';
2-
import got from 'got';
2+
import got, {GotOptions} from 'got';
33
import tunnel, {ProxyOptions} from 'tunnel';
44
import {URL} from 'url';
55

@@ -59,9 +59,25 @@ async function request(target: string, body: Body, {configuration, headers, json
5959
? tunnel.httpsOverHttp(parseProxy(httpsProxy))
6060
: globalHttpsAgent;
6161

62-
// @ts-ignore
63-
const res = await got(target, {agent, body, headers, json, method, encoding: null});
64-
62+
const gotOptions = {agent, body, headers, json, method, encoding: null};
63+
64+
const makeHooks = <T extends string | null>() => ({
65+
beforeRedirect: [
66+
(options: GotOptions<T>) => {
67+
if (options.headers && options.headers.authorization) {
68+
delete options.headers.authorization;
69+
}
70+
},
71+
],
72+
});
73+
74+
//@ts-ignore
75+
const gotClient = got.extend({
76+
...gotOptions,
77+
hooks: makeHooks(),
78+
});
79+
80+
const res = await gotClient(target);
6581
return await res.body;
6682
}
6783

0 commit comments

Comments
 (0)