aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-core/src/headless
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2022-03-29 13:47:32 +0200
committerFlorian Dold <florian@dold.me>2022-03-29 13:48:51 +0200
commitc265e7d019d445add2d2cfb7cfcbdeee059684d3 (patch)
tree6904e9cdb723992728ed511103be65094ca50034 /packages/taler-wallet-core/src/headless
parentbe489b6b3ea4214f546dfc33d6bb0f39ce82b5ab (diff)
downloadwallet-core-c265e7d019d445add2d2cfb7cfcbdeee059684d3.tar.xz
wallet: make retries more robust and consistent
Diffstat (limited to 'packages/taler-wallet-core/src/headless')
-rw-r--r--packages/taler-wallet-core/src/headless/NodeHttpLib.ts20
1 files changed, 16 insertions, 4 deletions
diff --git a/packages/taler-wallet-core/src/headless/NodeHttpLib.ts b/packages/taler-wallet-core/src/headless/NodeHttpLib.ts
index df25a1092..5290bd441 100644
--- a/packages/taler-wallet-core/src/headless/NodeHttpLib.ts
+++ b/packages/taler-wallet-core/src/headless/NodeHttpLib.ts
@@ -20,6 +20,7 @@
* Imports.
*/
import {
+ DEFAULT_REQUEST_TIMEOUT_MS,
Headers,
HttpRequestLibrary,
HttpRequestOptions,
@@ -65,13 +66,16 @@ export class NodeHttpLib implements HttpRequestLibrary {
`request to origin ${parsedUrl.origin} was throttled`,
);
}
- let timeout: number | undefined;
+ let timeoutMs: number | undefined;
if (typeof opt?.timeout?.d_ms === "number") {
- timeout = opt.timeout.d_ms;
+ timeoutMs = opt.timeout.d_ms;
+ } else {
+ timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS;
}
+ // FIXME: Use AbortController / etc. to handle cancellation
let resp: AxiosResponse;
try {
- resp = await Axios({
+ let respPromise = Axios({
method,
url: url,
responseType: "arraybuffer",
@@ -79,9 +83,13 @@ export class NodeHttpLib implements HttpRequestLibrary {
validateStatus: () => true,
transformResponse: (x) => x,
data: body,
- timeout,
+ timeout: timeoutMs,
maxRedirects: 0,
});
+ if (opt?.cancellationToken) {
+ respPromise = opt.cancellationToken.racePromise(respPromise);
+ }
+ resp = await respPromise;
} catch (e: any) {
throw TalerError.fromDetail(
TalerErrorCode.WALLET_NETWORK_ERROR,
@@ -94,11 +102,13 @@ export class NodeHttpLib implements HttpRequestLibrary {
}
const makeText = async (): Promise<string> => {
+ opt?.cancellationToken?.throwIfCancelled();
const respText = new Uint8Array(resp.data);
return bytesToString(respText);
};
const makeJson = async (): Promise<any> => {
+ opt?.cancellationToken?.throwIfCancelled();
let responseJson;
const respText = await makeText();
try {
@@ -130,6 +140,7 @@ export class NodeHttpLib implements HttpRequestLibrary {
return responseJson;
};
const makeBytes = async () => {
+ opt?.cancellationToken?.throwIfCancelled();
if (typeof resp.data.byteLength !== "number") {
throw Error("expected array buffer");
}
@@ -150,6 +161,7 @@ export class NodeHttpLib implements HttpRequestLibrary {
bytes: makeBytes,
};
}
+
async get(url: string, opt?: HttpRequestOptions): Promise<HttpResponse> {
return this.fetch(url, {
method: "GET",