A dual-mode message buffer for SMTP: accepts lines as they arrive from the wire, spools to disk when a size threshold is exceeded, and replays the message to one or more downstream consumers via a standard stream pipeline.
- Haraka/transaction.js
- haraka/test-fixtures/transaction
import MessageStream from '@haraka/message-stream'
const ms = new MessageStream(cfg, uuid, headers)The package is ESM and requires Node 24+. CommonJS code can still require('@haraka/message-stream'), which returns the class.
| Parameter | Type | Description |
|---|---|---|
cfg |
object |
Haraka config object. Reads cfg.main.spool_after (bytes; -1 = never spool; default 25 MiB) and cfg.main.spool_dir (path, default /tmp). |
uuid |
string |
Required. Used as the spool filename base (<spool_dir>/<uuid>.eml). |
headers |
string[] |
Optional. Pre-parsed header lines. When supplied they are emitted first and the raw header block in stored data is skipped. |
Feed lines in as they arrive. Call add_line_end() when the DATA terminator (.) is received.
ms.add_line('From: sender@example.com\r\n')
ms.add_line('To: rcpt@example.com\r\n')
ms.add_line('\r\n') // end-of-headers blank line
ms.add_line('Hello, world.\r\n')
await ms.add_line_end() // writing is complete; safe to start readingadd_line_end() returns a promise that resolves once all data is in memory or on disk. It takes no callback.
Replays the stored message to any writable stream. Can be called sequentially (not concurrently).
import { createWriteStream } from 'node:fs'
ms.pipe(createWriteStream('/tmp/out.eml'))Pipe options
| Option | Type | Default | Description |
|---|---|---|---|
line_endings |
'\r\n' | '\n' |
'\r\n' |
Output line endings. Use '\n' to strip \r for local processing. |
dot_stuffed |
boolean |
true |
When true, removes SMTP dot-stuffing (..line → .line) for local delivery/scanning. Set to false when relaying over SMTP so stored dot-stuffing is preserved on the wire. |
ending_dot |
boolean |
false |
Append .\r\n (or .\n) after the last body line — required when forwarding over SMTP. Forces stored dot-stuffing to be preserved on the wire (overrides dot_stuffed). |
clamd_style |
boolean |
false |
Prefix each chunk with a 4-byte big-endian length; append a 4-byte zero terminator. Used with ClamAV's INSTREAM protocol. |
skip_headers |
boolean |
false |
Omit the header block from output (emit body only). |
// Forward message over SMTP — preserve dot-stuffing, add terminating dot
ms.pipe(socket, { dot_stuffed: false, ending_dot: true })
// Local virus scan — unstuff dots, bare LF, clamd framing
ms.pipe(clamdSocket, {
line_endings: '\n',
dot_stuffed: true,
clamd_style: true,
})
// Body-only, no \r, sequential second pass
ms.pipe(bodyParser, { skip_headers: true, line_endings: '\n' })Tears down the internal source/transformer synchronously and frees the stream for a subsequent pipe(). Idempotent; no-op when no pipe is active.
Call unpipe() before destroying the destination when your handler reacts to a mid-stream failure (socket error, scan timeout, etc.) — the destination's 'close' event is asynchronous, so calling next() immediately after destination.destroy() would race the cleanup and the next consumer's pipe() would throw Cannot pipe while currently piping.
function onError(err) {
ms.unpipe() // synchronous teardown
socket.destroy()
next()
}
socket.on('error', onError)
socket.on('timeout', onError)
ms.pipe(socket)The library also installs prepended 'error' / 'close' listeners on the destination as a safety net, but consumers that synchronously start a new pipe must call unpipe() explicitly. See haraka/message-stream#22.
Returns a promise of the whole message. It rejects on a read error, or while another pipe is active.
const buf = await ms.get_data()
// with pipe options
const unix = await ms.get_data({ line_endings: '\n', dot_stuffed: true })The stream implements Symbol.asyncIterator, so you can consume it with a for await loop. All pipe options are supported.
// Default: CRLF, dot_stuffed: true
for await (const chunk of ms) {
process(chunk)
}
// With options
for await (const chunk of ms[Symbol.asyncIterator]({ line_endings: '\n' })) {
process(chunk)
}When total_buffered exceeds cfg.main.spool_after, the stream transparently spools to <spool_dir>/<uuid>.eml. The consumer API is identical in both cases. The spool file is not removed automatically — call ms.destroy() when done.
spool_after accepted values:
- a finite non-negative number — spool once
total_bufferedexceeds this many bytes (0spools immediately); -1— never spool, keep everything in memory (matches Haraka'sconnection.inisentinel);- omitted /
undefined— fall back to a safe 25 MiB default; - anything else (non-numeric strings,
NaN, other negatives) — rejected with aconsole.warn, default applies.
const cfg = {
main: {
spool_after: 1024 * 1024, // spool messages larger than 1 MB
spool_dir: '/var/spool/haraka',
},
}
const ms = new MessageStream(cfg, transaction.uuid)
// ... write lines ...
await ms.add_line_end()
for await (const chunk of ms) {
await forward(chunk)
}
ms.destroy() // close fd and remove spool filems.pause() // suspend the active pipe's source readable
ms.resume() // resume it
ms.unpipe() // abort the active pipe (synchronous; see Reading)
ms.destroy() // close the spool file descriptor and delete the fileThe 2.x refactor separated concerns into two composable Transform stages:
stored data
│
▼
HeaderSkipper (strips raw header block when constructor headers are used)
│
▼
LineTransformer (dot-unstuffing · CRLF normalisation · ending dot · clamd framing)
│
▼
destination
ChunkEmitter is still exported for write-side batching used by add_line.
import { ChunkEmitter } from '@haraka/message-stream'Spool write failures (open, write, flush) reject add_line_end(), and are also emitted as 'error' when the stream has an 'error' listener. Read errors reject get_data() and end a for await loop; with pipe(), they're emitted as 'error'. destroy() before the write completes reports a cancelled pipe() as an ERR_STREAM_DESTROYED 'error'.
ms.on('error', (err) => {
console.error('message-stream error', err)
})This module is used by Haraka Transaction.