Skip to content

Commit

Permalink
fix #233 - cors eats status message
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Apr 2, 2024
1 parent 33d1ef6 commit d6b7a17
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/cors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ describe('cors(options?: CorsOptions)', () => {
expect(corsified.headers.getSetCookie().length).toBe(2)
})

it('will preserve existing status codes', async () => {
const { corsify } = cors()
const response = new Response(null, { status: 403 })
const corsified = corsify(response.clone())

expect(response.status).toBe(403)
expect(corsified.status).toBe(403)
})

it('will not modify a websocket request', async () => {
const { corsify } = cors()
const response = new WebSocketResponse(null, { status: 101 }) as Response
Expand Down
17 changes: 8 additions & 9 deletions src/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,14 @@ export const cors = (options: CorsOptions = {}) => {
|| response.status == 101
) return response

return new Response(response.body, {
...response,
// @ts-expect-error
headers: [
...response.headers,
['access-control-allow-origin', getAccessControlOrigin(request)],
...Object.entries(corsHeaders),
].filter(v => v[1]),
})
const origin = getAccessControlOrigin(request)
if (origin) response.headers.append('access-control-allow-origin', origin)

for (const [key, value] of Object.entries(corsHeaders)) {
if (value) response.headers.append(key, value)
}

return response
}

// Return corsify and preflight methods.
Expand Down

0 comments on commit d6b7a17

Please sign in to comment.