lib/async/http/body/hijack.rb in async-http-0.56.6 vs lib/async/http/body/hijack.rb in async-http-0.57.0

- old
+ new

@@ -19,12 +19,14 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'protocol/http/body/readable' -require_relative 'stream' +require 'protocol/http/body/stream' +require_relative 'writable' + module Async module HTTP module Body # A body which is designed for hijacked server responses - a response which uses a block to read and write the request and response bodies respectively. class Hijack < ::Protocol::HTTP::Body::Readable @@ -40,10 +42,11 @@ @block = block @input = input @task = nil @stream = nil + @output = nil end # We prefer streaming directly as it's the lowest overhead. def stream? true @@ -55,35 +58,30 @@ attr :input # Has the producer called #finish and has the reader consumed the nil token? def empty? - if @stream - @stream.empty? - else - false - end + @output&.empty? end def ready? - if @stream - @stream.output.ready? - end + @output&.ready? end # Read the next available chunk. def read - unless @task - @stream = Stream.new(@input) + unless @output + @output = Writable.new + @stream = ::Protocol::HTTP::Body::Stream.new(@input, @output) @task = Task.current.async do |task| task.annotate "Streaming hijacked body." @block.call(@stream) end end - return @stream.output.read + return @output.read end def inspect "\#<#{self.class} #{@block.inspect}>" end