or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration-management.mdcontent-hashing.mdfile-operations.mdindex.mdpath-utilities.mdprocess-coordination.mdsystem-information.md
tile.json

content-hashing.mddocs/

Content Hashing

Content hashing functionality for generating consistent, fast digests used throughout Gatsby's build process for caching, content deduplication, and change detection.

Capabilities

Content Digest Creation

Creates an MD5 hash digest of any input, with specialized handling for objects vs primitive values.

/**
 * Creates an MD5 hash digest of the input
 * Uses object-hash for objects/arrays, crypto.createHash for primitives
 * @param input - The data to hash (object, string, Buffer, or any primitive)
 * @returns Hexadecimal string representation of the MD5 hash
 */
function createContentDigest(input: BinaryLike | string | any): string;

Usage Examples:

import { createContentDigest } from "gatsby-core-utils";

// Hash objects for consistent caching
const pageData = {
  title: "My Page",
  content: "Page content here",
  metadata: { author: "John Doe" }
};
const pageHash = createContentDigest(pageData);
console.log(pageHash); // "a1b2c3d4e5f6789..."

// Hash strings
const stringHash = createContentDigest("Hello, world!");
console.log(stringHash); // "6cd3556deb0da54bca060b4c39479839"

// Hash binary data
const buffer = Buffer.from("Binary data", "utf8");
const bufferHash = createContentDigest(buffer);

Advanced Hash Functions

High-performance hash functions from the hash-wasm library for specialized use cases.

/**
 * Creates MD5 hash using WebAssembly implementation
 * @param data - String or Uint8Array to hash
 * @returns Promise resolving to hex-encoded hash
 */
function md5(data: string | Uint8Array): Promise<string>;

/**
 * Creates reusable MD5 hasher instance for streaming operations
 * @returns Promise resolving to HashInterface for incremental hashing
 */
function createMD5(): Promise<HashInterface>;

/**
 * Creates SHA-256 hash using WebAssembly implementation  
 * @param data - String or Uint8Array to hash
 * @returns Promise resolving to hex-encoded hash
 */
function sha256(data: string | Uint8Array): Promise<string>;

/**
 * Creates SHA-1 hash using WebAssembly implementation
 * @param data - String or Uint8Array to hash  
 * @returns Promise resolving to hex-encoded hash
 */
function sha1(data: string | Uint8Array): Promise<string>;

Usage Examples:

import { md5, sha256, createMD5 } from "gatsby-core-utils";

// Simple MD5 hashing
const hash = await md5("Hello, world!");
console.log(hash); // WebAssembly-based MD5

// SHA-256 for stronger security requirements
const secureHash = await sha256("Sensitive data");
console.log(secureHash);

// Streaming MD5 for large data
const hasher = await createMD5();
hasher.update("First chunk");
hasher.update("Second chunk");
const streamHash = hasher.digest();

File-based MD5 Hashing

Memory-efficient MD5 hashing for files using Node.js streams.

/**
 * Creates MD5 hash from a file using streams for memory efficiency
 * @param filePath - Absolute path to the file to hash
 * @returns Promise resolving to MD5 hash in hexadecimal format
 * @throws Error if file cannot be read or hashing fails
 */
function md5File(filePath: string): Promise<string>;

Usage Examples:

import { md5File } from "gatsby-core-utils";

// Hash large files efficiently
const fileHash = await md5File("/path/to/large-image.jpg");
console.log(`File hash: ${fileHash}`);

// Use in build pipelines for change detection
const configHash = await md5File("./gatsby-config.js");
if (configHash !== previousHash) {
  console.log("Configuration changed, rebuilding...");
}

MurmurHash Algorithm

Fast, non-cryptographic hashing using MurmurHash2 algorithm for performance-critical scenarios.

/**
 * MurmurHash2 implementation for fast, non-cryptographic hashing
 * @param str - String to hash
 * @param seed - Seed value for hash function (affects output)
 * @returns 32-bit unsigned integer hash value
 */
function murmurhash(str: string, seed: number): number;

Usage Examples:

import { murmurhash } from "gatsby-core-utils";

// Fast hashing for internal data structures
const hash1 = murmurhash("page-id-123", 0);
const hash2 = murmurhash("page-id-123", 42); // Different seed = different hash

// Use for bucketing or partitioning
const bucket = murmurhash(userId, 0) % 10; // Distribute across 10 buckets

Types

interface HashInterface {
  update(data: string | Uint8Array): HashInterface;
  digest(): string;
}

type BinaryLike = string | NodeJS.ArrayBufferView;

Performance Characteristics

  • createContentDigest: Optimized for mixed object/primitive data, uses object-hash for deterministic object serialization
  • hash-wasm functions: WebAssembly-based implementations providing significant performance improvements over pure JavaScript
  • md5File: Stream-based processing prevents memory issues with large files
  • murmurhash: Fastest option for non-cryptographic use cases, ideal for internal data structures

Security Considerations

  • MD5 is cryptographically broken but sufficient for content addressing and caching
  • Use SHA-256 for security-sensitive applications
  • MurmurHash is not cryptographically secure and should only be used for performance-critical internal operations