WIP HAD to move NodeDish out - NONE of it is async!

This commit is contained in:
d98762625 2019-02-15 15:20:05 +00:00
parent aafde8986d
commit 04b7f2fa8c
16 changed files with 442 additions and 411 deletions

View file

@ -74,6 +74,7 @@ class Dish {
case "list<file>":
return Dish.LIST_FILE;
default:
console.log(typeStr);
throw new DishError("Invalid data type string. No matching enum.");
}
}
@ -383,7 +384,6 @@ class Dish {
return newDish;
}
}

View file

@ -9,6 +9,7 @@ import {fromBase64, toBase64} from "./lib/Base64";
import {fromHex} from "./lib/Hex";
import {fromDecimal} from "./lib/Decimal";
import {fromBinary} from "./lib/Binary";
import { fstat } from "fs";
/**
@ -919,7 +920,7 @@ class Utils {
/**
* Reads a File and returns the data as a Uint8Array.
*
* @param {File} file
* @param {File | for node: array|arrayBuffer|buffer|string} file
* @returns {Uint8Array}
*
* @example
@ -927,33 +928,49 @@ class Utils {
* await Utils.readFile(new File(["hello"], "test"))
*/
static readFile(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
const data = new Uint8Array(file.size);
let offset = 0;
const CHUNK_SIZE = 10485760; // 10MiB
if (Utils.isBrowser()) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
const data = new Uint8Array(file.size);
let offset = 0;
const CHUNK_SIZE = 10485760; // 10MiB
const seek = function() {
if (offset >= file.size) {
resolve(data);
return;
}
const slice = file.slice(offset, offset + CHUNK_SIZE);
reader.readAsArrayBuffer(slice);
};
const seek = function() {
if (offset >= file.size) {
resolve(data);
return;
}
const slice = file.slice(offset, offset + CHUNK_SIZE);
reader.readAsArrayBuffer(slice);
};
reader.onload = function(e) {
data.set(new Uint8Array(reader.result), offset);
offset += CHUNK_SIZE;
seek();
};
reader.onerror = function(e) {
reject(reader.error.message);
};
reader.onload = function(e) {
data.set(new Uint8Array(reader.result), offset);
offset += CHUNK_SIZE;
seek();
};
});
reader.onerror = function(e) {
reject(reader.error.message);
};
} else if (Utils.isNode()) {
return Buffer.from(file).buffer;
}
seek();
});
throw new Error("Unkown environment!");
}
/** */
static readFileSync(file) {
if (Utils.isBrowser()) {
throw new TypeError("Browser environment cannot support readFileSync");
}
return Buffer.from(file).buffer;
}
@ -1050,6 +1067,20 @@ class Utils {
}[token];
}
/**
* Check if code is running in a browser environment
*/
static isBrowser() {
return typeof window !== "undefined" && typeof window.document !== "undefined";
}
/**
* Check if code is running in a Node environment
*/
static isNode() {
return typeof process !== "undefined" && process.versions != null && process.versions.node != null;
}
}
export default Utils;

View file

@ -132,6 +132,8 @@ class Tar extends Operation {
tarball.writeBytes(input);
tarball.writeEndBlocks();
console.log("here");
return new File([new Uint8Array(tarball.bytes)], args[0]);
}

View file

@ -131,6 +131,7 @@ class Untar extends Operation {
* @returns {html}
*/
async present(files) {
console.log("err....");
return await Utils.displayFilesAsHTML(files);
}