@elysiajs/cors
Advanced tools
Comparing version 1.1.0-exp.0 to 1.1.0-exp.1
@@ -1,8 +0,9 @@ | ||
import { Elysia } from "elysia"; | ||
import { Elysia } from 'elysia'; | ||
type Origin = string | RegExp | ((request: Request) => boolean | void); | ||
export type HTTPMethod = "ACL" | "BIND" | "CHECKOUT" | "CONNECT" | "COPY" | "DELETE" | "GET" | "HEAD" | "LINK" | "LOCK" | "M-SEARCH" | "MERGE" | "MKACTIVITY" | "MKCALENDAR" | "MKCOL" | "MOVE" | "NOTIFY" | "OPTIONS" | "PATCH" | "POST" | "PROPFIND" | "PROPPATCH" | "PURGE" | "PUT" | "REBIND" | "REPORT" | "SEARCH" | "SOURCE" | "SUBSCRIBE" | "TRACE" | "UNBIND" | "UNLINK" | "UNLOCK" | "UNSUBSCRIBE"; | ||
export type HTTPMethod = 'ACL' | 'BIND' | 'CHECKOUT' | 'CONNECT' | 'COPY' | 'DELETE' | 'GET' | 'HEAD' | 'LINK' | 'LOCK' | 'M-SEARCH' | 'MERGE' | 'MKACTIVITY' | 'MKCALENDAR' | 'MKCOL' | 'MOVE' | 'NOTIFY' | 'OPTIONS' | 'PATCH' | 'POST' | 'PROPFIND' | 'PROPPATCH' | 'PURGE' | 'PUT' | 'REBIND' | 'REPORT' | 'SEARCH' | 'SOURCE' | 'SUBSCRIBE' | 'TRACE' | 'UNBIND' | 'UNLINK' | 'UNLOCK' | 'UNSUBSCRIBE'; | ||
type MaybeArray<T> = T | T[]; | ||
interface CORSConfig { | ||
aot?: boolean; | ||
origin?: Origin | boolean | Origin[]; | ||
methods?: boolean | undefined | null | "" | "*" | HTTPMethod | HTTPMethod[]; | ||
methods?: boolean | undefined | null | '' | '*' | MaybeArray<HTTPMethod | (string & {})>; | ||
allowedHeaders?: true | string | string[]; | ||
@@ -25,3 +26,2 @@ exposeHeaders?: true | string | string[]; | ||
macro: {}; | ||
macroFn: {}; | ||
}, {}, { | ||
@@ -28,0 +28,0 @@ derive: {}; |
@@ -5,9 +5,9 @@ "use strict"; | ||
const elysia_1 = require("elysia"); | ||
const isBun = typeof new Headers()?.toJSON === "function"; | ||
const isBun = typeof new Headers()?.toJSON === 'function'; | ||
const processHeaders = (headers) => { | ||
if (isBun) | ||
return Object.keys(headers.toJSON()).join(", "); | ||
let keys = ""; | ||
return Object.keys(headers.toJSON()).join(', '); | ||
let keys = ''; | ||
headers.forEach((_, key) => { | ||
keys += key + ", "; | ||
keys += key + ', '; | ||
}); | ||
@@ -22,9 +22,9 @@ if (keys) | ||
switch (typeof origin) { | ||
case "string": | ||
if (origin.indexOf("://") === -1) | ||
case 'string': | ||
if (origin.indexOf('://') === -1) | ||
return from.includes(origin); | ||
return origin === from; | ||
case "function": | ||
case 'function': | ||
return origin(request) === true; | ||
case "object": | ||
case 'object': | ||
if (origin instanceof RegExp) | ||
@@ -36,8 +36,8 @@ return origin.test(from); | ||
const cors = (config) => { | ||
let { aot = true, origin = true, methods = true, allowedHeaders = true, exposeHeaders = true, credentials = true, maxAge = 5, preflight = true, } = config ?? {}; | ||
let { aot = true, origin = true, methods = true, allowedHeaders = true, exposeHeaders = true, credentials = true, maxAge = 5, preflight = true } = config ?? {}; | ||
if (Array.isArray(allowedHeaders)) | ||
allowedHeaders = allowedHeaders.join(", "); | ||
allowedHeaders = allowedHeaders.join(', '); | ||
if (Array.isArray(exposeHeaders)) | ||
exposeHeaders = exposeHeaders.join(", "); | ||
const origins = typeof origin === "boolean" | ||
exposeHeaders = exposeHeaders.join(', '); | ||
const origins = typeof origin === 'boolean' | ||
? undefined | ||
@@ -48,17 +48,17 @@ : Array.isArray(origin) | ||
const app = new elysia_1.Elysia({ | ||
name: "@elysiajs/cors", | ||
name: '@elysiajs/cors', | ||
seed: config, | ||
aot, | ||
aot | ||
}); | ||
const anyOrigin = origins?.some((o) => o === "*"); | ||
const anyOrigin = origins?.some((o) => o === '*'); | ||
const handleOrigin = (set, request) => { | ||
if (origin === true) { | ||
set.headers["Vary"] = "*"; | ||
set.headers["Access-Control-Allow-Origin"] = | ||
request.headers.get("Origin") || "*"; | ||
set.headers.vary = '*'; | ||
set.headers['access-control-allow-origin'] = | ||
request.headers.get('Origin') || '*'; | ||
return; | ||
} | ||
if (anyOrigin) { | ||
set.headers["Vary"] = "*"; | ||
set.headers["Access-Control-Allow-Origin"] = "*"; | ||
set.headers.vary = '*'; | ||
set.headers['access-control-allow-origin'] = '*'; | ||
return; | ||
@@ -70,8 +70,8 @@ } | ||
if (origins.length) { | ||
const from = request.headers.get("Origin") ?? ""; | ||
const from = request.headers.get('Origin') ?? ''; | ||
for (let i = 0; i < origins.length; i++) { | ||
const value = processOrigin(origins[i], request, from); | ||
if (value === true) { | ||
set.headers["Vary"] = origin ? "Origin" : "*"; | ||
set.headers["Access-Control-Allow-Origin"] = from || "*"; | ||
set.headers.vary = origin ? 'Origin' : '*'; | ||
set.headers['access-control-allow-origin'] = from || '*'; | ||
return; | ||
@@ -83,59 +83,46 @@ } | ||
} | ||
set.headers["Vary"] = "Origin"; | ||
set.headers.vary = 'Origin'; | ||
if (headers.length) | ||
set.headers["Access-Control-Allow-Origin"] = headers.join(", "); | ||
set.headers['access-control-allow-origin'] = headers.join(', '); | ||
}; | ||
const handleMethod = (set, method) => { | ||
if (!method) | ||
return; | ||
if (methods === true) | ||
return (set.headers["Access-Control-Allow-Methods"] = | ||
method ?? "*"); | ||
return (set.headers['access-control-allow-methods'] = method ?? '*'); | ||
if (methods === false || !methods?.length) | ||
return; | ||
if (methods === "*") | ||
return (set.headers["Access-Control-Allow-Methods"] = "*"); | ||
if (methods === '*') | ||
return (set.headers['access-control-allow-methods'] = '*'); | ||
if (!Array.isArray(methods)) | ||
return (set.headers["Access-Control-Allow-Methods"] = methods); | ||
set.headers["Access-Control-Allow-Methods"] = methods.join(", "); | ||
return (set.headers['access-control-allow-methods'] = methods); | ||
set.headers['access-control-allow-methods'] = methods.join(', '); | ||
}; | ||
const defaultHeaders = {}; | ||
if (typeof exposeHeaders === "string") | ||
defaultHeaders["Access-Control-Expose-Headers"] = exposeHeaders; | ||
if (typeof allowedHeaders === "string") | ||
defaultHeaders["Access-Control-Allow-Headers"] = allowedHeaders; | ||
if (typeof exposeHeaders === 'string') | ||
defaultHeaders['access-control-expose-headers'] = exposeHeaders; | ||
if (typeof allowedHeaders === 'string') | ||
defaultHeaders['access-control-allow-headers'] = allowedHeaders; | ||
if (credentials === true) | ||
defaultHeaders["Access-Control-Allow-Credentials"] = "true"; | ||
defaultHeaders['access-control-allow-credentials'] = 'true'; | ||
app.headers(defaultHeaders); | ||
function handleOption({ set, request }) { | ||
handleOrigin(set, request); | ||
handleMethod(set, request.headers.get('access-control-request-method')); | ||
if (allowedHeaders === true || exposeHeaders === true) { | ||
const headers = processHeaders(request.headers); | ||
if (allowedHeaders === true) | ||
set.headers['access-control-allow-headers'] = headers; | ||
if (exposeHeaders === true) | ||
set.headers['access-control-expose-headers'] = headers; | ||
} | ||
if (maxAge) | ||
set.headers['access-control-max-age'] = maxAge.toString(); | ||
return new Response(null, { | ||
status: 204 | ||
}); | ||
} | ||
if (preflight) | ||
app.options("/", ({ set, request }) => { | ||
handleOrigin(set, request); | ||
handleMethod(set, request.method); | ||
if (maxAge) | ||
set.headers["Access-Control-Max-Age"] = maxAge.toString(); | ||
if (allowedHeaders === true || exposeHeaders === true) { | ||
const headers = processHeaders(request.headers); | ||
if (allowedHeaders === true) | ||
set.headers["Access-Control-Allow-Headers"] = headers; | ||
if (exposeHeaders === true) | ||
set.headers["Access-Control-Expose-Headers"] = headers; | ||
} | ||
return new Response(null, { | ||
status: 204, | ||
}); | ||
}).options("/*", ({ set, request }) => { | ||
handleOrigin(set, request); | ||
handleMethod(set, request.method); | ||
if (allowedHeaders === true || exposeHeaders === true) { | ||
const headers = processHeaders(request.headers); | ||
if (allowedHeaders === true) | ||
set.headers["Access-Control-Allow-Headers"] = headers; | ||
if (exposeHeaders === true) | ||
set.headers["Access-Control-Expose-Headers"] = headers; | ||
} | ||
if (maxAge) | ||
set.headers["Access-Control-Max-Age"] = maxAge.toString(); | ||
return new Response(null, { | ||
status: 204, | ||
}); | ||
}); | ||
return app.onRequest(({ set, request }) => { | ||
app.options('/', handleOption).options('/*', handleOption); | ||
return app.onRequest(function processCors({ set, request }) { | ||
handleOrigin(set, request); | ||
@@ -146,5 +133,5 @@ handleMethod(set, request.method); | ||
if (allowedHeaders === true) | ||
set.headers["Access-Control-Allow-Headers"] = headers; | ||
set.headers['access-control-allow-headers'] = headers; | ||
if (exposeHeaders === true) | ||
set.headers["Access-Control-Expose-Headers"] = headers; | ||
set.headers['access-control-expose-headers'] = headers; | ||
} | ||
@@ -151,0 +138,0 @@ }); |
@@ -1,8 +0,9 @@ | ||
import { Elysia } from "elysia"; | ||
import { Elysia } from 'elysia'; | ||
type Origin = string | RegExp | ((request: Request) => boolean | void); | ||
export type HTTPMethod = "ACL" | "BIND" | "CHECKOUT" | "CONNECT" | "COPY" | "DELETE" | "GET" | "HEAD" | "LINK" | "LOCK" | "M-SEARCH" | "MERGE" | "MKACTIVITY" | "MKCALENDAR" | "MKCOL" | "MOVE" | "NOTIFY" | "OPTIONS" | "PATCH" | "POST" | "PROPFIND" | "PROPPATCH" | "PURGE" | "PUT" | "REBIND" | "REPORT" | "SEARCH" | "SOURCE" | "SUBSCRIBE" | "TRACE" | "UNBIND" | "UNLINK" | "UNLOCK" | "UNSUBSCRIBE"; | ||
export type HTTPMethod = 'ACL' | 'BIND' | 'CHECKOUT' | 'CONNECT' | 'COPY' | 'DELETE' | 'GET' | 'HEAD' | 'LINK' | 'LOCK' | 'M-SEARCH' | 'MERGE' | 'MKACTIVITY' | 'MKCALENDAR' | 'MKCOL' | 'MOVE' | 'NOTIFY' | 'OPTIONS' | 'PATCH' | 'POST' | 'PROPFIND' | 'PROPPATCH' | 'PURGE' | 'PUT' | 'REBIND' | 'REPORT' | 'SEARCH' | 'SOURCE' | 'SUBSCRIBE' | 'TRACE' | 'UNBIND' | 'UNLINK' | 'UNLOCK' | 'UNSUBSCRIBE'; | ||
type MaybeArray<T> = T | T[]; | ||
interface CORSConfig { | ||
aot?: boolean; | ||
origin?: Origin | boolean | Origin[]; | ||
methods?: boolean | undefined | null | "" | "*" | HTTPMethod | HTTPMethod[]; | ||
methods?: boolean | undefined | null | '' | '*' | MaybeArray<HTTPMethod | (string & {})>; | ||
allowedHeaders?: true | string | string[]; | ||
@@ -25,3 +26,2 @@ exposeHeaders?: true | string | string[]; | ||
macro: {}; | ||
macroFn: {}; | ||
}, {}, { | ||
@@ -28,0 +28,0 @@ derive: {}; |
@@ -1,9 +0,9 @@ | ||
import { Elysia } from "elysia"; | ||
const isBun = typeof new Headers()?.toJSON === "function"; | ||
import { Elysia } from 'elysia'; | ||
const isBun = typeof new Headers()?.toJSON === 'function'; | ||
const processHeaders = (headers) => { | ||
if (isBun) | ||
return Object.keys(headers.toJSON()).join(", "); | ||
let keys = ""; | ||
return Object.keys(headers.toJSON()).join(', '); | ||
let keys = ''; | ||
headers.forEach((_, key) => { | ||
keys += key + ", "; | ||
keys += key + ', '; | ||
}); | ||
@@ -18,9 +18,9 @@ if (keys) | ||
switch (typeof origin) { | ||
case "string": | ||
if (origin.indexOf("://") === -1) | ||
case 'string': | ||
if (origin.indexOf('://') === -1) | ||
return from.includes(origin); | ||
return origin === from; | ||
case "function": | ||
case 'function': | ||
return origin(request) === true; | ||
case "object": | ||
case 'object': | ||
if (origin instanceof RegExp) | ||
@@ -32,8 +32,8 @@ return origin.test(from); | ||
export const cors = (config) => { | ||
let { aot = true, origin = true, methods = true, allowedHeaders = true, exposeHeaders = true, credentials = true, maxAge = 5, preflight = true, } = config ?? {}; | ||
let { aot = true, origin = true, methods = true, allowedHeaders = true, exposeHeaders = true, credentials = true, maxAge = 5, preflight = true } = config ?? {}; | ||
if (Array.isArray(allowedHeaders)) | ||
allowedHeaders = allowedHeaders.join(", "); | ||
allowedHeaders = allowedHeaders.join(', '); | ||
if (Array.isArray(exposeHeaders)) | ||
exposeHeaders = exposeHeaders.join(", "); | ||
const origins = typeof origin === "boolean" | ||
exposeHeaders = exposeHeaders.join(', '); | ||
const origins = typeof origin === 'boolean' | ||
? undefined | ||
@@ -44,17 +44,17 @@ : Array.isArray(origin) | ||
const app = new Elysia({ | ||
name: "@elysiajs/cors", | ||
name: '@elysiajs/cors', | ||
seed: config, | ||
aot, | ||
aot | ||
}); | ||
const anyOrigin = origins?.some((o) => o === "*"); | ||
const anyOrigin = origins?.some((o) => o === '*'); | ||
const handleOrigin = (set, request) => { | ||
if (origin === true) { | ||
set.headers["Vary"] = "*"; | ||
set.headers["Access-Control-Allow-Origin"] = | ||
request.headers.get("Origin") || "*"; | ||
set.headers.vary = '*'; | ||
set.headers['access-control-allow-origin'] = | ||
request.headers.get('Origin') || '*'; | ||
return; | ||
} | ||
if (anyOrigin) { | ||
set.headers["Vary"] = "*"; | ||
set.headers["Access-Control-Allow-Origin"] = "*"; | ||
set.headers.vary = '*'; | ||
set.headers['access-control-allow-origin'] = '*'; | ||
return; | ||
@@ -66,8 +66,8 @@ } | ||
if (origins.length) { | ||
const from = request.headers.get("Origin") ?? ""; | ||
const from = request.headers.get('Origin') ?? ''; | ||
for (let i = 0; i < origins.length; i++) { | ||
const value = processOrigin(origins[i], request, from); | ||
if (value === true) { | ||
set.headers["Vary"] = origin ? "Origin" : "*"; | ||
set.headers["Access-Control-Allow-Origin"] = from || "*"; | ||
set.headers.vary = origin ? 'Origin' : '*'; | ||
set.headers['access-control-allow-origin'] = from || '*'; | ||
return; | ||
@@ -79,59 +79,46 @@ } | ||
} | ||
set.headers["Vary"] = "Origin"; | ||
set.headers.vary = 'Origin'; | ||
if (headers.length) | ||
set.headers["Access-Control-Allow-Origin"] = headers.join(", "); | ||
set.headers['access-control-allow-origin'] = headers.join(', '); | ||
}; | ||
const handleMethod = (set, method) => { | ||
if (!method) | ||
return; | ||
if (methods === true) | ||
return (set.headers["Access-Control-Allow-Methods"] = | ||
method ?? "*"); | ||
return (set.headers['access-control-allow-methods'] = method ?? '*'); | ||
if (methods === false || !methods?.length) | ||
return; | ||
if (methods === "*") | ||
return (set.headers["Access-Control-Allow-Methods"] = "*"); | ||
if (methods === '*') | ||
return (set.headers['access-control-allow-methods'] = '*'); | ||
if (!Array.isArray(methods)) | ||
return (set.headers["Access-Control-Allow-Methods"] = methods); | ||
set.headers["Access-Control-Allow-Methods"] = methods.join(", "); | ||
return (set.headers['access-control-allow-methods'] = methods); | ||
set.headers['access-control-allow-methods'] = methods.join(', '); | ||
}; | ||
const defaultHeaders = {}; | ||
if (typeof exposeHeaders === "string") | ||
defaultHeaders["Access-Control-Expose-Headers"] = exposeHeaders; | ||
if (typeof allowedHeaders === "string") | ||
defaultHeaders["Access-Control-Allow-Headers"] = allowedHeaders; | ||
if (typeof exposeHeaders === 'string') | ||
defaultHeaders['access-control-expose-headers'] = exposeHeaders; | ||
if (typeof allowedHeaders === 'string') | ||
defaultHeaders['access-control-allow-headers'] = allowedHeaders; | ||
if (credentials === true) | ||
defaultHeaders["Access-Control-Allow-Credentials"] = "true"; | ||
defaultHeaders['access-control-allow-credentials'] = 'true'; | ||
app.headers(defaultHeaders); | ||
function handleOption({ set, request }) { | ||
handleOrigin(set, request); | ||
handleMethod(set, request.headers.get('access-control-request-method')); | ||
if (allowedHeaders === true || exposeHeaders === true) { | ||
const headers = processHeaders(request.headers); | ||
if (allowedHeaders === true) | ||
set.headers['access-control-allow-headers'] = headers; | ||
if (exposeHeaders === true) | ||
set.headers['access-control-expose-headers'] = headers; | ||
} | ||
if (maxAge) | ||
set.headers['access-control-max-age'] = maxAge.toString(); | ||
return new Response(null, { | ||
status: 204 | ||
}); | ||
} | ||
if (preflight) | ||
app.options("/", ({ set, request }) => { | ||
handleOrigin(set, request); | ||
handleMethod(set, request.method); | ||
if (maxAge) | ||
set.headers["Access-Control-Max-Age"] = maxAge.toString(); | ||
if (allowedHeaders === true || exposeHeaders === true) { | ||
const headers = processHeaders(request.headers); | ||
if (allowedHeaders === true) | ||
set.headers["Access-Control-Allow-Headers"] = headers; | ||
if (exposeHeaders === true) | ||
set.headers["Access-Control-Expose-Headers"] = headers; | ||
} | ||
return new Response(null, { | ||
status: 204, | ||
}); | ||
}).options("/*", ({ set, request }) => { | ||
handleOrigin(set, request); | ||
handleMethod(set, request.method); | ||
if (allowedHeaders === true || exposeHeaders === true) { | ||
const headers = processHeaders(request.headers); | ||
if (allowedHeaders === true) | ||
set.headers["Access-Control-Allow-Headers"] = headers; | ||
if (exposeHeaders === true) | ||
set.headers["Access-Control-Expose-Headers"] = headers; | ||
} | ||
if (maxAge) | ||
set.headers["Access-Control-Max-Age"] = maxAge.toString(); | ||
return new Response(null, { | ||
status: 204, | ||
}); | ||
}); | ||
return app.onRequest(({ set, request }) => { | ||
app.options('/', handleOption).options('/*', handleOption); | ||
return app.onRequest(function processCors({ set, request }) { | ||
handleOrigin(set, request); | ||
@@ -142,5 +129,5 @@ handleMethod(set, request.method); | ||
if (allowedHeaders === true) | ||
set.headers["Access-Control-Allow-Headers"] = headers; | ||
set.headers['access-control-allow-headers'] = headers; | ||
if (exposeHeaders === true) | ||
set.headers["Access-Control-Expose-Headers"] = headers; | ||
set.headers['access-control-expose-headers'] = headers; | ||
} | ||
@@ -147,0 +134,0 @@ }); |
{ | ||
"name": "@elysiajs/cors", | ||
"version": "1.1.0-exp.0", | ||
"version": "1.1.0-exp.1", | ||
"description": "Plugin for Elysia that for Cross Origin Requests (CORs)", | ||
@@ -14,10 +14,13 @@ "author": { | ||
}, | ||
"main": "./dist/cjs/index.js", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
"bun": "./dist/index.js", | ||
"node": "./dist/cjs/index.js", | ||
"require": "./dist/cjs/index.js", | ||
"import": "./dist/index.js", | ||
"default": "./dist/cjs/index.js" | ||
"bun": "./dist/index.js", | ||
"node": "./dist/cjs/index.js", | ||
"require": "./dist/cjs/index.js", | ||
"import": "./dist/index.js", | ||
"default": "./dist/cjs/index.js", | ||
"types": "./dist/index.d.ts" | ||
}, | ||
"types": "./src/index.ts", | ||
"homepage": "https://github.com/elysiajs/elysia-cors", | ||
@@ -39,3 +42,3 @@ "keywords": [ | ||
"@types/node": "^18.11.7", | ||
"elysia": "1.1.0-exp.17", | ||
"elysia": "1.1.0-exp.26", | ||
"eslint": "^8.26.0", | ||
@@ -46,4 +49,4 @@ "rimraf": "^3.0.2", | ||
"peerDependencies": { | ||
"elysia": ">= 1.1.0-exp.17" | ||
"elysia": ">= 1.1.0-exp.26" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18196
7
336
1