Skip to content

Commit

Permalink
Moved configuration code from index.ts to config.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Mar 22, 2024
1 parent da3d1ba commit 01a9d42
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 58 deletions.
54 changes: 54 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { Backend, BackendConfig } from './backends/backend.js';
import { isBackend, resolveBackendConfig } from './backends/backend.js';
import { Cred } from './cred.js';
import * as fs from './emulation/index.js';
import { setCred, type MountMapping } from './emulation/shared.js';
import { FileSystem } from './filesystem.js';

/**
* Initializes BrowserFS with the given file systems.
*/
export function initialize(mounts: { [point: string]: FileSystem }, uid: number = 0, gid: number = 0) {
setCred(new Cred(uid, gid, uid, gid, uid, gid));
fs.initialize(mounts);
}

/**
* Defines a mapping of mount points to their configurations
*/
export interface ConfigMapping {
[mountPoint: string]: FileSystem | BackendConfig | Backend;
}

/**
* A configuration for BrowserFS
*/
export type Configuration = FileSystem | BackendConfig | ConfigMapping;

/**
* Creates filesystems with the given configuration, and initializes BrowserFS with it.
* See the Configuration type for more info on the configuration object.
*/
export async function configure(config: Configuration): Promise<void> {
if ('backend' in config || config instanceof FileSystem) {
// single FS
config = { '/': <BackendConfig | FileSystem>config };
}
for (let [point, value] of Object.entries(config)) {

Check warning on line 37 in src/config.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

'point' is never reassigned. Use 'const' instead

Check warning on line 37 in src/config.ts

View workflow job for this annotation

GitHub Actions / macos-latest

'point' is never reassigned. Use 'const' instead

Check warning on line 37 in src/config.ts

View workflow job for this annotation

GitHub Actions / windows-latest

'point' is never reassigned. Use 'const' instead
if (typeof value == 'number') {
//should never happen
continue;
}

if (value instanceof FileSystem) {
continue;
}

if (isBackend(value)) {
value = { backend: value };
}

config[point] = await resolveBackendConfig(value);
}
initialize(<MountMapping>config);
}
62 changes: 4 additions & 58 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,12 @@
/**
* BrowserFS's main module. This is exposed in the browser via the BrowserFS global.
*/
import * as fs from './emulation/index.js';
import { FileSystem } from './filesystem.js';
import { Cred } from './cred.js';
import { isBackend, type Backend, type BackendConfig, resolveBackendConfig } from './backends/backend.js';
import { type MountMapping, setCred } from './emulation/shared.js';

/**
* Initializes BrowserFS with the given file systems.
*/
export function initialize(mounts: { [point: string]: FileSystem }, uid: number = 0, gid: number = 0) {
setCred(new Cred(uid, gid, uid, gid, uid, gid));
fs.initialize(mounts);
}

/**
* Defines a mapping of mount points to their configurations
*/
export interface ConfigMapping {
[mountPoint: string]: FileSystem | BackendConfig | Backend;
}

/**
* A configuration for BrowserFS
*/
export type Configuration = FileSystem | BackendConfig | ConfigMapping;

/**
* Creates filesystems with the given configuration, and initializes BrowserFS with it.
* See the Configuration type for more info on the configuration object.
*/
export async function configure(config: Configuration): Promise<void> {
if ('backend' in config || config instanceof FileSystem) {
// single FS
config = <ConfigMapping>{ '/': config };
}
for (let [point, value] of Object.entries(config)) {
if (typeof value == 'number') {
//should never happen
continue;
}

if (value instanceof FileSystem) {
continue;
}

if (isBackend(value)) {
value = { backend: value };
}

config[point] = await resolveBackendConfig(value);
}
initialize(<MountMapping>config);
}

export type { Backend, BackendConfig } from './backends/backend.js';
export * from './backends/backend.js';
export * from './backends/AsyncMirror.js';
export * from './backends/AsyncStore.js';
export * from './backends/InMemory.js';
export * from './backends/Locked.js';
export * from './backends/Overlay.js';
export * from './backends/SyncStore.js';
export * from './ApiError.js';
export * from './config.js';
export * from './cred.js';
export * from './file.js';
export * from './filesystem.js';
Expand All @@ -71,5 +15,7 @@ export * from './inode.js';
export * from './mutex.js';
export * from './stats.js';
export * from './utils.js';

import * as fs from './emulation/index.js';
export { fs };
export default fs;

0 comments on commit 01a9d42

Please sign in to comment.