Socket
Socket
Sign inDemoInstall

fsu

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    fsu

Unique file name with streams support


Version published
Weekly downloads
1.2M
increased by0.99%
Maintainers
1
Install size
9.78 kB
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 12 Mar 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc