|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +04/15/2026 |
| 5 | +HandshakeSimulator.js |
| 6 | +*/ |
| 7 | +import LoopbackTransport from './LoopbackTransport.js'; |
| 8 | +import { |
| 9 | + createSessionLifecycle, |
| 10 | + getSessionLifecycleContract, |
| 11 | + SESSION_STATES, |
| 12 | +} from './SessionLifecycleContract.js'; |
| 13 | +import { createTransportBoundary, getTransportContract } from './TransportContract.js'; |
| 14 | + |
| 15 | +function clone(value) { |
| 16 | + return JSON.parse(JSON.stringify(value)); |
| 17 | +} |
| 18 | + |
| 19 | +export const HANDSHAKE_MESSAGE_TYPES = Object.freeze({ |
| 20 | + HELLO: 'session.handshake.hello', |
| 21 | + ACCEPT: 'session.handshake.accept', |
| 22 | + CONFIRM: 'session.handshake.confirm', |
| 23 | + BYE: 'session.handshake.bye', |
| 24 | +}); |
| 25 | + |
| 26 | +function createHandshakePacket({ |
| 27 | + type, |
| 28 | + sessionId, |
| 29 | + from, |
| 30 | + to, |
| 31 | + payload = {}, |
| 32 | +}) { |
| 33 | + return { |
| 34 | + type, |
| 35 | + sessionId, |
| 36 | + from, |
| 37 | + to, |
| 38 | + payload: clone(payload), |
| 39 | + createdAt: Date.now(), |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +export function getHandshakeContract() { |
| 44 | + return { |
| 45 | + messages: { ...HANDSHAKE_MESSAGE_TYPES }, |
| 46 | + flow: [ |
| 47 | + HANDSHAKE_MESSAGE_TYPES.HELLO, |
| 48 | + HANDSHAKE_MESSAGE_TYPES.ACCEPT, |
| 49 | + HANDSHAKE_MESSAGE_TYPES.CONFIRM, |
| 50 | + ], |
| 51 | + transport: getTransportContract(), |
| 52 | + sessionLifecycle: getSessionLifecycleContract(), |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +export default class HandshakeSimulator { |
| 57 | + constructor({ |
| 58 | + sessionId = 'session', |
| 59 | + hostId = 'host', |
| 60 | + clientId = 'client', |
| 61 | + hostTransport = null, |
| 62 | + clientTransport = null, |
| 63 | + } = {}) { |
| 64 | + const transports = hostTransport && clientTransport |
| 65 | + ? [hostTransport, clientTransport] |
| 66 | + : LoopbackTransport.createLinkedPair(hostId, clientId); |
| 67 | + |
| 68 | + this.sessionId = sessionId; |
| 69 | + this.hostId = hostId; |
| 70 | + this.clientId = clientId; |
| 71 | + this.hostTransport = createTransportBoundary(transports[0], { name: 'hostTransport' }); |
| 72 | + this.clientTransport = createTransportBoundary(transports[1], { name: 'clientTransport' }); |
| 73 | + this.hostSession = createSessionLifecycle({ |
| 74 | + sessionId, |
| 75 | + peerId: hostId, |
| 76 | + role: 'host', |
| 77 | + }); |
| 78 | + this.clientSession = createSessionLifecycle({ |
| 79 | + sessionId, |
| 80 | + peerId: clientId, |
| 81 | + role: 'client', |
| 82 | + }); |
| 83 | + this.handshakeToken = null; |
| 84 | + this.handshakeComplete = false; |
| 85 | + this.eventLog = []; |
| 86 | + } |
| 87 | + |
| 88 | + log(event, details = {}) { |
| 89 | + this.eventLog.push({ |
| 90 | + event, |
| 91 | + details: clone(details), |
| 92 | + step: this.eventLog.length, |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + begin({ token = `${this.sessionId}:${this.clientId}` } = {}) { |
| 97 | + this.handshakeToken = token; |
| 98 | + this.hostSession.transition(SESSION_STATES.CONNECTING, 'host-connect'); |
| 99 | + this.clientSession.transition(SESSION_STATES.CONNECTING, 'client-connect'); |
| 100 | + this.hostSession.transition(SESSION_STATES.HANDSHAKING, 'host-ready-for-hello'); |
| 101 | + this.clientSession.transition(SESSION_STATES.HANDSHAKING, 'client-sending-hello'); |
| 102 | + |
| 103 | + const accepted = this.clientTransport.send(createHandshakePacket({ |
| 104 | + type: HANDSHAKE_MESSAGE_TYPES.HELLO, |
| 105 | + sessionId: this.sessionId, |
| 106 | + from: this.clientId, |
| 107 | + to: this.hostId, |
| 108 | + payload: { token: this.handshakeToken }, |
| 109 | + })); |
| 110 | + |
| 111 | + if (!accepted) { |
| 112 | + this.hostSession.transition(SESSION_STATES.FAILED, 'hello-send-failed'); |
| 113 | + this.clientSession.transition(SESSION_STATES.FAILED, 'hello-send-failed'); |
| 114 | + this.log('handshake.failed', { reason: 'hello-send-failed' }); |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + this.log('handshake.begin', { |
| 119 | + sessionId: this.sessionId, |
| 120 | + hostId: this.hostId, |
| 121 | + clientId: this.clientId, |
| 122 | + token: this.handshakeToken, |
| 123 | + }); |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + processHostInbox() { |
| 128 | + const packets = this.hostTransport.drainInbox(); |
| 129 | + packets.forEach((packet) => { |
| 130 | + if (packet.type === HANDSHAKE_MESSAGE_TYPES.HELLO) { |
| 131 | + this.log('handshake.hello.received', { from: packet.from }); |
| 132 | + this.hostTransport.send(createHandshakePacket({ |
| 133 | + type: HANDSHAKE_MESSAGE_TYPES.ACCEPT, |
| 134 | + sessionId: this.sessionId, |
| 135 | + from: this.hostId, |
| 136 | + to: this.clientId, |
| 137 | + payload: { acceptedToken: packet.payload?.token ?? null }, |
| 138 | + })); |
| 139 | + } else if (packet.type === HANDSHAKE_MESSAGE_TYPES.CONFIRM) { |
| 140 | + this.hostSession.transition(SESSION_STATES.ACTIVE, 'client-confirmed'); |
| 141 | + this.handshakeComplete = this.clientSession.getState() === SESSION_STATES.ACTIVE |
| 142 | + && this.hostSession.getState() === SESSION_STATES.ACTIVE; |
| 143 | + this.log('handshake.confirm.received', { from: packet.from }); |
| 144 | + } else if (packet.type === HANDSHAKE_MESSAGE_TYPES.BYE) { |
| 145 | + this.hostSession.transition(SESSION_STATES.DISCONNECTED, 'client-disconnect'); |
| 146 | + this.log('session.disconnected.host', { reason: packet.payload?.reason ?? 'remote' }); |
| 147 | + } |
| 148 | + }); |
| 149 | + return packets.length; |
| 150 | + } |
| 151 | + |
| 152 | + processClientInbox() { |
| 153 | + const packets = this.clientTransport.drainInbox(); |
| 154 | + packets.forEach((packet) => { |
| 155 | + if (packet.type === HANDSHAKE_MESSAGE_TYPES.ACCEPT) { |
| 156 | + this.log('handshake.accept.received', { from: packet.from }); |
| 157 | + this.clientSession.transition(SESSION_STATES.ACTIVE, 'host-accepted'); |
| 158 | + this.clientTransport.send(createHandshakePacket({ |
| 159 | + type: HANDSHAKE_MESSAGE_TYPES.CONFIRM, |
| 160 | + sessionId: this.sessionId, |
| 161 | + from: this.clientId, |
| 162 | + to: this.hostId, |
| 163 | + payload: { token: packet.payload?.acceptedToken ?? null }, |
| 164 | + })); |
| 165 | + } else if (packet.type === HANDSHAKE_MESSAGE_TYPES.BYE) { |
| 166 | + this.clientSession.transition(SESSION_STATES.DISCONNECTED, 'host-disconnect'); |
| 167 | + this.log('session.disconnected.client', { reason: packet.payload?.reason ?? 'remote' }); |
| 168 | + } |
| 169 | + }); |
| 170 | + return packets.length; |
| 171 | + } |
| 172 | + |
| 173 | + update({ maxPumpIterations = 8 } = {}) { |
| 174 | + let iteration = 0; |
| 175 | + while (iteration < maxPumpIterations) { |
| 176 | + const hostProcessed = this.processHostInbox(); |
| 177 | + const clientProcessed = this.processClientInbox(); |
| 178 | + if (!hostProcessed && !clientProcessed) { |
| 179 | + break; |
| 180 | + } |
| 181 | + iteration += 1; |
| 182 | + } |
| 183 | + |
| 184 | + return this.getState(); |
| 185 | + } |
| 186 | + |
| 187 | + disconnect(reason = 'manual') { |
| 188 | + const hostState = this.hostSession.getState(); |
| 189 | + const clientState = this.clientSession.getState(); |
| 190 | + if (hostState === SESSION_STATES.ACTIVE) { |
| 191 | + this.hostSession.transition(SESSION_STATES.DISCONNECTING, 'host-disconnecting'); |
| 192 | + this.hostTransport.send(createHandshakePacket({ |
| 193 | + type: HANDSHAKE_MESSAGE_TYPES.BYE, |
| 194 | + sessionId: this.sessionId, |
| 195 | + from: this.hostId, |
| 196 | + to: this.clientId, |
| 197 | + payload: { reason }, |
| 198 | + })); |
| 199 | + this.hostSession.transition(SESSION_STATES.DISCONNECTED, reason); |
| 200 | + } |
| 201 | + |
| 202 | + if (clientState === SESSION_STATES.ACTIVE) { |
| 203 | + this.clientSession.transition(SESSION_STATES.DISCONNECTING, 'client-disconnecting'); |
| 204 | + this.clientSession.transition(SESSION_STATES.DISCONNECTED, reason); |
| 205 | + } |
| 206 | + |
| 207 | + this.hostTransport.disconnect(); |
| 208 | + this.clientTransport.disconnect(); |
| 209 | + this.handshakeComplete = false; |
| 210 | + this.log('session.disconnect', { reason }); |
| 211 | + this.update(); |
| 212 | + return this.getState(); |
| 213 | + } |
| 214 | + |
| 215 | + getState() { |
| 216 | + return { |
| 217 | + sessionId: this.sessionId, |
| 218 | + host: this.hostSession.getSnapshot(), |
| 219 | + client: this.clientSession.getSnapshot(), |
| 220 | + handshakeComplete: this.handshakeComplete, |
| 221 | + transportConnected: { |
| 222 | + host: this.hostTransport.isConnected(), |
| 223 | + client: this.clientTransport.isConnected(), |
| 224 | + }, |
| 225 | + events: this.eventLog.map((entry) => ({ ...entry })), |
| 226 | + }; |
| 227 | + } |
| 228 | +} |
| 229 | + |
0 commit comments