mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-14 01:56:54 -04:00
Initial commit
This commit is contained in:
commit
b1d73a725d
238 changed files with 105357 additions and 0 deletions
120
src/js/core/Chef.js
Executable file
120
src/js/core/Chef.js
Executable file
|
@ -0,0 +1,120 @@
|
|||
/**
|
||||
* The main controller for CyberChef.
|
||||
*
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*
|
||||
* @class
|
||||
*/
|
||||
var Chef = function() {
|
||||
this.dish = new Dish();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Runs the recipe over the input.
|
||||
*
|
||||
* @param {string} input_text - The input data as a string
|
||||
* @param {Object[]} recipe_config - The recipe configuration object
|
||||
* @param {Object} options - The options object storing various user choices
|
||||
* @param {boolean} options.attemp_highlight - Whether or not to attempt highlighting
|
||||
* @param {number} progress - The position in the recipe to start from
|
||||
* @param {number} [step] - The number of operations to execute
|
||||
*
|
||||
* @returns {Object} response
|
||||
* @returns {string} response.result - The output of the recipe
|
||||
* @returns {string} response.type - The data type of the result
|
||||
* @returns {number} response.progress - The position that we have got to in the recipe
|
||||
* @returns {number} response.options - The app options object (which may have been changed)
|
||||
* @returns {number} response.duration - The number of ms it took to execute the recipe
|
||||
* @returns {number} response.error - The error object thrown by a failed operation (false if no error)
|
||||
*/
|
||||
Chef.prototype.bake = function(input_text, recipe_config, options, progress, step) {
|
||||
var start_time = new Date().getTime(),
|
||||
recipe = new Recipe(recipe_config),
|
||||
contains_fc = recipe.contains_flow_control(),
|
||||
error = false;
|
||||
|
||||
// Reset attempt_highlight flag
|
||||
if (options.hasOwnProperty("attempt_highlight")) {
|
||||
options.attempt_highlight = true;
|
||||
}
|
||||
|
||||
if (contains_fc) options.attempt_highlight = false;
|
||||
|
||||
// Clean up progress
|
||||
if (progress >= recipe_config.length) {
|
||||
progress = 0;
|
||||
}
|
||||
|
||||
if (step) {
|
||||
// Unset breakpoint on this step
|
||||
recipe.set_breakpoint(progress, false);
|
||||
// Set breakpoint on next step
|
||||
recipe.set_breakpoint(progress + 1, true);
|
||||
}
|
||||
|
||||
// If stepping with flow control, we have to start from the beginning
|
||||
// but still want to skip all previous breakpoints
|
||||
if (progress > 0 && contains_fc) {
|
||||
recipe.remove_breaks_up_to(progress);
|
||||
progress = 0;
|
||||
}
|
||||
|
||||
// If starting from scratch, load data
|
||||
if (progress === 0) {
|
||||
this.dish.set(input_text, Dish.STRING);
|
||||
}
|
||||
|
||||
try {
|
||||
progress = recipe.execute(this.dish, progress);
|
||||
} catch (err) {
|
||||
// We can't throw the error from here as we will return in the finally block and ignore it
|
||||
// so we return the error in the result instead.
|
||||
error = err;
|
||||
progress = err.progress;
|
||||
} finally {
|
||||
return {
|
||||
result: this.dish.type == Dish.HTML ?
|
||||
this.dish.get(Dish.HTML) :
|
||||
this.dish.get(Dish.STRING),
|
||||
type: Dish.enum_lookup(this.dish.type),
|
||||
progress: progress,
|
||||
options: options,
|
||||
duration: new Date().getTime() - start_time,
|
||||
error: error
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* When a browser tab is unfocused and the browser has to run lots of dynamic content in other tabs,
|
||||
* it swaps out the memory for that tab. If the CyberChef tab has been unfocused for more than a
|
||||
* minute, we run a silent bake which will force the browser to load and cache all the relevant
|
||||
* JavaScript code needed to do a real bake.
|
||||
*
|
||||
* This will stop baking taking a long time when the CyberChef browser tab has been unfocused for a
|
||||
* long time and the browser has swapped out all its memory.
|
||||
*
|
||||
* The output will not be modified (hence "silent" bake).
|
||||
*
|
||||
* This will only actually execute the recipe if auto-bake is enabled, otherwise it will just load
|
||||
* the recipe, ingredients and dish.
|
||||
*
|
||||
* @param {Object[]} recipe_config - The recipe configuration object
|
||||
* @returns {number} The time it took to run the silent bake in milliseconds.
|
||||
*/
|
||||
Chef.prototype.silent_bake = function(recipe_config) {
|
||||
var start_time = new Date().getTime(),
|
||||
recipe = new Recipe(recipe_config),
|
||||
dish = new Dish("", Dish.STRING);
|
||||
|
||||
try {
|
||||
recipe.execute(dish);
|
||||
} catch(err) {
|
||||
// Suppress all errors
|
||||
}
|
||||
return new Date().getTime() - start_time;
|
||||
};
|
202
src/js/core/Dish.js
Executable file
202
src/js/core/Dish.js
Executable file
|
@ -0,0 +1,202 @@
|
|||
/**
|
||||
* The data being operated on by each operation.
|
||||
*
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*
|
||||
* @class
|
||||
* @param {byte_array|string|number} value - The value of the input data.
|
||||
* @param {number} type - The data type of value, see Dish enums.
|
||||
*/
|
||||
var Dish = function(value, type) {
|
||||
this.value = value || typeof value == "string" ? value : null;
|
||||
this.type = type || Dish.BYTE_ARRAY;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Dish data type enum for byte arrays.
|
||||
* @readonly
|
||||
* @enum
|
||||
*/
|
||||
Dish.BYTE_ARRAY = 0;
|
||||
/**
|
||||
* Dish data type enum for strings.
|
||||
* @readonly
|
||||
* @enum
|
||||
*/
|
||||
Dish.STRING = 1;
|
||||
/**
|
||||
* Dish data type enum for numbers.
|
||||
* @readonly
|
||||
* @enum
|
||||
*/
|
||||
Dish.NUMBER = 2;
|
||||
/**
|
||||
* Dish data type enum for HTML.
|
||||
* @readonly
|
||||
* @enum
|
||||
*/
|
||||
Dish.HTML = 3;
|
||||
|
||||
|
||||
/**
|
||||
* Returns the data type enum for the given type string.
|
||||
*
|
||||
* @static
|
||||
* @param {string} type_str - The name of the data type.
|
||||
* @returns {number} The data type enum value.
|
||||
*/
|
||||
Dish.type_enum = function(type_str) {
|
||||
switch (type_str) {
|
||||
case "byte_array":
|
||||
case "Byte array":
|
||||
return Dish.BYTE_ARRAY;
|
||||
case "string":
|
||||
case "String":
|
||||
return Dish.STRING;
|
||||
case "number":
|
||||
case "Number":
|
||||
return Dish.NUMBER;
|
||||
case "html":
|
||||
case "HTML":
|
||||
return Dish.HTML;
|
||||
default:
|
||||
throw "Invalid data type string. No matching enum.";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns the data type string for the given type enum.
|
||||
*
|
||||
* @static
|
||||
* @param {string} type_enum - The enum value of the data type.
|
||||
* @returns {number} The data type as a string.
|
||||
*/
|
||||
Dish.enum_lookup = function(type_enum) {
|
||||
switch (type_enum) {
|
||||
case Dish.BYTE_ARRAY:
|
||||
return "byte_array";
|
||||
case Dish.STRING:
|
||||
return "string";
|
||||
case Dish.NUMBER:
|
||||
return "number";
|
||||
case Dish.HTML:
|
||||
return "html";
|
||||
default:
|
||||
throw "Invalid data type enum. No matching type.";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Sets the data value and type and then validates them.
|
||||
*
|
||||
* @param {byte_array|string|number} value - The value of the input data.
|
||||
* @param {number} type - The data type of value, see Dish enums.
|
||||
*/
|
||||
Dish.prototype.set = function(value, type) {
|
||||
this.value = value;
|
||||
this.type = type;
|
||||
|
||||
if (!this.valid()) {
|
||||
var sample = Utils.truncate(JSON.stringify(this.value), 13);
|
||||
throw "Data is not a valid " + Dish.enum_lookup(type) + ": " + sample;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value of the data in the type format specified.
|
||||
*
|
||||
* @param {number} type - The data type of value, see Dish enums.
|
||||
* @returns {byte_array|string|number} The value of the output data.
|
||||
*/
|
||||
Dish.prototype.get = function(type) {
|
||||
if (this.type != type) {
|
||||
this.translate(type);
|
||||
}
|
||||
return this.value;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Translates the data to the given type format.
|
||||
*
|
||||
* @param {number} to_type - The data type of value, see Dish enums.
|
||||
*/
|
||||
Dish.prototype.translate = function(to_type) {
|
||||
// Convert data to intermediate byte_array type
|
||||
switch (this.type) {
|
||||
case Dish.STRING:
|
||||
this.value = this.value ? Utils.str_to_byte_array(this.value) : [];
|
||||
this.type = Dish.BYTE_ARRAY;
|
||||
break;
|
||||
case Dish.NUMBER:
|
||||
this.value = typeof this.value == "number" ? Utils.str_to_byte_array(this.value.toString()) : [];
|
||||
this.type = Dish.BYTE_ARRAY;
|
||||
break;
|
||||
case Dish.HTML:
|
||||
this.value = this.value ? Utils.str_to_byte_array(Utils.strip_html_tags(this.value, true)) : [];
|
||||
this.type = Dish.BYTE_ARRAY;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Convert from byte_array to to_type
|
||||
switch (to_type) {
|
||||
case Dish.STRING:
|
||||
case Dish.HTML:
|
||||
this.value = this.value ? Utils.byte_array_to_utf8(this.value) : "";
|
||||
this.type = Dish.STRING;
|
||||
break;
|
||||
case Dish.NUMBER:
|
||||
this.value = this.value ? parseFloat(Utils.byte_array_to_utf8(this.value)) : 0;
|
||||
this.type = Dish.NUMBER;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Validates that the value is the type that has been specified.
|
||||
* May have to disable parts of BYTE_ARRAY validation if it effects performance.
|
||||
*
|
||||
* @returns {boolean} Whether the data is valid or not.
|
||||
*/
|
||||
Dish.prototype.valid = function() {
|
||||
switch (this.type) {
|
||||
case Dish.BYTE_ARRAY:
|
||||
if (!(this.value instanceof Array)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check that every value is a number between 0 - 255
|
||||
for (var i = 0; i < this.value.length; i++) {
|
||||
if (typeof this.value[i] != "number" ||
|
||||
this.value[i] < 0 ||
|
||||
this.value[i] > 255) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
case Dish.STRING:
|
||||
case Dish.HTML:
|
||||
if (typeof this.value == "string") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case Dish.NUMBER:
|
||||
if (typeof this.value == "number") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
};
|
171
src/js/core/FlowControl.js
Executable file
171
src/js/core/FlowControl.js
Executable file
|
@ -0,0 +1,171 @@
|
|||
/**
|
||||
* Flow Control operations.
|
||||
*
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*
|
||||
* @namespace
|
||||
*/
|
||||
var FlowControl = {
|
||||
|
||||
/**
|
||||
* @constant
|
||||
* @default
|
||||
*/
|
||||
FORK_DELIM: "\\n",
|
||||
/**
|
||||
* @constant
|
||||
* @default
|
||||
*/
|
||||
MERGE_DELIM: "\\n",
|
||||
|
||||
/**
|
||||
* Fork operation.
|
||||
*
|
||||
* @param {Object} state - The current state of the recipe.
|
||||
* @param {number} state.progress - The current position in the recipe.
|
||||
* @param {Dish} state.dish - The Dish being operated on.
|
||||
* @param {Operation[]} state.op_list - The list of operations in the recipe.
|
||||
* @returns {Object} The updated state of the recipe.
|
||||
*/
|
||||
run_fork: function(state) {
|
||||
var op_list = state.op_list,
|
||||
input_type = op_list[state.progress].input_type,
|
||||
output_type = op_list[state.progress].output_type,
|
||||
input = state.dish.get(input_type),
|
||||
ings = op_list[state.progress].get_ing_values(),
|
||||
split_delim = ings[0],
|
||||
merge_delim = ings[1],
|
||||
sub_op_list = [],
|
||||
inputs = [];
|
||||
|
||||
if (input)
|
||||
inputs = input.split(split_delim);
|
||||
|
||||
// Create sub_op_list for each tranche to operate on
|
||||
// (all remaining operations unless we encounter a Merge)
|
||||
for (var i = state.progress + 1; i < op_list.length; i++) {
|
||||
if (op_list[i].name == "Merge" && !op_list[i].is_disabled()) {
|
||||
break;
|
||||
} else {
|
||||
sub_op_list.push(op_list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
var recipe = new Recipe(),
|
||||
output = "",
|
||||
progress;
|
||||
|
||||
recipe.add_operations(sub_op_list);
|
||||
|
||||
// Run recipe over each tranche
|
||||
for (i = 0; i < inputs.length; i++) {
|
||||
var dish = new Dish(inputs[i], input_type);
|
||||
progress = recipe.execute(dish, 0);
|
||||
output += dish.get(output_type) + merge_delim;
|
||||
}
|
||||
|
||||
state.dish.set(output, output_type);
|
||||
state.progress += progress;
|
||||
return state;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Merge operation.
|
||||
*
|
||||
* @param {Object} state - The current state of the recipe.
|
||||
* @param {number} state.progress - The current position in the recipe.
|
||||
* @param {Dish} state.dish - The Dish being operated on.
|
||||
* @param {Operation[]} state.op_list - The list of operations in the recipe.
|
||||
* @returns {Object} The updated state of the recipe.
|
||||
*/
|
||||
run_merge: function(state) {
|
||||
// No need to actually do anything here. The fork operation will
|
||||
// merge when it sees this operation.
|
||||
return state;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* @constant
|
||||
* @default
|
||||
*/
|
||||
JUMP_NUM: 0,
|
||||
/**
|
||||
* @constant
|
||||
* @default
|
||||
*/
|
||||
MAX_JUMPS: 10,
|
||||
|
||||
/**
|
||||
* Jump operation.
|
||||
*
|
||||
* @param {Object} state - The current state of the recipe.
|
||||
* @param {number} state.progress - The current position in the recipe.
|
||||
* @param {Dish} state.dish - The Dish being operated on.
|
||||
* @param {Operation[]} state.op_list - The list of operations in the recipe.
|
||||
* @param {number} state.num_jumps - The number of jumps taken so far.
|
||||
* @returns {Object} The updated state of the recipe.
|
||||
*/
|
||||
run_jump: function(state) {
|
||||
var ings = state.op_list[state.progress].get_ing_values(),
|
||||
jump_num = ings[0],
|
||||
max_jumps = ings[1];
|
||||
|
||||
if (state.num_jumps >= max_jumps) {
|
||||
throw "Reached maximum jumps, sorry!";
|
||||
}
|
||||
|
||||
state.progress += jump_num;
|
||||
state.num_jumps++;
|
||||
return state;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Conditional Jump operation.
|
||||
*
|
||||
* @param {Object} state - The current state of the recipe.
|
||||
* @param {number} state.progress - The current position in the recipe.
|
||||
* @param {Dish} state.dish - The Dish being operated on.
|
||||
* @param {Operation[]} state.op_list - The list of operations in the recipe.
|
||||
* @param {number} state.num_jumps - The number of jumps taken so far.
|
||||
* @returns {Object} The updated state of the recipe.
|
||||
*/
|
||||
run_cond_jump: function(state) {
|
||||
var ings = state.op_list[state.progress].get_ing_values(),
|
||||
dish = state.dish,
|
||||
regex_str = ings[0],
|
||||
jump_num = ings[1],
|
||||
max_jumps = ings[2];
|
||||
|
||||
if (state.num_jumps >= max_jumps) {
|
||||
throw "Reached maximum jumps, sorry!";
|
||||
}
|
||||
|
||||
if (regex_str !== "" && dish.get(Dish.STRING).search(regex_str) > -1) {
|
||||
state.progress += jump_num;
|
||||
state.num_jumps++;
|
||||
}
|
||||
|
||||
return state;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Return operation.
|
||||
*
|
||||
* @param {Object} state - The current state of the recipe.
|
||||
* @param {number} state.progress - The current position in the recipe.
|
||||
* @param {Dish} state.dish - The Dish being operated on.
|
||||
* @param {Operation[]} state.op_list - The list of operations in the recipe.
|
||||
* @returns {Object} The updated state of the recipe.
|
||||
*/
|
||||
run_return: function(state) {
|
||||
state.progress = state.op_list.length;
|
||||
return state;
|
||||
},
|
||||
|
||||
};
|
86
src/js/core/Ingredient.js
Executable file
86
src/js/core/Ingredient.js
Executable file
|
@ -0,0 +1,86 @@
|
|||
/**
|
||||
* The arguments to operations.
|
||||
*
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*
|
||||
* @class
|
||||
* @param {Object} ingredient_config
|
||||
*/
|
||||
var Ingredient = function(ingredient_config) {
|
||||
this.name = "";
|
||||
this.type = "";
|
||||
this.value = null;
|
||||
|
||||
if (ingredient_config) {
|
||||
this._parse_config(ingredient_config);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Reads and parses the given config.
|
||||
*
|
||||
* @private
|
||||
* @param {Object} ingredient_config
|
||||
*/
|
||||
Ingredient.prototype._parse_config = function(ingredient_config) {
|
||||
this.name = ingredient_config.name;
|
||||
this.type = ingredient_config.type;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value of the Ingredient as it should be displayed in a recipe config.
|
||||
*
|
||||
* @returns {*}
|
||||
*/
|
||||
Ingredient.prototype.get_config = function() {
|
||||
return this.value;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Sets the value of the Ingredient.
|
||||
*
|
||||
* @param {*} value
|
||||
*/
|
||||
Ingredient.prototype.set_value = function(value) {
|
||||
this.value = Ingredient.prepare(value, this.type);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Most values will be strings when they are entered. This function converts them to the correct
|
||||
* type.
|
||||
*
|
||||
* @static
|
||||
* @param {*} data
|
||||
* @param {string} type - The name of the data type.
|
||||
*/
|
||||
Ingredient.prepare = function(data, type) {
|
||||
switch (type) {
|
||||
case "binary_string":
|
||||
case "binary_short_string":
|
||||
case "editable_option":
|
||||
return Utils.parse_escaped_chars(data);
|
||||
case "byte_array":
|
||||
if (typeof data == "string") {
|
||||
data = data.replace(/\s+/g, '');
|
||||
return Utils.hex_to_byte_array(data);
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
break;
|
||||
case "number":
|
||||
var number = parseFloat(data);
|
||||
if (isNaN(number)) {
|
||||
var sample = Utils.truncate(data.toString(), 10);
|
||||
throw "Invalid ingredient value. Not a number: " + sample;
|
||||
}
|
||||
return number;
|
||||
default:
|
||||
return data;
|
||||
}
|
||||
};
|
157
src/js/core/Operation.js
Executable file
157
src/js/core/Operation.js
Executable file
|
@ -0,0 +1,157 @@
|
|||
/**
|
||||
* The Operation specified by the user to be run.
|
||||
*
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*
|
||||
* @class
|
||||
* @param {string} operation_name
|
||||
* @param {Object} operation_config
|
||||
*/
|
||||
var Operation = function(operation_name, operation_config) {
|
||||
this.name = operation_name;
|
||||
this.description = "";
|
||||
this.input_type = -1;
|
||||
this.output_type = -1;
|
||||
this.run = null;
|
||||
this.highlight = null;
|
||||
this.highlight_reverse = null;
|
||||
this.breakpoint = false;
|
||||
this.disabled = false;
|
||||
this.ing_list = [];
|
||||
|
||||
if (operation_config) {
|
||||
this._parse_config(operation_config);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Reads and parses the given config.
|
||||
*
|
||||
* @private
|
||||
* @param {Object} operation_config
|
||||
*/
|
||||
Operation.prototype._parse_config = function(operation_config) {
|
||||
this.description = operation_config.description;
|
||||
this.input_type = Dish.type_enum(operation_config.input_type);
|
||||
this.output_type = Dish.type_enum(operation_config.output_type);
|
||||
this.run = operation_config.run;
|
||||
this.highlight = operation_config.highlight;
|
||||
this.highlight_reverse = operation_config.highlight_reverse;
|
||||
this.flow_control = operation_config.flow_control;
|
||||
|
||||
for (var a = 0; a < operation_config.args.length; a++) {
|
||||
var ingredient_config = operation_config.args[a];
|
||||
var ingredient = new Ingredient(ingredient_config);
|
||||
this.add_ingredient(ingredient);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value of the Operation as it should be displayed in a recipe config.
|
||||
*
|
||||
* @returns {Object}
|
||||
*/
|
||||
Operation.prototype.get_config = function() {
|
||||
var ingredient_config = [];
|
||||
|
||||
for (var o = 0; o < this.ing_list.length; o++) {
|
||||
ingredient_config.push(this.ing_list[o].get_config());
|
||||
}
|
||||
|
||||
var operation_config = {
|
||||
"op": this.name,
|
||||
"args": ingredient_config
|
||||
};
|
||||
|
||||
return operation_config;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Adds a new Ingredient to this Operation.
|
||||
*
|
||||
* @param {Ingredient} ingredient
|
||||
*/
|
||||
Operation.prototype.add_ingredient = function(ingredient) {
|
||||
this.ing_list.push(ingredient);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Set the Ingredient values for this Operation.
|
||||
*
|
||||
* @param {Object[]} ing_values
|
||||
*/
|
||||
Operation.prototype.set_ing_values = function(ing_values) {
|
||||
for (var i = 0; i < ing_values.length; i++) {
|
||||
this.ing_list[i].set_value(ing_values[i]);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get the Ingredient values for this Operation.
|
||||
*
|
||||
* @returns {Object[]}
|
||||
*/
|
||||
Operation.prototype.get_ing_values = function() {
|
||||
var ing_values = [];
|
||||
for (var i = 0; i < this.ing_list.length; i++) {
|
||||
ing_values.push(this.ing_list[i].value);
|
||||
}
|
||||
return ing_values;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Set whether this Operation has a breakpoint.
|
||||
*
|
||||
* @param {boolean} value
|
||||
*/
|
||||
Operation.prototype.set_breakpoint = function(value) {
|
||||
this.breakpoint = !!value;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if this Operation has a breakpoint set.
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
Operation.prototype.is_breakpoint = function() {
|
||||
return this.breakpoint;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Set whether this Operation is disabled.
|
||||
*
|
||||
* @param {boolean} value
|
||||
*/
|
||||
Operation.prototype.set_disabled = function(value) {
|
||||
this.disabled = !!value;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if this Operation is disabled.
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
Operation.prototype.is_disabled = function() {
|
||||
return this.disabled;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if this Operation is a flow control.
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
Operation.prototype.is_flow_control = function() {
|
||||
return this.flow_control;
|
||||
};
|
215
src/js/core/Recipe.js
Executable file
215
src/js/core/Recipe.js
Executable file
|
@ -0,0 +1,215 @@
|
|||
/**
|
||||
* The Recipe controls a list of Operations and the Dish they operate on.
|
||||
*
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*
|
||||
* @class
|
||||
* @param {Object} recipe_config
|
||||
*/
|
||||
var Recipe = function(recipe_config) {
|
||||
this.op_list = [];
|
||||
|
||||
if (recipe_config) {
|
||||
this._parse_config(recipe_config);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Reads and parses the given config.
|
||||
*
|
||||
* @private
|
||||
* @param {Object} recipe_config
|
||||
*/
|
||||
Recipe.prototype._parse_config = function(recipe_config) {
|
||||
for (var c = 0; c < recipe_config.length; c++) {
|
||||
var operation_name = recipe_config[c].op;
|
||||
var operation_config = OperationConfig[operation_name];
|
||||
var operation = new Operation(operation_name, operation_config);
|
||||
operation.set_ing_values(recipe_config[c].args);
|
||||
operation.set_breakpoint(recipe_config[c].breakpoint);
|
||||
operation.set_disabled(recipe_config[c].disabled);
|
||||
this.add_operation(operation);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value of the Recipe as it should be displayed in a recipe config.
|
||||
*
|
||||
* @returns {*}
|
||||
*/
|
||||
Recipe.prototype.get_config = function() {
|
||||
var recipe_config = [];
|
||||
|
||||
for (var o = 0; o < this.op_list.length; o++) {
|
||||
recipe_config.push(this.op_list[o].get_config());
|
||||
}
|
||||
|
||||
return recipe_config;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Adds a new Operation to this Recipe.
|
||||
*
|
||||
* @param {Operation} operation
|
||||
*/
|
||||
Recipe.prototype.add_operation = function(operation) {
|
||||
this.op_list.push(operation);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Adds a list of Operations to this Recipe.
|
||||
*
|
||||
* @param {Operation[]} operations
|
||||
*/
|
||||
Recipe.prototype.add_operations = function(operations) {
|
||||
this.op_list = this.op_list.concat(operations);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Set a breakpoint on a specified Operation.
|
||||
*
|
||||
* @param {number} position - The index of the Operation
|
||||
* @param {boolean} value
|
||||
*/
|
||||
Recipe.prototype.set_breakpoint = function(position, value) {
|
||||
try {
|
||||
this.op_list[position].set_breakpoint(value);
|
||||
} catch (err) {
|
||||
// Ignore index error
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Remove breakpoints on all Operations in the Recipe up to the specified position. Used by Flow
|
||||
* Control Fork operation.
|
||||
*
|
||||
* @param {number} pos
|
||||
*/
|
||||
Recipe.prototype.remove_breaks_up_to = function(pos) {
|
||||
for (var i = 0; i < pos; i++) {
|
||||
this.op_list[i].set_breakpoint(false);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if there is an Flow Control Operation in this Recipe.
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
Recipe.prototype.contains_flow_control = function() {
|
||||
for (var i = 0; i < this.op_list.length; i++) {
|
||||
if (this.op_list[i].is_flow_control()) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns the index of the last Operation index that will be executed, taking into account disabled
|
||||
* Operations and breakpoints.
|
||||
*
|
||||
* @param {number} [start_index=0] - The index to start searching from
|
||||
* @returns (number}
|
||||
*/
|
||||
Recipe.prototype.last_op_index = function(start_index) {
|
||||
var i = start_index + 1 || 0,
|
||||
op;
|
||||
|
||||
for (; i < this.op_list.length; i++) {
|
||||
op = this.op_list[i];
|
||||
if (op.is_disabled()) return i-1;
|
||||
if (op.is_breakpoint()) return i-1;
|
||||
}
|
||||
|
||||
return i-1;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Executes each operation in the recipe over the given Dish.
|
||||
*
|
||||
* @param {Dish} dish
|
||||
* @param {number} [start_from=0] - The index of the Operation to start executing from
|
||||
* @returns {number} - The final progress through the recipe
|
||||
*/
|
||||
Recipe.prototype.execute = function(dish, start_from) {
|
||||
start_from = start_from || 0;
|
||||
var op, input, output, num_jumps = 0;
|
||||
|
||||
for (var i = start_from; i < this.op_list.length; i++) {
|
||||
op = this.op_list[i];
|
||||
if (op.is_disabled()) {
|
||||
continue;
|
||||
}
|
||||
if (op.is_breakpoint()) {
|
||||
return i;
|
||||
}
|
||||
|
||||
try {
|
||||
input = dish.get(op.input_type);
|
||||
|
||||
if (op.is_flow_control()) {
|
||||
// Package up the current state
|
||||
var state = {
|
||||
"progress" : i,
|
||||
"dish" : dish,
|
||||
"op_list" : this.op_list,
|
||||
"num_jumps" : num_jumps
|
||||
};
|
||||
|
||||
state = op.run(state);
|
||||
i = state.progress;
|
||||
num_jumps = state.num_jumps;
|
||||
} else {
|
||||
output = op.run(input, op.get_ing_values());
|
||||
dish.set(output, op.output_type);
|
||||
}
|
||||
} catch (err) {
|
||||
var e = typeof err == "string" ? { message: err } : err;
|
||||
|
||||
e.progress = i;
|
||||
e.display_str = op.name + " - ";
|
||||
if (e.fileName) {
|
||||
e.display_str += e.name + " in " + e.fileName +
|
||||
" on line " + e.lineNumber +
|
||||
".<br><br>Message: " + e.message;
|
||||
} else {
|
||||
e.display_str += e.message;
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
return this.op_list.length;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns the recipe configuration in string format.
|
||||
*
|
||||
* @returns {string}
|
||||
*/
|
||||
Recipe.prototype.to_string = function() {
|
||||
return JSON.stringify(this.get_config());
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Creates a Recipe from a given configuration string.
|
||||
*
|
||||
* @param {string} recipe_str
|
||||
*/
|
||||
Recipe.prototype.from_string = function(recipe_str) {
|
||||
var recipe_config = JSON.parse(recipe_str);
|
||||
this._parse_config(recipe_config);
|
||||
};
|
1144
src/js/core/Utils.js
Executable file
1144
src/js/core/Utils.js
Executable file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue