Repository navigation
feat(pdfs): the @dcmjs-org/pdfs package — encapsulated PDF wrap and unwrap - #572
Merged
Merged
Conversation
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]>
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 ports the Encapsulated PDF module into its own
@dcmjs-org/pdfspackage and fixes review finding 16 inside the port.encapsulatePdfwraps 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;extractEncapsulatedPdfrecovers the exact PDF bytes, MIME type, and title from a parsed instance. Only the PDF half of the v2src/encapsulated/module comes over — the video half belongs to the media wave, and thetoPdf/fromPdffacade factories insrc/eventStream/api.jsstay put for the same reason.On the verbatim-vs-hand-written split: the 213-line
encapsulatedPdf.jsis 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.jsonto@dcmjs-org/core. That is also the package's entire dependency list: no@dcmjs-org/legacyanywhere, not even in the tests. The v2 suite leaned on legacy'sdatasetToBuffer/DicomMessage.readFilefor 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
0x00from 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 ending0x00returned as 9 bytes). The fix is the same shape the review prescribed and the video path already uses:encapsulatePdfnow recordsEncapsulatedDocumentLength (0042,0015)with the exact original length, andextractEncapsulatedPdfslices 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 without0042,0015still 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:
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