ssh2-sftp-client 8.0.0 → 9.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +348 -125
- package/README.org +138 -52
- package/package.json +9 -5
- package/src/constants.js +1 -0
- package/src/index.js +888 -657
- package/src/utils.js +43 -106
package/README.md
CHANGED
|
@@ -1,85 +1,106 @@
|
|
|
1
|
-
- [Overview](#
|
|
2
|
-
- [Version
|
|
3
|
-
- [Installation](#
|
|
4
|
-
- [Basic Usage](#
|
|
5
|
-
- [Documentation](#
|
|
6
|
-
- [Specifying Paths](#
|
|
7
|
-
- [Methods](#
|
|
8
|
-
- [new SftpClient(name) ===> SFTP client object](#
|
|
9
|
-
- [connect(config) ===> SFTP object](#
|
|
10
|
-
- [list(path,
|
|
11
|
-
- [exists(path) ==> boolean](#
|
|
12
|
-
- [stat(path) ==> object](#
|
|
13
|
-
- [get(path, dst, options) ==> String|Stream|Buffer](#
|
|
14
|
-
- [fastGet(remotePath, localPath, options) ===> string](#
|
|
15
|
-
- [put(src, remotePath, options) ==> string](#
|
|
16
|
-
- [fastPut(localPath, remotePath, options) ==> string](#
|
|
17
|
-
- [append(input, remotePath, options) ==> string](#
|
|
18
|
-
- [mkdir(path, recursive) ==> string](#
|
|
19
|
-
- [rmdir(path, recursive) ==> string](#
|
|
20
|
-
- [delete(path, noErrorOK) ==> string](#
|
|
21
|
-
- [rename(fromPath, toPath) ==> string](#
|
|
22
|
-
- [posixRename(fromPath, toPath) ==> string](#
|
|
23
|
-
- [chmod(path, mode) ==> string](#
|
|
24
|
-
- [realPath(path) ===> string](#
|
|
25
|
-
- [cwd() ==> string](#
|
|
26
|
-
- [uploadDir(srcDir, dstDir,
|
|
27
|
-
- [downloadDir(srcDir, dstDir,
|
|
28
|
-
- [
|
|
29
|
-
- [
|
|
30
|
-
- [
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
- [
|
|
35
|
-
- [
|
|
36
|
-
- [
|
|
37
|
-
- [
|
|
38
|
-
- [
|
|
39
|
-
|
|
40
|
-
- [
|
|
41
|
-
- [
|
|
42
|
-
- [How can I
|
|
43
|
-
- [
|
|
44
|
-
- [
|
|
45
|
-
- [
|
|
46
|
-
- [
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
- [
|
|
51
|
-
- [
|
|
52
|
-
|
|
53
|
-
- [
|
|
54
|
-
- [
|
|
55
|
-
- [
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
#
|
|
1
|
+
- [Overview](#org19c31e8)
|
|
2
|
+
- [Version 9.x Changes](#orgc330d84)
|
|
3
|
+
- [Installation](#org7c63f6a)
|
|
4
|
+
- [Basic Usage](#orgb99a07c)
|
|
5
|
+
- [Documentation](#org5550f37)
|
|
6
|
+
- [Specifying Paths](#org2f0a3a6)
|
|
7
|
+
- [Methods](#org8d230d5)
|
|
8
|
+
- [new SftpClient(name) ===> SFTP client object](#orgc8b55d7)
|
|
9
|
+
- [connect(config) ===> SFTP object](#org42512fb)
|
|
10
|
+
- [list(path, filter) ==> Array[object]](#orgbe9e165)
|
|
11
|
+
- [exists(path) ==> boolean](#org14dee5e)
|
|
12
|
+
- [stat(path) ==> object](#orga0033c0)
|
|
13
|
+
- [get(path, dst, options) ==> String|Stream|Buffer](#org02a3dc5)
|
|
14
|
+
- [fastGet(remotePath, localPath, options) ===> string](#orge9f875d)
|
|
15
|
+
- [put(src, remotePath, options) ==> string](#orgc7941ea)
|
|
16
|
+
- [fastPut(localPath, remotePath, options) ==> string](#org8f8794d)
|
|
17
|
+
- [append(input, remotePath, options) ==> string](#org82c75b5)
|
|
18
|
+
- [mkdir(path, recursive) ==> string](#org42c5eeb)
|
|
19
|
+
- [rmdir(path, recursive) ==> string](#orgd0aa59d)
|
|
20
|
+
- [delete(path, noErrorOK) ==> string](#orgb496048)
|
|
21
|
+
- [rename(fromPath, toPath) ==> string](#org1a2be8f)
|
|
22
|
+
- [posixRename(fromPath, toPath) ==> string](#org571c0b9)
|
|
23
|
+
- [chmod(path, mode) ==> string](#org02d64a4)
|
|
24
|
+
- [realPath(path) ===> string](#org008d567)
|
|
25
|
+
- [cwd() ==> string](#org7033d28)
|
|
26
|
+
- [uploadDir(srcDir, dstDir, options) ==> string](#orgfa0102a)
|
|
27
|
+
- [downloadDir(srcDir, dstDir, options) ==> string](#orgefe6fc6)
|
|
28
|
+
- [createReadStream(remotePath, options)) ==> stream object](#org783e363)
|
|
29
|
+
- [createWriteStream(remotePath, options) ==> stream object](#orgf45313d)
|
|
30
|
+
- [rcopy(srcPath, dstPath) ==> string](#orga31c8c1)
|
|
31
|
+
- [end() ==> boolean](#org4d02fde)
|
|
32
|
+
- [Add and Remove Listeners](#orge5769d3)
|
|
33
|
+
- [Platform Quirks & Warnings](#org3e6ddf9)
|
|
34
|
+
- [Server Capabilities](#org61f0fe5)
|
|
35
|
+
- [Promises, Events & Managing Exceptions](#orga7d6067)
|
|
36
|
+
- [Adding Custom Handlers](#orgc08d427)
|
|
37
|
+
- [Windows Based Servers](#org5439468)
|
|
38
|
+
- [Don't Re-use SftpClient Objects](#orgae9129c)
|
|
39
|
+
- [FAQ](#org3761660)
|
|
40
|
+
- [Remote server drops connections with only an end event](#org1e669d1)
|
|
41
|
+
- [How can I pass writable stream as dst for get method?](#org71d5bab)
|
|
42
|
+
- [How can I upload files without having to specify a password?](#orgaafc964)
|
|
43
|
+
- [How can I connect through a Socks Proxy](#orgebf85e2)
|
|
44
|
+
- [Timeout while waiting for handshake or handshake errors](#org528d9ea)
|
|
45
|
+
- [How can I limit upload/download speed](#org3b6ff20)
|
|
46
|
+
- [Connection hangs or fails for larger files](#orgb5e285f)
|
|
47
|
+
- [Examples](#org18ce0c2)
|
|
48
|
+
- [Troubleshooting](#orgf4b6c80)
|
|
49
|
+
- [Common Errors](#orga419582)
|
|
50
|
+
- [Not returning the promise in a `then()` block](#orgd5f3557)
|
|
51
|
+
- [Mixing Promise Chains and Async/Await](#org557d1c5)
|
|
52
|
+
- [Try/catch and Error Handlers](#org2bf9a3a)
|
|
53
|
+
- [Server Differences](#org89795d7)
|
|
54
|
+
- [Avoid Concurrent Operations](#orgbec08b0)
|
|
55
|
+
- [Debugging Support](#org124cfc9)
|
|
56
|
+
- [Logging Issues](#org5f3c941)
|
|
57
|
+
- [Pull Requests](#org6e82efc)
|
|
58
|
+
- [Contributors](#org680b5c2)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
<a id="org19c31e8"></a>
|
|
63
|
+
|
|
64
|
+
# Overview
|
|
59
65
|
|
|
60
66
|
an SFTP client for node.js, a wrapper around [SSH2](https://github.com/mscdex/ssh2) which provides a high level convenience abstraction as well as a Promise based API.
|
|
61
67
|
|
|
62
68
|
Documentation on the methods and available options in the underlying modules can be found on the [SSH2](https://github.com/mscdex/ssh2) project pages.
|
|
63
69
|
|
|
64
|
-
Current stable release is **
|
|
70
|
+
Current stable release is **v9.0.1**.
|
|
65
71
|
|
|
66
|
-
Code has been tested against Node versions 14.19.1, 16.
|
|
72
|
+
Code has been tested against Node versions 14.19.1, 16.15.0 and 18.1.0
|
|
67
73
|
|
|
68
|
-
Node versions <
|
|
74
|
+
Node versions < 14.x are not supported.
|
|
69
75
|
|
|
70
|
-
## Version 8.x.x Changes<a id="sec-1-1"></a>
|
|
71
76
|
|
|
72
|
-
|
|
77
|
+
<a id="orgc330d84"></a>
|
|
73
78
|
|
|
74
|
-
|
|
79
|
+
## Version 9.x Changes
|
|
75
80
|
|
|
76
|
-
|
|
81
|
+
- **Breaking Change**: This version uses syntax not supported in node versions prior to v14. Therefore, node versions less than v14 will not work.
|
|
82
|
+
- **Breaking Change**: This `list()` method no longer accepts a regular expression for filtering the entries to be returned. You can now specify a filter function instead. The function is called for each item in the list of items to be returned, passing in the item object as its only argument. Essentially, this is just a call to `Array.filter()`, so the filter function should behave in the same way i.e. return true for items to be retained and false for those to be dropped.
|
|
83
|
+
- **Breaking Change**: The ability to set `autoClose` on read and write streams and the ability to set `end` on `pipe` operations has been removed. These options caused confusion for users and were too easy to get wrong, plus it made the methods overly complicated. For those use-cases where you want to control streams at a low level, two new methods have been added, `createReadStream()` and `createWriteStream()`. However, it should be noted that client code is 100% responsible for managing streams obtained using these methods. Use at your own risk!
|
|
84
|
+
- **Breaking Change**: The 3rd argument to `uploadDir()` and `downloadDir()` methods has been change. Previously, the argument was a filter function used to select which directories and files to be transferred. The 3rd argument is now an options object with two supported properties, `filter` and `useFastput` (for `uploadDir()`) or `useFastget` (for `downloadDir()`). If `useFastput` is true, the `fastPut()` method will be pused to upload files. If `false` or missing, the slower, but better supported, `put()` method will be used. Likewise, the `useFastget` options can be set to `true` to use the `fastGet()` method for donwloading files, otherwise the slower, but more reliable, `get()` method will be used.
|
|
85
|
+
- The `uploadDir()` and `downloadDir()` methods now use asynchrounous processes to upload/download files. This should result in improved performance for these two methods.
|
|
86
|
+
- New Methods: Two new methods, `createWriteStream()` and `createReadStream()` have been added. These methods will return a stream object connected to a remote file on the `sftp` server. Client code is responsible for managing these stream objects. This includes adding any necessary event listeners and disposing of the objects once finished with them.
|
|
87
|
+
- Refactoring of Listeners: The library manages temporary listeners in order to provide a way to catch events and processes them inside a `Promise` context. Previously, every method added its own set of temporary listeners. However, this could result in multiple sets of listeners being added, especially for methods which call other methods as part of their processing e.g. `rmdir(),` `uploadDir()` and `dowqnloadDir()`. To avoid this, *internal only* versions of each method have been created. These internal methods use an *underscore* `_` prefix. Client code should not use these methods directly.
|
|
88
|
+
- New method: Added `rcopy()` method to perform a remote copy of a file on the remote SFTP server.
|
|
89
|
+
- Bumped ssh2 version to 1.11.0
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
<a id="org7c63f6a"></a>
|
|
93
|
+
|
|
94
|
+
# Installation
|
|
77
95
|
|
|
78
96
|
```shell
|
|
79
97
|
npm install ssh2-sftp-client
|
|
80
98
|
```
|
|
81
99
|
|
|
82
|
-
|
|
100
|
+
|
|
101
|
+
<a id="orgb99a07c"></a>
|
|
102
|
+
|
|
103
|
+
# Basic Usage
|
|
83
104
|
|
|
84
105
|
```javascript
|
|
85
106
|
let Client = require('ssh2-sftp-client');
|
|
@@ -99,13 +120,19 @@ sftp.connect({
|
|
|
99
120
|
});
|
|
100
121
|
```
|
|
101
122
|
|
|
102
|
-
|
|
123
|
+
|
|
124
|
+
<a id="org5550f37"></a>
|
|
125
|
+
|
|
126
|
+
# Documentation
|
|
103
127
|
|
|
104
128
|
The connection options are the same as those offered by the underlying SSH2 module. For full details, please see [SSH2 client methods](https://github.com/mscdex/ssh2#user-content-client-methods)
|
|
105
129
|
|
|
106
130
|
All the methods will return a Promise, except for `on()` and `removeListener()`, which are typically only used in special use cases.
|
|
107
131
|
|
|
108
|
-
|
|
132
|
+
|
|
133
|
+
<a id="org2f0a3a6"></a>
|
|
134
|
+
|
|
135
|
+
## Specifying Paths
|
|
109
136
|
|
|
110
137
|
The convention with both FTP and SFTP is that paths are specified using a 'nix' style i.e. use `/` as the path separator. This means that even if your SFTP server is running on a win32 platform, you should use `/` instead of `\` as the path separator. For example, for a win32 path of `C:\Users\fred` you would actually use `/C:/Users/fred`. If your win32 server does not support the 'nix' path convention, you can try setting the `remotePathSep` property of the `SftpClient` object to the path separator of your remote server. This **might** work, but has not been tested. Please let me know if you need to do this and provide details of the SFTP server so that I can try to create an appropriate environment and adjust things as necessary. At this point, I'm not aware of any win32 based SFTP servers which do not support the 'nix' path convention.
|
|
111
138
|
|
|
@@ -135,9 +162,15 @@ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
|
|
|
135
162
|
|
|
136
163
|
This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
|
|
137
164
|
|
|
138
|
-
## Methods<a id="sec-4-2"></a>
|
|
139
165
|
|
|
140
|
-
|
|
166
|
+
<a id="org8d230d5"></a>
|
|
167
|
+
|
|
168
|
+
## Methods
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
<a id="orgc8b55d7"></a>
|
|
172
|
+
|
|
173
|
+
### new SftpClient(name) ===> SFTP client object
|
|
141
174
|
|
|
142
175
|
Constructor to create a new `ssh2-sftp-client` object. An optional `name` string can be provided, which will be used in error messages to help identify which client has thrown the error.
|
|
143
176
|
|
|
@@ -173,7 +206,10 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
|
|
|
173
206
|
});
|
|
174
207
|
```
|
|
175
208
|
|
|
176
|
-
|
|
209
|
+
|
|
210
|
+
<a id="org42512fb"></a>
|
|
211
|
+
|
|
212
|
+
### connect(config) ===> SFTP object
|
|
177
213
|
|
|
178
214
|
Connect to an sftp server. Full documentation for connection options is available [here](https://github.com/mscdex/ssh2#user-content-client-methods)
|
|
179
215
|
|
|
@@ -236,12 +272,15 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
236
272
|
});
|
|
237
273
|
```
|
|
238
274
|
|
|
239
|
-
|
|
275
|
+
|
|
276
|
+
<a id="orgbe9e165"></a>
|
|
277
|
+
|
|
278
|
+
### list(path, filter) ==> Array[object]
|
|
240
279
|
|
|
241
280
|
Retrieves a directory listing. This method returns a Promise, which once realised, returns an array of objects representing items in the remote directory.
|
|
242
281
|
|
|
243
282
|
- **path:** {String} Remote directory path
|
|
244
|
-
- **
|
|
283
|
+
- **filter:** (optional) {function} A function used to filter the items included in the returned array. The function is called for each item with the item object being passed in as the argument. The function is passed to Array.filter() to perform the filtering.
|
|
245
284
|
|
|
246
285
|
1. Example Use
|
|
247
286
|
|
|
@@ -290,6 +329,7 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
290
329
|
},
|
|
291
330
|
owner: // user ID
|
|
292
331
|
group: // group ID
|
|
332
|
+
longname: // like ls -l line
|
|
293
333
|
}
|
|
294
334
|
```
|
|
295
335
|
|
|
@@ -303,7 +343,10 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
303
343
|
|
|
304
344
|
The *glob*-style matching is very simple. In most cases, you are best off using a real regular expression which will allow you to do more powerful matching and anchor matches to the beginning/end of the string etc.
|
|
305
345
|
|
|
306
|
-
|
|
346
|
+
|
|
347
|
+
<a id="org14dee5e"></a>
|
|
348
|
+
|
|
349
|
+
### exists(path) ==> boolean
|
|
307
350
|
|
|
308
351
|
Tests to see if remote file or directory exists. Returns type of remote object if it exists or false if it does not.
|
|
309
352
|
|
|
@@ -336,7 +379,10 @@ Tests to see if remote file or directory exists. Returns type of remote object i
|
|
|
336
379
|
});
|
|
337
380
|
```
|
|
338
381
|
|
|
339
|
-
|
|
382
|
+
|
|
383
|
+
<a id="orga0033c0"></a>
|
|
384
|
+
|
|
385
|
+
### stat(path) ==> object
|
|
340
386
|
|
|
341
387
|
Returns the attributes associated with the object pointed to by `path`.
|
|
342
388
|
|
|
@@ -384,7 +430,10 @@ Returns the attributes associated with the object pointed to by `path`.
|
|
|
384
430
|
});
|
|
385
431
|
```
|
|
386
432
|
|
|
387
|
-
|
|
433
|
+
|
|
434
|
+
<a id="org02a3dc5"></a>
|
|
435
|
+
|
|
436
|
+
### get(path, dst, options) ==> String|Stream|Buffer
|
|
388
437
|
|
|
389
438
|
Retrieve a file from a remote SFTP server. The `dst` argument defines the destination and can be either a string, a stream object or undefined. If it is a string, it is interpreted as the path to a location on the local file system (path should include the file name). If it is a stream object, the remote data is passed to it via a call to pipe(). If `dst` is undefined, the method will put the data into a buffer and return that buffer when the Promise is resolved. If `dst` is defined, it is returned when the Promise is resolved.
|
|
390
439
|
|
|
@@ -437,7 +486,10 @@ In general, if you're going to pass in a string as the destination, you are bett
|
|
|
437
486
|
|
|
438
487
|
- **Tip:** See examples file in the Git repository for more examples. You can pass any writeable stream in as the destination. For example, if you pass in `zlib.createGunzip()` writeable stream, you can both download and decompress a gzip file 'on the fly'.
|
|
439
488
|
|
|
440
|
-
|
|
489
|
+
|
|
490
|
+
<a id="orge9f875d"></a>
|
|
491
|
+
|
|
492
|
+
### fastGet(remotePath, localPath, options) ===> string
|
|
441
493
|
|
|
442
494
|
Downloads a file at remotePath to localPath using parallel reads for faster throughput. This is the simplest method if you just want to download a file.
|
|
443
495
|
|
|
@@ -477,7 +529,10 @@ Downloads a file at remotePath to localPath using parallel reads for faster thro
|
|
|
477
529
|
});
|
|
478
530
|
```
|
|
479
531
|
|
|
480
|
-
|
|
532
|
+
|
|
533
|
+
<a id="orgc7941ea"></a>
|
|
534
|
+
|
|
535
|
+
### put(src, remotePath, options) ==> string
|
|
481
536
|
|
|
482
537
|
Upload data from local system to remote server. If the `src` argument is a string, it is interpreted as a local file path to be used for the data to transfer. If the `src` argument is a buffer, the contents of the buffer are copied to the remote file and if it is a readable stream, the contents of that stream are piped to the `remotePath` on the server.
|
|
483
538
|
|
|
@@ -524,7 +579,10 @@ Upload data from local system to remote server. If the `src` argument is a strin
|
|
|
524
579
|
|
|
525
580
|
- **Tip:** If the src argument is a path string, consider just using `fastPut()`.
|
|
526
581
|
|
|
527
|
-
|
|
582
|
+
|
|
583
|
+
<a id="org8f8794d"></a>
|
|
584
|
+
|
|
585
|
+
### fastPut(localPath, remotePath, options) ==> string
|
|
528
586
|
|
|
529
587
|
Uploads the data in file at `localPath` to a new file on remote server at `remotePath` using concurrency. The options object allows tweaking of the fast put process.
|
|
530
588
|
|
|
@@ -565,7 +623,10 @@ Uploads the data in file at `localPath` to a new file on remote server at `remot
|
|
|
565
623
|
});
|
|
566
624
|
```
|
|
567
625
|
|
|
568
|
-
|
|
626
|
+
|
|
627
|
+
<a id="org82c75b5"></a>
|
|
628
|
+
|
|
629
|
+
### append(input, remotePath, options) ==> string
|
|
569
630
|
|
|
570
631
|
Append the `input` data to an existing remote file. There is no integrity checking performed apart from normal writeStream checks. This function simply opens a writeStream on the remote file in append mode and writes the data passed in to the file.
|
|
571
632
|
|
|
@@ -606,7 +667,10 @@ Append the `input` data to an existing remote file. There is no integrity checki
|
|
|
606
667
|
});
|
|
607
668
|
```
|
|
608
669
|
|
|
609
|
-
|
|
670
|
+
|
|
671
|
+
<a id="org42c5eeb"></a>
|
|
672
|
+
|
|
673
|
+
### mkdir(path, recursive) ==> string
|
|
610
674
|
|
|
611
675
|
Create a new directory. If the recursive flag is set to true, the method will create any directories in the path which do not already exist. Recursive flag defaults to false.
|
|
612
676
|
|
|
@@ -631,7 +695,10 @@ Create a new directory. If the recursive flag is set to true, the method will cr
|
|
|
631
695
|
});
|
|
632
696
|
```
|
|
633
697
|
|
|
634
|
-
|
|
698
|
+
|
|
699
|
+
<a id="orgd0aa59d"></a>
|
|
700
|
+
|
|
701
|
+
### rmdir(path, recursive) ==> string
|
|
635
702
|
|
|
636
703
|
Remove a directory. If removing a directory and recursive flag is set to `true`, the specified directory and all sub-directories and files will be deleted. If set to false and the directory has sub-directories or files, the action will fail.
|
|
637
704
|
|
|
@@ -658,7 +725,10 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
|
|
|
658
725
|
});
|
|
659
726
|
```
|
|
660
727
|
|
|
661
|
-
|
|
728
|
+
|
|
729
|
+
<a id="orgb496048"></a>
|
|
730
|
+
|
|
731
|
+
### delete(path, noErrorOK) ==> string
|
|
662
732
|
|
|
663
733
|
Delete a file on the remote server.
|
|
664
734
|
|
|
@@ -684,7 +754,10 @@ Delete a file on the remote server.
|
|
|
684
754
|
});
|
|
685
755
|
```
|
|
686
756
|
|
|
687
|
-
|
|
757
|
+
|
|
758
|
+
<a id="org1a2be8f"></a>
|
|
759
|
+
|
|
760
|
+
### rename(fromPath, toPath) ==> string
|
|
688
761
|
|
|
689
762
|
Rename a file or directory from `fromPath` to `toPath`. You must have the necessary permissions to modify the remote file.
|
|
690
763
|
|
|
@@ -710,7 +783,10 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
|
|
|
710
783
|
});
|
|
711
784
|
```
|
|
712
785
|
|
|
713
|
-
|
|
786
|
+
|
|
787
|
+
<a id="org571c0b9"></a>
|
|
788
|
+
|
|
789
|
+
### posixRename(fromPath, toPath) ==> string
|
|
714
790
|
|
|
715
791
|
This method uses the openssh POSIX rename extension introduced in OpenSSH 4.8. The advantage of this version of rename over standard SFTP rename is that it is an atomic operation and will allow renaming a resource where the destination name exists. The POSIX rename will also work on some filesystems which do not support standard SFTP rename because they don't support the system hardlink() call. The POSIX rename extension is available on all openSSH servers from 4.8 and some other implementations. This is an extension to the standard SFTP protocol and therefore is not supported on all sSFTP servers.
|
|
716
792
|
|
|
@@ -734,7 +810,10 @@ client.connect(config)
|
|
|
734
810
|
});
|
|
735
811
|
```
|
|
736
812
|
|
|
737
|
-
|
|
813
|
+
|
|
814
|
+
<a id="org02d64a4"></a>
|
|
815
|
+
|
|
816
|
+
### chmod(path, mode) ==> string
|
|
738
817
|
|
|
739
818
|
Change the mode (read, write or execute permissions) of a remote file or directory.
|
|
740
819
|
|
|
@@ -760,7 +839,10 @@ Change the mode (read, write or execute permissions) of a remote file or directo
|
|
|
760
839
|
});
|
|
761
840
|
```
|
|
762
841
|
|
|
763
|
-
|
|
842
|
+
|
|
843
|
+
<a id="org008d567"></a>
|
|
844
|
+
|
|
845
|
+
### realPath(path) ===> string
|
|
764
846
|
|
|
765
847
|
Converts a relative path to an absolute path on the remote server. This method is mainly used internally to resolve remote path names.
|
|
766
848
|
|
|
@@ -768,21 +850,31 @@ Converts a relative path to an absolute path on the remote server. This method i
|
|
|
768
850
|
|
|
769
851
|
- **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
|
|
770
852
|
|
|
771
|
-
|
|
853
|
+
|
|
854
|
+
<a id="org7033d28"></a>
|
|
855
|
+
|
|
856
|
+
### cwd() ==> string
|
|
772
857
|
|
|
773
858
|
Returns what the server believes is the current remote working directory.
|
|
774
859
|
|
|
775
|
-
|
|
860
|
+
|
|
861
|
+
<a id="orgfa0102a"></a>
|
|
862
|
+
|
|
863
|
+
### uploadDir(srcDir, dstDir, options) ==> string
|
|
776
864
|
|
|
777
865
|
Upload the directory specified by `srcDir` to the remote directory specified by `dstDir`. The `dstDir` will be created if necessary. Any sub directories within `srcDir` will also be uploaded. Any existing files in the remote path will be overwritten.
|
|
778
866
|
|
|
779
867
|
The upload process also emits 'upload' events. These events are fired for each successfully uploaded file. The `upload` event calls listeners with 1 argument, an object which has properties source and destination. The source property is the path of the file uploaded and the destination property is the path to where the file was uploaded. The purpose of this event is to provide some way for client code to get feedback on the upload progress. You can add your own listener using the `on()` method.
|
|
780
868
|
|
|
781
|
-
The
|
|
869
|
+
The 3rd argument is an options object with two supported properties, `filter` and `useFastput`.
|
|
870
|
+
|
|
871
|
+
The `filter` option is a function which will be called for each item to be uploaded. The function will be called with two arguments. The first argument is the full path of the item to be uploaded and the second argument is a boolean, which will be true if the target path is for a directory. The filter function will be called for each item in the source path. If the function returns true, the item will be uploaded. If it returns false, it will be filtered and not uploaded. The filter function is called via the `Array.filter` method. These array comprehension methods are known to be unsafe for asynchronous functions. Therefore, only synchronous filter functions are supported at this time.
|
|
872
|
+
|
|
873
|
+
The `useFastput` option is a boolean option. If `true`, the method will use the faster `fastPut()` method to upload files. Although this method is faster, it is not supported by all SFTP servers. Enabling this option when unsupported by the remote SFTP server will result in failures.
|
|
782
874
|
|
|
783
875
|
- **srcDir:** A local file path specified as a string
|
|
784
876
|
- **dstDir:** A remote file path specified as a string
|
|
785
|
-
- **
|
|
877
|
+
- **options:** An options object which supports two properties, `filter` and `useFastput`. A filter predicate function which is called for each item in the source path. The argument will receive two arguments. The first is the full path to the item and the second is a boolean which will be true if the item is a directory. If the function returns true, the item will be uploaded, otherwise it will be filtered out and ignored. The `useFastput` option is a boolean option. If `true`, the method will use the faster, but less supported, `fastPut()` method to transfer files. The default is to use the slightly slower, but better supported, `put()` method.
|
|
786
878
|
|
|
787
879
|
1. Example
|
|
788
880
|
|
|
@@ -834,17 +926,22 @@ The optionsl *filter* argument is a function which will be called for each item
|
|
|
834
926
|
|
|
835
927
|
```
|
|
836
928
|
|
|
837
|
-
|
|
929
|
+
|
|
930
|
+
<a id="orgefe6fc6"></a>
|
|
931
|
+
|
|
932
|
+
### downloadDir(srcDir, dstDir, options) ==> string
|
|
838
933
|
|
|
839
934
|
Download the remote directory specified by `srcDir` to the local file system directory specified by `dstDir`. The `dstDir` directory will be created if required. All sub directories within `srcDir` will also be copied. Any existing files in the local path will be overwritten. No files in the local path will be deleted.
|
|
840
935
|
|
|
841
936
|
The method also emites `download` events to provide a way to monitor download progress. The download event listener is called with one argument, an object with two properties, source and destination. The source property is the path to the remote file that has been downloaded and the destination is the local path to where the file was downloaded to. You can add a listener for this event using the `on()` method.
|
|
842
937
|
|
|
843
|
-
The
|
|
938
|
+
The `options` argument is an options object with two supported properties, `filter` and `useFastget`. The `filter` argument is a predicate function which will be called with two arguments for each potential item to be downloaded. The first argument is the full path of the item and the second argument is a boolean, which will be true if the item is a directory. If the function returns true, the item will be included in the download. If it returns false, it will be filtered and ignored. The filter function is called via the `Array.filter` method. These array comprehension methods are known to be unsafe for asynchronous functions. Therefore, only synchronous filter functions are supported at this time.
|
|
939
|
+
|
|
940
|
+
If the `useFastget` property is set to `true`, the method will use `fastGet()` to transfer files. The `fastGet` method is faster, but not supported by all SFTP services.
|
|
844
941
|
|
|
845
942
|
- **srcDir:** A remote file path specified as a string
|
|
846
943
|
- **dstDir:** A local file path specified as a string
|
|
847
|
-
- **
|
|
944
|
+
- **options:** An object with two supported properties, `filter` and `useFastget`. The filter property is a function accepting two arguments, the full path to an item and a boolean value which will be true if the item is a directory. The function is called for each item in the download path and should return true to include the item and false to exclude it in the download. The `useFastget` property is a boolean. If true, the `fastGet()` method will be used to transfer files. If `false` (the default), the slower but better supported `get()` mehtod is used. .
|
|
848
945
|
|
|
849
946
|
1. Example
|
|
850
947
|
|
|
@@ -894,7 +991,52 @@ The optional *filter* argument is a predicate function which will be called with
|
|
|
894
991
|
|
|
895
992
|
```
|
|
896
993
|
|
|
897
|
-
|
|
994
|
+
|
|
995
|
+
<a id="org783e363"></a>
|
|
996
|
+
|
|
997
|
+
### createReadStream(remotePath, options)) ==> stream object
|
|
998
|
+
|
|
999
|
+
Returns a read stream object which is attached to the remote file specified by the `remotePath` argument. This is a low level method which just returns a read stream object. Client code is fully responsible for managing and releasing the resources associated with the stream once finished i.e. closing files, removing listeners etc.
|
|
1000
|
+
|
|
1001
|
+
- **remotePath:** A remote file path specified as a string
|
|
1002
|
+
- **options:** An options object. Supported properties are
|
|
1003
|
+
- **flags:** defaults to 'r'
|
|
1004
|
+
- **encoding:** defaults to null
|
|
1005
|
+
- **handle:** defaults to null
|
|
1006
|
+
- **mode:** 0o666
|
|
1007
|
+
- **autoClose:** defaults to true. If set to false, client code is responsible for closing file descriptors when finished
|
|
1008
|
+
- **start:** Default 0. Position to start reading bytes from (inclusive)
|
|
1009
|
+
- **end:** Postion to stop reading bytes (inclusive).
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
<a id="orgf45313d"></a>
|
|
1013
|
+
|
|
1014
|
+
### createWriteStream(remotePath, options) ==> stream object
|
|
1015
|
+
|
|
1016
|
+
Returns a write stream object which is attached to the remote file specified in the `remotePath` argument. This is a low legvel function which just returns the stream object. Client code is fully responsible for managing that object, including closing any file descriptiors and removing listeners etc.
|
|
1017
|
+
|
|
1018
|
+
- **remotePath:** Path to the remote file specified as a string
|
|
1019
|
+
- **options:** An object containing stream options. Supported properties include
|
|
1020
|
+
- **flags:** default 'w'
|
|
1021
|
+
- **encoding:** defulat null
|
|
1022
|
+
- **mode:** 0o666
|
|
1023
|
+
- **autoClose:** true
|
|
1024
|
+
- **start:** Byte position to start writing from (inclusive). May require changing flag to 'r+'.
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
<a id="orga31c8c1"></a>
|
|
1028
|
+
|
|
1029
|
+
### rcopy(srcPath, dstPath) ==> string
|
|
1030
|
+
|
|
1031
|
+
Perfrom a remote file copy. The file identified by the `srcPath` argument will be copied to the file specified as the `dstPath` argument. The directory where `dstPath` will be placed must exist, but the actual file must not i.e. no overwrites allowed.
|
|
1032
|
+
|
|
1033
|
+
- **srcPath:** Path to remote file to be copied specified as a string
|
|
1034
|
+
- **dstPath:** Path to where the copy will be creaeted specified as a string
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
<a id="org4d02fde"></a>
|
|
1038
|
+
|
|
1039
|
+
### end() ==> boolean
|
|
898
1040
|
|
|
899
1041
|
Ends the current client session, releasing the client socket and associated resources. This function also removes all listeners associated with the client.
|
|
900
1042
|
|
|
@@ -915,7 +1057,10 @@ Ends the current client session, releasing the client socket and associated reso
|
|
|
915
1057
|
});
|
|
916
1058
|
```
|
|
917
1059
|
|
|
918
|
-
|
|
1060
|
+
|
|
1061
|
+
<a id="orge5769d3"></a>
|
|
1062
|
+
|
|
1063
|
+
### Add and Remove Listeners
|
|
919
1064
|
|
|
920
1065
|
Although normally not required, you can add and remove custom listeners on the ssh2 client object. This object supports a number of events, but only a few of them have any meaning in the context of SFTP. These are
|
|
921
1066
|
|
|
@@ -933,15 +1078,24 @@ Although normally not required, you can add and remove custom listeners on the s
|
|
|
933
1078
|
|
|
934
1079
|
Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
|
|
935
1080
|
|
|
936
|
-
# Platform Quirks & Warnings<a id="sec-5"></a>
|
|
937
1081
|
|
|
938
|
-
|
|
1082
|
+
<a id="org3e6ddf9"></a>
|
|
1083
|
+
|
|
1084
|
+
# Platform Quirks & Warnings
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
<a id="org61f0fe5"></a>
|
|
1088
|
+
|
|
1089
|
+
## Server Capabilities
|
|
939
1090
|
|
|
940
1091
|
All SFTP servers and platforms are not equal. Some facilities provided by `ssh2-sftp-client` either depend on capabilities of the remote server or the underlying capabilities of the remote server platform. As an example, consider `chmod()`. This command depends on a remote filesystem which implements the 'nix' concept of users and groups. The *win32* platform does not have the same concept of users and groups, so `chmod()` will not behave in the same way.
|
|
941
1092
|
|
|
942
1093
|
One way to determine whether an issue you are encountering is due to `ssh2-sftp-client` or due to the remote server or server platform is to use a simple CLI sftp program, such as openSSH's sftp command. If you observe the same behaviour using plain `sftp` on the command line, the issue is likely due to server or remote platform limitations. Note that you should not use a GUI sftp client, like `Filezilla` or `winSCP` as such GUI programs often attempt to hide these server and platform incompatibilities and will take additional steps to simulate missing functionality etc. You want to use a CLI program which does as little as possible.
|
|
943
1094
|
|
|
944
|
-
|
|
1095
|
+
|
|
1096
|
+
<a id="orga7d6067"></a>
|
|
1097
|
+
|
|
1098
|
+
## Promises, Events & Managing Exceptions
|
|
945
1099
|
|
|
946
1100
|
One of the challenges in providing a Promise based API over a module like SSH2, which is event based is how to ensure events are handled appropriately. The challenge is due to the synchronous nature of events. You cannot use `try/catch` for events because you have no way of knowing when the event might fire. For example, it could easily fire after your `try/catch` block as completed execution.
|
|
947
1101
|
|
|
@@ -957,25 +1111,40 @@ The other area where additional events are fired is during the end() call. To de
|
|
|
957
1111
|
|
|
958
1112
|
In addition to the promise based event handlers, `ssh2-sftp-client` also implements global event handlers which will catch any `error`, `end` or `close` events. Essentially, these global handlers only reset the `sftp` property of the client object, effectively ensuring any subsequent calls are rejected and in the case of an error, send the error to the console.
|
|
959
1113
|
|
|
960
|
-
|
|
1114
|
+
|
|
1115
|
+
<a id="orgc08d427"></a>
|
|
1116
|
+
|
|
1117
|
+
### Adding Custom Handlers
|
|
961
1118
|
|
|
962
1119
|
While the above strategies appear to work for the majority of use cases, there are always going to be edge cases which require more flexible or powerful event handling. To support this, the `on()` and `removeListener()` methods are provided. Any event listener added using the `on()` method will be added at the beginning of the list of handlers for that event, ensuring it will be called before any global or promise local events. See the documentation for the `on()` method for details.
|
|
963
1120
|
|
|
964
|
-
|
|
1121
|
+
|
|
1122
|
+
<a id="org5439468"></a>
|
|
1123
|
+
|
|
1124
|
+
## Windows Based Servers
|
|
965
1125
|
|
|
966
1126
|
It appears that when the sftp server is running on Windows, a *ECONNRESET* error signal is raised when the end() method is called. Unfortunately, this signal is raised after a considerable delay. This means we cannot remove the error handler used in the end() promise as otherwise you will get an uncaught exception error. Leaving the handler in place, even though we will ignore this error, solves that issue, but unfortunately introduces a new problem. Because we are not removing the listener, if you re-use the client object for subsequent connections, an additional error handler will be added. If this happens more than 11 times, you will eventually see the Node warning about a possible memory leak. This is because node monitors the number of error handlers and if it sees more than 11 added to an object, it assumes there is a problem and generates the warning.
|
|
967
1127
|
|
|
968
1128
|
The best way to avoid this issue is to not re-use client objects. Always generate a new sftp client object for each new connection.
|
|
969
1129
|
|
|
970
|
-
|
|
1130
|
+
|
|
1131
|
+
<a id="orgae9129c"></a>
|
|
1132
|
+
|
|
1133
|
+
## Don't Re-use SftpClient Objects
|
|
971
1134
|
|
|
972
1135
|
Due to an issue with *ECONNRESET* error signals when connecting to Windows based SFTP servers, it is not possible to remove the error handler in the end() method. This means that if you re-use the SftpClient object for multiple connections e.g. calling connect(), then end(), then connect() etc, you run the risk of multiple error handlers being added to the SftpClient object. After 11 handlers have been added, Node will generate a possible memory leak warning.
|
|
973
1136
|
|
|
974
1137
|
To avoid this problem, don't re-use SftpClient objects. Generate a new SftpClient object for each connection. You can perform multiple actions with a single connection e.g. upload multiple files, download multiple files etc, but after you have called end(), you should not try to re-use the object with a further connect() call. Create a new object instead.
|
|
975
1138
|
|
|
976
|
-
# FAQ<a id="sec-6"></a>
|
|
977
1139
|
|
|
978
|
-
|
|
1140
|
+
<a id="org3761660"></a>
|
|
1141
|
+
|
|
1142
|
+
# FAQ
|
|
1143
|
+
|
|
1144
|
+
|
|
1145
|
+
<a id="org1e669d1"></a>
|
|
1146
|
+
|
|
1147
|
+
## Remote server drops connections with only an end event
|
|
979
1148
|
|
|
980
1149
|
Many SFTP servers have rate limiting protection which will drop connections once a limit has been reached. In particular, openSSH has the setting `MaxStartups`, which can be a tuple of the form `max:drop:full` where `max` is the maximum allowed unauthenticated connections, `drop` is a percentage value which specifies percentage of connections to be dropped once `max` connections has been reached and `full` is the number of connections at which point all subsequent connections will be dropped. e.g. `10:30:60` means allow up to 10 unauthenticated connections after which drop 30% of connection attempts until reaching 60 unauthenticated connections, at which time, drop all attempts.
|
|
981
1150
|
|
|
@@ -983,7 +1152,10 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
|
|
|
983
1152
|
|
|
984
1153
|
One way to avoid this type of issue is to add a delay between connection attempts. It does not need to be a very long delay - just sufficient to permit the previous connection to be authenticated. In fact, the default setting for openSSH is `10:30:60`, so you really just need to have enough delay to ensure that the 1st connection has completed authentication before the 11th connection is attempted.
|
|
985
1154
|
|
|
986
|
-
|
|
1155
|
+
|
|
1156
|
+
<a id="org71d5bab"></a>
|
|
1157
|
+
|
|
1158
|
+
## How can I pass writable stream as dst for get method?
|
|
987
1159
|
|
|
988
1160
|
If the dst argument passed to the get method is a writeable stream, the remote file will be piped into that writeable. If the writeable you pass in is a writeable stream created with `fs.createWriteStream()`, the data will be written to the file specified in the constructor call to `createWriteStream()`.
|
|
989
1161
|
|
|
@@ -1039,7 +1211,10 @@ sftp
|
|
|
1039
1211
|
});
|
|
1040
1212
|
```
|
|
1041
1213
|
|
|
1042
|
-
|
|
1214
|
+
|
|
1215
|
+
<a id="orgaafc964"></a>
|
|
1216
|
+
|
|
1217
|
+
## How can I upload files without having to specify a password?
|
|
1043
1218
|
|
|
1044
1219
|
There are a couple of ways to do this. Essentially, you want to setup SSH keys and use these for authentication to the remote server.
|
|
1045
1220
|
|
|
@@ -1071,7 +1246,10 @@ sftp.connect({
|
|
|
1071
1246
|
}
|
|
1072
1247
|
```
|
|
1073
1248
|
|
|
1074
|
-
|
|
1249
|
+
|
|
1250
|
+
<a id="orgebf85e2"></a>
|
|
1251
|
+
|
|
1252
|
+
## How can I connect through a Socks Proxy
|
|
1075
1253
|
|
|
1076
1254
|
This solution was provided by @jmorino.
|
|
1077
1255
|
|
|
@@ -1083,7 +1261,7 @@ const host = 'my-sftp-server.net';
|
|
|
1083
1261
|
const port = 22; // default SSH/SFTP port on remote server
|
|
1084
1262
|
|
|
1085
1263
|
// connect to SOCKS 5 proxy
|
|
1086
|
-
const {
|
|
1264
|
+
const { socks } = await SocksClient.createConnection({
|
|
1087
1265
|
proxy: {
|
|
1088
1266
|
host: 'my.proxy', // proxy hostname
|
|
1089
1267
|
port: 1080, // proxy port
|
|
@@ -1096,7 +1274,7 @@ const { socket } = await SocksClient.createConnection({
|
|
|
1096
1274
|
const client = new SFTPClient();
|
|
1097
1275
|
client.connect({
|
|
1098
1276
|
host,
|
|
1099
|
-
sock: socket, // pass the socket to proxy here (see ssh2 doc)
|
|
1277
|
+
sock: socks.socket, // pass the socket to proxy here (see ssh2 doc)
|
|
1100
1278
|
username: '.....',
|
|
1101
1279
|
privateKey: '.....'
|
|
1102
1280
|
})
|
|
@@ -1104,13 +1282,19 @@ client.connect({
|
|
|
1104
1282
|
// client is connected
|
|
1105
1283
|
```
|
|
1106
1284
|
|
|
1107
|
-
|
|
1285
|
+
|
|
1286
|
+
<a id="org528d9ea"></a>
|
|
1287
|
+
|
|
1288
|
+
## Timeout while waiting for handshake or handshake errors
|
|
1108
1289
|
|
|
1109
1290
|
Some users have encountered the error 'Timeout while waiting for handshake' or 'Handshake failed, no matching client->server ciphers. This is often due to the client not having the correct configuration for the transport layer algorithms used by ssh2. One of the connect options provided by the ssh2 module is `algorithm`, which is an object that allows you to explicitly set the key exchange, ciphers, hmac and compression algorithms as well as server host key used to establish the initial secure connection. See the SSH2 documentation for details. Getting these parameters correct usually resolves the issue.
|
|
1110
1291
|
|
|
1111
1292
|
When encountering this type of problem, one worthwhile approach is to use openSSH's CLI sftp program with the `-v` switch to raise loggin levels. This will show you what algorithms the CLI is using. You can then use this information to match the names with the accepted algorithm names documented in the `ssh2` README to set the properties in the `algorithms` object.
|
|
1112
1293
|
|
|
1113
|
-
|
|
1294
|
+
|
|
1295
|
+
<a id="org3b6ff20"></a>
|
|
1296
|
+
|
|
1297
|
+
## How can I limit upload/download speed
|
|
1114
1298
|
|
|
1115
1299
|
If you want to limit the amount of bandwidth used during upload/download of data, you can use a stream to limit throughput. The following example was provided by *kennylbj*. Note that there is a caveat that we must set the `autoClose` flag to false to avoid calling an extra `_read()` on a closed stream that may cause \_get Permission Denied error in ssh2-streams.
|
|
1116
1300
|
|
|
@@ -1147,7 +1331,10 @@ try {
|
|
|
1147
1331
|
}
|
|
1148
1332
|
```
|
|
1149
1333
|
|
|
1150
|
-
|
|
1334
|
+
|
|
1335
|
+
<a id="orgb5e285f"></a>
|
|
1336
|
+
|
|
1337
|
+
## Connection hangs or fails for larger files
|
|
1151
1338
|
|
|
1152
1339
|
This was contributed by Ladislav Jacho. Thanks.
|
|
1153
1340
|
|
|
@@ -1155,11 +1342,17 @@ A symptom of this issue is that you are able to upload small files, but uploadin
|
|
|
1155
1342
|
|
|
1156
1343
|
For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
|
|
1157
1344
|
|
|
1158
|
-
|
|
1345
|
+
|
|
1346
|
+
<a id="org18ce0c2"></a>
|
|
1347
|
+
|
|
1348
|
+
# Examples
|
|
1159
1349
|
|
|
1160
1350
|
I have started collecting example scripts in the example directory of the repository. These are mainly scripts I have put together in order to investigate issues or provide samples for users. They are not robust, lack adequate error handling and may contain errors. However, I think they are still useful for helping developers see how the module and API can be used.
|
|
1161
1351
|
|
|
1162
|
-
|
|
1352
|
+
|
|
1353
|
+
<a id="orgf4b6c80"></a>
|
|
1354
|
+
|
|
1355
|
+
# Troubleshooting
|
|
1163
1356
|
|
|
1164
1357
|
The `ssh2-sftp-client` module is essentially a wrapper around the `ssh2` and `ssh2-streams` modules, providing a higher level `promise` based API. When you run into issues, it is important to try and determine where the issue lies - either in the ssh2-sftp-client module or the underlying `ssh2` and `ssh2-streams` modules. One way to do this is to first identify a minimal reproducible example which reproduces the issue. Once you have that, try to replicate the functionality just using the `ssh2` and `ssh2-streams` modules. If the issue still occurs, then you can be fairly confident it is something related to those later 2 modules and therefore and issue which should be referred to the maintainer of that module.
|
|
1165
1358
|
|
|
@@ -1171,11 +1364,17 @@ Note also that in the repository there are two useful directories. The first is
|
|
|
1171
1364
|
|
|
1172
1365
|
The second directory is the validation directory. I have some very simple scripts in this directory which perform basic tasks using only the `ssh2` modules (no `ssh2-sftp-client` module). These can be useful when trying to determine if the issue is with the underlying `ssh2` module or the `ssh2-sftp-client` wrapper module.
|
|
1173
1366
|
|
|
1174
|
-
|
|
1367
|
+
|
|
1368
|
+
<a id="orga419582"></a>
|
|
1369
|
+
|
|
1370
|
+
## Common Errors
|
|
1175
1371
|
|
|
1176
1372
|
There are some common errors people tend to make when using Promises or Asyc/Await. These are by far the most common problem found in issues logged against this module. Please check for some of these before logging your issue.
|
|
1177
1373
|
|
|
1178
|
-
|
|
1374
|
+
|
|
1375
|
+
<a id="orgd5f3557"></a>
|
|
1376
|
+
|
|
1377
|
+
### Not returning the promise in a `then()` block
|
|
1179
1378
|
|
|
1180
1379
|
All methods in `ssh2-sftp-client` return a Promise. This means methods are executed *asynchrnously*. When you call a method inside the `then()` block of a promise chain, it is critical that you return the Promise that call generates. Failing to do this will result in the `then()` block completing and your code starting execution of the next `then()`, `catch()` or `finally()` block before your promise has been fulfilled. For example, the following will not do what you expect
|
|
1181
1380
|
|
|
@@ -1209,7 +1408,10 @@ Note the `return` statements. These ensure that the Promise returned by the clie
|
|
|
1209
1408
|
|
|
1210
1409
|
A common symptom of this type of error is for file uploads or download to fail to complete or for data in those files to be truncated. What is happening is that the connection is being ended before the transfer has completed.
|
|
1211
1410
|
|
|
1212
|
-
|
|
1411
|
+
|
|
1412
|
+
<a id="org557d1c5"></a>
|
|
1413
|
+
|
|
1414
|
+
### Mixing Promise Chains and Async/Await
|
|
1213
1415
|
|
|
1214
1416
|
Another common error is to mix Promise chains and async/await calls. This is rarely a great idea. While you can do this, it tends to create complicated and difficult to maintain code. Select one approach and stick with it. Both approaches are functionally equivalent, so there is no reason to mix up the two paradigms. My personal preference would be to use async/await as I think that is more *natural* for most developers. For example, the following is more complex and difficult to follow than necessary (and has a bug!)
|
|
1215
1417
|
|
|
@@ -1266,7 +1468,10 @@ async function doSftp() {
|
|
|
1266
1468
|
}
|
|
1267
1469
|
```
|
|
1268
1470
|
|
|
1269
|
-
|
|
1471
|
+
|
|
1472
|
+
<a id="org2bf9a3a"></a>
|
|
1473
|
+
|
|
1474
|
+
### Try/catch and Error Handlers
|
|
1270
1475
|
|
|
1271
1476
|
Another common error is to try and use a try/catch block to catch event signals, such as an error event. In general, you cannot use try/catch blocks for asynchronous code and expect errors to be caught by the `catch` block. Handling errors in asynchronous code is one of the key reasons we now have the Promise and async/await frameworks.
|
|
1272
1477
|
|
|
@@ -1274,17 +1479,26 @@ The basic problem is that the try/catch block will have completed execution befo
|
|
|
1274
1479
|
|
|
1275
1480
|
Error events are essentially asynchronous code. You don't know when such events will fire. Therefore, you cannot use a try/catch block to catch such event errors. Even creating an error handler which then throws an exception won't help as the key problem is that your try/catch block has already executed. There are a number of alternative ways to deal with this situation. However, the key symptom is that you see occasional uncaught error exceptions that cause your script to exit abnormally despite having try/catch blocks in your script. What you need to do is look at your code and find where errors are raised asynchronously and use an event handler or some other mechanism to manage any errors raised.
|
|
1276
1481
|
|
|
1277
|
-
|
|
1482
|
+
|
|
1483
|
+
<a id="org89795d7"></a>
|
|
1484
|
+
|
|
1485
|
+
### Server Differences
|
|
1278
1486
|
|
|
1279
1487
|
Not all SFTP servers are the same. Like most standards, the SFTP protocol has some level of interpretation and allows different levels of compliance. This means there can be differences in behaviour between different servers and code which works with one server will not work the same with another. For example, the value returned by *realpath* for non-existent objects can differ significantly. Some servers will throw an error for a particular operation while others will just return null, some servers support concurrent operations (such as used by fastGet/fastPut) while others will not and of course, the text of error messages can vary significantly. In particular, we have noticed significant differences across different platforms. It is therefore advisable to do comprehensive testing when the SFTP server is moved to a new platform. This includes moving from to a cloud based service even if the underlying platform remains the same. I have noticed that some cloud platforms can generate unexpected events, possibly related to additional functionality or features associated with the cloud implementation. For example, it appears SFTP servers running under Azure will generate an error event when the connection is closed even when the client has requested the connection be terminated. The same SFTP server running natively on Windows does not appear to exhibit such behaviour.
|
|
1280
1488
|
|
|
1281
|
-
|
|
1489
|
+
|
|
1490
|
+
<a id="orgbec08b0"></a>
|
|
1491
|
+
|
|
1492
|
+
### Avoid Concurrent Operations
|
|
1282
1493
|
|
|
1283
1494
|
Technically, SFTP should be able to perform multiple operations concurrently. As node is single threaded, what we a really talking about is running multiple execution contexts as a pool where node will switch contexts when each context is blocked due to things like waiting on network data etc. However, I have found this to be extremely unreliable and of very little benefit from a performance perspective. My recommendation is to therefore avoid executing multiple requests over the same connection in parallel (for example, generating multiple `get()` promises and using something like `Promise.all()` to resolve them.
|
|
1284
1495
|
|
|
1285
1496
|
If you are going to try and perform concurrent operations, you need to test extensively and ensure you are using data which is large enough that context switching does occur (i.e. the request is not completed in a single run). Some SFTP servers will handle concurrent operations better than others.
|
|
1286
1497
|
|
|
1287
|
-
|
|
1498
|
+
|
|
1499
|
+
<a id="org124cfc9"></a>
|
|
1500
|
+
|
|
1501
|
+
## Debugging Support
|
|
1288
1502
|
|
|
1289
1503
|
You can add a `debug` property to the config object passed in to `connect()` to turn on debugging. This will generate quite a lot of output. The value of the property should be a function which accepts a single string argument. For example;
|
|
1290
1504
|
|
|
@@ -1314,7 +1528,10 @@ If you just want to see debug messages from `ssh2-sftp-client` and exclude debug
|
|
|
1314
1528
|
}
|
|
1315
1529
|
```
|
|
1316
1530
|
|
|
1317
|
-
|
|
1531
|
+
|
|
1532
|
+
<a id="org5f3c941"></a>
|
|
1533
|
+
|
|
1534
|
+
# Logging Issues
|
|
1318
1535
|
|
|
1319
1536
|
Please log an issue for all bugs, questions, feature and enhancement requests. Please ensure you include the module version, node version and platform.
|
|
1320
1537
|
|
|
@@ -1328,7 +1545,10 @@ I am happy to try and help diagnose and fix any issues you encounter while using
|
|
|
1328
1545
|
|
|
1329
1546
|
Perhaps the best assistance is a minimal reproducible example of the issue. Once the issue can be readily reproduced, it can usually be fixed very quickly.
|
|
1330
1547
|
|
|
1331
|
-
|
|
1548
|
+
|
|
1549
|
+
<a id="org6e82efc"></a>
|
|
1550
|
+
|
|
1551
|
+
# Pull Requests
|
|
1332
1552
|
|
|
1333
1553
|
Pull requests are always welcomed. However, please ensure your changes pass all tests and if you're adding a new feature, that tests for that feature are included. Likewise, for new features or enhancements, please include any relevant documentation updates.
|
|
1334
1554
|
|
|
@@ -1340,7 +1560,10 @@ This module will adopt a standard semantic versioning policy. Please indicate in
|
|
|
1340
1560
|
- **Minor:** Minor change, enhancement or new feature which does not change existing API and will not break existing client code.
|
|
1341
1561
|
- **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
|
|
1342
1562
|
|
|
1343
|
-
|
|
1563
|
+
|
|
1564
|
+
<a id="org680b5c2"></a>
|
|
1565
|
+
|
|
1566
|
+
# Contributors
|
|
1344
1567
|
|
|
1345
1568
|
This module was initially written by jyu213. On August 23rd, 2019, theophilusx took over responsibility for maintaining this module. A number of other people have contributed to this module, but until now, this was not tracked. My intention is to credit anyone who contributes going forward.
|
|
1346
1569
|
|
|
@@ -1359,4 +1582,4 @@ Thanks to the following for their contributions -
|
|
|
1359
1582
|
- **Emma Milner:** Contributed fix for put() bug
|
|
1360
1583
|
- **Witni Davis:** Contributed PR to fix put() RCE when using 'finish' rather than 'close' to resolve promise
|
|
1361
1584
|
- **Maik Marschner:** Contributed fix for connect() not returning sftp object. Also included test to check for this regression in future.
|
|
1362
|
-
- **cakemasher:** Contributed fix for removeTempListeners().
|
|
1585
|
+
- **cakemasher:** Contributed fix for removeTempListeners().
|