UNPKG

9.36 kBMarkdownView Raw
1![WebDAV](https://raw.githubusercontent.com/perry-mitchell/webdav-client/master/webdav.jpg)
2
3> A WebDAV client written in JavaScript for NodeJS.
4
5[![Build Status](https://travis-ci.org/perry-mitchell/webdav-client.svg?branch=master)](https://travis-ci.org/perry-mitchell/webdav-client) [![npm version](https://badge.fury.io/js/webdav.svg)](https://www.npmjs.com/package/webdav) [![monthly downloads](https://img.shields.io/npm/dm/webdav.svg)](https://www.npmjs.com/package/webdav)
6
7## About
8This client was branched from [webdav-fs](https://github.com/perry-mitchell/webdav-fs) as the core functionality deserved its own repository. As **webdav-fs**' API was designed to resemble NodeJS' fs API, little could be done to improve the adapter interface for regular use.
9
10This WebDAV client library is designed to provide an improved API for low-level WebDAV integration. This client uses `window.fetch` when available in the browser.
11
12Please read our [contribution guide](CONTRIBUTING.md) if you plan on making an issue or PR.
13
14## Installation
15To install for use with NodeJS, execute the following shell command:
16
17```shell
18npm install webdav --save
19```
20
21## Usage
22Usage is very simple ([API](API.md)) - the main exported object is a factory to create adapter instances:
23
24```js
25var createClient = require("webdav");
26
27var client = createClient(
28 "https://webdav-server.org/remote.php/webdav",
29 "username",
30 "password"
31);
32
33client
34 .getDirectoryContents("/")
35 .then(function(contents) {
36 console.log(JSON.stringify(contents, undefined, 4));
37 });
38```
39
40Each method returns a `Promise`.
41
42### Authentication
43`webdav` uses `Basic` authentication by default, if `username` and `password` are provided (if none are provided, no `Authorization` header is specified). It also supports OAuth tokens - simply pass the token data to the `username` field:
44
45```javascript
46createClient(
47 "https://address.com",
48 {
49 "access_token": "2YotnFZFEjr1zCsicMWpAA",
50 "token_type": "example",
51 "expires_in": 3600,
52 "refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA",
53 "example_parameter": "example_value"
54 }
55);
56```
57
58### Adapter methods
59These methods can be called on the object returned from the main factory.
60
61#### copyFile(remotePath, targetPath _[, options]_)
62Copy a file or directory from one path to another.
63
64#### createDirectory(remotePath _[, options]_)
65Create a new directory at the remote path.
66
67#### createReadStream(remotePath _[, options]_)
68Creates a readable stream on the remote path.
69
70Returns a readable stream instance.
71
72##### options.range
73Optionally request part of the remote file by specifying the `start` and `end` byte positions. The `end` byte position is optional and the rest of the file from `start` onwards will be streamed.
74
75```javascript
76var stream = client.createReadStream("/test/image.png", {
77 range: { start: 0, end: 499 } // first 500 bytes
78});
79```
80
81#### createWriteStream(remotePath _[, options]_)
82Creates a writeable stream to a remote path.
83
84Returns a writeable stream instance.
85
86#### deleteFile(remotePath _[, options]_)
87Delete a file or directory at `remotePath`.
88
89#### getDirectoryContents(remotePath _[, options]_)
90Get an array of items within a directory. `remotePath` is a string that begins with a forward-slash and indicates the remote directory to get the contents of.
91
92```js
93client
94 .getDirectoryContents("/MyFolder")
95 .then(function(contents) {
96 console.log(JSON.stringify(contents, undefined, 2));
97 });
98```
99
100The returned value is a Promise, which resolves with an array of [item stat objects](#item-stat).
101
102#### getFileContents(remotePath _[, options]_)
103Get the contents of the file at `remotePath` as a `Buffer` or `String`. `format` can either be "binary" or "text", where "binary" is default.
104
105```js
106var fs = require("fs");
107
108client
109 .getFileContents("/folder/myImage.jpg")
110 .then(function(imageData) {
111 fs.writeFileSync("./myImage.jpg", imageData);
112 });
113```
114
115Or with text:
116
117```js
118client
119 .getFileContents("/doc.txt", { format: "text" })
120 .then(function(text) {
121 console.log(text);
122 });
123```
124
125**Important**: When running on Node, `node-fetch` is used as the default fetch library. `node-fetch` provides the `.buffer()` method for responses, which returns a `Buffer` instance, but other libraries (and standard `fetch`) do not. When the `buffer` method is not available, this library will attempt to use `.arrayBuffer`. It is your responsibility to handle the output and any required conversion. The [`arraybuffer-to-buffer`](https://www.npmjs.com/package/arraybuffer-to-buffer) library makes it easy to convert back to a `Buffer` if you require it.
126
127#### getFileDownloadLink(remotePath _[, options]_)
128Get the external download link of a remote file. Only supported for non-authenticated connections or connections using Basic authentication.
129
130**Important note**: This method exposes the username and password **in the URL** - It is not recommended to send or store any output from this function.
131
132#### getQuota(_[options]_)
133Get quota information. Returns `null` upon failure or an object like so:
134
135```json
136{
137 "used": "12842",
138 "available": "512482001"
139}
140```
141
142Both values are provided in bytes in string form. `available` may also be one of the following:
143
144 * `unknown`: The available space is unknown or not yet calculated
145 * `unlimited`: The space available is not limited by quotas
146
147#### moveFile(remotePath, targetPath _[, options]_)
148Move a file or directory from `remotePath` to `targetPath`.
149
150```js
151// Move a directory
152client.moveFile("/some-dir", "/storage/moved-dir");
153
154// Rename a file
155client.moveFile("/images/pic.jpg", "/images/profile.jpg");
156```
157
158#### putFileContents(remotePath, data _[, options]_)
159Put some data in a remote file at `remotePath` from a `Buffer` or `String`. `data` is a `Buffer` or a `String`. `options` has a property called `format` which can be "binary" (default) or "text".
160
161```js
162var fs = require("fs");
163
164var imageData = fs.readFileSync("someImage.jpg");
165
166client.putFileContents("/folder/myImage.jpg", imageData, { format: "binary" });
167```
168
169```js
170client.putFileContents("/example.txt", "some text", { format: "text" });
171```
172
173`options`, which is **optional**, can be set to an object like the following:
174
175```json
176{
177 "format": "binary",
178 "headers": {
179 "Content-Type": "application/octet-stream"
180 },
181 "overwrite": true
182}
183```
184
185> `options.overwrite` (default: `true`), if set to false, will add an additional header which tells the server to abort writing if the target already exists.
186
187#### stat(remotePath _[, options]_)
188Get the stat properties of a remote file or directory at `remotePath`. Resolved object is a [item stat object](#item-stat).
189
190### Overriding the built-in fetch function
191Under the hood, `webdav-client` uses [`node-fetch`](https://github.com/bitinn/node-fetch) to perform requests. This can be overridden by running the following:
192
193```js
194// For example, use the `fetch` method in the browser:
195const createWebDAVClient = require("webdav");
196createWebDAVClient.setFetchMethod(window.fetch);
197```
198
199### Returned data structures
200
201#### Item stat
202Item stats are objects with properties that descibe a file or directory. They resemble the following:
203
204```json
205{
206 "filename": "/test",
207 "basename": "test",
208 "lastmod": "Tue, 05 Apr 2016 14:39:18 GMT",
209 "size": 0,
210 "type": "directory"
211}
212```
213
214or:
215
216```json
217{
218 "filename": "/image.jpg",
219 "basename": "image.jpg",
220 "lastmod": "Sun, 13 Mar 2016 04:23:32 GMT",
221 "size": 42497,
222 "type": "file",
223 "mime": "image/jpeg"
224}
225```
226
227Properties:
228
229| Property name | Type | Present | Description |
230|---------------|---------|--------------|---------------------------------------------|
231| filename | String | Always | File path of the remote item |
232| basename | String | Always | Base filename of the remote item, no path |
233| lastmod | String | Always | Last modification date of the item |
234| size | Number | Always | File size - 0 for directories |
235| type | String | Always | Item type - "file" or "directory" |
236| mime | String | Files only | Mime type - for file items only |
237
238## Compatibility
239This library has been tested to work with the following WebDAV servers or applications:
240
241 * [ownCloud](https://owncloud.org/)
242 * [Nextcloud](https://nextcloud.com/)
243 * [Yandex.ru](https://yandex.ru/)
244 * [jsDAV](https://github.com/mikedeboer/jsDAV)
245 * [webdav-server](https://github.com/OpenMarshal/npm-WebDAV-Server)
246
247### Webpack / Browserify
248WebDAV-client is browser friendly, after being transpiled. Refer to the use of WebDAV-fs in the [Buttercup mobile compatibility library](https://github.com/buttercup/buttercup-mobile-compat) or the [Buttercup browser extension](https://github.com/buttercup/buttercup-browser-extension) for guidance on preparation for the web.
249
250Please note that it is _not_ the responsibility of this library to be compatible with Webpack or Browserify. Small modifications may be made here to support them, but no guarantees of compatibility are made as there are an almost infinite number of configurations in both systems that could potentially cause issues with this library or a dependency therein.