-
Notifications
You must be signed in to change notification settings - Fork 9
/
hashed.js
71 lines (64 loc) · 1.98 KB
/
hashed.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
70
71
var WildEmitter = require('wildemitter');
var util = require('util');
var hashes = require('iana-hashes');
var base = require('./filetransfer');
// drop-in replacement for filetransfer which also calculates hashes
function Sender(opts) {
WildEmitter.call(this);
var self = this;
this.base = new base.Sender(opts);
var options = opts || {};
if (!options.hash) {
options.hash = 'sha-1';
}
this.hash = hashes.createHash(options.hash);
this.base.on('progress', function (start, size, data) {
self.emit('progress', start, size, data);
if (data) {
self.hash.update(new Uint8Array(data));
}
});
this.base.on('sentFile', function () {
self.emit('sentFile', {hash: self.hash.digest('hex'), algo: options.hash });
});
}
util.inherits(Sender, WildEmitter);
Sender.prototype.send = function () {
this.base.send.apply(this.base, arguments);
};
function Receiver(opts) {
WildEmitter.call(this);
var self = this;
this.base = new base.Receiver(opts);
var options = opts || {};
if (!options.hash) {
options.hash = 'sha-1';
}
this.hash = hashes.createHash(options.hash);
this.base.on('progress', function (start, size, data) {
self.emit('progress', start, size, data);
if (data) {
self.hash.update(new Uint8Array(data));
}
});
this.base.on('receivedFile', function (file, metadata) {
metadata.actualhash = self.hash.digest('hex');
self.emit('receivedFile', file, metadata);
});
}
util.inherits(Receiver, WildEmitter);
Receiver.prototype.receive = function () {
this.base.receive.apply(this.base, arguments);
};
Object.defineProperty(Receiver.prototype, 'metadata', {
get: function () {
return this.base.metadata;
},
set: function (value) {
this.base.metadata = value;
}
});
module.exports = {};
module.exports.support = base.support;
module.exports.Sender = Sender;
module.exports.Receiver = Receiver;