From 638093d40eb6080d9817d49055ea44be12854b60 Mon Sep 17 00:00:00 2001 From: d98762625 Date: Fri, 22 Mar 2019 09:39:43 +0000 Subject: [PATCH] correct translation from node Buffer to byte array --- src/core/Dish.mjs | 3 +-- src/node/NodeDish.mjs | 4 ++-- tests/node/tests/nodeApi.mjs | 5 ++++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/core/Dish.mjs b/src/core/Dish.mjs index d8c47d1f..e6e98670 100755 --- a/src/core/Dish.mjs +++ b/src/core/Dish.mjs @@ -36,7 +36,6 @@ class Dish { * literal input */ constructor(dishOrInput=null, type = null) { - this.value = []; this.type = Dish.BYTE_ARRAY; @@ -46,7 +45,7 @@ class Dish { dishOrInput.hasOwnProperty("type")) { this.set(dishOrInput.value, dishOrInput.type); // input and type defined separately - } else if (dishOrInput && type) { + } else if (dishOrInput && type !== null) { this.set(dishOrInput, type); // No type declared, so infer it. } else if (dishOrInput) { diff --git a/src/node/NodeDish.mjs b/src/node/NodeDish.mjs index 67fb30d6..ea39b774 100644 --- a/src/node/NodeDish.mjs +++ b/src/node/NodeDish.mjs @@ -22,9 +22,9 @@ class NodeDish extends Dish { // Allow `fs` file input: // Any node fs Buffers transformed to array buffer - // NOT Buffer.buff, as this makes a buffer of the whole object. + // Use Array.from as Uint8Array doesnt pass instanceof Array test if (Buffer.isBuffer(inputOrDish)) { - inputOrDish = new Uint8Array(inputOrDish).buffer; + inputOrDish = Array.from(inputOrDish); type = Dish.BYTE_ARRAY; } super(inputOrDish, type); diff --git a/tests/node/tests/nodeApi.mjs b/tests/node/tests/nodeApi.mjs index 24c7c4bf..1a05f7ce 100644 --- a/tests/node/tests/nodeApi.mjs +++ b/tests/node/tests/nodeApi.mjs @@ -357,6 +357,9 @@ TestRegister.addApiTests([ const arrayBufferDish = new Dish(Buffer.from("some buffer input").buffer); assert.strictEqual(arrayBufferDish.type, 4); + const byteArrayDish = new Dish(Buffer.from("some buffer input")); + assert.strictEqual(byteArrayDish.type, 0); + const JSONDish = new Dish({key: "value"}); assert.strictEqual(JSONDish.type, 6); }), @@ -364,7 +367,7 @@ TestRegister.addApiTests([ it("Composable dish: Buffer type dishes should be converted to strings", () => { fs.writeFileSync("test.txt", "abc"); const dish = new Dish(fs.readFileSync("test.txt")); - assert.strictEqual(dish.type, 4); + assert.strictEqual(dish.type, 0); fs.unlinkSync("test.txt"); }),