Skip to content

Commit 4bec3a3

Browse files
author
Diego Rodríguez Baquero
authored
Upstream controller don't abort all requests (#38)
* Upstream controller don't abort all requests * If upstream controller aborted, stop
1 parent 7e9dd0e commit 4bec3a3

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/client.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,13 @@ export class Saturn {
248248
* @param {string} [opts.url]
249249
* @param {number} [opts.connectTimeout=5000]
250250
* @param {number} [opts.downloadTimeout=0]
251+
* @param {AbortController} [opts.controller]
251252
* @returns {Promise<AsyncIterable<Uint8Array>>}
252253
*/
253254
async * fetchContentWithFallback (cidPath, opts = {}) {
255+
const upstreamController = opts.controller;
256+
delete opts.controller;
257+
254258
let lastError = null
255259
// we use this to checkpoint at which chunk a request failed.
256260
// this is temporary until range requests are supported.
@@ -261,6 +265,13 @@ export class Saturn {
261265
}
262266

263267
const fetchContent = async function * () {
268+
const controller = new AbortController();
269+
opts.controller = controller;
270+
if (upstreamController) {
271+
upstreamController.signal.addEventListener('abort', () => {
272+
controller.abort();
273+
});
274+
}
264275
let byteCount = 0
265276
const byteChunks = await this.fetchContent(cidPath, opts)
266277
for await (const chunk of byteChunks) {
@@ -298,7 +309,7 @@ export class Saturn {
298309
let fallbackCount = 0
299310
const nodes = this.nodes
300311
for (let i = 0; i < nodes.length; i++) {
301-
if (fallbackCount > this.opts.fallbackLimit) {
312+
if (fallbackCount > this.opts.fallbackLimit || upstreamController?.signal.aborted) {
302313
return
303314
}
304315
if (opts.raceNodes) {

0 commit comments

Comments
 (0)