修复固件哈希兼容性并完善部署配置

This commit is contained in:
mid
2026-08-08 16:18:41 +08:00
parent 290da46567
commit 34503f2638
13 changed files with 271 additions and 25 deletions
+18 -2
View File
@@ -16,6 +16,7 @@ const serverFile = fileURLToPath(import.meta.url)
const serverDir = path.dirname(serverFile)
const projectRoot = path.resolve(serverDir, '..')
export const MAX_JSON_BODY_SIZE = 64 * 1024
export const DEFAULT_PORT = 9000
const mimeTypes = new Map([
['.css', 'text/css; charset=utf-8'],
@@ -76,6 +77,19 @@ function isMissingFile(error) {
return error?.code === 'ENOENT' || error?.code === 'ENOTDIR'
}
export async function assertProductionBuild(staticRoot = path.join(projectRoot, 'dist')) {
const indexPath = path.join(path.resolve(staticRoot), 'index.html')
let details
try {
details = await stat(indexPath)
} catch (error) {
if (!isMissingFile(error)) throw error
}
if (!details?.isFile()) {
throw new Error(`Production build not found: ${indexPath}. Run "npm.cmd ci --include=dev" and "npm.cmd run build" before starting the server.`)
}
}
function acceptsHtmlNavigation(request, pathname) {
if (path.extname(pathname)) return false
return String(request.headers.accept || '').split(',').some((value) => value.trim().toLowerCase().startsWith('text/html'))
@@ -186,8 +200,10 @@ export async function createWebToolsServer(options = {}) {
export async function startWebToolsServer() {
const host = process.env.HOST || '0.0.0.0'
const port = Number(process.env.PORT || 8080)
const server = await createWebToolsServer()
const port = Number(process.env.PORT || DEFAULT_PORT)
const staticRoot = path.join(projectRoot, 'dist')
await assertProductionBuild(staticRoot)
const server = await createWebToolsServer({ staticRoot })
server.listen(port, host, () => {
console.log(`Web Tools listening on http://${host}:${port}`)
console.log(`Navigation config: ${path.resolve(process.env.DATA_DIR || path.join(projectRoot, 'data'), 'navigation.json')}`)