Exporing options with API.

This commit is contained in:
d98762625 2018-04-13 12:14:40 +01:00
parent 30aa4e05ef
commit 59877b5138
5 changed files with 149 additions and 17 deletions

99
src/node/Wrapper.mjs Normal file
View file

@ -0,0 +1,99 @@
/**
* Wrap operations in a
*
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Dish from "../core/Dish";
import log from "loglevel";
/**
*
*/
export default class Wrapper {
/**
*
* @param arg
*/
extractArg(arg) {
if (arg.type === "option" || arg.type === "editableOption") {
return arg.value[0];
}
return arg.value;
}
/**
*
*/
wrap(operation) {
this.operation = new operation();
// This for just exposing run function:
// return this.run.bind(this);
/**
*
* @param input
* @param args
*/
const _run = async(input, args=null) => {
const dish = new Dish(input);
try {
dish.findType();
} catch (e) {
log.debug(e);
}
if (!args) {
args = this.operation.args.map(this.extractArg);
} else {
// Allows single arg ops to have arg defined not in array
if (!(args instanceof Array)) {
args = [args];
}
}
const transformedInput = await dish.get(Dish.typeEnum(this.operation.inputType));
return this.operation.innerRun(transformedInput, args);
};
// There's got to be a nicer way to do this!
this.operation.innerRun = this.operation.run;
this.operation.run = _run;
return this.operation;
}
/**
*
* @param input
*/
async run(input, args = null) {
const dish = new Dish(input);
try {
dish.findType();
} catch (e) {
log.debug(e);
}
if (!args) {
args = this.operation.args.map(this.extractArg);
} else {
// Allows single arg ops to have arg defined not in array
if (!(args instanceof Array)) {
args = [args];
}
}
const transformedInput = await dish.get(Dish.typeEnum(this.operation.inputType));
return this.operation.run(transformedInput, args);
}
}

View file

@ -18,22 +18,36 @@ global.ENVIRONMENT_IS_WEB = function() {
return typeof window === "object";
};
import Chef from "../core/Chef";
// import Chef from "../core/Chef";
const CyberChef = {
// const CyberChef = {
bake: function(input, recipeConfig) {
this.chef = new Chef();
return this.chef.bake(
input,
recipeConfig,
{},
0,
false
);
// bake: function(input, recipeConfig) {
// this.chef = new Chef();
// return this.chef.bake(
// input,
// recipeConfig,
// {},
// 0,
// false
// );
// }
// };
// export default CyberChef;
// export {CyberChef};
import Wrapper from "./Wrapper";
import * as operations from "../core/operations/index";
const cyberChef = {
base32: {
from: new Wrapper().wrap(operations.FromBase32),
to: new Wrapper().wrap(operations.ToBase32),
}
};
export default CyberChef;
export {CyberChef};
export default cyberChef;
export {cyberChef};