Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/platformatic/tail-file-stream


https://github.com/platformatic/tail-file-stream

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

# 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](#install)
- [Usage](#usage)
- [API](#api)
- [createReadStream(path, [options])](#createreadstreampath-options)
- [TailFileStream](#tailfilestream)
- [License](#license)

## Install

```
npm i tail-file-stream --save
```

## Usage

```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)

```

## API

#### createReadStream(path, [options])

Creates a new `TailFileStream` instance.

- `path` `` The file path to be read.
- `options` `` Options for the stream.
- `flags` `` See [support of file system](https://nodejs.org/docs/latest/api/fs.html#file-system-flags) flags. Default: `'r'`.
- `mode` `` Default: `0o666`.
- `start` `` The byte offset to start reading from. Default: `0`.
- `end` `` The byte offset to stop reading. Default: `Infinity`.
- `highWaterMark` `` The maximum number of bytes to store in the internal buffer. Default: `64 * 1024`.
- `autoWatch` `` If `true`, the file will be watched for changes from the beginning. Default: `true`.
- `persistent` `` Indicates whether the process should continue to run as long as files are being watched. Default: `true`.
- Returns: `TailFileStream` The stream instance.

#### TailFileStream

The `TailFileStream` class extends the Node.js [Readable](https://nodejs.org/docs/latest/api/stream.html#class-streamreadable) 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.

## License

MIT