1
- import process from 'node:process' ;
2
1
import { EventEmitter } from 'node:events' ;
3
2
import { PassThrough as PassThroughStream } from 'node:stream' ;
4
3
import type { Socket } from 'node:net' ;
5
4
import http from 'node:http' ;
5
+ import https from 'node:https' ;
6
6
import test from 'ava' ;
7
7
import is from '@sindresorhus/is' ;
8
8
import type { Handler } from 'express' ;
@@ -22,18 +22,16 @@ const handler413: Handler = (_request, response) => {
22
22
response . end ( ) ;
23
23
} ;
24
24
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
+ }
35
31
36
- return stream as unknown as http . ClientRequest ;
32
+ return http . request ( url , {
33
+ timeout : socketTimeout ,
34
+ } ) ;
37
35
} ;
38
36
39
37
test ( 'works on timeout' , withServer , async ( t , server , got ) => {
@@ -57,7 +55,7 @@ test('works on timeout', withServer, async (t, server, got) => {
57
55
}
58
56
59
57
knocks ++ ;
60
- return createSocketTimeoutStream ( ) ;
58
+ return createSocketTimeoutStream ( server . url ) ;
61
59
} ,
62
60
} ) ) . body , 'who`s there?' ) ;
63
61
} ) ;
@@ -93,7 +91,7 @@ test('setting to `0` disables retrying', async t => {
93
91
return 0 ;
94
92
} ,
95
93
} ,
96
- request : ( ) => createSocketTimeoutStream ( ) ,
94
+ request : ( ) => createSocketTimeoutStream ( 'https://example.com' ) ,
97
95
} ) , {
98
96
instanceOf : TimeoutError ,
99
97
message : `Timeout awaiting 'socket' for ${ socketTimeout } ms` ,
0 commit comments