Skip to content

Use platform-agnostic worker interface instead of web-worker dependency#3153

Merged
joao-boechat merged 4 commits into
mainfrom
joaoboechat/remove-web-worker-dependency-alt
Apr 23, 2026
Merged

Use platform-agnostic worker interface instead of web-worker dependency#3153
joao-boechat merged 4 commits into
mainfrom
joaoboechat/remove-web-worker-dependency-alt

Conversation

@joao-boechat
Copy link
Copy Markdown
Contributor

@joao-boechat joao-boechat commented Apr 23, 2026

Summary

Replaces the web-worker npm polyfill with a platform-specific worker adapter layer. Each platform (browser, Node.js) now provides its own IWorkerHost and IWorkerSelf implementation, eliminating the third-party dependency while giving the shared code a single, clean interface for worker communication.

What changed

  • Removed the web-worker optional dependency and the globalThis.Worker polyfill in node.ts
  • Removed setWorkerType / workerType — no longer needed since each adapter handles worker creation internally
  • Added src/workers/adapters/ with IWorkerHost (main→worker) and IWorkerSelf (worker→main) interfaces
  • Added BrowserWorkerHost and NodeWorkerHost implementations
  • Added src/browser.ts entry point (mirrors node.ts)
  • Updated package.json exports to use browser.ts for browser builds
  • Worker scripts no longer call addEventListener("message", ...) or self.postMessage(...) directly — they use the WorkerSelf global

Architecture

Before: polyfill the browser API into Node

  MAIN THREAD                              WORKER THREAD
  ───────────                              ─────────────

  ╔══════════════╗  ╔══════════════╗
  ║  browser.js  ║  ║   node.js    ║
  ║              ║  ║              ║
  ║ uses native  ║  ║ polyfills    ║
  ║ Worker API   ║  ║ via web-     ║
  ║              ║  ║ worker pkg   ║
  ╚══════╤═══════╝  ╚══════╤═══════╝
         │                 │
         └────────┬────────┘
                  │
                  ▼
  ┌───────────────────────┐    ┌───────────────────────┐
  │   Browser Worker API  │    │   Browser Worker API  │
  │                       │    │                       │
  │  · new Worker(url)    │    │  · self.postMessage() │
  │  · addEventListener() │    │  · addEventListener() │
  │  · postMessage()      │    │                       │
  └───────────┬───────────┘    └───────────┬───────────┘
              │                            │
              ▼                            ▼
      ┌───────────────┐           ┌───────────────┐
      │  Shared code  │◄─messages──│  Shared code  │
      │  (main.ts)    │──messages─►│  (worker.ts)  │
      └───────────────┘           └───────────────┘
  • Node polyfilled the browser Worker global using web-worker
  • All code used browser-native APIs (addEventListener, self.postMessage)
  • workerType toggled between "classic" and "module"

After: each platform provides its own adapter

  MAIN THREAD                              WORKER THREAD
  ───────────                              ─────────────

  ╔══════════════╗  ╔══════════════╗
  ║  browser.ts  ║  ║   node.ts    ║
  ╚══════╤═══════╝  ╚══════╤═══════╝
         │                 │
         ▼                 ▼
  ┌────────────┐    ┌────────────┐         ┌──────────────────┐
  │ Browser    │    │ Node       │ boot-   │ WorkerSelf       │
  │ WorkerHost │    │ WorkerHost │ straps  │                  │
  │            │    │            │────────►│ blob URL (browser)│
  │ wraps      │    │ wraps      │         │ data URL (node)  │
  │ Worker API │    │ worker_    │         │                  │
  │            │    │ threads    │         └────────┬─────────┘
  └─────┬──────┘    └─────┬──────┘                  │
        │                 │                         │
        └────────┬────────┘                         │
                 │                                  │
                 ▼                                  ▼
      ╔═════════════════════╗            ╔═════════════════════╗
      ║   IWorkerHost       ║            ║   IWorkerSelf       ║
      ║                     ║            ║                     ║
      ║  · postMessage()    ║            ║  · postMessage()    ║
      ║  · onMessage()      ║            ║  · onMessage()      ║
      ║  · terminate()      ║            ║                     ║
      ╚══════════╤══════════╝            ╚══════════╤══════════╝
                 │                                  │
                 ▼                                  ▼
         ┌───────────────┐              ┌───────────────┐
         │  Shared code  │◄──messages───│  Shared code  │
         │  (main.ts)    │───messages──►│  (worker.ts)  │
         └───────────────┘              └───────────────┘
  • No polyfilling — each platform uses its native worker API behind the adapter
  • Shared code only touches IWorkerHost and IWorkerSelf interfaces
  • WorkerSelf is bootstrapped into the worker thread via blob URL (browser) or data URL (Node) before the worker script runs

@joao-boechat joao-boechat changed the title Joaoboechat/remove web worker dependency alt Use shared worker interface among browser and node Apr 23, 2026
@joao-boechat joao-boechat changed the title Use shared worker interface among browser and node Use platform-agnostic worker interface instead of web-worker dependency Apr 23, 2026
@joao-boechat joao-boechat marked this pull request as ready for review April 23, 2026 21:02
Comment thread source/npm/qsharp/src/workers/main.ts
Copy link
Copy Markdown
Member

@minestarks minestarks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Please test thoroughly (including maybe running multiplatform tests?) before merging

@joao-boechat joao-boechat added this pull request to the merge queue Apr 23, 2026
Merged via the queue into main with commit f6f32ea Apr 23, 2026
24 of 26 checks passed
@joao-boechat joao-boechat deleted the joaoboechat/remove-web-worker-dependency-alt branch April 23, 2026 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants