UNPKG

19.6 kBMarkdownView Raw
1SFTP events
2-----------
3
4**Client/Server events**
5
6* **ready**() - Emitted after initial protocol version check has passed.
7
8**Server-only events**
9
10_Responses to these client requests are sent using one of the methods listed further in this document under `Server-only methods`. The valid response(s) for each request are documented below._
11
12* **OPEN**(< _integer_ >reqID, < _string_ >filename, < _integer_ >flags, < _ATTRS_ >attrs)
13
14 `flags` is a bitfield containing any of the flags defined in `OPEN_MODE`.
15 Use the static method `flagsToString()` to convert the value to a mode
16 string to be used by `fs.open()` (e.g. `'r'`).
17
18 Respond using one of the following:
19
20 * `handle()` - This indicates a successful opening of the file and passes
21 the given handle back to the client to use to refer to this open file for
22 future operations (e.g. reading, writing, closing).
23
24 * `status()` - Use this to indicate a failure to open the requested file.
25
26* **READ**(< _integer_ >reqID, < _Buffer_ >handle, < _integer_ >offset, < _integer_ >length)
27
28 Respond using one of the following:
29
30 * `data()` - Use this to send the requested chunk of data back to the client.
31 The amount of data sent is allowed to be less than the `length` requested,
32 for example if the file ends between `offset` and `offset + length`.
33
34 * `status()` - Use this to indicate either end of file (`STATUS_CODE.EOF`)
35 has been reached (`offset` is past the end of the file) or if an error
36 occurred while reading the requested part of the file.
37
38* **WRITE**(< _integer_ >reqID, < _Buffer_ >handle, < _integer_ >offset, < _Buffer_ >data)
39
40 Respond using:
41
42 * `status()` - Use this to indicate success/failure of the write to the file.
43
44* **FSTAT**(< _integer_ >reqID, < _Buffer_ >handle)
45
46 Respond using one of the following:
47
48 * `attrs()` - Use this to send the attributes for the requested
49 file/directory back to the client.
50
51 * `status()` - Use this to indicate an error occurred while accessing the
52 file/directory.
53
54* **FSETSTAT**(< _integer_ >reqID, < _Buffer_ >handle, < _ATTRS_ >attrs)
55
56 Respond using:
57
58 * `status()` - Use this to indicates success/failure of the setting of the
59 given file/directory attributes.
60
61* **CLOSE**(< _integer_ >reqID, < _Buffer_ >handle)
62
63 Respond using:
64
65 * `status()` - Use this to indicate success (`STATUS_CODE.OK`) or failure of
66 the closing of the file identified by `handle`.
67
68* **OPENDIR**(< _integer_ >reqID, < _string_ >path)
69
70 Respond using one of the following:
71
72 * `handle()` - This indicates a successful opening of the directory and
73 passes the given handle back to the client to use to refer to this open
74 directory for future operations (e.g. reading directory contents, closing).
75
76 * `status()` - Use this to indicate a failure to open the requested
77 directory.
78
79* **READDIR**(< _integer_ >reqID, < _Buffer_ >handle)
80
81 Respond using one of the following:
82
83 * `name()` - Use this to send one or more directory listings for the open
84 directory back to the client.
85
86 * `status()` - Use this to indicate either end of directory contents
87 (`STATUS_CODE.EOF`) or if an error occurred while reading the directory
88 contents.
89
90* **LSTAT**(< _integer_ >reqID, < _string_ >path)
91
92 Respond using one of the following:
93
94 * `attrs()` - Use this to send the attributes for the requested
95 file/directory back to the client.
96
97 * `status()` - Use this to indicate an error occurred while accessing the
98 file/directory.
99
100* **STAT**(< _integer_ >reqID, < _string_ >path)
101
102 Respond using one of the following:
103
104 * `attrs()` - Use this to send the attributes for the requested
105 file/directory back to the client.
106
107 * `status()` - Use this to indicate an error occurred while accessing the
108 file/directory.
109
110* **REMOVE**(< _integer_ >reqID, < _string_ >path)
111
112 Respond using:
113
114 * `status()` - Use this to indicate success/failure of the removal of the
115 file at `path`.
116
117* **RMDIR**(< _integer_ >reqID, < _string_ >path)
118
119 Respond using:
120
121 * `status()` - Use this to indicate success/failure of the removal of the
122 directory at `path`.
123
124* **REALPATH**(< _integer_ >reqID, < _string_ >path)
125
126 Respond using one of the following:
127
128 * `name()` - Use this to respond with a normalized version of `path`.
129 No file/directory attributes are required to be sent in this response.
130
131 * `status()` - Use this to indicate a failure in normalizing `path`.
132
133* **READLINK**(< _integer_ >reqID, < _string_ >path)
134
135 Respond using one of the following:
136
137 * `name()` - Use this to respond with the target of the symlink at `path`.
138 No file/directory attributes are required to be sent in this response.
139
140 * `status()` - Use this to indicate a failure in reading the symlink at
141 `path`.
142
143* **SETSTAT**(< _integer_ >reqID, < _string_ >path, < _ATTRS_ >attrs)
144
145 Respond using:
146
147 * `status()` - Use this to indicates success/failure of the setting of the
148 given file/directory attributes.
149
150* **MKDIR**(< _integer_ >reqID, < _string_ >path, < _ATTRS_ >attrs)
151
152 Respond using:
153
154 * `status()` - Use this to indicate success/failure of the creation of the
155 directory at `path`.
156
157* **RENAME**(< _integer_ >reqID, < _string_ >oldPath, < _string_ >newPath)
158
159 Respond using:
160
161 * `status()` - Use this to indicate success/failure of the renaming of the
162 file/directory at `oldPath` to `newPath`.
163
164* **SYMLINK**(< _integer_ >reqID, < _string_ >linkPath, < _string_ >targetPath)
165
166 Respond using:
167
168 * `status()` - Use this to indicate success/failure of the symlink creation.
169
170
171Useful standalone data structures
172---------------------------------
173
174* **STATUS_CODE** - _object_ - Contains the various status codes (for use especially with `status()`):
175
176 * `OK`
177
178 * `EOF`
179
180 * `NO_SUCH_FILE`
181
182 * `PERMISSION_DENIED`
183
184 * `FAILURE`
185
186 * `BAD_MESSAGE`
187
188 * `OP_UNSUPPORTED`
189
190* **OPEN_MODE** - _object_ - Contains the various open file flags:
191
192 * `READ`
193
194 * `WRITE`
195
196 * `APPEND`
197
198 * `CREAT`
199
200 * `TRUNC`
201
202 * `EXCL`
203
204
205Useful standalone methods
206-------------------------
207
208* **stringToFlags**(< _string_ >flagsStr) - _integer_ - Converts string flags (e.g. `'r'`, `'a+'`, etc.) to the appropriate `OPEN_MODE` flag mask. Returns `null` if conversion failed.
209
210* **flagsToString**(< _integer_ >flagsMask) - _string_ - Converts flag mask (e.g. number containing `OPEN_MODE` values) to the appropriate string value. Returns `null` if conversion failed.
211
212
213SFTP methods
214------------
215
216* **(constructor)**(< _object_ >config[, < _string_ >remoteIdentRaw]) - Creates and returns a new SFTP instance. `remoteIdentRaw` can be the raw SSH identification string of the remote party. This is used to change internal behavior based on particular SFTP implementations. `config` can contain:
217
218 * **server** - _boolean_ - Set to `true` to create an instance in server mode. **Default:** `false`
219
220 * **debug** - _function_ - Set this to a function that receives a single string argument to get detailed (local) debug information. **Default:** (none)
221
222
223
224**Client-only methods**
225
226* **fastGet**(< _string_ >remotePath, < _string_ >localPath[, < _object_ >options], < _function_ >callback) - _(void)_ - Downloads a file at `remotePath` to `localPath` using parallel reads for faster throughput. `options` can have the following properties:
227
228 * **concurrency** - _integer_ - Number of concurrent reads **Default:** `64`
229
230 * **chunkSize** - _integer_ - Size of each read in bytes **Default:** `32768`
231
232 * **step** - _function_(< _integer_ >total_transferred, < _integer_ >chunk, < _integer_ >total) - Called every time a part of a file was transferred
233
234 `callback` has 1 parameter: < _Error_ >err.
235
236* **fastPut**(< _string_ >localPath, < _string_ >remotePath[, < _object_ >options], < _function_ >callback) - _(void)_ - Uploads a file from `localPath` to `remotePath` using parallel reads for faster throughput. `options` can have the following properties:
237
238 * **concurrency** - _integer_ - Number of concurrent reads **Default:** `64`
239
240 * **chunkSize** - _integer_ - Size of each read in bytes **Default:** `32768`
241
242 * **step** - _function_(< _integer_ >total_transferred, < _integer_ >chunk, < _integer_ >total) - Called every time a part of a file was transferred
243
244 * **mode** - _mixed_ - Integer or string representing the file mode to set for the uploaded file.
245
246 `callback` has 1 parameter: < _Error_ >err.
247
248* **createReadStream**(< _string_ >path[, < _object_ >options]) - _ReadStream_ - Returns a new readable stream for `path`. `options` has the following defaults:
249
250 ```javascript
251 { flags: 'r',
252 encoding: null,
253 handle: null,
254 mode: 0o666,
255 autoClose: true
256 }
257 ```
258
259 `options` can include `start` and `end` values to read a range of bytes from the file instead of the entire file. Both `start` and `end` are inclusive and start at 0. The `encoding` can be `'utf8'`, `'ascii'`, or `'base64'`.
260
261 If `autoClose` is false, then the file handle won't be closed, even if there's an error. It is your responsiblity to close it and make sure there's no file handle leak. If `autoClose` is set to true (default behavior), on `error` or `end` the file handle will be closed automatically.
262
263 An example to read the last 10 bytes of a file which is 100 bytes long:
264
265 ```javascript
266 sftp.createReadStream('sample.txt', {start: 90, end: 99});
267 ```
268
269* **createWriteStream**(< _string_ >path[, < _object_ >options]) - _WriteStream_ - Returns a new writable stream for `path`. `options` has the following defaults:
270
271 ```javascript
272 {
273 flags: 'w',
274 encoding: null,
275 mode: 0o666,
276 autoClose: true
277 }
278 ```
279
280 `options` may also include a `start` option to allow writing data at some position past the beginning of the file. Modifying a file rather than replacing it may require a flags mode of 'r+' rather than the default mode 'w'.
281
282 If 'autoClose' is set to false and you pipe to this stream, this stream will not automatically close after there is no more data upstream -- allowing future pipes and/or manual writes.
283
284* **open**(< _string_ >filename, < _string_ >flags, [< _mixed_ >attrs_mode, ]< _function_ >callback) - _(void)_ - Opens a file `filename` with `flags` with optional _ATTRS_ object or file mode `attrs_mode`. `flags` is any of the flags supported by `fs.open` (except sync flag). `callback` has 2 parameters: < _Error_ >err, < _Buffer_ >handle.
285
286* **close**(< _Buffer_ >handle, < _function_ >callback) - _(void)_ - Closes the resource associated with `handle` given by open() or opendir(). `callback` has 1 parameter: < _Error_ >err.
287
288* **read**(< _Buffer_ >handle, < _Buffer_ >buffer, < _integer_ >offset, < _integer_ >length, < _integer_ >position, < _function_ >callback) - _(void)_ - Reads `length` bytes from the resource associated with `handle` starting at `position` and stores the bytes in `buffer` starting at `offset`. `callback` has 4 parameters: < _Error_ >err, < _integer_ >bytesRead, < _Buffer_ >buffer (offset adjusted), < _integer_ >position.
289
290* **write**(< _Buffer_ >handle, < _Buffer_ >buffer, < _integer_ >offset, < _integer_ >length, < _integer_ >position, < _function_ >callback) - _(void)_ - Writes `length` bytes from `buffer` starting at `offset` to the resource associated with `handle` starting at `position`. `callback` has 1 parameter: < _Error_ >err.
291
292* **fstat**(< _Buffer_ >handle, < _function_ >callback) - _(void)_ - Retrieves attributes for the resource associated with `handle`. `callback` has 2 parameters: < _Error_ >err, < _Stats_ >stats.
293
294* **fsetstat**(< _Buffer_ >handle, < _ATTRS_ >attributes, < _function_ >callback) - _(void)_ - Sets the attributes defined in `attributes` for the resource associated with `handle`. `callback` has 1 parameter: < _Error_ >err.
295
296* **futimes**(< _Buffer_ >handle, < _mixed_ >atime, < _mixed_ >mtime, < _function_ >callback) - _(void)_ - Sets the access time and modified time for the resource associated with `handle`. `atime` and `mtime` can be Date instances or UNIX timestamps. `callback` has 1 parameter: < _Error_ >err.
297
298* **fchown**(< _Buffer_ >handle, < _integer_ >uid, < _integer_ >gid, < _function_ >callback) - _(void)_ - Sets the owner for the resource associated with `handle`. `callback` has 1 parameter: < _Error_ >err.
299
300* **fchmod**(< _Buffer_ >handle, < _mixed_ >mode, < _function_ >callback) - _(void)_ - Sets the mode for the resource associated with `handle`. `mode` can be an integer or a string containing an octal number. `callback` has 1 parameter: < _Error_ >err.
301
302* **opendir**(< _string_ >path, < _function_ >callback) - _(void)_ - Opens a directory `path`. `callback` has 2 parameters: < _Error_ >err, < _Buffer_ >handle.
303
304* **readdir**(< _mixed_ >location, < _function_ >callback) - _(void)_ - Retrieves a directory listing. `location` can either be a _Buffer_ containing a valid directory handle from opendir() or a _string_ containing the path to a directory. `callback` has 2 parameters: < _Error_ >err, < _mixed_ >list. `list` is an _Array_ of `{ filename: 'foo', longname: '....', attrs: {...} }` style objects (attrs is of type _ATTR_). If `location` is a directory handle, this function may need to be called multiple times until `list` is boolean false, which indicates that no more directory entries are available for that directory handle.
305
306* **unlink**(< _string_ >path, < _function_ >callback) - _(void)_ - Removes the file/symlink at `path`. `callback` has 1 parameter: < _Error_ >err.
307
308* **rename**(< _string_ >srcPath, < _string_ >destPath, < _function_ >callback) - _(void)_ - Renames/moves `srcPath` to `destPath`. `callback` has 1 parameter: < _Error_ >err.
309
310* **mkdir**(< _string_ >path, [< _ATTRS_ >attributes, ]< _function_ >callback) - _(void)_ - Creates a new directory `path`. `callback` has 1 parameter: < _Error_ >err.
311
312* **rmdir**(< _string_ >path, < _function_ >callback) - _(void)_ - Removes the directory at `path`. `callback` has 1 parameter: < _Error_ >err.
313
314* **stat**(< _string_ >path, < _function_ >callback) - _(void)_ - Retrieves attributes for `path`. `callback` has 2 parameter: < _Error_ >err, < _Stats_ >stats.
315
316* **lstat**(< _string_ >path, < _function_ >callback) - _(void)_ - Retrieves attributes for `path`. If `path` is a symlink, the link itself is stat'ed instead of the resource it refers to. `callback` has 2 parameters: < _Error_ >err, < _Stats_ >stats.
317
318* **setstat**(< _string_ >path, < _ATTRS_ >attributes, < _function_ >callback) - _(void)_ - Sets the attributes defined in `attributes` for `path`. `callback` has 1 parameter: < _Error_ >err.
319
320* **utimes**(< _string_ >path, < _mixed_ >atime, < _mixed_ >mtime, < _function_ >callback) - _(void)_ - Sets the access time and modified time for `path`. `atime` and `mtime` can be Date instances or UNIX timestamps. `callback` has 1 parameter: < _Error_ >err.
321
322* **chown**(< _string_ >path, < _integer_ >uid, < _integer_ >gid, < _function_ >callback) - _(void)_ - Sets the owner for `path`. `callback` has 1 parameter: < _Error_ >err.
323
324* **chmod**(< _string_ >path, < _mixed_ >mode, < _function_ >callback) - _(void)_ - Sets the mode for `path`. `mode` can be an integer or a string containing an octal number. `callback` has 1 parameter: < _Error_ >err.
325
326* **readlink**(< _string_ >path, < _function_ >callback) - _(void)_ - Retrieves the target for a symlink at `path`. `callback` has 2 parameters: < _Error_ >err, < _string_ >target.
327
328* **symlink**(< _string_ >targetPath, < _string_ >linkPath, < _function_ >callback) - _(void)_ - Creates a symlink at `linkPath` to `targetPath`. `callback` has 1 parameter: < _Error_ >err.
329
330* **realpath**(< _string_ >path, < _function_ >callback) - _(void)_ - Resolves `path` to an absolute path. `callback` has 2 parameters: < _Error_ >err, < _string_ >absPath.
331
332* **ext_openssh_rename**(< _string_ >srcPath, < _string_ >destPath, < _function_ >callback) - _(void)_ - **OpenSSH extension** Performs POSIX rename(3) from `srcPath` to `destPath`. `callback` has 1 parameter: < _Error_ >err.
333
334* **ext_openssh_statvfs**(< _string_ >path, < _function_ >callback) - _(void)_ - **OpenSSH extension** Performs POSIX statvfs(2) on `path`. `callback` has 2 parameters: < _Error_ >err, < _object_ >fsInfo. `fsInfo` contains the information as found in the [statvfs struct](http://linux.die.net/man/2/statvfs).
335
336* **ext_openssh_fstatvfs**(< _Buffer_ >handle, < _function_ >callback) - _(void)_ - **OpenSSH extension** Performs POSIX fstatvfs(2) on open handle `handle`. `callback` has 2 parameters: < _Error_ >err, < _object_ >fsInfo. `fsInfo` contains the information as found in the [statvfs struct](http://linux.die.net/man/2/statvfs).
337
338* **ext_openssh_hardlink**(< _string_ >targetPath, < _string_ >linkPath, < _function_ >callback) - _(void)_ - **OpenSSH extension** Performs POSIX link(2) to create a hard link to `targetPath` at `linkPath`. `callback` has 1 parameter: < _Error_ >err.
339
340* **ext_openssh_fsync**(< _Buffer_ >handle, < _function_ >callback) - _(void)_ - **OpenSSH extension** Performs POSIX fsync(3) on the open handle `handle`. `callback` has 1 parameter: < _Error_ >err.
341
342
343**Server-only methods**
344
345* **status**(< _integer_ >reqID, < _integer_ >statusCode[, < _string_ >message]) - _(void)_ - Sends a status response for the request identified by `id`.
346
347* **handle**(< _integer_ >reqID, < _Buffer_ >handle) - _(void)_ - Sends a handle response for the request identified by `id`. `handle` must be less than 256 bytes and is an opaque value that could merely contain the value of a backing file descriptor or some other unique, custom value.
348
349* **data**(< _integer_ >reqID, < _mixed_ >data[, < _string_ >encoding]) - _(void)_ - Sends a data response for the request identified by `id`. `data` can be a _Buffer_ or _string_. If `data` is a string, `encoding` is the encoding of `data`.
350
351* **name**(< _integer_ >reqID, < _array_ >names) - _(void)_ - Sends a name response for the request identified by `id`. `names` must be an _array_ of _object_ where each _object_ can contain:
352
353 * **filename** - _string_ - The entry's name.
354
355 * **longname** - _string_ - This is the `ls -l`-style format for the entry (e.g. `-rwxr--r-- 1 bar bar 718 Dec 8 2009 foo`)
356
357 * **attrs** - _ATTRS_ - This is an optional _ATTRS_ object that contains requested/available attributes for the entry.
358
359* **attrs**(< _integer_ >reqID, < _ATTRS_ >attrs) - _(void)_ - Sends an attrs response for the request identified by `id`. `attrs` contains the requested/available attributes.
360
361
362ATTRS
363-----
364
365An object with the following valid properties:
366
367* **mode** - _integer_ - Mode/permissions for the resource.
368
369* **uid** - _integer_ - User ID of the resource.
370
371* **gid** - _integer_ - Group ID of the resource.
372
373* **size** - _integer_ - Resource size in bytes.
374
375* **atime** - _integer_ - UNIX timestamp of the access time of the resource.
376
377* **mtime** - _integer_ - UNIX timestamp of the modified time of the resource.
378
379When supplying an ATTRS object to one of the SFTP methods:
380
381* `atime` and `mtime` can be either a Date instance or a UNIX timestamp.
382
383* `mode` can either be an integer or a string containing an octal number.
384
385
386Stats
387-----
388
389An object with the same attributes as an ATTRS object with the addition of the following methods:
390
391* `stats.isDirectory()`
392
393* `stats.isFile()`
394
395* `stats.isBlockDevice()`
396
397* `stats.isCharacterDevice()`
398
399* `stats.isSymbolicLink()`
400
401* `stats.isFIFO()`
402
403* `stats.isSocket()`