ssh2-sftp-client 12.0.0 → 12.0.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 +155 -143
- package/README.org +50 -31
- package/package.json +1 -1
- package/src/index.js +26 -11
- package/src/utils.js +2 -2
package/README.md
CHANGED
|
@@ -1,68 +1,69 @@
|
|
|
1
|
-
- [Overview](#
|
|
2
|
-
- [Version 12.0.0 Changes](#
|
|
3
|
-
- [
|
|
4
|
-
- [
|
|
5
|
-
- [
|
|
6
|
-
- [
|
|
7
|
-
|
|
8
|
-
- [
|
|
9
|
-
|
|
10
|
-
- [
|
|
11
|
-
- [
|
|
12
|
-
- [
|
|
13
|
-
- [
|
|
14
|
-
- [
|
|
15
|
-
- [
|
|
16
|
-
- [
|
|
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
|
-
- [How can I
|
|
45
|
-
- [How can I
|
|
46
|
-
- [
|
|
47
|
-
- [
|
|
48
|
-
- [
|
|
49
|
-
- [
|
|
50
|
-
- [
|
|
51
|
-
- [
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
- [
|
|
55
|
-
- [
|
|
56
|
-
- [
|
|
57
|
-
- [
|
|
58
|
-
|
|
59
|
-
- [
|
|
60
|
-
- [
|
|
61
|
-
- [
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
1
|
+
- [Overview](#org4015600)
|
|
2
|
+
- [Version 12.0.0 Changes](#org9ba2cfa)
|
|
3
|
+
- [Version 12.0.1 Changes](#org658a511)
|
|
4
|
+
- [Background](#org4d07682)
|
|
5
|
+
- [Installation](#orgb1c417b)
|
|
6
|
+
- [Basic Usage](#orgbddd3fd)
|
|
7
|
+
- [Documentation](#org8c374be)
|
|
8
|
+
- [Specifying Paths](#org95c6689)
|
|
9
|
+
- [Methods](#org8437ec6)
|
|
10
|
+
- [new SftpClient(name, callbacks) ===> SFTP client object](#org210a6e7)
|
|
11
|
+
- [connect(config) ===> SFTP object](#org9c48be5)
|
|
12
|
+
- [list(path, filter) ==> Array[object]](#org190df3b)
|
|
13
|
+
- [exists(path) ==> boolean](#org7d245b2)
|
|
14
|
+
- [stat(path) ==> object](#orga6a58bd)
|
|
15
|
+
- [get(path, dst, options) ==> String|Stream|Buffer](#orgc3ea293)
|
|
16
|
+
- [fastGet(remotePath, localPath, options) ===> string](#org6261e44)
|
|
17
|
+
- [put(src, remotePath, options) ==> string](#org6c23ce2)
|
|
18
|
+
- [fastPut(localPath, remotePath, options) ==> string](#orgfda72d4)
|
|
19
|
+
- [append(input, remotePath, options) ==> string](#org6c67ab9)
|
|
20
|
+
- [mkdir(path, recursive) ==> string](#org99a7d1c)
|
|
21
|
+
- [rmdir(path, recursive) ==> string](#org31ab7d2)
|
|
22
|
+
- [delete(path, noErrorOK) ==> string](#org3a4a52a)
|
|
23
|
+
- [rename(fromPath, toPath) ==> string](#org9fbf1b2)
|
|
24
|
+
- [posixRename(fromPath, toPath) ==> string](#org0be5342)
|
|
25
|
+
- [chmod(path, mode) ==> string](#org9227f61)
|
|
26
|
+
- [realPath(path) ===> string](#orgdbe4247)
|
|
27
|
+
- [cwd() ==> string](#orge856d52)
|
|
28
|
+
- [uploadDir(srcDir, dstDir, options) ==> string](#org9337523)
|
|
29
|
+
- [downloadDir(srcDir, dstDir, options) ==> string](#org30526ff)
|
|
30
|
+
- [createReadStream(remotePath, options)) ==> stream object](#org98dacc6)
|
|
31
|
+
- [createWriteStream(remotePath, options) ==> stream object](#orgf250341)
|
|
32
|
+
- [rcopy(srcPath, dstPath) ==> string](#org16daa1a)
|
|
33
|
+
- [end() ==> boolean](#orgb329f2e)
|
|
34
|
+
- [Add and Remove Listeners](#org80dffcb)
|
|
35
|
+
- [Platform Quirks & Warnings](#org6e83e30)
|
|
36
|
+
- [Server Capabilities](#org75bad4f)
|
|
37
|
+
- [Issues with `fastPut()` and `fastGet()` Methods](#org3fe5a31)
|
|
38
|
+
- [Promises, Events & Managing Exceptions](#org9fd1871)
|
|
39
|
+
- [Adding Custom Handlers](#org256ad96)
|
|
40
|
+
- [Windows Based Servers](#org1eef7b1)
|
|
41
|
+
- [Don't Re-use SftpClient Objects](#org37ac59e)
|
|
42
|
+
- [FAQ](#orgc01e9f3)
|
|
43
|
+
- [Remote server drops connections with only an end event](#orgcbb84bd)
|
|
44
|
+
- [How can I pass writeable stream as dst for get method?](#orgb7ba758)
|
|
45
|
+
- [How can I upload files without having to specify a password?](#orgf9d915c)
|
|
46
|
+
- [How can I connect through a Socks Proxy](#orgc977636)
|
|
47
|
+
- [Timeout while waiting for handshake or handshake errors](#org9caf1fc)
|
|
48
|
+
- [How can I limit upload/download speed](#orgc99cfed)
|
|
49
|
+
- [Connection hangs or fails for larger files](#orgff1cf60)
|
|
50
|
+
- [Typescript definition file out of date](#org312104a)
|
|
51
|
+
- [Examples](#org409c3ef)
|
|
52
|
+
- [Troubleshooting](#orge118375)
|
|
53
|
+
- [Common Errors](#org39ed676)
|
|
54
|
+
- [Not returning the promise in a `then()` block](#orge4fdace)
|
|
55
|
+
- [Mixing Promise Chains and Async/Await](#org613b5f0)
|
|
56
|
+
- [Try/catch and Error Handlers](#org3df8f9d)
|
|
57
|
+
- [Server Differences](#org295cfa3)
|
|
58
|
+
- [Avoid Concurrent Operations](#org5ee2ce1)
|
|
59
|
+
- [Debugging Support](#orgec0b62a)
|
|
60
|
+
- [Logging Issues](#org41f81c7)
|
|
61
|
+
- [Pull Requests](#orgd153e6d)
|
|
62
|
+
- [Contributors](#org267dc2a)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
<a id="org4015600"></a>
|
|
66
67
|
|
|
67
68
|
# Overview
|
|
68
69
|
|
|
@@ -70,62 +71,73 @@ This package provides the class SftpClient, an SFTP client for node.js. It is a
|
|
|
70
71
|
|
|
71
72
|
Documentation on the methods and available options in the underlying modules can be found on the [SSH2](https://github.com/mscdex/ssh2) project pages. As the ssh2-sftp-client package is just a wrapper around the `ssh2` module, you will find lots of useful information, tips and examples in the `ssh2` repository.
|
|
72
73
|
|
|
73
|
-
Current stable release is \*v12.0.
|
|
74
|
+
Current stable release is \*v12.0.1.
|
|
74
75
|
|
|
75
|
-
Code has been tested against Node versions 20.
|
|
76
|
+
Code has been tested against Node versions 20.19.2, 22.16.0, 23.11.1 and 24.2.0. Node versions prior to v20.x are not supported.
|
|
76
77
|
|
|
77
78
|
If you find this module useful and you would like to support the on-going maintenance and support of users, please consider making a small [donation](https://square.link/u/gB2kSdkY?src=embed).
|
|
78
79
|
|
|
79
80
|
|
|
80
|
-
<a id="
|
|
81
|
+
<a id="org9ba2cfa"></a>
|
|
81
82
|
|
|
82
83
|
## Version 12.0.0 Changes
|
|
83
84
|
|
|
84
|
-
The big and breaking change in this version is the removal of the connection retry code. This module no longer supports automatic connection retries when establishing the initial connection. This functionality was removed for a number of reasons. The main reasons are that it significantly complicated the connection handling code and error handling in particular and it was seldom useful. In most cases, if the
|
|
85
|
+
The big and breaking change in this version is the removal of the connection retry code. This module no longer supports automatic connection retries when establishing the initial connection. This functionality was removed for a number of reasons. The main reasons are that it significantly complicated the connection handling code and error handling in particular and it was seldom useful. In most cases, if the connection failed on the first attempt, it would also fail in all subsequent attempts and often resulted in just extending the time to failure as multiple retries were attempted.
|
|
85
86
|
|
|
86
|
-
|
|
87
|
+
Another reason this functionality was removed is because if you really want it, it is straight forward to create your own wrapper around the connect call which uses one of the mnay promise retry packages available. It is much simpler to add a retry mechanism for a specific application than it is to implement a flexible solution which supports all possible combination of connection options, authentication methods, proxies etc.
|
|
87
88
|
|
|
88
89
|
|
|
89
|
-
<a id="
|
|
90
|
+
<a id="org658a511"></a>
|
|
91
|
+
|
|
92
|
+
## Version 12.0.1 Changes
|
|
93
|
+
|
|
94
|
+
Bug Fixes
|
|
95
|
+
|
|
96
|
+
- Fix typo error in specification of generic error code in error handler. Typo was causing an undefined symbol error.
|
|
97
|
+
- Fix bug in stream error handling. Read and write streams had an error handler defined with 'once' rather than 'on'. This caused a bug when an API call caused more than one error. For example, a network connectivity error would cause a stream timeout error followed by a no response from remote host error when the error handler attempted to cleanup and destroy the remote stream. The second error whould not be handled and would bubble up to the parent node process, which would then exit with an unhandled exception error.
|
|
98
|
+
- Added calls to \_resetEventFlags() to finally clauses to ensure event flags are reset when temp listeners are removed. Without this change, global listeners would not fire when they should after their has been an earlier handled error in the last API call.
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
<a id="org4d07682"></a>
|
|
90
102
|
|
|
91
103
|
## Background
|
|
92
104
|
|
|
93
|
-
In basic terms `ssh2-sftp-client` is a simple wrapper around the `ssh2` package which provides a promise
|
|
105
|
+
In basic terms `ssh2-sftp-client` is a simple wrapper around the `ssh2` package which provides a promise based API for interacting with a remote SFTP server . The `ssh2` package provides an event based API for interacting with the `ssh` protocol. The `ssh2-sftp-client` package uses the `sftp` subsystem of this protocol to implement the basic operations typically associated with an `sftp` client.
|
|
94
106
|
|
|
95
|
-
Wrapping an event based API with a
|
|
107
|
+
Wrapping an event based API with a promise based API comes with a number of challenges. In particular, efficiently and reliably managing events within the context of asynchronous code execution. This package uses the following strategies;
|
|
96
108
|
|
|
97
|
-
- All direct interactions with the `ssh2` API are wrapped in promise objects. When the API call succeeds, the
|
|
109
|
+
- All direct interactions with the `ssh2` API are wrapped in promise objects. When the API call succeeds, the associated promise will be successfully resolved. When an error occurs, the promise is rejected.
|
|
98
110
|
|
|
99
111
|
- An error can either be due to a low level network error, such as a lost connection to the remote server or due to an operational error, such as a file not existing or not having the appropriate permissions for access.
|
|
100
112
|
|
|
101
|
-
- Each of the available `SftpClient` methods wrap the method call inside a promise. In
|
|
113
|
+
- Each of the available `SftpClient` methods wrap the method call inside a promise. In creating each promise, the class adds temporary event listeners for the error, end and close events and links those liseners to the method's promise via its `reject()` method.
|
|
102
114
|
|
|
103
|
-
- If a promise is waiting to be fulfilled when
|
|
115
|
+
- If a promise is waiting to be fulfilled when an error occurs, the error will be communicated back to the client code via the rejected promise.
|
|
104
116
|
|
|
105
|
-
- When the `ssh2` emitter raises an event outside the context of any promise, that event will be handled by global event handlers. By default, these event handlers will log the event and will invalidate any existing socket connection objects, preventing any further API calls until a new connection is
|
|
117
|
+
- When the `ssh2` emitter raises an event outside the context of any promise, that event will be handled by global event handlers. By default, these event handlers will log the event and will invalidate any existing socket connection objects, preventing any further API calls until a new connection is established.
|
|
106
118
|
|
|
107
|
-
- The `SftpClient` class constructor supports an optoinal second argument which is an objedct
|
|
119
|
+
- The `SftpClient` class constructor supports an optoinal second argument which is an objedct which can have any of the three properties error, end and close. The values associated with these properties are callback functions that will be run if an error event with?he same name as the property is raised and has not been handled by one of the temporary event handlers. The error callback should have a single parameter, the error raised by the event. The other callbacks have no arguments.
|
|
108
120
|
|
|
109
|
-
The need for both global listeners and temporary promise listeners is because network end, close or error events can occur at any time, including in-
|
|
121
|
+
The need for both global listeners and temporary promise listeners is because network end, close or error events can occur at any time, including in-between API calls. During an API call, a promise is active and can be used to communicate event information back to the calling code via normal promise communication channels i.e. async/await with try/catch or promise chain's then/catch mechanism. However, outside API calls, no promise exists and there is no reliable mechanism to return error and other event information back to calling code. You cannot reliably use try/cdatch to catch errors thrown inside event listenrs as you lack control over when the listener code runs. Your try/catch block can easily complete before the error is raised as there is no equivalent *await* type functionality in this situation.
|
|
110
122
|
|
|
111
123
|
As there is no simple default way to return error and other event information back to the calling code, `ssh2-sftp-client` doesn't try to. Instead, the default action is to just log the event information and invalidate any existing sftp connections. This strategy is often sufficient for many use cases. For those cases where it isn't, client code can pass in end, close and/or error listener functions when instantiating the `SftpClient` object. If provided, these listners will be executed whenever the default global listeners are executed, which is whenever the `ssh2` event emitter raises an end, close or error event which is not handled by one of the temporary promise linked event listeners.
|
|
112
124
|
|
|
113
|
-
Version 11 of `ssh2-sftp-client` also changes the behaviour of the temporary promise linked *end* and *close* listeners. Prior to
|
|
125
|
+
Version 11 of `ssh2-sftp-client` also changes the behaviour of the temporary promise linked *end* and *close* listeners. Prior to version 11, these listeners did not reject promises. They would only invalidate the underlying `ssh` connection object. Only the *error* listener would actually reject the associated promise. This was done because you cannot guarantee the order in which events are responded to.
|
|
114
126
|
|
|
115
|
-
In most cases, events will occur in the order *error*, *end* and then *close*. The error event would contain details about the cause of the error while the end and close events just
|
|
127
|
+
In most cases, events will occur in the order *error*, *end* and then *close*. The error event would contain details about the cause of the error while the end and close events just communicate that these events have been raised. The normal flow would be
|
|
116
128
|
|
|
117
|
-
- Error event occurs including error cause description. Listener catches event, creates an error object and calls the associated
|
|
129
|
+
- Error event occurs including error cause description. Listener catches event, creates an error object and calls the associated promise's reject function to reject the promise. The calling process receives a rejected promise object.
|
|
118
130
|
|
|
119
131
|
- End event occurs. The end listener catches the event and marks the connection object as invalid as the socket connection has been ended. There is no need to call the reject method of the associated promise as it has already been called by the error listener and you can only call one promise resolution function.
|
|
120
132
|
|
|
121
|
-
- Close event occurs. This event means the socket connection has been closed. The listener will ensure any connection information has been invalidated.
|
|
133
|
+
- Close event occurs. This event means the socket connection has been closed. The listener will ensure any connection information has been invalidated. Again, there is no need to call the reject method of the associated promise as it has already been called by the error listener.
|
|
122
134
|
|
|
123
|
-
In some cases, no
|
|
135
|
+
In some cases, no end event is raised and you only get an error event followed by a close event. In versions of `ssh2-sftp-client` prior to version 11, neither the end or the close listeners attempted to call the reject method of the associated promise. It was assumed that all sftp servers would raise an error event whenever a connection was unexpectedly ended or closed. Unfortunately, it turns out some sftp servers are not well behaved and will terminate the connection without providing any error information or raising an error event. When this occurred in versions prior to version 11, it could result in either an API call hanging because its associated promise never gets rejected or resolved or the call gets rejected with a timeout error aftrer a significant delay.
|
|
124
136
|
|
|
125
|
-
In order to handle the possible hanging issue in version 11, the temporary promise linked end and close listeners have been updated to always call the promise's reject function if they fire. While this works, it can cause a minor issue. As wse cannot gurantee the order in which events are
|
|
137
|
+
In order to handle the possible hanging issue in version 11, the temporary promise linked end and close listeners have been updated to always call the promise's reject function if they fire. While this works, it can cause a minor issue. As wse cannot gurantee the order in which events are responded to by listeners, it is possible that either the end or close listener may be executed before the error listener. When this occurs, the promise is rejected, but the only information wse have at that point is that the promise wsas reject due to either an end or close event. We don't yet have any details regarding what error has caused the unexpected end or close event. Furthermore, because only the first promise resolution function call has any effect, calling reject within the error listener (assuming an error event does eventually arrive) has no effect and does not communicate error information back to the caller. This means that in some circumstances, especially when working with some poorly behaved sftp servers, an sftp connection will be lost/closed with no indication as to reason. This can make diagnosis and bug tracking frustrating.
|
|
126
138
|
|
|
127
139
|
|
|
128
|
-
<a id="
|
|
140
|
+
<a id="orgb1c417b"></a>
|
|
129
141
|
|
|
130
142
|
# Installation
|
|
131
143
|
|
|
@@ -134,7 +146,7 @@ npm install ssh2-sftp-client
|
|
|
134
146
|
```
|
|
135
147
|
|
|
136
148
|
|
|
137
|
-
<a id="
|
|
149
|
+
<a id="orgbddd3fd"></a>
|
|
138
150
|
|
|
139
151
|
# Basic Usage
|
|
140
152
|
|
|
@@ -157,7 +169,7 @@ sftp.connect({
|
|
|
157
169
|
```
|
|
158
170
|
|
|
159
171
|
|
|
160
|
-
<a id="
|
|
172
|
+
<a id="org8c374be"></a>
|
|
161
173
|
|
|
162
174
|
# Documentation
|
|
163
175
|
|
|
@@ -168,7 +180,7 @@ All the methods will return a Promise, except for `on(), ~removeListener()`, `cr
|
|
|
168
180
|
Note that I don't use Typescript and I don't maintain any typescript definition files. There are some typescript type definition files for this module, but they are maintained separately and have nothing to do with this project. Therefore, please do not log any issues arising from the use of these definition files with this project. Instead, refer your issues to the maintainers of those modules.
|
|
169
181
|
|
|
170
182
|
|
|
171
|
-
<a id="
|
|
183
|
+
<a id="org95c6689"></a>
|
|
172
184
|
|
|
173
185
|
## Specifying Paths
|
|
174
186
|
|
|
@@ -201,12 +213,12 @@ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
|
|
|
201
213
|
This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
|
|
202
214
|
|
|
203
215
|
|
|
204
|
-
<a id="
|
|
216
|
+
<a id="org8437ec6"></a>
|
|
205
217
|
|
|
206
218
|
## Methods
|
|
207
219
|
|
|
208
220
|
|
|
209
|
-
<a id="
|
|
221
|
+
<a id="org210a6e7"></a>
|
|
210
222
|
|
|
211
223
|
### new SftpClient(name, callbacks) ===> SFTP client object
|
|
212
224
|
|
|
@@ -246,7 +258,7 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
|
|
|
246
258
|
```
|
|
247
259
|
|
|
248
260
|
|
|
249
|
-
<a id="
|
|
261
|
+
<a id="org9c48be5"></a>
|
|
250
262
|
|
|
251
263
|
### connect(config) ===> SFTP object
|
|
252
264
|
|
|
@@ -310,7 +322,7 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
310
322
|
```
|
|
311
323
|
|
|
312
324
|
|
|
313
|
-
<a id="
|
|
325
|
+
<a id="org190df3b"></a>
|
|
314
326
|
|
|
315
327
|
### list(path, filter) ==> Array[object]
|
|
316
328
|
|
|
@@ -371,7 +383,7 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
371
383
|
```
|
|
372
384
|
|
|
373
385
|
|
|
374
|
-
<a id="
|
|
386
|
+
<a id="org7d245b2"></a>
|
|
375
387
|
|
|
376
388
|
### exists(path) ==> boolean
|
|
377
389
|
|
|
@@ -407,7 +419,7 @@ Tests to see if remote file or directory exists. Returns type of remote object i
|
|
|
407
419
|
```
|
|
408
420
|
|
|
409
421
|
|
|
410
|
-
<a id="
|
|
422
|
+
<a id="orga6a58bd"></a>
|
|
411
423
|
|
|
412
424
|
### stat(path) ==> object
|
|
413
425
|
|
|
@@ -458,7 +470,7 @@ Returns the attributes associated with the object pointed to by `path`.
|
|
|
458
470
|
```
|
|
459
471
|
|
|
460
472
|
|
|
461
|
-
<a id="
|
|
473
|
+
<a id="orgc3ea293"></a>
|
|
462
474
|
|
|
463
475
|
### get(path, dst, options) ==> String|Stream|Buffer
|
|
464
476
|
|
|
@@ -514,7 +526,7 @@ In general, if you're going to pass in a string as the destination, you are bett
|
|
|
514
526
|
- **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'.
|
|
515
527
|
|
|
516
528
|
|
|
517
|
-
<a id="
|
|
529
|
+
<a id="org6261e44"></a>
|
|
518
530
|
|
|
519
531
|
### fastGet(remotePath, localPath, options) ===> string
|
|
520
532
|
|
|
@@ -559,7 +571,7 @@ Bottom line, when it works, it tends to work reliably. However, for many servers
|
|
|
559
571
|
```
|
|
560
572
|
|
|
561
573
|
|
|
562
|
-
<a id="
|
|
574
|
+
<a id="org6c23ce2"></a>
|
|
563
575
|
|
|
564
576
|
### put(src, remotePath, options) ==> string
|
|
565
577
|
|
|
@@ -609,7 +621,7 @@ Upload data from local system to remote server. If the `src` argument is a strin
|
|
|
609
621
|
- **Tip:** If the src argument is a path string, consider just using `fastPut()`.
|
|
610
622
|
|
|
611
623
|
|
|
612
|
-
<a id="
|
|
624
|
+
<a id="orgfda72d4"></a>
|
|
613
625
|
|
|
614
626
|
### fastPut(localPath, remotePath, options) ==> string
|
|
615
627
|
|
|
@@ -655,7 +667,7 @@ Bottom line, when it works, it tends to work well. However, when it doesn't work
|
|
|
655
667
|
```
|
|
656
668
|
|
|
657
669
|
|
|
658
|
-
<a id="
|
|
670
|
+
<a id="org6c67ab9"></a>
|
|
659
671
|
|
|
660
672
|
### append(input, remotePath, options) ==> string
|
|
661
673
|
|
|
@@ -699,7 +711,7 @@ Append the `input` data to an existing remote file. There is no integrity checki
|
|
|
699
711
|
```
|
|
700
712
|
|
|
701
713
|
|
|
702
|
-
<a id="
|
|
714
|
+
<a id="org99a7d1c"></a>
|
|
703
715
|
|
|
704
716
|
### mkdir(path, recursive) ==> string
|
|
705
717
|
|
|
@@ -727,7 +739,7 @@ Create a new directory. If the recursive flag is set to true, the method will cr
|
|
|
727
739
|
```
|
|
728
740
|
|
|
729
741
|
|
|
730
|
-
<a id="
|
|
742
|
+
<a id="org31ab7d2"></a>
|
|
731
743
|
|
|
732
744
|
### rmdir(path, recursive) ==> string
|
|
733
745
|
|
|
@@ -757,7 +769,7 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
|
|
|
757
769
|
```
|
|
758
770
|
|
|
759
771
|
|
|
760
|
-
<a id="
|
|
772
|
+
<a id="org3a4a52a"></a>
|
|
761
773
|
|
|
762
774
|
### delete(path, noErrorOK) ==> string
|
|
763
775
|
|
|
@@ -786,7 +798,7 @@ Delete a file on the remote server.
|
|
|
786
798
|
```
|
|
787
799
|
|
|
788
800
|
|
|
789
|
-
<a id="
|
|
801
|
+
<a id="org9fbf1b2"></a>
|
|
790
802
|
|
|
791
803
|
### rename(fromPath, toPath) ==> string
|
|
792
804
|
|
|
@@ -815,7 +827,7 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
|
|
|
815
827
|
```
|
|
816
828
|
|
|
817
829
|
|
|
818
|
-
<a id="
|
|
830
|
+
<a id="org0be5342"></a>
|
|
819
831
|
|
|
820
832
|
### posixRename(fromPath, toPath) ==> string
|
|
821
833
|
|
|
@@ -842,7 +854,7 @@ client.connect(config)
|
|
|
842
854
|
```
|
|
843
855
|
|
|
844
856
|
|
|
845
|
-
<a id="
|
|
857
|
+
<a id="org9227f61"></a>
|
|
846
858
|
|
|
847
859
|
### chmod(path, mode) ==> string
|
|
848
860
|
|
|
@@ -871,7 +883,7 @@ Change the mode (read, write or execute permissions) of a remote file or directo
|
|
|
871
883
|
```
|
|
872
884
|
|
|
873
885
|
|
|
874
|
-
<a id="
|
|
886
|
+
<a id="orgdbe4247"></a>
|
|
875
887
|
|
|
876
888
|
### realPath(path) ===> string
|
|
877
889
|
|
|
@@ -882,14 +894,14 @@ Converts a relative path to an absolute path on the remote server. This method i
|
|
|
882
894
|
- **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
|
|
883
895
|
|
|
884
896
|
|
|
885
|
-
<a id="
|
|
897
|
+
<a id="orge856d52"></a>
|
|
886
898
|
|
|
887
899
|
### cwd() ==> string
|
|
888
900
|
|
|
889
901
|
Returns what the server believes is the current remote working directory.
|
|
890
902
|
|
|
891
903
|
|
|
892
|
-
<a id="
|
|
904
|
+
<a id="org9337523"></a>
|
|
893
905
|
|
|
894
906
|
### uploadDir(srcDir, dstDir, options) ==> string
|
|
895
907
|
|
|
@@ -958,7 +970,7 @@ The `useFastput` option is a boolean option. If `true`, the method will use the
|
|
|
958
970
|
```
|
|
959
971
|
|
|
960
972
|
|
|
961
|
-
<a id="
|
|
973
|
+
<a id="org30526ff"></a>
|
|
962
974
|
|
|
963
975
|
### downloadDir(srcDir, dstDir, options) ==> string
|
|
964
976
|
|
|
@@ -1023,7 +1035,7 @@ If the `useFastget` property is set to `true`, the method will use `fastGet()` t
|
|
|
1023
1035
|
```
|
|
1024
1036
|
|
|
1025
1037
|
|
|
1026
|
-
<a id="
|
|
1038
|
+
<a id="org98dacc6"></a>
|
|
1027
1039
|
|
|
1028
1040
|
### createReadStream(remotePath, options)) ==> stream object
|
|
1029
1041
|
|
|
@@ -1040,7 +1052,7 @@ Returns a read stream object which is attached to the remote file specified by t
|
|
|
1040
1052
|
- **end:** Position to stop reading bytes (inclusive).
|
|
1041
1053
|
|
|
1042
1054
|
|
|
1043
|
-
<a id="
|
|
1055
|
+
<a id="orgf250341"></a>
|
|
1044
1056
|
|
|
1045
1057
|
### createWriteStream(remotePath, options) ==> stream object
|
|
1046
1058
|
|
|
@@ -1055,7 +1067,7 @@ Returns a write stream object which is attached to the remote file specified in
|
|
|
1055
1067
|
- **start:** Byte position to start writing from (inclusive). May require changing flag to 'r+'.
|
|
1056
1068
|
|
|
1057
1069
|
|
|
1058
|
-
<a id="
|
|
1070
|
+
<a id="org16daa1a"></a>
|
|
1059
1071
|
|
|
1060
1072
|
### rcopy(srcPath, dstPath) ==> string
|
|
1061
1073
|
|
|
@@ -1065,7 +1077,7 @@ Perform a remote file copy. The file identified by the `srcPath` argument will b
|
|
|
1065
1077
|
- **dstPath:** Path to where the copy will be created specified as a string
|
|
1066
1078
|
|
|
1067
1079
|
|
|
1068
|
-
<a id="
|
|
1080
|
+
<a id="orgb329f2e"></a>
|
|
1069
1081
|
|
|
1070
1082
|
### end() ==> boolean
|
|
1071
1083
|
|
|
@@ -1089,7 +1101,7 @@ Ends the current client session, releasing the client socket and associated reso
|
|
|
1089
1101
|
```
|
|
1090
1102
|
|
|
1091
1103
|
|
|
1092
|
-
<a id="
|
|
1104
|
+
<a id="org80dffcb"></a>
|
|
1093
1105
|
|
|
1094
1106
|
### Add and Remove Listeners
|
|
1095
1107
|
|
|
@@ -1110,12 +1122,12 @@ Although normally not required, you can add and remove custom listeners on the s
|
|
|
1110
1122
|
Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
|
|
1111
1123
|
|
|
1112
1124
|
|
|
1113
|
-
<a id="
|
|
1125
|
+
<a id="org6e83e30"></a>
|
|
1114
1126
|
|
|
1115
1127
|
# Platform Quirks & Warnings
|
|
1116
1128
|
|
|
1117
1129
|
|
|
1118
|
-
<a id="
|
|
1130
|
+
<a id="org75bad4f"></a>
|
|
1119
1131
|
|
|
1120
1132
|
## Server Capabilities
|
|
1121
1133
|
|
|
@@ -1124,7 +1136,7 @@ All SFTP servers and platforms are not equal. Some facilities provided by `ssh2-
|
|
|
1124
1136
|
One way to determine whether an issue you are encountering is due to `ssh2-sftp-client` or due to the remote server or server platform is to use a simple CLI sftp program, such as openSSH's sftp command. If you observe the same behaviour using plain `sftp` on the command line, the issue is likely due to server or remote platform limitations. Note that you should not use a GUI sftp client, like `Filezilla` or `winSCP` as such GUI programs often attempt to hide these server and platform incompatibilities and will take additional steps to simulate missing functionality etc. You want to use a CLI program which does as little as possible.
|
|
1125
1137
|
|
|
1126
1138
|
|
|
1127
|
-
<a id="
|
|
1139
|
+
<a id="org3fe5a31"></a>
|
|
1128
1140
|
|
|
1129
1141
|
## Issues with `fastPut()` and `fastGet()` Methods
|
|
1130
1142
|
|
|
@@ -1133,7 +1145,7 @@ The `fastPut()` and `fastGet()` methods are known to be somewhat dependent on SF
|
|
|
1133
1145
|
To see an example of the type of issues you can observe with `fastPut()` or `fastGet()`, have a look at [issue 407](https://github.com/theophilusx/ssh2-sftp-client/issues/407), which describes the experiences of one user. Bottom line, when it works, it tends to work well and be significantly faster than using just `get()` or `put()`. However, when developing code to run against different SFTP servers, especially where you are unable to test against each server, you are likely better off just using `get()` and `put()` or structuring your code so that users can select which method to use (this is what `ssh2-sftp-client` does - for example, see the `!downloadDir()` and `uploadDir()` methods.
|
|
1134
1146
|
|
|
1135
1147
|
|
|
1136
|
-
<a id="
|
|
1148
|
+
<a id="org9fd1871"></a>
|
|
1137
1149
|
|
|
1138
1150
|
## Promises, Events & Managing Exceptions
|
|
1139
1151
|
|
|
@@ -1152,14 +1164,14 @@ The other area where additional events are fired is during the end() call. To de
|
|
|
1152
1164
|
In addition to the promise based event handlers, `ssh2-sftp-client` also implements global event handlers which will catch any `error`, `end` or `close` events. Essentially, these global handlers only reset the `sftp` property of the client object, effectively ensuring any subsequent calls are rejected and in the case of an error, send the error to the console.
|
|
1153
1165
|
|
|
1154
1166
|
|
|
1155
|
-
<a id="
|
|
1167
|
+
<a id="org256ad96"></a>
|
|
1156
1168
|
|
|
1157
1169
|
### Adding Custom Handlers
|
|
1158
1170
|
|
|
1159
1171
|
While the above strategies appear to work for the majority of use cases, there are always going to be edge cases which require more flexible or powerful event handling. To support this, the `on()` and `removeListener()` methods are provided. Any event listener added using the `on()` method will be added at the beginning of the list of handlers for that event, ensuring it will be called before any global or promise local events. See the documentation for the `on()` method for details.
|
|
1160
1172
|
|
|
1161
1173
|
|
|
1162
|
-
<a id="
|
|
1174
|
+
<a id="org1eef7b1"></a>
|
|
1163
1175
|
|
|
1164
1176
|
## Windows Based Servers
|
|
1165
1177
|
|
|
@@ -1168,7 +1180,7 @@ It appears that when the sftp server is running on Windows, a *ECONNRESET* error
|
|
|
1168
1180
|
The best way to avoid this issue is to not re-use client objects. Always generate a new sftp client object for each new connection.
|
|
1169
1181
|
|
|
1170
1182
|
|
|
1171
|
-
<a id="
|
|
1183
|
+
<a id="org37ac59e"></a>
|
|
1172
1184
|
|
|
1173
1185
|
## Don't Re-use SftpClient Objects
|
|
1174
1186
|
|
|
@@ -1177,12 +1189,12 @@ Due to an issue with *ECONNRESET* error signals when connecting to Windows based
|
|
|
1177
1189
|
To avoid this problem, don't re-use SftpClient objects. Generate a new SftpClient object for each connection. You can perform multiple actions with a single connection e.g. upload multiple files, download multiple files etc, but after you have called end(), you should not try to re-use the object with a further connect() call. Create a new object instead.
|
|
1178
1190
|
|
|
1179
1191
|
|
|
1180
|
-
<a id="
|
|
1192
|
+
<a id="orgc01e9f3"></a>
|
|
1181
1193
|
|
|
1182
1194
|
# FAQ
|
|
1183
1195
|
|
|
1184
1196
|
|
|
1185
|
-
<a id="
|
|
1197
|
+
<a id="orgcbb84bd"></a>
|
|
1186
1198
|
|
|
1187
1199
|
## Remote server drops connections with only an end event
|
|
1188
1200
|
|
|
@@ -1193,7 +1205,7 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
|
|
|
1193
1205
|
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.
|
|
1194
1206
|
|
|
1195
1207
|
|
|
1196
|
-
<a id="
|
|
1208
|
+
<a id="orgb7ba758"></a>
|
|
1197
1209
|
|
|
1198
1210
|
## How can I pass writeable stream as dst for get method?
|
|
1199
1211
|
|
|
@@ -1252,7 +1264,7 @@ sftp
|
|
|
1252
1264
|
```
|
|
1253
1265
|
|
|
1254
1266
|
|
|
1255
|
-
<a id="
|
|
1267
|
+
<a id="orgf9d915c"></a>
|
|
1256
1268
|
|
|
1257
1269
|
## How can I upload files without having to specify a password?
|
|
1258
1270
|
|
|
@@ -1287,7 +1299,7 @@ sftp.connect({
|
|
|
1287
1299
|
```
|
|
1288
1300
|
|
|
1289
1301
|
|
|
1290
|
-
<a id="
|
|
1302
|
+
<a id="orgc977636"></a>
|
|
1291
1303
|
|
|
1292
1304
|
## How can I connect through a Socks Proxy
|
|
1293
1305
|
|
|
@@ -1324,7 +1336,7 @@ client.connect({
|
|
|
1324
1336
|
```
|
|
1325
1337
|
|
|
1326
1338
|
|
|
1327
|
-
<a id="
|
|
1339
|
+
<a id="org9caf1fc"></a>
|
|
1328
1340
|
|
|
1329
1341
|
## Timeout while waiting for handshake or handshake errors
|
|
1330
1342
|
|
|
@@ -1333,7 +1345,7 @@ Some users have encountered the error 'Timeout while waiting for handshake' or '
|
|
|
1333
1345
|
When encountering this type of problem, one worthwhile approach is to use openSSH's CLI sftp program with the `-v` switch to raise logging levels. This will show you what algorithms the CLI is using. You can then use this information to match the names with the accepted algorithm names documented in the `ssh2` README to set the properties in the `algorithms` object.
|
|
1334
1346
|
|
|
1335
1347
|
|
|
1336
|
-
<a id="
|
|
1348
|
+
<a id="orgc99cfed"></a>
|
|
1337
1349
|
|
|
1338
1350
|
## How can I limit upload/download speed
|
|
1339
1351
|
|
|
@@ -1373,7 +1385,7 @@ try {
|
|
|
1373
1385
|
```
|
|
1374
1386
|
|
|
1375
1387
|
|
|
1376
|
-
<a id="
|
|
1388
|
+
<a id="orgff1cf60"></a>
|
|
1377
1389
|
|
|
1378
1390
|
## Connection hangs or fails for larger files
|
|
1379
1391
|
|
|
@@ -1384,21 +1396,21 @@ A symptom of this issue is that you are able to upload small files, but uploadin
|
|
|
1384
1396
|
For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
|
|
1385
1397
|
|
|
1386
1398
|
|
|
1387
|
-
<a id="
|
|
1399
|
+
<a id="org312104a"></a>
|
|
1388
1400
|
|
|
1389
1401
|
## Typescript definition file out of date
|
|
1390
1402
|
|
|
1391
1403
|
This project does not use Typescript. However, typescript definition files are provided by other 3rd parties. Sometimes, these definition files have not stayed up-to-date with the current version of this module. If you encounter this issue, you need to report it to the party responsible for the definition file, not this project.
|
|
1392
1404
|
|
|
1393
1405
|
|
|
1394
|
-
<a id="
|
|
1406
|
+
<a id="org409c3ef"></a>
|
|
1395
1407
|
|
|
1396
1408
|
# Examples
|
|
1397
1409
|
|
|
1398
1410
|
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.
|
|
1399
1411
|
|
|
1400
1412
|
|
|
1401
|
-
<a id="
|
|
1413
|
+
<a id="orge118375"></a>
|
|
1402
1414
|
|
|
1403
1415
|
# Troubleshooting
|
|
1404
1416
|
|
|
@@ -1413,14 +1425,14 @@ Note also that in the repository there are two useful directories. The first is
|
|
|
1413
1425
|
The second directory is the validation directory. I have some very simple scripts in this directory which perform basic tasks using only the `ssh2` modules (no `ssh2-sftp-client` module). These can be useful when trying to determine if the issue is with the underlying `ssh2` module or the `ssh2-sftp-client` wrapper module.
|
|
1414
1426
|
|
|
1415
1427
|
|
|
1416
|
-
<a id="
|
|
1428
|
+
<a id="org39ed676"></a>
|
|
1417
1429
|
|
|
1418
1430
|
## Common Errors
|
|
1419
1431
|
|
|
1420
1432
|
There are some common errors people tend to make when using Promises or Async/Await. These are by far the most common problem found in issues logged against this module. Please check for some of these before logging your issue.
|
|
1421
1433
|
|
|
1422
1434
|
|
|
1423
|
-
<a id="
|
|
1435
|
+
<a id="orge4fdace"></a>
|
|
1424
1436
|
|
|
1425
1437
|
### Not returning the promise in a `then()` block
|
|
1426
1438
|
|
|
@@ -1457,7 +1469,7 @@ Note the `return` statements. These ensure that the Promise returned by the clie
|
|
|
1457
1469
|
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.
|
|
1458
1470
|
|
|
1459
1471
|
|
|
1460
|
-
<a id="
|
|
1472
|
+
<a id="org613b5f0"></a>
|
|
1461
1473
|
|
|
1462
1474
|
### Mixing Promise Chains and Async/Await
|
|
1463
1475
|
|
|
@@ -1517,7 +1529,7 @@ async function doSftp() {
|
|
|
1517
1529
|
```
|
|
1518
1530
|
|
|
1519
1531
|
|
|
1520
|
-
<a id="
|
|
1532
|
+
<a id="org3df8f9d"></a>
|
|
1521
1533
|
|
|
1522
1534
|
### Try/catch and Error Handlers
|
|
1523
1535
|
|
|
@@ -1528,14 +1540,14 @@ The basic problem is that the try/catch block will have completed execution befo
|
|
|
1528
1540
|
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.
|
|
1529
1541
|
|
|
1530
1542
|
|
|
1531
|
-
<a id="
|
|
1543
|
+
<a id="org295cfa3"></a>
|
|
1532
1544
|
|
|
1533
1545
|
### Server Differences
|
|
1534
1546
|
|
|
1535
1547
|
Not all SFTP servers are the same. Like most standards, the SFTP protocol has some level of interpretation and allows different levels of compliance. This means there can be differences in behaviour between different servers and code which works with one server will not work the same with another. For example, the value returned by *realpath* for non-existent objects can differ significantly. Some servers will throw an error for a particular operation while others will just return null, some servers support concurrent operations (such as used by fastGet/fastPut) while others will not and of course, the text of error messages can vary significantly. In particular, we have noticed significant differences across different platforms. It is therefore advisable to do comprehensive testing when the SFTP server is moved to a new platform. This includes moving from to a cloud based service even if the underlying platform remains the same. I have noticed that some cloud platforms can generate unexpected events, possibly related to additional functionality or features associated with the cloud implementation. For example, it appears SFTP servers running under Azure will generate an error event when the connection is closed even when the client has requested the connection be terminated. The same SFTP server running natively on Windows does not appear to exhibit such behaviour.
|
|
1536
1548
|
|
|
1537
1549
|
|
|
1538
|
-
<a id="
|
|
1550
|
+
<a id="org5ee2ce1"></a>
|
|
1539
1551
|
|
|
1540
1552
|
### Avoid Concurrent Operations
|
|
1541
1553
|
|
|
@@ -1544,7 +1556,7 @@ Technically, SFTP should be able to perform multiple operations concurrently. As
|
|
|
1544
1556
|
If you are going to try and perform concurrent operations, you need to test extensively and ensure you are using data which is large enough that context switching does occur (i.e. the request is not completed in a single run). Some SFTP servers will handle concurrent operations better than others.
|
|
1545
1557
|
|
|
1546
1558
|
|
|
1547
|
-
<a id="
|
|
1559
|
+
<a id="orgec0b62a"></a>
|
|
1548
1560
|
|
|
1549
1561
|
## Debugging Support
|
|
1550
1562
|
|
|
@@ -1577,7 +1589,7 @@ If you just want to see debug messages from `ssh2-sftp-client` and exclude debug
|
|
|
1577
1589
|
```
|
|
1578
1590
|
|
|
1579
1591
|
|
|
1580
|
-
<a id="
|
|
1592
|
+
<a id="org41f81c7"></a>
|
|
1581
1593
|
|
|
1582
1594
|
# Logging Issues
|
|
1583
1595
|
|
|
@@ -1594,7 +1606,7 @@ I am happy to try and help diagnose and fix any issues you encounter while using
|
|
|
1594
1606
|
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.
|
|
1595
1607
|
|
|
1596
1608
|
|
|
1597
|
-
<a id="
|
|
1609
|
+
<a id="orgd153e6d"></a>
|
|
1598
1610
|
|
|
1599
1611
|
# Pull Requests
|
|
1600
1612
|
|
|
@@ -1611,7 +1623,7 @@ This module will adopt a standard semantic versioning policy. Please indicate in
|
|
|
1611
1623
|
- **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
|
|
1612
1624
|
|
|
1613
1625
|
|
|
1614
|
-
<a id="
|
|
1626
|
+
<a id="org267dc2a"></a>
|
|
1615
1627
|
|
|
1616
1628
|
# Contributors
|
|
1617
1629
|
|
package/README.org
CHANGED
|
@@ -12,9 +12,9 @@ be found on the [[https://github.com/mscdex/ssh2][SSH2]] project pages. As the s
|
|
|
12
12
|
~ssh2~ module, you will find lots of useful information, tips and examples in the ~ssh2~
|
|
13
13
|
repository.
|
|
14
14
|
|
|
15
|
-
Current stable release is *v12.0.
|
|
15
|
+
Current stable release is *v12.0.1.
|
|
16
16
|
|
|
17
|
-
Code has been tested against Node versions 20.
|
|
17
|
+
Code has been tested against Node versions 20.19.2, 22.16.0, 23.11.1 and 24.2.0. Node versions
|
|
18
18
|
prior to v20.x are not supported.
|
|
19
19
|
|
|
20
20
|
If you find this module useful and you would like to support the on-going maintenance and
|
|
@@ -26,30 +26,47 @@ The big and breaking change in this version is the removal of the connection ret
|
|
|
26
26
|
code. This module no longer supports automatic connection retries when establishing the
|
|
27
27
|
initial connection. This functionality was removed for a number of reasons. The main
|
|
28
28
|
reasons are that it significantly complicated the connection handling code and error
|
|
29
|
-
handling in particular and it was seldom useful. In most cases, if the
|
|
30
|
-
on
|
|
29
|
+
handling in particular and it was seldom useful. In most cases, if the connection failed
|
|
30
|
+
on the first attempt, it would also fail in all subsequent attempts and often resulted in
|
|
31
31
|
just extending the time to failure as multiple retries were attempted.
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
of the mnay promise retry packages available.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
Another reason this functionality was removed is because if you really want it, it is
|
|
34
|
+
straight forward to create your own wrapper around the connect call which uses one
|
|
35
|
+
of the mnay promise retry packages available. It is much simpler to add a retry mechanism
|
|
36
|
+
for a specific application than it is to implement a flexible solution which supports all
|
|
37
|
+
possible combination of connection options, authentication methods, proxies etc.
|
|
38
|
+
|
|
39
|
+
** Version 12.0.1 Changes
|
|
40
|
+
|
|
41
|
+
Bug Fixes
|
|
42
|
+
|
|
43
|
+
- Fix typo error in specification of generic error code in error handler. Typo was
|
|
44
|
+
causing an undefined symbol error.
|
|
45
|
+
- Fix bug in stream error handling. Read and write streams had an error handler defined
|
|
46
|
+
with 'once' rather than 'on'. This caused a bug when an API call caused more than one
|
|
47
|
+
error. For example, a network connectivity error would cause a stream timeout error
|
|
48
|
+
followed by a no response from remote host error when the error handler attempted to
|
|
49
|
+
cleanup and destroy the remote stream. The second error whould not be handled and
|
|
50
|
+
would bubble up to the parent node process, which would then exit with an unhandled
|
|
51
|
+
exception error.
|
|
52
|
+
- Added calls to _resetEventFlags() to finally clauses to ensure event flags are reset
|
|
53
|
+
when temp listeners are removed. Without this change, global listeners would not fire
|
|
54
|
+
when they should after their has been an earlier handled error in the last API call.
|
|
55
|
+
|
|
39
56
|
** Background
|
|
40
57
|
|
|
41
58
|
In basic terms =ssh2-sftp-client= is a simple wrapper around the =ssh2= package which provides
|
|
42
|
-
a promise
|
|
59
|
+
a promise based API for interacting with a remote SFTP server . The =ssh2= package provides
|
|
43
60
|
an event based API for interacting with the ~ssh~ protocol. The ~ssh2-sftp-client~ package
|
|
44
61
|
uses the ~sftp~ subsystem of this protocol to implement the basic operations typically
|
|
45
62
|
associated with an ~sftp~ client.
|
|
46
63
|
|
|
47
|
-
Wrapping an event based API with a
|
|
64
|
+
Wrapping an event based API with a promise based API comes with a number of
|
|
48
65
|
challenges. In particular, efficiently and reliably managing events within the context of
|
|
49
|
-
|
|
66
|
+
asynchronous code execution. This package uses the following strategies;
|
|
50
67
|
|
|
51
68
|
- All direct interactions with the ~ssh2~ API are wrapped in promise objects. When the
|
|
52
|
-
API call succeeds, the
|
|
69
|
+
API call succeeds, the associated promise will be successfully resolved. When an error occurs,
|
|
53
70
|
the promise is rejected.
|
|
54
71
|
|
|
55
72
|
- An error can either be due to a low level network error, such as a lost connection
|
|
@@ -57,30 +74,32 @@ asynchrounous code execution. This package uses the following strategies;
|
|
|
57
74
|
existing or not having the appropriate permissions for access.
|
|
58
75
|
|
|
59
76
|
- Each of the available ~SftpClient~ methods wrap the method call inside a promise. In
|
|
60
|
-
|
|
61
|
-
and close events and links those liseners to the method's promise via
|
|
77
|
+
creating each promise, the class adds temporary event listeners for the error, end
|
|
78
|
+
and close events and links those liseners to the method's promise via its ~reject()~
|
|
62
79
|
method.
|
|
63
80
|
|
|
64
|
-
- If a promise is waiting to be fulfilled when
|
|
65
|
-
occurs, the error will be communicated back to client code via
|
|
81
|
+
- If a promise is waiting to be fulfilled when an error
|
|
82
|
+
occurs, the error will be communicated back to the client code via the rejected promise.
|
|
66
83
|
|
|
67
84
|
- When the ~ssh2~ emitter raises an event outside the context of any promise, that event
|
|
68
85
|
will be handled by global event handlers. By default, these event handlers will log
|
|
69
86
|
the event and will invalidate any existing socket connection objects, preventing any
|
|
70
|
-
further API calls until a new connection is
|
|
87
|
+
further API calls until a new connection is established.
|
|
71
88
|
|
|
72
89
|
- The ~SftpClient~ class constructor supports an optoinal second argument which is an
|
|
73
|
-
objedct
|
|
74
|
-
|
|
75
|
-
|
|
90
|
+
objedct which can have any of the three properties error, end and close. The values
|
|
91
|
+
associated with these properties are callback functions that will be run if an error
|
|
92
|
+
event with?he same name as the property is raised and has not been handled by one of
|
|
93
|
+
the temporary event handlers. The error callback should have a single parameter, the
|
|
94
|
+
error raised by the event. The other callbacks have no arguments.
|
|
76
95
|
|
|
77
96
|
The need for both global listeners and temporary promise listeners is because network end,
|
|
78
|
-
close or error events can occur at any time, including in-
|
|
97
|
+
close or error events can occur at any time, including in-between API calls. During an
|
|
79
98
|
API call, a promise is active and can be used to communicate event information back to the calling
|
|
80
|
-
code via normal promise communication
|
|
99
|
+
code via normal promise communication channels i.e. async/await with try/catch or promise chain's then/catch
|
|
81
100
|
mechanism. However, outside API calls, no promise exists and there is no reliable
|
|
82
101
|
mechanism to return error and other event information back to calling code. You cannot
|
|
83
|
-
reliably use try/cdatch to catch
|
|
102
|
+
reliably use try/cdatch to catch errors thrown inside event listenrs as you lack control
|
|
84
103
|
over when the listener code runs. Your try/catch block can easily complete before the
|
|
85
104
|
error is raised as there is no equivalent /await/ type functionality in this situation.
|
|
86
105
|
|
|
@@ -95,17 +114,17 @@ close or error event which is not handled by one of the temporary promise linked
|
|
|
95
114
|
listeners.
|
|
96
115
|
|
|
97
116
|
Version 11 of ~ssh2-sftp-client~ also changes the behaviour of the temporary promise linked
|
|
98
|
-
/end/ and /close/ listeners. Prior to
|
|
117
|
+
/end/ and /close/ listeners. Prior to version 11, these listeners did not reject
|
|
99
118
|
promises. They would only invalidate the underlying ~ssh~ connection object. Only the /error/
|
|
100
119
|
listener would actually reject the associated promise. This was done because you cannot
|
|
101
120
|
guarantee the order in which events are responded to.
|
|
102
121
|
|
|
103
122
|
In most cases, events will occur in the order /error/, /end/ and then /close/. The error event
|
|
104
123
|
would contain details about the cause of the error while the end and close events just
|
|
105
|
-
|
|
124
|
+
communicate that these events have been raised. The normal flow would be
|
|
106
125
|
|
|
107
126
|
- Error event occurs including error cause description. Listener catches event,
|
|
108
|
-
creates an error object and calls the associated
|
|
127
|
+
creates an error object and calls the associated promise's reject function to reject
|
|
109
128
|
the promise. The calling process receives a rejected promise object.
|
|
110
129
|
|
|
111
130
|
- End event occurs. The end listener catches the event and marks the connection object
|
|
@@ -114,11 +133,11 @@ communicvate that these events have been raised. The normal flow would be
|
|
|
114
133
|
listener and you can only call one promise resolution function.
|
|
115
134
|
|
|
116
135
|
- Close event occurs. This event means the socket connection has been closed. The
|
|
117
|
-
listener will ensure any connection information has been invalidated.
|
|
136
|
+
listener will ensure any connection information has been invalidated. Again, there
|
|
118
137
|
is no need to call the reject method of the associated promise as it has already
|
|
119
138
|
been called by the error listener.
|
|
120
139
|
|
|
121
|
-
In some cases, no
|
|
140
|
+
In some cases, no end event is raised and you only get an error event followed by a close
|
|
122
141
|
event. In versions of ~ssh2-sftp-client~ prior to version 11, neither the end or the close
|
|
123
142
|
listeners attempted to call the reject method of the associated promise. It was assumed
|
|
124
143
|
that all sftp servers would raise an error event whenever a connection was unexpectedly
|
|
@@ -131,7 +150,7 @@ call gets rejected with a timeout error aftrer a significant delay.
|
|
|
131
150
|
In order to handle the possible hanging issue in version 11, the temporary promise linked
|
|
132
151
|
end and close listeners have been updated to always call the promise's reject function if
|
|
133
152
|
they fire. While this works, it can cause a minor issue. As wse cannot gurantee the order
|
|
134
|
-
in which events are
|
|
153
|
+
in which events are responded to by listeners, it is possible that either the end or close
|
|
135
154
|
listener may be executed before the error listener. When this occurs, the promise is
|
|
136
155
|
rejected, but the only information wse have at that point is that the promise wsas reject
|
|
137
156
|
due to either an end or close event. We don't yet have any details regarding what error
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -25,7 +25,7 @@ class SftpClient {
|
|
|
25
25
|
close: () => console.log('Global close listener: close event raised'),
|
|
26
26
|
},
|
|
27
27
|
) {
|
|
28
|
-
this.version = '12.0.
|
|
28
|
+
this.version = '12.0.1';
|
|
29
29
|
this.client = new Client();
|
|
30
30
|
this.sftp = undefined;
|
|
31
31
|
this.clientName = clientName;
|
|
@@ -169,6 +169,7 @@ class SftpClient {
|
|
|
169
169
|
}).finally(() => {
|
|
170
170
|
this.removeListener('ready', doReady);
|
|
171
171
|
removeTempListeners(this, listeners, 'getConnection');
|
|
172
|
+
this._resetEventFlags();
|
|
172
173
|
});
|
|
173
174
|
}
|
|
174
175
|
|
|
@@ -203,6 +204,7 @@ class SftpClient {
|
|
|
203
204
|
}).finally(() => {
|
|
204
205
|
if (addListeners) {
|
|
205
206
|
removeTempListeners(this, listeners, 'realPath');
|
|
207
|
+
this._resetEventFlags();
|
|
206
208
|
}
|
|
207
209
|
});
|
|
208
210
|
}
|
|
@@ -267,6 +269,7 @@ class SftpClient {
|
|
|
267
269
|
}).finally(() => {
|
|
268
270
|
if (addListeners) {
|
|
269
271
|
removeTempListeners(this, listeners, '_xstat');
|
|
272
|
+
this._resetEventFlags();
|
|
270
273
|
}
|
|
271
274
|
});
|
|
272
275
|
}
|
|
@@ -397,6 +400,7 @@ class SftpClient {
|
|
|
397
400
|
}).finally(() => {
|
|
398
401
|
if (addListeners) {
|
|
399
402
|
removeTempListeners(this, listeners, 'list');
|
|
403
|
+
this._resetEventFlags();
|
|
400
404
|
}
|
|
401
405
|
});
|
|
402
406
|
}
|
|
@@ -437,7 +441,7 @@ class SftpClient {
|
|
|
437
441
|
pipeOptions: { ...options?.pipeOptions, end: true },
|
|
438
442
|
};
|
|
439
443
|
rdr = this.sftp.createReadStream(remotePath, options.readStreamOptions);
|
|
440
|
-
rdr.
|
|
444
|
+
rdr.on('error', (err) => {
|
|
441
445
|
if (dst && typeof dst === 'string' && wtr && !wtr.destroyed) {
|
|
442
446
|
wtr.destroy();
|
|
443
447
|
}
|
|
@@ -445,13 +449,11 @@ class SftpClient {
|
|
|
445
449
|
});
|
|
446
450
|
if (dst === undefined) {
|
|
447
451
|
// no dst specified, return buffer of data
|
|
448
|
-
this.debugMsg('get resolving with buffer of data');
|
|
449
452
|
wtr = concat((buff) => {
|
|
450
453
|
resolve(buff);
|
|
451
454
|
});
|
|
452
455
|
} else if (typeof dst === 'string') {
|
|
453
456
|
// dst local file path
|
|
454
|
-
this.debugMsg(`get called with file path destination ${dst}`);
|
|
455
457
|
const localCheck = haveLocalCreate(dst);
|
|
456
458
|
if (localCheck.status) {
|
|
457
459
|
wtr = fs.createWriteStream(dst, options.writeStreamOptions);
|
|
@@ -465,10 +467,9 @@ class SftpClient {
|
|
|
465
467
|
);
|
|
466
468
|
}
|
|
467
469
|
} else {
|
|
468
|
-
this.debugMsg('get called with stream destination');
|
|
469
470
|
wtr = dst;
|
|
470
471
|
}
|
|
471
|
-
wtr.
|
|
472
|
+
wtr.on('error', (err) => {
|
|
472
473
|
reject(
|
|
473
474
|
this.fmtError(
|
|
474
475
|
`${err.message} ${typeof dst === 'string' ? dst : '<stream>'}`,
|
|
@@ -492,6 +493,7 @@ class SftpClient {
|
|
|
492
493
|
}
|
|
493
494
|
if (addListeners) {
|
|
494
495
|
removeTempListeners(this, listeners, 'get');
|
|
496
|
+
this._resetEventFlags();
|
|
495
497
|
}
|
|
496
498
|
});
|
|
497
499
|
}
|
|
@@ -527,6 +529,7 @@ class SftpClient {
|
|
|
527
529
|
}).finally(() => {
|
|
528
530
|
if (addListeners) {
|
|
529
531
|
removeTempListeners(this, listeners, '_fastGet');
|
|
532
|
+
this._resetEventFlags();
|
|
530
533
|
}
|
|
531
534
|
});
|
|
532
535
|
}
|
|
@@ -593,6 +596,7 @@ class SftpClient {
|
|
|
593
596
|
}).finally(() => {
|
|
594
597
|
if (addListeners) {
|
|
595
598
|
removeTempListeners(this, listeners, '_fastPut');
|
|
599
|
+
this._resetEventFlags();
|
|
596
600
|
}
|
|
597
601
|
});
|
|
598
602
|
}
|
|
@@ -649,7 +653,7 @@ class SftpClient {
|
|
|
649
653
|
};
|
|
650
654
|
if (haveConnection(this, '_put', reject)) {
|
|
651
655
|
wtr = this.sftp.createWriteStream(rPath, opts.writeStreamOptions);
|
|
652
|
-
wtr.
|
|
656
|
+
wtr.on('error', (err) => {
|
|
653
657
|
if (typeof lPath === 'string' && rdr && !rdr.destroyed) {
|
|
654
658
|
rdr.destroy();
|
|
655
659
|
}
|
|
@@ -665,17 +669,14 @@ class SftpClient {
|
|
|
665
669
|
resolve(`Uploaded data stream to ${rPath}`);
|
|
666
670
|
});
|
|
667
671
|
if (lPath instanceof Buffer) {
|
|
668
|
-
this.debugMsg('put source is a buffer');
|
|
669
672
|
wtr.end(lPath);
|
|
670
673
|
} else {
|
|
671
674
|
if (typeof lPath === 'string') {
|
|
672
|
-
this.debugMsg('put source is string path');
|
|
673
675
|
rdr = fs.createReadStream(lPath, opts.readStreamOptions);
|
|
674
676
|
} else {
|
|
675
|
-
this.debugMsg('put source is a stream');
|
|
676
677
|
rdr = lPath;
|
|
677
678
|
}
|
|
678
|
-
rdr.
|
|
679
|
+
rdr.on('error', (err) => {
|
|
679
680
|
reject(
|
|
680
681
|
this.fmtError(
|
|
681
682
|
`Read stream error: ${err.message} ${
|
|
@@ -695,6 +696,7 @@ class SftpClient {
|
|
|
695
696
|
}
|
|
696
697
|
if (addListeners) {
|
|
697
698
|
removeTempListeners(this, listeners, '_put');
|
|
699
|
+
this._resetEventFlags();
|
|
698
700
|
}
|
|
699
701
|
});
|
|
700
702
|
}
|
|
@@ -750,6 +752,7 @@ class SftpClient {
|
|
|
750
752
|
}).finally(() => {
|
|
751
753
|
if (addListeners) {
|
|
752
754
|
removeTempListeners(this, listeners, '_append');
|
|
755
|
+
this._resetEventFlags();
|
|
753
756
|
}
|
|
754
757
|
});
|
|
755
758
|
}
|
|
@@ -821,6 +824,7 @@ class SftpClient {
|
|
|
821
824
|
}).finally(() => {
|
|
822
825
|
if (addListeners) {
|
|
823
826
|
removeTempListeners(this, listeners, '_doMkdir');
|
|
827
|
+
this._resetEventFlags();
|
|
824
828
|
}
|
|
825
829
|
});
|
|
826
830
|
}
|
|
@@ -894,6 +898,7 @@ class SftpClient {
|
|
|
894
898
|
});
|
|
895
899
|
}).finally(() => {
|
|
896
900
|
removeTempListeners(this, listeners, '_rmdir');
|
|
901
|
+
this._resetEventFlags();
|
|
897
902
|
});
|
|
898
903
|
};
|
|
899
904
|
|
|
@@ -983,6 +988,7 @@ class SftpClient {
|
|
|
983
988
|
}).finally(() => {
|
|
984
989
|
if (addListeners) {
|
|
985
990
|
removeTempListeners(this, listeners, 'delete');
|
|
991
|
+
this._resetEventFlags();
|
|
986
992
|
}
|
|
987
993
|
});
|
|
988
994
|
}
|
|
@@ -1021,6 +1027,7 @@ class SftpClient {
|
|
|
1021
1027
|
}).finally(() => {
|
|
1022
1028
|
if (addListeners) {
|
|
1023
1029
|
removeTempListeners(this, listeners, 'rename');
|
|
1030
|
+
this._resetEventFlags();
|
|
1024
1031
|
}
|
|
1025
1032
|
});
|
|
1026
1033
|
}
|
|
@@ -1059,6 +1066,7 @@ class SftpClient {
|
|
|
1059
1066
|
}
|
|
1060
1067
|
}).finally(() => {
|
|
1061
1068
|
removeTempListeners(this, listeners, 'posixRename');
|
|
1069
|
+
this._resetEventFlags();
|
|
1062
1070
|
});
|
|
1063
1071
|
}
|
|
1064
1072
|
|
|
@@ -1090,6 +1098,7 @@ class SftpClient {
|
|
|
1090
1098
|
}).finally(() => {
|
|
1091
1099
|
if (addListeners) {
|
|
1092
1100
|
removeTempListeners(this, listeners, 'chmod');
|
|
1101
|
+
this._resetEventFlags();
|
|
1093
1102
|
}
|
|
1094
1103
|
});
|
|
1095
1104
|
}
|
|
@@ -1175,6 +1184,7 @@ class SftpClient {
|
|
|
1175
1184
|
throw this.fmtError(`${e.message} ${srcDir} to ${dstDir}`, 'uploadFiles', e.code);
|
|
1176
1185
|
} finally {
|
|
1177
1186
|
removeTempListeners(this, listeners, uploadFiles);
|
|
1187
|
+
this._resetEventFlags();
|
|
1178
1188
|
}
|
|
1179
1189
|
};
|
|
1180
1190
|
|
|
@@ -1300,6 +1310,7 @@ class SftpClient {
|
|
|
1300
1310
|
);
|
|
1301
1311
|
} finally {
|
|
1302
1312
|
removeTempListeners(this, listeners, 'downloadFiles');
|
|
1313
|
+
this._resetEventFlags();
|
|
1303
1314
|
}
|
|
1304
1315
|
};
|
|
1305
1316
|
|
|
@@ -1347,6 +1358,7 @@ class SftpClient {
|
|
|
1347
1358
|
throw err.custom ? err : this.fmtError(err.message, 'createReadStream', err.code);
|
|
1348
1359
|
} finally {
|
|
1349
1360
|
removeTempListeners(this, listeners, 'createReadStream');
|
|
1361
|
+
this._resetEventFlags();
|
|
1350
1362
|
}
|
|
1351
1363
|
}
|
|
1352
1364
|
|
|
@@ -1372,6 +1384,7 @@ class SftpClient {
|
|
|
1372
1384
|
throw err.custom ? err : this.fmtError(err.message, 'createWriteStream', err.code);
|
|
1373
1385
|
} finally {
|
|
1374
1386
|
removeTempListeners(this, listeners, 'createWriteStream');
|
|
1387
|
+
this._resetEventFlags();
|
|
1375
1388
|
}
|
|
1376
1389
|
}
|
|
1377
1390
|
|
|
@@ -1435,6 +1448,7 @@ class SftpClient {
|
|
|
1435
1448
|
throw err.custom ? err : this.fmtError(err, 'rcopy');
|
|
1436
1449
|
} finally {
|
|
1437
1450
|
removeTempListeners(this, listeners, 'rcopy');
|
|
1451
|
+
this._resetEventFlags();
|
|
1438
1452
|
}
|
|
1439
1453
|
}
|
|
1440
1454
|
/**
|
|
@@ -1466,6 +1480,7 @@ class SftpClient {
|
|
|
1466
1480
|
}).finally(() => {
|
|
1467
1481
|
removeTempListeners(this, listeners, 'end');
|
|
1468
1482
|
this.removeListener('close', endCloseHandler);
|
|
1483
|
+
this._resetEventFlags();
|
|
1469
1484
|
});
|
|
1470
1485
|
}
|
|
1471
1486
|
}
|
package/src/utils.js
CHANGED
|
@@ -88,7 +88,7 @@ function endListener(client, name, reject) {
|
|
|
88
88
|
client.endHandled = true;
|
|
89
89
|
client.debugMsg(`${name} endListener - handling unexpected end event`);
|
|
90
90
|
const newError = new Error(`${name}: Unexpected end event`);
|
|
91
|
-
newError.code = errorCode.
|
|
91
|
+
newError.code = errorCode.generic;
|
|
92
92
|
if (reject) {
|
|
93
93
|
reject(newError);
|
|
94
94
|
} else {
|
|
@@ -109,7 +109,7 @@ function closeListener(client, name, reject) {
|
|
|
109
109
|
client.closeHandled = true;
|
|
110
110
|
client.debugMsg(`${name} closeListener - handling unexpected close event`);
|
|
111
111
|
const newError = new Error(`${name}: Unexpected close event`);
|
|
112
|
-
newError.code = errorCode.
|
|
112
|
+
newError.code = errorCode.generic;
|
|
113
113
|
if (reject) {
|
|
114
114
|
reject(newError);
|
|
115
115
|
} else {
|