Skip to content

Commit afaeb65

Browse files
committed
Fix CORS race condition
1 parent b2a0b23 commit afaeb65

File tree

6 files changed

+40
-6
lines changed

6 files changed

+40
-6
lines changed

lib/plugins/aws/package/compile/events/apiGateway/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class AwsCompileApigEvents {
2323
this.options = options;
2424
this.provider = this.serverless.getProvider('aws');
2525

26+
// used for the generated method logical ids (GET, PATCH, PUT, DELETE, OPTIONS, ...)
27+
this.apiGatewayMethodLogicalIds = [];
28+
2629
Object.assign(
2730
this,
2831
validate,

lib/plugins/aws/package/compile/events/apiGateway/index.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ describe('AwsCompileApigEvents', () => {
7575
it('should set the provider variable to be an instanceof AwsProvider', () =>
7676
expect(awsCompileApigEvents.provider).to.be.instanceof(AwsProvider));
7777

78+
it('should setup an empty array to gather the method logical ids', () =>
79+
expect(awsCompileApigEvents.apiGatewayMethodLogicalIds).to.deep.equal([]));
80+
7881
it('should run "package:compileEvents" promise chain in order', () => {
7982
const validateStub = sinon
8083
.stub(awsCompileApigEvents, 'validate').returns({

lib/plugins/aws/package/compile/events/apiGateway/lib/cors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const _ = require('lodash');
44
const BbPromise = require('bluebird');
55

66
module.exports = {
7-
87
compileCors() {
98
_.forEach(this.validated.corsPreflight, (config, path) => {
109
const resourceName = this.getResourceName(path);
@@ -41,6 +40,8 @@ module.exports = {
4140
.replace('ANY', 'DELETE,GET,HEAD,PATCH,POST,PUT');
4241
}
4342

43+
this.apiGatewayMethodLogicalIds.push(corsMethodLogicalId);
44+
4445
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
4546
[corsMethodLogicalId]: {
4647
Type: 'AWS::ApiGateway::Method',
@@ -97,5 +98,4 @@ module.exports = {
9798
},
9899
];
99100
},
100-
101101
};

lib/plugins/aws/package/compile/events/apiGateway/lib/cors.test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ describe('#compileCors()', () => {
3333
},
3434
};
3535
awsCompileApigEvents = new AwsCompileApigEvents(serverless, options);
36+
awsCompileApigEvents.apiGatewayMethodLogicalIds = [];
3637
awsCompileApigEvents.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi';
3738
awsCompileApigEvents.apiGatewayResources = {
3839
'users/create': {
3940
name: 'UsersCreate',
4041
resourceLogicalId: 'ApiGatewayResourceUsersCreate',
4142
},
42-
4343
'users/list': {
4444
name: 'UsersList',
4545
resourceLogicalId: 'ApiGatewayResourceUsersList',
@@ -252,4 +252,29 @@ describe('#compileCors()', () => {
252252
expect(() => awsCompileApigEvents.compileCors())
253253
.to.throw(Error, 'maxAge should be an integer over 0');
254254
});
255+
256+
it('should add the methods resource logical id to the array of method logical ids', () => {
257+
awsCompileApigEvents.validated.corsPreflight = {
258+
'users/create': {
259+
origins: ['*', 'http://example.com'],
260+
headers: ['*'],
261+
methods: ['OPTIONS', 'POST'],
262+
allowCredentials: true,
263+
maxAge: 86400,
264+
},
265+
'users/any': {
266+
origins: ['http://example.com'],
267+
headers: ['*'],
268+
methods: ['OPTIONS', 'ANY'],
269+
allowCredentials: false,
270+
},
271+
};
272+
return awsCompileApigEvents.compileCors().then(() => {
273+
expect(awsCompileApigEvents.apiGatewayMethodLogicalIds)
274+
.to.deep.equal([
275+
'ApiGatewayMethodUsersCreateOptions',
276+
'ApiGatewayMethodUsersAnyOptions',
277+
]);
278+
});
279+
});
255280
});

lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ const BbPromise = require('bluebird');
44
const _ = require('lodash');
55

66
module.exports = {
7-
87
compileMethods() {
9-
this.apiGatewayMethodLogicalIds = [];
108
this.permissionMapping = [];
119

1210
this.validated.events.forEach((event) => {

lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('#compileMethods()', () => {
3434
};
3535
awsCompileApigEvents = new AwsCompileApigEvents(serverless, options);
3636
awsCompileApigEvents.validated = {};
37+
awsCompileApigEvents.apiGatewayMethodLogicalIds = [];
3738
awsCompileApigEvents.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi';
3839
awsCompileApigEvents.apiGatewayResources = {
3940
'users/create': {
@@ -609,7 +610,7 @@ describe('#compileMethods()', () => {
609610
});
610611
});
611612

612-
it('should create methodLogicalIds array', () => {
613+
it('should update the method logical ids array', () => {
613614
awsCompileApigEvents.validated.events = [
614615
{
615616
functionName: 'First',
@@ -628,6 +629,10 @@ describe('#compileMethods()', () => {
628629
];
629630
return awsCompileApigEvents.compileMethods().then(() => {
630631
expect(awsCompileApigEvents.apiGatewayMethodLogicalIds.length).to.equal(2);
632+
expect(awsCompileApigEvents.apiGatewayMethodLogicalIds).to.deep.equal([
633+
'ApiGatewayMethodUsersCreatePost',
634+
'ApiGatewayMethodUsersListGet',
635+
]);
631636
});
632637
});
633638

0 commit comments

Comments
 (0)