ssh2-sftp-client 9.0.4 → 9.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 +431 -363
- package/README.org +44 -91
- package/package.json +3 -3
- package/src/index.js +587 -543
- package/src/utils.js +22 -5
package/README.md
CHANGED
|
@@ -1,110 +1,104 @@
|
|
|
1
|
-
- [Overview](#
|
|
2
|
-
- [Version 9.
|
|
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, filter) ==> 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, options) ==> string](#
|
|
27
|
-
- [downloadDir(srcDir, dstDir, options) ==> string](#
|
|
28
|
-
- [createReadStream(remotePath, options)) ==> stream object](#
|
|
29
|
-
- [createWriteStream(remotePath, options) ==> stream object](#
|
|
30
|
-
- [rcopy(srcPath, dstPath) ==> string](#
|
|
31
|
-
- [end() ==> boolean](#
|
|
32
|
-
- [Add and Remove Listeners](#
|
|
33
|
-
- [Platform Quirks & Warnings](#
|
|
34
|
-
- [Server Capabilities](#
|
|
35
|
-
- [Issues with `fastPut()` and `fastGet()` Methods](#
|
|
36
|
-
- [Promises, Events & Managing Exceptions](#
|
|
37
|
-
- [Adding Custom Handlers](#
|
|
38
|
-
- [Windows Based Servers](#
|
|
39
|
-
- [Don't Re-use SftpClient Objects](#
|
|
40
|
-
- [FAQ](#
|
|
41
|
-
- [Remote server drops connections with only an end event](#
|
|
42
|
-
- [How can I pass writeable stream as dst for get method?](#
|
|
43
|
-
- [How can I upload files without having to specify a password?](#
|
|
44
|
-
- [How can I connect through a Socks Proxy](#
|
|
45
|
-
- [Timeout while waiting for handshake or handshake errors](#
|
|
46
|
-
- [How can I limit upload/download speed](#
|
|
47
|
-
- [Connection hangs or fails for larger files](#
|
|
48
|
-
- [
|
|
49
|
-
- [
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
- [
|
|
53
|
-
- [
|
|
54
|
-
- [
|
|
55
|
-
- [
|
|
56
|
-
|
|
57
|
-
- [
|
|
58
|
-
- [
|
|
59
|
-
- [
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
1
|
+
- [Overview](#orgd72cafd)
|
|
2
|
+
- [Version 9.1.0 Changes](#org9aa6de9)
|
|
3
|
+
- [Installation](#orgfa343ca)
|
|
4
|
+
- [Basic Usage](#org4ac2087)
|
|
5
|
+
- [Documentation](#orgd23cd4f)
|
|
6
|
+
- [Specifying Paths](#org0cb332c)
|
|
7
|
+
- [Methods](#org67943d5)
|
|
8
|
+
- [new SftpClient(name) ===> SFTP client object](#orgc12cc98)
|
|
9
|
+
- [connect(config) ===> SFTP object](#org93b5103)
|
|
10
|
+
- [list(path, filter) ==> Array[object]](#org330e94d)
|
|
11
|
+
- [exists(path) ==> boolean](#orgb3e6094)
|
|
12
|
+
- [stat(path) ==> object](#org2b25315)
|
|
13
|
+
- [get(path, dst, options) ==> String|Stream|Buffer](#org252a716)
|
|
14
|
+
- [fastGet(remotePath, localPath, options) ===> string](#org5edb596)
|
|
15
|
+
- [put(src, remotePath, options) ==> string](#org99d1b64)
|
|
16
|
+
- [fastPut(localPath, remotePath, options) ==> string](#orgdce84b4)
|
|
17
|
+
- [append(input, remotePath, options) ==> string](#org86b6dd8)
|
|
18
|
+
- [mkdir(path, recursive) ==> string](#orga867f75)
|
|
19
|
+
- [rmdir(path, recursive) ==> string](#org893c3d4)
|
|
20
|
+
- [delete(path, noErrorOK) ==> string](#org877ca9f)
|
|
21
|
+
- [rename(fromPath, toPath) ==> string](#orgc41333c)
|
|
22
|
+
- [posixRename(fromPath, toPath) ==> string](#org9493ebc)
|
|
23
|
+
- [chmod(path, mode) ==> string](#orgf78d430)
|
|
24
|
+
- [realPath(path) ===> string](#orgf15a5df)
|
|
25
|
+
- [cwd() ==> string](#org2f554a8)
|
|
26
|
+
- [uploadDir(srcDir, dstDir, options) ==> string](#orge085c95)
|
|
27
|
+
- [downloadDir(srcDir, dstDir, options) ==> string](#orgd7cebbc)
|
|
28
|
+
- [createReadStream(remotePath, options)) ==> stream object](#orgc133c2f)
|
|
29
|
+
- [createWriteStream(remotePath, options) ==> stream object](#org44e3122)
|
|
30
|
+
- [rcopy(srcPath, dstPath) ==> string](#org3394196)
|
|
31
|
+
- [end() ==> boolean](#org5f0471c)
|
|
32
|
+
- [Add and Remove Listeners](#orga12e63e)
|
|
33
|
+
- [Platform Quirks & Warnings](#org3cbc095)
|
|
34
|
+
- [Server Capabilities](#orge04f28e)
|
|
35
|
+
- [Issues with `fastPut()` and `fastGet()` Methods](#orgc96002e)
|
|
36
|
+
- [Promises, Events & Managing Exceptions](#org955a771)
|
|
37
|
+
- [Adding Custom Handlers](#org8f7b2d0)
|
|
38
|
+
- [Windows Based Servers](#orge15889c)
|
|
39
|
+
- [Don't Re-use SftpClient Objects](#orgb38b894)
|
|
40
|
+
- [FAQ](#org1e76649)
|
|
41
|
+
- [Remote server drops connections with only an end event](#orge3e4159)
|
|
42
|
+
- [How can I pass writeable stream as dst for get method?](#org77ed2d3)
|
|
43
|
+
- [How can I upload files without having to specify a password?](#orgad09bd5)
|
|
44
|
+
- [How can I connect through a Socks Proxy](#org175d696)
|
|
45
|
+
- [Timeout while waiting for handshake or handshake errors](#orgd310a6a)
|
|
46
|
+
- [How can I limit upload/download speed](#org5a0faa2)
|
|
47
|
+
- [Connection hangs or fails for larger files](#orgffe3a3c)
|
|
48
|
+
- [Typescript definition file out of date](#org390645f)
|
|
49
|
+
- [Examples](#orgce20a25)
|
|
50
|
+
- [Troubleshooting](#org31d0059)
|
|
51
|
+
- [Common Errors](#orgf1c2f29)
|
|
52
|
+
- [Not returning the promise in a `then()` block](#org14ab27b)
|
|
53
|
+
- [Mixing Promise Chains and Async/Await](#org6c257e3)
|
|
54
|
+
- [Try/catch and Error Handlers](#org765b1b2)
|
|
55
|
+
- [Server Differences](#orge67d910)
|
|
56
|
+
- [Avoid Concurrent Operations](#org734662f)
|
|
57
|
+
- [Debugging Support](#org658f3d6)
|
|
58
|
+
- [Logging Issues](#orgb0fbb63)
|
|
59
|
+
- [Pull Requests](#orgc22fdba)
|
|
60
|
+
- [Contributors](#org7bb6675)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
<a id="orgd72cafd"></a>
|
|
65
|
+
|
|
66
|
+
# Overview
|
|
63
67
|
|
|
64
68
|
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.
|
|
65
69
|
|
|
66
70
|
Documentation on the methods and available options in the underlying modules can be found on the [SSH2](https://github.com/mscdex/ssh2) project pages.
|
|
67
71
|
|
|
68
|
-
Current stable release is
|
|
72
|
+
Current stable release is \*v9.1.0.
|
|
69
73
|
|
|
70
|
-
Code has been tested against Node versions 14.
|
|
74
|
+
Code has been tested against Node versions 14.21.3, 16.19.1, 18.16.0 and 20.0.0
|
|
71
75
|
|
|
72
76
|
Node versions < 14.x are not supported.
|
|
73
77
|
|
|
74
|
-
## Version 9.x Changes<a id="sec-1-1"></a>
|
|
75
|
-
|
|
76
|
-
- Fix bug in `connect()` method when private key data was corrupted. The method was not handling errors fro corrupted ssh private keys and would hang indefinitely without reporting any error. Now reports that it was unable to parse the private key.
|
|
77
|
-
- Fix bug in `end()` method where it was possible for the module to attempt calling the underlying ssh2 `end()` method when ssh2 has not been initialised. This could lead to undefined reference errors.
|
|
78
|
-
- Fix bug in `get()` method where supplied destination streams were not close, creating a possible resource leak. If the remote file did not exist, the method would return an error, but failed to close any passed in stream supplied as the destination for the data in the `get()` call.
|
|
79
|
-
- Change the default end and close handlers not to throw error or reject promises. Previously, an end or close event would cause an error to be raised or a promise to be rejected if the event was deemed to be *unexpected*. However, classification of events as being unexpected was unreliable and didn't add much real value. Both these handlers will now invalidate the SFTP connection object and log that the event fired and nothing else.
|
|
80
|
-
- Changed when event handled flags are reset. Now they are reset after a new set of temporary listeners are added.
|
|
81
|
-
- Don't throw an error when calling end() if there is no active SFTP connection. It does no harm to call end() when there is no connection, so no need to raise an error.
|
|
82
|
-
- Use nullish coalescing when setting retry parameters instead of or'ing with defaults. Allows setting values to 0.
|
|
83
|
-
- **Breaking Change**: This version uses syntax not supported in node versions prior to v14. Therefore, node versions less than v14 will not work.
|
|
84
|
-
- **Breaking Change**: This `list()` method no longer accepts a regular expression for filtering the entries to be returned. You can now specify a filter function instead. The function is called for each item in the list of items to be returned, passing in the item object as its only argument. Essentially, this is just a call to `Array.filter()`, so the filter function should behave in the same way i.e. return true for items to be retained and false for those to be dropped.
|
|
85
|
-
- **Breaking Change**: The ability to set `autoClose` on read and write streams and the ability to set `end` on `pipe` operations has been removed. These options caused confusion for users and were too easy to get wrong, plus it made the methods overly complicated. For those use-cases where you want to control streams at a low level, two new methods have been added, `createReadStream()` and `createWriteStream()`. However, it should be noted that client code is 100% responsible for managing streams obtained using these methods. Use at your own risk!
|
|
86
|
-
- **Breaking Change**: The 3rd argument to `uploadDir()` and `downloadDir()` methods has been change. Previously, the argument was a filter function used to select which directories and files to be transferred. The 3rd argument is now an options object with two supported properties, `filter` and `useFastput` (for `uploadDir()`) or `useFastget` (for `downloadDir()`). If `useFastput` is true, the `fastPut()` method will be used to upload files. If `false` or missing, the slower, but better supported, `put()` method will be used. Likewise, the `useFastget` options can be set to `true` to use the `fastGet()` method for downloading files, otherwise the slower, but more reliable, `get()` method will be used.
|
|
87
|
-
- The `uploadDir()` and `downloadDir()` methods now use asynchronous processes to upload/download files. This should result in improved performance for these two methods.
|
|
88
|
-
- New Methods: Two new methods, `createWriteStream()` and `createReadStream()` have been added. These methods will return a stream object connected to a remote file on the `SFTP` server. Client code is responsible for managing these stream objects. This includes adding any necessary event listeners and disposing of the objects once finished with them.
|
|
89
|
-
- Re-factoring of Listeners: The library manages temporary listeners in order to provide a way to catch events and processes them inside a `Promise` context. Previously, every method added its own set of temporary listeners. However, this could result in multiple sets of listeners being added, especially for methods which call other methods as part of their processing e.g. `rmdir(),` `uploadDir()` and `dowqnloadDir()`. To avoid this, *internal only* versions of each method have been created. These internal methods use an *underscore* `_` prefix. Client code should not use these methods directly.
|
|
90
|
-
- New method: Added `rcopy()` method to perform a remote copy of a file on the remote SFTP server.
|
|
91
|
-
- Bumped ssh2 version to 1.11.0
|
|
92
|
-
|
|
93
|
-
# Installation<a id="sec-2"></a>
|
|
94
78
|
|
|
95
|
-
|
|
79
|
+
<a id="org9aa6de9"></a>
|
|
80
|
+
|
|
81
|
+
## Version 9.1.0 Changes
|
|
82
|
+
|
|
83
|
+
- Added lstat() method
|
|
84
|
+
- Fixed bug in option hadnling which was preventing setting file mode in get() and put() methods
|
|
85
|
+
- Fixed bug where a loss of network connections between establishment of the connection and calling various sftp methods was not handled and could result in an event causing the node process to exit with an error.
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
<a id="orgfa343ca"></a>
|
|
96
89
|
|
|
97
|
-
|
|
90
|
+
# Installation
|
|
98
91
|
|
|
92
|
+
```shell
|
|
99
93
|
npm install ssh2-sftp-client
|
|
100
94
|
```
|
|
101
95
|
|
|
102
|
-
# Basic Usage<a id="sec-3"></a>
|
|
103
96
|
|
|
104
|
-
|
|
97
|
+
<a id="org4ac2087"></a>
|
|
105
98
|
|
|
106
|
-
|
|
99
|
+
# Basic Usage
|
|
107
100
|
|
|
101
|
+
```js
|
|
108
102
|
let Client = require('ssh2-sftp-client');
|
|
109
103
|
let sftp = new Client();
|
|
110
104
|
|
|
@@ -122,13 +116,19 @@ sftp.connect({
|
|
|
122
116
|
});
|
|
123
117
|
```
|
|
124
118
|
|
|
125
|
-
|
|
119
|
+
|
|
120
|
+
<a id="orgd23cd4f"></a>
|
|
121
|
+
|
|
122
|
+
# Documentation
|
|
126
123
|
|
|
127
124
|
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)
|
|
128
125
|
|
|
129
126
|
All the methods will return a Promise, except for `on()` and `removeListener()`, which are typically only used in special use cases.
|
|
130
127
|
|
|
131
|
-
|
|
128
|
+
|
|
129
|
+
<a id="org0cb332c"></a>
|
|
130
|
+
|
|
131
|
+
## Specifying Paths
|
|
132
132
|
|
|
133
133
|
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.
|
|
134
134
|
|
|
@@ -141,35 +141,32 @@ There is a small performance hit for using `./` and `../` as the module must que
|
|
|
141
141
|
When specifying file paths, ensure to include a full path i.e. include the remote file name. Don't expect the module to append the local file name to the path you provide. For example, the following will not work
|
|
142
142
|
|
|
143
143
|
```javascript
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
144
|
client.put('/home/fred/test.txt', '/remote/dir');
|
|
148
145
|
```
|
|
149
146
|
|
|
150
147
|
will not result in the file `test.txt` being copied to `/remote/dir/test.txt`. You need to specify the target file name as well e.g.
|
|
151
148
|
|
|
152
149
|
```javascript
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
150
|
client.put('/home/fred/test.txt', '/remote/dir/test.txt');
|
|
157
151
|
```
|
|
158
152
|
|
|
159
153
|
Note that the remote file name does not have to be the same as the local file name. The following works fine;
|
|
160
154
|
|
|
161
155
|
```javascript
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
156
|
client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
|
|
166
157
|
```
|
|
167
158
|
|
|
168
159
|
This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
|
|
169
160
|
|
|
170
|
-
## Methods<a id="sec-4-2"></a>
|
|
171
161
|
|
|
172
|
-
|
|
162
|
+
<a id="org67943d5"></a>
|
|
163
|
+
|
|
164
|
+
## Methods
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
<a id="orgc12cc98"></a>
|
|
168
|
+
|
|
169
|
+
### new SftpClient(name) ===> SFTP client object
|
|
173
170
|
|
|
174
171
|
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.
|
|
175
172
|
|
|
@@ -180,9 +177,6 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
|
|
|
180
177
|
2. Example Use
|
|
181
178
|
|
|
182
179
|
```javascript
|
|
183
|
-
|
|
184
|
-
'use
|
|
185
|
-
|
|
186
180
|
'use strict';
|
|
187
181
|
|
|
188
182
|
const Client = require('ssh2-sftp-client');
|
|
@@ -208,7 +202,10 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
|
|
|
208
202
|
});
|
|
209
203
|
```
|
|
210
204
|
|
|
211
|
-
|
|
205
|
+
|
|
206
|
+
<a id="org93b5103"></a>
|
|
207
|
+
|
|
208
|
+
### connect(config) ===> SFTP object
|
|
212
209
|
|
|
213
210
|
Connect to an sftp server. Full documentation for connection options is available [here](https://github.com/mscdex/ssh2#user-content-client-methods)
|
|
214
211
|
|
|
@@ -219,9 +216,6 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
219
216
|
The `retries`, `retry_factor` and `retry_minTimeout` options are not part of the SSH2 module. These are part of the configuration for the [retry](https://www.npmjs.com/package/retry) package and what is used to enable retrying of sftp connection attempts. See the documentation for that package for an explanation of these values.
|
|
220
217
|
|
|
221
218
|
```javascript
|
|
222
|
-
|
|
223
|
-
//
|
|
224
|
-
|
|
225
219
|
// common options
|
|
226
220
|
|
|
227
221
|
let commonOpts {
|
|
@@ -235,12 +229,12 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
235
229
|
privateKey: fs.readFileSync('/path/to/key'), // Buffer or string that contains
|
|
236
230
|
passphrase: 'a pass phrase', // string - For an encrypted private key
|
|
237
231
|
readyTimeout: 20000, // integer How long (in ms) to wait for the SSH handshake
|
|
238
|
-
strictVendor: true // boolean - Performs a strict server vendor check
|
|
239
|
-
debug: myDebug
|
|
232
|
+
strictVendor: true, // boolean - Performs a strict server vendor check
|
|
233
|
+
debug: myDebug,// function - Set this to a function that receives a single
|
|
240
234
|
// string argument to get detailed (local) debug information.
|
|
241
|
-
retries: 2 // integer. Number of times to retry connecting
|
|
242
|
-
retry_factor: 2 // integer. Time factor used to calculate time between retries
|
|
243
|
-
retry_minTimeout: 2000 // integer. Minimum timeout between attempts
|
|
235
|
+
retries: 2, // integer. Number of times to retry connecting
|
|
236
|
+
retry_factor: 2, // integer. Time factor used to calculate time between retries
|
|
237
|
+
retry_minTimeout: 2000, // integer. Minimum timeout between attempts
|
|
244
238
|
};
|
|
245
239
|
|
|
246
240
|
// rarely used options
|
|
@@ -266,19 +260,18 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
266
260
|
2. Example Use
|
|
267
261
|
|
|
268
262
|
```javascript
|
|
269
|
-
|
|
270
|
-
sftp.connect({
|
|
271
|
-
|
|
272
|
-
|
|
273
263
|
sftp.connect({
|
|
274
|
-
host: example.com,
|
|
264
|
+
host: 'example.com',
|
|
275
265
|
port: 22,
|
|
276
266
|
username: 'donald',
|
|
277
267
|
password: 'youarefired'
|
|
278
268
|
});
|
|
279
269
|
```
|
|
280
270
|
|
|
281
|
-
|
|
271
|
+
|
|
272
|
+
<a id="org330e94d"></a>
|
|
273
|
+
|
|
274
|
+
### list(path, filter) ==> Array[object]
|
|
282
275
|
|
|
283
276
|
Retrieves a directory listing. This method returns a Promise, which once realised, returns an array of objects representing items in the remote directory.
|
|
284
277
|
|
|
@@ -288,9 +281,6 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
288
281
|
1. Example Use
|
|
289
282
|
|
|
290
283
|
```javascript
|
|
291
|
-
|
|
292
|
-
const
|
|
293
|
-
|
|
294
284
|
const Client = require('ssh2-sftp-client');
|
|
295
285
|
|
|
296
286
|
const config = {
|
|
@@ -300,7 +290,7 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
300
290
|
password: 'my-secret'
|
|
301
291
|
};
|
|
302
292
|
|
|
303
|
-
let sftp = new Client;
|
|
293
|
+
let sftp = new Client();
|
|
304
294
|
|
|
305
295
|
sftp.connect(config)
|
|
306
296
|
.then(() => {
|
|
@@ -321,24 +311,34 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
321
311
|
|
|
322
312
|
The objects in the array returned by `list()` have the following properties;
|
|
323
313
|
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
314
|
+
```javascript
|
|
315
|
+
{
|
|
316
|
+
type: '-', // file type(-, d, l)
|
|
317
|
+
name: 'example.txt', // file name
|
|
318
|
+
size: 43, // file size
|
|
319
|
+
modifyTime: 1675645360000, // file timestamp of modified time
|
|
320
|
+
accessTime: 1675645360000, // file timestamp of access time
|
|
321
|
+
rights: {
|
|
322
|
+
user: 'rw',
|
|
323
|
+
group: 'r',
|
|
324
|
+
other: 'r',
|
|
325
|
+
},
|
|
326
|
+
owner: 1000, // user ID
|
|
327
|
+
group: 1000, // group ID
|
|
328
|
+
longname: '-rw-r--r-- 1 fred fred 43 Feb 6 12:02 exaple.txt', // like ls -l line
|
|
329
|
+
}
|
|
330
330
|
```
|
|
331
331
|
|
|
332
|
-
|
|
332
|
+
|
|
333
|
+
<a id="orgb3e6094"></a>
|
|
334
|
+
|
|
335
|
+
### exists(path) ==> boolean
|
|
333
336
|
|
|
334
337
|
Tests to see if remote file or directory exists. Returns type of remote object if it exists or false if it does not.
|
|
335
338
|
|
|
336
339
|
1. Example Use
|
|
337
340
|
|
|
338
341
|
```javascript
|
|
339
|
-
|
|
340
|
-
const
|
|
341
|
-
|
|
342
342
|
const Client = require('ssh2-sftp-client');
|
|
343
343
|
|
|
344
344
|
const config = {
|
|
@@ -348,7 +348,7 @@ Tests to see if remote file or directory exists. Returns type of remote object i
|
|
|
348
348
|
password: 'my-secret'
|
|
349
349
|
};
|
|
350
350
|
|
|
351
|
-
let sftp = new Client;
|
|
351
|
+
let sftp = new Client();
|
|
352
352
|
|
|
353
353
|
sftp.connect(config)
|
|
354
354
|
.then(() => {
|
|
@@ -365,7 +365,10 @@ Tests to see if remote file or directory exists. Returns type of remote object i
|
|
|
365
365
|
});
|
|
366
366
|
```
|
|
367
367
|
|
|
368
|
-
|
|
368
|
+
|
|
369
|
+
<a id="org2b25315"></a>
|
|
370
|
+
|
|
371
|
+
### stat(path) ==> object
|
|
369
372
|
|
|
370
373
|
Returns the attributes associated with the object pointed to by `path`.
|
|
371
374
|
|
|
@@ -376,9 +379,6 @@ Returns the attributes associated with the object pointed to by `path`.
|
|
|
376
379
|
The `stat()` method returns an object with the following properties;
|
|
377
380
|
|
|
378
381
|
```javascript
|
|
379
|
-
|
|
380
|
-
let
|
|
381
|
-
|
|
382
382
|
let stats = {
|
|
383
383
|
mode: 33279, // integer representing type and permissions
|
|
384
384
|
uid: 1000, // user ID
|
|
@@ -399,9 +399,6 @@ Returns the attributes associated with the object pointed to by `path`.
|
|
|
399
399
|
2. Example Use
|
|
400
400
|
|
|
401
401
|
```javascript
|
|
402
|
-
|
|
403
|
-
let
|
|
404
|
-
|
|
405
402
|
let client = new Client();
|
|
406
403
|
|
|
407
404
|
client.connect(config)
|
|
@@ -419,7 +416,10 @@ Returns the attributes associated with the object pointed to by `path`.
|
|
|
419
416
|
});
|
|
420
417
|
```
|
|
421
418
|
|
|
422
|
-
|
|
419
|
+
|
|
420
|
+
<a id="org252a716"></a>
|
|
421
|
+
|
|
422
|
+
### get(path, dst, options) ==> String|Stream|Buffer
|
|
423
423
|
|
|
424
424
|
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.
|
|
425
425
|
|
|
@@ -434,9 +434,6 @@ In general, if you're going to pass in a string as the destination, you are bett
|
|
|
434
434
|
The `options` argument can be used to pass options to the underlying streams and pipe call used by this method. The argument is an object with three possible properties, `readStreamOptions`, `writeStreamOptions` and `pipeOptions`. The values for each of these properties should be an object containing the required options. For example, possible read stream and pipe options could be defined as
|
|
435
435
|
|
|
436
436
|
```javascript
|
|
437
|
-
|
|
438
|
-
let
|
|
439
|
-
|
|
440
437
|
let options = {
|
|
441
438
|
readStreamOptions: {
|
|
442
439
|
flags: 'r',
|
|
@@ -456,9 +453,6 @@ In general, if you're going to pass in a string as the destination, you are bett
|
|
|
456
453
|
2. Example Use
|
|
457
454
|
|
|
458
455
|
```javascript
|
|
459
|
-
|
|
460
|
-
let
|
|
461
|
-
|
|
462
456
|
let client = new Client();
|
|
463
457
|
|
|
464
458
|
let remotePath = '/remote/server/path/file.txt';
|
|
@@ -478,7 +472,10 @@ In general, if you're going to pass in a string as the destination, you are bett
|
|
|
478
472
|
|
|
479
473
|
- **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'.
|
|
480
474
|
|
|
481
|
-
|
|
475
|
+
|
|
476
|
+
<a id="org5edb596"></a>
|
|
477
|
+
|
|
478
|
+
### fastGet(remotePath, localPath, options) ===> string
|
|
482
479
|
|
|
483
480
|
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.
|
|
484
481
|
|
|
@@ -488,12 +485,13 @@ Downloads a file at remotePath to localPath using parallel reads for faster thro
|
|
|
488
485
|
|
|
489
486
|
1. Options
|
|
490
487
|
|
|
491
|
-
```
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
488
|
+
```javascript
|
|
489
|
+
{
|
|
490
|
+
concurrency: 64, // integer. Number of concurrent reads to use
|
|
491
|
+
chunkSize: 32768, // integer. Size of each read in bytes
|
|
492
|
+
step: function(total_transferred, chunk, total) // callback called each time a
|
|
493
|
+
// chunk is transferred
|
|
494
|
+
}
|
|
497
495
|
```
|
|
498
496
|
|
|
499
497
|
- **Warning:** Some servers do not respond correctly to requests to alter chunk size. This can result in lost or corrupted data.
|
|
@@ -501,9 +499,6 @@ Downloads a file at remotePath to localPath using parallel reads for faster thro
|
|
|
501
499
|
2. Sample Use
|
|
502
500
|
|
|
503
501
|
```javascript
|
|
504
|
-
|
|
505
|
-
let
|
|
506
|
-
|
|
507
502
|
let client = new Client();
|
|
508
503
|
let remotePath = '/server/path/file.txt';
|
|
509
504
|
let localPath = '/local/path/file.txt';
|
|
@@ -520,7 +515,10 @@ Downloads a file at remotePath to localPath using parallel reads for faster thro
|
|
|
520
515
|
});
|
|
521
516
|
```
|
|
522
517
|
|
|
523
|
-
|
|
518
|
+
|
|
519
|
+
<a id="org99d1b64"></a>
|
|
520
|
+
|
|
521
|
+
### put(src, remotePath, options) ==> string
|
|
524
522
|
|
|
525
523
|
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.
|
|
526
524
|
|
|
@@ -532,12 +530,13 @@ Upload data from local system to remote server. If the `src` argument is a strin
|
|
|
532
530
|
|
|
533
531
|
The options object supports three properties, `readStreamOptions`, `writeStreamOptions` and `pipeOptions`. The value for each property should be an object with options as properties and their associated values representing the option value. For example, you might use the following to set `writeStream` options.
|
|
534
532
|
|
|
535
|
-
```
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
533
|
+
```javascript
|
|
534
|
+
{
|
|
535
|
+
writeStreamOptions: {
|
|
536
|
+
flags: 'w', // w - write and a - append
|
|
537
|
+
encoding: null, // use null for binary files
|
|
538
|
+
mode: 0o666, // mode to use for created file (rwx)
|
|
539
|
+
}}
|
|
541
540
|
```
|
|
542
541
|
|
|
543
542
|
The most common options to use are mode and encoding. The values shown above are the defaults. You do not have to set encoding to utf-8 for text files, null is fine for all file types. However, using utf-8 encoding for binary files will often result in data corruption.
|
|
@@ -547,9 +546,6 @@ Upload data from local system to remote server. If the `src` argument is a strin
|
|
|
547
546
|
2. Example Use
|
|
548
547
|
|
|
549
548
|
```javascript
|
|
550
|
-
|
|
551
|
-
let
|
|
552
|
-
|
|
553
549
|
let client = new Client();
|
|
554
550
|
|
|
555
551
|
let data = fs.createReadStream('/path/to/local/file.txt');
|
|
@@ -569,7 +565,10 @@ Upload data from local system to remote server. If the `src` argument is a strin
|
|
|
569
565
|
|
|
570
566
|
- **Tip:** If the src argument is a path string, consider just using `fastPut()`.
|
|
571
567
|
|
|
572
|
-
|
|
568
|
+
|
|
569
|
+
<a id="orgdce84b4"></a>
|
|
570
|
+
|
|
571
|
+
### fastPut(localPath, remotePath, options) ==> string
|
|
573
572
|
|
|
574
573
|
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.
|
|
575
574
|
|
|
@@ -579,12 +578,14 @@ Uploads the data in file at `localPath` to a new file on remote server at `remot
|
|
|
579
578
|
|
|
580
579
|
1. Options
|
|
581
580
|
|
|
582
|
-
```
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
581
|
+
```javascript
|
|
582
|
+
{
|
|
583
|
+
concurrency: 64, // integer. Number of concurrent reads
|
|
584
|
+
chunkSize: 32768, // integer. Size of each read in bytes
|
|
585
|
+
mode: 0o755, // mixed. Integer or string representing the file mode to set
|
|
586
|
+
step: function(total_transferred, chunk, total) // function. Called every time
|
|
587
|
+
// a part of a file was transferred
|
|
588
|
+
}
|
|
588
589
|
```
|
|
589
590
|
|
|
590
591
|
- **Warning:** There have been reports that some SFTP servers will not honour requests for non-default chunk sizes. This can result in data loss or corruption.
|
|
@@ -592,9 +593,6 @@ Uploads the data in file at `localPath` to a new file on remote server at `remot
|
|
|
592
593
|
2. Example Use
|
|
593
594
|
|
|
594
595
|
```javascript
|
|
595
|
-
|
|
596
|
-
let
|
|
597
|
-
|
|
598
596
|
let localFile = '/path/to/file.txt';
|
|
599
597
|
let remoteFile = '/path/to/remote/file.txt';
|
|
600
598
|
let client = new Client();
|
|
@@ -611,7 +609,10 @@ Uploads the data in file at `localPath` to a new file on remote server at `remot
|
|
|
611
609
|
});
|
|
612
610
|
```
|
|
613
611
|
|
|
614
|
-
|
|
612
|
+
|
|
613
|
+
<a id="org86b6dd8"></a>
|
|
614
|
+
|
|
615
|
+
### append(input, remotePath, options) ==> string
|
|
615
616
|
|
|
616
617
|
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.
|
|
617
618
|
|
|
@@ -623,12 +624,13 @@ Append the `input` data to an existing remote file. There is no integrity checki
|
|
|
623
624
|
|
|
624
625
|
The following options are supported;
|
|
625
626
|
|
|
626
|
-
```
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
627
|
+
```javascript
|
|
628
|
+
{
|
|
629
|
+
flags: 'a', // w - write and a - append
|
|
630
|
+
encoding: null, // use null for binary files
|
|
631
|
+
mode: 0o666, // mode to use for created file (rwx)
|
|
632
|
+
autoClose: true // automatically close the write stream when finished
|
|
633
|
+
}
|
|
632
634
|
```
|
|
633
635
|
|
|
634
636
|
The most common options to use are mode and encoding. The values shown above are the defaults. You do not have to set encoding to utf-8 for text files, null is fine for all file types. Generally, I would not attempt to append binary files.
|
|
@@ -636,9 +638,6 @@ Append the `input` data to an existing remote file. There is no integrity checki
|
|
|
636
638
|
2. Example Use
|
|
637
639
|
|
|
638
640
|
```javascript
|
|
639
|
-
|
|
640
|
-
let
|
|
641
|
-
|
|
642
641
|
let remotePath = '/path/to/remote/file.txt';
|
|
643
642
|
let client = new Client();
|
|
644
643
|
|
|
@@ -654,7 +653,10 @@ Append the `input` data to an existing remote file. There is no integrity checki
|
|
|
654
653
|
});
|
|
655
654
|
```
|
|
656
655
|
|
|
657
|
-
|
|
656
|
+
|
|
657
|
+
<a id="orga867f75"></a>
|
|
658
|
+
|
|
659
|
+
### mkdir(path, recursive) ==> string
|
|
658
660
|
|
|
659
661
|
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.
|
|
660
662
|
|
|
@@ -664,9 +666,6 @@ Create a new directory. If the recursive flag is set to true, the method will cr
|
|
|
664
666
|
1. Example Use
|
|
665
667
|
|
|
666
668
|
```javascript
|
|
667
|
-
|
|
668
|
-
let
|
|
669
|
-
|
|
670
669
|
let remoteDir = '/path/to/new/dir';
|
|
671
670
|
let client = new Client();
|
|
672
671
|
|
|
@@ -682,7 +681,10 @@ Create a new directory. If the recursive flag is set to true, the method will cr
|
|
|
682
681
|
});
|
|
683
682
|
```
|
|
684
683
|
|
|
685
|
-
|
|
684
|
+
|
|
685
|
+
<a id="org893c3d4"></a>
|
|
686
|
+
|
|
687
|
+
### rmdir(path, recursive) ==> string
|
|
686
688
|
|
|
687
689
|
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.
|
|
688
690
|
|
|
@@ -694,9 +696,6 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
|
|
|
694
696
|
1. Example Use
|
|
695
697
|
|
|
696
698
|
```javascript
|
|
697
|
-
|
|
698
|
-
let
|
|
699
|
-
|
|
700
699
|
let remoteDir = '/path/to/remote/dir';
|
|
701
700
|
let client = new Client();
|
|
702
701
|
|
|
@@ -712,7 +711,10 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
|
|
|
712
711
|
});
|
|
713
712
|
```
|
|
714
713
|
|
|
715
|
-
|
|
714
|
+
|
|
715
|
+
<a id="org877ca9f"></a>
|
|
716
|
+
|
|
717
|
+
### delete(path, noErrorOK) ==> string
|
|
716
718
|
|
|
717
719
|
Delete a file on the remote server.
|
|
718
720
|
|
|
@@ -723,9 +725,6 @@ Delete a file on the remote server.
|
|
|
723
725
|
1. Example Use
|
|
724
726
|
|
|
725
727
|
```javascript
|
|
726
|
-
|
|
727
|
-
let
|
|
728
|
-
|
|
729
728
|
let remoteFile = '/path/to/remote/file.txt';
|
|
730
729
|
let client = new Client();
|
|
731
730
|
|
|
@@ -741,7 +740,10 @@ Delete a file on the remote server.
|
|
|
741
740
|
});
|
|
742
741
|
```
|
|
743
742
|
|
|
744
|
-
|
|
743
|
+
|
|
744
|
+
<a id="orgc41333c"></a>
|
|
745
|
+
|
|
746
|
+
### rename(fromPath, toPath) ==> string
|
|
745
747
|
|
|
746
748
|
Rename a file or directory from `fromPath` to `toPath`. You must have the necessary permissions to modify the remote file.
|
|
747
749
|
|
|
@@ -751,9 +753,6 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
|
|
|
751
753
|
1. Example Use
|
|
752
754
|
|
|
753
755
|
```javascript
|
|
754
|
-
|
|
755
|
-
let
|
|
756
|
-
|
|
757
756
|
let from = '/remote/path/to/old.txt';
|
|
758
757
|
let to = '/remote/path/to/new.txt';
|
|
759
758
|
let client = new Client();
|
|
@@ -770,7 +769,10 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
|
|
|
770
769
|
});
|
|
771
770
|
```
|
|
772
771
|
|
|
773
|
-
|
|
772
|
+
|
|
773
|
+
<a id="org9493ebc"></a>
|
|
774
|
+
|
|
775
|
+
### posixRename(fromPath, toPath) ==> string
|
|
774
776
|
|
|
775
777
|
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 file systems 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 sftp servers.
|
|
776
778
|
|
|
@@ -778,9 +780,6 @@ This method uses the openssh POSIX rename extension introduced in OpenSSH 4.8. T
|
|
|
778
780
|
- **toPath:** string. Path for new name. If it already exists, it will be replaced by file specified in fromPath
|
|
779
781
|
|
|
780
782
|
```javascript
|
|
781
|
-
|
|
782
|
-
let
|
|
783
|
-
|
|
784
783
|
let from = '/remote/path/to/old.txt';
|
|
785
784
|
let to = '/remote/path/to/new.txt';
|
|
786
785
|
let client = new Client();
|
|
@@ -797,7 +796,10 @@ client.connect(config)
|
|
|
797
796
|
});
|
|
798
797
|
```
|
|
799
798
|
|
|
800
|
-
|
|
799
|
+
|
|
800
|
+
<a id="orgf78d430"></a>
|
|
801
|
+
|
|
802
|
+
### chmod(path, mode) ==> string
|
|
801
803
|
|
|
802
804
|
Change the mode (read, write or execute permissions) of a remote file or directory.
|
|
803
805
|
|
|
@@ -807,9 +809,6 @@ Change the mode (read, write or execute permissions) of a remote file or directo
|
|
|
807
809
|
1. Example Use
|
|
808
810
|
|
|
809
811
|
```javascript
|
|
810
|
-
|
|
811
|
-
let
|
|
812
|
-
|
|
813
812
|
let path = '/path/to/remote/file.txt';
|
|
814
813
|
let newMode = 0o644; // rw-r-r
|
|
815
814
|
let client = new Client();
|
|
@@ -826,7 +825,10 @@ Change the mode (read, write or execute permissions) of a remote file or directo
|
|
|
826
825
|
});
|
|
827
826
|
```
|
|
828
827
|
|
|
829
|
-
|
|
828
|
+
|
|
829
|
+
<a id="orgf15a5df"></a>
|
|
830
|
+
|
|
831
|
+
### realPath(path) ===> string
|
|
830
832
|
|
|
831
833
|
Converts a relative path to an absolute path on the remote server. This method is mainly used internally to resolve remote path names.
|
|
832
834
|
|
|
@@ -834,11 +836,17 @@ Converts a relative path to an absolute path on the remote server. This method i
|
|
|
834
836
|
|
|
835
837
|
- **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
|
|
836
838
|
|
|
837
|
-
|
|
839
|
+
|
|
840
|
+
<a id="org2f554a8"></a>
|
|
841
|
+
|
|
842
|
+
### cwd() ==> string
|
|
838
843
|
|
|
839
844
|
Returns what the server believes is the current remote working directory.
|
|
840
845
|
|
|
841
|
-
|
|
846
|
+
|
|
847
|
+
<a id="orge085c95"></a>
|
|
848
|
+
|
|
849
|
+
### uploadDir(srcDir, dstDir, options) ==> string
|
|
842
850
|
|
|
843
851
|
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.
|
|
844
852
|
|
|
@@ -857,57 +865,57 @@ The `useFastput` option is a boolean option. If `true`, the method will use the
|
|
|
857
865
|
1. Example
|
|
858
866
|
|
|
859
867
|
```javascript
|
|
868
|
+
'use strict';
|
|
860
869
|
|
|
870
|
+
// Example of using the uploadDir() method to upload a directory
|
|
871
|
+
// to a remote SFTP server
|
|
861
872
|
|
|
873
|
+
const path = require('path');
|
|
874
|
+
const SftpClient = require('../src/index');
|
|
862
875
|
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
// Example of using the uploadDir() method to upload a directory
|
|
866
|
-
// to a remote SFTP server
|
|
867
|
-
|
|
868
|
-
const path = require('path');
|
|
869
|
-
const SftpClient = require('../src/index');
|
|
870
|
-
|
|
871
|
-
const dotenvPath = path.join(__dirname, '..', '.env');
|
|
872
|
-
require('dotenv').config({path: dotenvPath});
|
|
876
|
+
const dotenvPath = path.join(__dirname, '..', '.env');
|
|
877
|
+
require('dotenv').config({path: dotenvPath});
|
|
873
878
|
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
879
|
+
const config = {
|
|
880
|
+
host: process.env.SFTP_SERVER,
|
|
881
|
+
username: process.env.SFTP_USER,
|
|
882
|
+
password: process.env.SFTP_PASSWORD,
|
|
883
|
+
port: process.env.SFTP_PORT || 22
|
|
884
|
+
};
|
|
880
885
|
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
886
|
+
async function main() {
|
|
887
|
+
const client = new SftpClient('upload-test');
|
|
888
|
+
const src = path.join(__dirname, '..', 'test', 'testData', 'upload-src');
|
|
889
|
+
const dst = '/home/tim/upload-test';
|
|
885
890
|
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
891
|
+
try {
|
|
892
|
+
await client.connect(config);
|
|
893
|
+
client.on('upload', info => {
|
|
894
|
+
console.log(`Listener: Uploaded ${info.source}`);
|
|
895
|
+
});
|
|
896
|
+
let rslt = await client.uploadDir(src, dst);
|
|
897
|
+
return rslt;
|
|
898
|
+
} catch (err) {
|
|
899
|
+
console.error(err);
|
|
900
|
+
} finally {
|
|
901
|
+
client.end();
|
|
902
|
+
}
|
|
903
|
+
}
|
|
899
904
|
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
905
|
+
main()
|
|
906
|
+
.then(msg => {
|
|
907
|
+
console.log(msg);
|
|
908
|
+
})
|
|
909
|
+
.catch(err => {
|
|
910
|
+
console.log(`main error: ${err.message}`);
|
|
911
|
+
});
|
|
907
912
|
|
|
908
913
|
```
|
|
909
914
|
|
|
910
|
-
|
|
915
|
+
|
|
916
|
+
<a id="orgd7cebbc"></a>
|
|
917
|
+
|
|
918
|
+
### downloadDir(srcDir, dstDir, options) ==> string
|
|
911
919
|
|
|
912
920
|
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.
|
|
913
921
|
|
|
@@ -924,9 +932,6 @@ If the `useFastget` property is set to `true`, the method will use `fastGet()` t
|
|
|
924
932
|
1. Example
|
|
925
933
|
|
|
926
934
|
```javascript
|
|
927
|
-
|
|
928
|
-
'use
|
|
929
|
-
|
|
930
935
|
'use strict';
|
|
931
936
|
|
|
932
937
|
// Example of using the downloadDir() method to upload a directory
|
|
@@ -972,7 +977,10 @@ If the `useFastget` property is set to `true`, the method will use `fastGet()` t
|
|
|
972
977
|
|
|
973
978
|
```
|
|
974
979
|
|
|
975
|
-
|
|
980
|
+
|
|
981
|
+
<a id="orgc133c2f"></a>
|
|
982
|
+
|
|
983
|
+
### createReadStream(remotePath, options)) ==> stream object
|
|
976
984
|
|
|
977
985
|
Returns a read stream object which is attached to the remote file specified by the `remotePath` argument. This is a low level method which just returns a read stream object. Client code is fully responsible for managing and releasing the resources associated with the stream once finished i.e. closing files, removing listeners etc.
|
|
978
986
|
|
|
@@ -986,7 +994,10 @@ Returns a read stream object which is attached to the remote file specified by t
|
|
|
986
994
|
- **start:** Default 0. Position to start reading bytes from (inclusive)
|
|
987
995
|
- **end:** Position to stop reading bytes (inclusive).
|
|
988
996
|
|
|
989
|
-
|
|
997
|
+
|
|
998
|
+
<a id="org44e3122"></a>
|
|
999
|
+
|
|
1000
|
+
### createWriteStream(remotePath, options) ==> stream object
|
|
990
1001
|
|
|
991
1002
|
Returns a write stream object which is attached to the remote file specified in the `remotePath` argument. This is a low level function which just returns the stream object. Client code is fully responsible for managing that object, including closing any file descriptors and removing listeners etc.
|
|
992
1003
|
|
|
@@ -998,23 +1009,26 @@ Returns a write stream object which is attached to the remote file specified in
|
|
|
998
1009
|
- **autoClose:** true
|
|
999
1010
|
- **start:** Byte position to start writing from (inclusive). May require changing flag to 'r+'.
|
|
1000
1011
|
|
|
1001
|
-
|
|
1012
|
+
|
|
1013
|
+
<a id="org3394196"></a>
|
|
1014
|
+
|
|
1015
|
+
### rcopy(srcPath, dstPath) ==> string
|
|
1002
1016
|
|
|
1003
1017
|
Perform a remote file copy. The file identified by the `srcPath` argument will be copied to the file specified as the `dstPath` argument. The directory where `dstPath` will be placed must exist, but the actual file must not i.e. no overwrites allowed.
|
|
1004
1018
|
|
|
1005
1019
|
- **srcPath:** Path to remote file to be copied specified as a string
|
|
1006
1020
|
- **dstPath:** Path to where the copy will be created specified as a string
|
|
1007
1021
|
|
|
1008
|
-
|
|
1022
|
+
|
|
1023
|
+
<a id="org5f0471c"></a>
|
|
1024
|
+
|
|
1025
|
+
### end() ==> boolean
|
|
1009
1026
|
|
|
1010
1027
|
Ends the current client session, releasing the client socket and associated resources. This function also removes all listeners associated with the client.
|
|
1011
1028
|
|
|
1012
1029
|
1. Example Use
|
|
1013
1030
|
|
|
1014
1031
|
```javascript
|
|
1015
|
-
|
|
1016
|
-
let
|
|
1017
|
-
|
|
1018
1032
|
let client = new Client();
|
|
1019
1033
|
|
|
1020
1034
|
client.connect(config)
|
|
@@ -1029,7 +1043,10 @@ Ends the current client session, releasing the client socket and associated reso
|
|
|
1029
1043
|
});
|
|
1030
1044
|
```
|
|
1031
1045
|
|
|
1032
|
-
|
|
1046
|
+
|
|
1047
|
+
<a id="orga12e63e"></a>
|
|
1048
|
+
|
|
1049
|
+
### Add and Remove Listeners
|
|
1033
1050
|
|
|
1034
1051
|
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
|
|
1035
1052
|
|
|
@@ -1047,21 +1064,33 @@ Although normally not required, you can add and remove custom listeners on the s
|
|
|
1047
1064
|
|
|
1048
1065
|
Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
|
|
1049
1066
|
|
|
1050
|
-
# Platform Quirks & Warnings<a id="sec-5"></a>
|
|
1051
1067
|
|
|
1052
|
-
|
|
1068
|
+
<a id="org3cbc095"></a>
|
|
1069
|
+
|
|
1070
|
+
# Platform Quirks & Warnings
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
<a id="orge04f28e"></a>
|
|
1074
|
+
|
|
1075
|
+
## Server Capabilities
|
|
1053
1076
|
|
|
1054
1077
|
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 file system 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.
|
|
1055
1078
|
|
|
1056
1079
|
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.
|
|
1057
1080
|
|
|
1058
|
-
|
|
1081
|
+
|
|
1082
|
+
<a id="orgc96002e"></a>
|
|
1083
|
+
|
|
1084
|
+
## Issues with `fastPut()` and `fastGet()` Methods
|
|
1059
1085
|
|
|
1060
1086
|
The `fastPut()` and `fastGet()` methods are known to be somewhat dependent on SFTP server capabilities. Some SFTP servers just do not work correctly with concurrent connections and some are known to have issues with negotiating packet sizes. These issues can sometimes be resolved by tweaking the options supplied to the methods, such as setting number of concurrent connections or a specific packet size.
|
|
1061
1087
|
|
|
1062
1088
|
To see an example of the type of issues you can observe with `fastPut()` or `fastGet()`, have a look at [issue 407](https://github.com/theophilusx/ssh2-sftp-client/issues/407), which describes the experiences of one user. Bottom line, when it works, it tends to work well and be significantly faster than using just `get()` or `put()`. However, when developing code to run against different SFTP servers, especially where you are unable to test against each server, you are likely better off just using `get()` and `put()` or structuring your code so that users can select which method to use (this is what `ssh2-sftp-client` does - for example, see the `!downloadDir()` and `uploadDir()` methods.
|
|
1063
1089
|
|
|
1064
|
-
|
|
1090
|
+
|
|
1091
|
+
<a id="org955a771"></a>
|
|
1092
|
+
|
|
1093
|
+
## Promises, Events & Managing Exceptions
|
|
1065
1094
|
|
|
1066
1095
|
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.
|
|
1067
1096
|
|
|
@@ -1077,25 +1106,40 @@ The other area where additional events are fired is during the end() call. To de
|
|
|
1077
1106
|
|
|
1078
1107
|
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.
|
|
1079
1108
|
|
|
1080
|
-
|
|
1109
|
+
|
|
1110
|
+
<a id="org8f7b2d0"></a>
|
|
1111
|
+
|
|
1112
|
+
### Adding Custom Handlers
|
|
1081
1113
|
|
|
1082
1114
|
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.
|
|
1083
1115
|
|
|
1084
|
-
|
|
1116
|
+
|
|
1117
|
+
<a id="orge15889c"></a>
|
|
1118
|
+
|
|
1119
|
+
## Windows Based Servers
|
|
1085
1120
|
|
|
1086
1121
|
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.
|
|
1087
1122
|
|
|
1088
1123
|
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.
|
|
1089
1124
|
|
|
1090
|
-
|
|
1125
|
+
|
|
1126
|
+
<a id="orgb38b894"></a>
|
|
1127
|
+
|
|
1128
|
+
## Don't Re-use SftpClient Objects
|
|
1091
1129
|
|
|
1092
1130
|
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.
|
|
1093
1131
|
|
|
1094
1132
|
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.
|
|
1095
1133
|
|
|
1096
|
-
# FAQ<a id="sec-6"></a>
|
|
1097
1134
|
|
|
1098
|
-
|
|
1135
|
+
<a id="org1e76649"></a>
|
|
1136
|
+
|
|
1137
|
+
# FAQ
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
<a id="orge3e4159"></a>
|
|
1141
|
+
|
|
1142
|
+
## Remote server drops connections with only an end event
|
|
1099
1143
|
|
|
1100
1144
|
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.
|
|
1101
1145
|
|
|
@@ -1103,16 +1147,16 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
|
|
|
1103
1147
|
|
|
1104
1148
|
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.
|
|
1105
1149
|
|
|
1106
|
-
|
|
1150
|
+
|
|
1151
|
+
<a id="org77ed2d3"></a>
|
|
1152
|
+
|
|
1153
|
+
## How can I pass writeable stream as dst for get method?
|
|
1107
1154
|
|
|
1108
1155
|
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()`.
|
|
1109
1156
|
|
|
1110
1157
|
The writeable stream can be any type of write stream. For example, the below code will convert all the characters in the remote file to upper case before it is saved to the local file system. This could just as easily be something like a gunzip stream from `zlib`, enabling you to decompress remote zipped files as you bring them across before saving to local file system.
|
|
1111
1158
|
|
|
1112
1159
|
```javascript
|
|
1113
|
-
|
|
1114
|
-
'use
|
|
1115
|
-
|
|
1116
1160
|
'use strict';
|
|
1117
1161
|
|
|
1118
1162
|
// Example of using a writeable with get to retrieve a file.
|
|
@@ -1162,16 +1206,16 @@ sftp
|
|
|
1162
1206
|
});
|
|
1163
1207
|
```
|
|
1164
1208
|
|
|
1165
|
-
|
|
1209
|
+
|
|
1210
|
+
<a id="orgad09bd5"></a>
|
|
1211
|
+
|
|
1212
|
+
## How can I upload files without having to specify a password?
|
|
1166
1213
|
|
|
1167
1214
|
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.
|
|
1168
1215
|
|
|
1169
1216
|
One solution, provided by @KalleVuorjoki is to use the SSH agent process. **Note**: SSH<sub>AUTH</sub><sub>SOCK</sub> is normally created by your OS when you load the ssh-agent as part of the login session.
|
|
1170
1217
|
|
|
1171
1218
|
```javascript
|
|
1172
|
-
|
|
1173
|
-
let
|
|
1174
|
-
|
|
1175
1219
|
let sftp = new Client();
|
|
1176
1220
|
sftp.connect({
|
|
1177
1221
|
host: 'YOUR-HOST',
|
|
@@ -1186,9 +1230,6 @@ sftp.connect({
|
|
|
1186
1230
|
Another alternative is to just pass in the SSH key directly as part of the configuration.
|
|
1187
1231
|
|
|
1188
1232
|
```javascript
|
|
1189
|
-
|
|
1190
|
-
let
|
|
1191
|
-
|
|
1192
1233
|
let sftp = new Client();
|
|
1193
1234
|
sftp.connect({
|
|
1194
1235
|
host: 'YOUR-HOST',
|
|
@@ -1200,14 +1241,14 @@ sftp.connect({
|
|
|
1200
1241
|
}
|
|
1201
1242
|
```
|
|
1202
1243
|
|
|
1203
|
-
## How can I connect through a Socks Proxy<a id="sec-6-4"></a>
|
|
1204
1244
|
|
|
1205
|
-
|
|
1245
|
+
<a id="org175d696"></a>
|
|
1206
1246
|
|
|
1207
|
-
|
|
1247
|
+
## How can I connect through a Socks Proxy
|
|
1208
1248
|
|
|
1209
|
-
|
|
1249
|
+
This solution was provided by @jmorino.
|
|
1210
1250
|
|
|
1251
|
+
```javascript
|
|
1211
1252
|
import { SocksClient } from 'socks';
|
|
1212
1253
|
import SFTPClient from 'ssh2-sftp-client';
|
|
1213
1254
|
|
|
@@ -1236,71 +1277,84 @@ client.connect({
|
|
|
1236
1277
|
// client is connected
|
|
1237
1278
|
```
|
|
1238
1279
|
|
|
1239
|
-
|
|
1280
|
+
|
|
1281
|
+
<a id="orgd310a6a"></a>
|
|
1282
|
+
|
|
1283
|
+
## Timeout while waiting for handshake or handshake errors
|
|
1240
1284
|
|
|
1241
1285
|
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.
|
|
1242
1286
|
|
|
1243
1287
|
When encountering this type of problem, one worthwhile approach is to use openSSH's CLI sftp program with the `-v` switch to raise logging 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.
|
|
1244
1288
|
|
|
1245
|
-
|
|
1289
|
+
|
|
1290
|
+
<a id="org5a0faa2"></a>
|
|
1291
|
+
|
|
1292
|
+
## How can I limit upload/download speed
|
|
1246
1293
|
|
|
1247
1294
|
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.
|
|
1248
1295
|
|
|
1249
1296
|
```javascript
|
|
1250
1297
|
|
|
1251
1298
|
|
|
1299
|
+
const Throttle = require('throttle');
|
|
1300
|
+
const progress = require('progress-stream');
|
|
1252
1301
|
|
|
1302
|
+
// limit download speed
|
|
1303
|
+
const throttleStream = new Throttle(config.throttle);
|
|
1253
1304
|
|
|
1305
|
+
// download progress stream
|
|
1306
|
+
const progressStream = progress({
|
|
1307
|
+
length: fileSize,
|
|
1308
|
+
time: 500,
|
|
1309
|
+
});
|
|
1310
|
+
progressStream.on('progress', (progress) => {
|
|
1311
|
+
console.log(progress.percentage.toFixed(2));
|
|
1312
|
+
});
|
|
1254
1313
|
|
|
1314
|
+
const outStream = createWriteStream(localPath);
|
|
1255
1315
|
|
|
1316
|
+
// pipe streams together
|
|
1317
|
+
throttleStream.pipe(progressStream).pipe(outStream);
|
|
1256
1318
|
|
|
1319
|
+
try {
|
|
1320
|
+
// set autoClose to false
|
|
1321
|
+
await client.get(remotePath, throttleStream, { autoClose: false });
|
|
1322
|
+
} catch (e) {
|
|
1323
|
+
console.log('sftp error', e);
|
|
1324
|
+
} finally {
|
|
1325
|
+
await client.end();
|
|
1326
|
+
}
|
|
1327
|
+
```
|
|
1257
1328
|
|
|
1258
1329
|
|
|
1330
|
+
<a id="orgffe3a3c"></a>
|
|
1259
1331
|
|
|
1332
|
+
## Connection hangs or fails for larger files
|
|
1260
1333
|
|
|
1261
|
-
|
|
1262
|
-
const progress = require('progress-stream');
|
|
1334
|
+
This was contributed by Ladislav Jacho. Thanks.
|
|
1263
1335
|
|
|
1264
|
-
|
|
1265
|
-
const throttleStream = new Throttle(config.throttle);
|
|
1336
|
+
A symptom of this issue is that you are able to upload small files, but uploading larger ones fail. You probably have an MTU/fragmentation problem. For each network interface on both client and server set the MTU to 576, e.g. `ifconfig eth0 mtu 576`. If that works, you need to find the largest MTU which will work for your network. An MTU which is too small will adversely affect throughput speed. A common value to use is an MTU of 1400.
|
|
1266
1337
|
|
|
1267
|
-
|
|
1268
|
-
const progressStream = progress({
|
|
1269
|
-
length: fileSize,
|
|
1270
|
-
time: 500,
|
|
1271
|
-
});
|
|
1272
|
-
progressStream.on('progress', (progress) => {
|
|
1273
|
-
console.log(progress.percentage.toFixed(2));
|
|
1274
|
-
});
|
|
1338
|
+
For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
|
|
1275
1339
|
|
|
1276
|
-
const outStream = createWriteStream(localPath);
|
|
1277
1340
|
|
|
1278
|
-
|
|
1279
|
-
throttleStream.pipe(progressStream).pipe(outStream);
|
|
1341
|
+
<a id="org390645f"></a>
|
|
1280
1342
|
|
|
1281
|
-
|
|
1282
|
-
// set autoClose to false
|
|
1283
|
-
await client.get(remotePath, throttleStream, { autoClose: false });
|
|
1284
|
-
} catch (e) {
|
|
1285
|
-
console.log('sftp error', e);
|
|
1286
|
-
} finally {
|
|
1287
|
-
await client.end();
|
|
1288
|
-
}
|
|
1289
|
-
```
|
|
1343
|
+
## Typescript definition file out of date
|
|
1290
1344
|
|
|
1291
|
-
|
|
1345
|
+
This project does not use Typescript. However, typescript definition files are provided by other 3rd parties. Sometimes, these definition files have not stayed up-to-date with the current version of this module. If you encounter this issue, you need to report it to the party responsible for the definition file, not this project.
|
|
1292
1346
|
|
|
1293
|
-
This was contributed by Ladislav Jacho. Thanks.
|
|
1294
1347
|
|
|
1295
|
-
|
|
1348
|
+
<a id="orgce20a25"></a>
|
|
1296
1349
|
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
# Examples<a id="sec-7"></a>
|
|
1350
|
+
# Examples
|
|
1300
1351
|
|
|
1301
1352
|
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.
|
|
1302
1353
|
|
|
1303
|
-
|
|
1354
|
+
|
|
1355
|
+
<a id="org31d0059"></a>
|
|
1356
|
+
|
|
1357
|
+
# Troubleshooting
|
|
1304
1358
|
|
|
1305
1359
|
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.
|
|
1306
1360
|
|
|
@@ -1312,18 +1366,21 @@ Note also that in the repository there are two useful directories. The first is
|
|
|
1312
1366
|
|
|
1313
1367
|
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.
|
|
1314
1368
|
|
|
1315
|
-
## Common Errors<a id="sec-8-1"></a>
|
|
1316
1369
|
|
|
1317
|
-
|
|
1370
|
+
<a id="orgf1c2f29"></a>
|
|
1318
1371
|
|
|
1319
|
-
|
|
1372
|
+
## Common Errors
|
|
1320
1373
|
|
|
1321
|
-
|
|
1374
|
+
There are some common errors people tend to make when using Promises or Async/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.
|
|
1322
1375
|
|
|
1323
|
-
```javascript
|
|
1324
1376
|
|
|
1377
|
+
<a id="org14ab27b"></a>
|
|
1378
|
+
|
|
1379
|
+
### Not returning the promise in a `then()` block
|
|
1325
1380
|
|
|
1381
|
+
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
|
|
1326
1382
|
|
|
1383
|
+
```javascript
|
|
1327
1384
|
sftp.connect(config)
|
|
1328
1385
|
.then(() => {
|
|
1329
1386
|
sftp.fastGet('foo.txt', 'bar.txt');
|
|
@@ -1338,9 +1395,6 @@ sftp.connect(config)
|
|
|
1338
1395
|
In the above code, the `sftp.end()` method will almost certainly be called before `sftp.fastGet()` has been fulfilled (unless the *foo.txt* file is really small!). In fact, the whole promise chain will complete and exit even before the `sftp.end()` call has been fulfilled. The correct code would be something like
|
|
1339
1396
|
|
|
1340
1397
|
```javascript
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
1398
|
sftp.connect(config)
|
|
1345
1399
|
.then(() => {
|
|
1346
1400
|
return sftp.fastGet('foo.txt', 'bar.txt');
|
|
@@ -1356,14 +1410,14 @@ Note the `return` statements. These ensure that the Promise returned by the clie
|
|
|
1356
1410
|
|
|
1357
1411
|
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.
|
|
1358
1412
|
|
|
1359
|
-
### Mixing Promise Chains and Async/Await<a id="sec-8-1-2"></a>
|
|
1360
|
-
|
|
1361
|
-
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!)
|
|
1362
1413
|
|
|
1363
|
-
|
|
1414
|
+
<a id="org6c257e3"></a>
|
|
1364
1415
|
|
|
1416
|
+
### Mixing Promise Chains and Async/Await
|
|
1365
1417
|
|
|
1418
|
+
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!)
|
|
1366
1419
|
|
|
1420
|
+
```javascript
|
|
1367
1421
|
sftp.connect(config)
|
|
1368
1422
|
.then(() => {
|
|
1369
1423
|
return sftp.cwd();
|
|
@@ -1386,9 +1440,6 @@ The main bug in the above code is the `then()` block is not returning the Promis
|
|
|
1386
1440
|
Using async/await inside the promise chain has created unnecessary complexity and leads to incorrect assumptions regarding how the code will execute. A quick glance at the code is likely to give the impression that execution will wait for the `sftp.fastGet()` call to be fulfilled before continuing. This is not the case. The code would be more clearly expressed as either
|
|
1387
1441
|
|
|
1388
1442
|
```javascript
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
1443
|
sftp.connect(config)
|
|
1393
1444
|
.then(() => {
|
|
1394
1445
|
return sftp.cwd();
|
|
@@ -1405,9 +1456,6 @@ sftp.connect(config)
|
|
|
1405
1456
|
**or, using async/await**
|
|
1406
1457
|
|
|
1407
1458
|
```javascript
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
1459
|
async function doSftp() {
|
|
1412
1460
|
try {
|
|
1413
1461
|
let sftp = await sftp.connect(conf);
|
|
@@ -1416,13 +1464,16 @@ async function doSftp() {
|
|
|
1416
1464
|
await sftp.fastGet(`${d}/foo.txt`, 'bat.txt');
|
|
1417
1465
|
} catch (e) {
|
|
1418
1466
|
console.error(e.message);
|
|
1419
|
-
} finally
|
|
1467
|
+
} finally {
|
|
1420
1468
|
await sftp.end();
|
|
1421
1469
|
}
|
|
1422
1470
|
}
|
|
1423
1471
|
```
|
|
1424
1472
|
|
|
1425
|
-
|
|
1473
|
+
|
|
1474
|
+
<a id="org765b1b2"></a>
|
|
1475
|
+
|
|
1476
|
+
### Try/catch and Error Handlers
|
|
1426
1477
|
|
|
1427
1478
|
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.
|
|
1428
1479
|
|
|
@@ -1430,24 +1481,30 @@ The basic problem is that the try/catch block will have completed execution befo
|
|
|
1430
1481
|
|
|
1431
1482
|
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.
|
|
1432
1483
|
|
|
1433
|
-
|
|
1484
|
+
|
|
1485
|
+
<a id="orge67d910"></a>
|
|
1486
|
+
|
|
1487
|
+
### Server Differences
|
|
1434
1488
|
|
|
1435
1489
|
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.
|
|
1436
1490
|
|
|
1437
|
-
|
|
1491
|
+
|
|
1492
|
+
<a id="org734662f"></a>
|
|
1493
|
+
|
|
1494
|
+
### Avoid Concurrent Operations
|
|
1438
1495
|
|
|
1439
1496
|
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.
|
|
1440
1497
|
|
|
1441
1498
|
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.
|
|
1442
1499
|
|
|
1443
|
-
## Debugging Support<a id="sec-8-2"></a>
|
|
1444
1500
|
|
|
1445
|
-
|
|
1501
|
+
<a id="org658f3d6"></a>
|
|
1446
1502
|
|
|
1447
|
-
|
|
1503
|
+
## Debugging Support
|
|
1448
1504
|
|
|
1449
|
-
config.
|
|
1505
|
+
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;
|
|
1450
1506
|
|
|
1507
|
+
```javascript
|
|
1451
1508
|
config.debug = msg => {
|
|
1452
1509
|
console.error(msg);
|
|
1453
1510
|
};
|
|
@@ -1457,23 +1514,26 @@ config.debug = msg => {
|
|
|
1457
1514
|
Enabling debugging can generate a lot of output. If you use console.error() as the output (as in the example above), you can redirect the output to a file using shell redirection e.g.
|
|
1458
1515
|
|
|
1459
1516
|
```shell
|
|
1460
|
-
|
|
1461
|
-
node
|
|
1462
|
-
|
|
1463
1517
|
node script.js 2> debug.log
|
|
1464
1518
|
|
|
1465
1519
|
```
|
|
1466
1520
|
|
|
1467
1521
|
If you just want to see debug messages from `ssh2-sftp-client` and exclude debug messages from the underlying `ssh2` and `ssh2-streams` modules, you can filter based on messages which start with 'CLIENT' e.g.
|
|
1468
1522
|
|
|
1469
|
-
```
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1523
|
+
```javascript
|
|
1524
|
+
{
|
|
1525
|
+
debug: (msg) => {
|
|
1526
|
+
if (msg.startsWith('CLIENT')) {
|
|
1527
|
+
console.error(msg);
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1474
1531
|
```
|
|
1475
1532
|
|
|
1476
|
-
|
|
1533
|
+
|
|
1534
|
+
<a id="orgb0fbb63"></a>
|
|
1535
|
+
|
|
1536
|
+
# Logging Issues
|
|
1477
1537
|
|
|
1478
1538
|
Please log an issue for all bugs, questions, feature and enhancement requests. Please ensure you include the module version, node version and platform.
|
|
1479
1539
|
|
|
@@ -1487,7 +1547,10 @@ I am happy to try and help diagnose and fix any issues you encounter while using
|
|
|
1487
1547
|
|
|
1488
1548
|
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.
|
|
1489
1549
|
|
|
1490
|
-
|
|
1550
|
+
|
|
1551
|
+
<a id="orgc22fdba"></a>
|
|
1552
|
+
|
|
1553
|
+
# Pull Requests
|
|
1491
1554
|
|
|
1492
1555
|
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.
|
|
1493
1556
|
|
|
@@ -1496,10 +1559,15 @@ Pull requests are always welcomed. However, please ensure your changes pass all
|
|
|
1496
1559
|
This module will adopt a standard semantic versioning policy. Please indicate in your pull request what level of change it represents i.e.
|
|
1497
1560
|
|
|
1498
1561
|
- **Major:** Change to API or major change in functionality which will require an increase in major version number.
|
|
1562
|
+
|
|
1499
1563
|
- **Minor:** Minor change, enhancement or new feature which does not change existing API and will not break existing client code.
|
|
1564
|
+
|
|
1500
1565
|
- **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
|
|
1501
1566
|
|
|
1502
|
-
|
|
1567
|
+
|
|
1568
|
+
<a id="org7bb6675"></a>
|
|
1569
|
+
|
|
1570
|
+
# Contributors
|
|
1503
1571
|
|
|
1504
1572
|
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.
|
|
1505
1573
|
|
|
@@ -1518,4 +1586,4 @@ Thanks to the following for their contributions -
|
|
|
1518
1586
|
- **Emma Milner:** Contributed fix for put() bug
|
|
1519
1587
|
- **Witni Davis:** Contributed PR to fix put() RCE when using 'finish' rather than 'close' to resolve promise
|
|
1520
1588
|
- **Maik Marschner:** Contributed fix for connect() not returning sftp object. Also included test to check for this regression in future.
|
|
1521
|
-
- **cakemasher:** Contributed fix for removeTempListeners().
|
|
1589
|
+
- **cakemasher:** Contributed fix for removeTempListeners().
|