Sfoglia il codice sorgente

Readme update, refactoring

sanex3339 8 anni fa
parent
commit
ae224d2aa8

+ 17 - 21
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('<inputPath> [options]').option('-o, --output <path>', 'Output path for obfuscated code').option('--compact <boolean>', 'Disable one line output code compacting', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtection <boolean>', 'Disable browser Debug panel (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtectionInterval <boolean>', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--disableConsoleOutput <boolean>', 'Allow console.log, console.info, console.error and console.warn messages output into browser console', JavaScriptObfuscatorCLI.parseBoolean).option('--encodeUnicodeLiterals <boolean>', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed)', JavaScriptObfuscatorCLI.parseBoolean).option('--reservedNames <list>', '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 <boolean>', 'Disable rotation of unicode array values during obfuscation', JavaScriptObfuscatorCLI.parseBoolean).option('--selfDefending <boolean>', 'Disables self-defending for obfuscated code', JavaScriptObfuscatorCLI.parseBoolean).option('--sourceMap <boolean>', 'Enables source map generation', JavaScriptObfuscatorCLI.parseBoolean).option('--sourceMapMode <string> [separate, inline]', 'Creates a separate files with code and source map or combines them into a single file', JavaScriptObfuscatorCLI.parseSourceMapMode).option('--unicodeArray <boolean>', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call', JavaScriptObfuscatorCLI.parseBoolean).option('--unicodeArrayThreshold <number>', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1)', parseFloat).option('--wrapUnicodeArrayCalls <boolean>', 'Disables usage of special access function instead of direct array call', JavaScriptObfuscatorCLI.parseBoolean).parse(this.rawArguments);
+            }).option('--rotateUnicodeArray <boolean>', 'Disable rotation of unicode array values during obfuscation', JavaScriptObfuscatorCLI.parseBoolean).option('--selfDefending <boolean>', 'Disables self-defending for obfuscated code', JavaScriptObfuscatorCLI.parseBoolean).option('--sourceMap <boolean>', 'Enables source map generation', JavaScriptObfuscatorCLI.parseBoolean).option('--sourceMapMode <string> [inline, separate]', 'Specify source map output mode', JavaScriptObfuscatorCLI.parseSourceMapMode).option('--unicodeArray <boolean>', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call', JavaScriptObfuscatorCLI.parseBoolean).option('--unicodeArrayThreshold <number>', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1)', parseFloat).option('--wrapUnicodeArrayCalls <boolean>', '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);

+ 32 - 2
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 <list> (comma separated)
     --rotateUnicodeArray <boolean>
     --selfDefending <boolean>
+    --sourceMap <boolean>
+    --sourceMapMode <string> [inline, separate]
     --unicodeArray <boolean>
     --unicodeArrayThreshold <number>
     --wrapUnicodeArrayCalls <boolean>
@@ -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`
 

+ 1 - 7
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();
     }
 

+ 1 - 6
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,

+ 11 - 6
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 '';
         }
 

+ 13 - 8
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 <boolean>', 'Disables self-defending for obfuscated code', JavaScriptObfuscatorCLI.parseBoolean)
             .option('--sourceMap <boolean>', 'Enables source map generation', JavaScriptObfuscatorCLI.parseBoolean)
             .option(
-                '--sourceMapMode <string> [separate, inline]',
-                'Creates a separate files with code and source map or combines them into a single file',
+                '--sourceMapMode <string> [inline, separate]',
+                'Specify source map output mode',
                 JavaScriptObfuscatorCLI.parseSourceMapMode
             )
             .option('--unicodeArray <boolean>', '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);
 

+ 1 - 1
src/types/TSourceMapMode.d.ts

@@ -1 +1 @@
-export type TSourceMapMode = 'inline' | 'string';
+export type TSourceMapMode = 'inline' | 'separate';