mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
* perf(cli): commands run without esbuild, puppeteer-core or sharp until they need them A host that bundles the CLI can now leave these packages out: each command loads them only where it uses them, and sharp installs once on first use when it is missing. The build check fails on any static import of them again. * fix(cli): use a host's sharp from NODE_PATH and let hosts turn first-use installs off * fix(cli): capture fails when sharp is missing, and one install serves every caller A missing sharp now stops the asset download up front instead of counting every image as unavailable. Concurrent first uses share one install, a failed install is not retried in the same run, and preview thumbnails report why. Hosts can turn off installs for a list of packages, e.g. HYPERFRAMES_NO_OPTIONAL_INSTALL=sharp. * fix(cli): snapshot --describe still works without sharp, resizing only when it is installed * style(cli): format the sharp loader and its tests * build(cli): sharp is an optional dependency, so a host can ship the CLI without it * build(studio-server): keep sharp out of the bundle now that it is optional
101 lines
3.1 KiB
TypeScript
101 lines
3.1 KiB
TypeScript
export const PUPPETEER_DEFAULT_PROTOCOL_TIMEOUT_MS = 180_000;
|
|
|
|
export class StageBudgetTimeoutError extends Error {
|
|
readonly budgetMs: number;
|
|
|
|
constructor(label: string, budgetMs: number) {
|
|
super(`stage budget exceeded while ${label} (${budgetMs}ms)`);
|
|
this.name = "StageBudgetTimeoutError";
|
|
this.budgetMs = budgetMs;
|
|
}
|
|
}
|
|
|
|
export function captureProtocolTimeoutMs(navTimeoutMs: number, postNavBudgetMs: number): number {
|
|
if (!Number.isFinite(navTimeoutMs) || !Number.isFinite(postNavBudgetMs)) {
|
|
return PUPPETEER_DEFAULT_PROTOCOL_TIMEOUT_MS;
|
|
}
|
|
return Math.max(60_000, Math.max(0, navTimeoutMs), Math.max(0, postNavBudgetMs));
|
|
}
|
|
|
|
function errorMessage(err: unknown): string {
|
|
return err instanceof Error ? err.message : String(err);
|
|
}
|
|
|
|
function isPuppeteerTimeoutError(err: unknown): err is Error {
|
|
return err instanceof Error && err.constructor.name === "TimeoutError";
|
|
}
|
|
|
|
function hasNavigationTimeoutMessage(msg: string): boolean {
|
|
return /navigation timeout/i.test(msg);
|
|
}
|
|
|
|
function hasProtocolEvaluateTimeoutMessage(msg: string): boolean {
|
|
return /Runtime\.evaluate timed out|protocolTimeout|protocol timeout/i.test(msg);
|
|
}
|
|
|
|
export function isNavigationTimeoutError(err: unknown): boolean {
|
|
if (isPuppeteerTimeoutError(err)) {
|
|
return hasNavigationTimeoutMessage(err.message);
|
|
}
|
|
// String path used by BLOCKED.md formatting.
|
|
return typeof err === "string" && hasNavigationTimeoutMessage(err);
|
|
}
|
|
|
|
function isStageBudgetTimeoutError(err: unknown): boolean {
|
|
return err instanceof StageBudgetTimeoutError;
|
|
}
|
|
|
|
export function isProtocolEvaluateTimeoutError(err: unknown): boolean {
|
|
if (isPuppeteerTimeoutError(err)) {
|
|
return !hasNavigationTimeoutMessage(err.message);
|
|
}
|
|
return hasProtocolEvaluateTimeoutMessage(errorMessage(err));
|
|
}
|
|
|
|
export function isDegradableEvaluateTimeoutError(err: unknown): boolean {
|
|
return isStageBudgetTimeoutError(err) || isProtocolEvaluateTimeoutError(err);
|
|
}
|
|
|
|
export async function withRemainingBudget<T>(
|
|
work: Promise<T>,
|
|
remainingMs: number,
|
|
label: string,
|
|
timeoutError: () => Error = () => new StageBudgetTimeoutError(label, Math.max(0, remainingMs)),
|
|
): Promise<T> {
|
|
if (!(remainingMs > 0)) {
|
|
throw timeoutError();
|
|
}
|
|
|
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
try {
|
|
return await Promise.race([
|
|
work,
|
|
new Promise<never>((_resolve, reject) => {
|
|
timer = setTimeout(() => {
|
|
reject(timeoutError());
|
|
}, remainingMs);
|
|
}),
|
|
]);
|
|
} finally {
|
|
if (timer !== undefined) {
|
|
clearTimeout(timer);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function formatCaptureFailureReason(errMsg: string): string {
|
|
if (hasProtocolEvaluateTimeoutMessage(errMsg) || /stage budget exceeded/i.test(errMsg)) {
|
|
return (
|
|
`Page extraction timed out while running in-page script (${errMsg}). ` +
|
|
"The page likely opened, but a later capture step hung."
|
|
);
|
|
}
|
|
if (hasNavigationTimeoutMessage(errMsg)) {
|
|
return "Page navigation timed out — the site may be blocking headless browsers or requires authentication.";
|
|
}
|
|
if (/timeout|timed out/i.test(errMsg)) {
|
|
return `Capture timed out: ${errMsg}`;
|
|
}
|
|
return `Capture failed: ${errMsg}`;
|
|
}
|