diff --git a/.changeset/shiny-crews-warn.md b/.changeset/shiny-crews-warn.md new file mode 100644 index 000000000000..f4572e6c6eb5 --- /dev/null +++ b/.changeset/shiny-crews-warn.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +Add experimental support for worker-to-worker service bindings. This introduces a new field in configuration `experimental_services`, and serialises it when creating and uploading a worker definition. This is highly experimental, and doesn't work with `wrangler dev` yet. diff --git a/packages/wrangler/src/api/form_data.ts b/packages/wrangler/src/api/form_data.ts index 3adf347c3756..df7b921d93b4 100644 --- a/packages/wrangler/src/api/form_data.ts +++ b/packages/wrangler/src/api/form_data.ts @@ -44,6 +44,12 @@ interface WorkerMetadata { class_name: string; script_name?: string; } + | { + type: "service"; + name: string; + service: string; + environment: string; + } )[]; } @@ -88,6 +94,15 @@ export function toFormData(worker: CfWorkerInit): FormData { metadataBindings.push({ name: key, type: "plain_text", text: value }); }); + bindings.services?.forEach(({ name, service, environment }) => { + metadataBindings.push({ + name, + type: "service", + service, + environment, + }); + }); + const metadata: WorkerMetadata = { ...(mainType !== "commonjs" ? { main_module: name } : { body_part: name }), bindings: metadataBindings, diff --git a/packages/wrangler/src/api/worker.ts b/packages/wrangler/src/api/worker.ts index ec101e07b28c..382cad8487dd 100644 --- a/packages/wrangler/src/api/worker.ts +++ b/packages/wrangler/src/api/worker.ts @@ -86,6 +86,15 @@ interface CfDurableObject { script_name?: string; } +/** + * A Service. + */ +interface CfService { + name: string; + service: string; + environment: string; +} + export interface CfDurableObjectMigrations { old_tag?: string; new_tag: string; @@ -119,6 +128,7 @@ export interface CfWorkerInit { kv_namespaces?: CfKvNamespace[]; durable_objects?: { bindings: CfDurableObject[] }; vars?: CfVars; + services?: CfService[]; }; migrations: void | CfDurableObjectMigrations; compatibility_date: string | void; diff --git a/packages/wrangler/src/config.ts b/packages/wrangler/src/config.ts index 1af53f8114a8..6992fba7c74a 100644 --- a/packages/wrangler/src/config.ts +++ b/packages/wrangler/src/config.ts @@ -41,6 +41,12 @@ type DurableObject = { script_name?: string; }; +type Service = { + name: string; + service: string; + environment: string; +}; + type Build = { command?: string; cwd?: string; @@ -86,6 +92,8 @@ type Env = { vars?: Vars; durable_objects?: { bindings: DurableObject[] }; kv_namespaces?: KVNamespace[]; + experimental_services?: Service[]; + migrations?: DurableObjectMigration[]; usage_model?: UsageModel; // inherited }; @@ -108,9 +116,10 @@ export type Config = { jsx_factory?: string; // inherited jsx_fragment?: string; // inherited vars?: Vars; - migrations?: DurableObjectMigration[]; durable_objects?: { bindings: DurableObject[] }; kv_namespaces?: KVNamespace[]; + experimental_services?: Service[]; + migrations?: DurableObjectMigration[]; site?: Site; // inherited // we should use typescript to parse cron patterns triggers?: { crons: Cron[] }; // inherited diff --git a/packages/wrangler/src/index.tsx b/packages/wrangler/src/index.tsx index cc707993d018..b3e31f390b4b 100644 --- a/packages/wrangler/src/index.tsx +++ b/packages/wrangler/src/index.tsx @@ -79,7 +79,12 @@ async function readConfig(path?: string): Promise { }); }); - const mirroredFields = ["vars", "kv_namespaces", "durable_objects"]; + const mirroredFields = [ + "vars", + "kv_namespaces", + "durable_objects", + "experimental_services", + ]; Object.keys(config.env || {}).forEach((env) => { mirroredFields.forEach((field) => { // if it exists on top level, it should exist on env defns @@ -93,6 +98,12 @@ async function readConfig(path?: string): Promise { }); }); + if ("experimental_services" in config) { + console.warn( + "The experimental_services field is only for cloudflare internal usage right now, and is subject to change. Please do not use this on production projects" + ); + } + // todo: validate, add defaults // let's just do some basics for now @@ -528,6 +539,7 @@ export async function main(argv: string[]): Promise { ), vars: envRootObj.vars, durable_objects: envRootObj.durable_objects, + services: envRootObj.experimental_services, }} /> ); diff --git a/packages/wrangler/src/publish.ts b/packages/wrangler/src/publish.ts index 81e2daecd24c..b16f1d2f5163 100644 --- a/packages/wrangler/src/publish.ts +++ b/packages/wrangler/src/publish.ts @@ -209,6 +209,7 @@ export default async function publish(props: Props): Promise { ), vars: envRootObj.vars, durable_objects: envRootObj.durable_objects, + services: envRootObj.experimental_services, }; const worker: CfWorkerInit = {