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 🙏

© 2024 – Pkg Stats / Ryan Hefner

hermes-worker

v1.2.4

Published

A simple lib for WebWorker

Downloads

1,381

Readme

Hermes Worker

Lint and test NPM Version License

Hermes Worker is a small library made to simplify WebWorker usage.

In a nutshell, it is a tool which can be used by a web page for computation and which returns the result when it is ready.

Install

Add the package to your project as a dependency:

npm install hermes-worker

Please note that current version does not work with NodeJS (Blob not available)

Example

const HermesWorker = require("hermes-worker");

// Code to be run on worker side
const workerFunction = () => {
    // Expose a function
    hermes.on("add", (a, b) => {
        return a + b;
    });

    // Finish worker initialization
    hermes.ready();
}

// Create a worker
const hermes = new HermesWorker(workerFunction, {});

// Call function "add" and retrieve answer
hermes.call("add", [1, 2]).then(result => {
    console.log(result); // result === 3
});

Features

  • Simple communication between page and worker
  • Instantiate worker from url or function
  • Multiple instances of the same worker
  • Single external scripts import shared between worker instances
  • Serialize data between page and worker
  • Add custom serializers

Usage

Instantiate worker from url

script.js

const HermesWorker = require("hermes-worker");

// Create a worker
const hermes = new HermesWorker("workerFile.js", {});

// Call function "add" and retrieve answer
hermes.call("add", [1, 2]).then(result => {
    console.log(result); // result === 3
});

workerFile.js

// Code to be run in worker side
// Expose a function
hermes.on("add", (a, b) => {
    return a + b;
});

// Finish worker initialization
hermes.ready();

Multiple instances of the same worker

const HermesWorker = require("hermes-worker");

const workerFunction = () => {
    const fibo = (n) => {
        if (n === 0 || n === 1) {
            return 1;
        }
        return fibo(n - 1) + fibo(n - 2);
    }
    hermes.on("fibo", (n) => fibo(n));
    hermes.on("hello", () => {
        console.log(`Hello world from instance ${hermes.config.threadInstance}`)
    })
    hermes.ready();
}

// Create two workers with same code
const hermes = new HermesWorker(workerFunction, {
    threadInstances: 2
});

// Run by first instance
hermes.call("fibo", [12]);

// Run by second instance
hermes.call("fibo", [34]);

hermes.call("hello"); // Output => "Hello world from instance 0"
hermes.call("hello"); // Output => "Hello world from instance 1"

Imports scripts

const HermesWorker = require("hermes-worker");

const workerFunction = () => {
    /** HERE BABYLON IS DEFINED **/
    hermes.on("length", (x, y) => {
        return new BABYLON.Vector2(x, y).length();
    });

    hermes.ready();
};

// Create a worker
const hermes = new HermesWorker(workerFunction, {
    scripts: [
        "https://cdn.babylonjs.com/babylon.js"
    ]
});

hermes.call("length", [1, 0]).then(result => {
    console.log(result); // result === 1
});

Serialization

Data passed between page and worker are serialized to avoid data loss.

List of handled types:

  • Number
  • Number[]
  • String
  • String[]
  • Object
  • Object[]
  • Error
  • TypedArray
  • ImageData
  • ArrayBuffer
  • DataView

You can also create your own serializer

const HermesWorker = require("hermes-worker");

// Caution: Hermes only reads `serialize` and `unserialize` keys
const BabylonSerializer = {
    serialize: (args) => {
        return args.map((arg) => {
            if (arg instanceof BABYLON.Vector2) {
                return {
                    _x: arg.x,
                    _y: arg.y,
                    __type__: "BABYLON.Vector2",
                };
            }
            return arg;
        });
    },
    unserialize: (args) => {
        return args => args.map((arg) => {
            if (arg.__type__ === "BABYLON.Vector2") {
                return new BABYLON.Vector2(arg._x, arg._y);
            }
            return arg;
        });
    },
};

const workerFunction = () => {
    hermes.on("length", (vector) => {
        return vector.length();
    });

    hermes.ready();
};

const hermes = new HermesWorker(workerFunction, {
    scripts: ["https://cdn.babylonjs.com/babylon.js"],
    serializers: [
        BabylonSerializer,
    ],
});

hermes.call("length", [new BABYLON.Vector2(2, 0)]).then(result => {
    console.log(result); // result === 2
});

Changelog

  • v1.0.0: Initial release
  • v1.0.1: Add latest file in npm
  • v1.1.0:
    • Clean Serializer
    • Fix bug transferable
  • v1.2.0:
    • Throw error on unsupported type are send
    • Add possibility to use offscreencanvas