Skip to content

Commit

Permalink
Round ttl to integer ms
Browse files Browse the repository at this point in the history
Fixes #35
  • Loading branch information
kornelski committed Nov 16, 2023
1 parent 2449650 commit eefc726
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ module.exports = class CachePolicy {
}

/**
* Value of applicable max-age (or heuristic equivalent) in seconds. This counts since response's `Date`.
* Possibly outdated value of applicable max-age (or heuristic equivalent) in seconds.
* This counts since response's `Date`.
*
* For an up-to-date value, see `timeToLive()`.
*
Expand Down Expand Up @@ -446,11 +447,16 @@ module.exports = class CachePolicy {
return defaultMinTtl;
}

/**
* Up-to-date `max-age` value, in *milliseconds*.
*
* Prefer this method over `maxAge()`.
*/
timeToLive() {
const age = this.maxAge() - this.age();
const staleIfErrorAge = age + toNumberOrZero(this._rescc['stale-if-error']);
const staleWhileRevalidateAge = age + toNumberOrZero(this._rescc['stale-while-revalidate']);
return Math.max(0, age, staleIfErrorAge, staleWhileRevalidateAge) * 1000;
return Math.round(Math.max(0, age, staleIfErrorAge, staleWhileRevalidateAge) * 1000);
}

stale() {
Expand Down

0 comments on commit eefc726

Please sign in to comment.