@oxog/spark 1.1.0 → 1.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 (64) hide show
  1. package/CHANGELOG.md +632 -293
  2. package/LICENSE +20 -20
  3. package/README.md +288 -241
  4. package/package.json +83 -89
  5. package/src/core/application.js +1070 -956
  6. package/src/core/context.js +1100 -830
  7. package/src/core/middleware.js +305 -217
  8. package/src/index.js +183 -278
  9. package/src/middleware/body-parser.js +484 -526
  10. package/src/middleware/cache.js +179 -170
  11. package/src/middleware/compression.js +179 -182
  12. package/src/middleware/cors.js +178 -95
  13. package/src/middleware/health.js +221 -218
  14. package/src/middleware/logger.js +197 -194
  15. package/src/middleware/metrics.js +341 -327
  16. package/src/middleware/rate-limit.js +369 -319
  17. package/src/middleware/security.js +444 -332
  18. package/src/middleware/session.js +649 -529
  19. package/src/middleware/static.js +406 -299
  20. package/src/router/layer.js +199 -117
  21. package/src/router/route.js +151 -123
  22. package/src/router/router.js +705 -755
  23. package/src/utils/async-handler.js +147 -144
  24. package/src/utils/context-pool.js +85 -82
  25. package/src/utils/regex-validator.js +189 -176
  26. package/src/utils/safe-json.js +110 -0
  27. package/types/index.d.ts +536 -291
  28. package/dist/LICENSE +0 -21
  29. package/dist/README.md +0 -242
  30. package/dist/core/application.js +0 -957
  31. package/dist/core/context.js +0 -831
  32. package/dist/core/middleware.js +0 -218
  33. package/dist/core/request.js +0 -333
  34. package/dist/core/response.js +0 -375
  35. package/dist/index.js +0 -278
  36. package/dist/middleware/body-parser.js +0 -526
  37. package/dist/middleware/cache.js +0 -171
  38. package/dist/middleware/compression.js +0 -183
  39. package/dist/middleware/cors.js +0 -95
  40. package/dist/middleware/health.js +0 -219
  41. package/dist/middleware/index.js +0 -21
  42. package/dist/middleware/logger.js +0 -195
  43. package/dist/middleware/metrics.js +0 -328
  44. package/dist/middleware/rate-limit.js +0 -320
  45. package/dist/middleware/security.js +0 -333
  46. package/dist/middleware/session.js +0 -530
  47. package/dist/middleware/static.js +0 -299
  48. package/dist/package.json +0 -62
  49. package/dist/router/layer.js +0 -117
  50. package/dist/router/route.js +0 -123
  51. package/dist/router/router.js +0 -756
  52. package/dist/types/index.d.ts +0 -291
  53. package/dist/utils/async-handler.js +0 -145
  54. package/dist/utils/cache.js +0 -441
  55. package/dist/utils/context-pool.js +0 -83
  56. package/dist/utils/error-types.js +0 -132
  57. package/dist/utils/http.js +0 -413
  58. package/dist/utils/regex-validator.js +0 -177
  59. package/src/core/request.js +0 -333
  60. package/src/core/response.js +0 -375
  61. package/src/middleware/index.js +0 -21
  62. package/src/utils/cache.js +0 -441
  63. package/src/utils/error-types.js +0 -132
  64. package/src/utils/http.js +0 -413
@@ -1,183 +1,180 @@
1
- const zlib = require('zlib');
2
- const { promisify } = require('util');
3
-
4
- const gzip = promisify(zlib.gzip);
5
- const deflate = promisify(zlib.deflate);
6
- const brotliCompress = promisify(zlib.brotliCompress);
7
-
8
- const DEFAULT_THRESHOLD = 1024;
9
- const DEFAULT_LEVEL = 6;
10
-
11
- function compression(options = {}) {
12
- const opts = {
13
- threshold: options.threshold || DEFAULT_THRESHOLD,
14
- level: options.level || DEFAULT_LEVEL,
15
- chunkSize: options.chunkSize || 16 * 1024,
16
- windowBits: options.windowBits || 15,
17
- memLevel: options.memLevel || 8,
18
- strategy: options.strategy || zlib.constants.Z_DEFAULT_STRATEGY,
19
- filter: options.filter || shouldCompress,
20
- ...options
21
- };
22
-
23
- return async (ctx, next) => {
24
- const originalSend = ctx.send;
25
-
26
- ctx.send = function(body) {
27
- return compressResponse.call(this, body, opts, originalSend);
28
- };
29
-
30
- await next();
31
- };
32
- }
33
-
34
- async function compressResponse(body, opts, originalSend) {
35
- if (this.headersSent || this.responded) {
36
- return originalSend.call(this, body);
37
- }
38
-
39
- const shouldCompress = opts.filter(this, body);
40
- if (!shouldCompress) {
41
- return originalSend.call(this, body);
42
- }
43
-
44
- const acceptEncoding = this.get('accept-encoding') || '';
45
- const encoding = getPreferredEncoding(acceptEncoding);
46
-
47
- if (!encoding) {
48
- return originalSend.call(this, body);
49
- }
50
-
51
- const bodyBuffer = Buffer.isBuffer(body) ? body : Buffer.from(String(body));
52
-
53
- if (bodyBuffer.length < opts.threshold) {
54
- return originalSend.call(this, body);
55
- }
56
-
57
- try {
58
- const compressed = await compressBuffer(bodyBuffer, encoding, opts);
59
-
60
- this.set('Content-Encoding', encoding);
61
- this.set('Content-Length', compressed.length);
62
- this.removeHeader('Content-Length');
63
- this.set('Vary', 'Accept-Encoding');
64
-
65
- return originalSend.call(this, compressed);
66
- } catch (error) {
67
- return originalSend.call(this, body);
68
- }
69
- }
70
-
71
- function shouldCompress(ctx, body) {
72
- const contentType = ctx.get('content-type');
73
-
74
- if (!contentType) {
75
- return false;
76
- }
77
-
78
- const compressibleTypes = [
79
- 'text/',
80
- 'application/json',
81
- 'application/javascript',
82
- 'application/xml',
83
- 'application/rss+xml',
84
- 'application/atom+xml',
85
- 'image/svg+xml'
86
- ];
87
-
88
- return compressibleTypes.some(type => contentType.includes(type));
89
- }
90
-
91
- function getPreferredEncoding(acceptEncoding) {
92
- const encodings = parseAcceptEncoding(acceptEncoding);
93
-
94
- if (encodings.br && encodings.br.q > 0) {
95
- return 'br';
96
- }
97
-
98
- if (encodings.gzip && encodings.gzip.q > 0) {
99
- return 'gzip';
100
- }
101
-
102
- if (encodings.deflate && encodings.deflate.q > 0) {
103
- return 'deflate';
104
- }
105
-
106
- return null;
107
- }
108
-
109
- function parseAcceptEncoding(acceptEncoding) {
110
- const encodings = {};
111
-
112
- if (!acceptEncoding) {
113
- return encodings;
114
- }
115
-
116
- acceptEncoding.split(',').forEach(encoding => {
117
- const parts = encoding.trim().split(';');
118
- const name = parts[0].trim();
119
-
120
- let q = 1;
121
- if (parts[1]) {
122
- const qMatch = parts[1].match(/q=([0-9.]+)/);
123
- if (qMatch) {
124
- q = parseFloat(qMatch[1]);
125
- }
126
- }
127
-
128
- encodings[name] = { q };
129
- });
130
-
131
- return encodings;
132
- }
133
-
134
- async function compressBuffer(buffer, encoding, opts) {
135
- const options = {
136
- level: opts.level,
137
- chunkSize: opts.chunkSize,
138
- windowBits: opts.windowBits,
139
- memLevel: opts.memLevel,
140
- strategy: opts.strategy
141
- };
142
-
143
- switch (encoding) {
144
- case 'br':
145
- return await brotliCompress(buffer, {
146
- params: {
147
- [zlib.constants.BROTLI_PARAM_QUALITY]: opts.level
148
- }
149
- });
150
- case 'gzip':
151
- return await gzip(buffer, options);
152
- case 'deflate':
153
- return await deflate(buffer, options);
154
- default:
155
- throw new Error(`Unsupported encoding: ${encoding}`);
156
- }
157
- }
158
-
159
- function gzipCompression(options) {
160
- return compression({ ...options, filter: (ctx, body) => {
161
- const acceptEncoding = ctx.get('accept-encoding') || '';
162
- return acceptEncoding.includes('gzip') && shouldCompress(ctx, body);
163
- }});
164
- }
165
-
166
- function deflateCompression(options) {
167
- return compression({ ...options, filter: (ctx, body) => {
168
- const acceptEncoding = ctx.get('accept-encoding') || '';
169
- return acceptEncoding.includes('deflate') && shouldCompress(ctx, body);
170
- }});
171
- }
172
-
173
- function brotliCompression(options) {
174
- return compression({ ...options, filter: (ctx, body) => {
175
- const acceptEncoding = ctx.get('accept-encoding') || '';
176
- return acceptEncoding.includes('br') && shouldCompress(ctx, body);
177
- }});
178
- }
179
-
180
- module.exports = compression;
181
- module.exports.gzip = gzipCompression;
182
- module.exports.deflate = deflateCompression;
1
+ 'use strict';
2
+
3
+ const zlib = require('zlib');
4
+ const { appendVary } = require('./cors');
5
+ const { promisify } = require('util');
6
+
7
+ const gzip = promisify(zlib.gzip);
8
+ const deflate = promisify(zlib.deflate);
9
+ const brotliCompress = promisify(zlib.brotliCompress);
10
+
11
+ const DEFAULT_THRESHOLD = 1024;
12
+ const DEFAULT_LEVEL = 6;
13
+
14
+ function compression(options = {}) {
15
+ const opts = {
16
+ ...options,
17
+ threshold: options.threshold !== undefined ? options.threshold : DEFAULT_THRESHOLD,
18
+ level: options.level || DEFAULT_LEVEL,
19
+ chunkSize: options.chunkSize || 16 * 1024,
20
+ windowBits: options.windowBits || 15,
21
+ memLevel: options.memLevel || 8,
22
+ strategy: options.strategy || zlib.constants.Z_DEFAULT_STRATEGY,
23
+ filter: options.filter || shouldCompress,
24
+ // Encodings this middleware may produce, in order of preference
25
+ encodings: options.encodings || (options.algorithm ? [options.algorithm] : ['br', 'gzip', 'deflate'])
26
+ };
27
+
28
+ return async (ctx, next) => {
29
+ const originalSend = ctx.send;
30
+
31
+ ctx.send = function(body) {
32
+ return compressResponse.call(this, body, opts, originalSend);
33
+ };
34
+
35
+ await next();
36
+ };
37
+ }
38
+
39
+ async function compressResponse(body, opts, originalSend) {
40
+ if (this.headersSent || this.responded) {
41
+ return originalSend.call(this, body);
42
+ }
43
+
44
+ if (body === null || body === undefined || this.getHeader('content-encoding') ||
45
+ this.method === 'HEAD' || this.statusCode === 204 || this.statusCode === 304) {
46
+ return originalSend.call(this, body);
47
+ }
48
+
49
+ const shouldCompress = opts.filter(this, body);
50
+ if (!shouldCompress) {
51
+ return originalSend.call(this, body);
52
+ }
53
+
54
+ // The response depends on Accept-Encoding whether or not it is compressed
55
+ appendVary(this, 'Accept-Encoding');
56
+
57
+ const acceptEncoding = this.get('accept-encoding') || '';
58
+ const encoding = getPreferredEncoding(acceptEncoding, opts.encodings);
59
+
60
+ if (!encoding) {
61
+ return originalSend.call(this, body);
62
+ }
63
+
64
+ const bodyBuffer = Buffer.isBuffer(body) ? body : Buffer.from(String(body));
65
+
66
+ if (bodyBuffer.length < opts.threshold) {
67
+ return originalSend.call(this, body);
68
+ }
69
+
70
+ try {
71
+ const compressed = await compressBuffer(bodyBuffer, encoding, opts);
72
+
73
+ this.set('Content-Encoding', encoding);
74
+ this.set('Content-Length', compressed.length);
75
+
76
+ return originalSend.call(this, compressed);
77
+ } catch (error) {
78
+ return originalSend.call(this, body);
79
+ }
80
+ }
81
+
82
+ function shouldCompress(ctx, body) {
83
+ // The response Content-Type (not the request's) decides compressibility
84
+ const contentType = ctx.getHeader('content-type');
85
+
86
+ if (!contentType) {
87
+ return false;
88
+ }
89
+
90
+ const compressibleTypes = [
91
+ 'text/',
92
+ 'application/json',
93
+ 'application/javascript',
94
+ 'application/xml',
95
+ 'application/rss+xml',
96
+ 'application/atom+xml',
97
+ 'image/svg+xml'
98
+ ];
99
+
100
+ return compressibleTypes.some(type => contentType.includes(type));
101
+ }
102
+
103
+ function getPreferredEncoding(acceptEncoding, allowed = ['br', 'gzip', 'deflate']) {
104
+ const encodings = parseAcceptEncoding(acceptEncoding);
105
+
106
+ for (const name of allowed) {
107
+ if (encodings[name] && encodings[name].q > 0) {
108
+ return name;
109
+ }
110
+ }
111
+
112
+ return null;
113
+ }
114
+
115
+ function parseAcceptEncoding(acceptEncoding) {
116
+ const encodings = {};
117
+
118
+ if (!acceptEncoding) {
119
+ return encodings;
120
+ }
121
+
122
+ acceptEncoding.split(',').forEach(encoding => {
123
+ const parts = encoding.trim().split(';');
124
+ const name = parts[0].trim();
125
+
126
+ let q = 1;
127
+ if (parts[1]) {
128
+ const qMatch = parts[1].match(/q=([0-9.]+)/);
129
+ if (qMatch) {
130
+ q = parseFloat(qMatch[1]);
131
+ }
132
+ }
133
+
134
+ encodings[name] = { q };
135
+ });
136
+
137
+ return encodings;
138
+ }
139
+
140
+ async function compressBuffer(buffer, encoding, opts) {
141
+ const options = {
142
+ level: opts.level,
143
+ chunkSize: opts.chunkSize,
144
+ windowBits: opts.windowBits,
145
+ memLevel: opts.memLevel,
146
+ strategy: opts.strategy
147
+ };
148
+
149
+ switch (encoding) {
150
+ case 'br':
151
+ return await brotliCompress(buffer, {
152
+ params: {
153
+ [zlib.constants.BROTLI_PARAM_QUALITY]: opts.level
154
+ }
155
+ });
156
+ case 'gzip':
157
+ return await gzip(buffer, options);
158
+ case 'deflate':
159
+ return await deflate(buffer, options);
160
+ default:
161
+ throw new Error(`Unsupported encoding: ${encoding}`);
162
+ }
163
+ }
164
+
165
+ function gzipCompression(options) {
166
+ return compression({ ...options, encodings: ['gzip'] });
167
+ }
168
+
169
+ function deflateCompression(options) {
170
+ return compression({ ...options, encodings: ['deflate'] });
171
+ }
172
+
173
+ function brotliCompression(options) {
174
+ return compression({ ...options, encodings: ['br'] });
175
+ }
176
+
177
+ module.exports = compression;
178
+ module.exports.gzip = gzipCompression;
179
+ module.exports.deflate = deflateCompression;
183
180
  module.exports.brotli = brotliCompression;
@@ -1,95 +1,178 @@
1
- const DEFAULT_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH'];
2
-
3
- function cors(options = {}) {
4
- const opts = {
5
- origin: options.origin !== undefined ? options.origin : false, // Secure default: CORS disabled
6
- methods: options.methods || DEFAULT_METHODS,
7
- allowedHeaders: options.allowedHeaders || ['Content-Type', 'Authorization'],
8
- exposedHeaders: options.exposedHeaders || [],
9
- credentials: options.credentials || false,
10
- maxAge: options.maxAge || 86400,
11
- preflightContinue: options.preflightContinue || false,
12
- optionsSuccessStatus: options.optionsSuccessStatus || 204,
13
- ...options
14
- };
15
-
16
- return async (ctx, next) => {
17
- const origin = getOrigin(ctx, opts.origin);
18
-
19
- if (origin) {
20
- ctx.set('Access-Control-Allow-Origin', origin);
21
- }
22
-
23
- if (opts.credentials) {
24
- ctx.set('Access-Control-Allow-Credentials', 'true');
25
- }
26
-
27
- if (opts.exposedHeaders && opts.exposedHeaders.length > 0) {
28
- ctx.set('Access-Control-Expose-Headers', opts.exposedHeaders.join(', '));
29
- }
30
-
31
- if (ctx.method === 'OPTIONS') {
32
- return handlePreflight(ctx, opts);
33
- }
34
-
35
- await next();
36
- };
37
- }
38
-
39
- function getOrigin(ctx, origin) {
40
- if (origin === false) {
41
- return null; // CORS disabled
42
- }
43
-
44
- if (origin === '*') {
45
- return '*';
46
- }
47
-
48
- if (origin === true) {
49
- // Reflect the request origin
50
- return ctx.get('origin') || '*';
51
- }
52
-
53
- if (typeof origin === 'string') {
54
- return origin;
55
- }
56
-
57
- if (typeof origin === 'function') {
58
- return origin(ctx);
59
- }
60
-
61
- if (Array.isArray(origin)) {
62
- const requestOrigin = ctx.get('origin');
63
- return origin.includes(requestOrigin) ? requestOrigin : null;
64
- }
65
-
66
- return null;
67
- }
68
-
69
- function handlePreflight(ctx, opts) {
70
- ctx.set('Access-Control-Allow-Methods', opts.methods.join(', '));
71
-
72
- const requestedHeaders = ctx.get('Access-Control-Request-Headers');
73
- if (requestedHeaders) {
74
- const allowedHeaders = typeof opts.allowedHeaders === 'function'
75
- ? opts.allowedHeaders(ctx)
76
- : opts.allowedHeaders;
77
-
78
- if (allowedHeaders) {
79
- ctx.set('Access-Control-Allow-Headers',
80
- Array.isArray(allowedHeaders) ? allowedHeaders.join(', ') : allowedHeaders);
81
- }
82
- }
83
-
84
- if (opts.maxAge) {
85
- ctx.set('Access-Control-Max-Age', opts.maxAge);
86
- }
87
-
88
- if (opts.preflightContinue) {
89
- return;
90
- }
91
-
92
- ctx.status(opts.optionsSuccessStatus).end();
93
- }
94
-
95
- module.exports = cors;
1
+ 'use strict';
2
+
3
+ /**
4
+ * @fileoverview CORS middleware for Spark Framework
5
+ * @since 1.0.0
6
+ */
7
+
8
+ const DEFAULT_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH'];
9
+
10
+ /**
11
+ * Create a CORS middleware.
12
+ *
13
+ * `origin` may be `false` (default: CORS disabled), `'*'`, `true` (reflect any
14
+ * origin; public APIs only), a fixed origin string, an array of allowed
15
+ * origins (strings or RegExps), or a function `(ctx) => string | boolean`.
16
+ * Responses whose `Access-Control-Allow-Origin` depends on the request carry
17
+ * `Vary: Origin` so shared caches do not serve them to other origins.
18
+ * `'*'` or `true` combined with `credentials: true` is rejected when the
19
+ * middleware is created.
20
+ *
21
+ * Only real preflight requests (OPTIONS with `Origin` and
22
+ * `Access-Control-Request-Method`) are answered by the middleware; other
23
+ * OPTIONS requests continue to the application.
24
+ *
25
+ * @param {Object} [options={}] - CORS options
26
+ * @returns {Function} Middleware
27
+ */
28
+ function cors(options = {}) {
29
+ const opts = {
30
+ ...options,
31
+ origin: options.origin !== undefined ? options.origin : false, // Secure default: CORS disabled
32
+ methods: options.methods || DEFAULT_METHODS,
33
+ allowedHeaders: options.allowedHeaders || ['Content-Type', 'Authorization'],
34
+ exposedHeaders: options.exposedHeaders || [],
35
+ credentials: options.credentials || false,
36
+ maxAge: options.maxAge || 86400,
37
+ preflightContinue: options.preflightContinue || false,
38
+ optionsSuccessStatus: options.optionsSuccessStatus || 204
39
+ };
40
+
41
+ if (opts.credentials && (opts.origin === '*' || opts.origin === true)) {
42
+ throw new Error(
43
+ `CORS origin ${opts.origin === '*' ? '"*"' : 'true (reflect all)'} cannot be used with credentials enabled. ` +
44
+ 'Use a whitelist instead.'
45
+ );
46
+ }
47
+
48
+ if (opts.origin === true) {
49
+ console.warn('[SECURITY WARNING] CORS origin: true reflects all origins. ' +
50
+ 'This should only be used for public APIs. Use a whitelist array or function for better security.');
51
+ }
52
+
53
+ const dynamicOrigin = opts.origin !== false && opts.origin !== '*' && typeof opts.origin !== 'string';
54
+
55
+ return async (ctx, next) => {
56
+ if (dynamicOrigin) {
57
+ appendVary(ctx, 'Origin');
58
+ }
59
+
60
+ const origin = await getOrigin(ctx, opts);
61
+ if (origin) {
62
+ ctx.set('Access-Control-Allow-Origin', origin);
63
+
64
+ if (opts.credentials) {
65
+ ctx.set('Access-Control-Allow-Credentials', 'true');
66
+ }
67
+
68
+ if (opts.exposedHeaders && opts.exposedHeaders.length > 0) {
69
+ ctx.set('Access-Control-Expose-Headers', opts.exposedHeaders.join(', '));
70
+ }
71
+ }
72
+
73
+ const isPreflight = ctx.method === 'OPTIONS' &&
74
+ ctx.get('origin') !== undefined &&
75
+ ctx.get('access-control-request-method') !== undefined;
76
+
77
+ if (isPreflight) {
78
+ handlePreflight(ctx, opts);
79
+ if (!opts.preflightContinue) {
80
+ return undefined;
81
+ }
82
+ }
83
+
84
+ return next();
85
+ };
86
+ }
87
+
88
+ /**
89
+ * Resolve the Access-Control-Allow-Origin value for a request.
90
+ *
91
+ * @param {Context} ctx - Request context
92
+ * @param {Object} opts - CORS options
93
+ * @returns {Promise<string|null>} Origin to allow, or null
94
+ * @private
95
+ */
96
+ async function getOrigin(ctx, opts) {
97
+ const origin = opts.origin;
98
+ const requestOrigin = ctx.get('origin');
99
+
100
+ if (origin === false) {
101
+ return null; // CORS disabled
102
+ }
103
+
104
+ if (origin === '*') {
105
+ return '*';
106
+ }
107
+
108
+ if (origin === true) {
109
+ return requestOrigin || '*';
110
+ }
111
+
112
+ if (typeof origin === 'string') {
113
+ return origin;
114
+ }
115
+
116
+ if (typeof origin === 'function') {
117
+ const result = await origin(ctx);
118
+ if (result === true) {
119
+ return requestOrigin || null;
120
+ }
121
+ return typeof result === 'string' && result ? result : null;
122
+ }
123
+
124
+ if (Array.isArray(origin) && requestOrigin) {
125
+ const allowed = origin.some(entry =>
126
+ entry instanceof RegExp ? entry.test(requestOrigin) : entry === requestOrigin
127
+ );
128
+ return allowed ? requestOrigin : null;
129
+ }
130
+
131
+ return null;
132
+ }
133
+
134
+ /**
135
+ * Add a token to the Vary response header.
136
+ *
137
+ * @param {Context} ctx - Request context
138
+ * @param {string} field - Header name
139
+ * @private
140
+ */
141
+ function appendVary(ctx, field) {
142
+ const current = ctx.getHeader('vary');
143
+ if (!current) {
144
+ ctx.set('Vary', field);
145
+ return;
146
+ }
147
+ const fields = String(current).split(',').map(f => f.trim().toLowerCase());
148
+ if (!fields.includes(field.toLowerCase()) && !fields.includes('*')) {
149
+ ctx.set('Vary', `${current}, ${field}`);
150
+ }
151
+ }
152
+
153
+ function handlePreflight(ctx, opts) {
154
+ ctx.set('Access-Control-Allow-Methods', opts.methods.join(', '));
155
+
156
+ const requestedHeaders = ctx.get('access-control-request-headers');
157
+ if (requestedHeaders) {
158
+ const allowedHeaders = typeof opts.allowedHeaders === 'function'
159
+ ? opts.allowedHeaders(ctx)
160
+ : opts.allowedHeaders;
161
+
162
+ if (allowedHeaders) {
163
+ ctx.set('Access-Control-Allow-Headers',
164
+ Array.isArray(allowedHeaders) ? allowedHeaders.join(', ') : allowedHeaders);
165
+ }
166
+ }
167
+
168
+ if (opts.maxAge) {
169
+ ctx.set('Access-Control-Max-Age', opts.maxAge);
170
+ }
171
+
172
+ if (!opts.preflightContinue) {
173
+ ctx.status(opts.optionsSuccessStatus).end();
174
+ }
175
+ }
176
+
177
+ module.exports = cors;
178
+ module.exports.appendVary = appendVary;