MCPcopy Create free account
hub / github.com/nodejs/node / watch

Function watch

lib/internal/fs/watchers.js:380–485  ·  view source on GitHub ↗
(filename, options = kEmptyObject)

Source from the content-addressed store, hash-verified

378let kResistStopPropagation;
379
380async 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)) {

Callers 2

_watchFunction · 0.70
#watchFileMethod · 0.70

Calls 9

toNamespacedPathFunction · 0.85
validateAbortSignalFunction · 0.85
createIgnoreMatcherFunction · 0.85
addEventListenerMethod · 0.65
closeMethod · 0.65
removeEventListenerMethod · 0.65
requireFunction · 0.50
resolveFunction · 0.50
startMethod · 0.45

Tested by

no test coverage detected