Skip to content

Commit

Permalink
Restore Readable#buffered and add Buffered#buffered.
Browse files Browse the repository at this point in the history
Fixes compatibility with `async-http-cache`.
  • Loading branch information
ioquatix committed Oct 14, 2024
1 parent 5a9a061 commit c60c9f8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/protocol/http/body/buffered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def initialize(chunks = [], length = nil)

attr :chunks

# A rewindable body wraps some other body. Convert it to a buffered body. The buffered body will share the same chunks as the rewindable body.
#
# @returns [Buffered] the buffered body.
def buffered
self.class.new(@chunks)
end

def finish
self
end
Expand All @@ -64,7 +71,7 @@ def close(error = nil)
end

def clear
@chunks.clear
@chunks = []
@length = 0
@index = 0
end
Expand Down
13 changes: 11 additions & 2 deletions lib/protocol/http/body/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ module Body
#
# Typically, you'd override `#read` to return chunks of data.
#
# I n general, you read chunks of data from a body until it is empty and returns `nil`. Upon reading `nil`, the body is considered consumed and should not be read from again.
# In general, you read chunks of data from a body until it is empty and returns `nil`. Upon reading `nil`, the body is considered consumed and should not be read from again.
#
# Reading can also fail, for example if the body represents a streaming upload, and the connection is lost. In this case, the body will raise some kind of error.
# Reading can also fail, for example if the body represents a streaming upload, and the connection is lost. In this case, `#read` will raise some kind of error.
#
# If you don't want to read from a stream, and instead want to close it immediately, you can call `close` on the body. If the body is already completely consumed, `close` will do nothing, but if there is still data to be read, it will cause the underlying stream to be reset (and possibly closed).
class Readable
Expand Down Expand Up @@ -50,6 +50,15 @@ def rewind
false
end

# Return a buffered representation of this body.
#
# This method must return a buffered body if `#rewindable?`.
#
# @returns [Buffered | Nil] The buffered body.
def buffered
nil
end

# The total length of the body, if known.
# @returns [Integer | Nil] The total length of the body, or `nil` if the length is unknown.
def length
Expand Down
4 changes: 4 additions & 0 deletions lib/protocol/http/body/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def ready?
@body.ready?
end

def buffered
@body.buffered
end

def rewind
@body.rewind
end
Expand Down
17 changes: 17 additions & 0 deletions test/protocol/http/body/buffered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@
end
end

with "#buffered" do
let(:buffered_body) {body.buffered}

it "returns a buffered body" do
expect(buffered_body).to be_a(subject)
expect(buffered_body.read).to be == "Hello"
expect(buffered_body.read).to be == "World"
end

it "doesn't affect the original body" do
expect(buffered_body.join).to be == "HelloWorld"

expect(buffered_body).to be(:empty?)
expect(body).not.to be(:empty?)
end
end

with "#each" do
with "a block" do
it "iterates over chunks" do
Expand Down

0 comments on commit c60c9f8

Please sign in to comment.