@@ -18,6 +18,7 @@ import { urlToHttpOptions } from "ext:deno_node/internal/url.ts";
18
18
import { constants , TCP } from "ext:deno_node/internal_binding/tcp_wrap.ts" ;
19
19
import * as denoHttp from "ext:deno_http/01_http.js" ;
20
20
import * as httpRuntime from "ext:runtime/40_http.js" ;
21
+ import { connResetException } from "ext:deno_node/internal/errors.ts" ;
21
22
22
23
enum STATUS_CODES {
23
24
/** RFC 7231, 6.2.1 */
@@ -259,16 +260,21 @@ class ClientRequest extends NodeWritable {
259
260
method : this . opts . method ,
260
261
client,
261
262
headers : this . opts . headers ,
263
+ signal : this . opts . signal ?? undefined ,
262
264
} ;
263
265
const mayResponse = fetch ( this . _createUrlStrFromOptions ( this . opts ) , opts )
264
266
. catch ( ( e ) => {
265
267
if ( e . message . includes ( "connection closed before message completed" ) ) {
266
268
// Node.js seems ignoring this error
269
+ } else if ( e . message . includes ( "The signal has been aborted" ) ) {
270
+ // Remap this error
271
+ this . emit ( "error" , connResetException ( "socket hang up" ) ) ;
267
272
} else {
268
273
this . emit ( "error" , e ) ;
269
274
}
270
275
return undefined ;
271
276
} ) ;
277
+
272
278
const res = new IncomingMessageForClient (
273
279
await mayResponse ,
274
280
this . _createSocket ( ) ,
@@ -279,6 +285,10 @@ class ClientRequest extends NodeWritable {
279
285
client . close ( ) ;
280
286
} ) ;
281
287
}
288
+ if ( this . opts . timeout != undefined ) {
289
+ clearTimeout ( this . opts . timeout ) ;
290
+ this . opts . timeout = undefined ;
291
+ }
282
292
this . cb ?.( res ) ;
283
293
}
284
294
@@ -340,8 +350,19 @@ class ClientRequest extends NodeWritable {
340
350
} ${ path } `;
341
351
}
342
352
343
- setTimeout ( ) {
344
- console . log ( "not implemented: ClientRequest.setTimeout" ) ;
353
+ setTimeout ( timeout : number , callback ?: ( ) => void ) {
354
+ const controller = new AbortController ( ) ;
355
+ this . opts . signal = controller . signal ;
356
+
357
+ this . opts . timeout = setTimeout ( ( ) => {
358
+ controller . abort ( ) ;
359
+
360
+ this . emit ( "timeout" ) ;
361
+
362
+ if ( callback !== undefined ) {
363
+ callback ( ) ;
364
+ }
365
+ } , timeout ) ;
345
366
}
346
367
}
347
368
0 commit comments