Skip to content

Add browser-compatible Worker polyfill and WPT coverage - #8

Draft
matthargett wants to merge 37 commits into
napi-v7from
feature/web-worker
Draft

Add browser-compatible Worker polyfill and WPT coverage#8
matthargett wants to merge 37 commits into
napi-v7from
feature/web-worker

Conversation

@matthargett

@matthargett matthargett commented Jul 21, 2026

Copy link
Copy Markdown
Member

Dependency and stack

This is the Worker follow-up to BabylonJS/JsRuntimeHost#189. It is built on the refreshed napi-v7 head (231fa40), which incorporates the issue-7 Node-API work, the latest audited CTS updates, and the deterministic JavaScriptCore follow-up collection needed by the CTS finalizer contract. The refreshed base is merged as a parent so the existing Worker review history remains intact; the resulting tree exactly matches the locally tested restack.

The Worker branch composes the independently reviewable browser-polyfill work while those PRs are pending:

Worker initializes those targets in browser-compatible dependency order. It no longer embeds reduced private copies of Streams, Blob.stream, Response, decompression, or IndexedDB, and no longer links zlib itself.

Browser-compatible Worker surface

  • One native thread, AppRuntime, engine realm, and event loop per dedicated Worker.
  • Worker, EventTarget, Event, MessageEvent, ErrorEvent, DOMException, handler attributes, postMessage, and terminate.
  • DedicatedWorkerGlobalScope with self, location, name, navigator, close, synchronous importScripts, timers, fetch/XHR, URL, Blob/File, text codecs, WebSocket, performance, abort APIs, Streams, CompressionStream, and IndexedDB.
  • Structured clone for cyclic/shared graphs, special numbers, BigInt, Date, RegExp, Map, Set, Error, ArrayBuffer/DataView, and typed arrays.
  • Transfer-list validation and N-API v7 ArrayBuffer detachment, including duplicate, invalid, and already-detached DataCloneError paths.
  • Root-confined app:/// and relative scripts/assets, explicit file://, percent-encoded data:, and a thread-safe packaged-asset resolver.
  • Worker-relative fetch inputs resolved against the Worker location.

System JavaScriptCore uses its execution-time-limit ABI to interrupt top-level infinite evaluation. No JSC binary is downloaded or vendored: the Linux lanes install the OS libjavascriptcoregtk-4.1-dev package. QuickJS remains the additional race/conformance test engine.

Visualization startup and playback integration

The reduced integration fixture is pinned to rebeckerspecialties/webapp@3765b13, specifically GithubPortfolio.worker.ts, its parent interop, and IndexedDBCache.

The source trace found two blockers beyond the base Worker interface:

  1. pipelineCreate synchronously constructs the IndexedDB cache, so a missing indexedDB stops startup before playback can fall back to live data.
  2. Prerecorded playback fetches a Worker-relative ${owner}.${repo}.gz, then runs Blob.stream().pipeThrough(new DecompressionStream("gzip")), constructs a Response, parses the cache, and writes the records in one IndexedDB transaction.

The fixture constructs a named { type: "module" } Worker, queues pipelineCreate and subscribe while the bundle is evaluating, hydrates rebeckerLoaderCacheStore, and plays three 128-item streams. It verifies relative fetch, gzip rejection behavior, transaction completion, Date/Map/Set/cyclic/shared-alias clone fidelity, 384 updated items, abort reasons, and browser-style constructor feature detection.

The deployed GitHub Pages origin itself was not launched in this environment, so this is a source-pinned, app-derived integration test rather than a claim about an end-to-end XR render. The tested contract reaches pipelineCreated, emits the three playback streams, and preserves the data required by the XR consumer.

Browser regression hardening

The regression fixtures carry their source links and guard known browser implementation failures:

The JSC sanitizer pass also exposed a per-stream implementation cost in Blob.stream(): capturing node-addon-api callbacks attach hidden callback-data properties to each generated function, causing two extra wrappers and JSC Function structure transitions per stream. The standalone Blob branch now uses a shared internal stream-source prototype, so its native pull/cancel callbacks are created once per realm instead of once per stream; the same commit is composed here and keeps the existing BYOB, cancellation, and lifetime tests.

The restack also caught an integration-specific variant of the same “polyfill replaces a host primitive” antipattern: Worker event glue replaced IndexedDB’s existing DOMException, breaking DataCloneError instanceof DOMException. Worker now preserves a host constructor, with a dedicated native regression test.

Tests

The vendored Worker subset pins web-platform-tests/wpt@4809b72, its license, and testharness.js.

Fresh local QuickJS results after the restack:

  • Worker host/composition + WPT/app-derived integration: 2/2 pass.
  • Streams/Compression/IndexedDB/Fetch initializer and host-preservation tests: 6/6 pass.
  • QuickJS Node-API throw-status contract: 1/1 pass.
  • Combined native Worker/browser-runtime regression filter: 9/9 pass.
  • Focused Headers/Response/Streams/Compression/Blob/IndexedDB JavaScript regressions: 81/81 pass.
  • The target builds cleanly with the full Worker dependency graph.
  • GitHub Worker matrix: 3/3 pass (system JSC, JSC ASan/UBSan, and QuickJS TSan).

The general JavaScript suite was also started, but this container’s external XHR cases block on network access before reaching the remaining suites; the focused pass above runs the complete changed-feature set. GitHub Actions supplies the system-JSC, ASan/UBSan, and QuickJS-TSan gates.

Deliberate first-cut boundaries

  • type: "module" accepts self-contained script-compatible bundles; imports/exports must be flattened because public JSC has no module-loader hook.
  • IndexedDB is complete enough for object stores, indexes, cursors, key ranges, upgrades, rollback, and storage clone, but remains per-realm and in-memory. Durable or cross-realm storage is a separate backend concern.
  • The existing native fetch transport is still not the complete Fetch standard.
  • unhandledrejection synthesis, SharedArrayBuffer, MessagePort transfer, Blob object URLs, nested Workers, and full module-graph loading are not included yet.
  • The legacy JSI adapter has no v7 detachable-ArrayBuffer ABI, so Worker remains unavailable there.
  • Tight-loop interruption is implemented for system JSC first; other engines stop cooperatively between dispatches until their native interrupt hooks are connected.

Exercise the deployed webapp's module-worker startup contract with an app-derived integration fixture, provide the cache-oriented in-memory IndexedDB subset needed by pipelineCreate, accept WHATWG-normalized app:/ worker URLs, and preserve actionable startup errors across engines.

Also separate graceful WorkerGlobalScope.close() from forced parent termination so same-task messages and errors remain observable.
Vendor the ES5 web-streams-polyfill 4.3.0 ponyfill and expose an idempotent Streams initializer that preserves constructors supplied by the selected JavaScript engine.

Cover readable, writable, transform, BYOB, error, tee, subclassing, and host-constructor behavior with focused ports from WPT plus Firefox and Chromium regression tests. Validate the implementation on JavaScriptCore under ASan/UBSan and QuickJS Release.
Add optional CompressionStream and DecompressionStream polyfills for gzip, deflate, and raw deflate on top of WHATWG Streams. Reuse one native output buffer per active codec, borrow input views only synchronously, and defer JavaScript enqueue callbacks until zlib has finished consuming each input.

Cover constructor and BufferSource behavior, split and empty chunks, large flushes, corrupt/truncated/trailing input, reentrant input mutation, and repeated stream teardown with focused WPT and browser-engine regression tests. Preserve host constructors and use platform zlib where available with a pinned fallback.
Implement iterable BlobPart conversion, MIME and endings normalization, zero-copy Blob composition and slicing, and lazy 64 KiB byte streams. Delegate File streaming and slicing to its backing Blob while preserving browser class identity.\n\nUse immutable shared segments so nested Blob and slice construction do not duplicate payload bytes. Add focused WPT, WebKit, and Firefox regression coverage for constructor ordering, iterator closure, UTF-8 decoding, BYOB reads, cancellation, lifetime safety, and large nested streams.
Replace the ad hoc buffered fetch response object with standard-shaped Headers and Response classes backed by ReadableStream bodies. Preserve host constructors, normalize and validate header values, enforce single-use bodies, and expose native responses with one transport-to-JavaScript copy.

Reuse normalized header views between mutations, compact header storage in place, retain stream chunks only until consumption, and allocate a contiguous body result at most once when required.

Add focused WPT, WebKit, Firefox, and Chromium regression coverage plus JavaScriptCore/QuickJS and initialization tests.
Close byte streams for zero-length BufferSource bodies without enqueueing an invalid empty chunk. Preserve the non-null body and bodyUsed semantics, and add focused regression coverage alongside the WPT empty-response cases.
Resolve percent-encoded and base64 data URLs without entering the network transport. Use a validating WHATWG-style forgiving-base64 decoder, strip URL fragments from payloads, and preserve response metadata through the standard Response path.

Decode into pre-sized native storage and copy the completed body once into JavaScript-owned memory. Cover representative WPT data URL and forgiving-base64 vectors, plus Chromium whitespace cases.
Feed percent-decoded bytes directly into validating and decoding passes for Base64 data URLs. Allocate the final native payload at its exact size, avoiding the previous percent-decoded staging vector and any vector growth before the single JavaScript ownership copy.
Replace a partial or null host Streams surface as a complete constructor suite so stream products retain compatible instanceof relationships. Preserve a complete suite on repeated initialization.\n\nHarden the native initialization test so N-API failures complete the test promise instead of hanging, and cover partial host replacement plus null handling and cross-constructor compatibility.
Use ReadableByteStreamController.byobRequest views directly and respond with the produced byte count instead of allocating and enqueueing a 64 KiB intermediate chunk. Keep the existing bounded allocation path for default readers.\n\nAdd WPT-derived coverage for small offset BYOB views that cross immutable Blob segment boundaries.
Observe standard ReadableStream consumption paths with weak per-stream state instead of depending on web-streams-polyfill's private _disturbed field. Instrument stream readers and piping once during Fetch initialization, preserve stream identity, and cache the initialized Fetch implementation so repeated initialization does not stack wrappers or replace its internal state.

Treat Headers and Response as an implementation pair when either host global is missing or null. Harden native initialization tests against unhandled N-API failures and add WPT-derived coverage for reads, cancellation, piping, and streams disturbed before Response construction.
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.

1 participant