-
Notifications
You must be signed in to change notification settings - Fork 1.9k
S3 Signature on Worker #1702
S3 Signature on Worker #1702
Changes from 7 commits
0d9c341
7d1e503
8f0d01e
1e2f448
cd5aae7
a056ce6
27ad4cc
d24316a
6408a8d
8b5a693
d4bbf7e
1333b2a
15e61c7
bd2d0b6
da0fc05
51796ec
f335a7a
5e58d5c
a0f9812
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* globals qq, CryptoJS */ | ||
|
||
/** | ||
* Manages creation and communication of s3 signature workers. | ||
*/ | ||
qq.s3.RequestSignerWorkerManager = function (o) { | ||
"use strict"; | ||
var _worker = null, | ||
_workerPromises = {}, | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
options = { | ||
workerUrl: null, | ||
log: function(str, level) {} | ||
}; | ||
qq.extend(options, o, true); | ||
|
||
function init() { | ||
var workerUrl; | ||
switch (typeof options.workerUrl) { | ||
case "string": | ||
if (options.workerUrl !== "inline") { | ||
workerUrl = options.workerUrl; | ||
} else { | ||
if (!qq.s3.createS3InlineWorkerUrl) { | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
qq.Error("Missing inline s3 worker"); | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
return; | ||
} | ||
workerUrl = qq.s3.createS3InlineWorkerUrl(); | ||
} | ||
break; | ||
case "function": | ||
workerUrl = options.workerUrl(); | ||
break; | ||
default: | ||
break; | ||
} | ||
if (!workerUrl) { | ||
return; | ||
} | ||
try { | ||
_worker = new Worker(workerUrl); | ||
_worker.onerror = function (e) { | ||
// Prevent the event from bubbling | ||
e.preventDefault(); | ||
// log the error, and fail any pending promises. | ||
options.log("Worker encountered an error. Disabling. " + e.message, "warn"); | ||
_worker = null; | ||
var outstandingRequests = Object.keys(_workerPromises), | ||
i; | ||
for (i = 0; i < outstandingRequests.length; i++) { | ||
_workerPromises[outstandingRequests[i]].failure(e); | ||
delete _workerPromises[outstandingRequests[i]]; | ||
} | ||
}; | ||
_worker.onmessage = function (e) { | ||
if (!_workerPromises[e.data.id]) { | ||
options.log("Worker returned a result for an request we dont know about."); | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
return; | ||
} | ||
if (e.data.err) { | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
_workerPromises[e.data.id].failure(e.data.err); | ||
} else { | ||
_workerPromises[e.data.id].success(e.data.resp); | ||
} | ||
delete _workerPromises[e.data.id]; | ||
}; | ||
} catch (ex) { | ||
// worker is not supported or invalid | ||
options.log("Worker failed to be created. Defaulting back to main thread processing." + ex, "warn"); | ||
_worker = null; | ||
} | ||
} | ||
init(); | ||
/* | ||
Generates the signuare of the given file. | ||
@param file the file/slice to generate the signature for. | ||
@returns a promise or null if we can't generate signatures at all. | ||
*/ | ||
this.generateSignature = function (file) { | ||
if (!_worker) { | ||
return null; | ||
} | ||
var promise = new qq.Promise(), | ||
task = {file: file, id: qq.getUniqueId()}; | ||
_workerPromises[task.id] = promise; | ||
_worker.postMessage(task); | ||
return promise; | ||
}; | ||
}; |
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.