Skip to content

Commit

Permalink
Fix for s3.createBucket mutates args (#3138)
Browse files Browse the repository at this point in the history
Fixes #3112
  • Loading branch information
ericmustin committed Mar 18, 2020
1 parent 737fc1b commit cd2fe1e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-s3-a752bb97.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "bugfix",
"category": "s3",
"description": "createBucket mutates params argument when endpoint is configured by appending CreateBucketConfigurationon key, this side effect is now fixed"
}
8 changes: 6 additions & 2 deletions lib/services/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -1314,10 +1314,14 @@ AWS.util.update(AWS.S3.prototype, {
params = {};
}
var hostname = this.endpoint.hostname;
// copy params so that appending keys does not unintentioinallly
// mutate params object argument passed in by user
var copiedParams = AWS.util.copy(params);

if (hostname !== this.api.globalEndpoint && !params.CreateBucketConfiguration) {
params.CreateBucketConfiguration = { LocationConstraint: this.config.region };
copiedParams.CreateBucketConfiguration = { LocationConstraint: this.config.region };
}
return this.makeRequest('createBucket', params, callback);
return this.makeRequest('createBucket', copiedParams, callback);
},

/**
Expand Down
28 changes: 28 additions & 0 deletions test/services/s3.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2391,6 +2391,34 @@ describe('AWS.S3', function() {
done();
});
});

it('appends CreateBucketConfiguration key to copy of params, not original, when endpoint is configured', function(done) {
var loc = null;
var params = {
Bucket: 'name'
};

s3 = new AWS.S3({
region: 'eu-west-1',
Bucket: 'name',
endpoint: 'https://foo.bar.baz:555/prefix'
});

s3.makeRequest = function(op, copiedParams, cb) {
expect(copiedParams).to['be'].a('object');
loc = copiedParams.CreateBucketConfiguration.LocationConstraint;
if (typeof cb === 'function') {
return cb();
}
};

helpers.mockHttpResponse(200, {}, '');
s3.createBucket(params, function() {
expect(params.CreateBucketConfiguration).to.not.exist;
expect(loc).to.be.a.string;
done();
});
});
});

describe('deleteBucket', function() {
Expand Down

0 comments on commit cd2fe1e

Please sign in to comment.