forked from TTLabs/EvaporateJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaporate.js
760 lines (573 loc) · 24.6 KB
/
evaporate.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/*Copyright (c) 2014, TT Labs, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the TT Labs, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
/***************************************************************************************************
* *
* version 0.0.2 *
* *
* TODO: *
* calculate MD5s and send with PUTs *
* post eTags to application server to allow resumability after client-side crash/restart *
* *
* *
***************************************************************************************************/
(function() {
var Evaporate = function(config){
this.supported = !((typeof(File)=='undefined') ||
(typeof(Blob)=='undefined') ||
!(!!Blob.prototype.webkitSlice || !!Blob.prototype.mozSlice || Blob.prototype.slice) ||
config.testUnsupported);
if(!this.supported){
return;
}
var PENDING = 0, EVAPORATING = 2, COMPLETE = 3, PAUSED = 4, CANCELED = 5, ERROR = 10, ABORTED = 20, AWS_URL = config.aws_url || 'https://s3.amazonaws.com', ETAG_OF_0_LENGTH_BLOB = '"d41d8cd98f00b204e9800998ecf8427e"';
var _ = this;
var files = [];
var con = extend({
logging: true,
maxConcurrentParts: 5,
partSize: 6 * 1024 * 1024,
retryBackoffPower: 2,
maxRetryBackoffSecs: 300,
progressIntervalMS: 500,
cloudfront: false
}, config);
//con.simulateStalling = true
_.add = function(file){
l.d('add');
var err;
if (typeof file == 'undefined'){
return 'Missing file';
}
if (typeof file.name == 'undefined'){
err = 'Missing attribute: name ';
}
else{
file.name = encodeURIComponent(file.name); // prevent signature fail in case file name has spaces
}
/*if (!(file.file instanceof File)){
err += '.file attribute must be instanceof File';
}*/
if (err){return err;}
var newId = addFile(file);
asynProcessQueue();
return newId;
};
_.cancel = function(id){
l.d('cancel ', id);
if (files[id]){
files[id].stop();
return true;
} else {
return false;
}
};
_.pause = function(id){
};
_.resume = function(id){
};
_.forceRetry = function(){
};
var l = {d:function(){}, w: function(){}, e:function(){}};
if(con.logging && console && console.log){
l = console;
l.d = l.log;
if (console.warn){
l.w = l.warn;
}else{
l.w = l.log;
}
if (console.error){
l.e = l.error;
}else{
l.e = l.log;
}
}
function addFile(file){
var id = files.length;
files.push(new FileUpload(extend({
progress: function(){},
complete: function(){},
cancelled: function(){},
info: function(){},
warn: function(){},
error: function(){}
},file,{
id: id,
status: PENDING,
priority: 0,
onStatusChange: onFileUploadStatusChange,
loadedBytes: 0,
sizeBytes: file.file.size
})));
return id;
}
function onFileUploadStatusChange(){
l.d('onFileUploadStatusChange');
processQueue();
}
function asynProcessQueue(){
setTimeout(processQueue,1);
}
function processQueue(){
l.d('processQueue length: ' + files.length);
var next = -1, priorityOfNext = -1, readyForNext = true;
files.forEach(function(file,i){
if (file.priority > priorityOfNext && file.status == PENDING){
next = i;
priorityOfNext = file.priority;
}
if (file.status == EVAPORATING){
readyForNext = false;
}
});
if (readyForNext && next >= 0){
files[next].start();
}
}
function FileUpload(file){
var me = this, parts = [], progressTotalInterval, progressPartsInterval, countUploadAttempts = 0, xhrs = [];
extend(me,file);
me.start = function(){
l.d('starting FileUpload ' + me.id);
setStatus(EVAPORATING);
initiateUpload();
monitorTotalProgress();
monitorPartsProgress();
};
me.stop = function(){
l.d('stopping FileUpload ', me.id);
me.cancelled();
me.info('upload canceled');
setStatus(CANCELED);
cancelAllRequests();
};
function setStatus(s){
if (s == COMPLETE || s == ERROR || s == CANCELED){
clearInterval(progressTotalInterval);
clearInterval(progressPartsInterval);
}
me.status = s;
me.onStatusChange();
}
function cancelAllRequests(){
l.d('cancelAllRequests()');
xhrs.forEach(function(xhr,i){
xhr.abort();
});
}
function initiateUpload(){ // see: http://docs.amazonwebservices.com/AmazonS3/latest/API/mpUploadInitiate.html
var initiate = {
method: 'POST',
path: getPath() + '?uploads',
step: 'initiate',
x_amz_headers: me.xAmzHeadersAtInitiate,
not_signed_headers: me.notSignedHeadersAtInitiate
};
if (me.contentType){
initiate.contentType = me.contentType;
}
initiate.onErr = function(xhr){
l.d('onInitiateError for FileUpload ' + me.id);
setStatus(ERROR);
};
initiate.on200 = function(xhr){
var match = xhr.response.match(/<UploadId\>(.+)<\/UploadId\>/);
if (match && match[1]){
me.uploadId = match[1];
l.d('requester success. got uploadId ' + me.uploadId);
makeParts();
processPartsList();
}else{
initiate.onErr();
}
};
setupRequest(initiate);
authorizedSend(initiate);
}
function uploadPart(partNumber){ //http://docs.amazonwebservices.com/AmazonS3/latest/API/mpUploadUploadPart.html
var backOff, hasErrored, upload, part;
part = parts[partNumber];
part.status = EVAPORATING;
countUploadAttempts++;
part.loadedBytesPrevious = null;
backOff = part.attempts++ === 0 ? 0 : 1000 * Math.min(
con.maxRetryBackoffSecs,
Math.pow(con.retryBackoffPower,part.attempts-2)
);
l.d('uploadPart #' + partNumber + ' will wait ' + backOff + 'ms to try');
upload = {
method: 'PUT',
path: getPath() + '?partNumber='+partNumber+'&uploadId='+me.uploadId,
step: 'upload #' + partNumber,
x_amz_headers: me.xAmzHeadersAtUpload,
attempts: part.attempts
};
// TODO: add md5
upload.onErr = function (xhr, isOnError){
var msg = 'problem uploading part #' + partNumber + ', http status: ' + xhr.status +
', hasErrored: ' + !!hasErrored + ', part status: ' + part.status +
', readyState: ' + xhr.readyState + (isOnError ? ', isOnError' : '');
l.w(msg);
me.warn(msg);
if (hasErrored){
return;
}
hasErrored = true;
if (xhr.status == 404){
var errMsg = '404 error resulted in abortion of both this part and the entire file.';
l.w(errMsg + ' Server response: ' + xhr.response);
me.error(errMsg);
// TODO: kill off other uploading parts when file is aborted
part.status = ABORTED;
setStatus(ABORTED);
} else {
part.status = ERROR;
part.loadedBytes = 0;
processPartsList();
}
xhr.abort();
// TODO: does AWS have other error codes that we can handle?
};
upload.on200 = function (xhr){
var eTag = xhr.getResponseHeader('ETag'), msg;
l.d('uploadPart 200 response for part #' + partNumber + ' ETag: ' + eTag);
if(part.isEmpty || (eTag != ETAG_OF_0_LENGTH_BLOB)) // issue #58
{
part.eTag = eTag;
part.status = COMPLETE;
}
else
{
part.status = ERROR;
part.loadedBytes = 0;
msg = 'eTag matches MD5 of 0 length blob for part #' + partNumber + ' Retrying part.';
l.w(msg);
me.warn(msg);
}
processPartsList();
};
upload.onProgress = function (evt){
part.loadedBytes = evt.loaded;
};
var slicerFn = (me.file.slice ? 'slice' : (me.file.mozSlice ? 'mozSlice' : 'webkitSlice'));
// browsers' implementation of the Blob.slice function has been renamed a couple of times, and the meaning of the 2nd parameter changed. For example Gecko went from slice(start,length) -> mozSlice(start, end) -> slice(start, end). As of 12/12/12, it seems that the unified 'slice' is the best bet, hence it being first in the list. See https://developer.mozilla.org/en-US/docs/DOM/Blob for more info.
upload.toSend = function() {
var slice= me.file[slicerFn](part.start, part.end);
l.d('sending part # ' + partNumber + ' (bytes ' + part.start + ' -> ' + part.end + ') reported length: ' + slice.size);
if(!part.isEmpty && slice.size === 0) // issue #58
{
l.w(' *** WARN: blob reporting size of 0 bytes. Will try upload anyway..');
}
return slice;
};
upload.onFailedAuth = function(xhr){
var msg = 'onFailedAuth for uploadPart #' + partNumber + '. Will set status to ERROR';
l.w(msg);
me.warn(msg);
part.status = ERROR;
part.loadedBytes = 0;
processPartsList();
};
setupRequest(upload);
setTimeout(function(){
authorizedSend(upload);
l.d('upload #',partNumber,upload);
},backOff);
part.uploader = upload;
}
function abortPart(partNumber){
var part = parts[partNumber];
if (part.uploader.awsXhr){
part.uploader.awsXhr.abort();
}
if (part.uploader.authXhr){
part.uploader.authXhr.abort();
}
}
function completeUpload(){ //http://docs.amazonwebservices.com/AmazonS3/latest/API/mpUploadComplete.html
l.d('completeUpload');
me.info('will attempt to complete upload');
var completeDoc = '<CompleteMultipartUpload>';
parts.forEach(function(part,partNumber){
if (part){
completeDoc += '<Part><PartNumber>' + partNumber + '</PartNumber><ETag>' + part.eTag + '</ETag></Part>';
}
});
completeDoc += '</CompleteMultipartUpload>';
var complete = {
method: 'POST',
contentType: 'application/xml; charset=UTF-8',
path: getPath() + '?uploadId='+me.uploadId,
x_amz_headers: me.xAmzHeadersAtComplete,
step: 'complete'
};
complete.onErr = function (){
var msg = 'Error completing upload.';
l.w(msg);
me.error(msg);
setStatus(ERROR);
};
complete.on200 = function(xhr){
me.complete(xhr);
setStatus(COMPLETE);
};
complete.toSend = function() {
return completeDoc;
};
setupRequest(complete);
authorizedSend(complete);
}
function makeParts(){
var numParts = Math.ceil(me.file.size / con.partSize) || 1; // issue #58
for (var part = 1; part <= numParts; part++){
parts[part] = {
status: PENDING,
start: (part-1)*con.partSize,
end: (part*con.partSize),
attempts: 0,
loadedBytes: 0,
loadedBytesPrevious: null,
isEmpty: (me.file.size === 0) // issue #58
};
}
}
function processPartsList(){
var evaporatingCount = 0, finished = true, anyPartHasErrored = false, stati = [], bytesLoaded = [], info;
if (me.status != EVAPORATING){
me.info('will not process parts list, as not currently evaporating');
return;
}
parts.forEach(function(part,i){
var requiresUpload = false;
stati.push(part.status);
if (part){
switch(part.status){
case EVAPORATING:
finished = false;
evaporatingCount++;
bytesLoaded.push(part.loadedBytes);
break;
case ERROR:
anyPartHasErrored = true;
requiresUpload = true;
break;
case PENDING:
requiresUpload = true;
break;
default:
break;
}
if (requiresUpload){
finished = false;
if (evaporatingCount < con.maxConcurrentParts){
uploadPart(i);
evaporatingCount++;
}
}
}
});
info = stati.toString() + ' // bytesLoaded: ' + bytesLoaded.toString();
l.d('processPartsList() anyPartHasErrored: ' + anyPartHasErrored,info);
if (countUploadAttempts >= (parts.length-1) || anyPartHasErrored){
me.info('part stati: ' + info);
}
// parts.length is always 1 greater than the actually number of parts, because AWS part numbers start at 1, not 0, so for a 3 part upload, the parts array is: [undefined, object, object, object], which has length 4.
if (finished){
completeUpload();
}
}
function monitorTotalProgress(){
progressTotalInterval = setInterval(function(){
var totalBytesLoaded = 0;
parts.forEach(function(part,i){
totalBytesLoaded += part.loadedBytes;
});
me.progress(totalBytesLoaded/me.sizeBytes);
},con.progressIntervalMS);
}
/*
Issue #6 identified that some parts would stall silently.
The issue was only noted on Safari on OSX. A bug was filed with Apple, #16136393
This function was addeded as a work-around. It check the progress of each part every 2 minutes.
If it finds a part that has made no progress in the last 2 minutes then it aborts it. It will then be detected as an error, and restarted in the same manner of any other errored part
*/
function monitorPartsProgress(){
progressPartsInterval = setInterval(function(){
l.d('monitorPartsProgress() ' + Date());
parts.forEach(function(part,i){
var healthy;
if (part.status != EVAPORATING){
l.d(i, 'not evaporating ');
return;
}
if (part.loadedBytesPrevious === null){
l.d(i,'no previous ');
part.loadedBytesPrevious = part.loadedBytes;
return;
}
healthy = part.loadedBytesPrevious < part.loadedBytes;
if (con.simulateStalling && i == 4){
if (Math.random() < 0.25){
healthy = false;
}
}
l.d(i, (healthy ? 'moving. ' : 'stalled.'), part.loadedBytesPrevious, part.loadedBytes);
if (!healthy){
setTimeout(function(){
me.info('part #' + i + ' stalled. will abort. ' + part.loadedBytesPrevious + ' ' + part.loadedBytes);
abortPart(i);
},0);
}
part.loadedBytesPrevious = part.loadedBytes;
});
},2 * 60 * 1000);
}
function setupRequest(requester){
l.d('setupRequest()',requester);
if(!con.timeUrl)
{
requester.dateString = new Date().toUTCString();
}
else
{
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("GET", con.timeUrl + '?requestTime=' + new Date().getTime(), false);
xmlHttpRequest.send();
requester.dateString = xmlHttpRequest.responseText;
}
requester.x_amz_headers = extend(requester.x_amz_headers,{
'x-amz-date': requester.dateString
});
requester.onGotAuth = function (){
var xhr = new XMLHttpRequest();
xhrs.push(xhr);
requester.awsXhr = xhr;
var payload = requester.toSend ? requester.toSend() : null;
var url = AWS_URL + requester.path;
var all_headers = {};
extend(all_headers, requester.not_signed_headers);
extend(all_headers, requester.x_amz_headers);
if (con.simulateErrors && requester.attempts == 1 &&requester.step == 'upload #3'){
l.d('simulating error by POST part #3 to invalid url');
url = 'https:///foo';
}
xhr.open(requester.method, url);
xhr.setRequestHeader('Authorization', 'AWS ' + con.aws_key + ':' + requester.auth);
for (var key in all_headers) {
if (all_headers.hasOwnProperty(key)) {
xhr.setRequestHeader(key, all_headers[key]);
}
}
if (requester.contentType){
xhr.setRequestHeader('Content-Type', requester.contentType);
}
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if(payload){l.d(' ### ' + payload.size);} // Test, per http://code.google.com/p/chromium/issues/detail?id=167111#c20
if (xhr.status == 200){
requester.on200(xhr);
} else {
requester.onErr(xhr);
}
}
};
xhr.onerror = function(){requester.onErr(xhr,true);};
if (typeof requester.onProgress == 'function'){
xhr.upload.onprogress = function(evt){
requester.onProgress(evt);
};
}
xhr.send(payload);
};
requester.onFailedAuth = requester.onFailedAuth || function(xhr){
me.error('Error onFailedAuth for step: ' + requester.step);
requester.onErr(xhr);
};
}
//see: http://docs.amazonwebservices.com/AmazonS3/latest/dev/RESTAuthentication.html#ConstructingTheAuthenticationHeader
function authorizedSend(authRequester){
l.d('authorizedSend() ' + authRequester.step);
var xhr = new XMLHttpRequest();
xhrs.push(xhr);
authRequester.authXhr = xhr;
var url = con.signerUrl+'?to_sign='+makeStringToSign(authRequester);
var warnMsg;
for (var param in me.signParams) {
if (!me.signParams.hasOwnProperty(param)) {continue;}
url += ('&'+escape(param)+'='+escape(me.signParams[param]));
}
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if (xhr.status == 200 && xhr.response.length == 28){
l.d('authorizedSend got signature for step: \'' + authRequester.step + '\' sig: '+ xhr.response);
authRequester.auth = xhr.response;
authRequester.onGotAuth();
} else {
warnMsg = 'failed to get authorization (readyState=4) for ' + authRequester.step + '. xhr.status: ' + xhr.status + '. xhr.response: ' + xhr.response;
l.w(warnMsg);
me.warn(warnMsg);
authRequester.onFailedAuth(xhr);
}
}
};
xhr.onerror = function(){
warnMsg = 'failed to get authorization (onerror) for ' + authRequester.step + '. xhr.status: ' + xhr.status + '. xhr.response: ' + xhr.response;
l.w(warnMsg);
me.warn(warnMsg);
authRequester.onFailedAuth(xhr);
};
xhr.open('GET', url);
xhr.send();
}
function makeStringToSign(request){
var x_amz_headers = '', to_sign, header_key_array = [];
for (var key in request.x_amz_headers) {
if (request.x_amz_headers.hasOwnProperty(key)) {
header_key_array.push(key);
}
}
header_key_array.sort();
header_key_array.forEach(function(header_key,i){
x_amz_headers += (header_key + ':'+ request.x_amz_headers[header_key] + '\n');
});
to_sign = request.method+'\n'+
'\n'+
(request.contentType || '')+'\n'+
'\n'+
x_amz_headers +
(con.cloudfront ? '/' + con.bucket : '')+
request.path;
return encodeURIComponent(to_sign);
}
function getPath() {
var path = '/' + con.bucket + '/' + me.name;
if (con.cloudfront || AWS_URL.indexOf('cloudfront') > -1) {
path = '/' + me.name;
}
return path;
}
}
function extend(obj1, obj2, obj3){
if (typeof obj1 == 'undefined'){obj1 = {};}
if (typeof obj3 == 'object'){
for (var key in obj3){
obj2[key]=obj3[key];
}
}
for (var key2 in obj2){
obj1[key2]=obj2[key2];
}
return obj1;
}
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Evaporate;
} else if (typeof window !== 'undefined') {
window.Evaporate = Evaporate;
}
})();