From 9c2b497a1914a5f7b1f234078a4358179f1a03be Mon Sep 17 00:00:00 2001 From: Jason Mulligan <jamullig@adobe.com> Date: Wed, 14 Oct 2015 11:33:09 -0400 Subject: [PATCH] Adding `require` to the inline script context for convenience, fixing API mistake by removing `close()` from the instance & updating README --- README.md | 5 ++--- lib/index.js | 5 ----- lib/worker.js | 8 ++++---- package.json | 2 +- src/index.js | 4 ---- src/worker.js | 8 ++++---- test/worker_test.js | 28 ++++++++++++++++++++++++++++ 7 files changed, 39 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 33d8ec2..8aed1e5 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 471a6d2..504c2ad 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 4ee31f6..b4d2b7b 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 27c2a53..1a42348 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 af7957d..5914921 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 9ee0f0f..11a8494 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 f156b0a..07f19ea 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); + } +}; -- GitLab