-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathdeployment-bucket-endpoint.js
59 lines (49 loc) · 1.68 KB
/
deployment-bucket-endpoint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use-strict';
const { endpoint } = require('aws-info');
const https = require('https');
function getOptions(cli, bucket, endpoint) {
const method = 'HEAD';
if (bucket.match(/^[a-z0-9][a-z0-9-]*[a-z0-9]$/) === null) {
cli.log(`WARNING: DNS incompatible bucket name detected (${bucket}).`);
cli.log(` These will cause errors starting 30 Sep 2020 due to S3 API changes`);
cli.log(` and SSL certificate mismatches for virtual-hosting style S3 URLs.`);
cli.log(` Read more here: https://forums.aws.amazon.com/ann.jspa?annID=6776`);
return {
method,
hostname: endpoint,
path: `/${bucket}`
};
}
return {
method,
hostname: `${bucket}.${endpoint}`,
path: '/'
};
}
module.exports = function setDeploymentBucketEndpoint() {
return new Promise((resolve, reject) => {
const { service: { provider } } = this.serverless;
const { deploymentBucket } = provider;
const { region = provider.region } = this.options;
const s3Endpoint = endpoint('S3', region);
if (deploymentBucket) {
const bucket = typeof deploymentBucket === 'object'
? deploymentBucket.name
: deploymentBucket;
const options = getOptions(this.serverless.cli, bucket, s3Endpoint);
const request = https.request(options);
request.on('response', response => {
const bucketRegion = response.headers['x-amz-bucket-region'];
const bucketEndpoint = endpoint('S3', bucketRegion);
resolve(bucketEndpoint);
});
request.on('error', reject);
request.end();
} else {
resolve(s3Endpoint);
}
})
.then(bucketEndpoint => {
this.deploymentBucketEndpoint = bucketEndpoint
});
}