@@ -51,15 +51,18 @@ const { Buffer } = require('buffer');
5151const { defaultTriggerAsyncIdScope } = require ( 'internal/async_hooks' ) ;
5252const { URL , urlToOptions, searchParamsSymbol } = require ( 'internal/url' ) ;
5353const { kOutHeaders, kNeedDrain } = require ( 'internal/http' ) ;
54- const { connResetException, codes } = require ( 'internal/errors' ) ;
54+ const { AbortError , connResetException, codes } = require ( 'internal/errors' ) ;
5555const {
5656 ERR_HTTP_HEADERS_SENT ,
5757 ERR_INVALID_ARG_TYPE ,
5858 ERR_INVALID_HTTP_TOKEN ,
5959 ERR_INVALID_PROTOCOL ,
6060 ERR_UNESCAPED_CHARACTERS
6161} = codes ;
62- const { validateInteger } = require ( 'internal/validators' ) ;
62+ const {
63+ validateInteger,
64+ validateAbortSignal,
65+ } = require ( 'internal/validators' ) ;
6366const { getTimerDuration } = require ( 'internal/timers' ) ;
6467const {
6568 DTRACE_HTTP_CLIENT_REQUEST ,
@@ -169,6 +172,15 @@ function ClientRequest(input, options, cb) {
169172 if ( options . timeout !== undefined )
170173 this . timeout = getTimerDuration ( options . timeout , 'timeout' ) ;
171174
175+ const signal = options . signal ;
176+ if ( signal ) {
177+ validateAbortSignal ( signal , 'options.signal' ) ;
178+ const listener = ( e ) => this . destroy ( new AbortError ( ) ) ;
179+ signal . addEventListener ( 'abort' , listener ) ;
180+ this . once ( 'close' , ( ) => {
181+ signal . removeEventListener ( 'abort' , listener ) ;
182+ } ) ;
183+ }
172184 let method = options . method ;
173185 const methodIsString = ( typeof method === 'string' ) ;
174186 if ( method !== null && method !== undefined && ! methodIsString ) {
0 commit comments