mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 14:56:19 -04:00
correct translation from node Buffer to byte array
This commit is contained in:
parent
b98cab7d62
commit
638093d40e
3 changed files with 7 additions and 5 deletions
|
@ -36,7 +36,6 @@ class Dish {
|
||||||
* literal input
|
* literal input
|
||||||
*/
|
*/
|
||||||
constructor(dishOrInput=null, type = null) {
|
constructor(dishOrInput=null, type = null) {
|
||||||
|
|
||||||
this.value = [];
|
this.value = [];
|
||||||
this.type = Dish.BYTE_ARRAY;
|
this.type = Dish.BYTE_ARRAY;
|
||||||
|
|
||||||
|
@ -46,7 +45,7 @@ class Dish {
|
||||||
dishOrInput.hasOwnProperty("type")) {
|
dishOrInput.hasOwnProperty("type")) {
|
||||||
this.set(dishOrInput.value, dishOrInput.type);
|
this.set(dishOrInput.value, dishOrInput.type);
|
||||||
// input and type defined separately
|
// input and type defined separately
|
||||||
} else if (dishOrInput && type) {
|
} else if (dishOrInput && type !== null) {
|
||||||
this.set(dishOrInput, type);
|
this.set(dishOrInput, type);
|
||||||
// No type declared, so infer it.
|
// No type declared, so infer it.
|
||||||
} else if (dishOrInput) {
|
} else if (dishOrInput) {
|
||||||
|
|
|
@ -22,9 +22,9 @@ class NodeDish extends Dish {
|
||||||
|
|
||||||
// Allow `fs` file input:
|
// Allow `fs` file input:
|
||||||
// Any node fs Buffers transformed to array buffer
|
// 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)) {
|
if (Buffer.isBuffer(inputOrDish)) {
|
||||||
inputOrDish = new Uint8Array(inputOrDish).buffer;
|
inputOrDish = Array.from(inputOrDish);
|
||||||
type = Dish.BYTE_ARRAY;
|
type = Dish.BYTE_ARRAY;
|
||||||
}
|
}
|
||||||
super(inputOrDish, type);
|
super(inputOrDish, type);
|
||||||
|
|
|
@ -357,6 +357,9 @@ TestRegister.addApiTests([
|
||||||
const arrayBufferDish = new Dish(Buffer.from("some buffer input").buffer);
|
const arrayBufferDish = new Dish(Buffer.from("some buffer input").buffer);
|
||||||
assert.strictEqual(arrayBufferDish.type, 4);
|
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"});
|
const JSONDish = new Dish({key: "value"});
|
||||||
assert.strictEqual(JSONDish.type, 6);
|
assert.strictEqual(JSONDish.type, 6);
|
||||||
}),
|
}),
|
||||||
|
@ -364,7 +367,7 @@ TestRegister.addApiTests([
|
||||||
it("Composable dish: Buffer type dishes should be converted to strings", () => {
|
it("Composable dish: Buffer type dishes should be converted to strings", () => {
|
||||||
fs.writeFileSync("test.txt", "abc");
|
fs.writeFileSync("test.txt", "abc");
|
||||||
const dish = new Dish(fs.readFileSync("test.txt"));
|
const dish = new Dish(fs.readFileSync("test.txt"));
|
||||||
assert.strictEqual(dish.type, 4);
|
assert.strictEqual(dish.type, 0);
|
||||||
fs.unlinkSync("test.txt");
|
fs.unlinkSync("test.txt");
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue