Comparing version 0.0.2 to 0.0.3
{ | ||
"name": "microjob", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A tiny wrapper for Node.js worker_threads lib", | ||
@@ -35,5 +35,4 @@ "main": "src/job.js", | ||
"gitbook-cli": "^2.3.2", | ||
"mocha": "^5.2.0", | ||
"sinon": "^6.1.5" | ||
"mocha": "^5.2.0" | ||
} | ||
} |
# Microjob | ||
A tiny wrapper for turning [Node.js threads](https://nodejs.org/api/worker_threads.htm) in easy-to-use routines for high CPU-bound. | ||
A tiny wrapper for turning [Node.js threads](https://nodejs.org/api/worker_threads.html) in easy-to-use routines for CPU-bound. | ||
## Requirements | ||
## Introduction | ||
Microjob is a tiny wrapper for Node.js threads and is intended to perform heavy CPU loads using anonymous functions. | ||
So, Microjob treats Node.js threads as temporary working units: if you need to spawn a long-living thread, then you should use the [default API](https://nodejs.org/api/worker_threads.html). | ||
Microjob follows the same line of the original Node.js documentation: use it only for CPU-bound jobs and not for I/O-bound purposes. | ||
Quoting the documentation: | ||
> Workers are useful for performing CPU-intensive JavaScript operations; do not use them for I/O, since Node.js’s built-in mechanisms for performing operations asynchronously already treat it more efficiently than Worker threads can. | ||
**Microjob** can be used only with **Node.js 10.5+** and with the **--experimental-worker** flag activated, otherwise it won't work. | ||
@@ -9,5 +17,5 @@ | ||
```js | ||
const { job } = require('microjob') | ||
(async () => { | ||
const { job } = require('microjob') | ||
(async () => { | ||
try { | ||
@@ -17,3 +25,5 @@ // this function will be executed in another thread | ||
let i = 0 | ||
for (i = 0; i < 1000000; i++) {} | ||
for (i = 0; i < 1000000; i++) { | ||
// heavy CPU load ... | ||
} | ||
@@ -31,2 +41,2 @@ return i | ||
## Documentation | ||
Dive deep into the documentation to know more: **[https://wilk.github.io/microjob/](https://wilk.github.io/microjob/)** | ||
Dive deep into the documentation to find more examples: **[API](API.md)** |
@@ -21,2 +21,4 @@ const { Worker } = require('worker_threads') | ||
for (const key in config.ctx) { | ||
if (!config.ctx.hasOwnProperty(key)) continue | ||
let variable | ||
@@ -49,6 +51,18 @@ switch (typeof config.ctx[key]) { | ||
} catch (err) { | ||
response.error = err | ||
response.error = { | ||
message: err.message, | ||
stack: err.stack | ||
} | ||
} | ||
parentPort.postMessage(response) | ||
try { | ||
parentPort.postMessage(response) | ||
} catch (err) { | ||
response.data = null | ||
response.error = { | ||
message: err.message, | ||
stack: err.stack | ||
} | ||
parentPort.postMessage(response) | ||
} | ||
})() | ||
@@ -63,4 +77,7 @@ ` | ||
worker.on('message', message => { | ||
if (message.error) reject(message.error) | ||
else resolve(message.data) | ||
if (message.error) { | ||
const error = new Error(message.error.message) | ||
error.stack = message.error.stack | ||
reject(error) | ||
} else resolve(message.data) | ||
@@ -67,0 +84,0 @@ worker.unref() |
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
5910
6
80
40