Skip to content

Commit

Permalink
Added File and JSON Dish types and updated types for compression ops.
Browse files Browse the repository at this point in the history
  • Loading branch information
n1474335 committed Apr 21, 2018
1 parent 76a066a commit a8aa1bc
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 59 deletions.
47 changes: 45 additions & 2 deletions src/core/Dish.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
15 changes: 6 additions & 9 deletions src/core/operations/Gunzip.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

}
Expand Down
12 changes: 6 additions & 6 deletions src/core/operations/Gzip.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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],
Expand All @@ -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;
}

}
Expand Down
12 changes: 6 additions & 6 deletions src/core/operations/RawDeflate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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;
}

}
Expand Down
17 changes: 7 additions & 10 deletions src/core/operations/RawInflate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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",
Expand Down Expand Up @@ -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:
// ]....]....]....]....]....]....]....]....]....]....]....]....]....]...
Expand All @@ -97,7 +94,7 @@ class RawInflate extends Operation {
}
}
// This seems to be the easiest way...
return result;
return result.buffer;
}

}
Expand Down
6 changes: 3 additions & 3 deletions src/core/operations/Unzip.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -42,7 +42,7 @@ class Unzip extends Operation {
}

/**
* @param {byteArray} input
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {File[]}
*/
Expand All @@ -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 => {
Expand Down
17 changes: 9 additions & 8 deletions src/core/operations/Zip.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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]],
Expand All @@ -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);
}

}
Expand Down
12 changes: 6 additions & 6 deletions src/core/operations/ZlibDeflate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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;
}

}
Expand Down
Loading

0 comments on commit a8aa1bc

Please sign in to comment.