Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

phantom-workers

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

phantom-workers - npm Package Compare versions

Comparing version
0.1.3
to
0.2.0
+13
test/test-script/just-port.js
var port = require("system").stdin.readLine();
require('webserver').create().listen('127.0.0.1:' + port, function (req, res) {
try {
res.statusCode = 200;
res.write(req.post);
res.close();
} catch (e) {
res.statusCode = 500;
res.write(JSON.stringify(e));
res.close();
}
});
+3
-3

@@ -5,4 +5,4 @@ var net = require('net')

module.exports = function (port, cb) {
var host = '127.0.0.1';
module.exports = function (port, host, cb) {
var host = host;
var timeout = 50;

@@ -50,2 +50,2 @@ var connectionRefused = false;

socket.connect(port, host)
};
};

@@ -12,2 +12,3 @@ /*!

numCPUs = require('os').cpus().length,
cluster = require("cluster"),
PhantomWorker = require("./phantomWorker.js");

@@ -20,2 +21,4 @@

this.options.timeout = this.options.timeout || 180000;
this.options.host = this.options.host || "127.0.0.1";
this.tasksQueue = [];

@@ -36,3 +39,6 @@ };

self._phantomInstances.push(new PhantomWorker({
pathToPhantomScript: self.options.pathToPhantomScript
pathToPhantomScript: self.options.pathToPhantomScript,
host: self.options.host,
portLeftBoundary: self.options.portLeftBoundary,
portRightBoundary: self.options.portRightBoundary
}));

@@ -47,3 +53,3 @@ self._phantomInstances[i].start(function(err) {

});
};
}
};

@@ -55,3 +61,3 @@

});
}
};

@@ -58,0 +64,0 @@ PhantomManager.prototype.execute = function (options, cb) {

var childProcess = require('child_process'),
phantomjs = require("phantomjs"),
cluster = require("cluster"),
http = require('http'),
netCluster = require("net-cluster"),
checkPortStatus = require("./checkPortStatus.js");
checkPortStatus = require("./checkPortStatus.js"),
portScanner = require('portscanner');
var findFreePort = function (cb) {
var findFreePort = function (host, cb) {
var server = netCluster.createServer();

@@ -17,9 +20,32 @@ var port = 0;

});
server.listen(0);
server.listen(0, host);
};
var findFreePortInRange = function (host, portLeftBoundary, portRightBoundary, cb) {
//in cluster we don't want ports to collide, so we make a special space for every worker assuming max number of cluster workers is 5
if (cluster.worker) {
portLeftBoundary = portLeftBoundary + (((portRightBoundary - portLeftBoundary) / 5) * (cluster.worker.id - 1));
}
portScanner.findAPortNotInUse(portLeftBoundary, portRightBoundary, host, function(error, port) {
cb(error, port);
})
};
var PhantomWorker = module.exports = function (options) {
this.options = options;
this.isBusy = false;
if (options.portLeftBoundary && options.portRightBoundary) {
this.findFreePort = function (cb) {
findFreePortInRange(options.host, options.portLeftBoundary, options.portRightBoundary, cb);
};
}
else {
this.findFreePort = function (cb) {
findFreePort(options.host, cb);
};
}
};

@@ -29,3 +55,7 @@

var self = this;
findFreePort(function (err, port) {
this.findFreePort(function (err, port) {
if (err)
return cb(err);
self.port = port;

@@ -52,3 +82,6 @@

self._childProcess.stderr.pipe(process.stderr);
//we send port and host in two lines to make the package back compatible
self._childProcess.stdin.write(port + "\n");
self._childProcess.stdin.write(self.options.host + "\n");
});

@@ -60,3 +93,3 @@ };

shot = shot || 1;
checkPortStatus(this.port, function (err, status) {
checkPortStatus(this.port, this.options.host, function (err, status) {
if (!err && status === 'open') {

@@ -91,5 +124,4 @@ return cb();

this.isBusy = true;
var http_opts = {
hostname: '127.0.0.1',
hostname: this.options.host,
port: this.port,

@@ -96,0 +128,0 @@ path: '/',

{
"name": "phantom-workers",
"version": "0.1.3",
"version": "0.2.0",
"author": {

@@ -22,2 +22,3 @@ "name": "Jan Blaha",

"phantomjs": "1.9.15",
"portscanner": "1.0.0",
"underscore": "1.8.2"

@@ -24,0 +25,0 @@ },

#phantom-workers
[![NPM Version](http://img.shields.io/npm/v/phantom-workers.svg?style=flat-square)](https://npmjs.com/package/phantom-workers)
[![License](http://img.shields.io/npm/l/phantom-workers?style=flat-square)](http://opensource.org/licenses/MIT)
[![License](http://img.shields.io/npm/l/phantom-workers.svg?style=flat-square)](http://opensource.org/licenses/MIT)
[![Build Status](https://travis-ci.org/pofider/phantom-workers.png?branch=master)](https://travis-ci.org/pofider/phantom-workers)

@@ -17,3 +17,3 @@

require('webserver').create().listen('127.0.0.1:' + port, function (req, res) {
require('webserver').create().listen('127.0.0.1:' + port, function (req, res) {
//standard phantomjs script which get input parametrs from request

@@ -49,3 +49,13 @@ var page = require('webpage').create();

##Options
`pathToPhantomScript` (required) - absolute path to the phantom script
`timeout` - execution timeout in ms
`numberOfWorkers` - number of phantomjs instances
`host` - ip or hostname where to start listening phantomjs web service, default 127.0.0.1
`portLeftBoundary` - don't specify if you just want to take any random free port
`portRightBoundary` - don't specify if you just want to take any random free port
##License
See [license](https://github.com/pofider/phantom-workers/blob/master/LICENSE)
var port = require("system").stdin.readLine();
var host = require("system").stdin.readLine();
require('webserver').create().listen('127.0.0.1:' + port, function (req, res) {
require('webserver').create().listen(host + ':' + port, function (req, res) {
try {

@@ -5,0 +6,0 @@ res.statusCode = 200;

var port = require("system").stdin.readLine();
var host = require("system").stdin.readLine();
setTimeout(function() {
require('webserver').create().listen('127.0.0.1:' + port, function (req, res) {
require('webserver').create().listen(host + ':' + port, function (req, res) {
try {

@@ -6,0 +7,0 @@ res.statusCode = 200;

var port = require("system").stdin.readLine();
var host = require("system").stdin.readLine();
require('webserver').create().listen('127.0.0.1:' + port, function (req, res) {
require('webserver').create().listen(host + ':' + port, function (req, res) {
});

@@ -48,2 +48,37 @@ var should = require("should"),

it("should be able to start phantom in a port range", function (done) {
phantomManager = new PhantomManager({
pathToPhantomScript: path.join(__dirname, "test-script", "script.js"),
numberOfWorkers: 1,
portLeftBoundary: 10000,
portRightBoundary: 11000
});
phantomManager.start(function(err) {
if (err)
return done(err);
phantomManager._phantomInstances[0].port.should.be.within(10000,11000);
done();
});
});
it("should be able to communicate with just-port script", function (done) {
phantomManager = new PhantomManager({
pathToPhantomScript: path.join(__dirname, "test-script", "just-port.js"),
numberOfWorkers: 1
});
phantomManager.start(function(err) {
if (err)
return done(err);
phantomManager.execute({foo: "test"}, function (err, res) {
if (err)
return done(err);
res.should.have.property("foo");
done();
});
});
});
it("should be able to send just a simple string on input", function (done) {

@@ -113,3 +148,3 @@ phantomManager = new PhantomManager({

numberOfWorkers: 1,
timeout: 10
timeout: 100
});

@@ -116,0 +151,0 @@