{"version":3,"file":"worker.js","sources":["../src/transfer.js","../src/worker.js"],"sourcesContent":["/**\n * The helper class for transferring data from the worker to the main thread.\n *\n * @param {Object} message The object to deliver to the main thread.\n * @param {Object[]} transfer An array of transferable Objects to transfer ownership of.\n */\nfunction Transfer(message, transfer) {\n this.message = message;\n this.transfer = transfer;\n}\n\nmodule.exports = Transfer;\n","/**\n * worker must be started as a child process or a web worker.\n * It listens for RPC messages from the parent process.\n */\nvar Transfer = require('./transfer');\n\n/**\n * Special message sent by parent which causes the worker to terminate itself.\n * Not a \"message object\"; this string is the entire message.\n */\nvar TERMINATE_METHOD_ID = '__workerpool-terminate__';\n\n// var nodeOSPlatform = require('./environment').nodeOSPlatform;\n\n// create a worker API for sending and receiving messages which works both on\n// node.js and in the browser\nvar worker = {\n exit: function() {}\n};\nif (typeof self !== 'undefined' && typeof postMessage === 'function' && typeof addEventListener === 'function') {\n // worker in the browser\n worker.on = function (event, callback) {\n addEventListener(event, function (message) {\n callback(message.data);\n })\n };\n worker.send = function (message) {\n postMessage(message);\n };\n}\nelse if (typeof process !== 'undefined') {\n // node.js\n\n var WorkerThreads;\n try {\n WorkerThreads = require('worker_threads');\n } catch(error) {\n if (typeof error === 'object' && error !== null && error.code === 'MODULE_NOT_FOUND') {\n // no worker_threads, fallback to sub-process based workers\n } else {\n throw error;\n }\n }\n\n if (WorkerThreads &&\n /* if there is a parentPort, we are in a WorkerThread */\n WorkerThreads.parentPort !== null) {\n var parentPort = WorkerThreads.parentPort;\n worker.send = parentPort.postMessage.bind(parentPort);\n worker.on = parentPort.on.bind(parentPort);\n worker.exit = process.exit.bind(process);\n } else {\n worker.on = process.on.bind(process);\n // ignore transfer argument since it is not supported by process\n worker.send = function (message) {\n process.send(message);\n };\n // register disconnect handler only for subprocess worker to exit when parent is killed unexpectedly\n worker.on('disconnect', function () {\n process.exit(1);\n });\n worker.exit = process.exit.bind(process);\n }\n}\nelse {\n throw new Error('Script must be executed as a worker');\n}\n\nfunction convertError(error) {\n return Object.getOwnPropertyNames(error).reduce(function(product, name) {\n return Object.defineProperty(product, name, {\n\tvalue: error[name],\n\tenumerable: true\n });\n }, {});\n}\n\n/**\n * Test whether a value is a Promise via duck typing.\n * @param {*} value\n * @returns {boolean} Returns true when given value is an object\n * having functions `then` and `catch`.\n */\nfunction isPromise(value) {\n return value && (typeof value.then === 'function') && (typeof value.catch === 'function');\n}\n\n// functions available externally\nworker.methods = {};\n\n/**\n * Execute a function with provided arguments\n * @param {String} fn Stringified function\n * @param {Array} [args] Function arguments\n * @returns {*}\n */\nworker.methods.run = function run(fn, args) {\n var f = new Function('return (' + fn + ').apply(null, arguments);');\n return f.apply(f, args);\n};\n\n/**\n * Get a list with methods available on this worker\n * @return {String[]} methods\n */\nworker.methods.methods = function methods() {\n return Object.keys(worker.methods);\n};\n\n/**\n * Custom handler for when the worker is terminated.\n */\nworker.terminationHandler = undefined;\n\n/**\n * Cleanup and exit the worker.\n * @param {Number} code \n * @returns \n */\nworker.cleanupAndExit = function(code) {\n var _exit = function() {\n worker.exit(code);\n }\n\n if(!worker.terminationHandler) {\n return _exit();\n }\n\n var result = worker.terminationHandler(code);\n if (isPromise(result)) {\n result.then(_exit, _exit);\n } else {\n _exit();\n }\n}\n\nvar currentRequestId = null;\n\nworker.on('message', function (request) {\n if (request === TERMINATE_METHOD_ID) {\n return worker.cleanupAndExit(0);\n }\n try {\n var method = worker.methods[request.method];\n\n if (method) {\n currentRequestId = request.id;\n \n // execute the function\n var result = method.apply(method, request.params);\n\n if (isPromise(result)) {\n // promise returned, resolve this and then return\n result\n .then(function (result) {\n if (result instanceof Transfer) {\n worker.send({\n id: request.id,\n result: result.message,\n error: null\n }, result.transfer);\n } else {\n worker.send({\n id: request.id,\n result: result,\n error: null\n });\n }\n currentRequestId = null;\n })\n .catch(function (err) {\n worker.send({\n id: request.id,\n result: null,\n error: convertError(err)\n });\n currentRequestId = null;\n });\n }\n else {\n // immediate result\n if (result instanceof Transfer) {\n worker.send({\n id: request.id,\n result: result.message,\n error: null\n }, result.transfer);\n } else {\n worker.send({\n id: request.id,\n result: result,\n error: null\n });\n }\n\n currentRequestId = null;\n }\n }\n else {\n throw new Error('Unknown method \"' + request.method + '\"');\n }\n }\n catch (err) {\n worker.send({\n id: request.id,\n result: null,\n error: convertError(err)\n });\n }\n});\n\n/**\n * Register methods to the worker\n * @param {Object} [methods]\n * @param {import('./types.js').WorkerRegisterOptions} [options]\n */\nworker.register = function (methods, options) {\n\n if (methods) {\n for (var name in methods) {\n if (methods.hasOwnProperty(name)) {\n worker.methods[name] = methods[name];\n }\n }\n }\n\n if (options) {\n worker.terminationHandler = options.onTerminate;\n }\n\n worker.send('ready');\n};\n\nworker.emit = function (payload) {\n if (currentRequestId) {\n if (payload instanceof Transfer) {\n worker.send({\n id: currentRequestId,\n isEvent: true,\n payload: payload.message\n }, payload.transfer);\n return;\n }\n\n worker.send({\n id: currentRequestId,\n isEvent: true,\n payload\n });\n }\n};\n\nif (typeof exports !== 'undefined') {\n exports.add = worker.register;\n exports.emit = worker.emit;\n}\n"],"names":["Transfer","message","transfer","require$$0","TERMINATE_METHOD_ID","worker","exit","self","postMessage","addEventListener","on","event","callback","data","send","process","WorkerThreads","require","error","code","parentPort","bind","Error","convertError","Object","getOwnPropertyNames","reduce","product","name","defineProperty","value","enumerable","isPromise","then","catch","methods","run","fn","args","f","Function","apply","keys","terminationHandler","undefined","cleanupAndExit","_exit","result","currentRequestId","request","method","id","params","err","register","options","hasOwnProperty","onTerminate","emit","payload","isEvent","exports","add"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAMA,SAASA,QAAQA,CAACC,OAAO,EAAEC,QAAQ,EAAE;GACnC,IAAI,CAACD,OAAO,GAAGA,OAAO,CAAA;GACtB,IAAI,CAACC,QAAQ,GAAGA,QAAQ,CAAA;CAC1B,CAAA;CAEA,IAAAA,QAAc,GAAGF,QAAQ;;;;;;;GCPzB,IAAIA,QAAQ,GAAGG,QAAqB,CAA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