mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-24 16:56:15 -04:00
Move waiters and workers into separate folders.
This commit is contained in:
parent
31a3af1f84
commit
b77239fc15
16 changed files with 31 additions and 31 deletions
1058
src/web/workers/InputWorker.mjs
Normal file
1058
src/web/workers/InputWorker.mjs
Normal file
File diff suppressed because it is too large
Load diff
77
src/web/workers/LoaderWorker.js
Executable file
77
src/web/workers/LoaderWorker.js
Executable file
|
@ -0,0 +1,77 @@
|
|||
/**
|
||||
* Web Worker to load large amounts of data without locking up the UI.
|
||||
*
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2017
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
self.port = null;
|
||||
self.id = null;
|
||||
|
||||
|
||||
self.handleMessage = function(e) {
|
||||
const r = e.data;
|
||||
log.debug(`LoaderWorker receiving command '${r.action}'`);
|
||||
|
||||
switch (r.action) {
|
||||
case "loadInput":
|
||||
self.loadFile(r.data.file, r.data.inputNum);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Respond to message from parent thread.
|
||||
*/
|
||||
self.addEventListener("message", function(e) {
|
||||
const r = e.data;
|
||||
if (r.hasOwnProperty("file") && (r.hasOwnProperty("inputNum"))) {
|
||||
self.loadFile(r.file, r.inputNum);
|
||||
} else if (r.hasOwnProperty("file")) {
|
||||
self.loadFile(r.file, "");
|
||||
} else if (r.hasOwnProperty("id")) {
|
||||
self.id = r.id;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Loads a file object into an ArrayBuffer, then transfers it back to the parent thread.
|
||||
*
|
||||
* @param {File} file
|
||||
* @param {string} inputNum
|
||||
*/
|
||||
self.loadFile = function(file, inputNum) {
|
||||
const reader = new FileReader();
|
||||
if (file.size >= 256*256*256*128) {
|
||||
self.postMessage({"error": "File size too large.", "inputNum": inputNum, "id": self.id});
|
||||
return;
|
||||
}
|
||||
const data = new Uint8Array(file.size);
|
||||
let offset = 0;
|
||||
const CHUNK_SIZE = 10485760; // 10MiB
|
||||
|
||||
const seek = function() {
|
||||
if (offset >= file.size) {
|
||||
self.postMessage({"fileBuffer": data.buffer, "inputNum": inputNum, "id": self.id}, [data.buffer]);
|
||||
return;
|
||||
}
|
||||
self.postMessage({"progress": Math.round(offset / file.size * 100), "inputNum": inputNum});
|
||||
const slice = file.slice(offset, offset + CHUNK_SIZE);
|
||||
reader.readAsArrayBuffer(slice);
|
||||
};
|
||||
|
||||
reader.onload = function(e) {
|
||||
data.set(new Uint8Array(reader.result), offset);
|
||||
offset += CHUNK_SIZE;
|
||||
seek();
|
||||
};
|
||||
|
||||
reader.onerror = function(e) {
|
||||
self.postMessage({"error": reader.error.message, "inputNum": inputNum, "id": self.id});
|
||||
};
|
||||
|
||||
seek();
|
||||
};
|
82
src/web/workers/ZipWorker.mjs
Normal file
82
src/web/workers/ZipWorker.mjs
Normal file
|
@ -0,0 +1,82 @@
|
|||
/**
|
||||
* Web Worker to handle zipping the outputs for download.
|
||||
*
|
||||
* @author j433866 [j433866@gmail.com]
|
||||
* @copyright Crown Copyright 2019
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import zip from "zlibjs/bin/zip.min";
|
||||
import Utils from "../../core/Utils";
|
||||
import {detectFileType} from "../../core/lib/FileType";
|
||||
|
||||
const Zlib = zip.Zlib;
|
||||
|
||||
/**
|
||||
* Respond to message from parent thread.
|
||||
*/
|
||||
self.addEventListener("message", function(e) {
|
||||
const r = e.data;
|
||||
if (!r.hasOwnProperty("outputs")) {
|
||||
log.error("No files were passed to the ZipWorker.");
|
||||
return;
|
||||
}
|
||||
if (!r.hasOwnProperty("filename")) {
|
||||
log.error("No filename was passed to the ZipWorker");
|
||||
return;
|
||||
}
|
||||
if (!r.hasOwnProperty("fileExtension")) {
|
||||
log.error("No file extension was passed to the ZipWorker");
|
||||
return;
|
||||
}
|
||||
|
||||
self.zipFiles(r.outputs, r.filename, r.fileExtension);
|
||||
});
|
||||
|
||||
self.setOption = function(...args) {};
|
||||
|
||||
/**
|
||||
* Compress the files into a zip file and send the zip back
|
||||
* to the OutputWaiter.
|
||||
*
|
||||
* @param {object} outputs
|
||||
* @param {string} filename
|
||||
* @param {string} fileExtension
|
||||
*/
|
||||
self.zipFiles = function(outputs, filename, fileExtension) {
|
||||
const zip = new Zlib.Zip();
|
||||
const inputNums = Object.keys(outputs);
|
||||
|
||||
for (let i = 0; i < inputNums.length; i++) {
|
||||
const iNum = inputNums[i];
|
||||
let ext = fileExtension;
|
||||
|
||||
let output;
|
||||
if (outputs[iNum].data === null) {
|
||||
output = new Uint8Array(0);
|
||||
} else if (typeof outputs[iNum].data.dish.value === "string") {
|
||||
output = new Uint8Array(Utils.strToArrayBuffer(outputs[iNum].data.dish.value));
|
||||
} else {
|
||||
output = new Uint8Array(outputs[iNum].data.dish.value);
|
||||
}
|
||||
|
||||
if (fileExtension === "") {
|
||||
// Detect automatically
|
||||
const types = detectFileType(output);
|
||||
if (!types.length) {
|
||||
ext = ".dat";
|
||||
} else {
|
||||
ext = `.${types[0].extension.split(",", 1)[0]}`;
|
||||
}
|
||||
}
|
||||
const name = Utils.strToByteArray(iNum + ext);
|
||||
|
||||
zip.addFile(output, {filename: name});
|
||||
}
|
||||
|
||||
const zippedFile = zip.compress();
|
||||
self.postMessage({
|
||||
zippedFile: zippedFile.buffer,
|
||||
filename: filename
|
||||
}, [zippedFile.buffer]);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue