volume.d.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. import { Link, Superblock, DirectoryJSON, NestedDirectoryJSON, TFileId, type IProcess } from '@jsonjoy.com/fs-core';
  2. import Stats from './Stats';
  3. import Dirent from './Dirent';
  4. import StatFs from './StatFs';
  5. import { Buffer } from '@jsonjoy.com/fs-node-builtins/lib/internal/buffer';
  6. import { TSetTimeout } from '@jsonjoy.com/fs-node-utils/lib/setTimeoutUnref';
  7. import { Writable } from '@jsonjoy.com/fs-node-builtins/lib/stream';
  8. import { constants, TDataOut } from '@jsonjoy.com/fs-node-utils';
  9. import { EventEmitter } from '@jsonjoy.com/fs-node-builtins/lib/events';
  10. import * as misc from '@jsonjoy.com/fs-node-utils/lib/types/misc';
  11. import * as opts from '@jsonjoy.com/fs-node-utils/lib/types/options';
  12. import { FsCallbackApi, WritevCallback } from '@jsonjoy.com/fs-node-utils/lib/types/FsCallbackApi';
  13. import { ToTreeOptions } from '@jsonjoy.com/fs-print';
  14. import * as fsSnapshot from '@jsonjoy.com/fs-snapshot';
  15. import type { PathLike, symlink } from '@jsonjoy.com/fs-node-utils/lib/types/misc';
  16. import type { FsPromisesApi, FsSynchronousApi } from '@jsonjoy.com/fs-node-utils';
  17. import { Dir } from './Dir';
  18. export type { SnapshotNode } from '@jsonjoy.com/fs-snapshot';
  19. export interface IError extends Error {
  20. code?: string;
  21. }
  22. export type TData = TDataOut | ArrayBufferView | DataView;
  23. export type TFlags = string | number;
  24. export type TMode = string | number;
  25. export type TTime = number | string | Date;
  26. export type TFlagsCopy = typeof constants.COPYFILE_EXCL | typeof constants.COPYFILE_FICLONE | typeof constants.COPYFILE_FICLONE_FORCE;
  27. export interface IAppendFileOptions extends opts.IFileOptions {
  28. }
  29. export type IWatchFileOptions = opts.IWatchFileOptions;
  30. export type IWatchOptions = opts.IWatchOptions;
  31. export declare function pathToSteps(path: PathLike): string[];
  32. export declare function dataToStr(data: TData, encoding?: BufferEncoding): string;
  33. export declare function toUnixTimestamp(time: any): any;
  34. /**
  35. * `Volume` represents a file system.
  36. */
  37. export declare class Volume implements FsCallbackApi, FsSynchronousApi {
  38. readonly _core: Superblock;
  39. static readonly fromJSON: (json: DirectoryJSON, cwd?: string, opts?: {
  40. process?: IProcess;
  41. }) => Volume;
  42. static readonly fromNestedJSON: (json: NestedDirectoryJSON, cwd?: string, opts?: {
  43. process?: IProcess;
  44. }) => Volume;
  45. StatWatcher: new () => StatWatcher;
  46. ReadStream: new (...args: any[]) => misc.IReadStream;
  47. WriteStream: new (...args: any[]) => IWriteStream;
  48. FSWatcher: new () => FSWatcher;
  49. realpath: {
  50. (path: PathLike, callback: misc.TCallback<TDataOut>): void;
  51. (path: PathLike, options: opts.IRealpathOptions | string, callback: misc.TCallback<TDataOut>): void;
  52. native: {
  53. (path: PathLike, callback: misc.TCallback<TDataOut>): void;
  54. (path: PathLike, options: opts.IRealpathOptions | string, callback: misc.TCallback<TDataOut>): void;
  55. };
  56. };
  57. realpathSync: {
  58. (path: PathLike, options?: opts.IRealpathOptions | string): TDataOut;
  59. native: (path: PathLike, options?: opts.IRealpathOptions | string) => TDataOut;
  60. };
  61. private promisesApi;
  62. get promises(): FsPromisesApi;
  63. constructor(_core?: Superblock);
  64. private wrapAsync;
  65. reset(): void;
  66. toJSON(paths?: PathLike | PathLike[], json?: {}, isRelative?: boolean, asBuffer?: boolean): DirectoryJSON<string | null>;
  67. fromJSON(json: DirectoryJSON, cwd?: string): void;
  68. fromNestedJSON(json: NestedDirectoryJSON, cwd?: string): void;
  69. mountSync(mountpoint: string, json: DirectoryJSON): void;
  70. openSync: (path: PathLike, flags: TFlags, mode?: TMode) => number;
  71. open: {
  72. (path: PathLike, flags: TFlags, /* ... */ callback: misc.TCallback<number>): void;
  73. (path: PathLike, flags: TFlags, mode: TMode, callback: misc.TCallback<number>): void;
  74. };
  75. closeSync: (fd: number) => void;
  76. close: (fd: number, callback: misc.TCallback<void>) => void;
  77. readSync: (fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, position: number | null) => number;
  78. read: (fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, position: number | null, callback: (err?: Error | null, bytesRead?: number, buffer?: Buffer | ArrayBufferView | DataView) => void) => void;
  79. readv: {
  80. (fd: number, buffers: ArrayBufferView[], callback: misc.TCallback2<number, ArrayBufferView[]>): void;
  81. (fd: number, buffers: ArrayBufferView[], position: number | null, callback: misc.TCallback2<number, ArrayBufferView[]>): void;
  82. };
  83. readvSync: (fd: number, buffers: ArrayBufferView[], position?: number | null) => number;
  84. private readonly _readfile;
  85. readFileSync: (file: TFileId, options?: opts.IReadFileOptions | string) => TDataOut;
  86. readFile: {
  87. (id: TFileId, callback: misc.TCallback<TDataOut>): any;
  88. (id: TFileId, options: opts.IReadFileOptions | string, callback: misc.TCallback<TDataOut>): any;
  89. };
  90. private _write;
  91. writeSync: {
  92. (fd: number, buffer: Buffer | ArrayBufferView | DataView, offset?: number, length?: number, position?: number | null): number;
  93. (fd: number, str: string, position?: number, encoding?: BufferEncoding): number;
  94. };
  95. write: {
  96. (fd: number, buffer: Buffer | ArrayBufferView | DataView, callback: (...args: any[]) => void): any;
  97. (fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, callback: (...args: any[]) => void): any;
  98. (fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, callback: (...args: any[]) => void): any;
  99. (fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, position: number, callback: (...args: any[]) => void): any;
  100. (fd: number, str: string, callback: (...args: any[]) => void): any;
  101. (fd: number, str: string, position: number, callback: (...args: any[]) => void): any;
  102. (fd: number, str: string, position: number, encoding: BufferEncoding, callback: (...args: any[]) => void): any;
  103. };
  104. private writevBase;
  105. writev: {
  106. (fd: number, buffers: ArrayBufferView[], callback: WritevCallback): void;
  107. (fd: number, buffers: ArrayBufferView[], position: number | null, callback: WritevCallback): void;
  108. };
  109. writevSync: (fd: number, buffers: ArrayBufferView[], position?: number | null) => number;
  110. writeFileSync: (id: TFileId, data: TData, options?: opts.IWriteFileOptions) => void;
  111. writeFile: {
  112. (id: TFileId, data: TData, callback: misc.TCallback<void>): void;
  113. (id: TFileId, data: TData, options: opts.IWriteFileOptions | string, callback: misc.TCallback<void>): void;
  114. };
  115. private _copyFile;
  116. copyFileSync: (src: PathLike, dest: PathLike, flags?: TFlagsCopy) => void;
  117. copyFile: {
  118. (src: PathLike, dest: PathLike, callback: misc.TCallback<void>): any;
  119. (src: PathLike, dest: PathLike, flags: TFlagsCopy, callback: misc.TCallback<void>): any;
  120. };
  121. private readonly _cp;
  122. private isSrcSubdir;
  123. private cpFileSync;
  124. private cpDirSync;
  125. private cpSymlinkSync;
  126. linkSync: (existingPath: PathLike, newPath: PathLike) => void;
  127. link: (existingPath: PathLike, newPath: PathLike, callback: misc.TCallback<void>) => void;
  128. unlinkSync: (path: PathLike) => void;
  129. unlink: (path: PathLike, callback: misc.TCallback<void>) => void;
  130. /**
  131. * `type` argument works only on Windows.
  132. * @param target
  133. * @param path
  134. * @param type
  135. */
  136. symlinkSync: (target: PathLike, path: PathLike, type?: symlink.Type) => void;
  137. symlink: {
  138. (target: PathLike, path: PathLike, callback: misc.TCallback<void>): any;
  139. (target: PathLike, path: PathLike, type: symlink.Type, callback: misc.TCallback<void>): any;
  140. };
  141. private readonly _lstat;
  142. lstatSync: {
  143. (path: PathLike): Stats<number>;
  144. (path: PathLike, options: {
  145. throwIfNoEntry?: true | undefined;
  146. }): Stats<number>;
  147. (path: PathLike, options: {
  148. bigint: false;
  149. throwIfNoEntry?: true | undefined;
  150. }): Stats<number>;
  151. (path: PathLike, options: {
  152. bigint: true;
  153. throwIfNoEntry?: true | undefined;
  154. }): Stats<bigint>;
  155. (path: PathLike, options: {
  156. throwIfNoEntry: false;
  157. }): Stats<number> | undefined;
  158. (path: PathLike, options: {
  159. bigint: false;
  160. throwIfNoEntry: false;
  161. }): Stats<number> | undefined;
  162. (path: PathLike, options: {
  163. bigint: true;
  164. throwIfNoEntry: false;
  165. }): Stats<bigint> | undefined;
  166. };
  167. lstat(path: PathLike, callback: misc.TCallback<Stats>): void;
  168. lstat(path: PathLike, options: opts.IStatOptions, callback: misc.TCallback<Stats>): void;
  169. private _stat;
  170. private _statOrThrow;
  171. statSync(path: PathLike): Stats<number>;
  172. statSync(path: PathLike, options: {
  173. throwIfNoEntry?: true;
  174. }): Stats<number>;
  175. statSync(path: PathLike, options: {
  176. throwIfNoEntry: false;
  177. }): Stats<number> | undefined;
  178. statSync(path: PathLike, options: {
  179. bigint: false;
  180. throwIfNoEntry?: true;
  181. }): Stats<number>;
  182. statSync(path: PathLike, options: {
  183. bigint: true;
  184. throwIfNoEntry?: true;
  185. }): Stats<bigint>;
  186. statSync(path: PathLike, options: {
  187. bigint: false;
  188. throwIfNoEntry: false;
  189. }): Stats<number> | undefined;
  190. statSync(path: PathLike, options: {
  191. bigint: true;
  192. throwIfNoEntry: false;
  193. }): Stats<bigint> | undefined;
  194. stat(path: PathLike, callback: misc.TCallback<Stats>): void;
  195. stat(path: PathLike, options: opts.IStatOptions, callback: misc.TCallback<Stats>): void;
  196. private fstatBase;
  197. fstatSync(fd: number): Stats<number>;
  198. fstatSync(fd: number, options: {
  199. bigint: false;
  200. }): Stats<number>;
  201. fstatSync(fd: number, options: {
  202. bigint: true;
  203. }): Stats<bigint>;
  204. fstat(fd: number, callback: misc.TCallback<Stats>): void;
  205. fstat(fd: number, options: opts.IFStatOptions, callback: misc.TCallback<Stats>): void;
  206. renameSync: (oldPath: PathLike, newPath: PathLike) => void;
  207. rename: (oldPath: PathLike, newPath: PathLike, callback: misc.TCallback<void>) => void;
  208. private _exists;
  209. existsSync: (path: PathLike) => boolean;
  210. exists: (path: PathLike, callback: (exists: boolean) => void) => void;
  211. private _access;
  212. accessSync: (path: PathLike, mode?: number) => void;
  213. access: {
  214. (path: PathLike, callback: misc.TCallback<void>): any;
  215. (path: PathLike, mode: number, callback: misc.TCallback<void>): any;
  216. };
  217. appendFileSync: (id: TFileId, data: TData, options?: IAppendFileOptions | string) => void;
  218. appendFile: {
  219. (id: TFileId, data: TData, callback: misc.TCallback<void>): any;
  220. (id: TFileId, data: TData, options: IAppendFileOptions | string, callback: misc.TCallback<void>): any;
  221. };
  222. private readonly _readdir;
  223. readdirSync: (path: PathLike, options?: opts.IReaddirOptions | string) => TDataOut[] | Dirent[];
  224. readdir: {
  225. (path: PathLike, callback: misc.TCallback<TDataOut[] | Dirent[]>): any;
  226. (path: PathLike, options: opts.IReaddirOptions | string, callback: misc.TCallback<TDataOut[] | Dirent[]>): any;
  227. };
  228. private readonly _readlink;
  229. readlinkSync: (path: PathLike, options?: opts.IOptions) => TDataOut;
  230. readlink: {
  231. (path: PathLike, callback: misc.TCallback<TDataOut>): any;
  232. (path: PathLike, options: opts.IOptions, callback: misc.TCallback<TDataOut>): any;
  233. };
  234. private readonly _fsync;
  235. fsyncSync: (fd: number) => void;
  236. fsync: (fd: number, callback: misc.TCallback<void>) => void;
  237. private readonly _fdatasync;
  238. fdatasyncSync: (fd: number) => void;
  239. fdatasync: (fd: number, callback: misc.TCallback<void>) => void;
  240. private readonly _ftruncate;
  241. ftruncateSync: (fd: number, len?: number) => void;
  242. ftruncate: {
  243. (fd: number, callback: misc.TCallback<void>): any;
  244. (fd: number, len: number, callback: misc.TCallback<void>): any;
  245. };
  246. private readonly _truncate;
  247. /**
  248. * `id` should be a file descriptor or a path. `id` as file descriptor will
  249. * not be supported soon.
  250. */
  251. truncateSync: (id: TFileId, len?: number) => void;
  252. truncate: {
  253. (id: TFileId, callback: misc.TCallback<void>): any;
  254. (id: TFileId, len: number, callback: misc.TCallback<void>): any;
  255. };
  256. private readonly _futimes;
  257. futimesSync: (fd: number, atime: TTime, mtime: TTime) => void;
  258. futimes: (fd: number, atime: TTime, mtime: TTime, callback: misc.TCallback<void>) => void;
  259. private readonly _utimes;
  260. utimesSync: (path: PathLike, atime: TTime, mtime: TTime) => void;
  261. utimes: (path: PathLike, atime: TTime, mtime: TTime, callback: misc.TCallback<void>) => void;
  262. lutimesSync: (path: PathLike, atime: TTime, mtime: TTime) => void;
  263. lutimes: (path: PathLike, atime: TTime, mtime: TTime, callback: misc.TCallback<void>) => void;
  264. mkdirSync: {
  265. (path: PathLike, options: opts.IMkdirOptions & {
  266. recursive: true;
  267. }): string | undefined;
  268. (path: PathLike, options?: TMode | (opts.IMkdirOptions & {
  269. recursive?: false;
  270. })): void;
  271. (path: PathLike, options?: TMode | opts.IMkdirOptions): string | undefined;
  272. };
  273. mkdir: {
  274. (path: PathLike, callback: misc.TCallback<void>): any;
  275. (path: PathLike, mode: TMode | (opts.IMkdirOptions & {
  276. recursive?: false;
  277. }), callback: misc.TCallback<void>): any;
  278. (path: PathLike, mode: opts.IMkdirOptions & {
  279. recursive: true;
  280. }, callback: misc.TCallback<string>): any;
  281. (path: PathLike, mode: TMode | opts.IMkdirOptions, callback: misc.TCallback<string>): any;
  282. };
  283. private readonly _mkdtemp;
  284. mkdtempSync: (prefix: string, options?: opts.IOptions) => TDataOut;
  285. mkdtemp: {
  286. (prefix: string, callback: misc.TCallback<string>): any;
  287. (prefix: string, options: opts.IOptions, callback: misc.TCallback<string>): any;
  288. };
  289. rmdirSync: (path: PathLike, options?: opts.IRmdirOptions) => void;
  290. rmdir: {
  291. (path: PathLike, callback: misc.TCallback<void>): any;
  292. (path: PathLike, options: opts.IRmdirOptions, callback: misc.TCallback<void>): any;
  293. };
  294. rmSync: (path: PathLike, options?: opts.IRmOptions) => void;
  295. rm: {
  296. (path: PathLike, callback: misc.TCallback<void>): void;
  297. (path: PathLike, options: opts.IRmOptions, callback: misc.TCallback<void>): void;
  298. };
  299. private readonly _fchmod;
  300. fchmodSync: (fd: number, mode: TMode) => void;
  301. fchmod: (fd: number, mode: TMode, callback: misc.TCallback<void>) => void;
  302. private readonly _chmod;
  303. chmodSync: (path: PathLike, mode: TMode) => void;
  304. chmod: (path: PathLike, mode: TMode, callback: misc.TCallback<void>) => void;
  305. private readonly _lchmod;
  306. lchmodSync: (path: PathLike, mode: TMode) => void;
  307. lchmod: (path: PathLike, mode: TMode, callback: misc.TCallback<void>) => void;
  308. private readonly _fchown;
  309. fchownSync: (fd: number, uid: number, gid: number) => void;
  310. fchown: (fd: number, uid: number, gid: number, callback: misc.TCallback<void>) => void;
  311. private readonly _chown;
  312. chownSync: (path: PathLike, uid: number, gid: number) => void;
  313. chown: (path: PathLike, uid: number, gid: number, callback: misc.TCallback<void>) => void;
  314. private readonly _lchown;
  315. lchownSync: (path: PathLike, uid: number, gid: number) => void;
  316. lchown: (path: PathLike, uid: number, gid: number, callback: misc.TCallback<void>) => void;
  317. private statWatchers;
  318. watchFile(path: PathLike, listener: (curr: Stats, prev: Stats) => void): StatWatcher;
  319. watchFile(path: PathLike, options: IWatchFileOptions, listener: (curr: Stats, prev: Stats) => void): StatWatcher;
  320. unwatchFile(path: PathLike, listener?: (curr: Stats, prev: Stats) => void): void;
  321. createReadStream(path: misc.PathLike, options?: opts.IReadStreamOptions | string): misc.IReadStream;
  322. createWriteStream(path: PathLike, options?: opts.IWriteStreamOptions | string): IWriteStream;
  323. watch(path: PathLike, listener?: (eventType: string, filename: string) => void): FSWatcher;
  324. watch(path: PathLike, options: 'buffer' | (IWatchOptions & {
  325. encoding: 'buffer';
  326. }), listener?: (eventType: string, filename: Buffer) => void): FSWatcher;
  327. watch(path: PathLike, options: IWatchOptions | string | undefined, listener?: (eventType: string, filename: string) => void): FSWatcher;
  328. cpSync: (src: string | URL, dest: string | URL, options?: opts.ICpOptions) => void;
  329. cp: {
  330. (src: string | URL, dest: string | URL, callback: misc.TCallback<void>): any;
  331. (src: string | URL, dest: string | URL, options: opts.ICpOptions, callback: misc.TCallback<void>): any;
  332. };
  333. private _statfs;
  334. statfsSync(path: PathLike): StatFs<number>;
  335. statfsSync(path: PathLike, options: {
  336. bigint: false;
  337. }): StatFs<number>;
  338. statfsSync(path: PathLike, options: {
  339. bigint: true;
  340. }): StatFs<bigint>;
  341. statfs(path: PathLike, callback: misc.TCallback<StatFs>): void;
  342. statfs(path: PathLike, options: opts.IStafsOptions, callback: misc.TCallback<StatFs>): void;
  343. openAsBlob: (path: PathLike, options?: opts.IOpenAsBlobOptions) => Promise<Blob>;
  344. glob: FsCallbackApi['glob'];
  345. globSync: FsSynchronousApi['globSync'];
  346. private readonly _globSync;
  347. private readonly _opendir;
  348. opendirSync: (path: PathLike, options?: opts.IOpendirOptions | string) => Dir;
  349. opendir: {
  350. (path: PathLike, callback: misc.TCallback<Dir>): any;
  351. (path: PathLike, options: opts.IOpendirOptions | string, callback: misc.TCallback<Dir>): any;
  352. };
  353. toTree(opts?: ToTreeOptions): string;
  354. toSnapshot(path?: string): fsSnapshot.SnapshotNode;
  355. fromSnapshot(snapshot: fsSnapshot.SnapshotNode, path?: string): void;
  356. toBinarySnapshot(path?: string): Uint8Array;
  357. fromBinarySnapshot(binary: Uint8Array, path?: string): void;
  358. toJsonSnapshot(path?: string): string;
  359. fromJsonSnapshot(json: string, path?: string): void;
  360. }
  361. export declare class StatWatcher extends EventEmitter {
  362. vol: Volume;
  363. filename: string;
  364. interval: number;
  365. timeoutRef?: any;
  366. setTimeout: TSetTimeout;
  367. prev: Stats;
  368. private bigint;
  369. private fileExists;
  370. private stopped;
  371. constructor(vol: Volume);
  372. private loop;
  373. private hasChanged;
  374. private statSafe;
  375. private onInterval;
  376. start(path: string, persistent?: boolean, interval?: number, bigint?: boolean): void;
  377. stop(): void;
  378. /**
  379. * Keep the event loop alive while the watcher is active. Polling continues
  380. * either way; this only controls whether the process is kept running.
  381. */
  382. ref(): this;
  383. /** Let the process exit while the watcher is active. Polling continues. */
  384. unref(): this;
  385. }
  386. export interface IWriteStream extends Writable {
  387. bytesWritten: number;
  388. path: string;
  389. pending: boolean;
  390. new (path: PathLike, options: opts.IWriteStreamOptions): any;
  391. open(): any;
  392. close(): any;
  393. }
  394. export declare class FSWatcher extends EventEmitter {
  395. _vol: Volume;
  396. _filename: string;
  397. _steps: string[];
  398. _filenameEncoded: TDataOut;
  399. _recursive: boolean;
  400. _encoding: BufferEncoding;
  401. _link: Link;
  402. _timer: any;
  403. private _watcher;
  404. private _closed;
  405. private _ignore;
  406. constructor(vol: Volume);
  407. private _emit;
  408. private _onEvent;
  409. private _persist;
  410. /**
  411. * Keep the event loop alive while the watcher is active. All watchers are
  412. * ref'ed by default (unless started with `persistent: false`). No-op when
  413. * already ref'ed, closed, or never started.
  414. */
  415. ref(): this;
  416. /**
  417. * Let the process exit while the watcher is active. Events are still
  418. * delivered. No-op when already unref'ed.
  419. */
  420. unref(): this;
  421. start(path: PathLike, persistent?: boolean, recursive?: boolean, encoding?: BufferEncoding, ignore?: opts.TWatchIgnorePattern | opts.TWatchIgnorePattern[]): void;
  422. close(): void;
  423. }