forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xhr-loader.js
130 lines (116 loc) · 3.52 KB
/
xhr-loader.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
/**
* XHR based logger
*/
import {logger} from '../utils/logger';
class XhrLoader {
constructor(config) {
if (config && config.xhrSetup) {
this.xhrSetup = config.xhrSetup;
}
}
destroy() {
this.abort();
this.loader = null;
}
abort() {
var loader = this.loader,
timeoutHandle = this.timeoutHandle;
if (loader && loader.readyState !== 4) {
this.stats.aborted = true;
loader.abort();
}
if (timeoutHandle) {
window.clearTimeout(timeoutHandle);
}
}
load(url, context, responseType, onSuccess, onError, onTimeout, timeout, maxRetry, retryDelay, onProgress = null, frag = null) {
this.url = url;
this.context = context;
if (context) {
context.url = url;
}
if (frag && !isNaN(frag.byteRangeStartOffset) && !isNaN(frag.byteRangeEndOffset)) {
this.byteRange = frag.byteRangeStartOffset + '-' + (frag.byteRangeEndOffset-1);
}
this.responseType = responseType;
this.onSuccess = onSuccess;
this.onProgress = onProgress;
this.onTimeout = onTimeout;
this.onError = onError;
this.stats = {trequest: performance.now(), retry: 0};
this.timeout = timeout;
this.maxRetry = maxRetry;
this.retryDelay = retryDelay;
this.loadInternal();
}
loadInternal() {
var xhr;
if (typeof XDomainRequest !== 'undefined') {
xhr = this.loader = new XDomainRequest();
} else {
xhr = this.loader = new XMLHttpRequest();
}
xhr.onloadend = this.loadend.bind(this);
xhr.onprogress = this.loadprogress.bind(this);
xhr.open('GET', this.url, true);
if (this.byteRange) {
xhr.setRequestHeader('Range', 'bytes=' + this.byteRange);
}
xhr.responseType = this.responseType;
let stats = this.stats;
stats.tfirst = 0;
stats.loaded = 0;
if (this.xhrSetup) {
this.xhrSetup(xhr, this.url);
}
this.timeoutHandle = window.setTimeout(this.loadtimeout.bind(this), this.timeout);
xhr.send();
}
loadend(event) {
var xhr = event.currentTarget,
status = xhr.status,
stats = this.stats,
context = this.context;
// don't proceed if xhr has been aborted
if (!stats.aborted) {
// http status between 200 to 299 are all successful
if (status >= 200 && status < 300) {
window.clearTimeout(this.timeoutHandle);
stats.tload = Math.max(stats.tfirst,performance.now());
this.onSuccess(event, stats, context);
} else {
// error ...
if (stats.retry < this.maxRetry) {
logger.warn(`${status} while loading ${this.url}, retrying in ${this.retryDelay}...`);
this.destroy();
window.setTimeout(this.loadInternal.bind(this), this.retryDelay);
// exponential backoff
this.retryDelay = Math.min(2 * this.retryDelay, 64000);
stats.retry++;
} else {
window.clearTimeout(this.timeoutHandle);
logger.error(`${status} while loading ${this.url}` );
this.onError(event, context);
}
}
}
}
loadtimeout(event) {
logger.warn(`timeout while loading ${this.url}` );
this.onTimeout(event, this.stats, this.context);
}
loadprogress(event) {
var stats = this.stats;
if (stats.tfirst === 0) {
stats.tfirst = Math.max(performance.now(), stats.trequest);
}
stats.loaded = event.loaded;
if (event.lengthComputable) {
stats.total = event.total;
}
if (this.onProgress) {
this.onProgress(event, stats, this.context);
}
}
}
export default XhrLoader;