hasher.ts


Overview

The [hasher.ts](/projects/291/68807) file provides functionality to generate a cryptographic hash that represents the content of key components within the project’s source tree. This hash is primarily used to uniquely identify the state of the project files that contribute to the final build image (referred to as the "base"). By hashing selected files and directories, the system can detect changes efficiently, which is useful for cache invalidation, build optimization, or integrity verification.

The core feature of this file is the asynchronous function `getBaseHash`, which computes a SHA-1 hash over specific selected files and directories, including configuration files at the root, package source files, common coinstack code, and coinstack dependencies. It leverages the `folder-hash` package to recursively hash folder contents with precise include/exclude filters, and Node.js’s native `crypto` module for hashing.


Detailed Explanation

Imports

import { createHash } from 'crypto'
import { hashElement } from 'folder-hash'

Constants

const rootDir = `${__dirname}/../..`
const nodeDir = `${__dirname}/../../node`

Function: getBaseHash

export const getBaseHash = async (): Promise<string>

Purpose

Computes a SHA-1 hash string representing the current state of the build-related files and directories, effectively summarizing the base build context.

Parameters

Returns

Implementation Details

  1. Initialize SHA-1 Hash Context

    • Uses Node.js createHash('sha1') to create a hash object that will be updated incrementally.

  2. Hash Root-Level Unchained Files

    • Calls hashElement on rootDir.

    • Filters:

      • Excludes all folders and files by default (folders.exclude: ['.*', '*']).

      • Includes only specific files: package.json, lerna.json, yarn.lock, Dockerfile.node.

    • Updates the SHA-1 hash with the resulting folder hash.

  3. Hash Contents of Packages

    • Targets ${nodeDir}/packages.

    • Includes all folders except hidden (.*), dist, node_modules, and pulumi.

    • Includes files with extensions .ts, .json, and Dockerfile.

    • Updates the SHA-1 hash with this packages hash.

  4. Hash Common Coinstack Code

    • Targets ${nodeDir}/coinstacks/common.

    • Same folder and file filters as packages.

    • Updates the SHA-1 hash with the common coinstack hash.

  5. Hash Coinstacks Dependencies

    • Targets ${nodeDir}/coinstacks.

    • Includes all folders except hidden, common, dist, node_modules, and pulumi.

    • Includes only package.json files.

    • Updates the SHA-1 hash with the dependencies hash.

  6. Return Final Hash

    • Calls hash.digest('hex') to produce the final hexadecimal string.

Usage Example

import { getBaseHash } from './hasher'

async function main() {
  const baseHash = await getBaseHash()
  console.log('Current base hash:', baseHash)
}

main()

This snippet will print the unique hash representing the current state of the base build files.


Important Implementation Notes


Interaction with Other Parts of the System


Mermaid Diagram: Class / Function Structure

Since this file contains a single exported asynchronous function without any classes, the diagram below illustrates the functional flow and relationships between the hashing steps within `getBaseHash`.

flowchart TD
    A[getBaseHash()]
    A --> B[hash rootDir files]
    B --> C[hash packages directory]
    C --> D[hash coinstacks/common directory]
    D --> E[hash coinstacks dependencies]
    E --> F[combine all hashes]
    F --> G[return SHA-1 digest string]

Summary

The [hasher.ts](/projects/291/68807) file is a utility module responsible for creating a composite SHA-1 hash that captures the state of critical source files and configuration within the project. This hash aids in build optimization by detecting changes relevant to the base build image. It uses recursive folder hashing with precise include/exclude rules to ensure accurate and meaningful hashing, and exposes a single asynchronous function `getBaseHash` for external use.


End of Documentation