diff --git a/src/index.js b/src/index.js index 86394dc..7374d3c 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,13 @@ const { customOptionsHandler } = require('./handlers/custom-options'); const { browserHeadersHandler } = require('./handlers/browser-headers'); const { proxyHandler } = require('./handlers/proxy'); +const isResponseOk = (response) => { + const { statusCode } = response; + const limitStatusCode = response.request.options.followRedirect ? 299 : 399; + + return (statusCode >= 200 && statusCode <= limitStatusCode) || statusCode === 304; +}; + const mutableGot = got.extend({ // Must be mutable in order to override the defaults // https://github.com/sindresorhus/got#instances @@ -16,6 +23,18 @@ const mutableGot = got.extend({ // Custom header generator instance. headerGenerator: new HeaderGenerator(), }, + // Got has issues with terminating requests and it can cause unhandled exceptions + hooks: { + afterResponse: [ + (response) => { + if (isResponseOk(response)) { + response.request.destroy(); + } + + return response; + }, + ], + }, }); // Overriding the mutableGot defaults by merging its defaults and our scraping defaults.