Add CORS for GETs
All checks were successful
Publish Docker Image / Publish Docker Image (push) Successful in 4s

This commit is contained in:
2025-05-01 22:11:06 -04:00
parent 4f782560de
commit ef60832bc2
2 changed files with 20 additions and 3 deletions

View File

@ -2,10 +2,17 @@ import type { FastifyReply } from 'fastify'
import { isProduction } from '@redwoodjs/api/logger'
export const setCorsHeaders = (res: FastifyReply) => {
export const setCorsHeaders = (
res: FastifyReply,
isPublic: boolean = false
) => {
res.raw.setHeader(
'Access-Control-Allow-Origin',
isProduction ? process.env.ADDRESS_PROD : process.env.ADDRESS_DEV
isPublic
? '*'
: isProduction
? process.env.ADDRESS_PROD
: process.env.ADDRESS_DEV
)
res.raw.setHeader(
'Access-Control-Allow-Methods',
@ -16,4 +23,9 @@ export const setCorsHeaders = (res: FastifyReply) => {
'Origin, X-Requested-With, Content-Type, Accept, Authorization, Tus-Resumable, Upload-Length, Upload-Metadata, Upload-Offset'
)
res.raw.setHeader('Access-Control-Allow-Credentials', 'true')
res.raw.setHeader(
'Access-Control-Expose-Headers',
'Upload-Offset, Upload-Length, Upload-Metadata, Tus-Version,' +
'Tus-Resumable, Tus-Max-Size, Tus-Extension, Tus-Checksum-Algorithm'
)
}

View File

@ -26,6 +26,11 @@ export const handleTusUpload = (
isPublicEndpoint: boolean
) => {
res.hijack()
if (req.method === 'GET' && isPublicEndpoint) {
setCorsHeaders(res)
}
if (isProduction) {
if (req.method === 'OPTIONS') handleOptionsRequest(res)
else if (isPublicEndpoint && req.method === 'GET')
@ -41,7 +46,7 @@ export const handleTusUpload = (
res.raw.end('Method not allowed')
}
} else {
setCorsHeaders(res)
setCorsHeaders(res, isPublicEndpoint)
void tusHandler.handle(req.raw, res.raw)
}
}