@middy/http-cors 3.0.0-alpha.6 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -25
- package/index.cjs +3 -0
- package/index.js +2 -109
- package/package.json +13 -6
package/README.md
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
1
|
-
# Middy CORS middleware
|
|
2
|
-
|
|
3
|
-
<div align="center">
|
|
4
|
-
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
|
|
5
|
-
</div>
|
|
6
|
-
|
|
7
1
|
<div align="center">
|
|
2
|
+
<h1>Middy CORS middleware</h1>
|
|
3
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
|
|
8
4
|
<p><strong>CORS middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
|
|
9
|
-
</div>
|
|
10
|
-
|
|
11
|
-
<div align="center">
|
|
12
5
|
<p>
|
|
13
|
-
<a href="
|
|
6
|
+
<a href="https://www.npmjs.com/package/@middy/http-cors?activeTab=versions">
|
|
14
7
|
<img src="https://badge.fury.io/js/%40middy%2Fhttp-cors.svg" alt="npm version" style="max-width:100%;">
|
|
15
8
|
</a>
|
|
9
|
+
<a href="https://packagephobia.com/result?p=@middy/http-cors">
|
|
10
|
+
<img src="https://packagephobia.com/badge?p=@middy/http-cors" alt="npm install size" style="max-width:100%;">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
|
|
14
|
+
</a>
|
|
15
|
+
<br/>
|
|
16
|
+
<a href="https://standardjs.com/">
|
|
17
|
+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
|
|
18
|
+
</a>
|
|
16
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
20
|
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
|
|
18
21
|
</a>
|
|
19
|
-
<a href="https://
|
|
20
|
-
<img src="https://img.shields.io/
|
|
22
|
+
<a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
|
|
23
|
+
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
|
|
21
24
|
</a>
|
|
25
|
+
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
|
+
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
|
27
|
+
</a>
|
|
28
|
+
<br/>
|
|
22
29
|
<a href="https://gitter.im/middyjs/Lobby">
|
|
23
|
-
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter"
|
|
30
|
+
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
|
|
33
|
+
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
24
34
|
</a>
|
|
25
35
|
</p>
|
|
26
36
|
</div>
|
|
@@ -38,20 +48,20 @@ To install this middleware you can use NPM:
|
|
|
38
48
|
npm install --save @middy/http-cors
|
|
39
49
|
```
|
|
40
50
|
|
|
41
|
-
|
|
42
51
|
## Options
|
|
43
52
|
|
|
44
|
-
- `credentials` (
|
|
45
|
-
- `headers` (string) (
|
|
46
|
-
- `methods` (string) (
|
|
47
|
-
- `getOrigin` (function(incomingOrigin:string, options))
|
|
48
|
-
- `origin` (string) (
|
|
49
|
-
- `origins` (array) (
|
|
50
|
-
- `exposeHeaders` (string) (
|
|
51
|
-
- `maxAge` (string) (
|
|
52
|
-
- `requestHeaders` (string) (
|
|
53
|
-
- `requestMethods` (string) (
|
|
54
|
-
- `cacheControl` (string) (
|
|
53
|
+
- `credentials` (boolean) (default `undefined`): if true, sets `Access-Control-Allow-Credentials`
|
|
54
|
+
- `headers` (string) (default `undefined`): value to put in `Access-Control-Allow-Headers`
|
|
55
|
+
- `methods` (string) (default `undefined`): value to put in `Access-Control-Allow-Methods`
|
|
56
|
+
- `getOrigin` (function(incomingOrigin:string, options)): take full control of the generating the returned origin. Defaults to using the origin or origins option.
|
|
57
|
+
- `origin` (string) (default `'*'`): origin to put in the header
|
|
58
|
+
- `origins` (array) (default `[]`): An array of allowed origins. The incoming origin is matched against the list and is returned if present.
|
|
59
|
+
- `exposeHeaders` (string) (default `undefined`): value to put in `Access-Control-Expose-Headers`
|
|
60
|
+
- `maxAge` (string) (default `undefined`): value to put in `Access-Control-Max-Age` header
|
|
61
|
+
- `requestHeaders` (string) (default `undefined`): value to put in `Access-Control-Request-Headers`
|
|
62
|
+
- `requestMethods` (string) (default `undefined`): value to put in `Access-Control-Request-Methods`
|
|
63
|
+
- `cacheControl` (string) (default `undefined`): value to put in `Cache-Control header` on pre-flight (OPTIONS) requests
|
|
64
|
+
- `vary` (string) (default `undefined`): value to put in `Vary`, will set to `Origin` if `Access-Control-Allow-Origin` is not `*` and option unset.
|
|
55
65
|
|
|
56
66
|
```javascript
|
|
57
67
|
import middy from '@middy/core'
|
package/index.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _util=require("@middy/util");const getOrigin=(incomingOrigin,options={})=>{if(options.origins.length>0){if(incomingOrigin&&options.origins.includes(incomingOrigin)){return incomingOrigin}else{return options.origins[0]}}else{if(incomingOrigin&&options.credentials&&options.origin==="*"){return incomingOrigin}return options.origin}};const defaults={getOrigin,credentials:undefined,headers:undefined,methods:undefined,origin:"*",origins:[],exposeHeaders:undefined,maxAge:undefined,requestHeaders:undefined,requestMethods:undefined,cacheControl:undefined,vary:undefined};const httpCorsMiddleware=(opts={})=>{const options={...defaults,...opts};const httpCorsMiddlewareAfter=async request=>{(0,_util).normalizeHttpResponse(request);const{headers}=request.response;const existingHeaders=Object.keys(headers);if(existingHeaders.includes("Access-Control-Allow-Credentials")){options.credentials=headers["Access-Control-Allow-Credentials"]==="true"}if(options.credentials){headers["Access-Control-Allow-Credentials"]=String(options.credentials)}if(options.headers&&!existingHeaders.includes("Access-Control-Allow-Headers")){headers["Access-Control-Allow-Headers"]=options.headers}if(options.methods&&!existingHeaders.includes("Access-Control-Allow-Methods")){headers["Access-Control-Allow-Methods"]=options.methods}if(!existingHeaders.includes("Access-Control-Allow-Origin")){const eventHeaders=request.event.headers??{};const incomingOrigin=eventHeaders.Origin??eventHeaders.origin;headers["Access-Control-Allow-Origin"]=options.getOrigin(incomingOrigin,options)}let vary=options.vary;if(headers["Access-Control-Allow-Origin"]!=="*"&&!vary){vary="Origin"}if(vary&&!existingHeaders.includes("Vary")){headers.Vary=vary}if(options.exposeHeaders&&!existingHeaders.includes("Access-Control-Expose-Headers")){headers["Access-Control-Expose-Headers"]=options.exposeHeaders}if(options.maxAge&&!existingHeaders.includes("Access-Control-Max-Age")){headers["Access-Control-Max-Age"]=String(options.maxAge)}if(options.requestHeaders&&!existingHeaders.includes("Access-Control-Request-Headers")){headers["Access-Control-Request-Headers"]=options.requestHeaders}if(options.requestMethods&&!existingHeaders.includes("Access-Control-Request-Methods")){headers["Access-Control-Request-Methods"]=options.requestMethods}const httpMethod=getVersionHttpMethod[request.event.version??"1.0"]?.(request.event);if(!httpMethod){throw new Error("[http-cors] Unknown http event format")}if(httpMethod==="OPTIONS"&&options.cacheControl&&!existingHeaders.includes("Cache-Control")){headers["Cache-Control"]=options.cacheControl}request.response.headers=headers};const httpCorsMiddlewareOnError=async request=>{if(request.response===undefined)return;return httpCorsMiddlewareAfter(request)};return{after:httpCorsMiddlewareAfter,onError:httpCorsMiddlewareOnError}};const getVersionHttpMethod={"1.0":event=>event.httpMethod,"2.0":event=>event.requestContext.http.method};var _default=httpCorsMiddleware;exports.default=_default
|
|
2
|
+
|
|
3
|
+
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,110 +1,3 @@
|
|
|
1
|
-
import
|
|
1
|
+
import{normalizeHttpResponse}from"@middy/util";const getOrigin=(incomingOrigin,options={})=>{if(options.origins.length>0){if(incomingOrigin&&options.origins.includes(incomingOrigin)){return incomingOrigin}else{return options.origins[0]}}else{if(incomingOrigin&&options.credentials&&options.origin==="*"){return incomingOrigin}return options.origin}};const defaults={getOrigin,credentials:undefined,headers:undefined,methods:undefined,origin:"*",origins:[],exposeHeaders:undefined,maxAge:undefined,requestHeaders:undefined,requestMethods:undefined,cacheControl:undefined,vary:undefined};const httpCorsMiddleware=(opts={})=>{const options={...defaults,...opts};const httpCorsMiddlewareAfter=async request=>{normalizeHttpResponse(request);const{headers}=request.response;const existingHeaders=Object.keys(headers);if(existingHeaders.includes("Access-Control-Allow-Credentials")){options.credentials=headers["Access-Control-Allow-Credentials"]==="true"}if(options.credentials){headers["Access-Control-Allow-Credentials"]=String(options.credentials)}if(options.headers&&!existingHeaders.includes("Access-Control-Allow-Headers")){headers["Access-Control-Allow-Headers"]=options.headers}if(options.methods&&!existingHeaders.includes("Access-Control-Allow-Methods")){headers["Access-Control-Allow-Methods"]=options.methods}if(!existingHeaders.includes("Access-Control-Allow-Origin")){const eventHeaders=request.event.headers??{};const incomingOrigin=eventHeaders.Origin??eventHeaders.origin;headers["Access-Control-Allow-Origin"]=options.getOrigin(incomingOrigin,options)}let vary=options.vary;if(headers["Access-Control-Allow-Origin"]!=="*"&&!vary){vary="Origin"}if(vary&&!existingHeaders.includes("Vary")){headers.Vary=vary}if(options.exposeHeaders&&!existingHeaders.includes("Access-Control-Expose-Headers")){headers["Access-Control-Expose-Headers"]=options.exposeHeaders}if(options.maxAge&&!existingHeaders.includes("Access-Control-Max-Age")){headers["Access-Control-Max-Age"]=String(options.maxAge)}if(options.requestHeaders&&!existingHeaders.includes("Access-Control-Request-Headers")){headers["Access-Control-Request-Headers"]=options.requestHeaders}if(options.requestMethods&&!existingHeaders.includes("Access-Control-Request-Methods")){headers["Access-Control-Request-Methods"]=options.requestMethods}const httpMethod=getVersionHttpMethod[request.event.version??"1.0"]?.(request.event);if(!httpMethod){throw new Error("[http-cors] Unknown http event format")}if(httpMethod==="OPTIONS"&&options.cacheControl&&!existingHeaders.includes("Cache-Control")){headers["Cache-Control"]=options.cacheControl}request.response.headers=headers};const httpCorsMiddlewareOnError=async request=>{if(request.response===undefined)return;return httpCorsMiddlewareAfter(request)};return{after:httpCorsMiddlewareAfter,onError:httpCorsMiddlewareOnError}};const getVersionHttpMethod={"1.0":event=>event.httpMethod,"2.0":event=>event.requestContext.http.method};export default httpCorsMiddleware
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
if (options.origins.length > 0) {
|
|
5
|
-
if (incomingOrigin && options.origins.includes(incomingOrigin)) {
|
|
6
|
-
return incomingOrigin;
|
|
7
|
-
} else {
|
|
8
|
-
return options.origins[0];
|
|
9
|
-
}
|
|
10
|
-
} else {
|
|
11
|
-
if (incomingOrigin && options.credentials && options.origin === '*') {
|
|
12
|
-
return incomingOrigin;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return options.origin;
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const defaults = {
|
|
20
|
-
getOrigin,
|
|
21
|
-
credentials: undefined,
|
|
22
|
-
headers: undefined,
|
|
23
|
-
methods: undefined,
|
|
24
|
-
origin: '*',
|
|
25
|
-
origins: [],
|
|
26
|
-
exposeHeaders: undefined,
|
|
27
|
-
maxAge: undefined,
|
|
28
|
-
requestHeaders: undefined,
|
|
29
|
-
requestMethods: undefined,
|
|
30
|
-
cacheControl: undefined
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const httpCorsMiddleware = (opts = {}) => {
|
|
34
|
-
const options = { ...defaults,
|
|
35
|
-
...opts
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const httpCorsMiddlewareAfter = async request => {
|
|
39
|
-
var _getVersionHttpMethod;
|
|
40
|
-
|
|
41
|
-
normalizeHttpResponse(request);
|
|
42
|
-
const existingHeaders = Object.keys(request.response.headers);
|
|
43
|
-
|
|
44
|
-
if (existingHeaders.includes('Access-Control-Allow-Credentials')) {
|
|
45
|
-
options.credentials = request.response.headers['Access-Control-Allow-Credentials'] === 'true';
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (options.credentials) {
|
|
49
|
-
request.response.headers['Access-Control-Allow-Credentials'] = String(options.credentials);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (options.headers && !existingHeaders.includes('Access-Control-Allow-Headers')) {
|
|
53
|
-
request.response.headers['Access-Control-Allow-Headers'] = options.headers;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (options.methods && !existingHeaders.includes('Access-Control-Allow-Methods')) {
|
|
57
|
-
request.response.headers['Access-Control-Allow-Methods'] = options.methods;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (!existingHeaders.includes('Access-Control-Allow-Origin')) {
|
|
61
|
-
const eventHeaders = request.event.headers ?? {};
|
|
62
|
-
const incomingOrigin = eventHeaders.origin ?? eventHeaders.Origin;
|
|
63
|
-
request.response.headers['Access-Control-Allow-Origin'] = options.getOrigin(incomingOrigin, options);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (options.exposeHeaders && !existingHeaders.includes('Access-Control-Expose-Headers')) {
|
|
67
|
-
request.response.headers['Access-Control-Expose-Headers'] = options.exposeHeaders;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (options.maxAge && !existingHeaders.includes('Access-Control-Max-Age')) {
|
|
71
|
-
request.response.headers['Access-Control-Max-Age'] = String(options.maxAge);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (options.requestHeaders && !existingHeaders.includes('Access-Control-Request-Headers')) {
|
|
75
|
-
request.response.headers['Access-Control-Request-Headers'] = options.requestHeaders;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (options.requestMethods && !existingHeaders.includes('Access-Control-Request-Methods')) {
|
|
79
|
-
request.response.headers['Access-Control-Request-Methods'] = options.requestMethods;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const httpMethod = (_getVersionHttpMethod = getVersionHttpMethod[request.event.version ?? '1.0']) === null || _getVersionHttpMethod === void 0 ? void 0 : _getVersionHttpMethod.call(getVersionHttpMethod, request.event);
|
|
83
|
-
|
|
84
|
-
if (!httpMethod) {
|
|
85
|
-
throw new Error('[http-cors] Unknown http event format');
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (httpMethod === 'OPTIONS') {
|
|
89
|
-
if (options.cacheControl && !existingHeaders.includes('Cache-Control')) {
|
|
90
|
-
request.response.headers['Cache-Control'] = String(options.cacheControl);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const httpCorsMiddlewareOnError = async request => {
|
|
96
|
-
if (request.response === undefined) return;
|
|
97
|
-
return httpCorsMiddlewareAfter(request);
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
return {
|
|
101
|
-
after: httpCorsMiddlewareAfter,
|
|
102
|
-
onError: httpCorsMiddlewareOnError
|
|
103
|
-
};
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
const getVersionHttpMethod = {
|
|
107
|
-
'1.0': event => event.httpMethod,
|
|
108
|
-
'2.0': event => event.requestContext.http.method
|
|
109
|
-
};
|
|
110
|
-
export default httpCorsMiddleware;
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-cors",
|
|
3
|
-
"version": "3.0.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "CORS (Cross-Origin Resource Sharing) middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,10 +10,17 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"exports":
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./index.js",
|
|
16
|
+
"require": "./index.cjs",
|
|
17
|
+
"types": "./index.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
14
20
|
"types": "index.d.ts",
|
|
15
21
|
"files": [
|
|
16
22
|
"index.js",
|
|
23
|
+
"index.cjs",
|
|
17
24
|
"index.d.ts"
|
|
18
25
|
],
|
|
19
26
|
"scripts": {
|
|
@@ -45,12 +52,12 @@
|
|
|
45
52
|
"bugs": {
|
|
46
53
|
"url": "https://github.com/middyjs/middy/issues"
|
|
47
54
|
},
|
|
48
|
-
"homepage": "https://
|
|
49
|
-
"gitHead": "
|
|
55
|
+
"homepage": "https://middy.js.org",
|
|
56
|
+
"gitHead": "01520fa8628a36c2f89e126cad656a16547ea0d6",
|
|
50
57
|
"dependencies": {
|
|
51
|
-
"@middy/util": "^3.0.0
|
|
58
|
+
"@middy/util": "^3.0.0"
|
|
52
59
|
},
|
|
53
60
|
"devDependencies": {
|
|
54
|
-
"@middy/core": "^3.0.0
|
|
61
|
+
"@middy/core": "^3.0.0"
|
|
55
62
|
}
|
|
56
63
|
}
|