Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use await import instead of require for functions modules that contain top-level awaits #1651

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
node-version:
- 18.x
- 20.x
- 22.x
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
Expand All @@ -52,6 +53,7 @@ jobs:
node-version:
- 18.x
- 20.x
- 22.x
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Add an authPolicy callback to CallableOptions for reusable auth middleware as well as helper auth policies (#1650)
- Handle ESM functions codebases containing top-level awaits, which would break in node 22.12+ (#1651)
- Multiple breaking changes to the not-yet-announced streaming feature for Callable Functions (#1652)
3 changes: 3 additions & 0 deletions scripts/bin-test/sources/esm-top-level-await/exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const fn = () => {
return null;
}
8 changes: 8 additions & 0 deletions scripts/bin-test/sources/esm-top-level-await/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as functionsv2 from "firebase-functions/v2";

const { fn } = await import('./exports.js');

export const v2http = functionsv2.https.onRequest((req, resp) => {
fn()
resp.status(200).send("PASS");
});
4 changes: 4 additions & 0 deletions scripts/bin-test/sources/esm-top-level-await/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "esm",
"type": "module"
}
4 changes: 2 additions & 2 deletions src/runtime/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ async function loadModule(functionsDir: string) {
try {
return require(path.resolve(absolutePath));
} catch (e) {
if (e.code === "ERR_REQUIRE_ESM") {
// This is an ESM package!
if (e.code === "ERR_REQUIRE_ESM" || e.code === "ERR_REQUIRE_ASYNC_MODULE") {
// This is an ESM package, or one containing top-level awaits!
const modulePath = require.resolve(absolutePath);
// Resolve module path to file:// URL. Required for windows support.
const moduleURL = url.pathToFileURL(modulePath).href;
Expand Down
Loading