-
Notifications
You must be signed in to change notification settings - Fork 378
/
Copy pathdownloader.js
219 lines (195 loc) · 6.12 KB
/
downloader.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
self.importScripts("common.js");
function Downloader() {
this.logger = new Logger("Downloader");
this.ws = null;
}
Downloader.prototype.appendBuffer = function (buffer1, buffer2) {
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
};
Downloader.prototype.reportFileSize = function (sz, st) {
var objData = {
t: kGetFileInfoRsp,
i: {
sz: sz,
st: st
}
};
//this.logger.logInfo("File size " + sz + " bytes.");
self.postMessage(objData);
};
Downloader.prototype.reportData = function (start, end, seq, data) {
var objData = {
t: kFileData,
s: start,
e: end,
d: data,
q: seq
};
self.postMessage(objData, [objData.d]);
};
// Http implement.
Downloader.prototype.getFileInfoByHttp = function (url) {
this.logger.logInfo("Getting file size " + url + ".");
var size = 0;
var status = 0;
var reported = false;
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
var self = this;
xhr.onreadystatechange = () => {
var len = xhr.getResponseHeader("Content-Length");
if (len) {
size = len;
}
if (xhr.status) {
status = xhr.status;
}
//Completed.
if (!reported && ((size > 0 && status > 0) || xhr.readyState == 4)) {
self.reportFileSize(size, status);
reported = true;
xhr.abort();
}
};
xhr.send();
};
Downloader.prototype.downloadFileByHttp = function (url, start, end, seq) {
//this.logger.logInfo("Downloading file " + url + ", bytes=" + start + "-" + end + ".");
var xhr = new XMLHttpRequest;
xhr.open('get', url, true);
xhr.responseType = 'arraybuffer';
xhr.setRequestHeader("Range", "bytes=" + start + "-" + end);
var self = this;
xhr.onload = function () {
self.reportData(start, end, seq, xhr.response);
};
xhr.send();
};
// Websocket implement, NOTICE MUST call requestWebsocket serially, MUST wait
// for result of last websocket request(cb called) for there's only one stream
// exists.
Downloader.prototype.requestWebsocket = function (url, msg, cb) {
if (this.ws == null) {
this.ws = new WebSocket(url);
this.ws.binaryType = 'arraybuffer';
var self = this;
this.ws.onopen = function(evt) {
self.logger.logInfo("Ws connected.");
self.ws.send(msg);
};
this.ws.onerror = function(evt) {
self.logger.logError("Ws connect error " + evt.data);
}
this.ws.onmessage = cb.onmessage;
} else {
this.ws.onmessage = cb.onmessage;
this.ws.send(msg);
}
};
Downloader.prototype.getFileInfoByWebsocket = function (url) {
//this.logger.logInfo("Getting file size " + url + ".");
// TBD, consider tcp sticky package.
var data = null;
var expectLength = 4;
var self = this;
var cmd = {
url : url,
cmd : "size",
};
this.requestWebsocket(url, JSON.stringify(cmd), {
onmessage : function(evt) {
if (data != null) {
data = self.appendBuffer(data, evt.data);
} else if (evt.data.byteLength < expectLength) {
data = evt.data.slice(0);
} else {
data = evt.data;
}
// Assume 4 bytes header as file size.
if (data.byteLength == expectLength) {
let int32array = new Int32Array(data, 0, 1);
let size = int32array[0];
self.reportFileSize(size, 200);
//self.logger.logInfo("Got file size " + self.fileSize + ".");
}
}
});
};
Downloader.prototype.downloadFileByWebsocket = function (url, start, end, seq) {
//this.logger.logInfo("Downloading file " + url + ", bytes=" + start + "-" + end + ".");
var data = null;
var expectLength = end - start + 1;
var self = this;
var cmd = {
url : url,
cmd : "data",
start : start,
end : end
};
this.requestWebsocket(url, JSON.stringify(cmd), {
onmessage : function(evt) {
if (data != null) {
data = self.appendBuffer(data, evt.data);
} else if (evt.data.byteLength < expectLength) {
data = evt.data.slice(0);
} else {
data = evt.data;
}
// Wait for expect data length.
if (data.byteLength == expectLength) {
self.reportData(start, end, seq, data);
}
}
});
};
// Interface.
Downloader.prototype.getFileInfo = function (proto, url) {
switch (proto) {
case kProtoHttp:
this.getFileInfoByHttp(url);
break;
case kProtoWebsocket:
this.getFileInfoByWebsocket(url);
break;
default:
this.logger.logError("Invalid protocol " + proto);
break;
}
};
Downloader.prototype.downloadFile = function (proto, url, start, end, seq) {
switch (proto) {
case kProtoHttp:
this.downloadFileByHttp(url, start, end, seq);
break;
case kProtoWebsocket:
this.downloadFileByWebsocket(url, start, end, seq);
break;
default:
this.logger.logError("Invalid protocol " + proto);
break;
}
}
self.downloader = new Downloader();
self.onmessage = function (evt) {
if (!self.downloader) {
console.log("[ER] Downloader not initialized!");
return;
}
var objData = evt.data;
switch (objData.t) {
case kGetFileInfoReq:
self.downloader.getFileInfo(objData.p, objData.u);
break;
case kDownloadFileReq:
self.downloader.downloadFile(objData.p, objData.u, objData.s, objData.e, objData.q);
break;
case kCloseDownloaderReq:
//Nothing to do.
break;
default:
self.downloader.logger.logError("Unsupport messsage " + objData.t);
}
};