Browse Source

File shim now translates correctly

d98762625 6 years ago
parent
commit
2019ae43d7

+ 1 - 1
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"
   }

+ 0 - 2
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)

+ 5 - 6
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;
     }
 
 

+ 2 - 9
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");
     }
 }
 

+ 0 - 2
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);
     }
 
     /**

+ 0 - 2
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]);
     }
 

+ 6 - 6
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);
     }
 
     /**