ssh2-sftp-client 5.1.1 → 5.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1076 -1561
- package/README.org +133 -48
- package/package.json +6 -9
- package/src/index.js +118 -40
- package/src/utils.js +41 -15
package/README.md
CHANGED
|
@@ -1,421 +1,266 @@
|
|
|
1
|
-
|
|
2
|
-
#
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
5.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
1
|
+
- [SSH2 SFTP Client](#sec-1)
|
|
2
|
+
- [Installation](#sec-2)
|
|
3
|
+
- [Basic Usage](#sec-3)
|
|
4
|
+
- [Version 5.x](#sec-4)
|
|
5
|
+
- [Breaking Changes in Version 5.x](#sec-4-1)
|
|
6
|
+
- [Error Event Handling](#sec-4-1-1)
|
|
7
|
+
- [Technical Details](#sec-4-1-2)
|
|
8
|
+
- [New Methods](#sec-4-2)
|
|
9
|
+
- [Version 5.0.1](#sec-4-3)
|
|
10
|
+
- [Version 5.0.2](#sec-4-4)
|
|
11
|
+
- [Version 5.1.0](#sec-4-5)
|
|
12
|
+
- [Version 5.1.1](#sec-4-6)
|
|
13
|
+
- [Version 5.1.2](#sec-4-7)
|
|
14
|
+
- [Version 5.1.3](#sec-4-8)
|
|
15
|
+
- [Version 5.2.0](#sec-4-9)
|
|
16
|
+
- [Version 5.2.1](#sec-4-10)
|
|
17
|
+
- [Documentation](#sec-5)
|
|
18
|
+
- [Specifying Paths](#sec-5-1)
|
|
19
|
+
- [Methods](#sec-5-2)
|
|
20
|
+
- [new SftpClient(name) ===> SFTP client object](#sec-5-2-1)
|
|
21
|
+
- [connect(config) ===> SFTPstream](#sec-5-2-2)
|
|
22
|
+
- [list(path, pattern) ==> Array[object]](#sec-5-2-3)
|
|
23
|
+
- [exists(path) ==> boolean](#sec-5-2-4)
|
|
24
|
+
- [stat(path) ==> object](#sec-5-2-5)
|
|
25
|
+
- [get(path, dst, options) ==> String|Stream|Buffer](#sec-5-2-6)
|
|
26
|
+
- [fastGet(remotePath, localPath, options) ===> string](#sec-5-2-7)
|
|
27
|
+
- [put(src, remotePath, options) ==> string](#sec-5-2-8)
|
|
28
|
+
- [fastPut(localPath, remotePath, options) ==> string](#sec-5-2-9)
|
|
29
|
+
- [append(input, remotePath, options) ==> string](#sec-5-2-10)
|
|
30
|
+
- [mkdir(path, recursive) ==> string](#sec-5-2-11)
|
|
31
|
+
- [rmdir(path, recursive) ==> string](#sec-5-2-12)
|
|
32
|
+
- [delete(path) ==> string](#sec-5-2-13)
|
|
33
|
+
- [rename(fromPath, toPath) ==> string](#sec-5-2-14)
|
|
34
|
+
- [posixRename(fromPath, toPath) ==> string](#sec-5-2-15)
|
|
35
|
+
- [chmod(path, mode) ==> string](#sec-5-2-16)
|
|
36
|
+
- [realPath(path) ===> string](#sec-5-2-17)
|
|
37
|
+
- [cwd() ==> string](#sec-5-2-18)
|
|
38
|
+
- [uploadDir(srcDir, dstDir) ==> string](#sec-5-2-19)
|
|
39
|
+
- [downloadDir(srcDir, dstDir) ==> string](#sec-5-2-20)
|
|
40
|
+
- [end() ==> boolean](#sec-5-2-21)
|
|
41
|
+
- [Add and Remove Listeners](#sec-5-2-22)
|
|
42
|
+
- [FAQ](#sec-6)
|
|
43
|
+
- [Remote server drops connections with only an end event](#sec-6-1)
|
|
44
|
+
- [How can you pass writable stream as dst for get method?](#sec-6-2)
|
|
45
|
+
- [How can I upload files without having to specify a password?](#sec-6-3)
|
|
46
|
+
- [How can I connect through a Socks Proxy](#sec-6-4)
|
|
47
|
+
- [Timeout while waiting for handshake or handshake errors](#sec-6-5)
|
|
48
|
+
- [Examples](#sec-7)
|
|
49
|
+
- [Change Log](#sec-8)
|
|
50
|
+
- [v5.2.1 (Prod Version)](#sec-8-1)
|
|
51
|
+
- [v5.2.0](#sec-8-2)
|
|
52
|
+
- [v5.1.3](#sec-8-3)
|
|
53
|
+
- [v5.1.2](#sec-8-4)
|
|
54
|
+
- [v5.1.1](#sec-8-5)
|
|
55
|
+
- [v5.1.0](#sec-8-6)
|
|
56
|
+
- [v5.0.2](#sec-8-7)
|
|
57
|
+
- [v5.0.1](#sec-8-8)
|
|
58
|
+
- [v5.0.0](#sec-8-9)
|
|
59
|
+
- [v4.3.1](#sec-8-10)
|
|
60
|
+
- [v4.3.0](#sec-8-11)
|
|
61
|
+
- [v4.2.4](#sec-8-12)
|
|
62
|
+
- [v4.2.3](#sec-8-13)
|
|
63
|
+
- [v4.2.2](#sec-8-14)
|
|
64
|
+
- [v4.2.1](#sec-8-15)
|
|
65
|
+
- [v4.2.0](#sec-8-16)
|
|
66
|
+
- [v4.1.0](#sec-8-17)
|
|
67
|
+
- [v4.0.4](#sec-8-18)
|
|
68
|
+
- [v4.0.3](#sec-8-19)
|
|
69
|
+
- [v4.0.2](#sec-8-20)
|
|
70
|
+
- [v4.0.0](#sec-8-21)
|
|
71
|
+
- [Older Versions](#sec-8-22)
|
|
72
|
+
- [v2.5.2](#sec-8-22-1)
|
|
73
|
+
- [v2.5.1](#sec-8-22-2)
|
|
74
|
+
- [v2.5.0](#sec-8-22-3)
|
|
75
|
+
- [v2.4.3](#sec-8-22-4)
|
|
76
|
+
- [v2.4.2](#sec-8-22-5)
|
|
77
|
+
- [v2.4.1](#sec-8-22-6)
|
|
78
|
+
- [v2.4.0](#sec-8-22-7)
|
|
79
|
+
- [v2.3.0](#sec-8-22-8)
|
|
80
|
+
- [v3.0.0 – deprecate this version](#sec-8-22-9)
|
|
81
|
+
- [v2.1.1](#sec-8-22-10)
|
|
82
|
+
- [v2.0.1](#sec-8-22-11)
|
|
83
|
+
- [v1.1.0](#sec-8-22-12)
|
|
84
|
+
- [v1.0.5:](#sec-8-22-13)
|
|
85
|
+
- [Troubleshooting](#sec-9)
|
|
86
|
+
- [Common Errors](#sec-9-1)
|
|
87
|
+
- [Not returning the promise in a `then()` block](#sec-9-1-1)
|
|
88
|
+
- [Mixing Promise Chains and Async/Await](#sec-9-1-2)
|
|
89
|
+
- [Try/catch and Error Handlers](#sec-9-1-3)
|
|
90
|
+
- [Debugging Support](#sec-9-2)
|
|
91
|
+
- [Logging Issues](#sec-10)
|
|
92
|
+
- [Pull Requests](#sec-11)
|
|
93
|
+
- [Contributors](#sec-12)
|
|
94
|
+
|
|
95
|
+
# SSH2 SFTP Client<a id="sec-1"></a>
|
|
96
|
+
|
|
97
|
+
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.
|
|
98
|
+
|
|
99
|
+
Documentation on the methods and available options in the underlying modules can be found on the [SSH2](https://github.com/mscdex/ssh2) and [SSH2-STREAMS](https://github.com/mscdex/ssh2-streams/blob/master/SFTPStream.md) project pages.
|
|
100
|
+
|
|
101
|
+
Current stable release is **v5.2.1**.
|
|
102
|
+
|
|
103
|
+
Code has been tested against Node versions 12.18.2 and 13.14.0
|
|
102
104
|
|
|
103
105
|
Node versions < 10.x are not supported.
|
|
104
106
|
|
|
107
|
+
<span class="underline">WARNING</span> There is currently an issue with both the fastPut() and fastGet() methods when using Node versions greater than 14.0.0. This is a bug in the underlying ssh2-streams library and needs to be fixed upstream. The issue appears to be related to the concurrency operations of these two functions. A workaround is to set concurrency to 1 using the options object. Alternatively, use get() or put(), which do not use concurrency and which will provide the same performance as fastGet() or fastPut() when they are set to use a concurrency of 1. A bug report has been logged against the ssh2-streams library as [issue 156](https://github.com/mscdex/ssh2-streams/issues/156).
|
|
105
108
|
|
|
106
|
-
<a id="
|
|
109
|
+
# Installation<a id="sec-2"></a>
|
|
107
110
|
|
|
108
|
-
|
|
111
|
+
```shell
|
|
112
|
+
npm install ssh2-sftp-client
|
|
113
|
+
```
|
|
109
114
|
|
|
110
|
-
|
|
115
|
+
# Basic Usage<a id="sec-3"></a>
|
|
111
116
|
|
|
117
|
+
```javascript
|
|
118
|
+
let Client = require('ssh2-sftp-client');
|
|
119
|
+
let sftp = new Client();
|
|
112
120
|
|
|
113
|
-
|
|
121
|
+
sftp.connect({
|
|
122
|
+
host: '127.0.0.1',
|
|
123
|
+
port: '8080',
|
|
124
|
+
username: 'username',
|
|
125
|
+
password: '******'
|
|
126
|
+
}).then(() => {
|
|
127
|
+
return sftp.list('/pathname');
|
|
128
|
+
}).then(data => {
|
|
129
|
+
console.log(data, 'the data info');
|
|
130
|
+
}).catch(err => {
|
|
131
|
+
console.log(err, 'catch error');
|
|
132
|
+
});
|
|
133
|
+
```
|
|
114
134
|
|
|
115
|
-
#
|
|
135
|
+
# Version 5.x<a id="sec-4"></a>
|
|
116
136
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}).then(data => {
|
|
128
|
-
console.log(data, 'the data info');
|
|
129
|
-
}).catch(err => {
|
|
130
|
-
console.log(err, 'catch error');
|
|
131
|
-
});
|
|
137
|
+
## Breaking Changes in Version 5.x<a id="sec-4-1"></a>
|
|
138
|
+
|
|
139
|
+
- The auxList() method has been removed. This method was flagged as deprecated in version 4.x. The functionality provided by `auxList()` is available in `list()`, making `auxList()` unnecessary.
|
|
140
|
+
- The realPath() method now returns `''` if the path does not exist rather than throwing an exception.
|
|
141
|
+
- Improved error handling. The `ssh2` and `ssh2-streams` libraries use events to signal errors. Providing a clean Promise based API and managing these events can be challenging as an error event can fire at any time (including in-between the resolution of one promise and the commencement of another). As you cannot use `try/catch` blocks to reliably manage error events (for a similar reason - see Node's event documentation for details), a slightly more complex solution was required. See the section below on Error Event Handling for more details. In basic terms, a default handler is now used that will log the error and clear the SFTP connection if no Promise error handler has handled the error. This prevents the uncaughtException error and provides a reasonably clean way to deal with unexpected errors that fire in-between Promise execution activities.
|
|
142
|
+
- Ignore Errors during `end()` processing. At least one SFTP server (Azure SFTP) seems to generate an error in response to the `end()` call. As `end()` has been called, we don't really care if an error occurs provided the connection is closed. Therefore, a new default error listener for the `end()` method has been added that will simply ignore any errors which occur during a call to end the connection.
|
|
143
|
+
|
|
144
|
+
### Error Event Handling<a id="sec-4-1-1"></a>
|
|
145
|
+
|
|
146
|
+
Providing a clean Promise API for the SSH2 to manage basic SFTP functionality presents a couple of challenges for managing errors. The `SSH2` module uses events to communicate various state changes and error conditions. These events can fire at any time.
|
|
132
147
|
|
|
148
|
+
On the client side, we wrap basic SFTP actions in Javascript Promises, allowing clients to use either the standard Promise API or async/await to model SFTP interactions. Creating an SFTP connection returns a promise, which resolves if a connection is successfully established and is rejected otherwise. Downloading a file using `get()` or `fastGet()` generates a new Promise which is either resolved, indicating file has been successfully downloaded or rejected, indicating the download failed. All pretty straight-forward.
|
|
133
149
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
<a id="orgeddc8c4"></a>
|
|
140
|
-
|
|
141
|
-
## Breaking Changes in Version 5.x
|
|
142
|
-
|
|
143
|
-
- The auxList() method has been removed. This method was flagged as deprecated
|
|
144
|
-
in version 4.x. The functionality provided by `auxList()` is available in
|
|
145
|
-
`list()`, making `auxList()` unnecessary.
|
|
146
|
-
- The realPath() method now returns `''` if the path does not exist
|
|
147
|
-
rather than throwing an exception.
|
|
148
|
-
- Improved error handling. The `ssh2` and `ssh2-streams` libraries use events
|
|
149
|
-
to signal errors. Providing a clean Promise based API and managing these
|
|
150
|
-
events can be challenging as an error event can fire at any time (including
|
|
151
|
-
in-between the resolution of one promise and the commencement of another). As
|
|
152
|
-
you cannot use `try/catch` blocks to reliably manage error events (for a
|
|
153
|
-
similar reason - see Node's event documentation for details), a slightly
|
|
154
|
-
more complex solution was required. See the section below on Error Event
|
|
155
|
-
Handling for more details. In basic terms, a default handler is now used
|
|
156
|
-
that will log the error and clear the SFTP connection if no Promise error
|
|
157
|
-
handler has handled the error. This prevents the uncaughtException error
|
|
158
|
-
and provides a reasonably clean way to deal with unexpected errors that
|
|
159
|
-
fire in-between Promise execution activities.
|
|
160
|
-
- Ignore Errors during `end()` processing. At least one SFTP server (Azure
|
|
161
|
-
SFTP) seems to generate an error in response to the `end()` call. As
|
|
162
|
-
`end()` has been called, we don't really care if an error occurs provided
|
|
163
|
-
the connection is closed. Therefore, a new default error listener for the
|
|
164
|
-
`end()` method has been added that will simply ignore any errors which
|
|
165
|
-
occur during a call to end the connection.
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
<a id="orgf624683"></a>
|
|
169
|
-
|
|
170
|
-
### Error Event Handling
|
|
171
|
-
|
|
172
|
-
Providing a clean Promise API for the SSH2 to manage basic SFTP functionality
|
|
173
|
-
presents a couple of challenges for managing errors. The `SSH2` module uses
|
|
174
|
-
events to communicate various state changes and error conditions. These
|
|
175
|
-
events can fire at any time.
|
|
176
|
-
|
|
177
|
-
On the client side, we wrap basic SFTP actions in Javascript Promises,
|
|
178
|
-
allowing clients to use either the standard Promise API or async/await to
|
|
179
|
-
model SFTP interactions. Creating an SFTP connection returns a promise, which
|
|
180
|
-
resolves if a connection is successfully established and is rejected
|
|
181
|
-
otherwise. Downloading a file using `get()` or `fastGet()` generates a new
|
|
182
|
-
Promise which is either resolved, indicating file has been successfully
|
|
183
|
-
downloaded or rejected, indicating the download failed. All pretty
|
|
184
|
-
straight-forward.
|
|
185
|
-
|
|
186
|
-
When the Promise is created, an error event handler is added to the SFTP
|
|
187
|
-
object to catch any errors that fire during the execution of the promise. If
|
|
188
|
-
an error event fires, the Promise is rejected and the error returned to the
|
|
189
|
-
client as part of the rejection. After the Promise has resolved or rejected,
|
|
190
|
-
the error listener is removed (the error listener is specific to each promise
|
|
191
|
-
because it needs to call the reject method associated with that promise). As
|
|
192
|
-
a promise can only be resolved or rejected once, after the Promise has
|
|
193
|
-
completed, the error listener is of no further use.
|
|
194
|
-
|
|
195
|
-
This all works fine when an error event fires during the execution of a
|
|
196
|
-
Promise. However, what about outside promise execution? Consider the
|
|
197
|
-
following flow;
|
|
150
|
+
When the Promise is created, an error event handler is added to the SFTP object to catch any errors that fire during the execution of the promise. If an error event fires, the Promise is rejected and the error returned to the client as part of the rejection. After the Promise has resolved or rejected, the error listener is removed (the error listener is specific to each promise because it needs to call the reject method associated with that promise). As a promise can only be resolved or rejected once, after the Promise has completed, the error listener is of no further use.
|
|
151
|
+
|
|
152
|
+
This all works fine when an error event fires during the execution of a Promise. However, what about outside promise execution? Consider the following flow;
|
|
198
153
|
|
|
199
154
|
1. You have an active SFTP connection which you use to download a file
|
|
200
|
-
2. When you make the download request, a new Promise is created which will
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
3. You start processing the data downloaded. At this point, you still have an
|
|
204
|
-
open connection to the SFTP server, but you are not actively interacting
|
|
205
|
-
with it. There is no active Promise in play.
|
|
206
|
-
4. The remote SFTP server resets the connection for some reason, generating a
|
|
207
|
-
ECONNRESET error that is emitted as an error event.
|
|
208
|
-
|
|
209
|
-
What happens at this point? There is no active promise executing, so no
|
|
210
|
-
Promise specific error handler in play. Your script is off processing the
|
|
211
|
-
data from the previously downloaded file, so there is no currently executing
|
|
212
|
-
try/catch block around the SFTP client object. Basically, there is nothing
|
|
213
|
-
listening of any errors at this point. What will happen?
|
|
214
|
-
|
|
215
|
-
Well, basically, the error event will bubble up to the top level of the node
|
|
216
|
-
process context and cause an uncaughtException error, display the error and
|
|
217
|
-
dump a stack trace and cause the node process to exit. In basic terms, your
|
|
218
|
-
process will crash. Not a great outcome.
|
|
219
|
-
|
|
220
|
-
There are a number of things we can do to improve the situation. However,
|
|
221
|
-
nearly all of them have some drawbacks. We could -
|
|
222
|
-
|
|
223
|
-
- Add our own error handler. The `client.on()` method would allow you to add
|
|
224
|
-
your own error handler. This would provide a way to manage error events,
|
|
225
|
-
but you want to make sure you only handle error events not handled already
|
|
226
|
-
by the Promise error handlers. Worse yet, you cannot know before hand the
|
|
227
|
-
processing context of your script at the point the error event fires. This
|
|
228
|
-
means your error handling is likely to be complex and difficult to
|
|
229
|
-
manage. Worse yet, these types of errors are quite rare in most situations
|
|
230
|
-
and your now being required to add significant additional complexity to
|
|
231
|
-
deal with a rare edge case. However, sometimes, you just need to deal with
|
|
232
|
-
this sort of complexity and the `client.on()` method does give you that
|
|
233
|
-
option.
|
|
234
|
-
- Another alternative is to just add an uncaughtException handler to your Node
|
|
235
|
-
process object. This would also prevent node from dumping the error and
|
|
236
|
-
exiting abruptly. However, now you need to think about ALL the possible
|
|
237
|
-
uncaughtExceptions which might happen, not just those associated with the
|
|
238
|
-
SFTP client. Again, things are getting complicated for something which only
|
|
239
|
-
occurs occasionally. .
|
|
240
|
-
|
|
241
|
-
What we really want is a solution which will be simple for the majority of
|
|
242
|
-
clients, but provide additional power when needed. What we have done is add a
|
|
243
|
-
default error handler which will only take action if no Promise error handler
|
|
244
|
-
has fired. All the default error handler does is log the error to
|
|
245
|
-
console.error() and set the SFTP connection to undefined so that any further
|
|
246
|
-
attempts to use the connection will throw an error inside the Promise which
|
|
247
|
-
attempts to use it.
|
|
248
|
-
|
|
249
|
-
The advantage of this approach is that it stops the abrupt exiting of the
|
|
250
|
-
node script due to an uncaught exception error and provides a reasonable
|
|
251
|
-
outcome for most use cases. For example, in the scenario outlined above, if
|
|
252
|
-
an error event fires while your script is processing the data already
|
|
253
|
-
downloaded, it will not impact on your script immediately. An error will be
|
|
254
|
-
logged to console.error(), but your script will continue to run. Once you
|
|
255
|
-
have completed processing your data, if you attempt another SFTP call, it
|
|
256
|
-
will fail with an error about no available SFTP connections. As this will
|
|
257
|
-
occur within the context of interacting with the SFTP server, your script can
|
|
258
|
-
take appropriate action to resolve the issue (such as re-connecting to the
|
|
259
|
-
server). On the other hand, if after processing the file your done and just
|
|
260
|
-
want to end, then you can just ignore the error, perform any necessary
|
|
261
|
-
cleanup work and exit successfully.
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
<a id="orga4b6f9f"></a>
|
|
265
|
-
|
|
266
|
-
### Technical Details
|
|
267
|
-
|
|
268
|
-
The event handlers added by each Promise are added using the `prependListener()`
|
|
269
|
-
function. This ensures the handler is fired before any other error handlers
|
|
270
|
-
which may be defined. As part of the processing, these error handler set a flag
|
|
271
|
-
property `this.errorHandled` to true, indicating the error has been handled.
|
|
272
|
-
|
|
273
|
-
In addition to the Promise error handlers, there is a default error handler
|
|
274
|
-
which will fire after any Promise error handler. The default error handler looks
|
|
275
|
-
to see if the `this.errorHandler` flag is true. If it is, it knows the error has
|
|
276
|
-
been handled and it just resets it to false, taking no other action (so taht we
|
|
277
|
-
are ready for the next error). If the flag is false, the default handler knows
|
|
278
|
-
it must handle the error. In this case, the handler will log the error to
|
|
279
|
-
`console.error()`, will set the SFTP connection to undefined to prevent any
|
|
280
|
-
further attempts to use it and finally, ensure the `this.errorHandler` flag is
|
|
281
|
-
reset to false in preparation for the next error.
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
<a id="org27219e5"></a>
|
|
285
|
-
|
|
286
|
-
## New Methods
|
|
287
|
-
|
|
288
|
-
- Added the method uploadDir(). This method will upload a directory
|
|
289
|
-
(including any subdirectories) to the remote server. Only directories and
|
|
290
|
-
regular files are uploaded (no symbolic links, FIFOs, socket FDs etc). Will
|
|
291
|
-
overwrite existing files or directories, but will not delete any remote
|
|
292
|
-
files or directories.
|
|
293
|
-
- Added the method downloadDir(). this method will download a directory
|
|
294
|
-
(including any subdirectories) to the local file system. Only directories
|
|
295
|
-
and regular files are downloaded (no symbolic links, FIFOs, socket FDs
|
|
296
|
-
etc).. Will overwrite existing files or directories, but will not delete
|
|
297
|
-
any local files in the directories.
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
<a id="org39008c4"></a>
|
|
301
|
-
|
|
302
|
-
## Version 5.0.1
|
|
303
|
-
|
|
304
|
-
- The error checking was a little too stringent. The use of exist() to test
|
|
305
|
-
for file types had a problem when the user does not have read/execute
|
|
306
|
-
rights on the directory. Replaced with stat() method, which should avoid
|
|
307
|
-
this issue.
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
<a id="orgc1b8da4"></a>
|
|
311
|
-
|
|
312
|
-
## Version 5.0.2
|
|
155
|
+
2. When you make the download request, a new Promise is created which will resolve when the file is downloaded or be rejected if the download fails for some reason. The promise resolves successfully.
|
|
156
|
+
3. You start processing the data downloaded. At this point, you still have an open connection to the SFTP server, but you are not actively interacting with it. There is no active Promise in play.
|
|
157
|
+
4. The remote SFTP server resets the connection for some reason, generating a ECONNRESET error that is emitted as an error event.
|
|
313
158
|
|
|
314
|
-
|
|
315
|
-
- Fix path handling under win32. Paths were not being parsed correctly due to
|
|
316
|
-
the use of path.posix.parse() instead of path.parse().
|
|
159
|
+
What happens at this point? There is no active promise executing, so no Promise specific error handler in play. Your script is off processing the data from the previously downloaded file, so there is no currently executing try/catch block around the SFTP client object. Basically, there is nothing listening of any errors at this point. What will happen?
|
|
317
160
|
|
|
161
|
+
Well, basically, the error event will bubble up to the top level of the node process context and cause an uncaughtException error, display the error and dump a stack trace and cause the node process to exit. In basic terms, your process will crash. Not a great outcome.
|
|
318
162
|
|
|
319
|
-
|
|
163
|
+
There are a number of things we can do to improve the situation. However, nearly all of them have some drawbacks. We could -
|
|
320
164
|
|
|
321
|
-
|
|
165
|
+
- Add our own error handler. The `client.on()` method would allow you to add your own error handler. This would provide a way to manage error events, but you want to make sure you only handle error events not handled already by the Promise error handlers. Worse yet, you cannot know before hand the processing context of your script at the point the error event fires. This means your error handling is likely to be complex and difficult to manage. Worse yet, these types of errors are quite rare in most situations and your now being required to add significant additional complexity to deal with a rare edge case. However, sometimes, you just need to deal with this sort of complexity and the `client.on()` method does give you that option.
|
|
166
|
+
- Another alternative is to just add an uncaughtException handler to your Node process object. This would also prevent node from dumping the error and exiting abruptly. However, now you need to think about ALL the possible uncaughtExceptions which might happen, not just those associated with the SFTP client. Again, things are getting complicated for something which only occurs occasionally. .
|
|
322
167
|
|
|
323
|
-
|
|
324
|
-
- Add debugging support. Now adding a debug property to the connection
|
|
325
|
-
configuration object will enable debugging. The value of the debug property
|
|
326
|
-
should be a function which accepts a single string argument. Typically,
|
|
327
|
-
this function will send the value passed in to stderr or a file.
|
|
328
|
-
- Fix bug in checkRemotePath() relating to poor path specifications where
|
|
329
|
-
you cannot determine parent directory.
|
|
168
|
+
What we really want is a solution which will be simple for the majority of clients, but provide additional power when needed. What we have done is add a default error handler which will only take action if no Promise error handler has fired. All the default error handler does is log the error to console.error() and set the SFTP connection to undefined so that any further attempts to use the connection will throw an error inside the Promise which attempts to use it.
|
|
330
169
|
|
|
170
|
+
The advantage of this approach is that it stops the abrupt exiting of the node script due to an uncaught exception error and provides a reasonable outcome for most use cases. For example, in the scenario outlined above, if an error event fires while your script is processing the data already downloaded, it will not impact on your script immediately. An error will be logged to console.error(), but your script will continue to run. Once you have completed processing your data, if you attempt another SFTP call, it will fail with an error about no available SFTP connections. As this will occur within the context of interacting with the SFTP server, your script can take appropriate action to resolve the issue (such as re-connecting to the server). On the other hand, if after processing the file your done and just want to end, then you can just ignore the error, perform any necessary cleanup work and exit successfully.
|
|
331
171
|
|
|
332
|
-
<a id="
|
|
172
|
+
### Technical Details<a id="sec-4-1-2"></a>
|
|
333
173
|
|
|
334
|
-
|
|
174
|
+
The event handlers added by each Promise are added using the `prependListener()` function. This ensures the handler is fired before any other error handlers which may be defined. As part of the processing, these error handler set a flag property `this.errorHandled` to true, indicating the error has been handled.
|
|
335
175
|
|
|
336
|
-
|
|
337
|
-
connections can be unexpectedly closed without an accompanying error event.
|
|
338
|
-
As methods only looked for error events, the method promise wold never
|
|
339
|
-
fulfil and the method would appear to hang. Have now added close event
|
|
340
|
-
handlers to each method that will reject the promise if the connection is
|
|
341
|
-
closed unexpectedly.
|
|
342
|
-
- Missing return statement in connect method would result in the connect
|
|
343
|
-
method attempting to re-connect again after it had reached maximum connect
|
|
344
|
-
retries. Added the missing return statement.
|
|
345
|
-
- Added some more troubleshooting documentation. Numerous issues have been
|
|
346
|
-
raised that turn out to be due to client code failing to return Promises
|
|
347
|
-
inside promise chains. Common symptom is what appears to be truncated file
|
|
348
|
-
upload/download. What is really happening is that the end method is being
|
|
349
|
-
called before the transfer has completed.
|
|
176
|
+
In addition to the Promise error handlers, there is a default error handler which will fire after any Promise error handler. The default error handler looks to see if the `this.errorHandler` flag is true. If it is, it knows the error has been handled and it just resets it to false, taking no other action (so taht we are ready for the next error). If the flag is false, the default handler knows it must handle the error. In this case, the handler will log the error to `console.error()`, will set the SFTP connection to undefined to prevent any further attempts to use it and finally, ensure the `this.errorHandler` flag is reset to false in preparation for the next error.
|
|
350
177
|
|
|
178
|
+
## New Methods<a id="sec-4-2"></a>
|
|
351
179
|
|
|
352
|
-
|
|
180
|
+
- Added the method uploadDir(). This method will upload a directory (including any subdirectories) to the remote server. Only directories and regular files are uploaded (no symbolic links, FIFOs, socket FDs etc). Will overwrite existing files or directories, but will not delete any remote files or directories.
|
|
181
|
+
- Added the method downloadDir(). This method will download a directory (including any subdirectories) to the local file system. Only directories and regular files are downloaded (no symbolic links, FIFOs, socket FDs etc).. Will overwrite existing files or directories, but will not delete any local files in the directories.
|
|
182
|
+
- Added the method posixRename(). This method will use the POSIX atomic rename openSSH extension. As this is an extension to the SFTP protocol, not all servers will support this operation.
|
|
353
183
|
|
|
354
|
-
|
|
184
|
+
## Version 5.0.1<a id="sec-4-3"></a>
|
|
355
185
|
|
|
356
|
-
The
|
|
357
|
-
module. For full details, please see [SSH2 client methods](https://github.com/mscdex/ssh2#user-content-client-methods)
|
|
186
|
+
- The error checking was a little too stringent. The use of exist() to test for file types had a problem when the user does not have read/execute rights on the directory. Replaced with stat() method, which should avoid this issue.
|
|
358
187
|
|
|
359
|
-
|
|
360
|
-
`removeListener()`, which are typically only used in special use cases.
|
|
188
|
+
## Version 5.0.2<a id="sec-4-4"></a>
|
|
361
189
|
|
|
190
|
+
- Fix error in local directory tests due to missing await statement.
|
|
191
|
+
- Fix path handling under win32. Paths were not being parsed correctly due to the use of path.posix.parse() instead of path.parse().
|
|
192
|
+
|
|
193
|
+
## Version 5.1.0<a id="sec-4-5"></a>
|
|
194
|
+
|
|
195
|
+
- Add missing connection check in end() method
|
|
196
|
+
- Add debugging support. Now adding a debug property to the connection configuration object will enable debugging. The value of the debug property should be a function which accepts a single string argument. Typically, this function will send the value passed in to stderr or a file.
|
|
197
|
+
- Fix bug in checkRemotePath() relating to poor path specifications where you cannot determine parent directory.
|
|
362
198
|
|
|
363
|
-
<a id="
|
|
199
|
+
## Version 5.1.1<a id="sec-4-6"></a>
|
|
364
200
|
|
|
365
|
-
|
|
201
|
+
- Bug fix for unexpected close of connections. It would seem that a connections can be unexpectedly closed without an accompanying error event. As methods only looked for error events, the method promise wold never fulfil and the method would appear to hang. Have now added close event handlers to each method that will reject the promise if the connection is closed unexpectedly.
|
|
202
|
+
- Missing return statement in connect method would result in the connect method attempting to re-connect again after it had reached maximum connect retries. Added the missing return statement.
|
|
203
|
+
- Added some more troubleshooting documentation. Numerous issues have been raised that turn out to be due to client code failing to return Promises inside promise chains. Common symptom is what appears to be truncated file upload/download. What is really happening is that the end method is being called before the transfer has completed.
|
|
366
204
|
|
|
367
|
-
|
|
368
|
-
can be relative with a prefix of either `./` (relative to current remote
|
|
369
|
-
directory) or `../` (relative to parent of current remote directory) e.g.
|
|
370
|
-
`./relative/path/to/file` or `../relative/to/parent/file`. It is also possible
|
|
371
|
-
to do things like `../../../file` to specify the parent of the parent of the
|
|
372
|
-
parent of the current remote directory. The shell tilde (`~`) and common
|
|
373
|
-
environment variables like `$HOME` are NOT supported.
|
|
205
|
+
## Version 5.1.2<a id="sec-4-7"></a>
|
|
374
206
|
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
and how the SFTP server has been configured. When things don't seem to be
|
|
378
|
-
working as expected, it is often a good idea to verify your assumptions
|
|
379
|
-
regarding the remote directory and remote paths. One way to do this is to login
|
|
380
|
-
using a command line program like `sftp` or `lftp`.
|
|
207
|
+
- Mainly a bug fix. We needed to add back a global close listener to ensure the sftp object is unset whenever a close event occurs. As close events can occur outside main method calls, only having method based listeners was not sufficient.
|
|
208
|
+
- Also added a utils.dumpListeners() method, useful when debugging issues with listener 'leakage' due to failure to remove listeners when no longer required.
|
|
381
209
|
|
|
382
|
-
|
|
383
|
-
query the remote server to determine what the root path is and derive the
|
|
384
|
-
absolute path. Using absolute paths are therefore more efficient and likely more
|
|
385
|
-
robust.
|
|
210
|
+
## Version 5.1.3<a id="sec-4-8"></a>
|
|
386
211
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
212
|
+
- Fix issue with permissions for writing to root directory
|
|
213
|
+
- Cleanup tests to use less connections and eliminate need for test delays
|
|
214
|
+
- Bumped some dependencies to latest versions
|
|
390
215
|
|
|
391
|
-
|
|
216
|
+
## Version 5.2.0<a id="sec-4-9"></a>
|
|
392
217
|
|
|
393
|
-
|
|
394
|
-
|
|
218
|
+
- Add posixRename() method. This is an openssh extension added in openssh v4.8 and will only work on servers which support this extension.conflict
|
|
219
|
+
- Bumped through2 dependency version to 4.0.2
|
|
395
220
|
|
|
396
|
-
|
|
221
|
+
## Version 5.2.1<a id="sec-4-10"></a>
|
|
397
222
|
|
|
398
|
-
|
|
399
|
-
name. The following works fine;
|
|
223
|
+
- Move some dev dependencies from dependencies to devDependencies.
|
|
400
224
|
|
|
401
|
-
|
|
225
|
+
# Documentation<a id="sec-5"></a>
|
|
402
226
|
|
|
403
|
-
|
|
404
|
-
the directory `/remote/dir`.
|
|
227
|
+
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)
|
|
405
228
|
|
|
229
|
+
All the methods will return a Promise, except for `on()` and `removeListener()`, which are typically only used in special use cases.
|
|
406
230
|
|
|
407
|
-
<a id="
|
|
231
|
+
## Specifying Paths<a id="sec-5-1"></a>
|
|
408
232
|
|
|
409
|
-
|
|
233
|
+
All remote paths must either be absolute e.g. `/absolute/path/to/file` or they can be relative with a prefix of either `./` (relative to current remote directory) or `../` (relative to parent of current remote directory) e.g. `./relative/path/to/file` or `../relative/to/parent/file`. It is also possible to do things like `../../../file` to specify the parent of the parent of the parent of the current remote directory. The shell tilde (`~`) and common environment variables like `$HOME` are NOT supported.
|
|
410
234
|
|
|
235
|
+
It is important to recognise that the current remote directory may not always be what you may expect. A lot will depend on the remote platform of the SFTP server and how the SFTP server has been configured. When things don't seem to be working as expected, it is often a good idea to verify your assumptions regarding the remote directory and remote paths. One way to do this is to login using a command line program like `sftp` or `lftp`.
|
|
411
236
|
|
|
412
|
-
|
|
237
|
+
There is a small performance hit for using `./` and `../` as the module must query the remote server to determine what the root path is and derive the absolute path. Using absolute paths are therefore more efficient and likely more robust.
|
|
413
238
|
|
|
414
|
-
|
|
239
|
+
When specifying file paths, ensure to include a full path i.e. include the remote filename. Don't expect the module to append the local file name to the path you provide. For example, the following will not work
|
|
415
240
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
241
|
+
```javascript
|
|
242
|
+
client.put('/home/fred/test.txt', '/remote/dir');
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
will not result in the file `test.txt` being copied to `/remote/dir/test.txt`. You need to specify the target filename as well e.g.
|
|
246
|
+
|
|
247
|
+
```javascript
|
|
248
|
+
client.put('/home/fred/test.txt', '/remote/dir/test.txt');
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
Note that the remote file name does not have to be the same as the local file name. The following works fine;
|
|
252
|
+
|
|
253
|
+
```javascript
|
|
254
|
+
client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
|
|
258
|
+
|
|
259
|
+
## Methods<a id="sec-5-2"></a>
|
|
260
|
+
|
|
261
|
+
### new SftpClient(name) ===> SFTP client object<a id="sec-5-2-1"></a>
|
|
262
|
+
|
|
263
|
+
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.
|
|
419
264
|
|
|
420
265
|
1. Constructor Arguments
|
|
421
266
|
|
|
@@ -423,212 +268,196 @@ client has thrown the error.
|
|
|
423
268
|
|
|
424
269
|
2. Example Use
|
|
425
270
|
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
<a id="org4ae0479"></a>
|
|
271
|
+
```javascript
|
|
272
|
+
'use strict';
|
|
273
|
+
|
|
274
|
+
const Client = require('ssh2-sftp-client');
|
|
275
|
+
|
|
276
|
+
const config = {
|
|
277
|
+
host: 'example.com',
|
|
278
|
+
username: 'donald',
|
|
279
|
+
password: 'my-secret'
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
const sftp = new Client('example-client');
|
|
283
|
+
|
|
284
|
+
sftp.connect(config)
|
|
285
|
+
.then(() => {
|
|
286
|
+
return sftp.cwd();
|
|
287
|
+
})
|
|
288
|
+
.then(p => {
|
|
289
|
+
console.log(`Remote working directory is ${p}`);
|
|
290
|
+
return sftp.end();
|
|
291
|
+
})
|
|
292
|
+
.catch(err => {
|
|
293
|
+
console.log(`Error: ${err.message}`); // error message will include 'example-client'
|
|
294
|
+
});
|
|
295
|
+
```
|
|
452
296
|
|
|
453
|
-
### connect(config) ===> SFTPstream
|
|
297
|
+
### connect(config) ===> SFTPstream<a id="sec-5-2-2"></a>
|
|
454
298
|
|
|
455
|
-
Connect to an sftp server. Full documentation for connection options is
|
|
456
|
-
available [here](https://github.com/mscdex/ssh2#user-content-client-methods)
|
|
299
|
+
Connect to an sftp server. Full documentation for connection options is available [here](https://github.com/mscdex/ssh2#user-content-client-methods)
|
|
457
300
|
|
|
458
301
|
1. Connection Options
|
|
459
302
|
|
|
460
|
-
This module is based on the excellent [SSH2](https://github.com/mscdex/ssh2#client) module. That module is a general SSH2
|
|
461
|
-
client and server library and provides much more functionality than just SFTP
|
|
462
|
-
connectivity. Many of the connect options provided by that module are less
|
|
463
|
-
relevant for SFTP connections. It is recommended you keep the config options to
|
|
464
|
-
the minimum needed and stick to the options listed in the `commonOpts` below.
|
|
303
|
+
This module is based on the excellent [SSH2](https://github.com/mscdex/ssh2#client) module. That module is a general SSH2 client and server library and provides much more functionality than just SFTP connectivity. Many of the connect options provided by that module are less relevant for SFTP connections. It is recommended you keep the config options to the minimum needed and stick to the options listed in the `commonOpts` below.
|
|
465
304
|
|
|
466
|
-
The `retries`, `retry_factor` and `retry_minTimeout` options are not part of the
|
|
467
|
-
SSH2 module. These are part of the configuration for the [retry](https://www.npmjs.com/package/retry) package and what
|
|
468
|
-
is used to enable retrying of sftp connection attempts. See the documentation
|
|
469
|
-
for that package for an explanation of these values.
|
|
305
|
+
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.
|
|
470
306
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
307
|
+
```javascript
|
|
308
|
+
// common options
|
|
309
|
+
|
|
310
|
+
let commonOpts {
|
|
311
|
+
host: 'localhost', // string Hostname or IP of server.
|
|
312
|
+
port: 22, // Port number of the server.
|
|
313
|
+
forceIPv4: false, // boolean (optional) Only connect via IPv4 address
|
|
314
|
+
forceIPv6: false, // boolean (optional) Only connect via IPv6 address
|
|
315
|
+
username: 'donald', // string Username for authentication.
|
|
316
|
+
password: 'borsch', // string Password for password-based user authentication
|
|
317
|
+
agent: process.env.SSH_AGENT, // string - Path to ssh-agent's UNIX socket
|
|
318
|
+
privateKey: fs.readFileSync('/path/to/key'), // Buffer or string that contains
|
|
319
|
+
passphrase; 'a pass phrase', // string - For an encrypted private key
|
|
320
|
+
readyTimeout: 20000, // integer How long (in ms) to wait for the SSH handshake
|
|
321
|
+
strictVendor: true // boolean - Performs a strict server vendor check
|
|
322
|
+
debug: myDebug // function - Set this to a function that receives a single
|
|
323
|
+
// string argument to get detailed (local) debug information.
|
|
324
|
+
retries: 2 // integer. Number of times to retry connecting
|
|
325
|
+
retry_factor: 2 // integer. Time factor used to calculate time between retries
|
|
326
|
+
retry_minTimeout: 2000 // integer. Minimum timeout between attempts
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
// rarely used options
|
|
330
|
+
|
|
331
|
+
let advancedOpts {
|
|
332
|
+
localAddress,
|
|
333
|
+
localPort,
|
|
334
|
+
hostHash,
|
|
335
|
+
hostVerifier,
|
|
336
|
+
agentForward,
|
|
337
|
+
localHostname,
|
|
338
|
+
localUsername,
|
|
339
|
+
tryKeyboard,
|
|
340
|
+
authHandler,
|
|
341
|
+
keepaliveInterval,
|
|
342
|
+
keepaliveCountMax,
|
|
343
|
+
sock,
|
|
344
|
+
algorithms,
|
|
345
|
+
compress
|
|
346
|
+
};
|
|
347
|
+
```
|
|
510
348
|
|
|
511
349
|
2. Example Use
|
|
512
350
|
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
<a id="orgf4b0036"></a>
|
|
351
|
+
```javascript
|
|
352
|
+
sftp.connect({
|
|
353
|
+
host: example.com,
|
|
354
|
+
port: 22,
|
|
355
|
+
username: 'donald',
|
|
356
|
+
password: 'youarefired'
|
|
357
|
+
});
|
|
358
|
+
```
|
|
522
359
|
|
|
523
|
-
### list(path, pattern) ==> Array[object]
|
|
360
|
+
### list(path, pattern) ==> Array[object]<a id="sec-5-2-3"></a>
|
|
524
361
|
|
|
525
|
-
Retrieves a directory listing. This method returns a Promise, which once
|
|
526
|
-
realised, returns an array of objects representing items in the remote
|
|
527
|
-
directory.
|
|
362
|
+
Retrieves a directory listing. This method returns a Promise, which once realised, returns an array of objects representing items in the remote directory.
|
|
528
363
|
|
|
529
364
|
- **path:** {String} Remote directory path
|
|
530
|
-
- **pattern:** (optional) {string|RegExp} A pattern used to filter the items included in the returned
|
|
531
|
-
array. Pattern can be a simple *glob*-style string or a regular
|
|
532
|
-
expression. Defaults to `/.*/`.
|
|
365
|
+
- **pattern:** (optional) {string|RegExp} A pattern used to filter the items included in the returned array. Pattern can be a simple *glob*-style string or a regular expression. Defaults to `/.*/`.
|
|
533
366
|
|
|
534
367
|
1. Example Use
|
|
535
368
|
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
369
|
+
```javascript
|
|
370
|
+
const Client = require('ssh2-sftp-client');
|
|
371
|
+
|
|
372
|
+
const config = {
|
|
373
|
+
host: 'example.com',
|
|
374
|
+
port: 22,
|
|
375
|
+
username: 'red-don',
|
|
376
|
+
password: 'my-secret'
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
let sftp = new Client;
|
|
380
|
+
|
|
381
|
+
sftp.connect(config)
|
|
382
|
+
.then(() => {
|
|
383
|
+
return sftp.list('/path/to/remote/dir');
|
|
384
|
+
})
|
|
385
|
+
.then(data => {
|
|
386
|
+
console.log(data);
|
|
387
|
+
})
|
|
388
|
+
.then(() => {
|
|
389
|
+
sftp.end();
|
|
390
|
+
})
|
|
391
|
+
.catch(err => {
|
|
392
|
+
console.error(err.message);
|
|
393
|
+
});
|
|
394
|
+
```
|
|
560
395
|
|
|
561
396
|
2. Return Objects
|
|
562
397
|
|
|
563
398
|
The objects in the array returned by `list()` have the following properties;
|
|
564
399
|
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
400
|
+
```javascript
|
|
401
|
+
{
|
|
402
|
+
type: // file type(-, d, l)
|
|
403
|
+
name: // file name
|
|
404
|
+
size: // file size
|
|
405
|
+
modifyTime: // file timestamp of modified time
|
|
406
|
+
accessTime: // file timestamp of access time
|
|
407
|
+
rights: {
|
|
408
|
+
user:
|
|
409
|
+
group:
|
|
410
|
+
other:
|
|
411
|
+
},
|
|
412
|
+
owner: // user ID
|
|
413
|
+
group: // group ID
|
|
414
|
+
}
|
|
415
|
+
```
|
|
579
416
|
|
|
580
417
|
3. Pattern Filter
|
|
581
418
|
|
|
582
|
-
The filter options can be a regular expression (most powerful option) or a
|
|
583
|
-
simple *glob*-like string where \* will match any number of characters, e.g.
|
|
419
|
+
The filter options can be a regular expression (most powerful option) or a simple *glob*-like string where \* will match any number of characters, e.g.
|
|
584
420
|
|
|
585
421
|
foo* => foo, foobar, foobaz
|
|
586
422
|
*bar => bar, foobar, tabbar
|
|
587
423
|
*oo* => foo, foobar, look, book
|
|
588
424
|
|
|
589
|
-
The *glob*-style matching is very simple. In most cases, you are best off using
|
|
590
|
-
a real regular expression which will allow you to do more powerful matching and
|
|
591
|
-
anchor matches to the beginning/end of the string etc.
|
|
425
|
+
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.
|
|
592
426
|
|
|
427
|
+
### exists(path) ==> boolean<a id="sec-5-2-4"></a>
|
|
593
428
|
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
### exists(path) ==> boolean
|
|
597
|
-
|
|
598
|
-
Tests to see if remote file or directory exists. Returns type of remote object
|
|
599
|
-
if it exists or false if it does not.
|
|
429
|
+
Tests to see if remote file or directory exists. Returns type of remote object if it exists or false if it does not.
|
|
600
430
|
|
|
601
431
|
1. Example Use
|
|
602
432
|
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
### stat(path) ==> object
|
|
433
|
+
```javascript
|
|
434
|
+
const Client = require('ssh2-sftp-client');
|
|
435
|
+
|
|
436
|
+
const config = {
|
|
437
|
+
host: 'example.com',
|
|
438
|
+
port: 22,
|
|
439
|
+
username: 'red-don',
|
|
440
|
+
password: 'my-secret'
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
let sftp = new Client;
|
|
444
|
+
|
|
445
|
+
sftp.connect(config)
|
|
446
|
+
.then(() => {
|
|
447
|
+
return sftp.exists('/path/to/remote/dir');
|
|
448
|
+
})
|
|
449
|
+
.then(data => {
|
|
450
|
+
console.log(data); // will be false or d, -, l (dir, file or link)
|
|
451
|
+
})
|
|
452
|
+
.then(() => {
|
|
453
|
+
sftp.end();
|
|
454
|
+
})
|
|
455
|
+
.catch(err => {
|
|
456
|
+
console.error(err.message);
|
|
457
|
+
});
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
### stat(path) ==> object<a id="sec-5-2-5"></a>
|
|
632
461
|
|
|
633
462
|
Returns the attributes associated with the object pointed to by `path`.
|
|
634
463
|
|
|
@@ -638,204 +467,180 @@ Returns the attributes associated with the object pointed to by `path`.
|
|
|
638
467
|
|
|
639
468
|
The `stat()` method returns an object with the following properties;
|
|
640
469
|
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
470
|
+
```javascript
|
|
471
|
+
let stats = {
|
|
472
|
+
mode: 33279, // integer representing type and permissions
|
|
473
|
+
uid: 1000, // user ID
|
|
474
|
+
gid: 985, // group ID
|
|
475
|
+
size: 5, // file size
|
|
476
|
+
accessTime: 1566868566000, // Last access time. milliseconds
|
|
477
|
+
modifyTime: 1566868566000, // last modify time. milliseconds
|
|
478
|
+
isDirectory: false, // true if object is a directory
|
|
479
|
+
isFile: true, // true if object is a file
|
|
480
|
+
isBlockDevice: false, // true if object is a block device
|
|
481
|
+
isCharacterDevice: false, // true if object is a character device
|
|
482
|
+
isSymbolicLink: false, // true if object is a symbolic link
|
|
483
|
+
isFIFO: false, // true if object is a FIFO
|
|
484
|
+
isSocket: false // true if object is a socket
|
|
485
|
+
};
|
|
486
|
+
```
|
|
656
487
|
|
|
657
488
|
2. Example Use
|
|
658
489
|
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
string, it is interpreted as the path to a location on the local file system
|
|
683
|
-
(path should include the file name). If it is a stream object, the remote data
|
|
684
|
-
is passed to it via a call to pipe(). If `dst` is undefined, the method will put
|
|
685
|
-
the data into a buffer and return that buffer when the Promise is resolved. If
|
|
686
|
-
`dst` is defined, it is returned when the Promise is resolved.
|
|
687
|
-
|
|
688
|
-
In general, if your going to pass in a string as the destination, you are
|
|
689
|
-
better off using the `fastGet()` method.
|
|
490
|
+
```javascript
|
|
491
|
+
let client = new Client();
|
|
492
|
+
|
|
493
|
+
client.connect(config)
|
|
494
|
+
.then(() => {
|
|
495
|
+
return client.stat('/path/to/remote/file');
|
|
496
|
+
})
|
|
497
|
+
.then(data => {
|
|
498
|
+
// do something with data
|
|
499
|
+
})
|
|
500
|
+
.then(() => {
|
|
501
|
+
client.end();
|
|
502
|
+
})
|
|
503
|
+
.catch(err => {
|
|
504
|
+
console.error(err.message);
|
|
505
|
+
});
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
### get(path, dst, options) ==> String|Stream|Buffer<a id="sec-5-2-6"></a>
|
|
509
|
+
|
|
510
|
+
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.
|
|
511
|
+
|
|
512
|
+
In general, if your going to pass in a string as the destination, you are better off using the `fastGet()` method.
|
|
690
513
|
|
|
691
514
|
- **path:** String. Path to the remote file to download
|
|
692
|
-
- **dst:** String|Stream. Destination for the data. If a string, it
|
|
693
|
-
should be a local file path.
|
|
515
|
+
- **dst:** String|Stream. Destination for the data. If a string, it should be a local file path.
|
|
694
516
|
- **options:** Options for the `get()` command (see below).
|
|
695
517
|
|
|
696
518
|
1. Options
|
|
697
519
|
|
|
698
|
-
The options object can be used to pass options to the underlying readStream used
|
|
699
|
-
to read the data from the remote server.
|
|
520
|
+
The options object can be used to pass options to the underlying readStream used to read the data from the remote server.
|
|
700
521
|
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
522
|
+
```javascript
|
|
523
|
+
{
|
|
524
|
+
flags: 'r',
|
|
525
|
+
encoding: null,
|
|
526
|
+
handle: null,
|
|
527
|
+
mode: 0o666,
|
|
528
|
+
autoClose: true
|
|
529
|
+
}
|
|
530
|
+
```
|
|
708
531
|
|
|
709
|
-
Most of the time, you won't want to use any options. Sometimes, it may be useful
|
|
710
|
-
to set the encoding. For example, to 'utf-8'. However, it is important not to do
|
|
711
|
-
this for binary files to avoid data corruption.
|
|
532
|
+
Most of the time, you won't want to use any options. Sometimes, it may be useful to set the encoding. For example, to 'utf-8'. However, it is important not to do this for binary files to avoid data corruption.
|
|
712
533
|
|
|
713
534
|
2. Example Use
|
|
714
535
|
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
let remotePath = '/remote/server/path/file.txt';
|
|
718
|
-
let dst = fs.createWriteStream('/local/file/path/copy.txt');
|
|
719
|
-
|
|
720
|
-
client.connect(config)
|
|
721
|
-
.then(() => {
|
|
722
|
-
return client.get(remotePath, dst);
|
|
723
|
-
})
|
|
724
|
-
.then(() => {
|
|
725
|
-
client.end();
|
|
726
|
-
})
|
|
727
|
-
.catch(err => {
|
|
728
|
-
console.error(err.message);
|
|
729
|
-
});
|
|
536
|
+
```javascript
|
|
537
|
+
let client = new Client();
|
|
730
538
|
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
539
|
+
let remotePath = '/remote/server/path/file.txt';
|
|
540
|
+
let dst = fs.createWriteStream('/local/file/path/copy.txt');
|
|
541
|
+
|
|
542
|
+
client.connect(config)
|
|
543
|
+
.then(() => {
|
|
544
|
+
return client.get(remotePath, dst);
|
|
545
|
+
})
|
|
546
|
+
.then(() => {
|
|
547
|
+
client.end();
|
|
548
|
+
})
|
|
549
|
+
.catch(err => {
|
|
550
|
+
console.error(err.message);
|
|
551
|
+
});
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
- **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'.
|
|
738
555
|
|
|
739
|
-
### fastGet(remotePath, localPath, options) ===> string
|
|
556
|
+
### fastGet(remotePath, localPath, options) ===> string<a id="sec-5-2-7"></a>
|
|
740
557
|
|
|
741
|
-
Downloads a file at remotePath to localPath using parallel reads for faster
|
|
742
|
-
throughput. This is the simplest method if you just want to download a file.
|
|
558
|
+
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.
|
|
743
559
|
|
|
744
560
|
- **remotePath:** String. Path to the remote file to download
|
|
745
|
-
- **localPath:** String. Path on local file system for the downloaded file. The
|
|
746
|
-
local path should include the filename to use for saving the
|
|
747
|
-
file.
|
|
561
|
+
- **localPath:** String. Path on local file system for the downloaded file. The local path should include the filename to use for saving the file.
|
|
748
562
|
- **options:** Options for `fastGet()` (see below)
|
|
749
563
|
|
|
750
564
|
1. Options
|
|
751
565
|
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
566
|
+
```javascript
|
|
567
|
+
{
|
|
568
|
+
concurrency: 64, // integer. Number of concurrent reads to use
|
|
569
|
+
chunkSize: 32768, // integer. Size of each read in bytes
|
|
570
|
+
step: function(total_transferred, chunk, total) // callback called each time a
|
|
571
|
+
// chunk is transferred
|
|
572
|
+
}
|
|
573
|
+
```
|
|
758
574
|
|
|
759
|
-
- **Warning:** Some servers do not respond correctly to requests to alter chunk
|
|
760
|
-
size. This can result in lost or corrupted data.
|
|
575
|
+
- **Warning:** Some servers do not respond correctly to requests to alter chunk size. This can result in lost or corrupted data.
|
|
761
576
|
|
|
762
577
|
2. Sample Use
|
|
763
578
|
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
string
|
|
786
|
-
transfer. If the `src` argument is a buffer, the contents of the buffer are
|
|
787
|
-
copied to the remote file and if it is a readable stream, the contents of that
|
|
788
|
-
stream are piped to the `remotePath` on the server.
|
|
789
|
-
|
|
790
|
-
- **src:** string | buffer | readable stream. Data source for data to copy to the
|
|
791
|
-
remote server.
|
|
579
|
+
```javascript
|
|
580
|
+
let client = new Client();
|
|
581
|
+
let remotePath = '/server/path/file.txt';
|
|
582
|
+
let localPath = '/local/path/file.txt';
|
|
583
|
+
|
|
584
|
+
client.connect(config)
|
|
585
|
+
.then(() => {
|
|
586
|
+
client.fastGet(remotePath, localPath);
|
|
587
|
+
})
|
|
588
|
+
.then(() => {
|
|
589
|
+
client.end();
|
|
590
|
+
})
|
|
591
|
+
.catch(err => {
|
|
592
|
+
console.error(err.message);
|
|
593
|
+
});
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
### put(src, remotePath, options) ==> string<a id="sec-5-2-8"></a>
|
|
597
|
+
|
|
598
|
+
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.
|
|
599
|
+
|
|
600
|
+
- **src:** string | buffer | readable stream. Data source for data to copy to the remote server.
|
|
792
601
|
- **remotePath:** string. Path to the remote file to be created on the server.
|
|
793
|
-
- **options:** object. Options which can be passed to adjust the write stream used
|
|
794
|
-
in sending the data to the remote server (see below).
|
|
602
|
+
- **options:** object. Options which can be passed to adjust the write stream used in sending the data to the remote server (see below).
|
|
795
603
|
|
|
796
604
|
1. Options
|
|
797
605
|
|
|
798
606
|
The following options are supported;
|
|
799
607
|
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
608
|
+
```javascript
|
|
609
|
+
{
|
|
610
|
+
flags: 'w', // w - write and a - append
|
|
611
|
+
encoding: null, // use null for binary files
|
|
612
|
+
mode: 0o666, // mode to use for created file (rwx)
|
|
613
|
+
autoClose: true // automatically close the write stream when finished
|
|
614
|
+
}
|
|
615
|
+
```
|
|
806
616
|
|
|
807
|
-
The most common options to use are mode and encoding. The values shown above are
|
|
808
|
-
the defaults. You do not have to set encoding to utf-8 for text files, null is
|
|
809
|
-
fine for all file types. However, using utf-8 encoding for binary files will
|
|
810
|
-
often result in data corruption.
|
|
617
|
+
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.
|
|
811
618
|
|
|
812
619
|
2. Example Use
|
|
813
620
|
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
621
|
+
```javascript
|
|
622
|
+
let client = new Client();
|
|
623
|
+
|
|
624
|
+
let data = fs.createReadStream('/path/to/local/file.txt');
|
|
625
|
+
let remote = '/path/to/remote/file.txt';
|
|
626
|
+
|
|
627
|
+
client.connect(config)
|
|
628
|
+
.then(() => {
|
|
629
|
+
return client.put(data, remote);
|
|
630
|
+
})
|
|
631
|
+
.then(() => {
|
|
632
|
+
return client.end();
|
|
633
|
+
})
|
|
634
|
+
.catch(err => {
|
|
635
|
+
console.error(err.message);
|
|
636
|
+
});
|
|
637
|
+
```
|
|
829
638
|
|
|
830
639
|
- **Tip:** If the src argument is a path string, consider just using `fastPut()`.
|
|
831
640
|
|
|
641
|
+
### fastPut(localPath, remotePath, options) ==> string<a id="sec-5-2-9"></a>
|
|
832
642
|
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
### fastPut(localPath, remotePath, options) ==> string
|
|
836
|
-
|
|
837
|
-
Uploads the data in file at `localPath` to a new file on remote server at
|
|
838
|
-
`remotePath` using concurrency. The options object allows tweaking of the fast put process.
|
|
643
|
+
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.
|
|
839
644
|
|
|
840
645
|
- **localPath:** string. Path to local file to upload
|
|
841
646
|
- **remotePath:** string. Path to remote file to create
|
|
@@ -843,44 +648,40 @@ Uploads the data in file at `localPath` to a new file on remote server at
|
|
|
843
648
|
|
|
844
649
|
1. Options
|
|
845
650
|
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
651
|
+
```javascript
|
|
652
|
+
{
|
|
653
|
+
concurrency: 64, // integer. Number of concurrent reads
|
|
654
|
+
chunkSize: 32768, // integer. Size of each read in bytes
|
|
655
|
+
mode: 0o755, // mixed. Integer or string representing the file mode to set
|
|
656
|
+
step: function(total_transferred, chunk, total) // function. Called every time
|
|
657
|
+
// a part of a file was transferred
|
|
658
|
+
}
|
|
659
|
+
```
|
|
853
660
|
|
|
854
|
-
- **Warning:** There have been reports that some SFTP servers will not honour
|
|
855
|
-
requests for non-default chunk sizes. This can result in data loss
|
|
856
|
-
or corruption.
|
|
661
|
+
- **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.
|
|
857
662
|
|
|
858
663
|
2. Example Use
|
|
859
664
|
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
<a id="org705c90e"></a>
|
|
665
|
+
```javascript
|
|
666
|
+
let localFile = '/path/to/file.txt';
|
|
667
|
+
let remoteFile = '/path/to/remote/file.txt';
|
|
668
|
+
let client = new Client();
|
|
669
|
+
|
|
670
|
+
client.connect(config)
|
|
671
|
+
.then(() => {
|
|
672
|
+
client.fastPut(localFile, remoteFile);
|
|
673
|
+
})
|
|
674
|
+
.then(() => {
|
|
675
|
+
client.end();
|
|
676
|
+
})
|
|
677
|
+
.catch(err => {
|
|
678
|
+
console.error(err.message);
|
|
679
|
+
});
|
|
680
|
+
```
|
|
877
681
|
|
|
878
|
-
### append(input, remotePath, options) ==> string
|
|
682
|
+
### append(input, remotePath, options) ==> string<a id="sec-5-2-10"></a>
|
|
879
683
|
|
|
880
|
-
Append the `input` data to an existing remote file. There is no integrity
|
|
881
|
-
checking performed apart from normal writeStream checks. This function simply
|
|
882
|
-
opens a writeStream on the remote file in append mode and writes the data passed
|
|
883
|
-
in to the file.
|
|
684
|
+
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.
|
|
884
685
|
|
|
885
686
|
- **input:** buffer | readStream. Data to append to remote file
|
|
886
687
|
- **remotePath:** string. Path to remote file
|
|
@@ -890,96 +691,86 @@ in to the file.
|
|
|
890
691
|
|
|
891
692
|
The following options are supported;
|
|
892
693
|
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
694
|
+
```javascript
|
|
695
|
+
{
|
|
696
|
+
flags: 'a', // w - write and a - append
|
|
697
|
+
encoding: null, // use null for binary files
|
|
698
|
+
mode: 0o666, // mode to use for created file (rwx)
|
|
699
|
+
autoClose: true // automatically close the write stream when finished
|
|
700
|
+
}
|
|
701
|
+
```
|
|
899
702
|
|
|
900
|
-
The most common options to use are mode and encoding. The values shown above are
|
|
901
|
-
the defaults. You do not have to set encoding to utf-8 for text files, null is
|
|
902
|
-
fine for all file types. Generally, I would not attempt to append binary files.
|
|
703
|
+
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.
|
|
903
704
|
|
|
904
705
|
2. Example Use
|
|
905
706
|
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
<a id="orgb9fee1c"></a>
|
|
707
|
+
```javascript
|
|
708
|
+
let remotePath = '/path/to/remote/file.txt';
|
|
709
|
+
let client = new Client();
|
|
710
|
+
|
|
711
|
+
client.connect(config)
|
|
712
|
+
.then(() => {
|
|
713
|
+
return client.append(Buffer.from('Hello world'), remotePath);
|
|
714
|
+
})
|
|
715
|
+
.then(() => {
|
|
716
|
+
return client.end();
|
|
717
|
+
})
|
|
718
|
+
.catch(err => {
|
|
719
|
+
console.error(err.message);
|
|
720
|
+
});
|
|
721
|
+
```
|
|
922
722
|
|
|
923
|
-
### mkdir(path, recursive) ==> string
|
|
723
|
+
### mkdir(path, recursive) ==> string<a id="sec-5-2-11"></a>
|
|
924
724
|
|
|
925
|
-
Create a new directory. If the recursive flag is set to true, the method will
|
|
926
|
-
create any directories in the path which do not already exist. Recursive flag
|
|
927
|
-
defaults to false.
|
|
725
|
+
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.
|
|
928
726
|
|
|
929
727
|
- **path:** string. Path to remote directory to create
|
|
930
|
-
- **recursive:** boolean. If true, create any missing directories in the path as
|
|
931
|
-
well
|
|
728
|
+
- **recursive:** boolean. If true, create any missing directories in the path as well
|
|
932
729
|
|
|
933
730
|
1. Example Use
|
|
934
731
|
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
<a id="org0e35c59"></a>
|
|
732
|
+
```javascript
|
|
733
|
+
let remoteDir = '/path/to/new/dir';
|
|
734
|
+
let client = new Client();
|
|
735
|
+
|
|
736
|
+
client.connect(config)
|
|
737
|
+
.then(() => {
|
|
738
|
+
return client.mkdir(remoteDir, true);
|
|
739
|
+
})
|
|
740
|
+
.then(() => {
|
|
741
|
+
return client.end();
|
|
742
|
+
})
|
|
743
|
+
.catch(err => {
|
|
744
|
+
console.error(err.message);
|
|
745
|
+
});
|
|
746
|
+
```
|
|
951
747
|
|
|
952
|
-
### rmdir(path, recursive) ==> string
|
|
748
|
+
### rmdir(path, recursive) ==> string<a id="sec-5-2-12"></a>
|
|
953
749
|
|
|
954
|
-
Remove a directory. If removing a directory and recursive flag is set to
|
|
955
|
-
`true`, the specified directory and all sub-directories and files will be
|
|
956
|
-
deleted. If set to false and the directory has sub-directories or files, the
|
|
957
|
-
action will fail.
|
|
750
|
+
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.
|
|
958
751
|
|
|
959
752
|
- **path:** string. Path to remote directory
|
|
960
|
-
- **recursive:** boolean. If true, remove all files and directories in target
|
|
961
|
-
directory. Defaults to false
|
|
753
|
+
- **recursive:** boolean. If true, remove all files and directories in target directory. Defaults to false
|
|
962
754
|
|
|
963
755
|
1. Example Use
|
|
964
756
|
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
<a id="org42f99cc"></a>
|
|
757
|
+
```javascript
|
|
758
|
+
let remoteDir = '/path/to/remote/dir';
|
|
759
|
+
let client = new Client();
|
|
760
|
+
|
|
761
|
+
client.connect(config)
|
|
762
|
+
.then(() => {
|
|
763
|
+
return client.rmdir(remoteDir, true);
|
|
764
|
+
})
|
|
765
|
+
.then(() => {
|
|
766
|
+
return client.end();
|
|
767
|
+
})
|
|
768
|
+
.catch(err => {
|
|
769
|
+
console.error(err.message);
|
|
770
|
+
});
|
|
771
|
+
```
|
|
981
772
|
|
|
982
|
-
### delete(path) ==> string
|
|
773
|
+
### delete(path) ==> string<a id="sec-5-2-13"></a>
|
|
983
774
|
|
|
984
775
|
Delete a file on the remote server.
|
|
985
776
|
|
|
@@ -987,607 +778,496 @@ Delete a file on the remote server.
|
|
|
987
778
|
|
|
988
779
|
1. Example Use
|
|
989
780
|
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
781
|
+
```javascript
|
|
782
|
+
let remoteFile = '/path/to/remote/file.txt';
|
|
783
|
+
let client = new Client();
|
|
784
|
+
|
|
785
|
+
client.connect(config)
|
|
786
|
+
.then(() => {
|
|
787
|
+
return client.delete(remoteFile);
|
|
788
|
+
})
|
|
789
|
+
.then(() => {
|
|
790
|
+
return client.end();
|
|
791
|
+
})
|
|
792
|
+
.catch(err => {
|
|
793
|
+
console.error(err.message);
|
|
794
|
+
});
|
|
795
|
+
```
|
|
1004
796
|
|
|
1005
|
-
<a id="
|
|
797
|
+
### rename(fromPath, toPath) ==> string<a id="sec-5-2-14"></a>
|
|
1006
798
|
|
|
1007
|
-
|
|
799
|
+
Rename a file or directory from `fromPath` to `toPath`. You must have the necessary permissions to modify the remote file.
|
|
1008
800
|
|
|
1009
|
-
|
|
1010
|
-
|
|
801
|
+
- **fromPath:** string. Path to existing file to be renamed
|
|
802
|
+
- **toPath:** string. Path to new file existing file is to be renamed to. Should not already exist.
|
|
1011
803
|
|
|
1012
804
|
1. Example Use
|
|
1013
805
|
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
806
|
+
```javascript
|
|
807
|
+
let from = '/remote/path/to/old.txt';
|
|
808
|
+
let to = '/remote/path/to/new.txt';
|
|
809
|
+
let client = new Client();
|
|
810
|
+
|
|
811
|
+
client.connect(config)
|
|
812
|
+
.then(() => {
|
|
813
|
+
return client.rename(from, to);
|
|
814
|
+
})
|
|
815
|
+
.then(() => {
|
|
816
|
+
return client.end();
|
|
817
|
+
})
|
|
818
|
+
.catch(err => {
|
|
819
|
+
console.error(err.message);
|
|
820
|
+
});
|
|
821
|
+
```
|
|
1029
822
|
|
|
1030
|
-
<a id="
|
|
823
|
+
### posixRename(fromPath, toPath) ==> string<a id="sec-5-2-15"></a>
|
|
1031
824
|
|
|
1032
|
-
|
|
825
|
+
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.
|
|
1033
826
|
|
|
1034
|
-
|
|
1035
|
-
|
|
827
|
+
- **fromPath:** string. Path to existing file to be renamed.
|
|
828
|
+
- **toPath:** string. Path for new name. If it already exists, it will be replaced by file specified in fromPath
|
|
1036
829
|
|
|
1037
|
-
|
|
1038
|
-
|
|
830
|
+
```javascript
|
|
831
|
+
let from = '/remote/path/to/old.txt';
|
|
832
|
+
let to = '/remote/path/to/new.txt';
|
|
833
|
+
let client = new Client();
|
|
1039
834
|
|
|
1040
|
-
|
|
835
|
+
client.connect(config)
|
|
836
|
+
.then(() => {
|
|
837
|
+
return client.posixRename(from, to);
|
|
838
|
+
})
|
|
839
|
+
.then(() => {
|
|
840
|
+
return client.end();
|
|
841
|
+
})
|
|
842
|
+
.catch(err => {
|
|
843
|
+
console.error(err.message);
|
|
844
|
+
});
|
|
845
|
+
```
|
|
1041
846
|
|
|
1042
|
-
|
|
1043
|
-
let ndwMode = 0o644; // rw-r-r
|
|
1044
|
-
let client = new Client();
|
|
1045
|
-
|
|
1046
|
-
client.connect(config)
|
|
1047
|
-
.then(() => {
|
|
1048
|
-
return client.chmod(path, newMode);
|
|
1049
|
-
})
|
|
1050
|
-
.then(() => {
|
|
1051
|
-
return client.end();
|
|
1052
|
-
})
|
|
1053
|
-
.catch(err => {
|
|
1054
|
-
console.error(err.message);
|
|
1055
|
-
});
|
|
847
|
+
### chmod(path, mode) ==> string<a id="sec-5-2-16"></a>
|
|
1056
848
|
|
|
849
|
+
Change the mode (read, write or execute permissions) of a remote file or directory.
|
|
1057
850
|
|
|
1058
|
-
|
|
851
|
+
- **path:** string. Path to the remote file or directory
|
|
852
|
+
- **mode:** octal. New mode to set for the remote file or directory
|
|
1059
853
|
|
|
1060
|
-
|
|
854
|
+
1. Example Use
|
|
1061
855
|
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
856
|
+
```javascript
|
|
857
|
+
let path = '/path/to/remote/file.txt';
|
|
858
|
+
let ndwMode = 0o644; // rw-r-r
|
|
859
|
+
let client = new Client();
|
|
860
|
+
|
|
861
|
+
client.connect(config)
|
|
862
|
+
.then(() => {
|
|
863
|
+
return client.chmod(path, newMode);
|
|
864
|
+
})
|
|
865
|
+
.then(() => {
|
|
866
|
+
return client.end();
|
|
867
|
+
})
|
|
868
|
+
.catch(err => {
|
|
869
|
+
console.error(err.message);
|
|
870
|
+
});
|
|
871
|
+
```
|
|
1065
872
|
|
|
1066
|
-
|
|
1067
|
-
does not expand '~'.
|
|
873
|
+
### realPath(path) ===> string<a id="sec-5-2-17"></a>
|
|
1068
874
|
|
|
875
|
+
Converts a relative path to an absolute path on the remote server. This method is mainly used internally to resolve remote path names. Returns '' if the path is not valid.
|
|
1069
876
|
|
|
1070
|
-
|
|
877
|
+
- **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
|
|
1071
878
|
|
|
1072
|
-
### cwd() ==> string
|
|
879
|
+
### cwd() ==> string<a id="sec-5-2-18"></a>
|
|
1073
880
|
|
|
1074
881
|
Returns what the server believes is the current remote working directory.
|
|
1075
882
|
|
|
883
|
+
### uploadDir(srcDir, dstDir) ==> string<a id="sec-5-2-19"></a>
|
|
1076
884
|
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
### uploadDir(srcDir, dstDir) ==> string
|
|
885
|
+
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.
|
|
1080
886
|
|
|
1081
|
-
|
|
1082
|
-
`dstDir`. The `dstDir` will be created if necessary. Any sub directories within
|
|
1083
|
-
`srcDir` will also be uploaded. Any existing files in the remote path will be
|
|
1084
|
-
overwritten.
|
|
1085
|
-
|
|
1086
|
-
The upload process also emits 'upload' events. These events are fired for each
|
|
1087
|
-
successfully uploaded file. The `upload` event calls listeners with 1 argument,
|
|
1088
|
-
an object which has properties source and destination. The source property is
|
|
1089
|
-
the path of the file uploaded and the destination property is the path to where
|
|
1090
|
-
the file was uploaded to. The purpose of this event is to provide some way for
|
|
1091
|
-
client code to get feedback on the upload progress. You can add your own lisener
|
|
1092
|
-
using the `on()` method.
|
|
887
|
+
The upload process also emits 'upload' events. These events are fired for each successfully uploaded file. The `upload` event calls listeners with 1 argument, an object which has properties source and destination. The source property is the path of the file uploaded and the destination property is the path to where the file was uploaded to. The purpose of this event is to provide some way for client code to get feedback on the upload progress. You can add your own lisener using the `on()` method.
|
|
1093
888
|
|
|
1094
889
|
- **srcDir:** A local file path specified as a string
|
|
1095
890
|
- **dstDir:** A remote file path specified as a string
|
|
1096
891
|
|
|
1097
892
|
1. Example
|
|
1098
893
|
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
// Example of using the uploadDir() method to upload a directory
|
|
1102
|
-
// to a remote SFTP server
|
|
1103
|
-
|
|
1104
|
-
const path = require('path');
|
|
1105
|
-
const SftpClient = require('../src/index');
|
|
1106
|
-
|
|
1107
|
-
const dotenvPath = path.join(__dirname, '..', '.env');
|
|
1108
|
-
require('dotenv').config({path: dotenvPath});
|
|
1109
|
-
|
|
1110
|
-
const config = {
|
|
1111
|
-
host: process.env.SFTP_SERVER,
|
|
1112
|
-
username: process.env.SFTP_USER,
|
|
1113
|
-
password: process.env.SFTP_PASSWORD,
|
|
1114
|
-
port: process.env.SFTP_PORT || 22
|
|
1115
|
-
};
|
|
1116
|
-
|
|
1117
|
-
async function main() {
|
|
1118
|
-
const client = new SftpClient('upload-test');
|
|
1119
|
-
const src = path.join(__dirname, '..', 'test', 'testData', 'upload-src');
|
|
1120
|
-
const dst = '/home/tim/upload-test';
|
|
1121
|
-
|
|
1122
|
-
try {
|
|
1123
|
-
await client.connect(config);
|
|
1124
|
-
client.on('upload', info => {
|
|
1125
|
-
console.log(`Listener: Uploaded ${info.source}`);
|
|
1126
|
-
});
|
|
1127
|
-
let rslt = await client.uploadDir(src, dst);
|
|
1128
|
-
return rslt;
|
|
1129
|
-
} finally {
|
|
1130
|
-
client.end();
|
|
1131
|
-
}
|
|
1132
|
-
}
|
|
1133
|
-
|
|
1134
|
-
main()
|
|
1135
|
-
.then(msg => {
|
|
1136
|
-
console.log(msg);
|
|
1137
|
-
})
|
|
1138
|
-
.catch(err => {
|
|
1139
|
-
console.log(`main error: ${err.message}`);
|
|
1140
|
-
});
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
<a id="orgac03d65"></a>
|
|
1144
|
-
|
|
1145
|
-
### downloadDir(srcDir, dstDir) ==> string
|
|
1146
|
-
|
|
1147
|
-
Download the remote directory specified by `srcDir` to the local file system
|
|
1148
|
-
directory specified by `dstDir`. The `dstDir` directory will be created if
|
|
1149
|
-
required. All sub directories within `srcDir` will also be copied. Any existing
|
|
1150
|
-
files in the local path will be overwritten. No files in the local path will be
|
|
1151
|
-
deleted.
|
|
1152
|
-
|
|
1153
|
-
The method also emites `download` events to provide a way to monitor download
|
|
1154
|
-
progress. The download event listener is called with one argument, an object
|
|
1155
|
-
with two properties, source and destination. The source property is the path to
|
|
1156
|
-
the remote file that has been downloaded and the destination is the local path
|
|
1157
|
-
to where the file was downloaded to. You can add a listener for this event using
|
|
1158
|
-
the `on()` method.
|
|
1159
|
-
|
|
1160
|
-
- **srcDir:** A remote file path specified as a string
|
|
1161
|
-
- **dstDir:** A local file path specified as a string
|
|
1162
|
-
|
|
1163
|
-
1. Example
|
|
1164
|
-
|
|
894
|
+
```javascript
|
|
1165
895
|
'use strict';
|
|
1166
|
-
|
|
1167
|
-
// Example of using the
|
|
896
|
+
|
|
897
|
+
// Example of using the uploadDir() method to upload a directory
|
|
1168
898
|
// to a remote SFTP server
|
|
1169
|
-
|
|
899
|
+
|
|
1170
900
|
const path = require('path');
|
|
1171
901
|
const SftpClient = require('../src/index');
|
|
1172
|
-
|
|
902
|
+
|
|
1173
903
|
const dotenvPath = path.join(__dirname, '..', '.env');
|
|
1174
904
|
require('dotenv').config({path: dotenvPath});
|
|
1175
|
-
|
|
905
|
+
|
|
1176
906
|
const config = {
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
907
|
+
host: process.env.SFTP_SERVER,
|
|
908
|
+
username: process.env.SFTP_USER,
|
|
909
|
+
password: process.env.SFTP_PASSWORD,
|
|
910
|
+
port: process.env.SFTP_PORT || 22
|
|
1181
911
|
};
|
|
1182
|
-
|
|
912
|
+
|
|
1183
913
|
async function main() {
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
console.log(`Listener:
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
914
|
+
const client = new SftpClient('upload-test');
|
|
915
|
+
const src = path.join(__dirname, '..', 'test', 'testData', 'upload-src');
|
|
916
|
+
const dst = '/home/tim/upload-test';
|
|
917
|
+
|
|
918
|
+
try {
|
|
919
|
+
await client.connect(config);
|
|
920
|
+
client.on('upload', info => {
|
|
921
|
+
console.log(`Listener: Uploaded ${info.source}`);
|
|
922
|
+
});
|
|
923
|
+
let rslt = await client.uploadDir(src, dst);
|
|
924
|
+
return rslt;
|
|
925
|
+
} finally {
|
|
926
|
+
client.end();
|
|
927
|
+
}
|
|
1198
928
|
}
|
|
1199
|
-
|
|
929
|
+
|
|
1200
930
|
main()
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
<a id="org13b315d"></a>
|
|
1210
|
-
|
|
1211
|
-
### end() ==> boolean
|
|
1212
|
-
|
|
1213
|
-
Ends the current client session, releasing the client socket and associated
|
|
1214
|
-
resources. This function also removes all listeners associated with the client.
|
|
1215
|
-
|
|
1216
|
-
1. Example Use
|
|
1217
|
-
|
|
1218
|
-
let client = new Client();
|
|
1219
|
-
|
|
1220
|
-
client.connect(config)
|
|
1221
|
-
.then(() => {
|
|
1222
|
-
// do some sftp stuff
|
|
1223
|
-
})
|
|
1224
|
-
.then(() => {
|
|
1225
|
-
return client.end();
|
|
1226
|
-
})
|
|
1227
|
-
.catch(err => {
|
|
1228
|
-
console.error(err.message);
|
|
1229
|
-
});
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
<a id="orgb64906e"></a>
|
|
1233
|
-
|
|
1234
|
-
### Add and Remove Listeners
|
|
1235
|
-
|
|
1236
|
-
Although normally not required, you can add and remove custom listeners on the
|
|
1237
|
-
ssh2 client object. This object supports a number of events, but only a few of
|
|
1238
|
-
them have any meaning in the context of SFTP. These are
|
|
1239
|
-
|
|
1240
|
-
- **error:** An error occurred. Calls listener with an error argument.
|
|
1241
|
-
- **end:** The socket has been disconnected. No argument.
|
|
1242
|
-
- **close:** The socket was closed. Boolean argument which is true when the socket
|
|
1243
|
-
was closed due to errors.
|
|
1244
|
-
|
|
1245
|
-
1. on(eventType, listener)
|
|
1246
|
-
|
|
1247
|
-
Adds the specified listener to the specified event type. It the event type is
|
|
1248
|
-
`error`, the listener should accept 1 argument, which will be an Error object. If
|
|
1249
|
-
the event type is `close`, the listener should accept one argument of a boolean
|
|
1250
|
-
type, which will be true when the client connection was closed due to errors.
|
|
1251
|
-
|
|
1252
|
-
2. removeListener(eventType, listener)
|
|
1253
|
-
|
|
1254
|
-
Removes the specified listener from the event specified in eventType. Note that
|
|
1255
|
-
the `end()` method automatically removes all listeners from the client object.
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
<a id="orgf6407fb"></a>
|
|
1259
|
-
|
|
1260
|
-
# FAQ
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
<a id="org63fc49c"></a>
|
|
1264
|
-
|
|
1265
|
-
## Remote server drops connections with only an end event
|
|
1266
|
-
|
|
1267
|
-
Many SFTP servers have rate limiting protection which will drop connections once
|
|
1268
|
-
a limit has been reached. In particular, openSSH has the setting `MaxStartups`,
|
|
1269
|
-
which can be a tuple of the form `max:drop:full` where `max` is the maximum
|
|
1270
|
-
allowed unauthenticated connections, `drop` is a percentage value which
|
|
1271
|
-
specifies percentage of connections to be dropped once `max` connections has
|
|
1272
|
-
been reached and `full` is the number of connections at which point all
|
|
1273
|
-
subsequent connections will be dropped. e.g. `10:30:60` means allow up to 10
|
|
1274
|
-
unauthenticated connections after which drop 30% of connection attempts until
|
|
1275
|
-
reaching 60 unauthenticated connections, at which time, drop all attempts.
|
|
1276
|
-
|
|
1277
|
-
Clients first make an unauthenticated connection to the SFTP server to begin
|
|
1278
|
-
negotiation of protocol settings (cipher, authentication method etc). If you are
|
|
1279
|
-
creating multiple connections in a script, it is easy to exceed the limit,
|
|
1280
|
-
resulting in some connections being dropped. As SSH2 only raises an 'end' event
|
|
1281
|
-
for these dropped connections, no error is detected. The `ssh2-sftp-client` now
|
|
1282
|
-
listens for `end` events during the connection process and if one is detected,
|
|
1283
|
-
will reject the connection promise.
|
|
1284
|
-
|
|
1285
|
-
One way to avoid this type of issue is to add a delay between connection
|
|
1286
|
-
attempts. It does not need to be a very long delay - just sufficient to permit
|
|
1287
|
-
the previous connection to be authenticated. In fact, the default setting for
|
|
1288
|
-
openSSH is `10:30:60`, so you really just need to have enough delay to ensure
|
|
1289
|
-
that the 1st connection has completed authentication before the 11th connection
|
|
1290
|
-
is attempted.
|
|
931
|
+
.then(msg => {
|
|
932
|
+
console.log(msg);
|
|
933
|
+
})
|
|
934
|
+
.catch(err => {
|
|
935
|
+
console.log(`main error: ${err.message}`);
|
|
936
|
+
});
|
|
937
|
+
|
|
938
|
+
```
|
|
1291
939
|
|
|
940
|
+
### downloadDir(srcDir, dstDir) ==> string<a id="sec-5-2-20"></a>
|
|
1292
941
|
|
|
1293
|
-
|
|
942
|
+
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.
|
|
1294
943
|
|
|
1295
|
-
|
|
944
|
+
The method also emites `download` events to provide a way to monitor download progress. The download event listener is called with one argument, an object with two properties, source and destination. The source property is the path to the remote file that has been downloaded and the destination is the local path to where the file was downloaded to. You can add a listener for this event using the `on()` method.
|
|
1296
945
|
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
writeable stream created with `fs.createWriteStream()`, the data will be written
|
|
1300
|
-
to the file specified in the constructor call to `createWriteStream()`.
|
|
946
|
+
- **srcDir:** A remote file path specified as a string
|
|
947
|
+
- **dstDir:** A local file path specified as a string
|
|
1301
948
|
|
|
1302
|
-
|
|
1303
|
-
will convert all the characters in the remote file to upper case before it is
|
|
1304
|
-
saved to the local file system. This could just as easily be something like a
|
|
1305
|
-
gunzip stream from `zlib`, enabling you to decompress remote zipped files as you
|
|
1306
|
-
bring them across before saving to local file system.
|
|
949
|
+
1. Example
|
|
1307
950
|
|
|
951
|
+
```javascript
|
|
1308
952
|
'use strict';
|
|
1309
953
|
|
|
1310
|
-
// Example of using
|
|
1311
|
-
//
|
|
1312
|
-
// and then save it to a local file
|
|
954
|
+
// Example of using the downloadDir() method to upload a directory
|
|
955
|
+
// to a remote SFTP server
|
|
1313
956
|
|
|
1314
|
-
const Client = require('../src/index.js');
|
|
1315
957
|
const path = require('path');
|
|
1316
|
-
const
|
|
1317
|
-
|
|
958
|
+
const SftpClient = require('../src/index');
|
|
959
|
+
|
|
960
|
+
const dotenvPath = path.join(__dirname, '..', '.env');
|
|
961
|
+
require('dotenv').config({path: dotenvPath});
|
|
1318
962
|
|
|
1319
963
|
const config = {
|
|
1320
|
-
host:
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
964
|
+
host: process.env.SFTP_SERVER,
|
|
965
|
+
username: process.env.SFTP_USER,
|
|
966
|
+
password: process.env.SFTP_PASSWORD,
|
|
967
|
+
port: process.env.SFTP_PORT || 22
|
|
1324
968
|
};
|
|
1325
969
|
|
|
1326
|
-
|
|
1327
|
-
|
|
970
|
+
async function main() {
|
|
971
|
+
const client = new SftpClient('upload-test');
|
|
972
|
+
const dst = '/tmp';
|
|
973
|
+
const src = '/home/tim/upload-test';
|
|
1328
974
|
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
975
|
+
try {
|
|
976
|
+
await client.connect(config);
|
|
977
|
+
client.on('download', info => {
|
|
978
|
+
console.log(`Listener: Download ${info.source}`);
|
|
979
|
+
});
|
|
980
|
+
let rslt = await client.downloadDir(src, dst);
|
|
981
|
+
return rslt;
|
|
982
|
+
} finally {
|
|
983
|
+
client.end();
|
|
984
|
+
}
|
|
1333
985
|
}
|
|
1334
986
|
|
|
1335
|
-
|
|
1336
|
-
.
|
|
1337
|
-
|
|
1338
|
-
return sftp.list(remoteDir);
|
|
987
|
+
main()
|
|
988
|
+
.then(msg => {
|
|
989
|
+
console.log(msg);
|
|
1339
990
|
})
|
|
1340
|
-
.
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
991
|
+
.catch(err => {
|
|
992
|
+
console.log(`main error: ${err.message}`);
|
|
993
|
+
});
|
|
994
|
+
|
|
995
|
+
```
|
|
996
|
+
|
|
997
|
+
### end() ==> boolean<a id="sec-5-2-21"></a>
|
|
998
|
+
|
|
999
|
+
Ends the current client session, releasing the client socket and associated resources. This function also removes all listeners associated with the client.
|
|
1000
|
+
|
|
1001
|
+
1. Example Use
|
|
1002
|
+
|
|
1003
|
+
```javascript
|
|
1004
|
+
let client = new Client();
|
|
1005
|
+
|
|
1006
|
+
client.connect(config)
|
|
1007
|
+
.then(() => {
|
|
1008
|
+
// do some sftp stuff
|
|
1348
1009
|
})
|
|
1349
1010
|
.then(() => {
|
|
1350
|
-
return
|
|
1011
|
+
return client.end();
|
|
1351
1012
|
})
|
|
1352
1013
|
.catch(err => {
|
|
1353
1014
|
console.error(err.message);
|
|
1354
1015
|
});
|
|
1016
|
+
```
|
|
1355
1017
|
|
|
1018
|
+
### Add and Remove Listeners<a id="sec-5-2-22"></a>
|
|
1356
1019
|
|
|
1357
|
-
|
|
1020
|
+
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
|
|
1358
1021
|
|
|
1359
|
-
|
|
1022
|
+
- **error:** An error occurred. Calls listener with an error argument.
|
|
1023
|
+
- **end:** The socket has been disconnected. No argument.
|
|
1024
|
+
- **close:** The socket was closed. Boolean argument which is true when the socket was closed due to errors.
|
|
1360
1025
|
|
|
1361
|
-
|
|
1362
|
-
and use these for authentication to the remote server.
|
|
1026
|
+
1. on(eventType, listener)
|
|
1363
1027
|
|
|
1364
|
-
|
|
1365
|
-
process. **Note**: SSH<sub>AUTH</sub><sub>SOCK</sub> is normally created by your OS when you load the
|
|
1366
|
-
ssh-agent as part of the login session.
|
|
1028
|
+
Adds the specified listener to the specified event type. It the event type is `error`, the listener should accept 1 argument, which will be an Error object. If the event type is `close`, the listener should accept one argument of a boolean type, which will be true when the client connection was closed due to errors.
|
|
1367
1029
|
|
|
1368
|
-
|
|
1369
|
-
sftp.connect({
|
|
1370
|
-
host: 'YOUR-HOST',
|
|
1371
|
-
port: 'YOUR-PORT',
|
|
1372
|
-
username: 'YOUR-USERNAME',
|
|
1373
|
-
agent: process.env.SSH_AUTH_SOCK
|
|
1374
|
-
}).then(() => {
|
|
1375
|
-
sftp.fastPut(/* ... */)
|
|
1376
|
-
}
|
|
1030
|
+
2. removeListener(eventType, listener)
|
|
1377
1031
|
|
|
1378
|
-
|
|
1379
|
-
|
|
1032
|
+
Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
|
|
1033
|
+
|
|
1034
|
+
# FAQ<a id="sec-6"></a>
|
|
1035
|
+
|
|
1036
|
+
## Remote server drops connections with only an end event<a id="sec-6-1"></a>
|
|
1037
|
+
|
|
1038
|
+
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.
|
|
1039
|
+
|
|
1040
|
+
Clients first make an unauthenticated connection to the SFTP server to begin negotiation of protocol settings (cipher, authentication method etc). If you are creating multiple connections in a script, it is easy to exceed the limit, resulting in some connections being dropped. As SSH2 only raises an 'end' event for these dropped connections, no error is detected. The `ssh2-sftp-client` now listens for `end` events during the connection process and if one is detected, will reject the connection promise.
|
|
1041
|
+
|
|
1042
|
+
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.
|
|
1043
|
+
|
|
1044
|
+
## How can you pass writable stream as dst for get method?<a id="sec-6-2"></a>
|
|
1045
|
+
|
|
1046
|
+
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()`.
|
|
1047
|
+
|
|
1048
|
+
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.
|
|
1049
|
+
|
|
1050
|
+
```javascript
|
|
1051
|
+
'use strict';
|
|
1052
|
+
|
|
1053
|
+
// Example of using a writeable with get to retrieve a file.
|
|
1054
|
+
// This code will read the remote file, convert all characters to upper case
|
|
1055
|
+
// and then save it to a local file
|
|
1056
|
+
|
|
1057
|
+
const Client = require('../src/index.js');
|
|
1058
|
+
const path = require('path');
|
|
1059
|
+
const fs = require('fs');
|
|
1060
|
+
const through = require('through2');
|
|
1061
|
+
|
|
1062
|
+
const config = {
|
|
1063
|
+
host: 'arch-vbox',
|
|
1064
|
+
port: 22,
|
|
1065
|
+
username: 'tim',
|
|
1066
|
+
password: 'xxxx'
|
|
1067
|
+
};
|
|
1068
|
+
|
|
1069
|
+
const sftp = new Client();
|
|
1070
|
+
const remoteDir = '/home/tim/testServer';
|
|
1071
|
+
|
|
1072
|
+
function toupper() {
|
|
1073
|
+
return through(function(buf, enc, next) {
|
|
1074
|
+
next(null, buf.toString().toUpperCase());
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
sftp
|
|
1079
|
+
.connect(config)
|
|
1080
|
+
.then(() => {
|
|
1081
|
+
return sftp.list(remoteDir);
|
|
1082
|
+
})
|
|
1083
|
+
.then(data => {
|
|
1084
|
+
// list of files in testServer
|
|
1085
|
+
console.dir(data);
|
|
1086
|
+
let remoteFile = path.join(remoteDir, 'test.txt');
|
|
1087
|
+
let upperWtr = toupper();
|
|
1088
|
+
let fileWtr = fs.createWriteStream(path.join(__dirname, 'loud-text.txt'));
|
|
1089
|
+
upperWtr.pipe(fileWtr);
|
|
1090
|
+
return sftp.get(remoteFile, upperWtr);
|
|
1091
|
+
})
|
|
1092
|
+
.then(() => {
|
|
1093
|
+
return sftp.end();
|
|
1094
|
+
})
|
|
1095
|
+
.catch(err => {
|
|
1096
|
+
console.error(err.message);
|
|
1097
|
+
});
|
|
1098
|
+
```
|
|
1099
|
+
|
|
1100
|
+
## How can I upload files without having to specify a password?<a id="sec-6-3"></a>
|
|
1101
|
+
|
|
1102
|
+
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.
|
|
1103
|
+
|
|
1104
|
+
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.
|
|
1105
|
+
|
|
1106
|
+
```javascript
|
|
1107
|
+
let sftp = new Client();
|
|
1108
|
+
sftp.connect({
|
|
1109
|
+
host: 'YOUR-HOST',
|
|
1110
|
+
port: 'YOUR-PORT',
|
|
1111
|
+
username: 'YOUR-USERNAME',
|
|
1112
|
+
agent: process.env.SSH_AUTH_SOCK
|
|
1113
|
+
}).then(() => {
|
|
1114
|
+
sftp.fastPut(/* ... */)
|
|
1115
|
+
}
|
|
1116
|
+
```
|
|
1117
|
+
|
|
1118
|
+
Another alternative is to just pass in the SSH key directly as part of the configuration.
|
|
1119
|
+
|
|
1120
|
+
```javascript
|
|
1121
|
+
let sftp = new Client();
|
|
1122
|
+
sftp.connect({
|
|
1123
|
+
host: 'YOUR-HOST',
|
|
1124
|
+
port: 'YOUR-PORT',
|
|
1125
|
+
username: 'YOUR-USERNAME',
|
|
1126
|
+
privateKey: fs.readFileSync('/path/to/ssh/key')
|
|
1127
|
+
}).then(() => {
|
|
1128
|
+
sftp.fastPut(/* ... */)
|
|
1129
|
+
}
|
|
1130
|
+
```
|
|
1131
|
+
|
|
1132
|
+
## How can I connect through a Socks Proxy<a id="sec-6-4"></a>
|
|
1380
1133
|
|
|
1381
|
-
|
|
1382
|
-
sftp.connect({
|
|
1383
|
-
host: 'YOUR-HOST',
|
|
1384
|
-
port: 'YOUR-PORT',
|
|
1385
|
-
username: 'YOUR-USERNAME',
|
|
1386
|
-
privateKey: fs.readFileSync('/path/to/ssh/key')
|
|
1387
|
-
}).then(() => {
|
|
1388
|
-
sftp.fastPut(/* ... */)
|
|
1389
|
-
}
|
|
1134
|
+
This solution was provided by @jmorino.
|
|
1390
1135
|
|
|
1136
|
+
```javascript
|
|
1137
|
+
import { SocksClient } from 'socks';
|
|
1138
|
+
import SFTPClient from 'ssh2-sftp-client';
|
|
1391
1139
|
|
|
1392
|
-
|
|
1140
|
+
const host = 'my-sftp-server.net';
|
|
1141
|
+
const port = 22; // default SSH/SFTP port on remote server
|
|
1393
1142
|
|
|
1394
|
-
|
|
1143
|
+
// connect to SOCKS 5 proxy
|
|
1144
|
+
const { socket } = await SocksClient.createConnection({
|
|
1145
|
+
proxy: {
|
|
1146
|
+
host: 'my.proxy', // proxy hostname
|
|
1147
|
+
port: 1080, // proxy port
|
|
1148
|
+
type: 5, // for SOCKS v5
|
|
1149
|
+
},
|
|
1150
|
+
command: 'connect',
|
|
1151
|
+
destination: { host, port } // the remote SFTP server
|
|
1152
|
+
});
|
|
1395
1153
|
|
|
1396
|
-
|
|
1154
|
+
const client = new SFTPClient();
|
|
1155
|
+
client.connect({
|
|
1156
|
+
host,
|
|
1157
|
+
sock: socket, // pass the socket to proxy here (see ssh2 doc)
|
|
1158
|
+
username: '.....',
|
|
1159
|
+
privateKey: '.....'
|
|
1160
|
+
})
|
|
1397
1161
|
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
const host = 'my-sftp-server.net';
|
|
1402
|
-
const port = 22; // default SSH/SFTP port on remote server
|
|
1403
|
-
|
|
1404
|
-
// connect to SOCKS 5 proxy
|
|
1405
|
-
const { socket } = await SocksClient.createConnection({
|
|
1406
|
-
proxy: {
|
|
1407
|
-
host: 'my.proxy', // proxy hostname
|
|
1408
|
-
port: 1080, // proxy port
|
|
1409
|
-
type: 5, // for SOCKS v5
|
|
1410
|
-
},
|
|
1411
|
-
command: 'connect',
|
|
1412
|
-
destination: { host, port } // the remote SFTP server
|
|
1413
|
-
});
|
|
1414
|
-
|
|
1415
|
-
const client = new SFTPClient();
|
|
1416
|
-
client.connect({
|
|
1417
|
-
host,
|
|
1418
|
-
sock: socket, // pass the socket to proxy here (see ssh2 doc)
|
|
1419
|
-
username: '.....',
|
|
1420
|
-
privateKey: '.....'
|
|
1421
|
-
})
|
|
1422
|
-
|
|
1423
|
-
// client is connected
|
|
1162
|
+
// client is connected
|
|
1163
|
+
```
|
|
1424
1164
|
|
|
1165
|
+
## Timeout while waiting for handshake or handshake errors<a id="sec-6-5"></a>
|
|
1425
1166
|
|
|
1426
|
-
|
|
1167
|
+
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.
|
|
1427
1168
|
|
|
1428
|
-
|
|
1169
|
+
# Examples<a id="sec-7"></a>
|
|
1429
1170
|
|
|
1430
|
-
|
|
1431
|
-
'Handshake failed, no matching client->server ciphers. This is often due to the
|
|
1432
|
-
client not having the correct configuration for the transport layer algorithms
|
|
1433
|
-
used by ssh2. One of the connect options provided by the ssh2 module is
|
|
1434
|
-
`algorithm`, which is an object that allows you to explicitly set the key
|
|
1435
|
-
exchange, ciphers, hmac and compression algorithms as well as server
|
|
1436
|
-
host key used to establish the initial secure connection. See the SSH2
|
|
1437
|
-
documentation for details. Getting these parameters correct usually resolves the
|
|
1438
|
-
issue.
|
|
1171
|
+
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.
|
|
1439
1172
|
|
|
1173
|
+
# Change Log<a id="sec-8"></a>
|
|
1440
1174
|
|
|
1441
|
-
<a id="
|
|
1175
|
+
## v5.2.1 (Prod Version)<a id="sec-8-1"></a>
|
|
1442
1176
|
|
|
1443
|
-
|
|
1177
|
+
- Move some dependencies into dev-Dependencies
|
|
1444
1178
|
|
|
1445
|
-
|
|
1446
|
-
repository. These are mainly scripts I have put together in order to investigate
|
|
1447
|
-
issues or provide samples for users. They are not robust, lack adequate error
|
|
1448
|
-
handling and may contain errors. However, I think they are still useful for
|
|
1449
|
-
helping developers see how the module and API can be used.
|
|
1179
|
+
## v5.2.0<a id="sec-8-2"></a>
|
|
1450
1180
|
|
|
1181
|
+
- Add new method posixRename() which uses the openSSH POSIX rename extension.
|
|
1451
1182
|
|
|
1452
|
-
<a id="
|
|
1183
|
+
## v5.1.3<a id="sec-8-3"></a>
|
|
1453
1184
|
|
|
1454
|
-
|
|
1185
|
+
- Fix bug when writing to root directory and failure due to not being able to determine parent
|
|
1186
|
+
- Refactor some tests to eliminate need to have artificial delays between tests
|
|
1187
|
+
- Bumped some dependency versions to latest version
|
|
1455
1188
|
|
|
1189
|
+
## v5.1.2<a id="sec-8-4"></a>
|
|
1456
1190
|
|
|
1457
|
-
|
|
1191
|
+
- Added back global close handler
|
|
1192
|
+
- Added dumpListeners() method
|
|
1458
1193
|
|
|
1459
|
-
## v5.1.1
|
|
1194
|
+
## v5.1.1<a id="sec-8-5"></a>
|
|
1460
1195
|
|
|
1461
1196
|
- Added separate close handlers to each method.
|
|
1462
1197
|
- Added missing return statement in connect method
|
|
1463
|
-
- Added additional troubleshooting documentation for
|
|
1464
|
-
common errors.
|
|
1465
|
-
|
|
1198
|
+
- Added additional troubleshooting documentation for common errors.
|
|
1466
1199
|
|
|
1467
|
-
<a id="
|
|
1200
|
+
## v5.1.0<a id="sec-8-6"></a>
|
|
1468
1201
|
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
- Fix bug in checkRemotePath() relating to handling of badly
|
|
1472
|
-
specified paths (issue #213)
|
|
1202
|
+
- Fix bug in checkRemotePath() relating to handling of badly specified paths (issue #213)
|
|
1473
1203
|
- Added additional debugging support
|
|
1474
1204
|
- Add missing test for valid connection in end() method.
|
|
1475
1205
|
- Bump ssh2 version to v0.8.8
|
|
1476
1206
|
|
|
1477
|
-
|
|
1478
|
-
<a id="orgfee3d13"></a>
|
|
1479
|
-
|
|
1480
|
-
## v5.0.2
|
|
1207
|
+
## v5.0.2<a id="sec-8-7"></a>
|
|
1481
1208
|
|
|
1482
1209
|
- Fix bugs related to win32 platform and local tests for valid directories
|
|
1483
1210
|
- Fix problem with parsing of file paths
|
|
1484
1211
|
|
|
1212
|
+
## v5.0.1<a id="sec-8-8"></a>
|
|
1485
1213
|
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
## v5.0.1
|
|
1489
|
-
|
|
1490
|
-
- Turn down error checking to be less stringent and handle situations
|
|
1491
|
-
where user does not have read permission on parent directory.
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
<a id="org721d150"></a>
|
|
1214
|
+
- Turn down error checking to be less stringent and handle situations where user does not have read permission on parent directory.
|
|
1495
1215
|
|
|
1496
|
-
## v5.0.0
|
|
1216
|
+
## v5.0.0<a id="sec-8-9"></a>
|
|
1497
1217
|
|
|
1498
1218
|
- Added two new methods `uploadDir()` and `downloadDir()`
|
|
1499
1219
|
- Removed deprecated `auxList()` method
|
|
1500
1220
|
- Improved error message consistency
|
|
1501
|
-
- Added additional error checking to enable more accurate and useful error
|
|
1502
|
-
|
|
1503
|
-
-
|
|
1504
|
-
|
|
1505
|
-
- Modified event handlers to ensure that only event handlers added by the
|
|
1506
|
-
module are removed by the module (users now responsible for removing any
|
|
1507
|
-
custom event handlers they add).
|
|
1508
|
-
- Module error handlers added using `prependListener` to ensure they are
|
|
1509
|
-
called before any additional custom handlers added by client code.
|
|
1221
|
+
- Added additional error checking to enable more accurate and useful error messages.
|
|
1222
|
+
- Added default error handler to deal with event errors which fire outside of active SftpClient methods (i.e. connection unexpectedly reset by remote host).
|
|
1223
|
+
- Modified event handlers to ensure that only event handlers added by the module are removed by the module (users now responsible for removing any custom event handlers they add).
|
|
1224
|
+
- Module error handlers added using `prependListener` to ensure they are called before any additional custom handlers added by client code.
|
|
1510
1225
|
- Any error events fired during an `end()` call are now ignored.
|
|
1511
1226
|
|
|
1512
|
-
|
|
1513
|
-
<a id="orgc2006e2"></a>
|
|
1514
|
-
|
|
1515
|
-
## v4.3.1
|
|
1227
|
+
## v4.3.1<a id="sec-8-10"></a>
|
|
1516
1228
|
|
|
1517
1229
|
- Updated end() method to resolve once close event fires
|
|
1518
|
-
- Added errorListener to error event in each promise to catch error events
|
|
1519
|
-
and reject the promise. This should resolve the issue of some error events
|
|
1520
|
-
causing uncaughtException erros and causing the process to exit.
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
<a id="org07eb3bc"></a>
|
|
1230
|
+
- Added errorListener to error event in each promise to catch error events and reject the promise. This should resolve the issue of some error events causing uncaughtException erros and causing the process to exit.
|
|
1524
1231
|
|
|
1525
|
-
## v4.3.0
|
|
1232
|
+
## v4.3.0<a id="sec-8-11"></a>
|
|
1526
1233
|
|
|
1527
|
-
- Ensure errors include an err.code property and pass through the error code
|
|
1528
|
-
|
|
1529
|
-
- Change tests for error type to use `error.code` instead of matching on
|
|
1530
|
-
`error.message`.
|
|
1234
|
+
- Ensure errors include an err.code property and pass through the error code from the originating error
|
|
1235
|
+
- Change tests for error type to use `error.code` instead of matching on `error.message`.
|
|
1531
1236
|
|
|
1532
|
-
|
|
1533
|
-
<a id="org7463d6d"></a>
|
|
1534
|
-
|
|
1535
|
-
## v4.2.4
|
|
1237
|
+
## v4.2.4<a id="sec-8-12"></a>
|
|
1536
1238
|
|
|
1537
1239
|
- Bumped ssh2 to v0.8.6
|
|
1538
1240
|
- Added exists() usage example to examples directory
|
|
1539
1241
|
- Clarify documentation on get() method
|
|
1540
1242
|
|
|
1541
|
-
|
|
1542
|
-
<a id="org6657b1f"></a>
|
|
1543
|
-
|
|
1544
|
-
## v4.2.3
|
|
1243
|
+
## v4.2.3<a id="sec-8-13"></a>
|
|
1545
1244
|
|
|
1546
1245
|
- Fix bug in `exist()` where tests on root directory returned false
|
|
1547
1246
|
- Minor documentation fixes
|
|
1548
1247
|
- Clean up mkdir example
|
|
1549
1248
|
|
|
1550
|
-
|
|
1551
|
-
<a id="orgf9ca850"></a>
|
|
1552
|
-
|
|
1553
|
-
## v4.2.2
|
|
1249
|
+
## v4.2.2<a id="sec-8-14"></a>
|
|
1554
1250
|
|
|
1555
1251
|
- Minor documentation fixes
|
|
1556
1252
|
- Added additional examples in the `example` directory
|
|
1557
1253
|
|
|
1254
|
+
## v4.2.1<a id="sec-8-15"></a>
|
|
1558
1255
|
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
- Remove default close listener. changes in ssh2 API removed the utility of a
|
|
1564
|
-
default close listener
|
|
1565
|
-
- Fix path handling. Under mixed environments (where client platform and
|
|
1566
|
-
server platform were different i.e. one windows the other unix), path
|
|
1567
|
-
handling was broken due tot he use of path.join().
|
|
1568
|
-
- Ensure error messages include path details. Instead of errors such as "No
|
|
1569
|
-
such file" now report "No such file /path/to/missing/file" to help with
|
|
1570
|
-
debugging
|
|
1571
|
-
|
|
1256
|
+
- Remove default close listener. changes in ssh2 API removed the utility of a default close listener
|
|
1257
|
+
- Fix path handling. Under mixed environments (where client platform and server platform were different i.e. one windows the other unix), path handling was broken due tot he use of path.join().
|
|
1258
|
+
- Ensure error messages include path details. Instead of errors such as "No such file" now report "No such file /path/to/missing/file" to help with debugging
|
|
1572
1259
|
|
|
1573
|
-
<a id="
|
|
1574
|
-
|
|
1575
|
-
## v4.2.0
|
|
1260
|
+
## v4.2.0<a id="sec-8-16"></a>
|
|
1576
1261
|
|
|
1577
1262
|
- Work-around for SSH2 `end` event bug
|
|
1578
1263
|
- Added ability to set client name in constructor method
|
|
1579
|
-
- Added additional error checking to prevent `connect()` being called on
|
|
1580
|
-
already connected client
|
|
1264
|
+
- Added additional error checking to prevent `connect()` being called on already connected client
|
|
1581
1265
|
- Added additional examples in `example` directory
|
|
1582
1266
|
|
|
1583
|
-
|
|
1584
|
-
<a id="org106c114"></a>
|
|
1585
|
-
|
|
1586
|
-
## v4.1.0
|
|
1267
|
+
## v4.1.0<a id="sec-8-17"></a>
|
|
1587
1268
|
|
|
1588
1269
|
- move `end()` call to resolve into close hook
|
|
1589
|
-
- Prevent `put()` and `get()` from creating empty files in destination when
|
|
1590
|
-
unable to read source
|
|
1270
|
+
- Prevent `put()` and `get()` from creating empty files in destination when unable to read source
|
|
1591
1271
|
- Expand tests for operations when lacking required permissions
|
|
1592
1272
|
- Add additional data checks for `append()`
|
|
1593
1273
|
- Verify file exists
|
|
@@ -1597,97 +1277,63 @@ helping developers see how the module and API can be used.
|
|
|
1597
1277
|
- Add `realPath()` method
|
|
1598
1278
|
- Add `cwd()` method
|
|
1599
1279
|
|
|
1600
|
-
|
|
1601
|
-
<a id="org07257da"></a>
|
|
1602
|
-
|
|
1603
|
-
## v4.0.4
|
|
1280
|
+
## v4.0.4<a id="sec-8-18"></a>
|
|
1604
1281
|
|
|
1605
1282
|
- Minor documentation fix
|
|
1606
1283
|
- Fix return value from `get()`
|
|
1607
1284
|
|
|
1608
|
-
|
|
1609
|
-
<a id="org8ca64ad"></a>
|
|
1610
|
-
|
|
1611
|
-
## v4.0.3
|
|
1285
|
+
## v4.0.3<a id="sec-8-19"></a>
|
|
1612
1286
|
|
|
1613
1287
|
- Fix bug in mkdir() relating to handling of relative paths
|
|
1614
1288
|
- Modify exists() to always return 'd' if path is '.'
|
|
1615
1289
|
|
|
1616
|
-
|
|
1617
|
-
<a id="org5c44eae"></a>
|
|
1618
|
-
|
|
1619
|
-
## v4.0.2
|
|
1290
|
+
## v4.0.2<a id="sec-8-20"></a>
|
|
1620
1291
|
|
|
1621
1292
|
- Fix some minor packaging issues
|
|
1622
1293
|
|
|
1623
|
-
|
|
1624
|
-
<a id="orgc7f98e7"></a>
|
|
1625
|
-
|
|
1626
|
-
## v4.0.0
|
|
1294
|
+
## v4.0.0<a id="sec-8-21"></a>
|
|
1627
1295
|
|
|
1628
1296
|
- Remove support for node < 8.x
|
|
1629
1297
|
- Fix connection retry feature
|
|
1630
1298
|
- sftp connection object set to null when 'end' signal is raised
|
|
1631
1299
|
- Removed 'connectMethod' argument from connect method.
|
|
1632
|
-
- Refined adding/removing of listeners in connect() and end() methods to enable
|
|
1633
|
-
errors to be adequately caught and reported.
|
|
1300
|
+
- Refined adding/removing of listeners in connect() and end() methods to enable errors to be adequately caught and reported.
|
|
1634
1301
|
- Deprecate auxList() and add pattern/regexp filter option to list()
|
|
1635
1302
|
- Refactored handling of event signals to provide better feedback to clients
|
|
1636
|
-
- Removed pointless 'permissions' property from objects returned by `stat()`
|
|
1637
|
-
(same as mode property). Added additional properties describing the type of
|
|
1638
|
-
object.
|
|
1303
|
+
- Removed pointless 'permissions' property from objects returned by `stat()` (same as mode property). Added additional properties describing the type of object.
|
|
1639
1304
|
- Added the `removeListener()` method to compliment the existing `on()` method.
|
|
1640
1305
|
|
|
1306
|
+
## Older Versions<a id="sec-8-22"></a>
|
|
1641
1307
|
|
|
1642
|
-
<a id="
|
|
1643
|
-
|
|
1644
|
-
## v2.5.2
|
|
1308
|
+
### v2.5.2<a id="sec-8-22-1"></a>
|
|
1645
1309
|
|
|
1646
1310
|
- Repository transferred to theophilusx
|
|
1647
1311
|
- Fix error in package.json pointing to wrong repository
|
|
1648
1312
|
|
|
1649
|
-
|
|
1650
|
-
<a id="org24f7807"></a>
|
|
1651
|
-
|
|
1652
|
-
## v2.5.1
|
|
1313
|
+
### v2.5.1<a id="sec-8-22-2"></a>
|
|
1653
1314
|
|
|
1654
1315
|
- Apply 4 pull requests to address minor issues prior to transfer
|
|
1655
1316
|
|
|
1656
|
-
|
|
1657
|
-
<a id="org223df69"></a>
|
|
1658
|
-
|
|
1659
|
-
## v2.5.0
|
|
1317
|
+
### v2.5.0<a id="sec-8-22-3"></a>
|
|
1660
1318
|
|
|
1661
1319
|
- ???
|
|
1662
1320
|
|
|
1663
|
-
|
|
1664
|
-
<a id="orgf4aafcf"></a>
|
|
1665
|
-
|
|
1666
|
-
## v2.4.3
|
|
1321
|
+
### v2.4.3<a id="sec-8-22-4"></a>
|
|
1667
1322
|
|
|
1668
1323
|
- merge #108, #110
|
|
1669
1324
|
- fix connect promise if connection ends
|
|
1670
1325
|
|
|
1671
|
-
|
|
1672
|
-
<a id="org76d7e49"></a>
|
|
1673
|
-
|
|
1674
|
-
## v2.4.2
|
|
1326
|
+
### v2.4.2<a id="sec-8-22-5"></a>
|
|
1675
1327
|
|
|
1676
1328
|
- merge #105
|
|
1677
1329
|
- fix windows path
|
|
1678
1330
|
|
|
1679
|
-
|
|
1680
|
-
<a id="org4cd8310"></a>
|
|
1681
|
-
|
|
1682
|
-
## v2.4.1
|
|
1331
|
+
### v2.4.1<a id="sec-8-22-6"></a>
|
|
1683
1332
|
|
|
1684
1333
|
- merge pr #99, #100
|
|
1685
1334
|
- bug fix
|
|
1686
1335
|
|
|
1687
|
-
|
|
1688
|
-
<a id="org4ef3485"></a>
|
|
1689
|
-
|
|
1690
|
-
## v2.4.0
|
|
1336
|
+
### v2.4.0<a id="sec-8-22-7"></a>
|
|
1691
1337
|
|
|
1692
1338
|
- Requires node.js v7.5.0 or above.
|
|
1693
1339
|
- merge pr #97, thanks for @theophilusx
|
|
@@ -1697,335 +1343,204 @@ helping developers see how the module and API can be used.
|
|
|
1697
1343
|
- re-factored test
|
|
1698
1344
|
- Added new 'exists' method and re-factored mkdir/rmdir
|
|
1699
1345
|
|
|
1700
|
-
|
|
1701
|
-
<a id="orgfc595af"></a>
|
|
1702
|
-
|
|
1703
|
-
## v2.3.0
|
|
1346
|
+
### v2.3.0<a id="sec-8-22-8"></a>
|
|
1704
1347
|
|
|
1705
1348
|
- add: `stat` method
|
|
1706
1349
|
- add `fastGet` and `fastPut` method.
|
|
1707
1350
|
- fix: `mkdir` file exists decision logic
|
|
1708
1351
|
|
|
1709
|
-
|
|
1710
|
-
<a id="orgd01eb93"></a>
|
|
1711
|
-
|
|
1712
|
-
## v3.0.0 – deprecate this version
|
|
1352
|
+
### v3.0.0 – deprecate this version<a id="sec-8-22-9"></a>
|
|
1713
1353
|
|
|
1714
1354
|
- change: `sftp.get` will return chunk not stream anymore
|
|
1715
1355
|
- fix: get readable not emitting data events in node 10.0.0
|
|
1716
1356
|
|
|
1717
|
-
|
|
1718
|
-
<a id="org5180ae0"></a>
|
|
1719
|
-
|
|
1720
|
-
## v2.1.1
|
|
1357
|
+
### v2.1.1<a id="sec-8-22-10"></a>
|
|
1721
1358
|
|
|
1722
1359
|
- add: event listener. [doc](https://github.com/jyu213/ssh2-sftp-client#Event)
|
|
1723
1360
|
- add: `get` or `put` method add extra options [pr#52](https://github.com/jyu213/ssh2-sftp-client/pull/52)
|
|
1724
1361
|
|
|
1725
|
-
|
|
1726
|
-
<a id="orga128f4c"></a>
|
|
1727
|
-
|
|
1728
|
-
## v2.0.1
|
|
1362
|
+
### v2.0.1<a id="sec-8-22-11"></a>
|
|
1729
1363
|
|
|
1730
1364
|
- add: `chmod` method [pr#33](https://github.com/jyu213/ssh2-sftp-client/pull/33)
|
|
1731
1365
|
- update: upgrade ssh2 to V0.5.0 [pr#30](https://github.com/jyu213/ssh2-sftp-client/pull/30)
|
|
1732
1366
|
- fix: get method stream error reject unwork [#22](https://github.com/jyu213/ssh2-sftp-client/issues/22)
|
|
1733
1367
|
- fix: return Error object on promise rejection [pr#20](https://github.com/jyu213/ssh2-sftp-client/pull/20)
|
|
1734
1368
|
|
|
1735
|
-
|
|
1736
|
-
<a id="org441359e"></a>
|
|
1737
|
-
|
|
1738
|
-
## v1.1.0
|
|
1369
|
+
### v1.1.0<a id="sec-8-22-12"></a>
|
|
1739
1370
|
|
|
1740
1371
|
- fix: add encoding control support for binary stream
|
|
1741
1372
|
|
|
1742
|
-
|
|
1743
|
-
<a id="org4db8e23"></a>
|
|
1744
|
-
|
|
1745
|
-
## v1.0.5:
|
|
1373
|
+
### v1.0.5:<a id="sec-8-22-13"></a>
|
|
1746
1374
|
|
|
1747
1375
|
- fix: multi image upload
|
|
1748
1376
|
- change: remove `this.client.sftp` to `connect` function
|
|
1749
1377
|
|
|
1378
|
+
# Troubleshooting<a id="sec-9"></a>
|
|
1750
1379
|
|
|
1751
|
-
|
|
1380
|
+
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.
|
|
1752
1381
|
|
|
1753
|
-
|
|
1382
|
+
The `ssh2` and `ssh2-streams` modules are very solid, high quality modules with a large user base. Most of the time, issues with those modules are due to client misconfiguration. It is therefore very important when trying to diagnose an issue to also check the documentation for both `ssh2` and `ssh2-streams`. While these modules have good defaults, the flexibility of the ssh2 protocol means that not all options are available by default. You may need to tweak the connection options, ssh2 algorithms and ciphers etc for some remote servers. The documentation for both the `ssh2` and `ssh2-streams` module is quite comprehensive and there is lots of valuable information in the issue logs.
|
|
1754
1383
|
|
|
1755
|
-
|
|
1756
|
-
`ssh2-streams` modules, providing a higher level `promise` based API. When you
|
|
1757
|
-
run into issues, it is important to try and determine where the issue lies -
|
|
1758
|
-
either in the ssh2-sftp-client module or the underlying `ssh2` and
|
|
1759
|
-
`ssh2-streams` modules. One way to do this is to first identify a minimal
|
|
1760
|
-
reproducible example which reproduces the issue. Once you have that, try to
|
|
1761
|
-
replicate the functionality just using the `ssh2` and `ssh2-streams` modules. If
|
|
1762
|
-
the issue still occurs, then you can be fairly confident it is something related
|
|
1763
|
-
to those later 2 modules and therefore and issue which should be referred to the
|
|
1764
|
-
maintainer of that module.
|
|
1384
|
+
If you run into an issue which is not repeatable with just the `ssh2` and `ssh2-streams` modules, then please log an issue against the `ssh2-sftp-client` module and I will investigate. Please note the next section on logging issues.
|
|
1765
1385
|
|
|
1766
|
-
The
|
|
1767
|
-
a large user base. Most of the time, issues with those modules are due to client
|
|
1768
|
-
misconfiguration. It is therefore very important when trying to diagnose an
|
|
1769
|
-
issue to also check the documentation for both `ssh2` and `ssh2-streams`. While
|
|
1770
|
-
these modules have good defaults, the flexibility of the ssh2 protocol means
|
|
1771
|
-
that not all options are available by default. You may need to tweak the
|
|
1772
|
-
connection options, ssh2 algorithms and ciphers etc for some remote servers. The
|
|
1773
|
-
documentation for both the `ssh2` and `ssh2-streams` module is quite
|
|
1774
|
-
comprehensive and there is lots of valuable information in the issue logs.
|
|
1386
|
+
Note also that in the repository there are two useful directories. The first is the examples directory, which contain some examples of using `ssh2-sftp-client` to perform common tasks. A few minutes reviewing these examples can provide that additional bit of detail to help fix any problems you are encountering.
|
|
1775
1387
|
|
|
1776
|
-
|
|
1777
|
-
`ssh2-streams` modules, then please log an issue against the `ssh2-sftp-client`
|
|
1778
|
-
module and I will investigate. Please note the next section on logging issues.
|
|
1388
|
+
The second directory is the tools directory. I have some very basic simple scripts in this directory which perform basic tasks using only the `ssh2` and `ssh2-streams` modules (no ssh2-sftp-client module). These can be useful when trying to determine if the issue is with the underlying `ssh2` and `ssh2-streams` modules.
|
|
1779
1389
|
|
|
1780
|
-
|
|
1781
|
-
the examples directory, which contain some examples of using `ssh2-sftp-client`
|
|
1782
|
-
to perform common tasks. A few minutes reviewing these examples can provide that
|
|
1783
|
-
additional bit of detail to help fix any problems you are encountering.
|
|
1390
|
+
## Common Errors<a id="sec-9-1"></a>
|
|
1784
1391
|
|
|
1785
|
-
|
|
1786
|
-
scripts in this directory which perform basic tasks using only the `ssh2` and
|
|
1787
|
-
`ssh2-streams` modules (no ssh2-sftp-client module). These can be useful when
|
|
1788
|
-
trying to determine if the issue is with the underlying `ssh2` and
|
|
1789
|
-
`ssh2-streams` modules.
|
|
1392
|
+
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.
|
|
1790
1393
|
|
|
1394
|
+
### Not returning the promise in a `then()` block<a id="sec-9-1-1"></a>
|
|
1791
1395
|
|
|
1792
|
-
|
|
1396
|
+
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 exmaple, the following will not do what you expect
|
|
1793
1397
|
|
|
1794
|
-
|
|
1398
|
+
```javascript
|
|
1399
|
+
sftp.connect(config)
|
|
1400
|
+
.then(() => {
|
|
1401
|
+
sftp.fastGet('foo.txt', 'bar.txt');
|
|
1402
|
+
}).then(rslt => {
|
|
1403
|
+
console.log(rslt);
|
|
1404
|
+
sftp.end();
|
|
1405
|
+
}).catch(e => {
|
|
1406
|
+
console.error(e.message);
|
|
1407
|
+
});
|
|
1408
|
+
```
|
|
1795
1409
|
|
|
1796
|
-
|
|
1797
|
-
Asyc/Await. These are by far the most common problem found in issues logged
|
|
1798
|
-
against this module. Please check for some of these before logging your
|
|
1799
|
-
issue.
|
|
1410
|
+
In the above code, the `sftp.end()` method will almost certainly be called before `sftp.gastGet()` 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
|
|
1800
1411
|
|
|
1412
|
+
```javascript
|
|
1413
|
+
sftp.connect(config)
|
|
1414
|
+
.then(() => {
|
|
1415
|
+
return sftp.fastGet('foo.txt', 'bar.txt');
|
|
1416
|
+
}).then(rslt => {
|
|
1417
|
+
console.log(rslt);
|
|
1418
|
+
return sftp.end();
|
|
1419
|
+
}).catch(e => {
|
|
1420
|
+
console.error(e.message);
|
|
1421
|
+
});
|
|
1422
|
+
```
|
|
1801
1423
|
|
|
1802
|
-
|
|
1424
|
+
Note the `return` statements. These ensure that the Promise returned by the client method is returned into the promise chain. It will be this promise the next block in the chain will wait on to be fulfilled before the next block is executed. Without the return statement, that block will return the default promise for that block, which essentially says *this block has been fulfilled*. What you really want is the promise which says *your sftp client method call has been fulfilled*.
|
|
1803
1425
|
|
|
1804
|
-
|
|
1426
|
+
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.
|
|
1805
1427
|
|
|
1806
|
-
|
|
1807
|
-
executed *asynchrnously*. When you call a method inside the `then()` block
|
|
1808
|
-
of a promise chain, it is critical that you return the Promise that call
|
|
1809
|
-
generates. Failing to do this will result in the `then()` block completing
|
|
1810
|
-
and your code starting execution of the next `then()`, `catch()` or
|
|
1811
|
-
`finally()` block before your promise has been fulfilled. For exmaple, the
|
|
1812
|
-
following will not do what you expect
|
|
1428
|
+
### Mixing Promise Chains and Async/Await<a id="sec-9-1-2"></a>
|
|
1813
1429
|
|
|
1814
|
-
|
|
1815
|
-
.then(() => {
|
|
1816
|
-
sftp.fastGet('foo.txt', 'bar.txt');
|
|
1817
|
-
}).then(rslt => {
|
|
1818
|
-
console.log(rslt);
|
|
1819
|
-
sftp.end();
|
|
1820
|
-
}).catch(e => {
|
|
1821
|
-
console.error(e.message);
|
|
1822
|
-
});
|
|
1430
|
+
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!)
|
|
1823
1431
|
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
.
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
the
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
rarely a great idea. While you can do this, it tends to create complicated
|
|
1860
|
-
and difficult to maintain code. Select one approach and stick with it. Both
|
|
1861
|
-
approaches are functionally equivalent, so there is no reason to mix up the
|
|
1862
|
-
two paradigms. My personal preference would be to use async/await as I think
|
|
1863
|
-
that is more *natural* for most developers. For example, the following is
|
|
1864
|
-
more complex and difficult to follow than necessary (and has a bug!)
|
|
1865
|
-
|
|
1866
|
-
sftp.connect(config)
|
|
1867
|
-
.then(() => {
|
|
1868
|
-
return sftp.cwd();
|
|
1869
|
-
}).then(async (d) => {
|
|
1870
|
-
console.log(`Remote directory is ${d}`);
|
|
1871
|
-
try {
|
|
1872
|
-
await sftp.fastGet(`${d}/foo.txt`, `./bar.txt`);
|
|
1873
|
-
}.catch(e => {
|
|
1874
|
-
console.error(e.message);
|
|
1875
|
-
});
|
|
1876
|
-
}).catch(e => {
|
|
1877
|
-
console.error(e.message);
|
|
1878
|
-
}).finally(() => {
|
|
1879
|
-
sftp.end();
|
|
1880
|
-
});
|
|
1881
|
-
|
|
1882
|
-
The main bug in the above code is the `then()` block is not returning the
|
|
1883
|
-
Promise generated by the call to `sftp.fastGet()`. What it is actually
|
|
1884
|
-
returning is a fulfilled promise which says the `then()` block has been run
|
|
1885
|
-
(note that the await'ed promise is not being returned and is therefore
|
|
1886
|
-
outside the main Promise chain). As a result, the `finally()` block will be
|
|
1887
|
-
executed before the await promise has been fulfilled.
|
|
1888
|
-
|
|
1889
|
-
Using async/await inside the promise chain has created unnecessary
|
|
1890
|
-
complexity and leads to incorrect assumptions regarding how the code will
|
|
1891
|
-
execute. A quick glance at the code is likely to give the impression that
|
|
1892
|
-
execution will wait for the `sftp.fastGet()` call to be fulfilled before
|
|
1893
|
-
continuing. This is not the case. The code would be more clearly expressed
|
|
1894
|
-
as either
|
|
1895
|
-
|
|
1896
|
-
sftp.connect(config)
|
|
1897
|
-
.then(() => {
|
|
1898
|
-
return sftp.cwd();
|
|
1899
|
-
}).then(d => {
|
|
1900
|
-
console.log(`remote dir ${d}`);
|
|
1901
|
-
return sftp.fastGet(`${d}/foot.txt`, 'bar.txt');
|
|
1902
|
-
}).catch(e => {
|
|
1903
|
-
console.error(e.message);
|
|
1904
|
-
}).finally(() => {
|
|
1905
|
-
return sftp.end();
|
|
1906
|
-
});
|
|
1432
|
+
```javascript
|
|
1433
|
+
sftp.connect(config)
|
|
1434
|
+
.then(() => {
|
|
1435
|
+
return sftp.cwd();
|
|
1436
|
+
}).then(async (d) => {
|
|
1437
|
+
console.log(`Remote directory is ${d}`);
|
|
1438
|
+
try {
|
|
1439
|
+
await sftp.fastGet(`${d}/foo.txt`, `./bar.txt`);
|
|
1440
|
+
}.catch(e => {
|
|
1441
|
+
console.error(e.message);
|
|
1442
|
+
});
|
|
1443
|
+
}).catch(e => {
|
|
1444
|
+
console.error(e.message);
|
|
1445
|
+
}).finally(() => {
|
|
1446
|
+
sftp.end();
|
|
1447
|
+
});
|
|
1448
|
+
```
|
|
1449
|
+
|
|
1450
|
+
The main bug in the above code is the `then()` block is not returning the Promise generated by the call to `sftp.fastGet()`. What it is actually returning is a fulfilled promise which says the `then()` block has been run (note that the await'ed promise is not being returned and is therefore outside the main Promise chain). As a result, the `finally()` block will be executed before the await promise has been fulfilled.
|
|
1451
|
+
|
|
1452
|
+
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
|
|
1453
|
+
|
|
1454
|
+
```javascript
|
|
1455
|
+
sftp.connect(config)
|
|
1456
|
+
.then(() => {
|
|
1457
|
+
return sftp.cwd();
|
|
1458
|
+
}).then(d => {
|
|
1459
|
+
console.log(`remote dir ${d}`);
|
|
1460
|
+
return sftp.fastGet(`${d}/foot.txt`, 'bar.txt');
|
|
1461
|
+
}).catch(e => {
|
|
1462
|
+
console.error(e.message);
|
|
1463
|
+
}).finally(() => {
|
|
1464
|
+
return sftp.end();
|
|
1465
|
+
});
|
|
1466
|
+
```
|
|
1907
1467
|
|
|
1908
1468
|
**or, using async/await**
|
|
1909
1469
|
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1470
|
+
```javascript
|
|
1471
|
+
async function doSftp() {
|
|
1472
|
+
try {
|
|
1473
|
+
let sftp = await sftp.connect(conf);
|
|
1474
|
+
let d = await sftp.cwd();
|
|
1475
|
+
console.log(`remote dir is ${d}`);
|
|
1476
|
+
await sftp.fastGet(`${d}/foo.txt`, 'bat.txt');
|
|
1477
|
+
} catch (e) {
|
|
1478
|
+
console.error(e.message);
|
|
1479
|
+
} finally () {
|
|
1480
|
+
await sftp.end();
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
```
|
|
1922
1484
|
|
|
1485
|
+
### Try/catch and Error Handlers<a id="sec-9-1-3"></a>
|
|
1923
1486
|
|
|
1924
|
-
|
|
1487
|
+
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.
|
|
1925
1488
|
|
|
1926
|
-
|
|
1489
|
+
The basic problem is that the try/catch block will have completed execution before the asynchronous code has completed. If the asynchronous code has not compleed, then there is a potential for it to raise an error. However, as the try/catch block has already completed, there is no *catch* waiting to catch the error. It will bubble up and probably result in your script exiting with an uncaught exception error.
|
|
1927
1490
|
|
|
1928
|
-
|
|
1929
|
-
signals, such as an error event. In general, you cannot use try/catch blocks
|
|
1930
|
-
for asynchronous code and expect errors to be caught by the `catch` block.
|
|
1931
|
-
Handling errors in asynchronous code is one of the key reasons we now have
|
|
1932
|
-
the Promise and async/await frameworks.
|
|
1491
|
+
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.
|
|
1933
1492
|
|
|
1934
|
-
|
|
1935
|
-
before the asynchronous code has completed. If the asynchronous code has not
|
|
1936
|
-
compleed, then there is a potential for it to raise an error. However, as
|
|
1937
|
-
the try/catch block has already completed, there is no *catch* waiting to
|
|
1938
|
-
catch the error. It will bubble up and probably result in your script
|
|
1939
|
-
exiting with an uncaught exception error.
|
|
1493
|
+
## Debugging Support<a id="sec-9-2"></a>
|
|
1940
1494
|
|
|
1941
|
-
|
|
1942
|
-
events will fire. Therefore, you cannot use a try/catch block to catch such
|
|
1943
|
-
event errors. Even creating an error handler which then throws an exception
|
|
1944
|
-
won't help as the key problem is that your try/catch block has already
|
|
1945
|
-
executed. There are a number of alternative ways to deal with this
|
|
1946
|
-
situation. However, the key symptom is that you see occasional uncaught
|
|
1947
|
-
error exceptions that cause your script to exit abnormally despite having
|
|
1948
|
-
try/catch blocks in your script. What you need to do is look at your code
|
|
1949
|
-
and find where errors are raised asynchronously and use an event handler or
|
|
1950
|
-
some other mechanism to manage any errors raised.
|
|
1495
|
+
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;
|
|
1951
1496
|
|
|
1497
|
+
```javascript
|
|
1498
|
+
config.debug = msg => {
|
|
1499
|
+
console.error(msg);
|
|
1500
|
+
};
|
|
1952
1501
|
|
|
1953
|
-
|
|
1502
|
+
```
|
|
1954
1503
|
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
You can add a `debug` property to the config object passed in to `connect()` to
|
|
1958
|
-
turn on debugging. This will generate quite a lot of output. The value of the
|
|
1959
|
-
property should be a function which accepts a single string argument. For example;
|
|
1960
|
-
|
|
1961
|
-
config.debug = msg => {
|
|
1962
|
-
console.error(msg);
|
|
1963
|
-
};
|
|
1504
|
+
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.
|
|
1964
1505
|
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
using shell redirection e.g.
|
|
1506
|
+
```shell
|
|
1507
|
+
node script.js 2> debug.log
|
|
1968
1508
|
|
|
1969
|
-
|
|
1509
|
+
```
|
|
1970
1510
|
|
|
1511
|
+
# Logging Issues<a id="sec-10"></a>
|
|
1971
1512
|
|
|
1972
|
-
|
|
1513
|
+
Please log an issue for all bugs, questions, feature and enhancement requests. Please ensure you include the module version, node version and platform.
|
|
1973
1514
|
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
Please log an issue for all bugs, questions, feature and enhancement
|
|
1977
|
-
requests. Please ensure you include the module version, node version and
|
|
1978
|
-
platform.
|
|
1979
|
-
|
|
1980
|
-
I am happy to try and help diagnose and fix any issues you encounter while using
|
|
1981
|
-
the `ssh2-sftp-client` module. However, I will only put in effort if you are
|
|
1982
|
-
prepared to put in the effort to provide the information necessary to reproduce
|
|
1983
|
-
the issue. Things which will help
|
|
1515
|
+
I am happy to try and help diagnose and fix any issues you encounter while using the `ssh2-sftp-client` module. However, I will only put in effort if you are prepared to put in the effort to provide the information necessary to reproduce the issue. Things which will help
|
|
1984
1516
|
|
|
1985
1517
|
- Node version you are using
|
|
1986
1518
|
- Version of ssh2-sftp-client you are using
|
|
1987
1519
|
- Platform your client is running on (Linux, macOS, Windows)
|
|
1988
1520
|
- Platform and software for the remote SFTP server when possible
|
|
1989
|
-
- Example of your code. By far, the most common issue is incorrect use of the
|
|
1990
|
-
module API. Example code can usually result in such issues being resolved very
|
|
1991
|
-
quickly.
|
|
1992
|
-
|
|
1993
|
-
Perhaps the best assistance is a minimal reproducible example of the issue. Once
|
|
1994
|
-
the issue can be readily reproduced, it can usually be fixed very quickly.
|
|
1521
|
+
- Example of your code. By far, the most common issue is incorrect use of the module API. Example code can usually result in such issues being resolved very quickly.
|
|
1995
1522
|
|
|
1523
|
+
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.
|
|
1996
1524
|
|
|
1997
|
-
<a id="
|
|
1525
|
+
# Pull Requests<a id="sec-11"></a>
|
|
1998
1526
|
|
|
1999
|
-
|
|
1527
|
+
Pull requests are always welcomed. However, please ensure your changes pass all tests and if your adding a new feature, that tests for that feature are included. Likewise, for new features or enhancements, please include any relevant documentation updates.
|
|
2000
1528
|
|
|
2001
|
-
|
|
2002
|
-
tests and if your adding a new feature, that tests for that feature are
|
|
2003
|
-
included. Likewise, for new features or enhancements, please include any
|
|
2004
|
-
relevant documentation updates.
|
|
1529
|
+
This module will adopt a standard semantic versioning policy. Please indicate in your pull request what level of change it represents i.e.
|
|
2005
1530
|
|
|
2006
|
-
|
|
2007
|
-
|
|
1531
|
+
- **Major:** Change to API or major change in functionality which will require an increase in major version number.
|
|
1532
|
+
- **Minor:** Minor change, enhancement or new feature which does not change existing API and will not break existing client code.
|
|
1533
|
+
- **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
|
|
2008
1534
|
|
|
2009
|
-
|
|
2010
|
-
increase in major version number.
|
|
2011
|
-
- **Minor:** Minor change, enhancement or new feature which does not change
|
|
2012
|
-
existing API and will not break existing client code.
|
|
2013
|
-
- **Bug Fix:** No change to functionality or features. Simple fix of an existing
|
|
2014
|
-
bug.
|
|
1535
|
+
# Contributors<a id="sec-12"></a>
|
|
2015
1536
|
|
|
1537
|
+
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.
|
|
2016
1538
|
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
# Contributors
|
|
2020
|
-
|
|
2021
|
-
This module was initially written by jyu213. On August 23rd, 2019, theophilusx
|
|
2022
|
-
took over responsibility for maintaining this module. A number of other people
|
|
2023
|
-
have contributed to this module, but until now, this was not tracked. My
|
|
2024
|
-
intention is to credit anyone who contributes going forward.
|
|
1539
|
+
Thanks to the following for their contributions -
|
|
2025
1540
|
|
|
2026
1541
|
- **jyu213:** Original author
|
|
2027
1542
|
- **theophilusx:** Current maintainer
|
|
2028
1543
|
- **henrytk:** Documentation fix
|
|
2029
1544
|
- **waldyrious:** Documentation fixes
|
|
2030
1545
|
- **james-pellow:** Cleanup and fix for connect method logic
|
|
2031
|
-
|
|
1546
|
+
- **jhorbulyk:** Contributed posixRename() functionality
|