Skip to content

Commit

Permalink
fix #233 - cors eats status message (#234)
Browse files Browse the repository at this point in the history
* fix #233 - cors eats status message

* CHANGELOG
  • Loading branch information
kwhitley committed Apr 2, 2024
1 parent 33d1ef6 commit 1ab9ca7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Changelog

- **v5.0.6**
- fixed: corsify as replacing status codes (now mutates original response)
- **v5.0.5**
- fixed: corsify now properly ignores WebSocket responses
- **v5.0.4**
Expand Down
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 1ab9ca7

Please sign in to comment.