WIP dynamically define async functions in Dish, only if needed

This commit is contained in:
d98762625 2019-02-15 15:40:29 +00:00
parent 04b7f2fa8c
commit 573a292e16
3 changed files with 301 additions and 263 deletions

View file

@ -10,6 +10,229 @@ import DishError from "./errors/DishError";
import BigNumber from "bignumber.js"; import BigNumber from "bignumber.js";
import log from "loglevel"; import log from "loglevel";
/**
* 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.
*/
async function _asyncTranslate(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 = await Utils.readFile(this.value);
this.value = Array.prototype.slice.call(this.value);
break;
case Dish.LIST_FILE:
this.value = await Promise.all(this.value.map(async f => Utils.readFile(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}`);
}
}
/**
* 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. * The data being operated on by each operation.
*/ */
@ -24,6 +247,15 @@ class Dish {
* literal input * literal input
*/ */
constructor(dishOrInput=null, type = null) { 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.value = [];
this.type = Dish.BYTE_ARRAY; this.type = Dish.BYTE_ARRAY;
@ -74,7 +306,6 @@ class Dish {
case "list<file>": case "list<file>":
return Dish.LIST_FILE; return Dish.LIST_FILE;
default: default:
console.log(typeStr);
throw new DishError("Invalid data type string. No matching enum."); throw new DishError("Invalid data type string. No matching enum.");
} }
} }
@ -136,118 +367,6 @@ class Dish {
} }
/**
* 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.
*
* @param {number} toType - The data type of value, see Dish enums.
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
*/
async _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 = await Utils.readFile(this.value);
this.value = Array.prototype.slice.call(this.value);
break;
case Dish.LIST_FILE:
this.value = await Promise.all(this.value.map(async f => Utils.readFile(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}`);
}
}
/** /**
* Validates that the value is the type that has been specified. * Validates that the value is the type that has been specified.
* May have to disable parts of BYTE_ARRAY validation if it effects performance. * May have to disable parts of BYTE_ARRAY validation if it effects performance.

View file

@ -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;

View file

@ -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;
* Async wrapped operation run function
* @param {*} input
* @param {Object | String[]} args - either in Object or normal args array
* @returns {Promise<NodeDish>} operation's output, on a Dish.
* @throws {OperationError} if the operation throws one.
*/
const wrapped = async (input, args=null) => {
const dish = ensureIsDish(input);
// Transform object-style args to original args array // If async, wrap must be async.
const transformedArgs = reconcileOpArgs(opInstance.args, args); if (isAsync) {
/**
// ensure the input is the correct type * Async wrapped operation run function
const transformedInput = await dish.get(opInstance.inputType); * @param {*} input
* @param {Object | String[]} args - either in Object or normal args array
const result = await opInstance.run(transformedInput, transformedArgs); * @returns {Promise<SyncDish>} operation's output, on a Dish.
return new NodeDish({ * @throws {OperationError} if the operation throws one.
value: result, */
type: opInstance.outputType wrapped = async (input, args=null) => {
}); const {transformedInput, transformedArgs} = prepareOp(opInstance, input, args);
}; const result = await opInstance.run(transformedInput, transformedArgs);
return new NodeDish({
value: result,
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) {