Programmatic configuration (2)·续4
Declares a WorkerEntrypoint export defined by this Worker.
<details>
<summary>Options (1)</summary>
<dl>
<dt><code>cache?: { /* Whether cache is enabled for this entrypoint. / enabled: boolean; }</code></dt> <dd></dd>
</dl></details>
<code>cache</code>Optional<a href="#config-exports-exports-worker-default-2">Link to cache</a>
<code>cache?: { /* Whether cache is enabled for this entrypoint. / enabled: boolean; }</code>
The type definition does not include a description.
<code>enabled</code>Required<a href="#config-exports-exports-worker-default-2">Link to enabled</a>
<code>enabled: boolean</code>
Whether cache is enabled for this entrypoint.
</details>
<details>
<summary>Counter: exports.durableObject({ storage: "sqlite" }), (durableObject, storage reference)</summary>
<code>durableObject</code>Builder<a href="#config-exports-exports-durableobject-created">Link to durableObject</a>
<code>durableObject<TContainer extends ContainerDefinition | undefined = undefined>(options: DurableObjectCreatedExportOptions<TContainer>): DurableObjectCreatedExport<TContainer>;</code>
Declares a Durable Object class defined by this Worker. For more information about Durable Objects, see the documentation at <a href="https://developers.cloudflare.com/workers/learning/using-durable-objects">https://developers.cloudflare.com/workers/learning/using-durable-objects</a> For reference, see <a href="https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects">https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects</a>
<details>
<summary>Options (4)</summary>
<dl>
<dt><code>state?: "created"</code></dt> <dd></dd>
<dt><code>storage: "sqlite"</code></dt> <dd>Selects the SQLite-backed storage engine (recommended for new classes).</dd>
<dt><code>container?: TContainer</code></dt> <dd>Attach a Container application to this Durable Object by config reference.</dd>
<dt><code>storage: "legacy-kv"</code></dt> <dd>Selects the legacy key-value storage engine.</dd></dl></details>
<code>storage</code>Required<a href="#config-exports-exports-durableobject-created">Link to storage</a>
<code>storage: "sqlite" | "legacy-kv"</code>
Selects the SQLite-backed storage engine (recommended for new classes). Selects the legacy key-value storage engine.
</details>
<details>
<summary>OrderWorkflow: exports.workflow({ (workflow reference)</summary>
<code>workflow</code>Builder<a href="#config-exports-exports-workflow-default">Link to workflow</a>
<code>workflow(options: WorkflowExportOptions): WorkflowExport;</code>
Declares a Workflow defined by this Worker. The export's key must name a class that extends <code>WorkflowEntrypoint</code>. For more information about Workflows, see the documentation at <a href="https://developers.cloudflare.com/workflows/">https://developers.cloudflare.com/workflows/</a>
<details>
<summary>Options (5)</summary>
<dl>
<dt><code>name: string</code></dt> <dd>The name of the Workflow. It identifies the Workflow's instances and must be unique within the account.</dd>
<dt><code>limits?: { /* Maximum number of steps a single Workflow instance may run. / steps?: number; }</code></dt> <dd></dd>
<dt><code>concurrency?: { /* Maximum number of Workflow instances that can run concurrently. / limit?: number; }</code></dt> <dd></dd>
<dt><code>schedules?: string | string[]</code></dt> <dd>Cron schedule(s) that automatically trigger Workflow instances.</dd>
<dt><code>defaultRetention?: { / How long to retain instances that completed successfully or were terminated. */ successRetention?: number | string; / How long to retain errored instances. */ errorRetention?: number | string; }</code></dt> <dd>Default retention for instances of this Workflow, applied when an instance does not set its own retention. Accepts milliseconds or a duration string such as <code>"3 days"</code>.</dd>
</dl></details>
</details>
<details>
<summary>name: "order-workflow", (name reference)</summary>
<code>name</code>Required<a href="#config-exports-exports-workflow-default-name">Link to name</a>
<code>name: string</code>
The name of the Workflow. It identifies the Workflow's instances and must be unique within the account.
</details>
<details>
<summary>limits: { steps: 100 }, (limits, steps reference)</summary>
<code>limits</code>Optional<a href="#config-exports-exports-workflow-default-limits">Link to limits</a>
<code>limits?: { /* Maximum number of steps a single Workflow instance may run. / steps?: number; }</code>
The type definition does not include a description.
<code>steps</code>Optional<a href="#config-exports-exports-workflow-default-limits">Link to steps</a>
<code>steps?: number</code>
Maximum number of steps a single Workflow instance may run.
</details>
<details>
<summary>defaultRetention: { (defaultRetention reference)</summary>
<code>defaultRetention</code>Optional<a href="#config-exports-exports-workflow-default-defaultretention">Link to defaultRetention</a>
<code>defaultRetention?: { / How long to retain instances that completed successfully or were terminated. */ successRetention?: number | string; / How long to retain errored instances. */ errorRetention?: number | string; }</code>
Default retention for instances of this Workflow, applied when an instance does not set its own retention. Accepts milliseconds or a duration string such as <code>"3 days"</code>.
</details>
<details>
<summary>successRetention: "3 days", (successRetention reference)</summary>
<code>successRetention</code>Optional<a href="#config-exports-exports-workflow-default-defaultretention-successretention">Link to successRetention</a>
<code>successRetention?: number | string</code>
How long to retain instances that completed successfully or were terminated.
</details>
<details>
<summary>errorRetention: "7 days", (errorRetention reference)</summary>
<code>errorRetention</code>Optional<a href="#config-exports-exports-workflow-default-defaultretention-errorretention">Link to errorRetention</a>
<code>errorRetention?: number | string</code>
How long to retain errored instances.
</details>
},
}),
},
},
});
The builders describe exported code. They do not create JavaScript exports. The entrypoint must still export Admin, Counter, and OrderWorkflow.
exports.workflow() requires a name, which must be unique within the account. It also accepts limits.steps, concurrency.limit, schedules for cron schedules that start instances, and defaultRetention. Retention values accept milliseconds or a duration string, such as "3 days".
To bind to a Workflow, from the same Worker or another one, use bindings.workflow(). It requires the Workflow's name, the worker that defines it (a Worker name or a Worker definition), and the exportName of its class, such as bindings.workflow({ name: "order-workflow", worker: "example-worker", exportName: "OrderWorkflow" }).
Manage Durable Object lifecycle
The exports map replaces an ordered Durable Object migration history. Keep live classes in the map. Add temporary tombstones when a class is deleted, renamed, or transferred.
The key of each entry is the Durable Object class name. The state field selects the valid lifecycle options:
| State | Purpose |
|---|---|
created or omitted |
Declares a live class with sqlite or legacy-kv storage |
deleted |
Retires a namespace after its class is removed |
renamed |
Renames the key to the live class named by renamedTo |
expecting-transfer |
Prepares the destination Worker to receive a namespace from transferFrom |
transferred |
Transfers ownership to the same-account Worker named by transferredTo |
Use sqlite for new classes. A live or incoming class that uses sqlite can attach a Container definition. You cannot attach a Container to a legacy-kv class or a tombstone.
Distinguish exports from bindings
Exports and bindings serve different purposes:
| Configuration | Purpose |
|---|---|
exports.Counter |
Declares the class and manages its namespace lifecycle |
env.COUNTERS |
Injects a namespace binding under env.COUNTERS |
ctx.exports.Counter |
Accesses a class exported by the same Worker without an env binding |
Live Durable Object exports become typed properties on ctx.exports. A Worker does not need an env binding to call its own class.
Select a highlighted line to show its type and description below it.
cloudflare.config.ts
Expand allCopy
import { defineConfig, exports } from "cf/config";
import * as entrypoint from "./src/index.ts" with { type: "cf-worker" };
<details>
<summary>export default defineConfig({ (defineConfig reference)</summary>
<code>defineConfig</code>Function<a href="#config-ctx-exports-defineconfig">Link to defineConfig</a>
<code>defineConfig<T extends ConfigInput<CloudflareConfig>>(config: T): T;</code>
Defines the default export of <code>cloudflare.config.ts</code>. Pass a configuration object, a promise that resolves to one, or a function that receives the config context (<code>isPreview</code> and <code>mode</code>) and returns either.
<details>
<summary>Options (4)</summary>
<dl>
<dt><code>accountId?: string</code></dt> <dd>This is the ID of the account associated with your zone. It can also be specified through the <code>CLOUDFLARE_ACCOUNT_ID</code> environment variable.</dd>
<dt><code>complianceRegion?: "public" | "fedramp-high"</code></dt> <dd>The compliance boundary in which commands should operate. When omitted, this can be supplied through <code>CLOUDFLARE_COMPLIANCE_REGION</code>.</dd>
<dt><code>worker?: ConfigInput<WorkerConfig></code></dt> <dd>The Worker defined by this configuration.</dd>
<dt><code>containers?: ConfigInput<ContainerConfig>[]</code></dt> <dd>Container applications defined by this configuration.</dd></dl></details>
</details>
<details>
<summary>worker: { (worker reference)</summary>
<code>worker</code>Optional<a href="#config-ctx-exports-cloudflareconfig-worker">Link to worker</a>
<code>worker?: ConfigInput<WorkerConfig></code>
The Worker defined by this configuration.
</details>
<details>
<summary>name: "counter-worker", (name reference)</summary>
<code>name</code>Required<a href="#config-ctx-exports-workerconfig-name">Link to name</a>
<code>name: string</code>
The name of your Worker.
</details>
<details>
<summary>entrypoint, (entrypoint reference)</summary>
<code>entrypoint</code>Optional<a href="#config-ctx-exports-workerconfig-entrypoint">Link to entrypoint</a>
<code>entrypoint?: string | WorkerModule</code>
The entrypoint module that will be executed. May be either a path string (e.g. <code>"./src/index.ts"</code>) or a module namespace imported with the <code>cf-worker</code> import attribute.
</details>
<details>
<summary>compatibilityDate: "<COMPATIBILITY_DATE>", (compatibilityDate reference)</summary>
<code>compatibilityDate</code>Required<a href="#config-ctx-exports-workerconfig-compatibilitydate">Link to compatibilityDate</a>
<code>compatibilityDate: string</code>
A date in the form yyyy-mm-dd, which will be used to determine which version of the Workers runtime is used. More details at <a href="https://developers.cloudflare.com/workers/configuration/compatibility-dates">https://developers.cloudflare.com/workers/configuration/compatibility-dates</a>
</details>
<details>
<summary>compatibilityFlags: ["enable_ctx_exports"], (compatibilityFlags reference)</summary>
<code>compatibilityFlags</code>Optional<a href="#config-ctx-exports-workerconfig-compatibilityflags">Link to compatibilityFlags</a>
<code>compatibilityFlags?: string[]</code>
A list of flags that enable features from upcoming features of the Workers runtime, usually used together with <code>compatibilityDate</code>. More details at <a href="https://developers.cloudflare.com/workers/configuration/compatibility-flags/">https://developers.cloudflare.com/workers/configuration/compatibility-flags/</a>
Default: <code>[]</code>
</details>
<details>
<summary>exports: { (exports reference)</summary>
<code>exports</code>Optional<a href="#config-ctx-exports-workerconfig-exports">Link to exports</a>
<code>exports?: Record<string, Export></code>
Configuration for named exports declared by the Worker. Each entry's key is the exported class name; the value configures the export. - Construct entries with <code>exports.durableObject(...)</code>. - Declares Durable Object classes exported from this Worker. For more information about Durable Objects, see the documentation at <a href="https://developers.cloudflare.com/workers/learning/using-durable-objects">https://developers.cloudflare.com/workers/learning/using-durable-objects</a>. For reference, see <a href="https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects">https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects</a>. - Construct entries with <code>exports.workflow(...)</code>. - Declares Workflows defined by this Worker. For more information about Workflows, see the documentation at <a href="https://developers.cloudflare.com/workflows/">https://developers.cloudflare.com/workflows/</a>.
</details>
<details>
<summary>Counter: exports.durableObject({ storage: "sqlite" }), (durableObject, storage reference)</summary>
<code>durableObject</code>Builder<a href="#config-ctx-exports-exports-durableobject-created">Link to durableObject</a>
<code>durableObject<TContainer extends ContainerDefinition | undefined = undefined>(options: DurableObjectCreatedExportOptions<TContainer>): DurableObjectCreatedExport<TContainer>;</code>
Declares a Durable Object class defined by this Worker. For more information about Durable Objects, see the documentation at <a href="https://developers.cloudflare.com/workers/learning/using-durable-objects">https://developers.cloudflare.com/workers/learning/using-durable-objects</a> For reference, see <a href="https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects">https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects</a>
<details>
<summary>Options (4)</summary>
<dl>
<dt><code>state?: "created"</code></dt> <dd></dd>
<dt><code>storage: "sqlite"</code></dt> <dd>Selects the SQLite-backed storage engine (recommended for new classes).</dd>
<dt><code>container?: TContainer</code></dt> <dd>Attach a Container application to this Durable Object by config reference.</dd>
<dt><code>storage: "legacy-kv"</code></dt> <dd>Selects the legacy key-value storage engine.</dd></dl></details>
<code>storage</code>Required<a href="#config-ctx-exports-exports-durableobject-created">Link to storage</a>
<code>storage: "sqlite" | "legacy-kv"</code>
Selects the SQLite-backed storage engine (recommended for new classes). Selects the legacy key-value storage engine.
</details>
},
},
});
src/index.jsjs
import { DurableObject } from "cloudflare:workers";
export class Counter extends DurableObject {
getValue() {
return this.ctx.storage.get("value");
}
}
export default {
async fetch(_request, _env, ctx) {
const id = ctx.exports.Counter.idFromName("global");
const stub = ctx.exports.Counter.get(id);
return Response.json({ value: await stub.getValue() });
},
};
src/index.tsts
import { DurableObject } from "cloudflare:workers";
export class Counter extends DurableObject {
getValue() {
return this.ctx.storage.get<number>("value");
}
}
export default {
async fetch(_request, _env, ctx) {
const id = ctx.exports.Counter.idFromName("global");
const stub = ctx.exports.Counter.get(id);
return Response.json({ value: await stub.getValue() });
},
} satisfies ExportedHandler<Env>;
The generated types include live and incoming Durable Object exports. Tombstones do not appear on ctx.exports.
Rename or delete a class
A rename keeps the old name as a tombstone. The destination name must appear as a live entry in the same map. Remove the old class from the Worker code.
Select a highlighted line to show its type and description below it.
cloudflare.config.ts
Expand allCopy
import { defineConfig, exports } from "cf/config";
import * as entrypoint from "./src/index.ts" with { type: "cf-worker" };
<details>
<summary>export default defineConfig({ (defineConfig reference)</summary>
<code>defineConfig</code>Function<a href="#config-rename-delete-defineconfig">Link to defineConfig</a>
<code>defineConfig<T extends ConfigInput<CloudflareConfig>>(config: T): T;</code>
Defines the default export of <code>cloudflare.config.ts</code>. Pass a configuration object, a promise that resolves to one, or a function that receives the config context (<code>isPreview</code> and <code>mode</code>) and returns either.
<details>
<summary>Options (4)</summary>
<dl>
<dt><code>accountId?: string</code></dt> <dd>This is the ID of the account associated with your zone. It can also be specified through the <code>CLOUDFLARE_ACCOUNT_ID</code> environment variable.</dd>
<dt><code>complianceRegion?: "public" | "fedramp-high"</code></dt> <dd>The compliance boundary in which commands should operate. When omitted, this can be supplied through <code>CLOUDFLARE_COMPLIANCE_REGION</code>.</dd>
<dt><code>worker?: ConfigInput<WorkerConfig></code></dt> <dd>The Worker defined by this configuration.</dd>
