Skip to content

Documentation / base / VFS

Abstract Class: VFS

Defined in: libs/base/src/vfs/vfs.ts:314

Abstract base class for virtual file systems.

Provides a standardized interface for file system operations and supports mounting other VFS instances at specific paths for composition.

Extends

  • Observable<{ changed: ["created" | "deleted" | "moved" | "modified", string, "file" | "directory"]; }>

Extended by

Constructors

Constructor

new VFS(readOnly?): VFS

Defined in: libs/base/src/vfs/vfs.ts:337

Creates a new VFS instance.

Parameters

readOnly?

boolean = false

Whether this file system should be read-only

Returns

VFS

Overrides

Observable.constructor

Properties

_readOnly

protected _readOnly: boolean

Defined in: libs/base/src/vfs/vfs.ts:318

Whether this file system is read-only

Accessors

readOnly

Get Signature

get readOnly(): boolean

Defined in: libs/base/src/vfs/vfs.ts:348

Toggle readonly

Returns

boolean

Set Signature

set readOnly(val): void

Defined in: libs/base/src/vfs/vfs.ts:351

Parameters
val

boolean

Returns

void

Methods

on()

on<K>(type, listener, context?): void

Defined in: libs/base/src/event.ts:104

Sets up a function that will be called whenever the specified event is delivered to the target

Type Parameters

K

K extends "changed"

Parameters

type

K

The event type to listen for

listener

EventListener<{ changed: ["created" | "deleted" | "moved" | "modified", string, "file" | "directory"]; }, K>

The callback function

context?

unknown

Context object of the listener function

Returns

void

Inherited from

Observable.on


once()

once<K>(type, listener, context?): void

Defined in: libs/base/src/event.ts:117

Sets up a function that will be called only once when the specified event is delivered to the target

Type Parameters

K

K extends "changed"

Parameters

type

K

The event type to listen for

listener

EventListener<{ changed: ["created" | "deleted" | "moved" | "modified", string, "file" | "directory"]; }, K>

The callback function

context?

unknown

Context object of the listener function

Returns

void

Inherited from

Observable.once


off()

off<K>(type, listener?, context?): void

Defined in: libs/base/src/event.ts:130

Removes an event listener function previously registered.

Type Parameters

K

K extends "changed"

Parameters

type

K

The event type for which to remove an event listener

listener?

EventListener<{ changed: ["created" | "deleted" | "moved" | "modified", string, "file" | "directory"]; }>

The callback function to be removed

context?

unknown

Returns

void

Inherited from

Observable.off


dispatchEvent()

dispatchEvent<K>(type, ...args): void

Defined in: libs/base/src/event.ts:138

Synchronously invoke the affected event listeners with an event object

Type Parameters

K

K extends "changed"

Parameters

type

K

args

...object[K]

Returns

void

false if the event was canceled, otherwise true

Inherited from

Observable.dispatchEvent


getSimpleMountPoints()

getSimpleMountPoints(): string[]

Defined in: libs/base/src/vfs/vfs.ts:359

Gets all simple mount points.

Returns

string[]

Array of mount point paths


hasMounts()

hasMounts(): boolean

Defined in: libs/base/src/vfs/vfs.ts:368

Checks if this VFS has any mount points.

Returns

boolean

True if there are mounted VFS instances, false otherwise


parseDataURI()

parseDataURI(uri): RegExpMatchArray

Defined in: libs/base/src/vfs/vfs.ts:377

Parse DataURL

Parameters

uri

string

URL to parse

Returns

RegExpMatchArray

parts of data URL


isObjectURL()

isObjectURL(url): boolean

Defined in: libs/base/src/vfs/vfs.ts:385

Checks wether a URL is object url created by URL.createObjectURL()

Parameters

url

string

URL to check

Returns

boolean

true if the URL is object url, otherwise false


isAbsoluteURL()

isAbsoluteURL(url): boolean

Defined in: libs/base/src/vfs/vfs.ts:393

Checks wether a URL is absolute URL

Parameters

url

string

URL to check

Returns

boolean

true if the URL is absolute URL, otherwise false


deleteFileSystem()

deleteFileSystem(): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:399

Disposes of this file system and cleans up resources. (for IndexedDB only).

Returns

Promise<void>


wipe()

wipe(): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:406

Delete entire database (for IndexedDB only).

Returns

Promise<void>


getCwd()

getCwd(): string

Defined in: libs/base/src/vfs/vfs.ts:421

Gets the current working directory.

Returns

string

The current working directory path

Example

typescript
const cwd = fs.getCwd();
console.log(`Current directory: ${cwd}`);

chdir()

chdir(path): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:437

Changes the current working directory.

Parameters

path

string

The new working directory path (absolute or relative)

Returns

Promise<void>

Promise that resolves when directory is changed

Example

typescript
await fs.chdir('/home/user');
await fs.chdir('../documents'); // Relative path

pushd()

pushd(path): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:466

Pushes the current directory onto the directory stack and changes to the specified directory.

Parameters

path

string

The directory to change to (absolute or relative)

Returns

Promise<void>

Promise that resolves when directory is changed

Example

typescript
await fs.pushd('/tmp');        // Push current dir and go to /tmp
await fs.pushd('../other');    // Push /tmp and go to /other

popd()

popd(): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:482

Pops a directory from the directory stack and changes to it.

Returns

Promise<void>

Promise that resolves when directory is changed

Throws

VFSError if the directory stack is empty

Example

typescript
await fs.popd(); // Return to previously pushed directory

normalizePath()

normalizePath(path): string

Defined in: libs/base/src/vfs/vfs.ts:508

Normalizes a path, resolving . and .. components and making it absolute. Supports both absolute and relative paths.

Parameters

path

string

The path to normalize

Returns

string

The normalized absolute path

Example

typescript
// Assuming CWD is /home/user
fs.normalizePath('.');           // -> /home/user
fs.normalizePath('..');          // -> /home
fs.normalizePath('../docs');     // -> /home/docs
fs.normalizePath('/tmp');        // -> /tmp
fs.normalizePath('sub/dir');     // -> /home/user/sub/dir

join()

join(...paths): string

Defined in: libs/base/src/vfs/vfs.ts:541

Join paths together, making the result relative to CWD if not absolute.

Parameters

paths

...string[]

Paths to join

Returns

string

Complete normalized path

Example

typescript
// Assuming CWD is /home/user
fs.join('docs', 'file.txt');     // -> /home/user/docs/file.txt
fs.join('/tmp', 'file.txt');     // -> /tmp/file.txt

dirname()

dirname(path): string

Defined in: libs/base/src/vfs/vfs.ts:560

Extract directory part for a path

Parameters

path

string

path

Returns

string

Directory part of the path


isAbsolute()

isAbsolute(path): boolean

Defined in: libs/base/src/vfs/vfs.ts:568

Returns whether a path is absolute or not

Parameters

path

string

path to check

Returns

boolean

true if the path is an absolute path


basename()

basename(path, ext?): string

Defined in: libs/base/src/vfs/vfs.ts:577

Extract base file name part for a path

Parameters

path

string

path

ext?

string

Optional extension to strip (exact suffix match).

Returns

string

The base name of the path.


extname()

extname(path): string

Defined in: libs/base/src/vfs/vfs.ts:585

Extract extension file name part for a path

Parameters

path

string

path

Returns

string

extension file name part of the path


relative()

relative(path, parent?): string

Defined in: libs/base/src/vfs/vfs.ts:602

Converts an absolute path to a path relative to the current working directory.

Parameters

path

string

The absolute path to convert

parent?

string

Returns

string

The relative path from CWD

Example

typescript
// Assuming CWD is /home/user
fs.relative('/home/user/docs');  // -> docs
fs.relative('/home');            // -> ..
fs.relative('/tmp');             // -> ../../tmp

isParentOf()

isParentOf(parentPath, path): boolean

Defined in: libs/base/src/vfs/vfs.ts:618

Determine whether parentPath is parent directory of path (includes the case where parentPath is same as path)

Parameters

parentPath

string

The possible parent path

path

string

The possible child path

Returns

boolean

true if parentPath is parent directory of path or parent directory is same as path


move()

move(sourcePath, targetPath, options?): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:648

Moves/renames a file or directory.

Parameters

sourcePath

string

Source path

targetPath

string

Target path

options?

MoveOptions

Move options

Returns

Promise<void>

Example

typescript
// Rename file
await fs.move('/old_name.txt', '/new_name.txt');

// Move file to directory
await fs.move('/file.txt', '/subdir/file.txt');

// Rename directory
await fs.move('/old_dir', '/new_dir');

makeDirectory()

makeDirectory(path, recursive?): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:693

Makes new directory

Parameters

path

string

Directory path

recursive?

boolean

If true, create parent directory if not exists

Returns

Promise<void>


readDirectory()

readDirectory(path, options?): Promise<FileMetadata[]>

Defined in: libs/base/src/vfs/vfs.ts:710

Parameters

path

string

options?

ListOptions

Returns

Promise<FileMetadata[]>


deleteDirectory()

deleteDirectory(path, recursive?): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:735

Parameters

path

string

recursive?

boolean

Returns

Promise<void>


readFile()

readFile(path, options?): Promise<string | ArrayBuffer>

Defined in: libs/base/src/vfs/vfs.ts:777

Read from a VFS file

Parameters

path

string

File path to read

options?

ReadOptions

Read options

Returns

Promise<string | ArrayBuffer>

The file contents


writeFile()

writeFile(path, data, options?): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:820

Write to a VFS file

Parameters

path

string

File path to write

data

string | ArrayBuffer

Data to be written

options?

WriteOptions

Write options

Returns

Promise<void>


deleteFile()

deleteFile(path): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:848

Deletes a VFS file

Parameters

path

string

File path to delete

Returns

Promise<void>


exists()

exists(path): Promise<boolean>

Defined in: libs/base/src/vfs/vfs.ts:870

Test whether a VFS file or directory exists for given path

Parameters

path

string

Path to test

Returns

Promise<boolean>

true if exists


stat()

stat(path): Promise<FileStat>

Defined in: libs/base/src/vfs/vfs.ts:888

Gets the statistics about a given path

Parameters

path

string

path

Returns

Promise<FileStat>

Statistics about the path


copyFile()

copyFile(src, dest, options?): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:913

Copies a VFS file

Parameters

src

string

Source file path

dest

string

Destination file path

options?

Copy options

overwrite?

boolean

targetVFS?

VFS

Returns

Promise<void>


copyFileEx()

copyFileEx(sourcePattern, targetDirectory, options?): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:985

Copy multiple files matching a pattern to a target directory

Parameters

sourcePattern

string | string[]

Source pattern (glob pattern, file path, or array of file paths)

targetDirectory

string

Target directory path

options?

Copy options (can include targetVFS for cross-VFS copy)

cwd?

string

overwrite?

boolean

targetVFS?

VFS

onProgress?

(current, total) => void

Returns

Promise<void>

Copy operation result

Example

typescript
// Copy all .txt files to backup directory
const result = await vfs.copyFileEx('/data/*.txt', '/backup');

// Copy directory to different VFS
const result = await vfsA.copyFileEx('DirToCopy/**/*', 'Foo/DirToCopy', {
  overwrite: true,
  targetVFS: vfsB
});

// Copy specific files
const result = await vfs.copyFileEx(['/file1.txt', '/file2.txt'], '/backup');

glob()

glob(pattern, options?): Promise<GlobResult[]>

Defined in: libs/base/src/vfs/vfs.ts:1084

Query file list by matching pattern(s)

Parameters

pattern

string | string[]

Matching pattern(s)

options?

GlobOptions = {}

Matching options

Returns

Promise<GlobResult[]>

Informations of matching files


guessMIMEType()

guessMIMEType(path): string

Defined in: libs/base/src/vfs/vfs.ts:1201

Parameters

path

string

Returns

string


mount()

mount(path, vfs): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:1219

Mounts another VFS at the specified path.

Constraints/Behavior:

  • Mount path can be an existing directory or non existing directory.
  • Changes from child VFS will bubble to host namespace (path is rewritten with mount prefix)

Parameters

path

string

The path where to mount the VFS

vfs

VFS

The VFS instance to mount

Returns

Promise<void>


unmount()

unmount(path): Promise<boolean>

Defined in: libs/base/src/vfs/vfs.ts:1250

Unmounts a VFS from the specified path.

Behavior:

  • Removes the mount record.
  • If the mount directory was auto-created by this VFS and cleanupOnUnmount is true, it will be removed only if it is still empty.

Parameters

path

string

The path to unmount

Returns

Promise<boolean>

True if a VFS was unmounted, false otherwise


close()

close(): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:1268

Closes file system and release resources

Returns

Promise<void>


onChange()

protected onChange(type, path, itemType): void

Defined in: libs/base/src/vfs/vfs.ts:1315

VFS file changing event

Parameters

type

"created" | "deleted" | "moved" | "modified"

Change type

path

string

File path that causes changing

itemType

"file" | "directory"

File type

Returns

void


_makeDirectory()

abstract protected _makeDirectory(path, recursive): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:1328

Creates a directory in the file system.

Parameters

path

string

Directory path

recursive

boolean

Whether to create parent directories

Returns

Promise<void>


_readDirectory()

abstract protected _readDirectory(path, options?): Promise<FileMetadata[]>

Defined in: libs/base/src/vfs/vfs.ts:1336

Reads the contents of a directory from the file system.

Parameters

path

string

Directory path

options?

ListOptions

Listing options (recursive, filter, etc)

Returns

Promise<FileMetadata[]>

List of FileMetadata for entries in the directory


_deleteDirectory()

abstract protected _deleteDirectory(path, recursive): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:1343

Deletes a directory and its contents (if recursive).

Parameters

path

string

Directory path

recursive

boolean

If true, delete contents recursively

Returns

Promise<void>


_readFile()

abstract protected _readFile(path, options?): Promise<string | ArrayBuffer>

Defined in: libs/base/src/vfs/vfs.ts:1351

Reads a file from the file system.

Parameters

path

string

Path to file

options?

ReadOptions

Read options (encoding, offset, length)

Returns

Promise<string | ArrayBuffer>

File contents as ArrayBuffer or string


_writeFile()

abstract protected _writeFile(path, data, options?): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:1359

Writes data to a file (or stages for writing in memory).

Parameters

path

string

File path

data

string | ArrayBuffer

Data to write

options?

WriteOptions

Write options (append, encoding, create parent dirs)

Returns

Promise<void>


_deleteFile()

abstract protected _deleteFile(path): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:1369

Deletes a file in the file system.

Parameters

path

string

File path

Returns

Promise<void>


_exists()

abstract protected _exists(path): Promise<boolean>

Defined in: libs/base/src/vfs/vfs.ts:1376

Checks if a file or directory exists in the file system.

Parameters

path

string

File or directory path

Returns

Promise<boolean>

True if exists


_stat()

abstract protected _stat(path): Promise<FileStat>

Defined in: libs/base/src/vfs/vfs.ts:1383

Gets statistics/metadata for a file or directory.

Parameters

path

string

File or directory path

Returns

Promise<FileStat>

FileStat (type, size, times, etc)


_deleteFileSystem()

abstract protected _deleteFileSystem(): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:1384

Returns

Promise<void>


_wipe()

abstract protected _wipe(): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:1385

Returns

Promise<void>


_move()

abstract protected _move(sourcePath, targetPath, options?): Promise<void>

Defined in: libs/base/src/vfs/vfs.ts:1394

Moves/renames a file or directory within the same VFS. Implementation should avoid file copying and use metadata operations only.

Parameters

sourcePath

string

Source path (normalized)

targetPath

string

Target path (normalized)

options?

MoveOptions

Move options

Returns

Promise<void>

Released under the MIT License.