Skip to content

Commit

Permalink
Properly set CORS headers for emulated functions (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtstern committed May 13, 2019
1 parent 73e417f commit 7251c1f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
fixed - Functions emulator no longer watches node_modules files.
fixed - Fixed issue with CORS rejecting some callable functions.
fixed - Functions emulator no longer watches node_modules files.
12 changes: 10 additions & 2 deletions src/emulator/functionsEmulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,17 @@ export class FunctionsEmulator implements EmulatorInstance {

hub.use((req, res, next) => {
// Allow CORS to facilitate easier testing.
// Source: https://enable-cors.org/server_expressjCannot understand what targets to deploys.html
// Sources:
// * https://enable-cors.org/server_expressjCannot understand what targets to deploys.html
// * https://stackoverflow.com/a/37228330/324977
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

// Callable functions send "Authorization" and "Content-Type".
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Authorization, Accept"
);
res.header("Access-Control-Allow-Methods", "GET,OPTIONS,POST");

let data = "";
req.on("data", (chunk: any) => {
Expand Down

1 comment on commit 7251c1f

@milansar
Copy link

Choose a reason for hiding this comment

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

should Access-Control-Allow-Methods have PUT as well as fire base functions supports it?
What about scenarios where project use additional headers which are not set by default? Ideally someone having additional headers should have their own CORS implemented but with test I found out that some how code written in our own function implementation is not executed

Please sign in to comment.