@tinyhttp/cors
Advanced tools
+6
-2
| /// <reference types="node" /> | ||
| import { IncomingMessage as Request } from 'http'; | ||
| import { ServerResponse as Response } from "http"; | ||
| declare const METHODS: string[]; | ||
| declare const defaultMethods: string[]; | ||
| declare const defaultHeaders: string[]; | ||
| /** | ||
| * CORS Middleware | ||
| */ | ||
| declare const cors: ({ host, methods, headers }: { | ||
@@ -10,2 +14,2 @@ host?: string; | ||
| }) => (_req: Request, res: Response) => void; | ||
| export { cors as default, METHODS }; | ||
| export { defaultMethods, defaultHeaders, cors }; |
+1
-12
@@ -1,12 +0,1 @@ | ||
| const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'HEAD']; | ||
| const cors = ({ host = '*', methods = METHODS, headers = ['Origin', 'X-Requested-With', 'Content-Type'] }) => { | ||
| const prefix = 'Access-Control-Allow'; | ||
| return (_req, res) => { | ||
| res.setHeader(`${prefix}-Origin`, host); | ||
| res.setHeader(`${prefix}-Headers`, headers.join(', ')); | ||
| res.setHeader(`${prefix}-Methods`, methods.join(', ')); | ||
| }; | ||
| }; | ||
| export default cors; | ||
| export { METHODS }; | ||
| const e=["GET","POST","PUT","PATCH","HEAD"],t=["Origin","X-Requested-With","Content-Type"],s=({host:s="*",methods:o=e,headers:r=t})=>{const n="Access-Control-Allow";return(e,t)=>{t.setHeader(n+"-Origin",s),t.setHeader(n+"-Headers",r.join(", ")),t.setHeader(n+"-Methods",o.join(", "))}};export{s as cors,t as defaultHeaders,e as defaultMethods}; |
+1
-16
@@ -1,16 +0,1 @@ | ||
| 'use strict'; | ||
| Object.defineProperty(exports, '__esModule', { value: true }); | ||
| const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'HEAD']; | ||
| const cors = ({ host = '*', methods = METHODS, headers = ['Origin', 'X-Requested-With', 'Content-Type'] }) => { | ||
| const prefix = 'Access-Control-Allow'; | ||
| return (_req, res) => { | ||
| res.setHeader(`${prefix}-Origin`, host); | ||
| res.setHeader(`${prefix}-Headers`, headers.join(', ')); | ||
| res.setHeader(`${prefix}-Methods`, methods.join(', ')); | ||
| }; | ||
| }; | ||
| exports.METHODS = METHODS; | ||
| exports.default = cors; | ||
| "use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=["GET","POST","PUT","PATCH","HEAD"],t=["Origin","X-Requested-With","Content-Type"];exports.cors=({host:s="*",methods:o=e,headers:r=t})=>{const d="Access-Control-Allow";return(e,t)=>{t.setHeader(d+"-Origin",s),t.setHeader(d+"-Headers",r.join(", ")),t.setHeader(d+"-Methods",o.join(", "))}},exports.defaultHeaders=t,exports.defaultMethods=e; |
+21
-22
| { | ||
| "name": "@tinyhttp/cors", | ||
| "version": "0.1.10", | ||
| "description": "tinyhttp CORS module", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "homepage": "https://github.com/talentlessguy/tinyhttp", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/talentlessguy/tinyhttp.git", | ||
| "directory": "packages/cors" | ||
| }, | ||
| "keywords": [ | ||
| "tinyhttp", | ||
| "node.js", | ||
| "web framework", | ||
| "web", | ||
| "backend", | ||
| "cors" | ||
| ], | ||
| "author": "v1rtl", | ||
| "license": "MIT", | ||
| "gitHead": "584a7d9a98e23543ef5285af59532a583c578d11" | ||
| "name": "@tinyhttp/cors", | ||
| "version": "0.1.21", | ||
| "description": "tinyhttp CORS module", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "homepage": "https://github.com/talentlessguy/tinyhttp", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/talentlessguy/tinyhttp.git", | ||
| "directory": "packages/cors" | ||
| }, | ||
| "keywords": [ | ||
| "tinyhttp", | ||
| "node.js", | ||
| "web framework", | ||
| "web", | ||
| "backend", | ||
| "cors" | ||
| ], | ||
| "author": "v1rtl", | ||
| "license": "MIT" | ||
| } |
+62
-12
@@ -7,23 +7,73 @@ # @tinyhttp/cors | ||
| This is a [Node.js](https://nodejs.org/) module available through the | ||
| [npm registry](https://www.npmjs.com/). It can be installed using the | ||
| [`npm`](https://docs.npmjs.com/getting-started/installing-npm-packages-locally) | ||
| or | ||
| [`yarn`](https://yarnpkg.com/en/) | ||
| command line tools. | ||
| ```sh | ||
| npm install @tinyhttp/cors --save | ||
| npm install @tinyhttp/cors | ||
| ``` | ||
| ## Dependencies | ||
| ## API | ||
| - [@tinyhttp/app](https://ghub.io/@tinyhttp/app): tinyhttp core | ||
| ```js | ||
| import { cors } from '@tinyhttp/cors' | ||
| ``` | ||
| ## Dev Dependencies | ||
| ### Options | ||
| None | ||
| #### `host` | ||
| Host that is allowed to send cross-origin requests. Defaults to '\*'. | ||
| #### `methods` | ||
| Allowed methods for performing a cross-origin request. Default ones are `['GET', 'POST', 'PUT', 'PATCH', 'HEAD']` and can be accessed with `defaultMethods`: | ||
| ```ts | ||
| import { App } from '@tinyhttp/app' | ||
| import { defaultMethods, cors } from '@tinyhttp/cors' | ||
| const app = new App() | ||
| app.use( | ||
| cors({ | ||
| methods: defaultMethods.filter(m => m !== 'DELETE'), | ||
| host: 'https://example.com' | ||
| }) | ||
| ) | ||
| ``` | ||
| #### `headers` | ||
| Allowed HTTP headers that can be sent in a cross-origin request. Default ones are `['Origin', 'X-Requested-With', 'Content-Type']` and can be accessed with `defaultHeaders`: | ||
| ```ts | ||
| import { App } from '@tinyhttp/app' | ||
| import { defaultHeaders, cors } from '@tinyhttp/cors' | ||
| const app = new App() | ||
| app.use( | ||
| cors({ | ||
| methods: [...defaultHeaders, 'X-Custom-Header'], | ||
| host: 'https://example.com' | ||
| }) | ||
| ) | ||
| ``` | ||
| ## Example | ||
| ```ts | ||
| import { App } from '@tinyhttp/app' | ||
| import { cors } from '@tinyhttp/cors' | ||
| const app = new App() | ||
| app | ||
| .use( | ||
| cors({ | ||
| host: 'https://example.com' | ||
| }) | ||
| ) | ||
| .get('/', (_, res) => void res.end('Hello World')) | ||
| ``` | ||
| ## License | ||
| MIT |
+7
-4
| import { IncomingMessage as Request, ServerResponse as Response } from 'http' | ||
| export const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'HEAD'] | ||
| export const defaultMethods = ['GET', 'POST', 'PUT', 'PATCH', 'HEAD'] | ||
| const cors = ({ host = '*', methods = METHODS, headers = ['Origin', 'X-Requested-With', 'Content-Type'] }) => { | ||
| export const defaultHeaders = ['Origin', 'X-Requested-With', 'Content-Type'] | ||
| /** | ||
| * CORS Middleware | ||
| */ | ||
| export const cors = ({ host = '*', methods = defaultMethods, headers = defaultHeaders }) => { | ||
| const prefix = 'Access-Control-Allow' | ||
@@ -14,3 +19,1 @@ | ||
| } | ||
| export default cors |
-21
| MIT License | ||
| Copyright (c) 2020 v 1 r t l | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 2 instances in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
79
172.41%3660
-8.98%6
-14.29%32
-28.89%3
200%