diff --git a/package.json b/package.json index 922ff861..30272107 100644 --- a/package.json +++ b/package.json @@ -21,13 +21,13 @@ }, "types": "index.d.ts", "dependencies": { - "@nuxtjs/opencollective": "^0.2.2", + "@nuxtjs/opencollective": "0.2.2", "chalk": "3.0.0", "chance": "1.1.4", "class-validator": "0.11.0", "commander": "4.0.1", "escodegen-wallaby": "1.6.27", - "espree": "^6.1.2", + "espree": "6.1.2", "estraverse": "4.3.0", "eventemitter3": "4.0.0", "inversify": "5.0.1", @@ -59,12 +59,12 @@ "chai": "4.2.0", "coveralls": "3.0.9", "mocha": "6.2.2", - "nyc": "^15.0.0", + "nyc": "15.0.0", "pjson": "1.0.9", "pre-commit": "1.2.2", "rimraf": "3.0.0", "sinon": "7.5.0", - "threads": "0.12.0", + "threads": "1.0.0", "ts-node": "6.1.0", "tslint": "5.20.1", "tslint-eslint-rules": "5.4.0", diff --git a/src/declarations/threads.d.ts b/src/declarations/threads.d.ts deleted file mode 100644 index a8d463ae..00000000 --- a/src/declarations/threads.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -declare module 'threads' { - type PostMessage = (data: U) => void; - type SpawnCallback = (data: T, postMessage: PostMessage ) => void; - type ResponseCallback = (response: U) => void; - - class Thread { - public killed: boolean; - - public send (data: T): Thread ; - public on (eventType: string, responseCallback: ResponseCallback): Thread ; - public kill (): void; - } - - export function spawn (spawnCallback: SpawnCallback ): Thread ; -} diff --git a/test/declarations/index.d.ts b/test/declarations/index.d.ts index 3db447f4..cdbdd652 100644 --- a/test/declarations/index.d.ts +++ b/test/declarations/index.d.ts @@ -2,5 +2,4 @@ /// /// /// -/// -/// \ No newline at end of file +/// \ No newline at end of file diff --git a/test/functional-tests/templates/debug-protection-nodes/DebugProtectionFunctionCallTemplate.spec.ts b/test/functional-tests/templates/debug-protection-nodes/DebugProtectionFunctionCallTemplate.spec.ts index 7679a811..c78f6cc3 100644 --- a/test/functional-tests/templates/debug-protection-nodes/DebugProtectionFunctionCallTemplate.spec.ts +++ b/test/functional-tests/templates/debug-protection-nodes/DebugProtectionFunctionCallTemplate.spec.ts @@ -1,5 +1,5 @@ import { assert } from 'chai'; -import { spawn } from 'threads'; +import { spawn, Thread, Worker } from 'threads/dist'; import { readFileAsString } from '../../../helpers/readFileAsString'; @@ -10,26 +10,26 @@ import { ObfuscationTarget } from '../../../../src/enums/ObfuscationTarget'; import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade'; -function spawnThread(inputCallback: Function, threadCallback: Function, timeoutCallback: Function): void { - const thread = spawn((input: string, postMessage: Function) => { - postMessage(eval(input)); - }); + + +async function spawnThread(obfuscatedCode: string, threadCallback: Function, timeoutCallback: Function): Promise { + const evaluationWorker = await spawn(new Worker('./workers/evaluation-worker')); const timeout = setTimeout(() => { - thread.kill(); + Thread.terminate(evaluationWorker); timeoutCallback(); }, 1500); - thread - .send(inputCallback()) - .on('message', (response: number) => { - clearTimeout(timeout); - thread.kill(); - threadCallback(response); - }); + const result: string = await evaluationWorker.evaluate(obfuscatedCode); + + clearTimeout(timeout); + Thread.terminate(evaluationWorker); + threadCallback(result); } -describe('DebugProtectionFunctionCallTemplate', () => { +describe('DebugProtectionFunctionCallTemplate', function () { + this.timeout(5000); + describe('Variant #1: correctly obfuscate code with `HexadecimalIdentifierNamesGenerator``', () => { const expectedEvaluationResult: number = 1; @@ -49,7 +49,7 @@ describe('DebugProtectionFunctionCallTemplate', () => { ).getObfuscatedCode(); spawnThread( - () => obfuscatedCode, + obfuscatedCode, (response: number) => { evaluationResult = response; done(); @@ -84,7 +84,7 @@ describe('DebugProtectionFunctionCallTemplate', () => { ).getObfuscatedCode(); spawnThread( - () => obfuscatedCode, + obfuscatedCode, (response: number) => { evaluationResult = response; done(); @@ -120,7 +120,7 @@ describe('DebugProtectionFunctionCallTemplate', () => { ).getObfuscatedCode(); spawnThread( - () => obfuscatedCode, + obfuscatedCode, (response: number) => { evaluationResult = response; done(); @@ -155,7 +155,7 @@ describe('DebugProtectionFunctionCallTemplate', () => { ).getObfuscatedCode(); spawnThread( - () => obfuscatedCode, + obfuscatedCode, (response: number) => { evaluationResult = response; done(); @@ -190,7 +190,7 @@ describe('DebugProtectionFunctionCallTemplate', () => { obfuscatedCode = obfuscatedCode.replace(/\+\+ *_0x([a-f0-9]){4,6}/, ''); spawnThread( - () => obfuscatedCode, + obfuscatedCode, (response: number) => { evaluationResult = response; done(); @@ -224,7 +224,7 @@ describe('DebugProtectionFunctionCallTemplate', () => { ).getObfuscatedCode(); spawnThread( - () => obfuscatedCode, + obfuscatedCode, (response: number) => { evaluationResult = response; done(); diff --git a/test/functional-tests/templates/debug-protection-nodes/workers/evaluation-worker.js b/test/functional-tests/templates/debug-protection-nodes/workers/evaluation-worker.js new file mode 100644 index 00000000..76907743 --- /dev/null +++ b/test/functional-tests/templates/debug-protection-nodes/workers/evaluation-worker.js @@ -0,0 +1,7 @@ +const expose = require('threads/worker').expose; + +expose({ + evaluate: (obfuscatedCode) => { + return eval(obfuscatedCode); + } +}); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 98f32fa6..e00e51df 100644 --- a/yarn.lock +++ b/yarn.lock @@ -890,6 +890,11 @@ caller-id@^0.1.0: dependencies: stack-trace "~0.0.7" +callsites@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/callsites/-/callsites-3.1.0.tgz#b3630abd8943432f54b3f0519238e33cd7df2f73" + integrity sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ== + camelcase@^5.0.0, camelcase@^5.3.1: version "5.3.1" resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.3.1.tgz#e3c9b31569e106811df242f715725a1f4c494320" @@ -1531,6 +1536,11 @@ eslint-visitor-keys@^1.1.0: resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.1.0.tgz#e2a82cea84ff246ad6fb57f9bde5b46621459ec2" integrity sha512-8y9YjtM1JBJU/A9Kc+SbaOV4y29sSWckBwMHa+FGtVj5gN/sbnKDf6xJUl+8g7FAij9LVaP8C24DUiH/f/2Z9A== +esm@^3.2.25: + version "3.2.25" + resolved "https://registry.yarnpkg.com/esm/-/esm-3.2.25.tgz#342c18c29d56157688ba5ce31f8431fbb795cc10" + integrity sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA== + espree@^6.1.2: version "6.1.2" resolved "https://registry.yarnpkg.com/espree/-/espree-6.1.2.tgz#6c272650932b4f91c3714e5e7b5f5e2ecf47262d" @@ -1580,11 +1590,6 @@ eventemitter3@4.0.0: resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.0.tgz#d65176163887ee59f386d64c82610b696a4a74eb" integrity sha512-qerSRB0p+UDEssxTtm6EDKcE7W4OaoisfIMl4CngyEhjpYglocpNg6UEqCvemdGhosAsg4sO2dXJOdyBifPGCg== -eventemitter3@^2.0.2: - version "2.0.3" - resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-2.0.3.tgz#b5e1079b59fb5e1ba2771c0a993be060a58c99ba" - integrity sha1-teEHm1n7XhuidxwKmTvgYKWMmbo= - events@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/events/-/events-3.0.0.tgz#9a0a0dfaf62893d92b875b8f2698ca4114973e88" @@ -2282,6 +2287,13 @@ is-number@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/is-number/-/is-number-4.0.0.tgz#0026e37f5454d73e356dfe6564699867c6a7f0ff" +is-observable@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/is-observable/-/is-observable-1.1.0.tgz#b3e986c8f44de950867cab5403f5a3465005975e" + integrity sha512-NqCa4Sa2d+u7BWc6CukaObG3Fh+CU9bvixbpcXYhy2VvYS7vVGIdAgnIS5Ks3A/cqk4rebLJ9s8zBstT2aKnIA== + dependencies: + symbol-observable "^1.1.0" + is-odd@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/is-odd/-/is-odd-2.0.0.tgz#7646624671fd7ea558ccd9a2795182f2958f1b24" @@ -2895,11 +2907,6 @@ nanomatch@^1.2.9: snapdragon "^0.8.1" to-regex "^3.0.1" -native-promise-only@^0.8.1: - version "0.8.1" - resolved "https://registry.yarnpkg.com/native-promise-only/-/native-promise-only-0.8.1.tgz#20a318c30cb45f71fe7adfbf7b21c99c1472ef11" - integrity sha1-IKMYwwy0X3H+et+/eyHJnBRy7xE= - needle@^2.2.0: version "2.2.1" resolved "https://registry.yarnpkg.com/needle/-/needle-2.2.1.tgz#b5e325bd3aae8c2678902fa296f729455d1d3a7d" @@ -3125,6 +3132,11 @@ object.pick@^1.3.0: dependencies: isobject "^3.0.1" +observable-fns@^0.5.0: + version "0.5.0" + resolved "https://registry.yarnpkg.com/observable-fns/-/observable-fns-0.5.0.tgz#9b71bb3e1a6ac9840b3922b67de0092773230177" + integrity sha512-BWmxvFppAKRWWTYns+yW3RaoXLTkSVF1cnLNe06opO4/C4VVfZgRkwSEBLOAA6bcTNM1zDDRz2T+l52cPNuaZQ== + once@^1.3.0, once@^1.3.1, once@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" @@ -4042,6 +4054,11 @@ supports-color@^7.1.0: dependencies: has-flag "^4.0.0" +symbol-observable@^1.1.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804" + integrity sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ== + tapable@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/tapable/-/tapable-1.0.0.tgz#cbb639d9002eed9c6b5975eb20598d7936f1f9f2" @@ -4096,13 +4113,17 @@ test-exclude@^6.0.0: glob "^7.1.4" minimatch "^3.0.4" -threads@0.12.0: - version "0.12.0" - resolved "https://registry.yarnpkg.com/threads/-/threads-0.12.0.tgz#d8ae23d929fcb8274a585b7bc333324fdfacef67" - integrity sha512-4B7hd61lDsVW1Z/+FAVX7D9QbiQYUbtGMHVkkwWT/nKPKas8u4FEc+Rg8E8h2erhNTQGNqNJ0TsholmhpKNPRg== +threads@1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/threads/-/threads-1.0.0.tgz#d4ae092673f566cc650f25aca8bccd749bafbffd" + integrity sha512-E4qjYS3Za59Y+SVu4GNrnFwL4HbpimY9Co+In0PW5hSljIRo3z8b8ZmRy5V2f6J1fK6GsoVFSR61lzqmPnfKwQ== dependencies: - eventemitter3 "^2.0.2" - native-promise-only "^0.8.1" + callsites "^3.1.0" + debug "^4.1.1" + is-observable "^1.1.0" + observable-fns "^0.5.0" + optionalDependencies: + tiny-worker ">= 2" through2@^2.0.0: version "2.0.3" @@ -4117,6 +4138,13 @@ timers-browserify@^2.0.4: dependencies: setimmediate "^1.0.4" +"tiny-worker@>= 2": + version "2.3.0" + resolved "https://registry.yarnpkg.com/tiny-worker/-/tiny-worker-2.3.0.tgz#715ae34304c757a9af573ae9a8e3967177e6011e" + integrity sha512-pJ70wq5EAqTAEl9IkGzA+fN0836rycEuz2Cn6yeZ6FRzlVS5IDOkFHpIoEsksPRQV34GDqXm65+OlnZqUSyK2g== + dependencies: + esm "^3.2.25" + to-arraybuffer@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/to-arraybuffer/-/to-arraybuffer-1.0.1.tgz#7d229b1fcc637e466ca081180836a7aabff83f43"