mirror of
https://github.com/gchq/CyberChef
synced 2025-01-08 10:38:46 +00:00
WIP dynamically define async functions in Dish, only if needed
This commit is contained in:
parent
04b7f2fa8c
commit
573a292e16
3 changed files with 301 additions and 263 deletions
|
@ -10,157 +10,14 @@ import DishError from "./errors/DishError";
|
||||||
import BigNumber from "bignumber.js";
|
import BigNumber from "bignumber.js";
|
||||||
import log from "loglevel";
|
import log from "loglevel";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The data being operated on by each operation.
|
|
||||||
*/
|
|
||||||
class Dish {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dish constructor
|
|
||||||
*
|
|
||||||
* @param {Dish || *} [dishOrInput=null] - A dish to clone OR an object
|
|
||||||
* literal to make into a dish
|
|
||||||
* @param {Enum} [type=null] (optional) - A type to accompany object
|
|
||||||
* literal input
|
|
||||||
*/
|
|
||||||
constructor(dishOrInput=null, type = null) {
|
|
||||||
this.value = [];
|
|
||||||
this.type = Dish.BYTE_ARRAY;
|
|
||||||
|
|
||||||
// Case: dishOrInput is dish object
|
|
||||||
if (dishOrInput &&
|
|
||||||
dishOrInput.hasOwnProperty("value") &&
|
|
||||||
dishOrInput.hasOwnProperty("type")) {
|
|
||||||
this.set(dishOrInput.value, dishOrInput.type);
|
|
||||||
// input and type defined separately
|
|
||||||
} else if (dishOrInput && type) {
|
|
||||||
this.set(dishOrInput, type);
|
|
||||||
// No type declared, so infer it.
|
|
||||||
} else if (dishOrInput) {
|
|
||||||
const inferredType = Dish.typeEnum(dishOrInput.constructor.name);
|
|
||||||
this.set(dishOrInput, inferredType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the data type enum for the given type string.
|
|
||||||
*
|
|
||||||
* @param {string} typeStr - The name of the data type.
|
|
||||||
* @returns {number} The data type enum value.
|
|
||||||
*/
|
|
||||||
static typeEnum(typeStr) {
|
|
||||||
switch (typeStr.toLowerCase()) {
|
|
||||||
case "bytearray":
|
|
||||||
case "byte array":
|
|
||||||
return Dish.BYTE_ARRAY;
|
|
||||||
case "string":
|
|
||||||
return Dish.STRING;
|
|
||||||
case "number":
|
|
||||||
return Dish.NUMBER;
|
|
||||||
case "html":
|
|
||||||
return Dish.HTML;
|
|
||||||
case "arraybuffer":
|
|
||||||
case "array buffer":
|
|
||||||
return Dish.ARRAY_BUFFER;
|
|
||||||
case "bignumber":
|
|
||||||
case "big number":
|
|
||||||
return Dish.BIG_NUMBER;
|
|
||||||
case "json":
|
|
||||||
case "object": // object constructor name. To allow JSON input in node.
|
|
||||||
return Dish.JSON;
|
|
||||||
case "file":
|
|
||||||
return Dish.FILE;
|
|
||||||
case "list<file>":
|
|
||||||
return Dish.LIST_FILE;
|
|
||||||
default:
|
|
||||||
console.log(typeStr);
|
|
||||||
throw new DishError("Invalid data type string. No matching enum.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the data type string for the given type enum.
|
|
||||||
*
|
|
||||||
* @param {number} typeEnum - The enum value of the data type.
|
|
||||||
* @returns {string} The data type as a string.
|
|
||||||
*/
|
|
||||||
static enumLookup(typeEnum) {
|
|
||||||
switch (typeEnum) {
|
|
||||||
case Dish.BYTE_ARRAY:
|
|
||||||
return "byteArray";
|
|
||||||
case Dish.STRING:
|
|
||||||
return "string";
|
|
||||||
case Dish.NUMBER:
|
|
||||||
return "number";
|
|
||||||
case Dish.HTML:
|
|
||||||
return "html";
|
|
||||||
case Dish.ARRAY_BUFFER:
|
|
||||||
return "ArrayBuffer";
|
|
||||||
case Dish.BIG_NUMBER:
|
|
||||||
return "BigNumber";
|
|
||||||
case Dish.JSON:
|
|
||||||
return "JSON";
|
|
||||||
case Dish.FILE:
|
|
||||||
return "File";
|
|
||||||
case Dish.LIST_FILE:
|
|
||||||
return "List<File>";
|
|
||||||
default:
|
|
||||||
throw new DishError("Invalid data type enum. No matching type.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the data value and type and then validates them.
|
|
||||||
*
|
|
||||||
* @param {*} value
|
|
||||||
* - The value of the input data.
|
|
||||||
* @param {number} type
|
|
||||||
* - The data type of value, see Dish enums.
|
|
||||||
*/
|
|
||||||
set(value, type) {
|
|
||||||
if (typeof type === "string") {
|
|
||||||
type = Dish.typeEnum(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
log.debug("Dish type: " + Dish.enumLookup(type));
|
|
||||||
this.value = value;
|
|
||||||
this.type = type;
|
|
||||||
|
|
||||||
if (!this.valid()) {
|
|
||||||
const sample = Utils.truncate(JSON.stringify(this.value), 13);
|
|
||||||
throw new DishError(`Data is not a valid ${Dish.enumLookup(type)}: ${sample}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the value of the data in the type format specified.
|
|
||||||
*
|
|
||||||
* @param {number} type - The data type of value, see Dish enums.
|
|
||||||
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
|
|
||||||
* @returns {*} - The value of the output data.
|
|
||||||
*/
|
|
||||||
async get(type, notUTF8=false) {
|
|
||||||
if (typeof type === "string") {
|
|
||||||
type = Dish.typeEnum(type);
|
|
||||||
}
|
|
||||||
if (this.type !== type) {
|
|
||||||
await this._translate(type, notUTF8);
|
|
||||||
}
|
|
||||||
return this.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Translates the data to the given type format.
|
* Translates the data to the given type format.
|
||||||
*
|
*
|
||||||
* @param {number} toType - The data type of value, see Dish enums.
|
* @param {number} toType - The data type of value, see Dish enums.
|
||||||
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
|
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
|
||||||
*/
|
*/
|
||||||
async _translate(toType, notUTF8=false) {
|
async function _asyncTranslate(toType, notUTF8=false) {
|
||||||
log.debug(`Translating Dish from ${Dish.enumLookup(this.type)} to ${Dish.enumLookup(toType)}`);
|
log.debug(`Translating Dish from ${Dish.enumLookup(this.type)} to ${Dish.enumLookup(toType)}`);
|
||||||
const byteArrayToStr = notUTF8 ? Utils.byteArrayToChars : Utils.byteArrayToUtf8;
|
const byteArrayToStr = notUTF8 ? Utils.byteArrayToChars : Utils.byteArrayToUtf8;
|
||||||
|
|
||||||
|
@ -245,6 +102,268 @@ class Dish {
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new DishError(`Error translating from byteArray to ${Dish.enumLookup(toType)}: ${err}`);
|
throw new DishError(`Error translating from byteArray to ${Dish.enumLookup(toType)}: ${err}`);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translates the data to the given type format.
|
||||||
|
*
|
||||||
|
* @param {number} toType - The data type of value, see Dish enums.
|
||||||
|
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
|
||||||
|
*/
|
||||||
|
function _translate(toType, notUTF8=false) {
|
||||||
|
log.debug(`Translating Dish from ${Dish.enumLookup(this.type)} to ${Dish.enumLookup(toType)}`);
|
||||||
|
const byteArrayToStr = notUTF8 ? Utils.byteArrayToChars : Utils.byteArrayToUtf8;
|
||||||
|
|
||||||
|
// Convert data to intermediate byteArray type
|
||||||
|
try {
|
||||||
|
switch (this.type) {
|
||||||
|
case Dish.STRING:
|
||||||
|
this.value = this.value ? Utils.strToByteArray(this.value) : [];
|
||||||
|
break;
|
||||||
|
case Dish.NUMBER:
|
||||||
|
this.value = typeof this.value === "number" ? Utils.strToByteArray(this.value.toString()) : [];
|
||||||
|
break;
|
||||||
|
case Dish.HTML:
|
||||||
|
this.value = this.value ? Utils.strToByteArray(Utils.unescapeHtml(Utils.stripHtmlTags(this.value, true))) : [];
|
||||||
|
break;
|
||||||
|
case Dish.ARRAY_BUFFER:
|
||||||
|
// Array.from() would be nicer here, but it's slightly slower
|
||||||
|
this.value = Array.prototype.slice.call(new Uint8Array(this.value));
|
||||||
|
break;
|
||||||
|
case Dish.BIG_NUMBER:
|
||||||
|
this.value = BigNumber.isBigNumber(this.value) ? Utils.strToByteArray(this.value.toFixed()) : [];
|
||||||
|
break;
|
||||||
|
case Dish.JSON:
|
||||||
|
this.value = this.value ? Utils.strToByteArray(JSON.stringify(this.value, null, 4)) : [];
|
||||||
|
break;
|
||||||
|
case Dish.FILE:
|
||||||
|
this.value = Utils.readFileSync(this.value);
|
||||||
|
this.value = Array.prototype.slice.call(this.value);
|
||||||
|
break;
|
||||||
|
case Dish.LIST_FILE:
|
||||||
|
this.value = this.value.map(f => Utils.readFileSync(f));
|
||||||
|
this.value = this.value.map(b => Array.prototype.slice.call(b));
|
||||||
|
this.value = [].concat.apply([], this.value);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw new DishError(`Error translating from ${Dish.enumLookup(this.type)} to byteArray: ${err}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.type = Dish.BYTE_ARRAY;
|
||||||
|
|
||||||
|
// Convert from byteArray to toType
|
||||||
|
try {
|
||||||
|
switch (toType) {
|
||||||
|
case Dish.STRING:
|
||||||
|
case Dish.HTML:
|
||||||
|
this.value = this.value ? byteArrayToStr(this.value) : "";
|
||||||
|
this.type = Dish.STRING;
|
||||||
|
break;
|
||||||
|
case Dish.NUMBER:
|
||||||
|
this.value = this.value ? parseFloat(byteArrayToStr(this.value)) : 0;
|
||||||
|
this.type = Dish.NUMBER;
|
||||||
|
break;
|
||||||
|
case Dish.ARRAY_BUFFER:
|
||||||
|
this.value = new Uint8Array(this.value).buffer;
|
||||||
|
this.type = Dish.ARRAY_BUFFER;
|
||||||
|
break;
|
||||||
|
case Dish.BIG_NUMBER:
|
||||||
|
try {
|
||||||
|
this.value = new BigNumber(byteArrayToStr(this.value));
|
||||||
|
} catch (err) {
|
||||||
|
this.value = new BigNumber(NaN);
|
||||||
|
}
|
||||||
|
this.type = Dish.BIG_NUMBER;
|
||||||
|
break;
|
||||||
|
case Dish.JSON:
|
||||||
|
this.value = JSON.parse(byteArrayToStr(this.value));
|
||||||
|
this.type = Dish.JSON;
|
||||||
|
break;
|
||||||
|
case Dish.FILE:
|
||||||
|
this.value = new File(this.value, "unknown");
|
||||||
|
break;
|
||||||
|
case Dish.LIST_FILE:
|
||||||
|
this.value = [new File(this.value, "unknown")];
|
||||||
|
this.type = Dish.LIST_FILE;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw new DishError(`Error translating from byteArray to ${Dish.enumLookup(toType)}: ${err}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value of the data in the type format specified.
|
||||||
|
*
|
||||||
|
* @param {number} type - The data type of value, see Dish enums.
|
||||||
|
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
|
||||||
|
* @returns {*} - The value of the output data.
|
||||||
|
*/
|
||||||
|
async function asyncGet(type, notUTF8=false) {
|
||||||
|
if (typeof type === "string") {
|
||||||
|
type = Dish.typeEnum(type);
|
||||||
|
}
|
||||||
|
if (this.type !== type) {
|
||||||
|
await this._translate(type, notUTF8);
|
||||||
|
}
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value of the data in the type format specified.
|
||||||
|
*
|
||||||
|
* @param {number} type - The data type of value, see Dish enums.
|
||||||
|
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
|
||||||
|
* @returns {*} - The value of the output data.
|
||||||
|
*/
|
||||||
|
function get(type, notUTF8=false) {
|
||||||
|
if (typeof type === "string") {
|
||||||
|
type = Dish.typeEnum(type);
|
||||||
|
}
|
||||||
|
if (this.type !== type) {
|
||||||
|
this._translate(type, notUTF8);
|
||||||
|
}
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The data being operated on by each operation.
|
||||||
|
*/
|
||||||
|
class Dish {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dish constructor
|
||||||
|
*
|
||||||
|
* @param {Dish || *} [dishOrInput=null] - A dish to clone OR an object
|
||||||
|
* literal to make into a dish
|
||||||
|
* @param {Enum} [type=null] (optional) - A type to accompany object
|
||||||
|
* literal input
|
||||||
|
*/
|
||||||
|
constructor(dishOrInput=null, type = null) {
|
||||||
|
|
||||||
|
if (Utils.isBrowser()) {
|
||||||
|
this._translate = _asyncTranslate.bind(this);
|
||||||
|
this.get = asyncGet.bind(this);
|
||||||
|
} else {
|
||||||
|
this._translate = _translate.bind(this);
|
||||||
|
this.get = get.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.value = [];
|
||||||
|
this.type = Dish.BYTE_ARRAY;
|
||||||
|
|
||||||
|
// Case: dishOrInput is dish object
|
||||||
|
if (dishOrInput &&
|
||||||
|
dishOrInput.hasOwnProperty("value") &&
|
||||||
|
dishOrInput.hasOwnProperty("type")) {
|
||||||
|
this.set(dishOrInput.value, dishOrInput.type);
|
||||||
|
// input and type defined separately
|
||||||
|
} else if (dishOrInput && type) {
|
||||||
|
this.set(dishOrInput, type);
|
||||||
|
// No type declared, so infer it.
|
||||||
|
} else if (dishOrInput) {
|
||||||
|
const inferredType = Dish.typeEnum(dishOrInput.constructor.name);
|
||||||
|
this.set(dishOrInput, inferredType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the data type enum for the given type string.
|
||||||
|
*
|
||||||
|
* @param {string} typeStr - The name of the data type.
|
||||||
|
* @returns {number} The data type enum value.
|
||||||
|
*/
|
||||||
|
static typeEnum(typeStr) {
|
||||||
|
switch (typeStr.toLowerCase()) {
|
||||||
|
case "bytearray":
|
||||||
|
case "byte array":
|
||||||
|
return Dish.BYTE_ARRAY;
|
||||||
|
case "string":
|
||||||
|
return Dish.STRING;
|
||||||
|
case "number":
|
||||||
|
return Dish.NUMBER;
|
||||||
|
case "html":
|
||||||
|
return Dish.HTML;
|
||||||
|
case "arraybuffer":
|
||||||
|
case "array buffer":
|
||||||
|
return Dish.ARRAY_BUFFER;
|
||||||
|
case "bignumber":
|
||||||
|
case "big number":
|
||||||
|
return Dish.BIG_NUMBER;
|
||||||
|
case "json":
|
||||||
|
case "object": // object constructor name. To allow JSON input in node.
|
||||||
|
return Dish.JSON;
|
||||||
|
case "file":
|
||||||
|
return Dish.FILE;
|
||||||
|
case "list<file>":
|
||||||
|
return Dish.LIST_FILE;
|
||||||
|
default:
|
||||||
|
throw new DishError("Invalid data type string. No matching enum.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the data type string for the given type enum.
|
||||||
|
*
|
||||||
|
* @param {number} typeEnum - The enum value of the data type.
|
||||||
|
* @returns {string} The data type as a string.
|
||||||
|
*/
|
||||||
|
static enumLookup(typeEnum) {
|
||||||
|
switch (typeEnum) {
|
||||||
|
case Dish.BYTE_ARRAY:
|
||||||
|
return "byteArray";
|
||||||
|
case Dish.STRING:
|
||||||
|
return "string";
|
||||||
|
case Dish.NUMBER:
|
||||||
|
return "number";
|
||||||
|
case Dish.HTML:
|
||||||
|
return "html";
|
||||||
|
case Dish.ARRAY_BUFFER:
|
||||||
|
return "ArrayBuffer";
|
||||||
|
case Dish.BIG_NUMBER:
|
||||||
|
return "BigNumber";
|
||||||
|
case Dish.JSON:
|
||||||
|
return "JSON";
|
||||||
|
case Dish.FILE:
|
||||||
|
return "File";
|
||||||
|
case Dish.LIST_FILE:
|
||||||
|
return "List<File>";
|
||||||
|
default:
|
||||||
|
throw new DishError("Invalid data type enum. No matching type.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the data value and type and then validates them.
|
||||||
|
*
|
||||||
|
* @param {*} value
|
||||||
|
* - The value of the input data.
|
||||||
|
* @param {number} type
|
||||||
|
* - The data type of value, see Dish enums.
|
||||||
|
*/
|
||||||
|
set(value, type) {
|
||||||
|
if (typeof type === "string") {
|
||||||
|
type = Dish.typeEnum(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("Dish type: " + Dish.enumLookup(type));
|
||||||
|
this.value = value;
|
||||||
|
this.type = type;
|
||||||
|
|
||||||
|
if (!this.valid()) {
|
||||||
|
const sample = Utils.truncate(JSON.stringify(this.value), 13);
|
||||||
|
throw new DishError(`Data is not a valid ${Dish.enumLookup(type)}: ${sample}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,6 @@
|
||||||
|
|
||||||
import util from "util";
|
import util from "util";
|
||||||
import Dish from "../core/Dish";
|
import Dish from "../core/Dish";
|
||||||
import Utils from "../core/Utils";
|
|
||||||
import DishError from "../core/errors/DishError";
|
|
||||||
import BigNumber from "bignumber.js";
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subclass of Dish where `get` and `_translate` are synchronous.
|
* Subclass of Dish where `get` and `_translate` are synchronous.
|
||||||
|
@ -34,23 +30,6 @@ class NodeDish extends Dish {
|
||||||
super(inputOrDish, type);
|
super(inputOrDish, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the value of the data in the type format specified.
|
|
||||||
*
|
|
||||||
* @param {number} type - The data type of value, see Dish enums.
|
|
||||||
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
|
|
||||||
* @returns {*} - The value of the output data.
|
|
||||||
*/
|
|
||||||
get(type, notUTF8=false) {
|
|
||||||
if (typeof type === "string") {
|
|
||||||
type = Dish.typeEnum(type);
|
|
||||||
}
|
|
||||||
if (this.type !== type) {
|
|
||||||
this._translate(type, notUTF8);
|
|
||||||
}
|
|
||||||
return this.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* alias for get
|
* alias for get
|
||||||
* @param args see get args
|
* @param args see get args
|
||||||
|
@ -88,99 +67,6 @@ class NodeDish extends Dish {
|
||||||
return this.get(Dish.typeEnum("number"));
|
return this.get(Dish.typeEnum("number"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Translates the data to the given type format.
|
|
||||||
*
|
|
||||||
* @param {number} toType - The data type of value, see Dish enums.
|
|
||||||
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
|
|
||||||
*/
|
|
||||||
_translate(toType, notUTF8=false) {
|
|
||||||
log.debug(`Translating Dish from ${Dish.enumLookup(this.type)} to ${Dish.enumLookup(toType)}`);
|
|
||||||
const byteArrayToStr = notUTF8 ? Utils.byteArrayToChars : Utils.byteArrayToUtf8;
|
|
||||||
|
|
||||||
// Convert data to intermediate byteArray type
|
|
||||||
try {
|
|
||||||
switch (this.type) {
|
|
||||||
case Dish.STRING:
|
|
||||||
this.value = this.value ? Utils.strToByteArray(this.value) : [];
|
|
||||||
break;
|
|
||||||
case Dish.NUMBER:
|
|
||||||
this.value = typeof this.value === "number" ? Utils.strToByteArray(this.value.toString()) : [];
|
|
||||||
break;
|
|
||||||
case Dish.HTML:
|
|
||||||
this.value = this.value ? Utils.strToByteArray(Utils.unescapeHtml(Utils.stripHtmlTags(this.value, true))) : [];
|
|
||||||
break;
|
|
||||||
case Dish.ARRAY_BUFFER:
|
|
||||||
// Array.from() would be nicer here, but it's slightly slower
|
|
||||||
this.value = Array.prototype.slice.call(new Uint8Array(this.value));
|
|
||||||
break;
|
|
||||||
case Dish.BIG_NUMBER:
|
|
||||||
this.value = BigNumber.isBigNumber(this.value) ? Utils.strToByteArray(this.value.toFixed()) : [];
|
|
||||||
break;
|
|
||||||
case Dish.JSON:
|
|
||||||
this.value = this.value ? Utils.strToByteArray(JSON.stringify(this.value, null, 4)) : [];
|
|
||||||
break;
|
|
||||||
case Dish.FILE:
|
|
||||||
this.value = Utils.readFileSync(this.value);
|
|
||||||
this.value = Array.prototype.slice.call(this.value);
|
|
||||||
break;
|
|
||||||
case Dish.LIST_FILE:
|
|
||||||
this.value = this.value.map(f => Utils.readFileSync(f));
|
|
||||||
this.value = this.value.map(b => Array.prototype.slice.call(b));
|
|
||||||
this.value = [].concat.apply([], this.value);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
throw new DishError(`Error translating from ${Dish.enumLookup(this.type)} to byteArray: ${err}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.type = Dish.BYTE_ARRAY;
|
|
||||||
|
|
||||||
// Convert from byteArray to toType
|
|
||||||
try {
|
|
||||||
switch (toType) {
|
|
||||||
case Dish.STRING:
|
|
||||||
case Dish.HTML:
|
|
||||||
this.value = this.value ? byteArrayToStr(this.value) : "";
|
|
||||||
this.type = Dish.STRING;
|
|
||||||
break;
|
|
||||||
case Dish.NUMBER:
|
|
||||||
this.value = this.value ? parseFloat(byteArrayToStr(this.value)) : 0;
|
|
||||||
this.type = Dish.NUMBER;
|
|
||||||
break;
|
|
||||||
case Dish.ARRAY_BUFFER:
|
|
||||||
this.value = new Uint8Array(this.value).buffer;
|
|
||||||
this.type = Dish.ARRAY_BUFFER;
|
|
||||||
break;
|
|
||||||
case Dish.BIG_NUMBER:
|
|
||||||
try {
|
|
||||||
this.value = new BigNumber(byteArrayToStr(this.value));
|
|
||||||
} catch (err) {
|
|
||||||
this.value = new BigNumber(NaN);
|
|
||||||
}
|
|
||||||
this.type = Dish.BIG_NUMBER;
|
|
||||||
break;
|
|
||||||
case Dish.JSON:
|
|
||||||
this.value = JSON.parse(byteArrayToStr(this.value));
|
|
||||||
this.type = Dish.JSON;
|
|
||||||
break;
|
|
||||||
case Dish.FILE:
|
|
||||||
this.value = new File(this.value, "unknown");
|
|
||||||
break;
|
|
||||||
case Dish.LIST_FILE:
|
|
||||||
this.value = [new File(this.value, "unknown")];
|
|
||||||
this.type = Dish.LIST_FILE;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
throw new DishError(`Error translating from byteArray to ${Dish.enumLookup(toType)}: ${err}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default NodeDish;
|
export default NodeDish;
|
||||||
|
|
|
@ -28,24 +28,24 @@ import ExludedOperationError from "../core/errors/ExcludedOperationError";
|
||||||
* @param {Object[]} originalArgs - the operation-s args list
|
* @param {Object[]} originalArgs - the operation-s args list
|
||||||
* @param {Object} newArgs - any inputted args
|
* @param {Object} newArgs - any inputted args
|
||||||
*/
|
*/
|
||||||
function reconcileOpArgs(operationArgs, objectStyleArgs) {
|
function transformArgs(opArgsList, newArgs) {
|
||||||
|
|
||||||
if (Array.isArray(objectStyleArgs)) {
|
if (newArgs && Array.isArray(newArgs)) {
|
||||||
return objectStyleArgs;
|
return newArgs;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter out arg values that are list subheadings - they are surrounded in [].
|
// Filter out arg values that are list subheadings - they are surrounded in [].
|
||||||
// See Strings op for example.
|
// See Strings op for example.
|
||||||
const opArgs = Object.assign([], operationArgs).map((a) => {
|
const opArgs = Object.assign([], opArgsList).map((a) => {
|
||||||
if (Array.isArray(a.value)) {
|
if (Array.isArray(a.value)) {
|
||||||
a.value = removeSubheadingsFromArray(a.value);
|
a.value = removeSubheadingsFromArray(a.value);
|
||||||
}
|
}
|
||||||
return a;
|
return a;
|
||||||
});
|
});
|
||||||
|
|
||||||
// transform object style arg objects to the same shape as op's args
|
// Reconcile object style arg info to fit operation args shape.
|
||||||
if (objectStyleArgs) {
|
if (newArgs) {
|
||||||
Object.keys(objectStyleArgs).map((key) => {
|
Object.keys(newArgs).map((key) => {
|
||||||
const index = opArgs.findIndex((arg) => {
|
const index = opArgs.findIndex((arg) => {
|
||||||
return arg.name.toLowerCase().replace(/ /g, "") ===
|
return arg.name.toLowerCase().replace(/ /g, "") ===
|
||||||
key.toLowerCase().replace(/ /g, "");
|
key.toLowerCase().replace(/ /g, "");
|
||||||
|
@ -54,22 +54,23 @@ function reconcileOpArgs(operationArgs, objectStyleArgs) {
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
const argument = opArgs[index];
|
const argument = opArgs[index];
|
||||||
if (argument.type === "toggleString") {
|
if (argument.type === "toggleString") {
|
||||||
if (typeof objectStyleArgs[key] === "string") {
|
if (typeof newArgs[key] === "string") {
|
||||||
argument.string = objectStyleArgs[key];
|
argument.string = newArgs[key];
|
||||||
} else {
|
} else {
|
||||||
argument.string = objectStyleArgs[key].string;
|
argument.string = newArgs[key].string;
|
||||||
argument.option = objectStyleArgs[key].option;
|
argument.option = newArgs[key].option;
|
||||||
}
|
}
|
||||||
} else if (argument.type === "editableOption") {
|
} else if (argument.type === "editableOption") {
|
||||||
// takes key: "option", key: {name, val: "string"}, key: {name, val: [...]}
|
// takes key: "option", key: {name, val: "string"}, key: {name, val: [...]}
|
||||||
argument.value = typeof objectStyleArgs[key] === "string" ? objectStyleArgs[key]: objectStyleArgs[key].value;
|
argument.value = typeof newArgs[key] === "string" ? newArgs[key]: newArgs[key].value;
|
||||||
} else {
|
} else {
|
||||||
argument.value = objectStyleArgs[key];
|
argument.value = newArgs[key];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sanitise args
|
||||||
return opArgs.map((arg) => {
|
return opArgs.map((arg) => {
|
||||||
if (arg.type === "option") {
|
if (arg.type === "option") {
|
||||||
// pick default option if not already chosen
|
// pick default option if not already chosen
|
||||||
|
@ -93,7 +94,7 @@ function reconcileOpArgs(operationArgs, objectStyleArgs) {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure an input is a NodeDish object.
|
* Ensure an input is a SyncDish object.
|
||||||
* @param input
|
* @param input
|
||||||
*/
|
*/
|
||||||
function ensureIsDish(input) {
|
function ensureIsDish(input) {
|
||||||
|
@ -109,6 +110,22 @@ function ensureIsDish(input) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prepareOp: transform args, make input the right type.
|
||||||
|
* Also convert any Buffers to ArrayBuffers.
|
||||||
|
* @param opInstance - instance of the operation
|
||||||
|
* @param input - operation input
|
||||||
|
* @param args - operation args
|
||||||
|
*/
|
||||||
|
function prepareOp(opInstance, input, args) {
|
||||||
|
const dish = ensureIsDish(input);
|
||||||
|
// Transform object-style args to original args array
|
||||||
|
const transformedArgs = transformArgs(opInstance.args, args);
|
||||||
|
const transformedInput = dish.get(opInstance.inputType);
|
||||||
|
return {transformedInput, transformedArgs};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* createArgOptions
|
* createArgOptions
|
||||||
*
|
*
|
||||||
|
@ -133,6 +150,7 @@ function createArgOptions(op) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap an operation to be consumed by node API.
|
* Wrap an operation to be consumed by node API.
|
||||||
* Checks to see if run function is async or not.
|
* Checks to see if run function is async or not.
|
||||||
|
@ -147,29 +165,44 @@ export function wrap(OpClass) {
|
||||||
|
|
||||||
// Check to see if class's run function is async.
|
// Check to see if class's run function is async.
|
||||||
const opInstance = new OpClass();
|
const opInstance = new OpClass();
|
||||||
|
const isAsync = opInstance.run.constructor.name === "AsyncFunction";
|
||||||
|
|
||||||
|
let wrapped;
|
||||||
|
|
||||||
|
// If async, wrap must be async.
|
||||||
|
if (isAsync) {
|
||||||
/**
|
/**
|
||||||
* Async wrapped operation run function
|
* Async wrapped operation run function
|
||||||
* @param {*} input
|
* @param {*} input
|
||||||
* @param {Object | String[]} args - either in Object or normal args array
|
* @param {Object | String[]} args - either in Object or normal args array
|
||||||
* @returns {Promise<NodeDish>} operation's output, on a Dish.
|
* @returns {Promise<SyncDish>} operation's output, on a Dish.
|
||||||
* @throws {OperationError} if the operation throws one.
|
* @throws {OperationError} if the operation throws one.
|
||||||
*/
|
*/
|
||||||
const wrapped = async (input, args=null) => {
|
wrapped = async (input, args=null) => {
|
||||||
const dish = ensureIsDish(input);
|
const {transformedInput, transformedArgs} = prepareOp(opInstance, input, args);
|
||||||
|
|
||||||
// Transform object-style args to original args array
|
|
||||||
const transformedArgs = reconcileOpArgs(opInstance.args, args);
|
|
||||||
|
|
||||||
// ensure the input is the correct type
|
|
||||||
const transformedInput = await dish.get(opInstance.inputType);
|
|
||||||
|
|
||||||
const result = await opInstance.run(transformedInput, transformedArgs);
|
const result = await opInstance.run(transformedInput, transformedArgs);
|
||||||
return new NodeDish({
|
return new NodeDish({
|
||||||
value: result,
|
value: result,
|
||||||
type: opInstance.outputType
|
type: opInstance.outputType
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
/**
|
||||||
|
* wrapped operation run function
|
||||||
|
* @param {*} input
|
||||||
|
* @param {Object | String[]} args - either in Object or normal args array
|
||||||
|
* @returns {SyncDish} operation's output, on a Dish.
|
||||||
|
* @throws {OperationError} if the operation throws one.
|
||||||
|
*/
|
||||||
|
wrapped = (input, args=null) => {
|
||||||
|
const {transformedInput, transformedArgs} = prepareOp(opInstance, input, args);
|
||||||
|
const result = opInstance.run(transformedInput, transformedArgs);
|
||||||
|
return new NodeDish({
|
||||||
|
value: result,
|
||||||
|
type: opInstance.outputType
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// used in chef.help
|
// used in chef.help
|
||||||
wrapped.opName = OpClass.name;
|
wrapped.opName = OpClass.name;
|
||||||
|
@ -251,7 +284,7 @@ export function bake(operations){
|
||||||
* @param {*} input - some input for a recipe.
|
* @param {*} input - some input for a recipe.
|
||||||
* @param {String | Function | String[] | Function[] | [String | Function]} recipeConfig -
|
* @param {String | Function | String[] | Function[] | [String | Function]} recipeConfig -
|
||||||
* An operation, operation name, or an array of either.
|
* An operation, operation name, or an array of either.
|
||||||
* @returns {NodeDish} of the result
|
* @returns {SyncDish} of the result
|
||||||
* @throws {TypeError} if invalid recipe given.
|
* @throws {TypeError} if invalid recipe given.
|
||||||
*/
|
*/
|
||||||
return function(input, recipeConfig) {
|
return function(input, recipeConfig) {
|
||||||
|
|
Loading…
Reference in a new issue