Skip to content

Commit

Permalink
chore: extract node compat helper to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Jessop committed Aug 22, 2024
1 parent 8bcc905 commit 45e6b86
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 47 deletions.
2 changes: 1 addition & 1 deletion packages/wrangler/src/__tests__/node-compat.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { getNodeCompatMode } from "../deployment-bundle/node-compat";
import { getNodeCompatMode } from "../node-compat";

describe("getNodeCompatMode", () => {
it("should return v2 mode when experimental:nodejs_compat_v2 flag is present", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import assert from "node:assert";
import { readFileSync } from "node:fs";
import path from "node:path";
import { File, FormData } from "undici";
import { stripExperimentalPrefixes } from "../node-compat";
import { handleUnsafeCapnp } from "./capnp";
import { stripExperimentalPrefixes } from "./node-compat";
import type {
CfDurableObjectMigrations,
CfModuleType,
Expand Down
43 changes: 1 addition & 42 deletions packages/wrangler/src/deployment-bundle/node-compat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UserError } from "../errors";
import { logger } from "../logger";
import type { Config } from "../config";
import { getNodeCompatMode } from "../node-compat";

/**
* Wrangler can provide Node.js compatibility in a number of different modes:
Expand Down Expand Up @@ -89,44 +89,3 @@ export function validateNodeCompat({

return mode;
}

export function getNodeCompatMode({
compatibility_flags,
node_compat,
}: Pick<Config, "compatibility_flags" | "node_compat">) {
const nodejsCompat = compatibility_flags.includes("nodejs_compat");
const nodejsCompatV2 = compatibility_flags.includes(
"experimental:nodejs_compat_v2"
);

let mode: NodeJSCompatMode;
if (nodejsCompatV2) {
mode = "v2";
} else if (nodejsCompat) {
mode = "v1";
} else if (node_compat) {
mode = "legacy";
} else {
mode = null;
}

return {
legacy: mode === "legacy",
mode,
nodejsCompat,
nodejsCompatV2,
};
}

/**
* The nodejs_compat_v2 flag currently requires an `experimental:` prefix within Wrangler,
* but this needs to be stripped before sending to workerd, since that doesn't know about that.
*
* TODO: Remove this function when we graduate nodejs_v2 to non-experimental.
* See https://jira.cfdata.org/browse/DEVDASH-218
*/
export function stripExperimentalPrefixes(
compatFlags: string[] | undefined
): string[] | undefined {
return compatFlags?.map((flag) => flag.replace(/^experimental:/, ""));
}
2 changes: 1 addition & 1 deletion packages/wrangler/src/dev/miniflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import {
} from "../ai/fetcher";
import { readConfig } from "../config";
import { ModuleTypeToRuleType } from "../deployment-bundle/module-collection";
import { stripExperimentalPrefixes } from "../deployment-bundle/node-compat";
import { withSourceURLs } from "../deployment-bundle/source-url";
import { UserError } from "../errors";
import { logger } from "../logger";
import { stripExperimentalPrefixes } from "../node-compat";
import { getSourceMappedString } from "../sourcemap";
import { updateCheck } from "../update-check";
import type { ServiceFetch } from "../api";
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/dev/remote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { useErrorHandler } from "react-error-boundary";
import { helpIfErrorIsSizeOrScriptStartup } from "../deploy/deploy";
import { printBundleSize } from "../deployment-bundle/bundle-reporter";
import { getBundleType } from "../deployment-bundle/bundle-type";
import { stripExperimentalPrefixes } from "../deployment-bundle/node-compat";
import { withSourceURLs } from "../deployment-bundle/source-url";
import { getInferredHost } from "../dev";
import { UserError } from "../errors";
import { logger } from "../logger";
import { stripExperimentalPrefixes } from "../node-compat";
import { syncLegacyAssets } from "../sites";
import {
getAccountChoices,
Expand Down
51 changes: 51 additions & 0 deletions packages/wrangler/src/node-compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { Config } from "./config";

/**
* Wrangler can provide Node.js compatibility in a number of different modes:
* - "legacy" - this mode adds compile-time polyfills that are not well maintained and cannot work with workerd runtime builtins.
* - "v1" - this mode tells the workerd runtime to enable some Node.js builtin libraries (accessible only via `node:...` imports) but no globals.
* - "v2" - this mode tells the workerd runtime to enable more Node.js builtin libraries (accessible both with and without the `node:` prefix)
* and also some Node.js globals such as `Buffer`; it also turns on additional compile-time polyfills for those that are not provided by the runtime.
*/
export type NodeJSCompatMode = "legacy" | "v1" | "v2" | null;

export function getNodeCompatMode({
compatibility_flags,
node_compat,
}: Pick<Config, "compatibility_flags" | "node_compat">) {
const nodejsCompat = compatibility_flags.includes("nodejs_compat");
const nodejsCompatV2 = compatibility_flags.includes(
"experimental:nodejs_compat_v2"
);

let mode: NodeJSCompatMode;
if (nodejsCompatV2) {
mode = "v2";
} else if (nodejsCompat) {
mode = "v1";
} else if (node_compat) {
mode = "legacy";
} else {
mode = null;
}

return {
legacy: mode === "legacy",
mode,
nodejsCompat,
nodejsCompatV2,
};
}

/**
* The nodejs_compat_v2 flag currently requires an `experimental:` prefix within Wrangler,
* but this needs to be stripped before sending to workerd, since that doesn't know about that.
*
* TODO: Remove this function when we graduate nodejs_v2 to non-experimental.
* See https://jira.cfdata.org/browse/DEVDASH-218
*/
export function stripExperimentalPrefixes(
compatFlags: string[] | undefined
): string[] | undefined {
return compatFlags?.map((flag) => flag.replace(/^experimental:/, ""));
}
2 changes: 1 addition & 1 deletion packages/wrangler/src/type-generation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import * as esmLexer from "es-module-lexer";
import { findUpSync } from "find-up";
import { findWranglerToml, readConfig } from "../config";
import { getEntry } from "../deployment-bundle/entry";
import { getNodeCompatMode } from "../deployment-bundle/node-compat";
import { getVarsForDev } from "../dev/dev-vars";
import { UserError } from "../errors";
import { CommandLineArgsError } from "../index";
import { logger } from "../logger";
import { getNodeCompatMode } from "../node-compat";
import { parseJSONC } from "../parse";
import { printWranglerBanner } from "../update-check";
import { generateRuntimeTypes } from "./runtime";
Expand Down

0 comments on commit 45e6b86

Please sign in to comment.