Skip to content

Commit

Permalink
refactor: use async function, support options.origin return promise (#59
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dead-horse authored and fengmk2 committed Mar 11, 2019
1 parent 1fb61a0 commit 369d31d
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 192 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
sudo: false
language: node_js
node_js:
- '6'
- '8'
- '10'
script: 'npm run ci'
Expand Down
25 changes: 11 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = function(options) {
options.credentials = !!options.credentials;
options.keepHeadersOnError = options.keepHeadersOnError === undefined || !!options.keepHeadersOnError;

return function cors(ctx, next) {
return async function cors(ctx, next) {
// If the Origin header is not present terminate this set of steps.
// The request is outside the scope of this specification.
const requestOrigin = ctx.get('Origin');
Expand All @@ -51,18 +51,13 @@ module.exports = function(options) {
// https://github.com/rs/cors/issues/10
ctx.vary('Origin');

if (!requestOrigin) {
return next();
}
if (!requestOrigin) return await next();

let origin;

if (typeof options.origin === 'function') {
// FIXME: origin can be promise
origin = options.origin(ctx);
if (!origin) {
return next();
}
if (origin instanceof Promise) origin = await origin;
if (!origin) return await next();
} else {
origin = options.origin || requestOrigin;
}
Expand All @@ -87,17 +82,19 @@ module.exports = function(options) {
}

if (!options.keepHeadersOnError) {
return next();
return await next();
}
return next().catch(err => {
try {
return await next();
} catch (err) {
const errHeadersSet = err.headers || {};
const varyWithOrigin = vary.append(errHeadersSet.vary || errHeadersSet.Vary || '', 'Origin');
delete errHeadersSet.Vary;

err.headers = Object.assign({}, errHeadersSet, headersSet, {vary: varyWithOrigin});
err.headers = Object.assign({}, errHeadersSet, headersSet, { vary: varyWithOrigin });

throw err;
});
}
} else {
// Preflight Request

Expand All @@ -106,7 +103,7 @@ module.exports = function(options) {
// The request is outside the scope of this specification.
if (!ctx.get('Access-Control-Request-Method')) {
// this not preflight request, ignore it
return next();
return await next();
}

ctx.set('Access-Control-Allow-Origin', origin);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
},
"devDependencies": {
"autod": "*",
"eslint": "^2.3.0",
"eslint-config-egg": "^2.0.0",
"eslint": "^5.15.1",
"eslint-config-egg": "^7.1.0",
"istanbul": "*",
"koa": "^2.5.1",
"mocha": "3",
Expand All @@ -42,7 +42,7 @@
"koajs"
],
"engines": {
"node": ">= 6.0.0"
"node": ">= 8.0.0"
},
"author": "fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)",
"license": "MIT"
Expand Down

0 comments on commit 369d31d

Please sign in to comment.