import type { Context } from "../context.js"; import { type BundleProps } from "../esbuild/bundle.js"; import { Resource } from "../resource.js"; import { type CloudflareApiOptions } from "./api.js"; import type { Bindings } from "./bindings.js"; import type { Bound } from "./bound.js"; import { type EventSource } from "./event-source.js"; import type { SingleStepMigration } from "./worker-migration.js"; /** * Configuration options for static assets */ export interface AssetsConfig { /** * The contents of a _headers file (used to attach custom headers on asset responses) */ _headers?: string; /** * The contents of a _redirects file (used to apply redirects or proxy paths ahead of asset serving) */ _redirects?: string; /** * Determines the redirects and rewrites of requests for HTML content * @default "auto-trailing-slash" */ html_handling?: "auto-trailing-slash" | "force-trailing-slash" | "drop-trailing-slash" | "none"; /** * Determines the response when a request does not match a static asset, and there is no Worker script */ not_found_handling?: "none" | "404-page" | "single-page-application"; /** * When true, requests will always invoke the Worker script. * Otherwise, attempt to serve an asset matching the request, falling back to the Worker script. */ run_worker_first?: boolean; /** * When true and the incoming request matches an asset, that will be served instead of invoking the Worker script. * When false, requests will always invoke the Worker script. * @default true * @deprecated */ serve_directly?: boolean; } /** * Properties for creating or updating a Worker */ export interface WorkerProps extends CloudflareApiOptions { /** * The worker script content (JavaScript or WASM) * One of script, entryPoint, or bundle must be provided */ script?: string; /** * Path to the entry point file * Will be bundled using esbuild * One of script, entryPoint, or bundle must be provided */ entrypoint?: string; /** * Bundle options when using entryPoint * Ignored if bundle is provided */ bundle?: Omit; /** * Module format for the worker script * 'esm' - ECMAScript modules (default) * 'cjs' - CommonJS modules * @default 'esm' */ format?: "esm" | "cjs"; /** * Name for the worker * * @default id */ name?: string; /** * Bindings to attach to the worker */ bindings?: B; /** * Environment variables to attach to the worker * These will be converted to plain_text bindings */ env?: { [key: string]: string; }; /** * Whether to enable a workers.dev URL for this worker * If true, the worker will be available at {name}.{subdomain}.workers.dev * @default false */ url?: boolean; /** * Observability configuration for the worker * Controls whether worker logs are enabled * @default { enabled: true } */ observability?: { /** * Whether to enable worker logs * @default true */ enabled?: boolean; }; /** * Migrations to apply to the worker */ migrations?: SingleStepMigration; /** * Whether to adopt the Worker if it already exists when creating */ adopt?: boolean; /** * The compatibility date for the worker * @default "2025-04-26" */ compatibilityDate?: string; /** * The compatibility flags for the worker */ compatibilityFlags?: string[]; /** * Configuration for static assets */ assets?: AssetsConfig; /** * Cron expressions for the trigger. * Uses standard cron syntax (e.g. "0 0 * * *" for daily at midnight) * To clear all cron triggers, pass an empty array. * * @see https://developers.cloudflare.com/workers/configuration/cron-triggers/#examples */ crons?: string[]; /** * Event sources that this worker will consume. * Can include queues, streams, or other event sources. */ eventSources?: EventSource[]; } /** * Output returned after Worker creation/update */ export interface Worker extends Resource<"cloudflare::Worker">, Omit, "url"> { type: "service"; /** * The ID of the worker */ id: string; /** * The name of the worker */ name: string; /** * Time at which the worker was created */ createdAt: number; /** * Time at which the worker was last updated */ updatedAt: number; /** * The worker's URL if enabled * Format: {name}.{subdomain}.workers.dev */ url?: string; /** * The bindings that were created */ bindings: B | undefined; /** * Configuration for static assets */ assets?: AssetsConfig; Env: { [bindingName in keyof B]: Bound; }; /** * The compatibility date for the worker */ compatibilityDate: string; /** * The compatibility flags for the worker */ compatibilityFlags: string[]; } /** * A Cloudflare Worker is a serverless function that can be deployed to the Cloudflare network. * * @example * // Create a basic HTTP handler worker with custom domain routing * // and workers.dev URL: * const api = await Worker("api", { * name: "api-worker", * entrypoint: "./src/api.ts", * routes: ["api.example.com/*"], * url: true * }); * * @example * // Create a real-time chat worker using Durable Objects * // for state management: * const chatRooms = new DurableObjectNamespace("chat-rooms"); * const userStore = new DurableObjectNamespace("user-store"); * * const chat = await Worker("chat", { * name: "chat-worker", * entrypoint: "./src/chat.ts", * bindings: { * ROOMS: chatRooms, * USERS: userStore * }, * }); * * @example * // Create a worker with KV namespace for caching and data storage: * const cache = await KVNamespace("cache-store"); * const settings = await KVNamespace("user-settings"); * * const cacheWorker = await Worker("cache", { * name: "cache-worker", * entrypoint: "./src/cache.ts", * bindings: { * CACHE: cache, * SETTINGS: settings * } * }); * * @example * // Create a worker with R2 bucket for object storage: * const uploads = await R2Bucket("uploads", { * name: "user-uploads" * }); * const assets = await R2Bucket("assets", { * name: "static-assets", * allowPublicAccess: true * }); * * const storageWorker = await Worker("storage", { * name: "storage-worker", * entrypoint: "./src/storage.ts", * bindings: { * UPLOADS: uploads, * ASSETS: assets * } * }); * * @example * // Create a worker with static assets: * const staticAssets = await Assets("static", { * path: "./src/assets" * }); * * const frontendWorker = await Worker("frontend", { * name: "frontend-worker", * entrypoint: "./src/worker.ts", * bindings: { * ASSETS: staticAssets * } * }); * * @example * // Create a worker with scheduled cron triggers: * const cronWorker = await Worker("scheduled-tasks", { * name: "cron-worker", * entrypoint: "./src/scheduled.ts", * crons: ['* 15 * * *', '0 0 * * *', '0 12 * * MON'] * }) * * @see * https://developers.cloudflare.com/workers/ */ export declare const Worker: (((this: any, id: string, props?: {}) => never) & (new (_: never) => never)) | ((this: Context>>, id: string, props: WorkerProps) => Promise>); //# sourceMappingURL=worker.d.ts.map