# fsu

> Unique file name with streams support

Latest version **2.0.2** (published 2020-03-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install fsu
pnpm add fsu
yarn add fsu
bun add fsu
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2020-03-12 |
| First published | 2014-04-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Alexey Novikov |
| Maintainers | velocityzen |
| Keywords | counter, file, filename, fs, stream, unique |

## Links

- npm: https://www.npmjs.com/package/fsu
- Repository: https://github.com/velocityzen/fsu
- Homepage: https://github.com/velocityzen/fsu#readme
- Issues: https://github.com/velocityzen/fsu/issues
- npm.io page: https://npm.io/package/fsu

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 2.0.2 (latest) — 2020-03-12
- 2.0.1 — 2019-07-28
- 2.0.0 — 2019-07-15
- 1.1.1 — 2018-04-26
- 1.1.0 — 2018-04-25
- 1.0.4 — 2017-08-17
- 1.0.3 — 2017-04-26
- 1.0.2 — 2017-04-24
- 1.0.1 — 2016-08-03
- 1.0.0 — 2016-06-23
- 0.2.2 — 2015-08-04
- 0.2.1 — 2015-04-30
- 0.2.0 — 2015-03-12
- 0.1.1 — 2014-04-06
- 0.1.0 — 2014-04-06

## README

# fsu (fs unique)

[![NPM Version](https://img.shields.io/npm/v/fsu.svg?style=flat-square)](https://www.npmjs.com/package/fsu)
[![NPM Downloads](https://img.shields.io/npm/dt/fsu.svg?style=flat-square)](https://www.npmjs.com/package/fsu)

Unique filenames with streams support

**Checking if a file exists before opening is an anti-pattern that leaves you vulnerable to race conditions: another process can remove the file between the calls to fs.exists() and fs.open(). This functions doesn't use fs.exists functionality. If file doesn't exist this will work like usual fs module methods**

## Instalation
`npm install fsu`

## async openUnique(path, [mode])
Same as [fs.open](http://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback) but open for writing and creates unique filename.

```js
const fsu = require('fsu');

const { fd, path } = await fsu.openUnique("text{_###}.txt");

```

## async writeFileUnique(path, data, [options])
Same as [fs.writeFile](http://nodejs.org/api/fs.html#fs_fs_writefile_filename_data_options_callback) but creates unique filename.

```js
const fsu = require('fsu');

const path = await fsu.writeFileUnique("text{_###}.txt", "test");

```

## createWriteStreamUnique(path, [options])
Same as [fs.createWriteStream](https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options) but returns writable stream for unique file.

```js
const fsu = require('fsu');
let stream = fsu.createWriteStreamUnique("text{_###}.txt");
```

## new path
Stream has a `path` property that contains a new path

## force path creation
Add `force = true` to options, and it will recursively create directories if they are not exist.

## pattern
You must use `{#}` pattern in filename and path. All `#` characters will be change with counter for existing files. Number of `#` means padding for unique counter. **With no pattern in the filename works as usual 'fs' module.**

If we run second example several times filenames will be
```
text.txt
text_001.txt
text_002.txt
```


License: MIT

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