Skip to content

Commit c60c9f8

Browse files
committed
Restore Readable#buffered and add Buffered#buffered.
Fixes compatibility with `async-http-cache`.
1 parent 5a9a061 commit c60c9f8

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

lib/protocol/http/body/buffered.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ def initialize(chunks = [], length = nil)
5252

5353
attr :chunks
5454

55+
# 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.
56+
#
57+
# @returns [Buffered] the buffered body.
58+
def buffered
59+
self.class.new(@chunks)
60+
end
61+
5562
def finish
5663
self
5764
end
@@ -64,7 +71,7 @@ def close(error = nil)
6471
end
6572

6673
def clear
67-
@chunks.clear
74+
@chunks = []
6875
@length = 0
6976
@index = 0
7077
end

lib/protocol/http/body/readable.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ module Body
1111
#
1212
# Typically, you'd override `#read` to return chunks of data.
1313
#
14-
# 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.
14+
# 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.
1515
#
16-
# 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.
16+
# 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.
1717
#
1818
# 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).
1919
class Readable
@@ -50,6 +50,15 @@ def rewind
5050
false
5151
end
5252

53+
# Return a buffered representation of this body.
54+
#
55+
# This method must return a buffered body if `#rewindable?`.
56+
#
57+
# @returns [Buffered | Nil] The buffered body.
58+
def buffered
59+
nil
60+
end
61+
5362
# The total length of the body, if known.
5463
# @returns [Integer | Nil] The total length of the body, or `nil` if the length is unknown.
5564
def length

lib/protocol/http/body/wrapper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def ready?
4242
@body.ready?
4343
end
4444

45+
def buffered
46+
@body.buffered
47+
end
48+
4549
def rewind
4650
@body.rewind
4751
end

test/protocol/http/body/buffered.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@
131131
end
132132
end
133133

134+
with "#buffered" do
135+
let(:buffered_body) {body.buffered}
136+
137+
it "returns a buffered body" do
138+
expect(buffered_body).to be_a(subject)
139+
expect(buffered_body.read).to be == "Hello"
140+
expect(buffered_body.read).to be == "World"
141+
end
142+
143+
it "doesn't affect the original body" do
144+
expect(buffered_body.join).to be == "HelloWorld"
145+
146+
expect(buffered_body).to be(:empty?)
147+
expect(body).not.to be(:empty?)
148+
end
149+
end
150+
134151
with "#each" do
135152
with "a block" do
136153
it "iterates over chunks" do

0 commit comments

Comments
 (0)