<commit_before># frozen_string_literal: true

module Faraday
  class Adapter
    # EventMachine adapter. This adapter is useful for either asynchronous
    # requests when in an EM reactor loop, or for making parallel requests in
    # synchronous code.
    class EMHttp < Faraday::Adapter
      # Options is a module containing helpers to convert the Faraday env object
      # into options hashes for EMHTTP method calls.
      module Options
        # @return [Hash]
        def connection_config(env)
          options = {}
          configure_proxy(options, env)
          configure_timeout(options, env)
          configure_socket(options, env)
          configure_ssl(options, env)
          options
        end

        def request_config(env)
          options = {
            body: read_body(env),
            head: env[:request_headers]
            # keepalive: true,
            # file: 'path/to/file', # stream data off disk
          }
          configure_compression(options, env)
          options
        end

        def read_body(env)
          body = env[:body]
          body.respond_to?(:read) ? body.read : body
        end

        # Reads out proxy settings from env into options
        def configure_proxy(options, env)
          proxy = request_options(env)[:proxy]
          return unless proxy

          options[:proxy] = {
            host: proxy[:uri].host,
            port: proxy[:uri].port,
            authorization: [proxy[:user], proxy[:password]]
          }
        end

        # Reads out host and port settings from env into options
        def configure_socket(options, env)
          bind = request_options(env)[:bind]
          return unless bind

          options[:bind] = {
            host: bind[:host],
            port: bind[:port]
          }
        end

        # Reads out SSL certificate settings from env into options
        def configure_ssl(options, env)
          return unless env[:url].scheme == 'https' && env[:ssl]

          options[:ssl] = {
            cert_chain_file: env[:ssl][:ca_file],
            verify_peer: env[:ssl].fetch(:verify, true)
          }
        end

        # Reads out timeout settings from env into options
        def configure_timeout(options, env)
          req = request_options(env)
          options[:inactivity_timeout] = request_timeout(:read, req)
          options[:connect_timeout] = request_timeout(:open, req)
        end

        # Reads out compression header settings from env into options
        def configure_compression(options, env)
          return unless (env[:method] == :get) &&
                        !options[:head].key?('accept-encoding')

          options[:head]['accept-encoding'] = 'gzip, compressed'
        end

        def request_options(env)
          env[:request]
        end
      end

      include Options

      dependency do
        require 'em-http'

        if Faraday::Adapter::EMHttp.loaded?
          begin
            require 'openssl'
          rescue LoadError
            warn 'Warning: no such file to load -- openssl. ' \
              'Make sure it is installed if you want HTTPS support'
          else
            require 'em-http/version'
            if EventMachine::HttpRequest::VERSION < '1.1.6'
              require 'faraday/adapter/em_http_ssl_patch'
            end
          end
        end
      end

      self.supports_parallel = true

      # @return [Manager]
      def self.setup_parallel_manager(_options = nil)<commit_msg>EmHttp adapter: drop superfluous loaded? check (#1213)<commit_after># frozen_string_literal: true

module Faraday
  class Adapter
    # EventMachine adapter. This adapter is useful for either asynchronous
    # requests when in an EM reactor loop, or for making parallel requests in
    # synchronous code.
    class EMHttp < Faraday::Adapter
      # Options is a module containing helpers to convert the Faraday env object
      # into options hashes for EMHTTP method calls.
      module Options
        # @return [Hash]
        def connection_config(env)
          options = {}
          configure_proxy(options, env)
          configure_timeout(options, env)
          configure_socket(options, env)
          configure_ssl(options, env)
          options
        end

        def request_config(env)
          options = {
            body: read_body(env),
            head: env[:request_headers]
            # keepalive: true,
            # file: 'path/to/file', # stream data off disk
          }
          configure_compression(options, env)
          options
        end

        def read_body(env)
          body = env[:body]
          body.respond_to?(:read) ? body.read : body
        end

        # Reads out proxy settings from env into options
        def configure_proxy(options, env)
          proxy = request_options(env)[:proxy]
          return unless proxy

          options[:proxy] = {
            host: proxy[:uri].host,
            port: proxy[:uri].port,
            authorization: [proxy[:user], proxy[:password]]
          }
        end

        # Reads out host and port settings from env into options
        def configure_socket(options, env)
          bind = request_options(env)[:bind]
          return unless bind

          options[:bind] = {
            host: bind[:host],
            port: bind[:port]
          }
        end

        # Reads out SSL certificate settings from env into options
        def configure_ssl(options, env)
          return unless env[:url].scheme == 'https' && env[:ssl]

          options[:ssl] = {
            cert_chain_file: env[:ssl][:ca_file],
            verify_peer: env[:ssl].fetch(:verify, true)
          }
        end

        # Reads out timeout settings from env into options
        def configure_timeout(options, env)
          req = request_options(env)
          options[:inactivity_timeout] = request_timeout(:read, req)
          options[:connect_timeout] = request_timeout(:open, req)
        end

        # Reads out compression header settings from env into options
        def configure_compression(options, env)
          return unless (env[:method] == :get) &&
                        !options[:head].key?('accept-encoding')

          options[:head]['accept-encoding'] = 'gzip, compressed'
        end

        def request_options(env)
          env[:request]
        end
      end

      include Options

      dependency do
        require 'em-http'

        begin
          require 'openssl'
        rescue LoadError
          warn 'Warning: no such file to load -- openssl. ' \
            'Make sure it is installed if you want HTTPS support'
        else
          require 'em-http/version'
          if EventMachine::HttpRequest::VERSION < '1.1.6'
            require 'faraday/adapter/em_http_ssl_patch'
          end
        end
      end

      self.supports_parallel = true

      # @return [Manager]
      def self.setup_parallel_manager(_options = nil)
{'fraction_non_alphanumeric': 0.08449690513890888, 'fraction_numerical': 0.0014394702749388226, 'mean_word_length': 1.8698884758364311, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6413695', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1750, 'n_words': 534}