UNPKG

18.3 kBTypeScriptView Raw
1import { Node, Link, File } from './node';
2import Stats from './Stats';
3import Dirent from './Dirent';
4import { TSetTimeout } from './setTimeoutUnref';
5import { Writable } from 'stream';
6import { constants } from './constants';
7import { EventEmitter } from 'events';
8import { TDataOut } from './encoding';
9import * as misc from './node/types/misc';
10import * as opts from './node/types/options';
11import { FsCallbackApi, WritevCallback } from './node/types/FsCallbackApi';
12import { ToTreeOptions } from './print';
13import type { PathLike, symlink } from 'fs';
14import type { FsPromisesApi, FsSynchronousApi } from './node/types';
15import { Dir } from './Dir';
16export interface IError extends Error {
17 code?: string;
18}
19export type TFileId = PathLike | number;
20export type TData = TDataOut | ArrayBufferView | DataView;
21export type TFlags = string | number;
22export type TMode = string | number;
23export type TTime = number | string | Date;
24export type TCallback<TData> = (error?: IError | null, data?: TData) => void;
25export type TFlagsCopy = typeof constants.COPYFILE_EXCL | typeof constants.COPYFILE_FICLONE | typeof constants.COPYFILE_FICLONE_FORCE;
26export interface IAppendFileOptions extends opts.IFileOptions {
27}
28export interface IWatchFileOptions {
29 persistent?: boolean;
30 interval?: number;
31}
32export interface IWatchOptions extends opts.IOptions {
33 persistent?: boolean;
34 recursive?: boolean;
35}
36export declare function filenameToSteps(filename: string, base?: string): string[];
37export declare function pathToSteps(path: PathLike): string[];
38export declare function dataToStr(data: TData, encoding?: string): string;
39export declare function toUnixTimestamp(time: any): any;
40type DirectoryContent = string | Buffer | null;
41export interface DirectoryJSON<T extends DirectoryContent = DirectoryContent> {
42 [key: string]: T;
43}
44export interface NestedDirectoryJSON<T extends DirectoryContent = DirectoryContent> {
45 [key: string]: T | NestedDirectoryJSON;
46}
47/**
48 * `Volume` represents a file system.
49 */
50export 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, perm?: number): Link;
87 deleteLink(link: Link): boolean;
88 private newInoNumber;
89 private newFdNumber;
90 createNode(isDirectory?: boolean, perm?: 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 private mkdirBase;
279 /**
280 * Creates directory tree recursively.
281 */
282 private mkdirpBase;
283 mkdirSync(path: PathLike, options: opts.IMkdirOptions & {
284 recursive: true;
285 }): string | undefined;
286 mkdirSync(path: PathLike, options?: TMode | (opts.IMkdirOptions & {
287 recursive?: false;
288 })): void;
289 mkdirSync(path: PathLike, options?: TMode | opts.IMkdirOptions): string | undefined;
290 mkdir(path: PathLike, callback: TCallback<void>): any;
291 mkdir(path: PathLike, mode: TMode | (opts.IMkdirOptions & {
292 recursive?: false;
293 }), callback: TCallback<void>): any;
294 mkdir(path: PathLike, mode: opts.IMkdirOptions & {
295 recursive: true;
296 }, callback: TCallback<string>): any;
297 mkdir(path: PathLike, mode: TMode | opts.IMkdirOptions, callback: TCallback<string>): any;
298 private mkdtempBase;
299 mkdtempSync(prefix: string, options?: opts.IOptions): TDataOut;
300 mkdtemp(prefix: string, callback: TCallback<string>): any;
301 mkdtemp(prefix: string, options: opts.IOptions, callback: TCallback<string>): any;
302 private rmdirBase;
303 rmdirSync(path: PathLike, options?: opts.IRmdirOptions): void;
304 rmdir(path: PathLike, callback: TCallback<void>): any;
305 rmdir(path: PathLike, options: opts.IRmdirOptions, callback: TCallback<void>): any;
306 private rmBase;
307 rmSync(path: PathLike, options?: opts.IRmOptions): void;
308 rm(path: PathLike, callback: TCallback<void>): void;
309 rm(path: PathLike, options: opts.IRmOptions, callback: TCallback<void>): void;
310 private fchmodBase;
311 fchmodSync(fd: number, mode: TMode): void;
312 fchmod(fd: number, mode: TMode, callback: TCallback<void>): void;
313 private chmodBase;
314 chmodSync(path: PathLike, mode: TMode): void;
315 chmod(path: PathLike, mode: TMode, callback: TCallback<void>): void;
316 private lchmodBase;
317 lchmodSync(path: PathLike, mode: TMode): void;
318 lchmod(path: PathLike, mode: TMode, callback: TCallback<void>): void;
319 private fchownBase;
320 fchownSync(fd: number, uid: number, gid: number): void;
321 fchown(fd: number, uid: number, gid: number, callback: TCallback<void>): void;
322 private chownBase;
323 chownSync(path: PathLike, uid: number, gid: number): void;
324 chown(path: PathLike, uid: number, gid: number, callback: TCallback<void>): void;
325 private lchownBase;
326 lchownSync(path: PathLike, uid: number, gid: number): void;
327 lchown(path: PathLike, uid: number, gid: number, callback: TCallback<void>): void;
328 private statWatchers;
329 watchFile(path: PathLike, listener: (curr: Stats, prev: Stats) => void): StatWatcher;
330 watchFile(path: PathLike, options: IWatchFileOptions, listener: (curr: Stats, prev: Stats) => void): StatWatcher;
331 unwatchFile(path: PathLike, listener?: (curr: Stats, prev: Stats) => void): void;
332 createReadStream(path: misc.PathLike, options?: opts.IReadStreamOptions | string): misc.IReadStream;
333 createWriteStream(path: PathLike, options?: opts.IWriteStreamOptions | string): IWriteStream;
334 watch(path: PathLike, options?: IWatchOptions | string, listener?: (eventType: string, filename: string) => void): FSWatcher;
335 cpSync: FsSynchronousApi['cpSync'];
336 lutimesSync: FsSynchronousApi['lutimesSync'];
337 statfsSync: FsSynchronousApi['statfsSync'];
338 cp: FsCallbackApi['cp'];
339 lutimes: FsCallbackApi['lutimes'];
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}
347export 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}
361export 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}
369export 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}
387export {};