From 659e9cdbe6defd54d0f4713bb230e4a156262a2c Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 2 Sep 2020 12:23:11 +0530 Subject: respect cache header --- packages/taler-wallet-core/src/util/http.ts | 18 +++++++++++++++++- packages/taler-wallet-core/src/util/time.ts | 4 ++++ 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'packages/taler-wallet-core/src/util') diff --git a/packages/taler-wallet-core/src/util/http.ts b/packages/taler-wallet-core/src/util/http.ts index 58b04d455..0977b429e 100644 --- a/packages/taler-wallet-core/src/util/http.ts +++ b/packages/taler-wallet-core/src/util/http.ts @@ -26,7 +26,7 @@ import { Codec } from "./codec"; import { OperationFailedError, makeErrorDetails } from "../operations/errors"; import { TalerErrorCode } from "../TalerErrorCode"; import { Logger } from "./logging"; -import { Duration } from "./time"; +import { Duration, Timestamp, getTimestampNow } from "./time"; const logger = new Logger("http.ts"); @@ -253,3 +253,19 @@ export async function readSuccessResponseTextOrThrow( } throwUnexpectedRequestError(httpResponse, r.talerErrorResponse); } + +/** + * Get the timestamp at which the response's content is considered expired. + */ +export function getExpiryTimestamp(httpResponse: HttpResponse): Timestamp { + const expiryDateMs = new Date( + httpResponse.headers.get("expiry") ?? "", + ).getTime(); + if (Number.isNaN(expiryDateMs)) { + return getTimestampNow(); + } else { + return { + t_ms: expiryDateMs, + } + } +} diff --git a/packages/taler-wallet-core/src/util/time.ts b/packages/taler-wallet-core/src/util/time.ts index ccd75e14b..ff4c1885b 100644 --- a/packages/taler-wallet-core/src/util/time.ts +++ b/packages/taler-wallet-core/src/util/time.ts @@ -46,6 +46,10 @@ export function getTimestampNow(): Timestamp { }; } +export function isTimestampExpired(t: Timestamp) { + return timestampCmp(t, getTimestampNow()) <= 0; +} + export function getDurationRemaining( deadline: Timestamp, now = getTimestampNow(), -- cgit v1.2.3