Files
harbor-net/src/server/features/state/stateService.ts
T
dokril daec12e013
Build and Deploy Gateway / build-and-push (push) Failing after 14s
Build and Deploy Gateway / deploy (push) Has been skipped
Update Harbor client and gateway integration workflows
2026-08-19 18:16:10 +03:00

64 lines
1.8 KiB
TypeScript

import {
createStateSnapshot,
normalizeStoredState,
type GatewayAutoState,
type OperationState,
type StateSnapshot,
type StoredState,
} from '../../../shared/contracts/state.js';
import type { FailoverSnapshot } from '../../../shared/failover.js';
interface RuntimeState {
running?: boolean;
startedAt?: string | null;
}
export interface StateReadResult {
snapshot: StateSnapshot;
storedState: StoredState;
gatewayAuto: GatewayAutoState;
configExists: boolean;
}
interface StateServiceDependencies {
appMode: string;
readStoredState: () => unknown;
refreshRuntime: () => Promise<RuntimeState>;
getGatewayAutoState: () => GatewayAutoState;
getOperationState: () => OperationState;
configExists: () => boolean;
getFailoverSnapshot?: () => FailoverSnapshot;
}
function subscriptionHost(value: unknown) {
try {
return `${new URL(String(value)).host}/…`;
} catch {
return '';
}
}
export function createStateService(dependencies: StateServiceDependencies) {
return {
async read(): Promise<StateReadResult> {
const runtime = await dependencies.refreshRuntime();
const storedState = normalizeStoredState(dependencies.readStoredState());
const gatewayAuto = dependencies.getGatewayAutoState();
const configExists = dependencies.configExists();
const snapshot = createStateSnapshot({
storedState,
runtime,
gatewayAuto,
appMode: dependencies.appMode,
configExists,
subscriptionHost: subscriptionHost(storedState.subscriptionUrl),
operation: dependencies.getOperationState(),
failoverSnapshot: dependencies.getFailoverSnapshot?.(),
});
return { snapshot, storedState, gatewayAuto, configExists };
},
};
}
export type StateService = ReturnType<typeof createStateService>;