Skip to content

Commit ae62a5f

Browse files
committed
feat: optionally send zone_name with routes
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/). Fixes #793
1 parent b24aeb5 commit ae62a5f

File tree

9 files changed

+137
-24
lines changed

9 files changed

+137
-24
lines changed

.changeset/fast-news-dress.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
feat: optionally send `zone_name` with routes
6+
7+
A followup to https://github.com/cloudflare/wrangler2/pull/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/).
8+
9+
Fixes https://github.com/cloudflare/wrangler2/issues/793

packages/wrangler/src/__tests__/configuration.test.ts

+47-4
Original file line numberDiff line numberDiff line change
@@ -643,12 +643,17 @@ describe("normalizeAndValidateConfig()", () => {
643643
"example.com/*",
644644
{ pattern: 123, zone_id: "zone_id_1" },
645645
{ pattern: "route_2", zone_id: 123 },
646+
{ pattern: "route_2", zone_name: 123 },
646647
{ pattern: "route_3" },
647648
{ zone_id: "zone_id_4" },
649+
{ zone_name: "zone_name_4" },
648650
{ pattern: undefined },
649651
{ pattern: "route_5", zone_id: "zone_id_5", some_other_key: 123 },
652+
{ pattern: "route_5", zone_name: "zone_name_5", some_other_key: 123 },
650653
// this one's valid too
651654
{ pattern: "route_6", zone_id: "zone_id_6" },
655+
// as well as this one
656+
{ pattern: "route_6", zone_name: "zone_name_6" },
652657
],
653658
route: 888,
654659
jsx_factory: 999,
@@ -674,8 +679,43 @@ describe("normalizeAndValidateConfig()", () => {
674679
expect(diagnostics.hasWarnings()).toBe(false);
675680
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
676681
"Processing wrangler configuration:
677-
- Expected \\"route\\" to be either a string or an object with shape {pattern: string, zone_id: string}, but got 888.
678-
- 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}].
682+
- Expected \\"route\\" to be either a string, or an object with shape { pattern, zone_id | zone_name }, but got 888.
683+
- Expected \\"routes\\" to be an array of either strings or objects with the shape { pattern, zone_id | zone_name }, but these weren't valid: [
684+
666,
685+
777,
686+
{
687+
\\"pattern\\": 123,
688+
\\"zone_id\\": \\"zone_id_1\\"
689+
},
690+
{
691+
\\"pattern\\": \\"route_2\\",
692+
\\"zone_id\\": 123
693+
},
694+
{
695+
\\"pattern\\": \\"route_2\\",
696+
\\"zone_name\\": 123
697+
},
698+
{
699+
\\"pattern\\": \\"route_3\\"
700+
},
701+
{
702+
\\"zone_id\\": \\"zone_id_4\\"
703+
},
704+
{
705+
\\"zone_name\\": \\"zone_name_4\\"
706+
},
707+
{},
708+
{
709+
\\"pattern\\": \\"route_5\\",
710+
\\"zone_id\\": \\"zone_id_5\\",
711+
\\"some_other_key\\": 123
712+
},
713+
{
714+
\\"pattern\\": \\"route_5\\",
715+
\\"zone_name\\": \\"zone_name_5\\",
716+
\\"some_other_key\\": 123
717+
}
718+
].
679719
- Expected exactly one of the following fields [\\"routes\\",\\"route\\"].
680720
- Expected \\"workers_dev\\" to be of type boolean but got \\"BAD\\".
681721
- Expected \\"build.command\\" to be of type string but got 1444.
@@ -1816,8 +1856,11 @@ describe("normalizeAndValidateConfig()", () => {
18161856
"Processing wrangler configuration:
18171857
18181858
- \\"env.ENV1\\" environment configuration
1819-
- Expected \\"route\\" to be either a string or an object with shape {pattern: string, zone_id: string}, but got 888.
1820-
- 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].
1859+
- Expected \\"route\\" to be either a string, or an object with shape { pattern, zone_id | zone_name }, but got 888.
1860+
- Expected \\"routes\\" to be an array of either strings or objects with the shape { pattern, zone_id | zone_name }, but these weren't valid: [
1861+
666,
1862+
777
1863+
].
18211864
- Expected exactly one of the following fields [\\"routes\\",\\"route\\"].
18221865
- Expected \\"workers_dev\\" to be of type boolean but got \\"BAD\\".
18231866
- Expected \\"build.command\\" to be of type string but got 1444.

packages/wrangler/src/__tests__/dev.test.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,23 @@ describe("wrangler dev", () => {
233233
});
234234
});
235235

236+
it("should, when provided, use a zone_name to get a zone_id", async () => {
237+
writeWranglerToml({
238+
main: "index.js",
239+
routes: [
240+
{ pattern: "https://some-domain.com/*", zone_name: "some-zone.com" },
241+
],
242+
});
243+
fs.writeFileSync("index.js", `export default {};`);
244+
mockGetZones("some-zone.com", [{ id: "a-zone-id" }]);
245+
await runWrangler("dev");
246+
expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
247+
// note that it uses the provided zone_name as a host too
248+
host: "some-zone.com",
249+
id: "a-zone-id",
250+
});
251+
});
252+
236253
it("given a long host, it should use the longest subdomain that resolves to a zone", async () => {
237254
writeWranglerToml({
238255
main: "index.js",

packages/wrangler/src/__tests__/publish.test.ts

+27-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { runInTempDir } from "./helpers/run-in-tmp";
1717
import { runWrangler } from "./helpers/run-wrangler";
1818
import { writeWorkerSource } from "./helpers/write-worker-source";
1919
import writeWranglerToml from "./helpers/write-wrangler-toml";
20+
import type { Config } from "../config";
2021
import type { WorkerMetadata } from "../create-worker-upload-form";
2122
import type { KVNamespaceInfo } from "../kv";
2223
import type { CfWorkerInit } from "../worker";
@@ -317,11 +318,15 @@ describe("publish", () => {
317318
await runWrangler("publish ./index");
318319
});
319320

320-
it("should publish to a route with a pattern/zone_id combo", async () => {
321+
it("should publish to a route with a pattern/{zone_id|zone_name} combo", async () => {
321322
writeWranglerToml({
322323
routes: [
323324
"some-example.com/some-route/*",
324325
{ pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
326+
{
327+
pattern: "*another-boring-website.com",
328+
zone_name: "some-zone.com",
329+
},
325330
{ pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
326331
"more-examples.com/*",
327332
],
@@ -333,6 +338,10 @@ describe("publish", () => {
333338
routes: [
334339
"some-example.com/some-route/*",
335340
{ pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
341+
{
342+
pattern: "*another-boring-website.com",
343+
zone_name: "some-zone.com",
344+
},
336345
{ pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
337346
"more-examples.com/*",
338347
],
@@ -344,21 +353,26 @@ describe("publish", () => {
344353
"out": "Uploaded test-name (TIMINGS)
345354
Published test-name (TIMINGS)
346355
some-example.com/some-route/*
347-
*a-boring-website.com (zone: 54sdf7fsda)
348-
example.com/some-route/* (zone: JGHFHG654gjcj)
356+
*a-boring-website.com (zone id: 54sdf7fsda)
357+
*another-boring-website.com (zone name: some-zone.com)
358+
example.com/some-route/* (zone id: JGHFHG654gjcj)
349359
more-examples.com/*",
350360
"warn": "",
351361
}
352362
`);
353363
});
354364

355-
it("should publish to a route with a pattern/zone_id combo (service environments)", async () => {
365+
it("should publish to a route with a pattern/{zone_id|zone_name} combo (service environments)", async () => {
356366
writeWranglerToml({
357367
env: {
358368
staging: {
359369
routes: [
360370
"some-example.com/some-route/*",
361371
{ pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
372+
{
373+
pattern: "*another-boring-website.com",
374+
zone_name: "some-zone.com",
375+
},
362376
{ pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
363377
"more-examples.com/*",
364378
],
@@ -381,6 +395,10 @@ describe("publish", () => {
381395
routes: [
382396
"some-example.com/some-route/*",
383397
{ pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
398+
{
399+
pattern: "*another-boring-website.com",
400+
zone_name: "some-zone.com",
401+
},
384402
{ pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
385403
"more-examples.com/*",
386404
],
@@ -394,8 +412,9 @@ describe("publish", () => {
394412
"out": "Uploaded test-name (staging) (TIMINGS)
395413
Published test-name (staging) (TIMINGS)
396414
some-example.com/some-route/*
397-
*a-boring-website.com (zone: 54sdf7fsda)
398-
example.com/some-route/* (zone: JGHFHG654gjcj)
415+
*a-boring-website.com (zone id: 54sdf7fsda)
416+
*another-boring-website.com (zone name: some-zone.com)
417+
example.com/some-route/* (zone id: JGHFHG654gjcj)
399418
more-examples.com/*",
400419
"warn": "",
401420
}
@@ -3886,11 +3905,11 @@ function mockUpdateWorkerRequest({
38863905
}
38873906

38883907
function mockPublishRoutesRequest({
3889-
routes,
3908+
routes = [],
38903909
env = undefined,
38913910
legacyEnv = false,
38923911
}: {
3893-
routes: (string | { pattern: string; zone_id: string })[];
3912+
routes: Config["routes"];
38943913
env?: string | undefined;
38953914
legacyEnv?: boolean | undefined;
38963915
}) {

packages/wrangler/src/config/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export interface DevConfig {
185185
/**
186186
* Host to forward requests to, defaults to the host of the first route of project
187187
*/
188-
host: (string | { pattern: string; zone_id: string }) | undefined;
188+
host: Config["route"];
189189
}
190190

191191
export type RawDevConfig = Partial<DevConfig>;

packages/wrangler/src/config/environment.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ interface EnvironmentInheritable {
7474
*
7575
* @inheritable
7676
*/
77-
routes: (string | { pattern: string; zone_id: string })[] | undefined;
77+
routes:
78+
| (
79+
| string
80+
| { pattern: string; zone_id: string }
81+
| { pattern: string; zone_name: string }
82+
)[]
83+
| undefined;
7884

7985
/**
8086
* A route that your worker should be published to. Literally
@@ -85,7 +91,13 @@ interface EnvironmentInheritable {
8591
*
8692
* @inheritable
8793
*/
88-
route: (string | { pattern: string; zone_id: string }) | undefined;
94+
route:
95+
| (
96+
| string
97+
| { pattern: string; zone_id: string }
98+
| { pattern: string; zone_name: string }
99+
)
100+
| undefined;
89101

90102
/**
91103
* Path to a custom tsconfig

packages/wrangler/src/config/validation.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,11 @@ function isValidRouteValue(item: unknown): boolean {
472472
(typeof item === "object" &&
473473
hasProperty(item, "pattern") &&
474474
typeof item.pattern === "string" &&
475-
hasProperty(item, "zone_id") &&
476-
typeof item.zone_id === "string" &&
475+
// it could have a zone_name
476+
((hasProperty(item, "zone_name") &&
477+
typeof item.zone_name === "string") ||
478+
// or a zone_id
479+
(hasProperty(item, "zone_id") && typeof item.zone_id === "string")) &&
477480
Object.keys(item).length === 2))
478481
);
479482
}
@@ -484,7 +487,7 @@ function isValidRouteValue(item: unknown): boolean {
484487
const isRoute: ValidatorFn = (diagnostics, field, value) => {
485488
if (value !== undefined && !isValidRouteValue(value)) {
486489
diagnostics.errors.push(
487-
`Expected "${field}" to be either a string or an object with shape {pattern: string, zone_id: string}, but got ${JSON.stringify(
490+
`Expected "${field}" to be either a string, or an object with shape { pattern, zone_id | zone_name }, but got ${JSON.stringify(
488491
value
489492
)}.`
490493
);
@@ -514,8 +517,10 @@ const isRouteArray: ValidatorFn = (diagnostics, field, value) => {
514517
}
515518
if (invalidRoutes.length > 0) {
516519
diagnostics.errors.push(
517-
`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(
518-
invalidRoutes
520+
`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(
521+
invalidRoutes,
522+
null,
523+
2
519524
)}.`
520525
);
521526
}

packages/wrangler/src/index.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -813,13 +813,17 @@ export async function main(argv: string[]): Promise<void> {
813813
(config.routes && config.routes[0]);
814814

815815
let zoneId: string | undefined =
816-
typeof hostLike === "object" ? hostLike.zone_id : undefined;
816+
typeof hostLike === "object" && "zone_id" in hostLike
817+
? hostLike.zone_id
818+
: undefined;
817819

818820
const host =
819821
typeof hostLike === "string"
820822
? getHost(hostLike)
821823
: typeof hostLike === "object"
822-
? getHost(hostLike.pattern)
824+
? "zone_name" in hostLike
825+
? getHost(hostLike.zone_name)
826+
: getHost(hostLike.pattern)
823827
: undefined;
824828

825829
const hostPieces =

packages/wrangler/src/publish.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,18 @@ export default async function publish(props: Props): Promise<void> {
319319
.map((route) =>
320320
typeof route === "string"
321321
? route
322-
: `${route.pattern} (zone: ${route.zone_id})`
322+
: "zone_id" in route
323+
? `${route.pattern} (zone id: ${route.zone_id})`
324+
: `${route.pattern} (zone name: ${route.zone_name})`
323325
)
324326
.concat([`...and ${routes.length - 10} more routes`]);
325327
}
326328
return routes.map((route) =>
327329
typeof route === "string"
328330
? route
329-
: `${route.pattern} (zone: ${route.zone_id})`
331+
: "zone_id" in route
332+
? `${route.pattern} (zone id: ${route.zone_id})`
333+
: `${route.pattern} (zone name: ${route.zone_name})`
330334
);
331335
})
332336
);

0 commit comments

Comments
 (0)