|
| 1 | +import { EventEmitter } from 'node:events' |
| 2 | +import { lstatSync, statSync, type Stats } from 'node:fs' |
| 3 | +import { relative, resolve, sep } from 'node:path' |
| 4 | +import { |
| 5 | + createWatcherApi, |
| 6 | + type NotifyBindings, |
| 7 | + type Watcher, |
| 8 | + type Ignored, |
| 9 | + type WatcherEvent, |
| 10 | + type WatchMatcher, |
| 11 | +} from './factory.js' |
| 12 | + |
| 13 | +export type WatchEventName = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir' |
| 14 | +export type { Ignored, WatchMatcher } |
| 15 | + |
| 16 | +export interface NativeWatchEvent { |
| 17 | + kind: string |
| 18 | + path?: string |
| 19 | + isDir?: boolean |
| 20 | + error?: string |
| 21 | +} |
| 22 | + |
| 23 | +export interface AwaitWriteFinishOptions { |
| 24 | + stabilityThreshold?: number |
| 25 | + pollInterval?: number |
| 26 | +} |
| 27 | + |
| 28 | +export interface WatchOptions { |
| 29 | + ignored?: Ignored |
| 30 | + followSymlinks?: boolean |
| 31 | + cwd?: string |
| 32 | + usePolling?: boolean |
| 33 | + interval?: number |
| 34 | + ignorePermissionErrors?: boolean |
| 35 | + persistent?: boolean |
| 36 | + disableGlobbing?: boolean |
| 37 | + useFsEvents?: boolean |
| 38 | + alwaysStat?: boolean |
| 39 | + depth?: number |
| 40 | + binaryInterval?: number |
| 41 | + atomic?: boolean | number |
| 42 | + awaitWriteFinish?: AwaitWriteFinishOptions | boolean |
| 43 | +} |
| 44 | + |
| 45 | +const toArray = <T>(value: T | ReadonlyArray<T>): T[] => |
| 46 | + Array.isArray(value) ? [...value] : [value as T] |
| 47 | +const normalize = (value: string): string => value.replaceAll(sep, '/') |
| 48 | + |
| 49 | +export interface FSWatcher extends EventEmitter { |
| 50 | + readonly options: WatchOptions |
| 51 | + closed: boolean |
| 52 | + add(paths: string | ReadonlyArray<string>): this |
| 53 | + unwatch(paths: string | ReadonlyArray<string>): this |
| 54 | + isWatched(path: string): boolean |
| 55 | + ref(): this |
| 56 | + unref(): this |
| 57 | + close(): Promise<void> |
| 58 | + on(event: 'add' | 'addDir' | 'change', listener: (path: string, stats?: Stats) => void): this |
| 59 | + on(event: 'all', listener: (event: WatchEventName, path: string, stats?: Stats) => void): this |
| 60 | + on(event: 'error', listener: (error: Error) => void): this |
| 61 | + on(event: 'raw', listener: (event: string, path: string, details: NativeWatchEvent) => void): this |
| 62 | + on(event: 'ready', listener: () => void): this |
| 63 | + on(event: 'unlink' | 'unlinkDir', listener: (path: string) => void): this |
| 64 | + on(event: string, listener: (...args: any[]) => void): this |
| 65 | +} |
| 66 | + |
| 67 | +export interface ChokidarApi { |
| 68 | + FSWatcher: new (options?: WatchOptions) => FSWatcher |
| 69 | + watch: (paths: string | ReadonlyArray<string>, options?: WatchOptions) => FSWatcher |
| 70 | +} |
| 71 | + |
| 72 | +/** Create the Chokidar-compatible API using an addon's native bindings. */ |
| 73 | +export function createChokidarApi(binding: NotifyBindings): ChokidarApi { |
| 74 | + const { Watcher } = /* #__PURE__ */ createWatcherApi(binding) |
| 75 | + |
| 76 | + class FSWatcher extends EventEmitter { |
| 77 | + readonly options: WatchOptions |
| 78 | + closed = false |
| 79 | + private readonly cwd: string |
| 80 | + private readonly watcher: Watcher |
| 81 | + |
| 82 | + constructor(options: WatchOptions = {}) { |
| 83 | + super() |
| 84 | + this.options = options |
| 85 | + this.cwd = resolve(options.cwd ?? process.cwd()) |
| 86 | + this.watcher = new Watcher((event) => this.handle(event), { |
| 87 | + usePolling: options.usePolling, |
| 88 | + pollInterval: options.interval ?? 100, |
| 89 | + followSymlinks: options.followSymlinks, |
| 90 | + ignored: options.ignored, |
| 91 | + cwd: this.cwd, |
| 92 | + }) |
| 93 | + if (options.persistent === false) this.watcher.unref() |
| 94 | + |
| 95 | + setImmediate(() => { |
| 96 | + if (!this.closed) this.emit('ready') |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + add(paths: string | ReadonlyArray<string>): this { |
| 101 | + if (this.closed) return this |
| 102 | + this.watcher.add(toArray(paths).map((path) => this.absolute(path))) |
| 103 | + return this |
| 104 | + } |
| 105 | + |
| 106 | + unwatch(paths: string | ReadonlyArray<string>): this { |
| 107 | + if (!this.closed) { |
| 108 | + this.watcher.remove(toArray(paths).map((path) => this.absolute(path))) |
| 109 | + } |
| 110 | + return this |
| 111 | + } |
| 112 | + |
| 113 | + /** Checks configured watch coverage; resolves paths against cwd. */ |
| 114 | + isWatched(path: string): boolean { |
| 115 | + return !this.closed && this.watcher.isWatched(this.absolute(path)) |
| 116 | + } |
| 117 | + |
| 118 | + ref(): this { |
| 119 | + this.watcher.ref() |
| 120 | + return this |
| 121 | + } |
| 122 | + |
| 123 | + unref(): this { |
| 124 | + this.watcher.unref() |
| 125 | + return this |
| 126 | + } |
| 127 | + |
| 128 | + async close(): Promise<void> { |
| 129 | + if (this.closed) return |
| 130 | + this.closed = true |
| 131 | + this.watcher.close() |
| 132 | + this.removeAllListeners() |
| 133 | + } |
| 134 | + |
| 135 | + private absolute(path: string): string { |
| 136 | + return resolve(this.cwd, path) |
| 137 | + } |
| 138 | + |
| 139 | + private display(path: string): string { |
| 140 | + if (!this.options.cwd) return path |
| 141 | + return normalize(relative(this.cwd, path)) || '.' |
| 142 | + } |
| 143 | + |
| 144 | + private stats(path: string): Stats | undefined { |
| 145 | + try { |
| 146 | + const stat = this.options.followSymlinks === false ? lstatSync : statSync |
| 147 | + return stat(path, { throwIfNoEntry: false }) |
| 148 | + } catch (error) { |
| 149 | + if (!this.ignoreError(error)) this.reportError(error) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private ignoreError(error: unknown): boolean { |
| 154 | + const code = error instanceof Error && 'code' in error ? error.code : undefined |
| 155 | + const message = error instanceof Error ? error.message : String(error) |
| 156 | + return this.options.ignorePermissionErrors === true && ( |
| 157 | + code === 'EACCES' || code === 'EPERM' || |
| 158 | + /EACCES|EPERM|permission denied|operation not permitted/i.test(message) |
| 159 | + ) |
| 160 | + } |
| 161 | + |
| 162 | + private reportError(error: unknown): void { |
| 163 | + queueMicrotask(() => { |
| 164 | + if (!this.closed) this.emit('error', error) |
| 165 | + }) |
| 166 | + } |
| 167 | + |
| 168 | + private handle(event: WatcherEvent): void { |
| 169 | + if (this.closed) return |
| 170 | + if (event.kind === 'error') { |
| 171 | + const error = new Error(event.error) |
| 172 | + if (event.path) Object.assign(error, { path: event.path }) |
| 173 | + if (!this.ignoreError(error)) this.emit('error', error) |
| 174 | + return |
| 175 | + } |
| 176 | + if (!event.path) return |
| 177 | + const stats = event.kind === 'unlink' || event.kind === 'unlinkDir' |
| 178 | + ? undefined |
| 179 | + : this.stats(event.path) |
| 180 | + const path = this.display(event.path) |
| 181 | + this.emit(event.kind, path, stats) |
| 182 | + this.emit('all', event.kind, path, stats) |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + function watch( |
| 187 | + paths: string | ReadonlyArray<string>, |
| 188 | + options?: WatchOptions, |
| 189 | + ): FSWatcher { |
| 190 | + const watcher = new FSWatcher(options) |
| 191 | + try { |
| 192 | + return watcher.add(paths) |
| 193 | + } catch (error) { |
| 194 | + void watcher.close() |
| 195 | + throw error |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return { FSWatcher, watch } |
| 200 | +} |
0 commit comments