Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
react-hooks-worker
Advanced tools
React custom hooks for web workers.
React Hooks API is promising. Web Workers API is promising.
This is an experimental library to provide an easy way to call web workers. It's more or less for fun, but feedbacks are welcome to make this for production.
npm install react-hooks-worker
import React from 'react';
import { useWorker } from 'react-hooks-worker';
const calcFib = x => {
const fib = i => (i <= 1 ? i : fib(i - 1) + fib(i - 2));
return fib(x);
};
const CalcFib: React.FC<{ count: number }> = ({ count }) => {
const { result, error } = useWorker(calcFib, count);
if (error) return <div>Error: {error}</div>;
return <div>Result: {result}</div>;
};
const App = () => (
<div>
<CalcFib count={5} />
</div>
);
Using inline function or string representation has a potential risk of including malicious code. Please pay attention to protect from XSS.
If the function
passed into useWorker(function)
has external dependencies, then function
must be in its own file and directly import those node modules.
// WorkerFibFile.js
import SomeExternalModule from 'external-module-example'
// calcFib has an external dependency: `SomeExternalModule`
export default const calcFib = (x) => {
SomeExternalModule();
const fib = i => (i <= 1 ? i : fib(i - 1) + fib(i - 2));
return fib(x);
};
// App.js
import React from 'react';
import { useWorker } from 'react-hooks-worker';
import FibWorker from './WorkerFibFile';
const App = () => (
const { result, error }= useWorker(FibWorker, 5);
return <div>{result}</div>
);
Parcel allow your Web Worker script to be automatically bundled. To do this, just pass an instance of the Web Worker instead of the url:
const myWorker = new Worker('./slow_fib.js');
const { result, error } = useWorker(myWorker, count);
The examples folder contains working examples. You can run one of them with
PORT=8080 npm run examples:minimal
and open http://localhost:8080 in your web browser.
You can also try them in codesandbox.io: 01 02 03 04 05
[0.7.0] - 2019-07-13
FAQs
React custom hooks for web workers
The npm package react-hooks-worker receives a total of 486 weekly downloads. As such, react-hooks-worker popularity was classified as not popular.
We found that react-hooks-worker demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.