Programmatic configuration (2)·续3

<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-triggers-cloudflareconfig-worker">Link to worker</a>

<code>worker?: ConfigInput<WorkerConfig></code>

The Worker defined by this configuration.

</details>

<details>

<summary>name: "example-worker", (name reference)</summary>

<code>name</code>Required<a href="#config-triggers-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-triggers-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-triggers-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>triggers: [ (triggers reference)</summary>

<code>triggers</code>Optional<a href="#config-triggers-workerconfig-triggers">Link to triggers</a>

<code>triggers?: Trigger[]</code>

Event triggers — fetch routes, queue consumers, cron schedules, Email Routing addresses, and raw sockets — that invoke this Worker. Construct entries with <code>triggers.fetch(...)</code>, <code>triggers.queue(...)</code>, <code>triggers.scheduled(...)</code>, <code>triggers.email(...)</code>, or <code>triggers.connect(...)</code>. For reference, see <a href="https://developers.cloudflare.com/workers/wrangler/configuration/#triggers">https://developers.cloudflare.com/workers/wrangler/configuration/#triggers</a>

</details>

<details>

<summary>triggers.fetch({ (fetch reference)</summary>

<code>fetch</code>Builder<a href="#config-triggers-triggers-fetch-default">Link to fetch</a>

<code>fetch(options: FetchTriggerOptions): FetchTrigger;</code>

Fetch trigger — a route that your Worker should be published to. For reference, see <a href="https://developers.cloudflare.com/workers/wrangler/configuration/#types-of-routes">https://developers.cloudflare.com/workers/wrangler/configuration/#types-of-routes</a>

<details>

<summary>Options (2)</summary>

<dl>

<dt><code>pattern: string</code></dt> <dd>A route that your Worker should be published to. For reference, see <a href="https://developers.cloudflare.com/workers/wrangler/configuration/#types-of-routes">https://developers.cloudflare.com/workers/wrangler/configuration/#types-of-routes</a></dd>

<dt><code>zone?: string</code></dt> <dd>The DNS zone the pattern is attached to. Required when the pattern is ambiguous.</dd></dl></details>

</details>

<details>

<summary>pattern: "api.example.com/*", (pattern reference)</summary>

<code>pattern</code>Required<a href="#config-triggers-triggers-fetch-default-pattern">Link to pattern</a>

<code>pattern: string</code>

A route that your Worker should be published to. For reference, see <a href="https://developers.cloudflare.com/workers/wrangler/configuration/#types-of-routes">https://developers.cloudflare.com/workers/wrangler/configuration/#types-of-routes</a>

</details>

<details>

<summary>zone: "example.com", (zone reference)</summary>

<code>zone</code>Optional<a href="#config-triggers-triggers-fetch-default-zone">Link to zone</a>

<code>zone?: string</code>

The DNS zone the pattern is attached to. Required when the pattern is ambiguous.

</details>

}),

<details>

<summary>triggers.queue({ (queue reference)</summary>

<code>queue</code>Builder<a href="#config-triggers-triggers-queue-default">Link to queue</a>

<code>queue(options: QueueConsumerTriggerOptions): QueueConsumerTrigger;</code>

Queue consumer trigger — invokes this Worker when messages arrive on the named queue. For reference, see <a href="https://developers.cloudflare.com/workers/wrangler/configuration/#queues">https://developers.cloudflare.com/workers/wrangler/configuration/#queues</a>

<details>

<summary>Options (8)</summary>

<dl>

<dt><code>name: string</code></dt> <dd>The name of the queue from which this consumer should consume.</dd>

<dt><code>deadLetterQueue?: string</code></dt> <dd>The queue to send messages that failed to be consumed.</dd>

<dt><code>maxBatchSize?: number</code></dt> <dd>The maximum number of messages per batch.</dd>

<dt><code>maxBatchTimeout?: number</code></dt> <dd>The maximum number of seconds to wait to fill a batch with messages.</dd>

<dt><code>maxConcurrency?: number | null</code></dt> <dd>The maximum number of concurrent consumer Worker invocations. Leaving this unset will allow your consumer to scale to the maximum concurrency needed to keep up with the message backlog.</dd>

<dt><code>maxRetries?: number</code></dt> <dd>The maximum number of retries for each message.</dd>

<dt><code>retryDelay?: number</code></dt> <dd>The number of seconds to wait before retrying a message.</dd>

<dt><code>visibilityTimeoutMs?: number</code></dt> <dd>The number of milliseconds to wait for pulled messages to become visible again.</dd></dl></details>

</details>

<details>

<summary>name: "application-jobs", (name reference)</summary>

<code>name</code>Required<a href="#config-triggers-triggers-queue-default-name">Link to name</a>

<code>name: string</code>

The name of the queue from which this consumer should consume.

</details>

<details>

<summary>maxBatchSize: 10, (maxBatchSize reference)</summary>

<code>maxBatchSize</code>Optional<a href="#config-triggers-triggers-queue-default-maxbatchsize">Link to maxBatchSize</a>

<code>maxBatchSize?: number</code>

The maximum number of messages per batch.

</details>

<details>

<summary>maxRetries: 3, (maxRetries reference)</summary>

<code>maxRetries</code>Optional<a href="#config-triggers-triggers-queue-default-maxretries">Link to maxRetries</a>

<code>maxRetries?: number</code>

The maximum number of retries for each message.

</details>

}),

<details>

<summary>triggers.scheduled({ schedule: "0 * * * *" }), (scheduled, schedule reference)</summary>

<code>scheduled</code>Builder<a href="#config-triggers-triggers-scheduled-default">Link to scheduled</a>

<code>scheduled(options: ScheduledTriggerOptions): ScheduledTrigger;</code>

Scheduled (cron) trigger — invokes this Worker on the given schedules. More details here <a href="https://developers.cloudflare.com/workers/platform/cron-triggers">https://developers.cloudflare.com/workers/platform/cron-triggers</a>

<details>

<summary>Options (1)</summary>

<dl>

<dt><code>schedule: string</code></dt> <dd>A "cron" definition to trigger a Worker's "scheduled" function. Lets you call Workers periodically, much like a cron job. More details here <a href="https://developers.cloudflare.com/workers/platform/cron-triggers">https://developers.cloudflare.com/workers/platform/cron-triggers</a></dd>

</dl></details>

<code>schedule</code>Required<a href="#config-triggers-triggers-scheduled-default">Link to schedule</a>

<code>schedule: string</code>

A "cron" definition to trigger a Worker's "scheduled" function. Lets you call Workers periodically, much like a cron job. More details here <a href="https://developers.cloudflare.com/workers/platform/cron-triggers">https://developers.cloudflare.com/workers/platform/cron-triggers</a>

</details>

<details>

<summary>triggers.email({ addresses: ["support@example.com"] }), (email, addresses reference)</summary>

<code>email</code>Builder<a href="#config-triggers-triggers-email-default">Link to email</a>

<code>email(options: EmailTriggerOptions): EmailTrigger;</code>

Email trigger — invokes this Worker for the configured Email Routing addresses.

<details>

<summary>Options (1)</summary>

<dl>

<dt><code>addresses: string[]</code></dt> <dd>Inbound Email Routing addresses handled by this Worker. Each entry is a literal recipient address (e.g. <code>"support@example.com"</code>) or a <code>@domain</code> catch-all (e.g. <code>"@example.com"</code>).</dd>

</dl></details>

<code>addresses</code>Required<a href="#config-triggers-triggers-email-default">Link to addresses</a>

<code>addresses: string[]</code>

Inbound Email Routing addresses handled by this Worker. Each entry is a literal recipient address (e.g. <code>"support@example.com"</code>) or a <code>@domain</code> catch-all (e.g. <code>"@example.com"</code>).

</details>

<details>

<summary>triggers.connect({ protocol: "tcp", port: 5432 }), (connect, protocol, port reference)</summary>

<code>connect</code>Builder<a href="#config-triggers-triggers-connect-default">Link to connect</a>

<code>connect(options: ConnectTriggerOptions): ConnectTrigger;</code>

Connect trigger — invokes this Worker's <code>connect(socket, env, ctx)</code> handler for raw socket connections received on the configured protocol/port.

<details>

<summary>Options (6)</summary>

<dl>

<dt><code>port: number</code></dt> <dd>The port to listen on.</dd>

<dt><code>address?: string</code></dt> <dd>The address to bind to. Defaults to <code>127.0.0.1</code>.</dd>

<dt><code>protocol: "tcp"</code></dt> <dd></dd>

<dt><code>protocol: "udp"</code></dt> <dd></dd>

<dt><code>idleTimeoutMs?: number</code></dt> <dd>The idle timeout in milliseconds after which a peer flow is closed.</dd>

<dt><code>maxPendingBytes?: number</code></dt> <dd>The maximum number of pending datagram bytes per peer flow.</dd></dl></details>

<code>protocol</code>Required<a href="#config-triggers-triggers-connect-default">Link to protocol</a>

<code>protocol: "tcp" | "udp"</code>

The type definition does not include a description.

<code>port</code>Required<a href="#config-triggers-triggers-connect-default">Link to port</a>

<code>port: number</code>

The port to listen on.

</details>

],

},

});

The builder provides these trigger methods:

Method Event
fetch A route receives HTTP traffic
queue A queue delivers messages
scheduled A cron schedule runs
email Email Routing receives matching email
connect TCP or UDP traffic reaches the configured port

Set protocol to "tcp" or "udp" in triggers.connect(). UDP triggers also accept idleTimeoutMs and maxPendingBytes.

Triggers control which events invoke a Worker. They do not create env bindings. TypeScript checks the options for each trigger, while Workers runtime types provide the handler signatures.

Declare exports

The worker.exports map describes how the platform manages module exports. Each key must match the default export or a named class exported by the entrypoint.

Use exports.worker() to configure a WorkerEntrypoint. It accepts an optional per-entrypoint cache setting. Use exports.durableObject() to declare a Durable Object class and its lifecycle state. Use exports.workflow() to declare a class that extends WorkflowEntrypoint.

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-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-exports-cloudflareconfig-worker">Link to worker</a>

<code>worker?: ConfigInput<WorkerConfig></code>

The Worker defined by this configuration.

</details>

<details>

<summary>name: "example-worker", (name reference)</summary>

<code>name</code>Required<a href="#config-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-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-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>exports: { (exports reference)</summary>

<code>exports</code>Optional<a href="#config-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>default: exports.worker({ cache: { enabled: false } }), (worker, cache, enabled reference)</summary>

<code>worker</code>Builder<a href="#config-exports-exports-worker-default">Link to worker</a>

<code>worker(options?: WorkerEntrypointExportOptions): WorkerEntrypointExport;</code>

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">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">Link to enabled</a>

<code>enabled: boolean</code>

Whether cache is enabled for this entrypoint.

</details>

<details>

<summary>Admin: exports.worker({ cache: { enabled: true } }), (worker, cache, enabled reference)</summary>

<code>worker</code>Builder<a href="#config-exports-exports-worker-default-2">Link to worker</a>

<code>worker(options?: WorkerEntrypointExportOptions): WorkerEntrypointExport;</code>