Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #233 - cors eats status message #234

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading