diff --git a/README.md b/README.md index 33d8ec2955c2a9aebf5be3133bc29fcd5e6da633..8aed1e51f3aac23b104518ecbafa1a07029d669b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # tiny-worker Tiny WebWorker for Server +`require()` is available for flexible inline Worker scripts. + [](http://travis-ci.org/avoidwork/tiny-worker) ## Example @@ -53,9 +55,6 @@ Error handler, accepts an `Event` #### addEventListener(event, fn) Adds an event listener -#### close() -Terminates the `Worker`, same as `terminate()` - #### postMessage() Broadcasts a message to the `Worker` diff --git a/lib/index.js b/lib/index.js index 471a6d2fd0143e2c98c516cca8182d2a7416603a..504c2ade9d22e67443540fc8f471ae868daafd76 100644 --- a/lib/index.js +++ b/lib/index.js @@ -44,11 +44,6 @@ var Worker = (function () { this["on" + event] = fn; } } - }, { - key: "close", - value: function close() { - this.child.kill(); - } }, { key: "postMessage", value: function postMessage(msg) { diff --git a/lib/worker.js b/lib/worker.js index 4ee31f68b894e12ba15ebe2f8dd9426a7ea6be31..b4d2b7ba54924f3d962b1086852e2b2833cf66c3 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -22,8 +22,7 @@ function toFunction(arg) { // Bootstraps the Worker process.once("message", function (obj) { - var exp = obj.isfn ? toFunction(obj.input) : fs.readFileSync(obj.input, "utf8"), - sexp = undefined; + var exp = obj.isfn ? toFunction(obj.input) : fs.readFileSync(obj.input, "utf8"); global.self = { close: function close() { @@ -45,6 +44,8 @@ process.once("message", function (obj) { } }; + global.require = require; + global.importScripts = function () { var script = undefined, scripts = undefined; @@ -78,7 +79,6 @@ process.once("message", function (obj) { if (typeof exp === "function") { exp(); } else { - sexp = vm.createScript(exp); - sexp.runInThisContext(); + vm.createScript(exp).runInThisContext(); } }); diff --git a/package.json b/package.json index 27c2a532111919eefc8e06b902c2d02031d0bccb..1a4234853819bf6f6120b1886847b43f92dab636 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tiny-worker", - "version": "1.0.4", + "version": "1.1.0", "description": "Tiny WebWorker for Server", "main": "lib/index.js", "scripts": { diff --git a/src/index.js b/src/index.js index af7957dd5f5fd7320604914db68364e4adcfc008..5914921395e18b74fc710e930c4c1d4b028950b2 100644 --- a/src/index.js +++ b/src/index.js @@ -33,10 +33,6 @@ class Worker { } } - close () { - this.child.kill(); - } - postMessage (msg) { this.child.send(JSON.stringify({data: msg})); } diff --git a/src/worker.js b/src/worker.js index 9ee0f0fbdfb7fc73a2b2843d873fb38ed8e99d2c..11a8494123ac0edfb9b0ba340cff95a0937fcd55 100644 --- a/src/worker.js +++ b/src/worker.js @@ -20,8 +20,7 @@ function toFunction (arg) { // Bootstraps the Worker process.once("message", function (obj) { - let exp = obj.isfn ? toFunction(obj.input) : fs.readFileSync(obj.input, "utf8"), - sexp; + let exp = obj.isfn ? toFunction(obj.input) : fs.readFileSync(obj.input, "utf8"); global.self = { close: function () { @@ -43,6 +42,8 @@ process.once("message", function (obj) { } }; + global.require = require; + global.importScripts = function (...files) { let script, scripts; @@ -71,7 +72,6 @@ process.once("message", function (obj) { if (typeof exp === "function") { exp(); } else { - sexp = vm.createScript(exp); - sexp.runInThisContext(); + vm.createScript(exp).runInThisContext(); } }); diff --git a/test/worker_test.js b/test/worker_test.js index f156b0a0bc7616e06565c29e22416d04f4a1c1a9..07f19ea09ae450373fb8d24a5000396a5c8ae501 100644 --- a/test/worker_test.js +++ b/test/worker_test.js @@ -52,3 +52,31 @@ exports["inline script"] = { this.worker.postMessage(this.msg); } }; + +exports["inline script"] = { + setUp: function (done) { + this.worker = new Worker(function () { + self.onmessage = function (ev) { + postMessage(typeof require); + }; + }); + this.msg = "What is require?"; + this.expected = "function"; + done(); + }, + test: function (test) { + var self = this; + + test.expect(2); + test.notEqual(this.msg, this.response, "Should not match"); + + this.worker.onmessage = function (ev) { + self.response = ev.data; + self.worker.terminate(); + test.equal(self.expected, self.response, "Should be a match"); + test.done(); + }; + + this.worker.postMessage(this.msg); + } +};