nor-fs
Asynchronous filesystem library with chainable Q-based promises for Node.js.
It also has chainable synchronous interface.
Sample Use Case
So, let's say you need to do something with the filesystem and Node.js.
You also want to use promises. (Actually, we have fs.sync
for synchronous calls, too.)
Sure, you could use q-io -- but you would still feel like you're missing something, it could be easier and simpler!
The solution for the problem is to use our library which uses both Q and nor-extend to enable chainable promises.
Here's an example of creating a file named hello.txt
and changing permissions:
fs;
Here is how you would need to do it with traditional promises:
fs;
Quick Introduction
All methods are wrappers for fs
module
(Node.js File System module)
with same names and arguments when possible.
Asynchronous methods
Asynchronous methods take the same arguments as fs
but instead of callbacks
every asynchronous method returns an extended Q promise which can be chained.
fs;
You can shorten it with the path-based API:
fs;
Synchronous methods
Synchronous methods take same arguments as fs
module but are chainable, and
methods don't have leading Sync
in their names.
Synchronous API is provided through fs.sync
:
fssync;
You can shorten it with the path-based API:
fssync;
Getting Started
Installation
Installing from the NPM
You may install it from the NPM: npm install nor-fs
.
Dependencies
- Node.js v0.10 or newer
- nor-extend
- q
Require
var fs = ;
Issues, pull requests and updates
Issues, questions and other resources
Do not hesitate to create issues or ask questions for any reason. We're happy to receive pull requests, too.
Social media
Our company is @sendanorcom at Twitter and our channel on Freenode IRC network is #sendanor.
Original author and the lead is @jheusala.
Versioning
nor-fs
follows Semantic Versioning and is using
rolling release model.
We'll introduce new features when neccessary at a good pace. We'll try our best not to break backwards compatibility and if that happens we'll fix it as fast as possible.
TODO
Upcoming release
- Not decided yet.
Future releases
- Issue #3 -- Support for fs.ReadStream and fs.WriteStream
- Issue #4 -- Support for watch features
- Issue #5 --
.lchown()
and.lchmod()
not tested on Mac OS X
Full API Documentation
require("nor-fs")
The root of the library is an instance of FileSystem.
It also has these properties:
- fs.FileSystem
- fs.Path
- fs.Descriptor
- fs.SyncFileSystem
- fs.sync.Path
- fs.sync.Descriptor
- fs.sync -- Instance of class
fs.SyncFileSystem
And these methods:
- fs.path(name) -- Returns instance of
fs.Path(name)
- fs.sync.path(name) -- Returns instance of
fs.sync.Path(name)
Class FileSystem
new FileSystem()
The constructor of the FileSystem
objects.
Warning! You should not need to use it directly. However if you find any essential use for it in the userland, we would like hear it!
fs.rename(oldPath, newPath)
Renames a file from oldPath
to newPath
. Asynchronous
rename(2) which renames a file from oldPath
to newPath
and returns an extended chainable promise.
fs;
See also fs.rename from Node.js API Documentation
fs.ftruncate(fd, len)
Truncates a file pointed by a file descriptor to a specified length. Asynchronous ftruncate(2) which returns an extended chainable promise.
fs;
Note! Usually there is no need to call this method because fs.open()
returns an instance of fs.Descriptor
which can be chained.
See also fs.ftruncate from Node.js API Documentation.
fs.truncate(path, len)
Truncates a file pointed by a path to a specified length.
Asynchronous truncate(2) which returns an extended chainable promise.
fs;
See also fs.truncate at Node.js API documentation.
fs.chown(path, uid, gid)
Change file owner and group. Asynchronous chown(2) which returns an extended chainable promise.
fs;
See also fs.chown from the original Node.js API documentation.
Warning! It is automatically tested, but not so well, since normally users cannot change ownership of a file to other users.
fs.fchown(fd, uid, gid)
Change file owner and group pointed by a file descriptor. Asynchronous fchown(2) which returns an extended chainable promise.
fs;
Note! Usually there is no need to call this method because fs.open()
returns an instance of fs.Descriptor
which can be chained.
See also fs.fchown in the original Node.js API documentation.
fs.lchown(path, uid, gid)
Asynchronous lchown(2) which returns an extended chainable promise.
fs;
See also fs.lchown in the original Node.js API documentation.
Warning! Only available on MAC OS X system.
Warning! It is not tested yet. I don't have a MAC OS X system.
fs.chmod(path, mode)
Change file mode bits. Asynchronous chmod(2) which returns an extended chainable promise.
fs;
See also fs.chmod in the original Node.js API documentation.
fs.fchmod(fd, mode)
Change file mode bits. Asynchronous fchmod(2) which returns an extended chainable promise.
fs;
Note! Usually there is no need to call this method because fs.open()
returns an instance of fs.Descriptor
which can be chained.
See also fs.fchmod in the original Node.js API documentation.
fs.lchmod(path, mode)
Asynchronous lchmod(2) which returns an extended chainable promise.
fs;
See also fs.lchmod in the original Node.js API documentation.
Warning! Only available on MAC OS X system.
Warning! It is not tested yet. I don't have a MAC OS X system.
fs.stat(path)
Display file or file system status. Asynchronous stat(2) which returns a promise.
In case of a successful call instance of require("fs").Stats
object is passed
on. See the fs.stat in the original Node.js API documentation.
fs;
...which will return:
{ dev: 2113,
mode: 33204,
nlink: 1,
uid: 1000,
gid: 1000,
rdev: 0,
blksize: 4096,
ino: 5014598,
size: 0,
blocks: 0,
atime: Sun Sep 15 2013 09:03:33 GMT+0300 (EEST),
mtime: Sun Sep 15 2013 09:03:33 GMT+0300 (EEST),
ctime: Sun Sep 15 2013 09:03:33 GMT+0300 (EEST) }
fs.lstat(path)
Asynchronous lstat(2) which returns a promise.
lstat()
is identical to stat()
, except that if path is a
symbolic link, then the link itself is stat-ed, not the file that it refers to.
See original Node.js API documentation.
fs.fstat(fd)
Asynchronous fstat(2) which returns a promise.
fstat()
is identical to stat()
, except that the file to be stat-ed is specified by the file descriptor fd
.
fs;
Note! Usually there is no need to call this method because fs.open()
returns an instance of fs.Descriptor
which can be chained.
See original Node.js API documentation.
fs.link(srcpath, dstpath)
Make a new name for a file (by hard linking). Asynchronous link(2). Returns an extended chainable promise.
fslink"foo.txt" "bar.txt";
See original Node.js API documentation.
fs.symlink(srcpath, dstpath, [type])
Make a new name for a file (by symlinking). Asynchronous symlink(2). Returns an extended chainable promise.
fs;
See original Node.js API documentation.
fs.readlink(path)
Read value of a symbolic link. Asynchronous readlink(2). Returns an extended chainable promise.
fs;
See original Node.js API documentation.
fs.realpath(path, [cache])
Return the canonicalized absolute pathname. Asynchronous realpath(3). Returns an extended chainable promise.
fs;
See original Node.js API documentation.
fs.unlink(path)
Delete a name and possibly the file it refers to. Asynchronous unlink(2). Returns an extended chainable promise.
fs;
See original Node.js API documentation.
fs.unlinkIfExists(path)
Delete a name and possibly the file it refers to if that file exists. Asynchronous unlink(2). Only tries to unlink if the file exists. Returns an extended chainable promise.
fs;
fs.rmdir(path)
Delete a directory. Asynchronous rmdir(2). Returns an extended chainable promise.
fs;
See original Node.js API documentation.
fs.rmdirIfExists(path)
Delete a directory if it exists. Asynchronous rmdir(2). Only tries to remove the directory if it exists. Returns an extended chainable promise.
fs;
See original Node.js API documentation.
fs.mkdir(path, [mode])
Create a directory. Asynchronous mkdir(2). Returns an extended chainable promise.
fs;
See original Node.js API documentation.
fs.mkdirIfMissing(path, [mode])
Create a directory if it does not exist. Asynchronous mkdir(2). Only tries to create the directory if it does not already exist. Returns an extended chainable promise.
fs;
See original Node.js API documentation.
fs.readdir(path)
Read directory entry. Asynchronous readdir(3). Returns an extended chainable promise.
fs;
See original Node.js API documentation.
fs.open(path, flags, [mode])
Open and possibly create a file or device. Asynchronous file open. See open(2). Returns an extended chainable promise.
var fd buffer;fs;
See also original Node.js API documentation.
fs.close(fd)
Close a file descriptor. Asynchronous close(2). Returns an extended chainable promise.
See the example for fs.open().
See original Node.js API documentation.
fs.utimes(path, atime, mtime)
Change file timestamps of the file referenced by the supplied path.
Returns an extended chainable promise.
var now = ;fs;
See original Node.js API documentation.
fs.futimes(fd, atime, mtime)
Change the file timestamps of a file referenced by the supplied file descriptor.
Returns an extended chainable promise.
var now = ;fs;
Note! Usually there is no need to call this method because fs.open()
returns an instance of fs.Descriptor
which can be chained.
See original Node.js API documentation.
fs.fsync(fd)
Synchronize a file's in-core state with storage device. Asynchronous fsync(2). Returns an extended chainable promise.
fs;
Note! Usually there is no need to call this method because fs.open()
returns an instance of fs.Descriptor
which can be chained.
See original Node.js API documentation.
fs.write(fd, buffer, offset, length, position)
Returns an extended chainable promise which will on success pass on an object with properties .written
and .buffer
.
fs;
Note! Usually there is no need to call this method because fs.open()
returns an instance of fs.Descriptor
which can be chained:
var fd;fs;
See also [original Node.js API See original Node.js API documentation.
fs.read(fd, buffer, offset, length, position)
Returns an extended chainable promise which will on success pass on an object with properties .bytesRead
and .buffer
.
var fd buffer;fs;
Note! Usually there is no need to call this method because fs.open()
returns an instance of fs.Descriptor
which can be chained. See the example
for fs.open().
See original Node.js API documentation.
fs.readFile(filename, [options])
Read file contents.
Returns an extended chainable promise.
fs;
See original Node.js API documentation.
fs.writeFile(filename, data, [options])
Write file.
Returns an extended chainable promise.
fs;
See original Node.js API documentation.
fs.appendFile(filename, data, [options])
Append to file.
Returns an extended chainable promise.
fs;
See original Node.js API documentation.
fs.exists(path)
Checks if a path exists. Returns an extended chainable promise.
fs;
See original Node.js API documentation.
Class: fs.Stats
See original Node.js API documentation.
Class FileDescriptor
new FileDescriptor(fd)
The constructor of the FileDescriptor
objects.
It takes single argument fd
and if not defined will throw TypeError
as exception.
Each FileDescriptor
object will have a propery of .fd
.
Warning! Normally you shouldn't need to use it directly but it might be neccessary when using 3rd party addons which deal with raw fd
's.
fd.stat()
Alias for fd.fstat.
var fd buffer;fs;
fd.fstat()
Alias for fd.stat.
Returns an extended chainable promise.
See example above fd.stat.
See original Node.js API documentation.
fd.truncate(len)
Alias for fd.ftruncate.
fs;
fd.ftruncate(len)
Alias for fd.truncate.
Returns an extended chainable promise.
See original Node.js API documentation.
fd.chown(uid, gid)
Alias for fd.fchown.
fs;
fd.fchown(uid, gid)
Alias for fd.chown.
Returns an extended chainable promise.
See original Node.js API documentation.
fd.chmod(mode)
Alias for fd.fchmod.
fs;
fd.fchmod(mode)
Alias for fd.fchmod.
Returns an extended chainable promise.
See original Node.js API documentation.
fd.utimes(atime, mtime)
Alias for fd.futimes.
var now = ;fs;
fd.futimes(atime, mtime)
Alias for fd.utimes.
Returns an extended chainable promise.
See original Node.js API documentation.
fd.write(buffer, offset, length, position)
Returns an extended chainable promise.
var fd;var buffer = "Hello World" "utf8";fs;
See original Node.js API documentation.
fd.read(buffer, offset, length, position)
Returns an extended chainable promise.
var fd buffer;fs;
See original Node.js API documentation.
fd.sync()
Alias for fd.fsync.
var fd;var buffer = "Hello World" "utf8";fs;
fd.fsync()
Returns an extended chainable promise.
See original Node.js API documentation.
Class fs.SyncFileSystem
These are chainable synchronous wrappers for original calls like require("fs").{call}Sync(...)
.
new fs.SyncFileSystem()
fs.sync.rename(oldPath, newPath)
fs.sync.ftruncate(fd, len)
fs.sync.truncate(path, len)
fs.sync.chown(path, uid, gid)
fs.sync.lchown(path, uid, gid)
fs.sync.fchown(fd, uid, gid)
fs.sync.chmod(path, mode)
fs.sync.fchmod(fd, mode)
fs.sync.lchmod(path, mode)
fs.sync.stat(path)
fs.sync.lstat(path)
fs.sync.fstat(fd)
fs.sync.link(srcpath, dstpath)
fs.sync.symlink(srcpath, dstpath, [type])
fs.sync.readlink(path)
fs.sync.realpath(path, [cache])
fs.sync.unlink(path)
fs.sync.rmdir(path)
fs.sync.mkdir(path, [mode])
fs.sync.readdir(path)
fs.sync.close(fd)
fs.sync.open(path, flags, [mode])
Returns with promise of an instance of fs.sync.Descriptor
.
fs.sync.utimes(path, atime, mtime)
fs.sync.futimes(fd, atime, mtime)
fs.sync.fsync(fd)
fs.sync.write(fd, buffer, offset, length, position)
fs.sync.read(fd, buffer, offset, length, position)
fs.sync.readFile(filename, [options])
fs.sync.writeFile(filename, data, [options])
fs.sync.appendFile(filename, data, [options])
fs.sync.exists(path)
Class fs.sync.Descriptor
These are chainable synchronous wrappers for original calls like require("fs").{call}Sync(fd, ...)
.
new fs.sync.Descriptor(fd)
syncfd.ftruncate(len)
syncfd.fchown(uid, gid)
syncfd.fchmod(mode)
syncfd.fstat()
syncfd.close()
syncfd.futimes(atime, mtime)
syncfd.fsync()
syncfd.write(buffer, offset, length, position)
syncfd.read(buffer, offset, length, position)
Support for fs.ReadStream and fs.WriteStream
Warning! These are not implemented yet.
fs.createReadStream(path, [options])
Class: fs.ReadStream
Event: "open"
fs.createWriteStream(path, [options])
Class: fs.WriteStream
Event: "open"
file.bytesWritten
Support for watch features
Warning! These are not implemented yet.
fs.watchFile(filename, [options], listener)
Warning! It is not implemented yet.
Returns an extended chainable promise.
See original Node.js API documentation.
fs.unwatchFile(filename, [listener])
Warning! It is not implemented yet.
Returns an extended chainable promise.
See original Node.js API documentation.
fs.watch(filename, [options], [listener])
Warning! It is not implemented yet.
Returns an extended chainable promise.
See original Node.js API documentation.
Class: fs.FSWatcher
watcher.close()
Event: "change"
Event: "error"
Tests
Tests can be run by npm test
.
Licence
The MIT License (MIT), see the LICENCE file.
Commercial Support
You can buy commercial support from Sendanor.