siero-worker
TypeScript icon, indicating that this package has built-in type declarations

0.3.1 • Public • Published

Siero Worker

A managed worker that can be interacted with via Siero.

This package creates a worker designed to receive some APIs and then execute untrusted plugins, but it could be used in various different ways.

Install

npm install --save siero-worker

Usage

import SieroWorker from 'siero-worker';

// Let's create a new worker

const worker = new SieroWorker ();

// Let's pass it a value and register it on the global scope

const MyApp = {
  sum: ( a, b ) => {
    return a + b;
  },
  multiply: ( a, b ) => {
    return a * b;
  }
};

worker.global ( 'MyApp', MyApp );

// Let's now pass it some code to evaluate inside the worker
// For convenience here we are using a simple serializable function
// In real scenarios the code that you may want to evaluate in the worker would probably be bundled

const plugin = async () => {
  const sumResult = await MyApp.sum ( 2, 3 ); // => 5
  const multiplyResult = await MyApp.multiply ( 2, 3 ); // => 6
};

worker.eval ( `(${plugin.toString ()})()` );

// Let's now terminate the worker

worker.terminate ();

License

MIT © Fabio Spampinato

Package Sidebar

Install

npm i siero-worker

Weekly Downloads

11

Version

0.3.1

License

none

Unpacked Size

12.3 kB

Total Files

15

Last publish

Collaborators

  • fabiospampinato