Skip to content

Commit db8e3db

Browse files
Berliozinlined
andauthored
Use await import instead of require for functions modules that contain top-level awaits (#1651)
* Use await import instead of require for functions modules that contain top-level awaits * tests ensuring that es modules with top level awaits can be loaded * adds node 22 to the CI matrix on github * Add support for an authPolicy that returns Permission Denied when failed (#1650) * Add support for an authPolicy that returns Permission Denied when failed * Formatter * Changelog * remove ignorant comment * Update streaming callable API (#1652) * Update streaming callable API * Fix linter error * Stream type defaults to unknown * Changelog * Format fix * update changelog.md --------- Co-authored-by: Thomas Bouldin <[email protected]>
1 parent da95919 commit db8e3db

File tree

6 files changed

+20
-2
lines changed

6 files changed

+20
-2
lines changed

.github/workflows/test.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
node-version:
3333
- 18.x
3434
- 20.x
35+
- 22.x
3536
steps:
3637
- uses: actions/checkout@v1
3738
- uses: actions/setup-node@v1
@@ -52,6 +53,7 @@ jobs:
5253
node-version:
5354
- 18.x
5455
- 20.x
56+
- 22.x
5557
steps:
5658
- uses: actions/checkout@v1
5759
- uses: actions/setup-node@v1

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
- Add an authPolicy callback to CallableOptions for reusable auth middleware as well as helper auth policies (#1650)
2+
- Handle ESM functions codebases containing top-level awaits, which would break in node 22.12+ (#1651)
23
- Multiple breaking changes to the not-yet-announced streaming feature for Callable Functions (#1652)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const fn = () => {
2+
return null;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as functionsv2 from "firebase-functions/v2";
2+
3+
const { fn } = await import('./exports.js');
4+
5+
export const v2http = functionsv2.https.onRequest((req, resp) => {
6+
fn()
7+
resp.status(200).send("PASS");
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "esm",
3+
"type": "module"
4+
}

src/runtime/loader.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ async function loadModule(functionsDir: string) {
4848
try {
4949
return require(path.resolve(absolutePath));
5050
} catch (e) {
51-
if (e.code === "ERR_REQUIRE_ESM") {
52-
// This is an ESM package!
51+
if (e.code === "ERR_REQUIRE_ESM" || e.code === "ERR_REQUIRE_ASYNC_MODULE") {
52+
// This is an ESM package, or one containing top-level awaits!
5353
const modulePath = require.resolve(absolutePath);
5454
// Resolve module path to file:// URL. Required for windows support.
5555
const moduleURL = url.pathToFileURL(modulePath).href;

0 commit comments

Comments
 (0)