Skip to content

Commit

Permalink
Use await import instead of require for functions modules that contai…
Browse files Browse the repository at this point in the history
…n 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 <inlined@users.noreply.github.com>
  • Loading branch information
Berlioz and inlined authored Dec 19, 2024
1 parent da95919 commit db8e3db
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 2 deletions.
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

0 comments on commit db8e3db

Please sign in to comment.