@@ -39,21 +39,21 @@ class HlsjsIPFSLoader {
39
39
}
40
40
/**
41
41
* Call this by getting the HLSIPFSLoader instance from hls.js hls.coreComponents[0].loaders.manifest.setM3U8Provider()
42
- * @param {function } provider
42
+ * @param {function } provider
43
43
*/
44
44
setM3U8Provider ( provider ) {
45
45
this . m3u8provider = provider ;
46
46
}
47
47
/**
48
- *
49
- * @param {function } provider
48
+ *
49
+ * @param {function } provider
50
50
*/
51
51
setTsListProvider ( provider ) {
52
52
this . tsListProvider = provider ;
53
53
}
54
54
55
55
loadInternal ( ) {
56
- const { stats, context, config , callbacks } = this
56
+ const { stats, context, callbacks } = this
57
57
58
58
stats . tfirst = Math . max ( performance . now ( ) , stats . trequest )
59
59
stats . loaded = 0
@@ -102,32 +102,40 @@ class HlsjsIPFSLoader {
102
102
}
103
103
}
104
104
105
- function getFile ( ipfs , rootHash , filename , debug ) {
105
+ async function getFile ( ipfs , rootHash , filename , debug ) {
106
106
debug ( `Fetching hash for '${ rootHash } /${ filename } '` )
107
107
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 )
112
109
}
113
- return ipfs . ls ( rootHash ) . then ( res => {
114
- const link = res . find ( ( { name } ) => ( name === filename ) )
115
110
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
118
114
}
119
115
120
116
debug ( `Requesting '${ link . path } '` )
117
+ return cat ( link . cid , ipfs , debug )
118
+ }
121
119
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 } ` )
127
121
}
128
122
129
123
function buf2str ( buf ) {
130
124
return String . fromCharCode . apply ( null , new Uint8Array ( buf ) )
131
125
}
132
126
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
+
133
141
exports = module . exports = HlsjsIPFSLoader
0 commit comments