volume.d.ts 18 KB

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