ssh2-sftp-client 2.4.3 → 2.5.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.
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ const {join} = require('path');
4
+ const fs = require('fs');
5
+
6
+ function checksumCleanup(client, sftpUrl, localUrl) {
7
+ return client
8
+ .delete(join(sftpUrl, 'checksum-file1.txt'))
9
+ .then(() => {
10
+ return client.delete(join(sftpUrl, 'checksum-file2.txt.gz'));
11
+ })
12
+ .then(() => {
13
+ fs.unlinkSync(join(localUrl, 'checksum-file1.txt'));
14
+ fs.unlinkSync(join(localUrl, 'checksum-file2.txt.gz'));
15
+ return true;
16
+ })
17
+ .catch(err => {
18
+ throw new Error(err.message);
19
+ });
20
+ }
21
+
22
+ module.exports = {
23
+ checksumCleanup
24
+ };
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ const {join} = require('path');
4
+
5
+ function chmodSetup(client, sftpUrl) {
6
+ return client
7
+ .put(Buffer.from('hello'), join(sftpUrl, 'mocha-chmod.txt'), {
8
+ encoding: 'utf8'
9
+ })
10
+ .catch(err => {
11
+ throw new Error(`Chmod setup hook error: ${err.message}`);
12
+ });
13
+ }
14
+
15
+ function chmodCleanup(client, sftpUrl) {
16
+ return client.delete(join(sftpUrl, 'mocha-chmod.txt')).catch(err => {
17
+ throw new Error(`Chmod cleanup hook error: ${err.message}`);
18
+ });
19
+ }
20
+
21
+ module.exports = {
22
+ chmodSetup,
23
+ chmodCleanup
24
+ };
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+
3
+ const {join} = require('path');
4
+
5
+ function deleteSetup(client, sftpUrl) {
6
+ return client
7
+ .put(Buffer.from('hello'), join(sftpUrl, 'mocha-delete.md'), {
8
+ encoding: 'utf8'
9
+ })
10
+ .then(() => {
11
+ return client.put(
12
+ Buffer.from('promise'),
13
+ join(sftpUrl, 'mocha-delete-promise.md'),
14
+ {encoding: 'utf8'}
15
+ );
16
+ })
17
+ .then(() => {
18
+ return true;
19
+ })
20
+ .catch(err => {
21
+ throw new Error(`Delete setup hook error: ${err.message}`);
22
+ });
23
+ }
24
+
25
+ module.exports = {
26
+ deleteSetup
27
+ };
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ const {join} = require('path');
4
+
5
+ function existSetup(client, sftpUrl, localUrl) {
6
+ return client
7
+ .mkdir(join(sftpUrl, 'exist-dir'))
8
+ .then(() => {
9
+ return client.fastPut(
10
+ join(localUrl, 'test-file1.txt'),
11
+ join(sftpUrl, 'exist-file.txt')
12
+ );
13
+ })
14
+ .then(() => {
15
+ return true;
16
+ })
17
+ .catch(err => {
18
+ throw new Error(`Exist method test setup hook failure: ${err.message}`);
19
+ });
20
+ }
21
+
22
+ function existCleanup(client, sftpUrl) {
23
+ return client
24
+ .delete(join(sftpUrl, 'exist-file.txt'))
25
+ .then(() => {
26
+ return client.rmdir(join(sftpUrl, 'exist-dir'));
27
+ })
28
+ .then(() => {
29
+ return true;
30
+ })
31
+ .catch(err => {
32
+ throw new Error(`Exist method test clenaup hook failed: ${err.message}`);
33
+ });
34
+ }
35
+
36
+ module.exports = {
37
+ existSetup,
38
+ existCleanup
39
+ };
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+
3
+ const {join} = require('path');
4
+ const fs = require('fs');
5
+
6
+ function fastGetSetup(client, sftpUrl, localUrl) {
7
+ return client
8
+ .put(Buffer.from('fast get'), join(sftpUrl, 'mocha-fastget1.md'), {
9
+ encoding: 'utf8'
10
+ })
11
+ .then(() => {
12
+ return client.fastPut(
13
+ join(localUrl, 'test-file1.txt'),
14
+ join(sftpUrl, 'mocha-fastget2.txt'),
15
+ {encoding: 'utf8'}
16
+ );
17
+ })
18
+ .then(() => {
19
+ return client.fastPut(
20
+ join(localUrl, 'test-file2.txt.gz'),
21
+ join(sftpUrl, 'mocha-fastget3.txt.gz'),
22
+ {encoding: null}
23
+ );
24
+ })
25
+ .then(() => {
26
+ return fs.mkdirSync(join(localUrl, 'fastGet'));
27
+ })
28
+ .then(() => {
29
+ return true;
30
+ })
31
+ .catch(err => {
32
+ throw new Error(`FastGet test setup error: ${err.message}`);
33
+ });
34
+ }
35
+
36
+ function fastGetCleanup(client, sftpUrl, localUrl) {
37
+ let localDir = join(localUrl, 'fastGet');
38
+ return client
39
+ .delete(join(sftpUrl, 'mocha-fastget1.md'))
40
+ .then(() => {
41
+ return client.delete(join(sftpUrl, 'mocha-fastget2.txt'));
42
+ })
43
+ .then(() => {
44
+ return client.delete(join(sftpUrl, 'mocha-fastget3.txt.gz'));
45
+ })
46
+ .then(() => {
47
+ fs.unlinkSync(join(localDir, 'local1.md'));
48
+ fs.unlinkSync(join(localDir, 'local2.txt'));
49
+ fs.unlinkSync(join(localDir, 'local3.txt.gz'));
50
+ return fs.rmdirSync(localDir);
51
+ })
52
+ .then(() => {
53
+ return true;
54
+ })
55
+ .catch(err => {
56
+ throw new Error(`FastGet test cleanup hook error: ${err.message}`);
57
+ });
58
+ }
59
+
60
+ module.exports = {
61
+ fastGetSetup,
62
+ fastGetCleanup
63
+ };
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ const {join} = require('path');
4
+
5
+ function fastPutSetup(client, sftpUrl) {
6
+ return client
7
+ .put(Buffer.from('fast put'), join(sftpUrl, 'mocha-fastput.md'))
8
+ .then(() => {
9
+ return true;
10
+ })
11
+ .catch(err => {
12
+ throw new Error(`FastPut setup hook error: ${err.message}`);
13
+ });
14
+ }
15
+
16
+ function fastPutCleanup(client, sftpUrl) {
17
+ return client
18
+ .delete(join(sftpUrl, 'remote2.md.gz'))
19
+ .then(() => {
20
+ return client.delete(join(sftpUrl, 'remote.md'));
21
+ })
22
+ .then(() => {
23
+ return client.delete(join(sftpUrl, 'mocha-fastput.md'));
24
+ })
25
+ .catch(err => {
26
+ throw new Error(`FastPut cleanup hook error: ${err.message}`);
27
+ });
28
+ }
29
+
30
+ module.exports = {
31
+ fastPutSetup,
32
+ fastPutCleanup
33
+ };
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ const {join} = require('path');
4
+ const fs = require('fs');
5
+
6
+ function getSetup(client, sftpUrl, localUrl) {
7
+ return client
8
+ .put(Buffer.from('hello'), join(sftpUrl, 'mocha-file.md'), {
9
+ encoding: 'utf8'
10
+ })
11
+ .then(() => {
12
+ return client.fastPut(
13
+ join(localUrl, 'test-file1.txt'),
14
+ join(sftpUrl, 'large-file1.txt'),
15
+ {encoding: 'utf8'}
16
+ );
17
+ })
18
+ .then(() => {
19
+ return client.fastPut(
20
+ join(localUrl, 'test-file2.txt.gz'),
21
+ join(sftpUrl, 'gzipped-file.txt.gz')
22
+ );
23
+ })
24
+ .catch(err => {
25
+ throw new Error(`Get setup hook error: ${err.message}`);
26
+ });
27
+ }
28
+
29
+ function getCleanup(client, sftpUrl, localUrl) {
30
+ return client
31
+ .delete(join(sftpUrl, 'mocha-file.md'))
32
+ .then(() => {
33
+ return client.delete(join(sftpUrl, 'large-file1.txt'));
34
+ })
35
+ .then(() => {
36
+ return client.delete(join(sftpUrl, 'gzipped-file.txt.gz'));
37
+ })
38
+ .then(() => {
39
+ fs.unlinkSync(join(localUrl, 'local-large-file.txt'));
40
+ fs.unlinkSync(join(localUrl, 'local-gizipped-file.txt.gz'));
41
+ fs.unlinkSync(join(localUrl, 'local-gzipped-file.txt'));
42
+ return true;
43
+ })
44
+ .catch(err => {
45
+ throw new Error(`Get cleanup hook error: ${err.message}`);
46
+ });
47
+ }
48
+
49
+ module.exports = {
50
+ getSetup,
51
+ getCleanup
52
+ };
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ const dotenvPath = __dirname + '/../../.env';
4
+
5
+ require('dotenv').config({path: dotenvPath});
6
+
7
+ const Client = require('../../src/index.js');
8
+ const {join} = require('path');
9
+
10
+ // use your test ssh server config
11
+ const config = {
12
+ host: process.env['SFTP_SERVER'],
13
+ username: process.env['SFTP_USER'],
14
+ password: process.env['SFTP_PASSWORD'],
15
+ port: process.env['SFTP_PORT'] || 22
16
+ };
17
+
18
+ const testEnv = {
19
+ sftp: new Client(),
20
+ hookSftp: new Client(),
21
+ localUrl: join(__dirname, '../testData'),
22
+ sftpUrl: process.env['SFTP_URL']
23
+ };
24
+
25
+ let initialised = false;
26
+
27
+ function setup() {
28
+ if (!initialised) {
29
+ return testEnv.sftp
30
+ .connect(config, 'once')
31
+ .then(() => {
32
+ return testEnv.hookSftp.connect(config, 'once');
33
+ })
34
+ .then(() => {
35
+ initialised = true;
36
+ return testEnv;
37
+ })
38
+ .catch(err => {
39
+ throw new Error(`Global test setup failed: ${err.message}`);
40
+ });
41
+ }
42
+ return Promise.resolve(testEnv);
43
+ }
44
+
45
+ function closeDown() {
46
+ if (initialised) {
47
+ return testEnv.sftp
48
+ .end()
49
+ .then(() => {
50
+ return testEnv.hookSftp.end();
51
+ })
52
+ .then(() => {
53
+ initialised = false;
54
+ return true;
55
+ })
56
+ .catch(err => {
57
+ throw new Error(`Global test cleanup failed: ${err.message}`);
58
+ });
59
+ }
60
+ return Promise.resolve(true);
61
+ }
62
+
63
+ module.exports = {
64
+ setup,
65
+ closeDown
66
+ };
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+
3
+ const {join} = require('path');
4
+
5
+ function listSetup(client, sftpUrl, localUrl) {
6
+ return client
7
+ .mkdir(join(sftpUrl, 'mocha-list/dir1'), true)
8
+ .then(() => {
9
+ return client.mkdir(join(sftpUrl, 'mocha-list/dir2/sub1'), true);
10
+ })
11
+ .then(() => {
12
+ return client.mkdir(join(sftpUrl, 'mocha-list/empty'), true);
13
+ })
14
+ .then(() => {
15
+ return client.put(
16
+ Buffer.from('hello file1'),
17
+ join(sftpUrl, 'mocha-list/file1.html'),
18
+ {encoding: 'utf8'}
19
+ );
20
+ })
21
+ .then(() => {
22
+ return client.put(
23
+ Buffer.from('hello file2'),
24
+ join(sftpUrl, 'mocha-list/file2.md'),
25
+ {encoding: 'utf8'}
26
+ );
27
+ })
28
+ .then(() => {
29
+ return client.fastPut(
30
+ join(localUrl, 'test-file1.txt'),
31
+ join(sftpUrl, 'mocha-list/test-file1.txt'),
32
+ {encoding: 'utf8'}
33
+ );
34
+ })
35
+ .then(() => {
36
+ return client.fastPut(
37
+ join(localUrl, 'test-file2.txt.gz'),
38
+ join(sftpUrl, 'mocha-list/test-file2.txt.gz')
39
+ );
40
+ })
41
+ .then(() => {
42
+ return true;
43
+ })
44
+ .catch(err => {
45
+ throw new Error(`List method setup hook error: ${err.message}`);
46
+ });
47
+ }
48
+
49
+ function listCleanup(client, sftpUrl) {
50
+ return client
51
+ .rmdir(join(sftpUrl, 'mocha-list'), true)
52
+ .then(() => {
53
+ return true;
54
+ })
55
+ .catch(err => {
56
+ throw new Error(`List method cleanup error: ${err.message}`);
57
+ });
58
+ }
59
+
60
+ module.exports = {
61
+ listSetup,
62
+ listCleanup
63
+ };
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ const {join} = require('path');
4
+
5
+ function mkdirCleanup(client, sftpUrl) {
6
+ return client
7
+ .rmdir(join(sftpUrl, 'mocha'), true)
8
+ .then(() => {
9
+ return true;
10
+ })
11
+ .catch(err => {
12
+ throw new Error(`Mkdir test cleanup hook failure: ${err.message}`);
13
+ });
14
+ }
15
+
16
+ module.exports = {
17
+ mkdirCleanup
18
+ };
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ const {join} = require('path');
4
+
5
+ function putCleanup(client, sftpUrl) {
6
+ return client
7
+ .delete(join(sftpUrl, 'mocha-put-string.md'))
8
+ .then(() => {
9
+ return client.delete(join(sftpUrl, 'mocha-put-buffer.md'));
10
+ })
11
+ .then(() => {
12
+ return client.delete(join(sftpUrl, 'mocha-put-stream.md'));
13
+ })
14
+ .then(() => {
15
+ return true;
16
+ })
17
+ .catch(err => {
18
+ throw new Error(`Put cleanup hook error: ${err.message}`);
19
+ });
20
+ }
21
+
22
+ module.exports = {
23
+ putCleanup
24
+ };
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ const {join} = require('path');
4
+
5
+ function renameSetup(client, sftpUrl) {
6
+ return client
7
+ .put(Buffer.from('hello'), join(sftpUrl, 'mocha-rename.md'), {
8
+ encoding: 'utf8'
9
+ })
10
+ .then(() => {
11
+ return client.put(
12
+ Buffer.from('conflict file'),
13
+ join(sftpUrl, 'mocha-conflict.md'),
14
+ {encoding: 'utf8'}
15
+ );
16
+ })
17
+ .catch(err => {
18
+ throw new Error(`Rename setup hook error: ${err.message}`);
19
+ });
20
+ }
21
+
22
+ function renameCleanup(client, sftpUrl) {
23
+ return client
24
+ .delete(join(sftpUrl, 'mocha-rename-new.md'))
25
+ .then(() => {
26
+ return client.delete(join(sftpUrl, 'mocha-conflict.md'));
27
+ })
28
+ .catch(err => {
29
+ throw new Error(`Rename cleanup hook error: ${err.message}`);
30
+ });
31
+ }
32
+
33
+ module.exports = {
34
+ renameSetup,
35
+ renameCleanup
36
+ };
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ const {join} = require('path');
4
+
5
+ function rmdirSetup(client, sftpUrl) {
6
+ return client
7
+ .mkdir(join(sftpUrl, 'mocha'))
8
+ .then(() => {
9
+ return client.mkdir(join(sftpUrl, 'mocha-rmdir/dir1'), true);
10
+ })
11
+ .then(() => {
12
+ return client.mkdir(join(sftpUrl, 'mocha-rmdir/dir2'), true);
13
+ })
14
+ .then(() => {
15
+ return client.mkdir(join(sftpUrl, 'mocha-rmdir/dir3/subdir'), true);
16
+ })
17
+ .then(() => {
18
+ return client.put(
19
+ Buffer.from('hello'),
20
+ join(sftpUrl, 'mocha-rmdir/file1.md'),
21
+ {encoding: 'utf8'}
22
+ );
23
+ })
24
+ .then(() => {
25
+ return true;
26
+ })
27
+ .catch(err => {
28
+ throw new Error(`Rmdir setup hook error: ${err.message}`);
29
+ });
30
+ }
31
+
32
+ module.exports = {
33
+ rmdirSetup
34
+ };
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ const {join} = require('path');
4
+
5
+ function statSetup(client, sftpUrl) {
6
+ return client
7
+ .put(Buffer.from('hello'), join(sftpUrl, 'mocha-stat.md'), {
8
+ encoding: 'utf8',
9
+ mode: 0o777
10
+ })
11
+ .then(() => {
12
+ return true;
13
+ })
14
+ .catch(err => {
15
+ throw new Error(`Stat test setup hook failure: ${err.message}`);
16
+ });
17
+ }
18
+
19
+ function statCleanup(client, sftpUrl) {
20
+ return client
21
+ .delete(join(sftpUrl, 'mocha-stat.md'))
22
+ .then(() => {
23
+ return true;
24
+ })
25
+ .catch(err => {
26
+ throw new Error(`Stat test cleanup hook failed: ${err.message}`);
27
+ });
28
+ }
29
+
30
+ module.exports = {
31
+ statSetup,
32
+ statCleanup
33
+ };
package/test/mocha.opts CHANGED
@@ -1 +1 @@
1
- -t 5000
1
+ -t 0
package/test/put.js ADDED
@@ -0,0 +1,114 @@
1
+ 'use strict';
2
+
3
+ const chai = require('chai');
4
+ const expect = chai.expect;
5
+ const chaiSubset = require('chai-subset');
6
+ const chaiAsPromised = require('chai-as-promised');
7
+ const {join} = require('path');
8
+ const stream = require('stream');
9
+ const {setup, closeDown} = require('./hooks/global-hooks');
10
+ const pHooks = require('./hooks/put-hooks');
11
+
12
+ chai.use(chaiSubset);
13
+ chai.use(chaiAsPromised);
14
+
15
+ let hookSftp, sftp, sftpUrl, localUrl;
16
+
17
+ before('Global setup', function() {
18
+ return setup()
19
+ .then(testEnv => {
20
+ hookSftp = testEnv.hookSftp;
21
+ sftp = testEnv.sftp;
22
+ sftpUrl = testEnv.sftpUrl;
23
+ localUrl = testEnv.localUrl;
24
+ return true;
25
+ })
26
+ .catch(err => {
27
+ throw new Error(err.message);
28
+ });
29
+ });
30
+
31
+ after('Global shutdown', function() {
32
+ return closeDown()
33
+ .then(() => {
34
+ return true;
35
+ })
36
+ .catch(err => {
37
+ throw new Error(err.message);
38
+ });
39
+ });
40
+
41
+ describe('Put method tests', function() {
42
+ after('Put cleanup hook', function() {
43
+ return pHooks.putCleanup(hookSftp, sftpUrl).catch(err => {
44
+ throw new Error(err.message);
45
+ });
46
+ });
47
+
48
+ it('Put should return a promise', function() {
49
+ return expect(
50
+ sftp.put(Buffer.from('blah'), join(sftpUrl, 'mocha-put-buffer.md'))
51
+ ).to.be.a('promise');
52
+ });
53
+
54
+ it('Put large text file', function() {
55
+ return sftp
56
+ .put(
57
+ join(localUrl, 'test-file1.txt'),
58
+ join(sftpUrl, 'mocha-put-string.md')
59
+ )
60
+ .then(() => {
61
+ return sftp.stat(join(sftpUrl, 'mocha-put-string.md'));
62
+ })
63
+ .then(stats => {
64
+ return expect(stats).to.containSubset({size: 6973257});
65
+ });
66
+ });
67
+
68
+ it('Put data from buffer into remote file', function() {
69
+ return sftp
70
+ .put(Buffer.from('hello'), join(sftpUrl, 'mocha-put-buffer.md'), {
71
+ encoding: 'utf8'
72
+ })
73
+ .then(() => {
74
+ return sftp.stat(join(sftpUrl, 'mocha-put-buffer.md'));
75
+ })
76
+ .then(stats => {
77
+ return expect(stats).to.containSubset({size: 5});
78
+ });
79
+ });
80
+
81
+ it('Put data from stream into remote file', function() {
82
+ var str2 = new stream.Readable();
83
+ str2._read = function noop() {};
84
+ str2.push('your text here');
85
+ str2.push(null);
86
+
87
+ return sftp
88
+ .put(str2, join(sftpUrl, 'mocha-put-stream.md'), {encoding: 'utf8'})
89
+ .then(() => {
90
+ return sftp.stat(join(sftpUrl, 'mocha-put-stream.md'));
91
+ })
92
+ .then(stats => {
93
+ return expect(stats).to.containSubset({size: 14});
94
+ });
95
+ });
96
+
97
+ it('Put with no src file should be rejected', function() {
98
+ return expect(
99
+ sftp.put(
100
+ join(localUrl, 'no-such-file.txt'),
101
+ join(sftpUrl, 'mocha-put-no-file.txt')
102
+ )
103
+ ).to.be.rejectedWith('Failed to upload');
104
+ });
105
+
106
+ it('Put with bad dst path should be rejected', function() {
107
+ return expect(
108
+ sftp.put(
109
+ join(localUrl, 'test-file1.txt'),
110
+ join(sftpUrl, 'bad-directory', 'bad-file.txt')
111
+ )
112
+ ).to.be.rejectedWith('Failed to upload');
113
+ });
114
+ });