From d0e1bd5de8d321eeebb984cf620e17dd730062c0 Mon Sep 17 00:00:00 2001 From: Joe Lee Date: Mon, 9 Dec 2024 10:31:02 -0800 Subject: [PATCH] worker-types: Declare optional AlarmInvocationInfo parameter to Durable Object alarm() method The runtime passes in this parameter when invoking alarm(); the `retryCount` property gives a count of the number of previous times the runtime has tried to run this specific alarm event. The runtime has been passing this parameter to alarm() calls since it was originally introduced earlier in the year, in PR#1673, but the parameter declaration has not yet been added to the worker-types TypeScript declarations. This commit just updates the TypeScript declarations to include this parameter. --- src/workerd/api/actor.h | 2 +- types/defines/rpc.d.ts | 2 +- .../generated-snapshot/2021-11-03/index.d.ts | 4 ++-- types/generated-snapshot/2021-11-03/index.ts | 2 +- .../generated-snapshot/2022-01-31/index.d.ts | 4 ++-- types/generated-snapshot/2022-01-31/index.ts | 2 +- .../generated-snapshot/2022-03-21/index.d.ts | 4 ++-- types/generated-snapshot/2022-03-21/index.ts | 2 +- .../generated-snapshot/2022-08-04/index.d.ts | 4 ++-- types/generated-snapshot/2022-08-04/index.ts | 2 +- .../generated-snapshot/2022-10-31/index.d.ts | 4 ++-- types/generated-snapshot/2022-10-31/index.ts | 2 +- .../generated-snapshot/2022-11-30/index.d.ts | 4 ++-- types/generated-snapshot/2022-11-30/index.ts | 2 +- .../generated-snapshot/2023-03-01/index.d.ts | 4 ++-- types/generated-snapshot/2023-03-01/index.ts | 2 +- .../generated-snapshot/2023-07-01/index.d.ts | 4 ++-- types/generated-snapshot/2023-07-01/index.ts | 2 +- .../experimental/index.d.ts | 4 ++-- .../generated-snapshot/experimental/index.ts | 2 +- types/generated-snapshot/oldest/index.d.ts | 4 ++-- types/generated-snapshot/oldest/index.ts | 2 +- types/test/types/rpc.ts | 22 +++++++++++++++++++ 23 files changed, 54 insertions(+), 32 deletions(-) diff --git a/src/workerd/api/actor.h b/src/workerd/api/actor.h index cb96fcfdb4e..619e47c06ad 100644 --- a/src/workerd/api/actor.h +++ b/src/workerd/api/actor.h @@ -106,7 +106,7 @@ class DurableObject final: public Fetcher { JSG_TS_DEFINE(interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?(ws: WebSocket, message: string | ArrayBuffer): void | Promise; webSocketClose?(ws: WebSocket, code: number, reason: string, wasClean: boolean): void | Promise; webSocketError?(ws: WebSocket, error: unknown): void | Promise; diff --git a/types/defines/rpc.d.ts b/types/defines/rpc.d.ts index cba6f1d6e7e..724870f48f5 100644 --- a/types/defines/rpc.d.ts +++ b/types/defines/rpc.d.ts @@ -190,7 +190,7 @@ declare module "cloudflare:workers" { constructor(ctx: DurableObjectState, env: Env); fetch?(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer diff --git a/types/generated-snapshot/2021-11-03/index.d.ts b/types/generated-snapshot/2021-11-03/index.d.ts index af5bd8fdfbf..4905dfae304 100755 --- a/types/generated-snapshot/2021-11-03/index.d.ts +++ b/types/generated-snapshot/2021-11-03/index.d.ts @@ -467,7 +467,7 @@ interface Cloudflare { } interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, @@ -5312,7 +5312,7 @@ declare module "cloudflare:workers" { protected env: Env; constructor(ctx: DurableObjectState, env: Env); fetch?(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2021-11-03/index.ts b/types/generated-snapshot/2021-11-03/index.ts index bcc6bfdea5a..4231985343d 100755 --- a/types/generated-snapshot/2021-11-03/index.ts +++ b/types/generated-snapshot/2021-11-03/index.ts @@ -472,7 +472,7 @@ export interface Cloudflare { } export interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2022-01-31/index.d.ts b/types/generated-snapshot/2022-01-31/index.d.ts index 01fc53bf7ea..c18bfe41030 100755 --- a/types/generated-snapshot/2022-01-31/index.d.ts +++ b/types/generated-snapshot/2022-01-31/index.d.ts @@ -467,7 +467,7 @@ interface Cloudflare { } interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, @@ -5338,7 +5338,7 @@ declare module "cloudflare:workers" { protected env: Env; constructor(ctx: DurableObjectState, env: Env); fetch?(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2022-01-31/index.ts b/types/generated-snapshot/2022-01-31/index.ts index 1ce1c48dc6a..17208d314ed 100755 --- a/types/generated-snapshot/2022-01-31/index.ts +++ b/types/generated-snapshot/2022-01-31/index.ts @@ -472,7 +472,7 @@ export interface Cloudflare { } export interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2022-03-21/index.d.ts b/types/generated-snapshot/2022-03-21/index.d.ts index 4b73489f817..370c4c747de 100755 --- a/types/generated-snapshot/2022-03-21/index.d.ts +++ b/types/generated-snapshot/2022-03-21/index.d.ts @@ -485,7 +485,7 @@ interface Cloudflare { } interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, @@ -5363,7 +5363,7 @@ declare module "cloudflare:workers" { protected env: Env; constructor(ctx: DurableObjectState, env: Env); fetch?(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2022-03-21/index.ts b/types/generated-snapshot/2022-03-21/index.ts index a23b1e72dac..d9917cb2f7c 100755 --- a/types/generated-snapshot/2022-03-21/index.ts +++ b/types/generated-snapshot/2022-03-21/index.ts @@ -490,7 +490,7 @@ export interface Cloudflare { } export interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2022-08-04/index.d.ts b/types/generated-snapshot/2022-08-04/index.d.ts index ea2898b3657..add792eb061 100755 --- a/types/generated-snapshot/2022-08-04/index.d.ts +++ b/types/generated-snapshot/2022-08-04/index.d.ts @@ -485,7 +485,7 @@ interface Cloudflare { } interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, @@ -5364,7 +5364,7 @@ declare module "cloudflare:workers" { protected env: Env; constructor(ctx: DurableObjectState, env: Env); fetch?(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2022-08-04/index.ts b/types/generated-snapshot/2022-08-04/index.ts index 1bc6008d06c..6eb39496ac4 100755 --- a/types/generated-snapshot/2022-08-04/index.ts +++ b/types/generated-snapshot/2022-08-04/index.ts @@ -490,7 +490,7 @@ export interface Cloudflare { } export interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2022-10-31/index.d.ts b/types/generated-snapshot/2022-10-31/index.d.ts index a58d8c6e503..3cf4b1e36ae 100755 --- a/types/generated-snapshot/2022-10-31/index.d.ts +++ b/types/generated-snapshot/2022-10-31/index.d.ts @@ -485,7 +485,7 @@ interface Cloudflare { } interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, @@ -5367,7 +5367,7 @@ declare module "cloudflare:workers" { protected env: Env; constructor(ctx: DurableObjectState, env: Env); fetch?(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2022-10-31/index.ts b/types/generated-snapshot/2022-10-31/index.ts index cb6feb9fde0..9698a8cd8ce 100755 --- a/types/generated-snapshot/2022-10-31/index.ts +++ b/types/generated-snapshot/2022-10-31/index.ts @@ -490,7 +490,7 @@ export interface Cloudflare { } export interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2022-11-30/index.d.ts b/types/generated-snapshot/2022-11-30/index.d.ts index 48406bac695..4ce2a377d0a 100755 --- a/types/generated-snapshot/2022-11-30/index.d.ts +++ b/types/generated-snapshot/2022-11-30/index.d.ts @@ -490,7 +490,7 @@ interface Cloudflare { } interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, @@ -5372,7 +5372,7 @@ declare module "cloudflare:workers" { protected env: Env; constructor(ctx: DurableObjectState, env: Env); fetch?(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2022-11-30/index.ts b/types/generated-snapshot/2022-11-30/index.ts index 601de52bb5a..6d55c925fda 100755 --- a/types/generated-snapshot/2022-11-30/index.ts +++ b/types/generated-snapshot/2022-11-30/index.ts @@ -495,7 +495,7 @@ export interface Cloudflare { } export interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2023-03-01/index.d.ts b/types/generated-snapshot/2023-03-01/index.d.ts index fb3a812a0a7..fd23ede7d39 100755 --- a/types/generated-snapshot/2023-03-01/index.d.ts +++ b/types/generated-snapshot/2023-03-01/index.d.ts @@ -490,7 +490,7 @@ interface Cloudflare { } interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, @@ -5374,7 +5374,7 @@ declare module "cloudflare:workers" { protected env: Env; constructor(ctx: DurableObjectState, env: Env); fetch?(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2023-03-01/index.ts b/types/generated-snapshot/2023-03-01/index.ts index c014ca490bc..3aa7bf83773 100755 --- a/types/generated-snapshot/2023-03-01/index.ts +++ b/types/generated-snapshot/2023-03-01/index.ts @@ -495,7 +495,7 @@ export interface Cloudflare { } export interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2023-07-01/index.d.ts b/types/generated-snapshot/2023-07-01/index.d.ts index 6db3a0ec236..7fc9212977d 100755 --- a/types/generated-snapshot/2023-07-01/index.d.ts +++ b/types/generated-snapshot/2023-07-01/index.d.ts @@ -490,7 +490,7 @@ interface Cloudflare { } interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, @@ -5374,7 +5374,7 @@ declare module "cloudflare:workers" { protected env: Env; constructor(ctx: DurableObjectState, env: Env); fetch?(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/2023-07-01/index.ts b/types/generated-snapshot/2023-07-01/index.ts index e831fc15830..0adb9cee019 100755 --- a/types/generated-snapshot/2023-07-01/index.ts +++ b/types/generated-snapshot/2023-07-01/index.ts @@ -495,7 +495,7 @@ export interface Cloudflare { } export interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/experimental/index.d.ts b/types/generated-snapshot/experimental/index.d.ts index d7e15a66da7..f70928ea63a 100755 --- a/types/generated-snapshot/experimental/index.d.ts +++ b/types/generated-snapshot/experimental/index.d.ts @@ -491,7 +491,7 @@ interface Cloudflare { } interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, @@ -5440,7 +5440,7 @@ declare module "cloudflare:workers" { protected env: Env; constructor(ctx: DurableObjectState, env: Env); fetch?(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/experimental/index.ts b/types/generated-snapshot/experimental/index.ts index cb1bcd71ab3..4d7735ca0b1 100755 --- a/types/generated-snapshot/experimental/index.ts +++ b/types/generated-snapshot/experimental/index.ts @@ -496,7 +496,7 @@ export interface Cloudflare { } export interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/oldest/index.d.ts b/types/generated-snapshot/oldest/index.d.ts index 88d32434d95..70676eaceb1 100755 --- a/types/generated-snapshot/oldest/index.d.ts +++ b/types/generated-snapshot/oldest/index.d.ts @@ -467,7 +467,7 @@ interface Cloudflare { } interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, @@ -5312,7 +5312,7 @@ declare module "cloudflare:workers" { protected env: Env; constructor(ctx: DurableObjectState, env: Env); fetch?(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/generated-snapshot/oldest/index.ts b/types/generated-snapshot/oldest/index.ts index 91933733739..3e228fdab74 100755 --- a/types/generated-snapshot/oldest/index.ts +++ b/types/generated-snapshot/oldest/index.ts @@ -472,7 +472,7 @@ export interface Cloudflare { } export interface DurableObject { fetch(request: Request): Response | Promise; - alarm?(): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; webSocketMessage?( ws: WebSocket, message: string | ArrayBuffer, diff --git a/types/test/types/rpc.ts b/types/test/types/rpc.ts index 4307bb94692..a77a553ae87 100644 --- a/types/test/types/rpc.ts +++ b/types/test/types/rpc.ts @@ -306,6 +306,27 @@ class TestObject extends DurableObject { } } +class TestAlarmObject extends DurableObject { + // Can declare alarm method consuming optional alarmInfo parameter + async alarm(alarmInfo?: AlarmInvocationInfo) { + if (alarmInfo !== undefined) { + const _isRetry: boolean = alarmInfo.isRetry; + const _retryCount: number = alarmInfo.retryCount; + } + } + + // User code can invoke alarm() directly, if desired. + async runAlarmVoid(): Promise { + return await this.alarm(); + } + async runAlarmInfo(): Promise { + return await this.alarm({ + isRetry: true, + retryCount: 1, + }); + } +} + class TestNaughtyEntrypoint extends WorkerEntrypoint { // Check incorrectly typed methods // @ts-expect-error @@ -351,6 +372,7 @@ interface Env { REGULAR_OBJECT: DurableObjectNamespace; RPC_OBJECT: DurableObjectNamespace; + ALARM_OBJECT: DurableObjectNamespace; NAUGHTY_OBJECT: DurableObjectNamespace; // @ts-expect-error `BoringClass` isn't an RPC capable type __INVALID_OBJECT_1: DurableObjectNamespace;