Repository navigation
Support cancellable async DICOM stream reads - #4
Merged
Merged
Conversation
Add a coordinated async stream path that can abort the source when parsing stops before pixel data or after a caller-provided stop condition. Bound read-ahead so stream parsing can avoid buffering unwanted image payload when callers choose small source chunks.
Add async support for stopOnGreaterTag so metadata reads can stop when DICOM ordering shows the requested tag has been passed. Keep parent tag-stop options scoped to the current dataset level so sequence items do not trigger top-level stops.
Add shared tag normalization for parser stop options so sync and async readers accept clean, lowercase, and punctuated tag forms consistently. Invalid explicit untilTag values now fail fast instead of silently disabling early-stop behavior.
Node 24 can expose a larger pooled ArrayBuffer from fs.readFileSync().buffer for small fixtures. Use a shared test helper that slices by byteOffset and byteLength so DicomMessage reads only the fixture bytes.
Broaden async stream cancellation beyond Node destroy() by adapting Web ReadableStream sources through getReader(). Abort now calls reader.cancel(), waits for cancellation to settle, and releases reader locks while preserving existing Node stream behavior.
Only suppress abort errors that match the abort reason created by the pump. Preserve upstream failures even if parsing already reached an early-stop condition, keep generic async iterable aborts opt-in for awaiting, and cover abort-shaped source errors plus destroyOnAbort behavior.
jssuttles
marked this pull request as ready for review
July 9, 2026 02:23
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.
Summary
AsyncDicomReaderso callers can parse metadata from an async source and stop before reading Pixel Data.readFileFromAsyncStream,shouldStop,stopInfo, bounded read-ahead controls, and asyncstopOnGreaterTagsupport.ReadableStreams, and generic async iterables without adding Node-only production imports.untilTagoptions across sync and async readers so clean, lowercase, and punctuated tag forms behave consistently; invalid explicit tags fail fast.Bufferbacking stores to exact file bytes.Review guide
src/BufferStream.jsfor the stream adapter/cancellation behavior:destroy()unlessdestroyOnAbort: falseleaves ownership with the caller.reader.cancel()and release reader locks.iterator.return()but are not awaited by default to avoid hangs.src/AsyncDicomReader.jsfor parser lifecycle behavior:readFileFromAsyncStream()owns stream cleanup for the convenience path.stopInfodistinguishes tag start, parser stop offset, value offset, and buffered size.stopOnGreaterTagis scoped to the current dataset level and disabled for recursive sequence item reads.src/DicomMetaDictionary.jsandsrc/DicomMessage.jsto confirm parser option normalization is shared while public tag helper compatibility is preserved.test/readBufferStream.test.js,test/async-data.test.js,test/data-options.test.js, andtest/DicomMetaDictionary.test.jsfor the behavior contract and edge cases.Safety and risks
DicomMessage.readFile,reader.stream.addBuffer(), andfromAsyncStream()usage working.next()that never resolves, caller-level cancellation may still be required; callers can opt into waiting withstreamOptions.awaitAbort: true.stopOnGreaterTagrelies on DICOM’s sorted Data Element invariant within each individual dataset/item.Test plan
pnpm exec jest --testTimeout 60000 --runTestsByPath test/readBufferStream.test.js test/async-data.test.jspnpm exec jest --testTimeout 60000 --runTestsByPath test/DicomMetaDictionary.test.js test/data-options.test.js test/async-data.test.jspnpm exec prettier --check src/BufferStream.js src/AsyncDicomReader.js test/readBufferStream.test.js test/async-data.test.jspnpm exec eslint src/BufferStream.js src/AsyncDicomReader.js test/readBufferStream.test.js test/async-data.test.jspnpm exec prettier --check src/DicomMetaDictionary.js src/DicomMessage.js src/AsyncDicomReader.js test/DicomMetaDictionary.test.js test/data-options.test.js test/async-data.test.jspnpm exec eslint src/DicomMetaDictionary.js src/DicomMessage.js src/AsyncDicomReader.js test/DicomMetaDictionary.test.js test/data-options.test.js test/async-data.test.jspnpm exec prettier --check test/testUtils.js test/anonymizer.test.js test/data.test.jspnpm exec eslint test/testUtils.js test/anonymizer.test.js test/data.test.jspnpm testnpx -p node@24 -c "node node_modules/jest/bin/jest.js --testTimeout 60000 ."CI