Implement heartbeat send and re-pair trust reset
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
YONEXUS_PROTOCOL_VERSION,
|
YONEXUS_PROTOCOL_VERSION,
|
||||||
buildAuthRequest,
|
buildAuthRequest,
|
||||||
|
buildHeartbeat,
|
||||||
buildHello,
|
buildHello,
|
||||||
buildPairConfirm,
|
buildPairConfirm,
|
||||||
createAuthRequestSigningInput,
|
createAuthRequestSigningInput,
|
||||||
@@ -109,6 +110,7 @@ export class YonexusClientRuntime {
|
|||||||
|
|
||||||
async handleMessage(raw: string): Promise<void> {
|
async handleMessage(raw: string): Promise<void> {
|
||||||
if (raw === "heartbeat_tick") {
|
if (raw === "heartbeat_tick") {
|
||||||
|
await this.handleHeartbeatTick();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,14 +152,12 @@ export class YonexusClientRuntime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (envelope.type === "auth_failed") {
|
if (envelope.type === "auth_failed") {
|
||||||
this.handleAuthFailed(envelope as TypedBuiltinEnvelope<"auth_failed">);
|
await this.handleAuthFailed(envelope as TypedBuiltinEnvelope<"auth_failed">);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (envelope.type === "re_pair_required") {
|
if (envelope.type === "re_pair_required") {
|
||||||
this.pendingPairing = undefined;
|
await this.handleRePairRequired();
|
||||||
this.lastPairingFailure = "re_pair_required";
|
|
||||||
this.phase = "pair_required";
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -283,14 +283,22 @@ export class YonexusClientRuntime {
|
|||||||
this.phase = "waiting_pair_confirm";
|
this.phase = "waiting_pair_confirm";
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleAuthFailed(envelope: TypedBuiltinEnvelope<"auth_failed">): void {
|
private async handleAuthFailed(
|
||||||
|
envelope: TypedBuiltinEnvelope<"auth_failed">
|
||||||
|
): Promise<void> {
|
||||||
const payload = envelope.payload as AuthFailedPayload | undefined;
|
const payload = envelope.payload as AuthFailedPayload | undefined;
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (payload.reason === "re_pair_required") {
|
||||||
|
this.lastPairingFailure = payload.reason;
|
||||||
|
await this.resetTrustState();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.lastPairingFailure = payload.reason;
|
this.lastPairingFailure = payload.reason;
|
||||||
this.phase = payload.reason === "re_pair_required" ? "pair_required" : "auth_required";
|
this.phase = "auth_required";
|
||||||
}
|
}
|
||||||
|
|
||||||
private async sendAuthRequest(): Promise<void> {
|
private async sendAuthRequest(): Promise<void> {
|
||||||
@@ -330,6 +338,43 @@ export class YonexusClientRuntime {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async handleHeartbeatTick(): Promise<void> {
|
||||||
|
if (this.phase !== "authenticated" || !this.options.transport.isConnected) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.options.transport.send(
|
||||||
|
encodeBuiltin(
|
||||||
|
buildHeartbeat(
|
||||||
|
{
|
||||||
|
identifier: this.options.config.identifier,
|
||||||
|
status: "alive"
|
||||||
|
},
|
||||||
|
{ timestamp: this.now() }
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleRePairRequired(): Promise<void> {
|
||||||
|
this.pendingPairing = undefined;
|
||||||
|
this.lastPairingFailure = "re_pair_required";
|
||||||
|
await this.resetTrustState();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async resetTrustState(): Promise<void> {
|
||||||
|
this.clientState = {
|
||||||
|
...this.clientState,
|
||||||
|
secret: undefined,
|
||||||
|
pairedAt: undefined,
|
||||||
|
authenticatedAt: undefined,
|
||||||
|
updatedAt: this.now()
|
||||||
|
};
|
||||||
|
|
||||||
|
await this.options.stateStore.save(this.clientState);
|
||||||
|
this.phase = "pair_required";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createYonexusClientRuntime(
|
export function createYonexusClientRuntime(
|
||||||
|
|||||||
Reference in New Issue
Block a user