Skip to content

Commit 779707e

Browse files
committed
Allow CORS to be a parameter
1 parent d80f34a commit 779707e

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

spec/v2/providers/https.spec.ts

+41
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { FULL_ENDPOINT, MINIMAL_V2_ENDPOINT, FULL_OPTIONS, FULL_TRIGGER } from "
3232
import { onInit } from "../../../src/v2/core";
3333
import { Handler } from "express";
3434
import { genkit } from "genkit";
35+
import { clearParams, defineList } from "../../../src/params";
3536

3637
function request(args: {
3738
data?: any;
@@ -227,6 +228,46 @@ describe("onRequest", () => {
227228
});
228229
});
229230

231+
it("should allow cors params", async () => {
232+
const origins = defineList("ORIGINS");
233+
234+
try {
235+
process.env.ORIGINS = '["example.com","example2.com"]';
236+
const func = https.onRequest(
237+
{
238+
cors: origins,
239+
},
240+
(req, res) => {
241+
res.send("42");
242+
}
243+
);
244+
const req = new MockRequest(
245+
{
246+
data: {},
247+
},
248+
{
249+
referrer: "example.com",
250+
"content-type": "application/json",
251+
origin: "example.com",
252+
}
253+
);
254+
req.method = "OPTIONS";
255+
256+
const response = await runHandler(func, req as any);
257+
258+
expect(response.status).to.equal(204);
259+
expect(response.headers).to.deep.equal({
260+
"Access-Control-Allow-Origin": "example.com",
261+
"Access-Control-Allow-Methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
262+
"Content-Length": "0",
263+
Vary: "Origin, Access-Control-Request-Headers",
264+
});
265+
} finally {
266+
delete process.env.ORIGINS;
267+
clearParams();
268+
}
269+
});
270+
230271
it("should add CORS headers if debug feature is enabled", async () => {
231272
sinon.stub(debug, "isDebugFeatureEnabled").withArgs("enableCors").returns(true);
232273

src/v2/providers/https.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ export interface HttpsOptions extends Omit<GlobalOptions, "region" | "enforceApp
7272
* If this is an `Array`, allows requests from domains matching at least one entry of the array.
7373
* Defaults to true for {@link https.CallableFunction} and false otherwise.
7474
*/
75-
cors?: string | boolean | RegExp | Array<string | RegExp>;
75+
cors?:
76+
| string
77+
| Expression<string>
78+
| Expression<string[]>
79+
| boolean
80+
| RegExp
81+
| Array<string | RegExp>;
7682

7783
/**
7884
* Amount of memory to allocate to a function.
@@ -308,7 +314,7 @@ export function onRequest(
308314
}
309315

310316
if (isDebugFeatureEnabled("enableCors") || "cors" in opts) {
311-
let origin = opts.cors;
317+
let origin = opts.cors instanceof Expression ? opts.cors.value() : opts.cors;
312318
if (isDebugFeatureEnabled("enableCors")) {
313319
// Respect `cors: false` to turn off cors even if debug feature is enabled.
314320
origin = opts.cors === false ? false : true;
@@ -418,7 +424,18 @@ export function onCall<T = any, Return = any | Promise<any>, Stream = unknown>(
418424
opts = optsOrHandler as CallableOptions;
419425
}
420426

421-
let origin = isDebugFeatureEnabled("enableCors") ? true : "cors" in opts ? opts.cors : true;
427+
let cors: string | boolean | RegExp | Array<string | RegExp> | undefined;
428+
if ("cors" in opts) {
429+
if (opts.cors instanceof Expression) {
430+
cors = opts.cors.value();
431+
} else {
432+
cors = opts.cors;
433+
}
434+
} else {
435+
cors = true;
436+
}
437+
438+
let origin = isDebugFeatureEnabled("enableCors") ? true : cors;
422439
// Arrays cause the access-control-allow-origin header to be dynamic based
423440
// on the origin header of the request. If there is only one element in the
424441
// array, this is unnecessary.

0 commit comments

Comments
 (0)