Skip to content
Snippets Groups Projects
Unverified Commit b9be8d4d authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

fix(http): Destroy some got responses explicitly to avoid onCancel errors (#12533)

parent ca45a2c2
No related branches found
No related tags found
No related merge requests found
// Renovate issue: https://github.com/renovatebot/renovate/issues/12127
// Got issue: https://github.com/sindresorhus/got/issues/1489
// From here: https://github.com/sindresorhus/got/issues/1489#issuecomment-805485731
import type { Hooks, Response } from 'got';
function isResponseOk(response: Response): boolean {
const { statusCode } = response;
const limitStatusCode = response.request.options.followRedirect ? 299 : 399;
return (
(statusCode >= 200 && statusCode <= limitStatusCode) || statusCode === 304
);
}
export const hooks: Hooks = {
afterResponse: [
(response: Response): Response => {
if (isResponseOk(response)) {
response.request.destroy();
}
return response;
},
],
};
......@@ -8,6 +8,7 @@ import * as memCache from '../cache/memory';
import { clone } from '../clone';
import { resolveBaseUrl } from '../url';
import { applyAuthorization, removeAuthorization } from './auth';
import { hooks } from './hooks';
import { applyHostRules } from './host-rules';
import { getQueue } from './queue';
import type {
......@@ -98,7 +99,7 @@ async function gotRoutine<T>(
// Cheat the TS compiler using `as` to pick a specific overload.
// Otherwise it doesn't typecheck.
const resp = await got<T>(url, options as GotJSONOptions);
const resp = await got<T>(url, { ...options, hooks } as GotJSONOptions);
const duration =
resp.timings.phases.total || /* istanbul ignore next: can't be tested */ 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment