pitboss-ng
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -0,1 +1,7 @@ | ||
# 0.3.2 | ||
- added direct support for memory management at win32/win64 platforms | ||
- using [appVeyor](https://ci.appveyor.com/project/Apiary/pitboss) to test at Windows | ||
- using [gulp](https://www.npmjs.org/package/gulp) for development and tasks, works cross-platform | ||
# 0.3.1 | ||
@@ -2,0 +8,0 @@ |
@@ -1,2 +0,1 @@ | ||
// Generated by CoffeeScript 1.9.2 | ||
var STATUS, clone, create, error, errorStatus, errorStatusMsg, isFatalError, message, run, script, timeout, util, vm; | ||
@@ -3,0 +2,0 @@ |
@@ -1,3 +0,2 @@ | ||
// Generated by CoffeeScript 1.9.2 | ||
var EventEmitter, Pitboss, Runner, fork, path, pusage, | ||
var EventEmitter, Pitboss, Runner, csv, exec, fork, os, path, pusage, ref, | ||
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | ||
@@ -9,3 +8,3 @@ extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | ||
fork = require('child_process').fork; | ||
ref = require('child_process'), fork = ref.fork, exec = ref.exec; | ||
@@ -16,2 +15,6 @@ EventEmitter = require('events').EventEmitter; | ||
os = require('os'); | ||
csv = require('csv'); | ||
exports.Pitboss = Pitboss = (function() { | ||
@@ -24,5 +27,5 @@ function Pitboss(code, options) { | ||
Pitboss.prototype.run = function(arg, callback) { | ||
Pitboss.prototype.run = function(arg1, callback) { | ||
var context, libraries; | ||
context = arg.context, libraries = arg.libraries; | ||
context = arg1.context, libraries = arg1.libraries; | ||
this.queue.push({ | ||
@@ -37,4 +40,4 @@ context: context, | ||
Pitboss.prototype.kill = function() { | ||
var ref; | ||
return (ref = this.runner) != null ? ref.kill(1) : void 0; | ||
var ref1; | ||
return (ref1 = this.runner) != null ? ref1.kill(1) : void 0; | ||
}; | ||
@@ -67,2 +70,3 @@ | ||
this.options = options1; | ||
this.winMemory = bind(this.winMemory, this); | ||
this.notifyCompleted = bind(this.notifyCompleted, this); | ||
@@ -107,5 +111,5 @@ this.memoryExceeded = bind(this.memoryExceeded, this); | ||
Runner.prototype.run = function(arg, callback) { | ||
Runner.prototype.run = function(arg1, callback) { | ||
var context, id, libraries, msg; | ||
context = arg.context, libraries = arg.libraries; | ||
context = arg1.context, libraries = arg1.libraries; | ||
if (this.running) { | ||
@@ -182,8 +186,27 @@ return false; | ||
Runner.prototype.memoryExceeded = function() { | ||
var pid, ref; | ||
if (!((ref = this.proc) != null ? ref.pid : void 0)) { | ||
var pid, ref1, ref2; | ||
if (!((ref1 = this.proc) != null ? ref1.pid : void 0)) { | ||
return; | ||
} | ||
pid = this.proc.pid; | ||
pusage.stat(this.proc.pid, (function(_this) { | ||
if ((ref2 = os.platform()) === 'win' || ref2 === 'win32' || ref2 === 'win64') { | ||
return this.winMemory(pid, (function(_this) { | ||
return function(err, stats) { | ||
var ref3; | ||
if (stats == null) { | ||
stats = []; | ||
} | ||
if (err) { | ||
if (_this.running) { | ||
console.error("Process memory usage command failed", err); | ||
} | ||
} | ||
if (!err && ((stats != null ? (ref3 = stats[0]) != null ? ref3.memUsage : void 0 : void 0) || 0) > _this.options.memoryLimit) { | ||
_this.currentError = "MemoryExceeded"; | ||
_this.kill(); | ||
} | ||
}; | ||
})(this)); | ||
} | ||
pusage.stat(pid, (function(_this) { | ||
return function(err, stat) { | ||
@@ -229,4 +252,63 @@ if (stat == null) { | ||
Runner.prototype.winMemory = function(pid, cb) { | ||
var procStat, taskList, taskListPath; | ||
taskListPath = 'tasklist.exe '; | ||
taskList = function(arg, taskListCallback) { | ||
exec(taskListPath + arg, function(err, stdout) { | ||
taskListCallback(err, stdout); | ||
}); | ||
}; | ||
procStat = function(procStatCallback) { | ||
var arg, stats, type; | ||
type = 'PID'; | ||
arg = "/fi \"PID eq " + pid + "\" /fo CSV"; | ||
stats = []; | ||
taskList(arg, function(err, stdout) { | ||
if (stdout == null) { | ||
stdout = ''; | ||
} | ||
if (err || !stdout) { | ||
return; | ||
} | ||
csv.parse(stdout, function(err, rows) { | ||
var i, len, memVal, row; | ||
if (err) { | ||
return procStatCallback(err, stats); | ||
} | ||
if ((rows != null ? rows.length : void 0) > 0) { | ||
for (i = 0, len = rows.length; i < len; i++) { | ||
row = rows[i]; | ||
if (!(parseInt(row[1], 10) === pid)) { | ||
continue; | ||
} | ||
if (row[4]) { | ||
memVal = ("" + (row[4] || '')).toLowerCase().replace(',', '.').trim(); | ||
if (memVal.indexOf('k')) { | ||
memVal = 1000 * parseInt(memVal.slice(0, -1)); | ||
} else if (memVal.indexOf('m')) { | ||
memVal = 1000 * 1000 * parseInt(memVal.slice(0, -1), 10); | ||
} else { | ||
memVal = 1000 * parseFloat(memVal.slice(0, -1)); | ||
} | ||
} else { | ||
memVal = parseFloat(row[4]); | ||
} | ||
stats.push({ | ||
name: row[0], | ||
pid: pid, | ||
memUsage: memVal | ||
}); | ||
} | ||
procStatCallback(err, stats); | ||
} else { | ||
procStatCallback(err, stats); | ||
} | ||
}); | ||
}); | ||
}; | ||
procStat(cb); | ||
}; | ||
return Runner; | ||
})(EventEmitter); |
{ | ||
"name": "pitboss-ng", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Run untrusted code in a seperate process using VM module. With timeout and memory limit management", | ||
@@ -20,7 +20,8 @@ "keywords": [ | ||
"scripts": { | ||
"prepublish": "./scripts/compile", | ||
"test": "./scripts/test" | ||
"test": "gulp test", | ||
"prepublish": "gulp build" | ||
}, | ||
"dependencies": { | ||
"clone": "^1.0.2", | ||
"csv": "^0.4.2", | ||
"pidusage": "^0.1.1" | ||
@@ -31,2 +32,6 @@ }, | ||
"coffee-script": "^1.9.2", | ||
"gulp": "^3.8.11", | ||
"gulp-coffee": "^2.3.1", | ||
"gulp-mocha": "^2.0.1", | ||
"gulp-util": "^3.0.4", | ||
"mocha": "^2.2.4" | ||
@@ -33,0 +38,0 @@ }, |
@@ -1,3 +0,3 @@ | ||
[![Build | ||
Status](https://secure.travis-ci.org/apiaryio/pitboss.png)](http://travis-ci.org/apiaryio/pitboss) | ||
[![Build Status](https://secure.travis-ci.org/apiaryio/pitboss.png)](http://travis-ci.org/apiaryio/pitboss) | ||
[![Build Status](https://ci.appveyor.com/api/projects/status/nctklpxwtt14vv4r?svg=true)](https://ci.appveyor.com/project/Apiary/pitboss) | ||
@@ -13,3 +13,3 @@ ![Pitboss](http://s3.amazonaws.com/img.mdp.im/renobankclubinside4.jpg_%28705%C3%97453%29-20120923-100859.jpg) | ||
var untrustedCode = "var a = !true; a"; | ||
var untrustedCode = "var a = !true;\n a"; | ||
@@ -23,11 +23,15 @@ var sandbox = new Pitboss(untrustedCode, { | ||
sandbox.run({ | ||
context: { | ||
'foo': 'bar', | ||
'key': 'value' // context must be JSON.stringify positive | ||
context: { // context is an object of variables/values accessible by the untrusted code | ||
'foo': 'bar', // context must be JSON.stringify positive | ||
'key': 'value' // = no RegExp, Date, circular references, Buffer or more crazy things | ||
}, | ||
libraries: { | ||
myModule: path.join(__dirname, './my/own/module') | ||
myModule: path.join(__dirname, './my/own/module'), | ||
// will be available as global "myModule" variable for the untrusted code | ||
'crypto': 'crypto', // you can also require system/installed packages | ||
'_': 'underscore' // require underscore the traditional way | ||
} | ||
}, function callback (err, result) { | ||
// result is synchronous "return" of the last line in your untrusted code, here "a = !true", so false | ||
console.log('Result is:', result); // prints "Result is: false" | ||
sandbox.kill(); // don't forget to kill the sandbox, if you don't need it anymore | ||
@@ -38,3 +42,3 @@ }); | ||
sandbox.run({ | ||
context: {}, | ||
context: {}, // no data-variables are passed to context | ||
libraries: ['console', 'lodash'] // we will be using global "lodash" & "console" | ||
@@ -41,0 +45,0 @@ }, function callback (err, result) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31285
14
442
130
3
7
+ Addedcsv@^0.4.2
+ Addedcsv@0.4.6(transitive)
+ Addedcsv-generate@0.0.6(transitive)
+ Addedcsv-parse@1.3.3(transitive)
+ Addedcsv-stringify@0.0.8(transitive)
+ Addedstream-transform@0.1.2(transitive)