Files
SpineParticlesWeb/scripts/verify-connector-sea.mjs
2026-09-05 16:20:23 +08:00

60 lines
2.5 KiB
JavaScript

import { access, mkdtemp, rm } from 'node:fs/promises'
import { constants } from 'node:fs'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { spawn } from 'node:child_process'
const projectDir = resolve(fileURLToPath(new URL('..', import.meta.url)))
const platformName = process.platform === 'darwin' ? 'macos' : process.platform === 'win32' ? 'windows' : process.platform
const executableName = process.platform === 'win32' ? 'SpineParticleConnector.exe' : 'SpineParticleConnector'
const executablePath = join(projectDir, 'release', 'connector', `${platformName}-${process.arch}`, executableName)
const port = 27844
async function waitForHealth() {
const deadline = Date.now() + 10_000
let lastError = null
while (Date.now() < deadline) {
try {
const response = await fetch(`http://127.0.0.1:${port}/v1/health`)
if (response.ok) return response.json()
lastError = new Error(`健康检查返回 ${response.status}`)
} catch (error) {
lastError = error
}
await new Promise((resolveDelay) => setTimeout(resolveDelay, 150))
}
throw lastError || new Error('健康检查超时')
}
async function main() {
await access(executablePath, constants.X_OK)
const configDir = await mkdtemp(join(tmpdir(), 'spine-particle-sea-test-'))
const processHandle = spawn(executablePath, [], {
env: { ...process.env, SPINE_CONNECTOR_PORT: String(port), SPINE_CONNECTOR_CONFIG: join(configDir, 'config.json') },
stdio: ['ignore', 'pipe', 'pipe'],
})
let output = ''
processHandle.stdout.on('data', (chunk) => { output += chunk })
processHandle.stderr.on('data', (chunk) => { output += chunk })
try {
const health = await waitForHealth()
if (health.connectorVersion !== '1.0.0' || !Array.isArray(health.supportedVersions) || !health.supportedVersions.includes('4.3')) {
throw new Error(`独立连接器健康检查内容不正确:${JSON.stringify(health)}`)
}
console.log(`独立连接器验证通过:${executablePath}`)
console.log(`支持版本:${health.supportedVersions.join(', ')}`)
} finally {
if (!processHandle.killed) processHandle.kill('SIGTERM')
await new Promise((resolveDelay) => processHandle.once('exit', resolveDelay)).catch(() => {})
if (!processHandle.killed) processHandle.kill('SIGKILL')
await rm(configDir, { recursive: true, force: true })
}
}
main().catch((error) => {
console.error(error)
process.exitCode = 1
})