(filename, options = kEmptyObject)
| 378 | let kResistStopPropagation; |
| 379 | |
| 380 | async function* watch(filename, options = kEmptyObject) { |
| 381 | const path = toNamespacedPath(getValidatedPath(filename)); |
| 382 | validateObject(options, 'options'); |
| 383 | |
| 384 | const { |
| 385 | persistent = true, |
| 386 | recursive = false, |
| 387 | encoding = 'utf8', |
| 388 | maxQueue = 2048, |
| 389 | overflow = 'ignore', |
| 390 | signal, |
| 391 | ignore, |
| 392 | } = options; |
| 393 | |
| 394 | validateBoolean(persistent, 'options.persistent'); |
| 395 | validateBoolean(recursive, 'options.recursive'); |
| 396 | validateInteger(maxQueue, 'options.maxQueue'); |
| 397 | validateOneOf(overflow, 'options.overflow', ['ignore', 'error']); |
| 398 | validateAbortSignal(signal, 'options.signal'); |
| 399 | validateIgnoreOption(ignore, 'options.ignore'); |
| 400 | |
| 401 | if (encoding && !isEncoding(encoding)) { |
| 402 | const reason = 'is invalid encoding'; |
| 403 | throw new ERR_INVALID_ARG_VALUE('encoding', encoding, reason); |
| 404 | } |
| 405 | |
| 406 | if (signal?.aborted) |
| 407 | throw new AbortError(undefined, { cause: signal.reason }); |
| 408 | |
| 409 | const handle = new FSEvent(); |
| 410 | const ignoreMatcher = createIgnoreMatcher(ignore); |
| 411 | let { promise, resolve } = PromiseWithResolvers(); |
| 412 | const queue = []; |
| 413 | const oncancel = () => { |
| 414 | handle.close(); |
| 415 | resolve(); |
| 416 | }; |
| 417 | |
| 418 | try { |
| 419 | if (signal) { |
| 420 | kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation; |
| 421 | signal.addEventListener('abort', oncancel, { __proto__: null, once: true, [kResistStopPropagation]: true }); |
| 422 | } |
| 423 | handle.onchange = (status, eventType, filename) => { |
| 424 | if (status < 0) { |
| 425 | const error = new UVException({ |
| 426 | errno: status, |
| 427 | syscall: 'watch', |
| 428 | path: filename, |
| 429 | }); |
| 430 | error.filename = filename; |
| 431 | handle.close(); |
| 432 | ArrayPrototypePush(queue, error); |
| 433 | resolve(); |
| 434 | return; |
| 435 | } |
| 436 | // Filter events if ignore matcher is set and filename is available |
| 437 | if (filename != null && ignoreMatcher?.(filename)) { |
no test coverage detected