mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-25 01:06:16 -04:00
Reviewed Input and Output Waiters and improved logging in workers
This commit is contained in:
parent
56d1a016da
commit
91f1be8c70
12 changed files with 204 additions and 111 deletions
|
@ -35,6 +35,14 @@ class BackgroundWorkerWaiter {
|
|||
log.debug("Registering new background ChefWorker");
|
||||
this.chefWorker = new ChefWorker();
|
||||
this.chefWorker.addEventListener("message", this.handleChefMessage.bind(this));
|
||||
this.chefWorker.postMessage({
|
||||
action: "setLogPrefix",
|
||||
data: "BGChefWorker"
|
||||
});
|
||||
this.chefWorker.postMessage({
|
||||
action: "setLogLevel",
|
||||
data: log.getLevel()
|
||||
});
|
||||
|
||||
let docURL = document.location.href.split(/[#?]/)[0];
|
||||
const index = docURL.lastIndexOf("/");
|
||||
|
@ -52,7 +60,7 @@ class BackgroundWorkerWaiter {
|
|||
*/
|
||||
handleChefMessage(e) {
|
||||
const r = e.data;
|
||||
log.debug("Receiving '" + r.action + "' from ChefWorker in the background");
|
||||
log.debug(`Receiving '${r.action}' from BGChefWorker`);
|
||||
|
||||
switch (r.action) {
|
||||
case "bakeComplete":
|
||||
|
@ -152,6 +160,18 @@ class BackgroundWorkerWaiter {
|
|||
this.manager.output.backgroundMagicResult(response.dish.value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the console log level in the workers.
|
||||
*/
|
||||
setLogLevel() {
|
||||
if (!this.chefWorker) return;
|
||||
this.chefWorker.postMessage({
|
||||
action: "setLogLevel",
|
||||
data: log.getLevel()
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -305,11 +305,20 @@ class InputWaiter {
|
|||
if (this.loaderWorkers.length === this.maxWorkers) {
|
||||
return -1;
|
||||
}
|
||||
log.debug("Adding new LoaderWorker.");
|
||||
log.debug(`Adding new LoaderWorker (${this.loaderWorkers.length + 1}/${this.maxWorkers}).`);
|
||||
const newWorker = new LoaderWorker();
|
||||
const workerId = this.workerId++;
|
||||
newWorker.addEventListener("message", this.handleLoaderMessage.bind(this));
|
||||
newWorker.postMessage({id: workerId});
|
||||
newWorker.postMessage({
|
||||
action: "setLogLevel",
|
||||
data: log.getLevel()
|
||||
});
|
||||
newWorker.postMessage({
|
||||
action: "setID",
|
||||
data: {
|
||||
id: workerId
|
||||
}
|
||||
});
|
||||
const newWorkerObj = {
|
||||
worker: newWorker,
|
||||
id: workerId
|
||||
|
@ -374,8 +383,11 @@ class InputWaiter {
|
|||
const idx = this.getLoaderWorkerIndex(inputData.workerId);
|
||||
if (idx === -1) return;
|
||||
this.loaderWorkers[idx].worker.postMessage({
|
||||
file: inputData.file,
|
||||
inputNum: inputData.inputNum
|
||||
action: "loadFile",
|
||||
data: {
|
||||
file: inputData.file,
|
||||
inputNum: inputData.inputNum
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -402,7 +414,7 @@ class InputWaiter {
|
|||
|
||||
|
||||
/**
|
||||
* Handler for messages sent back by the inputWorker
|
||||
* Handler for messages sent back by the InputWorker
|
||||
*
|
||||
* @param {MessageEvent} e
|
||||
*/
|
||||
|
@ -414,7 +426,7 @@ class InputWaiter {
|
|||
return;
|
||||
}
|
||||
|
||||
log.debug(`Receiving ${r.action} from InputWorker.`);
|
||||
log.debug(`Receiving '${r.action}' from InputWorker.`);
|
||||
|
||||
switch (r.action) {
|
||||
case "activateLoaderWorker":
|
||||
|
@ -802,7 +814,6 @@ class InputWaiter {
|
|||
else delay = 500;
|
||||
|
||||
debounce(function(e) {
|
||||
console.log("inputChange", inputLength, delay);
|
||||
const value = this.getInput();
|
||||
const activeTab = this.manager.tabs.getActiveTab("input");
|
||||
|
||||
|
@ -1092,11 +1103,16 @@ class InputWaiter {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the console log level in the worker.
|
||||
*
|
||||
* @param {string} level
|
||||
* Sets the console log level in the workers.
|
||||
*/
|
||||
setLogLevel(level) {
|
||||
setLogLevel() {
|
||||
this.loaderWorkers.forEach(w => {
|
||||
w.postMessage({
|
||||
action: "setLogLevel",
|
||||
data: log.getLevel()
|
||||
});
|
||||
});
|
||||
|
||||
if (!this.inputWorker) return;
|
||||
this.inputWorker.postMessage({
|
||||
action: "setLogLevel",
|
||||
|
|
|
@ -180,6 +180,8 @@ class OptionsWaiter {
|
|||
log.setLevel(level, false);
|
||||
this.manager.worker.setLogLevel();
|
||||
this.manager.input.setLogLevel();
|
||||
this.manager.output.setLogLevel();
|
||||
this.manager.background.setLogLevel();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -146,13 +146,11 @@ class OutputWaiter {
|
|||
* @param {string} eolVal
|
||||
*/
|
||||
eolChange(eolVal) {
|
||||
const oldOutputVal = this.getOutput();
|
||||
|
||||
const currentTabNum = this.manager.tabs.getActiveTab("output");
|
||||
if (currentTabNum >= 0) {
|
||||
this.outputs[currentTabNum].eolSequence = eolVal;
|
||||
} else {
|
||||
throw new Error("Cannot change output eol sequence to " + eolVal);
|
||||
throw new Error(`Cannot change output ${currentTabNum} EOL sequence to ${eolVal}`);
|
||||
}
|
||||
|
||||
// Update the EOL value
|
||||
|
@ -161,7 +159,7 @@ class OutputWaiter {
|
|||
});
|
||||
|
||||
// Reset the output so that lines are recalculated, preserving the old EOL values
|
||||
this.setOutput(oldOutputVal, true);
|
||||
this.setOutput(this.currentOutputCache, true);
|
||||
// Update the URL manually since we aren't firing a statechange event
|
||||
this.app.updateURL(true);
|
||||
}
|
||||
|
@ -191,7 +189,7 @@ class OutputWaiter {
|
|||
if (currentTabNum >= 0) {
|
||||
this.outputs[currentTabNum].encoding = chrEncVal;
|
||||
} else {
|
||||
throw new Error("Cannot change output chrEnc to " + chrEncVal);
|
||||
throw new Error(`Cannot change output ${currentTabNum} chrEnc to ${chrEncVal}`);
|
||||
}
|
||||
|
||||
// Reset the output, forcing it to re-decode the data with the new character encoding
|
||||
|
@ -224,16 +222,6 @@ class OutputWaiter {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the current output
|
||||
* @returns {string}
|
||||
*/
|
||||
getOutput() {
|
||||
const doc = this.outputEditorView.state.doc;
|
||||
const eol = this.outputEditorView.state.lineBreak;
|
||||
return doc.sliceString(0, doc.length, eol);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the current output
|
||||
* @param {string|ArrayBuffer} data
|
||||
|
@ -498,7 +486,7 @@ class OutputWaiter {
|
|||
removeOutput(inputNum) {
|
||||
if (!this.outputExists(inputNum)) return;
|
||||
|
||||
delete (this.outputs[inputNum]);
|
||||
delete this.outputs[inputNum];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -600,7 +588,7 @@ class OutputWaiter {
|
|||
}
|
||||
|
||||
/**
|
||||
* Retrieves the dish as a string, returning the cached version if possible.
|
||||
* Retrieves the dish as a string
|
||||
*
|
||||
* @param {Dish} dish
|
||||
* @returns {string}
|
||||
|
@ -614,7 +602,7 @@ class OutputWaiter {
|
|||
}
|
||||
|
||||
/**
|
||||
* Retrieves the dish as an ArrayBuffer, returning the cached version if possible.
|
||||
* Retrieves the dish as an ArrayBuffer
|
||||
*
|
||||
* @param {Dish} dish
|
||||
* @returns {ArrayBuffer}
|
||||
|
@ -719,12 +707,12 @@ class OutputWaiter {
|
|||
|
||||
const data = await dish.get(Dish.ARRAY_BUFFER),
|
||||
file = new File([data], fileName);
|
||||
FileSaver.saveAs(file, fileName, false);
|
||||
FileSaver.saveAs(file, fileName, {autoBom: false});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for save all click event
|
||||
* Saves all outputs to a single archvie file
|
||||
* Saves all outputs to a single archive file
|
||||
*/
|
||||
async saveAllClick() {
|
||||
const downloadButton = document.getElementById("save-all-to-file");
|
||||
|
@ -745,7 +733,6 @@ class OutputWaiter {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Spawns a new ZipWorker and sends it the outputs so that they can
|
||||
* be zipped for download
|
||||
|
@ -801,9 +788,16 @@ class OutputWaiter {
|
|||
log.debug("Creating ZipWorker");
|
||||
this.zipWorker = new ZipWorker();
|
||||
this.zipWorker.postMessage({
|
||||
outputs: this.outputs,
|
||||
filename: fileName,
|
||||
fileExtension: fileExt
|
||||
action: "setLogLevel",
|
||||
data: log.getLevel()
|
||||
});
|
||||
this.zipWorker.postMessage({
|
||||
action: "zipFiles",
|
||||
data: {
|
||||
outputs: this.outputs,
|
||||
filename: fileName,
|
||||
fileExtension: fileExt
|
||||
}
|
||||
});
|
||||
this.zipWorker.addEventListener("message", this.handleZipWorkerMessage.bind(this));
|
||||
}
|
||||
|
@ -820,16 +814,12 @@ class OutputWaiter {
|
|||
this.zipWorker = null;
|
||||
|
||||
const downloadButton = document.getElementById("save-all-to-file");
|
||||
|
||||
downloadButton.classList.remove("spin");
|
||||
downloadButton.title = "Save all outputs to a zip file";
|
||||
downloadButton.setAttribute("data-original-title", "Save all outputs to a zip file");
|
||||
|
||||
downloadButton.firstElementChild.innerHTML = "archive";
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handle messages sent back by the ZipWorker
|
||||
*/
|
||||
|
@ -847,7 +837,7 @@ class OutputWaiter {
|
|||
}
|
||||
|
||||
const file = new File([r.zippedFile], r.filename);
|
||||
FileSaver.saveAs(file, r.filename, false);
|
||||
FileSaver.saveAs(file, r.filename, {autoBom: false});
|
||||
|
||||
this.terminateZipWorker();
|
||||
}
|
||||
|
@ -1050,9 +1040,7 @@ class OutputWaiter {
|
|||
nums.push(newNum);
|
||||
}
|
||||
}
|
||||
nums.sort(function(a, b) {
|
||||
return a - b;
|
||||
});
|
||||
nums.sort((a, b) => a - b); // Forces the sort function to treat a and b as numbers
|
||||
return nums;
|
||||
}
|
||||
|
||||
|
@ -1139,6 +1127,7 @@ class OutputWaiter {
|
|||
|
||||
/**
|
||||
* Redraw the entire tab bar to remove any outdated tabs
|
||||
*
|
||||
* @param {number} activeTab
|
||||
* @param {string} direction - Either "left" or "right"
|
||||
*/
|
||||
|
@ -1152,7 +1141,6 @@ class OutputWaiter {
|
|||
for (let i = 0; i < newNums.length; i++) {
|
||||
this.displayTabInfo(newNums[i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1297,7 +1285,7 @@ class OutputWaiter {
|
|||
return;
|
||||
}
|
||||
|
||||
const output = await dish.get(Dish.STRING);
|
||||
const output = await this.getDishStr(dish);
|
||||
const self = this;
|
||||
|
||||
navigator.clipboard.writeText(output).then(function() {
|
||||
|
@ -1313,8 +1301,8 @@ class OutputWaiter {
|
|||
*/
|
||||
async switchClick() {
|
||||
const activeTab = this.manager.tabs.getActiveTab("output");
|
||||
|
||||
const switchButton = document.getElementById("switch");
|
||||
|
||||
switchButton.classList.add("spin");
|
||||
switchButton.disabled = true;
|
||||
switchButton.firstElementChild.innerHTML = "autorenew";
|
||||
|
@ -1480,6 +1468,18 @@ class OutputWaiter {
|
|||
$("#output-tab-modal").modal("hide");
|
||||
this.changeTab(inputNum, this.app.options.syncTabs);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the console log level in the workers.
|
||||
*/
|
||||
setLogLevel() {
|
||||
if (!this.zipWorker) return;
|
||||
this.zipWorker.postMessage({
|
||||
action: "setLogLevel",
|
||||
data: log.getLevel()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default OutputWaiter;
|
||||
|
|
|
@ -72,6 +72,10 @@ class WorkerWaiter {
|
|||
|
||||
this.dishWorker.worker = new DishWorker();
|
||||
this.dishWorker.worker.addEventListener("message", this.handleDishMessage.bind(this));
|
||||
this.dishWorker.worker.postMessage({
|
||||
action: "setLogLevel",
|
||||
data: log.getLevel()
|
||||
});
|
||||
|
||||
if (this.dishWorkerQueue.length > 0) {
|
||||
this.postDishMessage(this.dishWorkerQueue.splice(0, 1)[0]);
|
||||
|
@ -89,22 +93,27 @@ class WorkerWaiter {
|
|||
return -1;
|
||||
}
|
||||
|
||||
log.debug("Adding new ChefWorker");
|
||||
log.debug(`Adding new ChefWorker (${this.chefWorkers.length + 1}/${this.maxWorkers})`);
|
||||
|
||||
// Create a new ChefWorker and send it the docURL
|
||||
const newWorker = new ChefWorker();
|
||||
newWorker.addEventListener("message", this.handleChefMessage.bind(this));
|
||||
newWorker.postMessage({
|
||||
action: "setLogPrefix",
|
||||
data: "ChefWorker"
|
||||
});
|
||||
newWorker.postMessage({
|
||||
action: "setLogLevel",
|
||||
data: log.getLevel()
|
||||
});
|
||||
|
||||
let docURL = document.location.href.split(/[#?]/)[0];
|
||||
const index = docURL.lastIndexOf("/");
|
||||
if (index > 0) {
|
||||
docURL = docURL.substring(0, index);
|
||||
}
|
||||
|
||||
newWorker.postMessage({"action": "docURL", "data": docURL});
|
||||
newWorker.postMessage({
|
||||
action: "setLogLevel",
|
||||
data: log.getLevel()
|
||||
});
|
||||
|
||||
|
||||
// Store the worker, whether or not it's active, and the inputNum as an object
|
||||
const newWorkerObj = {
|
||||
|
@ -177,7 +186,7 @@ class WorkerWaiter {
|
|||
handleChefMessage(e) {
|
||||
const r = e.data;
|
||||
let inputNum = 0;
|
||||
log.debug(`Receiving ${r.action} from ChefWorker.`);
|
||||
log.debug(`Receiving '${r.action}' from ChefWorker.`);
|
||||
|
||||
if (Object.prototype.hasOwnProperty.call(r.data, "inputNum")) {
|
||||
inputNum = r.data.inputNum;
|
||||
|
@ -626,7 +635,7 @@ class WorkerWaiter {
|
|||
*/
|
||||
handleDishMessage(e) {
|
||||
const r = e.data;
|
||||
log.debug(`Receiving ${r.action} from DishWorker`);
|
||||
log.debug(`Receiving '${r.action}' from DishWorker`);
|
||||
|
||||
switch (r.action) {
|
||||
case "dishReturned":
|
||||
|
@ -729,12 +738,18 @@ class WorkerWaiter {
|
|||
* Sets the console log level in the workers.
|
||||
*/
|
||||
setLogLevel() {
|
||||
for (let i = 0; i < this.chefWorkers.length; i++) {
|
||||
this.chefWorkers[i].worker.postMessage({
|
||||
this.chefWorkers.forEach(w => {
|
||||
w.postMessage({
|
||||
action: "setLogLevel",
|
||||
data: log.getLevel()
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (!this.dishWorker.worker) return;
|
||||
this.dishWorker.worker.postMessage({
|
||||
action: "setLogLevel",
|
||||
data: log.getLevel()
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue