New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

microjob

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

microjob - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

_book/API.html

7

package.json
{
"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 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc