-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle options req for cors preflight (#137)
When we use the Console App to create a space, it will send an OPTIONS, and then a POST request to the UCAN Invocation Server, which is behind the CORS handler, and that CORS Handler doesn't support OPTIONS requests for CORS preflight. I've added a new handler to process that OPTIONS request.
- Loading branch information
Showing
3 changed files
with
24 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @import { Middleware, Context } from '@web3-storage/gateway-lib' | ||
*/ | ||
|
||
/** | ||
* Handles OPTIONS requests for CORS preflight. | ||
* @type {Middleware<Context, Context, {}>} | ||
*/ | ||
export function withOptionsRequest (handler) { | ||
return async (request, env, ctx) => { | ||
if (request.method === 'OPTIONS') { | ||
const headers = new Headers() | ||
headers.set('Access-Control-Allow-Origin', '*') | ||
headers.set('Access-Control-Allow-Methods', 'GET, HEAD, POST, OPTIONS') | ||
headers.set('Access-Control-Allow-Headers', 'Content-Type, Authorization') | ||
return new Response(null, { headers, status: 204 }) | ||
} | ||
return handler(request, env, ctx) | ||
} | ||
} |