Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

webdav-fs

perry-mitchell4kMIT4.0.1TypeScript support: included

WebDAV wrapper for Node's fs

webdav, fs, node, remote, readFile, readdir, stream

readme

WebDAV-FS

Node FS wrapper for WebDAV services

Node fs wrapper for WebDAV. Perform basic filesystem tasks in a similar manner to using async fs methods like readdir and writeFile. webdav-fs uses webdav-client under the hood.

build status npm version npm downloads

Installation

Install webdav-fs using npm:

npm install webdav-fs --save

Supports NodeJS 14 and above. For Node 10, use v3, for Node 8, use v2.

Examples

You can use webdav-fs in authenticated or non-authenticated mode:

// Using authentication:
import { createAdapter } from "webdav-fs";

const wfs = createAdapter("http://example.com/webdav/", {
    username: "username",
    password: "password"
});

wfs.readdir("/Work", (err, contents) => {
    if (!err) {
        console.log("Dir contents:", contents);
    } else {
        console.log("Error:", err.message);
    }
});
// Without using authentication:
import { createAdapter } from "webdav-fs";

const wfs = createAdapter("http://example.com/webdav/");

wfs.stat("/report.docx", (err, data) => {
    console.log("Is file:", data.isFile());
});

For more control over the HTTP/TLS connection options, you can pass an instance of http.Agent or https.Agent:

import { Agent } from "https";
import { createAdapter } from "webdav-fs";

const agent = new Agent({
    keepAlive: true
    // we can also control certificate verification behaviour here
});

const wfs = createAdapter("https://example.com/webdav/", {
    username: "username",
    password: "password",
    httpsAgent: agent
});

API

The following methods are available on the webdav-fs module:

createReadStream(path[, options])

Create a read stream on a remote file:

wfs
    .createReadStream("/dir/somefile.dat")
    .pipe(fs.createWriteStream("./somefile.dat"));

The options object supports overriding remote headers as well as a range (start and end as byte indexes). When specifying a range, only the start value is required (if end is not provided the rest of the file is read).

The following requests the first 300 bytes of a file:

const myPartialStream = wfs.createReadStream("/dir/somefile.dat", { start: 0, end: 299 });

createWriteStream(path[, options])

Create a write stream for a remote file:

fs
    .createReadStream("./myFile.dat")
    .pipe(wfs.createWriteStream("/data/myFile.dat"));

The options object supports overriding remote headers.

mkdir(path, callback)

Create a remote directory:

wfs.mkdir("/remote/dir", err => {
    // handle error if truthy
});

readdir(path[, mode], callback)

Read the contents of a remote directory:

wfs.readdir("/some/remote/path/", "node", (err, contents) => {
    // callback is an array of filenames
});

mode is an optional processing mode, where:

  • 'node' (the default mode) will output an array of filename strings
  • 'stat' will output an array of stat objects (plus a name field)

readFile(path, [encoding,] callback)

Read the contents of a remote file:

wfs.readFile("/website/index.php", "utf8", (err, data) => {
    // data is the contents of the file
    // encoding is optional
});

encoding is optional and can be either utf8 (default - returns a string) or binary (returns a Buffer).

rename(currentPath, destinationPath, callback)

Move/rename a file to another location/name. This does not create new directories for nested files (moving a file into a new directory will not work).

wfs.rename("/my-document.docx", "/Documents/personal.docx", err => {
    // handle error
});

stat(path, callback)

Stat a remote file:

wfs.stat("/the-internet.dat", (err, fileStat) => {
    console.log(fileStat);
});

A stat has the following properties:

Property Type Description
isFile Function Check if the item is a file
isDirectory Function Check if the item is a directory
mtime Number Last modification timestamp
size Number Size of the item in bytes

Delete a remote file or directory:

wfs.unlink("/remote/path", (err) => {
    // handle error if truthy
});

writeFile(path, data, [encoding,] callback)

Write data to a remote file:

wfs.writeFile("/Temp/im-here.txt", "This is a saved file! REALLY!!", err => {
    if (err) {
        console.error(err.message);
    }
});

writeFile supports writing binary files as well:

fs.readFile(sourceFile, "binary", (err, data) => {
    wfs.writeFile(destFile, data, "binary", function(err) {
        // handle error
    });
});

writeFile supports just a couple of encodings:

  • utf8
  • binary

When writing binary files, data must either be a binary string from a read file in Node (then passed to new Buffer(data, "binary")) or a Buffer.

Usage

Browser usage and CORS

This library isn't exactly designed for use within the browser, although it might work if bundled using a tool like Webpack.

When in the browser take care with policies such as CORS - If the server is not configured correctly WebDAV requests will fail. Make sure that you return the correct CORS headers. Issues surrounding CORS errors without any indication that an error lies in this library will be closed.

changelog

WebDAV-fs changelog

v4.0.1

2021-11-18

  • Bugfix:
    • Package.json script entry incorrect

v4.0.0

2021-10-17

  • Upgrade WebDAV client to v4
  • Typescript
  • Deprecate Node < 14

v3.0.0

2020-10-08

  • Upgrade WebDAV client to 3.4.0
  • Deprecate Node 6 and 8 (min 10)
  • Upgrade all dependencies

v2.0.0

2018-12-11

  • Upgrade webdav to 2.0.0
    • Use axios instead of node-fetch
  • Move mode parameter in readdir method (second optional parameter)

v1.12.0

2018-11-11

  • Expose agent parameter in constructor to support custom HTTP/S agents being passed
  • Upgrade webdav to 1.6.1
    • Bugfix: Requests fail on Seafile instances
    • Removed restrictive namespace detection on responses

v1.11.0

2018-09-15

  • Upgrade webdav to 1.6.0
    • Bugfix: Directory contents fetch fails on Windows
    • Bugfix: Moving items to directories with spaces fails
  • Update dependencies, audit

v1.10.2

2018-07-07

  • Upgrade webdav to 1.5.3
    • Bugfix: Fix encoding issues with special characters

v1.10.1

2018-03-25

  • Upgrade webdav to 1.5.2
    • (#56) readdir includes parent directory

v1.10.0

2018-03-24

v1.9.0

2018-03-15

  • Upgrade webdav to 1.4.0 (Change deepmerge to merge)

v1.8.0

2018-02-25

  • Upgrade webdav to 1.2.0 (Fix unicode paths)

v1.7.0

2018-02-21

  • Update dependencies
  • Upgrade webdav to 1.1.2 (Fix directory contents bug)

v1.6.0

2017-11-12

  • Upgrade webdav to 1.1.0

v1.5.0

2017-07-05

  • Expose webdav's setFetchMethod function

v1.4.0

2017-07-03

  • Upgrade webdav to 1.0.0-rc1
  • Add createWriteStream method

v1.3.0

2017-06-24

  • Upgrade webdav to 0.10.0
    • Disable window.fetch for improved stability

v1.2.0

2017-06-08

  • Add createReadStream method for streaming remote file contents (includes start and end support)

v1.1.0

2017-06-03

  • Upgrade webdav to 0.7.0
    • Remove lodash

v1.0.0

2017-04-18

  • Upgrade webdav to 0.6.0
    • Add support for non-prefixed response nodes
    • HTTP response codes in errors

v1.0.0-rc7

2017-02-11

  • Upgrade webdav to 0.5.0
    • Use window.fetch where available

v1.0.0-rc6

2017-02-05

  • Upgrade webdav to 0.4.1
    • Custom header support

v1.0.0-rc5

2017-01-18

  • Upgrade webdav (core) to 0.3.0
    • Remove node querystring dependency (downstream support)

v1.0.0-rc3

2017-01-17

  • Added type flag for downstream (any-fs) recognition
  • Removed support for Node 0.12

v1.0.0-rc2

2016-10-24

  • Updated webdav-client to 0.1.1
    • Fixes auth URLs with special characters in the username or password

v1.0.0-rc1

2016-10-15

v0.7.1

2016-10-01

  • Fixed a bug where readFile would return corrupted data when used in "binary" mode. The issue was to do with the node-fetch package which didn't have appropriate support for buffer output at the time of original implementation. node-fetch was updated in this release.

v0.7.0

2016-08-02

  • Added stat support to readdir, which can now optionally return an array of stat objects instead of just filename strings. Due to complicated configurations like this, and their unintuitive nature in their default behaviour, webdav-fs may drift from its fs alignment at some stage in the near future (possibly for v1.0).

v0.6.0

2016-04-05

  • Added support for renaming files and directories, which also allows for the moving of items.