Skip to content

Commit

Permalink
Run external plugins in sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Dec 9, 2014
1 parent 9ad2447 commit d3196a2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 52 deletions.
1 change: 0 additions & 1 deletion lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ Hexo.prototype.init = function(){
// Load external plugins & scripts
return Promise.all([
require('./load_plugins')(self),
require('./load_scripts')(self),
require('./update_package')(self)
]);
}).then(function(){
Expand Down
74 changes: 67 additions & 7 deletions lib/hexo/load_plugins.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,83 @@
var pathFn = require('path');
var fs = require('hexo-fs');
var tildify = require('tildify');
var Promise = require('bluebird');
var vm = require('vm');
var Module = require('module');

var pre = '(function(exports, require, module, __filename, __dirname, hexo){';
var post = '});';

require('colors');

module.exports = function(ctx){
if (!ctx.env.init || ctx.env.safe) return;

return Promise.all([
loadModules(ctx),
loadScripts(ctx)
]);
};

function runInContext(ctx, path){
return fs.readFile(path).then(function(script){
// Based on: https://github.com/joyent/node/blob/v0.10.33/src/node.js#L516
var module = new Module(path);
module.filename = path;
module.paths = Module._nodeModulePaths(path);

function require(path){
return module.require(path);
}

require.resolve = function(request){
return Module._resolveFilename(request, module);
};

require.main = process.mainModule;
require.extensions = Module._extensions;
require.cache = Module._cache;

var fn = vm.runInThisContext(pre + script + post, path);

return fn(module.exports, require, module, path, pathFn.dirname(path), ctx);
});
}

function loadModules(ctx){
var pluginDir = ctx.plugin_dir;
var currentName = '';

return fs.exists(pluginDir).then(function(exist){
return exist ? fs.readdir(pluginDir) : [];
}).filter(function(name){
return name.slice(0, 5) === 'hexo-';
}).map(function(name){
currentName = name;
require(pathFn.join(pluginDir, name));
ctx.log.debug('Plugin loaded: %s', name.magenta);
}).catch(function(err){
ctx.log.error({err: err}, 'Plugin load failed: %s', currentName.magenta);
var path = require.resolve(pathFn.join(pluginDir, name));

return runInContext(ctx, path).then(function(){
ctx.log.debug('Plugin loaded: %s', name.magenta);
}, function(err){
ctx.log.error({err: err}, 'Plugin load failed: %s', name.magenta);
});
});
}

function loadScripts(ctx){
return Promise.filter([
ctx.script_dir,
ctx.theme_script_dir
], function(scriptDir){
return scriptDir ? fs.exists(scriptDir) : false;
}).map(function(scriptDir){
var path = '';

return fs.listDir(scriptDir).map(function(name){
path = pathFn.join(scriptDir, name);
return runInContext(ctx, path);
}).then(function(){
ctx.log.debug('Script loaded: %s', tildify(path).magenta);
}, function(err){
ctx.log.error({err: err}, 'Script load failed: %s', tildify(path).magenta);
});
});
};
}
25 changes: 0 additions & 25 deletions lib/hexo/load_scripts.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/plugins/tag/include_code.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var fs = require('graceful-fs'),
var fs = require('hexo-fs'),
pathFn = require('path'),
util = require('../../util'),
file = util.file2,
Expand Down
34 changes: 16 additions & 18 deletions test/scripts/helpers/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@ var should = require('chai').should();
describe('number', function(){
var number = require('../../../lib/plugins/helper/number');

describe('number_format', function(){
it('default', function(){
number.number_format(1234.567).should.eql('1,234.567');
});
it('default', function(){
number.number_format(1234.567).should.eql('1,234.567');
});

it('precision', function(){
number.number_format(1234.567, {precision: false}).should.eql('1,234.567');
number.number_format(1234.567, {precision: 0}).should.eql('1,234');
number.number_format(1234.567, {precision: 1}).should.eql('1,234.6');
number.number_format(1234.567, {precision: 2}).should.eql('1,234.57');
number.number_format(1234.567, {precision: 3}).should.eql('1,234.567');
number.number_format(1234.567, {precision: 4}).should.eql('1,234.5670');
});
it('precision', function(){
number.number_format(1234.567, {precision: false}).should.eql('1,234.567');
number.number_format(1234.567, {precision: 0}).should.eql('1,234');
number.number_format(1234.567, {precision: 1}).should.eql('1,234.6');
number.number_format(1234.567, {precision: 2}).should.eql('1,234.57');
number.number_format(1234.567, {precision: 3}).should.eql('1,234.567');
number.number_format(1234.567, {precision: 4}).should.eql('1,234.5670');
});

it('delimiter', function(){
number.number_format(1234.567, {delimiter: ' '}).should.eql('1 234.567');
});
it('delimiter', function(){
number.number_format(1234.567, {delimiter: ' '}).should.eql('1 234.567');
});

it('separator', function(){
number.number_format(1234.567, {separator: '*'}).should.eql('1,234*567');
});
it('separator', function(){
number.number_format(1234.567, {separator: '*'}).should.eql('1,234*567');
});
});

0 comments on commit d3196a2

Please sign in to comment.