Skip to content

Commit

Permalink
Merge branch 'master' of github.com:observing/square into development
Browse files Browse the repository at this point in the history
Conflicts:
	lib/square.js
  • Loading branch information
3rd-Eden committed May 17, 2012
2 parents 5a21ab4 + c78d1d0 commit fcb7949
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
9 changes: 7 additions & 2 deletions bin/square
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ program
.option('-i, --ignore [files]', 'ignore these files', filters.list) .option('-i, --ignore [files]', 'ignore these files', filters.list)
.option('-f, --filename <file>', 'alternate filenames', filters.list) .option('-f, --filename <file>', 'alternate filenames', filters.list)
.option('-p, --plugin <plugins>', 'which plugins should be included', filters.list) .option('-p, --plugin <plugins>', 'which plugins should be included', filters.list)
.option('-g, --group <groups>', 'which groups should be processed', filters.list)
.option('-l, --list', 'show a list of plugins'); .option('-l, --list', 'show a list of plugins');


/** /**
Expand Down Expand Up @@ -148,7 +149,7 @@ program.on('--watch', function watching (live) {
square.logger.notice('changes detected, refreshing %s', files.join(', ')); square.logger.notice('changes detected, refreshing %s', files.join(', '));


square.refresh(files); square.refresh(files);
square.build(program.extension, function finished (changes) { square.build(program.extension, program.group, function finished (changes) {
// check if we are listening for live changes, if so, emit the changes // check if we are listening for live changes, if so, emit the changes
if (!socket) return; if (!socket) return;


Expand Down Expand Up @@ -265,6 +266,10 @@ program.ignore = Array.isArray(program.ignore)
? program.ignore ? program.ignore
: []; : [];


program.group = Array.isArray(program.group)
? program.group
: [];

program.bundle = program.bundle || process.env.PWD; program.bundle = program.bundle || process.env.PWD;


program.plugin = Array.isArray(program.plugin) program.plugin = Array.isArray(program.plugin)
Expand Down Expand Up @@ -342,5 +347,5 @@ found.on('end', function end () {
square.plugin(plugin); square.plugin(plugin);
}); });


square.build(program.extension); square.build(program.extension, program.group);
}); });
11 changes: 7 additions & 4 deletions lib/square.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -458,12 +458,14 @@ Square.prototype.directive = function directive (data, extension, seen) {
* Build the stuff. * Build the stuff.
* *
* @param {String} extension * @param {String} extension
* @param {Array} groupsout
* @param {Function} fn * @param {Function} fn
* @api public * @api public
*/ */


Square.prototype.build = function build (extension, fn) { Square.prototype.build = function build (extension, groupsout, fn) {
var self = this; var self = this
, files = [];


// default arguments // default arguments
extension = extension || 'js'; extension = extension || 'js';
Expand All @@ -480,7 +482,6 @@ Square.prototype.build = function build (extension, fn) {
this.on('merge', function merged (collection) { this.on('merge', function merged (collection) {
var layers = self.middleware.slice(0) var layers = self.middleware.slice(0)
, errors = [] , errors = []
, files = []
, groupCount = collection.groupCount || 1 , groupCount = collection.groupCount || 1
, backup; , backup;


Expand Down Expand Up @@ -545,7 +546,8 @@ Square.prototype.build = function build (extension, fn) {
if (!this.stdout) this.write(collection, 'dev', doneGroup); if (!this.stdout) this.write(collection, 'dev', doneGroup);
}); });


return this.emit('build'); this.emit('build');
return this.merge(extension, groupsout);
}; };


/** /**
Expand Down Expand Up @@ -595,6 +597,7 @@ Square.prototype.tag = function tag (collection, type) {
return _.extend({ return _.extend({
type: type || 'min' type: type || 'min'
, md5: createHash('md5').update(collection.content || '').digest('hex') , md5: createHash('md5').update(collection.content || '').digest('hex')
, group: collection.group
, branch: branch , branch: branch
, sha: sha , sha: sha
, ext: collection.extension || 'js' , ext: collection.extension || 'js'
Expand Down
24 changes: 20 additions & 4 deletions plugins/update.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ module.exports = function setup (options) {
// fetch that update // fetch that update
provider(bundle.latest, configuration, function test (err, version, content) { provider(bundle.latest, configuration, function test (err, version, content) {
if (err) return cb(err); if (err) return cb(err);
if (!version) return cb(new Error('unable to find and parse the version')); if (!version) return cb(new Error('unable to find and parse the version for ' + key));
if (version === bundle.version) return cb(); if (version === bundle.version) return cb();


self.logger.notice('%s is out of date, latest version is %s', key, version.green); self.logger.notice('%s is out of date, latest version is %s', key, version.green);
Expand Down Expand Up @@ -163,7 +163,15 @@ module.exports = function setup (options) {


exports.download(data, done); exports.download(data, done);
}); });
}, next); }, function finished (err, data) {
if (err && err.forEach) {
err.forEach(function failed (err) {
self.logger.error(err);
});
}

next();
});
}; };
}; };


Expand Down Expand Up @@ -248,7 +256,13 @@ exports.selector = function fetch (uri, options, fn) {
exports.version = function search (content, options) { exports.version = function search (content, options) {
var version; var version;


return [options.strict, options.loose].some(function some (regexp) { // a "feature" of calling exec on a regexp with a global flag is that it
// renders it useless for new calls as it will do checks based on the new
// matches. We can bypass this behavior by recompiling regexps
[
new RegExp(options.strict.source)
, new RegExp(options.loose.source)
].some(function some (regexp) {
var match = regexp.exec(content); var match = regexp.exec(content);


if (match && match.length) { if (match && match.length) {
Expand All @@ -260,7 +274,9 @@ exports.version = function search (content, options) {
} }


return !!version; return !!version;
}) ? version : null; });

return version;
}; };


/** /**
Expand Down
28 changes: 20 additions & 8 deletions plugins/wrap.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ var _ = require('underscore')._
, canihas = require('../lib/canihas'); , canihas = require('../lib/canihas');


/** /**
* Attempts to detect leaking globals and provides a wrapper for it. * Can wrap you compiled code and plurge the leaked globals.
* *
* Options: * Options:
* *
* - `timeout` time to wait for the script to be initialized, number in ms. * - `timeout` time to wait for the script to be initialized, number in ms.
* - `header` first section of the leak prevention, string. * - `header` first section of the leak prevention, string.
* - `body` content for the leak prevention function body, array. * - `body` content for the leak prevention function body, array.
* - `footer` closing section of th leak prevention, string. * - `footer` closing section of th leak prevention, string.
* - `leaks` should we detect globals and patch it, boolean
* *
* @param {Object} options * @param {Object} options
* @returns {Function} middleware * @returns {Function} middleware
Expand All @@ -22,6 +23,7 @@ module.exports = function setup (options) {
var settings = { var settings = {
timeout: 1000 timeout: 1000
, header: '(function (expose) {' , header: '(function (expose) {'
, leaks: true
, body: [ , body: [
'this.contentWindow = this.self = this.window = this;' 'this.contentWindow = this.self = this.window = this;'
, 'var window = this' , 'var window = this'
Expand Down Expand Up @@ -58,6 +60,11 @@ module.exports = function setup (options) {
var logger = this.logger var logger = this.logger
, timeout = configuration.timeout; , timeout = configuration.timeout;


if (!configuration.leaks) {
output.content = configuration.header + output.content + configuration.footer;
return next(null, output);
}

// search for leaks // search for leaks
exports.sandboxleak(output.content, timeout, function found (err, leaks) { exports.sandboxleak(output.content, timeout, function found (err, leaks) {
if (err) { if (err) {
Expand Down Expand Up @@ -125,7 +132,7 @@ module.exports = function setup (options) {
* @api private * @api private
*/ */


module.exports.description = 'Tries to detect global leaks in your code and attempts to patch it.'; module.exports.description = 'Can wrap you compiled code and plurge the leaked globals';


/** /**
* Detect leaking code. * Detect leaking code.
Expand All @@ -138,6 +145,8 @@ module.exports.description = 'Tries to detect global leaks in your code and atte


exports.sandboxleak = function sandboxleak (content, timeout, fn) { exports.sandboxleak = function sandboxleak (content, timeout, fn) {
canihas.jsdom(function canihasJSDOM (err, jsdom) { canihas.jsdom(function canihasJSDOM (err, jsdom) {
if (err) return err;

var html = '<html><body></body></html>' var html = '<html><body></body></html>'
, DOM = jsdom.jsdom; , DOM = jsdom.jsdom;


Expand All @@ -151,15 +160,18 @@ exports.sandboxleak = function sandboxleak (content, timeout, fn) {
jsdom.env({ jsdom.env({
html: html html: html
, src: [ content ] , src: [ content ]
, features: {
FetchExternalResources: false
}
, done: function done (err, window) { , done: function done (err, window) {
if (err) return fn(err); var globals;


var infected = Object.keys(window) if (window) {
, globals = _.difference(infected, regular); globals = _.difference(Object.keys(window), regular);

window.close();
window.close(); }


fn(null, globals); fn(err, globals);
} }
}); });
}); });
Expand Down

0 comments on commit fcb7949

Please sign in to comment.