or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-memory-fs

A simple in-memory filesystem that holds data in a javascript object.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/memory-fs@0.5.x

To install, run

npx @tessl/cli install tessl/npm-memory-fs@0.5.0

index.mddocs/

memory-fs

A simple in-memory filesystem that stores file and directory data in JavaScript objects. It provides a familiar Node.js fs-like API with both synchronous and asynchronous methods for common filesystem operations. Designed for scenarios where temporary file storage is needed without touching the actual filesystem.

Package Information

  • Package Name: memory-fs
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install memory-fs

Core Imports

const MemoryFileSystem = require("memory-fs");

Basic Usage

const MemoryFileSystem = require("memory-fs");

// Create filesystem instance
const fs = new MemoryFileSystem(); // Optionally pass existing data object

// Create directories
fs.mkdirpSync("/a/test/dir");

// Write files
fs.writeFileSync("/a/test/dir/file.txt", "Hello World");

// Read files
const content = fs.readFileSync("/a/test/dir/file.txt"); // Returns Buffer
const text = fs.readFileSync("/a/test/dir/file.txt", "utf-8"); // Returns string

// List directory contents
fs.readdirSync("/a/test"); // Returns ["dir"]

// Check file/directory stats
fs.statSync("/a/test/dir").isDirectory(); // Returns true

// Remove files and directories  
fs.unlinkSync("/a/test/dir/file.txt");
fs.rmdirSync("/a/test/dir");

// Cross-platform path support
fs.mkdirpSync("C:\\\\use\\\\windows\\\\style\\\\paths");

Capabilities

Constructor and Initialization

Create a new in-memory filesystem instance.

/**
 * Create a new MemoryFileSystem instance
 * @param {Object} [data] - Optional initial data object to populate filesystem
 */
class MemoryFileSystem {
  constructor(data);
}

File Existence and Information

Check if files or directories exist and get their metadata.

/**
 * Get internal metadata for a file or directory (synchronous)
 * @param {string} path - File or directory path
 * @returns {Object|undefined} Internal metadata object or undefined if not found
 */
meta(path);

/**
 * Check if a file or directory exists (synchronous)
 * @param {string} path - File or directory path
 * @returns {boolean} True if exists, false otherwise
 */
existsSync(path);

/**
 * Check if a file or directory exists (asynchronous)
 * @param {string} path - File or directory path
 * @param {function} callback - Callback function receiving (exists)
 */
exists(path, callback);

/**
 * Get file or directory statistics (synchronous)
 * @param {string} path - File or directory path
 * @returns {Object} Stat object with isFile(), isDirectory(), etc. methods
 */
statSync(path);

/**
 * Get file or directory statistics (asynchronous)
 * @param {string} path - File or directory path
 * @param {function} callback - Callback function receiving (err, stats)
 */
stat(path, callback);

File Operations

Read and write file contents with various encoding options.

/**
 * Read file contents (synchronous)
 * @param {string} path - File path
 * @param {string|Object} [optionsOrEncoding] - Encoding string or options object
 * @returns {Buffer|string} File contents as Buffer or string if encoding specified
 */
readFileSync(path, optionsOrEncoding);

/**
 * Read file contents (asynchronous)
 * @param {string} path - File path
 * @param {string|Object} [encoding] - Encoding string or options object
 * @param {function} callback - Callback function receiving (err, data)
 */
readFile(path, encoding, callback);

/**
 * Write file contents (synchronous)
 * @param {string} path - File path
 * @param {string|Buffer} content - Content to write
 * @param {string|Object} [optionsOrEncoding] - Encoding string or options object
 */
writeFileSync(path, content, optionsOrEncoding);

/**
 * Write file contents (asynchronous)
 * @param {string} path - File path
 * @param {string|Buffer} content - Content to write
 * @param {string|Object} [encoding] - Optional encoding string or options object
 * @param {function} callback - Callback function receiving (err)
 */
writeFile(path, content, [encoding], callback);

/**
 * Remove a file (synchronous)
 * @param {string} path - File path
 */
unlinkSync(path);

/**
 * Remove a file (asynchronous)
 * @param {string} path - File path
 * @param {function} callback - Callback function receiving (err)
 */
unlink(path, callback);

Directory Operations

Create, read, and remove directories with full cross-platform path support.

/**
 * Read directory contents (synchronous)
 * @param {string} path - Directory path
 * @returns {string[]} Array of file and directory names
 */
readdirSync(path);

/**
 * Read directory contents (asynchronous)
 * @param {string} path - Directory path
 * @param {function} callback - Callback function receiving (err, files)
 */
readdir(path, callback);

/**
 * Create a single directory (synchronous)
 * @param {string} path - Directory path
 * @throws {Error} If parent directory doesn't exist
 */
mkdirSync(path);

/**
 * Create a single directory (asynchronous)
 * @param {string} path - Directory path
 * @param {Object} [options] - Optional options object
 * @param {function} callback - Callback function receiving (err)
 */
mkdir(path, [options], callback);

/**
 * Create directory recursively (synchronous)
 * @param {string} path - Directory path
 */
mkdirpSync(path);

/**
 * Create directory recursively (asynchronous)
 * @param {string} path - Directory path
 * @param {function} callback - Callback function receiving (err)
 */
mkdirp(path, callback);

/**
 * Remove an empty directory (synchronous)
 * @param {string} path - Directory path
 */
rmdirSync(path);

/**
 * Remove an empty directory (asynchronous)
 * @param {string} path - Directory path
 * @param {function} callback - Callback function receiving (err)
 */
rmdir(path, callback);

Stream Operations

Create readable and writable streams for file operations.

/**
 * Create a readable stream for a file
 * @param {string} path - File path
 * @param {Object} [options] - Stream options with start/end properties
 * @returns {ReadableStream} Readable stream for the file
 */
createReadStream(path, options);

/**
 * Create a writable stream for a file
 * @param {string} path - File path
 * @returns {WritableStream} Writable stream for the file
 */
createWriteStream(path);

Instance Properties

Path manipulation utilities available as instance properties.

/**
 * Join path components with proper separator handling
 * @param {string} path - Base path
 * @param {string} request - Path component to join
 * @returns {string} Joined and normalized path
 */
fs.join(path, request);

/**
 * Normalize path by resolving . and .. components
 * @param {string} path - Path to normalize
 * @returns {string} Normalized path
 */
fs.normalize(path);

/**
 * Convert path string to array of components
 * @param {string} path - Path to convert
 * @returns {string[]} Array of path components
 */
fs.pathToArray(path);

Symbolic Link Operations

Limited symbolic link support (readlink throws ENOSYS).

/**
 * Read symbolic link target (synchronous) - NOT IMPLEMENTED
 * @param {string} path - Symbolic link path
 * @throws {Error} Always throws ENOSYS error (not implemented)
 */
readlinkSync(path);

/**
 * Read symbolic link target (asynchronous) - NOT IMPLEMENTED
 * @param {string} path - Symbolic link path
 * @param {function} callback - Callback function receiving (err, linkString)
 */
readlink(path, callback);

Types

Stat Object

/**
 * File/directory statistics object returned by statSync() and stat()
 */
interface StatObject {
  isFile(): boolean;        // Returns true if path is a file
  isDirectory(): boolean;   // Returns true if path is a directory
  isBlockDevice(): boolean; // Always returns false
  isCharacterDevice(): boolean; // Always returns false
  isSymbolicLink(): boolean; // Always returns false
  isFIFO(): boolean;        // Always returns false
  isSocket(): boolean;      // Always returns false
}

Stream Types

/**
 * Readable stream created by createReadStream()
 */
interface ReadableStream {
  // Standard Node.js ReadableStream interface
  on(event: string, listener: function): ReadableStream;
  pipe(destination: WritableStream): WritableStream;
}

/**
 * Writable stream created by createWriteStream()
 */
interface WritableStream {
  // Standard Node.js WritableStream interface
  write(chunk: string|Buffer, encoding?: string, callback?: function): boolean;
  end(chunk?: string|Buffer, encoding?: string, callback?: function): void;
}

Error Handling

memory-fs uses errno-compatible error codes through the MemoryFileSystemError class:

/**
 * Custom error class for filesystem operations
 * @param {Object} err - Error object with code and description
 * @param {string} path - File path that caused the error
 * @param {string} operation - Operation that failed
 */
class MemoryFileSystemError extends Error {
  constructor(err, path, operation);
  
  name: string;     // Error name ("MemoryFileSystemError")
  message: string;  // Formatted error message
  code: string;     // Error code (ENOENT, EISDIR, ENOTDIR, etc.)
  errno: number;    // Error number
  path: string;     // Path that caused the error
  operation: string; // Operation that failed
}

Common Error Codes:

  • ENOENT: File or directory does not exist
  • EISDIR: Operation expected a file but found a directory
  • ENOTDIR: Operation expected a directory but found a file
  • EEXIST: File or directory already exists
  • EPERM: Operation not permitted
  • ENOSYS: Function not implemented (readlink operations)

Key Features

  • Cross-platform paths: Supports both POSIX (/path/to/file) and Windows (C:\\path\\to\\file) style paths
  • Node.js compatibility: API closely mirrors Node.js fs module with familiar method names and signatures
  • Error compatibility: Uses errno-compatible error codes for consistent error handling
  • Stream support: Provides readable and writable streams for large file operations
  • Encoding support: Handles various text encodings for string/Buffer conversion
  • Recursive operations: mkdirpSync() creates parent directories automatically
  • In-memory storage: Files stored as Buffer objects, directories marked with special properties