Package Exports
- safefs
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (safefs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Safe FS 
Say goodbye to EMFILE errors! Open only as many files as the operating system supports
Install
Backend
- Install Node.js
npm install --save safefs
Frontend
Usage
Example
// Import
var safefs = require('safefs');
// Indicate we're wanting to open a file and reserve our space in the queue
// If there is space in the pool, our callback will run right away
// If there isn't space, our callback will fire as soon as there is
safefs.openFile(function(){
// We just got some available space, lets do our stuff with the file
require('fs').writeFileSync('some-file', 'data')
// Once we're done, indicate it, so that other tasks can swim in the pool too
safefs.closeFile();
});
// If we're working with an asynchronous function, it'll look like this
safefs.openFile(function(){
require('fs').writeFile('some-file', 'data', function(err){
safefs.closeFile();
});
});
// as we only want to close file once we are completely done with it
// However, that's pretty annoying have to wrap all our calls in openFile and closeFile
// so it's a good thing that safefs provides wrappers for all the asynchronous fs methods for us
// allowing us to just do
safefs.writeFile('some-file', 'data', function(err){
// all done
});
// which will open and close the spot in the pool for us automatically, yay!
Methods
- Custom methods:
openFile(next)
closeFile()
- Wrapped fs/path methods:
readFile
writeFile
appendFile
mkdir
stat
readdir
unlink
rmdir
exists
existsSync
Notes
- You should call
openFile
before andafterFile
after ALL file system interaction - To make this possible, we maintain the following globals:
numberOfOpenFiles
- defaults to0
maxNumberOfOpenFiles
- defaults toprocess.env.NODE_MAX_OPEN_FILES
if available, otherwise sets to100
waitingToOpenFileDelay
- defaults to100
History
You can discover the history inside the History.md file
License
Licensed under the incredibly permissive MIT License
Copyright © 2013+ Bevry Pty Ltd
Copyright © 2011-2012 Benjamin Arthur Lupton