Skip to content

Commit

Permalink
fix: only show fetch warning if on old compatibility_date (#7038)
Browse files Browse the repository at this point in the history
* fix: only show fetch warning if on old compatibility_date

Now that we have the `allow_custom_ports` compatibility flag, we only need to show the fetch warnings when that flag is not enabled.

Fixes cloudflare/workerd#2955

* fix: typo
  • Loading branch information
petebacondarwin authored Nov 18, 2024
1 parent b4a0e74 commit e2e6912
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .changeset/shaggy-monkeys-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

fix: only show fetch warning if on old compatibility_date

Now that we have the `allow_custom_ports` compatibility flag, we only need to show the fetch warnings when that flag is not enabled.

Fixes https://github.com/cloudflare/workerd/issues/2955
28 changes: 28 additions & 0 deletions packages/wrangler/src/__tests__/check-fetch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import "vitest";
import { shouldCheckFetch } from "../deployment-bundle/bundle";

describe("shouldCheckFetch()", () => {
it("should be true for old compat date", () => {
expect(shouldCheckFetch("2024-09-01")).toBe(true);
});

it("should be false for new compat date", () => {
expect(shouldCheckFetch("2024-09-02")).toBe(false);
});

it("should be true for old compat date + old compat flag", () => {
expect(shouldCheckFetch("2024-09-01", ["ignore_custom_ports"])).toBe(true);
});

it("should be false for old compat date + new compat flag", () => {
expect(shouldCheckFetch("2024-09-01", ["allow_custom_ports"])).toBe(false);
});

it("should be true for new compat date + old compat flag", () => {
expect(shouldCheckFetch("2024-09-02", ["ignore_custom_ports"])).toBe(true);
});

it("should be false for new compat date + new compat flag", () => {
expect(shouldCheckFetch("2024-09-02", ["allow_custom_ports"])).toBe(false);
});
});
8 changes: 8 additions & 0 deletions packages/wrangler/src/api/pages/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { cwd } from "node:process";
import { File, FormData } from "undici";
import { fetchResult } from "../../cfetch";
import { readConfig } from "../../config";
import { shouldCheckFetch } from "../../deployment-bundle/bundle";
import { validateNodeCompatMode } from "../../deployment-bundle/node-compat";
import { FatalError } from "../../errors";
import { logger } from "../../logger";
Expand Down Expand Up @@ -186,6 +187,10 @@ export async function deploy({
config?.compatibility_date ?? deploymentConfig.compatibility_date,
config?.compatibility_flags ?? deploymentConfig.compatibility_flags
);
const checkFetch = shouldCheckFetch(
config?.compatibility_date ?? deploymentConfig.compatibility_date,
config?.compatibility_flags ?? deploymentConfig.compatibility_flags
);

/**
* Evaluate if this is an Advanced Mode or Pages Functions project. If Advanced Mode, we'll
Expand Down Expand Up @@ -221,6 +226,7 @@ export async function deploy({
local: false,
nodejsCompatMode,
defineNavigatorUserAgent,
checkFetch,
});

builtFunctions = readFileSync(
Expand Down Expand Up @@ -319,6 +325,7 @@ export async function deploy({
buildOutputDirectory: directory,
nodejsCompatMode,
defineNavigatorUserAgent,
checkFetch,
sourceMaps: sourceMaps,
});
} else if (_workerJS) {
Expand All @@ -337,6 +344,7 @@ export async function deploy({
onEnd: () => {},
nodejsCompatMode,
defineNavigatorUserAgent,
checkFetch,
});
} else {
await checkRawWorker(_workerPath, nodejsCompatMode, () => {});
Expand Down
11 changes: 9 additions & 2 deletions packages/wrangler/src/api/startDevWorker/BundlerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFileSync, realpathSync, writeFileSync } from "fs";
import path from "path";
import { watch } from "chokidar";
import { noBundleWorker } from "../../deploy/deploy";
import { bundleWorker } from "../../deployment-bundle/bundle";
import { bundleWorker, shouldCheckFetch } from "../../deployment-bundle/bundle";
import { getBundleType } from "../../deployment-bundle/bundle-type";
import {
createModuleCollector,
Expand Down Expand Up @@ -117,7 +117,10 @@ export class BundlerController extends Controller<BundlerControllerEventMap> {
minify: config.build.minify,
nodejsCompatMode: config.build.nodejsCompatMode,
define: config.build.define,
checkFetch: true,
checkFetch: shouldCheckFetch(
config.compatibilityDate,
config.compatibilityFlags
),
mockAnalyticsEngineDatasets:
bindings.analytics_engine_datasets ?? [],
alias: config.build.alias,
Expand Down Expand Up @@ -265,6 +268,10 @@ export class BundlerController extends Controller<BundlerControllerEventMap> {
onStart: () => {
this.emitBundleStartEvent(config);
},
checkFetch: shouldCheckFetch(
config.compatibilityDate,
config.compatibilityFlags
),
defineNavigatorUserAgent: isNavigatorDefined(
config.compatibilityDate,
config.compatibilityFlags
Expand Down
23 changes: 23 additions & 0 deletions packages/wrangler/src/deployment-bundle/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,26 @@ class BuildFailure extends Error {
super(message);
}
}

/**
* Whether to add middleware to check whether fetch requests use custom ports.
*
* This is controlled in the runtime by compatibility_flags:
* - `ignore_custom_ports` - check fetch
* - `allow_custom_ports` - do not check fetch
*
* `allow_custom_ports` became the default on 2024-09-02.
*/
export function shouldCheckFetch(
compatibilityDate: string = "2000-01-01", // Default to some arbitrary old date
compatibilityFlags: string[] = []
): boolean {
// Yes, the logic can be less verbose than this but doing it this way makes it very clear.
if (compatibilityFlags.includes("ignore_custom_ports")) {
return true;
}
if (compatibilityFlags.includes("allow_custom_ports")) {
return false;
}
return compatibilityDate < "2024-09-02";
}
4 changes: 3 additions & 1 deletion packages/wrangler/src/dev/use-esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export function runBuild(
projectRoot,
onStart,
defineNavigatorUserAgent,
checkFetch,
}: {
entry: Entry;
destination: string | undefined;
Expand All @@ -84,6 +85,7 @@ export function runBuild(
projectRoot: string | undefined;
onStart: () => void;
defineNavigatorUserAgent: boolean;
checkFetch: boolean;
},
setBundle: (
cb: (previous: EsbuildBundle | undefined) => EsbuildBundle
Expand Down Expand Up @@ -158,7 +160,6 @@ export function runBuild(
workflowBindings: workflows,
alias,
define,
checkFetch: true,
mockAnalyticsEngineDatasets,
legacyAssets,
// disable the cache in dev
Expand All @@ -178,6 +179,7 @@ export function runBuild(

// sourcemap defaults to true in dev
sourcemap: undefined,
checkFetch,
})
: undefined;

Expand Down
15 changes: 15 additions & 0 deletions packages/wrangler/src/pages/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import path, {
} from "node:path";
import { createUploadWorkerBundleContents } from "../api/pages/create-worker-bundle-contents";
import { readConfig } from "../config";
import { shouldCheckFetch } from "../deployment-bundle/bundle";
import { writeAdditionalModules } from "../deployment-bundle/find-additional-modules";
import { validateNodeCompatMode } from "../deployment-bundle/node-compat";
import { FatalError } from "../errors";
Expand Down Expand Up @@ -152,6 +153,7 @@ export const Handler = async (args: PagesBuildArgs) => {
plugin,
nodejsCompatMode,
defineNavigatorUserAgent,
checkFetch,
external,
} = validatedArgs;

Expand All @@ -178,6 +180,7 @@ export const Handler = async (args: PagesBuildArgs) => {
routesOutputPath,
local: false,
defineNavigatorUserAgent,
checkFetch,
external,
});
} catch (e) {
Expand Down Expand Up @@ -219,6 +222,7 @@ export const Handler = async (args: PagesBuildArgs) => {
nodejsCompatMode,
workerScriptPath,
defineNavigatorUserAgent,
checkFetch,
external,
} = validatedArgs;

Expand All @@ -234,6 +238,7 @@ export const Handler = async (args: PagesBuildArgs) => {
buildOutputDirectory,
nodejsCompatMode,
defineNavigatorUserAgent,
checkFetch,
sourceMaps: config?.upload_source_maps ?? sourcemap,
});
} else {
Expand All @@ -252,6 +257,7 @@ export const Handler = async (args: PagesBuildArgs) => {
watch,
nodejsCompatMode,
defineNavigatorUserAgent,
checkFetch,
externalModules: external,
});
}
Expand All @@ -277,6 +283,7 @@ export const Handler = async (args: PagesBuildArgs) => {
routesOutputPath,
local: false,
defineNavigatorUserAgent,
checkFetch,
external,
});
} catch (e) {
Expand Down Expand Up @@ -320,6 +327,7 @@ type WorkerBundleArgs = Omit<PagesBuildArgs, "nodeCompat"> & {
buildOutputDirectory: string;
nodejsCompatMode: NodeJSCompatMode;
defineNavigatorUserAgent: boolean;
checkFetch: boolean;
workerScriptPath: string;
config: Config | undefined;
buildMetadata:
Expand All @@ -337,6 +345,7 @@ type PluginArgs = Omit<
outdir: string;
nodejsCompatMode: NodeJSCompatMode;
defineNavigatorUserAgent: boolean;
checkFetch: boolean;
};
async function maybeReadPagesConfig(
args: PagesBuildArgs
Expand Down Expand Up @@ -452,6 +461,11 @@ const validateArgs = async (args: PagesBuildArgs): Promise<ValidatedArgs> => {
args.compatibilityFlags
);

const checkFetch = shouldCheckFetch(
args.compatibilityDate,
args.compatibilityFlags
);

let workerScriptPath: string | undefined;

if (args.buildOutputDirectory) {
Expand Down Expand Up @@ -493,6 +507,7 @@ We looked for the Functions directory (${basename(
workerScriptPath,
nodejsCompatMode,
defineNavigatorUserAgent,
checkFetch,
config,
buildMetadata:
config && args.projectDirectory && config.pages_build_output_dir
Expand Down
4 changes: 4 additions & 0 deletions packages/wrangler/src/pages/buildFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function buildFunctions({
`./functionsRoutes-${Math.random()}.mjs`
),
defineNavigatorUserAgent,
checkFetch,
external,
}: Partial<
Pick<
Expand All @@ -64,6 +65,7 @@ export async function buildFunctions({
// temporary directory each time
routesModule?: string;
defineNavigatorUserAgent: boolean;
checkFetch: boolean;
}) {
RUNNING_BUILDERS.forEach(
(runningBuilder) => runningBuilder.stop && runningBuilder.stop()
Expand Down Expand Up @@ -121,6 +123,7 @@ export async function buildFunctions({
functionsDirectory: absoluteFunctionsDirectory,
local,
defineNavigatorUserAgent,
checkFetch,
external,
});
} else {
Expand All @@ -138,6 +141,7 @@ export async function buildFunctions({
buildOutputDirectory,
nodejsCompatMode,
defineNavigatorUserAgent,
checkFetch,
external,
});
}
Expand Down
7 changes: 7 additions & 0 deletions packages/wrangler/src/pages/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as esbuild from "esbuild";
import { unstable_dev } from "../api";
import { readConfig } from "../config";
import { isBuildFailure } from "../deployment-bundle/build-failures";
import { shouldCheckFetch } from "../deployment-bundle/bundle";
import { esbuildAliasExternalPlugin } from "../deployment-bundle/esbuild-plugins/alias-external";
import { validateNodeCompatMode } from "../deployment-bundle/node-compat";
import { FatalError } from "../errors";
Expand Down Expand Up @@ -384,6 +385,9 @@ export const Handler = async (args: PagesDevArguments) => {
compatibilityDate,
compatibilityFlags
);

const checkFetch = shouldCheckFetch(compatibilityDate, compatibilityFlags);

let modules: CfModule[] = [];

if (usingWorkerDirectory) {
Expand All @@ -394,6 +398,7 @@ export const Handler = async (args: PagesDevArguments) => {
buildOutputDirectory: directory ?? ".",
nodejsCompatMode,
defineNavigatorUserAgent,
checkFetch,
sourceMaps: config?.upload_source_maps ?? false,
});
modules = bundleResult.modules;
Expand Down Expand Up @@ -462,6 +467,7 @@ export const Handler = async (args: PagesDevArguments) => {
watch: false,
onEnd: () => scriptReadyResolve(),
defineNavigatorUserAgent,
checkFetch,
});

/*
Expand Down Expand Up @@ -632,6 +638,7 @@ export const Handler = async (args: PagesDevArguments) => {
local: true,
routesModule,
defineNavigatorUserAgent,
checkFetch,
});

/*
Expand Down
3 changes: 2 additions & 1 deletion packages/wrangler/src/pages/functions/buildPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function buildPluginFromFunctions({
functionsDirectory,
local,
defineNavigatorUserAgent,
checkFetch,
external,
}: Options) {
const entry: Entry = {
Expand Down Expand Up @@ -107,7 +108,7 @@ export function buildPluginFromFunctions({
},
],
serveLegacyAssetsFromWorker: false,
checkFetch: local,
checkFetch: local && checkFetch,
// TODO: mock AE datasets in Pages functions for dev
mockAnalyticsEngineDatasets: [],
targetConsumer: local ? "dev" : "deploy",
Expand Down
Loading

0 comments on commit e2e6912

Please sign in to comment.