Skip to content

Commit dc67087

Browse files
committed
Use Streaming implementation from protocol-http.
1 parent c99892b commit dc67087

File tree

4 files changed

+11
-83
lines changed

4 files changed

+11
-83
lines changed

gems.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
gem "decode"
2121
gem "rubocop"
2222

23-
gem "sus-fixtures-async-http", "~> 0.1"
23+
gem "sus-fixtures-async"
24+
gem "sus-fixtures-async-http"
2425

25-
gem "bake-test", "~> 0.1"
26-
gem "bake-test-external", "~> 0.1"
26+
gem "bake-test"
27+
gem "bake-test-external"
2728
end

lib/protocol/rack/body/streaming.rb

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,12 @@
33
# Released under the MIT License.
44
# Copyright, 2022-2024, by Samuel Williams.
55

6-
require 'protocol/http/body/readable'
7-
require 'protocol/http/body/stream'
6+
require 'protocol/http/body/streamable'
87

98
module Protocol
109
module Rack
1110
module Body
12-
# Wraps a streaming response body into a compatible Protocol::HTTP body.
13-
class Streaming < ::Protocol::HTTP::Body::Readable
14-
def initialize(block, input = nil)
15-
@block = block
16-
@input = input
17-
@output = nil
18-
end
19-
20-
attr :block
21-
22-
class Output
23-
def initialize(input, block)
24-
stream = ::Protocol::HTTP::Body::Stream.new(input, self)
25-
26-
@from = nil
27-
28-
@fiber = Fiber.new do |from|
29-
@from = from
30-
block.call(stream)
31-
@fiber = nil
32-
end
33-
end
34-
35-
def write(chunk)
36-
if from = @from
37-
@from = nil
38-
@from = from.transfer(chunk)
39-
else
40-
raise RuntimeError, "Stream is not being read!"
41-
end
42-
end
43-
44-
def close(error = nil)
45-
@fiber = nil
46-
47-
if from = @from
48-
@from = nil
49-
if error
50-
from.raise(error)
51-
else
52-
from.transfer(nil)
53-
end
54-
end
55-
end
56-
57-
def close_write(error = nil)
58-
close(error)
59-
end
60-
61-
def read
62-
raise RuntimeError, "Stream is already being read!" if @from
63-
64-
@fiber&.transfer(Fiber.current)
65-
end
66-
end
67-
68-
# Invokes the block in a fiber which yields chunks when they are available.
69-
def read
70-
@output ||= Output.new(@input, @block)
71-
return @output.read
72-
end
73-
74-
def stream?
75-
true
76-
end
77-
78-
def call(stream)
79-
raise "Streaming body has already been read!" if @output
80-
@block.call(stream)
81-
end
82-
end
11+
Streaming = ::Protocol::HTTP::Body::Streamable::ResponseBody
8312
end
8413
end
8514
end

protocol-rack.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ Gem::Specification.new do |spec|
2424

2525
spec.required_ruby_version = ">= 3.1"
2626

27-
spec.add_dependency "protocol-http", "~> 0.27"
27+
spec.add_dependency "protocol-http", "~> 0.37"
2828
spec.add_dependency "rack", ">= 1.0"
2929
end

test/protocol/rack/body/streaming.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55

66
require 'protocol/rack/body/streaming'
77

8+
require 'sus/fixtures/async/scheduler_context'
9+
810
describe Protocol::Rack::Body::Streaming do
11+
include Sus::Fixtures::Async::SchedulerContext
12+
913
let(:block) {proc{|stream| stream.write("Hello"); stream.write("World"); stream.close}}
1014
let(:body) {subject.new(block)}
1115

12-
with '#block' do
13-
it "should wrap block" do
14-
expect(body.block).to be == block
15-
end
16-
end
17-
1816
with '#read' do
1917
it "can read the body" do
2018
expect(body.read).to be == "Hello"

0 commit comments

Comments
 (0)