File tree Expand file tree Collapse file tree 4 files changed +11
-83
lines changed Expand file tree Collapse file tree 4 files changed +11
-83
lines changed Original file line number Diff line number Diff line change 20
20
gem "decode"
21
21
gem "rubocop"
22
22
23
- gem "sus-fixtures-async-http" , "~> 0.1"
23
+ gem "sus-fixtures-async"
24
+ gem "sus-fixtures-async-http"
24
25
25
- gem "bake-test" , "~> 0.1"
26
- gem "bake-test-external" , "~> 0.1"
26
+ gem "bake-test"
27
+ gem "bake-test-external"
27
28
end
Original file line number Diff line number Diff line change 3
3
# Released under the MIT License.
4
4
# Copyright, 2022-2024, by Samuel Williams.
5
5
6
- require 'protocol/http/body/readable'
7
- require 'protocol/http/body/stream'
6
+ require 'protocol/http/body/streamable'
8
7
9
8
module Protocol
10
9
module Rack
11
10
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
83
12
end
84
13
end
85
14
end
Original file line number Diff line number Diff line change @@ -24,6 +24,6 @@ Gem::Specification.new do |spec|
24
24
25
25
spec . required_ruby_version = ">= 3.1"
26
26
27
- spec . add_dependency "protocol-http" , "~> 0.27 "
27
+ spec . add_dependency "protocol-http" , "~> 0.37 "
28
28
spec . add_dependency "rack" , ">= 1.0"
29
29
end
Original file line number Diff line number Diff line change 5
5
6
6
require 'protocol/rack/body/streaming'
7
7
8
+ require 'sus/fixtures/async/scheduler_context'
9
+
8
10
describe Protocol ::Rack ::Body ::Streaming do
11
+ include Sus ::Fixtures ::Async ::SchedulerContext
12
+
9
13
let ( :block ) { proc { |stream | stream . write ( "Hello" ) ; stream . write ( "World" ) ; stream . close } }
10
14
let ( :body ) { subject . new ( block ) }
11
15
12
- with '#block' do
13
- it "should wrap block" do
14
- expect ( body . block ) . to be == block
15
- end
16
- end
17
-
18
16
with '#read' do
19
17
it "can read the body" do
20
18
expect ( body . read ) . to be == "Hello"
You can’t perform that action at this time.
0 commit comments