fix: add type safety improvements and fix Bun runtime type issues

- Replace embeddedAssets stub with generated type definitions
- Fix Bun.isCompiled access with proper type assertion to prevent undefined errors
- Improve router search validation with explicit SessionFileSearch type
- Simplify middleware return patterns for better readability
This commit is contained in:
weishu
2025-12-21 19:53:44 +08:00
parent 81852a3176
commit 3aa41fcef2
4 changed files with 25 additions and 20 deletions
+9
View File
@@ -0,0 +1,9 @@
// Stub types for embeddedAssets.generated.ts when the manifest is not generated.
export interface EmbeddedWebAsset {
path: string;
sourcePath: string;
mimeType: string;
}
export const embeddedAssets: EmbeddedWebAsset[];
-10
View File
@@ -1,10 +0,0 @@
// Stub types for embeddedAssets.generated.ts when the manifest is not generated.
declare module './embeddedAssets.generated' {
export interface EmbeddedWebAsset {
path: string;
sourcePath: string;
mimeType: string;
}
export const embeddedAssets: EmbeddedWebAsset[];
}
+5 -6
View File
@@ -95,13 +95,11 @@ function createWebApp(options: {
app.use('*', async (c, next) => {
if (c.req.path.startsWith('/api')) {
await next()
return
return await next()
}
if (c.req.method !== 'GET' && c.req.method !== 'HEAD') {
await next()
return
return await next()
}
const asset = embeddedAssetMap.get(c.req.path)
@@ -109,7 +107,7 @@ function createWebApp(options: {
return serveEmbeddedAsset(asset)
}
await next()
return await next()
})
app.get('*', async (c, next) => {
@@ -165,7 +163,8 @@ export async function startWebServer(options: {
jwtSecret: Uint8Array
socketEngine: SocketEngine
}): Promise<BunServer<WebSocketData>> {
const embeddedAssetMap = Bun.isCompiled ? await loadEmbeddedAssetMap() : null
const bunRuntime = (globalThis as typeof globalThis & { Bun?: { isCompiled?: boolean } }).Bun
const embeddedAssetMap = bunRuntime?.isCompiled ? await loadEmbeddedAssetMap() : null
const app = createWebApp({
getSyncEngine: options.getSyncEngine,
getSseManager: options.getSseManager,
+11 -4
View File
@@ -201,17 +201,24 @@ const sessionFilesRoute = createRoute({
component: FilesPage,
})
type SessionFileSearch = {
path: string
staged?: boolean
}
const sessionFileRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/sessions/$sessionId/file',
validateSearch: (search: Record<string, unknown>) => ({
path: typeof search.path === 'string' ? search.path : '',
staged: search.staged === true || search.staged === 'true'
validateSearch: (search: Record<string, unknown>): SessionFileSearch => {
const path = typeof search.path === 'string' ? search.path : ''
const staged = search.staged === true || search.staged === 'true'
? true
: search.staged === false || search.staged === 'false'
? false
: undefined
}),
return staged === undefined ? { path } : { path, staged }
},
component: FilePage,
})