/** * @file * * In-memory implementation of Obsidian's `DataAdapter` interface, the filesystem shared by the * `FileSystemAdapter` and `CapacitorAdapter` mocks. */ import type { DataAdapter as DataAdapterOriginal, DataWriteOptions as DataWriteOptionsOriginal, ListedFiles as ListedFilesOriginal, Stat as StatOriginal } from 'obsidian'; import type { AdapterListing } from './types.cjs'; /** * An in-memory `DataAdapter`: text files, binary files and folders live in maps keyed by vault-relative path, * with a creation time, modification time and size kept per file. * * Parent folders are created implicitly whenever a file or folder is written. Paths are case-sensitive unless * {@link InMemoryAdapter.insensitive} is set. */ export declare class InMemoryAdapter implements DataAdapterOriginal { protected readonly basePath: string; /** * Whether the simulated filesystem is case-insensitive. When `true`, {@link InMemoryAdapter.exists} matches paths * ignoring case unless a case-sensitive check is requested. */ insensitive: boolean; /** * Mock-only: whether {@link InMemoryAdapter.trashSystem} can reach a system trash. Turn it off to make it answer * `false`, which is what Obsidian's desktop adapter does when the Electron shell refuses the item and its mobile * adapter when the native call throws — and what makes `Vault.trash`'s fallback to * {@link InMemoryAdapter.trashLocal} reachable in a test. */ isSystemTrashAvailable__: boolean; private readonly binaryFiles; private readonly directories; private readonly fileMeta; private readonly lowerCaseKeys; private readonly textFiles; /** * Creates an empty filesystem holding only the vault root. * * @param basePath - The absolute path the vault pretends to live at, used by `getFullPath`. */ protected constructor(basePath: string); /** * Adds text to the end of a text file, creating the file when it does not exist. * * @param normalizedPath - The vault-relative path of the file. * @param data - The text to append. * @param options - Explicit `ctime` / `mtime` to record; by default `ctime` is kept and `mtime` is now. */ append(normalizedPath: string, data: string, options?: DataWriteOptionsOriginal): Promise; /** * Adds bytes to the end of a binary file, creating the file when it does not exist. * * @param normalizedPath - The vault-relative path of the file. * @param data - The bytes to append. * @param options - Explicit `ctime` / `mtime` to record; by default `ctime` is kept and `mtime` is now. */ appendBinary(normalizedPath: string, data: ArrayBuffer, options?: DataWriteOptionsOriginal): Promise; /** * Copies a file, or a folder together with everything under it, stamping each copied file with the current time * and creating missing parent folders of the destination. As in Obsidian, a file is never copied over an existing * file, while a folder copied onto an existing folder merges into it. The desktop `FileSystemAdapter` mock refuses * to copy a file into a missing folder, as the desktop app does; the mobile app's native copy is not modelled. * * @param normalizedPath - The vault-relative path of the file or folder to copy. * @param normalizedNewPath - The vault-relative path of the copy. * @throws Error when nothing exists at `normalizedPath`, or when a file would be copied onto an existing path. */ copy(normalizedPath: string, normalizedNewPath: string): Promise; /** * Checks whether a file or folder exists at a path. * * @param normalizedPath - The vault-relative path to check. * @param sensitive - Forces a case-sensitive check even when {@link InMemoryAdapter.insensitive} is set. * @returns `true` when a text file, binary file or folder exists at the path. */ exists(normalizedPath: string, sensitive?: boolean): Promise; /** * Resolves a vault-relative path against the adapter's base path. * * @param normalizedPath - The vault-relative path. * @returns The base path joined with `normalizedPath` by a `/`. */ getFullPath(normalizedPath: string): string; /** * Gets the vault's name. * * @returns Always `mock-vault` in the mock. */ getName(): string; /** * Returns a URI the browser engine can load, for example to embed an image. * * @param normalizedPath - The vault-relative path of the file. * @returns A fake `app://local/` URI for the path; nothing is served at it. */ getResourcePath(normalizedPath: string): string; /** * Lists the files and folders directly inside a folder, not recursively. * * @param normalizedPath - The vault-relative path of the folder; an empty string for the vault root. * @returns The vault-relative paths of the direct child files and folders. */ list(normalizedPath: string): Promise; /** * Mock-only: lists every file and folder in the filesystem, at any depth. * * @returns The paths of all text and binary files, and of all folders except the vault root. */ listAll__(): AdapterListing; /** * Creates a folder and any missing parent folders. * * @param normalizedPath - The vault-relative path of the folder. */ mkdir(normalizedPath: string): Promise; /** * Mock-only: synchronous {@link InMemoryAdapter.mkdir}, for seeding a vault without awaiting. * * @param normalizedPath - The vault-relative path of the folder. */ mkdirSync__(normalizedPath: string): void; /** * Reads a text file, transforms its content and writes the result back. * * @param normalizedPath - The vault-relative path of the file. * @param $function - Receives the current content and returns the new content synchronously. * @param options - Write options, as for {@link InMemoryAdapter.write}. * @returns The content that was written. * @throws Error when no text file exists at `normalizedPath`. */ process(normalizedPath: string, $function: (data: string) => string, options?: DataWriteOptionsOriginal): Promise; /** * Reads a text file. * * @param normalizedPath - The vault-relative path of the file. * @returns The file's content. * @throws Error when no text file exists at `normalizedPath`; binary files are not readable as text. */ read(normalizedPath: string): Promise; /** * Reads a binary file. * * @param normalizedPath - The vault-relative path of the file. * @returns The stored buffer itself, not a copy. * @throws Error when no binary file exists at `normalizedPath`; text files are not readable as binary. */ readBinary(normalizedPath: string): Promise; /** * Mock-only: synchronous {@link InMemoryAdapter.read}. * * @param normalizedPath - The vault-relative path of the file. * @returns The file's content. * @throws Error when no text file exists at `normalizedPath`. */ readSync__(normalizedPath: string): string; /** * Deletes a file. Deleting a path that holds no file does nothing. * * @param normalizedPath - The vault-relative path of the file. */ remove(normalizedPath: string): Promise; /** * Moves a file, or a folder together with everything under it, creating missing parent folders of the destination. * Renaming a path onto itself does nothing. * * @param normalizedPath - The current vault-relative path. * @param normalizedNewPath - The new vault-relative path. Nothing may exist there yet, except that a case-only * rename is allowed when {@link InMemoryAdapter.insensitive} is set. * @throws Error when the destination already exists, or when neither a folder nor a file exists at `normalizedPath`. */ rename(normalizedPath: string, normalizedNewPath: string): Promise; /** * Removes a folder together with everything under it. * * `recursive` is ignored here, as Obsidian 1.14.2's mobile adapter ignores it: its `rmdir` always removes * recursively, a non-empty folder included. The desktop `FileSystemAdapter` mock overrides this to refuse a * non-recursive removal, as the desktop app does. * * @param normalizedPath - The vault-relative path of the folder. * @param _recursive - Whether to delete everything under the folder too; ignored. * @throws Error `ENOENT: no such file or directory, lstat …` when nothing exists at `normalizedPath`. */ rmdir(normalizedPath: string, _recursive: boolean): Promise; /** * Retrieves metadata about a file or folder. * * @param normalizedPath - The vault-relative path. * @returns The type, times and size, with all three numbers `0` for a folder; `null` when nothing exists there. */ stat(normalizedPath: string): Promise; /** * Mock-only: synchronous {@link InMemoryAdapter.stat}. * * @param normalizedPath - The vault-relative path. * @returns The type, times and size, with all three numbers `0` for a folder; `null` when nothing exists there. */ statSync__(normalizedPath: string): null | StatOriginal; /** * Moves a file or folder into the vault's `.trash` folder, creating that folder first, exactly as both of * Obsidian's adapters do. The entry keeps its own name there; when that name is taken the copy is numbered from * `2` — `note.md`, then `note 2.md`, `note 3.md` — with the extension kept last. A folder moves with everything * under it, and `.trash` is a dot path, so the vault never tracks what lands there. * * @param normalizedPath - The vault-relative path of the file or folder. * @throws Error `ENOENT: no such file or directory, rename …` when nothing exists at `normalizedPath`; `.trash` * has been created by then, as it has in the real adapters. */ trashLocal(normalizedPath: string): Promise; /** * Moves a file or folder to the system trash. The mock has no system trash to move it to, so the entry is removed * outright — a folder together with everything under it — which is what the vault observes either way. * * @param normalizedPath - The vault-relative path of the file or folder. * @returns `true` once the entry is gone; `false`, having changed nothing, when nothing exists at * `normalizedPath` or when {@link InMemoryAdapter.isSystemTrashAvailable__} is off. Obsidian's `Vault.trash` * falls back to {@link InMemoryAdapter.trashLocal} on a `false`. */ trashSystem(normalizedPath: string): Promise; /** * Writes a text file, overwriting any existing content and creating missing parent folders. * * @param normalizedPath - The vault-relative path of the file. * @param data - The new content. * @param options - Explicit `ctime` / `mtime` to record; by default `ctime` is kept and `mtime` is now. */ write(normalizedPath: string, data: string, options?: DataWriteOptionsOriginal): Promise; /** * Writes a binary file, overwriting any existing content and creating missing parent folders. * * @param normalizedPath - The vault-relative path of the file. * @param data - The new content, stored as is without copying. * @param options - Explicit `ctime` / `mtime` to record; by default `ctime` is kept and `mtime` is now. */ writeBinary(normalizedPath: string, data: ArrayBuffer, options?: DataWriteOptionsOriginal): Promise; /** * Mock-only: synchronous {@link InMemoryAdapter.write}, for seeding a vault without awaiting. * * @param normalizedPath - The vault-relative path of the file. * @param data - The new content. * @param options - Explicit `ctime` / `mtime` to record; by default `ctime` is kept and `mtime` is now. */ writeSync__(normalizedPath: string, data: string, options?: DataWriteOptionsOriginal): void; /** * Throws what Obsidian's desktop `fs.rm` throws for a missing path, before anything is removed. * * @param normalizedPath - The vault-relative path about to be removed. * @throws Error `ENOENT: no such file or directory, lstat …` when nothing exists at `normalizedPath`. */ protected ensureExistsForRmdir(normalizedPath: string): void; private addLowerCaseKey; private copyFile; private ensureParentDirectories; private existsSync; private getAvailableTrashPath; private isDirectChild; private moveMapEntry; private rebuildLowerCaseKeys; }