Sha256: de59ef045d3b2c8a5f088c406e96d357e13a5d65d50f87d63f1afea80108317c

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Gitlab
  module Styles
    module Rubocop
      module Cop
        module Performance
          # This cop flags inefficient uses of rubyzip's Zip::File, since when instantiated
          # it reads the file's Central Directory into memory entirely. For zips with many
          # files and directories, this can be very expensive even when the archive's size
          # in bytes is small.
          #
          # See also:
          # - https://github.com/rubyzip/rubyzip/issues/506
          # - https://github.com/rubyzip/rubyzip#notes-on-zipinputstream
          class Rubyzip < RuboCop::Cop::Cop
            MSG = 'Be careful when opening or iterating zip files via Zip::File. ' \
                  'Zip archives may contain many entries, and their file index is ' \
                  'read into memory upon construction, which can lead to ' \
                  'high memory use and poor performance. ' \
                  'Consider iterating archive entries via Zip::InputStream instead.'

            def_node_matcher :reads_central_directory?, <<-PATTERN
              (send
                (const
                  (const {nil? (cbase)} :Zip) :File) {:new :open :foreach} ...)
            PATTERN

            def on_send(node)
              return unless reads_central_directory?(node)

              add_offense(node)
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gitlab-styles-7.1.0 lib/gitlab/styles/rubocop/cop/performance/rubyzip.rb
gitlab-styles-7.0.0 lib/gitlab/styles/rubocop/cop/performance/rubyzip.rb
gitlab-styles-6.6.0 lib/gitlab/styles/rubocop/cop/performance/rubyzip.rb