@@ -43,6 +43,7 @@ const {
43
43
ObjectValues,
44
44
ReflectApply,
45
45
StringPrototypePadStart,
46
+ SymbolDispose,
46
47
} = primordials ;
47
48
48
49
const {
@@ -63,6 +64,7 @@ const {
63
64
} = require ( 'internal/util/inspect' ) ;
64
65
const { debuglog } = require ( 'internal/util/debuglog' ) ;
65
66
const {
67
+ validateAbortSignal,
66
68
validateFunction,
67
69
validateNumber,
68
70
} = require ( 'internal/validators' ) ;
@@ -73,6 +75,7 @@ const {
73
75
deprecate,
74
76
getSystemErrorMap,
75
77
getSystemErrorName : internalErrorName ,
78
+ kEmptyObject,
76
79
promisify,
77
80
toUSVString,
78
81
defineLazyProperties,
@@ -86,6 +89,7 @@ function lazyAbortController() {
86
89
}
87
90
88
91
let internalDeepEqual ;
92
+ let kResistStopPropagation ;
89
93
90
94
/**
91
95
* @deprecated since v4.0.0
@@ -345,11 +349,31 @@ function getSystemErrorName(err) {
345
349
return internalErrorName ( err ) ;
346
350
}
347
351
352
+ function addSafeAbortSignalAbortListener ( signal , listener , options = kEmptyObject ) {
353
+ if ( signal === undefined ) {
354
+ throw new ERR_INVALID_ARG_TYPE ( 'signal' , 'AbortSignal' , signal )
355
+ }
356
+ validateAbortSignal ( signal , 'signal' ) ;
357
+ kResistStopPropagation ??= require ( 'internal/event_target' ) . kResistStopPropagation ;
358
+ signal . addEventListener ( 'abort' , listener , { ...options , [ kResistStopPropagation ] : true } ) ;
359
+ const removeEventListener = ( ) => {
360
+ signal . removeEventListener ( 'abort' , listener , options ) ;
361
+ } ;
362
+ return {
363
+ __proto__ : null ,
364
+ removeEventListener,
365
+ [ SymbolDispose ] ( ) {
366
+ removeEventListener ( ) ;
367
+ } ,
368
+ } ;
369
+ }
370
+
348
371
// Keep the `exports =` so that various functions can still be monkeypatched
349
372
module . exports = {
350
373
_errnoException : errnoException ,
351
374
_exceptionWithHostPort : exceptionWithHostPort ,
352
375
_extend,
376
+ addSafeAbortSignalAbortListener,
353
377
callbackify,
354
378
debug : debuglog ,
355
379
debuglog,
0 commit comments