mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 14:56:19 -04:00
make chef.help emulate search function in UI. Didnt extract search logic from web as it is fairly entwined.
This commit is contained in:
parent
3466f4e452
commit
63a23e4dcc
5 changed files with 80 additions and 30 deletions
|
@ -174,8 +174,4 @@ class SyncDish extends Dish {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default SyncDish;
|
export default SyncDish;
|
||||||
|
|
|
@ -6,6 +6,9 @@
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*eslint no-console: ["off"] */
|
||||||
|
|
||||||
|
|
||||||
import Dish from "../core/Dish";
|
import Dish from "../core/Dish";
|
||||||
import SyncDish from "./SyncDish";
|
import SyncDish from "./SyncDish";
|
||||||
import NodeRecipe from "./NodeRecipe";
|
import NodeRecipe from "./NodeRecipe";
|
||||||
|
@ -199,29 +202,59 @@ export function wrap(OpClass) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @namespace Api
|
* @namespace Api
|
||||||
* @param {String} searchTerm - the name of the operation to get help for.
|
* help: Give information about operations matching the given search term,
|
||||||
|
* or inputted operation.
|
||||||
|
* @param {String || wrapped operation} input - the name of the operation to get help for.
|
||||||
* Case and whitespace are ignored in search.
|
* Case and whitespace are ignored in search.
|
||||||
* @returns {Object} Describe function matching searchTerm.
|
* @returns {Object[]} Config of matching operations.
|
||||||
*/
|
*/
|
||||||
export function help(searchTerm) {
|
export function help(input) {
|
||||||
let sanitised = false;
|
let searchTerm = false;
|
||||||
if (typeof searchTerm === "string") {
|
if (typeof input === "string") {
|
||||||
sanitised = searchTerm;
|
searchTerm = input;
|
||||||
} else if (typeof searchTerm === "function") {
|
} else if (typeof input === "function") {
|
||||||
sanitised = searchTerm.opName;
|
searchTerm = input.opName;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sanitised) {
|
if (!searchTerm) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const key = Object.keys(OperationConfig)
|
// Look for matches in operation name and description, listing name
|
||||||
.find(o => sanitise(o) === sanitise(sanitised));
|
// matches first.
|
||||||
if (key) {
|
const matches = Object.keys(OperationConfig)
|
||||||
const result = OperationConfig[key];
|
// hydrate operation: swap op name for op config object (with name)
|
||||||
result.name = key;
|
.map((m) => {
|
||||||
return result;
|
const hydrated = OperationConfig[m];
|
||||||
|
hydrated.name = m;
|
||||||
|
|
||||||
|
// Return hydrated along with what type of match it was
|
||||||
|
return {
|
||||||
|
hydrated,
|
||||||
|
nameMatch: sanitise(hydrated.name).includes(sanitise(searchTerm)),
|
||||||
|
descMatch: sanitise(hydrated.description).includes(sanitise(searchTerm))
|
||||||
|
};
|
||||||
|
})
|
||||||
|
// Filter out non-matches
|
||||||
|
.filter((result) => {
|
||||||
|
return result.nameMatch || result.descMatch;
|
||||||
|
})
|
||||||
|
// sort results with name match first
|
||||||
|
.sort((a, b) => {
|
||||||
|
const aInt = a.nameMatch ? 1 : 0;
|
||||||
|
const bInt = b.nameMatch ? 1 : 0;
|
||||||
|
return bInt - aInt;
|
||||||
|
})
|
||||||
|
// extract just the hydrated config
|
||||||
|
.map(result => result.hydrated);
|
||||||
|
|
||||||
|
// Concatenate matches but remove duplicates
|
||||||
|
if (matches && matches.length) {
|
||||||
|
console.log(`${matches.length} results found.`);
|
||||||
|
return matches;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("No results found.");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1045,7 +1045,7 @@ const operations = [
|
||||||
];
|
];
|
||||||
|
|
||||||
const prebaked = bake(operations);
|
const prebaked = bake(operations);
|
||||||
chef.bake = prebaked
|
chef.bake = prebaked;
|
||||||
export default chef;
|
export default chef;
|
||||||
|
|
||||||
// Operations as top level exports.
|
// Operations as top level exports.
|
||||||
|
|
|
@ -115,12 +115,12 @@ TestRegister.addApiTests([
|
||||||
|
|
||||||
it("chef.help: should describe a operation", () => {
|
it("chef.help: should describe a operation", () => {
|
||||||
const result = chef.help("tripleDESDecrypt");
|
const result = chef.help("tripleDESDecrypt");
|
||||||
assert.strictEqual(result.name, "Triple DES Decrypt");
|
assert.strictEqual(result[0].name, "Triple DES Decrypt");
|
||||||
assert.strictEqual(result.module, "Ciphers");
|
assert.strictEqual(result[0].module, "Ciphers");
|
||||||
assert.strictEqual(result.inputType, "string");
|
assert.strictEqual(result[0].inputType, "string");
|
||||||
assert.strictEqual(result.outputType, "string");
|
assert.strictEqual(result[0].outputType, "string");
|
||||||
assert.strictEqual(result.description, "Triple DES applies DES three times to each block to increase key size.<br><br><b>Key:</b> Triple DES uses a key length of 24 bytes (192 bits).<br>DES uses a key length of 8 bytes (64 bits).<br><br><b>IV:</b> The Initialization Vector should be 8 bytes long. If not entered, it will default to 8 null bytes.<br><br><b>Padding:</b> In CBC and ECB mode, PKCS#7 padding will be used.");
|
assert.strictEqual(result[0].description, "Triple DES applies DES three times to each block to increase key size.<br><br><b>Key:</b> Triple DES uses a key length of 24 bytes (192 bits).<br>DES uses a key length of 8 bytes (64 bits).<br><br><b>IV:</b> The Initialization Vector should be 8 bytes long. If not entered, it will default to 8 null bytes.<br><br><b>Padding:</b> In CBC and ECB mode, PKCS#7 padding will be used.");
|
||||||
assert.strictEqual(result.args.length, 5);
|
assert.strictEqual(result[0].args.length, 5);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("chef.help: null for invalid operation", () => {
|
it("chef.help: null for invalid operation", () => {
|
||||||
|
@ -130,8 +130,29 @@ TestRegister.addApiTests([
|
||||||
|
|
||||||
it("chef.help: takes a wrapped operation as input", () => {
|
it("chef.help: takes a wrapped operation as input", () => {
|
||||||
const result = chef.help(chef.toBase32);
|
const result = chef.help(chef.toBase32);
|
||||||
assert.strictEqual(result.name, "To Base32");
|
assert.strictEqual(result[0].name, "To Base32");
|
||||||
assert.strictEqual(result.module, "Default");
|
assert.strictEqual(result[0].module, "Default");
|
||||||
|
}),
|
||||||
|
|
||||||
|
it("chef.help: returns multiple results", () => {
|
||||||
|
const result = chef.help("base 64");
|
||||||
|
assert.strictEqual(result.length, 8);
|
||||||
|
}),
|
||||||
|
|
||||||
|
it("chef.help: looks in description for matches too", () => {
|
||||||
|
// string only in one operation's description.
|
||||||
|
const result = chef.help("Converts a unit of data to another format.");
|
||||||
|
assert.strictEqual(result.length, 1);
|
||||||
|
assert.strictEqual(result[0].name, "Convert data units");
|
||||||
|
}),
|
||||||
|
|
||||||
|
it("chef.help: lists name matches before desc matches", () => {
|
||||||
|
const result = chef.help("MD5");
|
||||||
|
assert.ok(result[0].name.includes("MD5"));
|
||||||
|
assert.strictEqual(result[1].name.includes("MD5"), false);
|
||||||
|
assert.strictEqual(result[2].name.includes("MD5"), false);
|
||||||
|
assert.ok(result[1].description.includes("MD5"));
|
||||||
|
assert.ok(result[2].description.includes("MD5"));
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("chef.bake: should exist", () => {
|
it("chef.bake: should exist", () => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue