Skip to main content
Version: v0.0.3

Function: encodeFrame()

encodeFrame(payload, opts?): Buffer

Encode payload in canonical MLLP framing: VT (0x0B) + payload + FS (0x1C) + CR (0x0D).

Strict by default: throws MllpFramingError if the payload contains VT or FS bytes. Set { allowDelimiterBytesInPayload: true } to pass those bytes through verbatim and receive an MllpWarning per offending byte via onWarning instead.

The returned Buffer has length payload.length + 3. Bytes at positions 1 through payload.length are an exact copy of the payload (independent of the input buffer).

Parameters

payload

Buffer

opts?

EncoderOptions

Returns

Buffer

Throws

with code MLLP_PAYLOAD_CONTAINS_VT when payload contains byte 0x0B and allowDelimiterBytesInPayload is false (default).

Throws

with code MLLP_PAYLOAD_CONTAINS_FS when payload contains byte 0x1C and allowDelimiterBytesInPayload is false (default).

Example

import { encodeFrame } from '@cosyte/mllp';

// Strict (default), throws on delimiter bytes in payload
const frame = encodeFrame(Buffer.from('MSH|^~\\&|SEND|FAC|RECV|FAC|...'));
socket.write(frame);

// Tolerant, passes delimiter bytes through with a warning
const frame2 = encodeFrame(dirtyPayload, {
allowDelimiterBytesInPayload: true,
onWarning: (w) => logger.warn(w),
});