Skip to content

feat(pdfs): the @dcmjs-org/pdfs package — encapsulated PDF wrap and unwrap - #572

Merged
awatson1978 merged 3 commits into
release/1.0-beta-pdfsfrom
pdfs/package-port
Oct 2, 2026
Merged

awatson1978 merged 3 commits into
release/1.0-beta-pdfsfrom
pdfs/package-port

Conversation

@awatson1978

Copy link
Copy Markdown
Collaborator

This ports the Encapsulated PDF module into its own @dcmjs-org/pdfs package and fixes review finding 16 inside the port. encapsulatePdf wraps PDF bytes into a conformant naturalized Encapsulated PDF dataset (PS3.3 A.45.1, SOP Class 1.2.840.10008.5.1.4.1.1.104.1), ready for the Part 10 writers; extractEncapsulatedPdf recovers the exact PDF bytes, MIME type, and title from a parsed instance. Only the PDF half of the v2 src/encapsulated/ module comes over — the video half belongs to the media wave, and the toPdf/fromPdf facade factories in src/eventStream/api.js stay put for the same reason.

On the verbatim-vs-hand-written split: the 213-line encapsulatedPdf.js is a verbatim port of the v2 branch HEAD (including the { InlineBinary } unwrap block that postdates the PR #512 review), with exactly one change beyond the finding fix — its import rebases from ../DicomMetaDictionary.js onto @dcmjs-org/core. That is also the package's entire dependency list: no @dcmjs-org/legacy anywhere, not even in the tests. The v2 suite leaned on legacy's datasetToBuffer/DicomMessage.readFile for its round-trips; the ported suite drives the same round-trips through this branch's own engine instead — denaturalize → fromDataSet → Part10Writer → fromPart10 → NaturalizedListener — which has the nice side effect of exercising the naturalizer's { InlineBinary } wrapper path that the unwrap block exists for.

Review finding 16 (MED), fixed in the port with its test written first. The v2 extractor trimmed a trailing 0x00 from any even-length payload, on the theory that it must be the writer's odd-length OB pad byte. But the writer pads only odd lengths, so a stored even length is ambiguous — and an even-length original that genuinely ends in a zero byte came back corrupted (a 10-byte payload ending 0x00 returned as 9 bytes). The fix is the same shape the review prescribed and the video path already uses: encapsulatePdf now records EncapsulatedDocumentLength (0042,0015) with the exact original length, and extractEncapsulatedPdf slices to the declared length when present, keeping the trim heuristic only as the fallback for files written without the element. The regression tests went in against the ported-unfixed code and were observed failing — the even-length zero-tail round-trip red at 9 bytes — before the fix turned them green; a fourth test pins that a file without 0042,0015 still extracts the odd-length original exactly via the fallback.

One more departure from the v2 suite, declared explicitly: its last test read test/sample-dicom.dcm, whose DATACITATION provenance is still being traced. Rather than carry that dependency into the new package, the rejection test now round-trips a synthetic MR instance through the same write/read pipeline and asserts the extractor refuses it — same proof, no fixture.

Two tests from the suite, which is how the whole thing is meant to be used:

const dataset = encapsulatePdf(pdfBytes, {
    PatientName: "Doe^Jane",
    DocumentTitle: "Discharge Summary"
});
// dataset.EncapsulatedDocumentLength === pdfBytes.byteLength (0042,0015)
// ... write via Part10Writer, read back via fromPart10 + NaturalizedListener ...
const { bytes, mimeType, title } = extractEncapsulatedPdf(readBack);
// bytes are byte-identical to the input PDF, even one ending 0x00

Twelve tests in the new suite — the eight ported ones (odd-length fixture gate, exports, conformant defaults, caller options, PDF-magic rejection, Part 10 round-trip, exact extraction, non-encapsulated rejection) plus the four finding-16 tests. 856 passing overall on this branch (12 skipped), build green.

🤖 Generated with Claude Code

awatson1978 and others added 3 commits October 2, 2026 12:17
The package manifest (publishable under the @dcmjs-org scope, main at
src/index.js, repository directory set), a short README, and the lockfile
entry linking the one dependency, @dcmjs-org/core.

Co-Authored-By: Claude Fable 5 <[email protected]>
A verbatim port of src/encapsulated/encapsulatedPdf.js from the v2 branch
HEAD (including the { InlineBinary } unwrap that postdates the PR #512
review), with its core import rebased onto @dcmjs-org/core and the PDF half
of the encapsulated index as the package surface. The video half stays on
the v2 branch for the media wave.

The eight-test v2 suite comes along, reworked for this branch's engine: the
legacy datasetToBuffer / DicomMessage.readFile round-trip becomes
denaturalize -> fromDataSet -> Part10Writer -> fromPart10 ->
NaturalizedListener, and the last test's read of test/sample-dicom.dcm
(provenance still being traced) is replaced by a synthetic MR instance
round-tripped through the same pipeline, so neither the package nor its
suite touches @dcmjs-org/legacy or the fixture.

Co-Authored-By: Claude Fable 5 <[email protected]>
…ding 16)

The pad-trim heuristic strips a real zero byte: the Part 10 writer pads
only an odd-length OB value, so a stored even length is ambiguous — the
old code trimmed a trailing 0x00 anyway, corrupting an even-length
original that genuinely ends in a zero byte (a 10-byte payload ending
0x00 came back as 9 bytes).

encapsulatePdf now records EncapsulatedDocumentLength (0042,0015) with
the exact original length, and extractEncapsulatedPdf slices to the
declared length when present, keeping the trailing-NUL heuristic only as
the fallback for files written before the element existed.

The four new tests were written first and observed failing against the
ported-unfixed code: the even-length zero-tail round-trip (9 bytes back,
red), the recorded length element, and the declared-length odd slice;
the heuristic-fallback test passed before and after, pinning that files
without 0042,0015 still extract exactly.

Co-Authored-By: Claude Fable 5 <[email protected]>
@awatson1978
awatson1978 merged commit 7563129 into release/1.0-beta-pdfs Oct 2, 2026
10 checks passed
@awatson1978
awatson1978 deleted the pdfs/package-port branch October 2, 2026 17:35
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