Skip to content

Commit

Permalink
refactor: use @fastify/cors for CORS-related headers
Browse files Browse the repository at this point in the history
  • Loading branch information
boywithkeyboard committed Apr 27, 2024
1 parent 5fa2a99 commit 70658ed
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@aws-sdk/client-s3": "^3.564.0",
"@fastify/cors": "^9.0.1",
"@sinclair/typebox": "^0.32.26",
"fastify": "^4.26.2",
"fflate": "^0.8.2",
Expand Down
10 changes: 8 additions & 2 deletions registry/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fastifyCors from '@fastify/cors'
import fastify, { type FastifyReply } from 'fastify'
import favicon from '../build/favicon.svg'
import html from '../build/index.html'
Expand All @@ -12,6 +13,12 @@ import { validExt } from './valid_ext'

const app = fastify()

app.register(fastifyCors, {
maxAge: 1800,
methods: 'GET',
origin: '*',
})

app.get('/', (_, res) => {
res.header('Cache-Control', 's-max-age=1800, max-age=300')
res.header('Content-Type', 'text/html; charset=utf-8')
Expand Down Expand Up @@ -186,7 +193,6 @@ app.setNotFoundHandler(async (req, res) => {
if (previousEtag === checksum) {
return await respondWith(res, 304, null, {
headers: {
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'public, max-age=2592000, immutable', // a month
'Content-Type': contentType + '; charset=utf-8',
'ETag': checksum,
Expand All @@ -198,7 +204,6 @@ app.setNotFoundHandler(async (req, res) => {

await respondWith(res, 200, content, {
headers: {
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'public, max-age=2592000, immutable', // a month
'Content-Type': contentType + '; charset=utf-8',
'ETag': checksum,
Expand Down Expand Up @@ -228,4 +233,5 @@ const start = async () => {
process.exit(1)
}
}

start()

0 comments on commit 70658ed

Please sign in to comment.