Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

webworker-typed

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webworker-typed

Typed Web Workers with Async/Await

  • 1.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
87
increased by16%
Maintainers
0
Weekly downloads
 
Created
Source

webworker-typed

Dead simple type-safe Web Workers.

Installation

npm install webworker-typed

Usage

Create a worker file and export the functions you want to expose:

// my-worker.ts
import { exportWorker } from 'webworker-typed';

function foo() { return 'bar'; }

async function asyncFoo() { return 'bar'; }

export default exportWorker({
  foo,
  asyncFoo,
  inline: () => 'bar',
});

From your main thread, import the worker and call the functions:

// main.ts
import { importWorker } from 'webworker-typed';
import type MyWorker from './my-worker.ts';

const untypedWorker = new Worker(new URL('./my-worker.ts', import.meta.url)); // example with webpack
const worker = importWorker<typeof MyWorker>(untypedWorker);

(async () => {
  // all imported methods are async
  console.assert(await worker.foo() === 'bar');
  console.assert(await worker.asyncFoo() === 'bar');
  console.assert(await worker.inline() === 'bar');
})();

License

This project is licensed under the MIT License.

FAQs

Package last updated on 14 Aug 2024

Did you know?

Socket

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.

Install

Related posts

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