diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 000000000..e708b79b1 --- /dev/null +++ b/src/config.ts @@ -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 { + if ('backend' in config || config instanceof FileSystem) { + // single FS + config = { '/': 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(config); +} diff --git a/src/index.ts b/src/index.ts index ae8d0d074..061ec4f7b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,61 +1,4 @@ -/** - * 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 { - if ('backend' in config || config instanceof FileSystem) { - // single FS - config = { '/': 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(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'; @@ -63,6 +6,7 @@ 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'; @@ -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;