fsu
Unique file name with streams support
Last updated 11 years ago by velocityzen .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install fsu 
SYNC missed versions from official npm registry.

fsu (fs unique)

NPM Version NPM Downloads

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 but open for writing and creates unique filename.

const fsu = require('fsu');

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

async writeFileUnique(path, data, [options])

Same as fs.writeFile but creates unique filename.

const fsu = require('fsu');

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

createWriteStreamUnique(path, [options])

Same as fs.createWriteStream but returns writable stream for unique file.

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

Current Tags

  • 2.0.2                                ...           latest (5 years ago)

15 Versions

  • 2.0.2                                ...           5 years ago
  • 2.0.1                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.1.1                                ...           7 years ago
  • 1.1.0                                ...           7 years ago
  • 1.0.4                                ...           7 years ago
  • 1.0.3                                ...           8 years ago
  • 1.0.2                                ...           8 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           9 years ago
  • 0.2.2                                ...           9 years ago
  • 0.2.1                                ...           10 years ago
  • 0.2.0                                ...           10 years ago
  • 0.1.1                                ...           11 years ago
  • 0.1.0                                ...           11 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE