add dish translation tests for node

This commit is contained in:
d98762625 2019-04-29 17:09:01 +01:00
parent aeb08caf77
commit 65a3897f87
7 changed files with 147 additions and 24 deletions

View file

@ -208,7 +208,7 @@ class Dish {
valid() {
switch (this.type) {
case Dish.BYTE_ARRAY:
if (!(this.value instanceof Array)) {
if (!(this.value instanceof Uint8Array) && !(this.value instanceof Array)) {
return false;
}

View file

@ -1039,8 +1039,7 @@ class Utils {
if (!Utils.isNode()) {
throw new TypeError("Browser environment cannot support readFileSync");
}
// Resist using node's Buffer.buffer here - this yields a 8192 length byteArray
// regardless of the length of the buffer.
const arrayBuffer = Uint8Array.from(file.data);
return arrayBuffer.buffer;
}

View file

@ -19,7 +19,6 @@ class DishFile extends DishTranslationType {
static toArrayBuffer() {
DishFile.checkForValue(this.value);
if (Utils.isNode()) {
// TODO
this.value = Utils.readFileSync(this.value);
} else {
return new Promise((resolve, reject) => {

View file

@ -4,14 +4,13 @@
* @license Apache-2.0
*/
import DishTranslationType from "./DishTranslationType";
import Utils from "../Utils";
import DishString from "./DishString";
import Utils from "../Utils";
/**
* Translation methods for HTML Dishes
*/
class DishHTML extends DishTranslationType {
class DishHTML extends DishString {
/**
* convert the given value to a ArrayBuffer
@ -22,13 +21,6 @@ class DishHTML extends DishTranslationType {
this.value = this.value ? Utils.strToArrayBuffer(Utils.unescapeHtml(Utils.stripHtmlTags(this.value, true))) : new ArrayBuffer;
}
/**
* convert the given value from a ArrayBuffer
* @param {boolean} notUTF8
*/
static fromArrayBuffer(notUTF8) {
DishString.fromByteArray(this.value, notUTF8);
}
}
export default DishHTML;

View file

@ -5,6 +5,7 @@
*/
import DishTranslationType from "./DishTranslationType";
import Utils from "../Utils.mjs";
/**
* Translation methods for ListFile Dishes
@ -16,6 +17,10 @@ class DishListFile extends DishTranslationType {
*/
static toArrayBuffer() {
DishListFile.checkForValue(this.value);
if (Utils.isNode()) {
this.value = this.value.map(file => Uint8Array.from(file.data));
}
this.value = DishListFile.concatenateTypedArrays(...this.value).buffer;
}