Class: Rack::Brotli::Deflater::BrotliStream

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/brotli/deflater.rb

Overview

Body class used for encoded responses.

Constant Summary collapse

BUFFER_LENGTH =
128 * 1_024

Instance Method Summary collapse

Constructor Details

#initialize(body, sync, br_options) ⇒ BrotliStream

Returns a new instance of BrotliStream.



72
73
74
75
76
# File 'lib/rack/brotli/deflater.rb', line 72

def initialize(body, sync, br_options)
  @body = body
  @br_options = br_options
  @sync = sync
end

Instance Method Details

#closeObject

Close the original body if possible.



107
108
109
# File 'lib/rack/brotli/deflater.rb', line 107

def close
  @body.close if @body.respond_to?(:close)
end

#each(&block) ⇒ Object

Yield compressed strings to the given block.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rack/brotli/deflater.rb', line 79

def each(&block)
  @writer = block
  br = Brotli::Writer.new(self, @br_options)
  # @body.each is equivalent to @body.gets (slow)
  if @body.is_a? ::File # XXX: Should probably be ::IO
    while part = @body.read(BUFFER_LENGTH)
      br.write(part)
      br.flush if @sync
    end
  else
    @body.each { |part|
      # Skip empty strings, as they would result in no output,
      # and flushing empty parts could raise an IO error.
      next if part.empty?
      br.write(part)
      br.flush if @sync
    }
  end
ensure
  br.finish
end

#write(data) ⇒ Object

Call the block passed to #each with the compressed data.



102
103
104
# File 'lib/rack/brotli/deflater.rb', line 102

def write(data)
  @writer.call(data)
end