Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
Add feature dynamically updating m3u8 and .ts source file (#15)
Browse files Browse the repository at this point in the history
* Dynamically updating m3u8 and .ts files

* add -lock.json gitignore

* Remove package-lock.json

* Correction
  • Loading branch information
vaultec81 authored Oct 4, 2019
1 parent d89cb49 commit 428ffd8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ typings/

# DynamoDB Local files
.dynamodb/

*-lock.json
62 changes: 61 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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))

Expand Down

0 comments on commit 428ffd8

Please sign in to comment.