-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.js
248 lines (217 loc) · 8.98 KB
/
index.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
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
245
246
247
248
/**
* Module dependencies
*/
var Writable = require('stream').Writable;
var _ = require('@sailshq/lodash');
var flaverr = require('flaverr');
var mime = require('mime');
var AWS = require('aws-sdk');
/**
* skipper-s3
*
* @param {Dictionary} globalOpts
* @property {String} key
* @property {String} secret
* @property {String} bucket
*
* @returns {Dictionary}
* @property {Function} read
* @property {Function} rm
* @property {Function} ls
* @property {Function} receive
*/
module.exports = function SkipperS3 (globalOpts) {
globalOpts = globalOpts || {};
return {
read: function (fd) {
if (arguments[1]) {
return arguments[1](new Error('For performance reasons, skipper-s3 does not support passing in a callback to `.read()`'));
}
var readable = _buildS3Client(globalOpts)
.getObject({
Bucket: globalOpts.bucket,
Key: fd.replace(/^\/+/, ''),// « strip leading slashes
})
.createReadStream();
return readable;
},
rm: function (fd, done) {
_buildS3Client(globalOpts)
.deleteObjects(_stripKeysWithUndefinedValues({
Bucket: globalOpts.bucket,
Delete: {
Quiet: false,
Objects: [
{
Key: fd.replace(/^\/+/, '')// « strip leading slashes
}
]
}
}), (err, result)=>{
if (err){ return done(err); }
if (result && result['Errors'] && result['Errors'].length > 0) {
return done(flaverr({raw: result['Errors']}, new Error('Failed to remove some file(s) from S3 (see `.raw`)')));
}
return done(undefined, result);
});//_∏_
},
ls: function (dirname, done) {
_buildS3Client(globalOpts)
.listObjectsV2(_stripKeysWithUndefinedValues({
Bucket: globalOpts.bucket,
// Delimiter: '/', « doesn't seem to make any meaningful difference
Prefix: (
// Allow empty dirname (defaults to ''), & strip leading slashes
// from dirname to form prefix
(dirname || '').replace(/^\/+/, '')
)
// FUTURE: maybe also check out "MaxKeys"..?
}), (err, result)=>{
if (err){ return done(err); }
var formattedResults;
try {
formattedResults = _.pluck(result['Contents'], 'Key');
} catch (err) { return done(err); }
return done(undefined, formattedResults);
});//_∏_
},
/**
* A simple receiver for Skipper that writes Upstreams to
* S3 to the configured bucket at the configured path.
*
* @param {Dictionary} s3ClientOpts
* @property {String} fd
* @property {String} bucket
* etc…
* @return {Receiver} (a writable stream)
*/
receive: function S3Receiver (s3ClientOpts) {
s3ClientOpts = s3ClientOpts || {};
s3ClientOpts = _.extend({}, globalOpts, s3ClientOpts);
var wasMaxBytesPerUpstreamQuotaExceeded;
var wasMaxBytesPerFileQuotaExceeded;
var maxBytesPerUpstream = s3ClientOpts.maxBytes || undefined;
var maxBytesPerFile = s3ClientOpts.maxBytesPerFile || undefined;
var receiver = Writable({ objectMode: true });
receiver.once('error', (unusedErr)=>{
// console.log('ERROR ON receiver ::', unusedErr);
});//œ
var bytesWrittenByFd = {};
// console.log('constructed receiver');
receiver._write = (incomingFileStream, encoding, proceed)=>{
// console.log('uploading file w/ skipperFd', incomingFileStream.skipperFd);
// Check for `.skipperFd` (or if not present, `.fd`, for backwards compatibility)
if (!_.isString(incomingFileStream.skipperFd) || incomingFileStream.skipperFd === '') {
if (!_.isString(incomingFileStream.fd) || incomingFileStream.fd === '') {
return proceed(new Error('In skipper-s3: Incoming file stream does not have the expected `.skipperFd` or `.fd` properties-- at least not as a valid string. If you are using sails-hook-uploads or skipper directly, this should have been automatically attached! Here is what we got for `.fd` (legacy property): `'+incomingFileStream.fd+'`. And here is what we got for `.skipperFd` (new property): `'+incomingFileStream.skipperFd+'`'));
} else {
// Backwards compatibility:
incomingFileStream.skipperFd = incomingFileStream.fd;
}
}//fi
var incomingFd = incomingFileStream.skipperFd;
bytesWrittenByFd[incomingFd] = 0;//« bytes written for this file so far
incomingFileStream.once('error', (unusedErr)=>{
// console.log('ERROR ON incoming readable file stream in Skipper S3 adapter (%s) ::', incomingFileStream.filename, unusedErr);
});//œ
_uploadFile(incomingFd, incomingFileStream, (progressInfo)=>{
bytesWrittenByFd[incomingFd] = progressInfo.written;
incomingFileStream.byteCount = progressInfo.written;//« used by Skipper core
let totalBytesWrittenForThisUpstream = 0;
for (let fd in bytesWrittenByFd) {
totalBytesWrittenForThisUpstream += bytesWrittenByFd[fd];
}//∞
// console.log('maxBytesPerUpstream',maxBytesPerUpstream);
// console.log('bytesWrittenByFd',bytesWrittenByFd);
// console.log('totalBytesWrittenForThisUpstream',totalBytesWrittenForThisUpstream);
if (maxBytesPerUpstream && totalBytesWrittenForThisUpstream > maxBytesPerUpstream) {
wasMaxBytesPerUpstreamQuotaExceeded = true;
return false;
} else if (maxBytesPerFile && bytesWrittenByFd[incomingFd] > maxBytesPerFile) {
wasMaxBytesPerFileQuotaExceeded = true;
return false;
} else {
if (s3ClientOpts.onProgress) {
s3ClientOpts.onProgress(progressInfo);
} else {
receiver.emit('progress', progressInfo);// « for backwards compatibility
}
return true;
}
}, s3ClientOpts, (err)=>{
if (err) {
// console.log(('Receiver: Error writing `' + incomingFileStream.filename + '`:: ' + require('util').inspect(err) + ' :: Cancelling upload and cleaning up already-written bytes...').red);
if (flaverr.taste({name: 'RequestAbortedError'}, err)) {
if (maxBytesPerUpstream && wasMaxBytesPerUpstreamQuotaExceeded) {
err = flaverr({code: 'E_EXCEEDS_UPLOAD_LIMIT'}, new Error(`Upload too big! Exceeded quota ("maxBytes": ${maxBytesPerUpstream})`));
} else if (maxBytesPerFile && wasMaxBytesPerFileQuotaExceeded) {
err = flaverr({code: 'E_EXCEEDS_FILE_SIZE_LIMIT'}, new Error(`One of the attempted file uploads was too big! Exceeded quota ("maxBytesPerFile": ${maxBytesPerFile})`));
}//fi
}//fi
receiver.emit('error', err);
} else {
incomingFileStream.byteCount = bytesWrittenByFd[incomingFd];//« used by Skipper core
receiver.emit('writefile', incomingFileStream);
return proceed();
}
});//_∏_
};//ƒ
return receiver;
}
};
};
//////////////////////////////////////////////////////////////////////////////
/**
* destructive -- mutates, returns reference only for convenience
*/
function _stripKeysWithUndefinedValues(dictionary) {
for (let k in dictionary) {
if (dictionary[k] === undefined) {
delete dictionary[k];
}
}
return dictionary;
}//ƒ
function _buildS3Client(s3ClientOpts) {
var s3ConstructorArgins = _stripKeysWithUndefinedValues({
apiVersion: '2006-03-01',
region: s3ClientOpts.region,
accessKeyId: s3ClientOpts.key,
secretAccessKey: s3ClientOpts.secret,
endpoint: s3ClientOpts.endpoint
});
return new AWS.S3(s3ConstructorArgins);
}//ƒ
function _uploadFile(incomingFd, incomingFileStream, handleProgress, s3ClientOpts, done) {
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property
var s3ManagedUpload = _buildS3Client(s3ClientOpts)
.upload(_stripKeysWithUndefinedValues({
Bucket: s3ClientOpts.bucket,
Key: incomingFd.replace(/^\/+/, ''),//« remove any leading slashes
Body: incomingFileStream,
ContentType: mime.getType(incomingFd)//« advisory; makes things nicer in the S3 dashboard
}), (err, rawS3ResponseData)=>{
if (err) {
return done(err);
} else {
return done(undefined, {
rawS3ResponseData
});
}
});//_∏_
s3ManagedUpload.on('httpUploadProgress', (event)=>{
// console.log('upload progress');
let written = _.isNumber(event.loaded) ? event.loaded : 0;
let total = _.isNumber(event.total) ? event.total : undefined;
let handledSuccessfully = handleProgress(_stripKeysWithUndefinedValues({
name: incomingFileStream.filename || incomingFd,
fd: incomingFd,
written,
total,
percent: total ? (written / total) : undefined
}));
if (!handledSuccessfully) {
s3ManagedUpload.abort();
}
});//œ
}//ƒ