Repository navigation
feat(fhir): port the DocumentReference mapper (findings 8, 16) - #582
Merged
Merged
Conversation
documentReference.js comes over verbatim, and the package index swaps the slice-two placeholder for the real import and re-export, completing the toFhir three-key surface. The ported test builds its Encapsulated PDF datasets synthetically (v2 built them with dcmjs.encapsulated, which belongs to the pdfs wave and is not on this branch yet), keeps the Part 10 round trip through @dcmjs-org/legacy as a test-only devDependency, and naturalizes the image fixture through @dcmjs-org/parser like the toFhir suite. Co-Authored-By: Claude Fable 5 <[email protected]>
…tLength Review findings 8 and 16 on PR #512. The event-stream naturalizer wraps binary elements as { InlineBinary } (base64 string, ArrayBuffer, or view); payloadBytes never unwrapped that shape, and new Uint8Array of a plain object is empty, so every naturalized encapsulated document silently mapped to { size: 0, data: "" }. The unwrap block mirrors the one in v2's extractEncapsulatedPdf, kept as a local copy because this package must not depend on the pdfs package. The even-length pad-trim heuristic also stripped real trailing zero bytes, since an even stored length never carries a writer pad. When EncapsulatedDocumentLength (0042,0015) is present it now wins over the heuristic; the write side that emits it lands with the pdfs wave. Co-Authored-By: Claude Fable 5 <[email protected]>
This was referenced Oct 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This completes the mapper set: the DocumentReference direction for encapsulated documents, with review findings 8 and 16 fixed inside the port.
documentReference.jsmaps a naturalized Encapsulated Document instance (Encapsulated PDF SOP class, or anything carrying a MIMETypeOfEncapsulatedDocument) to a FHIR R4B DocumentReference — masterIdentifier from the SOP Instance UID, type from ConceptNameCodeSequence with LOINC/DCM/SNOMED system mapping, the document itself as a base64 attachment, and a context.related link back to the originating study. The package index swaps the slice-two placeholder for the real import and re-export, sotoFhirnow routes encapsulated documents to a DocumentReference (and no ImagingStudy) exactly as v2 does.The source file is a verbatim port apart from the two fixes. The test needed more adaptation than the earlier slices, declared here: v2 built its input datasets with
dcmjs.encapsulated.encapsulatePdf, which belongs to the pdfs wave and is not on this branch yet, sobuildPdfDatasethand-constructs the same naturalized shape. The Part 10 round-trip test serializes through@dcmjs-org/legacy'sdatasetToBufferand reads back withDicomMessage.readFile— legacy is a test-only devDependency; the package's own sources still import nothing but@dcmjs-org/core. The image-fixture tests usepackages/fixtures/dicom/sample-dicom.dcm(provenance still being traced, as noted on the earlier slices) through@dcmjs-org/parser.The findings, each with regressions written against the ported-but-unfixed code and observed failing before the fix:
payloadBytesnever unwrapped the{ InlineBinary }shape that the event-stream naturalizer produces for binary elements, andnew Uint8Array(<plain object>)is zero-length — so every naturalized encapsulated document silently mapped to{"size": 0, "data": ""}. The pdf-side extractor already handled both shapes, meaning the two extractors disagreed on the same input. The fix ports that exact unwrap block (base64 string, ArrayBuffer, or typed-array view) as a local copy — this package must not import the pdfs package, so the ~20 lines are duplicated knowingly, with a comment naming the original. The regression feeds a base64InlineBinaryand asserts the attachment round-trips byte-exact; two red tests covered the string and buffer/view shapes.0x00(ordinary content for STL, OBJ, CDA, octet-stream).EncapsulatedDocumentLength (0042,0015)carries the exact length, so when present it now wins over the heuristic; without it the historical heuristic still applies, since for a bare PDF payload that remains the best available guess. This is the read half of a pair: the write side that actually emits (0042,0015) lands with the pdfs wave.The mapping in use:
Thirteen tests in the ported suite (the nine v2 tests — full mapping, subject/includeData options, ConceptNameCodeSequence typing, null for images, the odd-length Part 10 round trip — plus the four finding regressions), 51 in the package, 895 passing overall on this branch, and
pnpm run buildis green.🤖 Generated with Claude Code