mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-07 15:07:11 -04:00
Improve code in Recipe.execute with async/await
This commit is contained in:
parent
f25bfe96d7
commit
7d4ea26a74
1 changed files with 28 additions and 78 deletions
|
@ -145,10 +145,10 @@ Recipe.prototype.lastOpIndex = function(startIndex) {
|
||||||
* @param {number} [currentStep=0] - The index of the Operation to start executing from
|
* @param {number} [currentStep=0] - The index of the Operation to start executing from
|
||||||
* @returns {number} - The final progress through the recipe
|
* @returns {number} - The final progress through the recipe
|
||||||
*/
|
*/
|
||||||
Recipe.prototype.execute = function(dish, currentStep, state) {
|
Recipe.prototype.execute = async function(dish, currentStep, state) {
|
||||||
var recipe = this;
|
var recipe = this;
|
||||||
|
|
||||||
var formatErrMsg = function(err, step, op) {
|
let formatErrMsg = function(err, step, op) {
|
||||||
var e = typeof err == "string" ? { message: err } : err;
|
var e = typeof err == "string" ? { message: err } : err;
|
||||||
|
|
||||||
e.progress = step;
|
e.progress = step;
|
||||||
|
@ -163,90 +163,40 @@ Recipe.prototype.execute = function(dish, currentStep, state) {
|
||||||
return e;
|
return e;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Operations can be asynchronous so we have to return a Promise to a future value.
|
|
||||||
return new Promise(function(resolve, reject) {
|
|
||||||
// Helper function to clean up recursing to the next recipe step.
|
|
||||||
// It is a closure to avoid having to pass in resolve and reject.
|
|
||||||
var execRecipe = function(recipe, dish, step, state) {
|
|
||||||
return recipe.execute(dish, step, state)
|
|
||||||
.then(function(progress) {
|
|
||||||
resolve(progress);
|
|
||||||
})
|
|
||||||
.catch(function(err) {
|
|
||||||
// Pass back the error to the previous caller.
|
|
||||||
// We don't want to handle the error here as the current
|
|
||||||
// operation did not cause the error, and so it should
|
|
||||||
// not appear in the error message.
|
|
||||||
reject(err);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
currentStep = currentStep || 0;
|
currentStep = currentStep || 0;
|
||||||
|
|
||||||
if (currentStep >= recipe.opList.length) {
|
if (currentStep >= recipe.opList.length) {
|
||||||
resolve(currentStep);
|
return currentStep;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var op = recipe.opList[currentStep],
|
let op = recipe.opList[currentStep],
|
||||||
input = dish.get(op.inputType);
|
input = dish.get(op.inputType);
|
||||||
|
|
||||||
|
try {
|
||||||
if (op.isDisabled()) {
|
if (op.isDisabled()) {
|
||||||
// Skip to next operation
|
// Skip to next operation
|
||||||
var nextStep = currentStep + 1;
|
return recipe.execute(dish, currentStep + 1, state);
|
||||||
execRecipe(recipe, dish, nextStep, state);
|
|
||||||
} else if (op.isBreakpoint()) {
|
} else if (op.isBreakpoint()) {
|
||||||
// We are at a breakpoint, we shouldn't recurse to the next op.
|
// We are at a breakpoint, we shouldn't recurse to the next op.
|
||||||
resolve(currentStep);
|
return currentStep;
|
||||||
} else {
|
} else if (op.isFlowControl()) {
|
||||||
var operationResult;
|
|
||||||
|
|
||||||
// We must try/catch here because op.run can either return
|
|
||||||
// A) a value
|
|
||||||
// B) a promise
|
|
||||||
// Promise.resolve -> .catch will handle errors from promises
|
|
||||||
// try/catch will handle errors from values
|
|
||||||
try {
|
|
||||||
if (op.isFlowControl()) {
|
|
||||||
state = {
|
state = {
|
||||||
progress: currentStep,
|
progress: currentStep,
|
||||||
dish: dish,
|
dish: dish,
|
||||||
opList: recipe.opList,
|
opList: recipe.opList,
|
||||||
numJumps: (state && state.numJumps) || 0,
|
numJumps: (state && state.numJumps) || 0,
|
||||||
};
|
};
|
||||||
operationResult = op.run(state);
|
state = await op.run(state);
|
||||||
|
let progress = await recipe.execute(state.dish, state.progress + 1);
|
||||||
|
return progress;
|
||||||
} else {
|
} else {
|
||||||
operationResult = op.run(input, op.getIngValues());
|
let output = await op.run(input, op.getIngValues());
|
||||||
|
dish.set(output, op.outputType);
|
||||||
|
return recipe.execute(dish, currentStep + 1, state);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
reject(formatErrMsg(err, currentStep, op));
|
throw formatErrMsg(err, currentStep, op);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (op.isFlowControl()) {
|
|
||||||
Promise.resolve(operationResult)
|
|
||||||
.then(function(state) {
|
|
||||||
return recipe.execute(state.dish, state.progress + 1);
|
|
||||||
})
|
|
||||||
.then(function(progress) {
|
|
||||||
resolve(progress);
|
|
||||||
})
|
|
||||||
.catch(function(err) {
|
|
||||||
reject(formatErrMsg(err, currentStep, op));
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
Promise.resolve(operationResult)
|
|
||||||
.then(function(output) {
|
|
||||||
dish.set(output, op.outputType);
|
|
||||||
var nextStep = currentStep + 1;
|
|
||||||
execRecipe(recipe, dish, nextStep, state);
|
|
||||||
})
|
|
||||||
.catch(function(err) {
|
|
||||||
reject(formatErrMsg(err, currentStep, op));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue