Skip to content

feat: add the event-stream contract and its reference collector - #530

Merged
awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/event-contract
Sep 29, 2026
Merged

awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/event-contract

Conversation

@awatson1978

@awatson1978 awatson1978 commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

This is the first engine slice from RELEASE_PLAN.md (section 6, step 5). It defines the event-stream contract — the shared vocabulary every reader, writer, filter, and anonymizer in the rewrite will speak — plus a reference consumer to validate future generators against.

I opened it as a draft on purpose. Section 5 of the plan says new engine code needs a second set of eyes, so nothing here self-merges.

First, the idea, since this PR is where it starts. An "event stream" means a parser doesn't build one big tree and hand it back at the end. Instead, it calls a listener once per thing it encounters: startElement, value, endElement, startSequence, and so on. That callback vocabulary is the contract. Everything later in the refactor — byte readers, DICOMweb JSON sources, writers, anonymizers — produces or consumes these same calls.

Three files land in src/eventStream/:

  • EventStreamListener.js defines the vocabulary itself (EVENT_STREAM_VOCABULARY, fifteen callbacks covering dataset lifecycle, structure, values, and binary data) and the listener base class. Delivery is push: producers call the listener's methods synchronously, which keeps the hot path free of per-event allocations. Backpressure — letting a slow consumer pause a fast producer — happens out of band instead: producers await listener.awaitDrain() only at defined checkpoints (top-level element boundaries and binary fragments). Filters are first class; each one can wrap any vocabulary method in a next-style chain.
  • The same file exports mergeFragmentsPerBotWindow, the fragment-windowing helper. It merges encapsulated pixel-data fragments into one buffer per Basic Offset Table window, so listeners that rebuild a dataset can match the eager reader's one-entry-per-frame shape.
  • CollectorListener.js is the reference consumer. It rebuilds a tag-keyed { meta, dict } tree from the events — deliberately the same raw { vr, Value } shape that DicomMessage.readFile produces, so later generators can be validated by deep-comparing their collected output against the existing parse.

Here's the contract in motion, condensed from the tests — events in, tree out:

import { CollectorListener } from "dcmjs/src/eventStream";

const listener = new CollectorListener();

listener.startDataSet({ transferSyntaxUID: "1.2.840.10008.1.2.1" });
listener.startElement("00100010", { vr: "PN" });
listener.value("Doe^Jane");
listener.endElement();
listener.startSequence("00081110", { vr: "SQ" });
listener.startItem({});
listener.startElement("00081150", { vr: "UI" });
listener.value("1.2.3");
listener.endElement();
listener.endItem();
listener.endSequence();
listener.endDataSet();

listener.result;
// → { meta: {},
//     dict: { "00100010": { vr: "PN", Value: ["Doe^Jane"] },
//             "00081110": { vr: "SQ",
//                           Value: [{ "00081150": { vr: "UI", Value: ["1.2.3"] } }] } } }

The tests (test/eventStream/EventStreamListener.test.js and CollectorListener.test.js) drive listeners through hand-written event sequences like that one and assert the rebuilt tree, filter chaining, drain behavior, and fragment handling.

Two things are deliberately not here. The index.js barrel re-exports only what has landed. And the async pull adapter is excluded until review finding 13 is fixed in its own PR — the finding is that breaking out of the iterable leaks the producer. fromDataSet, the first real generator, follows as the next slice with its tests.

One more note: I triaged the routing of all 33 review findings against the rewrite branch tonight; it's drafted in FINDINGS-CHECKLIST.md in the workspace, ready for the tracking issue.

🤖 Generated with Claude Code

@awatson1978
awatson1978 marked this pull request as ready for review September 29, 2026 04:47
@awatson1978
awatson1978 merged commit 7e04238 into release/1.0-beta-core Sep 29, 2026
8 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.0.0-beta.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant