File tree Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -64,7 +64,7 @@ class Saturn {
64
64
startTime : new Date ( )
65
65
}
66
66
67
- const controller = new AbortController ( )
67
+ const controller = options . controller ?? new AbortController ( )
68
68
const connectTimeout = setTimeout ( ( ) => {
69
69
controller . abort ( )
70
70
} , options . connectTimeout )
@@ -105,7 +105,7 @@ class Saturn {
105
105
throw err
106
106
}
107
107
108
- return { res, log }
108
+ return { res, controller , log }
109
109
}
110
110
111
111
/**
@@ -118,7 +118,7 @@ class Saturn {
118
118
* @returns {Promise<AsyncIterable<Uint8Array>> }
119
119
*/
120
120
async * fetchContent ( cidPath , opts = { } ) {
121
- const { res, log } = await this . fetchCID ( cidPath , opts )
121
+ const { res, controller , log } = await this . fetchCID ( cidPath , opts )
122
122
123
123
async function * metricsIterable ( itr ) {
124
124
log . numBytesSent = 0
@@ -134,6 +134,8 @@ class Saturn {
134
134
yield * extractVerifiedContent ( cidPath , itr )
135
135
} catch ( err ) {
136
136
log . error = err . message
137
+ controller . abort ( )
138
+
137
139
throw err
138
140
} finally {
139
141
this . _finalizeLog ( log )
Original file line number Diff line number Diff line change @@ -53,6 +53,19 @@ describe('Saturn client', () => {
53
53
await assert . rejects ( client . fetchCID ( TEST_CID , { connectTimeout : 1 } ) )
54
54
} )
55
55
56
+ it ( 'should use external abort controller' , async ( ) => {
57
+ const controller = new AbortController ( )
58
+ setTimeout ( ( ) => controller . abort ( ) , 5 )
59
+
60
+ await assert . rejects (
61
+ client . fetchCID ( TEST_CID , { controller } ) ,
62
+ {
63
+ name : 'AbortError' ,
64
+ message : 'This operation was aborted'
65
+ }
66
+ )
67
+ } )
68
+
56
69
it . skip ( 'should fail when exceeding download timeout' , async ( ) => {
57
70
await assert . rejects ( client . fetchCID ( `${ TEST_CID } /blah` , { downloadTimeout : 1 } ) )
58
71
} )
You can’t perform that action at this time.
0 commit comments