Skip to main content
Version: v0.0.3

Function: parseStream()

Internal

implementation signature; overloads above carry the public JSDoc.

Call Signature

parseStream(source): AsyncGenerator<StreamMessageEntry, void, void>

Incrementally parse a chunked HL7 v2 byte / text stream, yielding one StreamMessageEntry per MSH-delimited message as its boundary completes, with O(one-message) memory: the whole stream is never retained. Demarcates by MSH boundaries inside the optional [FHS] { [BHS] { MSH… } [BTS] } [FTS] batch frame (HL7 v2 Ch. 2 §2.10.3):

  • a message split across chunk boundaries (mid-segment, mid-field, even mid-MSH|^~\&) is reassembled correctly: feeding the same bytes in 1-byte chunks vs. one chunk yields identical messages;
  • \r, \r\n, and \n segment terminators are all tolerated (a \r\n split across a chunk boundary is not mistaken for a bare \r);
  • each message is parsed by the shipped parseHL7 (no second grammar), ok entries carry the Hl7Message, a Tier-3 fatal is an isolated failure entry; a malformed message never suppresses later messages;
  • batch-envelope segments (FHS/BHS/BTS/FTS) are treated as boundaries and never yielded as messages, so yielded count == MSH count (envelope count reconciliation is splitBatch's job);
  • a final message with no trailing terminator is still yielded, flagged with a stream-level unterminatedStreamMessage warning: never a throw, the tail is never dropped.

The second argument, when given, is forwarded verbatim to parseHL7 for each message (profile, strict, charset, dateFormats, …), exactly as splitBatch forwards it.

Parameters

source

Hl7StreamSource

Returns

AsyncGenerator<StreamMessageEntry, void, void>

Example

import { parseStream } from "@cosyte/hl7";

async function* chunks() {
yield "MSH|^~\\&|A|F|B|G|20260101||ADT^A01|1|P|2.5\rPID|||1\r";
yield "MSH|^~\\&|A|F|B|G|20260101||ADT^A01|2|P|2.5\rPID|||2\r";
}

let count = 0;
for await (const entry of parseStream(chunks())) {
if (entry.ok) count += 1; // 2: one per MSH, streamed, released each time
}

Call Signature

parseStream(source, profile): AsyncGenerator<StreamMessageEntry, void, void>

Incrementally parse a chunked HL7 v2 byte / text stream, yielding one StreamMessageEntry per MSH-delimited message as its boundary completes, with O(one-message) memory: the whole stream is never retained. Demarcates by MSH boundaries inside the optional [FHS] { [BHS] { MSH… } [BTS] } [FTS] batch frame (HL7 v2 Ch. 2 §2.10.3):

  • a message split across chunk boundaries (mid-segment, mid-field, even mid-MSH|^~\&) is reassembled correctly: feeding the same bytes in 1-byte chunks vs. one chunk yields identical messages;
  • \r, \r\n, and \n segment terminators are all tolerated (a \r\n split across a chunk boundary is not mistaken for a bare \r);
  • each message is parsed by the shipped parseHL7 (no second grammar), ok entries carry the Hl7Message, a Tier-3 fatal is an isolated failure entry; a malformed message never suppresses later messages;
  • batch-envelope segments (FHS/BHS/BTS/FTS) are treated as boundaries and never yielded as messages, so yielded count == MSH count (envelope count reconciliation is splitBatch's job);
  • a final message with no trailing terminator is still yielded, flagged with a stream-level unterminatedStreamMessage warning: never a throw, the tail is never dropped.

The second argument, when given, is forwarded verbatim to parseHL7 for each message (profile, strict, charset, dateFormats, …), exactly as splitBatch forwards it.

Parameters

source

Hl7StreamSource

profile

Profile

Returns

AsyncGenerator<StreamMessageEntry, void, void>

Example

import { parseStream } from "@cosyte/hl7";

async function* chunks() {
yield "MSH|^~\\&|A|F|B|G|20260101||ADT^A01|1|P|2.5\rPID|||1\r";
yield "MSH|^~\\&|A|F|B|G|20260101||ADT^A01|2|P|2.5\rPID|||2\r";
}

let count = 0;
for await (const entry of parseStream(chunks())) {
if (entry.ok) count += 1; // 2: one per MSH, streamed, released each time
}

Call Signature

parseStream(source, options): AsyncGenerator<StreamMessageEntry, void, void>

Incrementally parse a chunked HL7 v2 byte / text stream, yielding one StreamMessageEntry per MSH-delimited message as its boundary completes, with O(one-message) memory: the whole stream is never retained. Demarcates by MSH boundaries inside the optional [FHS] { [BHS] { MSH… } [BTS] } [FTS] batch frame (HL7 v2 Ch. 2 §2.10.3):

  • a message split across chunk boundaries (mid-segment, mid-field, even mid-MSH|^~\&) is reassembled correctly: feeding the same bytes in 1-byte chunks vs. one chunk yields identical messages;
  • \r, \r\n, and \n segment terminators are all tolerated (a \r\n split across a chunk boundary is not mistaken for a bare \r);
  • each message is parsed by the shipped parseHL7 (no second grammar), ok entries carry the Hl7Message, a Tier-3 fatal is an isolated failure entry; a malformed message never suppresses later messages;
  • batch-envelope segments (FHS/BHS/BTS/FTS) are treated as boundaries and never yielded as messages, so yielded count == MSH count (envelope count reconciliation is splitBatch's job);
  • a final message with no trailing terminator is still yielded, flagged with a stream-level unterminatedStreamMessage warning: never a throw, the tail is never dropped.

The second argument, when given, is forwarded verbatim to parseHL7 for each message (profile, strict, charset, dateFormats, …), exactly as splitBatch forwards it.

Parameters

source

Hl7StreamSource

options

ParseOptions

Returns

AsyncGenerator<StreamMessageEntry, void, void>

Example

import { parseStream } from "@cosyte/hl7";

async function* chunks() {
yield "MSH|^~\\&|A|F|B|G|20260101||ADT^A01|1|P|2.5\rPID|||1\r";
yield "MSH|^~\\&|A|F|B|G|20260101||ADT^A01|2|P|2.5\rPID|||2\r";
}

let count = 0;
for await (const entry of parseStream(chunks())) {
if (entry.ok) count += 1; // 2: one per MSH, streamed, released each time
}