Remove Buffer from Dish. Convert into ByteArray in api and then pass in as ArrayBuffer. Add some PNG files to tests

This commit is contained in:
d98762625 2018-08-19 22:55:40 +01:00
parent 20bb104006
commit 55f7cac526
3 changed files with 14 additions and 45 deletions

View file

@ -61,8 +61,6 @@ class Dish {
return Dish.FILE; return Dish.FILE;
case "list<file>": case "list<file>":
return Dish.LIST_FILE; return Dish.LIST_FILE;
case "buffer":
return Dish.BUFFER;
default: default:
throw "Invalid data type string. No matching enum."; throw "Invalid data type string. No matching enum.";
} }
@ -95,8 +93,6 @@ class Dish {
return "File"; return "File";
case Dish.LIST_FILE: case Dish.LIST_FILE:
return "List<File>"; return "List<File>";
case Dish.BUFFER:
return "Buffer";
default: default:
throw "Invalid data type enum. No matching type."; throw "Invalid data type enum. No matching type.";
} }
@ -270,8 +266,6 @@ class Dish {
case Dish.LIST_FILE: case Dish.LIST_FILE:
return this.value instanceof Array && return this.value instanceof Array &&
this.value.reduce((acc, curr) => acc && curr instanceof File, true); this.value.reduce((acc, curr) => acc && curr instanceof File, true);
case Dish.BUFFER:
return this.value instanceof Buffer;
default: default:
return false; return false;
} }
@ -290,7 +284,6 @@ class Dish {
case Dish.BYTE_ARRAY: case Dish.BYTE_ARRAY:
case Dish.STRING: case Dish.STRING:
case Dish.HTML: case Dish.HTML:
case Dish.BUFFER:
return this.value.length; return this.value.length;
case Dish.NUMBER: case Dish.NUMBER:
case Dish.BIG_NUMBER: case Dish.BIG_NUMBER:
@ -364,12 +357,6 @@ class Dish {
this.type this.type
); );
break; break;
case Dish.BUFFER:
newDish.set(
Buffer.from(this.value),
this.type
);
break;
default: default:
throw new Error("Cannot clone Dish, unknown type"); throw new Error("Cannot clone Dish, unknown type");
} }
@ -434,12 +421,5 @@ Dish.FILE = 7;
* @enum * @enum
*/ */
Dish.LIST_FILE = 8; Dish.LIST_FILE = 8;
/**
* Dish data type enum for node Buffer.
* @readonly
* @enum
*/
Dish.BUFFER = 9;
export default Dish; export default Dish;

View file

@ -108,15 +108,20 @@ function ensureIsDish(input) {
dish.set(input, type); dish.set(input, type);
} }
return dish; return dish;
}; }
/** /**
* prepareOp: transform args, make input the right type. * prepareOp: transform args, make input the right type.
* Also convert any Buffers to ArrayBuffers.
* @param opInstance - instance of the operation * @param opInstance - instance of the operation
* @param input - operation input * @param input - operation input
* @param args - operation args * @param args - operation args
*/ */
function prepareOp(opInstance, input, args) { function prepareOp(opInstance, input, args) {
// convert any Buffers into ArrayBuffers.
if (input instanceof Buffer) {
input = input.buffer;
}
const dish = ensureIsDish(input); const dish = ensureIsDish(input);
let transformedArgs; let transformedArgs;
// Transform object-style args to original args array // Transform object-style args to original args array
@ -127,7 +132,7 @@ function prepareOp(opInstance, input, args) {
} }
const transformedInput = dish.get(opInstance.inputType); const transformedInput = dish.get(opInstance.inputType);
return {transformedInput, transformedArgs}; return {transformedInput, transformedArgs};
}; }
/** /**
* Wrap an operation to be consumed by node API. * Wrap an operation to be consumed by node API.

View file

@ -786,29 +786,13 @@ jmPGsv1elXxVzqs58UZLD2c3vBhGkU2BV6kRKh+lj/EcVrzsFhGCz/7DKxPoDHLS
it("Remove EXIF", () => { it("Remove EXIF", () => {
const result = chef.removeEXIF(fs.readFileSync("test/tests/nodeApi/sampleData/pic.jpg")); 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", () => { it("Scan for embedded files", () => {
const result = chef.scanForEmbeddedFiles(fs.readFileSync("test/tests/nodeApi/sampleData/pic.jpg")); 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. 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. const expected = "Scanning data for \'magic bytes\' which may indicate embedded files.";
assert.ok(result.toString().indexOf(expected) === 0);
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);
}), }),
it("Scrypt", () => { it("Scrypt", () => {
@ -950,9 +934,9 @@ smothering ampersand abreast
it("Detect file type", () => { it("Detect file type", () => {
assert.strictEqual( assert.strictEqual(
chef.detectFileType(fs.readFileSync("test/tests/nodeApi/sampleData/pic.jpg")).toString(), chef.detectFileType(fs.readFileSync("src/web/static/images/cook_male-32x32.png")).toString(),
`File extension: jpg `File extension: png
MIME type: image/jpeg`); MIME type: image/png`);
}), }),
it("extract EXIF", () => { it("extract EXIF", () => {