Options
All
  • Public
  • Public/Protected
  • All
Menu

HasteMap is a JavaScript implementation of Facebook's haste module system.

This implementation is inspired by https://github.com/facebook/node-haste and was built with for high-performance in large code repositories with hundreds of thousands of files. This implementation is scalable and provides predictable performance.

Because the haste map creation and synchronization is critical to startup performance and most tasks are blocked by I/O this class makes heavy use of synchronous operations. It uses worker processes for parallelizing file access and metadata extraction.

The data structures created by jest-haste-map can be used directly from the cache without further processing. The metadata objects in the files and map objects contain cross-references: a metadata object from one can look up the corresponding metadata object in the other map. Note that in most projects, the number of files will be greater than the number of haste modules one module can refer to many files based on platform extensions.

type HasteMap = { clocks: WatchmanClocks, files: {[filepath: string]: FileMetaData}, map: {[id: string]: ModuleMapItem}, mocks: {[id: string]: string}, }

// Watchman clocks are used for query synchronization and file system deltas. type WatchmanClocks = {[filepath: string]: string};

type FileMetaData = { id: ?string, // used to look up module metadata objects in map. mtime: number, // check for outdated files. size: number, // size of the file in bytes. visited: boolean, // whether the file has been parsed or not. dependencies: Array, // all relative dependencies of this file. sha1: ?string, // SHA-1 of the file, if requested via options. };

// Modules can be targeted to a specific platform based on the file name. // Example: platform.ios.js and Platform.android.js will both map to the same // Platform module. The platform should be specified during resolution. type ModuleMapItem = {[platform: string]: ModuleMetaData};

// type ModuleMetaData = { path: string, // the path to look up the file object in files. type: string, // the module type (either package or module). };

Note that the data structures described above are conceptual only. The actual implementation uses arrays and constant keys for metadata storage. Instead of {id: 'flatMap', mtime: 3421, size: 42, visited: true, dependencies: []} the real representation is similar to ['flatMap', 3421, 42, 1, []] to save storage space and reduce parse and write time of a big JSON blob.

The HasteMap is created as follows:

  1. read data from the cache or create an empty structure.

  2. crawl the file system.

    • empty cache: crawl the entire file system.
    • cache available:
      • if watchman is available: get file system delta changes.
      • if watchman is unavailable: crawl the entire file system.
    • build metadata objects for every file. This builds the files part of the HasteMap.
  3. parse and extract metadata from changed files.

    • this is done in parallel over worker processes to improve performance.
    • the worst case is to parse all files.
    • the best case is no file system access and retrieving all data from the cache.
    • the average case is a small number of changed files.
  4. serialize the new HasteMap in a cache file. Worker processes can directly access the cache through HasteMap.read().

Hierarchy

Index

Type aliases

Static FS

Static HasteChangeEvent

HasteChangeEvent: ChangeEvent

Static HasteMapObject

HasteMapObject: InternalHasteMapObject

Static SerializableModuleMap

SerializableModuleMap: HasteSerializableModuleMap

Constructors

constructor

  • Parameters

    Returns HasteMap

Properties

Private _buildFileMap

_buildFileMap: any
  1. crawl the file system.

Private _buildHasteMap

_buildHasteMap: any

Private _buildPromise

_buildPromise: any

Private _cachePath

_cachePath: any

Private Optional _changeInterval

_changeInterval: any

Private _cleanup

_cleanup: any

Private _console

_console: any

Private _crawl

_crawl: any

Private _createEmptyMap

_createEmptyMap: any

Private _getWorker

_getWorker: any

Creates workers or parses files and extracts metadata in-process.

Private _ignore

_ignore: any

Helpers

Private _isNodeModulesDir

_isNodeModulesDir: any

Private _options

_options: any

Private _persist

_persist: any
  1. serialize the new HasteMap in a cache file.

Private _processFile

_processFile: any
  1. parse and extract metadata from changed files.

Private _recoverDuplicates

_recoverDuplicates: any

This function should be called when the file under filePath is removed or changed. When that happens, we want to figure out if that file was part of a group of files that had the same ID. If it was, we want to remove it from the group. Furthermore, if there is only one file remaining in the group, then we want to restore that single file as the correct resolution for its ID, and cleanup the duplicates index.

Private _watch

_watch: any

Watch mode

Private _watchers

_watchers: any

Private _whitelist

_whitelist: any

Private _worker

_worker: any

Static DuplicateError

DuplicateError: DuplicateError

Static H

Static ModuleMap

ModuleMap: HasteModuleMap

Methods

addListener

  • addListener(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

build

  • build(): Promise<InternalHasteMapObject>
  • Returns Promise<InternalHasteMapObject>

emit

  • emit(event: string | symbol, ...args: any[]): boolean
  • Parameters

    • event: string | symbol
    • Rest ...args: any[]

    Returns boolean

end

  • end(): Promise<void>
  • Returns Promise<void>

eventNames

  • eventNames(): Array<string | symbol>
  • Returns Array<string | symbol>

getCacheFilePath

  • getCacheFilePath(): string
  • Returns string

getMaxListeners

  • getMaxListeners(): number

listenerCount

  • listenerCount(type: string | symbol): number
  • Parameters

    • type: string | symbol

    Returns number

listeners

  • listeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

off

  • off(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

on

  • on(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

once

  • once(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

prependListener

  • prependListener(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

prependOnceListener

  • prependOnceListener(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

rawListeners

  • rawListeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

read

    1. read data from the cache or create an empty structure.

    Returns InternalHasteMap

readModuleMap

  • readModuleMap(): HasteModuleMap
  • Returns HasteModuleMap

removeAllListeners

  • removeAllListeners(event?: string | symbol): this
  • Parameters

    • Optional event: string | symbol

    Returns this

removeListener

  • removeListener(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

setMaxListeners

  • setMaxListeners(n: number): this

Static getCacheFilePath

  • getCacheFilePath(tmpdir: Config.Path, name: string, ...extra: Array<string>): string
  • Parameters

    • tmpdir: Config.Path
    • name: string
    • Rest ...extra: Array<string>

    Returns string

Generated using TypeDoc