-
Notifications
You must be signed in to change notification settings - Fork 9
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
Set CORS headers #304
Set CORS headers #304
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {json} from '@remix-run/cloudflare' | ||
|
||
export const jsonCORS = <T>(data: T) => | ||
json(data, { | ||
headers: { | ||
'Access-Control-Allow-Methods': 'GET, OPTIONS', | ||
'Access-Control-Allow-Origin': ALLOW_ORIGINS, | ||
}, | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,29 @@ | ||
import {createEventHandler} from '@remix-run/cloudflare-workers' | ||
import url from 'url' | ||
import {createEventHandler, handleAsset} from '@remix-run/cloudflare-workers' | ||
import * as build from '@remix-run/dev/server-build' | ||
|
||
addEventListener('fetch', createEventHandler({build, mode: process.env.NODE_ENV})) | ||
const CORS_ASSETS = ['/tfWorker.js'] | ||
|
||
const isCorsEnabledAsset = (event) => { | ||
const parsedUrl = url.parse(event.request.url) | ||
const pathname = parsedUrl.pathname | ||
|
||
return CORS_ASSETS.includes(pathname) | ||
} | ||
|
||
const fetchCorsAsset = (event) => { | ||
const resp = handleAsset(event, build) | ||
return event.respondWith( | ||
resp.then((res) => { | ||
const headers = new Headers(res.headers) | ||
headers.set('Access-Control-Allow-Origin', ALLOW_ORIGINS || '') | ||
headers.set('Access-Control-Allow-Methods', 'GET, OPTIONS') | ||
return new Response(res.body, {headers}) | ||
}) | ||
) | ||
} | ||
|
||
const handler = createEventHandler({build, mode: process.env.NODE_ENV}) | ||
addEventListener('fetch', async (event) => | ||
isCorsEnabledAsset(event) ? fetchCorsAsset(event) : handler(event) | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ CODA_TOKEN = "{CODA_TOKEN}" | |
CODA_INCOMING_TOKEN = "{CODA_INCOMING_TOKEN}" | ||
CODA_WRITES_TOKEN = "{CODA_WRITES_TOKEN}" | ||
NLP_SEARCH_ENDPOINT = "https://stampy-nlp-t6p37v2uia-uw.a.run.app/" | ||
ALLOW_ORIGINS = "https://chat.aisafety.info" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I considered also setting this in the github deployment script, but left it out for now - this is pretty much the only allowed external site. If there are more in the future, then it can be changed to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be happy for GET request to be allowed from any origin ( but if you want to maintain the list of allowed origins, that is also fine |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will be set for each request to the endpoints that use this function. Should be ok, but I can change it to only do so when called from a domain what is allowed