From 9ec2d42498196b28459f91343bd072ba9f8dd1cd Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Mon, 21 Aug 2023 15:51:37 +0200 Subject: [PATCH] Set CORS headers --- app/routes/questions/glossary.ts | 6 ++++-- app/routes/questions/search.tsx | 4 +++- app/server-utils/responses.ts | 9 +++++++++ remix.env.d.ts | 1 + server.js | 29 +++++++++++++++++++++++++++-- wrangler.toml.template | 1 + 6 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 app/server-utils/responses.ts diff --git a/app/routes/questions/glossary.ts b/app/routes/questions/glossary.ts index a05f4f96..2ee291d7 100644 --- a/app/routes/questions/glossary.ts +++ b/app/routes/questions/glossary.ts @@ -1,11 +1,13 @@ +import {jsonCORS} from '../../server-utils/responses' import {LoaderArgs} from '@remix-run/cloudflare' import {reloadInBackgroundIfNeeded} from '~/server-utils/kv-cache' import {loadGlossary} from '~/server-utils/stampy' export const loader = async ({request}: LoaderArgs) => { - return await loadGlossary(request) + const data = await loadGlossary(request) + return jsonCORS(data) } -type Data = ReturnType +type Data = ReturnType export function fetchGlossary() { const url = `/questions/glossary` diff --git a/app/routes/questions/search.tsx b/app/routes/questions/search.tsx index f0c43228..f39cdb10 100644 --- a/app/routes/questions/search.tsx +++ b/app/routes/questions/search.tsx @@ -1,4 +1,5 @@ import type {LoaderArgs} from '@remix-run/cloudflare' +import {jsonCORS} from '../../server-utils/responses' export const loader = async ({request}: LoaderArgs) => { const url = new URL(request.url) @@ -7,7 +8,8 @@ export const loader = async ({request}: LoaderArgs) => { if (!question) return [] - return await search(question, onlyLive) + const results = await search(question, onlyLive) + return jsonCORS(results) } export function search(question: string, onlyLive: boolean) { diff --git a/app/server-utils/responses.ts b/app/server-utils/responses.ts new file mode 100644 index 00000000..54f245d4 --- /dev/null +++ b/app/server-utils/responses.ts @@ -0,0 +1,9 @@ +import {json} from '@remix-run/cloudflare' + +export const jsonCORS = (data: T) => + json(data, { + headers: { + 'Access-Control-Allow-Methods': 'GET, OPTIONS', + 'Access-Control-Allow-Origin': ALLOW_ORIGINS, + }, + }) diff --git a/remix.env.d.ts b/remix.env.d.ts index 6fe83eb2..2f91bcaf 100644 --- a/remix.env.d.ts +++ b/remix.env.d.ts @@ -7,3 +7,4 @@ declare const CODA_TOKEN: string declare const CODA_INCOMING_TOKEN: string declare const CODA_WRITES_TOKEN: string declare const NLP_SEARCH_ENDPOINT: string +declare const ALLOW_ORIGINS: string diff --git a/server.js b/server.js index ae133940..b1457bb3 100644 --- a/server.js +++ b/server.js @@ -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) +) diff --git a/wrangler.toml.template b/wrangler.toml.template index deeaef40..36d29744 100644 --- a/wrangler.toml.template +++ b/wrangler.toml.template @@ -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" \ No newline at end of file