Socket
Book a DemoInstallSign in
Socket

any-fs

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

any-fs

Abstract away any NodeJS fs-style object into a smarter interface

latest
Source
npmnpm
Version
0.4.1
Version published
Maintainers
1
Created
Source

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:

  • Node's fs
  • WebDAV-fs
  • Dropbox-fs (v0.0.5 and up)

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.

Keywords

fs

FAQs

Package last updated on 12 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts