obfuscator
Advanced tools
Comparing version 0.4.1 to 0.4.2
0.4.2 / 2014-04-07 | ||
================== | ||
* pkg: npm init | ||
* pkg: Remove 'preferGlobal' | ||
* Use Uglify's TreeTransformer to mangle strings rather than regular expressions. | ||
0.4.1 / 2014-03-31 | ||
@@ -3,0 +10,0 @@ ================== |
@@ -12,22 +12,5 @@ | ||
module.exports = obfuscator; | ||
/** | ||
* Force a filepath to start with _./_ | ||
* | ||
* @api private | ||
* @param {String} filepath | ||
* @return {String} | ||
*/ | ||
function dotslash(filepath) { | ||
filepath = filepath.replace(/\\/g, '/'); | ||
switch (filepath[0]) { | ||
case '.': | ||
case '/': | ||
return filepath; | ||
default: | ||
return './' + filepath; | ||
} | ||
} | ||
/** | ||
@@ -41,3 +24,4 @@ * Obfuscate and concatenate a NodeJS _"package"_ | ||
*/ | ||
var obfuscator = module.exports = function (options, cb) { | ||
function obfuscator(options, cb) { | ||
if (!options.files || !options.root || !options.entry) { | ||
@@ -54,3 +38,3 @@ return cb(new TypeError('Invalid options')); | ||
}); | ||
}; | ||
} | ||
@@ -60,3 +44,2 @@ // back-compat alias | ||
/** | ||
@@ -68,5 +51,5 @@ * Expose the current version | ||
*/ | ||
obfuscator.version = require('../package').version; | ||
/** | ||
@@ -78,5 +61,5 @@ * Expose utils | ||
*/ | ||
obfuscator.utils = utils; | ||
/** | ||
@@ -114,2 +97,3 @@ * Create an `options` object for the `obfuscator` | ||
*/ | ||
obfuscator.options = function (files, root, entry, strings) { | ||
@@ -141,3 +125,2 @@ // TODO support globbling | ||
/** | ||
@@ -151,2 +134,3 @@ * Register a `file` in location to `root` | ||
*/ | ||
obfuscator.register = function (root, file, cb) { | ||
@@ -173,3 +157,2 @@ var filename = dotslash(path.relative(root, file)); | ||
/** | ||
@@ -182,2 +165,3 @@ * Concatenate a list of files for pre-obfuscation | ||
*/ | ||
obfuscator.concatenate = function (options, cb) { | ||
@@ -223,1 +207,20 @@ var entry = dotslash(path.relative(options.root, options.entry)); | ||
}; | ||
/** | ||
* Force a filepath to start with _./_ | ||
* | ||
* @api private | ||
* @param {String} filepath | ||
* @return {String} | ||
*/ | ||
function dotslash(filepath) { | ||
filepath = filepath.replace(/\\/g, '/'); | ||
switch (filepath[0]) { | ||
case '.': | ||
case '/': | ||
return filepath; | ||
default: | ||
return './' + filepath; | ||
} | ||
} |
@@ -7,3 +7,2 @@ | ||
/** | ||
@@ -16,2 +15,3 @@ * Create an `AST` from the given `js`, invoking `cb(err, ast)` | ||
*/ | ||
exports.ast = function (js, cb) { | ||
@@ -32,3 +32,2 @@ try { | ||
/** | ||
@@ -41,7 +40,6 @@ * Compress the given `ast`, conditionally using `opts` | ||
*/ | ||
exports.compress = function (ast, opts) { | ||
opts = opts || exports.compress.defaults; | ||
var compressor = uglifyjs.Compressor(opts); | ||
// for some stupid reason, this is the | ||
@@ -52,3 +50,2 @@ // only non-modifier method... | ||
/** | ||
@@ -60,2 +57,3 @@ * Default compression options | ||
*/ | ||
exports.compress.defaults = { | ||
@@ -81,3 +79,2 @@ sequences: true, | ||
/** | ||
@@ -91,3 +88,5 @@ * Uglify the given `js` with `opts` | ||
*/ | ||
exports.uglify = function (js, opts, cb) { | ||
/** | ||
@@ -100,7 +99,8 @@ * Handle mangling and compression of the generated `AST` | ||
*/ | ||
function handleAST(err, ast) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (err) return cb(err); | ||
var stream = new uglifyjs.OutputStream; | ||
ast.figure_out_scope(); | ||
@@ -110,5 +110,13 @@ ast.mangle_names(); | ||
var str = ast.print_to_string(); | ||
if (opts.strings) { | ||
ast = mangleStrings(ast); | ||
// disable uglify's string escaping to prevent | ||
// double escaping our hex | ||
stream.print_string = function (str) { | ||
return this.print('"' + str + '"'); | ||
}; | ||
} | ||
return cb(null, opts.strings ? exports.strings(str) : str); | ||
ast.print(stream); | ||
return cb(null, stream.toString()); | ||
} | ||
@@ -125,2 +133,3 @@ | ||
/** | ||
@@ -138,3 +147,2 @@ * Escape map. | ||
/** | ||
@@ -150,2 +158,3 @@ * Convert (or _obfuscate_) a string to its escaped | ||
*/ | ||
exports.hex = function (str) { | ||
@@ -170,36 +179,32 @@ var result = ''; | ||
/** | ||
* Mangle simple strings contained in some `js` | ||
* Mangle strings contained in the given `ast`. | ||
* | ||
* Strings will be _mangled_ by replacing each | ||
* contained character with its escaped hexidecimal | ||
* representation. For example, "a" will render | ||
* to "\x63". | ||
* @api private | ||
* @param {AST} ast | ||
* @return {AST} mangled ast | ||
*/ | ||
function mangleStrings(ast) { | ||
var transformer = new uglifyjs.TreeTransformer(null, mangleString); | ||
return ast.transform(transformer); | ||
} | ||
/** | ||
* Mangle the given `node`, assuming it's an `AST_String`. | ||
* | ||
* Example: | ||
* | ||
* ```js | ||
* utils.strings('var foo = "foo"';); | ||
* //=> 'var foo = "\\x66\\x6f\\x6f";' | ||
* ``` | ||
* | ||
* @api public | ||
* @name obfuscator.utils.strings | ||
* @param {String} js | ||
* @return {String} | ||
* @api private | ||
* @param {AST_Node} node | ||
* @return {AST_Node} | ||
*/ | ||
exports.strings = function (js) { | ||
var expression = /("(?:(?!")[^\\\n]|\\.)*"|'(?:(?!')[^\\\n]|\\.)*')/g; | ||
function replacer(match, str) { | ||
// remove leading and trailing quote marks | ||
var result = str.substr(1, str.length - 2); | ||
// escape to the string to hex | ||
result = exports.hex(result); | ||
// add quotes back | ||
result = '"' + result + '"'; | ||
// done :) | ||
return result; | ||
function mangleString(node) { | ||
if (!(node instanceof uglifyjs.AST_String)) { | ||
return; | ||
} | ||
return js.replace(expression, replacer); | ||
}; | ||
var str = node.getValue(); | ||
var hex = exports.hex(str); | ||
var obj = merge({}, node); | ||
obj.value = hex; | ||
return new uglifyjs.AST_String(obj); | ||
} |
{ | ||
"name": "obfuscator", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"scripts": { | ||
@@ -30,5 +30,15 @@ "test": "make all" | ||
}, | ||
"bin": "bin/obfuscator", | ||
"bin": { | ||
"obfuscator": "bin/obfuscator" | ||
}, | ||
"main": "index.js", | ||
"preferGlobal": true | ||
"bugs": { | ||
"url": "https://github.com/stephenmathieson/node-obfuscator/issues" | ||
}, | ||
"homepage": "https://github.com/stephenmathieson/node-obfuscator", | ||
"directories": { | ||
"example": "examples", | ||
"test": "test" | ||
}, | ||
"license": "MIT" | ||
} |
No License Found
License(Experimental) License information could not be found
Found 1 instance in 1 package
19825
0
417