obfuscator
Advanced tools
Comparing version 0.1.0 to 0.2.0
# Obfuscator Documentation | ||
### `obfuscator.obfuscator(options, cb)` | ||
### `obfuscator(options, cb)` | ||
@@ -5,0 +5,0 @@ #### Parameters |
@@ -0,3 +1,4 @@ | ||
module.exports = process.env.OBF_COV | ||
? require('./lib-cov/obfuscator') | ||
: require('./lib/obfuscator'); |
@@ -0,1 +1,2 @@ | ||
'use strict'; | ||
@@ -5,3 +6,4 @@ | ||
path = require('path'), | ||
uglify = require('uglify-js'); | ||
uglify = require('uglify-js'), | ||
utils = require('./utils'); | ||
@@ -12,6 +14,2 @@ var rJSON = /\.json$/, | ||
// public api | ||
var obfuscator = module.exports = {}, | ||
utils = obfuscator.utils = require('./utils'); | ||
/** | ||
@@ -25,2 +23,3 @@ * Force a filepath to start with _./_ | ||
function dotslash(filepath) { | ||
filepath = filepath.replace(/\\/g, '/'); | ||
switch (filepath[0]) { | ||
@@ -35,3 +34,2 @@ case '.': | ||
obfuscator.version = require('../package').version; | ||
@@ -46,3 +44,3 @@ /** | ||
*/ | ||
obfuscator.obfuscator = function (options, cb) { | ||
var obfuscator = module.exports = function (options, cb) { | ||
if (!options.files || !options.root || !options.entry) { | ||
@@ -57,17 +55,29 @@ return cb(new TypeError('Invalid options')); | ||
utils.uglify(code, function (err, code) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
code = options.strings | ||
? utils.strings(code) | ||
: code; | ||
cb(null, code); | ||
}); | ||
utils.uglify(code, options, cb); | ||
}); | ||
}; | ||
// back-compat alias | ||
obfuscator.obfuscator = obfuscator; | ||
/** | ||
* Expose the current version | ||
* | ||
* @api private | ||
* @type {String} | ||
*/ | ||
obfuscator.version = require('../package').version; | ||
/** | ||
* Expose utils | ||
* | ||
* @api private | ||
* @type {Object} | ||
*/ | ||
obfuscator.utils = utils; | ||
/** | ||
* Create an `options` object for the `obfuscator` | ||
@@ -130,2 +140,3 @@ * | ||
/** | ||
@@ -148,3 +159,3 @@ * Register a `file` in location to `root` | ||
code = 'require.register("' + utils.hex(filename) + '",' | ||
code = 'require.register("' + filename + '",' | ||
+ 'function (module, exports, require) {' | ||
@@ -161,2 +172,3 @@ + (rJSON.test(file) | ||
/** | ||
@@ -178,3 +190,3 @@ * Concatenate a list of files for pre-obfuscation | ||
// export the exported stuff from entry | ||
built.push('this_module.exports = require("' + utils.hex(entry) + '");'); | ||
built.push('this_module.exports = require("' + entry + '");'); | ||
// end iffe | ||
@@ -181,0 +193,0 @@ built.push('}(require, module));'); |
137
lib/utils.js
@@ -0,9 +1,117 @@ | ||
'use strict'; | ||
var uglifyjs = require('uglify-js'); | ||
var uglifyjs = require('uglify-js'), | ||
merge = require('util')._extend; | ||
// public api | ||
var utils = module.exports = {}; | ||
/** | ||
* Create an `AST` from the given `js`, invoking `cb(err, ast)` | ||
* | ||
* @api private | ||
* @param {String} js | ||
* @param {Function} cb | ||
*/ | ||
exports.ast = function (js, cb) { | ||
try { | ||
var ast = uglifyjs.parse(js); | ||
cb(null, ast); | ||
} catch (err) { | ||
var e = new Error(err.message); | ||
// cheap hack to use actual errors | ||
// rather than the indecipherable JS_Parse_Error garbage | ||
merge(e, err); | ||
// expose the bad js | ||
e.source = js; | ||
cb(e); | ||
} | ||
}; | ||
/** | ||
* Compress the given `ast`, conditionally using `opts` | ||
* | ||
* @api private | ||
* @param {Object} [opts] | ||
* @return {AST} | ||
*/ | ||
exports.compress = function (ast, opts) { | ||
opts = opts || exports.compress.defaults; | ||
var compressor = uglifyjs.Compressor(opts); | ||
// for some stupid reason, this is the | ||
// only non-modifier method... | ||
return ast.transform(compressor); | ||
}; | ||
/** | ||
* Default compression options | ||
* | ||
* @api private | ||
* @type {Object} | ||
*/ | ||
exports.compress.defaults = { | ||
sequences: true, | ||
properties: true, | ||
dead_code: true, | ||
drop_debugger: true, | ||
unsafe: true, | ||
conditionals: true, | ||
comparisons: true, | ||
evaluate: true, | ||
booleans: true, | ||
loops: true, | ||
unused: true, | ||
hoist_funs: true, | ||
hoist_vars: true, | ||
if_return: true, | ||
join_vars: true, | ||
cascade: true, | ||
warnings: false | ||
}; | ||
/** | ||
* Uglify the given `js` with `opts` | ||
* | ||
* @api private | ||
* @param {String} js | ||
* @param {Object} [opts] | ||
* @param {Function} cb | ||
*/ | ||
exports.uglify = function (js, opts, cb) { | ||
/** | ||
* Handle mangling and compression of the generated `AST` | ||
* | ||
* @api private | ||
* @param {Error} err | ||
* @param {AST} ast | ||
*/ | ||
function handleAST(err, ast) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
ast.figure_out_scope(); | ||
ast.mangle_names(); | ||
ast = exports.compress(ast, opts.compressor); | ||
var str = ast.print_to_string(); | ||
return cb(null, opts.strings ? exports.strings(str) : str); | ||
} | ||
if (typeof opts === 'function') { | ||
cb = opts; | ||
opts = {}; | ||
} | ||
// build the AST | ||
exports.ast(js, handleAST); | ||
}; | ||
/** | ||
* Convert (or _obfuscate_) a string to its escaped | ||
@@ -18,3 +126,3 @@ * hexidecimal representation. For example, | ||
*/ | ||
utils.hex = function (str) { | ||
exports.hex = function (str) { | ||
var index, | ||
@@ -30,2 +138,3 @@ length = str.length, | ||
/** | ||
@@ -56,3 +165,3 @@ * Mangle simple strings contained in some `js` | ||
*/ | ||
utils.strings = function (js) { | ||
exports.strings = function (js) { | ||
var expression = /("[a-z\d\/\.\_\-]+")/gi; | ||
@@ -64,3 +173,3 @@ | ||
// escape to the string to hex | ||
result = utils.hex(result); | ||
result = exports.hex(result); | ||
// add quotes back | ||
@@ -74,17 +183,1 @@ result = '"' + result + '"'; | ||
}; | ||
/** | ||
* Uglify some `js` without throwing an exception | ||
* | ||
* @api private | ||
* @param {String} js | ||
* @param {Function} cb | ||
*/ | ||
utils.uglify = function (js, cb) { | ||
try { | ||
var code = uglifyjs.minify(js, { fromString: true }).code; | ||
cb(null, code); | ||
} catch (err) { | ||
cb(err); | ||
} | ||
}; |
{ | ||
"name": "obfuscator", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"scripts": { | ||
@@ -19,4 +19,4 @@ "test": "make validate" | ||
"dependencies": { | ||
"uglify-js": "~2.3.4", | ||
"commander": "~1.1.1" | ||
"uglify-js": "~2.3.6", | ||
"commander": "~2.0.0" | ||
}, | ||
@@ -27,3 +27,5 @@ "devDependencies": { | ||
"should": "~1.2.2", | ||
"supertest": "~0.6.0" | ||
"supertest": "~0.6.0", | ||
"better-assert": "~1.0.0", | ||
"glob": "~3.2.6" | ||
}, | ||
@@ -30,0 +32,0 @@ "bin": "bin/obfuscator", |
@@ -0,3 +1,6 @@ | ||
# Obfuscator | ||
[![Build Status](https://travis-ci.org/stephenmathieson/node-obfuscator.png?branch=master)](https://travis-ci.org/stephenmathieson/node-obfuscator) [![Dependency Status](https://gemnasium.com/stephenmathieson/node-obfuscator.png)](https://gemnasium.com/stephenmathieson/node-obfuscator) | ||
Obfuscate your node packages because your boss says so! | ||
@@ -70,2 +73,14 @@ | ||
var options = new Options([ '/path/to/file1.js', '/path/to/file2.js' ], '/path/to', 'file1.js', true); | ||
// custom compression options | ||
// see https://github.com/mishoo/UglifyJS2/#compressor-options | ||
options.compressor = { | ||
conditionals: true, | ||
evaluate: true, | ||
booleans: true, | ||
loops: true, | ||
unused: false, | ||
hoist_funs: false | ||
}; | ||
obfuscator(options, function (err, obfuscated) { | ||
@@ -98,7 +113,6 @@ if (err) { | ||
- you're not able to do silly things with `module.` | ||
- you don't have access to the global namespace | ||
## Contributing | ||
Do it, but add tests for your changes. Tests should be written with [Vows]. Use JSLint whitespace rules. | ||
Do it, but add tests for your changes. Tests should be written with [Mocha]. | ||
@@ -108,2 +122,9 @@ | ||
### 0.2.0 | ||
- fixed windows pathing bug ([#8](https://github.com/stephenmathieson/node-obfuscator/pull/8)) | ||
- `obfuscator` is now a function ([#1](https://github.com/stephenmathieson/node-obfuscator/issues/1)) | ||
- added support for custom compression options ([#2](https://github.com/stephenmathieson/node-obfuscator/issues/2)) | ||
- updated [UglifyJS] | ||
### 0.1.0 | ||
@@ -149,5 +170,5 @@ | ||
[UglifyJS]: https://github.com/mishoo/UglifyJS2 | ||
[Vows]: https://github.com/cloudhead/vows | ||
[Mocha]: https://github.com/visionmedia/mocha | ||
[acceptance tests]: https://github.com/stephenmathieson/node-obfuscator/tree/master/test/acceptance | ||
[grunt]: https://github.com/gruntjs/grunt | ||
[docs]: https://github.com/stephenmathieson/node-obfuscator/tree/master/docs.md |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 3 instances in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses eval() which is a dangerous function. This prevents the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 9 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
2
171
10
1
20185
6
11
1
452
+ Addedcommander@2.0.0(transitive)
- Removedcommander@1.1.1(transitive)
- Removedkeypress@0.1.0(transitive)
Updatedcommander@~2.0.0
Updateduglify-js@~2.3.6