Skip to content

Commit d367b73

Browse files
atlowChemiRafaelGSS
authored andcommitted
fs: use kResistStopPropagation
PR-URL: #48521 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 333907b commit d367b73

7 files changed

Lines changed: 54 additions & 15 deletions

File tree

‎lib/fs.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ let ReadStream;
154154
letWriteStream;
155155
letrimraf;
156156
letrimrafSync;
157+
letkResistStopPropagation;
157158

158159
// These have to be separate because of how graceful-fs happens to do it's
159160
// monkeypatching.
@@ -2439,7 +2440,8 @@ function watch(filename, options, listener) {
24392440
process.nextTick(()=>watcher.close());
24402441
}else{
24412442
constlistener=()=>watcher.close();
2442-
options.signal.addEventListener('abort',listener);
2443+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
2444+
options.signal.addEventListener('abort',listener,{__proto__: null,[kResistStopPropagation]: true});
24432445
watcher.once('close',()=>{
24442446
options.signal.removeEventListener('abort',listener);
24452447
});

‎lib/internal/fs/recursive_watch.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function lazyLoadFsSync() {
4242
internalSync??=require('fs');
4343
returninternalSync;
4444
}
45+
letkResistStopPropagation;
4546

4647
asyncfunctiontraverse(dir,files=newSafeMap(),symbolicLinks=newSafeSet()){
4748
const{ opendir }=lazyLoadFsPromises();
@@ -265,7 +266,8 @@ class FSWatcher extends EventEmitter {
265266
} : (resolve,reject)=>{
266267
constonAbort=()=>reject(newAbortError(undefined,{cause: signal.reason}));
267268
if(signal.aborted)returnonAbort();
268-
signal.addEventListener('abort',onAbort,{__proto__: null,once: true});
269+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
270+
signal.addEventListener('abort',onAbort,{__proto__: null,once: true,[kResistStopPropagation]: true});
269271
this.once('change',(eventType,filename)=>{
270272
signal.removeEventListener('abort',onAbort);
271273
resolve({__proto__: null,value: { eventType, filename }});

‎lib/internal/fs/watchers.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ ObjectDefineProperty(FSEvent.prototype, 'owner', {
299299
set(v){returnthis[owner_symbol]=v;},
300300
});
301301

302+
letkResistStopPropagation;
303+
302304
asyncfunction*watch(filename,options=kEmptyObject){
303305
constpath=toNamespacedPath(getValidatedPath(filename));
304306
validateObject(options,'options');
@@ -330,7 +332,10 @@ async function* watch(filename, options = kEmptyObject) {
330332
};
331333

332334
try{
333-
signal?.addEventListener('abort',oncancel,{once: true});
335+
if(signal){
336+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
337+
signal.addEventListener('abort',oncancel,{__proto__: null,once: true,[kResistStopPropagation]: true});
338+
}
334339
handle.onchange=(status,eventType,filename)=>{
335340
if(status<0){
336341
consterror=uvException({

‎lib/internal/test_runner/mock/mock_timers.js‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const nodeTimers = require('timers');
3333
constnodeTimersPromises=require('timers/promises');
3434
constEventEmitter=require('events');
3535

36+
letkResistStopPropagation;
37+
3638
functioncompareTimersLists(a,b){
3739
return(a.runAt-b.runAt)||(a.id-b.id);
3840
}
@@ -101,17 +103,19 @@ class MockTimers {
101103
if(options?.signal){
102104
validateAbortSignal(options.signal,'options.signal');
103105

104-
if(options.signal?.aborted){
106+
if(options.signal.aborted){
105107
throwabortIt(options.signal);
106108
}
107109

108110
constonAbort=(reason)=>{
109111
emitter.emit('data',{__proto__: null,aborted: true, reason });
110112
};
111113

112-
options.signal?.addEventListener('abort',onAbort,{
114+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
115+
options.signal.addEventListener('abort',onAbort,{
113116
__proto__: null,
114117
once: true,
118+
[kResistStopPropagation]: true,
115119
});
116120
}
117121

@@ -162,7 +166,7 @@ class MockTimers {
162166
returnreject(err);
163167
}
164168

165-
if(options.signal?.aborted){
169+
if(options.signal.aborted){
166170
returnreject(abortIt(options.signal));
167171
}
168172
}
@@ -176,10 +180,14 @@ class MockTimers {
176180
returnresolve(result||id);
177181
},ms);
178182

179-
options?.signal?.addEventListener('abort',onabort,{
180-
__proto__: null,
181-
once: true,
182-
});
183+
if(options?.signal){
184+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
185+
options.signal.addEventListener('abort',onabort,{
186+
__proto__: null,
187+
once: true,
188+
[kResistStopPropagation]: true,
189+
});
190+
}
183191
});
184192
}
185193

‎lib/internal/test_runner/runner.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ const kDiagnosticsFilterArgs = ['tests', 'suites', 'pass', 'fail', 'cancelled',
7777
constkCanceledTests=newSafeSet()
7878
.add(kCancelledByParent).add(kAborted).add(kTestTimeoutFailure);
7979

80+
letkResistStopPropagation;
81+
8082
// TODO(cjihrig): Replace this with recursive readdir once it lands.
8183
functionprocessPath(path,testFiles,options){
8284
conststats=statSync(path);
@@ -442,7 +444,14 @@ function watchFiles(testFiles, root, inspectPort, signal, testNamePatterns) {
442444
triggerUncaughtException(error,true/* fromPromise */);
443445
}));
444446
});
445-
signal?.addEventListener('abort',()=>root.postRun(),{__proto__: null,once: true});
447+
if(signal){
448+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
449+
signal.addEventListener(
450+
'abort',
451+
()=>root.postRun(),
452+
{__proto__: null,once: true,[kResistStopPropagation]: true},
453+
);
454+
}
446455

447456
returnfilesWatcher;
448457
}

‎lib/internal/test_runner/test.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const kUnwrapErrors = new SafeSet()
7272
.add(kTestCodeFailure).add(kHookFailure)
7373
.add('uncaughtException').add('unhandledRejection');
7474
const{ testNamePatterns, testOnlyFlag }=parseCommandLine();
75+
letkResistStopPropagation;
7576

7677
functionstopTest(timeout,signal){
7778
if(timeout===kDefaultTimeout){
@@ -266,7 +267,15 @@ class Test extends AsyncResource {
266267
this.signal=this.#abortController.signal;
267268

268269
validateAbortSignal(signal,'options.signal');
269-
this.#outerSignal?.addEventListener('abort',this.#abortHandler);
270+
if(signal){
271+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
272+
}
273+
274+
this.#outerSignal?.addEventListener(
275+
'abort',
276+
this.#abortHandler,
277+
{__proto__: null,[kResistStopPropagation]: true},
278+
);
270279

271280
this.fn=fn;
272281
this.harness=null;// Configured on the root test by the test harness.

‎lib/timers/promises.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const {
4040
}=require('internal/util');
4141

4242
constkScheduler=Symbol('kScheduler');
43+
letkResistStopPropagation;
4344

4445
functioncancelListenerHandler(clear,reject,signal){
4546
if(!this._destroyed){
@@ -81,7 +82,8 @@ function setTimeout(after, value, options = kEmptyObject) {
8182
if(signal){
8283
oncancel=FunctionPrototypeBind(cancelListenerHandler,
8384
timeout,clearTimeout,reject,signal);
84-
signal.addEventListener('abort',oncancel);
85+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
86+
signal.addEventListener('abort',oncancel,{__proto__: null,[kResistStopPropagation]: true});
8587
}
8688
});
8789
returnoncancel!==undefined ?
@@ -123,7 +125,8 @@ function setImmediate(value, options = kEmptyObject) {
123125
oncancel=FunctionPrototypeBind(cancelListenerHandler,
124126
immediate,clearImmediate,reject,
125127
signal);
126-
signal.addEventListener('abort',oncancel);
128+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
129+
signal.addEventListener('abort',oncancel,{__proto__: null,[kResistStopPropagation]: true});
127130
}
128131
});
129132
returnoncancel!==undefined ?
@@ -164,7 +167,8 @@ async function* setInterval(after, value, options = kEmptyObject) {
164167
callback=undefined;
165168
}
166169
};
167-
signal.addEventListener('abort',onCancel,{once: true});
170+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
171+
signal.addEventListener('abort',onCancel,{__proto__: null,once: true,[kResistStopPropagation]: true});
168172
}
169173

170174
while(!signal?.aborted){

0 commit comments

Comments
 (0)