@comunica/actor-http-fetch
Advanced tools
Comparing version 2.4.3 to 2.5.0
@@ -16,2 +16,12 @@ import type { IActionHttp, IActorHttpOutput, IActorHttpArgs } from '@comunica/bus-http'; | ||
test(action: IActionHttp): Promise<IMediatorTypeTime>; | ||
/** | ||
* Perform a fetch request, taking care of retries | ||
* @param fetchFn | ||
* @param requestInput Url or RequestInfo to pass to fetchFn | ||
* @param requestInit RequestInit to pass to fetch function | ||
* @param retryCount Maximum retries after which to abort | ||
* @param retryDelay Time in milliseconds to wait between retries | ||
* @returns a fetch `Response` object | ||
*/ | ||
private static getResponse; | ||
run(action: IActionHttp): Promise<IActorHttpOutput>; | ||
@@ -18,0 +28,0 @@ } |
@@ -27,2 +27,52 @@ "use strict"; | ||
} | ||
/** | ||
* Perform a fetch request, taking care of retries | ||
* @param fetchFn | ||
* @param requestInput Url or RequestInfo to pass to fetchFn | ||
* @param requestInit RequestInit to pass to fetch function | ||
* @param retryCount Maximum retries after which to abort | ||
* @param retryDelay Time in milliseconds to wait between retries | ||
* @returns a fetch `Response` object | ||
*/ | ||
static async getResponse(fetchFn, requestInput, requestInit, retryCount, retryDelay, throwOnServerError) { | ||
let lastError; | ||
// The retryCount is 0-based. Therefore, add 1 to triesLeft. | ||
let triesLeft = retryCount + 1; | ||
// When retry count is greater than 0, repeat fetch. | ||
while (triesLeft-- > 0) { | ||
try { | ||
const response = await fetchFn(requestInput, requestInit); | ||
// Check, if server sent a 5xx error response. | ||
if (throwOnServerError && response.status >= 500 && response.status < 600) { | ||
throw new Error(`Server replied with response code ${response.status}: ${response.statusText}`); | ||
} | ||
return response; | ||
} | ||
catch (error) { | ||
lastError = error; | ||
// If the fetch was aborted by timeout, we won't retry. | ||
if (requestInit.signal?.aborted) { | ||
throw error; | ||
} | ||
if (triesLeft > 0) { | ||
// Wait for specified delay, before retrying. | ||
await new Promise((resolve, reject) => { | ||
setTimeout(resolve, retryDelay); | ||
// Cancel waiting, if timeout is reached. | ||
requestInit.signal?.addEventListener('abort', () => { | ||
reject(new Error('Fetch aborted by timeout.')); | ||
}); | ||
}); | ||
} | ||
} | ||
} | ||
// The fetch was not successful. We throw. | ||
if (retryCount > 0) { | ||
// Feedback the last error, if there were retry attempts. | ||
throw new Error(`Number of fetch retries (${retryCount}) exceeded. Last error: ${String(lastError)}`); | ||
} | ||
else { | ||
throw lastError; | ||
} | ||
} | ||
async run(action) { | ||
@@ -66,6 +116,10 @@ // Prepare headers | ||
requestInit = await this.fetchInitPreprocessor.handle(requestInit); | ||
// Perform request | ||
// Number of retries to perform after a failed fetch. | ||
const retryCount = action.context?.get(context_entries_1.KeysHttp.httpRetryCount) ?? 0; | ||
const retryDelay = action.context?.get(context_entries_1.KeysHttp.httpRetryDelay) ?? 0; | ||
const retryOnSeverError = action.context?.get(context_entries_1.KeysHttp.httpRetryOnServerError) ?? false; | ||
const customFetch = action | ||
.context?.get(context_entries_1.KeysHttp.fetch); | ||
const response = await (customFetch || fetch)(action.input, requestInit); | ||
// Execute the fetch (with retries and timeouts, if applicable). | ||
const response = await ActorHttpFetch.getResponse(customFetch || fetch, action.input, requestInit, retryCount, retryDelay, retryOnSeverError); | ||
// We remove or update the timeout | ||
@@ -72,0 +126,0 @@ if (requestTimeout !== undefined) { |
{ | ||
"name": "@comunica/actor-http-fetch", | ||
"version": "2.4.3", | ||
"version": "2.5.0", | ||
"description": "A node-fetch http actor", | ||
@@ -29,8 +29,9 @@ "lsd:module": true, | ||
"lib/**/*.d.ts", | ||
"lib/**/*.js" | ||
"lib/**/*.js", | ||
"lib/**/*.js.map" | ||
], | ||
"dependencies": { | ||
"@comunica/bus-http": "^2.4.0", | ||
"@comunica/context-entries": "^2.4.0", | ||
"@comunica/mediatortype-time": "^2.4.0", | ||
"@comunica/bus-http": "^2.5.0", | ||
"@comunica/context-entries": "^2.5.0", | ||
"@comunica/mediatortype-time": "^2.5.0", | ||
"abort-controller": "^3.0.0", | ||
@@ -47,3 +48,3 @@ "cross-fetch": "^3.0.5" | ||
}, | ||
"gitHead": "8d1e8773e976da66eefbe224f5ffa19e81614ebc" | ||
"gitHead": "349d57f5d1e539200e980bdff96973c2e0b66caa" | ||
} |
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
46681
20
319