-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix nodejs compat v2 upgrade (#6824)
* fix: tidy up error messaging for unexpected use of Node.js APIs Fixes #6822 * fix: teach Wrangler about node_compat version date switch A compatibility of Sept 23, 2024 or later means that `nodejs_compat` is equivalent to `nodejs_compat_v2`. * refactor: move the node-compat parsing from Wrangler into Miniflare This logic is now also needed inside Miniflare to determine how to handle node imports. So this commit moves the relevant code in there and references it from Wrangler. The validation of the config is kept in Wrangler, since Miniflare doesn't need that particularly and the validation relies upon Wrangler logger and UserError. * fix: teach Miniflare about node_compat version date switch A compatibility of Sept 23, 2024 or later means that `nodejs_compat` is equivalent to `nodejs_compat_v2`. * test: update Node.js hybrid fixture to use nodejs_compat + compat date Now that we can set the compat date to Sept 23, 2024, we can update this fixture not to use the `nodejs_compat_v2` flag. * fixup! fix: teach Miniflare about node_compat version date switch * fixup! refactor: move the node-compat parsing from Wrangler into Miniflare * fixup! fix: tidy up error messaging for unexpected use of Node.js APIs * fixup! fix: tidy up error messaging for unexpected use of Node.js APIs * fixup! refactor: move the node-compat parsing from Wrangler into Miniflare * fixup! fix: tidy up error messaging for unexpected use of Node.js APIs * fixup! refactor: move the node-compat parsing from Wrangler into Miniflare * fix: also handle the case with `no_nodejs_compat_v2` * test: normalize output strings for tests * refactor: rename `getNodejsCompatMode()` to `getNodejsCompat()`
- Loading branch information
1 parent
5e2e62c
commit 1c58a74
Showing
31 changed files
with
451 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
fix: tidy up error messaging for unexpected use of Node.js APIs | ||
|
||
Fixes #6822 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"miniflare": patch | ||
--- | ||
|
||
fix: teach Miniflare about node_compat version date switch | ||
|
||
A compatibility of Sept 23, 2024 or later means that `nodejs_compat` is equivalent to `nodejs_compat_v2`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* We 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. | ||
* - "als": this mode tells the workerd runtime to enable only the Async Local Storage builtin library (accessible via `node:async_hooks`). | ||
* - "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. | ||
* - null - no Node.js compatibility. | ||
*/ | ||
export type NodeJSCompatMode = "legacy" | "als" | "v1" | "v2" | null; | ||
|
||
/** | ||
* Computes the Node.js compatibility mode we are running. | ||
* | ||
* NOTES: | ||
* - The v2 mode is configured via `nodejs_compat_v2` compat flag or via `nodejs_compat` plus a compatibility date of Sept 23rd. 2024 or later. | ||
* - See `EnvironmentInheritable` for `nodeCompat` and `noBundle`. | ||
* | ||
* @param compatibilityDateStr The compatibility date | ||
* @param compatibilityFlags The compatibility flags | ||
* @param opts.nodeCompat Whether the legacy node_compat arg is being used | ||
* @returns the mode and flags to indicate specific configuration for validating. | ||
*/ | ||
export function getNodeCompat( | ||
compatibilityDate: string = "2000-01-01", // Default to some arbitrary old date | ||
compatibilityFlags: string[], | ||
opts?: { | ||
nodeCompat?: boolean; | ||
} | ||
) { | ||
const { nodeCompat = false } = opts ?? {}; | ||
const { | ||
hasNodejsAlsFlag, | ||
hasNodejsCompatFlag, | ||
hasNodejsCompatV2Flag, | ||
hasNoNodejsCompatV2Flag, | ||
hasExperimentalNodejsCompatV2Flag, | ||
} = parseNodeCompatibilityFlags(compatibilityFlags); | ||
|
||
const nodeCompatSwitchOverDate = "2024-09-23"; | ||
const legacy = nodeCompat === true; | ||
let mode: NodeJSCompatMode = null; | ||
if ( | ||
hasNodejsCompatV2Flag || | ||
(hasNodejsCompatFlag && | ||
compatibilityDate >= nodeCompatSwitchOverDate && | ||
!hasNoNodejsCompatV2Flag) | ||
) { | ||
mode = "v2"; | ||
} else if (hasNodejsCompatFlag) { | ||
mode = "v1"; | ||
} else if (hasNodejsAlsFlag) { | ||
mode = "als"; | ||
} else if (legacy) { | ||
mode = "legacy"; | ||
} | ||
|
||
return { | ||
mode, | ||
hasNodejsAlsFlag, | ||
hasNodejsCompatFlag, | ||
hasNodejsCompatV2Flag, | ||
hasNoNodejsCompatV2Flag, | ||
hasExperimentalNodejsCompatV2Flag, | ||
}; | ||
} | ||
|
||
function parseNodeCompatibilityFlags(compatibilityFlags: string[]) { | ||
return { | ||
hasNodejsAlsFlag: compatibilityFlags.includes("nodejs_als"), | ||
hasNodejsCompatFlag: compatibilityFlags.includes("nodejs_compat"), | ||
hasNodejsCompatV2Flag: compatibilityFlags.includes("nodejs_compat_v2"), | ||
hasNoNodejsCompatV2Flag: compatibilityFlags.includes("no_nodejs_compat_v2"), | ||
hasExperimentalNodejsCompatV2Flag: compatibilityFlags.includes( | ||
"experimental:nodejs_compat_v2" | ||
), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.