<commit_before>require "http"

module Discord
  # Internal wrapper around HTTP::WebSocket to decode the Discord-specific
  # payload format used in the gateway and VWS.
  class WebSocket
    # :nodoc:
    struct Packet
      getter opcode, sequence, data, event_type

      def initialize(@opcode : Int64?, @sequence : Int64?, @data : IO::Memory, @event_type : String?)
      end

      def inspect(io : IO)
        io << "Discord::WebSocket::Packet(@opcode="
        opcode.inspect(io)
        io << " @sequence="
        sequence.inspect(io)
        io << " @data="
        data.to_s.inspect(io)
        io << " @event_type="
        event_type.inspect(io)
        io << ')'
      end
    end

    def initialize(@host : String, @path : String, @port : Int32, @tls : Bool, @logger : Logger)
      @websocket = HTTP::WebSocket.new(
        host: @host,
        path: @path,
        port: @port,
        tls: @tls
      )
    end

    def on_message(&handler : Packet ->)
      @websocket.on_message do |message|
        @logger.debug "[WS IN] #{message}" if @logger.debug?
        payload = parse_message(message)
        handler.call(payload)
      end
    end

    def on_close(&handler : String ->)
      @websocket.on_close(&handler)
    end

    delegate run, close, to: @websocket

    def send(message)
      @logger.debug "[WS OUT] #{message}" if @logger.debug?
      @websocket.send(message)
    end

    private def parse_message(message : String)
      parser = JSON::PullParser.new(message)

      opcode = nil
      sequence = nil
      event_type = nil
      data = IO::Memory.new

      parser.read_object do |key|
        case key
        when "op"<commit_msg>Detect and decompress compressed payloads<commit_after>require "http"
require "zlib"

module Discord
  # Internal wrapper around HTTP::WebSocket to decode the Discord-specific
  # payload format used in the gateway and VWS.
  class WebSocket
    # :nodoc:
    struct Packet
      getter opcode, sequence, data, event_type

      def initialize(@opcode : Int64?, @sequence : Int64?, @data : IO::Memory, @event_type : String?)
      end

      def inspect(io : IO)
        io << "Discord::WebSocket::Packet(@opcode="
        opcode.inspect(io)
        io << " @sequence="
        sequence.inspect(io)
        io << " @data="
        data.to_s.inspect(io)
        io << " @event_type="
        event_type.inspect(io)
        io << ')'
      end
    end

    def initialize(@host : String, @path : String, @port : Int32, @tls : Bool, @logger : Logger)
      @websocket = HTTP::WebSocket.new(
        host: @host,
        path: @path,
        port: @port,
        tls: @tls
      )
    end

    def on_message(&handler : Packet ->)
      @websocket.on_message do |message|
        @logger.debug "[WS IN] #{message}" if @logger.debug?
        payload = parse_message(message)
        handler.call(payload)
      end
    end

    def on_close(&handler : String ->)
      @websocket.on_close(&handler)
    end

    delegate run, close, to: @websocket

    def send(message)
      @logger.debug "[WS OUT] #{message}" if @logger.debug?
      @websocket.send(message)
    end

    private def parse_message(message : String)
      if message.byte_at(0) == 'x'
        # The message is compressed
        io = IO::Memory.new(message)
        Zlib::Reader.open(io, sync_close = true) { |reader| message = reader.gets_to_end }
      end

      parser = JSON::PullParser.new(message)

      opcode = nil
      sequence = nil
      event_type = nil
      data = IO::Memory.new

      parser.read_object do |key|
        case key
        when "op"
{'fraction_non_alphanumeric': 0.11948988078735792, 'fraction_numerical': 0.0036041031327973387, 'mean_word_length': 2.2829845313921746, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4410041', 'n_tokens_mistral': 1153, 'n_tokens_neox': 1104, 'n_words': 297}