Stream for reading file updates in real-time
npm install tail-file-stream__tail-file-stream__ is a library that provides a streaming interface for reading a file that can be appended.
It implements the Node.js fs.ReadStream functionality and watches the file for changes.
When the file is appended, the stream emits a 'data' event with the new data.
- Install
- Usage
- API
- [createReadStream(path, [options])](#createreadstreampath-options)
- TailFileStream
- License
```
npm i tail-file-stream --save
`js
const { createReadStream } = require('tail-file-stream')
const stream = createReadStream('./foo.txt')
stream.on('data', (chunk) => {
// Emits when has new data
})
stream.on('eof', () => {
// Emits when reaches the end of the file
})
stream.on('end', () => {
// Emits when stream ends
})
setTimeout(() => {
// Stop watching the file
stream.unwatch()
}, 10000)
`
#### createReadStream(path, [options])
Creates a new TailFileStream instance.
- path The file path to be read.options
-
#### TailFileStream
The TailFileStream class extends the Node.js Readable stream.
TailFileStream emits the following events:
- close - Emits when the file is closed.eof
- - Emits when reaches the end of the file.open
- - Emits when the file is opened.ready
- - Emits when the file is ready to be read.
TailFileStream has the following properties:
- bytesRead The number of bytes read from the file.path
- The file path.pending
- This property is true if the underlying file has not been opened yet, i.e. before the 'ready' event is emitted.watching
- Indicates whether the file is being watched.waiting
- Indicates whether the stream is waiting for file changes.
TailFileStream has the following methods:
- watch() - Starts watching the file for changes.unwatch()` - Stops watching the file. If the stream is waiting for file changes, it will be closed.
-
If the stream is reading, the data will be read until the end of the file and then it will be closed.
MIT