funthreads
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -1,23 +0,23 @@ | ||
import executeInThread from 'funthreads'; | ||
const { executeInThread } = require('funthreads'); | ||
// this will be executed in a dedicated thread | ||
const task = () => { | ||
// return is important! | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve('Done!'); | ||
}, 3000); | ||
}); | ||
}; | ||
function task() { | ||
// return is important! | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve('Done!'); | ||
}, 3000); | ||
}); | ||
} | ||
async function start() { | ||
try { | ||
const data = await executeInThread(task); | ||
try { | ||
const data = await executeInThread(task); | ||
console.log(data); | ||
} catch(err) { | ||
console.log(err); | ||
} | ||
console.log(data); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
start(); |
@@ -1,13 +0,12 @@ | ||
import executeInThread from 'funthreads'; | ||
const { executeInThread } = require('funthreads'); | ||
async function calculate() { | ||
const values = await Promise.all([ | ||
executeInThread(() => 2 ** 10), | ||
executeInThread(() => 3 ** 10) | ||
]); | ||
console.log(values); | ||
const values = await Promise.all([ | ||
executeInThread(() => 2 ** 10), | ||
executeInThread(() => 3 ** 10), | ||
]); | ||
console.log(values); | ||
} | ||
calculate(); |
@@ -1,16 +0,14 @@ | ||
import executeInThread from 'funthreads'; | ||
const { executeInThread } = require('funthreads'); | ||
// this will be executed in a dedicated thread | ||
const task = () => { | ||
return Promise.reject('Something wrong!'); | ||
}; | ||
const task = () => Promise.reject(new Error('Something wrong!')); | ||
async function start() { | ||
try { | ||
await executeInThread(task); | ||
} catch(err) { | ||
console.log(err); | ||
} | ||
try { | ||
await executeInThread(task); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
start(); |
@@ -1,19 +0,20 @@ | ||
import executeInThread from 'funthreads'; | ||
const { executeInThread, ThreadModules } = require('funthreads'); | ||
// this will be executed in a dedicated thread | ||
async function task(fileName) { | ||
// Closure doesn't work here | ||
const { writeFile } = require('fs/promises'); | ||
async function task(modules) { | ||
// Closure doesn't work here | ||
const { readFile } = modules['fs/promises']; | ||
await writeFile(fileName, 'Hello from a thread!'); | ||
const content = await readFile(__filename); | ||
return content.toString(); | ||
} | ||
const fileName = 'thread.txt'; | ||
async function read() { | ||
const modules = new ThreadModules('fs/promises'); | ||
const content = await executeInThread(task, modules); | ||
async function read() { | ||
const content = await executeInThread(task, fileName); | ||
console.log(content); | ||
console.log(content); | ||
} | ||
read(); | ||
read(); |
@@ -1,18 +0,18 @@ | ||
import executeInThread from 'funthreads'; | ||
const { executeInThread } = require('funthreads'); | ||
// this will be executed in a dedicated thread | ||
const task = (a, b, c) => { | ||
console.log('a', a, typeof a); | ||
console.log('b', b, typeof b); | ||
console.log('c', c, typeof c); | ||
console.log('a', a, typeof a); | ||
console.log('b', b, typeof b); | ||
console.log('c', c, typeof c); | ||
}; | ||
async function start() { | ||
try { | ||
await executeInThread(task, 1, {}, true); | ||
} catch(err) { | ||
console.log(err); | ||
} | ||
try { | ||
await executeInThread(task, 1, {}, true); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
start(); |
{ | ||
"name": "funthreads", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "A lightweight tool built on top of Node.js worker_threads, enabling multithreading.", | ||
"keywords": [ | ||
"node.js", | ||
"worker", | ||
"workers", | ||
"thread", | ||
"threads", | ||
"worker-threads" | ||
"worker_threads", | ||
"multithreading" | ||
], | ||
"main": "dist/index.js", | ||
"directories": { | ||
"example": "examples", | ||
"test": "test" | ||
}, | ||
"main": "src/funthreads.js", | ||
"engines": { | ||
@@ -22,9 +17,4 @@ "node": ">=10.5" | ||
"scripts": { | ||
"lint:src": "eslint src --fix", | ||
"lint:test": "NODE_ENV=development eslint test/**/* --fix", | ||
"lint:examples": "NODE_ENV=development eslint examples --fix", | ||
"build": "rimraf dist && tsc && npm run copy-files", | ||
"test": "jest test/**/*.ts --coverage", | ||
"copy-files": "copyfiles -u 1 src/**/*.js dist", | ||
"prepare": "npm run build" | ||
"test": "node --test ./test", | ||
"lint": "eslint ." | ||
}, | ||
@@ -42,15 +32,5 @@ "repository": { | ||
"devDependencies": { | ||
"@types/jest": "^26.0.23", | ||
"@types/node": "^15.12.1", | ||
"@typescript-eslint/eslint-plugin": "^4.26.0", | ||
"babel-eslint": "^10.1.0", | ||
"copyfiles": "^2.4.1", | ||
"eslint": "^7.28.0", | ||
"jest": "^27.0.4", | ||
"mocha": "^9.2.2", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^27.0.2", | ||
"ts-node": "^10.0.0", | ||
"typescript": "^4.3.2" | ||
"eslint": "8.49.0", | ||
"eslint-config-airbnb-base": "15.0.0" | ||
} | ||
} |
102
README.md
@@ -1,3 +0,1 @@ | ||
[](https://snyk.io/test/github/nairihar/funthreads) | ||
[](https://codeclimate.com/github/nairihar/funthreads/maintainability) | ||
 | ||
@@ -8,30 +6,15 @@  | ||
# funthreads | ||
A simple library that provides an abstraction for the Node.js worker_threads module. 🔥 | ||
A simple library that provides an abstraction for the Node.js `worker_threads` module. You can run your function in a dedicated thread by working with Promises. | ||
You can run your function in a dedicated thread by working with Promises. This library presents a simple tool that takes a task function as its parameter, orchestrates its execution in a new thread, and subsequently delivers a Promise. Within the confines of this task function, you retain the freedom to furnish either a Promise or an unprocessed value (ranging from objects to strings, and more). | ||
### Example | ||
```js | ||
const { executeInThread } = require('funthreads'); | ||
Furthermore, this library seamlessly integrates with Async/Await and TypeScript for an elevated development experience. | ||
## Installation | ||
```shell | ||
$ npm i funthreads | ||
``` | ||
## Example | ||
This example demonstrates the optimization of two resource-intensive calculations through parallel execution in distinct threads. | ||
By distributing the tasks across separate threads, significant time savings are achieved. | ||
```javascript | ||
import executeInThread from 'funthreads'; | ||
async function calculate() { | ||
const values = await Promise.all([ | ||
executeInThread(() => 2 ** 10), | ||
executeInThread(() => 2 ** 10), // this doesn't block the main thread | ||
executeInThread(() => 3 ** 10) | ||
]); | ||
console.log(values); | ||
console.log(values); // 1024, 59049 | ||
} | ||
@@ -42,4 +25,15 @@ | ||
This example demonstrates the optimization of two resource-intensive calculations through parallel execution in distinct threads. | ||
By distributing the tasks across separate threads, significant time savings are achieved. | ||
Funthreads takes a task function as its parameter, orchestrates its execution in a new thread, and subsequently delivers a Promise. | ||
**Surprisingly simple, isn't it?** | ||
## Installation | ||
```shell | ||
$ npm i funthreads | ||
``` | ||
## All examples: | ||
@@ -52,11 +46,21 @@ - [Basic example](https://github.com/nairihar/funthreads/tree/master/examples/basic.js) | ||
## Contributing | ||
See the [contributing guide](https://github.com/nairihar/funthreads/blob/master/CONTRIBUTING.md) for detailed instructions on how to get started with our project. | ||
## API | ||
### `executeInThread(task, ...params)` | ||
Execute a function in a thread. | ||
Runs the specified function in a separate thread. | ||
#### Parameters | ||
`- Task (Function)`: The function to be executed in a thread. | ||
- `Task (Function)`: The function to be executed in a thread. | ||
- This can also be a async function (promise). | ||
- `...params (Any)`: Additional arguments to be passed to the Task function. | ||
- Parameter cann't be a function. | ||
`- ...params (any)`: Additional arguments to be passed to the Task function. | ||
```js | ||
const task = function() { ... }; | ||
executeInThread(task, 'John', true, {}, ...); | ||
``` | ||
@@ -70,27 +74,47 @@ The `executeInThread` function allows you to execute a given task function in a dedicated thread, similar to the behavior of `setTimeout` or `setInterval`. You provide the main function to be executed, along with any additional arguments (...args) that should be passed to the given function. | ||
#### Important | ||
```js | ||
const number = await executeInThread(() => 123); // 123 | ||
const name = await executeInThread(() => Promise.resolve('John')); // John | ||
``` | ||
#### Important (limitation) | ||
Access to data outside of the task function is restricted. If you require the use of a module, it should be required within the task function. The sole method for accessing data within a task function from external sources is through the utilization of the parameters. Closures do not function in this context. | ||
#### Example | ||
In this example, we're reading a file in a separate thread and returning the data in string format. We start by defining a task function that will run within the thread, and then we prepare the necessary parameters to be passed as inputs to that function. | ||
```javascript | ||
import executeInThread from 'funthreads'; | ||
const { executeInThread } = require('funthreads'); | ||
// this will be executed in a dedicated thread | ||
async function task(fileName) { | ||
// Closure doesn't work here | ||
const { writeFile } = require('fs/promises'); | ||
// Closure doesn't work here | ||
const { readFile } = require('fs/promises'); | ||
const content = await readFile(__filename); | ||
return content.toString(); | ||
} | ||
await writeFile(fileName, 'Hello from a thread!'); | ||
async function read() { | ||
const content = await executeInThread(task, fileName); | ||
console.log(content); | ||
} | ||
const fileName = 'thread.txt'; | ||
read(); | ||
``` | ||
There is also another option if you don't want to use `require` inside the function. | ||
```js | ||
const { executeInThread, ThreadModules } = require('funthreads'); | ||
async function task(modules) { | ||
// Closure doesn't work here | ||
const { readFile } = modules['fs/promises']; | ||
const content = await readFile(__filename); | ||
return content.toString(); | ||
} | ||
async function read() { | ||
const content = await executeInThread(task, fileName); | ||
console.log(content); | ||
const modules = new ThreadModules('fs/promises', 'test', 'path', ...); | ||
const content = await executeInThread(task, modules); | ||
console.log(content); | ||
} | ||
@@ -100,1 +124,3 @@ | ||
``` | ||
The `ThreadModules` class lets you set up modules for the thread. Just provide it as the second argument, and you'll have access to the libraries through the `modules` object. |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
13663
2
122
0
16
136
2