import { CloseEvent } from "@hocuspocus/common";
import { Mutex } from "async-mutex";
import { Awareness } from "y-protocols/awareness";
import { Doc } from "yjs";
import { IncomingMessage as IncomingMessage$1, Server as Server$1, ServerResponse } from "node:http";
import { AddressInfo } from "node:net";
import { Decoder } from "lib0/decoding";
import { Encoder } from "lib0/encoding";

//#region packages/server/src/Document.d.ts
declare class Document extends Doc {
  awareness: Awareness;
  callbacks: {
    onUpdate: (document: Document, origin: unknown, update: Uint8Array) => void;
    beforeBroadcastStateless: (document: Document, stateless: string) => void;
    beforeHandleAwareness: (document: Document, states: Map<number, Record<string, any>>, transactionOrigin: unknown) => Promise<void>;
  };
  connections: Map<Connection, {
    clients: Set<any>;
  }>;
  directConnectionsCount: number;
  name: string;
  isLoading: boolean;
  isDestroyed: boolean;
  saveMutex: Mutex;
  lastChangeTime: number;
  /**
   * Constructor.
   */
  constructor(name: string, yDocOptions?: object);
  /**
   * Check if the Document (XMLFragment or Map) is empty
   */
  isEmpty(fieldName: string): boolean;
  /**
   * Merge the given document(s) into this one
   */
  merge(documents: Doc | Array<Doc>): Document;
  /**
   * Set a callback that will be triggered when the document is updated
   */
  onUpdate(callback: (document: Document, origin: unknown, update: Uint8Array) => void): Document;
  /**
   * Set a callback that will be triggered before a stateless message is broadcasted
   */
  beforeBroadcastStateless(callback: (document: Document, stateless: string) => void): Document;
  /**
   * Set a callback that will be triggered before an inbound awareness update
   * is applied to this document's awareness state. The callback receives the
   * document, the decoded per-client states as a mutable `Map`, and the
   * `TransactionOrigin` that will be forwarded to `applyAwarenessUpdate`.
   * Use `isTransactionOrigin(origin)` to discriminate sources. Mutate the
   * map in place (set/delete/field changes) to rewrite the update, or throw
   * to reject it entirely.
   */
  beforeHandleAwareness(callback: (document: Document, states: Map<number, Record<string, any>>, transactionOrigin: unknown) => Promise<any>): Document;
  /**
   * Register a connection and a set of clients on this document keyed by the
   * underlying websocket connection
   */
  addConnection(connection: Connection): Document;
  /**
   * Is the given connection registered on this document
   */
  hasConnection(connection: Connection): boolean;
  /**
   * Remove the given connection from this document
   */
  removeConnection(connection: Connection): Document;
  addDirectConnection(): Document;
  removeDirectConnection(): Document;
  /**
   * Get the number of active connections for this document
   */
  getConnectionsCount(): number;
  /**
   * Get an array of registered connections
   */
  getConnections(): Array<Connection>;
  /**
   * Get the client ids for the given connection instance
   */
  getClients(connection: Connection): Set<any>;
  /**
   * Has the document awareness states
   */
  hasAwarenessStates(): boolean;
  /**
   * Apply the given awareness update
   */
  applyAwarenessUpdate(connection: Connection, update: Uint8Array): Document;
  /**
   * Handle an awareness update and sync changes to clients
   * @private
   */
  private handleAwarenessUpdate;
  /**
   * Handle an updated document and sync changes to clients
   */
  private handleUpdate;
  /**
   * Broadcast stateless message to all connections
   */
  broadcastStateless(payload: string, filter?: (conn: Connection) => boolean): void;
  destroy(): void;
}
//#endregion
//#region packages/server/src/ClientConnection.d.ts
/**
 * The `ClientConnection` class is responsible for handling an incoming WebSocket
 *
 * TODO-refactor:
 * - use event handlers instead of calling hooks directly, hooks should probably be called from Hocuspocus.ts
 */
declare class ClientConnection<Context = any> {
  private readonly websocket;
  private readonly request;
  private readonly documentProvider;
  private readonly hooks;
  private readonly opts;
  private readonly defaultContext;
  private readonly documentConnections;
  private readonly incomingMessageQueue;
  private readonly documentConnectionsEstablished;
  private readonly hookPayloads;
  private readonly callbacks;
  private readonly socketId;
  timeout: number;
  pingInterval: NodeJS.Timeout;
  lastMessageReceivedAt: number;
  /**
   * The `ClientConnection` class receives incoming WebSocket connections,
   * runs all hooks:
   *
   *  - onConnect for all connections
   *  - onAuthenticate only if required
   *
   * … and if nothings fails it'll fully establish the connection and
   * load the Document then.
   */
  constructor(websocket: WebSocketLike, request: Request, documentProvider: {
    createDocument: Hocuspocus["createDocument"];
  }, hooks: Hocuspocus["hooks"], opts: {
    timeout: number;
  }, defaultContext?: Context);
  /**
   * Handle WebSocket close event. Call this from your integration
   * when the WebSocket connection closes.
   */
  handleClose(event?: CloseEvent): void;
  private close;
  /**
   * Close the connection if no messages have been received within the timeout period.
   * This replaces application-level ping/pong to maintain backward compatibility
   * with older provider versions that don't understand Ping/Pong message types.
   * Awareness updates (~every 30s) keep active connections alive.
   */
  private check;
  /**
   * Set a callback that will be triggered when the connection is closed
   */
  onClose(callback: (document: Document, payload: onDisconnectPayload) => void): ClientConnection;
  /**
   * Create a new connection by the given request and document
   */
  private createConnection;
  private setUpNewConnection;
  private handleQueueingMessage;
  /**
   * Handle an incoming WebSocket message. Call this from your integration
   * when the WebSocket receives a binary message.
   */
  handleMessage: (data: Uint8Array) => void;
}
//#endregion
//#region packages/server/src/DirectConnection.d.ts
declare class DirectConnection$1<Context = any> implements DirectConnection {
  document: Document | null;
  instance: Hocuspocus;
  context: Context;
  /**
   * Constructor.
   */
  constructor(document: Document, instance: Hocuspocus, context?: Context);
  transact(transaction: (document: Document) => void): Promise<void>;
  disconnect(): Promise<void>;
}
//#endregion
//#region packages/server/src/Server.d.ts
interface ServerConfiguration<Context = any> extends Configuration<Context> {
  port?: number;
  address?: string;
  stopOnSignals?: boolean;
  /**
   * Options passed to the underlying WebSocket server (ws).
   * Supports all ws ServerOptions, e.g. { maxPayload: 1024 * 1024 }
   */
  websocketOptions?: Record<string, any>;
}
declare const defaultServerConfiguration: {
  port: number;
  address: string;
  stopOnSignals: boolean;
};
declare class Server<Context = any> {
  httpServer: Server$1;
  private crossws;
  hocuspocus: Hocuspocus<Context>;
  configuration: ServerConfiguration<Context>;
  constructor(configuration?: Partial<ServerConfiguration<Context>>);
  private setupHttpUpgrade;
  requestHandler: (request: IncomingMessage$1, response: ServerResponse) => Promise<void>;
  listen(port?: number, callback?: any): Promise<Hocuspocus<Context>>;
  get address(): AddressInfo;
  destroy(): Promise<void>;
  get URL(): string;
  get webSocketURL(): string;
  get httpURL(): string;
  private showStartScreen;
}
//#endregion
//#region packages/server/src/Hocuspocus.d.ts
declare const defaultConfiguration: {
  name: null;
  timeout: number;
  debounce: number;
  maxDebounce: number;
  quiet: boolean;
  yDocOptions: {
    gc: boolean;
    gcFilter: () => boolean;
  };
  unloadImmediately: boolean;
};
declare class Hocuspocus<Context = any> {
  configuration: Configuration<Context>;
  loadingDocuments: Map<string, Promise<Document>>;
  unloadingDocuments: Map<string, Promise<void>>;
  documents: Map<string, Document>;
  server?: Server;
  debouncer: {
    debounce: (id: string, func: () => any | Promise<() => any>, debounce: number, maxDebounce: number) => Promise<any>;
    isDebounced: (id: string) => boolean;
    isCurrentlyExecuting: (id: string) => boolean;
    executeNow: (id: string) => any;
  };
  constructor(configuration?: Partial<Configuration<Context>>);
  /**
   * Configure Hocuspocus
   */
  configure(configuration: Partial<Configuration<Context>>): Hocuspocus<Context>;
  /**
   * Get the total number of active documents
   */
  getDocumentsCount(): number;
  /**
   * Get the total number of active connections
   */
  getConnectionsCount(): number;
  /**
   * Immediately execute all pending debounced onStoreDocument calls.
   * Useful during shutdown to ensure documents are persisted and unloaded
   * before the server exits, even when unloadImmediately is false.
   */
  flushPendingStores(): void;
  /**
   * Force close one or more connections
   */
  closeConnections(documentName?: string): void;
  /**
   * The `handleConnection` method receives incoming WebSocket connections,
   * runs all hooks:
   *
   *  - onConnect for all connections
   *  - onAuthenticate only if required
   *
   * … and if nothing fails it'll fully establish the connection and
   * load the Document then.
   */
  handleConnection(incoming: WebSocket | WebSocketLike, request: Request, defaultContext?: Context): ClientConnection;
  /**
   * Handle update of the given document
   *
   * "connection" is not necessarily type "Connection", it's the Yjs "origin" (which is "Connection" if
   * the update is incoming from the provider, but can be anything if the updates is originated from an extension.
   */
  private handleDocumentUpdate;
  /**
   * Create a new document by the given request
   */
  createDocument(documentName: string, request: Request, socketId: string, connection: ConnectionConfiguration, context?: Context): Promise<Document>;
  loadDocument(documentName: string, request: Request, socketId: string, connectionConfig: ConnectionConfiguration, context?: Context): Promise<Document>;
  storeDocumentHooks(document: Document, hookPayload: onStoreDocumentPayload, immediately?: boolean): Promise<any>;
  /**
   * Run the given hook on all configured extensions.
   * Runs the given callback after each hook.
   */
  hooks<T extends HookName>(name: T, payload: HookPayloadByName<Context>[T], callback?: Function | null): Promise<any>;
  shouldUnloadDocument(document: Document): boolean;
  unloadDocument(document: Document): Promise<any>;
  openDirectConnection(documentName: string, context?: Context): Promise<DirectConnection$1<Context>>;
}
//#endregion
//#region packages/server/src/types.d.ts
interface ConnectionTransactionOrigin {
  source: "connection";
  connection: Connection;
}
interface RedisTransactionOrigin {
  source: "redis";
}
interface LocalTransactionOrigin {
  source: "local";
  skipStoreHooks?: boolean;
  context?: any;
}
type TransactionOrigin = ConnectionTransactionOrigin | RedisTransactionOrigin | LocalTransactionOrigin;
declare function isTransactionOrigin(origin: unknown): origin is TransactionOrigin;
declare function shouldSkipStoreHooks(origin: unknown): boolean;
/**
 * Minimal interface for any WebSocket-like object for WebSocket, Bun's ServerWebSocket, ws, Deno, etc.
 */
interface WebSocketLike {
  send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
  close(code?: number, reason?: string): void;
  readyState: number;
}
declare enum MessageType {
  Unknown = -1,
  Sync = 0,
  Awareness = 1,
  Auth = 2,
  QueryAwareness = 3,
  SyncReply = 4,
  // same as Sync, but won't trigger another 'SyncStep1'
  Stateless = 5,
  BroadcastStateless = 6,
  CLOSE = 7,
  SyncStatus = 8,
  Ping = 9,
  Pong = 10
}
interface AwarenessUpdate {
  added: Array<any>;
  updated: Array<any>;
  removed: Array<any>;
}
interface ConnectionConfiguration {
  readOnly: boolean;
  isAuthenticated: boolean;
}
interface Extension<Context = any> {
  priority?: number;
  extensionName?: string;
  onConfigure?(data: onConfigurePayload): Promise<any>;
  onListen?(data: onListenPayload): Promise<any>;
  onUpgrade?(data: onUpgradePayload): Promise<any>;
  onConnect?(data: onConnectPayload<Context>): Promise<any>;
  connected?(data: connectedPayload<Context>): Promise<any>;
  onAuthenticate?(data: onAuthenticatePayload<Context>): Promise<any>;
  onTokenSync?(data: onTokenSyncPayload<Context>): Promise<any>;
  onCreateDocument?(data: onCreateDocumentPayload<Context>): Promise<any>;
  onLoadDocument?(data: onLoadDocumentPayload<Context>): Promise<any>;
  afterLoadDocument?(data: afterLoadDocumentPayload<Context>): Promise<any>;
  beforeHandleMessage?(data: beforeHandleMessagePayload<Context>): Promise<any>;
  /**
   * Fired before an inbound awareness update is applied to the document's
   * awareness state. The hook receives the decoded per-client `states` as a
   * mutable `Map` keyed by Yjs clientId. Mutate the map and the contained
   * state objects in place to rewrite fields, drop peers (`states.delete`),
   * or add synthetic ones (`states.set`); mutations are reflected in the
   * broadcast. Throw to reject the update without applying anything.
   *
   * Multiple extensions chain naturally: each extension sees the map as
   * mutated by previous extensions and can mutate it further.
   */
  beforeHandleAwareness?(data: beforeHandleAwarenessPayload<Context>): Promise<any>;
  beforeSync?(data: beforeSyncPayload<Context>): Promise<any>;
  beforeBroadcastStateless?(data: beforeBroadcastStatelessPayload): Promise<any>;
  onStateless?(payload: onStatelessPayload): Promise<any>;
  onChange?(data: onChangePayload<Context>): Promise<any>;
  onStoreDocument?(data: onStoreDocumentPayload<Context>): Promise<any>;
  afterStoreDocument?(data: afterStoreDocumentPayload<Context>): Promise<any>;
  onAwarenessUpdate?(data: onAwarenessUpdatePayload<Context>): Promise<any>;
  onRequest?(data: onRequestPayload): Promise<any>;
  onDisconnect?(data: onDisconnectPayload<Context>): Promise<any>;
  beforeUnloadDocument?(data: beforeUnloadDocumentPayload): Promise<any>;
  afterUnloadDocument?(data: afterUnloadDocumentPayload): Promise<any>;
  onDestroy?(data: onDestroyPayload): Promise<any>;
}
type HookName = "onConfigure" | "onListen" | "onUpgrade" | "onConnect" | "connected" | "onAuthenticate" | "onTokenSync" | "onCreateDocument" | "onLoadDocument" | "afterLoadDocument" | "beforeHandleMessage" | "beforeHandleAwareness" | "beforeBroadcastStateless" | "beforeSync" | "onStateless" | "onChange" | "onStoreDocument" | "afterStoreDocument" | "onAwarenessUpdate" | "onRequest" | "onDisconnect" | "beforeUnloadDocument" | "afterUnloadDocument" | "onDestroy";
type HookPayloadByName<Context = any> = {
  onConfigure: onConfigurePayload;
  onListen: onListenPayload;
  onUpgrade: onUpgradePayload;
  onConnect: onConnectPayload<Context>;
  connected: connectedPayload<Context>;
  onAuthenticate: onAuthenticatePayload<Context>;
  onTokenSync: onTokenSyncPayload<Context>;
  onCreateDocument: onCreateDocumentPayload<Context>;
  onLoadDocument: onLoadDocumentPayload<Context>;
  afterLoadDocument: afterLoadDocumentPayload<Context>;
  beforeHandleMessage: beforeHandleMessagePayload<Context>;
  beforeHandleAwareness: beforeHandleAwarenessPayload<Context>;
  beforeBroadcastStateless: beforeBroadcastStatelessPayload;
  beforeSync: beforeSyncPayload<Context>;
  onStateless: onStatelessPayload;
  onChange: onChangePayload<Context>;
  onStoreDocument: onStoreDocumentPayload<Context>;
  afterStoreDocument: afterStoreDocumentPayload<Context>;
  onAwarenessUpdate: onAwarenessUpdatePayload<Context>;
  onRequest: onRequestPayload;
  onDisconnect: onDisconnectPayload<Context>;
  afterUnloadDocument: afterUnloadDocumentPayload;
  beforeUnloadDocument: beforeUnloadDocumentPayload;
  onDestroy: onDestroyPayload;
};
interface Configuration<Context = any> extends Extension<Context> {
  /**
   * A name for the instance, used for logging.
   */
  name: string | null;
  /**
   * A list of hocuspocus extensions.
   */
  extensions: Array<Extension>;
  /**
   * Defines in which interval the server sends a ping, and closes the connection when no pong is sent back.
   */
  timeout: number;
  /**
   * Debounces the call of the `onStoreDocument` hook for the given amount of time in ms.
   * Otherwise every single update would be persisted.
   */
  debounce: number;
  /**
   * Makes sure to call `onStoreDocument` at least in the given amount of time (ms).
   */
  maxDebounce: number;
  /**
   * By default, the servers show a start screen. If passed false, the server will start quietly.
   */
  quiet: boolean;
  /**
   * If set to false, respects the debounce time of `onStoreDocument` before unloading a document.
   * Otherwise, the document will be unloaded immediately.
   *
   * This prevents a client from DOSing the server by repeatedly connecting and disconnecting when
   * your onStoreDocument is rate-limited.
   */
  unloadImmediately: boolean;
  /**
   * options to pass to the ydoc document
   */
  yDocOptions: {
    gc: boolean;
    gcFilter: () => boolean;
  };
}
interface onStatelessPayload {
  connection: Connection;
  documentName: string;
  document: Document;
  payload: string;
}
interface onAuthenticatePayload<Context = any> {
  context: Context;
  documentName: string;
  instance: Hocuspocus;
  requestHeaders: Headers;
  requestParameters: URLSearchParams;
  request: Request;
  socketId: string;
  token: string;
  connectionConfig: ConnectionConfiguration;
  providerVersion: string | null;
}
interface onTokenSyncPayload<Context = any> {
  context: Context;
  document: Document;
  documentName: string;
  instance: Hocuspocus;
  requestHeaders: Headers;
  requestParameters: URLSearchParams;
  socketId: string;
  token: string;
  connectionConfig: ConnectionConfiguration;
  connection: Connection<Context>;
}
interface onCreateDocumentPayload<Context = any> {
  context: Context;
  documentName: string;
  instance: Hocuspocus;
  requestHeaders: Headers;
  requestParameters: URLSearchParams;
  socketId: string;
  connectionConfig: ConnectionConfiguration;
}
interface onConnectPayload<Context = any> {
  context: Context;
  documentName: string;
  instance: Hocuspocus;
  request: Request;
  requestHeaders: Headers;
  requestParameters: URLSearchParams;
  socketId: string;
  connectionConfig: ConnectionConfiguration;
  providerVersion: string | null;
}
interface connectedPayload<Context = any> {
  context: Context;
  documentName: string;
  instance: Hocuspocus;
  request: Request;
  requestHeaders: Headers;
  requestParameters: URLSearchParams;
  socketId: string;
  connectionConfig: ConnectionConfiguration;
  connection: Connection<Context>;
  providerVersion: string | null;
}
interface onLoadDocumentPayload<Context = any> {
  context: Context;
  document: Document;
  documentName: string;
  instance: Hocuspocus;
  requestHeaders: Headers;
  requestParameters: URLSearchParams;
  socketId: string;
  connectionConfig: ConnectionConfiguration;
}
interface afterLoadDocumentPayload<Context = any> {
  context: Context;
  document: Document;
  documentName: string;
  instance: Hocuspocus;
  requestHeaders: Headers;
  requestParameters: URLSearchParams;
  socketId: string;
  connectionConfig: ConnectionConfiguration;
}
interface onChangePayload<Context = any> {
  clientsCount: number;
  context: Context;
  document: Document;
  documentName: string;
  instance: Hocuspocus;
  requestHeaders: Headers;
  requestParameters: URLSearchParams;
  update: Uint8Array;
  socketId: string;
  transactionOrigin: unknown;
  connection?: Connection<Context>;
}
interface beforeHandleMessagePayload<Context = any> {
  clientsCount: number;
  context: Context;
  document: Document;
  documentName: string;
  instance: Hocuspocus;
  requestHeaders: Headers;
  requestParameters: URLSearchParams;
  update: Uint8Array;
  socketId: string;
  connection: Connection<Context>;
}
interface beforeHandleAwarenessPayload<Context = any> {
  awareness: Awareness;
  clientsCount: number;
  /**
   * Connection context populated by `onAuthenticate`. `undefined` when the
   * update did not originate from a client connection (e.g. server-internal
   * writes via `DirectConnection`).
   */
  context: Context | undefined;
  document: Document;
  documentName: string;
  instance: Hocuspocus;
  requestHeaders: Headers;
  requestParameters: URLSearchParams;
  /**
   * Per-client awareness states decoded from the inbound update, keyed by
   * Yjs clientId. Mutate this map in place to rewrite the update: change
   * fields on a state object, `states.delete(clientId)` to drop a peer, or
   * `states.set(clientId, ...)` to add or replace one. The encoded update
   * sent to peers reflects whatever the map looks like after every hook in
   * the chain has run.
   */
  states: Map<number, Record<string, any>>;
  socketId: string;
  /**
   * The `TransactionOrigin` that will be passed to `applyAwarenessUpdate`.
   * Use `isTransactionOrigin(origin)` to discriminate sources. Matches the
   * `transactionOrigin` shape of `onAwarenessUpdatePayload`.
   */
  transactionOrigin: unknown;
  /**
   * Convenience shortcut: `origin.connection` when `transactionOrigin` is a
   * `ConnectionTransactionOrigin`, otherwise `undefined`. Matches the
   * `connection?` shape of `onAwarenessUpdatePayload`.
   */
  connection?: Connection<Context>;
}
interface beforeSyncPayload<Context = any> {
  clientsCount: number;
  context: Context;
  document: Document;
  documentName: string;
  connection: Connection<Context>;
  /**
   * The y-protocols/sync message type
   * @example
   * 0: SyncStep1
   * 1: SyncStep2
   * 2: YjsUpdate
   *
   * @see https://github.com/yjs/y-protocols/blob/master/sync.js#L13-L40
   */
  type: number;
  /**
   * The payload of the y-sync message.
   */
  payload: Uint8Array;
}
interface beforeBroadcastStatelessPayload {
  document: Document;
  documentName: string;
  payload: string;
}
interface onStoreDocumentPayload<Context = any> {
  clientsCount: number;
  document: Document;
  lastContext: Context;
  lastTransactionOrigin: unknown;
  documentName: string;
  instance: Hocuspocus;
}
interface afterStoreDocumentPayload<Context = any> extends onStoreDocumentPayload<Context> {}
interface onAwarenessUpdatePayload<Context = any> {
  document: Document;
  documentName: string;
  instance: Hocuspocus;
  transactionOrigin: unknown;
  connection?: Connection<Context>;
  added: number[];
  updated: number[];
  removed: number[];
  awareness: Awareness;
  states: StatesArray;
}
type StatesArray = {
  clientId: number;
  [key: string | number]: any;
}[];
interface fetchPayload<Context = any> {
  context: Context;
  document: Document;
  documentName: string;
  instance: Hocuspocus;
  requestHeaders: Headers;
  requestParameters: URLSearchParams;
  socketId: string;
  connectionConfig: ConnectionConfiguration;
}
interface storePayload<Context = any> extends onStoreDocumentPayload<Context> {
  state: Buffer;
}
interface onDisconnectPayload<Context = any> {
  clientsCount: number;
  context: Context;
  document: Document;
  documentName: string;
  instance: Hocuspocus;
  requestHeaders: Headers;
  requestParameters: URLSearchParams;
  socketId: string;
}
interface onRequestPayload {
  request: IncomingMessage$1;
  response: ServerResponse;
  instance: Hocuspocus;
}
interface onUpgradePayload {
  request: IncomingMessage$1;
  socket: any;
  head: any;
  instance: Hocuspocus;
}
interface onListenPayload {
  instance: Hocuspocus;
  configuration: Configuration;
  port: number;
}
interface onDestroyPayload {
  instance: Hocuspocus;
}
interface onConfigurePayload {
  instance: Hocuspocus;
  configuration: Configuration;
  version: string;
}
interface afterUnloadDocumentPayload {
  instance: Hocuspocus;
  documentName: string;
}
interface beforeUnloadDocumentPayload {
  instance: Hocuspocus;
  documentName: string;
  document: Document;
}
interface DirectConnection {
  transact(transaction: (document: Document) => void): Promise<void>;
  disconnect(): void;
}
//#endregion
//#region packages/server/src/Connection.d.ts
declare class Connection<Context = any> {
  webSocket: WebSocketLike;
  context: Context;
  document: Document;
  request: Request;
  callbacks: {
    onClose: ((document: Document, event?: CloseEvent) => void)[];
    beforeHandleMessage: (connection: Connection, update: Uint8Array) => Promise<void>;
    beforeSync: (connection: Connection, payload: Pick<beforeSyncPayload, "type" | "payload">) => Promise<void>;
    statelessCallback: (payload: onStatelessPayload) => Promise<void>;
    onTokenSyncCallback: (payload: {
      token: string;
    }) => Promise<void>;
  };
  socketId: string;
  readOnly: boolean;
  sessionId: string | null;
  providerVersion: string | null;
  /**
   * The address string prefixed to outgoing messages.
   * Session-aware clients get `documentName\0sessionId`; legacy clients get plain `documentName`.
   */
  get messageAddress(): string;
  private messageQueue;
  private processingPromise;
  /**
   * Constructor.
   */
  constructor(connection: WebSocketLike, request: Request, document: Document, socketId: string, context: Context, readOnly?: boolean, sessionId?: string | null, providerVersion?: string | null);
  /**
   * Set a callback that will be triggered when the connection is closed
   */
  onClose(callback: (document: Document, event?: CloseEvent) => void): Connection;
  /**
   * Set a callback that will be triggered when an stateless message is received
   */
  onStatelessCallback(callback: (payload: onStatelessPayload) => Promise<void>): Connection;
  /**
   * Set a callback that will be triggered before an message is handled
   */
  beforeHandleMessage(callback: (connection: Connection, update: Uint8Array) => Promise<any>): Connection;
  /**
   * Set a callback that will be triggered before a sync message is handled
   */
  beforeSync(callback: (connection: Connection, payload: Pick<beforeSyncPayload, "type" | "payload">) => Promise<any>): Connection;
  /**
   * Set a callback that will be triggered when on token sync message is received
   */
  onTokenSyncCallback(callback: (payload: {
    token: string;
  }) => Promise<void>): Connection;
  /**
   * Returns a promise that resolves when all queued messages have been processed.
   */
  waitForPendingMessages(): Promise<void>;
  /**
   * Send the given message
   */
  send(message: Uint8Array): void;
  /**
   * Send a stateless message with payload
   */
  sendStateless(payload: string): void;
  /**
   * Request current token from the client
   */
  requestToken(): void;
  /**
   * Graceful wrapper around the WebSocket close method.
   */
  close(event?: CloseEvent): void;
  /**
   * Send the current document awareness to the client, if any
   * @private
   */
  private sendCurrentAwareness;
  /**
   * Handle an incoming message
   * @public
   */
  handleMessage(data: Uint8Array): void;
  private processMessages;
}
//#endregion
//#region packages/server/src/IncomingMessage.d.ts
declare class IncomingMessage {
  /**
   * Access to the received message.
   */
  decoder: Decoder;
  /**
   * Private encoder; can be undefined.
   *
   * Lazy creation of the encoder speeds up IncomingMessages that need only a decoder.
   */
  private encoderInternal?;
  constructor(input: any);
  get encoder(): Encoder;
  readVarUint8Array(): Uint8Array<ArrayBufferLike>;
  peekVarUint8Array(): Uint8Array<ArrayBufferLike>;
  readVarUint(): number;
  readVarString(): string;
  toUint8Array(): Uint8Array<ArrayBuffer>;
  writeVarUint(type: MessageType): void;
  writeVarString(string: string): void;
  get length(): number;
}
//#endregion
//#region packages/server/src/MessageReceiver.d.ts
declare class MessageReceiver {
  message: IncomingMessage;
  defaultTransactionOrigin?: TransactionOrigin;
  constructor(message: IncomingMessage, defaultTransactionOrigin?: TransactionOrigin);
  apply(document: Document, connection?: Connection, reply?: (message: Uint8Array) => void): Promise<void>;
  readSyncMessage(message: IncomingMessage, document: Document, connection?: Connection, reply?: (message: Uint8Array) => void, requestFirstSync?: boolean): Promise<0 | 1 | 2>;
  applyQueryAwarenessMessage(document: Document, connection?: Connection, reply?: (message: Uint8Array) => void): void;
}
//#endregion
//#region packages/server/src/OutgoingMessage.d.ts
declare class OutgoingMessage {
  encoder: Encoder;
  type?: number;
  category?: string;
  constructor(documentName: string);
  createSyncMessage(): OutgoingMessage;
  createSyncReplyMessage(): OutgoingMessage;
  createAwarenessUpdateMessage(awareness: Awareness, changedClients?: Array<any>): OutgoingMessage;
  writeQueryAwareness(): OutgoingMessage;
  writeTokenSyncRequest(): OutgoingMessage;
  writeAuthenticated(readonly: boolean): OutgoingMessage;
  writePermissionDenied(reason: string): OutgoingMessage;
  writeFirstSyncStepFor(document: Document): OutgoingMessage;
  writeUpdate(update: Uint8Array): OutgoingMessage;
  writeStateless(payload: string): OutgoingMessage;
  writeBroadcastStateless(payload: string): OutgoingMessage;
  writeSyncStatus(updateSaved: boolean): OutgoingMessage;
  writeCloseMessage(reason: string): OutgoingMessage;
  toUint8Array(): Uint8Array;
}
//#endregion
//#region packages/server/src/util/debounce.d.ts
declare const useDebounce: () => {
  debounce: (id: string, func: () => any | Promise<() => any>, debounce: number, maxDebounce: number) => Promise<any>;
  isDebounced: (id: string) => boolean;
  isCurrentlyExecuting: (id: string) => boolean;
  executeNow: (id: string) => any;
};
//#endregion
export { AwarenessUpdate, Configuration, Connection, ConnectionConfiguration, ConnectionTransactionOrigin, DirectConnection, Document, Extension, Hocuspocus, HookName, HookPayloadByName, IncomingMessage, LocalTransactionOrigin, MessageReceiver, MessageType, OutgoingMessage, RedisTransactionOrigin, Server, ServerConfiguration, StatesArray, TransactionOrigin, WebSocketLike, afterLoadDocumentPayload, afterStoreDocumentPayload, afterUnloadDocumentPayload, beforeBroadcastStatelessPayload, beforeHandleAwarenessPayload, beforeHandleMessagePayload, beforeSyncPayload, beforeUnloadDocumentPayload, connectedPayload, defaultConfiguration, defaultServerConfiguration, fetchPayload, isTransactionOrigin, onAuthenticatePayload, onAwarenessUpdatePayload, onChangePayload, onConfigurePayload, onConnectPayload, onCreateDocumentPayload, onDestroyPayload, onDisconnectPayload, onListenPayload, onLoadDocumentPayload, onRequestPayload, onStatelessPayload, onStoreDocumentPayload, onTokenSyncPayload, onUpgradePayload, shouldSkipStoreHooks, storePayload, useDebounce };