1.0.0-alpha.1 • Published 5 years ago

normalized-fs v1.0.0-alpha.1

Weekly downloads
6
License
ISC
Repository
github
Last release
5 years ago

normalized-fs

GitHub license CircleCI Status AppVeyor Status Coverage Dependencies

A drop-in replacement for fs that aims to normalize the behavior across different platforms and environments, and to make filesystem access more resilient to errors.

Overall improvements over fs module

  • Queues up open and readdir calls, and retries them once something closes if there is an EMFILE error from too many open file descriptors.
  • Fixes broken lchmod for Node versions prior to 0.6.2.
  • Implements fs.lutimes if possible, otherwise it becomes a no-op.
  • Ignores EINVAL and EPERM errors in chown, fchown or lchown if the user isn't root.
  • If lchmod and lchown are unavailable, they become no-ops.
  • Retries reading a file if read results in EAGAIN error.

Windows-specific improvements

  • It makes renaming more resilient by retrying a rename for up to 60 seconds if EACCESS or EPERM error occurs. This can happen if the path has been locked by another process.

Installation

npm

$ npm install normalized-fs

yarn

$ yarn add normalized-fs

Usage

// import just like with fs
import fs from 'normalized-fs';

// now go and do stuff with it...
fs.readFileSync('some-file-or-whatever');

Global patching

If you want to patch the global fs module (or any other fs-like module) you can do this:

NOTE: This should only ever be done at the top-level application layer, in order to delay on EMFILE errors from any fs-using dependencies. You should not do this in a library, because it can cause unexpected delays in other parts of the program.

import realFs from 'fs';
import nfs from 'normalized-fs';
nfs.normalize(realFs);

Credits

Big thanks to isaacs who created graceful-fs which this package is based upon.