Skip to content

Commit

Permalink
feat(wrangler): Add support for placement hints (#6625)
Browse files Browse the repository at this point in the history
- Add hint field to smart placement configuration

Co-authored-by: Max Peterson <mpeterson@cloudflare.com>
  • Loading branch information
maxwellpeterson and Max Peterson committed Sep 11, 2024
1 parent 6523db2 commit 8dcd456
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/three-nails-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": minor
---

feature: Add support for placement hints

Adds the `hint` field to smart placement configuration. When set, placement hints will be used to decide where smart-placement-enabled Workers are run.
25 changes: 25 additions & 0 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3485,6 +3485,31 @@ describe("normalizeAndValidateConfig()", () => {
});
});

describe("[placement]", () => {
it(`should error if placement hint is set with placement mode "off"`, () => {
const { diagnostics } = normalizeAndValidateConfig(
{ placement: { mode: "off", hint: "wnam" } },
undefined,
{ env: undefined }
);

expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
- \\"placement.hint\\" cannot be set if \\"placement.mode\\" is not \\"smart\\""
`);
});

it(`should not error if placement hint is set with placement mode "smart"`, () => {
const { diagnostics } = normalizeAndValidateConfig(
{ placement: { mode: "smart", hint: "wnam" } },
undefined,
{ env: undefined }
);

expect(diagnostics.hasErrors()).toBe(false);
});
});

describe("(deprecated)", () => {
it("should remove and warn about deprecated properties", () => {
const rawConfig: RawConfig = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ function createWorkerBundleFormData(

// The upload API only accepts an empty string or no specified placement for the "off" mode.
const placement: CfPlacement | undefined =
config?.placement?.mode === "smart" ? { mode: "smart" } : undefined;
config?.placement?.mode === "smart"
? { mode: "smart", hint: config.placement.hint }
: undefined;

const worker: CfWorkerInit = {
name: mainModule.name,
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/config/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ interface EnvironmentInheritable {
*
* @inheritable
*/
placement: { mode: "off" | "smart" } | undefined;
placement: { mode: "off" | "smart"; hint?: string } | undefined;

/**
* Specify the directory of static assets to deploy/serve
Expand Down
12 changes: 12 additions & 0 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,18 @@ function normalizeAndValidatePlacement(
"string",
["off", "smart"]
);
validateOptionalProperty(
diagnostics,
"placement",
"hint",
rawEnv.placement.hint,
"string"
);
if (rawEnv.placement.hint && rawEnv.placement.mode !== "smart") {
diagnostics.errors.push(
`"placement.hint" cannot be set if "placement.mode" is not "smart"`
);
}
}

return inheritable(
Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,9 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m

// The upload API only accepts an empty string or no specified placement for the "off" mode.
const placement: CfPlacement | undefined =
config.placement?.mode === "smart" ? { mode: "smart" } : undefined;
config.placement?.mode === "smart"
? { mode: "smart", hint: config.placement.hint }
: undefined;

const entryPointName = path.basename(resolvedEntryPointPath);
const main: CfModule = {
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/deployment-bundle/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export interface CfDurableObjectMigrations {

export interface CfPlacement {
mode: "smart";
hint?: string;
}

export interface CfTailConsumer {
Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/versions/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m

// The upload API only accepts an empty string or no specified placement for the "off" mode.
const placement: CfPlacement | undefined =
config.placement?.mode === "smart" ? { mode: "smart" } : undefined;
config.placement?.mode === "smart"
? { mode: "smart", hint: config.placement.hint }
: undefined;

const entryPointName = path.basename(resolvedEntryPointPath);
const main = {
Expand Down

0 comments on commit 8dcd456

Please sign in to comment.