-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
4 changed files
with
172 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,154 @@ | ||
'use strict'; | ||
|
||
const { EventEmitter } = require('events'); | ||
const path = require('path'); | ||
const { Symbol, ObjectKeys } = primordials; | ||
|
||
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 = {}) { | ||
const { stat, readdir } = lazyLoadFsPromises(); | ||
|
||
files[dir] = await stat(dir); | ||
|
||
try { | ||
const directoryFiles = await readdir(dir); | ||
|
||
for (const file of directoryFiles) { | ||
const f = path.join(dir, file); | ||
|
||
try { | ||
const stats = await stat(f); | ||
|
||
files[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 = {}; | ||
|
||
/** | ||
* @param {{ | ||
* persistent?: boolean; | ||
* recursive?: boolean; | ||
* encoding?: string; | ||
* signal?: AbortSignal; | ||
* }} [options] | ||
*/ | ||
constructor(options) { | ||
super(); | ||
|
||
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 { readdir } = lazyLoadFsPromises(); | ||
const { stat, watchFile } = lazyLoadFsSync(); | ||
|
||
watchFile(file, this.#options, (event, payload) => { | ||
const existingStat = this.#files[file]; | ||
|
||
if (existingStat && !existingStat.isDirectory() && | ||
event.nlink !== 0 && existingStat.mtime.getTime() === event.mtime.getTime()) { | ||
return; | ||
} | ||
|
||
this.#files[file] = event; | ||
|
||
if (!event.isDirectory()) { | ||
this.emit(event, payload); | ||
} else { | ||
readdir(file) | ||
.then((files) => { | ||
for (const subfile of files) { | ||
const f = path.join(file, subfile); | ||
|
||
if (!this.#files[f]) { | ||
stat(f, (error, stat) => { | ||
if (error) { | ||
return; | ||
} | ||
|
||
this.#files[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); | ||
} | ||
} | ||
|
||
/** | ||
* @param {string} name | ||
* @param {Function=} callback | ||
*/ | ||
addEventListener(name, callback) { | ||
this.on(name, (...args) => callback(...args)); | ||
} | ||
} | ||
|
||
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