Skip to content

feat: the bounded-memory streaming Part 10 reader, free of the vendored tokenizer - #538

Merged
awatson1978 merged 2 commits into
release/1.0-beta-corefrom
core/streaming-reader
Sep 29, 2026
Merged

awatson1978 merged 2 commits into
release/1.0-beta-corefrom
core/streaming-reader

Conversation

@awatson1978

@awatson1978 awatson1978 commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

This is the big one: the streaming Part 10 reader. It lands under the explicitly granted budget waiver — fromPart10Stream is a single 1,468-line state machine and its test file is another 2,307 lines, so it arrives as one reviewable whole rather than artificially sliced into pieces that wouldn't make sense on their own.

Here's what it does in plain terms. You feed it DICOM bytes in chunks — from a file stream, a network response, or one big buffer — and it emits parse events as the bytes arrive, instead of waiting for the whole file. The File Meta Information (the small header group that says, among other things, which transfer syntax — the byte-encoding scheme — the rest of the file uses) is decoded incrementally, so those events fire before the body has even finished arriving. The body then streams natively for every transfer syntax, including deflate (the compressed variant; it's inflated incrementally via pako rather than buffered whole). Encapsulated pixel data — compressed image frames stored as a list of fragments — streams fragment by fragment, with the Basic Offset Table (the index saying where each frame's fragments start) used to window them.

Memory stays bounded the whole time. Each top-level element's chunk memory is released via consume() once the parse moves past it, and I added a backpressure relay to ReadBufferStream so that a throttled producer can never deadlock against a starved consumer. That relay is the machinery for review finding 12 (the feed loop used to keep buffering after a parse failure), and the K5b gate test covers it.

Two structural decisions from your review are implemented here rather than inherited. First, the inversion you recommended: the vendored dicom-parser tokenizer is gone. The buffered fromPart10 is now a thin wrapper that feeds the streaming reader a single chunk, so the library carries exactly one tokenizer instead of two that have to agree. The shared emit helpers moved into their own module, src/eventStream/emit.js. Second, non-DICOM input now fails loudly on both entry points, with the streaming reader's own error — we no longer delegate to the old tokenizer just to match its error text. If you genuinely have a bare DIMSE-style dataset (one with no File Meta group at all), you force-read it with the allowMissingHeader opt-in that landed in the previous PR. The issue #93 suite now pins that end to end, with its two placeholder tests unskipped.

One test flipped direction because of the inversion. It used to pin a known divergence: for a non-conformant undefined-length UT element, the old tokenizer path silently returned garbage while the stream threw. Both entry points are now the same code, so the test pins the opposite — both fail loudly instead of silently corrupting data.

Here's what using it looks like. Any chunked source works: Node streams, WHATWG ReadableStream, async generators, or a plain buffer as a degenerate single chunk.

import fs from "fs";
import { fromPart10Stream, EventStreamListener } from "./src/eventStream/index.js";

class ElementLogger extends EventStreamListener {
    _baseStartElement(tag, info) {
        console.log("element", tag, info.vr);
    }
    _baseBinaryFragment(chunk) {
        // each encapsulated fragment arrives as its own event;
        // the full PixelData is never held in memory at once
    }
}

await fromPart10Stream(fs.createReadStream("ct-image.dcm"), new ElementLogger());

On testing: the corpus gate runs every vendored test image — all endianness, deflate, and encapsulated permutations — through the raw-bytes-to-events path and compares element for element against the eager reader. That gate is also what surfaced the xs resolution defect fixed in the previous PR.

🤖 Generated with Claude Code

@awatson1978
awatson1978 merged commit 64432b9 into release/1.0-beta-core Sep 29, 2026
8 checks passed
awatson1978 added a commit that referenced this pull request Sep 30, 2026
Kept in-branch rather than as a GitHub issue for now. Finding 13 is
marked fixed (#533) and the fromPart10Stream budget question is recorded
as resolved by the granted waiver (#538).

Co-Authored-By: Claude Fable 5 <[email protected]>
@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