Skip to content

fix: avoid CORS declaration interop requirement - #1941

Open
cexbrayat wants to merge 1 commit into
firebase:masterfrom
cexbrayat:fix/cors-declaration-import
Open

fix: avoid CORS declaration interop requirement#1941
cexbrayat wants to merge 1 commit into
firebase:masterfrom
cexbrayat:fix/cors-declaration-import

Conversation

@cexbrayat

@cexbrayat cexbrayat commented Jul 28, 2026

Copy link
Copy Markdown

Description

@types/cors declares the package with export =. PR #1903 added exported CORS types that caused TypeScript to preserve the source default import in lib/common/providers/https.d.ts.

Default-importing an export = module in a published declaration forces consumers to enable esModuleInterop. Use a namespace import instead so that compiler-option requirement does not leak into consumer projects. The explicit .default invocation preserves the existing runtime behavior with the repository's current TypeScript and tsdown configuration.

This fixes the declaration regression visible in firebase-functions@7.3.0.

The packaging test now compiles a real packed consumer without esModuleInterop to prevent regressions.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds packaging tests to verify TypeScript declarations without esModuleInterop and updates the cors import in src/common/providers/https.ts to use namespace imports. The review feedback suggests improving runtime compatibility for the cors call by adding a fallback for cors.default and simplifying the Node.js script execution in scripts/test-packaging.sh by directly interpolating the script directory.

Comment on lines +799 to 801
cors.default(corsOptions)(req, res, () => {
resolve(wrapped(req, res));
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Directly calling cors.default assumes that the module loader or bundler always wraps the CommonJS module in an ES module compatibility layer that defines the default property.

However, in some environments (such as running tests directly on source files, or using certain toolchains/bundlers where esModuleInterop is disabled or behaves differently), cors.default might be undefined at runtime, leading to a TypeError: cors.default is not a function.

Using a fallback like (cors.default || cors) ensures maximum compatibility and robustness across different runtimes and build configurations.

      const corsFn = (cors.default || cors) as any;
      corsFn(corsOptions)(req, res, () => {
        resolve(wrapped(req, res));
      });

Comment thread scripts/test-packaging.sh
pushd "$WORK_DIR" > /dev/null
npm init -y > /dev/null
npm install "$TARBALL_PATH"
TYPESCRIPT_VERSION=$(node -p "require(process.argv[1]).devDependencies.typescript" "$SCRIPT_DIR/../package.json")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of using the process.argv[1] trick to pass the path to the inline Node script, you can directly interpolate the $SCRIPT_DIR variable into the double-quoted Node script string. This is much cleaner, more readable, and standard for bash scripts.

Suggested change
TYPESCRIPT_VERSION=$(node -p "require(process.argv[1]).devDependencies.typescript" "$SCRIPT_DIR/../package.json")
TYPESCRIPT_VERSION=$(node -p "require('$SCRIPT_DIR/../package.json').devDependencies.typescript")

@cexbrayat

Copy link
Copy Markdown
Author

Let me know if the gemini feedback should be applied (and if the packaging test is useful).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants