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

whatwg-worker

v1.0.2

Published

Consistent Web Workers in browser and Node.

Downloads

18

Readme

In Node, it's a web-compatible Worker implementation atop Node's worker_threads.

In the browser (and when bundled for the browser), it's simply an alias of Worker.

Features

Here's how this is different from worker_threads:

  • makes Worker code compatible across browser, Node and Deno
  • only supports Module Workers ({type:'module'})
  • uses DOM-style events (Event.data, Event.type, MessageEvent, etc)
  • supports event handler properties (worker.onmessage=..)
  • Worker() accepts a URL, Blob URL or Data URL (via loaders)
  • emulates browser-style [WorkerGlobalScope] within the worker
  • Worker thread can import data:, https:, blob:, file: files just fine
  • Worker constructed also supports those too new Worker('https://...')

Usage Example

In its simplest form:

import Worker from 'whatwg-worker'

const worker = new Worker('data:text/javascript,postMessage("hello")')
worker.onmessage = e => console.log(e.data)  // "hello"
import Worker from 'whatwg-worker'

const url = new URL('./worker.js', import.meta.url)
const worker = new Worker(url)

worker.addEventListener('message', e => {
  console.log(e.data)  // "hiya!"
})

worker.postMessage('hello')
addEventListener('message', e => {
  if (e.data === 'hello') {
    postMessage('hiya!')
  }
})

👉 Notice how new URL('./worker.js', import.meta.url) is used above to load the worker relative to the current module instead of the application base URL. Without this, Worker URLs are relative to a document's URL, which in Node.js is interpreted to be process.cwd().

Support for this pattern in build tools and test frameworks is still limited. We are working on growing this.

Module Workers

Module Workers are supported in Node 12.8+ using this plugin, leveraging Node's native ES Modules support. In the browser, they can be used natively in Chrome 80+, or in all browsers via [worker-plugin] or [rollup-plugin-off-main-thread]. As with classic workers, there is no difference in usage between Node and the browser:

import Worker from 'whatwg-worker'

const worker = new Worker(
  new URL('./worker.js', import.meta.url),
  { type: 'module' }
)
worker.addEventListener('message', e => {
  console.log(e.data)  // "200 OK"
})
worker.postMessage('https://httpstat.us/200')
addEventListener('message', async e => {
  const url = e.data
  const res = await fetch(url)
  const text = await res.text()
  postMessage(text)
})

Data URLs

Instantiating Worker using a Data URL is supported in both module and classic workers:

import Worker from 'whatwg-worker'

const worker = new Worker(`data:application/javascript,postMessage(42)`)
worker.addEventListener('message', e => {
  console.log(e.data)  // 42
})

Blob URLs

Instantiating Worker using a Blob URL is supported

import Worker from 'whatwg-worker'

const code = 'import fs from "node:fs"'
const blob = new Blob([code], { type: 'text/javascript' })
const worker = new Worker(URL.createObjectURL(blob))

HTTP loader supported.

Worker gets added https- loader support via --loader flag

import Worker from 'whatwg-worker'

const code = 'import xyz from "https://example.com/main.js"'
const blob = new Blob([code], { type: 'text/javascript' })
const worker = new Worker(URL.createObjectURL(blob))
import Worker from 'whatwg-worker'

const url = 'https://example.com/main.js'
const worker = new Worker(url)

Worker global scope

Each time when creating a new Worker it will get assigned some new global variables including

  • name
  • Worker (to create worker within a worker)
  • self
  • postMessage
  • and globalThis will be inherit EventTarget (addEventListener, remove and dispatch)