Skip to content
Open
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
3 changes: 3 additions & 0 deletions scripts/packaging-test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { onCall } from "firebase-functions/v2/https";

onCall({ cors: true }, () => null);
10 changes: 10 additions & 0 deletions scripts/packaging-test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"noEmit": true,
"strict": true,
"target": "es2022"
},
"files": ["index.ts"]
}
8 changes: 7 additions & 1 deletion scripts/test-packaging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ fi
echo "Setting up test project in $WORK_DIR..."
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")

npm install "$TARBALL_PATH" "typescript@$TYPESCRIPT_VERSION"

echo "Running verification script..."
cp "$SCRIPT_DIR/verify-exports.mjs" .
node verify-exports.mjs

echo "Verifying TypeScript declarations without esModuleInterop..."
cp "$SCRIPT_DIR/packaging-test/index.ts" .
cp "$SCRIPT_DIR/packaging-test/tsconfig.json" .
./node_modules/.bin/tsc --project tsconfig.json

popd > /dev/null
4 changes: 2 additions & 2 deletions src/common/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import cors from "cors";
import * as cors from "cors";
import * as express from "express";
import { DecodedAppCheckToken } from "firebase-admin/app-check";

Expand Down Expand Up @@ -796,7 +796,7 @@ export function onCallHandler<Req = any, Res = any, Stream = unknown>(
...options.cors,
origin,
};
cors(corsOptions as cors.CorsOptions)(req, res, () => {
cors.default(corsOptions)(req, res, () => {
resolve(wrapped(req, res));
});
Comment on lines +799 to 801

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));
      });

});
Expand Down