ssh2-sftp-client 7.2.2 → 8.1.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/README.md +334 -166
- package/README.org +107 -81
- package/package.json +19 -15
- package/src/index.js +232 -139
- package/src/utils.js +28 -37
package/README.md
CHANGED
|
@@ -1,79 +1,101 @@
|
|
|
1
|
-
- [Overview](#
|
|
2
|
-
- [
|
|
3
|
-
- [
|
|
4
|
-
- [
|
|
5
|
-
- [Documentation](#
|
|
6
|
-
- [Specifying Paths](#
|
|
7
|
-
- [Methods](#
|
|
8
|
-
- [new SftpClient(name) ===> SFTP client object](#
|
|
9
|
-
- [connect(config) ===> SFTP object](#
|
|
10
|
-
- [list(path, pattern) ==> Array[object]](#
|
|
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, filter) ==> string](#
|
|
27
|
-
- [downloadDir(srcDir, dstDir, filter) ==> string](#
|
|
28
|
-
- [end() ==> boolean](#
|
|
29
|
-
- [Add and Remove Listeners](#
|
|
30
|
-
- [Platform Quirks & Warnings](#
|
|
31
|
-
- [Server Capabilities](#
|
|
32
|
-
- [Promises, Events & Managing Exceptions](#
|
|
33
|
-
- [Adding Custom Handlers](#
|
|
34
|
-
- [Windows Based Servers](#
|
|
35
|
-
- [Don't Re-use SftpClient Objects](#
|
|
36
|
-
- [FAQ](#
|
|
37
|
-
- [Remote server drops connections with only an end event](#
|
|
38
|
-
- [How can I pass writable stream as dst for get method?](#
|
|
39
|
-
- [How can I upload files without having to specify a password?](#
|
|
40
|
-
- [How can I connect through a Socks Proxy](#
|
|
41
|
-
- [Timeout while waiting for handshake or handshake errors](#
|
|
42
|
-
- [How can I limit upload/download speed](#
|
|
43
|
-
- [Connection hangs or fails for larger files](#
|
|
44
|
-
- [Examples](#
|
|
45
|
-
- [Troubleshooting](#
|
|
46
|
-
- [Common Errors](#
|
|
47
|
-
- [Not returning the promise in a `then()` block](#
|
|
48
|
-
- [Mixing Promise Chains and Async/Await](#
|
|
49
|
-
- [Try/catch and Error Handlers](#
|
|
50
|
-
- [Server Differences](#
|
|
51
|
-
- [Avoid Concurrent Operations](#
|
|
52
|
-
- [Debugging Support](#
|
|
53
|
-
- [Logging Issues](#
|
|
54
|
-
- [Pull Requests](#
|
|
55
|
-
- [Contributors](#
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
1
|
+
- [Overview](#orgbeea605)
|
|
2
|
+
- [Version 8.x.x Changes](#org2589748)
|
|
3
|
+
- [Installation](#orgd7b0a7e)
|
|
4
|
+
- [Basic Usage](#org2e1bee4)
|
|
5
|
+
- [Documentation](#orgfac43d1)
|
|
6
|
+
- [Specifying Paths](#org38509ac)
|
|
7
|
+
- [Methods](#org26ecc4f)
|
|
8
|
+
- [new SftpClient(name) ===> SFTP client object](#orga50cd31)
|
|
9
|
+
- [connect(config) ===> SFTP object](#orgbc0c617)
|
|
10
|
+
- [list(path, pattern) ==> Array[object]](#orgdfab34d)
|
|
11
|
+
- [exists(path) ==> boolean](#orgd09b949)
|
|
12
|
+
- [stat(path) ==> object](#org31bacc1)
|
|
13
|
+
- [get(path, dst, options) ==> String|Stream|Buffer](#org12ed2ca)
|
|
14
|
+
- [fastGet(remotePath, localPath, options) ===> string](#org7b0d9e8)
|
|
15
|
+
- [put(src, remotePath, options) ==> string](#orgd38e78b)
|
|
16
|
+
- [fastPut(localPath, remotePath, options) ==> string](#orge130096)
|
|
17
|
+
- [append(input, remotePath, options) ==> string](#org4f2a1b1)
|
|
18
|
+
- [mkdir(path, recursive) ==> string](#orga9acefe)
|
|
19
|
+
- [rmdir(path, recursive) ==> string](#org9e51da5)
|
|
20
|
+
- [delete(path, noErrorOK) ==> string](#org2054a87)
|
|
21
|
+
- [rename(fromPath, toPath) ==> string](#org7497d9f)
|
|
22
|
+
- [posixRename(fromPath, toPath) ==> string](#orgb179088)
|
|
23
|
+
- [chmod(path, mode) ==> string](#org335a008)
|
|
24
|
+
- [realPath(path) ===> string](#org384f34a)
|
|
25
|
+
- [cwd() ==> string](#org8fa77d4)
|
|
26
|
+
- [uploadDir(srcDir, dstDir, filter) ==> string](#orgb824235)
|
|
27
|
+
- [downloadDir(srcDir, dstDir, filter) ==> string](#org46aa66f)
|
|
28
|
+
- [end() ==> boolean](#orge3fcb81)
|
|
29
|
+
- [Add and Remove Listeners](#org4607268)
|
|
30
|
+
- [Platform Quirks & Warnings](#org66a7061)
|
|
31
|
+
- [Server Capabilities](#org740553b)
|
|
32
|
+
- [Promises, Events & Managing Exceptions](#orgf97d765)
|
|
33
|
+
- [Adding Custom Handlers](#org6acf9c6)
|
|
34
|
+
- [Windows Based Servers](#orgb2f13ee)
|
|
35
|
+
- [Don't Re-use SftpClient Objects](#orgcd82e68)
|
|
36
|
+
- [FAQ](#orge1aa1d7)
|
|
37
|
+
- [Remote server drops connections with only an end event](#org9296404)
|
|
38
|
+
- [How can I pass writable stream as dst for get method?](#org3e48901)
|
|
39
|
+
- [How can I upload files without having to specify a password?](#org1474028)
|
|
40
|
+
- [How can I connect through a Socks Proxy](#orgb9a0ec1)
|
|
41
|
+
- [Timeout while waiting for handshake or handshake errors](#org62e1f3a)
|
|
42
|
+
- [How can I limit upload/download speed](#org7f26ef8)
|
|
43
|
+
- [Connection hangs or fails for larger files](#orgfb52441)
|
|
44
|
+
- [Examples](#orgbe2b99e)
|
|
45
|
+
- [Troubleshooting](#orgd2dcb05)
|
|
46
|
+
- [Common Errors](#orgae04d0a)
|
|
47
|
+
- [Not returning the promise in a `then()` block](#org2018983)
|
|
48
|
+
- [Mixing Promise Chains and Async/Await](#org6a3654b)
|
|
49
|
+
- [Try/catch and Error Handlers](#org36087fd)
|
|
50
|
+
- [Server Differences](#orgd09c9c8)
|
|
51
|
+
- [Avoid Concurrent Operations](#org67e5a24)
|
|
52
|
+
- [Debugging Support](#org00c676b)
|
|
53
|
+
- [Logging Issues](#org6f7f508)
|
|
54
|
+
- [Pull Requests](#org2d2ad0d)
|
|
55
|
+
- [Contributors](#orgba32989)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
<a id="orgbeea605"></a>
|
|
60
|
+
|
|
61
|
+
# Overview
|
|
59
62
|
|
|
60
63
|
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
64
|
|
|
62
65
|
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
66
|
|
|
64
|
-
Current stable release is **
|
|
67
|
+
Current stable release is **v8.1.0**.
|
|
65
68
|
|
|
66
|
-
Code has been tested against Node versions 14.
|
|
69
|
+
Code has been tested against Node versions 14.19.1, 16.15.0 and 18.1.0
|
|
67
70
|
|
|
68
|
-
Node versions <
|
|
71
|
+
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.
|
|
69
72
|
|
|
70
|
-
|
|
73
|
+
|
|
74
|
+
<a id="org2589748"></a>
|
|
75
|
+
|
|
76
|
+
## Version 8.x.x Changes
|
|
77
|
+
|
|
78
|
+
- **Breaking Change**: The API for `uploadDir()` and `downloadDir()` has been changed. These methods now expect a function as the optional 3rd argument. Previously, the 3rd argument was a regular expression used to filter out which files and directories should be included in the upload or download action. The method now expects a predicate function which will return true if the target is to be included in the upload or download and false if it is to be excluded. The predicate function will be called with two arguments, a full path to the target object and a boolean value which is true when the target is a directory, false otherwise. If no filter predicate is supplied, all files and directories under the initial target directory will be transferred. At this time, asynchronous filter functions are not supported.
|
|
79
|
+
|
|
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
|
+
|
|
82
|
+
- v8.1.0: The `list()` method now includes a `longname` property in the descriptor object for each file in the listing. The `longname` value is a string which resembles the `ls -l` listing.
|
|
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>
|
|
88
|
+
|
|
89
|
+
# Installation
|
|
71
90
|
|
|
72
91
|
```shell
|
|
73
92
|
npm install ssh2-sftp-client
|
|
74
93
|
```
|
|
75
94
|
|
|
76
|
-
|
|
95
|
+
|
|
96
|
+
<a id="org2e1bee4"></a>
|
|
97
|
+
|
|
98
|
+
# Basic Usage
|
|
77
99
|
|
|
78
100
|
```javascript
|
|
79
101
|
let Client = require('ssh2-sftp-client');
|
|
@@ -93,23 +115,19 @@ sftp.connect({
|
|
|
93
115
|
});
|
|
94
116
|
```
|
|
95
117
|
|
|
96
|
-
# Version 7.x Changes<a id="sec-4"></a>
|
|
97
|
-
|
|
98
|
-
- This version is based on version 1.x.x of `ssh2`. This version of `ssh2` is a complete re-write of the `ssh2` library. This re-write addresses issues encountered when using node v14 as well as some design weaknesses in the previous 0.8.x version.
|
|
99
|
-
|
|
100
|
-
- **Breaking Change** Expanded option handling for `get()` and `put()` methods. A number of use cases were identified where setting specific options on the read and write streams and the pipe operation are necessary. For example, disabling `autoClose` on streams or the `end` event in pipes. The `options` argument for `get()` and `put()` calls now supports properties for `readStreamOptions`, `writeStreamOptions` and `pipeOptions`. Note that options are only applied to streams created by the `get()` and `put()` methods. Streams passed into these methods are under the control of the client code and therefore cannot have options supplied in arguments to those streams (you would apply such options when you create the streams). Options are typically only necessary in special use cases. Most of the time, no options are required. However, if you are currently using options to either `put()` or `get()`, you will need to update your code to map these options to the new structure.
|
|
101
118
|
|
|
102
|
-
|
|
119
|
+
<a id="orgfac43d1"></a>
|
|
103
120
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
# Documentation<a id="sec-5"></a>
|
|
121
|
+
# Documentation
|
|
107
122
|
|
|
108
123
|
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)
|
|
109
124
|
|
|
110
125
|
All the methods will return a Promise, except for `on()` and `removeListener()`, which are typically only used in special use cases.
|
|
111
126
|
|
|
112
|
-
|
|
127
|
+
|
|
128
|
+
<a id="org38509ac"></a>
|
|
129
|
+
|
|
130
|
+
## Specifying Paths
|
|
113
131
|
|
|
114
132
|
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.
|
|
115
133
|
|
|
@@ -139,9 +157,15 @@ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
|
|
|
139
157
|
|
|
140
158
|
This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
|
|
141
159
|
|
|
142
|
-
## Methods<a id="sec-5-2"></a>
|
|
143
160
|
|
|
144
|
-
|
|
161
|
+
<a id="org26ecc4f"></a>
|
|
162
|
+
|
|
163
|
+
## Methods
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
<a id="orga50cd31"></a>
|
|
167
|
+
|
|
168
|
+
### new SftpClient(name) ===> SFTP client object
|
|
145
169
|
|
|
146
170
|
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.
|
|
147
171
|
|
|
@@ -177,7 +201,10 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
|
|
|
177
201
|
});
|
|
178
202
|
```
|
|
179
203
|
|
|
180
|
-
|
|
204
|
+
|
|
205
|
+
<a id="orgbc0c617"></a>
|
|
206
|
+
|
|
207
|
+
### connect(config) ===> SFTP object
|
|
181
208
|
|
|
182
209
|
Connect to an sftp server. Full documentation for connection options is available [here](https://github.com/mscdex/ssh2#user-content-client-methods)
|
|
183
210
|
|
|
@@ -240,7 +267,10 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
240
267
|
});
|
|
241
268
|
```
|
|
242
269
|
|
|
243
|
-
|
|
270
|
+
|
|
271
|
+
<a id="orgdfab34d"></a>
|
|
272
|
+
|
|
273
|
+
### list(path, pattern) ==> Array[object]
|
|
244
274
|
|
|
245
275
|
Retrieves a directory listing. This method returns a Promise, which once realised, returns an array of objects representing items in the remote directory.
|
|
246
276
|
|
|
@@ -294,6 +324,7 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
294
324
|
},
|
|
295
325
|
owner: // user ID
|
|
296
326
|
group: // group ID
|
|
327
|
+
longname: // like ls -l line
|
|
297
328
|
}
|
|
298
329
|
```
|
|
299
330
|
|
|
@@ -307,7 +338,10 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
307
338
|
|
|
308
339
|
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.
|
|
309
340
|
|
|
310
|
-
|
|
341
|
+
|
|
342
|
+
<a id="orgd09b949"></a>
|
|
343
|
+
|
|
344
|
+
### exists(path) ==> boolean
|
|
311
345
|
|
|
312
346
|
Tests to see if remote file or directory exists. Returns type of remote object if it exists or false if it does not.
|
|
313
347
|
|
|
@@ -340,7 +374,10 @@ Tests to see if remote file or directory exists. Returns type of remote object i
|
|
|
340
374
|
});
|
|
341
375
|
```
|
|
342
376
|
|
|
343
|
-
|
|
377
|
+
|
|
378
|
+
<a id="org31bacc1"></a>
|
|
379
|
+
|
|
380
|
+
### stat(path) ==> object
|
|
344
381
|
|
|
345
382
|
Returns the attributes associated with the object pointed to by `path`.
|
|
346
383
|
|
|
@@ -388,11 +425,14 @@ Returns the attributes associated with the object pointed to by `path`.
|
|
|
388
425
|
});
|
|
389
426
|
```
|
|
390
427
|
|
|
391
|
-
|
|
428
|
+
|
|
429
|
+
<a id="org12ed2ca"></a>
|
|
430
|
+
|
|
431
|
+
### get(path, dst, options) ==> String|Stream|Buffer
|
|
392
432
|
|
|
393
433
|
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.
|
|
394
434
|
|
|
395
|
-
In general, if
|
|
435
|
+
In general, if you're going to pass in a string as the destination, you are better off using the `fastGet()` method.
|
|
396
436
|
|
|
397
437
|
- **path:** String. Path to the remote file to download
|
|
398
438
|
- **dst:** String|Stream. Destination for the data. If a string, it should be a local file path.
|
|
@@ -441,7 +481,10 @@ In general, if your going to pass in a string as the destination, you are better
|
|
|
441
481
|
|
|
442
482
|
- **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'.
|
|
443
483
|
|
|
444
|
-
|
|
484
|
+
|
|
485
|
+
<a id="org7b0d9e8"></a>
|
|
486
|
+
|
|
487
|
+
### fastGet(remotePath, localPath, options) ===> string
|
|
445
488
|
|
|
446
489
|
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.
|
|
447
490
|
|
|
@@ -481,7 +524,10 @@ Downloads a file at remotePath to localPath using parallel reads for faster thro
|
|
|
481
524
|
});
|
|
482
525
|
```
|
|
483
526
|
|
|
484
|
-
|
|
527
|
+
|
|
528
|
+
<a id="orgd38e78b"></a>
|
|
529
|
+
|
|
530
|
+
### put(src, remotePath, options) ==> string
|
|
485
531
|
|
|
486
532
|
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.
|
|
487
533
|
|
|
@@ -528,7 +574,10 @@ Upload data from local system to remote server. If the `src` argument is a strin
|
|
|
528
574
|
|
|
529
575
|
- **Tip:** If the src argument is a path string, consider just using `fastPut()`.
|
|
530
576
|
|
|
531
|
-
|
|
577
|
+
|
|
578
|
+
<a id="orge130096"></a>
|
|
579
|
+
|
|
580
|
+
### fastPut(localPath, remotePath, options) ==> string
|
|
532
581
|
|
|
533
582
|
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.
|
|
534
583
|
|
|
@@ -569,7 +618,10 @@ Uploads the data in file at `localPath` to a new file on remote server at `remot
|
|
|
569
618
|
});
|
|
570
619
|
```
|
|
571
620
|
|
|
572
|
-
|
|
621
|
+
|
|
622
|
+
<a id="org4f2a1b1"></a>
|
|
623
|
+
|
|
624
|
+
### append(input, remotePath, options) ==> string
|
|
573
625
|
|
|
574
626
|
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.
|
|
575
627
|
|
|
@@ -610,7 +662,10 @@ Append the `input` data to an existing remote file. There is no integrity checki
|
|
|
610
662
|
});
|
|
611
663
|
```
|
|
612
664
|
|
|
613
|
-
|
|
665
|
+
|
|
666
|
+
<a id="orga9acefe"></a>
|
|
667
|
+
|
|
668
|
+
### mkdir(path, recursive) ==> string
|
|
614
669
|
|
|
615
670
|
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.
|
|
616
671
|
|
|
@@ -635,7 +690,10 @@ Create a new directory. If the recursive flag is set to true, the method will cr
|
|
|
635
690
|
});
|
|
636
691
|
```
|
|
637
692
|
|
|
638
|
-
|
|
693
|
+
|
|
694
|
+
<a id="org9e51da5"></a>
|
|
695
|
+
|
|
696
|
+
### rmdir(path, recursive) ==> string
|
|
639
697
|
|
|
640
698
|
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.
|
|
641
699
|
|
|
@@ -662,7 +720,10 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
|
|
|
662
720
|
});
|
|
663
721
|
```
|
|
664
722
|
|
|
665
|
-
|
|
723
|
+
|
|
724
|
+
<a id="org2054a87"></a>
|
|
725
|
+
|
|
726
|
+
### delete(path, noErrorOK) ==> string
|
|
666
727
|
|
|
667
728
|
Delete a file on the remote server.
|
|
668
729
|
|
|
@@ -688,7 +749,10 @@ Delete a file on the remote server.
|
|
|
688
749
|
});
|
|
689
750
|
```
|
|
690
751
|
|
|
691
|
-
|
|
752
|
+
|
|
753
|
+
<a id="org7497d9f"></a>
|
|
754
|
+
|
|
755
|
+
### rename(fromPath, toPath) ==> string
|
|
692
756
|
|
|
693
757
|
Rename a file or directory from `fromPath` to `toPath`. You must have the necessary permissions to modify the remote file.
|
|
694
758
|
|
|
@@ -714,7 +778,10 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
|
|
|
714
778
|
});
|
|
715
779
|
```
|
|
716
780
|
|
|
717
|
-
|
|
781
|
+
|
|
782
|
+
<a id="orgb179088"></a>
|
|
783
|
+
|
|
784
|
+
### posixRename(fromPath, toPath) ==> string
|
|
718
785
|
|
|
719
786
|
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.
|
|
720
787
|
|
|
@@ -738,7 +805,10 @@ client.connect(config)
|
|
|
738
805
|
});
|
|
739
806
|
```
|
|
740
807
|
|
|
741
|
-
|
|
808
|
+
|
|
809
|
+
<a id="org335a008"></a>
|
|
810
|
+
|
|
811
|
+
### chmod(path, mode) ==> string
|
|
742
812
|
|
|
743
813
|
Change the mode (read, write or execute permissions) of a remote file or directory.
|
|
744
814
|
|
|
@@ -764,7 +834,10 @@ Change the mode (read, write or execute permissions) of a remote file or directo
|
|
|
764
834
|
});
|
|
765
835
|
```
|
|
766
836
|
|
|
767
|
-
|
|
837
|
+
|
|
838
|
+
<a id="org384f34a"></a>
|
|
839
|
+
|
|
840
|
+
### realPath(path) ===> string
|
|
768
841
|
|
|
769
842
|
Converts a relative path to an absolute path on the remote server. This method is mainly used internally to resolve remote path names.
|
|
770
843
|
|
|
@@ -772,81 +845,92 @@ Converts a relative path to an absolute path on the remote server. This method i
|
|
|
772
845
|
|
|
773
846
|
- **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
|
|
774
847
|
|
|
775
|
-
|
|
848
|
+
|
|
849
|
+
<a id="org8fa77d4"></a>
|
|
850
|
+
|
|
851
|
+
### cwd() ==> string
|
|
776
852
|
|
|
777
853
|
Returns what the server believes is the current remote working directory.
|
|
778
854
|
|
|
779
|
-
|
|
855
|
+
|
|
856
|
+
<a id="orgb824235"></a>
|
|
857
|
+
|
|
858
|
+
### uploadDir(srcDir, dstDir, filter) ==> string
|
|
780
859
|
|
|
781
860
|
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.
|
|
782
861
|
|
|
783
|
-
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
|
|
862
|
+
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.
|
|
784
863
|
|
|
785
|
-
The optionsl *filter* argument is a
|
|
864
|
+
The optionsl *filter* argument 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.
|
|
786
865
|
|
|
787
866
|
- **srcDir:** A local file path specified as a string
|
|
788
867
|
- **dstDir:** A remote file path specified as a string
|
|
789
|
-
- **filter:** A
|
|
868
|
+
- **filter:** 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.
|
|
790
869
|
|
|
791
870
|
1. Example
|
|
792
871
|
|
|
793
872
|
```javascript
|
|
794
|
-
|
|
873
|
+
'use strict';
|
|
795
874
|
|
|
796
|
-
|
|
797
|
-
|
|
875
|
+
// Example of using the uploadDir() method to upload a directory
|
|
876
|
+
// to a remote SFTP server
|
|
798
877
|
|
|
799
|
-
|
|
800
|
-
|
|
878
|
+
const path = require('path');
|
|
879
|
+
const SftpClient = require('../src/index');
|
|
801
880
|
|
|
802
|
-
|
|
803
|
-
|
|
881
|
+
const dotenvPath = path.join(__dirname, '..', '.env');
|
|
882
|
+
require('dotenv').config({path: dotenvPath});
|
|
804
883
|
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
884
|
+
const config = {
|
|
885
|
+
host: process.env.SFTP_SERVER,
|
|
886
|
+
username: process.env.SFTP_USER,
|
|
887
|
+
password: process.env.SFTP_PASSWORD,
|
|
888
|
+
port: process.env.SFTP_PORT || 22
|
|
889
|
+
};
|
|
811
890
|
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
891
|
+
async function main() {
|
|
892
|
+
const client = new SftpClient('upload-test');
|
|
893
|
+
const src = path.join(__dirname, '..', 'test', 'testData', 'upload-src');
|
|
894
|
+
const dst = '/home/tim/upload-test';
|
|
816
895
|
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
896
|
+
try {
|
|
897
|
+
await client.connect(config);
|
|
898
|
+
client.on('upload', info => {
|
|
899
|
+
console.log(`Listener: Uploaded ${info.source}`);
|
|
900
|
+
});
|
|
901
|
+
let rslt = await client.uploadDir(src, dst);
|
|
902
|
+
return rslt;
|
|
903
|
+
} catch (err) {
|
|
904
|
+
console.error(err);
|
|
905
|
+
} finally {
|
|
906
|
+
client.end();
|
|
907
|
+
}
|
|
826
908
|
}
|
|
827
|
-
}
|
|
828
909
|
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
910
|
+
main()
|
|
911
|
+
.then(msg => {
|
|
912
|
+
console.log(msg);
|
|
913
|
+
})
|
|
914
|
+
.catch(err => {
|
|
915
|
+
console.log(`main error: ${err.message}`);
|
|
916
|
+
});
|
|
836
917
|
|
|
837
918
|
```
|
|
838
919
|
|
|
839
|
-
|
|
920
|
+
|
|
921
|
+
<a id="org46aa66f"></a>
|
|
922
|
+
|
|
923
|
+
### downloadDir(srcDir, dstDir, filter) ==> string
|
|
840
924
|
|
|
841
925
|
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.
|
|
842
926
|
|
|
843
927
|
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.
|
|
844
928
|
|
|
845
|
-
The optional *filter* argument is a
|
|
929
|
+
The optional *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.
|
|
846
930
|
|
|
847
931
|
- **srcDir:** A remote file path specified as a string
|
|
848
932
|
- **dstDir:** A local file path specified as a string
|
|
849
|
-
- **filter:** A
|
|
933
|
+
- **filter:** A predicate function called with 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.
|
|
850
934
|
|
|
851
935
|
1. Example
|
|
852
936
|
|
|
@@ -896,7 +980,10 @@ The optional *filter* argument is a regular expression which can be used to sele
|
|
|
896
980
|
|
|
897
981
|
```
|
|
898
982
|
|
|
899
|
-
|
|
983
|
+
|
|
984
|
+
<a id="orge3fcb81"></a>
|
|
985
|
+
|
|
986
|
+
### end() ==> boolean
|
|
900
987
|
|
|
901
988
|
Ends the current client session, releasing the client socket and associated resources. This function also removes all listeners associated with the client.
|
|
902
989
|
|
|
@@ -917,7 +1004,10 @@ Ends the current client session, releasing the client socket and associated reso
|
|
|
917
1004
|
});
|
|
918
1005
|
```
|
|
919
1006
|
|
|
920
|
-
|
|
1007
|
+
|
|
1008
|
+
<a id="org4607268"></a>
|
|
1009
|
+
|
|
1010
|
+
### Add and Remove Listeners
|
|
921
1011
|
|
|
922
1012
|
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
|
|
923
1013
|
|
|
@@ -935,15 +1025,24 @@ Although normally not required, you can add and remove custom listeners on the s
|
|
|
935
1025
|
|
|
936
1026
|
Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
|
|
937
1027
|
|
|
938
|
-
# Platform Quirks & Warnings<a id="sec-6"></a>
|
|
939
1028
|
|
|
940
|
-
|
|
1029
|
+
<a id="org66a7061"></a>
|
|
1030
|
+
|
|
1031
|
+
# Platform Quirks & Warnings
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
<a id="org740553b"></a>
|
|
1035
|
+
|
|
1036
|
+
## Server Capabilities
|
|
941
1037
|
|
|
942
1038
|
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.
|
|
943
1039
|
|
|
944
1040
|
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.
|
|
945
1041
|
|
|
946
|
-
|
|
1042
|
+
|
|
1043
|
+
<a id="orgf97d765"></a>
|
|
1044
|
+
|
|
1045
|
+
## Promises, Events & Managing Exceptions
|
|
947
1046
|
|
|
948
1047
|
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.
|
|
949
1048
|
|
|
@@ -959,25 +1058,40 @@ The other area where additional events are fired is during the end() call. To de
|
|
|
959
1058
|
|
|
960
1059
|
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.
|
|
961
1060
|
|
|
962
|
-
|
|
1061
|
+
|
|
1062
|
+
<a id="org6acf9c6"></a>
|
|
1063
|
+
|
|
1064
|
+
### Adding Custom Handlers
|
|
963
1065
|
|
|
964
1066
|
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.
|
|
965
1067
|
|
|
966
|
-
|
|
1068
|
+
|
|
1069
|
+
<a id="orgb2f13ee"></a>
|
|
1070
|
+
|
|
1071
|
+
## Windows Based Servers
|
|
967
1072
|
|
|
968
1073
|
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.
|
|
969
1074
|
|
|
970
1075
|
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.
|
|
971
1076
|
|
|
972
|
-
|
|
1077
|
+
|
|
1078
|
+
<a id="orgcd82e68"></a>
|
|
1079
|
+
|
|
1080
|
+
## Don't Re-use SftpClient Objects
|
|
973
1081
|
|
|
974
1082
|
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.
|
|
975
1083
|
|
|
976
1084
|
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.
|
|
977
1085
|
|
|
978
|
-
# FAQ<a id="sec-7"></a>
|
|
979
1086
|
|
|
980
|
-
|
|
1087
|
+
<a id="orge1aa1d7"></a>
|
|
1088
|
+
|
|
1089
|
+
# FAQ
|
|
1090
|
+
|
|
1091
|
+
|
|
1092
|
+
<a id="org9296404"></a>
|
|
1093
|
+
|
|
1094
|
+
## Remote server drops connections with only an end event
|
|
981
1095
|
|
|
982
1096
|
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.
|
|
983
1097
|
|
|
@@ -985,7 +1099,10 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
|
|
|
985
1099
|
|
|
986
1100
|
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.
|
|
987
1101
|
|
|
988
|
-
|
|
1102
|
+
|
|
1103
|
+
<a id="org3e48901"></a>
|
|
1104
|
+
|
|
1105
|
+
## How can I pass writable stream as dst for get method?
|
|
989
1106
|
|
|
990
1107
|
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()`.
|
|
991
1108
|
|
|
@@ -1041,7 +1158,10 @@ sftp
|
|
|
1041
1158
|
});
|
|
1042
1159
|
```
|
|
1043
1160
|
|
|
1044
|
-
|
|
1161
|
+
|
|
1162
|
+
<a id="org1474028"></a>
|
|
1163
|
+
|
|
1164
|
+
## How can I upload files without having to specify a password?
|
|
1045
1165
|
|
|
1046
1166
|
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.
|
|
1047
1167
|
|
|
@@ -1073,7 +1193,10 @@ sftp.connect({
|
|
|
1073
1193
|
}
|
|
1074
1194
|
```
|
|
1075
1195
|
|
|
1076
|
-
|
|
1196
|
+
|
|
1197
|
+
<a id="orgb9a0ec1"></a>
|
|
1198
|
+
|
|
1199
|
+
## How can I connect through a Socks Proxy
|
|
1077
1200
|
|
|
1078
1201
|
This solution was provided by @jmorino.
|
|
1079
1202
|
|
|
@@ -1085,7 +1208,7 @@ const host = 'my-sftp-server.net';
|
|
|
1085
1208
|
const port = 22; // default SSH/SFTP port on remote server
|
|
1086
1209
|
|
|
1087
1210
|
// connect to SOCKS 5 proxy
|
|
1088
|
-
const {
|
|
1211
|
+
const { socks } = await SocksClient.createConnection({
|
|
1089
1212
|
proxy: {
|
|
1090
1213
|
host: 'my.proxy', // proxy hostname
|
|
1091
1214
|
port: 1080, // proxy port
|
|
@@ -1098,7 +1221,7 @@ const { socket } = await SocksClient.createConnection({
|
|
|
1098
1221
|
const client = new SFTPClient();
|
|
1099
1222
|
client.connect({
|
|
1100
1223
|
host,
|
|
1101
|
-
sock: socket, // pass the socket to proxy here (see ssh2 doc)
|
|
1224
|
+
sock: socks.socket, // pass the socket to proxy here (see ssh2 doc)
|
|
1102
1225
|
username: '.....',
|
|
1103
1226
|
privateKey: '.....'
|
|
1104
1227
|
})
|
|
@@ -1106,13 +1229,19 @@ client.connect({
|
|
|
1106
1229
|
// client is connected
|
|
1107
1230
|
```
|
|
1108
1231
|
|
|
1109
|
-
|
|
1232
|
+
|
|
1233
|
+
<a id="org62e1f3a"></a>
|
|
1234
|
+
|
|
1235
|
+
## Timeout while waiting for handshake or handshake errors
|
|
1110
1236
|
|
|
1111
1237
|
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.
|
|
1112
1238
|
|
|
1113
1239
|
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.
|
|
1114
1240
|
|
|
1115
|
-
|
|
1241
|
+
|
|
1242
|
+
<a id="org7f26ef8"></a>
|
|
1243
|
+
|
|
1244
|
+
## How can I limit upload/download speed
|
|
1116
1245
|
|
|
1117
1246
|
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.
|
|
1118
1247
|
|
|
@@ -1149,7 +1278,10 @@ try {
|
|
|
1149
1278
|
}
|
|
1150
1279
|
```
|
|
1151
1280
|
|
|
1152
|
-
|
|
1281
|
+
|
|
1282
|
+
<a id="orgfb52441"></a>
|
|
1283
|
+
|
|
1284
|
+
## Connection hangs or fails for larger files
|
|
1153
1285
|
|
|
1154
1286
|
This was contributed by Ladislav Jacho. Thanks.
|
|
1155
1287
|
|
|
@@ -1157,11 +1289,17 @@ A symptom of this issue is that you are able to upload small files, but uploadin
|
|
|
1157
1289
|
|
|
1158
1290
|
For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
|
|
1159
1291
|
|
|
1160
|
-
|
|
1292
|
+
|
|
1293
|
+
<a id="orgbe2b99e"></a>
|
|
1294
|
+
|
|
1295
|
+
# Examples
|
|
1161
1296
|
|
|
1162
1297
|
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.
|
|
1163
1298
|
|
|
1164
|
-
|
|
1299
|
+
|
|
1300
|
+
<a id="orgd2dcb05"></a>
|
|
1301
|
+
|
|
1302
|
+
# Troubleshooting
|
|
1165
1303
|
|
|
1166
1304
|
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.
|
|
1167
1305
|
|
|
@@ -1173,11 +1311,17 @@ Note also that in the repository there are two useful directories. The first is
|
|
|
1173
1311
|
|
|
1174
1312
|
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.
|
|
1175
1313
|
|
|
1176
|
-
|
|
1314
|
+
|
|
1315
|
+
<a id="orgae04d0a"></a>
|
|
1316
|
+
|
|
1317
|
+
## Common Errors
|
|
1177
1318
|
|
|
1178
1319
|
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.
|
|
1179
1320
|
|
|
1180
|
-
|
|
1321
|
+
|
|
1322
|
+
<a id="org2018983"></a>
|
|
1323
|
+
|
|
1324
|
+
### Not returning the promise in a `then()` block
|
|
1181
1325
|
|
|
1182
1326
|
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
|
|
1183
1327
|
|
|
@@ -1211,7 +1355,10 @@ Note the `return` statements. These ensure that the Promise returned by the clie
|
|
|
1211
1355
|
|
|
1212
1356
|
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.
|
|
1213
1357
|
|
|
1214
|
-
|
|
1358
|
+
|
|
1359
|
+
<a id="org6a3654b"></a>
|
|
1360
|
+
|
|
1361
|
+
### Mixing Promise Chains and Async/Await
|
|
1215
1362
|
|
|
1216
1363
|
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!)
|
|
1217
1364
|
|
|
@@ -1268,7 +1415,10 @@ async function doSftp() {
|
|
|
1268
1415
|
}
|
|
1269
1416
|
```
|
|
1270
1417
|
|
|
1271
|
-
|
|
1418
|
+
|
|
1419
|
+
<a id="org36087fd"></a>
|
|
1420
|
+
|
|
1421
|
+
### Try/catch and Error Handlers
|
|
1272
1422
|
|
|
1273
1423
|
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.
|
|
1274
1424
|
|
|
@@ -1276,17 +1426,26 @@ The basic problem is that the try/catch block will have completed execution befo
|
|
|
1276
1426
|
|
|
1277
1427
|
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.
|
|
1278
1428
|
|
|
1279
|
-
|
|
1429
|
+
|
|
1430
|
+
<a id="orgd09c9c8"></a>
|
|
1431
|
+
|
|
1432
|
+
### Server Differences
|
|
1280
1433
|
|
|
1281
1434
|
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.
|
|
1282
1435
|
|
|
1283
|
-
|
|
1436
|
+
|
|
1437
|
+
<a id="org67e5a24"></a>
|
|
1438
|
+
|
|
1439
|
+
### Avoid Concurrent Operations
|
|
1284
1440
|
|
|
1285
1441
|
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.
|
|
1286
1442
|
|
|
1287
1443
|
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.
|
|
1288
1444
|
|
|
1289
|
-
|
|
1445
|
+
|
|
1446
|
+
<a id="org00c676b"></a>
|
|
1447
|
+
|
|
1448
|
+
## Debugging Support
|
|
1290
1449
|
|
|
1291
1450
|
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;
|
|
1292
1451
|
|
|
@@ -1316,7 +1475,10 @@ If you just want to see debug messages from `ssh2-sftp-client` and exclude debug
|
|
|
1316
1475
|
}
|
|
1317
1476
|
```
|
|
1318
1477
|
|
|
1319
|
-
|
|
1478
|
+
|
|
1479
|
+
<a id="org6f7f508"></a>
|
|
1480
|
+
|
|
1481
|
+
# Logging Issues
|
|
1320
1482
|
|
|
1321
1483
|
Please log an issue for all bugs, questions, feature and enhancement requests. Please ensure you include the module version, node version and platform.
|
|
1322
1484
|
|
|
@@ -1330,9 +1492,12 @@ I am happy to try and help diagnose and fix any issues you encounter while using
|
|
|
1330
1492
|
|
|
1331
1493
|
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.
|
|
1332
1494
|
|
|
1333
|
-
# Pull Requests<a id="sec-11"></a>
|
|
1334
1495
|
|
|
1335
|
-
|
|
1496
|
+
<a id="org2d2ad0d"></a>
|
|
1497
|
+
|
|
1498
|
+
# Pull Requests
|
|
1499
|
+
|
|
1500
|
+
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.
|
|
1336
1501
|
|
|
1337
1502
|
**Note**: The `README.md` file is generated from the `README.org` file. Therefore, any documentation updates or fixes need to be made to the `README.org` file. This file is *tangled* using `Emacs` org mode. If you don't use Emacs or org-mode, don't be too concerned. The org-mode syntax is straight-forward and similar to *markdown*. I will verify any updates to `README.org` and generate a new `README.md` when necessary. The main point to note is that any changes made directly to `README.md` will not persist and will be lost when a new version is generated, so don't modify that file.
|
|
1338
1503
|
|
|
@@ -1342,7 +1507,10 @@ This module will adopt a standard semantic versioning policy. Please indicate in
|
|
|
1342
1507
|
- **Minor:** Minor change, enhancement or new feature which does not change existing API and will not break existing client code.
|
|
1343
1508
|
- **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
|
|
1344
1509
|
|
|
1345
|
-
|
|
1510
|
+
|
|
1511
|
+
<a id="orgba32989"></a>
|
|
1512
|
+
|
|
1513
|
+
# Contributors
|
|
1346
1514
|
|
|
1347
1515
|
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.
|
|
1348
1516
|
|
|
@@ -1361,4 +1529,4 @@ Thanks to the following for their contributions -
|
|
|
1361
1529
|
- **Emma Milner:** Contributed fix for put() bug
|
|
1362
1530
|
- **Witni Davis:** Contributed PR to fix put() RCE when using 'finish' rather than 'close' to resolve promise
|
|
1363
1531
|
- **Maik Marschner:** Contributed fix for connect() not returning sftp object. Also included test to check for this regression in future.
|
|
1364
|
-
- **cakemasher:** Contributed fix for removeTempListeners().
|
|
1532
|
+
- **cakemasher:** Contributed fix for removeTempListeners().
|