volume.d.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. import { Node, Link, File } from './node';
  2. import Stats from './Stats';
  3. import Dirent from './Dirent';
  4. import { TSetTimeout } from './setTimeoutUnref';
  5. import { Writable } from 'stream';
  6. import { constants } from './constants';
  7. import { EventEmitter } from 'events';
  8. import { TDataOut } from './encoding';
  9. import * as misc from './node/types/misc';
  10. import * as opts from './node/types/options';
  11. import { FsCallbackApi, WritevCallback } from './node/types/FsCallbackApi';
  12. import { ToTreeOptions } from './print';
  13. import type { PathLike, symlink } from 'fs';
  14. import type { FsPromisesApi, FsSynchronousApi } from './node/types';
  15. import { Dir } from './Dir';
  16. export interface IError extends Error {
  17. code?: string;
  18. }
  19. export type TFileId = PathLike | number;
  20. export type TData = TDataOut | ArrayBufferView | DataView;
  21. export type TFlags = string | number;
  22. export type TMode = string | number;
  23. export type TTime = number | string | Date;
  24. export type TCallback<TData> = (error?: IError | null, data?: TData) => void;
  25. export type TFlagsCopy = typeof constants.COPYFILE_EXCL | typeof constants.COPYFILE_FICLONE | typeof constants.COPYFILE_FICLONE_FORCE;
  26. export interface IAppendFileOptions extends opts.IFileOptions {
  27. }
  28. export interface IWatchFileOptions {
  29. persistent?: boolean;
  30. interval?: number;
  31. }
  32. export interface IWatchOptions extends opts.IOptions {
  33. persistent?: boolean;
  34. recursive?: boolean;
  35. }
  36. export declare function filenameToSteps(filename: string, base?: string): string[];
  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. type DirectoryContent = string | Buffer | null;
  41. export interface DirectoryJSON<T extends DirectoryContent = DirectoryContent> {
  42. [key: string]: T;
  43. }
  44. export interface NestedDirectoryJSON<T extends DirectoryContent = DirectoryContent> {
  45. [key: string]: T | NestedDirectoryJSON;
  46. }
  47. /**
  48. * `Volume` represents a file system.
  49. */
  50. export declare class Volume implements FsCallbackApi, FsSynchronousApi {
  51. static fromJSON(json: DirectoryJSON, cwd?: string): Volume;
  52. static fromNestedJSON(json: NestedDirectoryJSON, cwd?: string): Volume;
  53. /**
  54. * Global file descriptor counter. UNIX file descriptors start from 0 and go sequentially
  55. * up, so here, in order not to conflict with them, we choose some big number and descrease
  56. * the file descriptor of every new opened file.
  57. * @type {number}
  58. * @todo This should not be static, right?
  59. */
  60. static fd: number;
  61. root: Link;
  62. ino: number;
  63. inodes: {
  64. [ino: number]: Node;
  65. };
  66. releasedInos: number[];
  67. fds: {
  68. [fd: number]: File;
  69. };
  70. releasedFds: number[];
  71. maxFiles: number;
  72. openFiles: number;
  73. StatWatcher: new () => StatWatcher;
  74. ReadStream: new (...args: any[]) => misc.IReadStream;
  75. WriteStream: new (...args: any[]) => IWriteStream;
  76. FSWatcher: new () => FSWatcher;
  77. props: {
  78. Node: new (...args: any[]) => Node;
  79. Link: new (...args: any[]) => Link;
  80. File: new (...args: any[]) => File;
  81. };
  82. private promisesApi;
  83. get promises(): FsPromisesApi;
  84. constructor(props?: {});
  85. createLink(): Link;
  86. createLink(parent: Link, name: string, isDirectory?: boolean, mode?: number): Link;
  87. deleteLink(link: Link): boolean;
  88. private newInoNumber;
  89. private newFdNumber;
  90. createNode(mode: number): Node;
  91. private deleteNode;
  92. private walk;
  93. getLink(steps: string[]): Link | null;
  94. getLinkOrThrow(filename: string, funcName?: string): Link;
  95. getResolvedLink(filenameOrSteps: string | string[]): Link | null;
  96. getResolvedLinkOrThrow(filename: string, funcName?: string): Link;
  97. resolveSymlinks(link: Link): Link | null;
  98. private getLinkAsDirOrThrow;
  99. private getLinkParent;
  100. private getLinkParentAsDirOrThrow;
  101. private getFileByFd;
  102. private getFileByFdOrThrow;
  103. /**
  104. * @todo This is not used anymore. Remove.
  105. */
  106. private wrapAsync;
  107. private _toJSON;
  108. toJSON(paths?: PathLike | PathLike[], json?: {}, isRelative?: boolean, asBuffer?: boolean): DirectoryJSON<string | null>;
  109. fromJSON(json: DirectoryJSON, cwd?: string): void;
  110. fromNestedJSON(json: NestedDirectoryJSON, cwd?: string): void;
  111. toTree(opts?: ToTreeOptions): string;
  112. reset(): void;
  113. mountSync(mountpoint: string, json: DirectoryJSON): void;
  114. private openLink;
  115. private openFile;
  116. private openBase;
  117. openSync(path: PathLike, flags: TFlags, mode?: TMode): number;
  118. open(path: PathLike, flags: TFlags, /* ... */ callback: TCallback<number>): any;
  119. open(path: PathLike, flags: TFlags, mode: TMode, callback: TCallback<number>): any;
  120. private closeFile;
  121. closeSync(fd: number): void;
  122. close(fd: number, callback: TCallback<void>): void;
  123. private openFileOrGetById;
  124. private readBase;
  125. readSync(fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, position: number | null): number;
  126. 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;
  127. private readvBase;
  128. readv(fd: number, buffers: ArrayBufferView[], callback: misc.TCallback2<number, ArrayBufferView[]>): void;
  129. readv(fd: number, buffers: ArrayBufferView[], position: number | null, callback: misc.TCallback2<number, ArrayBufferView[]>): void;
  130. readvSync(fd: number, buffers: ArrayBufferView[], position: number | null): number;
  131. private readFileBase;
  132. readFileSync(file: TFileId, options?: opts.IReadFileOptions | string): TDataOut;
  133. readFile(id: TFileId, callback: TCallback<TDataOut>): any;
  134. readFile(id: TFileId, options: opts.IReadFileOptions | string, callback: TCallback<TDataOut>): any;
  135. private writeBase;
  136. writeSync(fd: number, buffer: Buffer | ArrayBufferView | DataView, offset?: number, length?: number, position?: number): number;
  137. writeSync(fd: number, str: string, position?: number, encoding?: BufferEncoding): number;
  138. write(fd: number, buffer: Buffer | ArrayBufferView | DataView, callback: (...args: any[]) => void): any;
  139. write(fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, callback: (...args: any[]) => void): any;
  140. write(fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, callback: (...args: any[]) => void): any;
  141. write(fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, position: number, callback: (...args: any[]) => void): any;
  142. write(fd: number, str: string, callback: (...args: any[]) => void): any;
  143. write(fd: number, str: string, position: number, callback: (...args: any[]) => void): any;
  144. write(fd: number, str: string, position: number, encoding: BufferEncoding, callback: (...args: any[]) => void): any;
  145. private writevBase;
  146. writev(fd: number, buffers: ArrayBufferView[], callback: WritevCallback): void;
  147. writev(fd: number, buffers: ArrayBufferView[], position: number | null, callback: WritevCallback): void;
  148. writevSync(fd: number, buffers: ArrayBufferView[], position: number | null): number;
  149. private writeFileBase;
  150. writeFileSync(id: TFileId, data: TData, options?: opts.IWriteFileOptions): void;
  151. writeFile(id: TFileId, data: TData, callback: TCallback<void>): any;
  152. writeFile(id: TFileId, data: TData, options: opts.IWriteFileOptions | string, callback: TCallback<void>): any;
  153. private linkBase;
  154. private copyFileBase;
  155. copyFileSync(src: PathLike, dest: PathLike, flags?: TFlagsCopy): void;
  156. copyFile(src: PathLike, dest: PathLike, callback: TCallback<void>): any;
  157. copyFile(src: PathLike, dest: PathLike, flags: TFlagsCopy, callback: TCallback<void>): any;
  158. linkSync(existingPath: PathLike, newPath: PathLike): void;
  159. link(existingPath: PathLike, newPath: PathLike, callback: TCallback<void>): void;
  160. private unlinkBase;
  161. unlinkSync(path: PathLike): void;
  162. unlink(path: PathLike, callback: TCallback<void>): void;
  163. private symlinkBase;
  164. symlinkSync(target: PathLike, path: PathLike, type?: symlink.Type): void;
  165. symlink(target: PathLike, path: PathLike, callback: TCallback<void>): any;
  166. symlink(target: PathLike, path: PathLike, type: symlink.Type, callback: TCallback<void>): any;
  167. private realpathBase;
  168. realpathSync(path: PathLike, options?: opts.IRealpathOptions | string): TDataOut;
  169. realpath(path: PathLike, callback: TCallback<TDataOut>): any;
  170. realpath(path: PathLike, options: opts.IRealpathOptions | string, callback: TCallback<TDataOut>): any;
  171. private lstatBase;
  172. lstatSync(path: PathLike): Stats<number>;
  173. lstatSync(path: PathLike, options: {
  174. throwIfNoEntry?: true | undefined;
  175. }): Stats<number>;
  176. lstatSync(path: PathLike, options: {
  177. bigint: false;
  178. throwIfNoEntry?: true | undefined;
  179. }): Stats<number>;
  180. lstatSync(path: PathLike, options: {
  181. bigint: true;
  182. throwIfNoEntry?: true | undefined;
  183. }): Stats<bigint>;
  184. lstatSync(path: PathLike, options: {
  185. throwIfNoEntry: false;
  186. }): Stats<number> | undefined;
  187. lstatSync(path: PathLike, options: {
  188. bigint: false;
  189. throwIfNoEntry: false;
  190. }): Stats<number> | undefined;
  191. lstatSync(path: PathLike, options: {
  192. bigint: true;
  193. throwIfNoEntry: false;
  194. }): Stats<bigint> | undefined;
  195. lstat(path: PathLike, callback: TCallback<Stats>): void;
  196. lstat(path: PathLike, options: opts.IStatOptions, callback: TCallback<Stats>): void;
  197. private statBase;
  198. statSync(path: PathLike): Stats<number>;
  199. statSync(path: PathLike, options: {
  200. throwIfNoEntry?: true;
  201. }): Stats<number>;
  202. statSync(path: PathLike, options: {
  203. throwIfNoEntry: false;
  204. }): Stats<number> | undefined;
  205. statSync(path: PathLike, options: {
  206. bigint: false;
  207. throwIfNoEntry?: true;
  208. }): Stats<number>;
  209. statSync(path: PathLike, options: {
  210. bigint: true;
  211. throwIfNoEntry?: true;
  212. }): Stats<bigint>;
  213. statSync(path: PathLike, options: {
  214. bigint: false;
  215. throwIfNoEntry: false;
  216. }): Stats<number> | undefined;
  217. statSync(path: PathLike, options: {
  218. bigint: true;
  219. throwIfNoEntry: false;
  220. }): Stats<bigint> | undefined;
  221. stat(path: PathLike, callback: TCallback<Stats>): void;
  222. stat(path: PathLike, options: opts.IStatOptions, callback: TCallback<Stats>): void;
  223. private fstatBase;
  224. fstatSync(fd: number): Stats<number>;
  225. fstatSync(fd: number, options: {
  226. bigint: false;
  227. }): Stats<number>;
  228. fstatSync(fd: number, options: {
  229. bigint: true;
  230. }): Stats<bigint>;
  231. fstat(fd: number, callback: TCallback<Stats>): void;
  232. fstat(fd: number, options: opts.IFStatOptions, callback: TCallback<Stats>): void;
  233. private renameBase;
  234. renameSync(oldPath: PathLike, newPath: PathLike): void;
  235. rename(oldPath: PathLike, newPath: PathLike, callback: TCallback<void>): void;
  236. private existsBase;
  237. existsSync(path: PathLike): boolean;
  238. exists(path: PathLike, callback: (exists: boolean) => void): void;
  239. private accessBase;
  240. accessSync(path: PathLike, mode?: number): void;
  241. access(path: PathLike, callback: TCallback<void>): any;
  242. access(path: PathLike, mode: number, callback: TCallback<void>): any;
  243. appendFileSync(id: TFileId, data: TData, options?: IAppendFileOptions | string): void;
  244. appendFile(id: TFileId, data: TData, callback: TCallback<void>): any;
  245. appendFile(id: TFileId, data: TData, options: IAppendFileOptions | string, callback: TCallback<void>): any;
  246. private readdirBase;
  247. readdirSync(path: PathLike, options?: opts.IReaddirOptions | string): TDataOut[] | Dirent[];
  248. readdir(path: PathLike, callback: TCallback<TDataOut[] | Dirent[]>): any;
  249. readdir(path: PathLike, options: opts.IReaddirOptions | string, callback: TCallback<TDataOut[] | Dirent[]>): any;
  250. private readlinkBase;
  251. readlinkSync(path: PathLike, options?: opts.IOptions): TDataOut;
  252. readlink(path: PathLike, callback: TCallback<TDataOut>): any;
  253. readlink(path: PathLike, options: opts.IOptions, callback: TCallback<TDataOut>): any;
  254. private fsyncBase;
  255. fsyncSync(fd: number): void;
  256. fsync(fd: number, callback: TCallback<void>): void;
  257. private fdatasyncBase;
  258. fdatasyncSync(fd: number): void;
  259. fdatasync(fd: number, callback: TCallback<void>): void;
  260. private ftruncateBase;
  261. ftruncateSync(fd: number, len?: number): void;
  262. ftruncate(fd: number, callback: TCallback<void>): any;
  263. ftruncate(fd: number, len: number, callback: TCallback<void>): any;
  264. private truncateBase;
  265. /**
  266. * `id` should be a file descriptor or a path. `id` as file descriptor will
  267. * not be supported soon.
  268. */
  269. truncateSync(id: TFileId, len?: number): void;
  270. truncate(id: TFileId, callback: TCallback<void>): any;
  271. truncate(id: TFileId, len: number, callback: TCallback<void>): any;
  272. private futimesBase;
  273. futimesSync(fd: number, atime: TTime, mtime: TTime): void;
  274. futimes(fd: number, atime: TTime, mtime: TTime, callback: TCallback<void>): void;
  275. private utimesBase;
  276. utimesSync(path: PathLike, atime: TTime, mtime: TTime): void;
  277. utimes(path: PathLike, atime: TTime, mtime: TTime, callback: TCallback<void>): void;
  278. lutimesSync(path: PathLike, atime: TTime, mtime: TTime): void;
  279. lutimes(path: PathLike, atime: TTime, mtime: TTime, callback: TCallback<void>): void;
  280. private mkdirBase;
  281. /**
  282. * Creates directory tree recursively.
  283. */
  284. private mkdirpBase;
  285. mkdirSync(path: PathLike, options: opts.IMkdirOptions & {
  286. recursive: true;
  287. }): string | undefined;
  288. mkdirSync(path: PathLike, options?: TMode | (opts.IMkdirOptions & {
  289. recursive?: false;
  290. })): void;
  291. mkdirSync(path: PathLike, options?: TMode | opts.IMkdirOptions): string | undefined;
  292. mkdir(path: PathLike, callback: TCallback<void>): any;
  293. mkdir(path: PathLike, mode: TMode | (opts.IMkdirOptions & {
  294. recursive?: false;
  295. }), callback: TCallback<void>): any;
  296. mkdir(path: PathLike, mode: opts.IMkdirOptions & {
  297. recursive: true;
  298. }, callback: TCallback<string>): any;
  299. mkdir(path: PathLike, mode: TMode | opts.IMkdirOptions, callback: TCallback<string>): any;
  300. private mkdtempBase;
  301. mkdtempSync(prefix: string, options?: opts.IOptions): TDataOut;
  302. mkdtemp(prefix: string, callback: TCallback<string>): any;
  303. mkdtemp(prefix: string, options: opts.IOptions, callback: TCallback<string>): any;
  304. private rmdirBase;
  305. rmdirSync(path: PathLike, options?: opts.IRmdirOptions): void;
  306. rmdir(path: PathLike, callback: TCallback<void>): any;
  307. rmdir(path: PathLike, options: opts.IRmdirOptions, callback: TCallback<void>): any;
  308. private rmBase;
  309. rmSync(path: PathLike, options?: opts.IRmOptions): void;
  310. rm(path: PathLike, callback: TCallback<void>): void;
  311. rm(path: PathLike, options: opts.IRmOptions, callback: TCallback<void>): void;
  312. private fchmodBase;
  313. fchmodSync(fd: number, mode: TMode): void;
  314. fchmod(fd: number, mode: TMode, callback: TCallback<void>): void;
  315. private chmodBase;
  316. chmodSync(path: PathLike, mode: TMode): void;
  317. chmod(path: PathLike, mode: TMode, callback: TCallback<void>): void;
  318. private lchmodBase;
  319. lchmodSync(path: PathLike, mode: TMode): void;
  320. lchmod(path: PathLike, mode: TMode, callback: TCallback<void>): void;
  321. private fchownBase;
  322. fchownSync(fd: number, uid: number, gid: number): void;
  323. fchown(fd: number, uid: number, gid: number, callback: TCallback<void>): void;
  324. private chownBase;
  325. chownSync(path: PathLike, uid: number, gid: number): void;
  326. chown(path: PathLike, uid: number, gid: number, callback: TCallback<void>): void;
  327. private lchownBase;
  328. lchownSync(path: PathLike, uid: number, gid: number): void;
  329. lchown(path: PathLike, uid: number, gid: number, callback: TCallback<void>): void;
  330. private statWatchers;
  331. watchFile(path: PathLike, listener: (curr: Stats, prev: Stats) => void): StatWatcher;
  332. watchFile(path: PathLike, options: IWatchFileOptions, listener: (curr: Stats, prev: Stats) => void): StatWatcher;
  333. unwatchFile(path: PathLike, listener?: (curr: Stats, prev: Stats) => void): void;
  334. createReadStream(path: misc.PathLike, options?: opts.IReadStreamOptions | string): misc.IReadStream;
  335. createWriteStream(path: PathLike, options?: opts.IWriteStreamOptions | string): IWriteStream;
  336. watch(path: PathLike, options?: IWatchOptions | string, listener?: (eventType: string, filename: string) => void): FSWatcher;
  337. cpSync: FsSynchronousApi['cpSync'];
  338. statfsSync: FsSynchronousApi['statfsSync'];
  339. cp: FsCallbackApi['cp'];
  340. statfs: FsCallbackApi['statfs'];
  341. openAsBlob: FsCallbackApi['openAsBlob'];
  342. private opendirBase;
  343. opendirSync(path: PathLike, options?: opts.IOpendirOptions | string): Dir;
  344. opendir(path: PathLike, callback: TCallback<Dir>): any;
  345. opendir(path: PathLike, options: opts.IOpendirOptions | string, callback: TCallback<Dir>): any;
  346. }
  347. export declare class StatWatcher extends EventEmitter {
  348. vol: Volume;
  349. filename: string;
  350. interval: number;
  351. timeoutRef?: any;
  352. setTimeout: TSetTimeout;
  353. prev: Stats;
  354. constructor(vol: Volume);
  355. private loop;
  356. private hasChanged;
  357. private onInterval;
  358. start(path: string, persistent?: boolean, interval?: number): void;
  359. stop(): void;
  360. }
  361. export interface IWriteStream extends Writable {
  362. bytesWritten: number;
  363. path: string;
  364. pending: boolean;
  365. new (path: PathLike, options: opts.IWriteStreamOptions): any;
  366. open(): any;
  367. close(): any;
  368. }
  369. export declare class FSWatcher extends EventEmitter {
  370. _vol: Volume;
  371. _filename: string;
  372. _steps: string[];
  373. _filenameEncoded: TDataOut;
  374. _recursive: boolean;
  375. _encoding: BufferEncoding;
  376. _link: Link;
  377. _timer: any;
  378. private _listenerRemovers;
  379. constructor(vol: Volume);
  380. private _getName;
  381. private _onParentChild;
  382. private _emit;
  383. private _persist;
  384. start(path: PathLike, persistent?: boolean, recursive?: boolean, encoding?: BufferEncoding): void;
  385. close(): void;
  386. }
  387. export {};