Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v18.x backport] Backport a couple of PRs #49618

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
528 changes: 528 additions & 0 deletions doc/api/test.md

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ let ReadStream;
let WriteStream;
let rimraf;
let rimrafSync;
let kResistStopPropagation;

// These have to be separate because of how graceful-fs happens to do it's
// monkeypatching.
Expand Down Expand Up @@ -2428,7 +2429,8 @@ function watch(filename, options, listener) {
process.nextTick(() => watcher.close());
} else {
const listener = () => watcher.close();
options.signal.addEventListener('abort', listener);
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
options.signal.addEventListener('abort', listener, { __proto__: null, [kResistStopPropagation]: true });
watcher.once('close', () => {
options.signal.removeEventListener('abort', listener);
});
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/fs/recursive_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function lazyLoadFsSync() {
internalSync ??= require('fs');
return internalSync;
}
let kResistStopPropagation;

async function traverse(dir, files = new SafeMap(), symbolicLinks = new SafeSet()) {
const { opendir } = lazyLoadFsPromises();
Expand Down Expand Up @@ -265,7 +266,8 @@ class FSWatcher extends EventEmitter {
} : (resolve, reject) => {
const onAbort = () => reject(new AbortError(undefined, { cause: signal.reason }));
if (signal.aborted) return onAbort();
signal.addEventListener('abort', onAbort, { __proto__: null, once: true });
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
signal.addEventListener('abort', onAbort, { __proto__: null, once: true, [kResistStopPropagation]: true });
this.once('change', (eventType, filename) => {
signal.removeEventListener('abort', onAbort);
resolve({ __proto__: null, value: { eventType, filename } });
Expand Down
7 changes: 6 additions & 1 deletion lib/internal/fs/watchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ ObjectDefineProperty(FSEvent.prototype, 'owner', {
set(v) { return this[owner_symbol] = v; },
});

let kResistStopPropagation;

async function* watch(filename, options = kEmptyObject) {
const path = toNamespacedPath(getValidatedPath(filename));
validateObject(options, 'options');
Expand Down Expand Up @@ -330,7 +332,10 @@ async function* watch(filename, options = kEmptyObject) {
};

try {
signal?.addEventListener('abort', oncancel, { once: true });
if (signal) {
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
signal.addEventListener('abort', oncancel, { __proto__: null, once: true, [kResistStopPropagation]: true });
}
handle.onchange = (status, eventType, filename) => {
if (status < 0) {
const error = uvException({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {
validateInteger,
validateObject,
} = require('internal/validators');
const { MockTimers } = require('internal/test_runner/mock/mock_timers');

function kDefaultFunction() {}

Expand Down Expand Up @@ -106,6 +107,12 @@ delete MockFunctionContext.prototype.nextImpl;

class MockTracker {
#mocks = [];
#timers;

get timers() {
this.#timers ??= new MockTimers();
return this.#timers;
}

fn(
original = function() {},
Expand Down Expand Up @@ -265,6 +272,7 @@ class MockTracker {

reset() {
this.restoreAll();
this.#timers?.reset();
this.#mocks = [];
}

Expand Down
Loading