|
1 | 1 | const express = require('express');
|
2 | 2 | const bodyParser = require("body-parser");
|
3 |
| - |
4 |
| -const JavaScriptObfuscator = require('javascript-obfuscator'); |
| 3 | +const { spawn, Worker } = require('threads/dist'); |
5 | 4 |
|
6 | 5 | const app = express();
|
7 | 6 |
|
8 | 7 | process.env.PWD = process.cwd();
|
9 | 8 |
|
| 9 | +(async function () { |
| 10 | + const obfuscationWorker = await spawn(new Worker('./workers/obfuscation-worker')); |
10 | 11 |
|
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)); |
27 | 13 |
|
28 |
| - if (!options.sourceMap) { |
29 |
| - delete options.sourceMapMode |
30 |
| - } |
| 14 | + app.use(bodyParser.json({limit: '3mb'})); |
31 | 15 |
|
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')); |
37 | 19 |
|
38 |
| - let response = {}; |
| 20 | + app.get('/', function (req, res) { |
| 21 | + res.sendFile(__dirname + '/dist/index.html'); |
| 22 | + }); |
39 | 23 |
|
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; |
52 | 26 |
|
53 |
| - res.send(JSON.stringify(response)); |
| 27 | + const {code, options} = body; |
54 | 28 |
|
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 | + }); |
56 | 38 |
|
57 |
| -app.listen(app.get('port'), function () { |
| 39 | + app.listen(app.get('port'), function () { |
58 | 40 |
|
59 |
| -}); |
| 41 | + }); |
| 42 | +})(); |
0 commit comments