@applitools/req
Advanced tools
Comparing version 1.4.0 to 1.5.0
# Changelog | ||
## [1.5.0](https://github.com/applitools/eyes.sdk.javascript1/compare/js/req@1.4.0...js/req@1.5.0) (2023-07-18) | ||
### Features | ||
* support retries on stuck requests ([be673bb](https://github.com/applitools/eyes.sdk.javascript1/commit/be673bb505c9b21d6aea37d86e88513e95e3cb02)) | ||
## [1.4.0](https://github.com/applitools/eyes.sdk.javascript1/compare/js/req@1.3.3...js/req@1.4.0) (2023-07-05) | ||
@@ -4,0 +11,0 @@ |
import { AbortController } from 'abort-controller'; | ||
import { stop } from './stop.js'; | ||
import { makeAgent } from './agent.js'; | ||
import { AbortCode, RequestTimeoutError, ConnectionTimeoutError } from './req-errors.js'; | ||
import globalFetch, { Request, Headers, Response } from 'node-fetch'; | ||
@@ -15,2 +16,3 @@ import * as utils from '@applitools/utils'; | ||
const options = mergeOptions({}, ...requestOptions); | ||
let abortCode; | ||
if (options.hooks) | ||
@@ -22,4 +24,9 @@ options.hooks = utils.types.isArray(options.hooks) ? options.hooks : [options.hooks]; | ||
options.headers = Object.fromEntries(Object.entries(options.headers).filter(([_, value]) => value)); | ||
const controller = new AbortController(); | ||
const timeout = options.timeout ? setTimeout(() => controller.abort(), options.timeout) : null; | ||
const connectionController = new AbortController(); | ||
const connectionTimer = options.connectionTimeout | ||
? setTimeout(() => { | ||
abortCode = AbortCode.connectionTimeout; | ||
connectionController.abort(); | ||
}, options.connectionTimeout) | ||
: null; | ||
try { | ||
@@ -29,4 +36,4 @@ return await req(input, options); | ||
finally { | ||
if (timeout) | ||
clearTimeout(timeout); | ||
if (connectionTimer) | ||
clearTimeout(connectionTimer); | ||
} | ||
@@ -36,4 +43,17 @@ async function req(input, options) { | ||
const fetch = (_a = options.fetch) !== null && _a !== void 0 ? _a : globalFetch; | ||
if (options.signal) | ||
options.signal.onabort = () => controller.abort(); | ||
const requestController = new AbortController(); | ||
const requestTimer = options.requestTimeout | ||
? setTimeout(() => { | ||
abortCode !== null && abortCode !== void 0 ? abortCode : (abortCode = AbortCode.requestTimeout); | ||
requestController.abort(); | ||
}, options.requestTimeout) | ||
: null; | ||
if (connectionController.signal.aborted) | ||
requestController.abort(); | ||
connectionController.signal.onabort = () => requestController.abort(); | ||
if (options.signal) { | ||
if (options.signal.aborted) | ||
requestController.abort(); | ||
options.signal.onabort = () => requestController.abort(); | ||
} | ||
const url = new URL(String((_b = input.url) !== null && _b !== void 0 ? _b : input), options.baseUrl); | ||
@@ -61,3 +81,3 @@ if (options.query) { | ||
agent: makeAgent({ proxy: options.proxy, useDnsCache: options.useDnsCache }), | ||
signal: controller.signal, | ||
signal: requestController.signal, | ||
}); | ||
@@ -94,2 +114,6 @@ request = await beforeRequest({ request, options }); | ||
catch (error) { | ||
if (abortCode === AbortCode.requestTimeout) | ||
error = new RequestTimeoutError(); | ||
else if (abortCode === AbortCode.connectionTimeout) | ||
error = new ConnectionTimeoutError(); | ||
// if the request has to be retried due to network error | ||
@@ -123,2 +147,4 @@ const retry = await ((_k = options.retry) === null || _k === void 0 ? void 0 : _k.reduce((prev, retry) => { | ||
options.signal.onabort = null; | ||
if (requestTimer) | ||
clearTimeout(requestTimer); | ||
} | ||
@@ -125,0 +151,0 @@ } |
{ | ||
"name": "@applitools/req", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Applitools fetch-based request library", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -95,3 +95,4 @@ export type Stop = typeof stop; | ||
useDnsCache?: undefined | boolean; | ||
timeout?: undefined | number; | ||
connectionTimeout?: undefined | number; | ||
requestTimeout?: undefined | number; | ||
retry?: undefined | Retry | Array<Retry>; | ||
@@ -98,0 +99,0 @@ hooks?: undefined | Hooks<this> | Array<Hooks<this>>; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
447865
13
11000