pi-mono/packages/server/src/connection.ts
Christian Klotz 305c014dcc
fix: make session authentication transport-specific (#7551)
Set the initial protocol version to 1 and remove bearer authentication from framed messages. Require transports to establish authorized connections.
2026-08-03 16:40:00 +03:00

36 lines
1.1 KiB
TypeScript

import type { ClientMessageDecoder } from "@earendil-works/pi-protocol";
import type { MaybePromise } from "./types.ts";
/** An established, authorized ordered byte connection. */
export interface ByteConnection {
readonly closed: boolean;
send(chunk: Uint8Array): Promise<void>;
close(finalChunk?: Uint8Array): MaybePromise<void>;
}
export interface ByteConnectionHandler {
onData(chunk: Uint8Array): void;
onClose(): void;
onError(error: Error): void;
}
export type ByteConnectionAcceptor = (connection: ByteConnection) => ByteConnectionHandler;
export type ConnectionStage = "awaitingHello" | "handshaking" | "ready" | "closing" | "closed";
export interface ConnectionState {
id: string;
connection: ByteConnection;
decoder: ClientMessageDecoder;
sessionIds: Set<string>;
stage: ConnectionStage;
disconnected: boolean;
handshakeComplete: boolean;
handshake?: Promise<void>;
handshakeTimeout: NodeJS.Timeout;
}
export function isTerminalConnection(state: ConnectionState): boolean {
return state.disconnected || state.stage === "closing" || state.stage === "closed";
}