Pārlūkot izejas kodu

Added File and JSON Dish types and updated types for compression ops.

n1474335 7 gadi atpakaļ
vecāks
revīzija
a8aa1bc5e8

+ 45 - 2
src/core/Dish.mjs

@@ -54,6 +54,10 @@ class Dish {
             case "bignumber":
             case "big number":
                 return Dish.BIG_NUMBER;
+            case "json":
+                return Dish.JSON;
+            case "file":
+                return Dish.FILE;
             case "list<file>":
                 return Dish.LIST_FILE;
             default:
@@ -82,6 +86,10 @@ class Dish {
                 return "ArrayBuffer";
             case Dish.BIG_NUMBER:
                 return "BigNumber";
+            case Dish.JSON:
+                return "JSON";
+            case Dish.FILE:
+                return "File";
             case Dish.LIST_FILE:
                 return "List<File>";
             default:
@@ -160,6 +168,13 @@ class Dish {
             case Dish.BIG_NUMBER:
                 this.value = this.value instanceof BigNumber ? Utils.strToByteArray(this.value.toFixed()) : [];
                 break;
+            case Dish.JSON:
+                this.value = this.value ? Utils.strToByteArray(JSON.stringify(this.value)) : [];
+                break;
+            case Dish.FILE:
+                this.value = await Utils.readFile(this.value);
+                this.value = Array.prototype.slice.call(this.value);
+                break;
             case Dish.LIST_FILE:
                 this.value = await Promise.all(this.value.map(async f => Utils.readFile(f)));
                 this.value = this.value.map(b => Array.prototype.slice.call(b));
@@ -194,8 +209,15 @@ class Dish {
                 }
                 this.type = Dish.BIG_NUMBER;
                 break;
-            case Dish.LIST_FILE:
+            case Dish.JSON:
+                this.value = JSON.parse(byteArrayToStr(this.value));
+                this.type = Dish.JSON;
+                break;
+            case Dish.FILE:
                 this.value = new File(this.value, "unknown");
+                break;
+            case Dish.LIST_FILE:
+                this.value = [new File(this.value, "unknown")];
                 this.type = Dish.LIST_FILE;
                 break;
             default:
@@ -235,6 +257,11 @@ class Dish {
                 return this.value instanceof ArrayBuffer;
             case Dish.BIG_NUMBER:
                 return this.value instanceof BigNumber;
+            case Dish.JSON:
+                // All values can be serialised in some manner, so we return true in all cases
+                return true;
+            case Dish.FILE:
+                return this.value instanceof File;
             case Dish.LIST_FILE:
                 return this.value instanceof Array &&
                     this.value.reduce((acc, curr) => acc && curr instanceof File, true);
@@ -262,6 +289,10 @@ class Dish {
                 return this.value.toString().length;
             case Dish.ARRAY_BUFFER:
                 return this.value.byteLength;
+            case Dish.JSON:
+                return JSON.stringify(this.value).length;
+            case Dish.FILE:
+                return this.value.size;
             case Dish.LIST_FILE:
                 return this.value.reduce((acc, curr) => acc + curr.size, 0);
             default:
@@ -308,12 +339,24 @@ Dish.ARRAY_BUFFER = 4;
  * @enum
  */
 Dish.BIG_NUMBER = 5;
+/**
+ * Dish data type enum for JSON.
+ * @readonly
+ * @enum
+ */
+Dish.JSON = 6;
 /**
  * Dish data type enum for lists of files.
  * @readonly
  * @enum
  */
-Dish.LIST_FILE = 6;
+Dish.FILE = 7;
+/**
+* Dish data type enum for lists of files.
+* @readonly
+* @enum
+*/
+Dish.LIST_FILE = 8;
 
 
 export default Dish;

+ 6 - 9
src/core/operations/Gunzip.mjs

@@ -5,7 +5,6 @@
  */
 
 import Operation from "../Operation";
-import Utils from "../Utils";
 import zlibAndGzip from "zlibjs/bin/zlib_and_gzip.min";
 
 const Zlib = zlibAndGzip.Zlib;
@@ -24,21 +23,19 @@ class Gunzip extends Operation {
         this.name = "Gunzip";
         this.module = "Compression";
         this.description = "Decompresses data which has been compressed using the deflate algorithm with gzip headers.";
-        this.inputType = "byteArray";
-        this.outputType = "byteArray";
+        this.inputType = "ArrayBuffer";
+        this.outputType = "ArrayBuffer";
         this.args = [];
     }
 
     /**
-     * @param {byteArray} input
+     * @param {ArrayBuffer} input
      * @param {Object[]} args
-     * @returns {byteArray}
+     * @returns {File}
      */
     run(input, args) {
-        // Deal with character encoding issues
-        input = Utils.strToByteArray(Utils.byteArrayToUtf8(input));
-        const gunzip = new Zlib.Gunzip(input);
-        return Array.prototype.slice.call(gunzip.decompress());
+        const gunzip = new Zlib.Gunzip(new Uint8Array(input));
+        return new Uint8Array(gunzip.decompress()).buffer;
     }
 
 }

+ 6 - 6
src/core/operations/Gzip.mjs

@@ -24,8 +24,8 @@ class Gzip extends Operation {
         this.name = "Gzip";
         this.module = "Compression";
         this.description = "Compresses data using the deflate algorithm with gzip headers.";
-        this.inputType = "byteArray";
-        this.outputType = "byteArray";
+        this.inputType = "ArrayBuffer";
+        this.outputType = "ArrayBuffer";
         this.args = [
             {
                 name: "Compression type",
@@ -51,9 +51,9 @@ class Gzip extends Operation {
     }
 
     /**
-     * @param {byteArray} input
+     * @param {ArrayBuffer} input
      * @param {Object[]} args
-     * @returns {byteArray}
+     * @returns {ArrayBuffer}
      */
     run(input, args) {
         const filename = args[1],
@@ -76,8 +76,8 @@ class Gzip extends Operation {
             options.comment = comment;
         }
 
-        const gzip = new Zlib.Gzip(input, options);
-        return Array.prototype.slice.call(gzip.compress());
+        const gzip = new Zlib.Gzip(new Uint8Array(input), options);
+        return new Uint8Array(gzip.compress()).buffer;
     }
 
 }

+ 6 - 6
src/core/operations/RawDeflate.mjs

@@ -30,8 +30,8 @@ class RawDeflate extends Operation {
         this.name = "Raw Deflate";
         this.module = "Compression";
         this.description = "Compresses data using the deflate algorithm with no headers.";
-        this.inputType = "byteArray";
-        this.outputType = "byteArray";
+        this.inputType = "ArrayBuffer";
+        this.outputType = "ArrayBuffer";
         this.args = [
             {
                 name: "Compression type",
@@ -42,15 +42,15 @@ class RawDeflate extends Operation {
     }
 
     /**
-     * @param {byteArray} input
+     * @param {ArrayBuffer} input
      * @param {Object[]} args
-     * @returns {byteArray}
+     * @returns {ArrayBuffer}
      */
     run(input, args) {
-        const deflate = new Zlib.RawDeflate(input, {
+        const deflate = new Zlib.RawDeflate(new Uint8Array(input), {
             compressionType: RAW_COMPRESSION_TYPE_LOOKUP[args[0]]
         });
-        return Array.prototype.slice.call(deflate.compress());
+        return new Uint8Array(deflate.compress()).buffer;
     }
 
 }

+ 7 - 10
src/core/operations/RawInflate.mjs

@@ -5,7 +5,6 @@
  */
 
 import Operation from "../Operation";
-import Utils from "../Utils";
 import {INFLATE_BUFFER_TYPE} from "../lib/Zlib";
 import rawinflate from "zlibjs/bin/rawinflate.min";
 
@@ -30,8 +29,8 @@ class RawInflate extends Operation {
         this.name = "Raw Inflate";
         this.module = "Compression";
         this.description = "Decompresses data which has been compressed using the deflate algorithm with no headers.";
-        this.inputType = "byteArray";
-        this.outputType = "byteArray";
+        this.inputType = "ArrayBuffer";
+        this.outputType = "ArrayBuffer";
         this.args = [
             {
                 name: "Start index",
@@ -62,21 +61,19 @@ class RawInflate extends Operation {
     }
 
     /**
-     * @param {byteArray} input
+     * @param {ArrayBuffer} input
      * @param {Object[]} args
-     * @returns {byteArray}
+     * @returns {ArrayBuffer}
      */
     run(input, args) {
-        // Deal with character encoding issues
-        input = Utils.strToByteArray(Utils.byteArrayToUtf8(input));
-        const inflate = new Zlib.RawInflate(input, {
+        const inflate = new Zlib.RawInflate(new Uint8Array(input), {
                 index: args[0],
                 bufferSize: args[1],
                 bufferType: RAW_BUFFER_TYPE_LOOKUP[args[2]],
                 resize: args[3],
                 verify: args[4]
             }),
-            result = Array.prototype.slice.call(inflate.decompress());
+            result = new Uint8Array(inflate.decompress());
 
         // Raw Inflate somethimes messes up and returns nonsense like this:
         // ]....]....]....]....]....]....]....]....]....]....]....]....]....]...
@@ -97,7 +94,7 @@ class RawInflate extends Operation {
             }
         }
         // This seems to be the easiest way...
-        return result;
+        return result.buffer;
     }
 
 }

+ 3 - 3
src/core/operations/Unzip.mjs

@@ -24,7 +24,7 @@ class Unzip extends Operation {
         this.name = "Unzip";
         this.module = "Compression";
         this.description = "Decompresses data using the PKZIP algorithm and displays it per file, with support for passwords.";
-        this.inputType = "byteArray";
+        this.inputType = "ArrayBuffer";
         this.outputType = "List<File>";
         this.presentType = "html";
         this.args = [
@@ -42,7 +42,7 @@ class Unzip extends Operation {
     }
 
     /**
-     * @param {byteArray} input
+     * @param {ArrayBuffer} input
      * @param {Object[]} args
      * @returns {File[]}
      */
@@ -51,7 +51,7 @@ class Unzip extends Operation {
                 password: Utils.strToByteArray(args[0]),
                 verify: args[1]
             },
-            unzip = new Zlib.Unzip(input, options),
+            unzip = new Zlib.Unzip(new Uint8Array(input), options),
             filenames = unzip.getFilenames();
 
         return filenames.map(fileName => {

+ 9 - 8
src/core/operations/Zip.mjs

@@ -36,8 +36,8 @@ class Zip extends Operation {
         this.name = "Zip";
         this.module = "Compression";
         this.description = "Compresses data using the PKZIP algorithm with the given filename.<br><br>No support for multiple files at this time.";
-        this.inputType = "byteArray";
-        this.outputType = "byteArray";
+        this.inputType = "ArrayBuffer";
+        this.outputType = "File";
         this.args = [
             {
                 name: "Filename",
@@ -73,14 +73,15 @@ class Zip extends Operation {
     }
 
     /**
-     * @param {byteArray} input
+     * @param {ArrayBuffer} input
      * @param {Object[]} args
-     * @returns {byteArray}
+     * @returns {File}
      */
     run(input, args) {
-        const password = Utils.strToByteArray(args[2]),
+        const filename = args[0],
+            password = Utils.strToByteArray(args[2]),
             options = {
-                filename: Utils.strToByteArray(args[0]),
+                filename: Utils.strToByteArray(filename),
                 comment: Utils.strToByteArray(args[1]),
                 compressionMethod: ZIP_COMPRESSION_METHOD_LOOKUP[args[3]],
                 os: ZIP_OS_LOOKUP[args[4]],
@@ -92,8 +93,8 @@ class Zip extends Operation {
 
         if (password.length)
             zip.setPassword(password);
-        zip.addFile(input, options);
-        return Array.prototype.slice.call(zip.compress());
+        zip.addFile(new Uint8Array(input), options);
+        return new File([zip.compress()], filename);
     }
 
 }

+ 6 - 6
src/core/operations/ZlibDeflate.mjs

@@ -24,8 +24,8 @@ class ZlibDeflate extends Operation {
         this.name = "Zlib Deflate";
         this.module = "Compression";
         this.description = "Compresses data using the deflate algorithm adding zlib headers.";
-        this.inputType = "byteArray";
-        this.outputType = "byteArray";
+        this.inputType = "ArrayBuffer";
+        this.outputType = "ArrayBuffer";
         this.args = [
             {
                 name: "Compression type",
@@ -36,15 +36,15 @@ class ZlibDeflate extends Operation {
     }
 
     /**
-     * @param {byteArray} input
+     * @param {ArrayBuffer} input
      * @param {Object[]} args
-     * @returns {byteArray}
+     * @returns {ArrayBuffer}
      */
     run(input, args) {
-        const deflate = new Zlib.Deflate(input, {
+        const deflate = new Zlib.Deflate(new Uint8Array(input), {
             compressionType: ZLIB_COMPRESSION_TYPE_LOOKUP[args[0]]
         });
-        return Array.prototype.slice.call(deflate.compress());
+        return new Uint8Array(deflate.compress()).buffer;
     }
 
 }

+ 6 - 9
src/core/operations/ZlibInflate.mjs

@@ -5,7 +5,6 @@
  */
 
 import Operation from "../Operation";
-import Utils from "../Utils";
 import {INFLATE_BUFFER_TYPE} from "../lib/Zlib";
 import zlibAndGzip from "zlibjs/bin/zlib_and_gzip.min";
 
@@ -30,8 +29,8 @@ class ZlibInflate extends Operation {
         this.name = "Zlib Inflate";
         this.module = "Compression";
         this.description = "Decompresses data which has been compressed using the deflate algorithm with zlib headers.";
-        this.inputType = "byteArray";
-        this.outputType = "byteArray";
+        this.inputType = "ArrayBuffer";
+        this.outputType = "ArrayBuffer";
         this.args = [
             {
                 name: "Start index",
@@ -62,21 +61,19 @@ class ZlibInflate extends Operation {
     }
 
     /**
-     * @param {byteArray} input
+     * @param {ArrayBuffer} input
      * @param {Object[]} args
-     * @returns {byteArray}
+     * @returns {ArrayBuffer}
      */
     run(input, args) {
-        // Deal with character encoding issues
-        input = Utils.strToByteArray(Utils.byteArrayToUtf8(input));
-        const inflate = new Zlib.Inflate(input, {
+        const inflate = new Zlib.Inflate(new Uint8Array(input), {
             index: args[0],
             bufferSize: args[1],
             bufferType: ZLIB_BUFFER_TYPE_LOOKUP[args[2]],
             resize: args[3],
             verify: args[4]
         });
-        return Array.prototype.slice.call(inflate.decompress());
+        return new Uint8Array(inflate.decompress()).buffer;
     }
 
 }