Skip to content

feat(media): the DicomEventStream media factories (review finding 14) - #587

Merged
awatson1978 merged 1 commit into
release/1.0-beta-mediafrom
media/facade-factories
Oct 3, 2026
Merged

awatson1978 merged 1 commit into
release/1.0-beta-mediafrom
media/facade-factories

Conversation

@awatson1978

Copy link
Copy Markdown
Collaborator

This gives the DicomEventStream facade its media factories: fromImage, fromPdf, fromVideo, fromVideoStream, toPdf, and toVideo. With these merged, the facade speaks every bulk-data dialect on this staging branch in both directions — decoded pixels, a PDF, or an MP4 go in and Part 10 bytes come out; a parsed instance goes in and the embedded PDF or the verbatim video stream comes back out. The factory bodies are ports from v2's src/eventStream/api.js, but the imports now come from the workspace packages (encapsulatePdf/extractEncapsulatedPdf from @dcmjs-org/pdfs; buildImageDataset/createVideoEventSource/extractEncapsulatedVideo from @dcmjs-org/video), so the root facade composes the packages rather than duplicating them. fromFhir/toFhir are deliberately absent — they belong to the FHIR wave.

One fix rides in the port: review finding 14. v2's video factories called createVideoEventSource(...) eagerly at factory time, and the only await on that promise sat inside the run thunk. So DicomEventStream.fromVideo(bytesThatAreNotAnMp4) followed by any unrelated await produced an unhandled promise rejection — under Node's default --unhandled-rejections=throw that terminates the process before the caller ever gets a chance to catch anything. The ported code creates the source promise lazily inside the run thunk instead, cached after the first consumption so minted UIDs stay stable across re-runs of the same stream, exactly as before. The error now surfaces from .process() (or whichever sink runs first), which is the documented contract. Same fix in fromVideoStream.

I did this red-green: the regression test went in first, against the verbatim eager port. It registers a process.on("unhandledRejection") listener, calls fromVideo(garbage), performs an unrelated await, and asserts the captured-rejections array is empty and that the error instead rejects the sink call. Against the eager port both finding-14 tests failed with the leaked parseMp4Info: input is not an MP4 file (missing ftyp box) rejection:

✕ finding 14: fromVideo(garbage) does not throw an unhandled rejection before .process() (27 ms)
✕ finding 14: fromVideoStream(bad reader) is just as lazy (22 ms)
Tests: 2 failed, 4 passed, 6 total

After the lazy fix, all six pass and the full suite stays green.

To be plain about provenance: the six factory bodies and their doc comments are verbatim v2 except for the two lazy video thunks (the fix) and the import block, and test/eventStream/fromImage.test.js is the v2 file ported unchanged — it synthesizes its pixels in code, so no fixture-path rework turned out to be needed. Hand-written: the lazy thunks, the root package.json workspace deps (@dcmjs-org/pdfs, @dcmjs-org/video), and test/eventStream/mediaFactories.test.js — the finding-14 regression pair plus facade round trips in both directions for PDF (byte-identical through the writer's odd-length OB pad and back) and video (the synthetic makeTinyMp4 recovered byte-identically via toVideo), and the corrective toPdf-on-a-non-PDF error.

// in: an MP4 → a Video Photographic Image instance
const bytes = await DicomEventStream.fromVideo(mp4, { PatientName: "FOX^JANE" }).toPart10();

// out: the same bytes → the verbatim MP4 again
const { bytes: mp4Back } = await DicomEventStream.fromPart10(bytes).toVideo();

10 new tests (6 + 4 ported). Full suite on this branch: 945 passing (957 total, 12 skipped), build green.

🤖 Generated with Claude Code

Port fromImage, fromPdf, fromVideo, fromVideoStream, toPdf, and toVideo
from v2's src/eventStream/api.js, importing from @dcmjs-org/pdfs and
@dcmjs-org/video. The video factories create their event source lazily
on first consumption (cached across re-runs) instead of eagerly at
factory time, so a bad input can no longer raise an unhandled promise
rejection before anyone calls .process() — review finding 14.

Co-Authored-By: Claude Fable 5 <[email protected]>
@awatson1978
awatson1978 merged commit d2d0a19 into release/1.0-beta-media Oct 3, 2026
10 checks passed
@awatson1978
awatson1978 deleted the media/facade-factories branch October 3, 2026 00:36
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