Skip to content

Commit f6b52b7

Browse files
committed
Operations can now set options from within the worker
1 parent 13f07ab commit f6b52b7

File tree

10 files changed

+81
-38
lines changed

10 files changed

+81
-38
lines changed

.eslintrc.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@
9797

9898
"COMPILE_TIME": false,
9999
"COMPILE_MSG": false,
100-
"PKG_VERSION": false
100+
"PKG_VERSION": false,
101+
"ENVIRONMENT_IS_WORKER": false,
102+
"ENVIRONMENT_IS_NODE": false,
103+
"ENVIRONMENT_IS_WEB": false
101104
}
102105
}

Gruntfile.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,16 @@ module.exports = function (grunt) {
7272
BUILD_CONSTANTS = {
7373
COMPILE_TIME: JSON.stringify(compileTime),
7474
COMPILE_MSG: JSON.stringify(grunt.option("compile-msg") || grunt.option("msg") || ""),
75-
PKG_VERSION: JSON.stringify(pkg.version)
75+
PKG_VERSION: JSON.stringify(pkg.version),
76+
ENVIRONMENT_IS_WORKER: function() {
77+
return typeof importScripts === "function";
78+
},
79+
ENVIRONMENT_IS_NODE: function() {
80+
return typeof process === "object" && typeof require === "function";
81+
},
82+
ENVIRONMENT_IS_WEB: function() {
83+
return typeof window === "object";
84+
}
7685
},
7786
moduleEntryPoints = listEntryModules();
7887

@@ -277,7 +286,10 @@ module.exports = function (grunt) {
277286
output: {
278287
filename: "index.js",
279288
path: __dirname + "/build/test"
280-
}
289+
},
290+
plugins: [
291+
new webpack.DefinePlugin(BUILD_CONSTANTS)
292+
]
281293
},
282294
node: {
283295
target: "node",
@@ -288,7 +300,10 @@ module.exports = function (grunt) {
288300
path: __dirname + "/build/node",
289301
library: "CyberChef",
290302
libraryTarget: "commonjs2"
291-
}
303+
},
304+
plugins: [
305+
new webpack.DefinePlugin(BUILD_CONSTANTS)
306+
]
292307
}
293308
},
294309
"webpack-dev-server": {

src/core/Chef.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ const Chef = function() {
3030
* @returns {string} response.result - The output of the recipe
3131
* @returns {string} response.type - The data type of the result
3232
* @returns {number} response.progress - The position that we have got to in the recipe
33-
* @returns {number} response.options - The app options object (which may have been changed)
3433
* @returns {number} response.duration - The number of ms it took to execute the recipe
3534
* @returns {number} response.error - The error object thrown by a failed operation (false if no error)
3635
*/
@@ -40,12 +39,7 @@ Chef.prototype.bake = async function(inputText, recipeConfig, options, progress,
4039
containsFc = recipe.containsFlowControl(),
4140
error = false;
4241

43-
// Reset attemptHighlight flag
44-
if (options.hasOwnProperty("attemptHighlight")) {
45-
options.attemptHighlight = true;
46-
}
47-
48-
if (containsFc) options.attemptHighlight = false;
42+
if (containsFc && ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
4943

5044
// Clean up progress
5145
if (progress >= recipeConfig.length) {
@@ -86,7 +80,6 @@ Chef.prototype.bake = async function(inputText, recipeConfig, options, progress,
8680
this.dish.get(Dish.STRING),
8781
type: Dish.enumLookup(this.dish.type),
8882
progress: progress,
89-
options: options,
9083
duration: new Date().getTime() - startTime,
9184
error: error
9285
};

src/core/ChefWorker.js

+25-7
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,24 @@ self.postMessage({
4141
*/
4242
self.addEventListener("message", function(e) {
4343
// Handle message
44-
switch (e.data.action) {
44+
const r = e.data;
45+
switch (r.action) {
4546
case "bake":
46-
bake(e.data.data);
47+
bake(r.data);
4748
break;
4849
case "silentBake":
49-
silentBake(e.data.data);
50+
silentBake(r.data);
5051
break;
5152
case "docURL":
5253
// Used to set the URL of the current document so that scripts can be
5354
// imported into an inline worker.
54-
self.docURL = e.data.data;
55+
self.docURL = r.data;
5556
break;
5657
case "highlight":
5758
calculateHighlights(
58-
e.data.data.recipeConfig,
59-
e.data.data.direction,
60-
e.data.data.pos
59+
r.data.recipeConfig,
60+
r.data.direction,
61+
r.data.pos
6162
);
6263
break;
6364
default:
@@ -158,3 +159,20 @@ self.sendStatusMessage = function(msg) {
158159
data: msg
159160
});
160161
};
162+
163+
164+
/**
165+
* Send an option value update to the app.
166+
*
167+
* @param {string} option
168+
* @param {*} value
169+
*/
170+
self.setOption = function(option, value) {
171+
self.postMessage({
172+
action: "optionUpdate",
173+
data: {
174+
option: option,
175+
value: value
176+
}
177+
});
178+
};

src/core/Utils.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ const Utils = {
234234
* @returns {string}
235235
*/
236236
printable: function(str, preserveWs) {
237-
if (typeof window !== "undefined" && window.app && !window.app.options.treatAsUtf8) {
237+
if (ENVIRONMENT_IS_WEB() && window.app && !window.app.options.treatAsUtf8) {
238238
str = Utils.byteArrayToChars(Utils.strToByteArray(str));
239239
}
240240

@@ -384,8 +384,12 @@ const Utils = {
384384
let wordArray = CryptoJS.enc.Utf8.parse(str),
385385
byteArray = Utils.wordArrayToByteArray(wordArray);
386386

387-
if (typeof window !== "undefined" && str.length !== wordArray.sigBytes) {
388-
window.app.options.attemptHighlight = false;
387+
if (str.length !== wordArray.sigBytes) {
388+
if (ENVIRONMENT_IS_WORKER()) {
389+
self.setOption("attemptHighlight", false);
390+
} else if (ENVIRONMENT_IS_WEB()) {
391+
window.app.options.attemptHighlight = false;
392+
}
389393
}
390394
return byteArray;
391395
},
@@ -448,8 +452,13 @@ const Utils = {
448452
let wordArray = new CryptoJS.lib.WordArray.init(words, byteArray.length),
449453
str = CryptoJS.enc.Utf8.stringify(wordArray);
450454

451-
if (typeof window !== "undefined" && str.length !== wordArray.sigBytes)
452-
window.app.options.attemptHighlight = false;
455+
if (str.length !== wordArray.sigBytes) {
456+
if (ENVIRONMENT_IS_WORKER()) {
457+
self.setOption("attemptHighlight", false);
458+
} else if (ENVIRONMENT_IS_WEB()) {
459+
window.app.options.attemptHighlight = false;
460+
}
461+
}
453462
return str;
454463
} catch (err) {
455464
// If it fails, treat it as ANSI

src/core/operations/BitwiseOp.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ const BitwiseOp = {
137137

138138
input = input.slice(sampleOffset, sampleOffset + sampleLength);
139139

140-
if (self) self.sendStatusMessage("Calculating " + Math.pow(256, keyLength) + " values...");
140+
if (ENVIRONMENT_IS_WORKER())
141+
self.sendStatusMessage("Calculating " + Math.pow(256, keyLength) + " values...");
141142

142143
/**
143144
* Converts an integer to an array of bytes expressing that number.
@@ -156,7 +157,7 @@ const BitwiseOp = {
156157
};
157158

158159
for (let key = 1, l = Math.pow(256, keyLength); key < l; key++) {
159-
if (key % 10000 === 0 && self) {
160+
if (key % 10000 === 0 && ENVIRONMENT_IS_WORKER()) {
160161
self.sendStatusMessage("Calculating " + l + " values... " + Math.floor(key / l * 100) + "%");
161162
}
162163

src/core/operations/ByteRepr.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ const ByteRepr = {
119119
else if (ordinal < 4294967296) padding = 8;
120120
else padding = 2;
121121

122-
if (padding > 2 && app) app.options.attemptHighlight = false;
122+
if (padding > 2 && ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
123123

124124
output += Utils.hex(ordinal, padding) + delim;
125125
} else {
126-
if (app) app.options.attemptHighlight = false;
126+
if (ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
127127
output += ordinal.toString(base) + delim;
128128
}
129129
}
@@ -149,9 +149,7 @@ const ByteRepr = {
149149
throw "Error: Base argument must be between 2 and 36";
150150
}
151151

152-
if (base !== 16 && app) {
153-
app.options.attemptHighlight = false;
154-
}
152+
if (base !== 16 && ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
155153

156154
// Split into groups of 2 if the whole string is concatenated and
157155
// too long to be a single character

src/core/operations/Hexdump.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const Hexdump = {
9292
const w = (width - 13) / 4;
9393
// w should be the specified width of the hexdump and therefore a round number
9494
if (Math.floor(w) !== w || input.indexOf("\r") !== -1 || output.indexOf(13) !== -1) {
95-
//TODO if (self) self.setOption("attemptHighlight", false);
95+
if (ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
9696
}
9797
return output;
9898
},

src/web/App.js

+3
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ App.prototype.handleError = function(err) {
105105
App.prototype.bake = function(step) {
106106
if (this.baking) return;
107107

108+
// Reset attemptHighlight flag
109+
this.options.attemptHighlight = true;
110+
108111
this.manager.worker.bake(
109112
this.getInput(), // The user's input
110113
this.getRecipeConfig(), // The configuration of the recipe

src/web/WorkerWaiter.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ WorkerWaiter.prototype.registerChefWorker = function() {
4040
* @param {MessageEvent} e
4141
*/
4242
WorkerWaiter.prototype.handleChefMessage = function(e) {
43-
switch (e.data.action) {
43+
const r = e.data;
44+
switch (r.action) {
4445
case "bakeSuccess":
45-
this.bakingComplete(e.data.data);
46+
this.bakingComplete(r.data);
4647
break;
4748
case "bakeError":
48-
this.app.handleError(e.data.data);
49+
this.app.handleError(r.data);
4950
this.setBakingStatus(false);
5051
break;
5152
case "silentBakeComplete":
@@ -55,10 +56,13 @@ WorkerWaiter.prototype.handleChefMessage = function(e) {
5556
this.app.loaded();
5657
break;
5758
case "statusMessage":
58-
this.manager.output.setStatusMsg(e.data.data);
59+
this.manager.output.setStatusMsg(r.data);
60+
break;
61+
case "optionUpdate":
62+
this.app.options[r.data.option] = r.data.value;
5963
break;
6064
case "highlightsCalculated":
61-
this.manager.highlighter.displayHighlights(e.data.data.pos, e.data.data.direction);
65+
this.manager.highlighter.displayHighlights(r.data.pos, r.data.direction);
6266
break;
6367
default:
6468
console.error("Unrecognised message from ChefWorker", e);
@@ -104,7 +108,6 @@ WorkerWaiter.prototype.bakingComplete = function(response) {
104108
this.app.handleError(response.error);
105109
}
106110

107-
this.app.options = response.options;
108111
this.app.dishStr = response.type === "html" ? Utils.stripHtmlTags(response.result, true) : response.result;
109112
this.app.progress = response.progress;
110113
this.manager.recipe.updateBreakpointIndicator(response.progress);

0 commit comments

Comments
 (0)