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

Add debug feature to always enable cors for emulated v2 https functions #1099

Merged
merged 11 commits into from
May 10, 2022
1 change: 1 addition & 0 deletions src/common/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const debugMode = process.env.FIREBASE_DEBUG_MODE === 'true';

interface DebugFeatures {
skipTokenVerification?: boolean;
enableCors?: boolean;
}

function loadDebugFeatures(): DebugFeatures {
Expand Down
12 changes: 9 additions & 3 deletions src/v2/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
import { ManifestEndpoint } from '../../runtime/manifest';
import * as options from '../options';
import { GlobalOptions, SupportedRegion } from '../options';
import { isDebugFeatureEnabled } from '../../common/debug';

export { Request, CallableRequest, FunctionsErrorCode, HttpsError };

Expand Down Expand Up @@ -89,12 +90,13 @@ export function onRequest(
opts = optsOrHandler as HttpsOptions;
}

if ('cors' in opts) {
if (isDebugFeatureEnabled('enableCors') || 'cors' in opts) {
taeold marked this conversation as resolved.
Show resolved Hide resolved
const corsOrigin = isDebugFeatureEnabled('enableCors') ? true : opts.cors;
const userProvidedHandler = handler;
handler = (req: Request, res: express.Response): void | Promise<void> => {
return new Promise((resolve) => {
res.on('finish', resolve);
cors({ origin: opts.cors })(req, res, () => {
cors({ origin: corsOrigin })(req, res, () => {
resolve(userProvidedHandler(req, res));
});
});
Expand Down Expand Up @@ -179,7 +181,11 @@ export function onCall<T = any, Return = any | Promise<any>>(
opts = optsOrHandler as HttpsOptions;
}

const origin = 'cors' in opts ? opts.cors : true;
const origin = isDebugFeatureEnabled('enableCors')
? true
: 'cors' in opts
? opts.cors
: true;

// onCallHandler sniffs the function length to determine which API to present.
// fix the length to prevent api versions from being mismatched.
Expand Down