2
2
3
3
class HlsjsIPFSLoader {
4
4
constructor ( config ) {
5
- this . _abortFlag = [ false ] ;
5
+ this . _abortFlag = new AbortController ( ) ;
6
6
this . ipfs = config . ipfs
7
7
this . hash = config . ipfsHash
8
8
if ( config . debug === false ) {
@@ -28,7 +28,7 @@ class HlsjsIPFSLoader {
28
28
}
29
29
30
30
abort ( ) {
31
- this . _abortFlag [ 0 ] = true ;
31
+ this . _abortFlag . abort ( ) ;
32
32
}
33
33
34
34
load ( context , config , callbacks ) {
@@ -106,7 +106,7 @@ class HlsjsIPFSLoader {
106
106
}
107
107
return ;
108
108
}
109
- this . _abortFlag [ 0 ] = false ;
109
+ this . _abortFlag = new AbortController ( ) ;
110
110
getFile ( this . ipfs , this . hash , filename , options , this . debug , this . _abortFlag ) . then ( res => {
111
111
const data = ( context . responseType === 'arraybuffer' ) ? res : buf2str ( res )
112
112
stats . loaded = stats . total = data . length
@@ -120,37 +120,34 @@ async function getFile(ipfs, rootHash, filename, options, debug, abortFlag) {
120
120
debug ( `Fetching hash for '${ rootHash } /${ filename } '` )
121
121
const path = `${ rootHash } /${ filename } `
122
122
try {
123
- return await cat ( path , options , ipfs , debug , abortFlag )
124
- } catch ( ex ) {
125
- throw new Error ( `File not found: ${ rootHash } /${ filename } ` )
123
+ return await cat ( path , options , ipfs , debug , abortFlag ) ;
124
+ } catch ( e ) {
125
+ if ( e . name === "AbortError" || "ABORT_ERR" ) {
126
+ throw new Error ( `Cancelled reading ${ rootHash ? `${ rootHash } /` : "" } /${ filename } from IPFS` ) ;
127
+ }
128
+ debug ( e . message ) ;
129
+ throw new Error ( `File not found: ${ rootHash ? `${ rootHash } /` : "" } ${ filename } ` ) ;
126
130
}
127
131
}
128
132
129
133
function buf2str ( buf ) {
130
134
return new TextDecoder ( ) . decode ( buf )
131
135
}
132
136
133
- async function cat ( cid , options , ipfs , debug , abortFlag ) {
134
- const parts = [ ]
135
- let length = 0 , offset = 0
136
-
137
- for await ( const buf of ipfs . cat ( cid , options ) ) {
138
- parts . push ( buf )
139
- length += buf . length
140
- if ( abortFlag [ 0 ] ) {
141
- debug ( 'Cancel reading from ipfs' )
142
- break
143
- }
137
+ async function cat ( cid , options , ipfs , debug , { signal } ) {
138
+ const parts = [ ] ;
139
+ let length = 0 , offset = 0 ;
140
+ for await ( const buf of ipfs . cat ( cid , { ...options , signal } ) ) {
141
+ parts . push ( buf ) ;
142
+ length += buf . length ;
144
143
}
145
-
146
- const value = new Uint8Array ( length )
144
+ const value = new Uint8Array ( length ) ;
147
145
for ( const buf of parts ) {
148
- value . set ( buf , offset )
149
- offset += buf . length
146
+ value . set ( buf , offset ) ;
147
+ offset += buf . length ;
150
148
}
151
-
152
- debug ( `Received data for file '${ cid } ' size: ${ value . length } in ${ parts . length } blocks` )
153
- return value
149
+ debug ( `Received data for file '${ cid } ' size: ${ value . length } in ${ parts . length } blocks` ) ;
150
+ return value ;
154
151
}
155
152
156
153
exports = module . exports = HlsjsIPFSLoader
0 commit comments