Add CORS for GETs
All checks were successful
Publish Docker Image / Publish Docker Image (push) Successful in 4s
All checks were successful
Publish Docker Image / Publish Docker Image (push) Successful in 4s
This commit is contained in:
@ -2,10 +2,17 @@ import type { FastifyReply } from 'fastify'
|
|||||||
|
|
||||||
import { isProduction } from '@redwoodjs/api/logger'
|
import { isProduction } from '@redwoodjs/api/logger'
|
||||||
|
|
||||||
export const setCorsHeaders = (res: FastifyReply) => {
|
export const setCorsHeaders = (
|
||||||
|
res: FastifyReply,
|
||||||
|
isPublic: boolean = false
|
||||||
|
) => {
|
||||||
res.raw.setHeader(
|
res.raw.setHeader(
|
||||||
'Access-Control-Allow-Origin',
|
'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(
|
res.raw.setHeader(
|
||||||
'Access-Control-Allow-Methods',
|
'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'
|
'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-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'
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,11 @@ export const handleTusUpload = (
|
|||||||
isPublicEndpoint: boolean
|
isPublicEndpoint: boolean
|
||||||
) => {
|
) => {
|
||||||
res.hijack()
|
res.hijack()
|
||||||
|
|
||||||
|
if (req.method === 'GET' && isPublicEndpoint) {
|
||||||
|
setCorsHeaders(res)
|
||||||
|
}
|
||||||
|
|
||||||
if (isProduction) {
|
if (isProduction) {
|
||||||
if (req.method === 'OPTIONS') handleOptionsRequest(res)
|
if (req.method === 'OPTIONS') handleOptionsRequest(res)
|
||||||
else if (isPublicEndpoint && req.method === 'GET')
|
else if (isPublicEndpoint && req.method === 'GET')
|
||||||
@ -41,7 +46,7 @@ export const handleTusUpload = (
|
|||||||
res.raw.end('Method not allowed')
|
res.raw.end('Method not allowed')
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
setCorsHeaders(res)
|
setCorsHeaders(res, isPublicEndpoint)
|
||||||
void tusHandler.handle(req.raw, res.raw)
|
void tusHandler.handle(req.raw, res.raw)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user