ssh2-sftp-client 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/example/demo.js +75 -57
- package/package.json +1 -1
- package/src/index.js +9 -6
- package/test/index.js +36 -27
- package/testServer/mocha-rmdir/file1.md +1 -0
- package/test/test.js +0 -9
package/example/demo.js
CHANGED
|
@@ -1,62 +1,80 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
// .then((data) => sftp.list(BASIC_URL))
|
|
21
|
-
// .then(() => {
|
|
22
|
-
// let str = '<body>string html</body>';
|
|
23
|
-
// let filePath = '/Library/WebServer/Documents/nodejs/ssh2-sftp-client/test.html';
|
|
24
|
-
// let buffer = new Buffer('this is the bufffffer test');
|
|
25
|
-
// let stream = fs.createReadStream(filePath);
|
|
26
|
-
|
|
27
|
-
// // test ok.
|
|
28
|
-
// // return sftp.put(filePath, BASIC_URL + 'hello1.html');
|
|
29
|
-
// // test ok.
|
|
30
|
-
// // sftp.put(buffer, BASIC_URL + 'hello3.html');
|
|
31
|
-
// // test ok.
|
|
32
|
-
// // sftp.put(stream, BASIC_URL + 'h5.html');
|
|
33
|
-
// })
|
|
34
|
-
|
|
35
|
-
// .then(() => {
|
|
36
|
-
// return sftp.get(BASIC_URL + 'hello1.html');
|
|
37
|
-
// })
|
|
38
|
-
|
|
39
|
-
// .then(() => {
|
|
40
|
-
// return sftp.mkdir(BASIC_URL + 'qq1/tt/a1.html', true);
|
|
41
|
-
// })
|
|
42
|
-
|
|
43
|
-
// .then(() => {
|
|
44
|
-
// return sftp.delete(BASIC_URL + 'hello1.html');
|
|
45
|
-
// })
|
|
46
|
-
|
|
47
|
-
// .then(() => {
|
|
48
|
-
// return sftp.rename(BASIC_URL + 'h5.html', BASIC_URL + 'bb/ooo.html');
|
|
49
|
-
// })
|
|
50
|
-
|
|
51
|
-
.then(() => {
|
|
52
|
-
return sftp.rmdir(BASIC_URL + 'mocha-rmdir', true);
|
|
53
|
-
})
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const Client = require('../src/index.js');
|
|
5
|
+
const sftp = new Client();
|
|
6
|
+
const BASIC_URL = '/sftp-test/';
|
|
7
|
+
|
|
8
|
+
const config = {
|
|
9
|
+
host: '127.0.0.1',
|
|
10
|
+
port: '8080',
|
|
11
|
+
username: 'username',
|
|
12
|
+
password: '******'
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// get connect
|
|
16
|
+
const connect = () => {
|
|
17
|
+
sftp.connect(config);
|
|
18
|
+
};
|
|
54
19
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
20
|
+
// get list
|
|
21
|
+
const list = () => {
|
|
22
|
+
sftp.connect(config).then(() => {
|
|
23
|
+
return sftp.list(BASIC_URL);
|
|
24
|
+
}).then((data) => {
|
|
25
|
+
let body = data.on('data', (chunk) => {
|
|
26
|
+
body += chunk;
|
|
27
|
+
});
|
|
58
28
|
|
|
59
|
-
|
|
60
|
-
|
|
29
|
+
data.on('end', () => {
|
|
30
|
+
console.log(body)
|
|
31
|
+
});
|
|
61
32
|
})
|
|
62
|
-
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// get file
|
|
36
|
+
const get = () => {
|
|
37
|
+
sftp.connect(config).then(() => {
|
|
38
|
+
return sftp.get(BASIC_URL + 'a.js');
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// put file
|
|
43
|
+
const put1 = () => {
|
|
44
|
+
sftp.connect(config).then(() => {
|
|
45
|
+
return sftp.put(BASIC_URL + 'localpath.js', BASIC_URL + 'remotepath.js');
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
const put2 = () => {
|
|
49
|
+
sftp.connect(config).then(() => {
|
|
50
|
+
return sftp.put(new Buffer('hello'), BASIC_URL + 'file.js', true, 'utf-8');
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// mkdir
|
|
55
|
+
const mkdir = () => {
|
|
56
|
+
sftp.connect(config).then(() => {
|
|
57
|
+
return sftp.mkdir(BASIC_URL + 'change/log', true);
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// rmdir
|
|
62
|
+
const rmdir = () => {
|
|
63
|
+
sftp.connect(config).then(() => {
|
|
64
|
+
return sftp.rmdir(BASIC_URL + 'change/log', true);
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// delete
|
|
69
|
+
const deleteFile = () => {
|
|
70
|
+
sftp.connect(config).then(() => {
|
|
71
|
+
return sftp.delete(BASIC_URL + 'file.js');
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// rename
|
|
76
|
+
const rename = () => {
|
|
77
|
+
sftp.connect(config).then(() => {
|
|
78
|
+
return sftp.rename(BASIC_URL + 'source.js', BASIC_URL + 'remote.js');
|
|
79
|
+
});
|
|
80
|
+
}
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -70,9 +70,12 @@ SftpClient.prototype.get = function(path, useCompression, encoding) {
|
|
|
70
70
|
try {
|
|
71
71
|
let stream = sftp.createReadStream(path, options);
|
|
72
72
|
|
|
73
|
-
stream.on('error',
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
stream.on('error', (err) => {
|
|
74
|
+
reject(err);
|
|
75
|
+
});
|
|
76
|
+
stream.on('readable', () => {
|
|
77
|
+
resolve(stream);
|
|
78
|
+
});
|
|
76
79
|
} catch(err) {
|
|
77
80
|
reject(err);
|
|
78
81
|
}
|
|
@@ -294,12 +297,12 @@ SftpClient.prototype.chmod = function(remotePath, mode) {
|
|
|
294
297
|
});
|
|
295
298
|
}
|
|
296
299
|
|
|
297
|
-
SftpClient.prototype.connect = function(config) {
|
|
300
|
+
SftpClient.prototype.connect = function(config, connectMethod) {
|
|
301
|
+
connectMethod = connectMethod || 'on'
|
|
298
302
|
var c = this.client;
|
|
299
303
|
|
|
300
304
|
return new Promise((resolve, reject) => {
|
|
301
|
-
this.client
|
|
302
|
-
|
|
305
|
+
this.client[connectMethod]('ready', () => {
|
|
303
306
|
this.client.sftp((err, sftp) => {
|
|
304
307
|
if (err) {
|
|
305
308
|
reject(err);
|
package/test/index.js
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
const stream = require('stream');
|
|
2
|
+
const chai = require('chai');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const expect = chai.expect;
|
|
5
|
+
const chaiSubset = require('chai-subset');
|
|
6
|
+
const Client = require('../src/index.js');
|
|
7
|
+
const sftp = new Client();
|
|
8
|
+
|
|
9
|
+
// use your test ssh server config
|
|
10
|
+
const config = {
|
|
11
|
+
host: '172.29.84.8',
|
|
12
|
+
username: 'jyu213',
|
|
13
|
+
password: '**'
|
|
14
|
+
};
|
|
15
|
+
const BASIC_URL = path.resolve(__dirname, '../testServer/') + '/';
|
|
12
16
|
|
|
13
17
|
after(() => {
|
|
14
18
|
sftp.end();
|
|
@@ -18,7 +22,7 @@ describe('list', () => {
|
|
|
18
22
|
chai.use(chaiSubset);
|
|
19
23
|
|
|
20
24
|
before(() => {
|
|
21
|
-
return sftp.connect(config).then(() => {
|
|
25
|
+
return sftp.connect(config, 'once').then(() => {
|
|
22
26
|
return sftp.mkdir(BASIC_URL + 'mocha-list/dir1', true);
|
|
23
27
|
}).then(() => {
|
|
24
28
|
return sftp.mkdir(BASIC_URL + 'mocha-list/dir2/sub1', true);
|
|
@@ -29,7 +33,7 @@ describe('list', () => {
|
|
|
29
33
|
});
|
|
30
34
|
});
|
|
31
35
|
after(() => {
|
|
32
|
-
return sftp.connect(config).then(() => {
|
|
36
|
+
return sftp.connect(config, 'once').then(() => {
|
|
33
37
|
return sftp.rmdir(BASIC_URL + 'mocha-list', true)
|
|
34
38
|
.then(() => {
|
|
35
39
|
return sftp.end();
|
|
@@ -53,12 +57,12 @@ describe('list', () => {
|
|
|
53
57
|
|
|
54
58
|
describe('get', () => {
|
|
55
59
|
before(() => {
|
|
56
|
-
return sftp.connect(config).then(() => {
|
|
60
|
+
return sftp.connect(config, 'once').then(() => {
|
|
57
61
|
return sftp.put(new Buffer('hello'), BASIC_URL + 'mocha-file.md', true);
|
|
58
62
|
});
|
|
59
63
|
});
|
|
60
64
|
after(() => {
|
|
61
|
-
return sftp.connect(config).then(() => {
|
|
65
|
+
return sftp.connect(config, 'once').then(() => {
|
|
62
66
|
sftp.delete(BASIC_URL + 'mocha-file.md');
|
|
63
67
|
}).then(() => {
|
|
64
68
|
return sftp.end();
|
|
@@ -79,11 +83,16 @@ describe('get', () => {
|
|
|
79
83
|
});
|
|
80
84
|
});
|
|
81
85
|
});
|
|
86
|
+
it('get file faild', () => {
|
|
87
|
+
return sftp.get(BASIC_URL + 'mocha-file1.md').catch((err) => {
|
|
88
|
+
expect(err.message).to.equal('No such file');
|
|
89
|
+
});
|
|
90
|
+
});
|
|
82
91
|
});
|
|
83
92
|
|
|
84
93
|
describe('put', () => {
|
|
85
94
|
before(() => {
|
|
86
|
-
return sftp.connect(config);
|
|
95
|
+
return sftp.connect(config, 'once');
|
|
87
96
|
});
|
|
88
97
|
after(() => {
|
|
89
98
|
return sftp.delete(BASIC_URL + 'mocha-put-string.md').then(() => {
|
|
@@ -136,7 +145,7 @@ describe('mkdir', () => {
|
|
|
136
145
|
chai.use(chaiSubset);
|
|
137
146
|
|
|
138
147
|
before(() => {
|
|
139
|
-
return sftp.connect(config);
|
|
148
|
+
return sftp.connect(config, 'once');
|
|
140
149
|
});
|
|
141
150
|
after(() => {
|
|
142
151
|
return sftp.rmdir(BASIC_URL + 'mocha', true).then(() => {
|
|
@@ -167,10 +176,10 @@ describe('rmdir', () => {
|
|
|
167
176
|
chai.use(chaiSubset);
|
|
168
177
|
|
|
169
178
|
// beforeEach(() => {
|
|
170
|
-
// return sftp.connect(config);
|
|
179
|
+
// return sftp.connect(config, 'once');
|
|
171
180
|
// });
|
|
172
181
|
before(() => {
|
|
173
|
-
return sftp.connect(config).then(() => {
|
|
182
|
+
return sftp.connect(config, 'once').then(() => {
|
|
174
183
|
return sftp.mkdir(BASIC_URL + 'mocha-rmdir/dir1', true);
|
|
175
184
|
}).then(() => {
|
|
176
185
|
return sftp.mkdir(BASIC_URL + 'mocha-rmdir/dir2', true);
|
|
@@ -199,7 +208,7 @@ describe('rmdir', () => {
|
|
|
199
208
|
});
|
|
200
209
|
|
|
201
210
|
it('remove directory recursive', () => {
|
|
202
|
-
return sftp.connect(config).then(() => {
|
|
211
|
+
return sftp.connect(config, 'once').then(() => {
|
|
203
212
|
sftp.rmdir(BASIC_URL + 'mocha-rmdir', true).then(() => {
|
|
204
213
|
return sftp.list(BASIC_URL);
|
|
205
214
|
}).then((list) => {
|
|
@@ -213,7 +222,7 @@ describe('delete', () => {
|
|
|
213
222
|
chai.use(chaiSubset);
|
|
214
223
|
|
|
215
224
|
before(() => {
|
|
216
|
-
return sftp.connect(config).then(() => {
|
|
225
|
+
return sftp.connect(config, 'once').then(() => {
|
|
217
226
|
sftp.put(new Buffer('hello'), BASIC_URL + 'mocha-delete.md', true);
|
|
218
227
|
});
|
|
219
228
|
});
|
|
@@ -238,7 +247,7 @@ describe('rename', () => {
|
|
|
238
247
|
chai.use(chaiSubset);
|
|
239
248
|
|
|
240
249
|
before(() => {
|
|
241
|
-
return sftp.connect(config).then(() => {
|
|
250
|
+
return sftp.connect(config, 'once').then(() => {
|
|
242
251
|
return sftp.put(new Buffer('hello'), BASIC_URL + 'mocha-rename.md', true);
|
|
243
252
|
});
|
|
244
253
|
});
|
|
@@ -262,7 +271,7 @@ describe('rename', () => {
|
|
|
262
271
|
});
|
|
263
272
|
|
|
264
273
|
describe('getOptions', () => {
|
|
265
|
-
|
|
274
|
+
|
|
266
275
|
it('encoding should be utf8 if undefined', () => {
|
|
267
276
|
return expect(sftp.getOptions()).to.have.property('encoding', 'utf8')
|
|
268
277
|
});
|
|
@@ -270,15 +279,15 @@ describe('getOptions', () => {
|
|
|
270
279
|
it('encoding should be utf8 if undefined 1', () => {
|
|
271
280
|
return expect(sftp.getOptions(false)).to.have.property('encoding', 'utf8')
|
|
272
281
|
});
|
|
273
|
-
|
|
282
|
+
|
|
274
283
|
it('encoding should be utf8 if undefined 2', () => {
|
|
275
284
|
return expect(sftp.getOptions(false, undefined)).to.have.property('encoding', 'utf8')
|
|
276
285
|
});
|
|
277
|
-
|
|
286
|
+
|
|
278
287
|
it('encoding should be null if null', () => {
|
|
279
288
|
return expect(sftp.getOptions(false, null)).to.have.property('encoding', null)
|
|
280
289
|
});
|
|
281
|
-
|
|
290
|
+
|
|
282
291
|
it('encoding should be hex', () => {
|
|
283
292
|
return expect(sftp.getOptions(false, 'hex')).to.have.property('encoding', 'hex')
|
|
284
293
|
});
|
|
@@ -288,7 +297,7 @@ describe('chmod', () => {
|
|
|
288
297
|
chai.use(chaiSubset);
|
|
289
298
|
|
|
290
299
|
before(() => {
|
|
291
|
-
return sftp.connect(config).then(() => {
|
|
300
|
+
return sftp.connect(config, 'once').then(() => {
|
|
292
301
|
return sftp.put(new Buffer('hello'), BASIC_URL + 'mocha-chmod.txt', true);
|
|
293
302
|
});
|
|
294
303
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hello
|
package/test/test.js
DELETED