Skip to content

Commit

Permalink
Updated to use the YUI compressor instead of JSMin
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Dec 19, 2010
1 parent 76fce81 commit f56172a
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 199 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
`packnode` runs node modules through JSMin and encrypts them for private use.
`packnode` minifies, obfuscates or encrypts node modules for private use.

To install packnode, use [npm](http://github.com/isaacs/npm)

$ npm install pack

## Packing a module

To pack `myscript.js` using the password `pass123`, run
To compress and obfuscate `myscript.js` using the YUI compressor, run

$ cat myscript.js | packnode > packed.js

To encrypt `myscript.js` using the password `pass123`, run

$ cat myscript.js | packnode pass123 > packed.js

Encrypted modules can be accessed by calling require(packed).unpack(password);
Encrypted modules can be accessed by calling require(packed).unpack(password);

## Example

Expand Down Expand Up @@ -41,10 +45,6 @@ CoffeeScript modules can be packed using

$ cat myscript.coffee | coffee -c -s | packnode pass123 > packed.js

To obfuscate code, use the included YUI Compressor v2.4.2 (requires Java)

$ java -jar yui.jar myscript.js | packnode pass123 > packed.js

To specify a custom encryption algorithm or output encoding, use `-a` and `-e`

$ packnode -a aes256 -e hex < myscript.js > packed.js
Expand Down
3 changes: 3 additions & 0 deletions bin/packnode
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('packnode').pack();
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports = module.exports = require('./lib');
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports = module.exports = require('./packnode');
136 changes: 136 additions & 0 deletions lib/packnode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
var fs = require('fs'),
crypto = require('crypto'),
child = require('child_process'),
args = process.argv.slice(2);

var stdin = process.openStdin(), input = '';

var cipher, data = '', encrypted, out, chunk = [],
packed, decipher,
encoding = 'base64',
algorithm = 'aes256',
chunk_length = 100,
password, unpack, minify;

var exit = function (msg, is_error) {
console.log(msg);
process.exit(1);
};

var usage = ''
+ '\x1b[1mUsage\x1b[0m: packnode [OPTIONS] <PASSWORD>\n'
+ '\n'
+ '\x1b[1mExample 1\x1b[0m: cat input.js | packnode mypassword > output.js\n'
+ '\x1b[1mExample 2\x1b[0m: cat input.coffee | coffee -c -s | packnode mypassword > output.js\n'
+ '\n'
+ '\x1b[1mOptions\x1b[0m:\n'
+ ' -m, --minify Just run input through the YUI Compressor\n'
+ ' -a, --algorithm <..> Use the specified algorithm. Default is "aes256"\n'
+ ' -e, --encoding <..> Encode with base64|hex|binary. Default is "hex"\n'
+ ' -u, --unpack Unpack the module using the specified password\n'
+ ' -h, --help Display help information\n'
;

var min = function (data, callback) {
var tmp_file = Math.floor(Math.random() * 1000000) + '.js';

fs.writeFileSync(__dirname + '/' + tmp_file, data, 'utf8');

var remove_tmp = function () {
try {
fs.unlinkSync(__dirname + '/' + tmp_file);
} catch (e) {}
};

['SIGINT', 'SIGTERM', 'SIGKILL', 'SIGQUIT', 'SIGHUP', 'exit'].forEach(function (signal) {
process.on(signal, remove_tmp);
});

child.exec('java -jar yuicompressor-2.4.2.jar "' + tmp_file + '"', {cwd: __dirname}, function (err, stdout, stderr) {
remove_tmp();
if (err || stderr) throw err || stderr;
callback(stdout);
});
};

exports.pack = function () {

if (!args.length) {
exit(usage);
}

while (args.length) {
arg = args.shift();
switch (arg) {
case '-a':
case '--algorithm':
algorithm = args.shift();
break;
case '-e':
case '--encoding':
encoding = args.shift();
break;
case '-u':
case '--unpack':
unpack = true;
break;
case '-m':
case '--minify':
minify = true;
break;
case '-h':
case '--help':
exit(usage);
break;
default:
password = arg;
break;
}
}

// Read from stdin
stdin.setEncoding('utf8');
stdin.on('data', function (input) { data += input; });
stdin.on('end', function () {

if (!unpack) {

// Minify the file
min(data, function (data) {

// Are we just minifying the file?
if (minify) {
process.stdout.write(data);
return;
}

// Encrypt the file
cipher = crypto.createCipher(algorithm, password);
encrypted = cipher.update(data, 'binary', encoding) + cipher.final(encoding);
for (var i = 0, l = encrypted.length; i < l; i += chunk_length) {
chunk.push(encrypted.substr(i, chunk_length));
}

// Prepare the output
out = 'e="' + chunk.join('"\n+"') + '";\n';
out += 'exports.unpack=function(p){var d=require("crypto").createDecipher("' + algorithm + '",p);\n';
out += 'eval(d.update(e,"' + encoding + '","utf8")+d.final("utf8"));return exports;}';

// ..and write to stdout
process.stdout.write(out);

});

} else {

// Read and decrypt the file
packed = (function () { eval(data); return e; }());
decipher = crypto.createDecipher(algorithm, password);
out = decipher.update(packed, encoding, 'utf8') + decipher.final('utf8');

process.stdout.write(out);

}

});
}
File renamed without changes.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{ "name" : "pack",
"description" : "Create private node modules",
"version" : "0.1.5",
"description" : "Minify, obfuscate and encrypt node modules",
"version" : "0.2.0",
"homepage" : "http://github.com/chriso/packnode",
"keywords" : ["pack","packnode","encrypt","compress","unpack"],
"keywords" : ["pack","packnode","encrypt","compress","unpack","yui","obfuscate","compressor","closure","jsmin"],
"author" : "Chris O'Hara <cohara87@gmail.com>",
"main" : "./lib",
"directories" : { "lib" : "./lib" },
"bugs": {
"mail": "cohara87@gmail.com",
"web": "http://github.com/chriso/packnode/issues"
Expand All @@ -14,7 +16,7 @@
},
"engines": { "node": ">=0.2.5" },
"bin": {
"packnode": "./packnode",
"packnode": "./bin/packnode",
},
"licenses": [{
"type": "MIT",
Expand Down
188 changes: 0 additions & 188 deletions packnode

This file was deleted.

0 comments on commit f56172a

Please sign in to comment.