Loopback Harness

Run your driver in the current process through an fs/promises-shaped API. This needs no kernel or privileges and works on every platform.

import { createLoopback } from "mountx";
import type { Loopback } from "mountx";

#createLoopback(driver)

function createLoopback(driver: FsDriver): Loopback;

The returned object has every optional driver method. A missing method throws ENOSYS. The object normalizes each path and resolves all capabilities. It also adds two whole-file helper methods.

interface Loopback extends Required<Omit<FsDriver, "capabilities" | "mountx">> {
  readonly driver: FsDriver;
  readonly capabilities: ResolvedCapabilities;
  readonly mountx: MountxExtensions | undefined;
  readFile(path: string): Promise<Uint8Array>;
  writeFile(path: string, data: string | Uint8Array): Promise<void>;
}
const fs = createLoopback(driver);

fs.capabilities; // every capability resolved
fs.driver; // the driver you passed
await fs.writeFile("/a.txt", "text"); // create + truncate
await fs.readFile("/a.txt"); // no handle dance

Driver authors test against this API. The Tier-0 conformance suite also uses it. If an operation works through createLoopback(), it works through a mount. The loopback harness is not a mock. It uses the same path normalization and capability layer as a real session. It does not include the kernel.

Note

createLoopback() reads the driver's shape one time, when you create the loopback.

It records the available methods and function values at that time.

Each session creates one loopback and sends every request through it.

If you add a method to the driver later, the existing loopback does not use it.

Pass a complete driver or create a new loopback.

#Next

  • Mounting shows how to expose the same driver as a directory.
  • Capabilities explains the values in fs.capabilities.

mountx  Write a filesystem in JavaScript, mount it as a real folder.