|
1 | 1 | 'use strict'
|
2 | 2 | function HlsjsIPFSLoaderFactory (ipfs) {
|
3 | 3 | return class HlsjsIPFSLoader {
|
4 |
| - constructor (config) { |
5 |
| - this._abortFlag = [ false ]; |
| 4 | + constructor(config) { |
| 5 | + this._abortFlag = [false]; |
6 | 6 | this.ipfs = ipfs;
|
7 | 7 | this.hash = config.ipfsHash;
|
8 |
| - this.debug = config.debug === false ? function () {} : config.debug === true ? console.log : config.debug; |
| 8 | + this.debug = config.debug === false ? function () { |
| 9 | + } : config.debug === true ? console.log : config.debug; |
9 | 10 | this.m3u8provider = config.m3u8provider ?? null;
|
10 | 11 | this.tsListProvider = config.tsListProvider ?? null;
|
11 | 12 | }
|
12 | 13 |
|
13 |
| - destroy() { |
14 |
| - } |
15 |
| - |
16 |
| - abort() { |
17 |
| - this._abortFlag[0] = true; |
18 |
| - } |
| 14 | + destroy() { |
| 15 | + } |
19 | 16 |
|
20 |
| - load(context, config, callbacks) { |
21 |
| - this.context = context |
22 |
| - this.config = config |
23 |
| - this.callbacks = callbacks |
24 |
| - this.stats = { trequest: performance.now(), retry: 0 } |
25 |
| - this.retryDelay = config.retryDelay |
26 |
| - this.loadInternal() |
27 |
| - } |
28 |
| - /** |
29 |
| - * Call this by getting the HLSIPFSLoader instance from hls.js hls.coreComponents[0].loaders.manifest.setM3U8Provider() |
30 |
| - * @param {function} provider |
31 |
| - */ |
32 |
| - setM3U8Provider(provider) { |
33 |
| - this.m3u8provider = provider; |
34 |
| - } |
35 |
| - /** |
36 |
| - * |
37 |
| - * @param {function} provider |
38 |
| - */ |
39 |
| - setTsListProvider(provider) { |
40 |
| - this.tsListProvider = provider; |
41 |
| - } |
| 17 | + abort() { |
| 18 | + this._abortFlag[0] = true; |
| 19 | + } |
42 | 20 |
|
43 |
| - loadInternal() { |
44 |
| - const { stats, context, callbacks } = this |
| 21 | + load(context, config, callbacks) { |
| 22 | + this.context = context |
| 23 | + this.config = config |
| 24 | + this.callbacks = callbacks |
| 25 | + this.stats = {trequest: performance.now(), retry: 0} |
| 26 | + this.retryDelay = config.retryDelay |
| 27 | + this.loadInternal() |
| 28 | + } |
45 | 29 |
|
46 |
| - stats.tfirst = Math.max(performance.now(), stats.trequest) |
47 |
| - stats.loaded = 0 |
| 30 | + /** |
| 31 | + * Call this by getting the HLSIPFSLoader instance from hls.js hls.coreComponents[0].loaders.manifest.setM3U8Provider() |
| 32 | + * @param {function} provider |
| 33 | + */ |
| 34 | + setM3U8Provider(provider) { |
| 35 | + this.m3u8provider = provider; |
| 36 | + } |
48 | 37 |
|
49 |
| - //When using absolute path (https://example.com/index.html) vs https://example.com/ |
50 |
| - const urlParts = window.location.href.split("/") |
51 |
| - if(urlParts[urlParts.length - 1] !== "") { |
52 |
| - urlParts[urlParts.length - 1] = "" |
| 38 | + /** |
| 39 | + * |
| 40 | + * @param {function} provider |
| 41 | + */ |
| 42 | + setTsListProvider(provider) { |
| 43 | + this.tsListProvider = provider; |
53 | 44 | }
|
54 |
| - const filename = context.url.replace(urlParts.join("/"), "") |
55 | 45 |
|
56 |
| - const options = {} |
57 |
| - if (Number.isFinite(context.rangeStart)) { |
| 46 | + loadInternal() { |
| 47 | + const {stats, context, callbacks} = this |
| 48 | + |
| 49 | + stats.tfirst = Math.max(performance.now(), stats.trequest) |
| 50 | + stats.loaded = 0 |
| 51 | + |
| 52 | + //When using absolute path (https://example.com/index.html) vs https://example.com/ |
| 53 | + const urlParts = window.location.href.split("/") |
| 54 | + if (urlParts[urlParts.length - 1] !== "") { |
| 55 | + urlParts[urlParts.length - 1] = "" |
| 56 | + } |
| 57 | + const filename = context.url.replace(urlParts.join("/"), "") |
| 58 | + |
| 59 | + const options = {} |
| 60 | + if (Number.isFinite(context.rangeStart)) { |
58 | 61 | options.offset = context.rangeStart;
|
59 | 62 | if (Number.isFinite(context.rangeEnd)) {
|
60 |
| - options.length = context.rangeEnd - context.rangeStart; |
| 63 | + options.length = context.rangeEnd - context.rangeStart; |
61 | 64 | }
|
62 |
| - } |
| 65 | + } |
63 | 66 |
|
64 |
| - if(filename.split(".")[1] === "m3u8" && this.m3u8provider !== null) { |
65 |
| - const res = this.m3u8provider(); |
66 |
| - let data; |
67 |
| - if(Buffer.isBuffer(res)) { |
68 |
| - data = buf2str(res) |
69 |
| - } else { |
70 |
| - data = res; |
| 67 | + if (filename.split(".")[1] === "m3u8" && this.m3u8provider !== null) { |
| 68 | + const res = this.m3u8provider(); |
| 69 | + let data; |
| 70 | + if (Buffer.isBuffer(res)) { |
| 71 | + data = buf2str(res) |
| 72 | + } else { |
| 73 | + data = res; |
| 74 | + } |
| 75 | + const response = {url: context.url, data: data} |
| 76 | + callbacks.onSuccess(response, stats, context) |
| 77 | + return; |
71 | 78 | }
|
72 |
| - const response = { url: context.url, data: data } |
73 |
| - callbacks.onSuccess(response, stats, context) |
74 |
| - return; |
75 |
| - } |
76 |
| - if(filename.split(".")[1] === "m3u8" && this.tsListProvider !== null) { |
77 |
| - var tslist = this.tsListProvider(); |
78 |
| - var hash = tslist[filename]; |
79 |
| - if(hash) { |
80 |
| - this.cat(hash).then(res => { |
81 |
| - let data; |
82 |
| - if(Buffer.isBuffer(res)) { |
83 |
| - data = buf2str(res) |
84 |
| - } else { |
85 |
| - data = res; |
86 |
| - } |
87 |
| - stats.loaded = stats.total = data.length |
88 |
| - stats.tload = Math.max(stats.tfirst, performance.now()) |
89 |
| - const response = { url: context.url, data: data } |
90 |
| - callbacks.onSuccess(response, stats, context) |
91 |
| - }); |
| 79 | + if (filename.split(".")[1] === "m3u8" && this.tsListProvider !== null) { |
| 80 | + var tslist = this.tsListProvider(); |
| 81 | + var hash = tslist[filename]; |
| 82 | + if (hash) { |
| 83 | + this.cat(hash).then(res => { |
| 84 | + let data; |
| 85 | + if (Buffer.isBuffer(res)) { |
| 86 | + data = buf2str(res) |
| 87 | + } else { |
| 88 | + data = res; |
| 89 | + } |
| 90 | + stats.loaded = stats.total = data.length |
| 91 | + stats.tload = Math.max(stats.tfirst, performance.now()) |
| 92 | + const response = {url: context.url, data: data} |
| 93 | + callbacks.onSuccess(response, stats, context) |
| 94 | + }); |
| 95 | + } |
| 96 | + return; |
92 | 97 | }
|
93 |
| - return; |
| 98 | + this._abortFlag[0] = false; |
| 99 | + getFile(this.ipfs, this.hash, filename, options, this.debug, this._abortFlag).then(res => { |
| 100 | + const data = (context.responseType === 'arraybuffer') ? res : buf2str(res) |
| 101 | + stats.loaded = stats.total = data.length |
| 102 | + stats.tload = Math.max(stats.tfirst, performance.now()) |
| 103 | + const response = {url: context.url, data: data} |
| 104 | + callbacks.onSuccess(response, stats, context) |
| 105 | + }, console.error) |
94 | 106 | }
|
95 |
| - this._abortFlag[0] = false; |
96 |
| - getFile(this.ipfs, this.hash, filename, options, this.debug, this._abortFlag).then(res => { |
97 |
| - const data = (context.responseType === 'arraybuffer') ? res : buf2str(res) |
98 |
| - stats.loaded = stats.total = data.length |
99 |
| - stats.tload = Math.max(stats.tfirst, performance.now()) |
100 |
| - const response = { url: context.url, data: data } |
101 |
| - callbacks.onSuccess(response, stats, context) |
102 |
| - }, console.error) |
103 | 107 | }
|
104 | 108 | }
|
105 | 109 | async function getFile(ipfs, rootHash, filename, options, debug, abortFlag) {
|
|
0 commit comments