volume.d.ts 18 KB

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