Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/workerd/api/global-scope.c++
Original file line number Diff line number Diff line change
Expand Up @@ -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<jsg::Value> reason) {
KJ_IF_SOME(r, reason) {
IoContext::current().abort(js.exceptionToKj(kj::mv(r)));
Expand Down
31 changes: 31 additions & 0 deletions src/workerd/api/global-scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ class ExecutionContext: public jsg::Object {
return props.getHandle(js);
}

// Returns an optional cache object for Cache-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
Expand All @@ -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);
}
Expand All @@ -264,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()) {
Expand All @@ -276,11 +295,17 @@ class ExecutionContext: public jsg::Object {
readonly key?: string;
readonly override?: string;
};
readonly cache?: {
purge(options: CachePurgeOptions): Promise<CachePurgeResult>;
};
});
} else {
JSG_TS_OVERRIDE(<Props = unknown> {
readonly props: Props;
readonly exports: Cloudflare.Exports;
readonly cache?: {
purge(options: CachePurgeOptions): Promise<CachePurgeResult>;
};
});
}
} else {
Expand All @@ -293,10 +318,16 @@ class ExecutionContext: public jsg::Object {
readonly key?: string;
readonly override?: string;
};
readonly cache?: {
purge(options: CachePurgeOptions): Promise<CachePurgeResult>;
};
});
} else {
JSG_TS_OVERRIDE(<Props = unknown> {
readonly props: Props;
readonly cache?: {
purge(options: CachePurgeOptions): Promise<CachePurgeResult>;
};
});
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/workerd/io/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,12 @@ class Worker::Api {
virtual jsg::JsObject wrapExecutionContext(
jsg::Lock& lock, jsg::Ref<api::ExecutionContext> 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;

Expand Down
17 changes: 17 additions & 0 deletions types/generated-snapshot/experimental/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +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<Props = unknown> {
waitUntil(promise: Promise<any>): void;
passThroughOnException(): void;
readonly exports: Cloudflare.Exports;
readonly props: Props;
readonly cache?: {
purge(options: CachePurgeOptions): Promise<CachePurgeResult>;
};
readonly version?: {
readonly metadata?: {
readonly id: string;
Expand Down
17 changes: 17 additions & 0 deletions types/generated-snapshot/experimental/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +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<Props = unknown> {
waitUntil(promise: Promise<any>): void;
passThroughOnException(): void;
readonly exports: Cloudflare.Exports;
readonly props: Props;
readonly cache?: {
purge(options: CachePurgeOptions): Promise<CachePurgeResult>;
};
readonly version?: {
readonly metadata?: {
readonly id: string;
Expand Down
17 changes: 17 additions & 0 deletions types/generated-snapshot/latest/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +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<Props = unknown> {
waitUntil(promise: Promise<any>): void;
passThroughOnException(): void;
readonly exports: Cloudflare.Exports;
readonly props: Props;
readonly cache?: {
purge(options: CachePurgeOptions): Promise<CachePurgeResult>;
};
}
type ExportedHandlerFetchHandler<
Env = unknown,
Expand Down
17 changes: 17 additions & 0 deletions types/generated-snapshot/latest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +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<Props = unknown> {
waitUntil(promise: Promise<any>): void;
passThroughOnException(): void;
readonly exports: Cloudflare.Exports;
readonly props: Props;
readonly cache?: {
purge(options: CachePurgeOptions): Promise<CachePurgeResult>;
};
}
export type ExportedHandlerFetchHandler<
Env = unknown,
Expand Down
Loading