tail
The util/tail module provides a polling-based single-file follower with behavior similar to tail -F.
It is designed for caller-managed lifecycle: the caller periodically invokes poll() (typically with setInterval()).
It is available:
require('util/tail')
tail.create()
Creates a tailer instance.
Syntax
tail.create(path, options)Parameters
pathString: Target file path to follow.optionsObject:fromStartBoolean(default:false)false: Start following from the current end of file.true: Start reading from the beginning on first poll.
Returned object
| Method/Field | Type | Description |
|---|---|---|
path | String | Path used to create the tailer |
poll(callback?) | Function | Reads newly appended lines and returns String[] |
close() | Function | Closes file handle and ends the tailer |
poll(callback?) behavior:
- Return value: array of newly detected lines (
String[]) - If
callbackis provided, the same array is passed to the callback - Returns an empty array when there are no new lines
- Reflects truncate/rotation state at poll time
Basic usage example
| |
SSE adapter
util/tail/sse is an adapter that emits tail output as SSE frames.
require('util/tail/sse')require('util/tail').sse
tail/sse.create()
Syntax
tailSSE.create(path, options)Parameters
pathString: Target file path to follow.optionsObject:fromStartBoolean(default:false)eventString(default: empty string)retryMsNumber(optional)writeFunction(optional)- If omitted, output is written to
process.stdout.write()
- If omitted, output is written to
Returned object
| Method | Description |
|---|---|
writeHeaders() | Writes SSE response headers |
poll() | Reads new lines, emits SSE event/data frames, returns String[] |
send(data, event?) | Emits one arbitrary SSE event payload |
comment(text) | Emits an SSE comment frame (: ...) |
close() | Closes the internal tailer |
cgi-bin SSE example
| |
Behavior notes
- Polling cadence and lifecycle (cleanup/stop) are controlled by the caller.
- If the file does not exist,
poll()returns an empty array; following begins after the file appears. - Rotation and truncation are detected and applied at poll time.
Last updated on