Skip to content

fix(core): let the sequence writer run with no eager engine loaded (dicomdir wave) - #580

Merged
awatson1978 merged 1 commit into
release/1.0-beta-dicomdirfrom
dicomdir/standalone-sq-write
Oct 2, 2026
Merged

awatson1978 merged 1 commit into
release/1.0-beta-dicomdirfrom
dicomdir/standalone-sq-write

Conversation

@awatson1978

Copy link
Copy Markdown
Collaborator

This is a follow-up to #573, fixing a gap I found while verifying the assembly PR's worked example. The claim over there is that @dcmjs-org/dicomdir runs on @dcmjs-org/core and nothing else — and the imports say so — but when I actually ran writeDicomDir in a process that had never loaded @dcmjs-org/legacy, it threw. Core's SequenceOfItems.writeBytes serializes each sequence item through the late-bound DicomMessage class, and only legacy (or the dcmjs wrapper) ever registers one. The ported suite never noticed because its readback oracle imports DicomMessage from legacy, which wires the seam as a side effect of the import.

The fix follows the intent already written down in core/writeCore.js's header — "ValueRepresentation's sequence writer calls back into writeDataSet for its items" — which described a callback that didn't actually exist yet. I first tried the direct route, a top-level import of writeDataSet in ValueRepresentation.js, and it promptly broke everything: under babel's CJS output the new module-level cycle (ValueRepresentation → writeCore → Tag → ValueRepresentation) runs Tag.js's registration side effect against a half-evaluated class. So the fix uses the codebase's existing seam pattern instead: writeCore.js registers writeDataSet into ValueRepresentation at load (setWriteDataSet, documented next to the DicomMessage seam it mirrors), and the sequence writer calls the registered function. Since legacy's DicomMessage.write is a one-line delegate to that same writeDataSet, the bytes are identical no matter which package loaded first — nothing about existing behavior changes.

The regression test is packages/dicomdir/test/standalone.test.js, and its design is the point: it imports nothing from legacy, and jest gives each test file its own module registry, so the seam is guaranteed unwired when it runs. It writes a one-entry DICOMDIR and checks the bytes directly — DICM at offset 128, exactly four undefined-length FFFE,E000 item headers (PATIENT/STUDY/SERIES/IMAGE), and determinism under a fixed fileSetUID. Before the fix this file throws TypeError: Cannot read properties of undefined (reading 'write'); after it, green.

Hand-written lines: about 100 (the seam + registration + comments ~30 in core, the standalone test ~70). Full suite: 861 passed / 12 skipped / 873 total, build, lint, and format green.

🤖 Generated with Claude Code

Writing a DICOMDIR from @dcmjs-org/dicomdir alone threw: core's
SequenceOfItems.writeBytes serialized each item through the late-bound
DicomMessage, which only @dcmjs-org/legacy (or the dcmjs wrapper)
registers — so the package's hard no-legacy rule held in its imports
but not at runtime. The committed suite never saw it because its
readback oracle imports legacy, wiring the seam as a side effect.

core/writeCore.js now registers writeDataSet into ValueRepresentation
at load (a direct import between the two files is a module-level CJS
cycle that breaks Tag's registration, hence the seam), and the
sequence writer calls that instead of DicomMessage.write — the exact
function DicomMessage.write delegates to, so bytes are unchanged
either way. The new packages/dicomdir/test/standalone.test.js imports
nothing from legacy (jest's per-file module registry guarantees the
seam stays unwired) and proves a full DICOMDIR write: DICM magic,
four undefined-length item headers for PATIENT/STUDY/SERIES/IMAGE,
deterministic bytes.

Full suite: 861 passed, 12 skipped, 873 total.

Co-Authored-By: Claude Fable 5 <[email protected]>
@awatson1978
awatson1978 merged commit 866e59e into release/1.0-beta-dicomdir Oct 2, 2026
8 checks passed
@awatson1978
awatson1978 deleted the dicomdir/standalone-sq-write branch October 2, 2026 18:50
@awatson1978 awatson1978 changed the title fix: let core's sequence writer run with no eager engine loaded fix(core): let the sequence writer run with no eager engine loaded (dicomdir wave) Oct 2, 2026
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