Refactor Dish _translate to handle sync and async depending on environment.

This commit is contained in:
d98762625 2019-03-01 16:02:21 +00:00
parent 0a1ca18de5
commit b48c16b4db
16 changed files with 492 additions and 223 deletions

View File

@ -365,9 +365,6 @@ module.exports = function (grunt) {
"./config/modules/OpModules": "./config/modules/Default"
}
},
output: {
// globalObject: "this",
},
plugins: [
new webpack.DefinePlugin(BUILD_CONSTANTS),
new HtmlWebpackPlugin({

View File

@ -10,6 +10,17 @@ import DishError from "./errors/DishError";
import BigNumber from "bignumber.js";
import log from "loglevel";
import {
DishArrayBuffer,
DishBigNumber,
DishFile,
DishHTML,
DishJSON,
DishListFile,
DishNumber,
DishString,
} from "./dishTranslationTypes";
/**
* The data being operated on by each operation.
@ -116,6 +127,8 @@ class Dish {
/**
* Returns the value of the data in the type format specified.
*
* If running in a browser, get is asynchronous.
*
* @param {number} type - The data type of value, see Dish enums.
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
* @returns {* | Promise} - (Broswer) A promise | (Node) value of dish in given type
@ -127,8 +140,13 @@ class Dish {
if (this.type !== type) {
// Node environment => _translate is sync
if (Utils.isNode()) {
this._translate(type, notUTF8);
return this.value;
// Browser environment => _translate is async
if (Utils.isBrowser()) {
} else {
return new Promise((resolve, reject) => {
this._translate(type, notUTF8)
.then(() => {
@ -136,11 +154,6 @@ class Dish {
})
.catch(reject);
});
// Node environment => _translate is sync
} else {
this._translate(type, notUTF8);
return this.value;
}
}
@ -308,6 +321,110 @@ class Dish {
return newDish;
}
/**
* Translates the data to the given type format.
*
* If running in the browser, _translate is asynchronous.
*
* @param {number} toType - The data type of value, see Dish enums.
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
* @returns {Promise || undefined}
*/
_translate(toType, notUTF8=false) {
log.debug(`Translating Dish from ${Dish.enumLookup(this.type)} to ${Dish.enumLookup(toType)}`);
// Node environment => translate is sync
if (Utils.isNode()) {
this._toByteArray();
this._fromByteArray(toType, notUTF8);
// Browser environment => translate is async
} else {
return new Promise((resolve, reject) => {
this._toByteArray()
.then(() => this.type = Dish.BYTE_ARRAY)
.then(() => {
this._fromByteArray(toType);
resolve();
})
.catch(reject);
});
}
}
/**
* Convert this.value to a ByteArray
*
* If running in a browser, _toByteArray is asynchronous.
*
* @returns {Promise || undefined}
*/
_toByteArray() {
// Using 'bind' here to allow this.value to be mutated within translation functions
const toByteArrayFuncs = {
browser: {
[Dish.STRING]: () => Promise.resolve(DishString.toByteArray.bind(this)()),
[Dish.NUMBER]: () => Promise.resolve(DishNumber.toByteArray.bind(this)()),
[Dish.HTML]: () => Promise.resolve(DishHTML.toByteArray.bind(this)()),
[Dish.ARRAY_BUFFER]: () => Promise.resolve(DishArrayBuffer.toByteArray.bind(this)()),
[Dish.BIG_NUMBER]: () => Promise.resolve(DishBigNumber.toByteArray.bind(this)()),
[Dish.JSON]: () => Promise.resolve(DishJSON.toByteArray.bind(this)()),
[Dish.FILE]: () => DishFile.toByteArray.bind(this)(),
[Dish.LIST_FILE]: () => DishListFile.toByteArray.bind(this)(),
[Dish.BYTE_ARRAY]: () => Promise.resolve(),
},
node: {
[Dish.STRING]: () => DishString.toByteArray.bind(this)(),
[Dish.NUMBER]: () => DishNumber.toByteArray.bind(this)(),
[Dish.HTML]: () => DishHTML.toByteArray.bind(this)(),
[Dish.ARRAY_BUFFER]: () => DishArrayBuffer.toByteArray.bind(this)(),
[Dish.BIG_NUMBER]: () => DishBigNumber.toByteArray.bind(this)(),
[Dish.JSON]: () => DishJSON.toByteArray.bind(this)(),
[Dish.FILE]: () => DishFile.toByteArray.bind(this)(),
[Dish.LIST_FILE]: () => DishListFile.toByteArray.bind(this)(),
[Dish.BYTE_ARRAY]: () => {},
}
};
try {
return toByteArrayFuncs[Utils.isNode() && "node" || "browser"][this.type]();
} catch (err) {
throw new DishError(`Error translating from ${Dish.enumLookup(this.type)} to byteArray: ${err}`);
}
}
/**
* Convert this.value to the given type.
*
* @param {number} toType - the Dish enum to convert to
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
*/
_fromByteArray(toType, notUTF8) {
const byteArrayToStr = notUTF8 ? Utils.byteArrayToChars : Utils.byteArrayToUtf8;
// Using 'bind' here to allow this.value to be mutated within translation functions
const toTypeFunctions = {
[Dish.STRING]: () => DishString.fromByteArray.bind(this)(byteArrayToStr),
[Dish.NUMBER]: () => DishNumber.fromByteArray.bind(this)(byteArrayToStr),
[Dish.HTML]: () => DishHTML.fromByteArray.bind(this)(byteArrayToStr),
[Dish.ARRAY_BUFFER]: () => DishArrayBuffer.fromByteArray.bind(this)(),
[Dish.BIG_NUMBER]: () => DishBigNumber.fromByteArray.bind(this)(byteArrayToStr),
[Dish.JSON]: () => DishJSON.fromByteArray.bind(this)(byteArrayToStr),
[Dish.FILE]: () => DishFile.fromByteArray.bind(this)(),
[Dish.LIST_FILE]: () => DishListFile.fromByteArray.bind(this)(),
[Dish.BYTE_ARRAY]: () => {},
};
try {
toTypeFunctions[toType]();
this.type = toType;
} catch (err) {
throw new DishError(`Error translating from byteArray to ${Dish.enumLookup(toType)}: ${err}`);
}
}
}
@ -367,201 +484,4 @@ Dish.FILE = 7;
Dish.LIST_FILE = 8;
/**
* 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}`);
}
}
if (Utils.isBrowser()) {
Dish.prototype._translate = _asyncTranslate;
} else {
Dish.prototype._translate = _translate;
}
export default Dish;

View File

@ -926,7 +926,11 @@ class Utils {
* await Utils.readFile(new File(["hello"], "test"))
*/
static readFile(file) {
if (Utils.isBrowser()) {
if (Utils.isNode()) {
return Buffer.from(file).buffer;
} else {
return new Promise((resolve, reject) => {
const reader = new FileReader();
const data = new Uint8Array(file.size);
@ -954,17 +958,12 @@ class Utils {
seek();
});
} else if (Utils.isNode()) {
return Buffer.from(file).buffer;
}
throw new Error("Unkown environment!");
}
/** */
static readFileSync(file) {
if (Utils.isBrowser()) {
if (!Utils.isNode()) {
throw new TypeError("Browser environment cannot support readFileSync");
}
@ -1065,13 +1064,6 @@ class Utils {
}[token];
}
/**
* Check if code is running in a browser environment
*/
static isBrowser() {
return typeof window !== "undefined" && typeof window.document !== "undefined";
}
/**
* Check if code is running in a Node environment
*/

View File

@ -0,0 +1,32 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import DishTranslationType from "./DishTranslationType";
/**
* Translation methods for ArrayBuffer Dishes
*/
class DishArrayBuffer extends DishTranslationType {
/**
* convert the given value to a ByteArray
*/
static toByteArray() {
DishArrayBuffer.checkForValue(this.value);
this.value = Array.prototype.slice.call(new Uint8Array(this.value));
}
/**
* convert the given value from a ByteArray
* @param {function} byteArrayToStr
*/
static fromByteArray() {
DishArrayBuffer.checkForValue(this.value);
this.value = new Uint8Array(this.value).buffer;
}
}
export default DishArrayBuffer;

View File

@ -0,0 +1,40 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import DishTranslationType from "./DishTranslationType";
import Utils from "../Utils";
import BigNumber from "bignumber.js";
/**
* translation methods for BigNumber Dishes
*/
class DishBigNumber extends DishTranslationType {
/**
* convert the given value to a ByteArray
* @param {BigNumber} value
*/
static toByteArray() {
DishBigNumber.checkForValue(this.value);
this.value = BigNumber.isBigNumber(this.value) ? Utils.strToByteArray(this.value.toFixed()) : [];
}
/**
* convert the given value from a ByteArray
* @param {ByteArray} value
* @param {function} byteArrayToStr
*/
static fromByteArray(byteArrayToStr) {
DishBigNumber.checkForValue(this.value);
try {
this.value = new BigNumber(byteArrayToStr(this.value));
} catch (err) {
this.value = new BigNumber(NaN);
}
}
}
export default DishBigNumber;

View File

@ -0,0 +1,44 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import DishTranslationType from "./DishTranslationType";
import Utils from "../Utils";
/**
* Translation methods for file Dishes
*/
class DishFile extends DishTranslationType {
/**
* convert the given value to a ByteArray
* @param {File} value
*/
static toByteArray() {
DishFile.checkForValue(this.value);
if (Utils.isNode()) {
this.value = Array.prototype.slice.call(Utils.readFileSync(this.value));
} else {
return new Promise((resolve, reject) => {
Utils.readFile(this.value)
.then(v => this.value = Array.prototype.slice.call(v))
.then(resolve)
.catch(reject);
});
}
}
/**
* convert the given value from a ByteArray
* @param {ByteArray} value
* @param {function} byteArrayToStr
*/
static fromByteArray() {
DishFile.checkForValue(this.value);
this.value = new File(this.value, "unknown");
}
}
export default DishFile;

View File

@ -0,0 +1,35 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import DishTranslationType from "./DishTranslationType";
import Utils from "../Utils";
import DishString from "./DishString";
/**
* Translation methods for HTML Dishes
*/
class DishHTML extends DishTranslationType {
/**
* convert the given value to a ByteArray
* @param {String} value
*/
static toByteArray() {
DishHTML.checkForValue(this.value);
this.value = this.value ? Utils.strToByteArray(Utils.unescapeHtml(Utils.stripHtmlTags(this.value, true))) : [];
}
/**
* convert the given value from a ByteArray
* @param {function} byteArrayToStr
*/
static fromByteArray(byteArrayToStr) {
DishHTML.checkForValue(this.value);
DishString.fromByteArray(this.value, byteArrayToStr);
}
}
export default DishHTML;

View File

@ -0,0 +1,34 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import DishTranslationType from "./DishTranslationType";
import Utils from "../Utils";
/**
* Translation methods for JSON dishes
*/
class DishJSON extends DishTranslationType {
/**
* convert the given value to a ByteArray
*/
static toByteArray() {
DishJSON.checkForValue(this.value);
this.value = this.value ? Utils.strToByteArray(JSON.stringify(this.value, null, 4)) : [];
}
/**
* convert the given value from a ByteArray
* @param {ByteArray} value
* @param {function} byteArrayToStr
*/
static fromByteArray(byteArrayToStr) {
DishJSON.checkForValue(this.value);
this.value = JSON.parse(byteArrayToStr(this.value));
}
}
export default DishJSON;

View File

@ -0,0 +1,42 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import DishTranslationType from "./DishTranslationType";
import Utils from "../Utils";
/**
* Translation methods for ListFile Dishes
*/
class DishListFile extends DishTranslationType {
/**
* convert the given value to a ByteArray
*/
static toByteArray() {
DishListFile.checkForValue(this.value);
if (Utils.isNode()) {
this.value = [].concat.apply([], this.value.map(f => Utils.readFileSync(f)).map(b => Array.prototype.slice.call(b)));
} else {
return new Promise((resolve, reject) => {
Promise.all(this.value.map(async f => Utils.readFile(f)))
.then(values => this.value = values.map(b => [].concat.apply([], Array.prototype.slice.call(b))))
.then(resolve)
.catch(reject);
});
}
}
/**
* convert the given value from a ByteArray
* @param {function} byteArrayToStr
*/
static fromByteArray() {
DishListFile.checkForValue(this.value);
this.value = [new File(this.value, "unknown")];
}
}
export default DishListFile;

View File

@ -0,0 +1,34 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import DishTranslationType from "./DishTranslationType";
import Utils from "../Utils";
/**
* Translation methods for number dishes
*/
class DishNumber extends DishTranslationType {
/**
* convert the given value to a ByteArray
*/
static toByteArray() {
DishNumber.checkForValue(this.value);
this.value = typeof this.value === "number" ? Utils.strToByteArray(this.value.toString()) : [];
}
/**
* convert the given value from a ByteArray
* @param {function} byteArrayToStr
*/
static fromByteArray(byteArrayToStr) {
DishNumber.checkForValue(this.value);
this.value = this.value ? parseFloat(byteArrayToStr(this.value)) : 0;
}
}
export default DishNumber;

View File

@ -0,0 +1,34 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import DishTranslationType from "./DishTranslationType";
import Utils from "../Utils";
/**
* Translation methods for string dishes
*/
class DishString extends DishTranslationType {
/**
* convert the given value to a ByteArray
*/
static toByteArray() {
DishString.checkForValue(this.value);
this.value = this.value ? Utils.strToByteArray(this.value) : [];
}
/**
* convert the given value from a ByteArray
* @param {function} byteArrayToStr
*/
static fromByteArray(byteArrayToStr) {
DishString.checkForValue(this.value);
this.value = this.value ? byteArrayToStr(this.value) : "";
}
}
export default DishString;

View File

@ -0,0 +1,39 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
/**
* Abstract class for dish translation methods
*/
class DishTranslationType {
/**
* Warn translations dont work without value from bind
*/
static checkForValue(value) {
if (value === undefined) {
throw new Error("only use translation methods with .bind");
}
}
/**
* convert the given value to a ByteArray
* @param {*} value
*/
static toByteArray() {
throw new Error("toByteArray has not been implemented");
}
/**
* convert the given value from a ByteArray
* @param {function} byteArrayToStr
*/
static fromByteArray(byteArrayToStr=undefined) {
throw new Error("toType has not been implemented");
}
}
export default DishTranslationType;

View File

@ -0,0 +1,26 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import DishArrayBuffer from "./DishArrayBuffer";
import DishBigNumber from "./DishBigNumber";
import DishFile from "./DishFile";
import DishHTML from "./DishHTML";
import DishJSON from "./DishJSON";
import DishListFile from "./DishListFile";
import DishNumber from "./DishNumber";
import DishString from "./DishString";
export {
DishArrayBuffer,
DishBigNumber,
DishFile,
DishHTML,
DishJSON,
DishListFile,
DishNumber,
DishString,
};

View File

@ -18,7 +18,7 @@
* UTM: 30N 699456 5709791,
*/
import TestRegister from "../TestRegister";
import TestRegister from "../../lib/TestRegister";
TestRegister.addTests([
{

View File

@ -6,7 +6,7 @@
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import TestRegister from "../TestRegister";
import TestRegister from "../../lib/TestRegister";
TestRegister.addTests([
{

View File

@ -6,7 +6,7 @@
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import TestRegister from "../TestRegister";
import TestRegister from "../../lib/TestRegister";
TestRegister.addTests([
{