# tiny-worker

> Tiny WebWorker for Server

Latest version **2.3.0** (published 2019-09-20) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install tiny-worker
pnpm add tiny-worker
yarn add tiny-worker
bun add tiny-worker
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.3.0 |
| Published | 2019-09-20 |
| First published | 2015-09-29 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 12.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 252 |
| Author | Jason Mulligan |
| Maintainers | avoidwork |
| Keywords | web, worker, ps, webworker |

## Links

- npm: https://www.npmjs.com/package/tiny-worker
- Repository: https://github.com/avoidwork/tiny-worker
- Issues: https://github.com/avoidwork/tiny-worker/issues
- npm.io page: https://npm.io/package/tiny-worker

## Dependencies (1)

- [esm](https://npm.io/package/esm.md) ^3.2.25

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 2.3.0 (latest) — 2019-09-20
- 2.2.0 — 2019-04-11
- 2.1.2 — 2018-03-01
- 2.1.1 — 2017-05-04
- 2.1.0 — 2017-04-20
- 2.0.3 — 2017-03-13
- 2.0.2 — 2017-03-05
- 2.0.1 — 2017-01-05
- 2.0.0 — 2017-01-05
- 1.1.7 — 2016-10-03
- 1.1.6 — 2016-10-03
- 1.1.5 — 2016-08-05
- 1.1.4 — 2016-06-15
- 1.1.3 — 2016-06-15
- 1.1.2 — 2016-06-15
- … 8 more at https://npm.io/package/tiny-worker/versions

## README

# tiny-worker
Tiny WebWorker for Server

`require()` is available for flexible inline Worker scripts. Optional parameters `args` Array & `options` Object; see `child_process.fork()` documentation.

[![build status](https://secure.travis-ci.org/avoidwork/tiny-worker.svg)](http://travis-ci.org/avoidwork/tiny-worker)

## Example
#### Creating a Worker from a file
The worker script:
```javascript
onmessage = function (ev) {
	postMessage(ev.data);
};
```

The core script:
```javascript
var Worker = require("tiny-worker");
var worker = new Worker("repeat.js");

worker.onmessage = function (ev) {
	console.log(ev.data);
	worker.terminate();
};

worker.postMessage("Hello World!");
```

#### Enable ES6 import/export within Worker file
The worker helper script (helper.js):
```javascript
export const dataFormatter = (data) => {
	return `${data} World!`;
};
```

The worker script (repeat.js):
```javascript
import { dataFormatter } from "./helper";

onmessage = function (ev) {
	const data = dataFormatter(ev.data);
	postMessage(data);
};
```

The core script:
```javascript
var Worker = require("tiny-worker");
var worker = new Worker("repeat.js", [], {esm: true});

worker.onmessage = function (ev) {
	console.log(ev.data);
	worker.terminate();
};

worker.postMessage("Hello");
```

#### Creating a Worker from a Function
```javascript
var Worker = require("tiny-worker");
var worker = new Worker(function () {
	self.onmessage = function (ev) {
		postMessage(ev.data);
	};
});

worker.onmessage = function (ev) {
	console.log(ev.data);
	worker.terminate();
};

worker.postMessage("Hello World!");
```

# Debugging
To be able to debug a child process, it must have a differnt debug port than the parent. 
Tiny worker does this by adding a random port within a range to the parents debug port.
The default Range is `[1, 300]`, it can be changed with the `setRange(min, max)` method.
To disable any automatic port redirection set `options.noDebugRedirection = true`.

### automatic redirection
```javascript
//parent is started with '--debug=1234'
var Worker = require("tiny-worker");
Worker.setRange(2, 20);

var worker = new Worker(function () {
	postMessage(process.debugPort); 
});

worker.onmessage = function (ev) {
	console.log(ev.data); //prints any number between 1236 and 1254
	worker.terminate();
}
```

### manual redirection
```javascript
//parent is started with '--debug=1234'
var Worker = require("tiny-worker");

var worker = new Worker(function () {
	postMessage(process.debugPort); 
}, [], {noDebugRedirection: true, execArgv: ["--debug=1235"]});

worker.onmessage = function (ev) {
	console.log(ev.data); //prints 1235
	worker.terminate();
}
```

## Properties
#### onmessage
Message handler, accepts an `Event`

#### onerror
Error handler, accepts an `Event`

## API
#### addEventListener(event, fn)
Adds an event listener

#### postMessage()
Broadcasts a message to the `Worker`

#### terminate()
Terminates the `Worker`

#### static setRange(min, max)
Sets range for debug ports, only affects current process.
Returns true if successful.

## FAQ
1. I have an orphaned child process that lives on past the parent process' lifespan
  * Most likely a `SIGTERM` or `SIGINT` is not reaching the child process
2. How do I insure all process are terminated?
  * In your core script register a listener for `SIGTERM` or `SIGINT` via `process.on()` which terminates (all) worker process(es) and then gracefully shutdowns via `process.exit(0);`
3. Why `SIGTERM` or `SIGINT`?
  * Unix/BSD will work with `SIGTERM`, but if you also need to support Windows use `SIGINT`

## License
Copyright (c) 2019 Jason Mulligan
Licensed under the BSD-3 license

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