-
Notifications
You must be signed in to change notification settings - Fork 9
/
filetransfer.js
100 lines (89 loc) · 3.24 KB
/
filetransfer.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var WildEmitter = require('wildemitter');
var util = require('util');
function Sender(opts) {
WildEmitter.call(this);
var options = opts || {};
this.config = {
chunksize: 16384,
pacing: 0
};
// set our config from options
var item;
for (item in options) {
this.config[item] = options[item];
}
this.file = null;
this.channel = null;
}
util.inherits(Sender, WildEmitter);
Sender.prototype.send = function (file, channel) {
var self = this;
this.file = file;
this.channel = channel;
var usePoll = typeof channel.bufferedAmountLowThreshold !== 'number';
var offset = 0;
var sliceFile = function() {
var reader = new window.FileReader();
reader.onload = (function() {
return function(e) {
self.channel.send(e.target.result);
self.emit('progress', offset, file.size, e.target.result);
if (file.size > offset + e.target.result.byteLength) {
if (usePoll) {
window.setTimeout(sliceFile, self.config.pacing);
} else if (channel.bufferedAmount <= channel.bufferedAmountLowThreshold) {
window.setTimeout(sliceFile, 0);
} else {
// wait for bufferedAmountLow to fire
}
} else {
self.emit('progress', file.size, file.size, null);
self.emit('sentFile');
}
offset = offset + self.config.chunksize;
};
})(file);
var slice = file.slice(offset, offset + self.config.chunksize);
reader.readAsArrayBuffer(slice);
};
if (!usePoll) {
channel.bufferedAmountLowThreshold = 8 * this.config.chunksize;
channel.addEventListener('bufferedamountlow', sliceFile);
}
window.setTimeout(sliceFile, 0);
};
function Receiver() {
WildEmitter.call(this);
this.receiveBuffer = [];
this.received = 0;
this.metadata = {};
this.channel = null;
}
util.inherits(Receiver, WildEmitter);
Receiver.prototype.receive = function (metadata, channel) {
var self = this;
if (metadata) {
this.metadata = metadata;
}
this.channel = channel;
// chrome only supports arraybuffers and those make it easier to calc the hash
channel.binaryType = 'arraybuffer';
this.channel.onmessage = function (event) {
var len = event.data.byteLength;
self.received += len;
self.receiveBuffer.push(event.data);
self.emit('progress', self.received, self.metadata.size, event.data);
if (self.received === self.metadata.size) {
self.emit('receivedFile', new window.Blob(self.receiveBuffer), self.metadata);
self.receiveBuffer = []; // discard receivebuffer
} else if (self.received > self.metadata.size) {
// FIXME
console.error('received more than expected, discarding...');
self.receiveBuffer = []; // just discard...
}
};
};
module.exports = {};
module.exports.support = typeof window !== 'undefined' && window && window.File && window.FileReader && window.Blob;
module.exports.Sender = Sender;
module.exports.Receiver = Receiver;