CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jest-haste-map

A module used by Jest to create a fast lookup of files in a project

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

virtual-file-system.mddocs/

Virtual File System

Fast file system operations through a virtual interface that provides cached access to file metadata, dependencies, and content information. The HasteFS provides an efficient abstraction over the file system with built-in caching and fast lookup capabilities.

Capabilities

File Existence and Metadata

Core file system operations for checking file existence and retrieving metadata.

/**
 * Check if a file exists in the virtual file system
 * @param path - Absolute path to the file
 * @returns True if file exists in the mapped file system
 */
exists(path: string): boolean;

/**
 * Get the size of a file in bytes
 * @param path - Absolute path to the file
 * @returns File size in bytes, or null if file doesn't exist
 */
getSize(path: string): number | null;

/**
 * Get the module name (Haste ID) for a file
 * @param file - Absolute path to the file
 * @returns Module name if file has one, null otherwise
 */
getModuleName(file: string): string | null;

/**
 * Get the SHA-1 hash of a file if computed during crawling
 * @param file - Absolute path to the file
 * @returns SHA-1 hash as hex string, or null if not computed or file doesn't exist
 */
getSha1(file: string): string | null;

File Iteration and Listing

Methods for iterating over and listing files in the virtual file system.

/**
 * Get all files tracked by the haste map as absolute paths
 * @returns Array of all absolute file paths
 */
getAllFiles(): Array<string>;

/**
 * Get iterator for absolute file paths, memory efficient for large projects
 * @returns Iterable yielding absolute file paths
 */
getAbsoluteFileIterator(): Iterable<string>;

Dependency Analysis

Access to computed file dependencies for module resolution and build optimization.

/**
 * Get the dependencies of a file as extracted during crawling
 * @param file - Absolute path to the file
 * @returns Array of dependency specifiers, null if file not found, empty array if no dependencies
 */
getDependencies(file: string): Array<string> | null;

Pattern Matching and Search

Powerful file search capabilities using regular expressions and glob patterns.

/**
 * Find files matching a regular expression or string pattern
 * @param pattern - RegExp or string pattern to match against absolute file paths
 * @returns Array of absolute paths to matching files
 */
matchFiles(pattern: RegExp | string): Array<string>;

/**
 * Find files matching glob patterns with optional root restriction
 * @param globs - Array of glob patterns to match
 * @param root - Optional root directory to make patterns relative to
 * @returns Set of absolute paths to matching files
 */
matchFilesWithGlob(
  globs: ReadonlyArray<string>,
  root: string | null
): Set<string>;

Usage Examples:

import HasteMap from "jest-haste-map";

const hasteMap = await HasteMap.create({
  id: "my-project",
  extensions: ["js", "ts", "jsx", "tsx"],
  maxWorkers: 4,
  platforms: [],
  roots: ["/src"],
  retainAllFiles: true,
  rootDir: "/project/root",
});

const {hasteFS} = await hasteMap.build();

// Check file existence
if (hasteFS.exists("/project/root/src/components/Button.tsx")) {
  console.log("Button component exists");
}

// Get file metadata
const buttonSize = hasteFS.getSize("/project/root/src/components/Button.tsx");
console.log(`Button component is ${buttonSize} bytes`);

// Get all tracked files
const allFiles = hasteFS.getAllFiles();
console.log(`Tracking ${allFiles.length} files`);

// Memory-efficient iteration for large projects
for (const filePath of hasteFS.getAbsoluteFileIterator()) {
  if (filePath.endsWith('.test.js')) {
    console.log(`Found test file: ${filePath}`);
  }
}

// Get file dependencies
const dependencies = hasteFS.getDependencies("/project/root/src/App.tsx");
if (dependencies) {
  console.log("App.tsx depends on:", dependencies);
}

// Find files by pattern
const componentFiles = hasteFS.matchFiles(/\/components\/.*\.tsx$/);
console.log("Component files:", componentFiles);

// Find files by glob patterns
const testFiles = hasteFS.matchFilesWithGlob(
  ["**/*.test.js", "**/*.spec.js"],
  "/project/root"
);
console.log("Test files:", Array.from(testFiles));

// Find TypeScript files
const tsFiles = hasteFS.matchFiles("\\.ts$");
console.log("TypeScript files:", tsFiles);

// Get module names for Haste modules
const moduleName = hasteFS.getModuleName("/project/root/src/Button.js");
if (moduleName) {
  console.log(`File exports module: ${moduleName}`);
}

// Get SHA-1 hash if computed during crawling
const buttonSha1 = hasteFS.getSha1("/project/root/src/components/Button.tsx");
if (buttonSha1) {
  console.log(`Button component SHA-1: ${buttonSha1}`);
}

HasteFS Implementation Details

The HasteFS class provides the concrete implementation of the IHasteFS interface.

/**
 * Virtual file system implementation providing cached access to file metadata
 * and dependencies extracted during the haste map build process
 */
class HasteFS implements IHasteFS {
  /**
   * Create a HasteFS instance
   * @param options - Configuration with files map and root directory
   */
  constructor(options: {rootDir: string; files: FileData});
}

File Data Structure

Internal representation of file metadata used by HasteFS.

/**
 * Map of relative file paths to metadata arrays
 */
type FileData = Map<string, FileMetaData>;

/**
 * Efficient array-based file metadata storage
 * [id, mtime, size, visited, dependencies, sha1]
 */
type FileMetaData = [
  id: string,          // Module ID/name if applicable
  mtime: number,       // Last modified time in milliseconds
  size: number,        // File size in bytes
  visited: 0 | 1,      // Whether file has been processed (0 = false, 1 = true)
  dependencies: string, // Serialized dependencies joined by null character
  sha1: string | null | undefined, // SHA-1 hash if computed
];

Integration with Module Resolution

HasteFS works closely with ModuleMap to provide complete project introspection.

// Combined usage with module resolution
const {hasteFS, moduleMap} = await hasteMap.build();

// Find a module and get its dependencies
const buttonPath = moduleMap.getModule("Button");
if (buttonPath && hasteFS.exists(buttonPath)) {
  const buttonDeps = hasteFS.getDependencies(buttonPath);
  console.log("Button dependencies:", buttonDeps);
  
  const buttonSize = hasteFS.getSize(buttonPath);
  console.log(`Button is ${buttonSize} bytes`);
}

// Search for files and resolve them as modules
const utilFiles = hasteFS.matchFiles(/\/utils\/.*\.js$/);
for (const utilFile of utilFiles) {
  const moduleName = hasteFS.getModuleName(utilFile);
  if (moduleName) {
    console.log(`Utility ${moduleName} at ${utilFile}`);
  }
}

docs

file-system-mapping.md

file-system-watching.md

index.md

module-resolution.md

virtual-file-system.md

tile.json