# rw-stream

> A simple stream that allows reading and writing to the same file at the same time.[H[C[F

Latest version **0.3.3** (published 2019-07-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install rw-stream
pnpm add rw-stream
yarn add rw-stream
bun add rw-stream
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.3 |
| Published | 2019-07-22 |
| First published | 2018-11-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.6.0 |
| Dependencies | 0 |
| Unpacked size | 1 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Signicode |
| Maintainers | michalcz |
| Keywords | stream, streams, async, await, transform, replace, fs, write, pipe |

## Links

- npm: https://www.npmjs.com/package/rw-stream
- Repository: https://github.com/signicode/rw-stream
- Issues: https://github.com/signicode/rw-stream/issues
- npm.io page: https://npm.io/package/rw-stream

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 0.3.3 (latest) — 2019-07-22
- 0.3.2 — 2019-06-28
- 0.3.1 — 2019-06-12
- 0.3.0 — 2018-11-05

## README

# rw-stream

A stream that can substitute contents of a file in streaming fashion.

The aim of this module is to expose a readable and writable stream connected to a single file at the same time. It works similarily to `fs.createReadStream` and `fs.createWriteStream`, but allows writing and reading simultanously, without creating a new file or move operations.

This is achieved by disallowing any write operations to advance further than the current reading index. This module will then make sure that a byte of the file will be read before it's overwritten.

***Note:*** In any case, the file will be accessed and it's contents may be overwritten even if there's no actual read operation done (due to node.js streams buffering mechanisms).

## Usage:

```javascript
const rw = require("../");

const {fd, readStream, writeStream} = await rw(path, options);
```

Arguments:

 * **path** `string` - the path of the file to access.
 * **options** `object` - options, currently none.

Returns an `object` with the following properties:

 * **fd** `int` - file descriptor number (from `fs.open`)
 * **writeStream** `Writable` - Writable stream for new file contents.
 * **readStream** `Readable` - Readable stream containing previous contents.

## Samples

A simple module that replaces the contents of a file while keeping a backup:

```javascript
const replaceAndBackup = async (newContentStream) => {
    const {readStream, writeStream} = await rw(path);

    readStream.pipe(fs.createWriteStream(`${path}.bak`));
    newContentStream.pipe(writeStream);

    return new Promise((res, rej) => writeStream.on('finish', res).on('error', rej));
}
```

## Testing

For now there's just:

```bash
$ node test
```

Two tests will be executed:

* one for file that's being grown.
* one for file that's being shrinked.

We're working on more and proper tests.

---
_Source: https://npm.io/package/rw-stream · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
