Identifier names cache option prototype #2. Added readme.

This commit is contained in:
sanex
2021-05-30 01:01:59 +03:00
parent f087100c26
commit bee043cef3
38 changed files with 38015 additions and 131 deletions
+64
View File
@@ -231,6 +231,7 @@ Returns `ObfuscationResult` object which contains two public methods:
* `getObfuscatedCode()` - returns `string` with obfuscated code;
* `getSourceMap()` - if [`sourceMap`](#sourcemap) option is enabled - returns `string` with source map or an empty string if [`sourceMapMode`](#sourcemapmode) option is set as `inline`.
* `getIdentifierNamesCache()` - returns object with identifier names cache if `identifierNamesCache` option is enabled, `null` overwise;
Calling `toString()` for `ObfuscationResult` object will return `string` with obfuscated code.
@@ -361,6 +362,7 @@ Following options are available for the JS Obfuscator:
disableConsoleOutput: false,
domainLock: [],
forceTransformStrings: [],
identifierNamesCache: null,
identifierNamesGenerator: 'hexadecimal',
identifiersDictionary: [],
identifiersPrefix: '',
@@ -421,6 +423,7 @@ Following options are available for the JS Obfuscator:
--domain-lock '<list>' (comma separated)
--exclude '<list>' (comma separated)
--force-transform-strings '<list>' (comma separated)
--identifier-names-cache <string>
--identifier-names-generator <string> [dictionary, hexadecimal, mangled, mangled-shuffled]
--identifiers-dictionary '<list>' (comma separated)
--identifiers-prefix <string>
@@ -712,6 +715,67 @@ Example:
}
```
### `identifierNamesGenerator`
Type: `Object | null` Default: `null`
The main goal for this option is the ability to use the same identifier names of global identifiers between obfuscation of multiple source codes/files.
#### Node.js API
If an empty object (`{}`) is passed enables the writing of all names (original and obfuscated) to the object. This object will be accessed through the `getIdentifierNamesCache` method call of `ObfuscationResult` object.
The result of `getIdentifierNamesCache` call could be next used as `identifierNamesGenerator` option value for using these names during obfuscation of all matched global identifier names of next source codes.
Example:
```
const file1ObfuscationResult = JavaScriptObfuscator.obfuscate(
`
function foo(arg) {
console.log(arg)
}
function bar() {
var bark = 2;
}
`,
{
compact: false,
identifierNamesCache: {},
renameGlobals: true
}
)
console.log(file1ObfuscationResult.getIdentifierNamesCache());
// { foo: '_0x5de86d', bar: '_0x2a943b' }
const file2ObfuscationResult = JavaScriptObfuscator.obfuscate(
`
// Expect that these global functions are defined in another obfuscated file
foo(1);
bar();
// Expect that this global function is defined in third-party package
baz();
`,
{
compact: false,
identifierNamesCache: file1ObfuscationResult.getIdentifierNamesCache(),
renameGlobals: true
}
)
console.log(file2ObfuscationResult.getObfuscatedCode());
// _0x5de86d(0x1);
// _0x2a943b();
// baz();
```
#### CLI
CLI has a different option `--identifier-names-cache-path` that allows defining a path to the existing `.json` file that will be used to read and write global identifier names cache.
If a path to the empty file will be passed - identifier names cache will be written to that file.
This file with cache can be again used as `--identifier-names-cache-path` option value for using these names during obfuscation of all matched global identifier names of next files.
### `identifierNamesGenerator`
Type: `string` Default: `hexadecimal`