node-worker.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. exports.__esModule = true;
  3. // Mediocre shim
  4. var Worker;
  5. var workerAdd = ";var __w=require('worker_threads');__w.parentPort.on('message',function(m){onmessage({data:m})}),postMessage=function(m,t){__w.parentPort.postMessage(m,t)},close=process.exit;self=global";
  6. try {
  7. Worker = require('worker_threads').Worker;
  8. }
  9. catch (e) {
  10. }
  11. exports["default"] = Worker ? function (c, _, msg, transfer, cb) {
  12. var done = false;
  13. var w = new Worker(c + workerAdd, { eval: true })
  14. .on('error', function (e) { return cb(e, null); })
  15. .on('message', function (m) { return cb(null, m); })
  16. .on('exit', function (c) {
  17. if (c && !done)
  18. cb(new Error('exited with code ' + c), null);
  19. });
  20. w.postMessage(msg, transfer);
  21. w.terminate = function () {
  22. done = true;
  23. return Worker.prototype.terminate.call(w);
  24. };
  25. return w;
  26. } : function (_, __, ___, ____, cb) {
  27. setImmediate(function () { return cb(new Error('async operations unsupported - update to Node 12+ (or Node 10-11 with the --experimental-worker CLI flag)'), null); });
  28. var NOP = function () { };
  29. return {
  30. terminate: NOP,
  31. postMessage: NOP
  32. };
  33. };