Files
hapi/cli/src/runner/runnerIdentity.ts
T
ROOOOandGitHub f967bd9928 fix(runner): restart when hub identity changes (#303)
* fix(runner): restart when hub identity changes

* fix(runner): fail closed on missing identity
2026-03-18 10:43:36 +08:00

39 lines
1.1 KiB
TypeScript

import { createHash } from 'node:crypto'
import type { RunnerLocallyPersistedState } from '@/persistence'
export type RunnerConnectionIdentity = {
apiUrl: string
machineId?: string
cliApiTokenHash?: string
}
export function hashRunnerCliApiToken(token: string | null | undefined): string | undefined {
const trimmed = token?.trim()
if (!trimmed) {
return undefined
}
return createHash('sha256').update(trimmed).digest('hex')
}
export function isRunnerStateCompatibleWithIdentity(
state: Pick<
RunnerLocallyPersistedState,
'startedWithApiUrl' | 'startedWithMachineId' | 'startedWithCliApiTokenHash'
>,
current: RunnerConnectionIdentity
): boolean {
if (!state.startedWithApiUrl || state.startedWithApiUrl !== current.apiUrl) {
return false
}
if (!current.machineId || state.startedWithMachineId !== current.machineId) {
return false
}
if (!current.cliApiTokenHash || state.startedWithCliApiTokenHash !== current.cliApiTokenHash) {
return false
}
return true
}