Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
220 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
'use strict'; | ||
|
||
const { EventEmitter } = require('events'); | ||
const path = require('path'); | ||
const { SafeMap, Symbol, ObjectKeys } = primordials; | ||
const { validateObject } = require('internal/validators'); | ||
|
||
const kFSWatchStart = Symbol('kFSWatchStart'); | ||
|
||
let internalSync; | ||
let internalPromises; | ||
|
||
function lazyLoadFsPromises() { | ||
internalPromises ??= require('fs/promises'); | ||
return internalPromises; | ||
} | ||
|
||
function lazyLoadFsSync() { | ||
internalSync ??= require('fs'); | ||
return internalSync; | ||
} | ||
|
||
async function traverse(dir, files = new SafeMap()) { | ||
const { stat, opendir } = lazyLoadFsPromises(); | ||
|
||
files.set(dir, await stat(dir)); | ||
|
||
try { | ||
const directory = await opendir(dir); | ||
|
||
for await (const file of directory) { | ||
const f = path.join(dir, file); | ||
|
||
try { | ||
const stats = await stat(f); | ||
|
||
files.set(f, stats); | ||
|
||
if (stats.isDirectory()) { | ||
await traverse(f, files); | ||
} | ||
} catch (error) { | ||
if (error.code !== 'ENOENT' || error.code !== 'EPERM') { | ||
throw error; | ||
} | ||
} | ||
|
||
} | ||
} catch (error) { | ||
if (error.code !== 'EACCES') { | ||
throw error; | ||
} | ||
} | ||
|
||
return files; | ||
} | ||
|
||
class FSWatcher extends EventEmitter { | ||
#options = null; | ||
#closed = false; | ||
#files = new SafeMap(); | ||
|
||
/** | ||
* @param {{ | ||
* persistent?: boolean; | ||
* recursive?: boolean; | ||
* encoding?: string; | ||
* signal?: AbortSignal; | ||
* }} [options] | ||
*/ | ||
constructor(options) { | ||
super(); | ||
|
||
validateObject(options, 'options'); | ||
this.#options = options; | ||
} | ||
|
||
async close() { | ||
const { unwatchFile } = lazyLoadFsPromises(); | ||
this.#closed = true; | ||
|
||
for (const file of ObjectKeys(this.#files)) { | ||
await unwatchFile(file); | ||
} | ||
|
||
this.emit('close'); | ||
} | ||
|
||
/** | ||
* @param {string} file | ||
*/ | ||
#watchFile(file) { | ||
const { opendir } = lazyLoadFsPromises(); | ||
const { stat, watchFile } = lazyLoadFsSync(); | ||
|
||
watchFile(file, this.#options, (event, payload) => { | ||
const existingStat = this.#files.get(file); | ||
|
||
if (existingStat && !existingStat.isDirectory() && | ||
event.nlink !== 0 && existingStat.mtime.getTime() === event.mtime.getTime()) { | ||
return; | ||
} | ||
|
||
this.#files.set(file, event); | ||
|
||
if (!event.isDirectory()) { | ||
this.emit(event, payload); | ||
} else { | ||
opendir(file) | ||
.then(async (files) => { | ||
for await (const subfile of files) { | ||
const f = path.join(file, subfile); | ||
|
||
if (!this.#files.has(f)) { | ||
stat(f, (error, stat) => { | ||
if (error) { | ||
return; | ||
} | ||
|
||
this.#files.set(f, stat); | ||
this.#watchFile(f); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @param {string | Buffer | URL} filename | ||
*/ | ||
async [kFSWatchStart](filename) { | ||
this.#closed = false; | ||
this.#files = await traverse(filename); | ||
|
||
this.#watchFile(filename); | ||
|
||
for (const f in this.#files) { | ||
this.#watchFile(f); | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
FSWatcher, | ||
kFSWatchStart, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
// Only run these tests on Linux. | ||
// TODO: Uncomment this | ||
// if (!common.isLinux) { | ||
// return; | ||
// } | ||
|
||
const assert = require('assert'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
|
||
const testDir = tmpdir.path; | ||
const filenameOne = 'watch.txt'; | ||
|
||
tmpdir.refresh(); | ||
|
||
const testsubdir = fs.mkdtempSync(testDir + path.sep); | ||
const relativePathOne = path.join(path.basename(testsubdir), filenameOne); | ||
const filepathOne = path.join(testsubdir, filenameOne); | ||
|
||
const watcher = fs.watch(testDir, { recursive: true }); | ||
|
||
let watcherClosed = false; | ||
watcher.on('change', function(event, filename) { | ||
assert.ok(event === 'change' || event === 'rename'); | ||
|
||
// Ignore stale events generated by mkdir and other tests | ||
if (filename !== relativePathOne) | ||
return; | ||
|
||
if (common.isOSX) { | ||
clearInterval(interval); | ||
} | ||
watcher.close(); | ||
watcherClosed = true; | ||
}); | ||
|
||
let interval; | ||
if (common.isOSX) { | ||
interval = setInterval(function() { | ||
fs.writeFileSync(filepathOne, 'world'); | ||
}, 10); | ||
} else { | ||
fs.writeFileSync(filepathOne, 'world'); | ||
} | ||
|
||
process.on('exit', function() { | ||
assert(watcherClosed, 'watcher Object was not closed'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters