const path = require("path");
const fork = require("child_process").fork;
const worker = path.join(__dirname, "worker.js");
const events = /^(error|message)$/;

class Worker {
	constructor (arg) {
		let isfn = typeof arg === "function",
			input = isfn ? arg.toString() : arg;

		this.child = fork(worker);
		this.onerror = undefined;
		this.onmessage = undefined;

		this.child.on("error", e => {
			if (this.onerror) {
				this.onerror.call(this, e);
			}
		});

		this.child.on("message", msg => {
			if (this.onmessage) {
				this.onmessage.call(this, JSON.parse(msg));
			}
		});

		this.child.send({input: input, isfn: isfn});
	}

	addEventListener (event, fn) {
		if (events.test(event)) {
			this["on" + event] = fn;
		}
	}

	postMessage (msg) {
		this.child.send(JSON.stringify({data: msg}));
	}

	terminate () {
		this.child.kill("SIGHUP");
	}
}

module.exports = Worker;

const path = require("path");
const fork = require("child_process").fork;
const worker = path.join(__dirname, "worker.js");
const events = /^(error|message)$/;

class Worker {
	constructor (arg) {
		let isfn = typeof arg === "function",
			input = isfn ? arg.toString() : arg;

		this.child = fork(worker);
		this.onerror = undefined;
		this.onmessage = undefined;

		this.child.on("error", e => {
			if (this.onerror) {
				this.onerror.call(this, e);
			}
		});

		this.child.on("message", msg => {
			if (this.onmessage) {
				this.onmessage.call(this, JSON.parse(msg));
			}
		});

		this.child.send({input: input, isfn: isfn});
	}

	addEventListener (event, fn) {
		if (events.test(event)) {
			this["on" + event] = fn;
		}
	}

	postMessage (msg) {
		this.child.send(JSON.stringify({data: msg}));
	}

	terminate () {
		this.child.kill("SIGINT");
	}
}

module.exports = Worker;

Use SIGINT instead of SIGHUP to kill child
Use SIGINT instead of SIGHUP to kill child

`kill("SIGHUP")` does not work on Windows:

Error: kill ENOSYS
    at exports._errnoException (util.js:870:11)
    at ChildProcess.kill (internal/child_process.js:374:13)
    at Worker.terminate (C:\Users\xkr47\project\node_modules\tiny-worker\lib\index.js:55:15)

SIGINT works however, and fix confirmed elsewhere also: https://github.com/balderdashy/sails/issues/846#issuecomment-25187082