npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

string-worker

v0.0.1

Published

Makes creating WebWorkers easier

Readme

StringWorker

Online Use | 中文 | Version Log | Feedback bug | Gitee

0. Introduction

StringWorker is committed to helping developers access WebWorker at low cost. In webpack and rollup projects, js or ts modules are introduced as the internal code of the worker, and there is no need to maintain the internal code of the worker separately.

0.2 Features

  1. Use the code string to initialize the worker without using a third-party url
  2. Support Promise to get the message returned by the worker
  3. Support webpack, rollup import loader use (under development...)

1. Quick use

1.0 install

1.0.1 npm install

npm i string-worker
import StringWorker from 'string-worker';

1.0.2 cdn

<script src="https://cdn.jsdelivr.net/npm/string-worker/string-worker.min.js"></script>
<script>
  window.StringWorker;
</script>

1.1 Initializing with strings

1.1.1 Using raw data

const worker = new StringWorker(/* javascript*/`
  globalThis.addEventListener('message', function (e) {
    var data = e.data;

    // do something...
    console.log('Worker Receive: ', data);

    globalThis.postMessage('Worker Send: '+data);
  }, false);
`);

worker.onMessage(data => {
  console.log(data);
});

worker.postMessage('Hello World');

1.1.2 Use promise to get worker's return value

const worker = new StringWorker(/* javascript*/`
  globalThis.addEventListener('message', function (e) {
    var data = e.data;
    console.log('Worker Receive: ', data);

    // do something...
    var message = 'Worker Send: '+data.message;

    globalThis.postMessage({
      message: message,
      id: data.id
    });
  }, false);
`);

let id = 0;
worker.postMessage({
  message: 'Hello World',
  id: `msg_${id++}`, // need to pass in a unique id to match the message
}).then(d => {
  console.log('Worker Return: ', d);
});

1.2. Using function initialization

1.2.1 Using js

const worker = new StringWorker({
  setup () { // not required
    return {msg: 'hello world'};
  },
  onmessage (message, data) { // The second parameter is the return value of setup
    return {receive: message.send + data.msg};
  }
});

worker.postMessage({send: 'Hello'}).then(d => {
  console.log(d);
});

1.2.2 Use ts to pass in generics to declare types

When using ts references, generics can be passed in to standardize setup return values ​​and message types

const worker = new StringWorker<
  {msg: string}, // setup return value
  {send: string}, // type of send
  {receive: string} // return type
>({
  setup () { // not required
    return {msg: 'hello world'};
  },
  onmessage (message, data) { // The second parameter is the return value of setup
    return {receive: message.send + data.msg};
  }
});

worker.postMessage({send: 'Hello'}).then(d => {
  console.log(d);
});

2 Use string-worker-loader (in development...)

This part is currently under development. At present, developers can write an independent packaging module to package the worker code into a file, and then import the file as a StringWorker construction parameter to implement the loader function.

2.1 webpack-loader

2.2 rollup-loader

2.3 esbuild-loader