volume.d.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. import { FanOutUnsubscribe } from 'thingies/lib/fanout';
  2. import { Link, Superblock, DirectoryJSON, NestedDirectoryJSON, TFileId } from '@jsonjoy.com/fs-core';
  3. import Stats from './Stats';
  4. import Dirent from './Dirent';
  5. import StatFs from './StatFs';
  6. import { Buffer } from '@jsonjoy.com/fs-node-builtins/lib/internal/buffer';
  7. import { TSetTimeout } from '@jsonjoy.com/fs-node-utils/lib/setTimeoutUnref';
  8. import { Writable } from '@jsonjoy.com/fs-node-builtins/lib/stream';
  9. import { constants, TDataOut } from '@jsonjoy.com/fs-node-utils';
  10. import { EventEmitter } from '@jsonjoy.com/fs-node-builtins/lib/events';
  11. import * as misc from '@jsonjoy.com/fs-node-utils/lib/types/misc';
  12. import * as opts from '@jsonjoy.com/fs-node-utils/lib/types/options';
  13. import { FsCallbackApi, WritevCallback } from '@jsonjoy.com/fs-node-utils/lib/types/FsCallbackApi';
  14. import { ToTreeOptions } from '@jsonjoy.com/fs-print';
  15. import * as fsSnapshot from '@jsonjoy.com/fs-snapshot';
  16. import type { PathLike, symlink } from '@jsonjoy.com/fs-node-utils/lib/types/misc';
  17. import type { FsPromisesApi, FsSynchronousApi } from '@jsonjoy.com/fs-node-utils';
  18. import { Dir } from './Dir';
  19. export type { SnapshotNode } from '@jsonjoy.com/fs-snapshot';
  20. export interface IError extends Error {
  21. code?: string;
  22. }
  23. export type TData = TDataOut | ArrayBufferView | DataView;
  24. export type TFlags = string | number;
  25. export type TMode = string | number;
  26. export type TTime = number | string | Date;
  27. export type TFlagsCopy = typeof constants.COPYFILE_EXCL | typeof constants.COPYFILE_FICLONE | typeof constants.COPYFILE_FICLONE_FORCE;
  28. export interface IAppendFileOptions extends opts.IFileOptions {
  29. }
  30. export interface IWatchFileOptions {
  31. persistent?: boolean;
  32. interval?: number;
  33. }
  34. export interface IWatchOptions extends opts.IOptions {
  35. persistent?: boolean;
  36. recursive?: boolean;
  37. }
  38. export declare function pathToSteps(path: PathLike): string[];
  39. export declare function dataToStr(data: TData, encoding?: BufferEncoding): string;
  40. export declare function toUnixTimestamp(time: any): any;
  41. /**
  42. * `Volume` represents a file system.
  43. */
  44. export declare class Volume implements FsCallbackApi, FsSynchronousApi {
  45. readonly _core: Superblock;
  46. static readonly fromJSON: (json: DirectoryJSON, cwd?: string) => Volume;
  47. static readonly fromNestedJSON: (json: NestedDirectoryJSON, cwd?: string) => Volume;
  48. StatWatcher: new () => StatWatcher;
  49. ReadStream: new (...args: any[]) => misc.IReadStream;
  50. WriteStream: new (...args: any[]) => IWriteStream;
  51. FSWatcher: new () => FSWatcher;
  52. realpath: {
  53. (path: PathLike, callback: misc.TCallback<TDataOut>): void;
  54. (path: PathLike, options: opts.IRealpathOptions | string, callback: misc.TCallback<TDataOut>): void;
  55. native: {
  56. (path: PathLike, callback: misc.TCallback<TDataOut>): void;
  57. (path: PathLike, options: opts.IRealpathOptions | string, callback: misc.TCallback<TDataOut>): void;
  58. };
  59. };
  60. realpathSync: {
  61. (path: PathLike, options?: opts.IRealpathOptions | string): TDataOut;
  62. native: (path: PathLike, options?: opts.IRealpathOptions | string) => TDataOut;
  63. };
  64. private promisesApi;
  65. get promises(): FsPromisesApi;
  66. constructor(_core?: Superblock);
  67. private wrapAsync;
  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. private _statOrThrow;
  174. statSync(path: PathLike): Stats<number>;
  175. statSync(path: PathLike, options: {
  176. throwIfNoEntry?: true;
  177. }): Stats<number>;
  178. statSync(path: PathLike, options: {
  179. throwIfNoEntry: false;
  180. }): Stats<number> | undefined;
  181. statSync(path: PathLike, options: {
  182. bigint: false;
  183. throwIfNoEntry?: true;
  184. }): Stats<number>;
  185. statSync(path: PathLike, options: {
  186. bigint: true;
  187. throwIfNoEntry?: true;
  188. }): Stats<bigint>;
  189. statSync(path: PathLike, options: {
  190. bigint: false;
  191. throwIfNoEntry: false;
  192. }): Stats<number> | undefined;
  193. statSync(path: PathLike, options: {
  194. bigint: true;
  195. throwIfNoEntry: false;
  196. }): Stats<bigint> | undefined;
  197. stat(path: PathLike, callback: misc.TCallback<Stats>): void;
  198. stat(path: PathLike, options: opts.IStatOptions, callback: misc.TCallback<Stats>): void;
  199. private fstatBase;
  200. fstatSync(fd: number): Stats<number>;
  201. fstatSync(fd: number, options: {
  202. bigint: false;
  203. }): Stats<number>;
  204. fstatSync(fd: number, options: {
  205. bigint: true;
  206. }): Stats<bigint>;
  207. fstat(fd: number, callback: misc.TCallback<Stats>): void;
  208. fstat(fd: number, options: opts.IFStatOptions, callback: misc.TCallback<Stats>): void;
  209. renameSync: (oldPath: PathLike, newPath: PathLike) => void;
  210. rename: (oldPath: PathLike, newPath: PathLike, callback: misc.TCallback<void>) => void;
  211. private _exists;
  212. existsSync: (path: PathLike) => boolean;
  213. exists: (path: PathLike, callback: (exists: boolean) => void) => void;
  214. private _access;
  215. accessSync: (path: PathLike, mode?: number) => void;
  216. access: {
  217. (path: PathLike, callback: misc.TCallback<void>): any;
  218. (path: PathLike, mode: number, callback: misc.TCallback<void>): any;
  219. };
  220. appendFileSync: (id: TFileId, data: TData, options?: IAppendFileOptions | string) => void;
  221. appendFile: {
  222. (id: TFileId, data: TData, callback: misc.TCallback<void>): any;
  223. (id: TFileId, data: TData, options: IAppendFileOptions | string, callback: misc.TCallback<void>): any;
  224. };
  225. private readonly _readdir;
  226. readdirSync: (path: PathLike, options?: opts.IReaddirOptions | string) => TDataOut[] | Dirent[];
  227. readdir: {
  228. (path: PathLike, callback: misc.TCallback<TDataOut[] | Dirent[]>): any;
  229. (path: PathLike, options: opts.IReaddirOptions | string, callback: misc.TCallback<TDataOut[] | Dirent[]>): any;
  230. };
  231. private readonly _readlink;
  232. readlinkSync: (path: PathLike, options?: opts.IOptions) => TDataOut;
  233. readlink: {
  234. (path: PathLike, callback: misc.TCallback<TDataOut>): any;
  235. (path: PathLike, options: opts.IOptions, callback: misc.TCallback<TDataOut>): any;
  236. };
  237. private readonly _fsync;
  238. fsyncSync: (fd: number) => void;
  239. fsync: (fd: number, callback: misc.TCallback<void>) => void;
  240. private readonly _fdatasync;
  241. fdatasyncSync: (fd: number) => void;
  242. fdatasync: (fd: number, callback: misc.TCallback<void>) => void;
  243. private readonly _ftruncate;
  244. ftruncateSync: (fd: number, len?: number) => void;
  245. ftruncate: {
  246. (fd: number, callback: misc.TCallback<void>): any;
  247. (fd: number, len: number, callback: misc.TCallback<void>): any;
  248. };
  249. private readonly _truncate;
  250. /**
  251. * `id` should be a file descriptor or a path. `id` as file descriptor will
  252. * not be supported soon.
  253. */
  254. truncateSync: (id: TFileId, len?: number) => void;
  255. truncate: {
  256. (id: TFileId, callback: misc.TCallback<void>): any;
  257. (id: TFileId, len: number, callback: misc.TCallback<void>): any;
  258. };
  259. private readonly _futimes;
  260. futimesSync: (fd: number, atime: TTime, mtime: TTime) => void;
  261. futimes: (fd: number, atime: TTime, mtime: TTime, callback: misc.TCallback<void>) => void;
  262. private readonly _utimes;
  263. utimesSync: (path: PathLike, atime: TTime, mtime: TTime) => void;
  264. utimes: (path: PathLike, atime: TTime, mtime: TTime, callback: misc.TCallback<void>) => void;
  265. lutimesSync: (path: PathLike, atime: TTime, mtime: TTime) => void;
  266. lutimes: (path: PathLike, atime: TTime, mtime: TTime, callback: misc.TCallback<void>) => void;
  267. mkdirSync: {
  268. (path: PathLike, options: opts.IMkdirOptions & {
  269. recursive: true;
  270. }): string | undefined;
  271. (path: PathLike, options?: TMode | (opts.IMkdirOptions & {
  272. recursive?: false;
  273. })): void;
  274. (path: PathLike, options?: TMode | opts.IMkdirOptions): string | undefined;
  275. };
  276. mkdir: {
  277. (path: PathLike, callback: misc.TCallback<void>): any;
  278. (path: PathLike, mode: TMode | (opts.IMkdirOptions & {
  279. recursive?: false;
  280. }), callback: misc.TCallback<void>): any;
  281. (path: PathLike, mode: opts.IMkdirOptions & {
  282. recursive: true;
  283. }, callback: misc.TCallback<string>): any;
  284. (path: PathLike, mode: TMode | opts.IMkdirOptions, callback: misc.TCallback<string>): any;
  285. };
  286. private readonly _mkdtemp;
  287. mkdtempSync: (prefix: string, options?: opts.IOptions) => TDataOut;
  288. mkdtemp: {
  289. (prefix: string, callback: misc.TCallback<string>): any;
  290. (prefix: string, options: opts.IOptions, callback: misc.TCallback<string>): any;
  291. };
  292. rmdirSync: (path: PathLike, options?: opts.IRmdirOptions) => void;
  293. rmdir: {
  294. (path: PathLike, callback: misc.TCallback<void>): any;
  295. (path: PathLike, options: opts.IRmdirOptions, callback: misc.TCallback<void>): any;
  296. };
  297. rmSync: (path: PathLike, options?: opts.IRmOptions) => void;
  298. rm: {
  299. (path: PathLike, callback: misc.TCallback<void>): void;
  300. (path: PathLike, options: opts.IRmOptions, callback: misc.TCallback<void>): void;
  301. };
  302. private readonly _fchmod;
  303. fchmodSync: (fd: number, mode: TMode) => void;
  304. fchmod: (fd: number, mode: TMode, callback: misc.TCallback<void>) => void;
  305. private readonly _chmod;
  306. chmodSync: (path: PathLike, mode: TMode) => void;
  307. chmod: (path: PathLike, mode: TMode, callback: misc.TCallback<void>) => void;
  308. private readonly _lchmod;
  309. lchmodSync: (path: PathLike, mode: TMode) => void;
  310. lchmod: (path: PathLike, mode: TMode, callback: misc.TCallback<void>) => void;
  311. private readonly _fchown;
  312. fchownSync: (fd: number, uid: number, gid: number) => void;
  313. fchown: (fd: number, uid: number, gid: number, callback: misc.TCallback<void>) => void;
  314. private readonly _chown;
  315. chownSync: (path: PathLike, uid: number, gid: number) => void;
  316. chown: (path: PathLike, uid: number, gid: number, callback: misc.TCallback<void>) => void;
  317. private readonly _lchown;
  318. lchownSync: (path: PathLike, uid: number, gid: number) => void;
  319. lchown: (path: PathLike, uid: number, gid: number, callback: misc.TCallback<void>) => void;
  320. private statWatchers;
  321. watchFile(path: PathLike, listener: (curr: Stats, prev: Stats) => void): StatWatcher;
  322. watchFile(path: PathLike, options: IWatchFileOptions, listener: (curr: Stats, prev: Stats) => void): StatWatcher;
  323. unwatchFile(path: PathLike, listener?: (curr: Stats, prev: Stats) => void): void;
  324. createReadStream(path: misc.PathLike, options?: opts.IReadStreamOptions | string): misc.IReadStream;
  325. createWriteStream(path: PathLike, options?: opts.IWriteStreamOptions | string): IWriteStream;
  326. watch(path: PathLike, options?: IWatchOptions | string, listener?: (eventType: string, filename: string) => void): FSWatcher;
  327. cpSync: (src: string | URL, dest: string | URL, options?: opts.ICpOptions) => void;
  328. cp: {
  329. (src: string | URL, dest: string | URL, callback: misc.TCallback<void>): any;
  330. (src: string | URL, dest: string | URL, options: opts.ICpOptions, callback: misc.TCallback<void>): any;
  331. };
  332. private _statfs;
  333. statfsSync(path: PathLike): StatFs<number>;
  334. statfsSync(path: PathLike, options: {
  335. bigint: false;
  336. }): StatFs<number>;
  337. statfsSync(path: PathLike, options: {
  338. bigint: true;
  339. }): StatFs<bigint>;
  340. statfs(path: PathLike, callback: misc.TCallback<StatFs>): void;
  341. statfs(path: PathLike, options: opts.IStafsOptions, callback: misc.TCallback<StatFs>): void;
  342. openAsBlob: (path: PathLike, options?: opts.IOpenAsBlobOptions) => Promise<Blob>;
  343. glob: FsCallbackApi['glob'];
  344. globSync: FsSynchronousApi['globSync'];
  345. private readonly _globSync;
  346. private readonly _opendir;
  347. opendirSync: (path: PathLike, options?: opts.IOpendirOptions | string) => Dir;
  348. opendir: {
  349. (path: PathLike, callback: misc.TCallback<Dir>): any;
  350. (path: PathLike, options: opts.IOpendirOptions | string, callback: misc.TCallback<Dir>): any;
  351. };
  352. toTree(opts?: ToTreeOptions): string;
  353. toSnapshot(path?: string): fsSnapshot.SnapshotNode;
  354. fromSnapshot(snapshot: fsSnapshot.SnapshotNode, path?: string): void;
  355. toBinarySnapshot(path?: string): Uint8Array;
  356. fromBinarySnapshot(binary: Uint8Array, path?: string): void;
  357. toJsonSnapshot(path?: string): string;
  358. fromJsonSnapshot(json: string, path?: string): void;
  359. }
  360. export declare class StatWatcher extends EventEmitter {
  361. vol: Volume;
  362. filename: string;
  363. interval: number;
  364. timeoutRef?: any;
  365. setTimeout: TSetTimeout;
  366. prev: Stats;
  367. constructor(vol: Volume);
  368. private loop;
  369. private hasChanged;
  370. private onInterval;
  371. start(path: string, persistent?: boolean, interval?: number): void;
  372. stop(): void;
  373. }
  374. export interface IWriteStream extends Writable {
  375. bytesWritten: number;
  376. path: string;
  377. pending: boolean;
  378. new (path: PathLike, options: opts.IWriteStreamOptions): any;
  379. open(): any;
  380. close(): any;
  381. }
  382. export declare class FSWatcher extends EventEmitter {
  383. _vol: Volume;
  384. _filename: string;
  385. _steps: string[];
  386. _filenameEncoded: TDataOut;
  387. _recursive: boolean;
  388. _encoding: BufferEncoding;
  389. _link: Link;
  390. _timer: any;
  391. private _listenerRemovers;
  392. constructor(vol: Volume);
  393. private _getName;
  394. private _onParentChild;
  395. private _emit;
  396. private _persist;
  397. start(path: PathLike, persistent?: boolean, recursive?: boolean, encoding?: BufferEncoding): void;
  398. protected _parentChangesUnsub: FanOutUnsubscribe;
  399. close(): void;
  400. }