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

Commit 44d3dfa

Browse files
refactor: abortFlag replaced with AbortController and AbortSignal
1 parent e4ac6e2 commit 44d3dfa

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/index.js

+21-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class HlsjsIPFSLoader {
44
constructor(config) {
5-
this._abortFlag = [ false ];
5+
this._abortFlag = new AbortController();
66
this.ipfs = config.ipfs
77
this.hash = config.ipfsHash
88
if (config.debug === false) {
@@ -28,7 +28,7 @@ class HlsjsIPFSLoader {
2828
}
2929

3030
abort() {
31-
this._abortFlag[0] = true;
31+
this._abortFlag.abort();
3232
}
3333

3434
load(context, config, callbacks) {
@@ -106,7 +106,7 @@ class HlsjsIPFSLoader {
106106
}
107107
return;
108108
}
109-
this._abortFlag[0] = false;
109+
this._abortFlag = new AbortController();
110110
getFile(this.ipfs, this.hash, filename, options, this.debug, this._abortFlag).then(res => {
111111
const data = (context.responseType === 'arraybuffer') ? res : buf2str(res)
112112
stats.loaded = stats.total = data.length
@@ -120,37 +120,34 @@ async function getFile(ipfs, rootHash, filename, options, debug, abortFlag) {
120120
debug(`Fetching hash for '${rootHash}/${filename}'`)
121121
const path = `${rootHash}/${filename}`
122122
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 }`);
126130
}
127131
}
128132

129133
function buf2str(buf) {
130134
return new TextDecoder().decode(buf)
131135
}
132136

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;
144143
}
145-
146-
const value = new Uint8Array(length)
144+
const value = new Uint8Array(length);
147145
for (const buf of parts) {
148-
value.set(buf, offset)
149-
offset += buf.length
146+
value.set(buf, offset);
147+
offset += buf.length;
150148
}
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;
154151
}
155152

156153
exports = module.exports = HlsjsIPFSLoader

0 commit comments

Comments
 (0)