projects/sdk/src/internal/protocol/gateway.ts

/**
 * Gateway protocol-v1 constants: Opcodes, close codes and the reviewed close-code classification.
 * Invariant: Gateway code names every opcode and close code through these tables, and an unlisted close code enters
 * recovery because an unknown closure is not proof of a permanent fault.
 * Implements [SDK contracts: Connection and recovery](/docs/SDK-CONTRACTS.md#connection-and-recovery)
 */

/** Gateway opcodes used by the SDK's protocol-v1 session */
export const Opcode = Object.freeze({
    dispatch: 0,
    heartbeat: 1,
    identify: 2,
    presenceUpdate: 3,
    voiceStateUpdate: 4,
    voiceServerPing: 5,
    resume: 6,
    reconnect: 7,
    requestGuildMembers: 8,
    invalidSession: 9,
    hello: 10,
    heartbeatAck: 11,
    gatewayError: 12,
    voiceSignal: 13,
    memberSubscriptions: 14,
    requestGuildCounts: 15,
    requestChannelMemberCounts: 16,
} as const)

const opcodeNames = new Map<number, string>([
    [Opcode.dispatch, "DISPATCH"],
    [Opcode.heartbeat, "HEARTBEAT"],
    [Opcode.identify, "IDENTIFY"],
    [Opcode.presenceUpdate, "PRESENCE_UPDATE"],
    [Opcode.voiceStateUpdate, "VOICE_STATE_UPDATE"],
    [Opcode.voiceServerPing, "VOICE_SERVER_PING"],
    [Opcode.resume, "RESUME"],
    [Opcode.reconnect, "RECONNECT"],
    [Opcode.requestGuildMembers, "REQUEST_GUILD_MEMBERS"],
    [Opcode.invalidSession, "INVALID_SESSION"],
    [Opcode.hello, "HELLO"],
    [Opcode.heartbeatAck, "HEARTBEAT_ACK"],
    [Opcode.gatewayError, "GATEWAY_ERROR"],
    [Opcode.voiceSignal, "VOICE_SIGNAL"],
    [Opcode.memberSubscriptions, "MEMBER_SUBSCRIPTIONS"],
    [Opcode.requestGuildCounts, "REQUEST_GUILD_COUNTS"],
    [Opcode.requestChannelMemberCounts, "REQUEST_CHANNEL_MEMBER_COUNTS"],
    // Fluxer's gateway defines opcode 28 RequestForumUnreads (fluxer_gateway/src/fluxer_gateway/gateway/opcode.gleam:44, 68
    // at 66cede64). It is bot-accessible without session flags, silently rate-limited at 5 per 5,000 ms, accepts up to 40
    // unique thread IDs and returns FORUM_UNREADS with counts capped at 25 (session/server.gleam:609-618,
    // gateway/rate_limit.gleam:108-112, config/settings.gleam:606, guild/requests/forum_unreads.gleam:31-33)
    [28, "REQUEST_FORUM_UNREADS"],
])

/** Readable opcode name for logs, or null for an opcode this SDK does not know */
export function opcodeName(op: number): string | null {
    return opcodeNames.get(op) ?? null
}

/**
 * Opcodes an application may not send through gateway.send.
 * Heartbeat, Identify and Resume belong to the session the SDK manages, and a second Identify or an out-of-band
 * Resume would replace or end it. Server-only opcodes and the reserved Voice Server Ping close the connection with 4001
 * or 4003, which the SDK treats as permanent, so they are rejected locally instead
 */
export const reservedClientOpcodes: ReadonlySet<number> = new Set([
    Opcode.heartbeat,
    Opcode.identify,
    Opcode.resume,
    Opcode.dispatch,
    Opcode.reconnect,
    Opcode.invalidSession,
    Opcode.hello,
    Opcode.heartbeatAck,
    Opcode.gatewayError,
    Opcode.voiceServerPing,
])

/**
 * Maximum encoded client gateway payload, in UTF-8 bytes on the wire and after decompression. Fluxer instances older than
 * October 2026 allow 4,096 bytes. Uncompressed frames over 16,384 bytes close with WebSocket 1009 (MESSAGE_TOO_BIG)
 * before socket decoding, while payloads that exceed the limit only after decompression close with 4002, which stops
 * the shard (fluxer_gateway/src/fluxer_gateway/gateway/limits.gleam:6-8, gateway/ws/server.gleam:358-361, socket.gleam:435-452 at 66cede64)
 */
export const maxClientPayloadBytes = 16_384

/**
 * Identify session flags sent by this SDK. Fluxer also defines CHANNEL_THREADS at bit 2 and GUILD_VISITS at bit 3
 */
export const IdentifyFlag = Object.freeze({
    /** DEBOUNCE_MESSAGE_REACTIONS: Buffer community and direct-message reaction additions for 650 ms (cap 512) and flush multi-reaction runs as MESSAGE_REACTION_ADD_MANY */
    debounceMessageReactions: 1 << 1,
} as const)

/** Most ignored_events entries Fluxer accepts in one Identify. A longer array closes with 4002 */
export const maxIgnoredEvents = 256

/**
 * How long Fluxer retains a disconnected session for Resume, in milliseconds.
 * A client close with 1000 or 1001 destroys the session. Other client close codes or an abrupt socket end retain it
 * (fluxer_gateway/src/fluxer_gateway/gateway/limits.gleam and session/server.gleam at 66cede64)
 */
export const sessionRetentionMs = 60_000

/**
 * Most guilds one bot shard may serve, max_guilds_per_shard in
 * fluxer_gateway/src/fluxer_gateway/session/seed.gleam:114, 148-151 at 66cede64. Identify for a shard resolving to
 * more guilds closes with 4011
 */
export const maxGuildsPerShard = 2_500

/** Client-only private-use closure that retains the session for Resume, unlike client codes 1000 and 1001.
 * Code 4000 is accepted by ws and does not change classification of server-sent closures
 */
export const resumePreservingCloseCode = 4000

/** WebSocket and Fluxer gateway close codes the SDK recognizes */
export const CloseCode = Object.freeze({
    normal: 1000,
    goingAway: 1001,
    abnormal: 1006,
    invalidPayload: 1007,
    messageTooBig: 1009,
    serverError: 1011,
    unknownError: 4000,
    unknownOpcode: 4001,
    decodeError: 4002,
    notAuthenticated: 4003,
    authenticationFailed: 4004,
    alreadyAuthenticated: 4005,
    invalidSequence: 4007,
    rateLimited: 4008,
    sessionTimeout: 4009,
    invalidShard: 4010,
    shardingRequired: 4011,
    invalidApiVersion: 4012,
} as const)

/** How the SDK reacts to a gateway close code.
 * The resume action retries and resumes when possible. The identify action discards the session if an unfinished Resume
 * was rejected. Otherwise a retained session may be offered to Resume again. The wait action retries after the rate-limit
 * wait, and fatal stops retrying
 */
export type CloseAction = "resume" | "identify" | "wait" | "fatal"

export interface CloseCodeInfo {
    /** Stable constant-style name, or UNKNOWN for an unlisted code */
    readonly name: string
    readonly action: CloseAction
    /** Reviewed explanation of what the close code means, used in errors and log records */
    readonly meaning: string
}

// Server-sent 1007 and 1009 closures retry, while the local receive-limit and UTF-8 rejections are terminal in the gateway
// Each action and meaning is reviewed against Fluxer's GatewayConstants and the gateway handler call sites that send it
const closeCodes: ReadonlyMap<number, CloseCodeInfo> = new Map<number, CloseCodeInfo>([
    [CloseCode.normal, { name: "NORMAL", action: "resume", meaning: "The gateway closed the connection normally" }],
    [CloseCode.goingAway, { name: "GOING_AWAY", action: "resume", meaning: "The gateway is restarting or going away" }],
    [
        CloseCode.abnormal,
        { name: "ABNORMAL", action: "resume", meaning: "The connection dropped without a close frame" },
    ],
    [
        CloseCode.invalidPayload,
        {
            name: "INVALID_PAYLOAD",
            action: "resume",
            meaning: "The WebSocket connection rejected invalid payload encoding",
        },
    ],
    [
        CloseCode.messageTooBig,
        {
            name: "MESSAGE_TOO_BIG",
            action: "resume",
            meaning: "The WebSocket connection rejected a message exceeding its receive limit",
        },
    ],
    [
        CloseCode.serverError,
        { name: "SERVER_ERROR", action: "resume", meaning: "The gateway reported an internal error" },
    ],
    [
        CloseCode.unknownError,
        { name: "UNKNOWN_ERROR", action: "resume", meaning: "The gateway reported an unspecified error" },
    ],
    [
        CloseCode.unknownOpcode,
        {
            name: "UNKNOWN_OPCODE",
            action: "fatal",
            meaning: "The gateway rejected an unsupported command or missing command data",
        },
    ],
    [
        CloseCode.decodeError,
        {
            name: "DECODE_ERROR",
            action: "fatal",
            meaning: "The gateway rejected invalid payload encoding, size, compression or command fields",
        },
    ],
    [
        CloseCode.notAuthenticated,
        {
            name: "NOT_AUTHENTICATED",
            action: "fatal",
            meaning:
                "The gateway received a command before authentication established a session, or no Identify or Resume arrived in time",
        },
    ],
    [
        CloseCode.authenticationFailed,
        { name: "AUTHENTICATION_FAILED", action: "fatal", meaning: "Fluxer rejected the bot token" },
    ],
    [
        CloseCode.alreadyAuthenticated,
        {
            name: "ALREADY_AUTHENTICATED",
            action: "fatal",
            meaning:
                "The gateway rejected Identify or Resume because a session was already attached or command data was missing",
        },
    ],
    [
        CloseCode.invalidSequence,
        {
            name: "INVALID_SEQUENCE",
            action: "identify",
            meaning: "The gateway rejected the heartbeat or resume sequence",
        },
    ],
    [
        CloseCode.rateLimited,
        {
            name: "RATE_LIMITED",
            action: "wait",
            meaning: "The gateway rejected work because a connection, payload or session budget was exceeded",
        },
    ],
    [
        CloseCode.sessionTimeout,
        {
            name: "SESSION_TIMEOUT",
            action: "resume",
            meaning: "The gateway closed because it received no heartbeat from the bot in time",
        },
    ],
    [
        CloseCode.invalidShard,
        { name: "INVALID_SHARD", action: "fatal", meaning: "The gateway rejected the shard ID or total shard count" },
    ],
    [
        CloseCode.shardingRequired,
        { name: "SHARDING_REQUIRED", action: "fatal", meaning: "The gateway requires more shards for this bot" },
    ],
    [
        CloseCode.invalidApiVersion,
        {
            name: "INVALID_API_VERSION",
            action: "fatal",
            meaning: "The gateway rejected an absent or unsupported API version",
        },
    ],
])

/** Classification for any close code. Unlisted codes enter recovery, because an unknown closure is not proof of a permanent fault */
export function closeCodeInfo(code: number): CloseCodeInfo {
    return (
        closeCodes.get(code) ?? {
            name: "UNKNOWN",
            action: "resume",
            meaning: "The SDK does not recognize this Fluxer close code",
        }
    )
}

/** Readable next step after a retryable failure as one sentence, used in log messages.
 * The rateLimited flag says the wait is for a rate limit, and the resume flag says whether the next attempt will
 * actually send Resume, which requires a retained session and sequence.
 * The delay is already formatted with its unit, and established says whether the shard was connected before, so the
 * next attempt reconnects rather than retries startup
 */
export function closeActionText(rateLimited: boolean, resume: boolean, delay: string, established = true): string {
    const next = `${established ? "reconnecting" : "retrying"} ${resume ? "and resuming the session" : "with a new session"}`
    return rateLimited
        ? `Waiting ${delay} for the rate limit, then ${next}`
        : `${next.charAt(0).toUpperCase()}${next.slice(1)} in ${delay}`
}