Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495/** Resolves the on-wire transfer size for an asset. When a `.zst` companion exists alongside the requested file, its size is returned instead; this mirrors file servers that serve pre-compressed variants when the client advertises zstd support. Otherwise the raw file size is returned. */import { stat, } from 'node:fs/promises';
import { caughtValueText, } from '@monochromatic-dev/module-caught-value/ts';
/** Sentinel returned by {@link wireSize} when neither the requested asset nor its `.zst` companion exists. A `unique symbol` rather than a falsy default because `0` is a valid wire size (an empty file); callers narrow with `=== WIRE_SIZE_UNAVAILABLE`. */export const WIRE_SIZE_UNAVAILABLE: unique symbol = Symbol('page-weight wire size cannot be resolved',);
/** Returns `true` if the path exists and is a regular file, `false` otherwise. Any error accessing the path (permissions, I/O) also yields `false`. @param absolutePath - absolute filesystem path to probe @returns whether the path resolves to a readable file @example ```ts if (await fileReadable('/srv/www/index.html')) { ... } ``` */async function fileReadable( absolutePath: string,): Promise<boolean> { try { /** Stat record used to distinguish files from directories or symlinks. */ const info = await stat(absolutePath,); return info.isFile(); } catch (error) { console.warn( `[page-weight] file readability probe failed for ${absolutePath}: ${caughtValueText(error,)}`, ); return false; }}
/** Resolves the wire size of a file, preferring a `.zst` companion when present. If `path.zst` exists as a regular file, its size is returned. Otherwise the raw size of `path` is returned. Returns {@link WIRE_SIZE_UNAVAILABLE} when neither form exists, letting callers decide how to handle dead references instead of silently zero-counting them. @param absolutePath - absolute path to the originally-requested asset @returns wire size in bytes, or {@link WIRE_SIZE_UNAVAILABLE} if the file is missing @example ```ts const bytes = await wireSize('/srv/www/styles.css'); ``` */export async function wireSize( absolutePath: string,): Promise<number | typeof WIRE_SIZE_UNAVAILABLE> { /** Candidate companion path; pre-compressed asset served when the client supports zstd. */ const zstPath = `${absolutePath}.zst`; if (await fileReadable(zstPath,)) { /** Stat of the `.zst` companion; only `size` is needed downstream. */ const info = await stat(zstPath,); return info.size; } if (await fileReadable(absolutePath,)) { /** Stat of the uncompressed asset; fallback when no `.zst` companion exists. */ const info = await stat(absolutePath,); return info.size; } return WIRE_SIZE_UNAVAILABLE;}