@@ -41,12 +41,13 @@ import { getHttpRpcClient } from './rpc/http.js'
4141
4242function request ( url : string ) {
4343 const httpClient = getHttpRpcClient ( url )
44- return async ( { method, params } : any ) => {
44+ return async ( { method, params } : any , options ?: any ) => {
4545 const { error, result } = await httpClient . request ( {
4646 body : {
4747 method,
4848 params,
4949 } ,
50+ fetchOptions : options ?. signal ? { signal : options . signal } : undefined ,
5051 } )
5152 if ( error )
5253 throw new RpcRequestError ( {
@@ -254,6 +255,79 @@ describe('args', () => {
254255 } )
255256} )
256257
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+
257331describe ( 'behavior' , ( ) => {
258332 describe ( 'error types' , ( ) => {
259333 test ( 'BaseError' , async ( ) => {
0 commit comments