-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy paths3.js
More file actions
244 lines (212 loc) · 8.06 KB
/
s3.js
File metadata and controls
244 lines (212 loc) · 8.06 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*eslint-env node*/
var CoreObject = require('core-object');
var fs = require('fs');
var path = require('path');
var mime = require('mime');
var RSVP = require('rsvp');
var _ = require('lodash');
module.exports = CoreObject.extend({
init: function(options) {
this._super(options);
const {
fromIni
} = require('@aws-sdk/credential-providers');
const {
S3
} = require('@aws-sdk/client-s3');
var s3Options = {
region: this.plugin.readConfig('region')
};
const proxy = this.plugin.readConfig('proxy');
if (proxy) {
var agent;
this._proxyAgent = this.plugin.readConfig('proxyAgent');
if (this._proxyAgent) {
agent = this._proxyAgent(proxy);
} else {
const { ProxyAgent } = require('proxy-agent');
agent = new ProxyAgent(proxy);
}
s3Options.httpOptions = {
agent
};
}
const accessKeyId = this.plugin.readConfig('accessKeyId');
const secretAccessKey = this.plugin.readConfig('secretAccessKey');
const sessionToken = this.plugin.readConfig('sessionToken');
const profile = this.plugin.readConfig('profile');
const signatureVersion = this.plugin.readConfig('signatureVersion');
const endpoint = this.plugin.readConfig('endpoint');
if (accessKeyId && secretAccessKey) {
this.plugin.log('Using AWS access key id and secret access key from config', { verbose: true });
s3Options.credentials = {
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
};
if (sessionToken) {
this.plugin.log('Using AWS session token from config', { verbose: true });
s3Options.credentials.sessionToken = sessionToken;
}
}
if (signatureVersion) {
this.plugin.log('Using signature version from config', { verbose: true });
s3Options.signatureVersion = signatureVersion;
}
if (profile && !this.plugin.readConfig('s3Client')) {
this.plugin.log('Using AWS profile from config', { verbose: true });
s3Options.credentials = fromIni({ profile: profile });
}
if (endpoint) {
this.plugin.log('Using endpoint from config', { verbose: true });
s3Options.endpoint = endpoint;
}
this._client = this.plugin.readConfig('s3Client') || new S3(s3Options);
},
upload: function(options) {
options = options || {};
return this._determineFilePaths(options).then(function(filePaths) {
const allFilesUploaded = this._putObjects(filePaths, options);
const manifestPath = options.manifestPath;
if (manifestPath) {
return allFilesUploaded.then(function(filesUploaded) {
return this._putObject(manifestPath, options).then(function(manifestUploaded) {
return filesUploaded.concat(manifestUploaded);
});
}.bind(this));
} else {
return allFilesUploaded;
}
}.bind(this));
},
_determineFilePaths: function(options) {
var plugin = this.plugin;
var filePaths = options.filePaths || [];
if (typeof filePaths === 'string') {
filePaths = [filePaths];
}
var prefix = options.prefix;
var manifestPath = options.manifestPath;
if (manifestPath) {
var key = prefix === '' ? manifestPath : [prefix, manifestPath].join('/');
plugin.log('Downloading manifest for differential deploy from `' + key + '`...', { verbose: true });
return new RSVP.Promise(function(resolve, reject){
var params = { Bucket: options.bucket, Key: key};
this._client.getObject(params, function(error, data) {
if (error) {
reject(error);
} else {
resolve(data.Body.toString().split('\n'));
}
}.bind(this));
}.bind(this)).then(function(manifestEntries){
plugin.log("Manifest found. Differential deploy will be applied.", { verbose: true });
return _.difference(filePaths, manifestEntries);
}).catch(function(/* reason */){
plugin.log("Manifest not found. Disabling differential deploy.", { color: 'yellow', verbose: true });
return RSVP.resolve(filePaths);
});
} else {
return RSVP.resolve(filePaths);
}
},
_mimeCharsetsLookup: function(mimeType, fallback) {
// the node-mime library removed this method in v 2.0. This is the replacement
// code for what was formerly mime.charsets.lookup
return (/^text\/|^application\/(javascript|json)/).test(mimeType) ? 'UTF-8' : fallback;
},
_putObject: function(filePath, options, filePaths) {
var plugin = this.plugin;
var cwd = options.cwd;
var bucket = options.bucket;
var prefix = options.prefix;
var acl = options.acl;
var gzippedFilePaths = options.gzippedFilePaths || [];
var brotliCompressedFilePaths = options.brotliCompressedFilePaths || [];
var cacheControl = options.cacheControl;
var expires = options.expires;
var metadata = options.metadata;
var serverSideEncryption = options.serverSideEncryption;
var defaultType = options.defaultMimeType || mime.getType('bin');
var basePath = path.join(cwd, filePath);
var data = fs.readFileSync(basePath);
var contentType = mime.getType(basePath) || defaultType;
var encoding = this._mimeCharsetsLookup(contentType);
var key = prefix === '' ? filePath : [prefix, filePath].join('/');
var isGzipped = gzippedFilePaths.indexOf(filePath) !== -1;
var isBrotliCompressed = brotliCompressedFilePaths.indexOf(filePath) !== -1;
if (isGzipped && path.extname(basePath) === '.gz') {
var basePathUngzipped = filePath.slice(0, -3);
if (filePaths && filePaths.indexOf(basePathUngzipped) !== -1) {
contentType = mime.getType(basePathUngzipped) || defaultType;
encoding = this._mimeCharsetsLookup(contentType);
}
}
if (isBrotliCompressed) {
var basePathUncompressed = filePath.slice(0, -3);
if (filePaths && filePaths.indexOf(basePathUncompressed) !== -1) {
contentType = mime.getType(basePathUncompressed) || defaultType;
encoding = this._mimeCharsetsLookup(contentType);
}
}
if (encoding) {
contentType += '; charset=';
contentType += encoding.toLowerCase();
}
var params = {
Bucket: bucket,
ACL: acl,
Body: data,
ContentType: contentType,
Key: key,
CacheControl: cacheControl,
Expires: expires
};
if (serverSideEncryption) {
params.ServerSideEncryption = serverSideEncryption;
}
if (metadata) {
params.Metadata = metadata;
}
if (isGzipped) {
params.ContentEncoding = 'gzip';
}
if (isBrotliCompressed) {
params.ContentEncoding = 'br';
}
return new RSVP.Promise(function(resolve, reject) {
this._client.putObject(params, function(error) {
if (error) {
reject(error);
} else {
plugin.log('✔ ' + key, { verbose: true });
resolve(filePath);
}
});
}.bind(this));
},
_currentEnd: 0,
_putObjectsBatch: function(filePaths, options) {
var currentBatch = filePaths.slice(this._currentEnd, Math.min(this._currentEnd + options.batchSize, filePaths.length));
this._currentEnd += currentBatch.length;
//Execute our current batch of promises
return RSVP.all(currentBatch.map(function (filePath) {
return this._putObject(filePath, options, filePaths);
}.bind(this)))
//Then check if we need to execute another batch
.then(function () {
if (this._currentEnd < filePaths.length) {
return this._putObjectsBatch(filePaths, options);
}
return filePaths;
}.bind(this));
},
_putObjects: function (filePaths, options) {
if (options.batchSize > 0) {
this._currentEnd = 0;
return this._putObjectsBatch(filePaths, options);
}
return RSVP.all(filePaths.map(function (filePath) {
return this._putObject(filePath, options, filePaths);
}.bind(this)));
}
});