Files
openmotion/shared/hyperframes-capture-fix.mjs
open-design-crew[bot] 12ea6c7723 fix: preserve template GSAP and stabilize preview and capture (#473)
Preserve exact template-local GSAP dependencies in immutable export snapshots while retaining native history and concurrency guards.

Accept guarded preview transitions only when they match the pinned engine's deterministic ID stamping and disk-write condition, including existing empty IDs. Retain rejection of real edits and same-content restores.

Support legacy and current accelerated canvas APIs. Use the same-version complete Chrome for Testing application on macOS arm64 and derive complete-ZIP browser paths from the pinned runtime lock.

Validated with focused deterministic checks, real macOS runtime and accelerated-frame comparisons, fresh extracted-package smoke coverage, and all ten PR CI checks.
2026-11-16 21:46:41 +08:00

67 lines
3.4 KiB
JavaScript

import { registerHooks } from 'node:module';
function replace(source, before, after, count = 1) {
if (source.split(before).length - 1 !== count) {
throw new Error('Unsupported Hyperframes capture code; review the pinned 0.9.7 compatibility fix');
}
return source.replaceAll(before, after);
}
// shortcut: patch only the pinned engine in memory; remove when upstream handles current canvas APIs and socket-buffer exhaustion.
export function fixCaptureModule(url, source) {
if (url.endsWith('/node_modules/hyperframes/dist/chunk-IAZ2A7D6.js')) {
source = replace(source, ' canvas.setAttribute("layoutsubtree", "");',
` canvas.setAttribute("layoutsubtree", "");
if ("content" in HTMLCanvasElement.prototype) {
canvas.setAttribute("content", "drawable");
root.setAttribute("drawable", "");
}`);
return replace(source, 'var TRANSIENT_BROWSER_ERROR_PATTERNS = [',
'var TRANSIENT_BROWSER_ERROR_PATTERNS = [\n /net::ERR_NO_BUFFER_SPACE\\b/i,');
}
if (!url.endsWith('/node_modules/hyperframes/dist/chunk-Y2R4N6HQ.js')) return source;
source = replace(source,
' if (options.allowRetry && isTransientCaptureRetryEligible(failure.kind, remaining, transientRetriesUsed)) {',
` const bufferExhaustion = failure.kind === "transient_browser" && /net::ERR_NO_BUFFER_SPACE\\b/i.test(failure.message);
if (bufferExhaustion && transientRetriesUsed >= MAX_TRANSIENT_CAPTURE_RETRIES) {
options.log.warn("[Render] Socket buffers remain unavailable after the automatic retry. Close unused applications or other exports, then retry the export (CLI: --workers 1).", { error: failure.message });
throw error;
}
if (options.allowRetry && isTransientCaptureRetryEligible(failure.kind, remaining, transientRetriesUsed)) {`);
source = replace(source,
` transientRetriesUsed++;
options.log.warn(
"[Render] Transient browser failure during capture; retrying once with a fresh session.",`,
` transientRetriesUsed++;
const nextWorkers = bufferExhaustion ? getNextRetryWorkerCount(currentWorkers) : currentWorkers;
options.log.warn(
bufferExhaustion
? "[Render] Socket buffers unavailable; retrying once with fewer workers and fresh sessions."
: "[Render] Transient browser failure during capture; retrying once with a fresh session.",`);
source = replace(source,
` workers: currentWorkers,
missingFrames: remainingCount,
transientRetriesUsed,`,
` workers: currentWorkers,
nextWorkers,
missingFrames: remainingCount,
transientRetriesUsed,`);
source = replace(source,
` pendingTransientRetry = true;
continue;`,
` pendingTransientRetry = true;
currentWorkers = nextWorkers;
continue;`);
return replace(source,
'Capture attempt made no forward progress; composition is likely structurally broken, not retrying.',
'Capture attempt produced no frames; see the capture error for the cause. No further automatic retry is available.', 2);
}
registerHooks({
load(url, context, nextLoad) {
const loaded = nextLoad(url, context);
if (!/\/node_modules\/hyperframes\/dist\/chunk-(?:IAZ2A7D6|Y2R4N6HQ)\.js$/.test(url)) return loaded;
return { ...loaded, source: fixCaptureModule(url, typeof loaded.source === 'string' ? loaded.source : Buffer.from(loaded.source).toString('utf8')) };
},
});