-
Notifications
You must be signed in to change notification settings - Fork 237
/
stream.js
192 lines (174 loc) · 6.47 KB
/
stream.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
/********************************************************
Copyright (c) <2019> <copyright ErosZy>
"Anti 996" License Version 1.0 (Draft)
Permission is hereby granted to any individual or legal entity
obtaining a copy of this licensed work (including the source code,
documentation and/or related items, hereinafter collectively referred
to as the "licensed work"), free of charge, to deal with the licensed
work for any purpose, including without limitation, the rights to use,
reproduce, modify, prepare derivative works of, distribute, publish
and sublicense the licensed work, subject to the following conditions:
1. The individual or the legal entity must conspicuously display,
without modification, this License and the notice on each redistributed
or derivative copy of the Licensed Work.
2. The individual or the legal entity must strictly comply with all
applicable laws, regulations, rules and standards of the jurisdiction
relating to labor and employment where the individual is physically
located or where the individual was born or naturalized; or where the
legal entity is registered or is operating (whichever is stricter). In
case that the jurisdiction has no such laws, regulations, rules and
standards or its laws, regulations, rules and standards are
unenforceable, the individual or the legal entity are required to
comply with Core International Labor Standards.
3. The individual or the legal entity shall not induce, suggest or force
its employee(s), whether full-time or part-time, or its independent
contractor(s), in any methods, to agree in oral or written form, to
directly or indirectly restrict, weaken or relinquish his or her
rights or remedies under such laws, regulations, rules and standards
relating to labor and employment as mentioned above, no matter whether
such written or oral agreements are enforceable under the laws of the
said jurisdiction, nor shall such individual or the legal entity
limit, in any methods, the rights of its employee(s) or independent
contractor(s) from reporting or complaining to the copyright holder or
relevant authorities monitoring the compliance of the license about
its violation(s) of the said license.
THE LICENSED WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN ANY WAY CONNECTION WITH THE
LICENSED WORK OR THE USE OR OTHER DEALINGS IN THE LICENSED WORK.
*********************************************************/
// see: https://github.com/qiaozi-tech/WXInlinePlayer/issues/8
export default function() {
let supportSharedBuffer = false;
try {
supportSharedBuffer = !!new SharedArrayBuffer(0);
} catch (e) {
// nothing to do...
}
function concat(i, j) {
const buffer = new Uint8Array(i.length + j.length);
buffer.set(new Uint8Array(i), 0);
buffer.set(new Uint8Array(j), i.length);
return buffer;
}
function slice(buffer, startIndex, endIndex) {
if (!endIndex || endIndex - startIndex > buffer.length) {
endIndex = buffer.length;
}
if(supportSharedBuffer){
let sharedBuffer = new SharedArrayBuffer(endIndex - startIndex);
let result = new Uint8Array(sharedBuffer);
result.set(buffer.subarray(startIndex, endIndex));
return result;
}else{
return buffer.subarray(startIndex, endIndex);
}
}
function StreamLoader({ url, chunkSize = 256 * 1024 }) {
this.url = url;
this.done = false;
this.reader = null;
this.chunkSize = chunkSize;
this.data = new Uint8Array(0);
}
StreamLoader.prototype.hasData = function() {
return !this.done || (this.done && this.data.length);
};
StreamLoader.prototype.read = function() {
if (this.data.length < this.chunkSize) {
if (this.done) {
return this._getChunk();
}
return this._request().then(() => {
return this._getChunk();
});
}
return this._getChunk();
};
StreamLoader.prototype.cancel = function() {
this.data = null;
this.reader = null;
this.done = true;
};
StreamLoader.prototype._getChunk = function() {
return new Promise(resolve => {
const buffer = slice(this.data, 0, this.chunkSize);
this.data = slice(this.data, this.chunkSize);
resolve(buffer);
});
};
StreamLoader.prototype._request = function() {
if (this.reader) {
return this.reader.read().then(result => {
let { value, done } = result;
value = new Uint8Array(value ? value : 0);
this.data = concat(this.data, value);
if (done) {
this.done = true;
} else if (this.data.length < this.chunkSize) {
return this._request();
}
});
} else {
return fetch(this.url, {
method: 'GET'
})
.then(resp => {
const { status, statusText } = resp;
if (status < 200 || status > 299) {
return resp.text().then(text => {
self.postMessage({
type: 'event',
data: {
type: 'loadError',
data: { status, statusText, detail: text }
}
});
});
}
self.postMessage({ type: 'event', data: { type: 'loadSuccess' } });
this.reader = resp.body.getReader();
return this._request();
})
.catch(e => {
self.postMessage({
type: 'event',
data: {
type: 'loadError',
data: { status: -1, statusText: 'unknown error', detail: e }
}
});
});
}
};
let loader = null;
self.onmessage = message => {
const { type, id, data } = message.data;
if (type == 'constructor') {
loader = new StreamLoader(data);
self.postMessage({ id });
} else if (type == 'hasData') {
self.postMessage({
id,
data: !loader ? false : loader.hasData()
});
} else if (type == 'read') {
if (loader) {
loader.read().then(data => {
data = new Uint8Array(data);
self.postMessage({ id, data }, [data.buffer]);
});
} else {
self.postMessage({ id, data: new Uint8Array(0) });
}
} else if (type == 'cancel') {
if (loader) {
loader.cancel();
}
self.postMessage({ id, destroy: true });
}
};
}