Skip to content

Commit 5a21d8f

Browse files
atlowChemiCeres6
authored andcommitted
fs: use kResistStopPropagation
PR-URL: nodejs#48521 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 36c6831 commit 5a21d8f

File tree

7 files changed

+54
-15
lines changed

7 files changed

+54
-15
lines changed

lib/fs.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ let ReadStream;
154154
let WriteStream;
155155
let rimraf;
156156
let rimrafSync;
157+
let kResistStopPropagation;
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
const listener = () => 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

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function lazyLoadFsSync() {
4242
internalSync ??= require('fs');
4343
return internalSync;
4444
}
45+
let kResistStopPropagation;
4546

4647
async function traverse(dir, files = new SafeMap(), symbolicLinks = new SafeSet()) {
4748
const { opendir } = lazyLoadFsPromises();
@@ -265,7 +266,8 @@ class FSWatcher extends EventEmitter {
265266
} : (resolve, reject) => {
266267
const onAbort = () => reject(new AbortError(undefined, { cause: signal.reason }));
267268
if (signal.aborted) return onAbort();
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

+6-1
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ ObjectDefineProperty(FSEvent.prototype, 'owner', {
299299
set(v) { return this[owner_symbol] = v; },
300300
});
301301

302+
let kResistStopPropagation;
303+
302304
async function* watch(filename, options = kEmptyObject) {
303305
const path = 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
const error = uvException({

lib/internal/test_runner/mock/mock_timers.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const nodeTimers = require('timers');
3333
const nodeTimersPromises = require('timers/promises');
3434
const EventEmitter = require('events');
3535

36+
let kResistStopPropagation;
37+
3638
function compareTimersLists(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
throw abortIt(options.signal);
106108
}
107109

108110
const onAbort = (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
return reject(err);
163167
}
164168

165-
if (options.signal?.aborted) {
169+
if (options.signal.aborted) {
166170
return reject(abortIt(options.signal));
167171
}
168172
}
@@ -176,10 +180,14 @@ class MockTimers {
176180
return resolve(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

+10-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ const kDiagnosticsFilterArgs = ['tests', 'suites', 'pass', 'fail', 'cancelled',
7777
const kCanceledTests = new SafeSet()
7878
.add(kCancelledByParent).add(kAborted).add(kTestTimeoutFailure);
7979

80+
let kResistStopPropagation;
81+
8082
// TODO(cjihrig): Replace this with recursive readdir once it lands.
8183
function processPath(path, testFiles, options) {
8284
const stats = 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
return filesWatcher;
448457
}

lib/internal/test_runner/test.js

+10-1
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+
let kResistStopPropagation;
7576

7677
function stopTest(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

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

4242
const kScheduler = Symbol('kScheduler');
43+
let kResistStopPropagation;
4344

4445
function cancelListenerHandler(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
return oncancel !== 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
return oncancel !== 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)