Skip to content

Commit

Permalink
Added default FS
Browse files Browse the repository at this point in the history
Changed initialize to use mount
Removed unused options parameter for InMemory.Create
Added some comments
  • Loading branch information
james-pre committed Sep 23, 2023
1 parent 9994369 commit d2aad42
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/backends/InMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class InMemoryFileSystem extends SyncKeyValueFileSystem {

public static readonly Options: BackendOptions = {};

public static async Create(opts: object): Promise<InMemoryFileSystem> {
public static async Create(): Promise<InMemoryFileSystem> {
return new InMemoryFileSystem();
}

Expand Down
24 changes: 21 additions & 3 deletions src/emulation/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Cred } from '../cred';
import { FileSystem } from '../filesystem';
import { File } from '../file';
import { InternalBackendConstructor } from '../backends';
import { InMemoryFileSystem } from '../backends/InMemory';

/**
* converts Date or number to a fractional UNIX timestamp
Expand Down Expand Up @@ -122,9 +123,23 @@ export interface MountMapping {

export const mounts: Map<string, FileSystem> = new Map();

/*
Set a default root.
There is a very small but not 0 change that initialize() will try to unmount the default before it is mounted.
This can be fixed by using a top-level await, which is not done to maintain ES6 compatibility.
*/
InMemoryFileSystem.Create().then(fs => mount('/', fs));

/**
* Gets the file system mounted at `mountPoint`
*/
export function getMount(mountPoint: string): FileSystem {
return mounts.get(mountPoint);
}

/**
* Gets an object of mount points (keys) and filesystems (values)
*/
export function getMounts(): MountMapping {
return Object.fromEntries(mounts.entries());
}
Expand All @@ -143,6 +158,9 @@ export function mount(mountPoint: string, fs: FileSystem): void {
mounts.set(mountPoint, fs);
}

/**
* Unmounts the file system at the given mount point.
*/
export function umount(mountPoint: string): void {
if (mountPoint[0] !== '/') {
mountPoint = `/${mountPoint}`;
Expand Down Expand Up @@ -190,15 +208,15 @@ export function fixError<E extends Error>(e: E, paths: { [from: string]: string
}

export function initialize(mountMapping: MountMapping): void {
if (!mountMapping['/']) {
throw new ApiError(ErrorCode.EINVAL, 'BrowserFS must be initialized with a root filesystem.');
if (mountMapping['/']) {
umount('/');
}
for (const [point, fs] of Object.entries(mountMapping)) {
const FS = fs.constructor as unknown as InternalBackendConstructor;
if (!FS.isAvailable()) {
throw new ApiError(ErrorCode.EINVAL, `Can not mount "${point}" since the filesystem is unavailable.`);
}

mounts.set(point, fs);
mount(point, fs);
}
}

0 comments on commit d2aad42

Please sign in to comment.