refactor: tighten upload and viewer contracts

This commit is contained in:
Tom Boullay
2026-05-13 17:50:26 +02:00
parent 30ff9826dc
commit f6ac71dad2
11 changed files with 108 additions and 86 deletions
+12 -6
View File
@@ -3,7 +3,7 @@ import { dirname, join } from 'path'
import { mkdir, readdir, readFile, rm, writeFile } from 'fs/promises'
import { existsSync } from 'fs'
import { TMP_DIR } from '@/lib/constants'
import { isRecord } from '@/lib/guards'
import { getErrorMessage, isRecord } from '@/lib/guards'
import { getModelAssetPath } from '@/lib/model-paths'
import { prepareGitAssets } from '@/lib/prepare-git-assets'
import type {
@@ -28,7 +28,7 @@ interface StagedOriginalFile {
interface StagedPreparedData {
modelFilename: string
compressed: boolean
deliveryMode?: GitModelMode
deliveryMode: GitModelMode
compressionError?: string
assetSummaries: PreparedAssetSummary[]
}
@@ -94,7 +94,7 @@ function isStagedPreparedData(value: unknown): value is StagedPreparedData {
return isRecord(value)
&& typeof value.modelFilename === 'string'
&& typeof value.compressed === 'boolean'
&& (value.deliveryMode === undefined || isGitModelMode(value.deliveryMode))
&& isGitModelMode(value.deliveryMode)
&& (value.compressionError === undefined || typeof value.compressionError === 'string')
&& Array.isArray(value.assetSummaries)
&& value.assetSummaries.every(isPreparedAssetSummary)
@@ -146,8 +146,14 @@ async function cleanupExpiredStagingUploads() {
if (now - manifest.createdAt > STAGING_TTL_MS) {
await cleanupStagingUpload(stagingId)
}
} catch {
await cleanupStagingUpload(stagingId).catch(() => {})
} catch (err) {
await cleanupStagingUpload(stagingId).catch((cleanupErr) => {
console.warn('[WARN] Staging cleanup failed', {
stagingId,
error: getErrorMessage(cleanupErr),
originalError: getErrorMessage(err),
})
})
}
}
}
@@ -259,7 +265,7 @@ export async function ensurePreparedStagingAssets(stagingId: string): Promise<Pr
modelFilename: manifest.prepared.modelFilename,
assetSummaries: manifest.prepared.assetSummaries,
compressed: manifest.prepared.compressed,
deliveryMode: manifest.prepared.deliveryMode ?? manifest.gitModelMode,
deliveryMode: manifest.prepared.deliveryMode,
compressionError: manifest.prepared.compressionError,
}
}