<commit_before># encoding: utf-8

require "forwardable"

require_relative "./rack"

module SockJS
  module Thin
    class Request < Rack::Request
    end


    # This is just to make Rack happy.
    DUMMY_RESPONSE ||= [-1, Hash.new, Array.new]


    class Response < Response
      def async?
        @body.is_a?(DelayedResponseBody)
      end
    end


    class AsyncResponse < Response
      extend Forwardable

      # env["async.callback"]
      def initialize(async_callback, status = nil, headers = Hash.new, &block)
        @async_callback   = async_callback
        @status, @headers = status, headers
        @body = DelayedResponseBody.new

        block.call(self) if block
      end

      def write_head(status = nil, headers = nil)
        super(status, headers) do
          @async_callback.call(@status, @headers, @body)
        end
      end

      def_delegator :body, :write
      def_delegator :body, :finish
    end


    class SyncResponse < Response
      def write_head(status = nil, headers = nil)
        super(status, headers) do
          # Do nothing. Frankly, what the hell are we suppose to do when Rack doesn't support it?
        end
      end

      def write(data)
        super() do
          @body << data
        end
      end

      def finish(data = nil)
        super(data) do
          @body = [@body] if @body.respond_to?(:bytesize)
          [@status, @headers, @body]
        end
      end
    end


    class DelayedResponseBody
      include EventMachine::Deferrable

      def call(body)
        body.each do |chunk|
          self.write(chunk)
        end
      end

      def write(chunk)
        @body_callback.call(chunk)
      end

      def each(&block)
        @body_callback = block
      end

      alias_method :finish, :succeed
    end
  end
end
<commit_msg>Although ...<commit_after># encoding: utf-8

require "forwardable"

require_relative "./rack"

module SockJS
  module Thin
    class Request < Rack::Request
    end


    # This is just to make Rack happy.
    DUMMY_RESPONSE ||= [-1, Hash.new, Array.new]


    class Response < Response
      def async?
        @body.is_a?(DelayedResponseBody)
      end
    end


    class AsyncResponse < Response
      extend Forwardable

      # env["async.callback"]
      def initialize(async_callback, status = nil, headers = Hash.new, &block)
        @async_callback   = async_callback
        @status, @headers = status, headers
        @body = DelayedResponseBody.new

        block.call(self) if block
      end

      def write_head(status = nil, headers = nil)
        super(status, headers) do
          @async_callback.call(@status, @headers, @body)
        end
      end

      def_delegator :body, :write
      def_delegator :body, :finish
    end


    # Wouldn't it be better to make everything
    # simply async? The API we have is async anyway.
    # We would get rid of these stupid hacks AND
    # there's a significant chance that Rack::Lint
    # wouldn't screw with us anymore!
    class SyncResponse < Response
      def write_head(status = nil, headers = nil)
        super(status, headers) do
          # Do nothing. Frankly, what the hell are we suppose to do when Rack doesn't support it?
        end
      end

      def write(data)
        super() do
          @body << data
        end
      end

      def finish(data = nil)
        super(data) do
          @body = [@body] if @body.respond_to?(:bytesize)
          [@status, @headers, @body]
        end
      end
    end


    class DelayedResponseBody
      include EventMachine::Deferrable

      def call(body)
        body.each do |chunk|
          self.write(chunk)
        end
      end

      def write(chunk)
        @body_callback.call(chunk)
      end

      def each(&block)
        @body_callback = block
      end

      alias_method :finish, :succeed
    end
  end
end

{'fraction_non_alphanumeric': 0.08365608055770721, 'fraction_numerical': 0.0010327911179963851, 'mean_word_length': 1.9915057915057914, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, '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': '4314413', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1124, 'n_words': 353}