handler.d.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import type { WatchEventType, Stats, FSWatcher as NativeFsWatcher } from 'fs';
  2. import type { FSWatcher, WatchHelper, Throttler } from './index.js';
  3. import type { EntryInfo } from 'readdirp';
  4. export type Path = string;
  5. export declare const STR_DATA = "data";
  6. export declare const STR_END = "end";
  7. export declare const STR_CLOSE = "close";
  8. export declare const EMPTY_FN: () => void;
  9. export declare const IDENTITY_FN: (val: unknown) => unknown;
  10. export declare const isWindows: boolean;
  11. export declare const isMacos: boolean;
  12. export declare const isLinux: boolean;
  13. export declare const isFreeBSD: boolean;
  14. export declare const isIBMi: boolean;
  15. export declare const EVENTS: {
  16. readonly ALL: "all";
  17. readonly READY: "ready";
  18. readonly ADD: "add";
  19. readonly CHANGE: "change";
  20. readonly ADD_DIR: "addDir";
  21. readonly UNLINK: "unlink";
  22. readonly UNLINK_DIR: "unlinkDir";
  23. readonly RAW: "raw";
  24. readonly ERROR: "error";
  25. };
  26. export type EventName = (typeof EVENTS)[keyof typeof EVENTS];
  27. export type FsWatchContainer = {
  28. listeners: (path: string) => void | Set<any>;
  29. errHandlers: (err: unknown) => void | Set<any>;
  30. rawEmitters: (ev: WatchEventType, path: string, opts: unknown) => void | Set<any>;
  31. watcher: NativeFsWatcher;
  32. watcherUnusable?: boolean;
  33. };
  34. export interface WatchHandlers {
  35. listener: (path: string) => void;
  36. errHandler: (err: unknown) => void;
  37. rawEmitter: (ev: WatchEventType, path: string, opts: unknown) => void;
  38. }
  39. /**
  40. * @mixin
  41. */
  42. export declare class NodeFsHandler {
  43. fsw: FSWatcher;
  44. _boundHandleError: (error: unknown) => void;
  45. constructor(fsW: FSWatcher);
  46. /**
  47. * Watch file for changes with fs_watchFile or fs_watch.
  48. * @param path to file or dir
  49. * @param listener on fs change
  50. * @returns closer for the watcher instance
  51. */
  52. _watchWithNodeFs(path: string, listener: (path: string, newStats?: any) => void | Promise<void>): (() => void) | undefined;
  53. /**
  54. * Watch a file and emit add event if warranted.
  55. * @returns closer for the watcher instance
  56. */
  57. _handleFile(file: Path, stats: Stats, initialAdd: boolean): (() => void) | undefined;
  58. /**
  59. * Handle symlinks encountered while reading a dir.
  60. * @param entry returned by readdirp
  61. * @param directory path of dir being read
  62. * @param path of this item
  63. * @param item basename of this item
  64. * @returns true if no more processing is needed for this entry.
  65. */
  66. _handleSymlink(entry: EntryInfo, directory: string, path: Path, item: string): Promise<boolean | undefined>;
  67. _handleRead(directory: string, initialAdd: boolean, wh: WatchHelper, target: Path, dir: Path, depth: number, throttler: Throttler): Promise<unknown> | undefined;
  68. /**
  69. * Read directory to add / remove files from `@watched` list and re-read it on change.
  70. * @param dir fs path
  71. * @param stats
  72. * @param initialAdd
  73. * @param depth relative to user-supplied path
  74. * @param target child path targeted for watch
  75. * @param wh Common watch helpers for this path
  76. * @param realpath
  77. * @returns closer for the watcher instance.
  78. */
  79. _handleDir(dir: string, stats: Stats, initialAdd: boolean, depth: number, target: string, wh: WatchHelper, realpath: string): Promise<(() => void) | undefined>;
  80. /**
  81. * Handle added file, directory, or glob pattern.
  82. * Delegates call to _handleFile / _handleDir after checks.
  83. * @param path to file or ir
  84. * @param initialAdd was the file added at watch instantiation?
  85. * @param priorWh depth relative to user-supplied path
  86. * @param depth Child path actually targeted for watch
  87. * @param target Child path actually targeted for watch
  88. */
  89. _addToNodeFs(path: string, initialAdd: boolean, priorWh: WatchHelper | undefined, depth: number, target?: string): Promise<string | false | undefined>;
  90. }