diff options
author | Sebastian <sebasjm@gmail.com> | 2022-12-20 13:44:42 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-12-20 13:44:42 -0300 |
commit | 46607dc2601d6d7835d2112185c88de8fafc2b49 (patch) | |
tree | 936c5122e6b46a358a449a37b99fac41821877c2 /packages/taler-wallet-webextension | |
parent | 15d76cf77c95d1b11d37402087b1d83b8c13462a (diff) | |
download | wallet-core-46607dc2601d6d7835d2112185c88de8fafc2b49.tar.xz |
handler request timeout
Diffstat (limited to 'packages/taler-wallet-webextension')
-rw-r--r-- | packages/taler-wallet-webextension/src/serviceWorkerHttpLib.ts | 68 |
1 files changed, 48 insertions, 20 deletions
diff --git a/packages/taler-wallet-webextension/src/serviceWorkerHttpLib.ts b/packages/taler-wallet-webextension/src/serviceWorkerHttpLib.ts index 74c7f161d..c9327b8e6 100644 --- a/packages/taler-wallet-webextension/src/serviceWorkerHttpLib.ts +++ b/packages/taler-wallet-webextension/src/serviceWorkerHttpLib.ts @@ -17,7 +17,11 @@ /** * Imports. */ -import { RequestThrottler, TalerErrorCode } from "@gnu-taler/taler-util"; +import { + Logger, + RequestThrottler, + TalerErrorCode, +} from "@gnu-taler/taler-util"; import { Headers, HttpRequestLibrary, @@ -41,6 +45,7 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary { const requestMethod = options?.method ?? "GET"; const requestBody = options?.body; const requestHeader = options?.headers; + const requestTimeout = options?.timeout ?? { d_ms: 2 * 1000 }; if (this.throttlingEnabled && this.throttle.applyThrottle(requestUrl)) { const parsedUrl = new URL(requestUrl); @@ -70,26 +75,49 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary { } } - const response = await fetch(requestUrl, { - headers: requestHeader, - body: myBody, - method: requestMethod, - // timeout: options?.timeout - }); + const controller = new AbortController(); + let timeoutId: any | undefined; + if (requestTimeout.d_ms !== "forever") { + timeoutId = setTimeout(() => { + controller.abort(TalerErrorCode.WALLET_HTTP_REQUEST_GENERIC_TIMEOUT); + }, requestTimeout.d_ms); + } - const headerMap = new Headers(); - response.headers.forEach((value, key) => { - headerMap.set(key, value); - }); - return { - headers: headerMap, - status: response.status, - requestMethod, - requestUrl, - json: makeJsonHandler(response, requestUrl), - text: makeTextHandler(response, requestUrl), - bytes: async () => (await response.blob()).arrayBuffer(), - }; + try { + const response = await fetch(requestUrl, { + headers: requestHeader, + body: myBody, + method: requestMethod, + signal: controller.signal, + }); + + if (timeoutId) { + clearTimeout(timeoutId); + } + + const headerMap = new Headers(); + response.headers.forEach((value, key) => { + headerMap.set(key, value); + }); + return { + headers: headerMap, + status: response.status, + requestMethod, + requestUrl, + json: makeJsonHandler(response, requestUrl), + text: makeTextHandler(response, requestUrl), + bytes: async () => (await response.blob()).arrayBuffer(), + }; + } catch (e) { + if (controller.signal) { + throw TalerError.fromDetail( + controller.signal.reason, + {}, + `request to ${requestUrl} timed out`, + ); + } + throw e; + } } get(url: string, opt?: HttpRequestOptions): Promise<HttpResponse> { |