ssh2-sftp-client 9.0.3 → 9.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +177 -179
- package/README.org +66 -110
- package/package.json +3 -3
- package/src/index.js +600 -548
- package/src/utils.js +22 -5
package/README.md
CHANGED
|
@@ -1,66 +1,67 @@
|
|
|
1
|
-
- [Overview](#
|
|
2
|
-
- [Version 9.
|
|
3
|
-
- [Installation](#
|
|
4
|
-
- [Basic Usage](#
|
|
5
|
-
- [Documentation](#
|
|
6
|
-
- [Specifying Paths](#
|
|
7
|
-
- [Methods](#
|
|
8
|
-
- [new SftpClient(name) ===> SFTP client object](#
|
|
9
|
-
- [connect(config) ===> SFTP object](#
|
|
10
|
-
- [list(path, filter) ==> Array[object]](#
|
|
11
|
-
- [exists(path) ==> boolean](#
|
|
12
|
-
- [stat(path) ==> object](#
|
|
13
|
-
- [get(path, dst, options) ==> String|Stream|Buffer](#
|
|
14
|
-
- [fastGet(remotePath, localPath, options) ===> string](#
|
|
15
|
-
- [put(src, remotePath, options) ==> string](#
|
|
16
|
-
- [fastPut(localPath, remotePath, options) ==> string](#
|
|
17
|
-
- [append(input, remotePath, options) ==> string](#
|
|
18
|
-
- [mkdir(path, recursive) ==> string](#
|
|
19
|
-
- [rmdir(path, recursive) ==> string](#
|
|
20
|
-
- [delete(path, noErrorOK) ==> string](#
|
|
21
|
-
- [rename(fromPath, toPath) ==> string](#
|
|
22
|
-
- [posixRename(fromPath, toPath) ==> string](#
|
|
23
|
-
- [chmod(path, mode) ==> string](#
|
|
24
|
-
- [realPath(path) ===> string](#
|
|
25
|
-
- [cwd() ==> string](#
|
|
26
|
-
- [uploadDir(srcDir, dstDir, options) ==> string](#
|
|
27
|
-
- [downloadDir(srcDir, dstDir, options) ==> string](#
|
|
28
|
-
- [createReadStream(remotePath, options)) ==> stream object](#
|
|
29
|
-
- [createWriteStream(remotePath, options) ==> stream object](#
|
|
30
|
-
- [rcopy(srcPath, dstPath) ==> string](#
|
|
31
|
-
- [end() ==> boolean](#
|
|
32
|
-
- [Add and Remove Listeners](#
|
|
33
|
-
- [Platform Quirks & Warnings](#
|
|
34
|
-
- [Server Capabilities](#
|
|
35
|
-
- [Issues with `fastPut()` and `fastGet()` Methods](#
|
|
36
|
-
- [Promises, Events & Managing Exceptions](#
|
|
37
|
-
- [Adding Custom Handlers](#
|
|
38
|
-
- [Windows Based Servers](#
|
|
39
|
-
- [Don't Re-use SftpClient Objects](#
|
|
40
|
-
- [FAQ](#
|
|
41
|
-
- [Remote server drops connections with only an end event](#
|
|
42
|
-
- [How can I pass
|
|
43
|
-
- [How can I upload files without having to specify a password?](#
|
|
44
|
-
- [How can I connect through a Socks Proxy](#
|
|
45
|
-
- [Timeout while waiting for handshake or handshake errors](#
|
|
46
|
-
- [How can I limit upload/download speed](#
|
|
47
|
-
- [Connection hangs or fails for larger files](#
|
|
48
|
-
- [
|
|
49
|
-
- [
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
- [
|
|
53
|
-
- [
|
|
54
|
-
- [
|
|
55
|
-
- [
|
|
56
|
-
|
|
57
|
-
- [
|
|
58
|
-
- [
|
|
59
|
-
- [
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
1
|
+
- [Overview](#orgd72cafd)
|
|
2
|
+
- [Version 9.1.0 Changes](#org9aa6de9)
|
|
3
|
+
- [Installation](#orgfa343ca)
|
|
4
|
+
- [Basic Usage](#org4ac2087)
|
|
5
|
+
- [Documentation](#orgd23cd4f)
|
|
6
|
+
- [Specifying Paths](#org0cb332c)
|
|
7
|
+
- [Methods](#org67943d5)
|
|
8
|
+
- [new SftpClient(name) ===> SFTP client object](#orgc12cc98)
|
|
9
|
+
- [connect(config) ===> SFTP object](#org93b5103)
|
|
10
|
+
- [list(path, filter) ==> Array[object]](#org330e94d)
|
|
11
|
+
- [exists(path) ==> boolean](#orgb3e6094)
|
|
12
|
+
- [stat(path) ==> object](#org2b25315)
|
|
13
|
+
- [get(path, dst, options) ==> String|Stream|Buffer](#org252a716)
|
|
14
|
+
- [fastGet(remotePath, localPath, options) ===> string](#org5edb596)
|
|
15
|
+
- [put(src, remotePath, options) ==> string](#org99d1b64)
|
|
16
|
+
- [fastPut(localPath, remotePath, options) ==> string](#orgdce84b4)
|
|
17
|
+
- [append(input, remotePath, options) ==> string](#org86b6dd8)
|
|
18
|
+
- [mkdir(path, recursive) ==> string](#orga867f75)
|
|
19
|
+
- [rmdir(path, recursive) ==> string](#org893c3d4)
|
|
20
|
+
- [delete(path, noErrorOK) ==> string](#org877ca9f)
|
|
21
|
+
- [rename(fromPath, toPath) ==> string](#orgc41333c)
|
|
22
|
+
- [posixRename(fromPath, toPath) ==> string](#org9493ebc)
|
|
23
|
+
- [chmod(path, mode) ==> string](#orgf78d430)
|
|
24
|
+
- [realPath(path) ===> string](#orgf15a5df)
|
|
25
|
+
- [cwd() ==> string](#org2f554a8)
|
|
26
|
+
- [uploadDir(srcDir, dstDir, options) ==> string](#orge085c95)
|
|
27
|
+
- [downloadDir(srcDir, dstDir, options) ==> string](#orgd7cebbc)
|
|
28
|
+
- [createReadStream(remotePath, options)) ==> stream object](#orgc133c2f)
|
|
29
|
+
- [createWriteStream(remotePath, options) ==> stream object](#org44e3122)
|
|
30
|
+
- [rcopy(srcPath, dstPath) ==> string](#org3394196)
|
|
31
|
+
- [end() ==> boolean](#org5f0471c)
|
|
32
|
+
- [Add and Remove Listeners](#orga12e63e)
|
|
33
|
+
- [Platform Quirks & Warnings](#org3cbc095)
|
|
34
|
+
- [Server Capabilities](#orge04f28e)
|
|
35
|
+
- [Issues with `fastPut()` and `fastGet()` Methods](#orgc96002e)
|
|
36
|
+
- [Promises, Events & Managing Exceptions](#org955a771)
|
|
37
|
+
- [Adding Custom Handlers](#org8f7b2d0)
|
|
38
|
+
- [Windows Based Servers](#orge15889c)
|
|
39
|
+
- [Don't Re-use SftpClient Objects](#orgb38b894)
|
|
40
|
+
- [FAQ](#org1e76649)
|
|
41
|
+
- [Remote server drops connections with only an end event](#orge3e4159)
|
|
42
|
+
- [How can I pass writeable stream as dst for get method?](#org77ed2d3)
|
|
43
|
+
- [How can I upload files without having to specify a password?](#orgad09bd5)
|
|
44
|
+
- [How can I connect through a Socks Proxy](#org175d696)
|
|
45
|
+
- [Timeout while waiting for handshake or handshake errors](#orgd310a6a)
|
|
46
|
+
- [How can I limit upload/download speed](#org5a0faa2)
|
|
47
|
+
- [Connection hangs or fails for larger files](#orgffe3a3c)
|
|
48
|
+
- [Typescript definition file out of date](#org390645f)
|
|
49
|
+
- [Examples](#orgce20a25)
|
|
50
|
+
- [Troubleshooting](#org31d0059)
|
|
51
|
+
- [Common Errors](#orgf1c2f29)
|
|
52
|
+
- [Not returning the promise in a `then()` block](#org14ab27b)
|
|
53
|
+
- [Mixing Promise Chains and Async/Await](#org6c257e3)
|
|
54
|
+
- [Try/catch and Error Handlers](#org765b1b2)
|
|
55
|
+
- [Server Differences](#orge67d910)
|
|
56
|
+
- [Avoid Concurrent Operations](#org734662f)
|
|
57
|
+
- [Debugging Support](#org658f3d6)
|
|
58
|
+
- [Logging Issues](#orgb0fbb63)
|
|
59
|
+
- [Pull Requests](#orgc22fdba)
|
|
60
|
+
- [Contributors](#org7bb6675)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
<a id="orgd72cafd"></a>
|
|
64
65
|
|
|
65
66
|
# Overview
|
|
66
67
|
|
|
@@ -68,35 +69,23 @@ an SFTP client for node.js, a wrapper around [SSH2](https://github.com/mscdex/ss
|
|
|
68
69
|
|
|
69
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.
|
|
70
71
|
|
|
71
|
-
Current stable release is
|
|
72
|
+
Current stable release is \*v9.1.0.
|
|
72
73
|
|
|
73
|
-
Code has been tested against Node versions 14.19.1, 16.
|
|
74
|
+
Code has been tested against Node versions 14.21.3, 16.19.1, 18.16.0 and 20.0.0
|
|
74
75
|
|
|
75
76
|
Node versions < 14.x are not supported.
|
|
76
77
|
|
|
77
78
|
|
|
78
|
-
<a id="
|
|
79
|
+
<a id="org9aa6de9"></a>
|
|
79
80
|
|
|
80
|
-
## Version 9.
|
|
81
|
+
## Version 9.1.0 Changes
|
|
81
82
|
|
|
82
|
-
-
|
|
83
|
-
-
|
|
84
|
-
-
|
|
85
|
-
- Changed when event handled flags are reset. Now they are reset after a new set of temporary listeners are added.
|
|
86
|
-
- Don't throw an error when calling end() if there is no active sftp connection. It does no harm to call end() when there is no connection, so no need to raise an error.
|
|
87
|
-
- Use nullish coalescing when setting retry parameters instead of or'ing with defaults. Allows setting values to 0.
|
|
88
|
-
- **Breaking Change**: This version uses syntax not supported in node versions prior to v14. Therefore, node versions less than v14 will not work.
|
|
89
|
-
- **Breaking Change**: This `list()` method no longer accepts a regular expression for filtering the entries to be returned. You can now specify a filter function instead. The function is called for each item in the list of items to be returned, passing in the item object as its only argument. Essentially, this is just a call to `Array.filter()`, so the filter function should behave in the same way i.e. return true for items to be retained and false for those to be dropped.
|
|
90
|
-
- **Breaking Change**: The ability to set `autoClose` on read and write streams and the ability to set `end` on `pipe` operations has been removed. These options caused confusion for users and were too easy to get wrong, plus it made the methods overly complicated. For those use-cases where you want to control streams at a low level, two new methods have been added, `createReadStream()` and `createWriteStream()`. However, it should be noted that client code is 100% responsible for managing streams obtained using these methods. Use at your own risk!
|
|
91
|
-
- **Breaking Change**: The 3rd argument to `uploadDir()` and `downloadDir()` methods has been change. Previously, the argument was a filter function used to select which directories and files to be transferred. The 3rd argument is now an options object with two supported properties, `filter` and `useFastput` (for `uploadDir()`) or `useFastget` (for `downloadDir()`). If `useFastput` is true, the `fastPut()` method will be pused to upload files. If `false` or missing, the slower, but better supported, `put()` method will be used. Likewise, the `useFastget` options can be set to `true` to use the `fastGet()` method for donwloading files, otherwise the slower, but more reliable, `get()` method will be used.
|
|
92
|
-
- The `uploadDir()` and `downloadDir()` methods now use asynchrounous processes to upload/download files. This should result in improved performance for these two methods.
|
|
93
|
-
- New Methods: Two new methods, `createWriteStream()` and `createReadStream()` have been added. These methods will return a stream object connected to a remote file on the `sftp` server. Client code is responsible for managing these stream objects. This includes adding any necessary event listeners and disposing of the objects once finished with them.
|
|
94
|
-
- Refactoring of Listeners: The library manages temporary listeners in order to provide a way to catch events and processes them inside a `Promise` context. Previously, every method added its own set of temporary listeners. However, this could result in multiple sets of listeners being added, especially for methods which call other methods as part of their processing e.g. `rmdir(),` `uploadDir()` and `dowqnloadDir()`. To avoid this, *internal only* versions of each method have been created. These internal methods use an *underscore* `_` prefix. Client code should not use these methods directly.
|
|
95
|
-
- New method: Added `rcopy()` method to perform a remote copy of a file on the remote SFTP server.
|
|
96
|
-
- Bumped ssh2 version to 1.11.0
|
|
83
|
+
- Added lstat() method
|
|
84
|
+
- Fixed bug in option hadnling which was preventing setting file mode in get() and put() methods
|
|
85
|
+
- Fixed bug where a loss of network connections between establishment of the connection and calling various sftp methods was not handled and could result in an event causing the node process to exit with an error.
|
|
97
86
|
|
|
98
87
|
|
|
99
|
-
<a id="
|
|
88
|
+
<a id="orgfa343ca"></a>
|
|
100
89
|
|
|
101
90
|
# Installation
|
|
102
91
|
|
|
@@ -105,11 +94,11 @@ npm install ssh2-sftp-client
|
|
|
105
94
|
```
|
|
106
95
|
|
|
107
96
|
|
|
108
|
-
<a id="
|
|
97
|
+
<a id="org4ac2087"></a>
|
|
109
98
|
|
|
110
99
|
# Basic Usage
|
|
111
100
|
|
|
112
|
-
```
|
|
101
|
+
```js
|
|
113
102
|
let Client = require('ssh2-sftp-client');
|
|
114
103
|
let sftp = new Client();
|
|
115
104
|
|
|
@@ -128,7 +117,7 @@ sftp.connect({
|
|
|
128
117
|
```
|
|
129
118
|
|
|
130
119
|
|
|
131
|
-
<a id="
|
|
120
|
+
<a id="orgd23cd4f"></a>
|
|
132
121
|
|
|
133
122
|
# Documentation
|
|
134
123
|
|
|
@@ -137,7 +126,7 @@ The connection options are the same as those offered by the underlying SSH2 modu
|
|
|
137
126
|
All the methods will return a Promise, except for `on()` and `removeListener()`, which are typically only used in special use cases.
|
|
138
127
|
|
|
139
128
|
|
|
140
|
-
<a id="
|
|
129
|
+
<a id="org0cb332c"></a>
|
|
141
130
|
|
|
142
131
|
## Specifying Paths
|
|
143
132
|
|
|
@@ -149,13 +138,13 @@ It is important to recognise that the current remote directory may not always be
|
|
|
149
138
|
|
|
150
139
|
There is a small performance hit for using `./` and `../` as the module must query the remote server to determine what the root path is and derive the absolute path. Using absolute paths are therefore more efficient and likely more robust.
|
|
151
140
|
|
|
152
|
-
When specifying file paths, ensure to include a full path i.e. include the remote
|
|
141
|
+
When specifying file paths, ensure to include a full path i.e. include the remote file name. Don't expect the module to append the local file name to the path you provide. For example, the following will not work
|
|
153
142
|
|
|
154
143
|
```javascript
|
|
155
144
|
client.put('/home/fred/test.txt', '/remote/dir');
|
|
156
145
|
```
|
|
157
146
|
|
|
158
|
-
will not result in the file `test.txt` being copied to `/remote/dir/test.txt`. You need to specify the target
|
|
147
|
+
will not result in the file `test.txt` being copied to `/remote/dir/test.txt`. You need to specify the target file name as well e.g.
|
|
159
148
|
|
|
160
149
|
```javascript
|
|
161
150
|
client.put('/home/fred/test.txt', '/remote/dir/test.txt');
|
|
@@ -170,12 +159,12 @@ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
|
|
|
170
159
|
This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
|
|
171
160
|
|
|
172
161
|
|
|
173
|
-
<a id="
|
|
162
|
+
<a id="org67943d5"></a>
|
|
174
163
|
|
|
175
164
|
## Methods
|
|
176
165
|
|
|
177
166
|
|
|
178
|
-
<a id="
|
|
167
|
+
<a id="orgc12cc98"></a>
|
|
179
168
|
|
|
180
169
|
### new SftpClient(name) ===> SFTP client object
|
|
181
170
|
|
|
@@ -214,7 +203,7 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
|
|
|
214
203
|
```
|
|
215
204
|
|
|
216
205
|
|
|
217
|
-
<a id="
|
|
206
|
+
<a id="org93b5103"></a>
|
|
218
207
|
|
|
219
208
|
### connect(config) ===> SFTP object
|
|
220
209
|
|
|
@@ -240,12 +229,12 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
240
229
|
privateKey: fs.readFileSync('/path/to/key'), // Buffer or string that contains
|
|
241
230
|
passphrase: 'a pass phrase', // string - For an encrypted private key
|
|
242
231
|
readyTimeout: 20000, // integer How long (in ms) to wait for the SSH handshake
|
|
243
|
-
strictVendor: true // boolean - Performs a strict server vendor check
|
|
244
|
-
debug: myDebug
|
|
232
|
+
strictVendor: true, // boolean - Performs a strict server vendor check
|
|
233
|
+
debug: myDebug,// function - Set this to a function that receives a single
|
|
245
234
|
// string argument to get detailed (local) debug information.
|
|
246
|
-
retries: 2 // integer. Number of times to retry connecting
|
|
247
|
-
retry_factor: 2 // integer. Time factor used to calculate time between retries
|
|
248
|
-
retry_minTimeout: 2000 // integer. Minimum timeout between attempts
|
|
235
|
+
retries: 2, // integer. Number of times to retry connecting
|
|
236
|
+
retry_factor: 2, // integer. Time factor used to calculate time between retries
|
|
237
|
+
retry_minTimeout: 2000, // integer. Minimum timeout between attempts
|
|
249
238
|
};
|
|
250
239
|
|
|
251
240
|
// rarely used options
|
|
@@ -272,7 +261,7 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
272
261
|
|
|
273
262
|
```javascript
|
|
274
263
|
sftp.connect({
|
|
275
|
-
host: example.com,
|
|
264
|
+
host: 'example.com',
|
|
276
265
|
port: 22,
|
|
277
266
|
username: 'donald',
|
|
278
267
|
password: 'youarefired'
|
|
@@ -280,7 +269,7 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
280
269
|
```
|
|
281
270
|
|
|
282
271
|
|
|
283
|
-
<a id="
|
|
272
|
+
<a id="org330e94d"></a>
|
|
284
273
|
|
|
285
274
|
### list(path, filter) ==> Array[object]
|
|
286
275
|
|
|
@@ -301,7 +290,7 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
301
290
|
password: 'my-secret'
|
|
302
291
|
};
|
|
303
292
|
|
|
304
|
-
let sftp = new Client;
|
|
293
|
+
let sftp = new Client();
|
|
305
294
|
|
|
306
295
|
sftp.connect(config)
|
|
307
296
|
.then(() => {
|
|
@@ -324,24 +313,24 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
324
313
|
|
|
325
314
|
```javascript
|
|
326
315
|
{
|
|
327
|
-
type: // file type(-, d, l)
|
|
328
|
-
name: // file name
|
|
329
|
-
size: // file size
|
|
330
|
-
modifyTime: // file timestamp of modified time
|
|
331
|
-
accessTime: // file timestamp of access time
|
|
316
|
+
type: '-', // file type(-, d, l)
|
|
317
|
+
name: 'example.txt', // file name
|
|
318
|
+
size: 43, // file size
|
|
319
|
+
modifyTime: 1675645360000, // file timestamp of modified time
|
|
320
|
+
accessTime: 1675645360000, // file timestamp of access time
|
|
332
321
|
rights: {
|
|
333
|
-
user:
|
|
334
|
-
group:
|
|
335
|
-
other:
|
|
322
|
+
user: 'rw',
|
|
323
|
+
group: 'r',
|
|
324
|
+
other: 'r',
|
|
336
325
|
},
|
|
337
|
-
owner: // user ID
|
|
338
|
-
group: // group ID
|
|
339
|
-
longname: // like ls -l line
|
|
326
|
+
owner: 1000, // user ID
|
|
327
|
+
group: 1000, // group ID
|
|
328
|
+
longname: '-rw-r--r-- 1 fred fred 43 Feb 6 12:02 exaple.txt', // like ls -l line
|
|
340
329
|
}
|
|
341
330
|
```
|
|
342
331
|
|
|
343
332
|
|
|
344
|
-
<a id="
|
|
333
|
+
<a id="orgb3e6094"></a>
|
|
345
334
|
|
|
346
335
|
### exists(path) ==> boolean
|
|
347
336
|
|
|
@@ -359,7 +348,7 @@ Tests to see if remote file or directory exists. Returns type of remote object i
|
|
|
359
348
|
password: 'my-secret'
|
|
360
349
|
};
|
|
361
350
|
|
|
362
|
-
let sftp = new Client;
|
|
351
|
+
let sftp = new Client();
|
|
363
352
|
|
|
364
353
|
sftp.connect(config)
|
|
365
354
|
.then(() => {
|
|
@@ -377,7 +366,7 @@ Tests to see if remote file or directory exists. Returns type of remote object i
|
|
|
377
366
|
```
|
|
378
367
|
|
|
379
368
|
|
|
380
|
-
<a id="
|
|
369
|
+
<a id="org2b25315"></a>
|
|
381
370
|
|
|
382
371
|
### stat(path) ==> object
|
|
383
372
|
|
|
@@ -428,7 +417,7 @@ Returns the attributes associated with the object pointed to by `path`.
|
|
|
428
417
|
```
|
|
429
418
|
|
|
430
419
|
|
|
431
|
-
<a id="
|
|
420
|
+
<a id="org252a716"></a>
|
|
432
421
|
|
|
433
422
|
### get(path, dst, options) ==> String|Stream|Buffer
|
|
434
423
|
|
|
@@ -484,7 +473,7 @@ In general, if you're going to pass in a string as the destination, you are bett
|
|
|
484
473
|
- **Tip:** See examples file in the Git repository for more examples. You can pass any writeable stream in as the destination. For example, if you pass in `zlib.createGunzip()` writeable stream, you can both download and decompress a gzip file 'on the fly'.
|
|
485
474
|
|
|
486
475
|
|
|
487
|
-
<a id="
|
|
476
|
+
<a id="org5edb596"></a>
|
|
488
477
|
|
|
489
478
|
### fastGet(remotePath, localPath, options) ===> string
|
|
490
479
|
|
|
@@ -527,7 +516,7 @@ Downloads a file at remotePath to localPath using parallel reads for faster thro
|
|
|
527
516
|
```
|
|
528
517
|
|
|
529
518
|
|
|
530
|
-
<a id="
|
|
519
|
+
<a id="org99d1b64"></a>
|
|
531
520
|
|
|
532
521
|
### put(src, remotePath, options) ==> string
|
|
533
522
|
|
|
@@ -577,7 +566,7 @@ Upload data from local system to remote server. If the `src` argument is a strin
|
|
|
577
566
|
- **Tip:** If the src argument is a path string, consider just using `fastPut()`.
|
|
578
567
|
|
|
579
568
|
|
|
580
|
-
<a id="
|
|
569
|
+
<a id="orgdce84b4"></a>
|
|
581
570
|
|
|
582
571
|
### fastPut(localPath, remotePath, options) ==> string
|
|
583
572
|
|
|
@@ -621,7 +610,7 @@ Uploads the data in file at `localPath` to a new file on remote server at `remot
|
|
|
621
610
|
```
|
|
622
611
|
|
|
623
612
|
|
|
624
|
-
<a id="
|
|
613
|
+
<a id="org86b6dd8"></a>
|
|
625
614
|
|
|
626
615
|
### append(input, remotePath, options) ==> string
|
|
627
616
|
|
|
@@ -665,7 +654,7 @@ Append the `input` data to an existing remote file. There is no integrity checki
|
|
|
665
654
|
```
|
|
666
655
|
|
|
667
656
|
|
|
668
|
-
<a id="
|
|
657
|
+
<a id="orga867f75"></a>
|
|
669
658
|
|
|
670
659
|
### mkdir(path, recursive) ==> string
|
|
671
660
|
|
|
@@ -693,7 +682,7 @@ Create a new directory. If the recursive flag is set to true, the method will cr
|
|
|
693
682
|
```
|
|
694
683
|
|
|
695
684
|
|
|
696
|
-
<a id="
|
|
685
|
+
<a id="org893c3d4"></a>
|
|
697
686
|
|
|
698
687
|
### rmdir(path, recursive) ==> string
|
|
699
688
|
|
|
@@ -723,7 +712,7 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
|
|
|
723
712
|
```
|
|
724
713
|
|
|
725
714
|
|
|
726
|
-
<a id="
|
|
715
|
+
<a id="org877ca9f"></a>
|
|
727
716
|
|
|
728
717
|
### delete(path, noErrorOK) ==> string
|
|
729
718
|
|
|
@@ -752,7 +741,7 @@ Delete a file on the remote server.
|
|
|
752
741
|
```
|
|
753
742
|
|
|
754
743
|
|
|
755
|
-
<a id="
|
|
744
|
+
<a id="orgc41333c"></a>
|
|
756
745
|
|
|
757
746
|
### rename(fromPath, toPath) ==> string
|
|
758
747
|
|
|
@@ -781,11 +770,11 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
|
|
|
781
770
|
```
|
|
782
771
|
|
|
783
772
|
|
|
784
|
-
<a id="
|
|
773
|
+
<a id="org9493ebc"></a>
|
|
785
774
|
|
|
786
775
|
### posixRename(fromPath, toPath) ==> string
|
|
787
776
|
|
|
788
|
-
This method uses the openssh POSIX rename extension introduced in OpenSSH 4.8. The advantage of this version of rename over standard SFTP rename is that it is an atomic operation and will allow renaming a resource where the destination name exists. The POSIX rename will also work on some
|
|
777
|
+
This method uses the openssh POSIX rename extension introduced in OpenSSH 4.8. The advantage of this version of rename over standard SFTP rename is that it is an atomic operation and will allow renaming a resource where the destination name exists. The POSIX rename will also work on some file systems which do not support standard SFTP rename because they don't support the system hardlink() call. The POSIX rename extension is available on all openSSH servers from 4.8 and some other implementations. This is an extension to the standard SFTP protocol and therefore is not supported on all sftp servers.
|
|
789
778
|
|
|
790
779
|
- **fromPath:** string. Path to existing file to be renamed.
|
|
791
780
|
- **toPath:** string. Path for new name. If it already exists, it will be replaced by file specified in fromPath
|
|
@@ -808,7 +797,7 @@ client.connect(config)
|
|
|
808
797
|
```
|
|
809
798
|
|
|
810
799
|
|
|
811
|
-
<a id="
|
|
800
|
+
<a id="orgf78d430"></a>
|
|
812
801
|
|
|
813
802
|
### chmod(path, mode) ==> string
|
|
814
803
|
|
|
@@ -837,7 +826,7 @@ Change the mode (read, write or execute permissions) of a remote file or directo
|
|
|
837
826
|
```
|
|
838
827
|
|
|
839
828
|
|
|
840
|
-
<a id="
|
|
829
|
+
<a id="orgf15a5df"></a>
|
|
841
830
|
|
|
842
831
|
### realPath(path) ===> string
|
|
843
832
|
|
|
@@ -848,14 +837,14 @@ Converts a relative path to an absolute path on the remote server. This method i
|
|
|
848
837
|
- **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
|
|
849
838
|
|
|
850
839
|
|
|
851
|
-
<a id="
|
|
840
|
+
<a id="org2f554a8"></a>
|
|
852
841
|
|
|
853
842
|
### cwd() ==> string
|
|
854
843
|
|
|
855
844
|
Returns what the server believes is the current remote working directory.
|
|
856
845
|
|
|
857
846
|
|
|
858
|
-
<a id="
|
|
847
|
+
<a id="orge085c95"></a>
|
|
859
848
|
|
|
860
849
|
### uploadDir(srcDir, dstDir, options) ==> string
|
|
861
850
|
|
|
@@ -924,13 +913,13 @@ The `useFastput` option is a boolean option. If `true`, the method will use the
|
|
|
924
913
|
```
|
|
925
914
|
|
|
926
915
|
|
|
927
|
-
<a id="
|
|
916
|
+
<a id="orgd7cebbc"></a>
|
|
928
917
|
|
|
929
918
|
### downloadDir(srcDir, dstDir, options) ==> string
|
|
930
919
|
|
|
931
920
|
Download the remote directory specified by `srcDir` to the local file system directory specified by `dstDir`. The `dstDir` directory will be created if required. All sub directories within `srcDir` will also be copied. Any existing files in the local path will be overwritten. No files in the local path will be deleted.
|
|
932
921
|
|
|
933
|
-
The method also
|
|
922
|
+
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.
|
|
934
923
|
|
|
935
924
|
The `options` argument is an options object with two supported properties, `filter` and `useFastget`. The `filter` argument is a predicate function which will be called with two arguments for each potential item to be downloaded. The first argument is the full path of the item and the second argument is a boolean, which will be true if the item is a directory. If the function returns true, the item will be included in the download. If it returns false, it will be filtered and ignored. The filter function is called via the `Array.filter` method. These array comprehension methods are known to be unsafe for asynchronous functions. Therefore, only synchronous filter functions are supported at this time.
|
|
936
925
|
|
|
@@ -938,7 +927,7 @@ If the `useFastget` property is set to `true`, the method will use `fastGet()` t
|
|
|
938
927
|
|
|
939
928
|
- **srcDir:** A remote file path specified as a string
|
|
940
929
|
- **dstDir:** A local file path specified as a string
|
|
941
|
-
- **options:** An object with two supported properties, `filter` and `useFastget`. The filter property is a function accepting two arguments, the full path to an item and a boolean value which will be true if the item is a directory. The function is called for each item in the download path and should return true to include the item and false to exclude it in the download. The `useFastget` property is a boolean. If true, the `fastGet()` method will be used to transfer files. If `false` (the default), the slower but better supported `get()`
|
|
930
|
+
- **options:** An object with two supported properties, `filter` and `useFastget`. The filter property is a function accepting two arguments, the full path to an item and a boolean value which will be true if the item is a directory. The function is called for each item in the download path and should return true to include the item and false to exclude it in the download. The `useFastget` property is a boolean. If true, the `fastGet()` method will be used to transfer files. If `false` (the default), the slower but better supported `get()` method is used. .
|
|
942
931
|
|
|
943
932
|
1. Example
|
|
944
933
|
|
|
@@ -989,7 +978,7 @@ If the `useFastget` property is set to `true`, the method will use `fastGet()` t
|
|
|
989
978
|
```
|
|
990
979
|
|
|
991
980
|
|
|
992
|
-
<a id="
|
|
981
|
+
<a id="orgc133c2f"></a>
|
|
993
982
|
|
|
994
983
|
### createReadStream(remotePath, options)) ==> stream object
|
|
995
984
|
|
|
@@ -1003,35 +992,35 @@ Returns a read stream object which is attached to the remote file specified by t
|
|
|
1003
992
|
- **mode:** 0o666
|
|
1004
993
|
- **autoClose:** defaults to true. If set to false, client code is responsible for closing file descriptors when finished
|
|
1005
994
|
- **start:** Default 0. Position to start reading bytes from (inclusive)
|
|
1006
|
-
- **end:**
|
|
995
|
+
- **end:** Position to stop reading bytes (inclusive).
|
|
1007
996
|
|
|
1008
997
|
|
|
1009
|
-
<a id="
|
|
998
|
+
<a id="org44e3122"></a>
|
|
1010
999
|
|
|
1011
1000
|
### createWriteStream(remotePath, options) ==> stream object
|
|
1012
1001
|
|
|
1013
|
-
Returns a write stream object which is attached to the remote file specified in the `remotePath` argument. This is a low
|
|
1002
|
+
Returns a write stream object which is attached to the remote file specified in the `remotePath` argument. This is a low level function which just returns the stream object. Client code is fully responsible for managing that object, including closing any file descriptors and removing listeners etc.
|
|
1014
1003
|
|
|
1015
1004
|
- **remotePath:** Path to the remote file specified as a string
|
|
1016
1005
|
- **options:** An object containing stream options. Supported properties include
|
|
1017
1006
|
- **flags:** default 'w'
|
|
1018
|
-
- **encoding:**
|
|
1007
|
+
- **encoding:** default null
|
|
1019
1008
|
- **mode:** 0o666
|
|
1020
1009
|
- **autoClose:** true
|
|
1021
1010
|
- **start:** Byte position to start writing from (inclusive). May require changing flag to 'r+'.
|
|
1022
1011
|
|
|
1023
1012
|
|
|
1024
|
-
<a id="
|
|
1013
|
+
<a id="org3394196"></a>
|
|
1025
1014
|
|
|
1026
1015
|
### rcopy(srcPath, dstPath) ==> string
|
|
1027
1016
|
|
|
1028
|
-
|
|
1017
|
+
Perform a remote file copy. The file identified by the `srcPath` argument will be copied to the file specified as the `dstPath` argument. The directory where `dstPath` will be placed must exist, but the actual file must not i.e. no overwrites allowed.
|
|
1029
1018
|
|
|
1030
1019
|
- **srcPath:** Path to remote file to be copied specified as a string
|
|
1031
|
-
- **dstPath:** Path to where the copy will be
|
|
1020
|
+
- **dstPath:** Path to where the copy will be created specified as a string
|
|
1032
1021
|
|
|
1033
1022
|
|
|
1034
|
-
<a id="
|
|
1023
|
+
<a id="org5f0471c"></a>
|
|
1035
1024
|
|
|
1036
1025
|
### end() ==> boolean
|
|
1037
1026
|
|
|
@@ -1055,7 +1044,7 @@ Ends the current client session, releasing the client socket and associated reso
|
|
|
1055
1044
|
```
|
|
1056
1045
|
|
|
1057
1046
|
|
|
1058
|
-
<a id="
|
|
1047
|
+
<a id="orga12e63e"></a>
|
|
1059
1048
|
|
|
1060
1049
|
### Add and Remove Listeners
|
|
1061
1050
|
|
|
@@ -1076,56 +1065,56 @@ Although normally not required, you can add and remove custom listeners on the s
|
|
|
1076
1065
|
Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
|
|
1077
1066
|
|
|
1078
1067
|
|
|
1079
|
-
<a id="
|
|
1068
|
+
<a id="org3cbc095"></a>
|
|
1080
1069
|
|
|
1081
1070
|
# Platform Quirks & Warnings
|
|
1082
1071
|
|
|
1083
1072
|
|
|
1084
|
-
<a id="
|
|
1073
|
+
<a id="orge04f28e"></a>
|
|
1085
1074
|
|
|
1086
1075
|
## Server Capabilities
|
|
1087
1076
|
|
|
1088
|
-
All SFTP servers and platforms are not equal. Some facilities provided by `ssh2-sftp-client` either depend on capabilities of the remote server or the underlying capabilities of the remote server platform. As an example, consider `chmod()`. This command depends on a remote
|
|
1077
|
+
All SFTP servers and platforms are not equal. Some facilities provided by `ssh2-sftp-client` either depend on capabilities of the remote server or the underlying capabilities of the remote server platform. As an example, consider `chmod()`. This command depends on a remote file system which implements the 'nix' concept of users and groups. The *win32* platform does not have the same concept of users and groups, so `chmod()` will not behave in the same way.
|
|
1089
1078
|
|
|
1090
1079
|
One way to determine whether an issue you are encountering is due to `ssh2-sftp-client` or due to the remote server or server platform is to use a simple CLI sftp program, such as openSSH's sftp command. If you observe the same behaviour using plain `sftp` on the command line, the issue is likely due to server or remote platform limitations. Note that you should not use a GUI sftp client, like `Filezilla` or `winSCP` as such GUI programs often attempt to hide these server and platform incompatibilities and will take additional steps to simulate missing functionality etc. You want to use a CLI program which does as little as possible.
|
|
1091
1080
|
|
|
1092
1081
|
|
|
1093
|
-
<a id="
|
|
1082
|
+
<a id="orgc96002e"></a>
|
|
1094
1083
|
|
|
1095
1084
|
## Issues with `fastPut()` and `fastGet()` Methods
|
|
1096
1085
|
|
|
1097
|
-
The `fastPut()` and `fastGet()` methods are known to be somewhat dependent on SFTP server capabilities. Some SFTP servers just do not work correctly with concurrent connections and some are known to have issues with negotiating packet sizes. These issues can sometimes be resolved by tweaking the options supplied to the methods, such as setting number of concurrent connections or a
|
|
1086
|
+
The `fastPut()` and `fastGet()` methods are known to be somewhat dependent on SFTP server capabilities. Some SFTP servers just do not work correctly with concurrent connections and some are known to have issues with negotiating packet sizes. These issues can sometimes be resolved by tweaking the options supplied to the methods, such as setting number of concurrent connections or a specific packet size.
|
|
1098
1087
|
|
|
1099
1088
|
To see an example of the type of issues you can observe with `fastPut()` or `fastGet()`, have a look at [issue 407](https://github.com/theophilusx/ssh2-sftp-client/issues/407), which describes the experiences of one user. Bottom line, when it works, it tends to work well and be significantly faster than using just `get()` or `put()`. However, when developing code to run against different SFTP servers, especially where you are unable to test against each server, you are likely better off just using `get()` and `put()` or structuring your code so that users can select which method to use (this is what `ssh2-sftp-client` does - for example, see the `!downloadDir()` and `uploadDir()` methods.
|
|
1100
1089
|
|
|
1101
1090
|
|
|
1102
|
-
<a id="
|
|
1091
|
+
<a id="org955a771"></a>
|
|
1103
1092
|
|
|
1104
1093
|
## Promises, Events & Managing Exceptions
|
|
1105
1094
|
|
|
1106
1095
|
One of the challenges in providing a Promise based API over a module like SSH2, which is event based is how to ensure events are handled appropriately. The challenge is due to the synchronous nature of events. You cannot use `try/catch` for events because you have no way of knowing when the event might fire. For example, it could easily fire after your `try/catch` block as completed execution.
|
|
1107
1096
|
|
|
1108
|
-
Things become even more complicated once you mix in Promises. When you define a promise, you have to methods which can be called to fulfil a promise, `resolve` and `reject`. Only one can be called - once you call `resolve`, you cannot call `reject` (well, you can call it, but it won't have any impact on the fulfilment status of the promise). The problem arises when an event, for
|
|
1097
|
+
Things become even more complicated once you mix in Promises. When you define a promise, you have to methods which can be called to fulfil a promise, `resolve` and `reject`. Only one can be called - once you call `resolve`, you cannot call `reject` (well, you can call it, but it won't have any impact on the fulfilment status of the promise). The problem arises when an event, for example an `error` event is fired either after you have resolved a promise or possibly in-between promises. If you don't catch the `error` event, your script will likely crash with an `uncaught exception` error.
|
|
1109
1098
|
|
|
1110
1099
|
To make matters worse, some servers, particularly servers running on a Windows platform, will raise multiple errors for the same error *event*. For example, when you attempt to connect with a bad username or password, you will get a `All authentication methods have failed` exception. However, under Windows, you will also get a `Connection reset by peer` exception. If we reject the connect promise based on the authentication failure exception, what do we do with the `reset by peer` exception? More critically, what will handle that exception given the promise has already been fulfilled and completed? To make matters worse, it seems that Windows based servers also raise an error event for *non-errors*. For example, when you call the `end()` method, the connection is closed. On windows, this also results in a *connection reset by peer* error. While it could be argued that the remote server resetting the connection after receiving a disconnect request is not an error, it doesn't change the fact that one is raised and we need to somehow deal with it.
|
|
1111
1100
|
|
|
1112
|
-
To handle this, `ssh2-sftp-client` implements a couple of strategies. Firstly, when you call one of the module's methods, it adds `error`, `end` and `close` event listeners which will call the `reject`
|
|
1101
|
+
To handle this, `ssh2-sftp-client` implements a couple of strategies. Firstly, when you call one of the module's methods, it adds `error`, `end` and `close` event listeners which will call the `reject` method on the enclosing promise. It also keeps track of whether an error has been handled and if it has, it ignores any subsequent errors until the promise ends. Typically, the first error caught has the most relevant information and any subsequent error events are less critical or informative, so ignoring them has no negative impact. Provided one of the events is raised before the promise is fulfilled, these handlers will consume the event and deal with it appropriately.
|
|
1113
1102
|
|
|
1114
1103
|
In testing, it was found that in some situations, particularly during connect operations, subsequent errors fired with a small delay. This prevents the errors from being handled by the event handlers associated with the connect promise. To deal with this, a small 500ms delay has been added to the connect() method, which effectively delays the removal of the event handlers until all events have been caught.
|
|
1115
1104
|
|
|
1116
|
-
The other area where additional events are fired is during the end() call. To deal with these events, the `end()` method
|
|
1105
|
+
The other area where additional events are fired is during the end() call. To deal with these events, the `end()` method sets up listeners which will simply ignore additional `error`, `end` and `close` events. It is assumed that once you have called `end()` you really only care about any main error which occurs and no longer care about other errors that may be raised as the connection is terminated.
|
|
1117
1106
|
|
|
1118
1107
|
In addition to the promise based event handlers, `ssh2-sftp-client` also implements global event handlers which will catch any `error`, `end` or `close` events. Essentially, these global handlers only reset the `sftp` property of the client object, effectively ensuring any subsequent calls are rejected and in the case of an error, send the error to the console.
|
|
1119
1108
|
|
|
1120
1109
|
|
|
1121
|
-
<a id="
|
|
1110
|
+
<a id="org8f7b2d0"></a>
|
|
1122
1111
|
|
|
1123
1112
|
### Adding Custom Handlers
|
|
1124
1113
|
|
|
1125
1114
|
While the above strategies appear to work for the majority of use cases, there are always going to be edge cases which require more flexible or powerful event handling. To support this, the `on()` and `removeListener()` methods are provided. Any event listener added using the `on()` method will be added at the beginning of the list of handlers for that event, ensuring it will be called before any global or promise local events. See the documentation for the `on()` method for details.
|
|
1126
1115
|
|
|
1127
1116
|
|
|
1128
|
-
<a id="
|
|
1117
|
+
<a id="orge15889c"></a>
|
|
1129
1118
|
|
|
1130
1119
|
## Windows Based Servers
|
|
1131
1120
|
|
|
@@ -1134,7 +1123,7 @@ It appears that when the sftp server is running on Windows, a *ECONNRESET* error
|
|
|
1134
1123
|
The best way to avoid this issue is to not re-use client objects. Always generate a new sftp client object for each new connection.
|
|
1135
1124
|
|
|
1136
1125
|
|
|
1137
|
-
<a id="
|
|
1126
|
+
<a id="orgb38b894"></a>
|
|
1138
1127
|
|
|
1139
1128
|
## Don't Re-use SftpClient Objects
|
|
1140
1129
|
|
|
@@ -1143,12 +1132,12 @@ Due to an issue with *ECONNRESET* error signals when connecting to Windows based
|
|
|
1143
1132
|
To avoid this problem, don't re-use SftpClient objects. Generate a new SftpClient object for each connection. You can perform multiple actions with a single connection e.g. upload multiple files, download multiple files etc, but after you have called end(), you should not try to re-use the object with a further connect() call. Create a new object instead.
|
|
1144
1133
|
|
|
1145
1134
|
|
|
1146
|
-
<a id="
|
|
1135
|
+
<a id="org1e76649"></a>
|
|
1147
1136
|
|
|
1148
1137
|
# FAQ
|
|
1149
1138
|
|
|
1150
1139
|
|
|
1151
|
-
<a id="
|
|
1140
|
+
<a id="orge3e4159"></a>
|
|
1152
1141
|
|
|
1153
1142
|
## Remote server drops connections with only an end event
|
|
1154
1143
|
|
|
@@ -1159,9 +1148,9 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
|
|
|
1159
1148
|
One way to avoid this type of issue is to add a delay between connection attempts. It does not need to be a very long delay - just sufficient to permit the previous connection to be authenticated. In fact, the default setting for openSSH is `10:30:60`, so you really just need to have enough delay to ensure that the 1st connection has completed authentication before the 11th connection is attempted.
|
|
1160
1149
|
|
|
1161
1150
|
|
|
1162
|
-
<a id="
|
|
1151
|
+
<a id="org77ed2d3"></a>
|
|
1163
1152
|
|
|
1164
|
-
## How can I pass
|
|
1153
|
+
## How can I pass writeable stream as dst for get method?
|
|
1165
1154
|
|
|
1166
1155
|
If the dst argument passed to the get method is a writeable stream, the remote file will be piped into that writeable. If the writeable you pass in is a writeable stream created with `fs.createWriteStream()`, the data will be written to the file specified in the constructor call to `createWriteStream()`.
|
|
1167
1156
|
|
|
@@ -1218,7 +1207,7 @@ sftp
|
|
|
1218
1207
|
```
|
|
1219
1208
|
|
|
1220
1209
|
|
|
1221
|
-
<a id="
|
|
1210
|
+
<a id="orgad09bd5"></a>
|
|
1222
1211
|
|
|
1223
1212
|
## How can I upload files without having to specify a password?
|
|
1224
1213
|
|
|
@@ -1253,7 +1242,7 @@ sftp.connect({
|
|
|
1253
1242
|
```
|
|
1254
1243
|
|
|
1255
1244
|
|
|
1256
|
-
<a id="
|
|
1245
|
+
<a id="org175d696"></a>
|
|
1257
1246
|
|
|
1258
1247
|
## How can I connect through a Socks Proxy
|
|
1259
1248
|
|
|
@@ -1289,16 +1278,16 @@ client.connect({
|
|
|
1289
1278
|
```
|
|
1290
1279
|
|
|
1291
1280
|
|
|
1292
|
-
<a id="
|
|
1281
|
+
<a id="orgd310a6a"></a>
|
|
1293
1282
|
|
|
1294
1283
|
## Timeout while waiting for handshake or handshake errors
|
|
1295
1284
|
|
|
1296
1285
|
Some users have encountered the error 'Timeout while waiting for handshake' or 'Handshake failed, no matching client->server ciphers. This is often due to the client not having the correct configuration for the transport layer algorithms used by ssh2. One of the connect options provided by the ssh2 module is `algorithm`, which is an object that allows you to explicitly set the key exchange, ciphers, hmac and compression algorithms as well as server host key used to establish the initial secure connection. See the SSH2 documentation for details. Getting these parameters correct usually resolves the issue.
|
|
1297
1286
|
|
|
1298
|
-
When encountering this type of problem, one worthwhile approach is to use openSSH's CLI sftp program with the `-v` switch to raise
|
|
1287
|
+
When encountering this type of problem, one worthwhile approach is to use openSSH's CLI sftp program with the `-v` switch to raise logging levels. This will show you what algorithms the CLI is using. You can then use this information to match the names with the accepted algorithm names documented in the `ssh2` README to set the properties in the `algorithms` object.
|
|
1299
1288
|
|
|
1300
1289
|
|
|
1301
|
-
<a id="
|
|
1290
|
+
<a id="org5a0faa2"></a>
|
|
1302
1291
|
|
|
1303
1292
|
## How can I limit upload/download speed
|
|
1304
1293
|
|
|
@@ -1338,7 +1327,7 @@ try {
|
|
|
1338
1327
|
```
|
|
1339
1328
|
|
|
1340
1329
|
|
|
1341
|
-
<a id="
|
|
1330
|
+
<a id="orgffe3a3c"></a>
|
|
1342
1331
|
|
|
1343
1332
|
## Connection hangs or fails for larger files
|
|
1344
1333
|
|
|
@@ -1349,14 +1338,21 @@ A symptom of this issue is that you are able to upload small files, but uploadin
|
|
|
1349
1338
|
For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
|
|
1350
1339
|
|
|
1351
1340
|
|
|
1352
|
-
<a id="
|
|
1341
|
+
<a id="org390645f"></a>
|
|
1342
|
+
|
|
1343
|
+
## Typescript definition file out of date
|
|
1344
|
+
|
|
1345
|
+
This project does not use Typescript. However, typescript definition files are provided by other 3rd parties. Sometimes, these definition files have not stayed up-to-date with the current version of this module. If you encounter this issue, you need to report it to the party responsible for the definition file, not this project.
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
<a id="orgce20a25"></a>
|
|
1353
1349
|
|
|
1354
1350
|
# Examples
|
|
1355
1351
|
|
|
1356
1352
|
I have started collecting example scripts in the example directory of the repository. These are mainly scripts I have put together in order to investigate issues or provide samples for users. They are not robust, lack adequate error handling and may contain errors. However, I think they are still useful for helping developers see how the module and API can be used.
|
|
1357
1353
|
|
|
1358
1354
|
|
|
1359
|
-
<a id="
|
|
1355
|
+
<a id="org31d0059"></a>
|
|
1360
1356
|
|
|
1361
1357
|
# Troubleshooting
|
|
1362
1358
|
|
|
@@ -1371,14 +1367,14 @@ Note also that in the repository there are two useful directories. The first is
|
|
|
1371
1367
|
The second directory is the validation directory. I have some very simple scripts in this directory which perform basic tasks using only the `ssh2` modules (no `ssh2-sftp-client` module). These can be useful when trying to determine if the issue is with the underlying `ssh2` module or the `ssh2-sftp-client` wrapper module.
|
|
1372
1368
|
|
|
1373
1369
|
|
|
1374
|
-
<a id="
|
|
1370
|
+
<a id="orgf1c2f29"></a>
|
|
1375
1371
|
|
|
1376
1372
|
## Common Errors
|
|
1377
1373
|
|
|
1378
|
-
There are some common errors people tend to make when using Promises or
|
|
1374
|
+
There are some common errors people tend to make when using Promises or Async/Await. These are by far the most common problem found in issues logged against this module. Please check for some of these before logging your issue.
|
|
1379
1375
|
|
|
1380
1376
|
|
|
1381
|
-
<a id="
|
|
1377
|
+
<a id="org14ab27b"></a>
|
|
1382
1378
|
|
|
1383
1379
|
### Not returning the promise in a `then()` block
|
|
1384
1380
|
|
|
@@ -1415,7 +1411,7 @@ Note the `return` statements. These ensure that the Promise returned by the clie
|
|
|
1415
1411
|
A common symptom of this type of error is for file uploads or download to fail to complete or for data in those files to be truncated. What is happening is that the connection is being ended before the transfer has completed.
|
|
1416
1412
|
|
|
1417
1413
|
|
|
1418
|
-
<a id="
|
|
1414
|
+
<a id="org6c257e3"></a>
|
|
1419
1415
|
|
|
1420
1416
|
### Mixing Promise Chains and Async/Await
|
|
1421
1417
|
|
|
@@ -1468,32 +1464,32 @@ async function doSftp() {
|
|
|
1468
1464
|
await sftp.fastGet(`${d}/foo.txt`, 'bat.txt');
|
|
1469
1465
|
} catch (e) {
|
|
1470
1466
|
console.error(e.message);
|
|
1471
|
-
} finally
|
|
1467
|
+
} finally {
|
|
1472
1468
|
await sftp.end();
|
|
1473
1469
|
}
|
|
1474
1470
|
}
|
|
1475
1471
|
```
|
|
1476
1472
|
|
|
1477
1473
|
|
|
1478
|
-
<a id="
|
|
1474
|
+
<a id="org765b1b2"></a>
|
|
1479
1475
|
|
|
1480
1476
|
### Try/catch and Error Handlers
|
|
1481
1477
|
|
|
1482
1478
|
Another common error is to try and use a try/catch block to catch event signals, such as an error event. In general, you cannot use try/catch blocks for asynchronous code and expect errors to be caught by the `catch` block. Handling errors in asynchronous code is one of the key reasons we now have the Promise and async/await frameworks.
|
|
1483
1479
|
|
|
1484
|
-
The basic problem is that the try/catch block will have completed execution before the asynchronous code has completed. If the asynchronous code has not
|
|
1480
|
+
The basic problem is that the try/catch block will have completed execution before the asynchronous code has completed. If the asynchronous code has not completed, then there is a potential for it to raise an error. However, as the try/catch block has already completed, there is no *catch* waiting to catch the error. It will bubble up and probably result in your script exiting with an uncaught exception error.
|
|
1485
1481
|
|
|
1486
1482
|
Error events are essentially asynchronous code. You don't know when such events will fire. Therefore, you cannot use a try/catch block to catch such event errors. Even creating an error handler which then throws an exception won't help as the key problem is that your try/catch block has already executed. There are a number of alternative ways to deal with this situation. However, the key symptom is that you see occasional uncaught error exceptions that cause your script to exit abnormally despite having try/catch blocks in your script. What you need to do is look at your code and find where errors are raised asynchronously and use an event handler or some other mechanism to manage any errors raised.
|
|
1487
1483
|
|
|
1488
1484
|
|
|
1489
|
-
<a id="
|
|
1485
|
+
<a id="orge67d910"></a>
|
|
1490
1486
|
|
|
1491
1487
|
### Server Differences
|
|
1492
1488
|
|
|
1493
1489
|
Not all SFTP servers are the same. Like most standards, the SFTP protocol has some level of interpretation and allows different levels of compliance. This means there can be differences in behaviour between different servers and code which works with one server will not work the same with another. For example, the value returned by *realpath* for non-existent objects can differ significantly. Some servers will throw an error for a particular operation while others will just return null, some servers support concurrent operations (such as used by fastGet/fastPut) while others will not and of course, the text of error messages can vary significantly. In particular, we have noticed significant differences across different platforms. It is therefore advisable to do comprehensive testing when the SFTP server is moved to a new platform. This includes moving from to a cloud based service even if the underlying platform remains the same. I have noticed that some cloud platforms can generate unexpected events, possibly related to additional functionality or features associated with the cloud implementation. For example, it appears SFTP servers running under Azure will generate an error event when the connection is closed even when the client has requested the connection be terminated. The same SFTP server running natively on Windows does not appear to exhibit such behaviour.
|
|
1494
1490
|
|
|
1495
1491
|
|
|
1496
|
-
<a id="
|
|
1492
|
+
<a id="org734662f"></a>
|
|
1497
1493
|
|
|
1498
1494
|
### Avoid Concurrent Operations
|
|
1499
1495
|
|
|
@@ -1502,7 +1498,7 @@ Technically, SFTP should be able to perform multiple operations concurrently. As
|
|
|
1502
1498
|
If you are going to try and perform concurrent operations, you need to test extensively and ensure you are using data which is large enough that context switching does occur (i.e. the request is not completed in a single run). Some SFTP servers will handle concurrent operations better than others.
|
|
1503
1499
|
|
|
1504
1500
|
|
|
1505
|
-
<a id="
|
|
1501
|
+
<a id="org658f3d6"></a>
|
|
1506
1502
|
|
|
1507
1503
|
## Debugging Support
|
|
1508
1504
|
|
|
@@ -1535,7 +1531,7 @@ If you just want to see debug messages from `ssh2-sftp-client` and exclude debug
|
|
|
1535
1531
|
```
|
|
1536
1532
|
|
|
1537
1533
|
|
|
1538
|
-
<a id="
|
|
1534
|
+
<a id="orgb0fbb63"></a>
|
|
1539
1535
|
|
|
1540
1536
|
# Logging Issues
|
|
1541
1537
|
|
|
@@ -1552,7 +1548,7 @@ I am happy to try and help diagnose and fix any issues you encounter while using
|
|
|
1552
1548
|
Perhaps the best assistance is a minimal reproducible example of the issue. Once the issue can be readily reproduced, it can usually be fixed very quickly.
|
|
1553
1549
|
|
|
1554
1550
|
|
|
1555
|
-
<a id="
|
|
1551
|
+
<a id="orgc22fdba"></a>
|
|
1556
1552
|
|
|
1557
1553
|
# Pull Requests
|
|
1558
1554
|
|
|
@@ -1563,11 +1559,13 @@ Pull requests are always welcomed. However, please ensure your changes pass all
|
|
|
1563
1559
|
This module will adopt a standard semantic versioning policy. Please indicate in your pull request what level of change it represents i.e.
|
|
1564
1560
|
|
|
1565
1561
|
- **Major:** Change to API or major change in functionality which will require an increase in major version number.
|
|
1562
|
+
|
|
1566
1563
|
- **Minor:** Minor change, enhancement or new feature which does not change existing API and will not break existing client code.
|
|
1564
|
+
|
|
1567
1565
|
- **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
|
|
1568
1566
|
|
|
1569
1567
|
|
|
1570
|
-
<a id="
|
|
1568
|
+
<a id="org7bb6675"></a>
|
|
1571
1569
|
|
|
1572
1570
|
# Contributors
|
|
1573
1571
|
|