memfs

repository·master·Indexed 24 days ago

https://github.com/streamich/memfs

A collection of JavaScript utilities providing in-memory implementations of the Node.js fs module and the browser File System API. The ecosystem includes @jsonjoy.com/fs-core for fundamental primitives, @jsonjoy.com/fs-node for Node.js fs-compatible volumes, and adapters like @jsonjoy.com/fs-fsa-to-node and @jsonjoy.com/fs-node-to-fsa to bridge the gap between Node.js and the Web File System Access API.

Tokens
51.5K
Snippets
99
Records
289
Agent score
82%

What's inside memfs

  1. Overview of memfs APIs and capabilities

    master

    memfs is an in-memory file system that supports two primary interfaces:

    1. Node's fs module: A drop-in replacement for the standard Node.js fs module. It is ideal for mocks, tests, bundlers, and sandboxes where you want files to live in memory instead of on disk.
    2. Browser File System Access (FSA) API: Implements the FileSystemDirectoryHandle interface, allowing you to use an in-memory filesystem in a browser environment.

    Key Features:

    • Cross-API Adapters: Includes adapters to translate between the fs API and the FSA API, allowing you to run fs-based code in the browser on top of real directories, or expose an fs-like filesystem through FSA handles.
    • Runtime Support: Runs in Node.js, Deno, Bun, and browsers.
    • Storage: Uses Uint8Array buffers for file contents.
    • Ecosystem: Built from specialized @jsonjoy.com/fs-* packages, including utilities for snapshots (POJO/CBOR/JSON) and ASCII tree printing.
  2. Overview of memfs Node `fs` API implementation

    master

    memfs provides an in-memory implementation of the Node.js fs API. It is designed to mimic Node's file system behavior while storing all data in memory using Buffers.

    Key features include:

    • Full implementation of Node's fs API (subject to the missing list).
    • In-memory storage using Buffers.
    • Error handling that mimics Node.js error types.
    • Support for i-nodes.
    • Support for hard links.
    • Support for soft links (symbolic links).
    • Compatibility with browser environments.
  3. Overview of memfs features

    master

    memfs is an in-memory file system library designed for testing, mocking, or creating virtual file systems. Key capabilities include:

    • Node.js fs module API compatibility: Use standard Node.js file system methods.
    • Browser File System (Access) API implementation: Use modern web-standard file system APIs.
    • Adapters: Tools to convert between the fs and File System API interfaces.
    • Utilities: Support for directory snapshots and tree printing.
    • Environment Support: Works in both Node.js and modern browsers with full TypeScript support.
  4. Overview of memfs capabilities

    master

    memfs provides JavaScript file system utilities for both Node.js and the browser. It includes:

    • An in-memory implementation of the Node.js fs module API.
    • An in-memory implementation of the browser File System API.
    • Adapters to bridge the two worlds:
      • fs module to File System API adapter.
      • File System API to fs module adapter.
  5. What is `crudfs`?

    master

    crudfs is a CRUD-like file system abstraction that acts as a thin wrapper around a real file system. It allows you to perform standard Create, Read, Update, and Delete operations on files and folders using a simplified API. It is designed to be lightweight and suitable for browser bundling.

    In the crudfs mental model:

    • Folders are referred to as collections or types.
    • Files are referred to as resources.
  6. What is `casfs` and how does it work?

    master

    casfs is a Content Addressable Storage (CAS) abstraction over a file system. Unlike traditional file systems, it does not use folders or files. Instead, it manages blobs that are identified by their content.

    It provides two primary operations:

    1. put: Takes a blob, stores it in the underlying file system, and returns the blob's hash digest.
    2. get: Takes a hash digest and returns the corresponding blob if it exists.
  7. Understand the snapshot encoding format

    master

    Snapshots follow the Compact JSON scheme, where each node is a tuple. The first element of the tuple defines the SnapshotNodeType:

    • 0 (Folder): [type, metadata, childrenMap]
    • 1 (File): [type, metadata, contentsAsUint8Array]
    • 2 (Symlink): [type, metadataWithTarget]

    Example Structures:

    Folder:

    [
      0,
      {},
      {
        'file.bin': [1, {}, new Uint8Array([1, 2, 3])],
      },
    ];

    File:

    [1, {}, new Uint8Array([1, 2, 3])];

    Symlink:

    [2, { target: 'file.bin' }];