Skip to content

Commit c4b5d21

Browse files
authoredAug 19, 2022
refactor: use friendlier promise checking (#84)
1 parent fbe33bc commit c4b5d21

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed
 

‎index.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,15 @@ module.exports = function(options) {
6262

6363
let origin;
6464
if (typeof options.origin === 'function') {
65-
origin = options.origin(ctx);
66-
if (origin instanceof Promise) origin = await origin;
65+
origin = await options.origin(ctx);
6766
if (!origin) return await next();
6867
} else {
6968
origin = options.origin || requestOrigin;
7069
}
7170

7271
let credentials;
7372
if (typeof options.credentials === 'function') {
74-
credentials = options.credentials(ctx);
75-
if (credentials instanceof Promise) credentials = await credentials;
73+
credentials = await options.credentials(ctx);
7674
} else {
7775
credentials = !!options.credentials;
7876
}

‎test/cors.test.js

+62
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,37 @@ describe('cors.test.js', function() {
201201
.expect('Access-Control-Allow-Origin', '*')
202202
.expect(200, done);
203203
});
204+
205+
it('behaves correctly when the return type is promise-like', function(done) {
206+
class WrappedPromise {
207+
constructor(...args) {
208+
this.internalPromise = new Promise(...args);
209+
}
210+
211+
then(onFulfilled) {
212+
this.internalPromise.then(onFulfilled);
213+
}
214+
}
215+
216+
const app = new Koa()
217+
.use(cors({
218+
origin() {
219+
return new WrappedPromise(resolve => {
220+
return resolve('*');
221+
});
222+
},
223+
}))
224+
.use(function(ctx) {
225+
ctx.body = { foo: 'bar' };
226+
});
227+
228+
request(app.listen())
229+
.get('/')
230+
.set('Origin', 'http://koajs.com')
231+
.expect({ foo: 'bar' })
232+
.expect('Access-Control-Allow-Origin', '*')
233+
.expect(200, done);
234+
});
204235
});
205236

206237
describe('options.exposeHeaders', function() {
@@ -449,6 +480,37 @@ describe('cors.test.js', function() {
449480
.expect('Access-Control-Allow-Credentials', 'true')
450481
.expect(204, done);
451482
});
483+
484+
it('behaves correctly when the return type is promise-like', function(done) {
485+
class WrappedPromise {
486+
constructor(...args) {
487+
this.internalPromise = new Promise(...args);
488+
}
489+
490+
then(onFulfilled) {
491+
this.internalPromise.then(onFulfilled);
492+
}
493+
}
494+
495+
const app = new Koa()
496+
.use(cors({
497+
credentials() {
498+
return new WrappedPromise(resolve => {
499+
resolve(true);
500+
});
501+
},
502+
}))
503+
.use(function(ctx) {
504+
ctx.body = { foo: 'bar' };
505+
});
506+
507+
request(app.listen())
508+
.get('/')
509+
.set('Origin', 'http://koajs.com')
510+
.expect('Access-Control-Allow-Credentials', 'true')
511+
.expect({ foo: 'bar' })
512+
.expect(200, done);
513+
});
452514
});
453515

454516
describe('options.allowHeaders', function() {

0 commit comments

Comments
 (0)
Please sign in to comment.