From 18b1c667afb741e115b49e95b3bddf8bccc264ad Mon Sep 17 00:00:00 2001 From: tewaro Date: Mon, 30 Mar 2026 16:13:14 -0500 Subject: [PATCH 1/3] Cache purge stub --- src/workerd/api/global-scope.c++ | 9 +++++++++ src/workerd/api/global-scope.h | 5 +++++ src/workerd/io/worker.h | 6 ++++++ 3 files changed, 20 insertions(+) diff --git a/src/workerd/api/global-scope.c++ b/src/workerd/api/global-scope.c++ index ba4cb0c9f8e..d921ad60585 100644 --- a/src/workerd/api/global-scope.c++ +++ b/src/workerd/api/global-scope.c++ @@ -65,6 +65,15 @@ void ExecutionContext::passThroughOnException() { IoContext::current().setFailOpen(); } +jsg::JsValue ExecutionContext::getCache(jsg::Lock& js) { + // Hook for the embedding application (e.g. edgeworker) to provide a ctx.cache object. + // The default Worker::Api implementation returns undefined. + if (IoContext::hasCurrent()) { + return Worker::Isolate::from(js).getApi().getCtxCacheProperty(js); + } + return js.undefined(); +} + void ExecutionContext::abort(jsg::Lock& js, jsg::Optional reason) { KJ_IF_SOME(r, reason) { IoContext::current().abort(js.exceptionToKj(kj::mv(r))); diff --git a/src/workerd/api/global-scope.h b/src/workerd/api/global-scope.h index ffaf66fac9e..1e4d2812124 100644 --- a/src/workerd/api/global-scope.h +++ b/src/workerd/api/global-scope.h @@ -228,6 +228,10 @@ class ExecutionContext: public jsg::Object { return props.getHandle(js); } + // Returns an optional cache object for CacheW-enabled workers. + // The default implementation returns undefined. Overridden via IoChannelFactory. + jsg::JsValue getCache(jsg::Lock& js); + jsg::JsValue getVersion(jsg::Lock& js) { // TODO(soon): We should be able to assert for `version != kj::none` in the constructor when the // `enable_version_api` compat flag is enabled, but currently dynamic workers and "reusable @@ -246,6 +250,7 @@ class ExecutionContext: public jsg::Object { JSG_LAZY_INSTANCE_PROPERTY(exports, getExports); } JSG_LAZY_INSTANCE_PROPERTY(props, getProps); + JSG_LAZY_INSTANCE_PROPERTY(cache, getCache); if (flags.getEnableVersionApi()) { JSG_LAZY_INSTANCE_PROPERTY(version, getVersion); } diff --git a/src/workerd/io/worker.h b/src/workerd/io/worker.h index f0301b5a794..709b725b561 100644 --- a/src/workerd/io/worker.h +++ b/src/workerd/io/worker.h @@ -663,6 +663,12 @@ class Worker::Api { virtual jsg::JsObject wrapExecutionContext( jsg::Lock& lock, jsg::Ref ref) const = 0; + // Hook for the embedding application to provide a value for the ctx.cache property. + // The default implementation returns JS undefined. + virtual jsg::JsValue getCtxCacheProperty(jsg::Lock& js) const { + return js.undefined(); + } + virtual const jsg::IsolateObserver& getObserver() const = 0; virtual void setIsolateObserver(IsolateObserver&) = 0; From b6cc95455344544dd9b436848303da9c25e3c5f1 Mon Sep 17 00:00:00 2001 From: tewaro Date: Tue, 31 Mar 2026 11:44:25 -0500 Subject: [PATCH 2/3] Update types --- types/generated-snapshot/experimental/index.d.ts | 1 + types/generated-snapshot/experimental/index.ts | 1 + types/generated-snapshot/latest/index.d.ts | 1 + types/generated-snapshot/latest/index.ts | 1 + 4 files changed, 4 insertions(+) diff --git a/types/generated-snapshot/experimental/index.d.ts b/types/generated-snapshot/experimental/index.d.ts index e032661035b..22a24d99b5b 100755 --- a/types/generated-snapshot/experimental/index.d.ts +++ b/types/generated-snapshot/experimental/index.d.ts @@ -492,6 +492,7 @@ interface ExecutionContext { passThroughOnException(): void; readonly exports: Cloudflare.Exports; readonly props: Props; + cache: any; readonly version?: { readonly metadata?: { readonly id: string; diff --git a/types/generated-snapshot/experimental/index.ts b/types/generated-snapshot/experimental/index.ts index ccb44ba850b..553e6399448 100755 --- a/types/generated-snapshot/experimental/index.ts +++ b/types/generated-snapshot/experimental/index.ts @@ -494,6 +494,7 @@ export interface ExecutionContext { passThroughOnException(): void; readonly exports: Cloudflare.Exports; readonly props: Props; + cache: any; readonly version?: { readonly metadata?: { readonly id: string; diff --git a/types/generated-snapshot/latest/index.d.ts b/types/generated-snapshot/latest/index.d.ts index b569c0a3c41..3fae25c7588 100755 --- a/types/generated-snapshot/latest/index.d.ts +++ b/types/generated-snapshot/latest/index.d.ts @@ -479,6 +479,7 @@ interface ExecutionContext { passThroughOnException(): void; readonly exports: Cloudflare.Exports; readonly props: Props; + cache: any; } type ExportedHandlerFetchHandler< Env = unknown, diff --git a/types/generated-snapshot/latest/index.ts b/types/generated-snapshot/latest/index.ts index f2c4af42220..3cecc2af7ba 100755 --- a/types/generated-snapshot/latest/index.ts +++ b/types/generated-snapshot/latest/index.ts @@ -481,6 +481,7 @@ export interface ExecutionContext { passThroughOnException(): void; readonly exports: Cloudflare.Exports; readonly props: Props; + cache: any; } export type ExportedHandlerFetchHandler< Env = unknown, From 5535f5ef089ad190327435a4cff2d7c205a19b44 Mon Sep 17 00:00:00 2001 From: tewaro Date: Wed, 1 Apr 2026 13:10:48 -0500 Subject: [PATCH 3/3] Update types --- src/workerd/api/global-scope.h | 28 ++++++++++++++++++- .../experimental/index.d.ts | 18 +++++++++++- .../generated-snapshot/experimental/index.ts | 18 +++++++++++- types/generated-snapshot/latest/index.d.ts | 18 +++++++++++- types/generated-snapshot/latest/index.ts | 18 +++++++++++- 5 files changed, 95 insertions(+), 5 deletions(-) diff --git a/src/workerd/api/global-scope.h b/src/workerd/api/global-scope.h index 1e4d2812124..efd2df9c44e 100644 --- a/src/workerd/api/global-scope.h +++ b/src/workerd/api/global-scope.h @@ -228,7 +228,7 @@ class ExecutionContext: public jsg::Object { return props.getHandle(js); } - // Returns an optional cache object for CacheW-enabled workers. + // Returns an optional cache object for Cache-enabled workers. // The default implementation returns undefined. Overridden via IoChannelFactory. jsg::JsValue getCache(jsg::Lock& js); @@ -269,6 +269,20 @@ class ExecutionContext: public jsg::Object { JSG_METHOD(abort); } + JSG_TS_DEFINE( + interface CachePurgeOptions { + tags?: string[]; + hosts?: string[]; + prefixes?: string[]; + purge_everything?: boolean; + } + interface CachePurgeResult { + success: boolean; + zoneTag: string; + errors: { code: number; message: string }[]; + } + ); + // TODO(soon): This is getting unwieldy. if (flags.getEnableCtxExports()) { if (flags.getEnableVersionApi()) { @@ -281,11 +295,17 @@ class ExecutionContext: public jsg::Object { readonly key?: string; readonly override?: string; }; + readonly cache?: { + purge(options: CachePurgeOptions): Promise; + }; }); } else { JSG_TS_OVERRIDE( { readonly props: Props; readonly exports: Cloudflare.Exports; + readonly cache?: { + purge(options: CachePurgeOptions): Promise; + }; }); } } else { @@ -298,10 +318,16 @@ class ExecutionContext: public jsg::Object { readonly key?: string; readonly override?: string; }; + readonly cache?: { + purge(options: CachePurgeOptions): Promise; + }; }); } else { JSG_TS_OVERRIDE( { readonly props: Props; + readonly cache?: { + purge(options: CachePurgeOptions): Promise; + }; }); } } diff --git a/types/generated-snapshot/experimental/index.d.ts b/types/generated-snapshot/experimental/index.d.ts index 22a24d99b5b..a3b37f1e1aa 100755 --- a/types/generated-snapshot/experimental/index.d.ts +++ b/types/generated-snapshot/experimental/index.d.ts @@ -487,12 +487,28 @@ declare const Cloudflare: Cloudflare; declare const origin: string; declare const navigator: Navigator; interface TestController {} +interface CachePurgeOptions { + tags?: string[]; + hosts?: string[]; + prefixes?: string[]; + purge_everything?: boolean; +} +interface CachePurgeResult { + success: boolean; + zoneTag: string; + errors: { + code: number; + message: string; + }[]; +} interface ExecutionContext { waitUntil(promise: Promise): void; passThroughOnException(): void; readonly exports: Cloudflare.Exports; readonly props: Props; - cache: any; + readonly cache?: { + purge(options: CachePurgeOptions): Promise; + }; readonly version?: { readonly metadata?: { readonly id: string; diff --git a/types/generated-snapshot/experimental/index.ts b/types/generated-snapshot/experimental/index.ts index 553e6399448..6fa32a6a4ac 100755 --- a/types/generated-snapshot/experimental/index.ts +++ b/types/generated-snapshot/experimental/index.ts @@ -489,12 +489,28 @@ export declare const Cloudflare: Cloudflare; export declare const origin: string; export declare const navigator: Navigator; export interface TestController {} +export interface CachePurgeOptions { + tags?: string[]; + hosts?: string[]; + prefixes?: string[]; + purge_everything?: boolean; +} +export interface CachePurgeResult { + success: boolean; + zoneTag: string; + errors: { + code: number; + message: string; + }[]; +} export interface ExecutionContext { waitUntil(promise: Promise): void; passThroughOnException(): void; readonly exports: Cloudflare.Exports; readonly props: Props; - cache: any; + readonly cache?: { + purge(options: CachePurgeOptions): Promise; + }; readonly version?: { readonly metadata?: { readonly id: string; diff --git a/types/generated-snapshot/latest/index.d.ts b/types/generated-snapshot/latest/index.d.ts index 3fae25c7588..6f98c54d0e2 100755 --- a/types/generated-snapshot/latest/index.d.ts +++ b/types/generated-snapshot/latest/index.d.ts @@ -474,12 +474,28 @@ declare const Cloudflare: Cloudflare; declare const origin: string; declare const navigator: Navigator; interface TestController {} +interface CachePurgeOptions { + tags?: string[]; + hosts?: string[]; + prefixes?: string[]; + purge_everything?: boolean; +} +interface CachePurgeResult { + success: boolean; + zoneTag: string; + errors: { + code: number; + message: string; + }[]; +} interface ExecutionContext { waitUntil(promise: Promise): void; passThroughOnException(): void; readonly exports: Cloudflare.Exports; readonly props: Props; - cache: any; + readonly cache?: { + purge(options: CachePurgeOptions): Promise; + }; } type ExportedHandlerFetchHandler< Env = unknown, diff --git a/types/generated-snapshot/latest/index.ts b/types/generated-snapshot/latest/index.ts index 3cecc2af7ba..918a7f460cc 100755 --- a/types/generated-snapshot/latest/index.ts +++ b/types/generated-snapshot/latest/index.ts @@ -476,12 +476,28 @@ export declare const Cloudflare: Cloudflare; export declare const origin: string; export declare const navigator: Navigator; export interface TestController {} +export interface CachePurgeOptions { + tags?: string[]; + hosts?: string[]; + prefixes?: string[]; + purge_everything?: boolean; +} +export interface CachePurgeResult { + success: boolean; + zoneTag: string; + errors: { + code: number; + message: string; + }[]; +} export interface ExecutionContext { waitUntil(promise: Promise): void; passThroughOnException(): void; readonly exports: Cloudflare.Exports; readonly props: Props; - cache: any; + readonly cache?: { + purge(options: CachePurgeOptions): Promise; + }; } export type ExportedHandlerFetchHandler< Env = unknown,