@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.
- package/CHANGELOG.md +632 -293
- package/LICENSE +20 -20
- package/README.md +288 -241
- package/package.json +83 -89
- package/src/core/application.js +1070 -956
- package/src/core/context.js +1100 -830
- package/src/core/middleware.js +305 -217
- package/src/index.js +183 -278
- package/src/middleware/body-parser.js +484 -526
- package/src/middleware/cache.js +179 -170
- package/src/middleware/compression.js +179 -182
- package/src/middleware/cors.js +178 -95
- package/src/middleware/health.js +221 -218
- package/src/middleware/logger.js +197 -194
- package/src/middleware/metrics.js +341 -327
- package/src/middleware/rate-limit.js +369 -319
- package/src/middleware/security.js +444 -332
- package/src/middleware/session.js +649 -529
- package/src/middleware/static.js +406 -299
- package/src/router/layer.js +199 -117
- package/src/router/route.js +151 -123
- package/src/router/router.js +705 -755
- package/src/utils/async-handler.js +147 -144
- package/src/utils/context-pool.js +85 -82
- package/src/utils/regex-validator.js +189 -176
- package/src/utils/safe-json.js +110 -0
- package/types/index.d.ts +536 -291
- package/dist/LICENSE +0 -21
- package/dist/README.md +0 -242
- package/dist/core/application.js +0 -957
- package/dist/core/context.js +0 -831
- package/dist/core/middleware.js +0 -218
- package/dist/core/request.js +0 -333
- package/dist/core/response.js +0 -375
- package/dist/index.js +0 -278
- package/dist/middleware/body-parser.js +0 -526
- package/dist/middleware/cache.js +0 -171
- package/dist/middleware/compression.js +0 -183
- package/dist/middleware/cors.js +0 -95
- package/dist/middleware/health.js +0 -219
- package/dist/middleware/index.js +0 -21
- package/dist/middleware/logger.js +0 -195
- package/dist/middleware/metrics.js +0 -328
- package/dist/middleware/rate-limit.js +0 -320
- package/dist/middleware/security.js +0 -333
- package/dist/middleware/session.js +0 -530
- package/dist/middleware/static.js +0 -299
- package/dist/package.json +0 -62
- package/dist/router/layer.js +0 -117
- package/dist/router/route.js +0 -123
- package/dist/router/router.js +0 -756
- package/dist/types/index.d.ts +0 -291
- package/dist/utils/async-handler.js +0 -145
- package/dist/utils/cache.js +0 -441
- package/dist/utils/context-pool.js +0 -83
- package/dist/utils/error-types.js +0 -132
- package/dist/utils/http.js +0 -413
- package/dist/utils/regex-validator.js +0 -177
- package/src/core/request.js +0 -333
- package/src/core/response.js +0 -375
- package/src/middleware/index.js +0 -21
- package/src/utils/cache.js +0 -441
- package/src/utils/error-types.js +0 -132
- package/src/utils/http.js +0 -413
|
@@ -1,183 +1,180 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (
|
|
41
|
-
return originalSend.call(this, body);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return originalSend.call(this, body);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
case '
|
|
151
|
-
return await
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function brotliCompression(options) {
|
|
174
|
-
return compression({ ...options,
|
|
175
|
-
|
|
176
|
-
|
|
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;
|
package/src/middleware/cors.js
CHANGED
|
@@ -1,95 +1,178 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (origin === true) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
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;
|