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

Commit 08f57c4

Browse files
achingbrainvaultec81
authored andcommitted
Update to new IPFS API (#16)
Refactor code and examples to use the new async iterable API
1 parent 428ffd8 commit 08f57c4

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

examples/basic_usage.html

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,34 @@
55
<script src="../dist/index.js"></script>
66

77
<script>
8-
if (window.ipfs === undefined) {
9-
const repoPath = 'ipfs-' + Math.random()
10-
const node = new Ipfs({
11-
init: false,
12-
start: false,
13-
repo: repoPath
14-
})
15-
node.init().then(() => node.start()).then(() => handleInit(node))
16-
} else {
17-
window.ipfs.enable().then(handleInit)
18-
}
8+
(async () => {
9+
if (window.ipfs === undefined) {
10+
const repoPath = 'ipfs-' + Math.random()
11+
const node = await Ipfs.create({
12+
repo: repoPath
13+
})
14+
handleInit(node)
15+
} else {
16+
window.ipfs.enable().then(handleInit)
17+
}
1918

20-
function handleInit(node) {
21-
const testhash = "QmdpAidwAsBGptFB3b6A9Pyi5coEbgjHrL3K2Qrsutmj9K";
22-
Hls.DefaultConfig.loader = HlsjsIpfsLoader;
23-
Hls.DefaultConfig.debug = false;
24-
if (Hls.isSupported()) {
25-
const video = document.getElementById('video');
26-
const hls = new Hls();
27-
hls.config.ipfs = node;
28-
hls.config.ipfsHash = testhash;
29-
hls.loadSource('master.m3u8');
30-
hls.attachMedia(video);
31-
hls.on(Hls.Events.MANIFEST_PARSED, () => {
32-
video.play();
33-
});
19+
function handleInit(node) {
20+
const testhash = "QmdpAidwAsBGptFB3b6A9Pyi5coEbgjHrL3K2Qrsutmj9K";
21+
Hls.DefaultConfig.loader = HlsjsIpfsLoader;
22+
Hls.DefaultConfig.debug = false;
23+
if (Hls.isSupported()) {
24+
const video = document.getElementById('video');
25+
const hls = new Hls();
26+
hls.config.ipfs = node;
27+
hls.config.ipfsHash = testhash;
28+
hls.loadSource('master.m3u8');
29+
hls.attachMedia(video);
30+
hls.on(Hls.Events.MANIFEST_PARSED, () => {
31+
video.play();
32+
});
33+
}
3434
}
35-
}
35+
})()
3636
</script>
3737

3838
<video id="video" controls></video>

src/index.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@ class HlsjsIPFSLoader {
3939
}
4040
/**
4141
* Call this by getting the HLSIPFSLoader instance from hls.js hls.coreComponents[0].loaders.manifest.setM3U8Provider()
42-
* @param {function} provider
42+
* @param {function} provider
4343
*/
4444
setM3U8Provider(provider) {
4545
this.m3u8provider = provider;
4646
}
4747
/**
48-
*
49-
* @param {function} provider
48+
*
49+
* @param {function} provider
5050
*/
5151
setTsListProvider(provider) {
5252
this.tsListProvider = provider;
5353
}
5454

5555
loadInternal() {
56-
const { stats, context, config, callbacks } = this
56+
const { stats, context, callbacks } = this
5757

5858
stats.tfirst = Math.max(performance.now(), stats.trequest)
5959
stats.loaded = 0
@@ -102,32 +102,40 @@ class HlsjsIPFSLoader {
102102
}
103103
}
104104

105-
function getFile(ipfs, rootHash, filename, debug) {
105+
async function getFile(ipfs, rootHash, filename, debug) {
106106
debug(`Fetching hash for '${rootHash}/${filename}'`)
107107
if(filename === null) {
108-
return ipfs.cat(rootHash).then( value => {
109-
debug(`Received data for file '${rootHash}' size: ${value.length}`)
110-
return value
111-
});
108+
return cat(rootHash, ipfs, debug)
112109
}
113-
return ipfs.ls(rootHash).then(res => {
114-
const link = res.find(({ name }) => (name === filename))
115110

116-
if (link === undefined) {
117-
throw new Error(`File not found: ${rootHash}/${filename}`)
111+
for await (const link of ipfs.ls(rootHash)) {
112+
if (link.name !== filename) {
113+
continue
118114
}
119115

120116
debug(`Requesting '${link.path}'`)
117+
return cat(link.cid, ipfs, debug)
118+
}
121119

122-
return ipfs.cat(link.hash).then(value => {
123-
debug(`Received data for file '${link.path}' size: ${value.length}`)
124-
return value
125-
})
126-
})
120+
throw new Error(`File not found: ${rootHash}/${filename}`)
127121
}
128122

129123
function buf2str(buf) {
130124
return String.fromCharCode.apply(null, new Uint8Array(buf))
131125
}
132126

127+
async function cat (cid, ipfs, debug) {
128+
let value = new Uint8Array(0)
129+
130+
for await (const buf of ipfs.cat(cid)) {
131+
const newBuf = new Uint8Array(value.length + buf.length)
132+
newBuf.set(value)
133+
newBuf.set(buf, value.length)
134+
value = newBuf
135+
}
136+
137+
debug(`Received data for file '${cid}' size: ${value.length}`)
138+
return value
139+
}
140+
133141
exports = module.exports = HlsjsIPFSLoader

0 commit comments

Comments
 (0)