Skip to content

Commit d57c7d8

Browse files
committed
fix: avoid mutating frozen Hit objects in cacheOnFail path
1 parent 552e6ff commit d57c7d8

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/common.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ export class Hit {
3333
this.data = data;
3434
this.etag = existingEtag ?? generateEtag(JSON.stringify(data));
3535
}
36+
37+
withError(error: string, consecutiveErrors: number): Hit {
38+
const hit = new Hit(this.cacheName, this.data, this.etag);
39+
hit.cached = this.cached;
40+
hit.created = this.created;
41+
hit.error = error;
42+
hit.errorTime = new Date();
43+
hit.consecutiveErrors = consecutiveErrors;
44+
return hit;
45+
}
3646
}
3747

3848
export class Miss {

src/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ export class Cacheism {
6363
const errorMessage = err instanceof Error ? err.toString() : String(err);
6464

6565
if (status >= Status.cacheOnFail && hasCache && existing.isHit) {
66-
response = existing;
67-
response.error = errorMessage;
68-
response.errorTime = new Date();
69-
response.consecutiveErrors++;
66+
response = existing.withError(errorMessage, existing.consecutiveErrors + 1);
7067
} else {
7168
response = new Miss(name, errorMessage, existing.consecutiveErrors + 1);
7269
}

0 commit comments

Comments
 (0)