Skip to content

Commit

Permalink
Simplify getting zone id from zone_name based on PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
romeupalos committed Oct 24, 2023
1 parent 2787a56 commit 3378f99
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 72 deletions.
16 changes: 0 additions & 16 deletions packages/wrangler/src/__tests__/dev.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,22 +369,6 @@ describe("wrangler dev", () => {
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should fail for non-existing zones", async () => {
writeWranglerToml({
main: "index.js",
routes: [
{
pattern: "https://subdomain.does-not-exist.com/*",
zone_name: "exists.com",
},
],
});
await fs.promises.writeFile("index.js", `export default {};`);
await expect(runWrangler("dev --remote")).rejects.toEqual(
new Error("Could not find zone for subdomain.does-not-exist.com")
);
});

it("should fail for non-existing zones, when falling back from */*", async () => {
writeWranglerToml({
main: "index.js",
Expand Down
10 changes: 3 additions & 7 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import * as metrics from "./metrics";
import { getAssetPaths, getSiteAssetPaths } from "./sites";
import { getAccountFromCache, loginOrRefreshIfRequired } from "./user";
import { collectKeyValues } from "./utils/collectKeyValues";
import {
getHostFromRoutePattern,
getZoneForRoute,
getZoneIdFromHost,
} from "./zones";
import { getHostFromRoute, getZoneForRoute, getZoneIdFromHost } from "./zones";
import {
DEFAULT_INSPECTOR_PORT,
DEFAULT_LOCAL_PORT,
Expand Down Expand Up @@ -668,7 +664,7 @@ async function getZoneIdHostAndRoutes(args: StartDevOptions, config: Config) {
}
if (!zoneId && routes) {
const firstRoute = routes[0];
const zone = await getZoneForRoute(firstRoute, true);
const zone = await getZoneForRoute(firstRoute);
if (zone) {
zoneId = zone.id;
host = zone.host;
Expand All @@ -677,7 +673,7 @@ async function getZoneIdHostAndRoutes(args: StartDevOptions, config: Config) {
} else if (!host) {
if (routes) {
const firstRoute = routes[0];
host = getHostFromRoutePattern(firstRoute);
host = getHostFromRoute(firstRoute);

// TODO(consider): do we need really need to do this? I've added the condition to throw to match the previous implicit behaviour of `new URL()` throwing upon invalid URLs, but could we just continue here without an inferred host?
if (host === undefined) {
Expand Down
61 changes: 12 additions & 49 deletions packages/wrangler/src/zones.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,6 @@ export interface Zone {
export function getHostFromRoute(route: Route): string | undefined {
let host: string | undefined;

if (typeof route === "string") {
host = getHostFromUrl(route);
} else if (typeof route === "object") {
host = getHostFromUrl(route.pattern);

if ("zone_name" in route) {
const hostFromZoneName = getHostFromUrl(route.zone_name);
if (hostFromZoneName && !host?.endsWith?.(`.${hostFromZoneName}`)) {
// Pattern is not a subdomain of zone. Use zone name as host instead.
host = hostFromZoneName;
}
}
}

return host;
}

/**
* Get the hostname on which to run a Worker.
*
* The most accurate place is usually
* `route.pattern`, as that includes any subdomains. For example:
* ```js
* {
* pattern: foo.example.com
* zone_name: example.com
* }
* ```
* However, in the case of patterns that _can't_ be parsed as a hostname
* (primarily the pattern `*/ /*`), we fall back to the `zone_name`
* (and in the absence of that return undefined).
* @param route
*/
export function getHostFromRoutePattern(route: Route): string | undefined {
let host: string | undefined;

if (typeof route === "string") {
host = getHostFromUrl(route);
} else if (typeof route === "object") {
Expand All @@ -84,19 +48,18 @@ export function getHostFromRoutePattern(route: Route): string | undefined {
* - We try to extract a host from it
* - We try to get a zone id from the host
*/
export async function getZoneForRoute(
route: Route,
usePattern = false
): Promise<Zone | undefined> {
const host = usePattern
? getHostFromRoutePattern(route)
: getHostFromRoute(route);
const id =
typeof route === "object" && "zone_id" in route
? route.zone_id
: host
? await getZoneIdFromHost(host)
: undefined;
export async function getZoneForRoute(route: Route): Promise<Zone | undefined> {
const host = getHostFromRoute(route);
let id: string | undefined;

if (typeof route === "object" && "zone_id" in route) {
id = route.zone_id;
} else if (typeof route === "object" && "zone_name" in route) {
id = await getZoneIdFromHost(route.zone_name);
} else if (host) {
id = await getZoneIdFromHost(host);
}

return id && host ? { id, host } : undefined;
}

Expand Down

0 comments on commit 3378f99

Please sign in to comment.