js-confuser 1.7.2 → 2.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (263) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +6 -4
  2. package/.github/workflows/node.js.yml +1 -1
  3. package/CHANGELOG.md +105 -0
  4. package/Migration.md +57 -0
  5. package/README.md +23 -913
  6. package/dist/constants.js +69 -13
  7. package/dist/index.js +108 -152
  8. package/dist/obfuscator.js +316 -118
  9. package/dist/options.js +1 -109
  10. package/dist/order.js +30 -30
  11. package/dist/presets.js +47 -45
  12. package/dist/probability.js +25 -32
  13. package/dist/templates/bufferToStringTemplate.js +9 -0
  14. package/dist/templates/deadCodeTemplates.js +9 -0
  15. package/dist/templates/getGlobalTemplate.js +19 -0
  16. package/dist/templates/integrityTemplate.js +30 -0
  17. package/dist/templates/setFunctionLengthTemplate.js +9 -0
  18. package/dist/templates/stringCompressionTemplate.js +10 -0
  19. package/dist/templates/tamperProtectionTemplates.js +21 -0
  20. package/dist/templates/template.js +213 -93
  21. package/dist/transforms/astScrambler.js +100 -0
  22. package/dist/transforms/calculator.js +70 -127
  23. package/dist/transforms/controlFlowFlattening.js +1182 -0
  24. package/dist/transforms/deadCode.js +62 -577
  25. package/dist/transforms/dispatcher.js +300 -309
  26. package/dist/transforms/extraction/duplicateLiteralsRemoval.js +88 -189
  27. package/dist/transforms/extraction/objectExtraction.js +131 -215
  28. package/dist/transforms/finalizer.js +56 -59
  29. package/dist/transforms/flatten.js +275 -276
  30. package/dist/transforms/functionOutlining.js +230 -0
  31. package/dist/transforms/identifier/globalConcealing.js +217 -103
  32. package/dist/transforms/identifier/movedDeclarations.js +167 -91
  33. package/dist/transforms/identifier/renameVariables.js +240 -187
  34. package/dist/transforms/lock/integrity.js +61 -184
  35. package/dist/transforms/lock/lock.js +263 -303
  36. package/dist/transforms/minify.js +431 -436
  37. package/dist/transforms/opaquePredicates.js +65 -118
  38. package/dist/transforms/pack.js +160 -0
  39. package/dist/transforms/plugin.js +179 -0
  40. package/dist/transforms/preparation.js +263 -163
  41. package/dist/transforms/renameLabels.js +132 -56
  42. package/dist/transforms/rgf.js +142 -240
  43. package/dist/transforms/shuffle.js +52 -145
  44. package/dist/transforms/string/encoding.js +45 -173
  45. package/dist/transforms/string/stringCompression.js +81 -126
  46. package/dist/transforms/string/stringConcealing.js +189 -224
  47. package/dist/transforms/string/stringEncoding.js +32 -40
  48. package/dist/transforms/string/stringSplitting.js +54 -55
  49. package/dist/transforms/variableMasking.js +232 -0
  50. package/dist/utils/ControlObject.js +125 -0
  51. package/dist/utils/IntGen.js +46 -0
  52. package/dist/utils/NameGen.js +106 -0
  53. package/dist/utils/ast-utils.js +560 -0
  54. package/dist/utils/function-utils.js +56 -0
  55. package/dist/utils/gen-utils.js +48 -0
  56. package/dist/utils/node.js +77 -0
  57. package/dist/utils/object-utils.js +21 -0
  58. package/dist/utils/random-utils.js +91 -0
  59. package/dist/utils/static-utils.js +64 -0
  60. package/dist/validateOptions.js +122 -0
  61. package/index.d.ts +1 -17
  62. package/package.json +27 -22
  63. package/src/constants.ts +139 -77
  64. package/src/index.ts +70 -163
  65. package/src/obfuscationResult.ts +43 -0
  66. package/src/obfuscator.ts +328 -135
  67. package/src/options.ts +154 -623
  68. package/src/order.ts +14 -14
  69. package/src/presets.ts +39 -34
  70. package/src/probability.ts +21 -36
  71. package/src/templates/{bufferToString.ts → bufferToStringTemplate.ts} +5 -54
  72. package/src/templates/deadCodeTemplates.ts +1185 -0
  73. package/src/templates/getGlobalTemplate.ts +72 -0
  74. package/src/templates/integrityTemplate.ts +69 -0
  75. package/src/templates/setFunctionLengthTemplate.ts +11 -0
  76. package/src/templates/stringCompressionTemplate.ts +42 -0
  77. package/src/templates/tamperProtectionTemplates.ts +116 -0
  78. package/src/templates/template.ts +183 -92
  79. package/src/transforms/astScrambler.ts +99 -0
  80. package/src/transforms/calculator.ts +96 -224
  81. package/src/transforms/controlFlowFlattening.ts +1594 -0
  82. package/src/transforms/deadCode.ts +85 -628
  83. package/src/transforms/dispatcher.ts +431 -636
  84. package/src/transforms/extraction/duplicateLiteralsRemoval.ts +147 -299
  85. package/src/transforms/extraction/objectExtraction.ts +160 -333
  86. package/src/transforms/finalizer.ts +63 -64
  87. package/src/transforms/flatten.ts +439 -557
  88. package/src/transforms/functionOutlining.ts +225 -0
  89. package/src/transforms/identifier/globalConcealing.ts +261 -189
  90. package/src/transforms/identifier/movedDeclarations.ts +228 -142
  91. package/src/transforms/identifier/renameVariables.ts +252 -258
  92. package/src/transforms/lock/integrity.ts +84 -260
  93. package/src/transforms/lock/lock.ts +342 -491
  94. package/src/transforms/minify.ts +523 -663
  95. package/src/transforms/opaquePredicates.ts +90 -229
  96. package/src/transforms/pack.ts +195 -0
  97. package/src/transforms/plugin.ts +185 -0
  98. package/src/transforms/preparation.ts +337 -215
  99. package/src/transforms/renameLabels.ts +176 -77
  100. package/src/transforms/rgf.ts +293 -386
  101. package/src/transforms/shuffle.ts +80 -254
  102. package/src/transforms/string/encoding.ts +26 -129
  103. package/src/transforms/string/stringCompression.ts +118 -236
  104. package/src/transforms/string/stringConcealing.ts +255 -339
  105. package/src/transforms/string/stringEncoding.ts +28 -47
  106. package/src/transforms/string/stringSplitting.ts +61 -75
  107. package/src/transforms/variableMasking.ts +257 -0
  108. package/src/utils/ControlObject.ts +141 -0
  109. package/src/utils/IntGen.ts +33 -0
  110. package/src/utils/NameGen.ts +106 -0
  111. package/src/utils/ast-utils.ts +667 -0
  112. package/src/utils/function-utils.ts +50 -0
  113. package/src/utils/gen-utils.ts +48 -0
  114. package/src/utils/node.ts +78 -0
  115. package/src/utils/object-utils.ts +21 -0
  116. package/src/utils/random-utils.ts +79 -0
  117. package/src/utils/static-utils.ts +66 -0
  118. package/src/validateOptions.ts +256 -0
  119. package/tsconfig.json +13 -8
  120. package/babel.config.js +0 -12
  121. package/dev.js +0 -8
  122. package/dist/compiler.js +0 -34
  123. package/dist/parser.js +0 -59
  124. package/dist/precedence.js +0 -66
  125. package/dist/templates/bufferToString.js +0 -108
  126. package/dist/templates/crash.js +0 -59
  127. package/dist/templates/es5.js +0 -137
  128. package/dist/templates/functionLength.js +0 -34
  129. package/dist/templates/globals.js +0 -9
  130. package/dist/transforms/antiTooling.js +0 -88
  131. package/dist/transforms/controlFlowFlattening/controlFlowFlattening.js +0 -1281
  132. package/dist/transforms/controlFlowFlattening/expressionObfuscation.js +0 -131
  133. package/dist/transforms/es5/antiClass.js +0 -164
  134. package/dist/transforms/es5/antiDestructuring.js +0 -193
  135. package/dist/transforms/es5/antiES6Object.js +0 -185
  136. package/dist/transforms/es5/antiSpreadOperator.js +0 -35
  137. package/dist/transforms/es5/antiTemplate.js +0 -66
  138. package/dist/transforms/es5/es5.js +0 -123
  139. package/dist/transforms/extraction/classExtraction.js +0 -83
  140. package/dist/transforms/identifier/globalAnalysis.js +0 -70
  141. package/dist/transforms/identifier/variableAnalysis.js +0 -104
  142. package/dist/transforms/lock/antiDebug.js +0 -76
  143. package/dist/transforms/stack.js +0 -343
  144. package/dist/transforms/transform.js +0 -350
  145. package/dist/traverse.js +0 -110
  146. package/dist/util/compare.js +0 -145
  147. package/dist/util/gen.js +0 -564
  148. package/dist/util/guard.js +0 -9
  149. package/dist/util/identifiers.js +0 -355
  150. package/dist/util/insert.js +0 -362
  151. package/dist/util/math.js +0 -19
  152. package/dist/util/object.js +0 -40
  153. package/dist/util/random.js +0 -130
  154. package/dist/util/scope.js +0 -20
  155. package/docs/ControlFlowFlattening.md +0 -595
  156. package/docs/Countermeasures.md +0 -63
  157. package/docs/ES5.md +0 -197
  158. package/docs/Integrity.md +0 -75
  159. package/docs/RGF.md +0 -419
  160. package/samples/example.js +0 -15
  161. package/samples/high.js +0 -1
  162. package/samples/input.js +0 -3
  163. package/samples/javascriptobfuscator.com.js +0 -8
  164. package/samples/jscrambler_advanced.js +0 -1894
  165. package/samples/jscrambler_light.js +0 -1134
  166. package/samples/low.js +0 -1
  167. package/samples/medium.js +0 -1
  168. package/samples/obfuscator.io.js +0 -1686
  169. package/samples/preemptive.com.js +0 -16
  170. package/src/compiler.ts +0 -35
  171. package/src/parser.ts +0 -49
  172. package/src/precedence.ts +0 -61
  173. package/src/templates/crash.ts +0 -55
  174. package/src/templates/es5.ts +0 -131
  175. package/src/templates/functionLength.ts +0 -32
  176. package/src/templates/globals.ts +0 -3
  177. package/src/transforms/antiTooling.ts +0 -102
  178. package/src/transforms/controlFlowFlattening/controlFlowFlattening.ts +0 -2146
  179. package/src/transforms/controlFlowFlattening/expressionObfuscation.ts +0 -179
  180. package/src/transforms/es5/antiClass.ts +0 -272
  181. package/src/transforms/es5/antiDestructuring.ts +0 -294
  182. package/src/transforms/es5/antiES6Object.ts +0 -267
  183. package/src/transforms/es5/antiSpreadOperator.ts +0 -56
  184. package/src/transforms/es5/antiTemplate.ts +0 -98
  185. package/src/transforms/es5/es5.ts +0 -149
  186. package/src/transforms/extraction/classExtraction.ts +0 -168
  187. package/src/transforms/identifier/globalAnalysis.ts +0 -85
  188. package/src/transforms/identifier/variableAnalysis.ts +0 -118
  189. package/src/transforms/lock/antiDebug.ts +0 -112
  190. package/src/transforms/stack.ts +0 -551
  191. package/src/transforms/transform.ts +0 -453
  192. package/src/traverse.ts +0 -120
  193. package/src/types.ts +0 -131
  194. package/src/util/compare.ts +0 -181
  195. package/src/util/gen.ts +0 -651
  196. package/src/util/guard.ts +0 -7
  197. package/src/util/identifiers.ts +0 -494
  198. package/src/util/insert.ts +0 -419
  199. package/src/util/math.ts +0 -15
  200. package/src/util/object.ts +0 -39
  201. package/src/util/random.ts +0 -141
  202. package/src/util/scope.ts +0 -21
  203. package/test/code/Cash.src.js +0 -1011
  204. package/test/code/Cash.test.ts +0 -49
  205. package/test/code/Dynamic.src.js +0 -118
  206. package/test/code/Dynamic.test.ts +0 -49
  207. package/test/code/ES6.src.js +0 -235
  208. package/test/code/ES6.test.ts +0 -42
  209. package/test/code/NewFeatures.test.ts +0 -19
  210. package/test/code/StrictMode.src.js +0 -65
  211. package/test/code/StrictMode.test.js +0 -37
  212. package/test/compare.test.ts +0 -104
  213. package/test/index.test.ts +0 -249
  214. package/test/options.test.ts +0 -132
  215. package/test/presets.test.ts +0 -22
  216. package/test/probability.test.ts +0 -44
  217. package/test/templates/template.test.ts +0 -14
  218. package/test/transforms/antiTooling.test.ts +0 -52
  219. package/test/transforms/calculator.test.ts +0 -78
  220. package/test/transforms/controlFlowFlattening/controlFlowFlattening.test.ts +0 -1274
  221. package/test/transforms/controlFlowFlattening/expressionObfuscation.test.ts +0 -192
  222. package/test/transforms/deadCode.test.ts +0 -85
  223. package/test/transforms/dispatcher.test.ts +0 -457
  224. package/test/transforms/es5/antiClass.test.ts +0 -427
  225. package/test/transforms/es5/antiDestructuring.test.ts +0 -157
  226. package/test/transforms/es5/antiES6Object.test.ts +0 -245
  227. package/test/transforms/es5/antiTemplate.test.ts +0 -116
  228. package/test/transforms/es5/es5.test.ts +0 -110
  229. package/test/transforms/extraction/classExtraction.test.ts +0 -86
  230. package/test/transforms/extraction/duplicateLiteralsRemoval.test.ts +0 -200
  231. package/test/transforms/extraction/objectExtraction.test.ts +0 -491
  232. package/test/transforms/flatten.test.ts +0 -721
  233. package/test/transforms/hexadecimalNumbers.test.ts +0 -62
  234. package/test/transforms/identifier/globalConcealing.test.ts +0 -72
  235. package/test/transforms/identifier/movedDeclarations.test.ts +0 -275
  236. package/test/transforms/identifier/renameVariables.test.ts +0 -621
  237. package/test/transforms/lock/antiDebug.test.ts +0 -66
  238. package/test/transforms/lock/browserLock.test.ts +0 -129
  239. package/test/transforms/lock/countermeasures.test.ts +0 -100
  240. package/test/transforms/lock/integrity.test.ts +0 -161
  241. package/test/transforms/lock/lock.test.ts +0 -204
  242. package/test/transforms/lock/osLock.test.ts +0 -312
  243. package/test/transforms/lock/selfDefending.test.ts +0 -68
  244. package/test/transforms/minify.test.ts +0 -575
  245. package/test/transforms/opaquePredicates.test.ts +0 -43
  246. package/test/transforms/preparation.test.ts +0 -157
  247. package/test/transforms/renameLabels.test.ts +0 -95
  248. package/test/transforms/rgf.test.ts +0 -378
  249. package/test/transforms/shuffle.test.ts +0 -135
  250. package/test/transforms/stack.test.ts +0 -573
  251. package/test/transforms/string/stringCompression.test.ts +0 -120
  252. package/test/transforms/string/stringConcealing.test.ts +0 -299
  253. package/test/transforms/string/stringEncoding.test.ts +0 -95
  254. package/test/transforms/string/stringSplitting.test.ts +0 -135
  255. package/test/transforms/transform.test.ts +0 -66
  256. package/test/traverse.test.ts +0 -139
  257. package/test/util/compare.test.ts +0 -34
  258. package/test/util/gen.test.ts +0 -121
  259. package/test/util/identifiers.test.ts +0 -253
  260. package/test/util/insert.test.ts +0 -142
  261. package/test/util/math.test.ts +0 -5
  262. package/test/util/random.test.ts +0 -71
  263. /package/dist/{types.js → obfuscationResult.js} +0 -0
package/src/options.ts CHANGED
@@ -1,264 +1,200 @@
1
- import { ok } from "assert";
2
- import presets from "./presets";
3
- import { ProbabilityMap } from "./probability";
1
+ import Template from "./templates/template";
4
2
 
5
- export interface ObfuscateOptions {
3
+ // JS-Confuser.com imports this file for Type support, therefore some additional types are included here.
4
+
5
+ type Stringed<V> = (V extends string ? V : never) | "true" | "false";
6
+
7
+ /**
8
+ * Configurable probabilities for obfuscator options.
9
+ * - **`false`** = this feature is disabled
10
+ * - **`true`** = this feature is enabled, use default mode
11
+ * - **`0.5`** = 50% chance
12
+ * - **`"mode"`** = enabled, use specified mode
13
+ * - **`["mode1", "mode2"]`** - enabled, choose random mode each occurrence
14
+ * - **`{"mode1": 0.5, "mode2": 0.5}`** - enabled, choose based on specified probabilities
15
+ * - **`{"mode1": 50, "mode2": 50}`** - enabled, each is divided based on total
16
+ * - **`function(x){ return "custom_implementation" }`** - enabled, use specified function
17
+ */
18
+ export type ProbabilityMap<
19
+ T,
20
+ F extends (...args: any[]) => any = () => boolean // Default to a generic function
21
+ > = false | true | number | T | T[] | { [key in Stringed<T>]?: number } | F;
22
+
23
+ export interface CustomLock {
6
24
  /**
7
- * ### `preset`
25
+ * Template lock code that must contain:
8
26
  *
9
- * JS-Confuser comes with three presets built into the obfuscator.
27
+ * - `{countermeasures}`
10
28
  *
11
- * | Preset | Transforms | Performance Reduction | Sample |
12
- * | --- | --- | --- | --- |
13
- * | High | 22/25 | 98% | [Sample](https://github.com/MichaelXF/js-confuser/blob/master/samples/high.js) |
14
- * | Medium | 19/25 | 52% | [Sample](https://github.com/MichaelXF/js-confuser/blob/master/samples/medium.js) |
15
- * | Low | 15/25 | 30% | [Sample](https://github.com/MichaelXF/js-confuser/blob/master/samples/low.js) |
29
+ * The countermeasures function will be invoked when the lock is triggered.
16
30
  *
17
- * You can extend each preset or all go without them entirely. (`"high"/"medium"/"low"`)
31
+ * ```js
32
+ * if(window.navigator.userAgent.includes('Chrome')){
33
+ * {countermeasures}
34
+ * }
35
+ * ```
18
36
  *
19
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
37
+ * Multiple templates can be passed a string array, a random one will be chosen each time.
20
38
  */
21
- preset?: "high" | "medium" | "low" | false;
39
+ code: string | string[] | Template;
40
+ percentagePerBlock: number;
41
+ maxCount?: number;
42
+ minCount?: number;
43
+ }
22
44
 
45
+ export interface CustomStringEncoding {
23
46
  /**
24
- * ### `target`
47
+ * Template string decoder that must contain:
25
48
  *
26
- * The execution context for your output. _Required_.
49
+ * - `{fnName}`
27
50
  *
28
- * 1. `"node"`
29
- * 2. `"browser"`
51
+ * This function will be invoked by the obfuscated code to DECODE the string.
30
52
  *
31
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
53
+ * ```js
54
+ * function {fnName}(str){
55
+ * return Buffer.from(str, 'base64').toString('utf-8')
56
+ * }
57
+ * ```
32
58
  */
33
- target: "node" | "browser";
59
+ code?: string | Template;
60
+ encode: (str: string) => string;
34
61
 
35
62
  /**
36
- * ### `indent`
37
- *
38
- * Controls the indentation of the output.
39
- *
40
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
63
+ * Optional. A decoder function can be provided to ensure the string is able to decode properly.
64
+ * @param str
65
+ * @returns
41
66
  */
42
- indent?: 2 | 4 | "tabs";
67
+ decode?: (str: string) => string;
43
68
 
44
69
  /**
45
- * ### `compact`
46
- *
47
- * Remove's whitespace from the final output. (`true/false`)
70
+ * Should be used when created 'shuffled' variants of the same encoding.
71
+ */
72
+ identity?: string;
73
+ }
74
+
75
+ export interface ObfuscateOptions {
76
+ /**
77
+ * The preset to use for obfuscation.
78
+ */
79
+ preset?: "high" | "medium" | "low" | false;
80
+
81
+ /**
82
+ * The execution context for your output. _Required_.
48
83
  *
49
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
84
+ * 1. `"node"`
85
+ * 2. `"browser"`
86
+ */
87
+ target: "node" | "browser";
88
+
89
+ /**
90
+ * Remove's whitespace from the final output.
50
91
  */
51
92
  compact?: boolean;
52
93
 
53
94
  /**
54
- * ### `hexadecimalNumbers`
55
- *
56
- * Uses the hexadecimal representation for numbers. (`true/false`)
95
+ * Uses the hexadecimal representation for numbers.
57
96
  */
58
97
  hexadecimalNumbers?: boolean;
59
98
 
60
99
  /**
61
- * ### `minify`
62
- *
63
- * Minifies redundant code. (`true/false`)
64
- *
65
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
100
+ * Minifies redundant code.
66
101
  */
67
102
  minify?: boolean;
68
103
 
69
104
  /**
70
- * ### `es5`
71
- *
72
- * Converts output to ES5-compatible code. (`true/false`)
73
- *
74
- * Does not cover all cases such as Promises or Generator functions. Use [Babel](https://babel.dev/).
75
- *
76
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
105
+ * Renames labeled statements. Enabled by default.
77
106
  */
78
- es5?: boolean;
107
+ renameLabels?: ProbabilityMap<boolean, (labelName: string) => boolean>;
79
108
 
80
109
  /**
81
- * ### `renameVariables`
82
- *
83
- * Determines if variables should be renamed. (`true/false`)
84
- * - Potency High
85
- * - Resilience High
86
- * - Cost Medium
87
- *
88
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
110
+ * Determines if variables should be renamed.
89
111
  */
90
- renameVariables?: ProbabilityMap<boolean>;
112
+ renameVariables?: ProbabilityMap<
113
+ boolean,
114
+ (variableName: string, topLevel: boolean) => boolean
115
+ >;
91
116
 
92
117
  /**
93
- * ### `renameGlobals`
94
- *
95
- * Renames top-level variables, turn this off for web-related scripts. Enabled by default. (`true/false`)
96
- *
97
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
118
+ * Renames top-level variables, turn this off for web-related scripts. Enabled by default.
98
119
  */
99
- renameGlobals?: ProbabilityMap<boolean>;
120
+ renameGlobals?: ProbabilityMap<boolean, (variableName: string) => boolean>;
100
121
 
101
122
  /**
102
- * ### `identifierGenerator`
103
- *
104
123
  * Determines how variables are renamed.
105
124
  *
106
- * | Mode | Description | Example |
107
- * | --- | --- | --- |
108
- * | `"hexadecimal"` | Random hex strings | \_0xa8db5 |
109
- * | `"randomized"` | Random characters | w$Tsu4G |
110
- * | `"zeroWidth"` | Invisible characters | U+200D |
111
- * | `"mangled"` | Alphabet sequence | a, b, c |
112
- * | `"number"` | Numbered sequence | var_1, var_2 |
113
- * | `<function>` | Write a custom name generator | See Below |
114
- *
115
- * ```js
116
- * // Custom implementation
117
- * JsConfuser.obfuscate(code, {
118
- * target: "node",
119
- * renameVariables: true,
120
- * identifierGenerator: function () {
121
- * return "$" + Math.random().toString(36).substring(7);
122
- * },
123
- * });
124
- *
125
- * // Numbered variables
126
- * var counter = 0;
127
- * JsConfuser.obfuscate(code, {
128
- * target: "node",
129
- * renameVariables: true,
130
- * identifierGenerator: function () {
131
- * return "var_" + (counter++);
132
- * },
133
- * });
134
- * ```
135
- *
136
- * JSConfuser tries to reuse names when possible, creating very potent code.
137
- *
138
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
125
+ * JS-Confuser tries to reuse names when possible, creating very potent code.
139
126
  */
140
127
  identifierGenerator?: ProbabilityMap<
141
- "hexadecimal" | "randomized" | "zeroWidth" | "mangled" | "number"
128
+ "hexadecimal" | "randomized" | "zeroWidth" | "mangled" | "number",
129
+ () => string
142
130
  >;
143
131
 
144
132
  /**
145
- * ### `controlFlowFlattening`
146
- *
147
133
  * ⚠️ Significantly impacts performance, use sparingly!
148
134
  *
149
- * [Control-flow Flattening](https://docs.jscrambler.com/code-integrity/documentation/transformations/control-flow-flattening) hinders program comprehension by creating convoluted switch statements. (`true/false/0-1`)
135
+ * Control-flow Flattening hinders program comprehension by creating convoluted switch statements.
150
136
  *
151
137
  * Use a number to control the percentage from 0 to 1.
152
- *
153
- * - Potency High
154
- * - Resilience High
155
- * - Cost High
156
- *
157
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
158
138
  */
159
139
  controlFlowFlattening?: ProbabilityMap<boolean>;
160
140
 
161
141
  /**
162
- * ### `globalConcealing`
163
- *
164
- * Global Concealing hides global variables being accessed. (`true/false`)
165
- *
166
- * - Potency Medium
167
- * - Resilience High
168
- * - Cost Low
169
- *
170
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
142
+ * Global Concealing hides global variables being accessed.
171
143
  */
172
- globalConcealing?: ProbabilityMap<boolean>;
144
+ globalConcealing?: ProbabilityMap<boolean, (globalName: string) => boolean>;
173
145
 
174
146
  /**
175
- * ### `stringCompression`
176
- *
177
- * String Compression uses LZW's compression algorithm to compress strings. (`true/false/0-1`)
147
+ * String Compression uses LZW's compression algorithm to compress strings.
178
148
  *
179
149
  * `"console"` -> `inflate('replaĕ!ğğuģģ<~@')`
180
- *
181
- * - Potency High
182
- * - Resilience Medium
183
- * - Cost Medium
184
150
  */
185
- stringCompression?: ProbabilityMap<boolean>;
151
+ stringCompression?: ProbabilityMap<boolean, (strValue: string) => boolean>;
186
152
 
187
153
  /**
188
- * ### `stringConcealing`
189
- *
190
- * [String Concealing](https://docs.jscrambler.com/code-integrity/documentation/transformations/string-concealing) involves encoding strings to conceal plain-text values. (`true/false/0-1`)
154
+ * String Concealing involves encoding strings to conceal plain-text values.
191
155
  *
192
156
  * `"console"` -> `decrypt('<~@rH7+Dert~>')`
193
- *
194
- * - Potency High
195
- * - Resilience Medium
196
- * - Cost Medium
197
157
  */
198
- stringConcealing?: ProbabilityMap<boolean>;
158
+ stringConcealing?: ProbabilityMap<boolean, (strValue: string) => boolean>;
199
159
 
200
160
  /**
201
- * ### `stringEncoding`
202
- *
203
- * [String Encoding](https://docs.jscrambler.com/code-integrity/documentation/transformations/string-encoding) transforms a string into an encoded representation. (`true/false/0-1`)
161
+ * Custom String Encodings allows you to define your own string encoding/decoding functions.
162
+ */
163
+ customStringEncodings?: (
164
+ | CustomStringEncoding
165
+ | ((encodingImplementations: {
166
+ [identity: string]: CustomStringEncoding;
167
+ }) => CustomStringEncoding | null)
168
+ )[];
169
+
170
+ /**
171
+ * String Encoding transforms a string into an escaped unicode representation.
204
172
  *
205
173
  * `"console"` -> `'\x63\x6f\x6e\x73\x6f\x6c\x65'`
206
- *
207
- * - Potency Low
208
- * - Resilience Low
209
- * - Cost Low
210
- *
211
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
212
174
  */
213
- stringEncoding?: ProbabilityMap<boolean>;
175
+ stringEncoding?: ProbabilityMap<boolean, (strValue: string) => boolean>;
214
176
 
215
177
  /**
216
- * ### `stringSplitting`
217
- *
218
- * [String Splitting](https://docs.jscrambler.com/code-integrity/documentation/transformations/string-splitting) splits your strings into multiple expressions. (`true/false/0-1`)
178
+ * String Splitting splits your strings into multiple expressions.
219
179
  *
220
180
  * `"console"` -> `String.fromCharCode(99) + 'ons' + 'ole'`
221
- *
222
- * - Potency Medium
223
- * - Resilience Medium
224
- * - Cost Medium
225
- *
226
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
227
181
  */
228
- stringSplitting?: ProbabilityMap<boolean>;
182
+ stringSplitting?: ProbabilityMap<boolean, (strValue: string) => boolean>;
229
183
 
230
184
  /**
231
- * ### `duplicateLiteralsRemoval`
232
- *
233
- * [Duplicate Literals Removal](https://docs.jscrambler.com/code-integrity/documentation/transformations/duplicate-literals-removal) replaces duplicate literals with a single variable name. (`true/false`)
234
- *
235
- * - Potency Medium
236
- * - Resilience Low
237
- * - Cost High
238
- *
239
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
185
+ * Duplicate Literals Removal replaces duplicate literals with a single variable name.
240
186
  */
241
187
  duplicateLiteralsRemoval?: ProbabilityMap<boolean>;
242
188
 
243
189
  /**
244
- * ### `dispatcher`
245
- *
246
- * Creates a middleman function to process function calls. (`true/false/0-1`)
247
- *
248
- * - Potency Medium
249
- * - Resilience Medium
250
- * - Cost High
251
- *
252
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
190
+ * Creates a middleman function to process function calls.
253
191
  */
254
- dispatcher?: ProbabilityMap<boolean>;
192
+ dispatcher?: ProbabilityMap<boolean, (fnName: string) => boolean>;
255
193
 
256
194
  /**
257
- * ### `rgf`
258
- *
259
- * RGF (Runtime-Generated-Functions) uses the [`new Function(code...)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function) syntax to construct executable code from strings. (`"all"/true/false`)
195
+ * RGF (Runtime-Generated-Functions) uses the [`new Function(code...)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function) syntax to construct executable code from strings.
260
196
  *
261
- * - **This can break your code.
197
+ * - **This can break your code.**
262
198
  * - **Due to the security concerns of arbitrary code execution, you must enable this yourself.**
263
199
  * - The arbitrary code is also obfuscated.
264
200
  *
@@ -274,21 +210,14 @@ export interface ObfuscateOptions {
274
210
  * var C6z0jyO=[new Function('a2Fjjl',"function OqNW8x(OqNW8x){console['log'](OqNW8x)}return OqNW8x(...Array.prototype.slice.call(arguments,1))")];(function(){return C6z0jyO[0](C6z0jyO,...arguments)}('Hello World'))
275
211
  * ```
276
212
  *
277
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
278
213
  */
279
- rgf?: ProbabilityMap<boolean>;
214
+ rgf?: ProbabilityMap<boolean, (fnName: string, isGlobal: boolean) => boolean>;
280
215
 
281
216
  /**
282
- * ### `stack`
283
- *
284
217
  * Local variables are consolidated into a rotating array.
285
218
  *
286
219
  * [Similar to Jscrambler's Variable Masking](https://docs.jscrambler.com/code-integrity/documentation/transformations/variable-masking)
287
220
  *
288
- * - Potency Medium
289
- * - Resilience Medium
290
- * - Cost Low
291
- *
292
221
  * ```js
293
222
  * // Input
294
223
  * function add3(x, y, z){
@@ -302,16 +231,10 @@ export interface ObfuscateOptions {
302
231
  * };
303
232
  * ```
304
233
  */
305
- stack?: ProbabilityMap<boolean>;
234
+ variableMasking?: ProbabilityMap<boolean, (fnName: string) => boolean>;
306
235
 
307
236
  /**
308
- * ### `objectExtraction`
309
- *
310
- * Extracts object properties into separate variables. (`true/false`)
311
- *
312
- * - Potency Medium
313
- * - Resilience Medium
314
- * - Cost Low
237
+ * Extracts object properties into separate variables.
315
238
  *
316
239
  * ```js
317
240
  * // Input
@@ -330,529 +253,137 @@ export interface ObfuscateOptions {
330
253
  * // ...
331
254
  * }
332
255
  * ```
333
- *
334
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
335
256
  */
336
- objectExtraction?: ProbabilityMap<boolean>;
257
+ objectExtraction?: ProbabilityMap<boolean, (objectName: string) => boolean>;
337
258
 
338
259
  /**
339
- * ### `flatten`
340
- *
341
- * Brings independent declarations to the highest scope. (`true/false`)
342
- *
343
- * - Potency Medium
344
- * - Resilience Medium
345
- * - Cost High
346
- *
347
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
260
+ * Declares functions at the top of the program, preserving their original scope.
348
261
  */
349
- flatten?: ProbabilityMap<boolean>;
262
+ flatten?: ProbabilityMap<boolean, (fnName: string) => boolean>;
350
263
 
351
264
  /**
352
- * ### `deadCode`
353
- *
354
- * Randomly injects dead code. (`true/false/0-1`)
265
+ * Randomly injects dead code.
355
266
  *
356
267
  * Use a number to control the percentage from 0 to 1.
357
- *
358
- * - Potency Medium
359
- * - Resilience Medium
360
- * - Cost Low
361
- *
362
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
363
268
  */
364
269
  deadCode?: ProbabilityMap<boolean>;
365
270
 
366
271
  /**
367
- * ### `calculator`
368
- *
369
- * Creates a calculator function to handle arithmetic and logical expressions. (`true/false/0-1`)
370
- *
371
- * - Potency Medium
372
- * - Resilience Medium
373
- * - Cost Low
272
+ * Creates a calculator function to handle arithmetic and logical expressions.
374
273
  *
375
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
376
274
  */
377
275
  calculator?: ProbabilityMap<boolean>;
378
276
 
379
277
  lock?: {
380
278
  /**
381
- * ### `lock.selfDefending`
382
- *
383
279
  * Prevents the use of code beautifiers or formatters against your code.
384
280
  *
385
281
  * [Identical to Obfuscator.io's Self Defending](https://github.com/javascript-obfuscator/javascript-obfuscator#selfdefending)
386
282
  *
387
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
388
283
  */
389
284
  selfDefending?: boolean;
390
285
 
391
286
  /**
392
- * ### `lock.antiDebug`
393
- *
394
- * Adds `debugger` statements throughout the code. Additionally adds a background function for DevTools detection. (`true/false/0-1`)
395
- *
396
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
287
+ * Adds `debugger` statements throughout the code.
397
288
  */
398
289
  antiDebug?: ProbabilityMap<boolean>;
399
290
 
400
291
  /**
401
- * ### `lock.context`
292
+ * Tamper Protection safeguards the runtime behavior from being altered by JavaScript pitfalls.
402
293
  *
403
- * Properties that must be present on the `window` object (or `global` for NodeJS). (`string[]`)
294
+ * **⚠️ Tamper Protection requires eval and ran in a non-strict mode environment!**
404
295
  *
405
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
296
+ * - **This can break your code.**
297
+ * - **Due to the security concerns of arbitrary code execution, you must enable this yourself.**
298
+ *
299
+ * @see https://github.com/MichaelXF/js-confuser/blob/master/TamperProtection.md
406
300
  */
407
- context?: string[];
301
+ tamperProtection?: boolean | ((varName: string) => boolean);
408
302
 
409
303
  /**
410
- * ### `lock.startDate`
411
- *
412
304
  * When the program is first able to be used. (`number` or `Date`)
413
305
  *
414
306
  * Number should be in milliseconds.
415
- *
416
- * - Potency Low
417
- * - Resilience Medium
418
- * - Cost Medium
419
- *
420
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
421
307
  */
422
308
  startDate?: number | Date | false;
423
309
 
424
310
  /**
425
- * ### `lock.endDate`
426
- *
427
311
  * When the program is no longer able to be used. (`number` or `Date`)
428
312
  *
429
313
  * Number should be in milliseconds.
430
- *
431
- * - Potency Low
432
- * - Resilience Medium
433
- * - Cost Medium
434
- *
435
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
436
314
  */
437
315
  endDate?: number | Date | false;
438
316
 
439
317
  /**
440
- * ### `lock.domainLock`
441
- * Array of regex strings that the `window.location.href` must follow. (`Regex[]` or `string[]`)
442
- *
443
- * - Potency Low
444
- * - Resilience Medium
445
- * - Cost Medium
446
- *
447
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
318
+ * Array of regex strings that the `window.location.href` must follow.
448
319
  */
449
320
  domainLock?: RegExp[] | string[] | false;
450
321
 
451
322
  /**
452
- * ### `lock.osLock`
453
- * Array of operating-systems where the script is allowed to run. (`string[]`)
454
- *
455
- * - Potency Low
456
- * - Resilience Medium
457
- * - Cost Medium
458
- *
459
- * Allowed values: `"linux"`, `"windows"`, `"osx"`, `"android"`, `"ios"`
460
- *
461
- * Example: `["linux", "windows"]`
462
- *
463
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
464
- */
465
- osLock?: ("linux" | "windows" | "osx" | "android" | "ios")[] | false;
466
-
467
- /**
468
- * ### `lock.browserLock`
469
- * Array of browsers where the script is allowed to run. (`string[]`)
470
- *
471
- * - Potency Low
472
- * - Resilience Medium
473
- * - Cost Medium
323
+ * Integrity ensures the source code is unchanged.
474
324
  *
475
- * Allowed values: `"firefox"`, `"chrome"`, `"iexplorer"`, `"edge"`, `"safari"`, `"opera"`
476
- *
477
- * Example: `["firefox", "chrome"]`
478
- *
479
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
325
+ * @see https://github.com/MichaelXF/js-confuser/blob/master/Integrity.md
480
326
  */
481
- browserLock?:
482
- | ("firefox" | "chrome" | "iexplorer" | "edge" | "safari" | "opera")[]
483
- | false;
327
+ integrity?: ProbabilityMap<boolean, (fnName: string) => boolean>;
484
328
 
485
329
  /**
486
- * ### `lock.integrity`
487
- *
488
- * Integrity ensures the source code is unchanged. (`true/false/0-1`)
489
- *
490
- * [Learn more here](https://github.com/MichaelXF/js-confuser/blob/master/Integrity.md).
491
- *
492
- * - Potency Medium
493
- * - Resilience High
494
- * - Cost High
495
- *
496
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
497
- */
498
- integrity?: ProbabilityMap<boolean>;
499
-
500
- /**
501
- * ### `lock.countermeasures`
502
- *
503
330
  * A custom callback function to invoke when a lock is triggered. (`string/false`)
504
331
  *
505
332
  * This could be due to an invalid domain, incorrect time, or code's integrity changed.
506
333
  *
507
- * [Learn more about the rules of your countermeasures function](https://github.com/MichaelXF/js-confuser/blob/master/Countermeasures.md).
334
+ * If no countermeasures function is provided (`undefined` or `true`), the obfuscator falls back to crashing the process.
508
335
  *
509
- * Otherwise, the obfuscator falls back to crashing the process.
336
+ * If `countermeasures` is `false`, no crash will occur.
510
337
  *
511
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
338
+ * @see https://github.com/MichaelXF/js-confuser/blob/master/Countermeasures.md
512
339
  */
513
340
  countermeasures?: string | boolean;
341
+
342
+ customLocks?: CustomLock[];
514
343
  };
515
344
 
345
+ functionOutlining?: ProbabilityMap<boolean>;
346
+
516
347
  /**
517
- * ### `movedDeclarations`
518
- *
519
- * Moves variable declarations to the top of the context. (`true/false`)
520
- *
521
- * - Potency Medium
522
- * - Resilience Medium
523
- * - Cost Low
524
- *
525
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
348
+ * Moves variable declarations to the top of the context.
526
349
  */
527
350
  movedDeclarations?: ProbabilityMap<boolean>;
528
351
 
529
352
  /**
530
- * ### `opaquePredicates`
531
- *
532
353
  * An [Opaque Predicate](https://en.wikipedia.org/wiki/Opaque_predicate) is a predicate(true/false) that is evaluated at runtime, this can confuse reverse engineers
533
- * understanding your code. (`true/false`)
534
- *
535
- * - Potency Medium
536
- * - Resilience Medium
537
- * - Cost Low
538
- *
539
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
354
+ * understanding your code.
540
355
  */
541
356
  opaquePredicates?: ProbabilityMap<boolean>;
542
357
 
543
358
  /**
544
- * ### `shuffle`
545
- *
546
359
  * Shuffles the initial order of arrays. The order is brought back to the original during runtime. (`"hash"/true/false/0-1`)
547
- *
548
- * - Potency Medium
549
- * - Resilience Low
550
- * - Cost Low
551
- *
552
- * | Mode | Description |
553
- * | --- | --- |
554
- * | `"hash"`| Array is shifted based on hash of the elements |
555
- * | `true`| Arrays are shifted *n* elements, unshifted at runtime |
556
- * | `false` | Feature disabled |
557
- *
558
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
559
360
  */
560
- shuffle?: ProbabilityMap<boolean | "hash">;
361
+ shuffle?: ProbabilityMap<boolean>;
561
362
 
562
363
  /**
563
- * ### `verbose`
564
- *
565
- * Enable logs to view the obfuscator's state. (`true/false`)
566
- *
567
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
364
+ * Modified functions will retain the correct `function.length` property. Enabled by default.
568
365
  */
569
- verbose?: boolean;
366
+ preserveFunctionLength?: boolean;
570
367
 
571
368
  /**
572
- * ### `globalVariables`
573
- *
574
- * Set of global variables. *Optional*. (`Set<string>`)
575
- *
576
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
369
+ * Semantically changes the AST to bypass automated tools.
577
370
  */
578
- globalVariables?: Set<string>;
371
+ astScrambler?: boolean;
579
372
 
580
373
  /**
581
- * ### `debugComments`
582
- *
583
- * Enable debug comments. (`true/false`)
374
+ * Packs the output code into a single `Function()` call.
584
375
  *
585
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
376
+ * Designed to escape strict mode constraints.
586
377
  */
587
- debugComments?: boolean;
378
+ pack?: boolean;
588
379
 
589
380
  /**
590
- * ### `preserveFunctionLength`
591
- *
592
- * Modified functions will retain the correct `function.length` property. Enabled by default. (`true/false`)
593
- *
594
- * [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
381
+ * Set of global variables. *Optional*.
595
382
  */
596
- preserveFunctionLength?: boolean;
597
- }
598
-
599
- const validProperties = new Set([
600
- "preset",
601
- "target",
602
- "indent",
603
- "compact",
604
- "hexadecimalNumbers",
605
- "minify",
606
- "es5",
607
- "renameVariables",
608
- "renameGlobals",
609
- "identifierGenerator",
610
- "controlFlowFlattening",
611
- "globalConcealing",
612
- "stringCompression",
613
- "stringConcealing",
614
- "stringEncoding",
615
- "stringSplitting",
616
- "duplicateLiteralsRemoval",
617
- "dispatcher",
618
- "rgf",
619
- "objectExtraction",
620
- "flatten",
621
- "deadCode",
622
- "calculator",
623
- "lock",
624
- "movedDeclarations",
625
- "opaquePredicates",
626
- "shuffle",
627
- "stack",
628
- "verbose",
629
- "globalVariables",
630
- "debugComments",
631
- "preserveFunctionLength",
632
- ]);
633
-
634
- const validOses = new Set(["windows", "linux", "osx", "ios", "android"]);
635
- const validBrowsers = new Set([
636
- "firefox",
637
- "chrome",
638
- "iexplorer",
639
- "edge",
640
- "safari",
641
- "opera",
642
- ]);
643
-
644
- export function validateOptions(options: ObfuscateOptions) {
645
- if (!options || Object.keys(options).length <= 1) {
646
- /**
647
- * Give a welcoming introduction to those who skipped the documentation.
648
- */
649
- var line = `You provided zero obfuscation options. By default everything is disabled.\nYou can use a preset with:\n\n> {target: '${
650
- options.target || "node"
651
- }', preset: 'high' | 'medium' | 'low'}.\n\n\nView all settings here:\nhttps://github.com/MichaelXF/js-confuser#options`;
652
- throw new Error(
653
- `\n\n` +
654
- line
655
- .split("\n")
656
- .map((x) => `\t${x}`)
657
- .join("\n") +
658
- `\n\n`
659
- );
660
- }
661
-
662
- ok(options, "options cannot be null");
663
- ok(
664
- options.target,
665
- "Missing options.target option (required, must one the following: 'browser' or 'node')"
666
- );
667
- ok(
668
- ["browser", "node"].includes(options.target),
669
- `'${options.target}' is not a valid target mode`
670
- );
671
-
672
- Object.keys(options).forEach((key) => {
673
- if (!validProperties.has(key)) {
674
- throw new TypeError("Invalid option: '" + key + "'");
675
- }
676
- });
677
-
678
- if (
679
- options.target === "node" &&
680
- options.lock &&
681
- options.lock.browserLock &&
682
- options.lock.browserLock.length
683
- ) {
684
- throw new TypeError('browserLock can only be used when target="browser"');
685
- }
686
-
687
- if (options.lock) {
688
- // Validate browser-lock option
689
- if (
690
- options.lock.browserLock &&
691
- typeof options.lock.browserLock !== "undefined"
692
- ) {
693
- ok(
694
- Array.isArray(options.lock.browserLock),
695
- "browserLock must be an array"
696
- );
697
- ok(
698
- !options.lock.browserLock.find(
699
- (browserName) => !validBrowsers.has(browserName)
700
- ),
701
- 'Invalid browser name. Allowed: "firefox", "chrome", "iexplorer", "edge", "safari", "opera"'
702
- );
703
- }
704
- // Validate os-lock option
705
- if (options.lock.osLock && typeof options.lock.osLock !== "undefined") {
706
- ok(Array.isArray(options.lock.osLock), "osLock must be an array");
707
- ok(
708
- !options.lock.osLock.find((osName) => !validOses.has(osName)),
709
- 'Invalid OS name. Allowed: "windows", "linux", "osx", "ios", "android"'
710
- );
711
- }
712
- // Validate domain-lock option
713
- if (
714
- options.lock.domainLock &&
715
- typeof options.lock.domainLock !== "undefined"
716
- ) {
717
- ok(Array.isArray(options.lock.domainLock), "domainLock must be an array");
718
- }
719
-
720
- // Validate context option
721
- if (options.lock.context && typeof options.lock.context !== "undefined") {
722
- ok(Array.isArray(options.lock.context), "context must be an array");
723
- }
724
-
725
- // Validate start-date option
726
- if (
727
- typeof options.lock.startDate !== "undefined" &&
728
- options.lock.startDate
729
- ) {
730
- ok(
731
- typeof options.lock.startDate === "number" ||
732
- options.lock.startDate instanceof Date,
733
- "startDate must be Date object or number"
734
- );
735
- }
736
-
737
- // Validate end-date option
738
- if (typeof options.lock.endDate !== "undefined" && options.lock.endDate) {
739
- ok(
740
- typeof options.lock.endDate === "number" ||
741
- options.lock.endDate instanceof Date,
742
- "endDate must be Date object or number"
743
- );
744
- }
745
- }
746
-
747
- if (options.preset) {
748
- if (!presets[options.preset]) {
749
- throw new TypeError("Unknown preset of '" + options.preset + "'");
750
- }
751
- }
752
- }
383
+ globalVariables?: Set<string>;
753
384
 
754
- /**
755
- * Corrects the user's options. Sets the default values and validates the configuration.
756
- * @param options
757
- * @returns
758
- */
759
- export async function correctOptions(
760
- options: ObfuscateOptions
761
- ): Promise<ObfuscateOptions> {
762
- if (options.preset) {
763
- // Clone and allow overriding
764
- options = Object.assign({}, presets[options.preset], options);
765
- }
766
-
767
- if (!options.hasOwnProperty("debugComments")) {
768
- options.debugComments = false; // debugComments is off by default
769
- }
770
-
771
- if (!options.hasOwnProperty("compact")) {
772
- options.compact = true; // Compact is on by default
773
- }
774
- if (!options.hasOwnProperty("renameGlobals")) {
775
- options.renameGlobals = true; // RenameGlobals is on by default
776
- }
777
- if (!options.hasOwnProperty("preserveFunctionLength")) {
778
- options.preserveFunctionLength = true; // preserveFunctionLength is on by default
779
- }
780
-
781
- if (options.globalVariables && !(options.globalVariables instanceof Set)) {
782
- options.globalVariables = new Set(Object.keys(options.globalVariables));
783
- }
784
-
785
- if (options.lock && options.lock.selfDefending) {
786
- options.compact = true; // self defending forcibly enables this
787
- }
788
-
789
- // options.globalVariables outlines generic globals that should be present in the execution context
790
- if (!options.hasOwnProperty("globalVariables")) {
791
- options.globalVariables = new Set([]);
792
-
793
- if (options.target == "browser") {
794
- // browser
795
- [
796
- "window",
797
- "document",
798
- "postMessage",
799
- "alert",
800
- "confirm",
801
- "location",
802
- "btoa",
803
- "atob",
804
- "unescape",
805
- "encodeURIComponent",
806
- ].forEach((x) => options.globalVariables.add(x));
807
- } else {
808
- // node
809
- [
810
- "global",
811
- "Buffer",
812
- "require",
813
- "process",
814
- "exports",
815
- "module",
816
- "__dirname",
817
- "__filename",
818
- ].forEach((x) => options.globalVariables.add(x));
819
- }
820
-
821
- [
822
- "globalThis",
823
- "console",
824
- "parseInt",
825
- "parseFloat",
826
- "Math",
827
- "JSON",
828
- "Promise",
829
- "String",
830
- "Boolean",
831
- "Function",
832
- "Object",
833
- "Array",
834
- "Proxy",
835
- "Error",
836
- "TypeError",
837
- "ReferenceError",
838
- "RangeError",
839
- "EvalError",
840
- "setTimeout",
841
- "clearTimeout",
842
- "setInterval",
843
- "clearInterval",
844
- "setImmediate",
845
- "clearImmediate",
846
- "queueMicrotask",
847
- "isNaN",
848
- "isFinite",
849
- "Set",
850
- "Map",
851
- "WeakSet",
852
- "WeakMap",
853
- "Symbol",
854
- ].forEach((x) => options.globalVariables.add(x));
855
- }
856
-
857
- return options;
385
+ /**
386
+ * Enable logs to view the obfuscator's state.
387
+ */
388
+ verbose?: boolean;
858
389
  }