Skip to content

Commit 149fcaa

Browse files
committed
refactor and lint fix
1 parent e4ee91d commit 149fcaa

File tree

5 files changed

+24
-29
lines changed

5 files changed

+24
-29
lines changed

packages/cloudflare/src/api/cloudflare-context.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ declare global {
5757
NEXT_CACHE_DO_QUEUE_DISABLE_SQLITE?: string;
5858

5959
// Below are the optional env variables for purging the cache
60-
// Durable Object namespace to use for the durable object queue
60+
// Durable Object namespace to use for the durable object cache purge
6161
NEXT_CACHE_DO_PURGE?: DurableObjectNamespace<BucketCachePurge>;
62-
CACHE_ZONE_ID?: string;
63-
CACHE_API_TOKEN?: string;
64-
CACHE_BUFFER_TIME_IN_SECONDS?: string;
62+
// The amount of time in seconds that the cache purge will wait before purging the cache
63+
NEXT_CACHE_DO_PURGE_BUFFER_TIME_IN_SECONDS?: string;
64+
// The zone ID to use for the cache purge https://developers.cloudflare.com/fundamentals/setup/find-account-and-zone-ids/
65+
CACHE_PURGE_ZONE_ID?: string;
66+
// The API token to use for the cache purge. It should have the `Cache Purge` permission
67+
CACHE_PURGE_API_TOKEN?: string;
6568
}
6669
}
6770

packages/cloudflare/src/api/durable-objects/bucket-cache-purge.spec.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ vi.mock("cloudflare:workers", () => ({
1212
},
1313
}));
1414

15-
1615
const createBucketCachePurge = () => {
1716
const mockState = {
1817
waitUntil: vi.fn(),
@@ -39,7 +38,7 @@ describe("BucketCachePurge", () => {
3938
expect(cache.ctx.blockConcurrencyWhile).toHaveBeenCalled();
4039
// @ts-expect-error - testing private method
4140
expect(cache.ctx.storage.sql.exec).toHaveBeenCalledWith(
42-
expect.stringContaining("CREATE TABLE IF NOT EXISTS cache_purge"),
41+
expect.stringContaining("CREATE TABLE IF NOT EXISTS cache_purge")
4342
);
4443
});
4544

@@ -51,12 +50,12 @@ describe("BucketCachePurge", () => {
5150
// @ts-expect-error - testing private method
5251
expect(cache.ctx.storage.sql.exec).toHaveBeenCalledWith(
5352
expect.stringContaining("INSERT OR REPLACE INTO cache_purge"),
54-
[tags[0]],
53+
[tags[0]]
5554
);
5655
// @ts-expect-error - testing private method
5756
expect(cache.ctx.storage.sql.exec).toHaveBeenCalledWith(
5857
expect.stringContaining("INSERT OR REPLACE INTO cache_purge"),
59-
[tags[1]],
58+
[tags[1]]
6059
);
6160
});
6261

@@ -77,7 +76,7 @@ describe("BucketCachePurge", () => {
7776
// @ts-expect-error - testing private method
7877
expect(cache.ctx.storage.setAlarm).not.toHaveBeenCalled();
7978
});
80-
})
79+
});
8180

8281
describe("alarm", () => {
8382
it("should purge cache by tags and delete them from the sql table", async () => {
@@ -90,7 +89,7 @@ describe("BucketCachePurge", () => {
9089
// @ts-expect-error - testing private method
9190
expect(cache.ctx.storage.sql.exec).toHaveBeenCalledWith(
9291
expect.stringContaining("DELETE FROM cache_purge"),
93-
["tag1", "tag2"],
92+
["tag1", "tag2"]
9493
);
9594
});
9695
it("should not purge cache if no tags are found", async () => {
@@ -103,7 +102,7 @@ describe("BucketCachePurge", () => {
103102
// @ts-expect-error - testing private method
104103
expect(cache.ctx.storage.sql.exec).not.toHaveBeenCalledWith(
105104
expect.stringContaining("DELETE FROM cache_purge"),
106-
[],
105+
[]
107106
);
108107
});
109108

@@ -119,7 +118,7 @@ describe("BucketCachePurge", () => {
119118
expect(internalPurgeCacheByTagsSpy).toHaveBeenCalledWith(
120119
// @ts-expect-error - testing private method
121120
cache.env,
122-
tags,
121+
tags
123122
);
124123
// @ts-expect-error - testing private method 1st is constructor, 2nd is to get the tags and 3rd is to delete them
125124
expect(cache.ctx.storage.sql.exec).toHaveBeenCalledTimes(3);
@@ -137,16 +136,14 @@ describe("BucketCachePurge", () => {
137136
expect(internalPurgeCacheByTagsSpy).toHaveBeenCalledWith(
138137
// @ts-expect-error - testing private method
139138
cache.env,
140-
tags,
139+
tags
141140
);
142141
// @ts-expect-error - testing private method 1st is constructor, 2nd is to get the tags and 3rd is to delete them, 4th is to get the next 100 tags
143142
expect(cache.ctx.storage.sql.exec).toHaveBeenCalledTimes(4);
144143
// @ts-expect-error - testing private method
145144
expect(cache.ctx.storage.sql.exec).toHaveBeenLastCalledWith(
146-
expect.stringContaining("SELECT * FROM cache_purge LIMIT 100"),
145+
expect.stringContaining("SELECT * FROM cache_purge LIMIT 100")
147146
);
148147
});
149-
150-
151-
})
152-
});
148+
});
149+
});

packages/cloudflare/src/api/durable-objects/bucket-cache-purge.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export class BucketCachePurge extends DurableObject<CloudflareEnv> {
99

1010
constructor(state: DurableObjectState, env: CloudflareEnv) {
1111
super(state, env);
12-
this.bufferTimeInSeconds = env.CACHE_BUFFER_TIME_IN_SECONDS
13-
? parseInt(env.CACHE_BUFFER_TIME_IN_SECONDS)
12+
this.bufferTimeInSeconds = env.NEXT_CACHE_DO_PURGE_BUFFER_TIME_IN_SECONDS
13+
? parseInt(env.NEXT_CACHE_DO_PURGE_BUFFER_TIME_IN_SECONDS)
1414
: DEFAULT_BUFFER_TIME; // Default buffer time
1515

1616
// Initialize the sql table if it doesn't exist
@@ -21,7 +21,7 @@ export class BucketCachePurge extends DurableObject<CloudflareEnv> {
2121
);
2222
CREATE UNIQUE INDEX IF NOT EXISTS tag_index ON cache_purge (tag);
2323
`);
24-
})
24+
});
2525
}
2626

2727
async purgeCacheByTags(tags: string[]) {

packages/cloudflare/src/api/overrides/cache-purge/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export const purgeCache = ({ type = "direct" }: PurgeOptions) => {
1717
if (type === "direct") {
1818
await internalPurgeCacheByTags(env, tags);
1919
} else {
20-
console.log("purgeCacheByTags DO", tags);
2120
const durableObject = env.NEXT_CACHE_DO_PURGE;
2221
if (!durableObject) {
2322
debugCache("cdnInvalidation", "No durable object found. Skipping cache purge.");

packages/cloudflare/src/api/overrides/internal.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,18 @@ export async function purgeCacheByTags(tags: string[]) {
5252
}
5353

5454
export async function internalPurgeCacheByTags(env: CloudflareEnv, tags: string[]) {
55-
//TODO: Remove this before commit
56-
console.log("purgeCacheByTags", tags);
57-
58-
if (!env.CACHE_ZONE_ID && !env.CACHE_API_TOKEN) {
55+
if (!env.CACHE_PURGE_ZONE_ID && !env.CACHE_PURGE_API_TOKEN) {
5956
// THIS IS A NO-OP
6057
debugCache("purgeCacheByTags", "No cache zone ID or API token provided. Skipping cache purge.");
6158
return;
6259
}
6360

6461
try {
6562
const response = await fetch(
66-
`https://api.cloudflare.com/client/v4/zones/${env.CACHE_ZONE_ID}/purge_cache`,
63+
`https://api.cloudflare.com/client/v4/zones/${env.CACHE_PURGE_ZONE_ID}/purge_cache`,
6764
{
6865
headers: {
69-
Authorization: `Bearer ${env.CACHE_API_TOKEN}`,
66+
Authorization: `Bearer ${env.CACHE_PURGE_ZONE_ID}`,
7067
"Content-Type": "application/json",
7168
},
7269
method: "POST",
@@ -82,7 +79,6 @@ export async function internalPurgeCacheByTags(env: CloudflareEnv, tags: string[
8279
const bodyResponse = (await response.json()) as {
8380
success: boolean;
8481
errors: Array<{ code: number; message: string }>;
85-
messages: Array<{ code: number; message: string }>;
8682
};
8783
if (!bodyResponse.success) {
8884
throw new Error(`Failed to purge cache: ${JSON.stringify(bodyResponse.errors)}`);

0 commit comments

Comments
 (0)