Comparing version 2.0.3 to 3.0.0
// Generated by CoffeeScript 1.6.2 | ||
var fsUtil, pathUtil, safefs, _ref, _ref1, _ref2, _ref3; | ||
var TaskGroup, fsUtil, pathUtil, safefs, _base, _ref, _ref1, _ref2; | ||
@@ -8,35 +8,23 @@ fsUtil = require('fs'); | ||
if ((_ref = global.numberOfOpenFiles) == null) { | ||
global.numberOfOpenFiles = 0; | ||
} | ||
TaskGroup = require('taskgroup').TaskGroup; | ||
if ((_ref1 = global.maxNumberOfOpenFiles) == null) { | ||
global.maxNumberOfOpenFiles = (_ref2 = process.env.NODE_MAX_OPEN_FILES) != null ? _ref2 : 100; | ||
if ((_ref = global.safefsGlobal) == null) { | ||
global.safefsGlobal = {}; | ||
} | ||
if ((_ref3 = global.waitingToOpenFileDelay) == null) { | ||
global.waitingToOpenFileDelay = 100; | ||
if ((_ref1 = (_base = global.safefsGlobal).pool) == null) { | ||
_base.pool = new TaskGroup().setConfig({ | ||
concurrency: (_ref2 = process.env.NODE_MAX_OPEN_FILES) != null ? _ref2 : 100, | ||
pauseOnError: false | ||
}).run(); | ||
} | ||
safefs = { | ||
openFile: function(next) { | ||
if (global.numberOfOpenFiles < 0) { | ||
throw new Error("safefs.openFile: the numberOfOpenFiles is [" + global.numberOfOpenFiles + "] which should be impossible..."); | ||
} | ||
if (global.numberOfOpenFiles >= global.maxNumberOfOpenFiles) { | ||
setTimeout(function() { | ||
return safefs.openFile(next); | ||
}, global.waitingToOpenFileDelay); | ||
} else { | ||
++global.numberOfOpenFiles; | ||
next(); | ||
} | ||
return this; | ||
openFile: function(fn) { | ||
global.safefsGlobal.pool.addTask(fn); | ||
return safefs; | ||
}, | ||
closeFile: function(next) { | ||
--global.numberOfOpenFiles; | ||
if (typeof next === "function") { | ||
next(); | ||
} | ||
return this; | ||
closeFile: function() { | ||
console.log('safefs.closeFile has been deprecated, please use the safefs.openFile completion callback to close files'); | ||
return safefs; | ||
}, | ||
@@ -50,3 +38,3 @@ getParentPathSync: function(p) { | ||
ensurePath: function(path, options, next) { | ||
var _ref4; | ||
var _ref3; | ||
@@ -60,3 +48,3 @@ if (next == null) { | ||
} | ||
if ((_ref4 = options.mode) == null) { | ||
if ((_ref3 = options.mode) == null) { | ||
options.mode = null; | ||
@@ -86,3 +74,3 @@ } | ||
}); | ||
return this; | ||
return safefs; | ||
}, | ||
@@ -94,9 +82,9 @@ readFile: function(path, options, next) { | ||
} | ||
safefs.openFile(function() { | ||
safefs.openFile(function(closeFile) { | ||
return fsUtil.readFile(path, options, function(err, data) { | ||
safefs.closeFile(); | ||
closeFile(); | ||
return next(err, data); | ||
}); | ||
}); | ||
return this; | ||
return safefs; | ||
}, | ||
@@ -112,5 +100,5 @@ writeFile: function(path, data, options, next) { | ||
} | ||
return safefs.openFile(function() { | ||
return safefs.openFile(function(closeFile) { | ||
return fsUtil.writeFile(path, data, options, function(err) { | ||
safefs.closeFile(); | ||
closeFile(); | ||
return next(err); | ||
@@ -120,3 +108,3 @@ }); | ||
}); | ||
return this; | ||
return safefs; | ||
}, | ||
@@ -132,5 +120,5 @@ appendFile: function(path, data, options, next) { | ||
} | ||
return safefs.openFile(function() { | ||
return safefs.openFile(function(closeFile) { | ||
return fsUtil.appendFile(path, data, options, function(err) { | ||
safefs.closeFile(); | ||
closeFile(); | ||
return next(err); | ||
@@ -140,3 +128,3 @@ }); | ||
}); | ||
return this; | ||
return safefs; | ||
}, | ||
@@ -151,45 +139,45 @@ mkdir: function(path, mode, next) { | ||
} | ||
safefs.openFile(function() { | ||
safefs.openFile(function(closeFile) { | ||
return fsUtil.mkdir(path, mode, function(err) { | ||
safefs.closeFile(); | ||
closeFile(); | ||
return next(err); | ||
}); | ||
}); | ||
return this; | ||
return safefs; | ||
}, | ||
stat: function(path, next) { | ||
safefs.openFile(function() { | ||
safefs.openFile(function(closeFile) { | ||
return fsUtil.stat(path, function(err, stat) { | ||
safefs.closeFile(); | ||
closeFile(); | ||
return next(err, stat); | ||
}); | ||
}); | ||
return this; | ||
return safefs; | ||
}, | ||
readdir: function(path, next) { | ||
safefs.openFile(function() { | ||
safefs.openFile(function(closeFile) { | ||
return fsUtil.readdir(path, function(err, files) { | ||
safefs.closeFile(); | ||
closeFile(); | ||
return next(err, files); | ||
}); | ||
}); | ||
return this; | ||
return safefs; | ||
}, | ||
unlink: function(path, next) { | ||
safefs.openFile(function() { | ||
safefs.openFile(function(closeFile) { | ||
return fsUtil.unlink(path, function(err) { | ||
safefs.closeFile(); | ||
closeFile(); | ||
return next(err); | ||
}); | ||
}); | ||
return this; | ||
return safefs; | ||
}, | ||
rmdir: function(path, next) { | ||
safefs.openFile(function() { | ||
safefs.openFile(function(closeFile) { | ||
return fsUtil.rmdir(path, function(err) { | ||
safefs.closeFile(); | ||
closeFile(); | ||
return next(err); | ||
}); | ||
}); | ||
return this; | ||
return safefs; | ||
}, | ||
@@ -200,9 +188,9 @@ exists: function(path, next) { | ||
exists = fsUtil.exists || pathUtil.exists; | ||
safefs.openFile(function() { | ||
safefs.openFile(function(closeFile) { | ||
return exists(path, function(exists) { | ||
safefs.closeFile(); | ||
closeFile(); | ||
return next(exists); | ||
}); | ||
}); | ||
return this; | ||
return safefs; | ||
}, | ||
@@ -209,0 +197,0 @@ existsSync: function(path) { |
{ | ||
"name": "safefs", | ||
"version": "2.0.3", | ||
"description": "Say goodbye to EMFILE errors! Open only as many files as the operating system supports", | ||
"version": "3.0.0", | ||
"description": "Stop getting EMFILE errors! Open only as many files as the operating system supports.", | ||
"homepage": "https://github.com/bevry/safefs", | ||
@@ -26,4 +26,6 @@ "keywords": [ | ||
}, | ||
"dependencies": { | ||
"taskgroup": "~3.0.0" | ||
}, | ||
"devDependencies": { | ||
"taskgroup": "~2.0.0", | ||
"coffee-script": "~1.6.2", | ||
@@ -30,0 +32,0 @@ "joe": "~1.1.2", |
@@ -1,88 +0,52 @@ | ||
# Safe FS [![Build Status](https://secure.travis-ci.org/bevry/safefs.png?branch=master)](http://travis-ci.org/bevry/safefs) | ||
Say goodbye to EMFILE errors! Open only as many files as the operating system supports | ||
# Safe FS | ||
[![Build Status](https://secure.travis-ci.org/bevry/safefs.png?branch=master)](http://travis-ci.org/bevry/safefs) | ||
[![NPM version](https://badge.fury.io/js/safefs.png)](https://npmjs.org/package/safefs) | ||
Stop getting EMFILE errors! Open only as many files as the operating system supports. | ||
## Install | ||
### Backend | ||
1. [Install Node.js](http://bevry.me/node/install) | ||
2. `npm install --save safefs` | ||
### Frontend | ||
1. [See Browserify](http://browserify.org) | ||
## Usage | ||
### Example | ||
``` javascript | ||
// 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(); | ||
}); | ||
The following [file system](http://nodejs.org/docs/latest/api/all.html#all_file_system) methods are available (but wrapped in safe way to prevent EMFILE errors): | ||
// 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(); | ||
- `readFile(path, options?, next)` | ||
- `writeFile(path, data, options?, next)` - will also attempt to ensure the path exists | ||
- `appendFile(path, data, options?, next)` - will also attempt to ensure the path exists | ||
- `mkdir(path, mode?, next)` - mode defaults to `0o777 & (~process.umask())` | ||
- `stat(path, next)` | ||
- `readdir(path, next)` | ||
- `unlink(path, next)` | ||
- `rmdir(path, next)` | ||
- `exists(path, next)` | ||
For other file system interaction, you can do the following: | ||
``` javascript | ||
// get a slot in the file system queue | ||
require('safefs').openFile(function(closeFile){ | ||
// do our file system interaction | ||
require('fs').someOtherMethod(a,b,c,function(err,a,b,c){ | ||
// close the slot we are using in the file system queue | ||
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! | ||
``` | ||
To make this possible we define a global variable called `safefsGlobal` that manages the available slots for interacting with the file system. | ||
### Methods | ||
Arguments suffixed with `?` are optional. | ||
- Custom methods: | ||
- `openFile(next)` | ||
- `closeFile()` | ||
- `getParentPathSync(path)` | ||
- `ensurePath(path, options?, next)` - will attempt to ensure the path exists, options: | ||
- `mode` - defaults to `null` | ||
- Wrapped fs/path methods: | ||
- `readFile(path, options?, next)` | ||
- `writeFile(path, data, options?, next)` - will also attempt to ensure the path exists | ||
- `appendFile(path, data, options?, next)` - will also attempt to ensure the path exists | ||
- `mkdir(path, mode?, next)` - mode defaults to `0o777 & (~process.umask())` | ||
- `stat(path, next)` | ||
- `readdir(path, next)` | ||
- `unlink(path, next)` | ||
- `rmdir(path, next)` | ||
- `exists(path, next)` | ||
- `existsSync(path)` | ||
### Notes | ||
- You should call `openFile` before and `afterFile` after ALL file system interaction | ||
- To make this possible, we maintain the following globals: | ||
- `numberOfOpenFiles` - defaults to `0` | ||
- `maxNumberOfOpenFiles` - defaults to `process.env.NODE_MAX_OPEN_FILES` if available, otherwise sets to `100` | ||
- `waitingToOpenFileDelay` - defaults to `100` | ||
## History | ||
@@ -89,0 +53,0 @@ You can discover the history inside the [History.md](https://github.com/bevry/safefs/blob/master/History.md#files) file |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
9033
1
183
61
+ Addedtaskgroup@~3.0.0
+ Addedambi@2.0.0(transitive)
+ Addedeventemitter2@0.4.14(transitive)
+ Addedtaskgroup@3.0.0(transitive)
+ Addedtypechecker@2.0.8(transitive)