-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy paths3.js
211 lines (178 loc) · 6.92 KB
/
s3.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
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
'use strict';
var { fromIni } = require('@aws-sdk/credential-providers');
var { S3 } = require('@aws-sdk/client-s3');
var CoreObject = require('core-object');
var RSVP = require('rsvp');
var fs = require('fs');
var readFile = RSVP.denodeify(fs.readFile);
var mime = require('mime-types');
var joinUriSegments = require('./util/join-uri-segments');
async function headObject(client, params) {
try {
return await client.headObject(params);
} catch (err) {
if (err.name === 'NotFound') {
return;
}
throw err;
}
}
module.exports = CoreObject.extend({
init: function(options) {
this._super();
var plugin = options.plugin;
var config = plugin.pluginConfig;
var profile = plugin.readConfig('profile');
var endpoint = plugin.readConfig('endpoint');
var credentials;
this._plugin = plugin;
var providedS3Client = plugin.readConfig("s3Client");
if (profile && !providedS3Client) {
this._plugin.log("Using AWS profile from config", { verbose: true });
credentials = fromIni({ profile: profile });
}
if (endpoint) {
this._plugin.log('Using endpoint from config', { verbose: true });
}
this._client = providedS3Client || new S3(config);
if (endpoint) {
this._client.config.endpoint = endpoint;
}
if (credentials) {
this._client.config.credentials = credentials;
}
},
upload: function(options) {
var client = this._client;
var plugin = this._plugin;
var bucket = options.bucket;
var acl = options.acl;
var cacheControl = options.cacheControl;
var allowOverwrite = options.allowOverwrite;
var key = options.filePattern + ":" + options.revisionKey;
var revisionKey = joinUriSegments(options.prefix, key);
var putObject = RSVP.denodeify(client.putObject.bind(client));
var gzippedFilePaths = options.gzippedFilePaths || [];
var brotliCompressedFilePaths = options.brotliCompressedFilePaths || [];
var isGzipped = gzippedFilePaths.indexOf(options.filePattern) !== -1;
var isBrotliCompressed = brotliCompressedFilePaths.indexOf(options.filePattern) !== -1;
var serverSideEncryption = options.serverSideEncryption;
var checkForOverwrite = RSVP.resolve();
var params = {
Bucket: bucket,
Key: revisionKey,
ACL: acl,
ContentType: mime.lookup(options.filePath) || 'text/html',
CacheControl: cacheControl
};
if (serverSideEncryption) {
params.ServerSideEncryption = serverSideEncryption;
}
if (isGzipped) {
params.ContentEncoding = 'gzip';
}
if (isBrotliCompressed) {
params.ContentEncoding = 'br';
}
if (!allowOverwrite) {
checkForOverwrite = this.findRevision(options)
.then(function(found) {
if (found !== undefined) {
return RSVP.reject("REVISION ALREADY UPLOADED! (set `allowOverwrite: true` if you want to support overwriting revisions)");
}
return RSVP.resolve();
})
}
return checkForOverwrite
.then(readFile.bind(this, options.filePath))
.then(function(fileContents) {
params.Body = fileContents;
return putObject(params).then(function() {
plugin.log('✔ ' + revisionKey, { verbose: true });
});
});
},
activate: function(options) {
var plugin = this._plugin;
var client = this._client;
var bucket = options.bucket;
var acl = options.acl;
var prefix = options.prefix;
var filePattern = options.filePattern;
var key = filePattern + ":" + options.revisionKey;
var serverSideEncryption = options.serverSideEncryption;
var urlEncodeSourceObject = options.urlEncodeSourceObject;
var revisionKey = joinUriSegments(prefix, key);
var indexKey = joinUriSegments(prefix, filePattern);
var copyObject = RSVP.denodeify(client.copyObject.bind(client));
var params = {
Bucket: bucket,
Key: indexKey,
ACL: acl,
};
if (urlEncodeSourceObject) {
params.CopySource = encodeURIComponent([bucket, revisionKey].join('/'));
} else {
params.CopySource = `${bucket}/${revisionKey}`
}
if (serverSideEncryption) {
params.ServerSideEncryption = serverSideEncryption;
}
return this.findRevision(options).then(function(found) {
if (found !== undefined) {
return copyObject(params).then(function() {
plugin.log('✔ ' + revisionKey + " => " + indexKey);
});
} else {
return RSVP.reject("REVISION NOT FOUND!"); // see how we should handle a pipeline failure
}
});
},
findRevision: function(options) {
var client = this._client;
var listObjects = RSVP.denodeify(client.listObjects.bind(client));
var bucket = options.bucket;
var prefix = options.prefix;
var revisionPrefix = joinUriSegments(prefix, options.filePattern + ":" + options.revisionKey);
return listObjects({ Bucket: bucket, Prefix: revisionPrefix })
.then((response) => response.Contents?.find((element) => element.Key === revisionPrefix));
},
fetchRevisions: function(options) {
var client = this._client;
var bucket = options.bucket;
var prefix = options.prefix;
var revisionPrefix = joinUriSegments(prefix, options.filePattern + ":");
var indexKey = joinUriSegments(prefix, options.filePattern);
return RSVP.hash({
revisions: this.listAllObjects({ Bucket: bucket, Prefix: revisionPrefix }),
current: headObject(client, { Bucket: bucket, Key: indexKey }),
})
.then(function(data) {
return data.revisions.sort(function(a, b) {
return new Date(b.LastModified) - new Date(a.LastModified);
}).map(function(d) {
var revision = d.Key.substr(revisionPrefix.length);
var active = data.current && d.ETag === data.current.ETag;
return { revision: revision, timestamp: d.LastModified, active: active };
});
});
},
listAllObjects: function(options) {
var client = this._client;
var listObjects = RSVP.denodeify(client.listObjects.bind(client));
var allRevisions = [];
function listObjectRecursively(options) {
return listObjects(options).then(function(response) {
[].push.apply(allRevisions, response.Contents);
if (response.IsTruncated) {
var nextMarker = response.Contents[response.Contents.length - 1].Key;
options.Marker = nextMarker;
return listObjectRecursively(options);
} else {
return allRevisions;
}
});
}
return listObjectRecursively(options);
}
});