0.4.1 • Published 7 years ago

any-fs v0.4.1

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

Any-fs

Abstract away any filesystem (NodeJS) instance to a smarter interface

Build Status node min version npm version

Purpose

Any-fs was designed to wrap filesystem interfaces like the following:

Into a common interface that provides a more intuitive syntax. Any-fs is designed to blindly take any of these systems and return a common API.

Usage

Node's fs

Usage with the built-in fs interface is the simplest use-case:

const fs = require("fs");
const anyFs = require("any-fs");

const afs = anyFs(fs);

afs
    .readDirectory("/")
    .then(function(contents) {
        console.log(contents.pop());
        // returns something like:
        // {
        //    name: "filename.txt,
        //    isFile: Function,
        //    isDirectory: Function,
        // }
    });

This example returns an array if stat objects.

WebDAV-fs

Once WebDAV-fs has been setup with a remote location, it too can be wrapped by any-fs:

const wfs = require("webdav-fs")(
    "http://example.com/webdav/",
    "username",
    "password"
);
const anyFs = require("any-fs");

const afs = anyFs(fs);
afs
    .readFile("/movie.mp4")
    .then(function(data) {
        // `data` is a Buffer
    });

API

Any-fs supports a few handy commands.

readDirectory(directoryPath, options)

Read the contents of a directory, returning a Promise with an array of stats (refer to the stat output of the wrapped fs module).

ParameterTypeDescription
directoryPathStringThe path to scan
optionsString or ObjectEncoding (string) or config options (object)

options may contain properties mode and encoding (mode being either "node" or "stat"), but it is essentially passed down into whatever fs interface is being wrapped.

options is not used for readdir in Node's 'fs' module.

readFile(filePath, options)

Read the contents of a file, returning a Promise with a Buffer or string.

ParameterTypeDescription
filePathStringThe file to read
optionsString or ObjectEncoding (string) or config options (object)

options.encoding is set to null by default, returning the raw buffer. Use "utf8" to convert it to a UTF8 string.

stat(filePath)

Get statistics on a file or directory - returns a Promise with the stat object.

ParameterTypeDescription
filePathStringThe file or directory to check

writeFile(filePath, data, encoding)

Write data to a file - returns a Promise that resolves after writing has completed.

ParameterTypeDescription
filePathStringThe file to write to
dataBuffer or StringThe data to write
encodingString or undefinedThe encoding to use for writing. utf8 writes text.
0.4.1

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago