@@ -41,12 +41,13 @@ import { getHttpRpcClient } from './rpc/http.js'
41
41
42
42
function request ( url : string ) {
43
43
const httpClient = getHttpRpcClient ( url )
44
- return async ( { method, params } : any ) => {
44
+ return async ( { method, params } : any , options ?: any ) => {
45
45
const { error, result } = await httpClient . request ( {
46
46
body : {
47
47
method,
48
48
params,
49
49
} ,
50
+ fetchOptions : options ?. signal ? { signal : options . signal } : undefined ,
50
51
} )
51
52
if ( error )
52
53
throw new RpcRequestError ( {
@@ -254,6 +255,79 @@ describe('args', () => {
254
255
} )
255
256
} )
256
257
258
+ describe ( 'options' , ( ) => {
259
+ test ( 'passes signal to underlying request' , async ( ) => {
260
+ let receivedSignal : AbortSignal | undefined
261
+ const mockRequest = async ( _args : any , options ?: any ) => {
262
+ receivedSignal = options ?. signal
263
+ return 'success'
264
+ }
265
+
266
+ const controller = new AbortController ( )
267
+ const request_ = buildRequest ( mockRequest )
268
+
269
+ await request_ ( { method : 'eth_blockNumber' } , { signal : controller . signal } )
270
+
271
+ expect ( receivedSignal ) . toBe ( controller . signal )
272
+ } )
273
+
274
+ test ( 'passes other options alongside signal' , async ( ) => {
275
+ let receivedOptions : any
276
+ const mockRequest = async ( _args : any , options ?: any ) => {
277
+ receivedOptions = options
278
+ return 'success'
279
+ }
280
+
281
+ const controller = new AbortController ( )
282
+ const request_ = buildRequest ( mockRequest )
283
+
284
+ await request_ (
285
+ { method : 'eth_blockNumber' } ,
286
+ {
287
+ signal : controller . signal ,
288
+ retryCount : 1 ,
289
+ dedupe : true ,
290
+ } ,
291
+ )
292
+
293
+ expect ( receivedOptions ) . toEqual ( { signal : controller . signal } )
294
+ } )
295
+
296
+ test ( 'works without signal' , async ( ) => {
297
+ let receivedOptions : any
298
+ const mockRequest = async ( _args : any , options ?: any ) => {
299
+ receivedOptions = options
300
+ return 'success'
301
+ }
302
+
303
+ const request_ = buildRequest ( mockRequest )
304
+
305
+ await request_ ( { method : 'eth_blockNumber' } , { dedupe : true } )
306
+
307
+ expect ( receivedOptions ) . toBeUndefined ( )
308
+ } )
309
+
310
+ test ( 'prioritizes override options over initial options' , async ( ) => {
311
+ let receivedSignal : AbortSignal | undefined
312
+ const mockRequest = async ( _args : any , options ?: any ) => {
313
+ receivedSignal = options ?. signal
314
+ return 'success'
315
+ }
316
+
317
+ const controller1 = new AbortController ( )
318
+ const controller2 = new AbortController ( )
319
+
320
+ const request_ = buildRequest ( mockRequest , { signal : controller1 . signal } )
321
+
322
+ await request_ (
323
+ { method : 'eth_blockNumber' } ,
324
+ { signal : controller2 . signal } ,
325
+ )
326
+
327
+ expect ( receivedSignal ) . toBe ( controller2 . signal )
328
+ } )
329
+ } )
330
+
257
331
describe ( 'behavior' , ( ) => {
258
332
describe ( 'error types' , ( ) => {
259
333
test ( 'BaseError' , async ( ) => {
0 commit comments