From 428ffd84e58e5aed039f34f107639f4ae550e752 Mon Sep 17 00:00:00 2001 From: vaultec <47548474+vaultec81@users.noreply.github.com> Date: Thu, 3 Oct 2019 19:12:00 -0700 Subject: [PATCH] Add feature dynamically updating m3u8 and .ts source file (#15) * Dynamically updating m3u8 and .ts files * add -lock.json gitignore * Remove package-lock.json * Correction --- .gitignore | 2 ++ src/index.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ed0d3c8..91ddd78 100644 --- a/.gitignore +++ b/.gitignore @@ -86,3 +86,5 @@ typings/ # DynamoDB Local files .dynamodb/ + +*-lock.json \ No newline at end of file diff --git a/src/index.js b/src/index.js index 339f52e..cb77844 100644 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,16 @@ class HlsjsIPFSLoader { } else { this.debug = config.debug } + if(config.m3u8provider) { + this.m3u8provider = config.m3u8provider; + } else { + this.m3u8provider = null; + } + if(config.tsListProvider) { + this.tsListProvider = config.tsListProvider; + } else { + this.tsListProvider = null; + } } destroy() { @@ -27,6 +37,20 @@ class HlsjsIPFSLoader { this.retryDelay = config.retryDelay this.loadInternal() } + /** + * Call this by getting the HLSIPFSLoader instance from hls.js hls.coreComponents[0].loaders.manifest.setM3U8Provider() + * @param {function} provider + */ + setM3U8Provider(provider) { + this.m3u8provider = provider; + } + /** + * + * @param {function} provider + */ + setTsListProvider(provider) { + this.tsListProvider = provider; + } loadInternal() { const { stats, context, config, callbacks } = this @@ -37,6 +61,37 @@ class HlsjsIPFSLoader { const urlParts = context.url.split("/") const filename = urlParts[urlParts.length - 1] + if(filename.split(".")[1] === "m3u8" && this.m3u8provider !== null) { + const res = this.m3u8provider(); + let data; + if(Buffer.isBuffer(res)) { + data = buf2str(res) + } else { + data = res; + } + const response = { url: context.url, data: data } + callbacks.onSuccess(response, stats, context) + return; + } + if(filename.split(".")[1] === "m3u8" && this.tsListProvider !== null) { + var tslist = this.tsListProvider(); + var hash = tslist[filename]; + if(hash) { + this.cat(hash).then(res => { + let data; + if(Buffer.isBuffer(res)) { + data = buf2str(res) + } else { + data = res; + } + stats.loaded = stats.total = data.length + stats.tload = Math.max(stats.tfirst, performance.now()) + const response = { url: context.url, data: data } + callbacks.onSuccess(response, stats, context) + }); + } + return; + } getFile(this.ipfs, this.hash, filename, this.debug).then(res => { const data = (context.responseType === 'arraybuffer') ? res : buf2str(res) stats.loaded = stats.total = data.length @@ -49,7 +104,12 @@ class HlsjsIPFSLoader { function getFile(ipfs, rootHash, filename, debug) { debug(`Fetching hash for '${rootHash}/${filename}'`) - + if(filename === null) { + return ipfs.cat(rootHash).then( value => { + debug(`Received data for file '${rootHash}' size: ${value.length}`) + return value + }); + } return ipfs.ls(rootHash).then(res => { const link = res.find(({ name }) => (name === filename))