Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 24 additions & 2 deletions ext/node/polyfills/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3135,7 +3135,28 @@ type watchOptions = {
encoding?: string;
};

type watchListener = (eventType: string, filename: string) => void;
type watchListener = (
eventType: string,
filename: string | Buffer,
) => void;

// Match Node: `encoding: 'buffer'` returns a Buffer, any other named encoding
// returns the filename re-encoded from utf8. Default ('utf8' or absent) leaves
// the string unchanged. https://github.com/nodejs/node/blob/main/lib/internal/fs/watchers.js
function encodeWatchFilename(
filename: string,
encoding: string | undefined,
): string | Buffer {
if (!encoding || encoding === "utf8" || encoding === "utf-8") {
return filename;
}
const asBuffer = Buffer.from(filename);
if (encoding === "buffer") {
return asBuffer;
}
// deno-lint-ignore prefer-primordials
return asBuffer.toString(encoding as BufferEncoding);
}

function watch(
filename: string | URL,
Expand Down Expand Up @@ -3171,6 +3192,7 @@ function watch(
const watchPath = getValidatedPath(filename).toString();

const recursive = options?.recursive || false;
const encoding = options?.encoding;
const iterator: Deno.FsWatcher = Deno.watchFs(watchPath, {
recursive,
});
Expand All @@ -3190,7 +3212,7 @@ function watch(
fsWatcher.emit(
"change",
convertDenoFsEventToNodeFsEvent(val.kind),
filename,
encodeWatchFilename(filename, encoding),
);
}, (e) => {
fsWatcher.emit("error", e);
Expand Down
1 change: 1 addition & 0 deletions tests/node_compat/config.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,7 @@
"parallel/test-fs-util-validateoffsetlength.js": {},
"parallel/test-fs-utimes-y2K38.js": {},
"parallel/test-fs-utimes.js": {},
"parallel/test-fs-watch-encoding.js": {},
"parallel/test-fs-watch-file-enoent-after-deletion.js": {},
"parallel/test-fs-watch-recursive-add-file-to-existing-subfolder.js": {},
"parallel/test-fs-watch-recursive-add-folder.js": {},
Expand Down