# phantom-workers

> Run phantomjs scripts in managed workers

Latest version **0.6.0** (published 2022-07-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install phantom-workers
pnpm add phantom-workers
yarn add phantom-workers
bun add phantom-workers
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.0 |
| Published | 2022-07-20 |
| First published | 2015-03-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 28 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 15 |
| Author | Jan Blaha |
| Maintainers | pofider, bjrmatos |
| Keywords | phantomjs, workers |

## Links

- npm: https://www.npmjs.com/package/phantom-workers
- Repository: https://github.com/pofider/phantom-workers
- Issues: https://github.com/pofider/phantom-workers/issues
- npm.io page: https://npm.io/package/phantom-workers

## Dependencies (3)

- [phantomjs](https://npm.io/package/phantomjs.md) 1.9.20
- [lodash.find](https://npm.io/package/lodash.find.md) 4.6.0
- [object-assign](https://npm.io/package/object-assign.md) 4.1.1

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 0.6.0 (latest) — 2022-07-20
- 0.5.0 — 2019-07-09
- 0.4.3 — 2018-12-26
- 0.4.2 — 2017-05-16
- 0.4.1 — 2017-04-25
- 0.4.0 — 2016-05-26
- 0.3.3 — 2016-02-11
- 0.3.2 — 2016-01-02
- 0.3.1 — 2015-10-14
- 0.3.0 — 2015-09-28
- 0.2.1 — 2015-08-18
- 0.2.0 — 2015-04-29
- 0.1.3 — 2015-04-12
- 0.1.2 — 2015-04-05
- 0.1.1 — 2015-03-08
- … 1 more at https://npm.io/package/phantom-workers/versions

## README

# 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.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)

> **Run phantom scripts in multiple  managed reusable workers**

Running a script in phantom can soon become performance bottleneck when it comes to scale. Starting phantomjs process is not a cheap operation and you cannot start hundred of them at once. This package provides solution using phantomjs webserver and multiple phantomjs processes running in parallel.


## First create a phantomjs script wrapped in webserver

```js
//every worker gets unique port, get it from a process environment variables
var system = require("system");
var port = system.env['PHANTOM_WORKER_PORT'];
var host = system.env['PHANTOM_WORKER_HOST'];

require('webserver').create().listen(host + ':' + port, function (req, res) {
	//standard phantomjs script which get input parametrs from request
	var page = require('webpage').create();
	page.open(JSON.parse(req.post).url, function(status) {
    var title = page.evaluate(function() {
	    return document.title;
		});

		//write the result to the response
		res.statusCode = 200;
		res.setHeader('Content-Type', 'application/json');
	  res.write(JSON.stringify({ title: title }));
	  res.close();
	});
});

```

## Start phantomjs workers
```js
var phantom = require("phantom-workers")({
	pathToPhantomScript: "script.js",
	timeout: 5000,
	numberOfWorkers: 10
});

phantom.start(function(startErr) {
	if (startErr) {
		return console.error('Error while starting workers:', startErr);
	}

	phantom.execute({ url: "https://jsreport.net" }, function(err, res) {
		if (err) {
			return console.error('Error while executing:', err);
		}

		console.log(res.title);
	});
});
```

## Options

`pathToPhantomScript` (required) - absolute path to the phantom script<br/>
`timeout` - execution timeout in ms<br/>
`numberOfWorkers` - number of phantomjs instances<br/>
`host` - ip or hostname where to start listening phantomjs web service, default 127.0.0.1<br/>
`portLeftBoundary` - don't specify if you just want to take any random free port<br/>
`portRightBoundary` - don't specify if you just want to take any random free port<br/>
`hostEnvVarName` - customize the name of the environment variable passed to the phantom script that specifies the worker host. defaults to `PHANTOM_WORKER_HOST`<br/>
`portEnvVarName` - customize the name of the environment variable passed to the phantom script that specifies the worker port. defaults to `PHANTOM_WORKER_PORT`<br/>
`phantomPath` - path to the phantomjs library. If not specified, this will use the version of phantom declared in the `optionalDependencies` in `package.json`<br/>
`proxy,proxy-type,proxy-auth` - see phantomjs arguments for proxy setting details    
`workerEnv` - object with additional environment variables passed to the phantom process

## phantomjs2
This package includes phantomjs 1.9.x distribution. If you like to rather use latest phantomjs you can provide it in the phantomPath option.

Install phantomjs-prebuilt and then...
```js
var phantom = require("phantom-workers")({
	pathToPhantomScript: "script.js",
    phantomPath: require("phantomjs-prebuilt").path
});
```

## License
See [license](https://github.com/pofider/phantom-workers/blob/master/LICENSE)

---
_Source: https://npm.io/package/phantom-workers · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
