Skip to content

Support cancellable async DICOM stream reads - #4

Merged
jssuttles merged 6 commits into
masterfrom
cursor/dcmjs-stream-early-stop-208e
Jul 9, 2026
Merged

jssuttles merged 6 commits into
masterfrom
cursor/dcmjs-stream-early-stop-208e

Conversation

@jssuttles

@jssuttles jssuttles commented Jul 8, 2026 •

Copy link
Copy Markdown

Summary

  • Adds a cancellable async byte-stream path for AsyncDicomReader so callers can parse metadata from an async source and stop before reading Pixel Data.
  • Preserves existing ArrayBuffer/manual stream behavior while adding readFileFromAsyncStream, shouldStop, stopInfo, bounded read-ahead controls, and async stopOnGreaterTag support.
  • Supports Node streams, browser/Web ReadableStreams, and generic async iterables without adding Node-only production imports.
  • Normalizes parser untilTag options across sync and async readers so clean, lowercase, and punctuated tag forms behave consistently; invalid explicit tags fail fast.
  • Fixes Node 24 fixture reads in tests by slicing Node Buffer backing stores to exact file bytes.

Review guide

  • Review src/BufferStream.js for the stream adapter/cancellation behavior:
    • Node streams use destroy() unless destroyOnAbort: false leaves ownership with the caller.
    • Web streams use reader.cancel() and release reader locks.
    • Generic async iterables attempt iterator.return() but are not awaited by default to avoid hangs.
    • Upstream source failures are preserved; only errors matching the pump-created abort reason are treated as intentional aborts.
  • Review src/AsyncDicomReader.js for parser lifecycle behavior:
    • readFileFromAsyncStream() owns stream cleanup for the convenience path.
    • stopInfo distinguishes tag start, parser stop offset, value offset, and buffered size.
    • stopOnGreaterTag is scoped to the current dataset level and disabled for recursive sequence item reads.
  • Review src/DicomMetaDictionary.js and src/DicomMessage.js to confirm parser option normalization is shared while public tag helper compatibility is preserved.
  • Review tests in test/readBufferStream.test.js, test/async-data.test.js, test/data-options.test.js, and test/DicomMetaDictionary.test.js for the behavior contract and edge cases.

Safety and risks

  • This is additive and keeps existing DicomMessage.readFile, reader.stream.addBuffer(), and fromAsyncStream() usage working.
  • Node/Web streams can only stop between delivered chunks. Callers needing strict no-pixel-byte buffering should choose source chunk/range sizes accordingly.
  • Generic async iterables are cooperative. If a source has a pending next() that never resolves, caller-level cancellation may still be required; callers can opt into waiting with streamOptions.awaitAbort: true.
  • stopOnGreaterTag relies on DICOM’s sorted Data Element invariant within each individual dataset/item.
  • Deflated transfer syntax behavior is unchanged; this PR does not add async deflate support.

Test plan

  • pnpm exec jest --testTimeout 60000 --runTestsByPath test/readBufferStream.test.js test/async-data.test.js
  • pnpm exec jest --testTimeout 60000 --runTestsByPath test/DicomMetaDictionary.test.js test/data-options.test.js test/async-data.test.js
  • pnpm exec prettier --check src/BufferStream.js src/AsyncDicomReader.js test/readBufferStream.test.js test/async-data.test.js
  • pnpm exec eslint src/BufferStream.js src/AsyncDicomReader.js test/readBufferStream.test.js test/async-data.test.js
  • pnpm 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.js
  • pnpm exec eslint src/DicomMetaDictionary.js src/DicomMessage.js src/AsyncDicomReader.js test/DicomMetaDictionary.test.js test/data-options.test.js test/async-data.test.js
  • pnpm exec prettier --check test/testUtils.js test/anonymizer.test.js test/data.test.js
  • pnpm exec eslint test/testUtils.js test/anonymizer.test.js test/data.test.js
  • pnpm test
  • npx -p node@24 -c "node node_modules/jest/bin/jest.js --testTimeout 60000 ."

CI

  • lint-and-format
  • test (Node 22)
  • test (Node 24)
  • build
Open in Web Open in Cursor 

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
jssuttles marked this pull request as ready for review July 9, 2026 02:23
@jssuttles
jssuttles merged commit 23ffd65 into master Jul 9, 2026
8 checks passed
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.

2 participants