Skip to content

Commit e92db6e

Browse files
author
sanex3339
committed
Moved obfuscation logic to the worker
1 parent 23ce429 commit e92db6e

File tree

4 files changed

+256
-56
lines changed

4 files changed

+256
-56
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"semantic-ui-less": "2.3.1",
5151
"semantic-ui-react": "0.79.0",
5252
"style-loader": "0.20.3",
53+
"threads": "^1.0.2",
5354
"url-loader": "1.0.1",
5455
"webpack": "4.4.1",
5556
"webpack-cli": "2.0.13"

server.js

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,42 @@
11
const express = require('express');
22
const bodyParser = require("body-parser");
3-
4-
const JavaScriptObfuscator = require('javascript-obfuscator');
3+
const { spawn, Worker } = require('threads/dist');
54

65
const app = express();
76

87
process.env.PWD = process.cwd();
98

9+
(async function () {
10+
const obfuscationWorker = await spawn(new Worker('./workers/obfuscation-worker'));
1011

11-
app.set('port', (process.env.PORT || 3000));
12-
13-
app.use(bodyParser.json({limit: '3mb'}));
14-
15-
app.use('/static/dist', express.static(__dirname + '/dist'));
16-
app.use('/static/images', express.static(__dirname + '/public/images'));
17-
app.use('/static/semantic', express.static(__dirname + '/public/semantic'));
18-
19-
app.get('/', function (req, res) {
20-
res.sendFile(__dirname + '/dist/index.html');
21-
});
22-
23-
app.post('/obfuscate', function (req, res) {
24-
const body = req.body;
25-
26-
const {code, options} = body;
12+
app.set('port', (process.env.PORT || 3000));
2713

28-
if (!options.sourceMap) {
29-
delete options.sourceMapMode
30-
}
14+
app.use(bodyParser.json({limit: '3mb'}));
3115

32-
// options.stringArrayEncoding come from the client as strings, but the
33-
// obfuscator expects it to be a boolean or a string if 'base64'/'rc4'
34-
if (['false', 'true'].indexOf(options.stringArrayEncoding) !== -1) {
35-
options.stringArrayEncoding = options.stringArrayEncoding === 'true';
36-
}
16+
app.use('/static/dist', express.static(__dirname + '/dist'));
17+
app.use('/static/images', express.static(__dirname + '/public/images'));
18+
app.use('/static/semantic', express.static(__dirname + '/public/semantic'));
3719

38-
let response = {};
20+
app.get('/', function (req, res) {
21+
res.sendFile(__dirname + '/dist/index.html');
22+
});
3923

40-
try {
41-
const result = JavaScriptObfuscator.obfuscate(code, options);
42-
response = {
43-
code: result.getObfuscatedCode(),
44-
sourceMap: result.getSourceMap(),
45-
}
46-
} catch (e) {
47-
response = {
48-
code: e.toString(),
49-
sourceMap: '',
50-
}
51-
}
24+
app.post('/obfuscate', function (req, res) {
25+
const body = req.body;
5226

53-
res.send(JSON.stringify(response));
27+
const {code, options} = body;
5428

55-
});
29+
obfuscationWorker
30+
.obfuscate(code, options)
31+
.then(response => {
32+
res.send(JSON.stringify(response));
33+
})
34+
.catch(error => {
35+
res.send(JSON.stringify(error));
36+
});
37+
});
5638

57-
app.listen(app.get('port'), function () {
39+
app.listen(app.get('port'), function () {
5840

59-
});
41+
});
42+
})();

workers/obfuscation-worker.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const expose = require('threads/worker').expose;
2+
const JavaScriptObfuscator = require('javascript-obfuscator');
3+
4+
expose({
5+
obfuscate: (code, options) => {
6+
if (!options.sourceMap) {
7+
delete options.sourceMapMode
8+
}
9+
10+
// options.stringArrayEncoding come from the client as strings, but the
11+
// obfuscator expects it to be a boolean or a string if 'base64'/'rc4'
12+
if (['false', 'true'].indexOf(options.stringArrayEncoding) !== -1) {
13+
options.stringArrayEncoding = options.stringArrayEncoding === 'true';
14+
}
15+
16+
try {
17+
const result = JavaScriptObfuscator.obfuscate(code, options);
18+
19+
return {
20+
code: result.getObfuscatedCode(),
21+
sourceMap: result.getSourceMap(),
22+
}
23+
} catch (e) {
24+
return {
25+
code: e.toString(),
26+
sourceMap: '',
27+
}
28+
}
29+
}
30+
});

0 commit comments

Comments
 (0)