hololake-system-architecture/product-source/hololake-platform/src/lib/guanghuShanghaiNode.ts

52 lines
1.5 KiB
TypeScript

export interface GuanghuShanghaiNodeStatus {
nodeId: 'BS-SH-005'
reachability: 'online' | 'unreachable'
region: 'shanghai'
runtime: 'guanghu_os_native' | 'hosted_maintenance' | 'unknown'
ttl: number | null
}
export interface GuanghuShanghaiNodeState {
checkedAt: number | null
error: string | null
phase: 'checking' | 'online' | 'error'
status: GuanghuShanghaiNodeStatus | null
}
export const INITIAL_GUANGHU_SHANGHAI_NODE_STATE: GuanghuShanghaiNodeState = {
checkedAt: null,
error: null,
phase: 'checking',
status: null,
}
function record(value: unknown): Record<string, unknown> | null {
return typeof value === 'object' && value !== null && !Array.isArray(value)
? value as Record<string, unknown>
: null
}
export function parseGuanghuShanghaiNodeStatus(
value: unknown,
): GuanghuShanghaiNodeStatus {
const status = record(value)
const runtime = status?.runtime
const reachability = status?.reachability
if (
status?.node_id !== 'BS-SH-005'
|| status.region !== 'shanghai'
|| (reachability !== 'online' && reachability !== 'unreachable')
|| !['guanghu_os_native', 'hosted_maintenance', 'unknown'].includes(
typeof runtime === 'string' ? runtime : '',
)
) {
throw new Error('guanghu_shanghai_node_receipt_invalid')
}
return {
nodeId: 'BS-SH-005',
reachability,
region: 'shanghai',
runtime: runtime as GuanghuShanghaiNodeStatus['runtime'],
ttl: typeof status.ttl === 'number' ? status.ttl : null,
}
}