Skip to content

Commit

Permalink
feat: bind a durable object by environment (#1019)
Browse files Browse the repository at this point in the history
For durable objects, instead of just `{ name, class_name, script_name}`, this lets you bind by environment as well, like so `{ name, class_name, script_name, environment }`.

Fixes #996
  • Loading branch information
threepointone authored May 16, 2022
1 parent 02a2cae commit 5816eba
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 8 deletions.
9 changes: 9 additions & 0 deletions .changeset/sweet-worms-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

feat: bind a durable object by environment

For durable objects, instead of just `{ name, class_name, script_name}`, this lets you bind by environment as well, like so `{ name, class_name, script_name, environment }`.

Fixes https://github.com/cloudflare/wrangler2/issues/996
45 changes: 41 additions & 4 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,12 @@ describe("normalizeAndValidateConfig()", () => {
class_name: "CLASS2",
script_name: "SCRIPT2",
},
{
name: "DO_BINDING_3",
class_name: "CLASS3",
script_name: "SCRIPT3",
environment: "ENV3",
},
],
},
kv_namespaces: [
Expand Down Expand Up @@ -1106,13 +1112,29 @@ describe("normalizeAndValidateConfig()", () => {
durable_objects: {
bindings: [
{},
{ name: "VALID" },
{ name: "MISSING_CLASS" },
{ name: 1555, class_name: 1666 },
{
name: 1777,
class_name: 1888,
script_name: 1999,
},
{
name: "SOMENAME",
class_name: "SomeClass",
environment: "staging",
},
{
name: 1778,
class_name: 1889,
script_name: 1992,
environment: 2111,
},
{
name: 1772,
class_name: 1883,
environment: 2112,
},
],
},
} as unknown as RawConfig,
Expand All @@ -1133,7 +1155,7 @@ describe("normalizeAndValidateConfig()", () => {
- binding should have a string \\"name\\" field.
- binding should have a string \\"class_name\\" field.
- \\"durable_objects.bindings[1]\\": {\\"name\\":\\"VALID\\"}
- \\"durable_objects.bindings[1]\\": {\\"name\\":\\"MISSING_CLASS\\"}
- binding should have a string \\"class_name\\" field.
- \\"durable_objects.bindings[2]\\": {\\"name\\":1555,\\"class_name\\":1666}
Expand All @@ -1143,7 +1165,22 @@ describe("normalizeAndValidateConfig()", () => {
- \\"durable_objects.bindings[3]\\": {\\"name\\":1777,\\"class_name\\":1888,\\"script_name\\":1999}
- binding should have a string \\"name\\" field.
- binding should have a string \\"class_name\\" field.
- binding should, optionally, have a string \\"script_name\\" field."
- the field \\"script_name\\", when present, should be a string.
- \\"durable_objects.bindings[4]\\": {\\"name\\":\\"SOMENAME\\",\\"class_name\\":\\"SomeClass\\",\\"environment\\":\\"staging\\"}
- binding should have a \\"script_name\\" field if \\"environment\\" is present.
- \\"durable_objects.bindings[5]\\": {\\"name\\":1778,\\"class_name\\":1889,\\"script_name\\":1992,\\"environment\\":2111}
- binding should have a string \\"name\\" field.
- binding should have a string \\"class_name\\" field.
- the field \\"script_name\\", when present, should be a string.
- the field \\"environment\\", when present, should be a string.
- \\"durable_objects.bindings[6]\\": {\\"name\\":1772,\\"class_name\\":1883,\\"environment\\":2112}
- binding should have a string \\"name\\" field.
- binding should have a string \\"class_name\\" field.
- the field \\"environment\\", when present, should be a string.
- binding should have a \\"script_name\\" field if \\"environment\\" is present."
`);
});
});
Expand Down Expand Up @@ -2304,7 +2341,7 @@ describe("normalizeAndValidateConfig()", () => {
- \\"env.ENV1.durable_objects.bindings[3]\\": {\\"name\\":1777,\\"class_name\\":1888,\\"script_name\\":1999}
- binding should have a string \\"name\\" field.
- binding should have a string \\"class_name\\" field.
- binding should, optionally, have a string \\"script_name\\" field."
- the field \\"script_name\\", when present, should be a string."
`);
});
});
Expand Down
2 changes: 2 additions & 0 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3106,6 +3106,7 @@ addEventListener('fetch', event => {});`
name: "DURABLE_OBJECT_TWO",
class_name: "AnotherDurableObject",
script_name: "another-durable-object-worker",
environment: "staging",
},
],
},
Expand Down Expand Up @@ -3182,6 +3183,7 @@ addEventListener('fetch', event => {});`
},
{
class_name: "AnotherDurableObject",
environment: "staging",
name: "DURABLE_OBJECT_TWO",
script_name: "another-durable-object-worker",
type: "durable_object_namespace",
Expand Down
2 changes: 2 additions & 0 deletions packages/wrangler/src/config/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ interface EnvironmentNonInheritable {
class_name: string;
/** The script where the Durable Object is defined (if it's external to this worker) */
script_name?: string;
/** The service environment of the script_name to bind to */
environment?: string;
}[];
};

Expand Down
20 changes: 17 additions & 3 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ const validateRule: ValidatorFn = (diagnostics, field, value) => {

if (!isOptionalProperty(rule, "fallthrough", "boolean")) {
diagnostics.errors.push(
`binding should, optionally, have a boolean "fallthrough" field.`
`the field "fallthrough", when present, should be a boolean.`
);
isValid = false;
}
Expand Down Expand Up @@ -1135,7 +1135,7 @@ const validateDurableObjectBinding: ValidatorFn = (
return false;
}

// Durable Object bindings must have a name and class_name, and optionally a script_name.
// Durable Object bindings must have a name and class_name, and optionally a script_name and an environment.
let isValid = true;
if (!isRequiredProperty(value, "name", "string")) {
diagnostics.errors.push(`binding should have a string "name" field.`);
Expand All @@ -1147,7 +1147,21 @@ const validateDurableObjectBinding: ValidatorFn = (
}
if (!isOptionalProperty(value, "script_name", "string")) {
diagnostics.errors.push(
`binding should, optionally, have a string "script_name" field.`
`the field "script_name", when present, should be a string.`
);
isValid = false;
}
// environment requires a script_name
if (!isOptionalProperty(value, "environment", "string")) {
diagnostics.errors.push(
`the field "environment", when present, should be a string.`
);
isValid = false;
}

if ("environment" in value && !("script_name" in value)) {
diagnostics.errors.push(
`binding should have a "script_name" field if "environment" is present.`
);
isValid = false;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/create-worker-upload-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface WorkerMetadata {
name: string;
class_name: string;
script_name?: string;
environment?: string;
}
| { type: "r2_bucket"; name: string; bucket_name: string }
)[];
Expand Down Expand Up @@ -76,12 +77,13 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData {
});

bindings.durable_objects?.bindings.forEach(
({ name, class_name, script_name }) => {
({ name, class_name, script_name, environment }) => {
metadataBindings.push({
name,
type: "durable_object_namespace",
class_name: class_name,
...(script_name && { script_name }),
...(environment && { environment }),
});
}
);
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ interface CfDurableObject {
name: string;
class_name: string;
script_name?: string;
environment?: string;
}

interface CfR2Bucket {
Expand Down

0 comments on commit 5816eba

Please sign in to comment.