@oxog/spark 1.1.1 → 1.3.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 +656 -315
  2. package/LICENSE +20 -20
  3. package/README.md +288 -241
  4. package/package.json +90 -89
  5. package/src/core/application.js +1068 -956
  6. package/src/core/context.js +1098 -830
  7. package/src/core/middleware.js +303 -217
  8. package/src/index.js +183 -280
  9. package/src/middleware/body-parser.js +482 -526
  10. package/src/middleware/cache.js +177 -170
  11. package/src/middleware/compression.js +177 -182
  12. package/src/middleware/cors.js +176 -95
  13. package/src/middleware/health.js +219 -218
  14. package/src/middleware/logger.js +195 -194
  15. package/src/middleware/metrics.js +339 -327
  16. package/src/middleware/rate-limit.js +367 -319
  17. package/src/middleware/security.js +442 -332
  18. package/src/middleware/session.js +647 -529
  19. package/src/middleware/static.js +404 -299
  20. package/src/router/layer.js +197 -117
  21. package/src/router/route.js +149 -123
  22. package/src/router/router.js +705 -755
  23. package/src/utils/async-handler.js +145 -147
  24. package/src/utils/context-pool.js +83 -82
  25. package/src/utils/regex-validator.js +187 -163
  26. package/src/utils/safe-json.js +110 -0
  27. package/types/index.d.ts +538 -291
  28. package/dist/LICENSE +0 -21
  29. package/dist/README.md +0 -242
  30. package/dist/core/application.js +0 -959
  31. package/dist/core/context.js +0 -833
  32. package/dist/core/middleware.js +0 -220
  33. package/dist/core/request.js +0 -335
  34. package/dist/core/response.js +0 -377
  35. package/dist/index.js +0 -280
  36. package/dist/middleware/body-parser.js +0 -528
  37. package/dist/middleware/cache.js +0 -173
  38. package/dist/middleware/compression.js +0 -185
  39. package/dist/middleware/cors.js +0 -97
  40. package/dist/middleware/health.js +0 -221
  41. package/dist/middleware/index.js +0 -23
  42. package/dist/middleware/logger.js +0 -197
  43. package/dist/middleware/metrics.js +0 -330
  44. package/dist/middleware/rate-limit.js +0 -322
  45. package/dist/middleware/security.js +0 -335
  46. package/dist/middleware/session.js +0 -532
  47. package/dist/middleware/static.js +0 -301
  48. package/dist/package.json +0 -62
  49. package/dist/router/layer.js +0 -119
  50. package/dist/router/route.js +0 -125
  51. package/dist/router/router.js +0 -758
  52. package/dist/types/index.d.ts +0 -291
  53. package/dist/utils/async-handler.js +0 -150
  54. package/dist/utils/cache.js +0 -443
  55. package/dist/utils/context-pool.js +0 -85
  56. package/dist/utils/error-types.js +0 -134
  57. package/dist/utils/http.js +0 -415
  58. package/dist/utils/regex-validator.js +0 -166
  59. package/src/core/request.js +0 -335
  60. package/src/core/response.js +0 -377
  61. package/src/middleware/index.js +0 -23
  62. package/src/utils/cache.js +0 -443
  63. package/src/utils/error-types.js +0 -134
  64. package/src/utils/http.js +0 -415
@@ -1,185 +1,180 @@
1
1
  'use strict';
2
2
 
3
- const zlib = require('zlib');
4
- const { promisify } = require('util');
5
-
6
- const gzip = promisify(zlib.gzip);
7
- const deflate = promisify(zlib.deflate);
8
- const brotliCompress = promisify(zlib.brotliCompress);
9
-
10
- const DEFAULT_THRESHOLD = 1024;
11
- const DEFAULT_LEVEL = 6;
12
-
13
- function compression(options = {}) {
14
- const opts = {
15
- threshold: options.threshold || DEFAULT_THRESHOLD,
16
- level: options.level || DEFAULT_LEVEL,
17
- chunkSize: options.chunkSize || 16 * 1024,
18
- windowBits: options.windowBits || 15,
19
- memLevel: options.memLevel || 8,
20
- strategy: options.strategy || zlib.constants.Z_DEFAULT_STRATEGY,
21
- filter: options.filter || shouldCompress,
22
- ...options
23
- };
24
-
25
- return async (ctx, next) => {
26
- const originalSend = ctx.send;
27
-
28
- ctx.send = function(body) {
29
- return compressResponse.call(this, body, opts, originalSend);
30
- };
31
-
32
- await next();
33
- };
34
- }
35
-
36
- async function compressResponse(body, opts, originalSend) {
37
- if (this.headersSent || this.responded) {
38
- return originalSend.call(this, body);
39
- }
40
-
41
- const shouldCompress = opts.filter(this, body);
42
- if (!shouldCompress) {
43
- return originalSend.call(this, body);
44
- }
45
-
46
- const acceptEncoding = this.get('accept-encoding') || '';
47
- const encoding = getPreferredEncoding(acceptEncoding);
48
-
49
- if (!encoding) {
50
- return originalSend.call(this, body);
51
- }
52
-
53
- const bodyBuffer = Buffer.isBuffer(body) ? body : Buffer.from(String(body));
54
-
55
- if (bodyBuffer.length < opts.threshold) {
56
- return originalSend.call(this, body);
57
- }
58
-
59
- try {
60
- const compressed = await compressBuffer(bodyBuffer, encoding, opts);
61
-
62
- this.set('Content-Encoding', encoding);
63
- this.set('Content-Length', compressed.length);
64
- this.removeHeader('Content-Length');
65
- this.set('Vary', 'Accept-Encoding');
66
-
67
- return originalSend.call(this, compressed);
68
- } catch (error) {
69
- return originalSend.call(this, body);
70
- }
71
- }
72
-
73
- function shouldCompress(ctx, body) {
74
- const contentType = ctx.get('content-type');
75
-
76
- if (!contentType) {
77
- return false;
78
- }
79
-
80
- const compressibleTypes = [
81
- 'text/',
82
- 'application/json',
83
- 'application/javascript',
84
- 'application/xml',
85
- 'application/rss+xml',
86
- 'application/atom+xml',
87
- 'image/svg+xml'
88
- ];
89
-
90
- return compressibleTypes.some(type => contentType.includes(type));
91
- }
92
-
93
- function getPreferredEncoding(acceptEncoding) {
94
- const encodings = parseAcceptEncoding(acceptEncoding);
95
-
96
- if (encodings.br && encodings.br.q > 0) {
97
- return 'br';
98
- }
99
-
100
- if (encodings.gzip && encodings.gzip.q > 0) {
101
- return 'gzip';
102
- }
103
-
104
- if (encodings.deflate && encodings.deflate.q > 0) {
105
- return 'deflate';
106
- }
107
-
108
- return null;
109
- }
110
-
111
- function parseAcceptEncoding(acceptEncoding) {
112
- const encodings = {};
113
-
114
- if (!acceptEncoding) {
115
- return encodings;
116
- }
117
-
118
- acceptEncoding.split(',').forEach(encoding => {
119
- const parts = encoding.trim().split(';');
120
- const name = parts[0].trim();
121
-
122
- let q = 1;
123
- if (parts[1]) {
124
- const qMatch = parts[1].match(/q=([0-9.]+)/);
125
- if (qMatch) {
126
- q = parseFloat(qMatch[1]);
127
- }
128
- }
129
-
130
- encodings[name] = { q };
131
- });
132
-
133
- return encodings;
134
- }
135
-
136
- async function compressBuffer(buffer, encoding, opts) {
137
- const options = {
138
- level: opts.level,
139
- chunkSize: opts.chunkSize,
140
- windowBits: opts.windowBits,
141
- memLevel: opts.memLevel,
142
- strategy: opts.strategy
143
- };
144
-
145
- switch (encoding) {
146
- case 'br':
147
- return await brotliCompress(buffer, {
148
- params: {
149
- [zlib.constants.BROTLI_PARAM_QUALITY]: opts.level
150
- }
151
- });
152
- case 'gzip':
153
- return await gzip(buffer, options);
154
- case 'deflate':
155
- return await deflate(buffer, options);
156
- default:
157
- throw new Error(`Unsupported encoding: ${encoding}`);
158
- }
159
- }
160
-
161
- function gzipCompression(options) {
162
- return compression({ ...options, filter: (ctx, body) => {
163
- const acceptEncoding = ctx.get('accept-encoding') || '';
164
- return acceptEncoding.includes('gzip') && shouldCompress(ctx, body);
165
- }});
166
- }
167
-
168
- function deflateCompression(options) {
169
- return compression({ ...options, filter: (ctx, body) => {
170
- const acceptEncoding = ctx.get('accept-encoding') || '';
171
- return acceptEncoding.includes('deflate') && shouldCompress(ctx, body);
172
- }});
173
- }
174
-
175
- function brotliCompression(options) {
176
- return compression({ ...options, filter: (ctx, body) => {
177
- const acceptEncoding = ctx.get('accept-encoding') || '';
178
- return acceptEncoding.includes('br') && shouldCompress(ctx, body);
179
- }});
180
- }
181
-
182
- module.exports = compression;
183
- module.exports.gzip = gzipCompression;
184
- module.exports.deflate = deflateCompression;
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;
185
180
  module.exports.brotli = brotliCompression;
@@ -1,97 +1,178 @@
1
1
  'use strict';
2
2
 
3
- const DEFAULT_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH'];
4
-
5
- function cors(options = {}) {
6
- const opts = {
7
- origin: options.origin !== undefined ? options.origin : false, // Secure default: CORS disabled
8
- methods: options.methods || DEFAULT_METHODS,
9
- allowedHeaders: options.allowedHeaders || ['Content-Type', 'Authorization'],
10
- exposedHeaders: options.exposedHeaders || [],
11
- credentials: options.credentials || false,
12
- maxAge: options.maxAge || 86400,
13
- preflightContinue: options.preflightContinue || false,
14
- optionsSuccessStatus: options.optionsSuccessStatus || 204,
15
- ...options
16
- };
17
-
18
- return async (ctx, next) => {
19
- const origin = getOrigin(ctx, opts.origin);
20
-
21
- if (origin) {
22
- ctx.set('Access-Control-Allow-Origin', origin);
23
- }
24
-
25
- if (opts.credentials) {
26
- ctx.set('Access-Control-Allow-Credentials', 'true');
27
- }
28
-
29
- if (opts.exposedHeaders && opts.exposedHeaders.length > 0) {
30
- ctx.set('Access-Control-Expose-Headers', opts.exposedHeaders.join(', '));
31
- }
32
-
33
- if (ctx.method === 'OPTIONS') {
34
- return handlePreflight(ctx, opts);
35
- }
36
-
37
- await next();
38
- };
39
- }
40
-
41
- function getOrigin(ctx, origin) {
42
- if (origin === false) {
43
- return null; // CORS disabled
44
- }
45
-
46
- if (origin === '*') {
47
- return '*';
48
- }
49
-
50
- if (origin === true) {
51
- // Reflect the request origin
52
- return ctx.get('origin') || '*';
53
- }
54
-
55
- if (typeof origin === 'string') {
56
- return origin;
57
- }
58
-
59
- if (typeof origin === 'function') {
60
- return origin(ctx);
61
- }
62
-
63
- if (Array.isArray(origin)) {
64
- const requestOrigin = ctx.get('origin');
65
- return origin.includes(requestOrigin) ? requestOrigin : null;
66
- }
67
-
68
- return null;
69
- }
70
-
71
- function handlePreflight(ctx, opts) {
72
- ctx.set('Access-Control-Allow-Methods', opts.methods.join(', '));
73
-
74
- const requestedHeaders = ctx.get('Access-Control-Request-Headers');
75
- if (requestedHeaders) {
76
- const allowedHeaders = typeof opts.allowedHeaders === 'function'
77
- ? opts.allowedHeaders(ctx)
78
- : opts.allowedHeaders;
79
-
80
- if (allowedHeaders) {
81
- ctx.set('Access-Control-Allow-Headers',
82
- Array.isArray(allowedHeaders) ? allowedHeaders.join(', ') : allowedHeaders);
83
- }
84
- }
85
-
86
- if (opts.maxAge) {
87
- ctx.set('Access-Control-Max-Age', opts.maxAge);
88
- }
89
-
90
- if (opts.preflightContinue) {
91
- return;
92
- }
93
-
94
- ctx.status(opts.optionsSuccessStatus).end();
95
- }
96
-
97
- module.exports = cors;
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;