Skip to content

Commit c224ae6

Browse files
committed
🔧 fix: origin, default credential
1 parent 6a4593b commit c224ae6

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# 0.8.0-rc.1 - 21 Dec 2023
2+
Improvement:
3+
- Using `Elysia.headers` to set default headers
4+
- Get value of static field ahead of time
5+
6+
Change:
7+
- set `credential` to `true` by default
8+
9+
Bug fix:
10+
- origin with out protocol is now handle
111

212
# 0.8.0-rc.0 - 15 Dec 2023
313
Change:

src/index.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ interface CORSConfig {
8585
* - `HTTPMethod[]` - Allow multiple HTTP methods.
8686
* - eg: ['GET', 'PUT', 'POST']
8787
*/
88-
methods?: undefined | null | '' | '*' | HTTPMethod | HTTPMethod[]
88+
methods?: boolean | undefined | null | '' | '*' | HTTPMethod | HTTPMethod[]
8989
/**
9090
* @default `*`
9191
*
@@ -117,7 +117,7 @@ interface CORSConfig {
117117
*/
118118
exposedHeaders?: string | string[]
119119
/**
120-
* @default `false`
120+
* @default `true`
121121
*
122122
* Assign **Access-Control-Allow-Credentials** header.
123123
*
@@ -162,7 +162,7 @@ export const cors = (
162162
methods = '*',
163163
allowedHeaders = '*',
164164
exposedHeaders = '*',
165-
credentials = false,
165+
credentials = true,
166166
maxAge = 5,
167167
preflight = true
168168
} = config
@@ -183,12 +183,10 @@ export const cors = (
183183
switch (typeof origin) {
184184
case 'string':
185185
const protocolStart = from.indexOf('://')
186-
if (protocolStart !== -1)
187-
from = from.slice(protocolStart + 3)
186+
if (protocolStart !== -1) from = from.slice(protocolStart + 3)
188187

189188
const trailingSlash = from.indexOf('/', 0)
190-
if (trailingSlash !== -1)
191-
from = from.slice(trailingSlash)
189+
if (trailingSlash !== -1) from = from.slice(trailingSlash)
192190

193191
return origin === from
194192

@@ -204,7 +202,8 @@ export const cors = (
204202
// origin === `true` means any origin
205203
if (origin === true) {
206204
set.headers['Vary'] = '*'
207-
set.headers['Access-Control-Allow-Origin'] = request.headers.get('Origin') || '*'
205+
set.headers['Access-Control-Allow-Origin'] =
206+
request.headers.get('Origin') || '*'
208207

209208
return
210209
}
@@ -234,8 +233,11 @@ export const cors = (
234233
set.headers['Access-Control-Allow-Origin'] = headers.join(', ')
235234
}
236235

237-
const handleMethod = (set: Context['set']) => {
238-
if (!methods?.length) return
236+
const handleMethod = (set: Context['set'], method: string) => {
237+
if (methods === true)
238+
return (set.headers['Access-Control-Allow-Methods'] = method ?? '*')
239+
240+
if (methods === false || !methods?.length) return
239241

240242
if (methods === '*')
241243
return (set.headers['Access-Control-Allow-Methods'] = '*')
@@ -249,7 +251,7 @@ export const cors = (
249251
if (preflight)
250252
app.options('/', ({ set, request }) => {
251253
handleOrigin(set as any, request)
252-
handleMethod(set)
254+
handleMethod(set, request.method)
253255

254256
if (exposedHeaders.length)
255257
set.headers['Access-Control-Allow-Headers'] =
@@ -265,7 +267,7 @@ export const cors = (
265267
})
266268
}).options('/*', ({ set, request }) => {
267269
handleOrigin(set as any, request)
268-
handleMethod(set)
270+
handleMethod(set, request.method)
269271

270272
if (exposedHeaders.length)
271273
set.headers['Access-Control-Allow-Headers'] =
@@ -281,24 +283,22 @@ export const cors = (
281283
})
282284
})
283285

284-
return app.onRequest(({ set, request }) => {
286+
const defaultHeaders: Record<string, string> = {
287+
'Access-Control-Allow-Headers':
288+
typeof allowedHeaders === 'string'
289+
? allowedHeaders
290+
: allowedHeaders.join(', '),
291+
'Access-Control-Exposed-Headers':
292+
typeof exposedHeaders === 'string'
293+
? exposedHeaders
294+
: exposedHeaders.join(', ')
295+
}
296+
297+
if (credentials) defaultHeaders['Access-Control-Allow-Credentials'] = 'true'
298+
299+
return app.headers(defaultHeaders).onRequest(({ set, request }) => {
285300
handleOrigin(set, request)
286-
handleMethod(set)
287-
288-
if (allowedHeaders.length)
289-
set.headers['Access-Control-Allow-Headers'] =
290-
typeof allowedHeaders === 'string'
291-
? allowedHeaders
292-
: allowedHeaders.join(', ')
293-
294-
if (exposedHeaders.length)
295-
set.headers['Access-Control-Exposed-Headers'] =
296-
typeof exposedHeaders === 'string'
297-
? exposedHeaders
298-
: exposedHeaders.join(', ')
299-
300-
if (credentials)
301-
set.headers['Access-Control-Allow-Credentials'] = 'true'
301+
handleMethod(set, request.method)
302302
})
303303
}
304304

0 commit comments

Comments
 (0)