UNPKG

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