Skip to content

feat(html): layered API with a plain tree, sink and op log, from strings, bytes, iterables or streams - #2

Merged
natemoo-re merged 2 commits into
mainfrom
natemoo-re/html-api
Dec 4, 2026
Merged

natemoo-re merged 2 commits into
mainfrom
natemoo-re/html-api

Conversation

@natemoo-re

Copy link
Copy Markdown
Member

Today the only way to get something readable out of @bomb.sh/html is to parse into an op log and then replay() it into a 13-method TreeSink. This PR adds a front door that returns a plain tree, keeps the sink and the op log as lower layers, and lets every layer take streams and iterables. It should land before the "[ci] release" PR, which would otherwise publish the old API as 0.1.0.

API

Each layer has parse(input, options?) and parseFragment(input, options). The module you import decides what comes back:

Import Returns For
@bomb.sh/html a plain tree: Document, DocumentFragment, Element, … with a type tag most callers
@bomb.sh/html/sink the root your TreeSink<N> builds DOMs like @clack/ui
@bomb.sh/html/log an OpLog, plus replay() streaming, caching, tests
parse(html);                                   // Document
parseFragment(html, { context: 'div' });       // DocumentFragment
parseFragment(response.body, { context: 'td' }); // Promise<DocumentFragment>
sink.parseFragment(html, { context, sink, limits });
log.replay(log.parse(bytes), mySink);
  • Input. Strings, UTF-8 bytes, and sync iterables of either return synchronously. Async iterables and ReadableStream return a promise.
  • Chunks. A surrogate pair or UTF-8 sequence split across chunks stays one character. A UTF-8 byte order mark is dropped from byte input. A string's leading U+FEFF is text, and stays.
  • Streaming is phase 1. Chunks are gathered and parsed once the input ends. Incremental parsing needs engine work, and the docs reserve the right for an async sink parse to call the sink before an error.
  • Options bag. context and sink live in the options object because the @bomb.sh/tools lint rule max-params allows two parameters.
  • Errors. Bad input throws InputError, because of no-generic-error.
  • Removed from the public surface. fastPath (the tests use it through src/engine.ts) and parseBytes (folded into Uint8Array input).
  • Atomicity. With sync input, a LimitError or ResourceError is thrown before the sink receives its first call. @clack/ui relies on this so a failed innerHTML set leaves the document unchanged.

Structure

  • refactor(html): move the engine host to src/engine.ts is a pure rename with callers repointed. It is green on its own, and keeps blame for the engine host.
  • The second commit adds the layers. src/engine.ts is still the only module that talks to the wasm. input.ts, tree.ts, sink.ts, log.ts and index.ts are thin layers on top of it.
  • The metrics harness now measures src/engine.ts. It falls back to src/index.ts for refs from before the split, so --compare main still works.

Verification

  • pnpm lint, pnpm format:check, pnpm check:engine and pnpm build pass. The build emits index, sink and log.
  • pnpm test: 11,060 passed and 14 expected failures. Every WPT tree-construction case now also runs through the public parse/parseFragment tree, and all 3,891 match the reference replay. test/api.test.ts covers input kinds, errors and layers.
  • node scripts/metrics.ts --compare main --suite fragment --samples 16: fragment e2e fast +0.1% [-1.4%, +1.5%]. No engine change, as expected.

Not in this PR

  • @clack/ui still uses its own workspace copy of the old API. It moves to @bomb.sh/html/sink when it switches to the published package.
  • A Zena-facing API (engine/index.zena) waits on whether Zena consumers can link the engine's .wat runtime.
  • The default tree has no parent pointers yet. Adding them later breaks no caller.

@pkg-pr-new

pkg-pr-new Bot commented Dec 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/bombshell-dev/html/@bomb.sh/html@2

commit: 3d55fc0

@natemoo-re
natemoo-re merged commit 6ad6fe4 into main Dec 4, 2026
7 checks passed
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