Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 43 additions & 49 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
(function () {

'use strict';

var assign = require('object-assign');
Expand All @@ -9,7 +8,7 @@
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204
optionsSuccessStatus: 204,
};

function isString(s) {
Expand All @@ -34,39 +33,29 @@
}

function configureOrigin(options, req) {
var requestOrigin = req.headers.origin,
headers = [],
isAllowed;
var requestOrigin = req.headers.origin;
var headers = [];
var isAllowed;

if (!options.origin || options.origin === '*') {
// allow any origin
headers.push([{
key: 'Access-Control-Allow-Origin',
value: '*'
}]);
headers.push([{ key: 'Access-Control-Allow-Origin', value: '*' }]);
} else if (isString(options.origin)) {
// fixed origin
headers.push([{
key: 'Access-Control-Allow-Origin',
value: options.origin
}]);
headers.push([{
key: 'Vary',
value: 'Origin'
}]);
headers.push([
{ key: 'Access-Control-Allow-Origin', value: options.origin },
]);
} else {
isAllowed = isOriginAllowed(requestOrigin, options.origin);
// reflect origin
headers.push([{
key: 'Access-Control-Allow-Origin',
value: isAllowed ? requestOrigin : false
}]);
headers.push([{
key: 'Vary',
value: 'Origin'
}]);
headers.push([
{
key: 'Access-Control-Allow-Origin',
value: isAllowed ? requestOrigin : false,
},
]);
}

// **Ensure `Vary: Origin` is always set**
headers.push([{ key: 'Vary', value: 'Origin' }]);

return headers;
}

Expand All @@ -77,15 +66,15 @@
}
return {
key: 'Access-Control-Allow-Methods',
value: methods
value: methods,
};
}

function configureCredentials(options) {
if (options.credentials === true) {
return {
key: 'Access-Control-Allow-Credentials',
value: 'true'
value: 'true',
};
}
return null;
Expand All @@ -97,18 +86,22 @@

if (!allowedHeaders) {
allowedHeaders = req.headers['access-control-request-headers']; // .headers wasn't specified, so reflect the request headers
headers.push([{
key: 'Vary',
value: 'Access-Control-Request-Headers'
}]);
headers.push([
{
key: 'Vary',
value: 'Access-Control-Request-Headers',
},
]);
} else if (allowedHeaders.join) {
allowedHeaders = allowedHeaders.join(','); // .headers is an array, so turn it into a string
}
if (allowedHeaders && allowedHeaders.length) {
headers.push([{
key: 'Access-Control-Allow-Headers',
value: allowedHeaders
}]);
headers.push([
{
key: 'Access-Control-Allow-Headers',
value: allowedHeaders,
},
]);
}

return headers;
Expand All @@ -124,18 +117,20 @@
if (headers && headers.length) {
return {
key: 'Access-Control-Expose-Headers',
value: headers
value: headers,
};
}
return null;
}

function configureMaxAge(options) {
var maxAge = (typeof options.maxAge === 'number' || options.maxAge) && options.maxAge.toString()
var maxAge =
(typeof options.maxAge === 'number' || options.maxAge) &&
options.maxAge.toString();
if (maxAge && maxAge.length) {
return {
key: 'Access-Control-Max-Age',
value: maxAge
value: maxAge,
};
}
return null;
Expand Down Expand Up @@ -163,11 +158,11 @@
if (method === 'OPTIONS') {
// preflight
headers.push(configureOrigin(options, req));
headers.push(configureCredentials(options))
headers.push(configureMethods(options))
headers.push(configureCredentials(options));
headers.push(configureMethods(options));
headers.push(configureAllowedHeaders(options, req));
headers.push(configureMaxAge(options))
headers.push(configureExposedHeaders(options))
headers.push(configureMaxAge(options));
headers.push(configureExposedHeaders(options));
applyHeaders(headers, res);

if (options.preflightContinue) {
Expand All @@ -182,8 +177,8 @@
} else {
// actual response
headers.push(configureOrigin(options, req));
headers.push(configureCredentials(options))
headers.push(configureExposedHeaders(options))
headers.push(configureCredentials(options));
headers.push(configureExposedHeaders(options));
applyHeaders(headers, res);
next();
}
Expand Down Expand Up @@ -234,5 +229,4 @@

// can pass either an options hash, an options delegate, or nothing
module.exports = middlewareWrapper;

}());
})();
Loading
Loading