|
| 1 | +import { ZrUiError } from "@rezi-ui/core"; |
| 2 | + |
| 3 | +export const DEFAULT_FPS_CAP = 60 as const; |
| 4 | +export const MAX_SAFE_FPS_CAP = 1000 as const; |
| 5 | +export const DEFAULT_MAX_EVENT_BYTES = 1 << 20; |
| 6 | +export const MAX_SAFE_EVENT_BYTES = 4 << 20; |
| 7 | + |
| 8 | +const EMPTY_NATIVE_CONFIG = Object.freeze({}) as Readonly<Record<string, unknown>>; |
| 9 | + |
| 10 | +const DEFAULT_NATIVE_LIMITS: Readonly<Record<string, number>> = Object.freeze({ |
| 11 | + // Align native validation caps with JS drawlist builder defaults. |
| 12 | + // |
| 13 | + // Native defaults are intentionally conservative; however, @rezi-ui/core's |
| 14 | + // drawlist builders default to 2 MiB max drawlist bytes and large command |
| 15 | + // budgets. Without overriding, moderately large frames (e.g. images/canvas) |
| 16 | + // can fail with ZR_ERR_LIMIT (-3) at submit time. |
| 17 | + outMaxBytesPerFrame: 2 * 1024 * 1024, |
| 18 | + dlMaxTotalBytes: 2 * 1024 * 1024, |
| 19 | + dlMaxCmds: 100_000, |
| 20 | + dlMaxStrings: 10_000, |
| 21 | + dlMaxBlobs: 10_000, |
| 22 | +}); |
| 23 | + |
| 24 | +function isPlainObject(v: unknown): v is Record<string, unknown> { |
| 25 | + return typeof v === "object" && v !== null && !Array.isArray(v); |
| 26 | +} |
| 27 | + |
| 28 | +export function mergeNativeLimits( |
| 29 | + nativeConfig: Readonly<Record<string, unknown>>, |
| 30 | +): Readonly<Record<string, unknown>> { |
| 31 | + // biome-ignore lint/complexity/useLiteralKeys: bracket access is required by noPropertyAccessFromIndexSignature. |
| 32 | + const limitsValue = nativeConfig["limits"]; |
| 33 | + const existingLimits = isPlainObject(limitsValue) |
| 34 | + ? (limitsValue as Record<string, unknown>) |
| 35 | + : null; |
| 36 | + const limits: Record<string, unknown> = { ...(existingLimits ?? {}) }; |
| 37 | + |
| 38 | + const has = (camel: string): boolean => { |
| 39 | + const snake = camel.replace(/[A-Z]/g, (m) => `_${m.toLowerCase()}`); |
| 40 | + return ( |
| 41 | + Object.prototype.hasOwnProperty.call(limits, camel) || |
| 42 | + Object.prototype.hasOwnProperty.call(limits, snake) |
| 43 | + ); |
| 44 | + }; |
| 45 | + |
| 46 | + for (const [camel, value] of Object.entries(DEFAULT_NATIVE_LIMITS)) { |
| 47 | + if (has(camel)) continue; |
| 48 | + limits[camel] = value; |
| 49 | + } |
| 50 | + |
| 51 | + return Object.freeze({ ...nativeConfig, limits: Object.freeze(limits) }); |
| 52 | +} |
| 53 | + |
| 54 | +export function normalizeBackendNativeConfig( |
| 55 | + nativeConfig: unknown, |
| 56 | +): Readonly<Record<string, unknown>> { |
| 57 | + return isPlainObject(nativeConfig) |
| 58 | + ? mergeNativeLimits(nativeConfig) |
| 59 | + : mergeNativeLimits(EMPTY_NATIVE_CONFIG); |
| 60 | +} |
| 61 | + |
| 62 | +export function parsePositiveIntOr(n: unknown, fallback: number): number { |
| 63 | + if (typeof n !== "number") return fallback; |
| 64 | + if (!Number.isFinite(n)) return fallback; |
| 65 | + if (!Number.isInteger(n)) return fallback; |
| 66 | + if (n <= 0) return fallback; |
| 67 | + return n; |
| 68 | +} |
| 69 | + |
| 70 | +export function parsePositiveInt(n: unknown): number | null { |
| 71 | + if (typeof n !== "number") return null; |
| 72 | + if (!Number.isFinite(n)) return null; |
| 73 | + if (!Number.isInteger(n)) return null; |
| 74 | + if (n <= 0) return null; |
| 75 | + return n; |
| 76 | +} |
| 77 | + |
| 78 | +export function parseBoundedPositiveIntOrThrow( |
| 79 | + name: string, |
| 80 | + value: unknown, |
| 81 | + fallback: number, |
| 82 | + max: number, |
| 83 | +): number { |
| 84 | + if (value === undefined) return fallback; |
| 85 | + const parsed = parsePositiveInt(value); |
| 86 | + if (parsed === null) { |
| 87 | + throw new ZrUiError("ZRUI_INVALID_PROPS", `${name} must be a positive integer`); |
| 88 | + } |
| 89 | + if (parsed > max) { |
| 90 | + throw new ZrUiError("ZRUI_INVALID_PROPS", `${name} must be <= ${String(max)}`); |
| 91 | + } |
| 92 | + return parsed; |
| 93 | +} |
| 94 | + |
| 95 | +function readNativeTargetFpsValues( |
| 96 | + cfg: Readonly<Record<string, unknown>>, |
| 97 | +): Readonly<{ camel: number | null; snake: number | null }> { |
| 98 | + const targetFpsCfg = cfg as Readonly<{ targetFps?: unknown; target_fps?: unknown }>; |
| 99 | + return { |
| 100 | + camel: parsePositiveInt(targetFpsCfg.targetFps), |
| 101 | + snake: parsePositiveInt(targetFpsCfg.target_fps), |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | +export function resolveTargetFps( |
| 106 | + fpsCap: number, |
| 107 | + nativeConfig: Readonly<Record<string, unknown>>, |
| 108 | +): number { |
| 109 | + const values = readNativeTargetFpsValues(nativeConfig); |
| 110 | + if (values.camel !== null && values.snake !== null && values.camel !== values.snake) { |
| 111 | + throw new ZrUiError( |
| 112 | + "ZRUI_INVALID_PROPS", |
| 113 | + `createNodeBackend config mismatch: nativeConfig.targetFps=${String(values.camel)} must match nativeConfig.target_fps=${String(values.snake)}.`, |
| 114 | + ); |
| 115 | + } |
| 116 | + const nativeTargetFps = values.camel ?? values.snake; |
| 117 | + if (nativeTargetFps !== null && nativeTargetFps !== fpsCap) { |
| 118 | + throw new ZrUiError( |
| 119 | + "ZRUI_INVALID_PROPS", |
| 120 | + `createNodeBackend config mismatch: fpsCap=${String(fpsCap)} must match nativeConfig.targetFps/target_fps=${String(nativeTargetFps)}. Fix: set nativeConfig.targetFps (or target_fps) to ${String(fpsCap)}, or remove the override and use fpsCap only.`, |
| 121 | + ); |
| 122 | + } |
| 123 | + return fpsCap; |
| 124 | +} |
0 commit comments