Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental worker-to-worker bindings support #166

Merged
merged 6 commits into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shiny-crews-warn.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions packages/wrangler/src/api/form_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ interface WorkerMetadata {
class_name: string;
script_name?: string;
}
| {
type: "service";
name: string;
service: string;
environment: string;
}
)[];
}

Expand Down Expand Up @@ -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,
Electroid marked this conversation as resolved.
Show resolved Hide resolved
environment,
});
});

const metadata: WorkerMetadata = {
...(mainType !== "commonjs" ? { main_module: name } : { body_part: name }),
bindings: metadataBindings,
Expand Down
10 changes: 10 additions & 0 deletions packages/wrangler/src/api/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion packages/wrangler/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ type DurableObject = {
script_name?: string;
};

type Service = {
name: string;
service: string;
environment: string;
};

type Build = {
command?: string;
cwd?: string;
Expand Down Expand Up @@ -86,6 +92,8 @@ type Env = {
vars?: Vars;
durable_objects?: { bindings: DurableObject[] };
kv_namespaces?: KVNamespace[];
experimental_services?: Service[];
migrations?: DurableObjectMigration[];
usage_model?: UsageModel; // inherited
};

Expand All @@ -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
Expand Down
14 changes: 13 additions & 1 deletion packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ async function readConfig(path?: string): Promise<Config> {
});
});

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
Expand All @@ -93,6 +98,12 @@ async function readConfig(path?: string): Promise<Config> {
});
});

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

Expand Down Expand Up @@ -528,6 +539,7 @@ export async function main(argv: string[]): Promise<void> {
),
vars: envRootObj.vars,
durable_objects: envRootObj.durable_objects,
services: envRootObj.experimental_services,
}}
/>
);
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export default async function publish(props: Props): Promise<void> {
),
vars: envRootObj.vars,
durable_objects: envRootObj.durable_objects,
services: envRootObj.experimental_services,
};

const worker: CfWorkerInit = {
Expand Down