omni-obfuscator 1.0.0 → 1.1.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.
package/README.md CHANGED
@@ -61,3 +61,18 @@ Obfuscation is not encryption, and cannot guarantee against reverse engineering.
61
61
  npm ci; npm test; npm pack --dry-run; npm pack.
62
62
 
63
63
  MIT license for this wrapper. Upstream dependencies retain their respective licenses.
64
+
65
+ ## Automatic setup (v1.1.0)
66
+
67
+ ```sh
68
+ npm install -D omni-obfuscator
69
+ npx omni-obfuscator init
70
+ npm run build:obfuscated
71
+ npm run build:obfuscated:high
72
+ ```
73
+
74
+ `init` adds four build scripts without replacing existing custom commands. `detect` identifies the project, `doctor` reports basic compatibility, and `reset` removes matching generated scripts. An existing JSON configuration takes precedence. No configuration is needed for supported conventional layouts.
75
+
76
+ Electron Builder: requires an inline package.json build configuration with explicit simple files globs, no custom packaging hooks or custom app directory. Copies application files into a temporary app directory, obfuscates JavaScript, installs production dependencies there, runs Electron Builder, and removes staging in a finally block. Package output uses the existing output directory. Common Windows NSIS and portable build scripts are recognized; other custom script flags are not automatically reproduced. Full installer compatibility must be tested per application. Keep omni-obfuscator in devDependencies.
77
+
78
+ Vite, Vue CLI and Create React App: runs the existing build script and transforms the conventional dist or build directory. Custom output paths, integrity hashes, service worker manifests and source maps may require explicit integration. Prefer the Vite/Rollup plugin for hash-sensitive builds. Next.js, Angular, Electron Forge and unknown layouts are detected or require explicit configuration rather than guessing paths. The existing protect command and config-based pipeline remain supported. Dry run skips the actual packaging/build; it requires existing frontend output and may temporarily stage Electron files. Automatic cleanup is limited to the current run's own staging directory.
package/bin/cli.cjs CHANGED
@@ -4,13 +4,15 @@ const fs=require('node:fs/promises');
4
4
  const {parseArgs}=require('node:util');
5
5
  const {spawnSync}=require('node:child_process');
6
6
  const {protect}=require('../src/index.cjs');
7
+ const auto=require('../src/auto.cjs');
7
8
  const help=`Omni Obfuscator
8
9
  omni-obfuscator protect <file|directory> --output <path> [--mode light|medium|high]
9
10
  omni-obfuscator protect <compiled-dir> --in-place [--dry-run]
10
11
  omni-obfuscator build [--config omni-obfuscator.config.json] [--mode medium]
11
12
  Options: --target node|browser|browser-no-eval, --include <glob>, --exclude <glob>,
12
13
  --keep-source-maps, --dry-run, --help, --version
13
- build reads a JSON config with targets and optional npm scripts before/after.
14
+ init | detect | doctor | reset
15
+ build uses optional JSON configuration or automatic project detection.
14
16
  `;
15
17
  function script(name){
16
18
  if(!name)return;
@@ -28,7 +30,9 @@ function script(name){
28
30
  if(v['keep-source-maps'])overrides.removeSourceMaps=false;
29
31
  const report=r=>console.log(JSON.stringify(r,null,2));
30
32
  if(p[0]==='protect'&&p.length===2){report(await protect(p[1],{...overrides,output:v.output,inPlace:v['in-place']}));return;}
33
+ if(p.length===1&&['init','detect','doctor','reset'].includes(p[0])){ const result=await auto[p[0]](); if(p[0]==='detect')delete result.pkg; report(result);return; }
31
34
  if(p[0]==='build'&&p.length===1){
35
+ if(!v.config&&!await auto.exists('omni-obfuscator.config.json')){report(await auto.autoBuild(process.cwd(),overrides));return;}
32
36
  const cfg=JSON.parse(await fs.readFile(v.config||'omni-obfuscator.config.json','utf8'));
33
37
  if(!Array.isArray(cfg.targets)||!cfg.targets.length)throw new Error('Config requires targets');
34
38
  if(!v['dry-run'])script(cfg.before);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "omni-obfuscator",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Framework-independent JavaScript obfuscation CLI, API and build integrations",
5
5
  "license": "MIT",
6
6
  "main": "src/index.cjs",
@@ -27,7 +27,7 @@
27
27
  "node": ">=20"
28
28
  },
29
29
  "scripts": {
30
- "test": "node --test",
30
+ "test": "node --test test/*.test.cjs",
31
31
  "prepublishOnly": "npm test"
32
32
  },
33
33
  "dependencies": {
@@ -48,4 +48,4 @@
48
48
  "publishConfig": {
49
49
  "access": "public"
50
50
  }
51
- }
51
+ }
package/src/auto.cjs ADDED
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+ const fs=require('node:fs/promises');
3
+ const path=require('node:path');
4
+ const {spawnSync}=require('node:child_process');
5
+ const {protect}=require('./index.cjs');
6
+ const CONFIG='omni-obfuscator.config.json';
7
+ const commands=Object.fromEntries(['','light','medium','high'].map(m=>['build:obfuscated'+(m?':'+m:''),'omni-obfuscator build'+(m?' --mode '+m:'')]));
8
+ async function exists(p){try{await fs.access(p);return true;}catch(e){if(e.code==='ENOENT')return false;throw e;}}
9
+ async function read(p){return JSON.parse(await fs.readFile(p,'utf8'));}
10
+ async function detect(root=process.cwd()){
11
+ const pkg=await read(path.join(root,'package.json'));
12
+ const deps={...pkg.dependencies,...pkg.devDependencies};
13
+ const scripts=pkg.scripts||{};
14
+ const build=scripts.build||'';
15
+ let kind='generic',output=null;
16
+ if(deps['electron-builder']||/electron-builder/.test(build))kind='electron-builder';
17
+ else if(deps['@electron-forge/cli'])kind='electron-forge';
18
+ else if(deps.next)kind='next';
19
+ else if(deps.vite){kind='vite';output='dist';}
20
+ else if(deps['react-scripts']){kind='react';output='build';}
21
+ else if(deps['@angular/core'])kind='angular';
22
+ else if(deps['@vue/cli-service']){kind='vue';output='dist';}
23
+ return {kind,output,hasBuild:!!scripts.build,pkg};
24
+ }
25
+ async function init(root=process.cwd()){
26
+ const d=await detect(root);
27
+ const pkg=d.pkg;pkg.scripts||={};
28
+ for(const [k,v] of Object.entries(commands))if(pkg.scripts[k]&&pkg.scripts[k]!==v)throw new Error('Existing script conflicts: '+k);
29
+ Object.assign(pkg.scripts,commands);
30
+ await fs.writeFile(path.join(root,'package.json'),JSON.stringify(pkg,null,2)+'\n');
31
+ return {framework:d.kind,scripts:commands,customConfig:await exists(path.join(root,CONFIG)),note:'Automatic build supports conventional Electron Builder and frontend outputs; custom layouts may require configuration.'};
32
+ }
33
+ function npmScript(root,name){
34
+ if(!/^[a-zA-Z0-9:_-]+$/.test(name))throw new Error('Invalid script');
35
+ const r=spawnSync(process.platform==='win32'?'npm.cmd':'npm',['run',name],{cwd:root,stdio:'inherit',shell:process.platform==='win32'});
36
+ if(r.error)throw r.error;if(r.status!==0)throw new Error('Build script failed: '+name);
37
+ }
38
+ async function electronBuild(root,d,opts){
39
+ // Electron Builder afterPack is too late: use beforePack to stage via appDir.
40
+ // Build API accepts separate projectDir and directories.app; project config stays at root.
41
+ const pkg=d.pkg;
42
+ if(!pkg.build||typeof pkg.build!=='object'||Array.isArray(pkg.build))throw new Error('Automatic Electron requires package.json build configuration');
43
+ const cfg=pkg.build;
44
+ if(cfg.directories?.app&&cfg.directories.app!=='.')throw new Error('Custom directories.app requires explicit configuration');
45
+ if(cfg.extends||cfg.beforePack||cfg.afterPack||cfg.afterSign)throw new Error('Custom builder hooks/extends require explicit integration');
46
+ if(!Array.isArray(cfg.files)||!cfg.files.length||cfg.files.some(f=>typeof f!=='string'||f.startsWith('!')||path.isAbsolute(f)||f.includes('..')))throw new Error('Automatic Electron requires explicit simple build.files globs');
47
+ const dirs=[...new Set(cfg.files.map(f=>f.split('/')[0]))];
48
+ if(dirs.some(f=>/[*?{}\[\]]/.test(f)||['node_modules','.git','dist','.'].includes(f) || f.includes('\\') || f.includes(':')))throw new Error('Unsupported broad build.files; use explicit top-level paths');
49
+ const main=pkg.main||'index.js';
50
+ if(!dirs.includes(main.split('/')[0]))throw new Error('Main entry not covered by build.files');
51
+ const stage=await fs.mkdtemp(path.join(root,'.omni-stage-'));
52
+ try{
53
+ for(const dir of dirs){
54
+ const source=path.join(root,dir);if(!await exists(source))continue;
55
+ await fs.cp(source,path.join(stage,dir),{recursive:true,filter:async s=>{const st=await fs.lstat(s);return !st.isSymbolicLink()&&!['node_modules','.git'].includes(path.basename(s));}});
56
+ }
57
+ const stagedPkg={...pkg};delete stagedPkg.build;
58
+ await fs.writeFile(path.join(stage,'package.json'),JSON.stringify(stagedPkg,null,2));
59
+ const results=[];
60
+ for(const dir of dirs){
61
+ const input=path.join(stage,dir);if(!await exists(input))continue;
62
+ try{results.push(await protect(input,{target:'browser-no-eval',...opts,inPlace:true}));}
63
+ catch(e){if(e.message!=='No matching JavaScript files')throw e;}
64
+ }
65
+ if(!results.length)throw new Error('No application JavaScript found');
66
+ if(opts.dryRun)return {framework:d.kind,results,dryRun:true};
67
+ // Install production dependencies in the staging app, avoiding modification of original node_modules.
68
+ const lock=path.join(root,'package-lock.json');
69
+ if(await exists(lock))await fs.copyFile(lock,path.join(stage,'package-lock.json'));
70
+ const npm=process.platform==='win32'?'npm.cmd':'npm';
71
+ const install=spawnSync(npm,[await exists(lock)?'ci':'install','--omit=dev','--no-audit','--no-fund'],{cwd:stage,stdio:'inherit',shell:process.platform==='win32'});
72
+ if(install.error)throw install.error;if(install.status!==0)throw new Error('Staging dependency installation failed');
73
+ const builder=require(require.resolve('electron-builder',{paths:[root]}));
74
+ const config={...cfg,directories:{...cfg.directories,app:stage,output:path.resolve(root,cfg.directories?.output||'dist')},publish:null};
75
+ if(cfg.electronDist)config.electronDist=path.resolve(root,cfg.electronDist);
76
+ const command=pkg.scripts?.build||'';
77
+ const winScript=pkg.scripts?.['build:win']||'';
78
+ const effective=/npm run build:win/.test(command)?winScript:command;
79
+ const target=/--win\s+nsis\b/.test(effective)?builder.Platform.WINDOWS.createTarget('nsis'):/--win\s+portable\b/.test(effective)?builder.Platform.WINDOWS.createTarget('portable'):undefined;
80
+ const artifacts=await builder.build({projectDir:root,config,...(target?{targets:target}:{}),publish:'never'});
81
+ return {framework:d.kind,results,artifacts};
82
+ }finally{await fs.rm(stage,{recursive:true,force:true});}
83
+ }
84
+ async function autoBuild(root=process.cwd(),opts={}){
85
+ const d=await detect(root);
86
+ if(d.kind==='electron-builder')return electronBuild(root,d,opts);
87
+ if(['electron-forge','next','angular'].includes(d.kind))throw new Error(d.kind+' requires an explicit build configuration in this release');
88
+ if(!d.hasBuild)throw new Error('No build script; use protect or provide explicit configuration');
89
+ if(!d.output)throw new Error('Unknown build output; provide omni-obfuscator.config.json');
90
+ const output=path.join(root,d.output);
91
+ if(!opts.dryRun)npmScript(root,'build');
92
+ if((await fs.lstat(output)).isSymbolicLink())throw new Error('Build output must not be a symlink');
93
+ return {framework:d.kind,result:await protect(output,{...opts,inPlace:true})};
94
+ }
95
+ async function doctor(root=process.cwd()){
96
+ const d=await detect(root);
97
+ return {framework:d.kind,hasBuild:d.hasBuild,output:d.output,customConfig:await exists(path.join(root,CONFIG)),automatic:['electron-builder','vite','react','vue'].includes(d.kind)};
98
+ }
99
+ async function reset(root=process.cwd()){
100
+ const pkg=await read(path.join(root,'package.json'));let removed=[];
101
+ for(const [k,v] of Object.entries(commands))if(pkg.scripts?.[k]===v){delete pkg.scripts[k];removed.push(k);}
102
+ await fs.writeFile(path.join(root,'package.json'),JSON.stringify(pkg,null,2)+'\n');return {removed};
103
+ }
104
+ module.exports={detect,init,autoBuild,doctor,reset,exists};