import * as Native from '../Native.js'; import { LibSignalError } from '../Errors.js'; import { Environment, TokioAsyncContext } from '../net.js'; import * as KT from './KeyTransparency.js'; import { FakeChatRemote } from './FakeChat.js'; export type ChatRequest = Readonly<{ verb: string; path: string; headers: ReadonlyArray<[string, string]>; body?: Uint8Array; timeoutMillis?: number; }>; export type RequestOptions = { abortSignal?: AbortSignal; }; export type UploadForm = { cdn: number; key: string; headers: Map; signedUploadUrl: URL; }; type ConnectionManager = Native.Wrapper; export declare class ChatServerMessageAck { readonly _nativeHandle: Native.ServerMessageAck; constructor(_nativeHandle: Native.ServerMessageAck); send(statusCode: number): void; } export interface ConnectionEventsListener { /** * Called when the client gets disconnected from the server. * * This includes both deliberate disconnects as well as unexpected socket * closures. If the closure was not due to a deliberate disconnect, the error * will be provided. */ onConnectionInterrupted: (cause: LibSignalError | null) => void; } export interface ChatServiceListener extends ConnectionEventsListener { /** * Called when the server delivers an incoming message to the client. * * `timestamp` is in milliseconds. * * If `ack`'s `send` method is not called, the server will leave this message in the message * queue and attempt to deliver it again in the future. */ onIncomingMessage: (envelope: Uint8Array, timestamp: number, ack: ChatServerMessageAck) => void; /** * Called when the server indicates that there are no further messages in the message queue. * * Note that further messages may still be delivered; this merely indicates that all messages that * were in the queue *when the connection was established* have been delivered. */ onQueueEmpty: () => void; /** * Called when the server has alerts for the current device. * * In practice this happens as part of the connecting process. */ onReceivedAlerts?: (alerts: string[]) => void; } export interface ProvisioningConnectionListener extends ConnectionEventsListener { /** * Called at the start of the provisioning process. * * `address` should be considered an opaque token to pass to the primary device (usually via QR * code). * * `ack`'s `send` method can be called immediately to indicate successful delivery of the address. */ onReceivedAddress: (address: string, ack: ChatServerMessageAck) => void; /** * Called once when the primary sends an "envelope" via the server (using the address from * {@link #onReceivedAddress()}). * * Once the server receives the `ack` for this message, it will close this connection. */ onReceivedEnvelope: (envelope: Uint8Array, ack: ChatServerMessageAck) => void; } /** * A connection to the Chat Service. * * Provides API methods to communicate with the remote service. Make sure to * call {@link #disconnect()} when the instance is no longer needed. */ export type ChatConnection = { /** * Initiates termination of the underlying connection to the Chat Service. After the service is * disconnected, it cannot be used again. */ disconnect: () => Promise; /** * Sends request to the Chat service. */ fetch: (chatRequest: ChatRequest, options?: RequestOptions) => Promise; /** * Information about the connection to the Chat service. */ connectionInfo: () => ConnectionInfo; }; export interface ConnectionInfo { localPort: number; ipVersion: 'IPv4' | 'IPv6'; toString: () => string; } export declare class UnauthenticatedChatConnection implements ChatConnection { readonly _asyncContext: TokioAsyncContext; readonly _chatService: Native.Wrapper; private readonly chatListener; private readonly env?; static connect(asyncContext: TokioAsyncContext, connectionManager: ConnectionManager, listener: ConnectionEventsListener, env?: Environment, options?: { languages?: string[]; abortSignal?: AbortSignal; }): Promise; /** * Creates a chat connection backed by a fake remote end. * * @param asyncContext the async runtime to use * @param listener the listener to send events to * @param grpcOverrides gRPC method names to prefer for typed APIs that have both WS and gRPC * implementations. * @returns an {@link UnauthenticatedChatConnection} and handle for the remote * end of the fake connection. */ static fakeConnect(asyncContext: TokioAsyncContext, listener: ChatServiceListener, grpcOverrides?: ReadonlyArray): [UnauthenticatedChatConnection, FakeChatRemote]; private constructor(); fetch(chatRequest: ChatRequest, options?: RequestOptions): Promise; disconnect(): Promise; connectionInfo(): ConnectionInfo; keyTransparencyClient(): KT.Client; } export declare class AuthenticatedChatConnection implements ChatConnection { protected readonly asyncContext: TokioAsyncContext; protected readonly chatService: Native.Wrapper; private readonly chatListener; static connect(asyncContext: TokioAsyncContext, connectionManager: ConnectionManager, username: string, password: string, receiveStories: boolean, listener: ChatServiceListener, options?: { languages?: string[]; abortSignal?: AbortSignal; }): Promise; /** * Creates a chat connection backed by a fake remote end. * * @param asyncContext the async runtime to use * @param listener the listener to send events to * @param grpcOverrides gRPC method names to prefer for typed APIs that have both WS and gRPC * implementations. * @param alerts alerts to send immediately upon connect * @returns an {@link AuthenticatedChatConnection} and handle for the remote end of the fake * connection. */ static fakeConnect(asyncContext: TokioAsyncContext, listener: ChatServiceListener, grpcOverrides?: ReadonlyArray, alerts?: ReadonlyArray): [AuthenticatedChatConnection, FakeChatRemote]; private constructor(); fetch(chatRequest: ChatRequest, options?: { abortSignal?: AbortSignal; }): Promise; disconnect(): Promise; connectionInfo(): ConnectionInfo; } /** * A chat connection used specifically for provisioning linked devices. * * Note that no messages are sent *from* the client for a provisioning connection; all the * interesting functionality is in the events delivered to the {@link ProvisioningConnectionListener}. */ export declare class ProvisioningConnection { private readonly asyncContext; private readonly chatService; private readonly chatListener; static connect(asyncContext: TokioAsyncContext, connectionManager: ConnectionManager, listener: ProvisioningConnectionListener, options?: { abortSignal?: AbortSignal; }): Promise; /** * Creates a provisioning chat connection backed by a fake remote end. * * @param asyncContext the async runtime to use * @param listener the listener to send events to * @returns a {@link ProvisioningConnection} and handle for the remote * end of the fake connection. */ static fakeConnect(asyncContext: TokioAsyncContext, listener: ProvisioningConnectionListener): [ProvisioningConnection, FakeChatRemote]; private static makeNativeProvisioningListener; private constructor(); disconnect(): Promise; connectionInfo(): ConnectionInfo; } export declare function buildHttpRequest(chatRequest: ChatRequest): Native.Wrapper; export {};