Skip to content

Commit 232a690

Browse files
committed
refactor: add explicit TypeScript return type annotations
1 parent 7820ad0 commit 232a690

39 files changed

Lines changed: 110 additions & 78 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ async function getCloudflareContextAsync<
245245
* Note: this function should only be called inside the Next.js config file, and although async it doesn't need to be `await`ed
246246
* @param options options on how the function should operate and if/where to persist the platform data
247247
*/
248-
export async function initOpenNextCloudflareForDev(options?: GetPlatformProxyOptions) {
248+
export async function initOpenNextCloudflareForDev(options?: GetPlatformProxyOptions): Promise<void> {
249249
const shouldInitializationRun = shouldContextInitializationRun();
250250
if (!shouldInitializationRun) return;
251251

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class BucketCachePurge extends DurableObject<CloudflareEnv> {
2626
});
2727
}
2828

29-
async purgeCacheByTags(tags: string[]) {
29+
async purgeCacheByTags(tags: string[]): Promise<void> {
3030
for (const tag of tags) {
3131
// Insert the tag into the sql table
3232
this.ctx.storage.sql.exec(
@@ -43,7 +43,7 @@ export class BucketCachePurge extends DurableObject<CloudflareEnv> {
4343
}
4444
}
4545

46-
override async alarm() {
46+
override async alarm(): Promise<void> {
4747
let tags = this.ctx.storage.sql
4848
.exec<{ tag: string }>(
4949
`

packages/cloudflare/src/api/durable-objects/queue.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ export class DOQueueHandler extends DurableObject<CloudflareEnv> {
2323
// Ongoing revalidations are deduped by the deduplication id
2424
// Since this is running in waitUntil, we expect the durable object state to persist this during the duration of the revalidation
2525
// TODO: handle incremental cache with only eventual consistency (i.e. KV or R2/D1 with the optional cache layer on top)
26-
ongoingRevalidations = new Map<string, Promise<void>>();
26+
ongoingRevalidations: Map<string, Promise<void>> = new Map<string, Promise<void>>();
2727

2828
sql: SqlStorage;
2929

30-
routeInFailedState = new Map<string, FailedState>();
30+
routeInFailedState: Map<string, FailedState> = new Map<string, FailedState>();
3131

3232
service: NonNullable<CloudflareEnv["WORKER_SELF_REFERENCE"]>;
3333

@@ -72,7 +72,7 @@ export class DOQueueHandler extends DurableObject<CloudflareEnv> {
7272
debug(`Durable object initialized`);
7373
}
7474

75-
async revalidate(msg: QueueMessage) {
75+
async revalidate(msg: QueueMessage): Promise<void> {
7676
if (this.ongoingRevalidations.size > 2 * this.maxRevalidations) {
7777
warn(
7878
`Your durable object has 2 times the maximum number of revalidations (${this.maxRevalidations}) in progress. If this happens often, you should consider increasing the NEXT_CACHE_DO_QUEUE_MAX_REVALIDATION or the number of durable objects with the MAX_REVALIDATE_CONCURRENCY env var.`
@@ -110,7 +110,7 @@ export class DOQueueHandler extends DurableObject<CloudflareEnv> {
110110
this.ctx.waitUntil(revalidationPromise);
111111
}
112112

113-
async executeRevalidation(msg: QueueMessage) {
113+
async executeRevalidation(msg: QueueMessage): Promise<void> {
114114
let response: Response | undefined;
115115
try {
116116
debug(`Revalidating ${msg.MessageBody.host}${msg.MessageBody.url}`);
@@ -187,7 +187,7 @@ export class DOQueueHandler extends DurableObject<CloudflareEnv> {
187187
}
188188
}
189189

190-
override async alarm() {
190+
override async alarm(): Promise<void> {
191191
const currentDateTime = Date.now();
192192
// We fetch the first event that needs to be retried or if the date is expired
193193
const nextEventToRetry = Array.from(this.routeInFailedState.values())
@@ -204,7 +204,7 @@ export class DOQueueHandler extends DurableObject<CloudflareEnv> {
204204
}
205205
}
206206

207-
async addToFailedState(msg: QueueMessage) {
207+
async addToFailedState(msg: QueueMessage): Promise<void> {
208208
debug(`Adding ${msg.MessageBody.host}${msg.MessageBody.url} to the failed state`);
209209
const existingFailedState = this.routeInFailedState.get(msg.MessageDeduplicationId);
210210

@@ -245,7 +245,7 @@ export class DOQueueHandler extends DurableObject<CloudflareEnv> {
245245
await this.addAlarm();
246246
}
247247

248-
async addAlarm() {
248+
async addAlarm(): Promise<void> {
249249
const existingAlarm = await this.ctx.storage.getAlarm({ allowConcurrency: false });
250250
if (existingAlarm) return;
251251
if (this.routeInFailedState.size === 0) return;
@@ -263,7 +263,7 @@ export class DOQueueHandler extends DurableObject<CloudflareEnv> {
263263
// This function is used to restore the state of the durable object
264264
// We don't restore the ongoing revalidations because we cannot know in which state they are
265265
// We only restore the failed state and the alarm
266-
async initState() {
266+
async initState(): Promise<void> {
267267
if (this.disableSQLite) return;
268268
// We store the failed state as a blob, we don't want to do anything with it anyway besides restoring
269269
this.sql.exec("CREATE TABLE IF NOT EXISTS failed_state (id TEXT PRIMARY KEY, data TEXT, buildId TEXT)");
@@ -290,7 +290,7 @@ export class DOQueueHandler extends DurableObject<CloudflareEnv> {
290290
* @param msg
291291
* @returns `true` if the route has been revalidated since the lastModified from the message, `false` otherwise
292292
*/
293-
checkSyncTable(msg: QueueMessage) {
293+
checkSyncTable(msg: QueueMessage): boolean {
294294
try {
295295
if (this.disableSQLite) return false;
296296
return (

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface PurgeOptions {
88
type: "durableObject" | "direct";
99
}
1010

11-
export const purgeCache = ({ type = "direct" }: PurgeOptions) => {
11+
export const purgeCache = ({ type = "direct" }: PurgeOptions): CDNInvalidationHandler => {
1212
return {
1313
name: "cloudflare",
1414
async invalidatePaths(paths) {
@@ -29,7 +29,7 @@ export const purgeCache = ({ type = "direct" }: PurgeOptions) => {
2929
}
3030
debugCache("cdnInvalidation", "Invalidated paths:", tags);
3131
},
32-
} satisfies CDNInvalidationHandler;
32+
};
3333
};
3434

3535
export default purgeCache;

packages/cloudflare/src/api/overrides/incremental-cache/kv-incremental-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const PREFIX_ENV_NAME = "NEXT_INC_CACHE_KV_PREFIX";
2727
* when the constructor is called.
2828
*/
2929
class KVIncrementalCache implements IncrementalCache {
30-
readonly name = NAME;
30+
readonly name: string = NAME;
3131

3232
async get<CacheType extends CacheEntryType = "cache">(
3333
key: string,

packages/cloudflare/src/api/overrides/incremental-cache/r2-incremental-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const PREFIX_ENV_NAME = "NEXT_INC_CACHE_R2_PREFIX";
2424
* environment variable, and defaults to `incremental-cache`.
2525
*/
2626
class R2IncrementalCache implements IncrementalCache {
27-
readonly name = NAME;
27+
readonly name: string = NAME;
2828

2929
async get<CacheType extends CacheEntryType = "cache">(
3030
key: string,

packages/cloudflare/src/api/overrides/incremental-cache/regional-cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class RegionalCache implements IncrementalCache {
189189
return this.localCache;
190190
}
191191

192-
protected getCacheUrlKey(key: string, cacheType?: CacheEntryType) {
192+
protected getCacheUrlKey(key: string, cacheType?: CacheEntryType): string {
193193
const buildId = process.env.NEXT_BUILD_ID ?? FALLBACK_BUILD_ID;
194194
return "http://cache.local" + `/${buildId}/${key}`.replace(/\/+/g, "/") + `.${cacheType ?? "cache"}`;
195195
}
@@ -235,7 +235,7 @@ class RegionalCache implements IncrementalCache {
235235
* @param cache Incremental cache instance.
236236
* @param opts Options for the regional cache.
237237
*/
238-
export function withRegionalCache(cache: IncrementalCache, opts: Options) {
238+
export function withRegionalCache(cache: IncrementalCache, opts: Options): RegionalCache {
239239
return new RegionalCache(cache, opts);
240240
}
241241

packages/cloudflare/src/api/overrides/incremental-cache/static-assets-incremental-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const NAME = "cf-static-assets-incremental-cache";
2121
* It should only be used for applications that do NOT want revalidation and ONLY want to serve prerendered data.
2222
*/
2323
class StaticAssetsIncrementalCache implements IncrementalCache {
24-
readonly name = NAME;
24+
readonly name: string = NAME;
2525

2626
async get<CacheType extends CacheEntryType = "cache">(
2727
key: string,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type IncrementalCacheEntry<CacheType extends CacheEntryType> = {
1010
lastModified: number;
1111
};
1212

13-
export const debugCache = (name: string, ...args: unknown[]) => {
13+
export const debugCache = (name: string, ...args: unknown[]): void => {
1414
if (process.env.NEXT_PRIVATE_DEBUG_CACHE) {
1515
console.log(`[${name}] `, ...args);
1616
}
@@ -26,7 +26,7 @@ export type KeyOptions = {
2626
buildId: string | undefined;
2727
};
2828

29-
export function computeCacheKey(key: string, options: KeyOptions) {
29+
export function computeCacheKey(key: string, options: KeyOptions): string {
3030
const { cacheType = "cache", prefix = DEFAULT_PREFIX, buildId = FALLBACK_BUILD_ID } = options;
3131
const hash = createHash("sha256").update(key).digest("hex");
3232
return `${prefix}/${buildId}/${hash}.${cacheType}`.replace(/\/+/g, "/");
@@ -39,7 +39,7 @@ export function isPurgeCacheEnabled(): boolean {
3939
return cdnInvalidation !== undefined && cdnInvalidation !== "dummy";
4040
}
4141

42-
export async function purgeCacheByTags(tags: string[]) {
42+
export async function purgeCacheByTags(tags: string[]): Promise<void> {
4343
const { env } = getCloudflareContext();
4444
// We have a durable object for purging cache
4545
// We should use it
@@ -55,7 +55,7 @@ export async function purgeCacheByTags(tags: string[]) {
5555
}
5656
}
5757

58-
export async function internalPurgeCacheByTags(env: CloudflareEnv, tags: string[]) {
58+
export async function internalPurgeCacheByTags(env: CloudflareEnv, tags: string[]): Promise<string> {
5959
if (!env.CACHE_PURGE_ZONE_ID || !env.CACHE_PURGE_API_TOKEN) {
6060
// THIS IS A NO-OP
6161
error("No cache zone ID or API token provided. Skipping cache purge.");

packages/cloudflare/src/api/overrides/queue/do-queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getCloudflareContext } from "../../cloudflare-context.js";
55

66
export default {
77
name: "durable-queue",
8-
send: async (msg: QueueMessage) => {
8+
send: async (msg: QueueMessage): Promise<void> => {
99
const durableObject = getCloudflareContext().env.NEXT_CACHE_DO_QUEUE;
1010
if (!durableObject) throw new IgnorableError("No durable object binding for cache revalidation");
1111

0 commit comments

Comments
 (0)