Skip to content

Commit

Permalink
feat: optionally send zone_name with routes
Browse files Browse the repository at this point in the history
A followup to #778, this lets you send an optional `zone_name` with routes. This is particularly useful when using ssl for saas (https://developers.cloudflare.com/ssl/ssl-for-saas/).
  • Loading branch information
threepointone committed Apr 14, 2022
1 parent 34552d9 commit 2113403
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 23 deletions.
51 changes: 47 additions & 4 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,12 +643,17 @@ describe("normalizeAndValidateConfig()", () => {
"example.com/*",
{ pattern: 123, zone_id: "zone_id_1" },
{ pattern: "route_2", zone_id: 123 },
{ pattern: "route_2", zone_name: 123 },
{ pattern: "route_3" },
{ zone_id: "zone_id_4" },
{ zone_name: "zone_name_4" },
{ pattern: undefined },
{ pattern: "route_5", zone_id: "zone_id_5", some_other_key: 123 },
{ pattern: "route_5", zone_name: "zone_name_5", some_other_key: 123 },
// this one's valid too
{ pattern: "route_6", zone_id: "zone_id_6" },
// as well as this one
{ pattern: "route_6", zone_name: "zone_name_6" },
],
route: 888,
jsx_factory: 999,
Expand All @@ -674,8 +679,43 @@ describe("normalizeAndValidateConfig()", () => {
expect(diagnostics.hasWarnings()).toBe(false);
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
- Expected \\"route\\" to be either a string or an object with shape {pattern: string, zone_id: string}, but got 888.
- Expected \\"routes\\" to be an array of either strings or objects with the shape {pattern: string, zone_id: string}, but these weren't valid: [666,777,{\\"pattern\\":123,\\"zone_id\\":\\"zone_id_1\\"},{\\"pattern\\":\\"route_2\\",\\"zone_id\\":123},{\\"pattern\\":\\"route_3\\"},{\\"zone_id\\":\\"zone_id_4\\"},{},{\\"pattern\\":\\"route_5\\",\\"zone_id\\":\\"zone_id_5\\",\\"some_other_key\\":123}].
- Expected \\"route\\" to be either a string, or an object with shape { pattern, zone_id | zone_name }, but got 888.
- Expected \\"routes\\" to be an array of either strings or objects with the shape { pattern, zone_id | zone_name }, but these weren't valid: [
666,
777,
{
\\"pattern\\": 123,
\\"zone_id\\": \\"zone_id_1\\"
},
{
\\"pattern\\": \\"route_2\\",
\\"zone_id\\": 123
},
{
\\"pattern\\": \\"route_2\\",
\\"zone_name\\": 123
},
{
\\"pattern\\": \\"route_3\\"
},
{
\\"zone_id\\": \\"zone_id_4\\"
},
{
\\"zone_name\\": \\"zone_name_4\\"
},
{},
{
\\"pattern\\": \\"route_5\\",
\\"zone_id\\": \\"zone_id_5\\",
\\"some_other_key\\": 123
},
{
\\"pattern\\": \\"route_5\\",
\\"zone_name\\": \\"zone_name_5\\",
\\"some_other_key\\": 123
}
].
- Expected exactly one of the following fields [\\"routes\\",\\"route\\"].
- Expected \\"workers_dev\\" to be of type boolean but got \\"BAD\\".
- Expected \\"build.command\\" to be of type string but got 1444.
Expand Down Expand Up @@ -1816,8 +1856,11 @@ describe("normalizeAndValidateConfig()", () => {
"Processing wrangler configuration:
- \\"env.ENV1\\" environment configuration
- Expected \\"route\\" to be either a string or an object with shape {pattern: string, zone_id: string}, but got 888.
- Expected \\"routes\\" to be an array of either strings or objects with the shape {pattern: string, zone_id: string}, but these weren't valid: [666,777].
- Expected \\"route\\" to be either a string, or an object with shape { pattern, zone_id | zone_name }, but got 888.
- Expected \\"routes\\" to be an array of either strings or objects with the shape { pattern, zone_id | zone_name }, but these weren't valid: [
666,
777
].
- Expected exactly one of the following fields [\\"routes\\",\\"route\\"].
- Expected \\"workers_dev\\" to be of type boolean but got \\"BAD\\".
- Expected \\"build.command\\" to be of type string but got 1444.
Expand Down
17 changes: 17 additions & 0 deletions packages/wrangler/src/__tests__/dev.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,23 @@ describe("wrangler dev", () => {
});
});

it("should, when provided, use a zone_name to get a zone_id", async () => {
writeWranglerToml({
main: "index.js",
routes: [
{ pattern: "https://some-domain.com/*", zone_name: "some-zone.com" },
],
});
fs.writeFileSync("index.js", `export default {};`);
mockGetZones("some-zone.com", [{ id: "a-zone-id" }]);
await runWrangler("dev");
expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
// note that it uses the provided zone_name as a host too
host: "some-zone.com",
id: "a-zone-id",
});
});

it("given a long host, it should use the longest subdomain that resolves to a zone", async () => {
writeWranglerToml({
main: "index.js",
Expand Down
36 changes: 29 additions & 7 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,15 @@ describe("publish", () => {
await runWrangler("publish ./index");
});

it("should publish to a route with a pattern/zone_id combo", async () => {
it("should publish to a route with a pattern/{zone_id|zone_name} combo", async () => {
writeWranglerToml({
routes: [
"some-example.com/some-route/*",
{ pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
{
pattern: "*another-boring-website.com",
zone_name: "some-zone.com",
},
{ pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
"more-examples.com/*",
],
Expand All @@ -332,6 +336,10 @@ describe("publish", () => {
routes: [
"some-example.com/some-route/*",
{ pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
{
pattern: "*another-boring-website.com",
zone_name: "some-zone.com",
},
{ pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
"more-examples.com/*",
],
Expand All @@ -343,21 +351,26 @@ describe("publish", () => {
"out": "Uploaded test-name (TIMINGS)
Published test-name (TIMINGS)
some-example.com/some-route/*
*a-boring-website.com (zone: 54sdf7fsda)
example.com/some-route/* (zone: JGHFHG654gjcj)
*a-boring-website.com (zone id: 54sdf7fsda)
*another-boring-website.com (zone name: some-zone.com)
example.com/some-route/* (zone id: JGHFHG654gjcj)
more-examples.com/*",
"warn": "",
}
`);
});

it("should publish to a route with a pattern/zone_id combo (service environments)", async () => {
it("should publish to a route with a pattern/{zone_id|zone_name} combo (service environments)", async () => {
writeWranglerToml({
env: {
staging: {
routes: [
"some-example.com/some-route/*",
{ pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
{
pattern: "*another-boring-website.com",
zone_name: "some-zone.com",
},
{ pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
"more-examples.com/*",
],
Expand All @@ -380,6 +393,10 @@ describe("publish", () => {
routes: [
"some-example.com/some-route/*",
{ pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
{
pattern: "*another-boring-website.com",
zone_name: "some-zone.com",
},
{ pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
"more-examples.com/*",
],
Expand All @@ -393,8 +410,9 @@ describe("publish", () => {
"out": "Uploaded test-name (staging) (TIMINGS)
Published test-name (staging) (TIMINGS)
some-example.com/some-route/*
*a-boring-website.com (zone: 54sdf7fsda)
example.com/some-route/* (zone: JGHFHG654gjcj)
*a-boring-website.com (zone id: 54sdf7fsda)
*another-boring-website.com (zone name: some-zone.com)
example.com/some-route/* (zone id: JGHFHG654gjcj)
more-examples.com/*",
"warn": "",
}
Expand Down Expand Up @@ -3919,7 +3937,11 @@ function mockPublishRoutesRequest({
env = undefined,
legacyEnv = false,
}: {
routes: (string | { pattern: string; zone_id: string })[];
routes: (
| string
| { pattern: string; zone_id: string }
| { pattern: string; zone_name: string }
)[];
env?: string | undefined;
legacyEnv?: boolean | undefined;
}) {
Expand Down
8 changes: 7 additions & 1 deletion packages/wrangler/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,13 @@ export interface DevConfig {
/**
* Host to forward requests to, defaults to the host of the first route of project
*/
host: (string | { pattern: string; zone_id: string }) | undefined;
host:
| (
| string
| { pattern: string; zone_id: string }
| { pattern: string; zone_name: string }
)
| undefined;
}

export type RawDevConfig = Partial<DevConfig>;
Expand Down
16 changes: 14 additions & 2 deletions packages/wrangler/src/config/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ interface EnvironmentInheritable {
*
* @inheritable
*/
routes: (string | { pattern: string; zone_id: string })[] | undefined;
routes:
| (
| string
| { pattern: string; zone_id: string }
| { pattern: string; zone_name: string }
)[]
| undefined;

/**
* A route that your worker should be published to. Literally
Expand All @@ -85,7 +91,13 @@ interface EnvironmentInheritable {
*
* @inheritable
*/
route: (string | { pattern: string; zone_id: string }) | undefined;
route:
| (
| string
| { pattern: string; zone_id: string }
| { pattern: string; zone_name: string }
)
| undefined;

/**
* Path to a custom tsconfig
Expand Down
15 changes: 10 additions & 5 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,11 @@ function isValidRouteValue(item: unknown): boolean {
(typeof item === "object" &&
hasProperty(item, "pattern") &&
typeof item.pattern === "string" &&
hasProperty(item, "zone_id") &&
typeof item.zone_id === "string" &&
// it could have a zone_name
((hasProperty(item, "zone_name") &&
typeof item.zone_name === "string") ||
// or a zone_id
(hasProperty(item, "zone_id") && typeof item.zone_id === "string")) &&
Object.keys(item).length === 2))
);
}
Expand All @@ -484,7 +487,7 @@ function isValidRouteValue(item: unknown): boolean {
const isRoute: ValidatorFn = (diagnostics, field, value) => {
if (value !== undefined && !isValidRouteValue(value)) {
diagnostics.errors.push(
`Expected "${field}" to be either a string or an object with shape {pattern: string, zone_id: string}, but got ${JSON.stringify(
`Expected "${field}" to be either a string, or an object with shape { pattern, zone_id | zone_name }, but got ${JSON.stringify(
value
)}.`
);
Expand Down Expand Up @@ -514,8 +517,10 @@ const isRouteArray: ValidatorFn = (diagnostics, field, value) => {
}
if (invalidRoutes.length > 0) {
diagnostics.errors.push(
`Expected "${field}" to be an array of either strings or objects with the shape {pattern: string, zone_id: string}, but these weren't valid: ${JSON.stringify(
invalidRoutes
`Expected "${field}" to be an array of either strings or objects with the shape { pattern, zone_id | zone_name }, but these weren't valid: ${JSON.stringify(
invalidRoutes,
null,
2
)}.`
);
}
Expand Down
8 changes: 6 additions & 2 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -813,13 +813,17 @@ export async function main(argv: string[]): Promise<void> {
(config.routes && config.routes[0]);

let zoneId: string | undefined =
typeof hostLike === "object" ? hostLike.zone_id : undefined;
typeof hostLike === "object" && "zone_id" in hostLike
? hostLike.zone_id
: undefined;

const host =
typeof hostLike === "string"
? getHost(hostLike)
: typeof hostLike === "object"
? getHost(hostLike.pattern)
? "zone_name" in hostLike
? getHost(hostLike.zone_name)
: getHost(hostLike.pattern)
: undefined;

const hostPieces =
Expand Down
8 changes: 6 additions & 2 deletions packages/wrangler/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,18 @@ export default async function publish(props: Props): Promise<void> {
.map((route) =>
typeof route === "string"
? route
: `${route.pattern} (zone: ${route.zone_id})`
: "zone_id" in route
? `${route.pattern} (zone id: ${route.zone_id})`
: `${route.pattern} (zone name: ${route.zone_name})`
)
.concat([`...and ${routes.length - 10} more routes`]);
}
return routes.map((route) =>
typeof route === "string"
? route
: `${route.pattern} (zone: ${route.zone_id})`
: "zone_id" in route
? `${route.pattern} (zone id: ${route.zone_id})`
: `${route.pattern} (zone name: ${route.zone_name})`
);
})
);
Expand Down

0 comments on commit 2113403

Please sign in to comment.