From 2019ae43d7fe12956b5145d11a68713284039782 Mon Sep 17 00:00:00 2001 From: d98762625 Date: Thu, 14 Mar 2019 16:33:09 +0000 Subject: [PATCH] File shim now translates correctly --- package.json | 2 +- src/core/Dish.mjs | 2 -- src/core/Utils.mjs | 11 +++++------ src/core/dishTranslationTypes/DishFile.mjs | 11 ++--------- src/core/dishTranslationTypes/DishString.mjs | 2 -- src/core/operations/Tar.mjs | 2 -- src/node/File.mjs | 12 ++++++------ 7 files changed, 14 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index caf10d64..c300d179 100644 --- a/package.json +++ b/package.json @@ -144,11 +144,11 @@ "build": "grunt prod", "node": "NODE_ENV=development grunt node", "node-prod": "NODE_ENV=production grunt node", + "repl": "grunt node && node build/node/CyberChef-repl.js", "test": "grunt test", "testui": "grunt testui", "docs": "grunt docs", "lint": "grunt lint", - "repl": "node --experimental-modules --no-warnings src/node/repl-index.mjs", "newop": "node --experimental-modules src/core/config/scripts/newOperation.mjs", "postinstall": "[ -f node_modules/crypto-api/src/crypto-api.mjs ] || npx j2m node_modules/crypto-api/src/crypto-api.js" } diff --git a/src/core/Dish.mjs b/src/core/Dish.mjs index 104bbc2b..452be80d 100755 --- a/src/core/Dish.mjs +++ b/src/core/Dish.mjs @@ -336,13 +336,11 @@ class Dish { // Node environment => translate is sync if (Utils.isNode()) { - console.log('Running in node'); this._toByteArray(); this._fromByteArray(toType, notUTF8); // Browser environment => translate is async } else { - console.log('Running in browser'); return new Promise((resolve, reject) => { this._toByteArray() .then(() => this.type = Dish.BYTE_ARRAY) diff --git a/src/core/Utils.mjs b/src/core/Utils.mjs index b61357de..979b6482 100755 --- a/src/core/Utils.mjs +++ b/src/core/Utils.mjs @@ -472,7 +472,6 @@ class Utils { const str = Utils.byteArrayToChars(byteArray); try { const utf8Str = utf8.decode(str); - if (str.length !== utf8Str.length) { if (ENVIRONMENT_IS_WORKER()) { self.setOption("attemptHighlight", false); @@ -966,12 +965,12 @@ class Utils { if (!Utils.isNode()) { throw new TypeError("Browser environment cannot support readFileSync"); } + let bytes = []; + for (const byte of file.data.values()) { + bytes = bytes.concat(byte); + } - console.log('readFileSync:'); - console.log(file); - console.log(Buffer.from(file.data).toString()); - - return Buffer.from(file.data).buffer; + return bytes; } diff --git a/src/core/dishTranslationTypes/DishFile.mjs b/src/core/dishTranslationTypes/DishFile.mjs index b04bd462..9e1df730 100644 --- a/src/core/dishTranslationTypes/DishFile.mjs +++ b/src/core/dishTranslationTypes/DishFile.mjs @@ -19,12 +19,7 @@ class DishFile extends DishTranslationType { static toByteArray() { DishFile.checkForValue(this.value); if (Utils.isNode()) { - console.log('toByteArray original value:'); - console.log(this.value); - // this.value = Utils.readFileSync(this.value); - this.value = Array.prototype.slice.call(Utils.readFileSync(this.value)); - console.log('toByteArray value:'); - console.log(this.value); + this.value = Utils.readFileSync(this.value); } else { return new Promise((resolve, reject) => { Utils.readFile(this.value) @@ -42,9 +37,7 @@ class DishFile extends DishTranslationType { */ static fromByteArray() { DishFile.checkForValue(this.value); - this.value = new File(this.value, "unknown"); - console.log('from Byte array'); - console.log(this.value); + this.value = new File(this.value, "file.txt"); } } diff --git a/src/core/dishTranslationTypes/DishString.mjs b/src/core/dishTranslationTypes/DishString.mjs index 40b23001..78c273c6 100644 --- a/src/core/dishTranslationTypes/DishString.mjs +++ b/src/core/dishTranslationTypes/DishString.mjs @@ -17,10 +17,8 @@ class DishString extends DishTranslationType { * convert the given value to a ByteArray */ static toByteArray() { - console.log('string to byte array'); DishString.checkForValue(this.value); this.value = this.value ? Utils.strToByteArray(this.value) : []; - console.log(this.value); } /** diff --git a/src/core/operations/Tar.mjs b/src/core/operations/Tar.mjs index 10748340..84674bff 100644 --- a/src/core/operations/Tar.mjs +++ b/src/core/operations/Tar.mjs @@ -132,8 +132,6 @@ class Tar extends Operation { tarball.writeBytes(input); tarball.writeEndBlocks(); - console.log('Tar bytes'); - console.log(tarball.bytes); return new File([new Uint8Array(tarball.bytes)], args[0]); } diff --git a/src/node/File.mjs b/src/node/File.mjs index 938c8fd4..33c0dc73 100644 --- a/src/node/File.mjs +++ b/src/node/File.mjs @@ -19,21 +19,21 @@ class File { /** * Constructor * + * https://w3c.github.io/FileAPI/#file-constructor + * * @param {String|Array|ArrayBuffer|Buffer} bits - file content * @param {String} name (optional) - file name * @param {Object} stats (optional) - file stats e.g. lastModified */ constructor(data, name="", stats={}) { - // Look at File API definition to see how to handle this. - this.data = Buffer.from(data[0]); + const buffers = data.map(d => Buffer.from(d)); + const totalLength = buffers.reduce((p, c) => p + c.length, 0); + this.data = Buffer.concat(buffers, totalLength); + this.name = name; this.lastModified = stats.lastModified || Date.now(); this.type = stats.type || mime.getType(this.name); - console.log('File constructor'); - console.log(typeof data); - console.log(data); - console.log(this.data); } /**