Fixed the magic bug where it wouldnt recommended operations that resulted in lists of files

This commit is contained in:
n1073645 2019-12-13 16:09:02 +00:00
parent 798f013219
commit 86db43e6dd
4 changed files with 29 additions and 7 deletions

View file

@ -5,7 +5,8 @@
*/ */
import DishType from "./DishType.mjs"; 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 * convert the given value to a ArrayBuffer
*/ */
static toArrayBuffer() { static async toArrayBuffer() {
DishListFile.checkForValue(this.value); DishListFile.checkForValue(this.value);
if (isNodeEnvironment()) { if (isNodeEnvironment()) {
this.value = this.value.map(file => Uint8Array.from(file.data)); 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")]; 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 * Concatenates a list of Uint8Arrays together

View file

@ -2947,7 +2947,7 @@ export function extractWAV(bytes, offset) {
stream.moveTo(4); stream.moveTo(4);
// Move to file size. // Move to file size.
stream.moveTo(stream.readInt(4, "le") - 4); stream.moveTo(stream.readInt(4, "le"));
return stream.carve(); return stream.carve();
} }

View file

@ -403,7 +403,7 @@ class Magic {
await recipe.execute(dish); await recipe.execute(dish);
// Return an empty buffer if the recipe did not run to completion // Return an empty buffer if the recipe did not run to completion
if (recipe.lastRunOp === recipe.opList[recipe.opList.length - 1]) { if (recipe.lastRunOp === recipe.opList[recipe.opList.length - 1]) {
return dish.get(Dish.ARRAY_BUFFER); return await dish.get(Dish.ARRAY_BUFFER);
} else { } else {
return new ArrayBuffer(); return new ArrayBuffer();
} }

View file

@ -187,8 +187,8 @@ TestRegister.addApiTests([
const dish = new Dish([file1, file2], Dish.LIST_FILE); const dish = new Dish([file1, file2], Dish.LIST_FILE);
dish.get(Dish.ARRAY_BUFFER); dish.get(Dish.ARRAY_BUFFER);
assert.deepStrictEqual(dish.value, new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b]).buffer); assert.deepStrictEqual(dish.value, [new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]), new Uint8Array([0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b])]);
assert.strictEqual(dish.value.byteLength, 11); assert.strictEqual(dish.value.length, 2);
dish.get(Dish.LIST_FILE); dish.get(Dish.LIST_FILE);
const dataArray = new Uint8Array(dish.value[0].data); const dataArray = new Uint8Array(dish.value[0].data);