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

funthreads

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

funthreads - npm Package Compare versions

Comparing version 1.1.1 to 1.2.0

dist/utils/worker.js

14

dist/index.js

@@ -6,8 +6,10 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
const worker_1 = __importDefault(require("./worker/worker"));
const metadata_1 = require("./worker/metadata");
const executeInThread = (task, ...params) => {
const metadata = metadata_1.createWorkerMetadata(task, params);
return worker_1.default(metadata);
exports.runOnThread = void 0;
const worker_1 = require("./utils/worker");
const worker_2 = __importDefault(require("./worker"));
const runOnThread = (cb, data = {}) => {
const workerData = worker_1.genWorkerData(cb, data);
return worker_2.default(workerData);
};
exports.default = executeInThread;
exports.runOnThread = runOnThread;
exports.default = exports.runOnThread;

@@ -0,5 +1,9 @@

/* eslint-disable no-alert, no-eval */
const { workerData } = require('worker_threads');
const { workerCode } = workerData;
const { threadData, workerCode } = workerData;
global.threadData = threadData;
eval(workerCode);
{
"name": "funthreads",
"version": "1.1.1",
"description": "A lightweight tool built on top of Node.js worker_threads, enabling multithreading.",
"version": "1.2.0",
"description": "Multi-threading library for Node.js. Run functions inside threads.",
"keywords": [

@@ -39,2 +39,3 @@ "node.js",

},
"type": "module",
"homepage": "https://github.com/nairihar/funthreads#readme",

@@ -41,0 +42,0 @@ "devDependencies": {

@@ -26,9 +26,9 @@ [![Build Status](https://travis-ci.org/nairihar/funthreads.svg?branch=master)](https://travis-ci.org/nairihar/funthreads)

```javascript
import executeInThread from 'funthreads';
import { runOnThread } from 'funthreads';
async function calculate() {
const values = await Promise.all([
executeInThread(() => 2 ** 10),
runOnThread(() => 2 ** 10)),
executeInThread(() => 3 ** 10)
runOnThread(() => 3 ** 10))
]);

@@ -44,25 +44,27 @@

A comprehensive example can be found here: [_basic/index.js_](https://github.com/nairihar/funthreads/blob/master/examples/basic/index.js):
## All examples:
- [Basic example](https://github.com/nairihar/funthreads/tree/master/examples/basic.js)
- [Parameters for the thread task](https://github.com/nairihar/funthreads/blob/master/examples/multi-params.js)
- [Async function inside the thread](https://github.com/nairihar/funthreads/blob/master/examples/async-task.js)
- [Error handling](https://github.com/nairihar/funthreads/blob/master/examples/error-handling.js)
- [Use modules inside the thread](https://github.com/nairihar/funthreads/blob/master/examples/modules-in-thread.js)
- [Basic example](https://github.com/nairihar/funthreads/tree/master/examples/basic)
- [Execute the task on a thread with pre-defined initial data](https://github.com/nairihar/funthreads/blob/master/examples/run_thread_with_custom_data/index.js)
- [Async thread](https://github.com/nairihar/funthreads/blob/master/examples/async_thread/index.js)
- [Error handling](https://github.com/nairihar/funthreads/blob/master/examples/error_handling/index.js)
- [Work with FileSystem](https://github.com/nairihar/funthreads/blob/master/examples/work_with_file_system/index.js)
## API
### `executeInThread(task, ...params)`
### `runOnThread(task, ...params)`
Execute a function in a thread.
#### Parameters
`- Task (Function)`: The function to be executed in a thread.
*Task (Function)*: The function to be executed in a thread.
*...params (any)*: Additional arguments to be passed to the Task function.
`- ...params (any)`: Additional arguments to be passed to the Task function.
The `runOnThread` 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.
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.
#### Returns
`Promise<any>`: A Promise that resolves with the return value of the callback.
*Promise<any>*: A Promise that resolves with the return value of the callback.
Inside the provided function, you have the flexibility to return any value, including a Promise. The returned value, whether it's a standard value or a Promise, will be passed back to you as the resolved result of the `Promise` returned by the `executeInThread` function.
Inside the provided function, you have the flexibility to return any value, including a Promise. The returned value, whether it's a standard value or a Promise, will be passed back to you as the resolved result of the `Promise` returned by the `runOnThread` function.

@@ -78,16 +80,20 @@ #### Important

```javascript
import executeInThread from 'funthreads';
import { runOnThread } from 'funthreads';
// this will be executed in a dedicated thread
async function task(fileName) {
// Closure doesn't work here
const { writeFile } = require('fs/promises');
await writeFile(fileName, 'Hello from a thread!');
async function task({ name }) {
// closure doesn't work here
const fs = require('fs/promises');
const buffer = await fsPromises.readFile(fileName);
return buffer.toString();
}
const fileName = 'thread.txt';
const params = {
name: 'test.txt',
};
async function read() {
const content = await executeInThread(task, fileName);
const content = await runOnThread(task, params);

@@ -94,0 +100,0 @@ console.log(content);

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