import { MqttConn, OutboundTopicAliasManager, Timer } from "./deps.ts"; import type { AnyPacket, ConnectPacket, ConnNext, ExtPublishPacket, PacketId, Persistence, ProtocolLevel, ReasonCode, ShareName, Topic } from "./deps.ts"; import type { Configuration } from "./config.ts"; /** Unique identifier for an MQTT client. */ export type ClientId = string; /** System topic prefix character used for internal MQTT system topics. */ export declare const SysPrefix = "$"; /** Standard UTF-8 encoder used across the server to convert strings to byte arrays. */ export declare const utf8Encoder: TextEncoder; /** Possible results from isAuthenticated and processAuth handler */ export type AuthenticatedResult = { reasonCode: ReasonCode; reasonString?: string; authData?: Uint8Array; }; export declare const SessionState: { readonly connecting: 0; readonly authenticating: 1; readonly connected: 2; readonly closing: 3; readonly disconnected: 4; }; export type SessionState = typeof SessionState[keyof typeof SessionState]; /** * Handlers are hooks that the server will call * and let you influence the servers behaviour. * The following handlers can be configured: * - preconnect() * - isAuthenticated() * - processAuth() * - isAuthorizedToPublish() * - isAuthorizedToSubscribe() */ export type Handlers = { /** * Default preconnect handler that unconditionally permits all connections. * @param conn - The connection context. * @returns {boolean} false will close the connection */ preconnect?(conn: ConnNext): boolean | Promise; /** * Hook to authenticate a client connection attempt. * @param {Context} ctx - The connection context. * @param {ClientId} clientId - The client identifier. * @param {string} username - The username provided by the client. * @param {Uint8Array} password - The password bytes provided by the client. * @param {ConnectPacket} connectPacket - The raw connect packet * @returns {AuthResult} The result of the authentication attempt. */ isAuthenticated?(ctx: Context, clientId: ClientId, username: string, password: Uint8Array, connectPacket: ConnectPacket): AuthenticatedResult | Promise; /** * Hook to authenticate a client connection attempt. * @param {Context} ctx - The connection context. * @param {ClientId} clientId - The client identifier. * @param {string} authMethod - The authentication method provided by the client. * @param {Uint8Array} authData- The authentication data provided by the client. * @returns {AuthenticationResult} The result of the authentication attempt. */ processAuth?(ctx: Context, clientId: ClientId, authMethod: string, authData: Uint8Array): AuthenticatedResult | Promise; /** * Hook to authorize a message publication to a specific topic. * @param {Context} ctx - The connection context. * @param {Topic} topic - The topic the client wants to publish to. * @returns {boolean} True if the client is authorized to publish, false otherwise. */ isAuthorizedToPublish?(ctx: Context, topic: Topic): boolean | Promise; /** * Hook to authorize a subscription request to a specific topic. * @param {Context} ctx - The connection context. * @param {Topic} topic - The topic filter the client wants to subscribe to. * @param {ShareName} shareName - The name of the share in case of shared subscriptions * @returns {boolean} True if the client is authorized to subscribe, false otherwise. */ isAuthorizedToSubscribe?(ctx: Context, topic: Topic, shareName: ShareName): boolean | Promise; }; /** * V5 options that can be passed by handleConnect */ export type ConnectOptions = { sessionExpiryInterval?: number | undefined; willDelayInterval?: number | undefined; topicAliasMaximum?: number | undefined; maximumOutgoingPacketSize?: number | undefined; receiveMaximum?: number | undefined; assignedClientIdentifier?: string | undefined; keepAlive?: number | undefined; }; /** * Per-connection server context that manages the lifecycle, packets, * timers, and persistence interactions for an individual client connection. */ export declare class Context { /** the client id is set after succesful connect */ clientId: ClientId | null; /** Indicates the current state of the client */ state: SessionState; /** Indicates whether the client asked for a clean session */ cleanSession: boolean; /** Indicates whether the client is considered a broker and allowed to use $SYS topics etc */ isBroker: boolean; /** The MQTT protocol level version determined during the connection handshake. */ protocolLevel: ProtocolLevel; /** The underlying raw socket connection. */ conn: ConnNext; /** The wrapped MQTT connection wrapper handling frame reading and writing. */ mqttConn: MqttConn; /** The persistence layer instance used by the server. */ persistence: Persistence; /** The configured authentication and authorization lifecycle hooks. */ handlers: Handlers; /** Configuration data */ config: Configuration; /** Global registry mapping active client identifiers to their respective connection context. */ static clientList: Map; /** The optional Will packet configured by the client to be published if disconnected unexpectedly. */ will?: ExtPublishPacket | undefined; /** The Keep Alive timer tracking the client activity timeout. */ timer?: Timer; /** Timer to keep track of when a V5 session ends */ sessionEndsTimer?: Timer; /** Timer to handle V5 delayed will delivery */ willTimer?: Timer; /** * Timer enforcing a deadline for the client to send a CONNECT packet * after establishing a socket connection. */ preconnectTimer?: Timer; /** * max number of inflight packets for this client, * in V5 client can set this lower than maxInflightMessages * theoretical max is maxInt */ receiveMaximum: number; /** * inflighPackets, how many publish packets have been sent and are waiting for ack */ inflightPackets: number; /** * set of Id's of publish packets waiting to be send to the client */ queuedPacketIds: Set; /** * V5,max topicAliases ,0 = none */ outgoingMaxTopicAlias: number; incomingMaxTopicAlias: number; /** V5 server topic aliases */ incomingTopicAliases: Map; /** V5 client topic aliases */ outgoingTopicAliasManager: undefined | OutboundTopicAliasManager; connectOptions: undefined | ConnectOptions; /** * Initializes a new instance of the connection Context. */ constructor(configuration: Configuration, // all settings persistence: Persistence, // The server persistence layer implementation. conn: ConnNext, // The underlying socket connection. handlers: Handlers); /** * Configures and starts the preconnect timer to enforce connection deadlines. */ private initializePreconnectTimer; /** * Transmits an MQTT packet to the client. */ send(packet: AnyPacket): Promise; /** * Dispatch the next publish packet in queue (if exists), * when a ack packet on an already sent publish arrives */ processAck(packetId: PacketId): Promise; /** * Dispatch publish packets to client */ dispatch(extPacket: ExtPublishPacket): Promise; /** * Helper to setup the timers */ private setupConnectionTimers; /** * process V5 connectOptions */ private applyConnectOptions; /** * Finalizes the client connection state, registers the client in persistence, * kicks out existing duplicate sessions, and broadcasts the client connection event. */ prepareConnect(packet: ConnectPacket, clientId: string, connectOpts?: ConnectOptions): void; connect(): Promise; /** * Proces -inbound- publication requests */ publish(packet: ExtPublishPacket): Promise; /** * Explicitly removes a client session and its stored state from the persistence layer. */ clean(): Promise; /** * Tears down the connection context, terminates the underlying socket, * clears internal timers, triggers the Will packet execution if requested, * and announces the client disconnection. * If executewill=true triggers the registered Will packet logic. */ close(executewill?: boolean): Promise; /** * Evaluates authorization and triggers the distribution of the client's Will packet. */ private handleWill; /** * Utility method to generate and publish an internal system message payload to the persistence engine. */ broadcast(topic: Topic, // The topic filter string to publish to. payload: string, // The plain-text message string to encode. retain?: boolean): Promise; /** * utility method to redeliver packets that were cached * called from handleConnect() */ handleRedelivery(): Promise; } //# sourceMappingURL=context.d.ts.map