From 86db43e6dda23c1b7eaa9ed411f9baedcc56de39 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Fri, 13 Dec 2019 16:09:02 +0000 Subject: [PATCH] Fixed the magic bug where it wouldnt recommended operations that resulted in lists of files --- src/core/dishTypes/DishListFile.mjs | 28 +++++++++++++++++++++++++--- src/core/lib/FileSignatures.mjs | 2 +- src/core/lib/Magic.mjs | 2 +- tests/node/tests/NodeDish.mjs | 4 ++-- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/core/dishTypes/DishListFile.mjs b/src/core/dishTypes/DishListFile.mjs index 198a974b..3bca6133 100644 --- a/src/core/dishTypes/DishListFile.mjs +++ b/src/core/dishTypes/DishListFile.mjs @@ -5,7 +5,8 @@ */ import DishType from "./DishType.mjs"; -import { isNodeEnvironment } from "../Utils.mjs"; +import Utils, { isNodeEnvironment } from "../Utils.mjs"; +import Dish from "../Dish.mjs"; /** @@ -16,13 +17,13 @@ class DishListFile extends DishType { /** * convert the given value to a ArrayBuffer */ - static toArrayBuffer() { + static async toArrayBuffer() { DishListFile.checkForValue(this.value); if (isNodeEnvironment()) { this.value = this.value.map(file => Uint8Array.from(file.data)); } - this.value = DishListFile.concatenateTypedArrays(...this.value).buffer; + this.value = (this.type === Dish.LIST_FILE ? await DishListFile.concatenateTypedArraysWithTypedElements(...this.value) : await DishListFile.concatenateTypedArrays(...this.value)).buffer; } /** @@ -33,6 +34,27 @@ class DishListFile extends DishType { this.value = [new File(this.value, "unknown")]; } + /** + * Concatenates a list of typed elements together. + * + * @param {Uint8Array[]} arrays + * @returns {Uint8Array} + */ + static async concatenateTypedArraysWithTypedElements(...arrays) { + let totalLength = 0; + for (const arr of arrays) { + totalLength += arr.size; + } + const myArray = new Uint8Array(totalLength); + + let offset = 0; + for (const arr of arrays) { + const data = await Utils.readFile(arr); + myArray.set(data, offset); + offset += data.length; + } + return myArray; + } /** * Concatenates a list of Uint8Arrays together diff --git a/src/core/lib/FileSignatures.mjs b/src/core/lib/FileSignatures.mjs index 17b17f23..07f3c101 100644 --- a/src/core/lib/FileSignatures.mjs +++ b/src/core/lib/FileSignatures.mjs @@ -2947,7 +2947,7 @@ export function extractWAV(bytes, offset) { stream.moveTo(4); // Move to file size. - stream.moveTo(stream.readInt(4, "le") - 4); + stream.moveTo(stream.readInt(4, "le")); return stream.carve(); } diff --git a/src/core/lib/Magic.mjs b/src/core/lib/Magic.mjs index 79c64452..5052db84 100644 --- a/src/core/lib/Magic.mjs +++ b/src/core/lib/Magic.mjs @@ -403,7 +403,7 @@ class Magic { await recipe.execute(dish); // Return an empty buffer if the recipe did not run to completion if (recipe.lastRunOp === recipe.opList[recipe.opList.length - 1]) { - return dish.get(Dish.ARRAY_BUFFER); + return await dish.get(Dish.ARRAY_BUFFER); } else { return new ArrayBuffer(); } diff --git a/tests/node/tests/NodeDish.mjs b/tests/node/tests/NodeDish.mjs index 429c38af..3ec8b7e2 100644 --- a/tests/node/tests/NodeDish.mjs +++ b/tests/node/tests/NodeDish.mjs @@ -187,8 +187,8 @@ TestRegister.addApiTests([ const dish = new Dish([file1, file2], Dish.LIST_FILE); dish.get(Dish.ARRAY_BUFFER); - assert.deepStrictEqual(dish.value, new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b]).buffer); - assert.strictEqual(dish.value.byteLength, 11); + assert.deepStrictEqual(dish.value, [new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]), new Uint8Array([0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b])]); + assert.strictEqual(dish.value.length, 2); dish.get(Dish.LIST_FILE); const dataArray = new Uint8Array(dish.value[0].data);