1- import process from 'node:process' ;
21import { EventEmitter } from 'node:events' ;
32import { PassThrough as PassThroughStream } from 'node:stream' ;
43import type { Socket } from 'node:net' ;
54import http from 'node:http' ;
5+ import https from 'node:https' ;
66import test from 'ava' ;
77import is from '@sindresorhus/is' ;
88import type { Handler } from 'express' ;
@@ -22,18 +22,16 @@ const handler413: Handler = (_request, response) => {
2222 response . end ( ) ;
2323} ;
2424
25- const createSocketTimeoutStream = ( ) : http . ClientRequest => {
26- const stream = new PassThroughStream ( ) ;
27- // @ts -expect-error Mocking the behaviour of a ClientRequest
28- stream . setTimeout = ( ms , callback ) => {
29- process . nextTick ( callback ) ;
30- } ;
31-
32- // @ts -expect-error Mocking the behaviour of a ClientRequest
33- stream . abort = ( ) => { } ;
34- stream . resume ( ) ;
25+ const createSocketTimeoutStream = ( url : string ) : http . ClientRequest => {
26+ if ( url . includes ( 'https:' ) ) {
27+ return https . request ( url , {
28+ timeout : 1 ,
29+ } ) ;
30+ }
3531
36- return stream as unknown as http . ClientRequest ;
32+ return http . request ( url , {
33+ timeout : socketTimeout ,
34+ } ) ;
3735} ;
3836
3937test ( 'works on timeout' , withServer , async ( t , server , got ) => {
@@ -57,7 +55,7 @@ test('works on timeout', withServer, async (t, server, got) => {
5755 }
5856
5957 knocks ++ ;
60- return createSocketTimeoutStream ( ) ;
58+ return createSocketTimeoutStream ( server . url ) ;
6159 } ,
6260 } ) ) . body , 'who`s there?' ) ;
6361} ) ;
@@ -93,7 +91,7 @@ test('setting to `0` disables retrying', async t => {
9391 return 0 ;
9492 } ,
9593 } ,
96- request : ( ) => createSocketTimeoutStream ( ) ,
94+ request : ( ) => createSocketTimeoutStream ( 'https://example.com' ) ,
9795 } ) , {
9896 instanceOf : TimeoutError ,
9997 message : `Timeout awaiting 'socket' for ${ socketTimeout } ms` ,
0 commit comments