File shim now translates correctly

This commit is contained in:
d98762625 2019-03-14 16:33:09 +00:00
parent d080c5dd14
commit 2019ae43d7
7 changed files with 14 additions and 28 deletions

View File

@ -144,11 +144,11 @@
"build": "grunt prod", "build": "grunt prod",
"node": "NODE_ENV=development grunt node", "node": "NODE_ENV=development grunt node",
"node-prod": "NODE_ENV=production grunt node", "node-prod": "NODE_ENV=production grunt node",
"repl": "grunt node && node build/node/CyberChef-repl.js",
"test": "grunt test", "test": "grunt test",
"testui": "grunt testui", "testui": "grunt testui",
"docs": "grunt docs", "docs": "grunt docs",
"lint": "grunt lint", "lint": "grunt lint",
"repl": "node --experimental-modules --no-warnings src/node/repl-index.mjs",
"newop": "node --experimental-modules src/core/config/scripts/newOperation.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" "postinstall": "[ -f node_modules/crypto-api/src/crypto-api.mjs ] || npx j2m node_modules/crypto-api/src/crypto-api.js"
} }

View File

@ -336,13 +336,11 @@ class Dish {
// Node environment => translate is sync // Node environment => translate is sync
if (Utils.isNode()) { if (Utils.isNode()) {
console.log('Running in node');
this._toByteArray(); this._toByteArray();
this._fromByteArray(toType, notUTF8); this._fromByteArray(toType, notUTF8);
// Browser environment => translate is async // Browser environment => translate is async
} else { } else {
console.log('Running in browser');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._toByteArray() this._toByteArray()
.then(() => this.type = Dish.BYTE_ARRAY) .then(() => this.type = Dish.BYTE_ARRAY)

View File

@ -472,7 +472,6 @@ class Utils {
const str = Utils.byteArrayToChars(byteArray); const str = Utils.byteArrayToChars(byteArray);
try { try {
const utf8Str = utf8.decode(str); const utf8Str = utf8.decode(str);
if (str.length !== utf8Str.length) { if (str.length !== utf8Str.length) {
if (ENVIRONMENT_IS_WORKER()) { if (ENVIRONMENT_IS_WORKER()) {
self.setOption("attemptHighlight", false); self.setOption("attemptHighlight", false);
@ -966,12 +965,12 @@ class Utils {
if (!Utils.isNode()) { if (!Utils.isNode()) {
throw new TypeError("Browser environment cannot support readFileSync"); throw new TypeError("Browser environment cannot support readFileSync");
} }
let bytes = [];
for (const byte of file.data.values()) {
bytes = bytes.concat(byte);
}
console.log('readFileSync:'); return bytes;
console.log(file);
console.log(Buffer.from(file.data).toString());
return Buffer.from(file.data).buffer;
} }

View File

@ -19,12 +19,7 @@ class DishFile extends DishTranslationType {
static toByteArray() { static toByteArray() {
DishFile.checkForValue(this.value); DishFile.checkForValue(this.value);
if (Utils.isNode()) { if (Utils.isNode()) {
console.log('toByteArray original value:'); this.value = Utils.readFileSync(this.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);
} else { } else {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
Utils.readFile(this.value) Utils.readFile(this.value)
@ -42,9 +37,7 @@ class DishFile extends DishTranslationType {
*/ */
static fromByteArray() { static fromByteArray() {
DishFile.checkForValue(this.value); DishFile.checkForValue(this.value);
this.value = new File(this.value, "unknown"); this.value = new File(this.value, "file.txt");
console.log('from Byte array');
console.log(this.value);
} }
} }

View File

@ -17,10 +17,8 @@ class DishString extends DishTranslationType {
* convert the given value to a ByteArray * convert the given value to a ByteArray
*/ */
static toByteArray() { static toByteArray() {
console.log('string to byte array');
DishString.checkForValue(this.value); DishString.checkForValue(this.value);
this.value = this.value ? Utils.strToByteArray(this.value) : []; this.value = this.value ? Utils.strToByteArray(this.value) : [];
console.log(this.value);
} }
/** /**

View File

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

View File

@ -19,21 +19,21 @@ class File {
/** /**
* Constructor * Constructor
* *
* https://w3c.github.io/FileAPI/#file-constructor
*
* @param {String|Array|ArrayBuffer|Buffer} bits - file content * @param {String|Array|ArrayBuffer|Buffer} bits - file content
* @param {String} name (optional) - file name * @param {String} name (optional) - file name
* @param {Object} stats (optional) - file stats e.g. lastModified * @param {Object} stats (optional) - file stats e.g. lastModified
*/ */
constructor(data, name="", stats={}) { constructor(data, name="", stats={}) {
// Look at File API definition to see how to handle this. const buffers = data.map(d => Buffer.from(d));
this.data = Buffer.from(data[0]); const totalLength = buffers.reduce((p, c) => p + c.length, 0);
this.data = Buffer.concat(buffers, totalLength);
this.name = name; this.name = name;
this.lastModified = stats.lastModified || Date.now(); this.lastModified = stats.lastModified || Date.now();
this.type = stats.type || mime.getType(this.name); this.type = stats.type || mime.getType(this.name);
console.log('File constructor');
console.log(typeof data);
console.log(data);
console.log(this.data);
} }
/** /**