ssh2-sftp-client 8.0.0 → 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 +287 -117
- package/README.org +14 -4
- package/package.json +7 -3
- package/src/index.js +57 -42
package/README.md
CHANGED
|
@@ -1,85 +1,101 @@
|
|
|
1
|
-
- [Overview](#
|
|
2
|
-
- [Version 8.x.x Changes](#
|
|
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, 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 **v8.
|
|
67
|
+
Current stable release is **v8.1.0**.
|
|
65
68
|
|
|
66
|
-
Code has been tested against Node versions 14.19.1, 16.
|
|
69
|
+
Code has been tested against Node versions 14.19.1, 16.15.0 and 18.1.0
|
|
67
70
|
|
|
68
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
|
|
71
77
|
|
|
72
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.
|
|
73
79
|
|
|
74
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).
|
|
75
81
|
|
|
76
|
-
|
|
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
|
|
77
90
|
|
|
78
91
|
```shell
|
|
79
92
|
npm install ssh2-sftp-client
|
|
80
93
|
```
|
|
81
94
|
|
|
82
|
-
|
|
95
|
+
|
|
96
|
+
<a id="org2e1bee4"></a>
|
|
97
|
+
|
|
98
|
+
# Basic Usage
|
|
83
99
|
|
|
84
100
|
```javascript
|
|
85
101
|
let Client = require('ssh2-sftp-client');
|
|
@@ -99,13 +115,19 @@ sftp.connect({
|
|
|
99
115
|
});
|
|
100
116
|
```
|
|
101
117
|
|
|
102
|
-
|
|
118
|
+
|
|
119
|
+
<a id="orgfac43d1"></a>
|
|
120
|
+
|
|
121
|
+
# Documentation
|
|
103
122
|
|
|
104
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)
|
|
105
124
|
|
|
106
125
|
All the methods will return a Promise, except for `on()` and `removeListener()`, which are typically only used in special use cases.
|
|
107
126
|
|
|
108
|
-
|
|
127
|
+
|
|
128
|
+
<a id="org38509ac"></a>
|
|
129
|
+
|
|
130
|
+
## Specifying Paths
|
|
109
131
|
|
|
110
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.
|
|
111
133
|
|
|
@@ -135,9 +157,15 @@ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
|
|
|
135
157
|
|
|
136
158
|
This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
|
|
137
159
|
|
|
138
|
-
## Methods<a id="sec-4-2"></a>
|
|
139
160
|
|
|
140
|
-
|
|
161
|
+
<a id="org26ecc4f"></a>
|
|
162
|
+
|
|
163
|
+
## Methods
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
<a id="orga50cd31"></a>
|
|
167
|
+
|
|
168
|
+
### new SftpClient(name) ===> SFTP client object
|
|
141
169
|
|
|
142
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.
|
|
143
171
|
|
|
@@ -173,7 +201,10 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
|
|
|
173
201
|
});
|
|
174
202
|
```
|
|
175
203
|
|
|
176
|
-
|
|
204
|
+
|
|
205
|
+
<a id="orgbc0c617"></a>
|
|
206
|
+
|
|
207
|
+
### connect(config) ===> SFTP object
|
|
177
208
|
|
|
178
209
|
Connect to an sftp server. Full documentation for connection options is available [here](https://github.com/mscdex/ssh2#user-content-client-methods)
|
|
179
210
|
|
|
@@ -236,7 +267,10 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
236
267
|
});
|
|
237
268
|
```
|
|
238
269
|
|
|
239
|
-
|
|
270
|
+
|
|
271
|
+
<a id="orgdfab34d"></a>
|
|
272
|
+
|
|
273
|
+
### list(path, pattern) ==> Array[object]
|
|
240
274
|
|
|
241
275
|
Retrieves a directory listing. This method returns a Promise, which once realised, returns an array of objects representing items in the remote directory.
|
|
242
276
|
|
|
@@ -290,6 +324,7 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
290
324
|
},
|
|
291
325
|
owner: // user ID
|
|
292
326
|
group: // group ID
|
|
327
|
+
longname: // like ls -l line
|
|
293
328
|
}
|
|
294
329
|
```
|
|
295
330
|
|
|
@@ -303,7 +338,10 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
303
338
|
|
|
304
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.
|
|
305
340
|
|
|
306
|
-
|
|
341
|
+
|
|
342
|
+
<a id="orgd09b949"></a>
|
|
343
|
+
|
|
344
|
+
### exists(path) ==> boolean
|
|
307
345
|
|
|
308
346
|
Tests to see if remote file or directory exists. Returns type of remote object if it exists or false if it does not.
|
|
309
347
|
|
|
@@ -336,7 +374,10 @@ Tests to see if remote file or directory exists. Returns type of remote object i
|
|
|
336
374
|
});
|
|
337
375
|
```
|
|
338
376
|
|
|
339
|
-
|
|
377
|
+
|
|
378
|
+
<a id="org31bacc1"></a>
|
|
379
|
+
|
|
380
|
+
### stat(path) ==> object
|
|
340
381
|
|
|
341
382
|
Returns the attributes associated with the object pointed to by `path`.
|
|
342
383
|
|
|
@@ -384,7 +425,10 @@ Returns the attributes associated with the object pointed to by `path`.
|
|
|
384
425
|
});
|
|
385
426
|
```
|
|
386
427
|
|
|
387
|
-
|
|
428
|
+
|
|
429
|
+
<a id="org12ed2ca"></a>
|
|
430
|
+
|
|
431
|
+
### get(path, dst, options) ==> String|Stream|Buffer
|
|
388
432
|
|
|
389
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.
|
|
390
434
|
|
|
@@ -437,7 +481,10 @@ In general, if you're going to pass in a string as the destination, you are bett
|
|
|
437
481
|
|
|
438
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'.
|
|
439
483
|
|
|
440
|
-
|
|
484
|
+
|
|
485
|
+
<a id="org7b0d9e8"></a>
|
|
486
|
+
|
|
487
|
+
### fastGet(remotePath, localPath, options) ===> string
|
|
441
488
|
|
|
442
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.
|
|
443
490
|
|
|
@@ -477,7 +524,10 @@ Downloads a file at remotePath to localPath using parallel reads for faster thro
|
|
|
477
524
|
});
|
|
478
525
|
```
|
|
479
526
|
|
|
480
|
-
|
|
527
|
+
|
|
528
|
+
<a id="orgd38e78b"></a>
|
|
529
|
+
|
|
530
|
+
### put(src, remotePath, options) ==> string
|
|
481
531
|
|
|
482
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.
|
|
483
533
|
|
|
@@ -524,7 +574,10 @@ Upload data from local system to remote server. If the `src` argument is a strin
|
|
|
524
574
|
|
|
525
575
|
- **Tip:** If the src argument is a path string, consider just using `fastPut()`.
|
|
526
576
|
|
|
527
|
-
|
|
577
|
+
|
|
578
|
+
<a id="orge130096"></a>
|
|
579
|
+
|
|
580
|
+
### fastPut(localPath, remotePath, options) ==> string
|
|
528
581
|
|
|
529
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.
|
|
530
583
|
|
|
@@ -565,7 +618,10 @@ Uploads the data in file at `localPath` to a new file on remote server at `remot
|
|
|
565
618
|
});
|
|
566
619
|
```
|
|
567
620
|
|
|
568
|
-
|
|
621
|
+
|
|
622
|
+
<a id="org4f2a1b1"></a>
|
|
623
|
+
|
|
624
|
+
### append(input, remotePath, options) ==> string
|
|
569
625
|
|
|
570
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.
|
|
571
627
|
|
|
@@ -606,7 +662,10 @@ Append the `input` data to an existing remote file. There is no integrity checki
|
|
|
606
662
|
});
|
|
607
663
|
```
|
|
608
664
|
|
|
609
|
-
|
|
665
|
+
|
|
666
|
+
<a id="orga9acefe"></a>
|
|
667
|
+
|
|
668
|
+
### mkdir(path, recursive) ==> string
|
|
610
669
|
|
|
611
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.
|
|
612
671
|
|
|
@@ -631,7 +690,10 @@ Create a new directory. If the recursive flag is set to true, the method will cr
|
|
|
631
690
|
});
|
|
632
691
|
```
|
|
633
692
|
|
|
634
|
-
|
|
693
|
+
|
|
694
|
+
<a id="org9e51da5"></a>
|
|
695
|
+
|
|
696
|
+
### rmdir(path, recursive) ==> string
|
|
635
697
|
|
|
636
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.
|
|
637
699
|
|
|
@@ -658,7 +720,10 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
|
|
|
658
720
|
});
|
|
659
721
|
```
|
|
660
722
|
|
|
661
|
-
|
|
723
|
+
|
|
724
|
+
<a id="org2054a87"></a>
|
|
725
|
+
|
|
726
|
+
### delete(path, noErrorOK) ==> string
|
|
662
727
|
|
|
663
728
|
Delete a file on the remote server.
|
|
664
729
|
|
|
@@ -684,7 +749,10 @@ Delete a file on the remote server.
|
|
|
684
749
|
});
|
|
685
750
|
```
|
|
686
751
|
|
|
687
|
-
|
|
752
|
+
|
|
753
|
+
<a id="org7497d9f"></a>
|
|
754
|
+
|
|
755
|
+
### rename(fromPath, toPath) ==> string
|
|
688
756
|
|
|
689
757
|
Rename a file or directory from `fromPath` to `toPath`. You must have the necessary permissions to modify the remote file.
|
|
690
758
|
|
|
@@ -710,7 +778,10 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
|
|
|
710
778
|
});
|
|
711
779
|
```
|
|
712
780
|
|
|
713
|
-
|
|
781
|
+
|
|
782
|
+
<a id="orgb179088"></a>
|
|
783
|
+
|
|
784
|
+
### posixRename(fromPath, toPath) ==> string
|
|
714
785
|
|
|
715
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.
|
|
716
787
|
|
|
@@ -734,7 +805,10 @@ client.connect(config)
|
|
|
734
805
|
});
|
|
735
806
|
```
|
|
736
807
|
|
|
737
|
-
|
|
808
|
+
|
|
809
|
+
<a id="org335a008"></a>
|
|
810
|
+
|
|
811
|
+
### chmod(path, mode) ==> string
|
|
738
812
|
|
|
739
813
|
Change the mode (read, write or execute permissions) of a remote file or directory.
|
|
740
814
|
|
|
@@ -760,7 +834,10 @@ Change the mode (read, write or execute permissions) of a remote file or directo
|
|
|
760
834
|
});
|
|
761
835
|
```
|
|
762
836
|
|
|
763
|
-
|
|
837
|
+
|
|
838
|
+
<a id="org384f34a"></a>
|
|
839
|
+
|
|
840
|
+
### realPath(path) ===> string
|
|
764
841
|
|
|
765
842
|
Converts a relative path to an absolute path on the remote server. This method is mainly used internally to resolve remote path names.
|
|
766
843
|
|
|
@@ -768,11 +845,17 @@ Converts a relative path to an absolute path on the remote server. This method i
|
|
|
768
845
|
|
|
769
846
|
- **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
|
|
770
847
|
|
|
771
|
-
|
|
848
|
+
|
|
849
|
+
<a id="org8fa77d4"></a>
|
|
850
|
+
|
|
851
|
+
### cwd() ==> string
|
|
772
852
|
|
|
773
853
|
Returns what the server believes is the current remote working directory.
|
|
774
854
|
|
|
775
|
-
|
|
855
|
+
|
|
856
|
+
<a id="orgb824235"></a>
|
|
857
|
+
|
|
858
|
+
### uploadDir(srcDir, dstDir, filter) ==> string
|
|
776
859
|
|
|
777
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.
|
|
778
861
|
|
|
@@ -834,7 +917,10 @@ The optionsl *filter* argument is a function which will be called for each item
|
|
|
834
917
|
|
|
835
918
|
```
|
|
836
919
|
|
|
837
|
-
|
|
920
|
+
|
|
921
|
+
<a id="org46aa66f"></a>
|
|
922
|
+
|
|
923
|
+
### downloadDir(srcDir, dstDir, filter) ==> string
|
|
838
924
|
|
|
839
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.
|
|
840
926
|
|
|
@@ -894,7 +980,10 @@ The optional *filter* argument is a predicate function which will be called with
|
|
|
894
980
|
|
|
895
981
|
```
|
|
896
982
|
|
|
897
|
-
|
|
983
|
+
|
|
984
|
+
<a id="orge3fcb81"></a>
|
|
985
|
+
|
|
986
|
+
### end() ==> boolean
|
|
898
987
|
|
|
899
988
|
Ends the current client session, releasing the client socket and associated resources. This function also removes all listeners associated with the client.
|
|
900
989
|
|
|
@@ -915,7 +1004,10 @@ Ends the current client session, releasing the client socket and associated reso
|
|
|
915
1004
|
});
|
|
916
1005
|
```
|
|
917
1006
|
|
|
918
|
-
|
|
1007
|
+
|
|
1008
|
+
<a id="org4607268"></a>
|
|
1009
|
+
|
|
1010
|
+
### Add and Remove Listeners
|
|
919
1011
|
|
|
920
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
|
|
921
1013
|
|
|
@@ -933,15 +1025,24 @@ Although normally not required, you can add and remove custom listeners on the s
|
|
|
933
1025
|
|
|
934
1026
|
Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
|
|
935
1027
|
|
|
936
|
-
# Platform Quirks & Warnings<a id="sec-5"></a>
|
|
937
1028
|
|
|
938
|
-
|
|
1029
|
+
<a id="org66a7061"></a>
|
|
1030
|
+
|
|
1031
|
+
# Platform Quirks & Warnings
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
<a id="org740553b"></a>
|
|
1035
|
+
|
|
1036
|
+
## Server Capabilities
|
|
939
1037
|
|
|
940
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.
|
|
941
1039
|
|
|
942
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.
|
|
943
1041
|
|
|
944
|
-
|
|
1042
|
+
|
|
1043
|
+
<a id="orgf97d765"></a>
|
|
1044
|
+
|
|
1045
|
+
## Promises, Events & Managing Exceptions
|
|
945
1046
|
|
|
946
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.
|
|
947
1048
|
|
|
@@ -957,25 +1058,40 @@ The other area where additional events are fired is during the end() call. To de
|
|
|
957
1058
|
|
|
958
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.
|
|
959
1060
|
|
|
960
|
-
|
|
1061
|
+
|
|
1062
|
+
<a id="org6acf9c6"></a>
|
|
1063
|
+
|
|
1064
|
+
### Adding Custom Handlers
|
|
961
1065
|
|
|
962
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.
|
|
963
1067
|
|
|
964
|
-
|
|
1068
|
+
|
|
1069
|
+
<a id="orgb2f13ee"></a>
|
|
1070
|
+
|
|
1071
|
+
## Windows Based Servers
|
|
965
1072
|
|
|
966
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.
|
|
967
1074
|
|
|
968
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.
|
|
969
1076
|
|
|
970
|
-
|
|
1077
|
+
|
|
1078
|
+
<a id="orgcd82e68"></a>
|
|
1079
|
+
|
|
1080
|
+
## Don't Re-use SftpClient Objects
|
|
971
1081
|
|
|
972
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.
|
|
973
1083
|
|
|
974
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.
|
|
975
1085
|
|
|
976
|
-
# FAQ<a id="sec-6"></a>
|
|
977
1086
|
|
|
978
|
-
|
|
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
|
|
979
1095
|
|
|
980
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.
|
|
981
1097
|
|
|
@@ -983,7 +1099,10 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
|
|
|
983
1099
|
|
|
984
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.
|
|
985
1101
|
|
|
986
|
-
|
|
1102
|
+
|
|
1103
|
+
<a id="org3e48901"></a>
|
|
1104
|
+
|
|
1105
|
+
## How can I pass writable stream as dst for get method?
|
|
987
1106
|
|
|
988
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()`.
|
|
989
1108
|
|
|
@@ -1039,7 +1158,10 @@ sftp
|
|
|
1039
1158
|
});
|
|
1040
1159
|
```
|
|
1041
1160
|
|
|
1042
|
-
|
|
1161
|
+
|
|
1162
|
+
<a id="org1474028"></a>
|
|
1163
|
+
|
|
1164
|
+
## How can I upload files without having to specify a password?
|
|
1043
1165
|
|
|
1044
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.
|
|
1045
1167
|
|
|
@@ -1071,7 +1193,10 @@ sftp.connect({
|
|
|
1071
1193
|
}
|
|
1072
1194
|
```
|
|
1073
1195
|
|
|
1074
|
-
|
|
1196
|
+
|
|
1197
|
+
<a id="orgb9a0ec1"></a>
|
|
1198
|
+
|
|
1199
|
+
## How can I connect through a Socks Proxy
|
|
1075
1200
|
|
|
1076
1201
|
This solution was provided by @jmorino.
|
|
1077
1202
|
|
|
@@ -1083,7 +1208,7 @@ const host = 'my-sftp-server.net';
|
|
|
1083
1208
|
const port = 22; // default SSH/SFTP port on remote server
|
|
1084
1209
|
|
|
1085
1210
|
// connect to SOCKS 5 proxy
|
|
1086
|
-
const {
|
|
1211
|
+
const { socks } = await SocksClient.createConnection({
|
|
1087
1212
|
proxy: {
|
|
1088
1213
|
host: 'my.proxy', // proxy hostname
|
|
1089
1214
|
port: 1080, // proxy port
|
|
@@ -1096,7 +1221,7 @@ const { socket } = await SocksClient.createConnection({
|
|
|
1096
1221
|
const client = new SFTPClient();
|
|
1097
1222
|
client.connect({
|
|
1098
1223
|
host,
|
|
1099
|
-
sock: socket, // pass the socket to proxy here (see ssh2 doc)
|
|
1224
|
+
sock: socks.socket, // pass the socket to proxy here (see ssh2 doc)
|
|
1100
1225
|
username: '.....',
|
|
1101
1226
|
privateKey: '.....'
|
|
1102
1227
|
})
|
|
@@ -1104,13 +1229,19 @@ client.connect({
|
|
|
1104
1229
|
// client is connected
|
|
1105
1230
|
```
|
|
1106
1231
|
|
|
1107
|
-
|
|
1232
|
+
|
|
1233
|
+
<a id="org62e1f3a"></a>
|
|
1234
|
+
|
|
1235
|
+
## Timeout while waiting for handshake or handshake errors
|
|
1108
1236
|
|
|
1109
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.
|
|
1110
1238
|
|
|
1111
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.
|
|
1112
1240
|
|
|
1113
|
-
|
|
1241
|
+
|
|
1242
|
+
<a id="org7f26ef8"></a>
|
|
1243
|
+
|
|
1244
|
+
## How can I limit upload/download speed
|
|
1114
1245
|
|
|
1115
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.
|
|
1116
1247
|
|
|
@@ -1147,7 +1278,10 @@ try {
|
|
|
1147
1278
|
}
|
|
1148
1279
|
```
|
|
1149
1280
|
|
|
1150
|
-
|
|
1281
|
+
|
|
1282
|
+
<a id="orgfb52441"></a>
|
|
1283
|
+
|
|
1284
|
+
## Connection hangs or fails for larger files
|
|
1151
1285
|
|
|
1152
1286
|
This was contributed by Ladislav Jacho. Thanks.
|
|
1153
1287
|
|
|
@@ -1155,11 +1289,17 @@ A symptom of this issue is that you are able to upload small files, but uploadin
|
|
|
1155
1289
|
|
|
1156
1290
|
For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
|
|
1157
1291
|
|
|
1158
|
-
|
|
1292
|
+
|
|
1293
|
+
<a id="orgbe2b99e"></a>
|
|
1294
|
+
|
|
1295
|
+
# Examples
|
|
1159
1296
|
|
|
1160
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.
|
|
1161
1298
|
|
|
1162
|
-
|
|
1299
|
+
|
|
1300
|
+
<a id="orgd2dcb05"></a>
|
|
1301
|
+
|
|
1302
|
+
# Troubleshooting
|
|
1163
1303
|
|
|
1164
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.
|
|
1165
1305
|
|
|
@@ -1171,11 +1311,17 @@ Note also that in the repository there are two useful directories. The first is
|
|
|
1171
1311
|
|
|
1172
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.
|
|
1173
1313
|
|
|
1174
|
-
|
|
1314
|
+
|
|
1315
|
+
<a id="orgae04d0a"></a>
|
|
1316
|
+
|
|
1317
|
+
## Common Errors
|
|
1175
1318
|
|
|
1176
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.
|
|
1177
1320
|
|
|
1178
|
-
|
|
1321
|
+
|
|
1322
|
+
<a id="org2018983"></a>
|
|
1323
|
+
|
|
1324
|
+
### Not returning the promise in a `then()` block
|
|
1179
1325
|
|
|
1180
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
|
|
1181
1327
|
|
|
@@ -1209,7 +1355,10 @@ Note the `return` statements. These ensure that the Promise returned by the clie
|
|
|
1209
1355
|
|
|
1210
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.
|
|
1211
1357
|
|
|
1212
|
-
|
|
1358
|
+
|
|
1359
|
+
<a id="org6a3654b"></a>
|
|
1360
|
+
|
|
1361
|
+
### Mixing Promise Chains and Async/Await
|
|
1213
1362
|
|
|
1214
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!)
|
|
1215
1364
|
|
|
@@ -1266,7 +1415,10 @@ async function doSftp() {
|
|
|
1266
1415
|
}
|
|
1267
1416
|
```
|
|
1268
1417
|
|
|
1269
|
-
|
|
1418
|
+
|
|
1419
|
+
<a id="org36087fd"></a>
|
|
1420
|
+
|
|
1421
|
+
### Try/catch and Error Handlers
|
|
1270
1422
|
|
|
1271
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.
|
|
1272
1424
|
|
|
@@ -1274,17 +1426,26 @@ The basic problem is that the try/catch block will have completed execution befo
|
|
|
1274
1426
|
|
|
1275
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.
|
|
1276
1428
|
|
|
1277
|
-
|
|
1429
|
+
|
|
1430
|
+
<a id="orgd09c9c8"></a>
|
|
1431
|
+
|
|
1432
|
+
### Server Differences
|
|
1278
1433
|
|
|
1279
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.
|
|
1280
1435
|
|
|
1281
|
-
|
|
1436
|
+
|
|
1437
|
+
<a id="org67e5a24"></a>
|
|
1438
|
+
|
|
1439
|
+
### Avoid Concurrent Operations
|
|
1282
1440
|
|
|
1283
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.
|
|
1284
1442
|
|
|
1285
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.
|
|
1286
1444
|
|
|
1287
|
-
|
|
1445
|
+
|
|
1446
|
+
<a id="org00c676b"></a>
|
|
1447
|
+
|
|
1448
|
+
## Debugging Support
|
|
1288
1449
|
|
|
1289
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;
|
|
1290
1451
|
|
|
@@ -1314,7 +1475,10 @@ If you just want to see debug messages from `ssh2-sftp-client` and exclude debug
|
|
|
1314
1475
|
}
|
|
1315
1476
|
```
|
|
1316
1477
|
|
|
1317
|
-
|
|
1478
|
+
|
|
1479
|
+
<a id="org6f7f508"></a>
|
|
1480
|
+
|
|
1481
|
+
# Logging Issues
|
|
1318
1482
|
|
|
1319
1483
|
Please log an issue for all bugs, questions, feature and enhancement requests. Please ensure you include the module version, node version and platform.
|
|
1320
1484
|
|
|
@@ -1328,7 +1492,10 @@ I am happy to try and help diagnose and fix any issues you encounter while using
|
|
|
1328
1492
|
|
|
1329
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.
|
|
1330
1494
|
|
|
1331
|
-
|
|
1495
|
+
|
|
1496
|
+
<a id="org2d2ad0d"></a>
|
|
1497
|
+
|
|
1498
|
+
# Pull Requests
|
|
1332
1499
|
|
|
1333
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.
|
|
1334
1501
|
|
|
@@ -1340,7 +1507,10 @@ This module will adopt a standard semantic versioning policy. Please indicate in
|
|
|
1340
1507
|
- **Minor:** Minor change, enhancement or new feature which does not change existing API and will not break existing client code.
|
|
1341
1508
|
- **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
|
|
1342
1509
|
|
|
1343
|
-
|
|
1510
|
+
|
|
1511
|
+
<a id="orgba32989"></a>
|
|
1512
|
+
|
|
1513
|
+
# Contributors
|
|
1344
1514
|
|
|
1345
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.
|
|
1346
1516
|
|
|
@@ -1359,4 +1529,4 @@ Thanks to the following for their contributions -
|
|
|
1359
1529
|
- **Emma Milner:** Contributed fix for put() bug
|
|
1360
1530
|
- **Witni Davis:** Contributed PR to fix put() RCE when using 'finish' rather than 'close' to resolve promise
|
|
1361
1531
|
- **Maik Marschner:** Contributed fix for connect() not returning sftp object. Also included test to check for this regression in future.
|
|
1362
|
-
- **cakemasher:** Contributed fix for removeTempListeners().
|
|
1532
|
+
- **cakemasher:** Contributed fix for removeTempListeners().
|
package/README.org
CHANGED
|
@@ -9,9 +9,9 @@ convenience abstraction as well as a Promise based API.
|
|
|
9
9
|
Documentation on the methods and available options in the underlying modules can
|
|
10
10
|
be found on the [[https://github.com/mscdex/ssh2][SSH2]] project pages.
|
|
11
11
|
|
|
12
|
-
Current stable release is *v8.
|
|
12
|
+
Current stable release is *v8.1.0*.
|
|
13
13
|
|
|
14
|
-
Code has been tested against Node versions 14.19.1, 16.
|
|
14
|
+
Code has been tested against Node versions 14.19.1, 16.15.0 and 18.1.0
|
|
15
15
|
|
|
16
16
|
Node versions < 12.x are not supported. However, node v10.x should still work,
|
|
17
17
|
although some tests will fail due to changes in file system functions used in
|
|
@@ -44,6 +44,15 @@ test setup and tear down.
|
|
|
44
44
|
extreme use case which is rare (a use case which wold also have been
|
|
45
45
|
problematic with the old implementation as performance would have been very
|
|
46
46
|
poor).
|
|
47
|
+
|
|
48
|
+
- v8.1.0: The ~list()~ method now includes a ~longname~ property in the descriptor
|
|
49
|
+
object for each file in the listing. The ~longname~ value is a string which
|
|
50
|
+
resembles the ~ls -l~ listing.
|
|
51
|
+
|
|
52
|
+
- v8.1.0: The ~rmdir()~ method has been further modified to not recurse into
|
|
53
|
+
directories using async. It was found that on large directories, this could
|
|
54
|
+
result in too many separate async processes. Only deletion of files is now
|
|
55
|
+
handled with asynchronous calls.
|
|
47
56
|
|
|
48
57
|
* Installation
|
|
49
58
|
|
|
@@ -308,6 +317,7 @@ The objects in the array returned by ~list()~ have the following properties;
|
|
|
308
317
|
},
|
|
309
318
|
owner: // user ID
|
|
310
319
|
group: // group ID
|
|
320
|
+
longname: // like ls -l line
|
|
311
321
|
}
|
|
312
322
|
#+end_src
|
|
313
323
|
|
|
@@ -1353,7 +1363,7 @@ This solution was provided by @jmorino.
|
|
|
1353
1363
|
const port = 22; // default SSH/SFTP port on remote server
|
|
1354
1364
|
|
|
1355
1365
|
// connect to SOCKS 5 proxy
|
|
1356
|
-
const {
|
|
1366
|
+
const { socks } = await SocksClient.createConnection({
|
|
1357
1367
|
proxy: {
|
|
1358
1368
|
host: 'my.proxy', // proxy hostname
|
|
1359
1369
|
port: 1080, // proxy port
|
|
@@ -1366,7 +1376,7 @@ This solution was provided by @jmorino.
|
|
|
1366
1376
|
const client = new SFTPClient();
|
|
1367
1377
|
client.connect({
|
|
1368
1378
|
host,
|
|
1369
|
-
sock: socket, // pass the socket to proxy here (see ssh2 doc)
|
|
1379
|
+
sock: socks.socket, // pass the socket to proxy here (see ssh2 doc)
|
|
1370
1380
|
username: '.....',
|
|
1371
1381
|
privateKey: '.....'
|
|
1372
1382
|
})
|
package/package.json
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ssh2-sftp-client",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.1.0",
|
|
4
4
|
"description": "ssh2 sftp client for node",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "https://github.com/theophilusx/ssh2-sftp-client"
|
|
9
9
|
},
|
|
10
|
-
"keywords": [
|
|
10
|
+
"keywords": [
|
|
11
|
+
"sftp",
|
|
12
|
+
"nodejs",
|
|
13
|
+
"promises"
|
|
14
|
+
],
|
|
11
15
|
"scripts": {
|
|
12
16
|
"test": "mocha",
|
|
13
17
|
"coverage": "nyc npm run test",
|
|
@@ -47,6 +51,6 @@
|
|
|
47
51
|
"dependencies": {
|
|
48
52
|
"concat-stream": "^2.0.0",
|
|
49
53
|
"promise-retry": "^2.0.1",
|
|
50
|
-
"ssh2": "^1.
|
|
54
|
+
"ssh2": "^1.10.0"
|
|
51
55
|
}
|
|
52
56
|
}
|
package/src/index.js
CHANGED
|
@@ -428,6 +428,7 @@ class SftpClient {
|
|
|
428
428
|
},
|
|
429
429
|
owner: item.attrs.uid,
|
|
430
430
|
group: item.attrs.gid,
|
|
431
|
+
longname: item.longname,
|
|
431
432
|
};
|
|
432
433
|
});
|
|
433
434
|
}
|
|
@@ -790,45 +791,63 @@ class SftpClient {
|
|
|
790
791
|
* @param {Object} options
|
|
791
792
|
* @return {Promise<String>}
|
|
792
793
|
*/
|
|
794
|
+
|
|
793
795
|
async append(input, remotePath, options = {}) {
|
|
794
|
-
const
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
796
|
+
const _append = (input, remotePath, options) => {
|
|
797
|
+
return new Promise((resolve, reject) => {
|
|
798
|
+
this.debugMsg(`append -> remote: ${remotePath} `, options);
|
|
799
|
+
options.flags = 'a';
|
|
800
|
+
let stream = this.sftp.createWriteStream(remotePath, options);
|
|
801
|
+
stream.on('error', (err) => {
|
|
802
|
+
this.debugMsg(
|
|
803
|
+
`append: Error ${err.message} appending to ${remotePath}`
|
|
804
|
+
);
|
|
805
|
+
reject(fmtError(`${err.message} ${remotePath}`, 'append', err.code));
|
|
806
|
+
});
|
|
807
|
+
stream.on('close', () => {
|
|
808
|
+
this.debugMsg(`append: data appended to ${remotePath}`);
|
|
809
|
+
resolve(`Appended data to ${remotePath}`);
|
|
810
|
+
});
|
|
811
|
+
if (input instanceof Buffer) {
|
|
812
|
+
this.debugMsg('append: writing data buffer to remote file');
|
|
813
|
+
stream.write(input);
|
|
814
|
+
stream.end();
|
|
808
815
|
} else {
|
|
809
|
-
this.debugMsg(
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
816
|
+
this.debugMsg('append: writing stream to remote file');
|
|
817
|
+
input.pipe(stream);
|
|
818
|
+
}
|
|
819
|
+
});
|
|
820
|
+
};
|
|
821
|
+
|
|
822
|
+
let listeners;
|
|
823
|
+
try {
|
|
824
|
+
listeners = addTempListeners(this, 'append');
|
|
825
|
+
if (typeof input === 'string') {
|
|
826
|
+
this.debugMsg('append: attempt to append two files - throw');
|
|
827
|
+
throw fmtError(
|
|
828
|
+
'Cannot append one file to another',
|
|
829
|
+
'append',
|
|
830
|
+
errorCode.badPath
|
|
831
|
+
);
|
|
832
|
+
}
|
|
833
|
+
if (haveConnection(this, 'append')) {
|
|
834
|
+
const fileType = await this.exists(remotePath);
|
|
835
|
+
if (fileType && fileType === 'd') {
|
|
836
|
+
this.debugMsg(`append: Error ${remotePath} not a file`);
|
|
837
|
+
throw fmtError(
|
|
838
|
+
`Bad path: ${remotePath}: cannot append to a directory`,
|
|
839
|
+
'append',
|
|
840
|
+
errorCode.badPath
|
|
841
|
+
);
|
|
826
842
|
}
|
|
843
|
+
await _append(input, remotePath, options);
|
|
827
844
|
}
|
|
828
|
-
}
|
|
845
|
+
} catch (e) {
|
|
846
|
+
throw e.custom ? e : fmtError(e.message, 'append', e.code);
|
|
847
|
+
} finally {
|
|
829
848
|
removeTempListeners(this, listeners, 'append');
|
|
830
849
|
this._resetEventFlags();
|
|
831
|
-
}
|
|
850
|
+
}
|
|
832
851
|
}
|
|
833
852
|
|
|
834
853
|
/**
|
|
@@ -951,15 +970,13 @@ class SftpClient {
|
|
|
951
970
|
let dirs = list.filter((item) => item.type === 'd');
|
|
952
971
|
this.debugMsg('rmdir contents (files): ', files);
|
|
953
972
|
this.debugMsg('rmdir contents (dirs): ', dirs);
|
|
973
|
+
for (let d of dirs) {
|
|
974
|
+
await _dormdir(`${p}${this.remotePathSep}${d.name}`, true);
|
|
975
|
+
}
|
|
954
976
|
let promiseList = [];
|
|
955
977
|
for (let f of files) {
|
|
956
978
|
promiseList.push(_delete(`${p}${this.remotePathSep}${f.name}`));
|
|
957
979
|
}
|
|
958
|
-
for (let d of dirs) {
|
|
959
|
-
promiseList.push(
|
|
960
|
-
_dormdir(`${p}${this.remotePathSep}${d.name}`, true)
|
|
961
|
-
);
|
|
962
|
-
}
|
|
963
980
|
await Promise.all(promiseList);
|
|
964
981
|
}
|
|
965
982
|
}
|
|
@@ -1146,8 +1163,7 @@ class SftpClient {
|
|
|
1146
1163
|
* server.
|
|
1147
1164
|
* @param {String} srcDir - local source directory
|
|
1148
1165
|
* @param {String} dstDir - remote destination directory
|
|
1149
|
-
* @param {
|
|
1150
|
-
* files and directories to upload
|
|
1166
|
+
* @param {function(String,Boolean):Boolean} filter - (Optional) 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. If the function returns true, the item will be uploaded
|
|
1151
1167
|
* @returns {Promise<String>}
|
|
1152
1168
|
*/
|
|
1153
1169
|
async uploadDir(srcDir, dstDir, filter) {
|
|
@@ -1224,8 +1240,7 @@ class SftpClient {
|
|
|
1224
1240
|
* file system.
|
|
1225
1241
|
* @param {String} srcDir - remote source directory
|
|
1226
1242
|
* @param {String} dstDir - local destination directory
|
|
1227
|
-
* @param {
|
|
1228
|
-
* files and directories to upload
|
|
1243
|
+
* @param {function(String,Boolean):Boolean} filter - (Optional) The first argument is the full path of the item to be downloaded and the second argument is a boolean, which will be true if the target path is for a directory. If the function returns true, the item will be downloaded
|
|
1229
1244
|
* @returns {Promise<String>}
|
|
1230
1245
|
*/
|
|
1231
1246
|
async downloadDir(srcDir, dstDir, filter) {
|