ssh2-sftp-client 8.1.0 → 9.0.0
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/.dir-local.el +1 -0
- package/README.md +182 -130
- package/README.org +129 -52
- package/package.json +4 -4
- package/src/constants.js +1 -0
- package/src/index.js +875 -661
- package/src/utils.js +43 -106
package/README.md
CHANGED
|
@@ -1,62 +1,65 @@
|
|
|
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
|
-
|
|
59
|
-
|
|
1
|
+
- [Overview](#orgdb4a63c)
|
|
2
|
+
- [Version 9.x Changes](#org3c3e874)
|
|
3
|
+
- [Installation](#orga783863)
|
|
4
|
+
- [Basic Usage](#org2f471c4)
|
|
5
|
+
- [Documentation](#orgb5102f6)
|
|
6
|
+
- [Specifying Paths](#orgdae808e)
|
|
7
|
+
- [Methods](#orga8e09d8)
|
|
8
|
+
- [new SftpClient(name) ===> SFTP client object](#org518d51a)
|
|
9
|
+
- [connect(config) ===> SFTP object](#orgda648f9)
|
|
10
|
+
- [list(path, filter) ==> Array[object]](#org3258731)
|
|
11
|
+
- [exists(path) ==> boolean](#orgaae7860)
|
|
12
|
+
- [stat(path) ==> object](#org1e4f14a)
|
|
13
|
+
- [get(path, dst, options) ==> String|Stream|Buffer](#org447f36c)
|
|
14
|
+
- [fastGet(remotePath, localPath, options) ===> string](#org54de461)
|
|
15
|
+
- [put(src, remotePath, options) ==> string](#orgee0d1ef)
|
|
16
|
+
- [fastPut(localPath, remotePath, options) ==> string](#orgb9d8ee2)
|
|
17
|
+
- [append(input, remotePath, options) ==> string](#org1eaae65)
|
|
18
|
+
- [mkdir(path, recursive) ==> string](#org1c3ab90)
|
|
19
|
+
- [rmdir(path, recursive) ==> string](#org003ef11)
|
|
20
|
+
- [delete(path, noErrorOK) ==> string](#orgd3e3a35)
|
|
21
|
+
- [rename(fromPath, toPath) ==> string](#org3343888)
|
|
22
|
+
- [posixRename(fromPath, toPath) ==> string](#orgb8f840c)
|
|
23
|
+
- [chmod(path, mode) ==> string](#org15551c9)
|
|
24
|
+
- [realPath(path) ===> string](#org4ec95c3)
|
|
25
|
+
- [cwd() ==> string](#orgc1265f0)
|
|
26
|
+
- [uploadDir(srcDir, dstDir, options) ==> string](#org5c10c06)
|
|
27
|
+
- [downloadDir(srcDir, dstDir, options) ==> string](#org614dc71)
|
|
28
|
+
- [createReadStream(remotePath, options)) ==> stream object](#org906505f)
|
|
29
|
+
- [createWriteStream(remotePath, options) ==> stream object](#orgf5b3858)
|
|
30
|
+
- [rcopy(srcPath, dstPath) ==> string](#org3716ce1)
|
|
31
|
+
- [end() ==> boolean](#org93bfcf3)
|
|
32
|
+
- [Add and Remove Listeners](#org8fe6c29)
|
|
33
|
+
- [Platform Quirks & Warnings](#org763e7bf)
|
|
34
|
+
- [Server Capabilities](#org3c7ec29)
|
|
35
|
+
- [Promises, Events & Managing Exceptions](#org13e7f16)
|
|
36
|
+
- [Adding Custom Handlers](#orgbea2ca0)
|
|
37
|
+
- [Windows Based Servers](#org13d5c2b)
|
|
38
|
+
- [Don't Re-use SftpClient Objects](#org56317e1)
|
|
39
|
+
- [FAQ](#orgb28c841)
|
|
40
|
+
- [Remote server drops connections with only an end event](#orgb4c1c9d)
|
|
41
|
+
- [How can I pass writable stream as dst for get method?](#orgf09bf16)
|
|
42
|
+
- [How can I upload files without having to specify a password?](#org10382e7)
|
|
43
|
+
- [How can I connect through a Socks Proxy](#org34d3eb7)
|
|
44
|
+
- [Timeout while waiting for handshake or handshake errors](#orgbe4be79)
|
|
45
|
+
- [How can I limit upload/download speed](#org36a1f06)
|
|
46
|
+
- [Connection hangs or fails for larger files](#org293ce0b)
|
|
47
|
+
- [Examples](#org897888b)
|
|
48
|
+
- [Troubleshooting](#orgd93090a)
|
|
49
|
+
- [Common Errors](#org44294d4)
|
|
50
|
+
- [Not returning the promise in a `then()` block](#org44783ba)
|
|
51
|
+
- [Mixing Promise Chains and Async/Await](#org1bee008)
|
|
52
|
+
- [Try/catch and Error Handlers](#org004696d)
|
|
53
|
+
- [Server Differences](#org9131050)
|
|
54
|
+
- [Avoid Concurrent Operations](#org6c9fbb1)
|
|
55
|
+
- [Debugging Support](#org52099c1)
|
|
56
|
+
- [Logging Issues](#orga96ad01)
|
|
57
|
+
- [Pull Requests](#org0fa18ba)
|
|
58
|
+
- [Contributors](#org05880d2)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
<a id="orgdb4a63c"></a>
|
|
60
63
|
|
|
61
64
|
# Overview
|
|
62
65
|
|
|
@@ -64,27 +67,28 @@ an SFTP client for node.js, a wrapper around [SSH2](https://github.com/mscdex/ss
|
|
|
64
67
|
|
|
65
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.
|
|
66
69
|
|
|
67
|
-
Current stable release is **
|
|
70
|
+
Current stable release is **v9.0.0**.
|
|
68
71
|
|
|
69
72
|
Code has been tested against Node versions 14.19.1, 16.15.0 and 18.1.0
|
|
70
73
|
|
|
71
74
|
Node versions < 12.x are not supported. However, node v10.x should still work, although some tests will fail due to changes in file system functions used in test setup and tear down.
|
|
72
75
|
|
|
73
76
|
|
|
74
|
-
<a id="
|
|
77
|
+
<a id="org3c3e874"></a>
|
|
75
78
|
|
|
76
|
-
## Version
|
|
79
|
+
## Version 9.x Changes
|
|
77
80
|
|
|
78
|
-
- **Breaking Change**:
|
|
81
|
+
- **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.
|
|
82
|
+
- **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!
|
|
83
|
+
- **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.
|
|
84
|
+
- The `uploadDir()` and `downloadDir()` methods now use asynchrounous processes to upload/download files. This should result in improved performance for these two methods.
|
|
85
|
+
- 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.
|
|
86
|
+
- 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.
|
|
87
|
+
- New method: Added `rcopy()` method to perform a remote copy of a file on the remote SFTP server.
|
|
88
|
+
- Bumped ssh2 version to 1.11.0
|
|
79
89
|
|
|
80
|
-
- Internal Change: The `rmdir()` method has been refactored to enable asynchronous deletion of files and sub-directories. This has significantly increased performance when deleting larger directory trees, especially trees which are *broad* with lots of files and directories at the same level. For deep narrow trees, there is less performance benefit because sub-directories must be removed before parents, which imposes synchronous processing. It is likely that for extremely large directory trees, the additional resources required to run large numbers of asynchronous tasks will become problematic. In such situations, it may be necessary to manually break up the deletion process into multiple `rmdir()` calls. However, this is considered a fairly extreme use case which is rare (a use case which wold also have been problematic with the old implementation as performance would have been very poor).
|
|
81
90
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
- v8.1.0: The `rmdir()` method has been further modified to not recurse into directories using async. It was found that on large directories, this could result in too many separate async processes. Only deletion of files is now handled with asynchronous calls.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
<a id="orgd7b0a7e"></a>
|
|
91
|
+
<a id="orga783863"></a>
|
|
88
92
|
|
|
89
93
|
# Installation
|
|
90
94
|
|
|
@@ -93,7 +97,7 @@ npm install ssh2-sftp-client
|
|
|
93
97
|
```
|
|
94
98
|
|
|
95
99
|
|
|
96
|
-
<a id="
|
|
100
|
+
<a id="org2f471c4"></a>
|
|
97
101
|
|
|
98
102
|
# Basic Usage
|
|
99
103
|
|
|
@@ -116,7 +120,7 @@ sftp.connect({
|
|
|
116
120
|
```
|
|
117
121
|
|
|
118
122
|
|
|
119
|
-
<a id="
|
|
123
|
+
<a id="orgb5102f6"></a>
|
|
120
124
|
|
|
121
125
|
# Documentation
|
|
122
126
|
|
|
@@ -125,7 +129,7 @@ The connection options are the same as those offered by the underlying SSH2 modu
|
|
|
125
129
|
All the methods will return a Promise, except for `on()` and `removeListener()`, which are typically only used in special use cases.
|
|
126
130
|
|
|
127
131
|
|
|
128
|
-
<a id="
|
|
132
|
+
<a id="orgdae808e"></a>
|
|
129
133
|
|
|
130
134
|
## Specifying Paths
|
|
131
135
|
|
|
@@ -158,12 +162,12 @@ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
|
|
|
158
162
|
This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
|
|
159
163
|
|
|
160
164
|
|
|
161
|
-
<a id="
|
|
165
|
+
<a id="orga8e09d8"></a>
|
|
162
166
|
|
|
163
167
|
## Methods
|
|
164
168
|
|
|
165
169
|
|
|
166
|
-
<a id="
|
|
170
|
+
<a id="org518d51a"></a>
|
|
167
171
|
|
|
168
172
|
### new SftpClient(name) ===> SFTP client object
|
|
169
173
|
|
|
@@ -202,7 +206,7 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
|
|
|
202
206
|
```
|
|
203
207
|
|
|
204
208
|
|
|
205
|
-
<a id="
|
|
209
|
+
<a id="orgda648f9"></a>
|
|
206
210
|
|
|
207
211
|
### connect(config) ===> SFTP object
|
|
208
212
|
|
|
@@ -268,14 +272,14 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
268
272
|
```
|
|
269
273
|
|
|
270
274
|
|
|
271
|
-
<a id="
|
|
275
|
+
<a id="org3258731"></a>
|
|
272
276
|
|
|
273
|
-
### list(path,
|
|
277
|
+
### list(path, filter) ==> Array[object]
|
|
274
278
|
|
|
275
279
|
Retrieves a directory listing. This method returns a Promise, which once realised, returns an array of objects representing items in the remote directory.
|
|
276
280
|
|
|
277
281
|
- **path:** {String} Remote directory path
|
|
278
|
-
- **
|
|
282
|
+
- **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.
|
|
279
283
|
|
|
280
284
|
1. Example Use
|
|
281
285
|
|
|
@@ -339,7 +343,7 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
339
343
|
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.
|
|
340
344
|
|
|
341
345
|
|
|
342
|
-
<a id="
|
|
346
|
+
<a id="orgaae7860"></a>
|
|
343
347
|
|
|
344
348
|
### exists(path) ==> boolean
|
|
345
349
|
|
|
@@ -375,7 +379,7 @@ Tests to see if remote file or directory exists. Returns type of remote object i
|
|
|
375
379
|
```
|
|
376
380
|
|
|
377
381
|
|
|
378
|
-
<a id="
|
|
382
|
+
<a id="org1e4f14a"></a>
|
|
379
383
|
|
|
380
384
|
### stat(path) ==> object
|
|
381
385
|
|
|
@@ -426,7 +430,7 @@ Returns the attributes associated with the object pointed to by `path`.
|
|
|
426
430
|
```
|
|
427
431
|
|
|
428
432
|
|
|
429
|
-
<a id="
|
|
433
|
+
<a id="org447f36c"></a>
|
|
430
434
|
|
|
431
435
|
### get(path, dst, options) ==> String|Stream|Buffer
|
|
432
436
|
|
|
@@ -482,7 +486,7 @@ In general, if you're going to pass in a string as the destination, you are bett
|
|
|
482
486
|
- **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'.
|
|
483
487
|
|
|
484
488
|
|
|
485
|
-
<a id="
|
|
489
|
+
<a id="org54de461"></a>
|
|
486
490
|
|
|
487
491
|
### fastGet(remotePath, localPath, options) ===> string
|
|
488
492
|
|
|
@@ -525,7 +529,7 @@ Downloads a file at remotePath to localPath using parallel reads for faster thro
|
|
|
525
529
|
```
|
|
526
530
|
|
|
527
531
|
|
|
528
|
-
<a id="
|
|
532
|
+
<a id="orgee0d1ef"></a>
|
|
529
533
|
|
|
530
534
|
### put(src, remotePath, options) ==> string
|
|
531
535
|
|
|
@@ -575,7 +579,7 @@ Upload data from local system to remote server. If the `src` argument is a strin
|
|
|
575
579
|
- **Tip:** If the src argument is a path string, consider just using `fastPut()`.
|
|
576
580
|
|
|
577
581
|
|
|
578
|
-
<a id="
|
|
582
|
+
<a id="orgb9d8ee2"></a>
|
|
579
583
|
|
|
580
584
|
### fastPut(localPath, remotePath, options) ==> string
|
|
581
585
|
|
|
@@ -619,7 +623,7 @@ Uploads the data in file at `localPath` to a new file on remote server at `remot
|
|
|
619
623
|
```
|
|
620
624
|
|
|
621
625
|
|
|
622
|
-
<a id="
|
|
626
|
+
<a id="org1eaae65"></a>
|
|
623
627
|
|
|
624
628
|
### append(input, remotePath, options) ==> string
|
|
625
629
|
|
|
@@ -663,7 +667,7 @@ Append the `input` data to an existing remote file. There is no integrity checki
|
|
|
663
667
|
```
|
|
664
668
|
|
|
665
669
|
|
|
666
|
-
<a id="
|
|
670
|
+
<a id="org1c3ab90"></a>
|
|
667
671
|
|
|
668
672
|
### mkdir(path, recursive) ==> string
|
|
669
673
|
|
|
@@ -691,7 +695,7 @@ Create a new directory. If the recursive flag is set to true, the method will cr
|
|
|
691
695
|
```
|
|
692
696
|
|
|
693
697
|
|
|
694
|
-
<a id="
|
|
698
|
+
<a id="org003ef11"></a>
|
|
695
699
|
|
|
696
700
|
### rmdir(path, recursive) ==> string
|
|
697
701
|
|
|
@@ -721,7 +725,7 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
|
|
|
721
725
|
```
|
|
722
726
|
|
|
723
727
|
|
|
724
|
-
<a id="
|
|
728
|
+
<a id="orgd3e3a35"></a>
|
|
725
729
|
|
|
726
730
|
### delete(path, noErrorOK) ==> string
|
|
727
731
|
|
|
@@ -750,7 +754,7 @@ Delete a file on the remote server.
|
|
|
750
754
|
```
|
|
751
755
|
|
|
752
756
|
|
|
753
|
-
<a id="
|
|
757
|
+
<a id="org3343888"></a>
|
|
754
758
|
|
|
755
759
|
### rename(fromPath, toPath) ==> string
|
|
756
760
|
|
|
@@ -779,7 +783,7 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
|
|
|
779
783
|
```
|
|
780
784
|
|
|
781
785
|
|
|
782
|
-
<a id="
|
|
786
|
+
<a id="orgb8f840c"></a>
|
|
783
787
|
|
|
784
788
|
### posixRename(fromPath, toPath) ==> string
|
|
785
789
|
|
|
@@ -806,7 +810,7 @@ client.connect(config)
|
|
|
806
810
|
```
|
|
807
811
|
|
|
808
812
|
|
|
809
|
-
<a id="
|
|
813
|
+
<a id="org15551c9"></a>
|
|
810
814
|
|
|
811
815
|
### chmod(path, mode) ==> string
|
|
812
816
|
|
|
@@ -835,7 +839,7 @@ Change the mode (read, write or execute permissions) of a remote file or directo
|
|
|
835
839
|
```
|
|
836
840
|
|
|
837
841
|
|
|
838
|
-
<a id="
|
|
842
|
+
<a id="org4ec95c3"></a>
|
|
839
843
|
|
|
840
844
|
### realPath(path) ===> string
|
|
841
845
|
|
|
@@ -846,26 +850,30 @@ Converts a relative path to an absolute path on the remote server. This method i
|
|
|
846
850
|
- **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
|
|
847
851
|
|
|
848
852
|
|
|
849
|
-
<a id="
|
|
853
|
+
<a id="orgc1265f0"></a>
|
|
850
854
|
|
|
851
855
|
### cwd() ==> string
|
|
852
856
|
|
|
853
857
|
Returns what the server believes is the current remote working directory.
|
|
854
858
|
|
|
855
859
|
|
|
856
|
-
<a id="
|
|
860
|
+
<a id="org5c10c06"></a>
|
|
857
861
|
|
|
858
|
-
### uploadDir(srcDir, dstDir,
|
|
862
|
+
### uploadDir(srcDir, dstDir, options) ==> string
|
|
859
863
|
|
|
860
864
|
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.
|
|
861
865
|
|
|
862
866
|
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.
|
|
863
867
|
|
|
864
|
-
The
|
|
868
|
+
The 3rd argument is an options object with two supported properties, `filter` and `useFastput`.
|
|
869
|
+
|
|
870
|
+
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.
|
|
871
|
+
|
|
872
|
+
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.
|
|
865
873
|
|
|
866
874
|
- **srcDir:** A local file path specified as a string
|
|
867
875
|
- **dstDir:** A remote file path specified as a string
|
|
868
|
-
- **
|
|
876
|
+
- **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.
|
|
869
877
|
|
|
870
878
|
1. Example
|
|
871
879
|
|
|
@@ -918,19 +926,21 @@ The optionsl *filter* argument is a function which will be called for each item
|
|
|
918
926
|
```
|
|
919
927
|
|
|
920
928
|
|
|
921
|
-
<a id="
|
|
929
|
+
<a id="org614dc71"></a>
|
|
922
930
|
|
|
923
|
-
### downloadDir(srcDir, dstDir,
|
|
931
|
+
### downloadDir(srcDir, dstDir, options) ==> string
|
|
924
932
|
|
|
925
933
|
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.
|
|
926
934
|
|
|
927
935
|
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.
|
|
928
936
|
|
|
929
|
-
The
|
|
937
|
+
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.
|
|
938
|
+
|
|
939
|
+
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.
|
|
930
940
|
|
|
931
941
|
- **srcDir:** A remote file path specified as a string
|
|
932
942
|
- **dstDir:** A local file path specified as a string
|
|
933
|
-
- **
|
|
943
|
+
- **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. .
|
|
934
944
|
|
|
935
945
|
1. Example
|
|
936
946
|
|
|
@@ -981,7 +991,49 @@ The optional *filter* argument is a predicate function which will be called with
|
|
|
981
991
|
```
|
|
982
992
|
|
|
983
993
|
|
|
984
|
-
<a id="
|
|
994
|
+
<a id="org906505f"></a>
|
|
995
|
+
|
|
996
|
+
### createReadStream(remotePath, options)) ==> stream object
|
|
997
|
+
|
|
998
|
+
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.
|
|
999
|
+
|
|
1000
|
+
- **remotePath:** A remote file path specified as a string
|
|
1001
|
+
- **options:** An options object. Supported properties are
|
|
1002
|
+
- **flags:** defaults to 'r'
|
|
1003
|
+
- **encoding:** defaults to null
|
|
1004
|
+
- **handle:** defaults to null
|
|
1005
|
+
- **mode:** 0o666
|
|
1006
|
+
- **autoClose:** defaults to true. If set to false, client code is responsible for closing file descriptors when finished
|
|
1007
|
+
- **start:** Default 0. Position to start reading bytes from (inclusive)
|
|
1008
|
+
- **end:** Postion to stop reading bytes (inclusive).
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
<a id="orgf5b3858"></a>
|
|
1012
|
+
|
|
1013
|
+
### createWriteStream(remotePath, options) ==> stream object
|
|
1014
|
+
|
|
1015
|
+
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.
|
|
1016
|
+
|
|
1017
|
+
- **remotePath:** Path to the remote file specified as a string
|
|
1018
|
+
- **options:** An object containing stream options. Supported properties include
|
|
1019
|
+
- **flags:** default 'w'
|
|
1020
|
+
- **encoding:** defulat null
|
|
1021
|
+
- **mode:** 0o666
|
|
1022
|
+
- **autoClose:** true
|
|
1023
|
+
- **start:** Byte position to start writing from (inclusive). May require changing flag to 'r+'.
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
<a id="org3716ce1"></a>
|
|
1027
|
+
|
|
1028
|
+
### rcopy(srcPath, dstPath) ==> string
|
|
1029
|
+
|
|
1030
|
+
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.
|
|
1031
|
+
|
|
1032
|
+
- **srcPath:** Path to remote file to be copied specified as a string
|
|
1033
|
+
- **dstPath:** Path to where the copy will be creaeted specified as a string
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
<a id="org93bfcf3"></a>
|
|
985
1037
|
|
|
986
1038
|
### end() ==> boolean
|
|
987
1039
|
|
|
@@ -1005,7 +1057,7 @@ Ends the current client session, releasing the client socket and associated reso
|
|
|
1005
1057
|
```
|
|
1006
1058
|
|
|
1007
1059
|
|
|
1008
|
-
<a id="
|
|
1060
|
+
<a id="org8fe6c29"></a>
|
|
1009
1061
|
|
|
1010
1062
|
### Add and Remove Listeners
|
|
1011
1063
|
|
|
@@ -1026,12 +1078,12 @@ Although normally not required, you can add and remove custom listeners on the s
|
|
|
1026
1078
|
Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
|
|
1027
1079
|
|
|
1028
1080
|
|
|
1029
|
-
<a id="
|
|
1081
|
+
<a id="org763e7bf"></a>
|
|
1030
1082
|
|
|
1031
1083
|
# Platform Quirks & Warnings
|
|
1032
1084
|
|
|
1033
1085
|
|
|
1034
|
-
<a id="
|
|
1086
|
+
<a id="org3c7ec29"></a>
|
|
1035
1087
|
|
|
1036
1088
|
## Server Capabilities
|
|
1037
1089
|
|
|
@@ -1040,7 +1092,7 @@ All SFTP servers and platforms are not equal. Some facilities provided by `ssh2-
|
|
|
1040
1092
|
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.
|
|
1041
1093
|
|
|
1042
1094
|
|
|
1043
|
-
<a id="
|
|
1095
|
+
<a id="org13e7f16"></a>
|
|
1044
1096
|
|
|
1045
1097
|
## Promises, Events & Managing Exceptions
|
|
1046
1098
|
|
|
@@ -1059,14 +1111,14 @@ The other area where additional events are fired is during the end() call. To de
|
|
|
1059
1111
|
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.
|
|
1060
1112
|
|
|
1061
1113
|
|
|
1062
|
-
<a id="
|
|
1114
|
+
<a id="orgbea2ca0"></a>
|
|
1063
1115
|
|
|
1064
1116
|
### Adding Custom Handlers
|
|
1065
1117
|
|
|
1066
1118
|
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.
|
|
1067
1119
|
|
|
1068
1120
|
|
|
1069
|
-
<a id="
|
|
1121
|
+
<a id="org13d5c2b"></a>
|
|
1070
1122
|
|
|
1071
1123
|
## Windows Based Servers
|
|
1072
1124
|
|
|
@@ -1075,7 +1127,7 @@ It appears that when the sftp server is running on Windows, a *ECONNRESET* error
|
|
|
1075
1127
|
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.
|
|
1076
1128
|
|
|
1077
1129
|
|
|
1078
|
-
<a id="
|
|
1130
|
+
<a id="org56317e1"></a>
|
|
1079
1131
|
|
|
1080
1132
|
## Don't Re-use SftpClient Objects
|
|
1081
1133
|
|
|
@@ -1084,12 +1136,12 @@ Due to an issue with *ECONNRESET* error signals when connecting to Windows based
|
|
|
1084
1136
|
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.
|
|
1085
1137
|
|
|
1086
1138
|
|
|
1087
|
-
<a id="
|
|
1139
|
+
<a id="orgb28c841"></a>
|
|
1088
1140
|
|
|
1089
1141
|
# FAQ
|
|
1090
1142
|
|
|
1091
1143
|
|
|
1092
|
-
<a id="
|
|
1144
|
+
<a id="orgb4c1c9d"></a>
|
|
1093
1145
|
|
|
1094
1146
|
## Remote server drops connections with only an end event
|
|
1095
1147
|
|
|
@@ -1100,7 +1152,7 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
|
|
|
1100
1152
|
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.
|
|
1101
1153
|
|
|
1102
1154
|
|
|
1103
|
-
<a id="
|
|
1155
|
+
<a id="orgf09bf16"></a>
|
|
1104
1156
|
|
|
1105
1157
|
## How can I pass writable stream as dst for get method?
|
|
1106
1158
|
|
|
@@ -1159,7 +1211,7 @@ sftp
|
|
|
1159
1211
|
```
|
|
1160
1212
|
|
|
1161
1213
|
|
|
1162
|
-
<a id="
|
|
1214
|
+
<a id="org10382e7"></a>
|
|
1163
1215
|
|
|
1164
1216
|
## How can I upload files without having to specify a password?
|
|
1165
1217
|
|
|
@@ -1194,7 +1246,7 @@ sftp.connect({
|
|
|
1194
1246
|
```
|
|
1195
1247
|
|
|
1196
1248
|
|
|
1197
|
-
<a id="
|
|
1249
|
+
<a id="org34d3eb7"></a>
|
|
1198
1250
|
|
|
1199
1251
|
## How can I connect through a Socks Proxy
|
|
1200
1252
|
|
|
@@ -1230,7 +1282,7 @@ client.connect({
|
|
|
1230
1282
|
```
|
|
1231
1283
|
|
|
1232
1284
|
|
|
1233
|
-
<a id="
|
|
1285
|
+
<a id="orgbe4be79"></a>
|
|
1234
1286
|
|
|
1235
1287
|
## Timeout while waiting for handshake or handshake errors
|
|
1236
1288
|
|
|
@@ -1239,7 +1291,7 @@ Some users have encountered the error 'Timeout while waiting for handshake' or '
|
|
|
1239
1291
|
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.
|
|
1240
1292
|
|
|
1241
1293
|
|
|
1242
|
-
<a id="
|
|
1294
|
+
<a id="org36a1f06"></a>
|
|
1243
1295
|
|
|
1244
1296
|
## How can I limit upload/download speed
|
|
1245
1297
|
|
|
@@ -1279,7 +1331,7 @@ try {
|
|
|
1279
1331
|
```
|
|
1280
1332
|
|
|
1281
1333
|
|
|
1282
|
-
<a id="
|
|
1334
|
+
<a id="org293ce0b"></a>
|
|
1283
1335
|
|
|
1284
1336
|
## Connection hangs or fails for larger files
|
|
1285
1337
|
|
|
@@ -1290,14 +1342,14 @@ A symptom of this issue is that you are able to upload small files, but uploadin
|
|
|
1290
1342
|
For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
|
|
1291
1343
|
|
|
1292
1344
|
|
|
1293
|
-
<a id="
|
|
1345
|
+
<a id="org897888b"></a>
|
|
1294
1346
|
|
|
1295
1347
|
# Examples
|
|
1296
1348
|
|
|
1297
1349
|
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.
|
|
1298
1350
|
|
|
1299
1351
|
|
|
1300
|
-
<a id="
|
|
1352
|
+
<a id="orgd93090a"></a>
|
|
1301
1353
|
|
|
1302
1354
|
# Troubleshooting
|
|
1303
1355
|
|
|
@@ -1312,14 +1364,14 @@ Note also that in the repository there are two useful directories. The first is
|
|
|
1312
1364
|
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.
|
|
1313
1365
|
|
|
1314
1366
|
|
|
1315
|
-
<a id="
|
|
1367
|
+
<a id="org44294d4"></a>
|
|
1316
1368
|
|
|
1317
1369
|
## Common Errors
|
|
1318
1370
|
|
|
1319
1371
|
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.
|
|
1320
1372
|
|
|
1321
1373
|
|
|
1322
|
-
<a id="
|
|
1374
|
+
<a id="org44783ba"></a>
|
|
1323
1375
|
|
|
1324
1376
|
### Not returning the promise in a `then()` block
|
|
1325
1377
|
|
|
@@ -1356,7 +1408,7 @@ Note the `return` statements. These ensure that the Promise returned by the clie
|
|
|
1356
1408
|
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.
|
|
1357
1409
|
|
|
1358
1410
|
|
|
1359
|
-
<a id="
|
|
1411
|
+
<a id="org1bee008"></a>
|
|
1360
1412
|
|
|
1361
1413
|
### Mixing Promise Chains and Async/Await
|
|
1362
1414
|
|
|
@@ -1416,7 +1468,7 @@ async function doSftp() {
|
|
|
1416
1468
|
```
|
|
1417
1469
|
|
|
1418
1470
|
|
|
1419
|
-
<a id="
|
|
1471
|
+
<a id="org004696d"></a>
|
|
1420
1472
|
|
|
1421
1473
|
### Try/catch and Error Handlers
|
|
1422
1474
|
|
|
@@ -1427,14 +1479,14 @@ The basic problem is that the try/catch block will have completed execution befo
|
|
|
1427
1479
|
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.
|
|
1428
1480
|
|
|
1429
1481
|
|
|
1430
|
-
<a id="
|
|
1482
|
+
<a id="org9131050"></a>
|
|
1431
1483
|
|
|
1432
1484
|
### Server Differences
|
|
1433
1485
|
|
|
1434
1486
|
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.
|
|
1435
1487
|
|
|
1436
1488
|
|
|
1437
|
-
<a id="
|
|
1489
|
+
<a id="org6c9fbb1"></a>
|
|
1438
1490
|
|
|
1439
1491
|
### Avoid Concurrent Operations
|
|
1440
1492
|
|
|
@@ -1443,7 +1495,7 @@ Technically, SFTP should be able to perform multiple operations concurrently. As
|
|
|
1443
1495
|
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.
|
|
1444
1496
|
|
|
1445
1497
|
|
|
1446
|
-
<a id="
|
|
1498
|
+
<a id="org52099c1"></a>
|
|
1447
1499
|
|
|
1448
1500
|
## Debugging Support
|
|
1449
1501
|
|
|
@@ -1476,7 +1528,7 @@ If you just want to see debug messages from `ssh2-sftp-client` and exclude debug
|
|
|
1476
1528
|
```
|
|
1477
1529
|
|
|
1478
1530
|
|
|
1479
|
-
<a id="
|
|
1531
|
+
<a id="orga96ad01"></a>
|
|
1480
1532
|
|
|
1481
1533
|
# Logging Issues
|
|
1482
1534
|
|
|
@@ -1493,7 +1545,7 @@ I am happy to try and help diagnose and fix any issues you encounter while using
|
|
|
1493
1545
|
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.
|
|
1494
1546
|
|
|
1495
1547
|
|
|
1496
|
-
<a id="
|
|
1548
|
+
<a id="org0fa18ba"></a>
|
|
1497
1549
|
|
|
1498
1550
|
# Pull Requests
|
|
1499
1551
|
|
|
@@ -1508,7 +1560,7 @@ This module will adopt a standard semantic versioning policy. Please indicate in
|
|
|
1508
1560
|
- **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
|
|
1509
1561
|
|
|
1510
1562
|
|
|
1511
|
-
<a id="
|
|
1563
|
+
<a id="org05880d2"></a>
|
|
1512
1564
|
|
|
1513
1565
|
# Contributors
|
|
1514
1566
|
|