You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# If the fiber then tries to write to the stream, it will raise a ClosedError, and we will end up here. We can ignore it, as we are already closing the stream and don't care about further writes.
97
109
end
@@ -102,7 +114,14 @@ def close(error = nil)
102
114
defread
103
115
raiseRuntimeError,"Stream is already being read!"if@from
104
116
105
-
@fiber&.transfer(Fiber.current)
117
+
ifto=@to
118
+
@from=Fiber.current
119
+
@to=nil
120
+
121
+
to.transfer
122
+
else
123
+
returnnil
124
+
end
106
125
end
107
126
end
108
127
@@ -155,30 +174,82 @@ def call(stream)
155
174
156
175
# Closing a stream indicates we are no longer interested in reading from it.
157
176
defclose(error=nil)
177
+
$stderr.puts"Closing output #{@output}..."
178
+
ifoutput=@output
179
+
@output=nil
180
+
# Closing the output here may take some time, as it may need to finish handling the stream:
181
+
output.close(error)
182
+
end
183
+
184
+
$stderr.puts"Closing input #{@input}..."
158
185
ifinput=@input
159
186
@input=nil
160
187
input.close(error)
161
188
end
162
-
163
-
ifoutput=@output
164
-
@output=nil
165
-
output.close(error)
189
+
end
190
+
end
191
+
192
+
classInput
193
+
definitialize(from=Fiber.current)
194
+
@from=from
195
+
@to=nil
196
+
end
197
+
198
+
defread
199
+
iffrom=@from
200
+
@from=nil
201
+
@to=Fiber.current
202
+
203
+
returnfrom.transfer
204
+
else
205
+
raiseClosedError,"Stream is not being written!"
206
+
end
207
+
end
208
+
209
+
defwrite(chunk)
210
+
ifto=@to
211
+
@from=Fiber.current
212
+
@to=nil
213
+
214
+
to.transfer(chunk)
215
+
else
216
+
raiseClosedError,"Stream is not being read!"
217
+
end
218
+
end
219
+
220
+
defclose_write(error=nil)
221
+
ifto=@to
222
+
@from=Fiber.current
223
+
@to=nil
224
+
225
+
iferror
226
+
to.raise(error)
227
+
else
228
+
to.transfer(nil)
229
+
end
230
+
end
231
+
end
232
+
233
+
defclose(error=nil)
234
+
close_write(error)
235
+
end
236
+
237
+
defstream(body)
238
+
body&.eachdo |chunk|
239
+
self.write(chunk)
166
240
end
167
241
end
168
242
end
169
243
170
244
# A deferred body has an extra `stream` method which can be used to stream data into the body, as the response body won't be available until the request has been sent.
171
245
classDeferredBody < Body
172
246
definitialize(block)
173
-
super(block,Writable.new)
247
+
super(block,Input.new)
174
248
end
175
249
176
250
# Stream the response body into the block's input.
0 commit comments