From ae224d2aa8a56c58285494539cb166b5da7a4dc4 Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Sat, 9 Jul 2016 15:18:59 +0300 Subject: [PATCH] Readme update, refactoring --- dist/index.js | 38 +++++++++++++---------------- readme.md | 34 ++++++++++++++++++++++++-- src/JavaScriptObfuscator.ts | 8 +----- src/JavaScriptObfuscatorInstance.ts | 7 +----- src/SourceMapCorrector.ts | 17 ++++++++----- src/cli/JavaScriptObfuscatorCLI.ts | 21 ++++++++++------ src/types/TSourceMapMode.d.ts | 2 +- 7 files changed, 76 insertions(+), 51 deletions(-) diff --git a/dist/index.js b/dist/index.js index a91068ab..6f1909db 100644 --- a/dist/index.js +++ b/dist/index.js @@ -649,13 +649,9 @@ var JavaScriptObfuscator = function () { key: "obfuscateWithSourceMap", value: function obfuscateWithSourceMap(sourceCode) { var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; - var sourceMapUrl = arguments[2]; var javaScriptObfuscator = new JavaScriptObfuscatorInstance_1.JavaScriptObfuscatorInstance(sourceCode, options); javaScriptObfuscator.obfuscate(); - if (sourceMapUrl) { - javaScriptObfuscator.setSourceMapUrl(sourceMapUrl); - } return javaScriptObfuscator.getObfuscationResult(); } }, { @@ -918,12 +914,7 @@ var JavaScriptObfuscatorInstance = function () { _createClass(JavaScriptObfuscatorInstance, [{ key: 'getObfuscationResult', value: function getObfuscationResult() { - var raw = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0]; - var obfuscationResult = new ObfuscationResult_1.ObfuscationResult(this.generatorOutput.code, this.generatorOutput.map); - if (raw) { - return obfuscationResult; - } return new SourceMapCorrector_1.SourceMapCorrector(obfuscationResult, this.sourceMapUrl, this.options.get('sourceMapMode')).correct(); } }, { @@ -1222,22 +1213,25 @@ var SourceMapCorrector = function () { if (!this.sourceMap) { return this.obfuscatedCode; } - var sourceMappingUrl = '//# sourceMappingURL=', - sourceMappingUrlContent = this.sourceMapUrl || this.sourceMap; + var sourceMappingUrl = '//# sourceMappingURL='; switch (this.sourceMapMode) { case SourceMapMode_1.SourceMapMode.Inline: - sourceMappingUrl += "data:application/json;base64," + Utils_1.Utils.btoa(sourceMappingUrlContent, false); + sourceMappingUrl += "data:application/json;base64," + Utils_1.Utils.btoa(this.sourceMapUrl || this.sourceMap, false); break; case SourceMapMode_1.SourceMapMode.Separate: default: - sourceMappingUrl += sourceMappingUrlContent; + if (this.sourceMapUrl) { + sourceMappingUrl += this.sourceMapUrl; + break; + } + return this.obfuscatedCode; } return this.obfuscatedCode + "\n" + sourceMappingUrl; } }, { key: "correctSourceMap", value: function correctSourceMap() { - if (!this.sourceMapUrl) { + if (this.sourceMapMode === SourceMapMode_1.SourceMapMode.Inline) { return ''; } return this.sourceMap; @@ -1354,6 +1348,7 @@ var SourceMapMode_1 = __webpack_require__(11); var DefaultPreset_1 = __webpack_require__(15); var CLIUtils_1 = __webpack_require__(24); var JavaScriptObfuscator_1 = __webpack_require__(8); +var JavaScriptObfuscatorInstance_1 = __webpack_require__(19); var Utils_1 = __webpack_require__(0); var JavaScriptObfuscatorCLI = function () { @@ -1398,7 +1393,7 @@ var JavaScriptObfuscatorCLI = function () { value: function configureCommands() { this.commands = new commander_1.Command().version(JavaScriptObfuscatorCLI.getBuildVersion(), '-v, --version').usage(' [options]').option('-o, --output ', 'Output path for obfuscated code').option('--compact ', 'Disable one line output code compacting', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtection ', 'Disable browser Debug panel (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtectionInterval ', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--disableConsoleOutput ', 'Allow console.log, console.info, console.error and console.warn messages output into browser console', JavaScriptObfuscatorCLI.parseBoolean).option('--encodeUnicodeLiterals ', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed)', JavaScriptObfuscatorCLI.parseBoolean).option('--reservedNames ', 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated)', function (val) { return val.split(','); - }).option('--rotateUnicodeArray ', 'Disable rotation of unicode array values during obfuscation', JavaScriptObfuscatorCLI.parseBoolean).option('--selfDefending ', 'Disables self-defending for obfuscated code', JavaScriptObfuscatorCLI.parseBoolean).option('--sourceMap ', 'Enables source map generation', JavaScriptObfuscatorCLI.parseBoolean).option('--sourceMapMode [separate, inline]', 'Creates a separate files with code and source map or combines them into a single file', JavaScriptObfuscatorCLI.parseSourceMapMode).option('--unicodeArray ', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call', JavaScriptObfuscatorCLI.parseBoolean).option('--unicodeArrayThreshold ', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1)', parseFloat).option('--wrapUnicodeArrayCalls ', 'Disables usage of special access function instead of direct array call', JavaScriptObfuscatorCLI.parseBoolean).parse(this.rawArguments); + }).option('--rotateUnicodeArray ', 'Disable rotation of unicode array values during obfuscation', JavaScriptObfuscatorCLI.parseBoolean).option('--selfDefending ', 'Disables self-defending for obfuscated code', JavaScriptObfuscatorCLI.parseBoolean).option('--sourceMap ', 'Enables source map generation', JavaScriptObfuscatorCLI.parseBoolean).option('--sourceMapMode [inline, separate]', 'Specify source map output mode', JavaScriptObfuscatorCLI.parseSourceMapMode).option('--unicodeArray ', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call', JavaScriptObfuscatorCLI.parseBoolean).option('--unicodeArrayThreshold ', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1)', parseFloat).option('--wrapUnicodeArrayCalls ', 'Disables usage of special access function instead of direct array call', JavaScriptObfuscatorCLI.parseBoolean).parse(this.rawArguments); this.commands.on('--help', function () { console.log(' Examples:\n'); console.log(' %> javascript-obfuscator in.js --compact true --selfDefending false'); @@ -1431,13 +1426,14 @@ var JavaScriptObfuscatorCLI = function () { }, { key: "processDataWithSourceMap", value: function processDataWithSourceMap(outputCodePath, options) { - var obfuscationResult = void 0, - outputSourceMapPath = CLIUtils_1.CLIUtils.getOutputSourceMapPath(outputCodePath), - sourceMapUrl = void 0; - if (options.sourceMapMode !== SourceMapMode_1.SourceMapMode.Inline) { - sourceMapUrl = [].concat(_toConsumableArray(outputSourceMapPath.split('/'))).pop(); + var javaScriptObfuscator = new JavaScriptObfuscatorInstance_1.JavaScriptObfuscatorInstance(this.data, options), + obfuscationResult = void 0, + outputSourceMapPath = CLIUtils_1.CLIUtils.getOutputSourceMapPath(outputCodePath); + javaScriptObfuscator.obfuscate(); + if (options.sourceMapMode === SourceMapMode_1.SourceMapMode.Separate) { + javaScriptObfuscator.setSourceMapUrl([].concat(_toConsumableArray(outputSourceMapPath.split('/'))).pop()); } - obfuscationResult = JavaScriptObfuscator_1.JavaScriptObfuscator.obfuscateWithSourceMap(this.data, options, sourceMapUrl); + obfuscationResult = javaScriptObfuscator.getObfuscationResult(); CLIUtils_1.CLIUtils.writeFile(outputCodePath, obfuscationResult.obfuscatedCode); if (obfuscationResult.sourceMap) { CLIUtils_1.CLIUtils.writeFile(outputSourceMapPath, obfuscationResult.sourceMap); diff --git a/readme.md b/readme.md index d2603e93..d037a04b 100644 --- a/readme.md +++ b/readme.md @@ -4,7 +4,7 @@ JavaScript obfuscator for Node.js is a free alternative to [js-obfuscator](https * without any limits and sending data to a server; * compatible with ES6; -* tested on Angular2 bundle; +* tested on Angular2 bundle. https://gist.github.com/sanex3339/ffc2876123b52e6d11ce45369fd53acf @@ -57,12 +57,26 @@ var _0xabf1 = [ ### `obfuscate(sourceCode, options)` +Returns `string` with obfuscated code. + This is the main function that runs the Obfuscator. It takes two parameters, `sourceCode` and `options` – the source code and the opitons respectively: * `sourceCode` (`string`, default: `null`) – any valid source code, passed as a string variable. JS Obfuscator will parse this string and apply a set of modificating functions to it, generating a string with the new (obfuscated) code and printing it to the console; * `options` (`Object`, default: `null`) – an object literal. -See [options](#options). +### `obfuscateWithSourceMap(sourceCode, options)` + +Same as `obfuscate(sourceCode, options)` but instead `string` with obfuscated code returns `ObfuscationResult` object which contains two properties: +* `obfuscatedCode` - `string` with obfuscated code; +* `sourceMap` - `string` with source map or an empty string if `sourceMapMode` option is set as `inline`. + +Parameters: + +* `sourceCode` (`string`, default: `null`) – any valid source code; +* `options` (`Object`, default: `null`) – an object literal. + See [options](#options). + +For available options see [options](#options). ## CLI usage Usage: @@ -100,6 +114,8 @@ At this moment of time, there are following options (`name: default`) available reservedNames: [], rotateUnicodeArray: true, selfDefending: true, + sourceMap: false, + sourceMapMode: 'separate', unicodeArray: true, unicodeArrayThreshold: 0.8, wrapUnicodeArrayCalls: true @@ -121,6 +137,8 @@ At this moment of time, there are following options (`name: default`) available --reservedNames (comma separated) --rotateUnicodeArray --selfDefending + --sourceMap + --sourceMapMode [inline, separate] --unicodeArray --unicodeArrayThreshold --wrapUnicodeArrayCalls @@ -198,6 +216,18 @@ Type: `boolean` Default: `true` Enables self-defending for obfuscated code. If obfuscated compact code is formatted, it will not work any more. +### `sourceMap` +Type: `boolean` Default: `false` + +Enables source map generation for obfuscated code. + +### `sourceMapMode` +Type: `string` Default: `separate` + +Specify source map generation mode: +* `inline` - emit a single file with source maps instead of having a separate file; +* `separate` - generates corresponding '.map' file with source map. If obfuscator run through CLI - adds link to source map file to the end of file with obfuscated code `//# sourceMappingUrl=file.js.map`. + ### `unicodeArray` Type: `boolean` Default: `true` diff --git a/src/JavaScriptObfuscator.ts b/src/JavaScriptObfuscator.ts index c4e421b5..7c0f9725 100644 --- a/src/JavaScriptObfuscator.ts +++ b/src/JavaScriptObfuscator.ts @@ -21,22 +21,16 @@ export class JavaScriptObfuscator { /** * @param sourceCode * @param options - * @param sourceMapUrl * @returns {string} */ public static obfuscateWithSourceMap ( sourceCode: string, - options: IOptionsPreset = {}, - sourceMapUrl?: string + options: IOptionsPreset = {} ): IObfuscationResult { let javaScriptObfuscator: JavaScriptObfuscatorInstance = new JavaScriptObfuscatorInstance(sourceCode, options); javaScriptObfuscator.obfuscate(); - if (sourceMapUrl) { - javaScriptObfuscator.setSourceMapUrl(sourceMapUrl); - } - return javaScriptObfuscator.getObfuscationResult(); } diff --git a/src/JavaScriptObfuscatorInstance.ts b/src/JavaScriptObfuscatorInstance.ts index ad85fde4..437c4d94 100644 --- a/src/JavaScriptObfuscatorInstance.ts +++ b/src/JavaScriptObfuscatorInstance.ts @@ -80,19 +80,14 @@ export class JavaScriptObfuscatorInstance { } /** - * @param raw * @returns {IObfuscationResult} */ - public getObfuscationResult (raw: boolean = false): IObfuscationResult { + public getObfuscationResult (): IObfuscationResult { let obfuscationResult: IObfuscationResult = new ObfuscationResult( this.generatorOutput.code, this.generatorOutput.map ); - if (raw) { - return obfuscationResult; - } - return new SourceMapCorrector( obfuscationResult, this.sourceMapUrl, diff --git a/src/SourceMapCorrector.ts b/src/SourceMapCorrector.ts index 405fa5d4..94af1274 100644 --- a/src/SourceMapCorrector.ts +++ b/src/SourceMapCorrector.ts @@ -59,28 +59,33 @@ export class SourceMapCorrector { return this.obfuscatedCode; } - let sourceMappingUrl: string = '//# sourceMappingURL=', - sourceMappingUrlContent: string = this.sourceMapUrl || this.sourceMap; + let sourceMappingUrl: string = '//# sourceMappingURL='; switch (this.sourceMapMode) { case SourceMapMode.Inline: - sourceMappingUrl += `data:application/json;base64,${Utils.btoa(sourceMappingUrlContent, false)}`; + sourceMappingUrl += `data:application/json;base64,${Utils.btoa(this.sourceMapUrl || this.sourceMap, false)}`; break; case SourceMapMode.Separate: default: - sourceMappingUrl += sourceMappingUrlContent; + if (this.sourceMapUrl) { + sourceMappingUrl += this.sourceMapUrl; + + break; + } + + return this.obfuscatedCode; } return `${this.obfuscatedCode}\n${sourceMappingUrl}`; }; /** - * @returns {any} + * @returns {string} */ private correctSourceMap (): string { - if (!this.sourceMapUrl) { + if (this.sourceMapMode === SourceMapMode.Inline) { return ''; } diff --git a/src/cli/JavaScriptObfuscatorCLI.ts b/src/cli/JavaScriptObfuscatorCLI.ts index a0d06939..819ac431 100644 --- a/src/cli/JavaScriptObfuscatorCLI.ts +++ b/src/cli/JavaScriptObfuscatorCLI.ts @@ -9,6 +9,7 @@ import { DEFAULT_PRESET } from "../preset-options/DefaultPreset"; import { CLIUtils } from "./CLIUtils"; import { JavaScriptObfuscator } from "../JavaScriptObfuscator"; +import { JavaScriptObfuscatorInstance } from "../JavaScriptObfuscatorInstance"; import { Utils } from "../Utils"; export class JavaScriptObfuscatorCLI { @@ -130,8 +131,8 @@ export class JavaScriptObfuscatorCLI { .option('--selfDefending ', 'Disables self-defending for obfuscated code', JavaScriptObfuscatorCLI.parseBoolean) .option('--sourceMap ', 'Enables source map generation', JavaScriptObfuscatorCLI.parseBoolean) .option( - '--sourceMapMode [separate, inline]', - 'Creates a separate files with code and source map or combines them into a single file', + '--sourceMapMode [inline, separate]', + 'Specify source map output mode', JavaScriptObfuscatorCLI.parseSourceMapMode ) .option('--unicodeArray ', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call', JavaScriptObfuscatorCLI.parseBoolean) @@ -177,15 +178,19 @@ export class JavaScriptObfuscatorCLI { * @param options */ private processDataWithSourceMap (outputCodePath: string, options: IOptionsPreset): void { - let obfuscationResult: IObfuscationResult, - outputSourceMapPath: string = CLIUtils.getOutputSourceMapPath(outputCodePath), - sourceMapUrl: string; + let javaScriptObfuscator: JavaScriptObfuscatorInstance = new JavaScriptObfuscatorInstance(this.data, options), + obfuscationResult: IObfuscationResult, + outputSourceMapPath: string = CLIUtils.getOutputSourceMapPath(outputCodePath); - if (options.sourceMapMode !== SourceMapMode.Inline) { - sourceMapUrl = [...outputSourceMapPath.split('/')].pop(); + javaScriptObfuscator.obfuscate(); + + if (options.sourceMapMode === SourceMapMode.Separate) { + javaScriptObfuscator.setSourceMapUrl( + [...outputSourceMapPath.split('/')].pop() + ); } - obfuscationResult = JavaScriptObfuscator.obfuscateWithSourceMap(this.data, options, sourceMapUrl); + obfuscationResult = javaScriptObfuscator.getObfuscationResult(); CLIUtils.writeFile(outputCodePath, obfuscationResult.obfuscatedCode); diff --git a/src/types/TSourceMapMode.d.ts b/src/types/TSourceMapMode.d.ts index 1b4b83de..ac3507b7 100644 --- a/src/types/TSourceMapMode.d.ts +++ b/src/types/TSourceMapMode.d.ts @@ -1 +1 @@ -export type TSourceMapMode = 'inline' | 'string'; +export type TSourceMapMode = 'inline' | 'separate';