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

refactor!: IPFS injection done right #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/basic_usage.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

function handleInit(node) {
const testhash = "QmdpAidwAsBGptFB3b6A9Pyi5coEbgjHrL3K2Qrsutmj9K";
Hls.DefaultConfig.loader = HlsjsIpfsLoader;
Hls.DefaultConfig.loader = HlsjsIPFSLoaderFactory(node);
Hls.DefaultConfig.debug = false;
if (Hls.isSupported()) {
const video = document.getElementById('video');
const hls = new Hls();
hls.config.ipfs = node;
hls.config.ipfsHash = testhash;
hls.loadSource('master.m3u8');
hls.attachMedia(video);
Expand Down
188 changes: 89 additions & 99 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,119 +1,109 @@
'use strict'

class HlsjsIPFSLoader {
constructor(config) {
this._abortFlag = [ false ];
this.ipfs = config.ipfs
this.hash = config.ipfsHash
if (config.debug === false) {
this.debug = function() {}
} else if (config.debug === true) {
this.debug = console.log
} else {
this.debug = config.debug
function HlsjsIPFSLoaderFactory (ipfs) {
return class HlsjsIPFSLoader {
constructor(config) {
this._abortFlag = [false];
this.ipfs = ipfs;
this.hash = config.ipfsHash;
this.debug = config.debug === false ? function () {
} : config.debug === true ? console.log : config.debug;
this.m3u8provider = config.m3u8provider ? config.m3u8provider : null;
this.tsListProvider = config.tsListProvider ? config.tsListProvider : null;
}
if(config.m3u8provider) {
this.m3u8provider = config.m3u8provider;
} else {
this.m3u8provider = null;

destroy() {
}
if(config.tsListProvider) {
this.tsListProvider = config.tsListProvider;
} else {
this.tsListProvider = null;

abort() {
this._abortFlag[0] = true;
}
}

destroy() {
}
load(context, config, callbacks) {
this.context = context
this.config = config
this.callbacks = callbacks
this.stats = {trequest: performance.now(), retry: 0}
this.retryDelay = config.retryDelay
this.loadInternal()
}

abort() {
this._abortFlag[0] = true;
}
/**
* Call this by getting the HLSIPFSLoader instance from hls.js hls.coreComponents[0].loaders.manifest.setM3U8Provider()
* @param {function} provider
*/
setM3U8Provider(provider) {
this.m3u8provider = provider;
}

load(context, config, callbacks) {
this.context = context
this.config = config
this.callbacks = callbacks
this.stats = { trequest: performance.now(), retry: 0 }
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;
}
/**
*
* @param {function} provider
*/
setTsListProvider(provider) {
this.tsListProvider = provider;
}

loadInternal() {
const { stats, context, callbacks } = this
loadInternal() {
const {stats, context, callbacks} = this

stats.tfirst = Math.max(performance.now(), stats.trequest)
stats.loaded = 0
stats.tfirst = Math.max(performance.now(), stats.trequest)
stats.loaded = 0

//When using absolute path (https://example.com/index.html) vs https://example.com/
const urlParts = window.location.href.split("/")
if(urlParts[urlParts.length - 1] !== "") {
urlParts[urlParts.length - 1] = ""
}
const filename = context.url.replace(urlParts.join("/"), "")
//When using absolute path (https://example.com/index.html) vs https://example.com/
const urlParts = window.location.href.split("/")
if (urlParts[urlParts.length - 1] !== "") {
urlParts[urlParts.length - 1] = ""
}
const filename = context.url.replace(urlParts.join("/"), "")

const options = {}
if (Number.isFinite(context.rangeStart)) {
const options = {}
if (Number.isFinite(context.rangeStart)) {
options.offset = context.rangeStart;
if (Number.isFinite(context.rangeEnd)) {
options.length = context.rangeEnd - context.rangeStart;
options.length = context.rangeEnd - context.rangeStart;
}
}
}

if(filename.split(".")[1] === "m3u8" && this.m3u8provider !== null) {
const res = this.m3u8provider();
let data;
if(Buffer.isBuffer(res)) {
data = buf2str(res)
} else {
data = res;
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;
}
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)
});
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;
}
return;
this._abortFlag[0] = false;
getFile(this.ipfs, this.hash, filename, options, this.debug, this._abortFlag).then(res => {
const data = (context.responseType === 'arraybuffer') ? res : buf2str(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)
}, console.error)
}
this._abortFlag[0] = false;
getFile(this.ipfs, this.hash, filename, options, this.debug, this._abortFlag).then(res => {
const data = (context.responseType === 'arraybuffer') ? res : buf2str(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)
}, console.error)
}
}
async function getFile(ipfs, rootHash, filename, options, debug, abortFlag) {
Expand Down Expand Up @@ -153,4 +143,4 @@ async function cat (cid, options, ipfs, debug, abortFlag) {
return value
}

exports = module.exports = HlsjsIPFSLoader
exports = module.exports = HlsjsIPFSLoaderFactory;