Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "microjob", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A tiny wrapper for Node.js worker_threads lib", | ||
@@ -8,4 +8,5 @@ "main": "src/job.js", | ||
"test": "node --experimental-worker node_modules/.bin/_mocha ./test", | ||
"doc": "node_modules/.bin/gitbook build . docs", | ||
"lint": "node_modules/.bin/eslint src/job.js" | ||
"docs": "gitbook build . docs", | ||
"serve-docs": "gitbook serve", | ||
"lint": "eslint src/job.js" | ||
}, | ||
@@ -12,0 +13,0 @@ "engines": { |
@@ -15,2 +15,9 @@ # Microjob | ||
## Installation | ||
Via **npm**: | ||
```bash | ||
$ npm install --save microjob | ||
``` | ||
## Quick Example | ||
@@ -17,0 +24,0 @@ ```js |
const { Worker } = require('worker_threads') | ||
const v8 = require('v8') | ||
const MISSING_HANDLER_ERROR = ` | ||
job needs a function or a string. | ||
Try with: | ||
> job(() => {...}, config) | ||
` | ||
const MISSING_HANDLER_ERROR = `job needs a function.\nTry with:\n> job(() => {...}, config)` | ||
function job(handler, config = { ctx: {}, data: {} }) { | ||
return new Promise((resolve, reject) => { | ||
if (typeof handler === 'undefined' || handler === null) return reject(new Error(MISSING_HANDLER_ERROR)) | ||
if (typeof handler !== 'function') return reject(new Error(MISSING_HANDLER_ERROR)) | ||
let worker | ||
if (typeof handler === 'string') { | ||
worker = new Worker(handler, { | ||
workerData: config.data | ||
}) | ||
} else if (typeof handler === 'function') { | ||
try { | ||
let variables = '' | ||
@@ -37,3 +29,2 @@ for (const key in config.ctx) { | ||
// @todo: issue with functions and classes on postMessage (v8.serialize does not work) | ||
const workerStr = ` | ||
@@ -69,22 +60,27 @@ (async function () { | ||
` | ||
worker = new Worker(workerStr, { | ||
// check for serialization's error, due to this issue: https://github.com/nodejs/node/issues/22736 | ||
v8.serialize(config.data) | ||
const worker = new Worker(workerStr, { | ||
eval: true, | ||
workerData: config.data | ||
}) | ||
} else return reject(new Error(MISSING_HANDLER_ERROR)) | ||
worker.on('message', message => { | ||
if (message.error) { | ||
const error = new Error(message.error.message) | ||
error.stack = message.error.stack | ||
reject(error) | ||
} else resolve(message.data) | ||
worker.on('message', message => { | ||
if (message.error) { | ||
const error = new Error(message.error.message) | ||
error.stack = message.error.stack | ||
reject(error) | ||
} else resolve(message.data) | ||
worker.unref() | ||
}) | ||
worker.unref() | ||
}) | ||
worker.on('error', error => { | ||
reject(error) | ||
worker.unref() | ||
}) | ||
worker.on('error', error => { | ||
reject(error) | ||
worker.unref() | ||
}) | ||
} catch (err) { | ||
reject(err) | ||
} | ||
}) | ||
@@ -91,0 +87,0 @@ } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
1337809
33
2598
47
3
10
1