UNPKG

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