Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
d596803
doc: add path rules and validation for export targets in package.json
0hmX Jun 9, 2025
893999e
src: replace V8 Fast API todo comment with note comment
dario-piotrowicz Jun 9, 2025
a4d7560
test: close FileHandle objects in tests explicitly
jasnell Jun 9, 2025
713fbad
test_runner: support object property mocking
idango10 Jun 9, 2025
4347ce3
src: add new CopyUtimes function to reduce code duplication
dario-piotrowicz Jun 9, 2025
dc10238
deps: update zlib to 1.3.1-470d3a2
nodejs-github-bot Jun 10, 2025
80eac14
deps: update simdjson to 3.13.0
nodejs-github-bot Jun 10, 2025
3aa2762
deps: update sqlite to 3.50.1
nodejs-github-bot Jun 10, 2025
181014a
test: cleanup status files
panva Jun 10, 2025
e9c6fa5
src: replace std::array with static arrays in contextify
mertcanaltin Jun 10, 2025
85f062c
test: deflake async-hooks/test-improper-order on AIX
bakigul1 Jun 3, 2025
7e34aa4
test: skip tests failing when run under root
LiviaMedeiros Jun 10, 2025
839964e
fs: allow correct handling of burst in fs-events with AsyncIterator
pipobscure Jun 10, 2025
450f481
deps: update amaro to 1.0.0
nodejs-github-bot Jun 11, 2025
76e3c8a
test: update WPT for es-exceptions to 2f96fa1996
nodejs-github-bot Jun 11, 2025
aa657f0
test: split indirect eval import tests
legendecas Jun 11, 2025
a3c7a63
module: allow cycles in require() in the CJS handling in ESM loader
joyeecheung Jun 11, 2025
9b28f40
module: remove experimental warning from type stripping
marco-ippolito Jun 11, 2025
fa089d6
test: update WPT for dom/abort to dc928169ee
nodejs-github-bot Jun 11, 2025
94e53d4
test: update WPT for urlpattern to 3ffda23e5a
nodejs-github-bot Jun 1, 2025
b1f60d2
http2: add diagnostics channel 'http2.server.stream.close'
RaisinTen Jun 11, 2025
b11da11
http2: fix DEP0194 message
climba03003 Jun 11, 2025
afbaf92
tools: improve release proposal linter
aduh95 Jun 11, 2025
6390f70
lib,src: support DOMException ser-des
legendecas Jun 11, 2025
b6760b3
esm: syncify default path of `ModuleLoader.load`
JakobJingleheimer Jun 11, 2025
ffff8ce
typings: add ZSTD_COMPRESS, ZSTD_DECOMPRESS to internalBinding
nektro Jun 12, 2025
45f7d16
module: refactor commonjs typescript loader
marco-ippolito Jun 12, 2025
ff8a369
module: fix typescript import.meta.main
marco-ippolito Jun 12, 2025
c1f9791
tools: edit commit-queue workflow file
aduh95 Jun 12, 2025
1cc77c7
doc: punctuation fix for Node-API versioning clarification
jiacai2050 Jun 12, 2025
268c8c1
tools: remove config.status under `make distclean`
Renegade334 Jun 12, 2025
b22e970
tools: switch to `@stylistic/eslint-plugin`
targos Jun 13, 2025
ec808b3
test: use `common.skipIfInspectorDisabled()` to skip tests
dario-piotrowicz Jun 13, 2025
dfb0144
src: enhance error messages for unknown options
pmarchini Jun 13, 2025
e6a1787
tools: bump brace-expansion from 1.1.11 to 1.1.12 in /tools/eslint
dependabot[bot] Jun 14, 2025
dc2f23e
tools: bump `brace-expansion` in `/tools/clang-format`
dependabot[bot] Jun 14, 2025
ef0230a
url: add fileURLToPathBuffer API
jasnell Jun 12, 2025
b7e488c
test: refactor repl tab complete tests
dario-piotrowicz Jun 15, 2025
c39d570
test: reduce the use of private symbols in test-events-once.js
kt3k Jun 15, 2025
17df800
typings: add Atomics primordials
Renegade334 Jun 4, 2025
704b1fa
test: add tests for REPL custom evals
dario-piotrowicz Jun 15, 2025
841609a
doc: add islandryu to collaborators
islandryu Jun 16, 2025
8aa0513
2025-06-17, Version 24.3.0 (Current)
nodejs-github-bot Jun 16, 2025
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
Prev Previous commit
Next Next commit
fs: allow correct handling of burst in fs-events with AsyncIterator
PR-URL: #58490
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
pipobscure authored and targos committed Jun 16, 2025
commit 839964ece8498c6e6e8e3eddb9a01d1c1bd9bb9d
7 changes: 7 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,13 @@ Path is a directory.
An attempt has been made to read a file whose size is larger than the maximum
allowed size for a `Buffer`.

<a id="ERR_FS_WATCH_QUEUE_OVERFLOW"></a>

### `ERR_FS_WATCH_QUEUE_OVERFLOW`

The number of file system events queued without being handled exceeded the size specified in
`maxQueue` in `fs.watch()`.

<a id="ERR_HTTP2_ALTSVC_INVALID_ORIGIN"></a>

### `ERR_HTTP2_ALTSVC_INVALID_ORIGIN`
Expand Down
5 changes: 5 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,11 @@ added:
filename passed to the listener. **Default:** `'utf8'`.
* `signal` {AbortSignal} An {AbortSignal} used to signal when the watcher
should stop.
* `maxQueue` {number} Specifies the number of events to queue between iterations
of the {AsyncIterator} returned. **Default:** `2048`.
* `overflow` {string} Either `'ignore'` or `'throw'` when there are more events to be
queued than `maxQueue` allows. `'ignore'` means overflow events are dropped and a
warning is emitted, while `'throw'` means to throw an exception. **Default:** `'ignore'`.
* Returns: {AsyncIterator} of objects with the properties:
* `eventType` {string} The type of change
* `filename` {string|Buffer|null} The name of the file changed.
Expand Down
1 change: 1 addition & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,7 @@ E('ERR_FS_CP_SYMLINK_TO_SUBDIRECTORY',
E('ERR_FS_CP_UNKNOWN', 'Cannot copy an unknown file type', SystemError);
E('ERR_FS_EISDIR', 'Path is a directory', SystemError, HideStackFramesError);
E('ERR_FS_FILE_TOO_LARGE', 'File size (%s) is greater than 2 GiB', RangeError);
E('ERR_FS_WATCH_QUEUE_OVERFLOW', 'fs.watch() queued more than %d events', Error);
E('ERR_HTTP2_ALTSVC_INVALID_ORIGIN',
'HTTP/2 ALTSVC frames require a valid origin', TypeError);
E('ERR_HTTP2_ALTSVC_LENGTH',
Expand Down
47 changes: 39 additions & 8 deletions lib/internal/fs/watchers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

const {
ArrayPrototypePush,
ArrayPrototypeShift,
Error,
FunctionPrototypeCall,
ObjectDefineProperty,
ObjectSetPrototypeOf,
Expand All @@ -12,9 +15,11 @@ const {
AbortError,
UVException,
codes: {
ERR_FS_WATCH_QUEUE_OVERFLOW,
ERR_INVALID_ARG_VALUE,
},
} = require('internal/errors');

const {
kEmptyObject,
} = require('internal/util');
Expand Down Expand Up @@ -45,6 +50,8 @@ const {
validateBoolean,
validateObject,
validateUint32,
validateInteger,
validateOneOf,
} = require('internal/validators');

const {
Expand Down Expand Up @@ -309,11 +316,15 @@ async function* watch(filename, options = kEmptyObject) {
persistent = true,
recursive = false,
encoding = 'utf8',
maxQueue = 2048,
overflow = 'ignore',
signal,
} = options;

validateBoolean(persistent, 'options.persistent');
validateBoolean(recursive, 'options.recursive');
validateInteger(maxQueue, 'options.maxQueue');
validateOneOf(overflow, 'options.overflow', ['ignore', 'error']);
validateAbortSignal(signal, 'options.signal');

if (encoding && !isEncoding(encoding)) {
Expand All @@ -325,10 +336,11 @@ async function* watch(filename, options = kEmptyObject) {
throw new AbortError(undefined, { cause: signal.reason });

const handle = new FSEvent();
let { promise, resolve, reject } = PromiseWithResolvers();
let { promise, resolve } = PromiseWithResolvers();
const queue = [];
const oncancel = () => {
handle.close();
reject(new AbortError(undefined, { cause: signal?.reason }));
resolve();
};

try {
Expand All @@ -345,11 +357,20 @@ async function* watch(filename, options = kEmptyObject) {
});
error.filename = filename;
handle.close();
reject(error);
ArrayPrototypePush(queue, error);
resolve();
return;
}

resolve({ eventType, filename });
if (queue.length < maxQueue) {
ArrayPrototypePush(queue, { __proto__: null, eventType, filename });
resolve();
} else if (overflow === 'error') {
queue.length = 0;
ArrayPrototypePush(queue, new ERR_FS_WATCH_QUEUE_OVERFLOW(maxQueue));
resolve();
} else {
process.emitWarning('fs.watch maxQueue exceeded');
}
};

const err = handle.start(path, persistent, recursive, encoding);
Expand All @@ -367,10 +388,20 @@ async function* watch(filename, options = kEmptyObject) {
}

while (!signal?.aborted) {
yield await promise;
({ promise, resolve, reject } = PromiseWithResolvers());
await promise;
while (queue.length) {
const item = ArrayPrototypeShift(queue);
if (item instanceof Error) {
throw item;
} else {
yield item;
}
}
({ promise, resolve } = PromiseWithResolvers());
}
if (signal?.aborted) {
throw new AbortError(undefined, { cause: signal?.reason });
}
throw new AbortError(undefined, { cause: signal?.reason });
} finally {
handle.close();
signal?.removeEventListener('abort', oncancel);
Expand Down
3 changes: 3 additions & 0 deletions test/parallel/parallel.status
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ test-domain-throw-error-then-throw-from-uncaught-exception-handler: PASS, FLAKY
test-domain-with-abort-on-uncaught-exception: PASS, FLAKY
# https://github.com/nodejs/node/issues/54346
test-esm-loader-hooks-inspect-wait: PASS, FLAKY
test-fs-promises-watch-iterator: SKIP
# https://github.com/nodejs/node/issues/50050
test-tick-processor-arguments: SKIP
# https://github.com/nodejs/node/issues/54534
Expand All @@ -85,6 +86,7 @@ test-runner-run-watch: PASS, FLAKY
[$system==freebsd]
# https://github.com/nodejs/node/issues/54346
test-esm-loader-hooks-inspect-wait: PASS, FLAKY
test-fs-promises-watch-iterator: SKIP

[$system==aix]
# https://github.com/nodejs/node/issues/54346
Expand All @@ -95,6 +97,7 @@ test-esm-loader-hooks-inspect-wait: PASS, FLAKY
test-child-process-fork-net-server: SKIP
test-cli-node-options: SKIP
test-cluster-shared-leak: SKIP
test-fs-promises-watch-iterator: SKIP
test-http-writable-true-after-close: SKIP
test-http2-connect-method: SKIP
test-net-error-twice: SKIP
Expand Down
57 changes: 57 additions & 0 deletions test/parallel/test-fs-promises-watch-iterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';
// This tests that when there is a burst of fs watch events, the events
// emitted after the consumer receives the initial event and before the
// control returns back to fs.watch() can be queued up and show up
// in the next iteration.
const common = require('../common');
const { watch, writeFile } = require('fs/promises');
const fs = require('fs');
const assert = require('assert');
const { join } = require('path');
const { setTimeout } = require('timers/promises');
const tmpdir = require('../common/tmpdir');

class WatchTestCase {
constructor(dirName, files) {
this.dirName = dirName;
this.files = files;
}
get dirPath() { return tmpdir.resolve(this.dirName); }
filePath(fileName) { return join(this.dirPath, fileName); }

async run() {
await Promise.all([this.watchFiles(), this.writeFiles()]);
assert(!this.files.length);
}
async watchFiles() {
const watcher = watch(this.dirPath);
for await (const evt of watcher) {
const idx = this.files.indexOf(evt.filename);
if (idx < 0) continue;
this.files.splice(idx, 1);
await setTimeout(common.platformTimeout(100));
if (!this.files.length) break;
}
}
async writeFiles() {
for (const fileName of [...this.files]) {
await writeFile(this.filePath(fileName), Date.now() + fileName.repeat(1e4));
}
await setTimeout(common.platformTimeout(100));
}
}

const kCases = [
// Watch on a directory should callback with a filename on supported systems
new WatchTestCase(
'watch1',
['foo', 'bar', 'baz']
),
];

tmpdir.refresh();

for (const testCase of kCases) {
fs.mkdirSync(testCase.dirPath);
testCase.run().then(common.mustCall());
}
19 changes: 19 additions & 0 deletions test/parallel/test-fs-promises-watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ assert.rejects(
},
{ code: 'ERR_INVALID_ARG_TYPE' }).then(common.mustCall());

assert.rejects(
async () => {
// eslint-disable-next-line no-unused-vars, no-empty
for await (const _ of watch('', { maxQueue: 'silly' })) { }
},
{ code: 'ERR_INVALID_ARG_TYPE' }).then(common.mustCall());
assert.rejects(
async () => {
// eslint-disable-next-line no-unused-vars, no-empty
for await (const _ of watch('', { overflow: 1 })) { }
},
{ code: 'ERR_INVALID_ARG_VALUE' }).then(common.mustCall());
assert.rejects(
async () => {
// eslint-disable-next-line no-unused-vars, no-empty
for await (const _ of watch('', { overflow: 'barf' })) { }
},
{ code: 'ERR_INVALID_ARG_VALUE' }).then(common.mustCall());

(async () => {
const ac = new AbortController();
const { signal } = ac;
Expand Down