Skip to content

Commit

Permalink
🔧 fix: origin doesn't process on array, default to * instead of true
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Mar 12, 2024
1 parent 853dd27 commit 61e4966
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 36 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@

# 1.0.0-rc.0 - 1 Mar 2024
Change:
- Add support for Elysia 1.0


# 1.0.0-beta.1 - 17 Feb 2024
Change:
- Add support for Elysia 1.0


# 1.0.0-beta.0 - 6 Feb 2024
Change:
- Add support for Elysia 1.0
Expand Down
Binary file modified bun.lockb
Binary file not shown.
9 changes: 8 additions & 1 deletion example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const app = new Elysia()
})
)
.get('/', () => 'A')
.post('/', ({ body, cookie: { session } }) => {
session!.value = 'hi'

return body
})
.listen(3000)

console.log(`Elysia is running at ${app.server?.hostname}:${app.server?.port}`)
Expand All @@ -18,6 +23,8 @@ app.handle(
origin: 'https://saltyaom.com'
}
})
).then(x => x.headers.toJSON()).then(console.log)
)
.then((x) => x.headers.toJSON())
.then(console.log)

export type App = typeof app
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elysiajs/cors",
"version": "1.0.0-beta.1",
"version": "1.0.0-rc.1",
"description": "Plugin for Elysia that for Cross Origin Requests (CORs)",
"author": {
"name": "saltyAom",
Expand Down Expand Up @@ -35,12 +35,12 @@
"devDependencies": {
"@types/bun": "^1.0.4",
"@types/node": "^18.11.7",
"elysia": "1.0.0-beta.1",
"elysia": "1.0.0-rc.12",
"eslint": "^8.26.0",
"rimraf": "^3.0.2",
"typescript": "^5.2.2"
},
"peerDependencies": {
"elysia": ">= 1.0.0-beta.1"
"elysia": ">= 1.0.0-rc.0"
}
}
77 changes: 45 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const processOrigin = (
from: string
): boolean => {
if (Array.isArray(origin))
origin.some((o) => processOrigin(o, request, from))
return origin.some((o) => processOrigin(o, request, from))

switch (typeof origin) {
case 'string':
Expand Down Expand Up @@ -192,22 +192,22 @@ export const cors = (
preflight: true
}
) => {
const {
let {
aot = true,
origin = true,
methods = true,
origin = '*',
methods = '*',
allowedHeaders = '*',
exposedHeaders = '*',
credentials = true,
maxAge = 5,
preflight = true
} = config

const app = new Elysia({
name: '@elysiajs/cors',
seed: config,
aot: false
})
if (Array.isArray(allowedHeaders))
allowedHeaders = allowedHeaders.join(', ')

if (Array.isArray(exposedHeaders))
exposedHeaders = exposedHeaders.join(', ')

const origins =
typeof origin === 'boolean'
Expand All @@ -216,6 +216,14 @@ export const cors = (
? origin
: [origin]

const app = new Elysia({
name: '@elysiajs/cors',
seed: config,
aot
})

const anyOrigin = origins?.some((o) => o === '*')

const handleOrigin = (set: Context['set'], request: Request) => {
// origin === `true` means any origin
if (origin === true) {
Expand All @@ -226,6 +234,13 @@ export const cors = (
return
}

if (anyOrigin) {
set.headers['Vary'] = '*'
set.headers['Access-Control-Allow-Origin'] = '*'

return
}

if (!origins?.length) return

const headers: string[] = []
Expand Down Expand Up @@ -272,9 +287,11 @@ export const cors = (

if (allowedHeaders.length)
set.headers['Access-Control-Allow-Headers'] =
typeof allowedHeaders === 'string'
? allowedHeaders
: allowedHeaders.join(', ')
allowedHeaders as string

if (exposedHeaders.length)
set.headers['Access-Control-Expose-Headers'] =
exposedHeaders as string

if (maxAge)
set.headers['Access-Control-Max-Age'] = maxAge.toString()
Expand All @@ -288,9 +305,11 @@ export const cors = (

if (allowedHeaders.length)
set.headers['Access-Control-Allow-Headers'] =
typeof allowedHeaders === 'string'
? allowedHeaders
: allowedHeaders.join(', ')
allowedHeaders as string

if (exposedHeaders.length)
set.headers['Access-Control-Expose-Headers'] =
exposedHeaders as string

if (maxAge)
set.headers['Access-Control-Max-Age'] = maxAge.toString()
Expand All @@ -301,30 +320,24 @@ export const cors = (
})

const defaultHeaders: Record<string, string> = {
'Access-Control-Allow-Headers':
typeof allowedHeaders === 'string'
? allowedHeaders
: allowedHeaders.join(', '),
'Access-Control-Exposed-Headers':
typeof exposedHeaders === 'string'
? exposedHeaders
: exposedHeaders.join(', ')
'Access-Control-Allow-Headers': allowedHeaders,
'Access-Control-Exposed-Headers': exposedHeaders
}

if (credentials === true)
defaultHeaders['Access-Control-Allow-Credentials'] = 'true'

if (app.headers)
return app.headers(defaultHeaders).onRequest(({ set, request }) => {
handleOrigin(set, request)
handleMethod(set, request.method)
})

return app.onRequest(({ set, request }) => {
Object.assign(set.headers, defaultHeaders)

return app.headers(defaultHeaders).onRequest(({ set, request }) => {
handleOrigin(set, request)
handleMethod(set, request.method)

if (allowedHeaders.length)
set.headers['Access-Control-Allow-Headers'] =
allowedHeaders as string

if (exposedHeaders.length)
set.headers['Access-Control-Expose-Headers'] =
exposedHeaders as string
})
}

Expand Down

0 comments on commit 61e4966

Please sign in to comment.