volume.d.ts 18 KB

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