This repository has been archived by the owner on Jan 15, 2020. It is now read-only.
/
offline-cache.js
622 lines (523 loc) · 16.5 KB
/
offline-cache.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/**
*
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
import Constants from '../constants/constants';
import LicensePersister from './license-persister';
import Utils from './utils';
class OfflineCache {
static has (name) {
if (!Constants.SUPPORTS_CACHING) {
return Promise.resolve(false);
}
name = this.convertPathToName(name);
// If the download is in flight, then assume we simply don't have it,
// which should fall things back to the network.
if (OfflineCache.getInFlight(name)) {
return Promise.resolve(false);
}
return this.purgePartialDownloads(name).then(_ => {
return caches.has(name);
});
}
static addInFlight (name) {
if (!this._inflight) {
this._inflight = new Set();
}
name = this.convertPathToName(name);
return this._inflight.add(name);
}
static getInFlight (name) {
if (!this._inflight) {
this._inflight = new Set();
}
name = this.convertPathToName(name);
return this._inflight.has(name);
}
static removeInFlight (name) {
if (!this._inflight) {
this._inflight = new Set();
}
name = this.convertPathToName(name);
return this._inflight.delete(name);
}
static purgePartialDownloads (name) {
name = this.convertPathToName(name);
return caches.has(name).then(hasCacheOfName => {
if (!hasCacheOfName) {
return;
}
return caches.open(name)
// 1. Get all the items in the cache.
.then(cache => cache.keys())
.then(cacheItems => {
// 2. Locate the chunks, if there are any.
let maxIndex = Number.NEGATIVE_INFINITY;
let url = '';
cacheItems.forEach(item => {
const endsInNumber = /_(\d+)$/.exec(item.url);
if (!endsInNumber) {
return;
}
const chunkIndex = parseInt(endsInNumber[1], 10);
if (chunkIndex < maxIndex) {
return;
}
maxIndex = chunkIndex;
url = item.url;
});
// No chunks stored at all - delete.
if (maxIndex === Number.NEGATIVE_INFINITY || url === '') {
return caches.delete(name);
}
// 3. Compare the largest chunk index to what we'd expect to have.
return caches.match(url).then(chunk => {
return parseInt(chunk.headers.get('Content-Length'), 10);
}).then(videoSize => {
if (isNaN(videoSize)) {
return;
}
const expectedIndex =
Math.floor(videoSize / Constants.CHUNK_SIZE);
if (expectedIndex == maxIndex) {
return;
}
// This is a partial download - purge it.
console.log(`Purging ${name}`);
return Promise.all([
caches.delete(name),
LicensePersister.remove(name)
]);
});
});
});
}
static hasPrefetched (assetPath) {
if (!Constants.SUPPORTS_CACHING) {
return Promise.resolve(false);
}
return caches.open('prefetch').then(cache => {
return cache.match(`${assetPath}/${Constants.PREFETCH_VIDEO_PATH}_0`);
});
}
static removeAllPrefetched () {
return caches.delete('prefetch');
}
static getAll () {
return caches.keys().then(caches => {
return caches.filter(c => {
return !(c === 'prefetch' || c.startsWith(Constants.APP_NAME));
});
});
}
static convertPathToName (path) {
return path
.replace(/^\//, '')
.replace(/\/$/, '');
}
constructor () {
this._cancel = new Set();
if (!Constants.SUPPORTS_BACKGROUND_FETCH) {
return;
}
this._serviceWorkerCallbacks = null;
this._onServiceWorkerMessage = this._onServiceWorkerMessage.bind(this);
navigator.serviceWorker.addEventListener('message',
this._onServiceWorkerMessage);
}
cancel (name) {
name = OfflineCache.convertPathToName(name);
return OfflineCache.has(name).then(hasCache => {
// Can't cancel if the item is already cached.
if (hasCache) {
return Promise.reject();
}
console.log('Setting cancel for ' + name);
this._cancel.add(name);
});
}
add (name, assetPath, pagePath, callbacks, drmInfo) {
if (!Constants.SUPPORTS_CACHING) {
return;
}
const assets = [];
const assetsToCache = [
// The meta asssets.
...Constants.OFFLINE_ASSET_LIST,
// The video.
{
src: Constants.OFFLINE_VIDEO_PATH,
chunk: true
},
// The audio.
{
src: Constants.OFFLINE_AUDIO_PATH,
chunk: true
}
];
// The meta assets.
assetsToCache.forEach(asset => {
const src = asset.src || asset;
const dest = asset.dest || asset.src || asset;
const chunk = asset.chunk || false;
assets.push({
request: `${assetPath}/${dest}`,
response: null,
responseInfo: {
url: `${assetPath}/${src}`,
options: {
mode: 'cors'
}
},
chunk
});
});
// Ensure that the request for the page isn't gzipped in response.
const headers = {
'X-No-Compression': true
};
assets.push({
request: pagePath,
response: null,
responseInfo: {
url: pagePath,
options: {
headers
}
}
});
const tasks = [];
if (drmInfo) {
console.log('Acquiring persistent license');
tasks.push(LicensePersister.persist(name, drmInfo));
}
if (Constants.SUPPORTS_BACKGROUND_FETCH) {
tasks.push(this._downloadBackground(name, assets, callbacks));
} else {
assets.forEach(asset => {
const {url, options} = asset.responseInfo;
if (options.headers) {
options.headers = new Headers(options.headers);
}
asset.response = fetch(url, options);
});
tasks.push(this._downloadForeground(name, assets, callbacks));
}
// Mark the download as being in-flight.
OfflineCache.addInFlight(name);
return Promise.all(tasks);
}
prefetch (manifestPath, prefetchLimit=30) {
if (typeof shaka === 'undefined') {
return;
}
const makeRequest = ({path, start, end, chunk}={}) => {
const headers = new Headers();
if (typeof start !== 'undefined' && typeof end !== 'undefined') {
headers.set('range', `bytes=${start}-${end}`);
if (!chunk) {
throw new Error(`Unable to cache unchunked, ranged requests.
(${path})`);
}
}
const response = fetch(path, {
mode: 'cors',
headers
});
return {
request: path,
response,
chunk
};
};
return this._getManifest(manifestPath)
.then(manifest => this._getRanges(manifestPath, manifest))
.then(ranges => {
return Promise.all([
this._getFileSegments(ranges.audio),
this._getFileSegments(ranges.video)
]).then(segments => {
return [
segments[0].filter(s => s.endTime < prefetchLimit),
segments[1].filter(s => s.endTime < prefetchLimit)
];
})
.then(filteredSegments => {
// One fetch for audio, another for video.
const fetches = filteredSegments.map((segments, idx) => {
const {start, end} = segments.reduce((prev, s) => {
prev.start = Math.min(prev.start, s.startByte);
prev.end = Math.max(prev.end, s.endByte);
return prev;
},
{
start: Number.POSITIVE_INFINITY,
end: Number.NEGATIVE_INFINITY
});
// Create a fetch for the particular byte range we want to use.
const path = idx === 0 ? ranges.audio.path : ranges.video.path;
return makeRequest({path, start, end, chunk: true});
});
// Add on the requests for the first n bytes of the other
// representations that equate to the headers. It means that we
// won't need to wait for their bytes.
ranges.extra.forEach(extra => {
fetches.push(makeRequest({
path: extra.path,
start: 0,
end: extra.end,
chunk: true
}));
});
fetches.push(makeRequest({path: manifestPath}));
return this._downloadForeground('prefetch', fetches, {
onProgressCallback () {},
onCompleteCallback () {
console.log(`Prefetched ${prefetchLimit}s.`);
}
}).catch(e => {
console.error(e);
});
})
.catch(prefetchErr => {
console.warn('Unable to prefetch content:', prefetchErr);
});
});
}
_downloadForeground (name, fetches, callbacks) {
name = OfflineCache.convertPathToName(name);
const downloads = Promise.all(fetches.map(r => r.response));
return downloads.then(responses => {
this._trackDownload(name, responses, callbacks);
return caches.open(name).then(cache => {
return Promise.all(fetches.map(asset => {
return asset.response.then(response => {
if (!asset.chunk) {
return cache.put(asset.request, response);
}
return Utils.cacheInChunks(cache, response);
});
}));
});
});
}
_downloadBackground (name, assets, callbacks) {
this._serviceWorkerCallbacks = callbacks;
navigator.serviceWorker.ready.then(registration => {
if (!registration.active) {
return;
}
console.log('Asking SW to background fetch assets');
registration.active.postMessage({
action: 'offline',
tag: name,
assets
});
});
}
_onServiceWorkerMessage (evt) {
const {offline, success, name} = evt.data;
// Ignore messages not intended for this.
if (!offline) {
return;
}
OfflineCache.removeInFlight(name);
if (!this._serviceWorkerCallbacks) {
return;
}
if (success) {
this._serviceWorkerCallbacks.onCompleteCallback();
} else {
this._serviceWorkerCallbacks.onCancelCallback();
}
this._serviceWorkerCallbacks = null;
}
_getManifest (manifestPath) {
return fetch(manifestPath).then(r => r.text()).then(dashManifest => {
const parser = new DOMParser();
const doc = parser.parseFromString(dashManifest, 'application/xml');
if (doc.querySelector('parsererror')) {
return Promise.reject('Unable to parse manifest.');
}
return doc;
});
}
_getFileSegments (file) {
if (file.path.endsWith('mp4')) {
return this._getMP4FileSegments(file);
} else if (file.path.endsWith('webm')) {
return this._getWebMFileSegments(file);
}
throw new Error(`Unsupported file type: ${file.path}`);
}
_getMP4FileSegments (file) {
const headers = new Headers();
headers.set('range', `bytes=${file.bytes.start}-${file.bytes.end}`);
return fetch(file.path, {headers})
.then(r => r.arrayBuffer())
.then(sidx => {
const refs = shaka.media.Mp4SegmentIndexParser(sidx, 0, [], 0);
refs.forEach(ref => {
ref.startByte += file.bytes.start;
ref.endByte += file.bytes.start;
});
refs.push({
startByte: 0,
endByte: file.bytes.end - 1,
startTime: 0,
endTime: 0
});
return refs;
});
}
_getWebMFileSegments (file) {
// Make a request from 0 to the end index indicated in the DASH manifest.
// This represents both the Cues and the WebM header, which can then be
// sliced out as needed.
const headers = new Headers();
headers.set('range', `bytes=0-${file.bytes.end}`);
if (file.path === null) {
console.warn('Unable to locate file to prefetch');
return;
}
return fetch(file.path, {headers})
.then(r => r.arrayBuffer())
.then(data => {
const cuesData = data.slice(file.bytes.start);
const initData = data.slice(0, file.bytes.start);
const parser = new shaka.media.WebmSegmentIndexParser();
const refs = parser.parse(cuesData, initData, [], 0);
refs.forEach(ref => {
ref.startByte += file.bytes.start;
ref.endByte += file.bytes.start;
});
refs.push({
startByte: 0,
endByte: file.bytes.end - 1,
startTime: 0,
endTime: 0
});
return refs;
});
}
_getRanges (manifestPath, doc) {
const manifestParentPath = manifestPath.replace(/\/[^\/]*$/, '');
const ranges = {
video: {
path: null,
bytes: {
start: null,
end: null
}
},
audio: {
path: null,
bytes: {
start: null,
end: null
}
},
extra: []
};
Array.from(doc.querySelectorAll('AdaptationSet')).forEach(adaptation => {
const baseURLs = adaptation.querySelectorAll('BaseURL');
const type = adaptation.getAttribute('contentType');
if (!baseURLs.length) {
return;
}
baseURLs.forEach(baseURL => {
const segment = baseURL.parentNode.querySelector('SegmentBase');
const path = baseURL.textContent;
const range = segment.getAttribute('indexRange');
if (!segment) {
return;
}
if (!range) {
return;
}
const rangeVals = range.split('-');
const prefetchPath = `mp4/${path}`;
if (prefetchPath === Constants.PREFETCH_VIDEO_PATH ||
prefetchPath === Constants.PREFETCH_AUDIO_PATH) {
ranges[type].path = `${manifestParentPath}/${path}`;
ranges[type].bytes.start = parseInt(rangeVals[0], 10);
ranges[type].bytes.end = parseInt(rangeVals[1], 10);
} else {
// We can grab the bytes for any other representations as well, so that
// when the player boots up and asks for their headers we can pass them
// back super quickly from the cache.
ranges.extra.push({
path: `${manifestParentPath}/${path}`,
end: parseInt(rangeVals[1], 10)
});
}
});
});
return ranges;
}
_trackDownload (name, responses, {
onProgressCallback,
onCompleteCallback,
onCancelCallback
}={
onProgressCallback () {},
onCompleteCallback () {},
onCancelCallback () {}
}) {
let byteCount = 0;
const byteTotal = responses.reduce((byteTotal, r) => {
let responseLength = parseInt(r.headers.get('Content-Length'), 10);
if (Number.isNaN(responseLength)) {
responseLength = 0;
}
return byteTotal + responseLength;
}, 0);
Promise.all(responses.map(response => {
return new Promise((resolve, reject) =>{
const clone = response.clone();
const onStreamData = result => {
if (result.done || this._cancel.has(name)) {
return resolve();
}
byteCount += result.value.length;
onProgressCallback(byteCount, byteTotal);
return reader.read().then(onStreamData);
};
const reader = clone.body.getReader();
reader.read().then(onStreamData);
});
})).then(_ => {
if (this._cancel.has(name)) {
this._cancel.delete(name);
OfflineCache.removeInFlight(name);
return onCancelCallback(name);
}
OfflineCache.removeInFlight(name);
onCompleteCallback(byteTotal);
});
}
remove (name) {
name = OfflineCache.convertPathToName(name);
if (this._cancel.has(name)) {
this._cancel.delete(name);
}
return Promise.all([
caches.delete(name),
LicensePersister.remove(name)
]);
}
}
export default OfflineCache;