volume.d.ts 19 KB

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