WorkerEntrypoint
Source:
src/Cloudflare/Workers/WorkerEntrypoint.ts
Bind a specific WorkerEntrypoint class exported by another Worker.
Binding a Worker directly in env (env: { TARGET: worker }) targets
its default entrypoint. A Worker that exposes additional
WorkerEntrypoint classes — workerd treats every named class export of
an entry module as an entrypoint — is bound with WorkerEntrypoint,
which selects the class by name and can deliver ctx.props to it.
Defining the Target Entrypoint
Section titled “Defining the Target Entrypoint”Export a class extending Cloudflare’s native WorkerEntrypoint from
the target Worker’s module. This Api class defines the RPC methods
that callers can invoke through a named service binding.
import { WorkerEntrypoint } from "cloudflare:workers";
export class Api extends WorkerEntrypoint { async greet(name: string): Promise<string> { return `hello ${name}`; }}
export default { async fetch() { return new Response("ok"); },};Binding a Named Entrypoint
Section titled “Binding a Named Entrypoint”Import the exported Api class as a type and select its named export
with "Api". Pass its instance type (Api, not typeof Api) to get
Cloudflare’s native Service<Api> RPC client. Without a type argument,
the binding is a bare Fetcher; the entrypoint name alone cannot
identify the class’s type.
import * as Cloudflare from "alchemy/Cloudflare";import type { Api } from "./target/src/worker.ts";
const target = yield* Cloudflare.Worker("Target", { main: "./target/src/worker.ts",});
const caller = yield* Cloudflare.Worker("Caller", { main: "./caller/src/worker.ts", env: { API: Cloudflare.WorkerEntrypoint<Api>(target, "Api"), },});import type { CallerEnv } from "../../alchemy.run.ts";
export default { async fetch(request: Request, env: CallerEnv) { return new Response(await env.API.greet("alice")); },};Delivering ctx.props
Section titled “Delivering ctx.props”The options form attaches properties the target reads from
this.ctx.props — workerd’s per-binding configuration channel. Output
values resolve at deploy time.
env: { VENDOR: Cloudflare.WorkerEntrypoint(vendorWorker, { entrypoint: "Vendor", props: { baseUrl: site.url }, }),}