From 55f7cac52648f532f61a08d4c8156305afc2cdd0 Mon Sep 17 00:00:00 2001 From: d98762625 Date: Sun, 19 Aug 2018 22:55:40 +0100 Subject: [PATCH] Remove Buffer from Dish. Convert into ByteArray in api and then pass in as ArrayBuffer. Add some PNG files to tests --- src/core/Dish.mjs | 20 -------------------- src/node/api.mjs | 9 +++++++-- test/tests/nodeApi/ops.mjs | 30 +++++++----------------------- 3 files changed, 14 insertions(+), 45 deletions(-) diff --git a/src/core/Dish.mjs b/src/core/Dish.mjs index da49b6e6..c6c68ae4 100755 --- a/src/core/Dish.mjs +++ b/src/core/Dish.mjs @@ -61,8 +61,6 @@ class Dish { return Dish.FILE; case "list": return Dish.LIST_FILE; - case "buffer": - return Dish.BUFFER; default: throw "Invalid data type string. No matching enum."; } @@ -95,8 +93,6 @@ class Dish { return "File"; case Dish.LIST_FILE: return "List"; - case Dish.BUFFER: - return "Buffer"; default: throw "Invalid data type enum. No matching type."; } @@ -270,8 +266,6 @@ class Dish { case Dish.LIST_FILE: return this.value instanceof Array && this.value.reduce((acc, curr) => acc && curr instanceof File, true); - case Dish.BUFFER: - return this.value instanceof Buffer; default: return false; } @@ -290,7 +284,6 @@ class Dish { case Dish.BYTE_ARRAY: case Dish.STRING: case Dish.HTML: - case Dish.BUFFER: return this.value.length; case Dish.NUMBER: case Dish.BIG_NUMBER: @@ -364,12 +357,6 @@ class Dish { this.type ); break; - case Dish.BUFFER: - newDish.set( - Buffer.from(this.value), - this.type - ); - break; default: throw new Error("Cannot clone Dish, unknown type"); } @@ -434,12 +421,5 @@ Dish.FILE = 7; * @enum */ Dish.LIST_FILE = 8; -/** -* Dish data type enum for node Buffer. -* @readonly -* @enum -*/ -Dish.BUFFER = 9; - export default Dish; diff --git a/src/node/api.mjs b/src/node/api.mjs index 3b9bb601..11dd6e49 100644 --- a/src/node/api.mjs +++ b/src/node/api.mjs @@ -108,15 +108,20 @@ function ensureIsDish(input) { dish.set(input, type); } return dish; -}; +} /** * prepareOp: transform args, make input the right type. + * Also convert any Buffers to ArrayBuffers. * @param opInstance - instance of the operation * @param input - operation input * @param args - operation args */ function prepareOp(opInstance, input, args) { + // convert any Buffers into ArrayBuffers. + if (input instanceof Buffer) { + input = input.buffer; + } const dish = ensureIsDish(input); let transformedArgs; // Transform object-style args to original args array @@ -127,7 +132,7 @@ function prepareOp(opInstance, input, args) { } const transformedInput = dish.get(opInstance.inputType); return {transformedInput, transformedArgs}; -}; +} /** * Wrap an operation to be consumed by node API. diff --git a/test/tests/nodeApi/ops.mjs b/test/tests/nodeApi/ops.mjs index 3f7066a7..abba985f 100644 --- a/test/tests/nodeApi/ops.mjs +++ b/test/tests/nodeApi/ops.mjs @@ -786,29 +786,13 @@ jmPGsv1elXxVzqs58UZLD2c3vBhGkU2BV6kRKh+lj/EcVrzsFhGCz/7DKxPoDHLS it("Remove EXIF", () => { const result = chef.removeEXIF(fs.readFileSync("test/tests/nodeApi/sampleData/pic.jpg")); - assert.strictEqual(result.toString().length(), 4582); + assert.strictEqual(result.toString().length, 4582); }), it("Scan for embedded files", () => { - const result = chef.scanForEmbeddedFiles(fs.readFileSync("test/tests/nodeApi/sampleData/pic.jpg")); - const expected = `Scanning data for 'magic bytes' which may indicate embedded files. The following results may be false positives and should not be treat as reliable. Any suffiently long file is likely to contain these magic bytes coincidentally. - -Offset 0 (0x00): - File extension: jpg - MIME type: image/jpeg - -Offset 30 (0x1e): - File extension: tif - MIME type: image/tiff - -Offset 212 (0xd4): - File extension: txt - MIME type: text/plain - Description: UTF-8 encoded Unicode byte order mark detected, commonly but not exclusively seen in text files. - - -16 file types were detected that have common byte sequences. These are likely to be false positives. Run this operation with the 'Ignore common byte sequences' option unchecked to see details.`; - assert.strictEqual(result.toString(), expected); + const result = chef.scanForEmbeddedFiles(fs.readFileSync("src/web/static/images/cook_male-32x32.png")); + const expected = "Scanning data for \'magic bytes\' which may indicate embedded files."; + assert.ok(result.toString().indexOf(expected) === 0); }), it("Scrypt", () => { @@ -950,9 +934,9 @@ smothering ampersand abreast it("Detect file type", () => { assert.strictEqual( - chef.detectFileType(fs.readFileSync("test/tests/nodeApi/sampleData/pic.jpg")).toString(), - `File extension: jpg -MIME type: image/jpeg`); + chef.detectFileType(fs.readFileSync("src/web/static/images/cook_male-32x32.png")).toString(), + `File extension: png +MIME type: image/png`); }), it("extract EXIF", () => {