-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
69 lines (65 loc) · 1.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const { debuglog } = require('util');
const Packet = require('./packet');
const Connection = require('./connection');
const debug = debuglog('tftp2');
/**
* [TFTP description]
* @param {[type]} options [description]
*/
const TFTP = (host, port, { BLOCK_SIZE = 512 } = {}) => {
const rinfo = { address: host, port };
const client = new Connection(rinfo);
const reset = () => client.setRemoteDescription(rinfo);
return {
close() {
return client.close();
},
/**
* read
* @param {*} filename
* @param {*} push
* @param {*} done
*/
async read(filename, push, done) {
let block = 0, rinfo, data;
await reset();
await client.sendRequest(Packet.OPCODE.RRQ, filename);
while (true) {
({ rinfo, block, data } = await client.waitBlock(block + 1));
debug('received block(%s) size(%s), from %s:%s', block, data.length, rinfo.address, rinfo.port);
await push(data);
await client.setRemoteDescription(rinfo);
await client.sendAck(block);
if (data.length < BLOCK_SIZE) {
done();
break;
}
}
},
/**
* write
* @param {*} filename
* @param {*} data
*/
async write(filename, data) {
let block, rinfo;
await reset();
await client.sendRequest(Packet.OPCODE.WRQ, filename);
const blocks = Math.floor(data.length / BLOCK_SIZE | 0) + 1;
for (var i = 0; i < blocks; i++) {
({ rinfo, block } = await client.waitAck(i));
await client.setRemoteDescription(rinfo);
debug('request block %s, from %s:%s', block, rinfo.address, rinfo.port);
const start = block * BLOCK_SIZE;
const end = Math.min(start + BLOCK_SIZE, data.length);
await client.sendBlock(block + 1, data.slice(start, end));
}
debug('done');
}
};
};
TFTP.Client = TFTP;
TFTP.Packet = Packet;
TFTP.Server = require('./server');
TFTP.createServer = () => new TFTP.Server();
module.exports = TFTP;