UNPKG

webssh2-server

Version:

A Websocket to SSH2 gateway using xterm.js, socket.io, ssh2

82 lines (81 loc) 2.32 kB
/** * Connection state reducer */ const connectionActionTypes = new Set([ 'CONNECTION_START', 'CONNECTION_ESTABLISHED', 'CONNECTION_ERROR', 'CONNECTION_CLOSED', 'CONNECTION_ACTIVITY' ]); function isConnectionAction(action) { return connectionActionTypes.has(action.type); } function isConnectionResetAction(action) { return action.type === 'AUTH_FAILURE' || action.type === 'AUTH_LOGOUT'; } function resetConnectionState(state) { return { status: 'disconnected', connectionId: null, host: state.host, port: state.port, errorMessage: null, lastActivity: Date.now() }; } function applyConnectionHandler(state, action) { switch (action.type) { case 'CONNECTION_START': return { ...state, status: 'connecting', host: action.payload.host, port: action.payload.port, errorMessage: null, lastActivity: Date.now() }; case 'CONNECTION_ESTABLISHED': return { ...state, status: 'connected', connectionId: action.payload.connectionId, errorMessage: null, lastActivity: Date.now() }; case 'CONNECTION_ERROR': return { ...state, status: 'error', errorMessage: action.payload.error, lastActivity: Date.now() }; case 'CONNECTION_CLOSED': return { ...state, status: 'closed', connectionId: null, errorMessage: action.payload.reason ?? null, lastActivity: Date.now() }; case 'CONNECTION_ACTIVITY': return { ...state, lastActivity: Date.now() }; default: return state; } } /** * Connection reducer - handles SSH connection state transitions */ export const connectionReducer = (state, action) => { if (isConnectionAction(action)) { return applyConnectionHandler(state, action); } if (isConnectionResetAction(action)) { return resetConnectionState(state); } return state; };