rollup-plugin-worker-factory
Advanced tools
Comparing version 0.5.5 to 0.5.6
@@ -0,1 +1,8 @@ | ||
<a name="0.5.6"></a> | ||
## [0.5.6](https://github.com/brandonocasey/rollup-plugin-worker-factory/compare/v0.5.5...v0.5.6) (2021-06-24) | ||
### Bug Fixes | ||
* slight parentPort API change workaround for node >= 14 ([31d3902](https://github.com/brandonocasey/rollup-plugin-worker-factory/commit/31d3902)) | ||
<a name="0.5.5"></a> | ||
@@ -2,0 +9,0 @@ ## [0.5.5](https://github.com/brandonocasey/rollup-plugin-worker-factory/compare/v0.5.4...v0.5.5) (2021-04-09) |
{ | ||
"name": "rollup-plugin-worker-factory", | ||
"version": "0.5.5", | ||
"version": "0.5.6", | ||
"description": "Bundle web workers that work in nodejs and the browser, without a separate build target.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
// used to make nodejs workers behave like the browser | ||
const nodeWorkerPolyfill = function(workerObj) { | ||
const nodeWorkerPolyfill = function(workerObj, EventEmitter) { | ||
const oldPost = workerObj.postMessage; | ||
@@ -10,6 +11,40 @@ | ||
}; | ||
// the browser only supports addEventListener/removeEventListener | ||
workerObj.addEventListener = workerObj.on; | ||
workerObj.removeEventListener = workerObj.off; | ||
// if workerObj is an instance of EventEmitter then | ||
// we only need to mirror on/off as addEventListener/removeEventListener | ||
// this is always the case for Worker but only the case for | ||
// parentPort in node 10/12 | ||
if (workerObj instanceof EventEmitter) { | ||
workerObj.addEventListener = workerObj.on; | ||
workerObj.removeEventListener = workerObj.off; | ||
// in node >= 14 parentPort extents EventTarget | ||
// https://github.com/nodejs/node/issues/35835 | ||
// and we need to do a carefull polyfill to make | ||
// the builtin addEventListener call on so that | ||
// the message callback works as expected. | ||
} else { | ||
const old = { | ||
addEventListener: workerObj.addEventListener | ||
}; | ||
const polyfill = function(type, fn, options) { | ||
// set addEventListener to the builtin function | ||
// as `on` is called with a special symbol `kIsNodeStyleListener` | ||
// to indicate that it part of MessagePort and | ||
// should function between the sender/receiver. | ||
workerObj.addEventListener = old.addEventListener; | ||
const retval = workerObj.on(type, fn); | ||
// set back to polyfill after the call so that | ||
// user calls to this function always come here | ||
// and set `kIsNodeStyleListener` | ||
workerObj.addEventListener = polyfill; | ||
return retval; | ||
}; | ||
workerObj.addEventListener = polyfill; | ||
} | ||
return workerObj; | ||
@@ -26,2 +61,4 @@ }; | ||
const getWorker = new Function('req', 'return req("worker_threads").Worker;'); | ||
// eslint-disable-next-line | ||
const getEventEmitter = new Function('req', 'return req("events").EventEmitter;'); | ||
@@ -31,4 +68,9 @@ export const factory = function(code) { | ||
const Worker = getWorker(require); | ||
const worker_ = new Worker(code, {eval: true}); | ||
return nodeWorkerPolyfill(new Worker(code, {eval: true})); | ||
worker_.on('error', function(e) { | ||
throw e; | ||
}); | ||
return nodeWorkerPolyfill(worker_, getEventEmitter(require)); | ||
}; | ||
@@ -39,5 +81,5 @@ }; | ||
return `const nodeWorkerPolyfill = ${nodeWorkerPolyfill.toString()};\n` + | ||
'global.self = nodeWorkerPolyfill(require("worker_threads").parentPort);\n' + | ||
'global.self = nodeWorkerPolyfill(require("worker_threads").parentPort, require("events").EventEmitter);\n' + | ||
code; | ||
}; | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27419
289