1 | 'use strict';
|
2 | const _ = require('lodash');
|
3 | const HeaderDate = 'Date';
|
4 | const RequestID = 'X-Fc-Request-Id';
|
5 | const CORSMaxAgeSeconds = '3600';
|
6 |
|
7 | const InvocationError = 'x-fc-error-type';
|
8 |
|
9 | const InvocationLogResult = 'x-fc-log-result';
|
10 |
|
11 | const MaxMemoryUsage = 'x-fc-max-memory-usage';
|
12 |
|
13 | const InvocationDuration = 'x-fc-invocation-duration';
|
14 |
|
15 | const InvocationCodeChecksum = 'x-fc-code-checksum';
|
16 |
|
17 | const InvocationCodeVersion = 'x-fc-invocation-code-version';
|
18 | const exposedHeaders = [HeaderDate, RequestID, InvocationError, InvocationCodeChecksum, InvocationDuration, MaxMemoryUsage, InvocationLogResult, InvocationCodeVersion];
|
19 | const CORSExposedHeaders = _.join(exposedHeaders, ',');
|
20 | function setCORSHeaders(req, res, next) {
|
21 | const origin = req.headers.origin;
|
22 | if (origin) {
|
23 | res.header('Access-Control-Allow-Origin', origin);
|
24 | }
|
25 | if (req.headers['access-control-request-method']) {
|
26 | res.header('Access-Control-Allow-Methods', req.headers['access-control-request-method']);
|
27 | }
|
28 | if (req.headers['access-control-request-headers']) {
|
29 | res.header('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
|
30 | }
|
31 | res.header('Access-Control-Expose-Headers', CORSExposedHeaders);
|
32 | if (_.toLower(req.method) === 'options') {
|
33 | res.header('Access-Control-Max-Age', CORSMaxAgeSeconds);
|
34 |
|
35 | res.sendStatus(200);
|
36 | }
|
37 | else {
|
38 | return next();
|
39 | }
|
40 | }
|
41 | module.exports = { setCORSHeaders };
|