Skip to content

feat(core): extensible registries for VRs, SOP classes and character sets; options-object streams - #650

Merged
wayfarer3130 merged 7 commits into
1.0-betafrom
feat/core-registries
Nov 24, 2026
Merged

wayfarer3130 merged 7 commits into
1.0-betafrom
feat/core-registries

Conversation

@wayfarer3130

Copy link
Copy Markdown
Contributor

Summary

This PR moves three ideas from #455 to 1.0. Luis Miguel Santos is co-author of each commit. The maintainers reshaped the ideas during the review of #455.

  • The buffer stream constructors take an options object. The new form uses little endian by default.
  • Each registry is one constants object that a caller can extend: Vrs, SopClasses, and the character sets.
  • registerCharacterSet registers a decode function and an encode function. The writer encodes text in the declared SpecificCharacterSet.

The specification is in docs/specs/registries.md (user requirements, REG-U-n). The implementation specification is in docs/specs/registries-implementation.md (REG-I-n).

Changes

  • ReadBufferStream(buffer, options) and WriteBufferStream(options). The options are littleEndian (default true), start, stop, noCopy, clearBuffers, and defaultSize.
  • The 0.x positional form still works. The shim detects the form by the argument count, and keeps every argument, also the third argument of ReadBufferStream. A positional call keeps the 0.x big-endian default. The shim logs one warning for each class.
  • Vrs holds the VR instances, for example Vrs.DS. registerVr(code, vr, flags?) adds a VR or replaces a VR. Each VR keeps its classification as properties. The old lists (singleVRs, BINARY_VRS) are now views of Vrs, with the same members.
  • SopClasses maps a name to a UID, for example SopClasses.CTImage. registerSopClass(name, uid) adds an entry. sopClassName(uid) gives the reverse lookup. DicomMetaDictionary.sopClassUIDsByName and sopClassNamesByUID stay plain objects, so property access still works.
  • registerCharacterSet(term, { label, createDecoder, encode }) adds a SpecificCharacterSet defined term. The read path and the write path use the same registry. encodingMapping stays a plain object.
  • The writer encodes text with the character set of the dataset. A character set in a sequence item applies only to that item. writePart10, Part10Writer, and StreamingPart10Writer all use this behavior.
  • The single-byte character sets get a generated encoder. A character that the set cannot encode causes an error. With ignoreErrors, the writer writes ?.
  • The multi-byte and ISO 2022 character sets have no encoder. For these sets, the writer logs one warning and writes UTF-8, as before.
  • An empty (0008,0005) and ISO_IR 100 use the windows-1252 table, the same table as the read path. The specification gives the reason.
  • docs/MIGRATION.md lists the byte-order change of the constructors. It also lists the charset change of the 0.x-compatible DicomDict.write() and DicomMessage.write().
  • The bundle-size ceilings increase from 1.075 to 1.08 (dcmjs.js) and from 1.06 to 1.062 (dcmjs.es.js).

Tests

The full suite passes: 1269 tests pass and 12 tests are skipped. The new tests are in test/core/registries.test.js.

The API must do this:

  • A positional ReadBufferStream call keeps start and stop. A positional call keeps big endian. The options form uses little endian.
  • registerVr makes an unknown VR decode with the registered instance, without the "Invalid vr type" warning.
  • SopClasses, registerSopClass, and DicomMetaDictionary.sopClassUIDsByName.Name give the same UIDs.
  • A registered custom character set term decodes and encodes.

The user experience must have this:

  • A dataset with ISO_IR 100 writes é as the byte 0xE9, and the read gives the same text. A number in an LO value also writes.
  • A character set in a sequence item does not apply to the next item or the next element. The test covers writePart10 and StreamingPart10Writer.
  • An unencodable character causes an error. With ignoreErrors, the writer writes ?.

Known limits

Related: #455, #454.

🤖 Generated with Claude Code

wayfarer3130 and others added 7 commits November 24, 2026 12:43
new ReadBufferStream(buffer, { littleEndian, start, stop, noCopy,
clearBuffers }) and new WriteBufferStream({ defaultSize, littleEndian }).
Little endian is the default. The 0.x positional forms still work, keep
every argument (including the 3rd options argument of ReadBufferStream),
and warn once per class. All internal callers use the options form.

Co-authored-by: Luis Miguel Santos <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The VR classification is stored once (constants/vrs.js flags), copied to
each VR instance, and singleVRs and BINARY_VRS are computed from it and
follow registerVr. Vrs replaces the private VRinstances table;
createByTypeString reads it, so a registered VR decodes without the
"Invalid vr type" warning. Members are unchanged (UV stays binary and
length32; OL/OV have only the byte-stream flag).

SopClasses (name -> UID), registerSopClass(name, uid) and
sopClassName(uid) replace the hand-kept sopClassNamesByUID table.
DicomMetaDictionary.sopClassUIDsByName and sopClassNamesByUID are the
registry's plain objects, and _generateUIDMap() still picks up direct
edits of sopClassNamesByUID.

Co-authored-by: Luis Miguel Santos <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rset

registerCharacterSet(term, { label, createDecoder, encode }) adds or
replaces a SpecificCharacterSet defined term for read and write. The
registry is seeded from encodingMapping (read at lookup time) and keeps
it in step. resolveCharsetDecoder looks terms up in the registry, so all
read paths decode a registered term.

writeDataSet sets the stream encoder when it writes (0008,0005) and
restores the caller's encoder at the end of the dataset or item; Tag.write
gives each child stream its parent's encoder. StreamingPart10Writer keeps
the same scope per item. Single-byte charsets get a reverse-table encoder
(unencodable characters throw, or write "?" with writeOptions.ignoreErrors);
UTF-8 uses TextEncoder; multi-byte and ISO 2022 charsets keep UTF-8 bytes
with a one-time warning.

Co-authored-by: Luis Miguel Santos <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The VR, SOP class and character-set registries and the stream
constructor shim add about 9 KB to the dcmjs.js bundle and 8 KB to the
dcmjs.es.js consumer bundle (ratios 1.079 and 1.061).

Co-authored-by: Luis Miguel Santos <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
docs/specs/registries.md (user requirements REG-U-n) and
registries-implementation.md (REG-I-n), and MIGRATION.md entries for the
stream constructors and for the write in the declared character set.

Co-authored-by: Luis Miguel Santos <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
- writeUTF8String encodes String(value): a number in a text VR no longer
  crashes the single-byte encoder (B1).
- writeDataSet restores the stream encoder in a finally block (N6).
- the charset warn-once key is the normalized term, so
  registerCharacterSet clears it (N1).
- registerSopClass drops the old reverse entry of a renamed UID (N4).
- bundle-size ceiling comment matches the ceilings (N8).
- MIGRATION.md lists every constructor whose byte order changed and
  rewords the intro about the writer (S4).
- tests: the unencodable character and "?" under ignoreErrors
  (REG-U-13), StreamingPart10Writer item scoping (REG-I-19), and a
  number in an LO under ISO_IR 100 (B1).

Co-authored-by: Luis Miguel Santos <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…isions

- ReadBufferStream treats any explicit non-object 2nd argument (undefined,
  0, ...) as the 0.x byte order, and WriteBufferStream any explicit
  non-object 1st argument, so those calls stay big endian as in 0.x and
  warn once. DeflatedReadBufferStream passes an options object.
- REG-U-12: an empty (0008,0005) or ISO 2022 IR 6 writes with the
  windows-1252 table, the common handling of an unspecified repertoire.
- REG-U-13: ISO_IR 100 keeps the windows-1252 table (symmetric with the
  read), so 0x80-0x9F characters write without an error.
- MIGRATION.md: the note is about the 0.x-compatible DicomDict.write and
  DicomMessage.write, which now delegate to writePart10.

Co-authored-by: Luis Miguel Santos <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wayfarer3130
wayfarer3130 marked this pull request as ready for review November 24, 2026 19:30
@wayfarer3130
wayfarer3130 merged commit ec6707e into 1.0-beta Nov 24, 2026
5 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.0.0-beta.16 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant