Files
workinf_Blender_Wasm/web/protocol/engine-variant.ts
mes123456 7c16b279ae
Some checks failed
M6 deployable RC / quick (push) Has been cancelled
M6 deployable RC / chromium (push) Has been cancelled
M6 deployable RC / release (push) Has been cancelled
Advance M7 workflows and release operations
2026-08-15 17:43:53 -04:00

97 lines
3.0 KiB
TypeScript

import { blockedGate, capabilityIssue, readyGate, type CapabilityGateResult } from "./capability-gates";
import type { WebEngineManifestV2, WebEngineVariantV2 } from "./manifest";
export interface WasmThreadingCapabilities {
crossOriginIsolated: boolean;
sharedArrayBuffer: boolean;
worker: boolean;
}
export type WebEngineVariantPolicy = "AUTO" | "SINGLE_REQUIRED" | "PTHREAD_REQUIRED";
export interface WebEngineVariantSelection {
policy: WebEngineVariantPolicy;
selectedVariant: WebEngineVariantV2 | null;
pthreadGate: CapabilityGateResult;
}
export type WebEngineReleaseBinding =
| { status: "READY"; releaseId: string; manifest: WebEngineManifestV2 }
| {
status: "REFRESH_REQUIRED";
expectedReleaseId: string;
actualReleaseId: string;
manifest: null;
};
export function bindWebEngineRelease(
expectedReleaseId: string,
manifest: WebEngineManifestV2,
): WebEngineReleaseBinding {
if (manifest.releaseId === expectedReleaseId) {
return { status: "READY", releaseId: manifest.releaseId, manifest };
}
return {
status: "REFRESH_REQUIRED",
expectedReleaseId,
actualReleaseId: manifest.releaseId,
manifest: null,
};
}
export function gateWasmThreadingCapability(capabilities: WasmThreadingCapabilities): CapabilityGateResult {
const issues = [];
if (!capabilities.crossOriginIsolated) {
issues.push(capabilityIssue(
"PLATFORM_CAPABILITY_UNAVAILABLE",
"Cross-origin isolation is required for the pthread WASM engine",
"crossOriginIsolated",
));
}
if (!capabilities.sharedArrayBuffer) {
issues.push(capabilityIssue(
"PLATFORM_CAPABILITY_UNAVAILABLE",
"SharedArrayBuffer is required for the pthread WASM engine",
"sharedArrayBuffer",
));
}
if (!capabilities.worker) {
issues.push(capabilityIssue(
"PLATFORM_CAPABILITY_UNAVAILABLE",
"Worker is required for the pthread WASM engine",
"worker",
));
}
return issues.length > 0
? blockedGate("M6-02", "WASM_PTHREAD_ENGINE", issues)
: readyGate("M6-02", "WASM_PTHREAD_ENGINE");
}
export function selectWebEngineVariant(
manifest: WebEngineManifestV2,
policy: WebEngineVariantPolicy,
capabilities: WasmThreadingCapabilities,
): WebEngineVariantSelection {
const single = manifest.variants.find((variant) => variant.id === "single");
const pthread = manifest.variants.find((variant) => variant.id === "pthread");
if (!single || !pthread) throw new Error("ENGINE_MANIFEST_VARIANTS_MISSING");
const pthreadGate = gateWasmThreadingCapability(capabilities);
if (policy === "SINGLE_REQUIRED") return { policy, selectedVariant: single, pthreadGate };
if (policy === "AUTO") {
return {
policy,
selectedVariant: pthreadGate.status === "READY" ? pthread : single,
pthreadGate,
};
}
if (policy === "PTHREAD_REQUIRED") {
return {
policy,
selectedVariant: pthreadGate.status === "READY" ? pthread : null,
pthreadGate,
};
}
throw new Error("ENGINE_VARIANT_POLICY_INVALID");
}