Skip to content

Commit

Permalink
bugfix: Allow route setting to be (#1352)
Browse files Browse the repository at this point in the history
Previously Wrangler1 behavior had allowed for . To keep parity it will be possible to set  in the config file and represent not setting a route, while providing a warning.

resolves #1329
  • Loading branch information
JacobMGEvans authored Jun 27, 2022
1 parent fdb4918 commit 4e03036
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .changeset/thirty-eagles-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"wrangler": patch
---

bugfix: Allow route setting to be `""`
Previously Wrangler1 behavior had allowed for `route = ""`. To keep parity it will be possible to set `route = ""` in the config file and represent not setting a route, while providing a warning.

resolves #1329
27 changes: 27 additions & 0 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,33 @@ describe("publish", () => {
await runWrangler("publish ./index");
});

it("should publish with an empty string route", async () => {
writeWranglerToml({
route: "",
});
writeWorkerSource();
mockUpdateWorkerRequest({ enabled: false });
mockUploadWorkerRequest({ expectedType: "esm" });
mockSubDomainRequest();
mockPublishRoutesRequest({ routes: [] });
await runWrangler("publish ./index");
expect(std).toMatchInlineSnapshot(`
Object {
"debug": "",
"err": "",
"out": "Total Upload: 0xx KiB / gzip: 0xx KiB
Uploaded test-name (TIMINGS)
Published test-name (TIMINGS)
test-name.test-sub-domain.workers.dev",
"warn": "▲ [WARNING] Processing wrangler.toml configuration:
- Route assignment \\"\\" will be ignored.
",
}
`);
});

it("should publish to a route with a pattern/{zone_id|zone_name} combo", async () => {
writeWranglerToml({
routes: [
Expand Down
25 changes: 22 additions & 3 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,25 @@ function isValidRouteValue(item: unknown): boolean {
return false;
}

/**
* Normalize empty string to `undefined` by mutating rawEnv.route value.
* As part of backward compatibility with Wrangler1 converting empty string to `undefined`
*/
function mutateEmptyStringRouteValue(
rawEnv: RawEnvironment,
diagnostics: Diagnostics
): RawEnvironment {
if (rawEnv["route"] === "") {
diagnostics.warnings.push(
`Route assignment ${JSON.stringify(rawEnv["route"])} will be ignored.`
);
rawEnv["route"] = undefined;
return rawEnv;
}

return rawEnv;
}

/**
* Validate that the field is a route.
*/
Expand Down Expand Up @@ -716,15 +735,15 @@ const isRouteArray: ValidatorFn = (diagnostics, field, value) => {
return invalidRoutes.length === 0;
};

function validateRoute(
function normalizeAndValidateRoute(
diagnostics: Diagnostics,
topLevelEnv: Environment | undefined,
rawEnv: RawEnvironment
): Config["route"] {
return inheritable(
diagnostics,
topLevelEnv,
rawEnv,
mutateEmptyStringRouteValue(rawEnv, diagnostics),
"route",
isRoute,
undefined
Expand Down Expand Up @@ -795,7 +814,7 @@ function normalizeAndValidateEnvironment(
experimental(diagnostics, rawEnv, "unsafe");
experimental(diagnostics, rawEnv, "services");

const route = validateRoute(diagnostics, topLevelEnv, rawEnv);
const route = normalizeAndValidateRoute(diagnostics, topLevelEnv, rawEnv);

const routes = validateRoutes(diagnostics, topLevelEnv, rawEnv);

Expand Down

0 comments on commit 4e03036

Please sign in to comment.