diff --git a/lib/worker.js b/lib/worker.js index 38028c3b6c03b3aad53283cd6911cf64457acf02..c1cf153c7a9f0441cb1bde6c56c0f074420c2c10 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -41,6 +41,8 @@ process.once("message", function (obj) { } }; + global.__dirname = __dirname; + global.__filename = __filename; global.require = require; global.importScripts = function () { diff --git a/package.json b/package.json index cbac20190e59ff4c6f0e17bc614dcdb31f01354a..5e83914b49238824161885c1116cf622f6207206 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tiny-worker", - "version": "1.1.5", + "version": "1.1.6", "description": "Tiny WebWorker for Server", "main": "lib/index.js", "scripts": { diff --git a/src/worker.js b/src/worker.js index 74e598d14857d2e61f68f8616017c96d1951d4f7..bba70e55b1116a1b8e4d3e7b0b980bec92968899 100644 --- a/src/worker.js +++ b/src/worker.js @@ -39,6 +39,8 @@ process.once("message", obj => { } }; + global.__dirname = __dirname; + global.__filename = __filename; global.require = require; global.importScripts = (...files) => { diff --git a/test/worker_test.js b/test/worker_test.js index 171fdd982abcd24745d08ece8721ffad85e06912..b793bc603cfd41d6f103f5aff9d0059e0e0190f8 100644 --- a/test/worker_test.js +++ b/test/worker_test.js @@ -56,7 +56,7 @@ exports["inline script"] = { exports["inline script - require"] = { setUp: function (done) { this.worker = new Worker(function () { - self.onmessage = function (ev) { + self.onmessage = function () { postMessage(typeof require); }; }); @@ -80,3 +80,59 @@ exports["inline script - require"] = { this.worker.postMessage(this.msg); } }; + +exports["inline script - __dirname"] = { + setUp: function (done) { + this.worker = new Worker(function () { + self.onmessage = function () { + postMessage(typeof __dirname); + }; + }); + this.msg = "What is __dirname?"; + this.expected = "string"; + 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); + } +}; + +exports["inline script - __filename"] = { + setUp: function (done) { + this.worker = new Worker(function () { + self.onmessage = function () { + postMessage(typeof __filename); + }; + }); + this.msg = "What is __filename?"; + this.expected = "string"; + 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); + } +};