All files / packages/design-system-2-site/src/lib/zeroheight hash.js

100% Statements 5/5
100% Branches 0/0
100% Functions 4/4
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38            5x               243x               241x     1421x                   240x    
/**
 * Content hashing for drift detection.
 */
 
import { createHash } from 'node:crypto';
 
const SIGNED_URL_RE = /(https?:\/\/[^\s)"'<>?]+)\?[^\s)"'<>]*X-Amz-[^\s)"'<>]*/g;
 
/**
 * Drop the query string from signed S3 URLs. The REST API re-signs image
 * URLs on every request, so they'd change the hash without a content change.
 * @param {string} text
 */
export function stripSignedUrls(text) {
  return text.replace(SIGNED_URL_RE, '$1');
}
 
/**
 * Normalize line endings and trailing whitespace so only real edits count.
 * @param {string} text
 */
export function normalizeForHash(text) {
  return stripSignedUrls(text)
    .replace(/\r\n?/g, '\n')
    .split('\n')
    .map((line) => line.trimEnd())
    .join('\n')
    .trim();
}
 
/**
 * Short sha256 of normalized text (16 hex chars is plenty for ~600 entries).
 * @param {string} text
 */
export function hashContent(text) {
  return createHash('sha256').update(normalizeForHash(text)).digest('hex').slice(0, 16);
}