@middy/http-cors 5.1.0 → 5.2.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.
Files changed (2) hide show
  1. package/index.js +143 -114
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,119 +1,148 @@
1
- import { normalizeHttpResponse } from '@middy/util';
2
- const getOrigin = (incomingOrigin, options = {})=>{
3
- if (options.origins.length > 0) {
4
- if (incomingOrigin && options.origins.includes(incomingOrigin)) {
5
- return incomingOrigin;
6
- } else {
7
- return options.origins[0];
8
- }
1
+ import { normalizeHttpResponse } from '@middy/util'
2
+
3
+ const getOrigin = (incomingOrigin, options = {}) => {
4
+ if (options.origins.length > 0) {
5
+ if (incomingOrigin && options.origins.includes(incomingOrigin)) {
6
+ return incomingOrigin
9
7
  } else {
10
- if (incomingOrigin && options.credentials && options.origin === '*') {
11
- return incomingOrigin;
12
- }
13
- return options.origin;
14
- }
15
- };
16
- const defaults = {
17
- disableBeforePreflightResponse: true,
18
- getOrigin,
19
- credentials: undefined,
20
- headers: undefined,
21
- methods: undefined,
22
- origin: '*',
23
- origins: [],
24
- exposeHeaders: undefined,
25
- maxAge: undefined,
26
- requestHeaders: undefined,
27
- requestMethods: undefined,
28
- cacheControl: undefined,
29
- vary: undefined
30
- };
31
- const httpCorsMiddleware = (opts = {})=>{
32
- const options = {
33
- ...defaults,
34
- ...opts
35
- };
36
- const httpCorsMiddlewareBefore = async (request)=>{
37
- if (options.disableBeforePreflightResponse) return;
38
- const method = getVersionHttpMethod[request.event.version ?? '1.0']?.(request.event);
39
- if (method === 'OPTIONS') {
40
- normalizeHttpResponse(request);
41
- const headers = {};
42
- modifyHeaders(headers, options, request);
43
- request.response.headers = headers;
44
- request.response.statusCode = 204;
45
- return request.response;
46
- }
47
- };
48
- const httpCorsMiddlewareAfter = async (request)=>{
49
- normalizeHttpResponse(request);
50
- const { headers } = request.response;
51
- modifyHeaders(headers, options, request);
52
- request.response.headers = headers;
53
- };
54
- const httpCorsMiddlewareOnError = async (request)=>{
55
- if (request.response === undefined) return;
56
- await httpCorsMiddlewareAfter(request);
57
- };
58
- return {
59
- before: httpCorsMiddlewareBefore,
60
- after: httpCorsMiddlewareAfter,
61
- onError: httpCorsMiddlewareOnError
62
- };
63
- };
64
- const getVersionHttpMethod = {
65
- '1.0': (event)=>event.httpMethod,
66
- '2.0': (event)=>event.requestContext.http.method
67
- };
68
- const modifyHeaders = (headers, options, request)=>{
69
- const existingHeaders = Object.keys(headers);
70
- if (existingHeaders.includes('Access-Control-Allow-Credentials')) {
71
- options.credentials = headers['Access-Control-Allow-Credentials'] === 'true';
72
- }
73
- if (options.credentials) {
74
- headers['Access-Control-Allow-Credentials'] = String(options.credentials);
75
- }
76
- if (options.headers && !existingHeaders.includes('Access-Control-Allow-Headers')) {
77
- headers['Access-Control-Allow-Headers'] = options.headers;
78
- }
79
- if (options.methods && !existingHeaders.includes('Access-Control-Allow-Methods')) {
80
- headers['Access-Control-Allow-Methods'] = options.methods;
81
- }
82
- if (!existingHeaders.includes('Access-Control-Allow-Origin')) {
83
- const eventHeaders = request.event.headers ?? {};
84
- const incomingOrigin = eventHeaders.Origin ?? eventHeaders.origin;
85
- headers['Access-Control-Allow-Origin'] = options.getOrigin(incomingOrigin, options);
86
- }
87
- let vary = options.vary;
88
- if (headers['Access-Control-Allow-Origin'] !== '*' && !vary) {
89
- vary = 'Origin';
90
- }
91
- if (vary && !existingHeaders.includes('Vary')) {
92
- headers.Vary = vary;
8
+ return options.origins[0]
93
9
  }
94
- if (options.exposeHeaders && !existingHeaders.includes('Access-Control-Expose-Headers')) {
95
- headers['Access-Control-Expose-Headers'] = options.exposeHeaders;
10
+ } else {
11
+ if (incomingOrigin && options.credentials && options.origin === '*') {
12
+ return incomingOrigin
96
13
  }
97
- if (options.maxAge && !existingHeaders.includes('Access-Control-Max-Age')) {
98
- headers['Access-Control-Max-Age'] = String(options.maxAge);
99
- }
100
- if (options.requestHeaders && !existingHeaders.includes('Access-Control-Request-Headers')) {
101
- headers['Access-Control-Request-Headers'] = options.requestHeaders;
102
- }
103
- if (options.requestMethods && !existingHeaders.includes('Access-Control-Request-Methods')) {
104
- headers['Access-Control-Request-Methods'] = options.requestMethods;
105
- }
106
- const httpMethod = getVersionHttpMethod[request.event.version ?? '1.0']?.(request.event);
107
- if (!httpMethod) {
108
- throw new Error('Unknown http event format', {
109
- cause: {
110
- package: '@middy/http-cors'
111
- }
112
- });
113
- }
114
- if (httpMethod === 'OPTIONS' && options.cacheControl && !existingHeaders.includes('Cache-Control')) {
115
- headers['Cache-Control'] = options.cacheControl;
14
+ return options.origin
15
+ }
16
+ }
17
+
18
+ const defaults = {
19
+ disableBeforePreflightResponse: true,
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
+ vary: undefined
32
+ }
33
+ const httpCorsMiddleware = (opts = {}) => {
34
+ const options = {
35
+ ...defaults,
36
+ ...opts
37
+ }
38
+ const httpCorsMiddlewareBefore = async (request) => {
39
+ if (options.disableBeforePreflightResponse) return
40
+
41
+ const method = getVersionHttpMethod[request.event.version ?? '1.0']?.(
42
+ request.event
43
+ )
44
+ if (method === 'OPTIONS') {
45
+ normalizeHttpResponse(request)
46
+ const headers = {}
47
+ modifyHeaders(headers, options, request)
48
+ request.response.headers = headers
49
+ request.response.statusCode = 204
50
+ return request.response
116
51
  }
117
- };
118
- export default httpCorsMiddleware;
52
+ }
53
+
54
+ const httpCorsMiddlewareAfter = async (request) => {
55
+ normalizeHttpResponse(request)
56
+ const { headers } = request.response
57
+ modifyHeaders(headers, options, request)
58
+ request.response.headers = headers
59
+ }
60
+ const httpCorsMiddlewareOnError = async (request) => {
61
+ if (request.response === undefined) return
62
+ await httpCorsMiddlewareAfter(request)
63
+ }
64
+ return {
65
+ before: httpCorsMiddlewareBefore,
66
+ after: httpCorsMiddlewareAfter,
67
+ onError: httpCorsMiddlewareOnError
68
+ }
69
+ }
70
+ const getVersionHttpMethod = {
71
+ '1.0': (event) => event.httpMethod,
72
+ '2.0': (event) => event.requestContext.http.method
73
+ }
74
+
75
+ const modifyHeaders = (headers, options, request) => {
76
+ const existingHeaders = Object.keys(headers)
77
+ if (existingHeaders.includes('Access-Control-Allow-Credentials')) {
78
+ options.credentials = headers['Access-Control-Allow-Credentials'] === 'true'
79
+ }
80
+ if (options.credentials) {
81
+ headers['Access-Control-Allow-Credentials'] = String(options.credentials)
82
+ }
83
+ if (
84
+ options.headers &&
85
+ !existingHeaders.includes('Access-Control-Allow-Headers')
86
+ ) {
87
+ headers['Access-Control-Allow-Headers'] = options.headers
88
+ }
89
+ if (
90
+ options.methods &&
91
+ !existingHeaders.includes('Access-Control-Allow-Methods')
92
+ ) {
93
+ headers['Access-Control-Allow-Methods'] = options.methods
94
+ }
95
+ if (!existingHeaders.includes('Access-Control-Allow-Origin')) {
96
+ const eventHeaders = request.event.headers ?? {}
97
+ const incomingOrigin = eventHeaders.Origin ?? eventHeaders.origin
98
+ headers['Access-Control-Allow-Origin'] = options.getOrigin(
99
+ incomingOrigin,
100
+ options
101
+ )
102
+ }
103
+ let vary = options.vary
104
+ if (headers['Access-Control-Allow-Origin'] !== '*' && !vary) {
105
+ vary = 'Origin'
106
+ }
107
+ if (vary && !existingHeaders.includes('Vary')) {
108
+ headers.Vary = vary
109
+ }
110
+ if (
111
+ options.exposeHeaders &&
112
+ !existingHeaders.includes('Access-Control-Expose-Headers')
113
+ ) {
114
+ headers['Access-Control-Expose-Headers'] = options.exposeHeaders
115
+ }
116
+ if (options.maxAge && !existingHeaders.includes('Access-Control-Max-Age')) {
117
+ headers['Access-Control-Max-Age'] = String(options.maxAge)
118
+ }
119
+ if (
120
+ options.requestHeaders &&
121
+ !existingHeaders.includes('Access-Control-Request-Headers')
122
+ ) {
123
+ headers['Access-Control-Request-Headers'] = options.requestHeaders
124
+ }
125
+ if (
126
+ options.requestMethods &&
127
+ !existingHeaders.includes('Access-Control-Request-Methods')
128
+ ) {
129
+ headers['Access-Control-Request-Methods'] = options.requestMethods
130
+ }
131
+ const httpMethod = getVersionHttpMethod[request.event.version ?? '1.0']?.(
132
+ request.event
133
+ )
134
+ if (!httpMethod) {
135
+ throw new Error('Unknown http event format', {
136
+ cause: { package: '@middy/http-cors' }
137
+ })
138
+ }
139
+ if (
140
+ httpMethod === 'OPTIONS' &&
141
+ options.cacheControl &&
142
+ !existingHeaders.includes('Cache-Control')
143
+ ) {
144
+ headers['Cache-Control'] = options.cacheControl
145
+ }
146
+ }
119
147
 
148
+ export default httpCorsMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-cors",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "CORS (Cross-Origin Resource Sharing) middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -58,11 +58,11 @@
58
58
  "type": "github",
59
59
  "url": "https://github.com/sponsors/willfarrell"
60
60
  },
61
- "gitHead": "bbdaf5843914921804ba085dd58117273febe6b5",
61
+ "gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0",
62
62
  "dependencies": {
63
- "@middy/util": "5.1.0"
63
+ "@middy/util": "5.2.0"
64
64
  },
65
65
  "devDependencies": {
66
- "@middy/core": "5.1.0"
66
+ "@middy/core": "5.2.0"
67
67
  }
68
68
  }