volume.d.ts 18 KB

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