esbuild-javascript-obfuscator 1.0.2 → 1.0.3

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.
package/README.md CHANGED
@@ -1,7 +1,16 @@
1
1
  # esbuild-javascript-obfuscator
2
2
 
3
- This project obfuscates your code using the javascript-obfuscator NPM package.
3
+ This project obfuscates your code using the [javascript-obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator) NPM package.
4
4
 
5
- This completely serves a plugin for esbuild.
5
+ ## Highlights
6
6
 
7
- Licensed with MIT.
7
+ - Currently the **only maintained** esbuild plugin for the **javascript-obfuscator** package.
8
+ - **The only** esbuild plugin that supports **VM Obfuscation** _(API key required)._
9
+
10
+ ## Acknowledgments
11
+
12
+ Thanks to [sanex3339](https://github.com/javascript-obfuscator/javascript-obfuscator/commits?author=sanex3339), the creator of **javascript-obfuscator**.
13
+
14
+ ## Important
15
+
16
+ Licensed under the MIT License.
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/plugin/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAMnC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAwDxE,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,mBAAmB,GA+DzD,OAAO,CAAC,MAAM,CACnB"}
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/plugin/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAMnC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAkDxE,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,mBAAmB,GAwEtD,OAAO,CAAC,MAAM,CACtB"}
@@ -4,13 +4,14 @@ import fs from 'fs';
4
4
  import path from 'path';
5
5
  import { ValidateOptions } from './optionValidator';
6
6
  import { CreateLogger } from '../logging';
7
- async function ObfuscateFile(pluginOptions, finalized, file) {
7
+ async function ObfuscateFile(pluginOptions, file) {
8
8
  const logger = CreateLogger(pluginOptions);
9
+ const originalCode = new TextDecoder().decode(file.contents);
9
10
  if (!file.path.endsWith('.js')) {
10
11
  logger('file does not end with .js, skipping obfuscation: ', file.path);
11
- return false;
12
+ return originalCode;
12
13
  }
13
- if (pluginOptions.VMProtection.Enabled) {
14
+ if (pluginOptions.VMProtection?.Enabled) {
14
15
  const { ApiKey, Version } = pluginOptions.VMProtection;
15
16
  logger('obfuscating with VM Protection ... ');
16
17
  const proApiOptions = {
@@ -19,20 +20,17 @@ async function ObfuscateFile(pluginOptions, finalized, file) {
19
20
  if (Version) {
20
21
  Reflect.set(proApiOptions, 'version', Version);
21
22
  }
22
- const obfuscateResult = await JsObf.obfuscatePro(new TextDecoder().decode(file.contents), pluginOptions.ObfuscatorOptions, proApiOptions, logger);
23
- finalized.push({ fileName: file.path, outputCode: obfuscateResult.getObfuscatedCode().toString() });
23
+ const obfuscateResult = await JsObf.obfuscatePro(originalCode, pluginOptions.ObfuscatorOptions, proApiOptions, logger);
24
24
  logger('obfuscation with VM Protection completed for file: ', file.path);
25
- return true;
25
+ return obfuscateResult.getObfuscatedCode().toString();
26
26
  }
27
- logger('obfusacting with no VM protection...');
28
- const obfuscateResult = JsObf.obfuscate(new TextDecoder().decode(file.contents), pluginOptions.ObfuscatorOptions);
29
- finalized.push({ fileName: file.path, outputCode: obfuscateResult.getObfuscatedCode().toString() });
27
+ logger('obfuscating with no VM protection...');
28
+ const obfuscateResult = JsObf.obfuscate(originalCode, pluginOptions.ObfuscatorOptions);
30
29
  logger('obfuscation completed for file: ', file.path);
31
- return true;
30
+ return obfuscateResult.getObfuscatedCode().toString();
32
31
  }
33
32
  export function JSObfuscatorPlugin(options) {
34
33
  const log = CreateLogger(options);
35
- const finalized = [];
36
34
  log('JSObfuscatorPlugin initialized with options:', options);
37
35
  const [isValid, errorMsg] = ValidateOptions(options);
38
36
  log('isValid: ', isValid, ' errorMsg: ', errorMsg);
@@ -40,7 +38,6 @@ export function JSObfuscatorPlugin(options) {
40
38
  throw new Error(`Invalid JSObfuscatorPlugin options: ${errorMsg || 'no error message provided'}`);
41
39
  }
42
40
  log('creating plugin');
43
- const obfuscateFile = ObfuscateFile.bind(null, options, finalized);
44
41
  return {
45
42
  name: 'esbuild-javascript-obfuscator',
46
43
  setup(build) {
@@ -56,22 +53,32 @@ export function JSObfuscatorPlugin(options) {
56
53
  log('No output files found, skipping obfuscation');
57
54
  return;
58
55
  }
59
- if (outputFiles.length > 1 && !options.ObfuscateAllFiles) {
60
- throw new Error('outputFiles.length > 1 but ObfuscateAllFiles not explicitly set');
61
- }
62
- if (outputFiles.length > 1 && options.ObfuscateAllFiles) {
63
- for (const file of outputFiles) {
64
- const success = await obfuscateFile(file);
65
- if (!success) {
66
- log('failed to obfuscate file: ', file.path);
56
+ /* keep this here: on `watch` modes, this can cause a memory leak if not placed in this certain way.*/
57
+ const finalized = [];
58
+ for (const file of outputFiles) {
59
+ const fileName = path.basename(file.path);
60
+ const shouldObfuscate = options.ObfuscateAllFiles || (options.ObfuscateFilesWhitelist?.includes(fileName) ?? false);
61
+ if (shouldObfuscate && file.path.endsWith('.js')) {
62
+ try {
63
+ const outputCode = await ObfuscateFile(options, file);
64
+ /* write obfuscated output */
65
+ finalized.push({ fileName: file.path, outputCode });
66
+ }
67
+ catch (err) {
68
+ log('failed to obfuscate file: ', file.path, err);
69
+ /* it failed, so just write the original file to the output */
70
+ finalized.push({
71
+ fileName: file.path,
72
+ outputCode: new TextDecoder().decode(file.contents),
73
+ });
67
74
  }
68
75
  }
69
- }
70
- else {
71
- const file = outputFiles[0];
72
- const success = await obfuscateFile(file);
73
- if (!success) {
74
- log('failed to obfuscate file: ', file.path);
76
+ else {
77
+ /* should not obfuscate, write orig file. */
78
+ finalized.push({
79
+ fileName: file.path,
80
+ outputCode: new TextDecoder().decode(file.contents),
81
+ });
75
82
  }
76
83
  }
77
84
  /* write pass */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esbuild-javascript-obfuscator",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "An esbuild plugin to obfuscate JavaScript code.",
5
5
 
6
6
  "author": {