Skip to content

Commit 41e3465

Browse files
committed
🎉 feat: origin
1 parent b001045 commit 41e3465

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.7.2 - 14 Nov 2023
2+
Bug fix:
3+
- Response using origin instead of '*' when presented
4+
15
# 0.7.1 - 26 Sep 2023
26
Bug fix:
37
- strictly handle `string[]`

example/index.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ import { cors } from '../src/index'
44
const app = new Elysia()
55
.use(
66
cors({
7-
origin: ['gehenna.sh', 'saltyaom.com']
7+
credentials: true
88
})
99
)
1010
.get('/', () => 'A')
1111
.listen(3000)
1212

1313
console.log(`Elysia is running at ${app.server?.hostname}:${app.server?.port}`)
1414

15-
app.fetch(
16-
new Request('http://localhost/awd', {
17-
headers: {
18-
origin: 'https://saltyaom.com'
19-
}
20-
})
21-
)
15+
// app.fetch(
16+
// new Request('http://localhost/awd', {
17+
// headers: {
18+
// origin: 'https://saltyaom.com'
19+
// }
20+
// })
21+
// )
2222

2323
export type App = typeof app
24-
console.log('Server is running on port 3000.')

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elysiajs/cors",
3-
"version": "0.7.1",
3+
"version": "0.7.2",
44
"description": "Plugin for Elysia that for Cross Origin Requests (CORs)",
55
"author": {
66
"name": "saltyAom",

src/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { Elysia, Handler, Context } from 'elysia'
2-
3-
import { isAbsolute } from 'path'
1+
import { Elysia, type Context } from 'elysia'
42

53
type Origin = string | RegExp | ((request: Request) => boolean | void)
64

@@ -183,6 +181,7 @@ export const cors = (
183181
const processOrigin = (origin: Origin, request: Request, from: string) => {
184182
switch (typeof origin) {
185183
case 'string':
184+
// eslint-disable-next-line no-case-declarations
186185
const protocolStart = from.indexOf('://')
187186

188187
// Malform URL, invalid protocol
@@ -202,7 +201,7 @@ export const cors = (
202201
// origin === `true` means any origin
203202
if (origin === true) {
204203
set.headers['Vary'] = '*'
205-
set.headers['Access-Control-Allow-Origin'] = '*'
204+
set.headers['Access-Control-Allow-Origin'] = request.headers.get('Origin') || '*'
206205

207206
return
208207
}
@@ -218,7 +217,7 @@ export const cors = (
218217
if (value === true) {
219218
set.headers['Vary'] = origin ? 'Origin' : '*'
220219
set.headers['Access-Control-Allow-Origin'] =
221-
request.headers.get('Origin') ?? '*'
220+
request.headers.get('Origin') || '*'
222221

223222
return
224223
}

0 commit comments

Comments
 (0)