Refactor VPN proxy client implementation
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import {
|
||||
createStateSnapshot,
|
||||
normalizeStoredState,
|
||||
type GatewayAutoState,
|
||||
type OperationState,
|
||||
type StateSnapshot,
|
||||
type StoredState,
|
||||
} from '../../../shared/contracts/state.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;
|
||||
}
|
||||
|
||||
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(),
|
||||
});
|
||||
return { snapshot, storedState, gatewayAuto, configExists };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export type StateService = ReturnType<typeof createStateService>;
|
||||
Reference in New Issue
Block a user