ssh2-sftp-client 10.0.3 → 11.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +175 -135
- package/README.org +141 -28
- package/package.json +7 -7
- package/src/index.js +38 -47
- package/src/utils.js +83 -38
package/README.md
CHANGED
|
@@ -1,92 +1,131 @@
|
|
|
1
|
-
- [Overview](#
|
|
2
|
-
- [Version
|
|
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
|
-
- [How can I
|
|
44
|
-
- [How can I
|
|
45
|
-
- [
|
|
46
|
-
- [
|
|
47
|
-
- [
|
|
48
|
-
- [
|
|
49
|
-
- [
|
|
50
|
-
- [
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
- [
|
|
54
|
-
- [
|
|
55
|
-
- [
|
|
56
|
-
- [
|
|
57
|
-
|
|
58
|
-
- [
|
|
59
|
-
- [
|
|
60
|
-
- [
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
1
|
+
- [Overview](#org653583b)
|
|
2
|
+
- [Version 11.0.0 Changes](#orgce280ef)
|
|
3
|
+
- [Background](#orga2f4da7)
|
|
4
|
+
- [Installation](#orgab4f971)
|
|
5
|
+
- [Basic Usage](#orged5a12e)
|
|
6
|
+
- [Documentation](#org5e3267a)
|
|
7
|
+
- [Specifying Paths](#orgc8bb488)
|
|
8
|
+
- [Methods](#org0b676b5)
|
|
9
|
+
- [new SftpClient(name, callbacks) ===> SFTP client object](#orge77dba5)
|
|
10
|
+
- [connect(config) ===> SFTP object](#org596a3a4)
|
|
11
|
+
- [list(path, filter) ==> Array[object]](#orgf088b6d)
|
|
12
|
+
- [exists(path) ==> boolean](#org1f048a8)
|
|
13
|
+
- [stat(path) ==> object](#org27201d5)
|
|
14
|
+
- [get(path, dst, options) ==> String|Stream|Buffer](#org639d68b)
|
|
15
|
+
- [fastGet(remotePath, localPath, options) ===> string](#org3f8db62)
|
|
16
|
+
- [put(src, remotePath, options) ==> string](#orgc6243de)
|
|
17
|
+
- [fastPut(localPath, remotePath, options) ==> string](#orgff7efe4)
|
|
18
|
+
- [append(input, remotePath, options) ==> string](#orgee8aa7c)
|
|
19
|
+
- [mkdir(path, recursive) ==> string](#orgb3108f5)
|
|
20
|
+
- [rmdir(path, recursive) ==> string](#orge7349b5)
|
|
21
|
+
- [delete(path, noErrorOK) ==> string](#org8bed2a0)
|
|
22
|
+
- [rename(fromPath, toPath) ==> string](#orgcc2fce2)
|
|
23
|
+
- [posixRename(fromPath, toPath) ==> string](#org9f7fb45)
|
|
24
|
+
- [chmod(path, mode) ==> string](#org8fd8a11)
|
|
25
|
+
- [realPath(path) ===> string](#orgc1989a6)
|
|
26
|
+
- [cwd() ==> string](#orgb7b4cfb)
|
|
27
|
+
- [uploadDir(srcDir, dstDir, options) ==> string](#org8a09e7c)
|
|
28
|
+
- [downloadDir(srcDir, dstDir, options) ==> string](#orge6dce87)
|
|
29
|
+
- [createReadStream(remotePath, options)) ==> stream object](#org7beaf75)
|
|
30
|
+
- [createWriteStream(remotePath, options) ==> stream object](#orgccad95e)
|
|
31
|
+
- [rcopy(srcPath, dstPath) ==> string](#org96954f3)
|
|
32
|
+
- [end() ==> boolean](#orga34b68f)
|
|
33
|
+
- [Add and Remove Listeners](#org58bd78f)
|
|
34
|
+
- [Platform Quirks & Warnings](#org4d91e2f)
|
|
35
|
+
- [Server Capabilities](#org8fa9b52)
|
|
36
|
+
- [Issues with `fastPut()` and `fastGet()` Methods](#orge45232c)
|
|
37
|
+
- [Promises, Events & Managing Exceptions](#org294ac94)
|
|
38
|
+
- [Adding Custom Handlers](#orgd3d6a3b)
|
|
39
|
+
- [Windows Based Servers](#org32d3804)
|
|
40
|
+
- [Don't Re-use SftpClient Objects](#org951849f)
|
|
41
|
+
- [FAQ](#orgd48de8d)
|
|
42
|
+
- [Remote server drops connections with only an end event](#orged95aaa)
|
|
43
|
+
- [How can I pass writeable stream as dst for get method?](#org62169e3)
|
|
44
|
+
- [How can I upload files without having to specify a password?](#org2bb26ed)
|
|
45
|
+
- [How can I connect through a Socks Proxy](#orgc5e26e6)
|
|
46
|
+
- [Timeout while waiting for handshake or handshake errors](#org8337f16)
|
|
47
|
+
- [How can I limit upload/download speed](#orga5eb823)
|
|
48
|
+
- [Connection hangs or fails for larger files](#org2469055)
|
|
49
|
+
- [Typescript definition file out of date](#orga1d843c)
|
|
50
|
+
- [Examples](#org0ab8b41)
|
|
51
|
+
- [Troubleshooting](#org74214b7)
|
|
52
|
+
- [Common Errors](#org9b87e06)
|
|
53
|
+
- [Not returning the promise in a `then()` block](#org0ef5618)
|
|
54
|
+
- [Mixing Promise Chains and Async/Await](#org3ef4393)
|
|
55
|
+
- [Try/catch and Error Handlers](#org0cfccc7)
|
|
56
|
+
- [Server Differences](#org93b86a4)
|
|
57
|
+
- [Avoid Concurrent Operations](#orge2e0c12)
|
|
58
|
+
- [Debugging Support](#orgbdaf17f)
|
|
59
|
+
- [Logging Issues](#org7853a7f)
|
|
60
|
+
- [Pull Requests](#org2eca80f)
|
|
61
|
+
- [Contributors](#org8a2fb26)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
<a id="org653583b"></a>
|
|
65
66
|
|
|
66
67
|
# Overview
|
|
67
68
|
|
|
68
|
-
an SFTP client for node.js
|
|
69
|
+
This packan provides the class SftpClient, an SFTP client for node.js. It is a promise based decorator class around the excdellent [SSH2](https://github.com/mscdex/ssh2) package, which provides a pure node Javascript event based ssh2 implementation.
|
|
69
70
|
|
|
70
|
-
Documentation on the methods and available options in the underlying modules can be found on the [SSH2](https://github.com/mscdex/ssh2) project pages. As
|
|
71
|
+
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 just a wrapper around the `ssh2` module, you will find lots of useful information, tips and examples in the `ssh2` repository.
|
|
71
72
|
|
|
72
|
-
Current stable release is \*
|
|
73
|
+
Current stable release is \*v11.0.0.
|
|
73
74
|
|
|
74
|
-
Code has been tested against Node versions
|
|
75
|
-
|
|
76
|
-
Node versions < 16.x are not supported.
|
|
75
|
+
Code has been tested against Node versions 18.20.4, 20.16.0 and 22.5.1. Node versions prior to v18.x are not supported. Also note there is currently a deprecation warning when using node v22+. This is from the `ssh2` package and outside control of this package.
|
|
77
76
|
|
|
78
77
|
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).
|
|
79
78
|
|
|
80
79
|
|
|
81
|
-
<a id="
|
|
80
|
+
<a id="orgce280ef"></a>
|
|
81
|
+
|
|
82
|
+
## Version 11.0.0 Changes
|
|
83
|
+
|
|
84
|
+
The main change in v11 concerns how the package manages events raised by the `ssh2` packagwe it depends on. Managing events within the context of asynchronous code as ocurs when using promises is challenging. To understand some of the more subtle issues involved, it is recommended you read the [Asynchronous vs. synchronous](https://nodejs.org/docs/latest/api/events.html#asynchronous-vs-synchronous) and [Error events](https://nodejs.org/docs/latest/api/events.html#error-events) sections of the Events chapter from the node documentation.
|
|
85
|
+
|
|
86
|
+
The previous versions of this package use a global event handler approach to manage events raised outside the execution of any promise. However, this approach is problematic as all the global listeners can really do is raise an error. Attempting to catch errors raised inside event handlers is extremely difficult to manage within client code because those errors are raised inside a separate asynchronous execution context. In version 11, this approach has been replaced by a mechanism whereby the client code can pass in application specific global handlers if desired. If no handlers are defined, default handlers will log the event and when necessary, invalidate any existing connection objects. See the [1.2](#orga2f4da7) section for details.
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
<a id="orga2f4da7"></a>
|
|
90
|
+
|
|
91
|
+
## Background
|
|
92
|
+
|
|
93
|
+
In basic terms `ssh2-sftp-client` is a simple wrapper around the `ssh2` package which provides a promise base API for interacting with a remote SFTP server . The `ssh2` package provides an event based API for interacting with the `ssh` protcolo. The `ssh2-sftp-client` package uses the `sftp` subsystem of this protocol to implement the basic operations typically associated with an `sftp` client.
|
|
94
|
+
|
|
95
|
+
Wrapping an event based API with a promised based API comes with a number of challenges. In particular, efficiently and reliably managing events within the context of asynchrounous code execution. This package uses the following strategies;
|
|
96
|
+
|
|
97
|
+
- All direct interactions with the `ssh2` API are wrapped in promise objects. When the API call succeeds, the assoiated promise will be sucessfully resolved. When an error occurs, the promise is rejected.
|
|
98
|
+
|
|
99
|
+
- 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
|
+
|
|
101
|
+
- Each of the available `SftpClient` methods wrap the method call inside a promise. In crwating eacvh promise, the class adds temporary event listenrs for the error, end and close events and links those liseners to the method's promise via it's `reject()` method.
|
|
102
|
+
|
|
103
|
+
- If a promise is waiting to be fulfilled when either of the two types of errors occurs, the error will be communicated back to client code via a rejected promise.
|
|
104
|
+
|
|
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 extablished.
|
|
106
|
+
|
|
107
|
+
- The `SftpClient` class constructor supports an optoinal second argument which is an objedct whi8ch can have any of three properties representing event callbaqck funct5ions which will be executed for each of the possible events error, end and close.
|
|
108
|
+
|
|
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-betwseen 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 means 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 errorsx 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
|
+
|
|
111
|
+
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
|
+
|
|
113
|
+
Version 11 of `ssh2-sftp-client` also changes the behaviour of the temporary promise linked *end* and *close* listeners. Prior to versioln 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
|
+
|
|
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 communicvate that these events have been raised. The normal flow would be
|
|
116
|
+
|
|
117
|
+
- Error event occurs including error cause description. Listener catches event, creates an error object and calls the associated promises reject function to reject the promise. The calling process receives a rejected promise object.
|
|
118
|
+
|
|
119
|
+
- 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
|
+
|
|
121
|
+
- Close event occurs. This event means the socket connection has been closed. The listener will ensure any connection information has been invalidated. Aga8in, ther is no need to call the reject method of the associated promise as it has already been called by the error listener.
|
|
82
122
|
|
|
83
|
-
|
|
123
|
+
In some cases, no ende 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.
|
|
84
124
|
|
|
85
|
-
|
|
86
|
-
- Various minor documentation fixes and some minor fixes for typos in option property names.
|
|
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 resonded 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.
|
|
87
126
|
|
|
88
127
|
|
|
89
|
-
<a id="
|
|
128
|
+
<a id="orgab4f971"></a>
|
|
90
129
|
|
|
91
130
|
# Installation
|
|
92
131
|
|
|
@@ -95,7 +134,7 @@ npm install ssh2-sftp-client
|
|
|
95
134
|
```
|
|
96
135
|
|
|
97
136
|
|
|
98
|
-
<a id="
|
|
137
|
+
<a id="orged5a12e"></a>
|
|
99
138
|
|
|
100
139
|
# Basic Usage
|
|
101
140
|
|
|
@@ -118,7 +157,7 @@ sftp.connect({
|
|
|
118
157
|
```
|
|
119
158
|
|
|
120
159
|
|
|
121
|
-
<a id="
|
|
160
|
+
<a id="org5e3267a"></a>
|
|
122
161
|
|
|
123
162
|
# Documentation
|
|
124
163
|
|
|
@@ -129,7 +168,7 @@ All the methods will return a Promise, except for `on(), ~removeListener()`, `cr
|
|
|
129
168
|
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.
|
|
130
169
|
|
|
131
170
|
|
|
132
|
-
<a id="
|
|
171
|
+
<a id="orgc8bb488"></a>
|
|
133
172
|
|
|
134
173
|
## Specifying Paths
|
|
135
174
|
|
|
@@ -162,20 +201,21 @@ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
|
|
|
162
201
|
This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
|
|
163
202
|
|
|
164
203
|
|
|
165
|
-
<a id="
|
|
204
|
+
<a id="org0b676b5"></a>
|
|
166
205
|
|
|
167
206
|
## Methods
|
|
168
207
|
|
|
169
208
|
|
|
170
|
-
<a id="
|
|
209
|
+
<a id="orge77dba5"></a>
|
|
171
210
|
|
|
172
|
-
### new SftpClient(name) ===> SFTP client object
|
|
211
|
+
### new SftpClient(name, callbacks) ===> SFTP client object
|
|
173
212
|
|
|
174
213
|
Constructor to create a new `ssh2-sftp-client` object. An optional `name` string can be provided, which will be used in error messages to help identify which client has thrown the error.
|
|
175
214
|
|
|
176
215
|
1. Constructor Arguments
|
|
177
216
|
|
|
178
|
-
- **name:** string. An optional name string used in error messages
|
|
217
|
+
- **name:** string. An optional name string used in error messages. Defaults to the string 'sftp'
|
|
218
|
+
- **callbacks:** object. An object with the properties error, end and close. Associted with each property is a function that will be executed whenever the associated global event listener for error, end or close is executed. The error function should accept one argument. The end and close functions have no arguments. Default functions just print a message to `console.log()`.
|
|
179
219
|
|
|
180
220
|
2. Example Use
|
|
181
221
|
|
|
@@ -206,7 +246,7 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
|
|
|
206
246
|
```
|
|
207
247
|
|
|
208
248
|
|
|
209
|
-
<a id="
|
|
249
|
+
<a id="org596a3a4"></a>
|
|
210
250
|
|
|
211
251
|
### connect(config) ===> SFTP object
|
|
212
252
|
|
|
@@ -275,7 +315,7 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
275
315
|
```
|
|
276
316
|
|
|
277
317
|
|
|
278
|
-
<a id="
|
|
318
|
+
<a id="orgf088b6d"></a>
|
|
279
319
|
|
|
280
320
|
### list(path, filter) ==> Array[object]
|
|
281
321
|
|
|
@@ -336,7 +376,7 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
336
376
|
```
|
|
337
377
|
|
|
338
378
|
|
|
339
|
-
<a id="
|
|
379
|
+
<a id="org1f048a8"></a>
|
|
340
380
|
|
|
341
381
|
### exists(path) ==> boolean
|
|
342
382
|
|
|
@@ -372,7 +412,7 @@ Tests to see if remote file or directory exists. Returns type of remote object i
|
|
|
372
412
|
```
|
|
373
413
|
|
|
374
414
|
|
|
375
|
-
<a id="
|
|
415
|
+
<a id="org27201d5"></a>
|
|
376
416
|
|
|
377
417
|
### stat(path) ==> object
|
|
378
418
|
|
|
@@ -423,7 +463,7 @@ Returns the attributes associated with the object pointed to by `path`.
|
|
|
423
463
|
```
|
|
424
464
|
|
|
425
465
|
|
|
426
|
-
<a id="
|
|
466
|
+
<a id="org639d68b"></a>
|
|
427
467
|
|
|
428
468
|
### get(path, dst, options) ==> String|Stream|Buffer
|
|
429
469
|
|
|
@@ -479,7 +519,7 @@ In general, if you're going to pass in a string as the destination, you are bett
|
|
|
479
519
|
- **Tip:** See examples file in the Git repository for more examples. You can pass any writeable stream in as the destination. For example, if you pass in `zlib.createGunzip()` writeable stream, you can both download and decompress a gzip file 'on the fly'.
|
|
480
520
|
|
|
481
521
|
|
|
482
|
-
<a id="
|
|
522
|
+
<a id="org3f8db62"></a>
|
|
483
523
|
|
|
484
524
|
### fastGet(remotePath, localPath, options) ===> string
|
|
485
525
|
|
|
@@ -524,7 +564,7 @@ Bottom line, when it works, it tends to work reliably. However, for many servers
|
|
|
524
564
|
```
|
|
525
565
|
|
|
526
566
|
|
|
527
|
-
<a id="
|
|
567
|
+
<a id="orgc6243de"></a>
|
|
528
568
|
|
|
529
569
|
### put(src, remotePath, options) ==> string
|
|
530
570
|
|
|
@@ -574,7 +614,7 @@ Upload data from local system to remote server. If the `src` argument is a strin
|
|
|
574
614
|
- **Tip:** If the src argument is a path string, consider just using `fastPut()`.
|
|
575
615
|
|
|
576
616
|
|
|
577
|
-
<a id="
|
|
617
|
+
<a id="orgff7efe4"></a>
|
|
578
618
|
|
|
579
619
|
### fastPut(localPath, remotePath, options) ==> string
|
|
580
620
|
|
|
@@ -620,7 +660,7 @@ Bottom line, when it works, it tends to work well. However, when it doesn't work
|
|
|
620
660
|
```
|
|
621
661
|
|
|
622
662
|
|
|
623
|
-
<a id="
|
|
663
|
+
<a id="orgee8aa7c"></a>
|
|
624
664
|
|
|
625
665
|
### append(input, remotePath, options) ==> string
|
|
626
666
|
|
|
@@ -664,7 +704,7 @@ Append the `input` data to an existing remote file. There is no integrity checki
|
|
|
664
704
|
```
|
|
665
705
|
|
|
666
706
|
|
|
667
|
-
<a id="
|
|
707
|
+
<a id="orgb3108f5"></a>
|
|
668
708
|
|
|
669
709
|
### mkdir(path, recursive) ==> string
|
|
670
710
|
|
|
@@ -692,7 +732,7 @@ Create a new directory. If the recursive flag is set to true, the method will cr
|
|
|
692
732
|
```
|
|
693
733
|
|
|
694
734
|
|
|
695
|
-
<a id="
|
|
735
|
+
<a id="orge7349b5"></a>
|
|
696
736
|
|
|
697
737
|
### rmdir(path, recursive) ==> string
|
|
698
738
|
|
|
@@ -722,7 +762,7 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
|
|
|
722
762
|
```
|
|
723
763
|
|
|
724
764
|
|
|
725
|
-
<a id="
|
|
765
|
+
<a id="org8bed2a0"></a>
|
|
726
766
|
|
|
727
767
|
### delete(path, noErrorOK) ==> string
|
|
728
768
|
|
|
@@ -751,7 +791,7 @@ Delete a file on the remote server.
|
|
|
751
791
|
```
|
|
752
792
|
|
|
753
793
|
|
|
754
|
-
<a id="
|
|
794
|
+
<a id="orgcc2fce2"></a>
|
|
755
795
|
|
|
756
796
|
### rename(fromPath, toPath) ==> string
|
|
757
797
|
|
|
@@ -780,7 +820,7 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
|
|
|
780
820
|
```
|
|
781
821
|
|
|
782
822
|
|
|
783
|
-
<a id="
|
|
823
|
+
<a id="org9f7fb45"></a>
|
|
784
824
|
|
|
785
825
|
### posixRename(fromPath, toPath) ==> string
|
|
786
826
|
|
|
@@ -807,7 +847,7 @@ client.connect(config)
|
|
|
807
847
|
```
|
|
808
848
|
|
|
809
849
|
|
|
810
|
-
<a id="
|
|
850
|
+
<a id="org8fd8a11"></a>
|
|
811
851
|
|
|
812
852
|
### chmod(path, mode) ==> string
|
|
813
853
|
|
|
@@ -836,7 +876,7 @@ Change the mode (read, write or execute permissions) of a remote file or directo
|
|
|
836
876
|
```
|
|
837
877
|
|
|
838
878
|
|
|
839
|
-
<a id="
|
|
879
|
+
<a id="orgc1989a6"></a>
|
|
840
880
|
|
|
841
881
|
### realPath(path) ===> string
|
|
842
882
|
|
|
@@ -847,14 +887,14 @@ Converts a relative path to an absolute path on the remote server. This method i
|
|
|
847
887
|
- **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
|
|
848
888
|
|
|
849
889
|
|
|
850
|
-
<a id="
|
|
890
|
+
<a id="orgb7b4cfb"></a>
|
|
851
891
|
|
|
852
892
|
### cwd() ==> string
|
|
853
893
|
|
|
854
894
|
Returns what the server believes is the current remote working directory.
|
|
855
895
|
|
|
856
896
|
|
|
857
|
-
<a id="
|
|
897
|
+
<a id="org8a09e7c"></a>
|
|
858
898
|
|
|
859
899
|
### uploadDir(srcDir, dstDir, options) ==> string
|
|
860
900
|
|
|
@@ -923,11 +963,11 @@ The `useFastput` option is a boolean option. If `true`, the method will use the
|
|
|
923
963
|
```
|
|
924
964
|
|
|
925
965
|
|
|
926
|
-
<a id="
|
|
966
|
+
<a id="orge6dce87"></a>
|
|
927
967
|
|
|
928
968
|
### downloadDir(srcDir, dstDir, options) ==> string
|
|
929
969
|
|
|
930
|
-
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.
|
|
970
|
+
Download the content of 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.
|
|
931
971
|
|
|
932
972
|
The method also emits `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.
|
|
933
973
|
|
|
@@ -988,7 +1028,7 @@ If the `useFastget` property is set to `true`, the method will use `fastGet()` t
|
|
|
988
1028
|
```
|
|
989
1029
|
|
|
990
1030
|
|
|
991
|
-
<a id="
|
|
1031
|
+
<a id="org7beaf75"></a>
|
|
992
1032
|
|
|
993
1033
|
### createReadStream(remotePath, options)) ==> stream object
|
|
994
1034
|
|
|
@@ -1005,7 +1045,7 @@ Returns a read stream object which is attached to the remote file specified by t
|
|
|
1005
1045
|
- **end:** Position to stop reading bytes (inclusive).
|
|
1006
1046
|
|
|
1007
1047
|
|
|
1008
|
-
<a id="
|
|
1048
|
+
<a id="orgccad95e"></a>
|
|
1009
1049
|
|
|
1010
1050
|
### createWriteStream(remotePath, options) ==> stream object
|
|
1011
1051
|
|
|
@@ -1020,7 +1060,7 @@ Returns a write stream object which is attached to the remote file specified in
|
|
|
1020
1060
|
- **start:** Byte position to start writing from (inclusive). May require changing flag to 'r+'.
|
|
1021
1061
|
|
|
1022
1062
|
|
|
1023
|
-
<a id="
|
|
1063
|
+
<a id="org96954f3"></a>
|
|
1024
1064
|
|
|
1025
1065
|
### rcopy(srcPath, dstPath) ==> string
|
|
1026
1066
|
|
|
@@ -1030,7 +1070,7 @@ Perform a remote file copy. The file identified by the `srcPath` argument will b
|
|
|
1030
1070
|
- **dstPath:** Path to where the copy will be created specified as a string
|
|
1031
1071
|
|
|
1032
1072
|
|
|
1033
|
-
<a id="
|
|
1073
|
+
<a id="orga34b68f"></a>
|
|
1034
1074
|
|
|
1035
1075
|
### end() ==> boolean
|
|
1036
1076
|
|
|
@@ -1054,7 +1094,7 @@ Ends the current client session, releasing the client socket and associated reso
|
|
|
1054
1094
|
```
|
|
1055
1095
|
|
|
1056
1096
|
|
|
1057
|
-
<a id="
|
|
1097
|
+
<a id="org58bd78f"></a>
|
|
1058
1098
|
|
|
1059
1099
|
### Add and Remove Listeners
|
|
1060
1100
|
|
|
@@ -1075,12 +1115,12 @@ Although normally not required, you can add and remove custom listeners on the s
|
|
|
1075
1115
|
Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
|
|
1076
1116
|
|
|
1077
1117
|
|
|
1078
|
-
<a id="
|
|
1118
|
+
<a id="org4d91e2f"></a>
|
|
1079
1119
|
|
|
1080
1120
|
# Platform Quirks & Warnings
|
|
1081
1121
|
|
|
1082
1122
|
|
|
1083
|
-
<a id="
|
|
1123
|
+
<a id="org8fa9b52"></a>
|
|
1084
1124
|
|
|
1085
1125
|
## Server Capabilities
|
|
1086
1126
|
|
|
@@ -1089,7 +1129,7 @@ All SFTP servers and platforms are not equal. Some facilities provided by `ssh2-
|
|
|
1089
1129
|
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.
|
|
1090
1130
|
|
|
1091
1131
|
|
|
1092
|
-
<a id="
|
|
1132
|
+
<a id="orge45232c"></a>
|
|
1093
1133
|
|
|
1094
1134
|
## Issues with `fastPut()` and `fastGet()` Methods
|
|
1095
1135
|
|
|
@@ -1098,7 +1138,7 @@ The `fastPut()` and `fastGet()` methods are known to be somewhat dependent on SF
|
|
|
1098
1138
|
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.
|
|
1099
1139
|
|
|
1100
1140
|
|
|
1101
|
-
<a id="
|
|
1141
|
+
<a id="org294ac94"></a>
|
|
1102
1142
|
|
|
1103
1143
|
## Promises, Events & Managing Exceptions
|
|
1104
1144
|
|
|
@@ -1117,14 +1157,14 @@ The other area where additional events are fired is during the end() call. To de
|
|
|
1117
1157
|
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.
|
|
1118
1158
|
|
|
1119
1159
|
|
|
1120
|
-
<a id="
|
|
1160
|
+
<a id="orgd3d6a3b"></a>
|
|
1121
1161
|
|
|
1122
1162
|
### Adding Custom Handlers
|
|
1123
1163
|
|
|
1124
1164
|
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.
|
|
1125
1165
|
|
|
1126
1166
|
|
|
1127
|
-
<a id="
|
|
1167
|
+
<a id="org32d3804"></a>
|
|
1128
1168
|
|
|
1129
1169
|
## Windows Based Servers
|
|
1130
1170
|
|
|
@@ -1133,7 +1173,7 @@ It appears that when the sftp server is running on Windows, a *ECONNRESET* error
|
|
|
1133
1173
|
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.
|
|
1134
1174
|
|
|
1135
1175
|
|
|
1136
|
-
<a id="
|
|
1176
|
+
<a id="org951849f"></a>
|
|
1137
1177
|
|
|
1138
1178
|
## Don't Re-use SftpClient Objects
|
|
1139
1179
|
|
|
@@ -1142,12 +1182,12 @@ Due to an issue with *ECONNRESET* error signals when connecting to Windows based
|
|
|
1142
1182
|
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.
|
|
1143
1183
|
|
|
1144
1184
|
|
|
1145
|
-
<a id="
|
|
1185
|
+
<a id="orgd48de8d"></a>
|
|
1146
1186
|
|
|
1147
1187
|
# FAQ
|
|
1148
1188
|
|
|
1149
1189
|
|
|
1150
|
-
<a id="
|
|
1190
|
+
<a id="orged95aaa"></a>
|
|
1151
1191
|
|
|
1152
1192
|
## Remote server drops connections with only an end event
|
|
1153
1193
|
|
|
@@ -1158,7 +1198,7 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
|
|
|
1158
1198
|
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.
|
|
1159
1199
|
|
|
1160
1200
|
|
|
1161
|
-
<a id="
|
|
1201
|
+
<a id="org62169e3"></a>
|
|
1162
1202
|
|
|
1163
1203
|
## How can I pass writeable stream as dst for get method?
|
|
1164
1204
|
|
|
@@ -1217,7 +1257,7 @@ sftp
|
|
|
1217
1257
|
```
|
|
1218
1258
|
|
|
1219
1259
|
|
|
1220
|
-
<a id="
|
|
1260
|
+
<a id="org2bb26ed"></a>
|
|
1221
1261
|
|
|
1222
1262
|
## How can I upload files without having to specify a password?
|
|
1223
1263
|
|
|
@@ -1252,7 +1292,7 @@ sftp.connect({
|
|
|
1252
1292
|
```
|
|
1253
1293
|
|
|
1254
1294
|
|
|
1255
|
-
<a id="
|
|
1295
|
+
<a id="orgc5e26e6"></a>
|
|
1256
1296
|
|
|
1257
1297
|
## How can I connect through a Socks Proxy
|
|
1258
1298
|
|
|
@@ -1289,7 +1329,7 @@ client.connect({
|
|
|
1289
1329
|
```
|
|
1290
1330
|
|
|
1291
1331
|
|
|
1292
|
-
<a id="
|
|
1332
|
+
<a id="org8337f16"></a>
|
|
1293
1333
|
|
|
1294
1334
|
## Timeout while waiting for handshake or handshake errors
|
|
1295
1335
|
|
|
@@ -1298,7 +1338,7 @@ Some users have encountered the error 'Timeout while waiting for handshake' or '
|
|
|
1298
1338
|
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.
|
|
1299
1339
|
|
|
1300
1340
|
|
|
1301
|
-
<a id="
|
|
1341
|
+
<a id="orga5eb823"></a>
|
|
1302
1342
|
|
|
1303
1343
|
## How can I limit upload/download speed
|
|
1304
1344
|
|
|
@@ -1338,7 +1378,7 @@ try {
|
|
|
1338
1378
|
```
|
|
1339
1379
|
|
|
1340
1380
|
|
|
1341
|
-
<a id="
|
|
1381
|
+
<a id="org2469055"></a>
|
|
1342
1382
|
|
|
1343
1383
|
## Connection hangs or fails for larger files
|
|
1344
1384
|
|
|
@@ -1349,21 +1389,21 @@ A symptom of this issue is that you are able to upload small files, but uploadin
|
|
|
1349
1389
|
For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
|
|
1350
1390
|
|
|
1351
1391
|
|
|
1352
|
-
<a id="
|
|
1392
|
+
<a id="orga1d843c"></a>
|
|
1353
1393
|
|
|
1354
1394
|
## Typescript definition file out of date
|
|
1355
1395
|
|
|
1356
1396
|
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.
|
|
1357
1397
|
|
|
1358
1398
|
|
|
1359
|
-
<a id="
|
|
1399
|
+
<a id="org0ab8b41"></a>
|
|
1360
1400
|
|
|
1361
1401
|
# Examples
|
|
1362
1402
|
|
|
1363
1403
|
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.
|
|
1364
1404
|
|
|
1365
1405
|
|
|
1366
|
-
<a id="
|
|
1406
|
+
<a id="org74214b7"></a>
|
|
1367
1407
|
|
|
1368
1408
|
# Troubleshooting
|
|
1369
1409
|
|
|
@@ -1378,14 +1418,14 @@ Note also that in the repository there are two useful directories. The first is
|
|
|
1378
1418
|
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.
|
|
1379
1419
|
|
|
1380
1420
|
|
|
1381
|
-
<a id="
|
|
1421
|
+
<a id="org9b87e06"></a>
|
|
1382
1422
|
|
|
1383
1423
|
## Common Errors
|
|
1384
1424
|
|
|
1385
1425
|
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.
|
|
1386
1426
|
|
|
1387
1427
|
|
|
1388
|
-
<a id="
|
|
1428
|
+
<a id="org0ef5618"></a>
|
|
1389
1429
|
|
|
1390
1430
|
### Not returning the promise in a `then()` block
|
|
1391
1431
|
|
|
@@ -1422,7 +1462,7 @@ Note the `return` statements. These ensure that the Promise returned by the clie
|
|
|
1422
1462
|
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.
|
|
1423
1463
|
|
|
1424
1464
|
|
|
1425
|
-
<a id="
|
|
1465
|
+
<a id="org3ef4393"></a>
|
|
1426
1466
|
|
|
1427
1467
|
### Mixing Promise Chains and Async/Await
|
|
1428
1468
|
|
|
@@ -1482,7 +1522,7 @@ async function doSftp() {
|
|
|
1482
1522
|
```
|
|
1483
1523
|
|
|
1484
1524
|
|
|
1485
|
-
<a id="
|
|
1525
|
+
<a id="org0cfccc7"></a>
|
|
1486
1526
|
|
|
1487
1527
|
### Try/catch and Error Handlers
|
|
1488
1528
|
|
|
@@ -1493,14 +1533,14 @@ The basic problem is that the try/catch block will have completed execution befo
|
|
|
1493
1533
|
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.
|
|
1494
1534
|
|
|
1495
1535
|
|
|
1496
|
-
<a id="
|
|
1536
|
+
<a id="org93b86a4"></a>
|
|
1497
1537
|
|
|
1498
1538
|
### Server Differences
|
|
1499
1539
|
|
|
1500
1540
|
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.
|
|
1501
1541
|
|
|
1502
1542
|
|
|
1503
|
-
<a id="
|
|
1543
|
+
<a id="orge2e0c12"></a>
|
|
1504
1544
|
|
|
1505
1545
|
### Avoid Concurrent Operations
|
|
1506
1546
|
|
|
@@ -1509,7 +1549,7 @@ Technically, SFTP should be able to perform multiple operations concurrently. As
|
|
|
1509
1549
|
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.
|
|
1510
1550
|
|
|
1511
1551
|
|
|
1512
|
-
<a id="
|
|
1552
|
+
<a id="orgbdaf17f"></a>
|
|
1513
1553
|
|
|
1514
1554
|
## Debugging Support
|
|
1515
1555
|
|
|
@@ -1542,7 +1582,7 @@ If you just want to see debug messages from `ssh2-sftp-client` and exclude debug
|
|
|
1542
1582
|
```
|
|
1543
1583
|
|
|
1544
1584
|
|
|
1545
|
-
<a id="
|
|
1585
|
+
<a id="org7853a7f"></a>
|
|
1546
1586
|
|
|
1547
1587
|
# Logging Issues
|
|
1548
1588
|
|
|
@@ -1559,7 +1599,7 @@ I am happy to try and help diagnose and fix any issues you encounter while using
|
|
|
1559
1599
|
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.
|
|
1560
1600
|
|
|
1561
1601
|
|
|
1562
|
-
<a id="
|
|
1602
|
+
<a id="org2eca80f"></a>
|
|
1563
1603
|
|
|
1564
1604
|
# Pull Requests
|
|
1565
1605
|
|
|
@@ -1576,7 +1616,7 @@ This module will adopt a standard semantic versioning policy. Please indicate in
|
|
|
1576
1616
|
- **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
|
|
1577
1617
|
|
|
1578
1618
|
|
|
1579
|
-
<a id="
|
|
1619
|
+
<a id="org8a2fb26"></a>
|
|
1580
1620
|
|
|
1581
1621
|
# Contributors
|
|
1582
1622
|
|