Skip to content
@deckyard/hl7

hl7 cheatsheet

Every export of @deckyard/hl7. Guides are listed in the introduction.

jennifer
import "@deckyard/hl7/" as hl7;

Every failure is Error{kind: "hl7"}. An m argument is a Message, an s a Segment, a d Delimiters, a c a Connection, an o Options. Fields, repetitions, components and subcomponents are numbered from 1, as HL7 numbers them; in MSH, MSH-1 is the field separator and MSH-2 the encoding characters.

Constants

ConstantValueMeaning
MAX_SEGMENTS4096the most segments parse reads in one call
MAX_FIELDS65536the most fields, repetitions, components and subcomponents parse reads in one call
DEFAULT_TIMEOUT_MS30000how long MLLP waits for an acknowledgement, or for a whole message once it began
DEFAULT_MAX_BYTES1048576the largest MLLP message

Messages

FunctionReturnsDoes
hl7.parse(text)Messageone message, every segment ended by CR
hl7.parseAll(text)list of Messagemessages that follow each other, each from its MSH
hl7.encode(m)stringthe message as text, checked; an unchanged parsed message gives its text back
hl7.parseBytes(b, charset)Messagea message from bytes, in MSH-18's character set or charset
hl7.encodeBytes(m, charset)bytesthe message as bytes, in MSH-18's character set or charset
hl7.newMessage(type, trigger, controlId, version)Messagea message with only its MSH
hl7.addSegment(m, s)Messageadd a segment at the end
hl7.ack(m, code, text, controlId)Messagethe acknowledgement: MSH with sender and receiver swapped, MSA
hl7.get(m, path)stringthe value at a path such as PID-5-1, OBX(2)-5, PID-3(2)-1; segment and repetition numbers from 1
hl7.set(m, path, value)Messageset the value at a path, escaped
hl7.segmentsOf(m, id)list of Segmentevery segment with an ID
hl7.count(m, id)inthow many segments have an ID

Segments

FunctionReturnsDoes
hl7.delimiters(separator, encoding)Delimitersthe delimiters of MSH-1 and MSH-2
hl7.defaultDelimiters()Delimiters| and ^~\&
hl7.segment(id, d)Segmenta new segment; an MSH gets MSH-2
hl7.segmentId(s)stringthe segment's ID
hl7.encodeSegment(s)stringthe segment as text, checked
hl7.raw(s, n)stringfield n as on the wire
hl7.field(s, n)stringfield n unescaped; one with repetitions, components or subcomponents is an error
hl7.repeats(s, n)inthow many repetitions field n has
hl7.components(s, n, rep)list of stringthe components of one repetition
hl7.component(s, n, rep, component)stringone component
hl7.subcomponent(s, n, rep, component, sub)stringone subcomponent
hl7.values(s, n)list of stringthe first component of every repetition
hl7.componentOfEach(s, n, component)list of stringone component of every repetition, reading the field once
hl7.setField(s, n, value)Segmentset field n, escaped
hl7.setComponent(s, n, rep, component, value)Segmentset one component, escaped
hl7.setSubcomponent(s, n, rep, component, sub, value)Segmentset one subcomponent, escaped
hl7.setRaw(s, n, text)Segmentset field n's wire text, checked

MLLP

FunctionReturnsDoes
hl7.options()OptionsASCII, 30 s, 3 retries, 1 MiB, one hour idle
hl7.connect(address, o)Connectionconnect to a listener
hl7.listen(address)net.Listenerlisten for clients
hl7.accept(listener, o)Connectionwait for a client
hl7.send(c, m)Messagesend a message and return its acknowledgement; sent again on no reply; late acknowledgements of the one before are skipped
hl7.receive(c, waitMs)list of Messagethe next message, or nothing when none began in time
hl7.reply(c, m)nullsend a reply without waiting
hl7.serve(listener, o, handler)nullanswer every client with handler(m), one task per connection; when the listener closes, close the connections and return
hl7.close(c)nullclose the connection

Types

Delimiters

FieldMeaning
field as stringMSH-1, usually |
component as stringusually ^
repetition as stringusually ~
escape as stringusually \
subcomponent as stringusually &
truncation as stringv2.7 and later, usually #; "" without

Segment

FieldMeaning
fields as list of stringthe ID and the fields as on the wire
delimiters as Delimitersthe delimiters of its message

Message

FieldMeaning
delimiters as Delimitersthe delimiters its MSH declares
segments as list of Segmentevery segment, MSH first

Options

FieldMeaning
charset as stringthe codec when MSH-18 is empty: ascii, iso-8859-1, utf-8, ...
timeoutMs as inthow long to wait for an acknowledgement, or for a whole message once it began
retries as inthow often send sends again without an acknowledgement, 0 to 100
maxBytes as intthe largest message, 1024 to 67108864 bytes
idleMs as inthow long serve keeps a connection that sends nothing

Connection

FieldMeaning
conn as net.Connthe socket
options as Optionshow it behaves
pending as channel of bytesbytes read past the last message
sent as channel of stringthe control ID sent last, whose late acknowledgements are skipped