summaryrefslogtreecommitdiff
path: root/packages/web-util/src/utils/http-impl.sw.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/web-util/src/utils/http-impl.sw.ts')
-rw-r--r--packages/web-util/src/utils/http-impl.sw.ts41
1 files changed, 13 insertions, 28 deletions
diff --git a/packages/web-util/src/utils/http-impl.sw.ts b/packages/web-util/src/utils/http-impl.sw.ts
index 2ae4ccd86..316b75dfd 100644
--- a/packages/web-util/src/utils/http-impl.sw.ts
+++ b/packages/web-util/src/utils/http-impl.sw.ts
@@ -39,7 +39,7 @@ import {
* An implementation of the [[HttpRequestLibrary]] using the
* browser's XMLHttpRequest.
*/
-export class ServiceWorkerHttpLib implements HttpRequestLibrary {
+export class BrowserFetchHttpLib implements HttpRequestLibrary {
private throttle = new RequestThrottler();
private throttlingEnabled = true;
private requireTls = false;
@@ -58,6 +58,7 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary {
const requestHeader = options?.headers;
const requestTimeout =
options?.timeout ?? Duration.fromMilliseconds(DEFAULT_REQUEST_TIMEOUT_MS);
+ const requestCancel = options?.cancellationToken;
const parsedUrl = new URL(requestUrl);
if (this.throttlingEnabled && this.throttle.applyThrottle(requestUrl)) {
@@ -82,8 +83,10 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary {
);
}
- let myBody: ArrayBuffer | undefined =
- requestMethod === "POST" ? encodeBody(requestBody) : undefined;
+ const myBody: ArrayBuffer | undefined =
+ requestMethod === "POST" || requestMethod === "PUT" || requestMethod === "PATCH"
+ ? encodeBody(requestBody)
+ : undefined;
const requestHeadersMap = getDefaultHeaders(requestMethod);
if (requestHeader) {
@@ -94,12 +97,17 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary {
}
const controller = new AbortController();
- let timeoutId: any | undefined;
+ let timeoutId: ReturnType<typeof setTimeout> | undefined;
if (requestTimeout.d_ms !== "forever") {
timeoutId = setTimeout(() => {
- controller.abort(TalerErrorCode.WALLET_HTTP_REQUEST_GENERIC_TIMEOUT);
+ controller.abort(TalerErrorCode.GENERIC_TIMEOUT);
}, requestTimeout.d_ms);
}
+ if (requestCancel) {
+ requestCancel.onCancelled(() => {
+ controller.abort(TalerErrorCode.GENERIC_CLIENT_INTERNAL_ERROR)
+ });
+ }
try {
const response = await fetch(requestUrl, {
@@ -142,29 +150,6 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary {
}
}
- get(url: string, opt?: HttpRequestOptions): Promise<HttpResponse> {
- return this.fetch(url, {
- method: "GET",
- ...opt,
- });
- }
-
- postJson(
- url: string,
- body: any,
- opt?: HttpRequestOptions,
- ): Promise<HttpResponse> {
- return this.fetch(url, {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify(body),
- ...opt,
- });
- }
-
- stop(): void {
- // Nothing to do
- }
}
function makeTextHandler(