Module: Sinatra::Streaming
- Defined in:
- lib/cloudflare_workers/stream.rb
Class Method Summary collapse
-
.registered(app) ⇒ Object
Register the helper on a Sinatra app.
Instance Method Summary collapse
-
#sse(headers: nil, &block) ⇒ Object
Emit a Server-Sent Events response.
-
#stream(keep_open: false, type: :plain, headers: nil, &block) ⇒ Object
Sinatra本家の ‘stream do |out| …
Class Method Details
.registered(app) ⇒ Object
Register the helper on a Sinatra app. Use ‘register Sinatra::Streaming`.
285 286 287 |
# File 'lib/cloudflare_workers/stream.rb', line 285 def self.registered(app) app.helpers Streaming end |
Instance Method Details
#sse(headers: nil, &block) ⇒ Object
Emit a Server-Sent Events response. The block receives a writer (‘out`) that accepts `<<` (raw string) and `event(data, event:, id:)` for framed events. The block runs in an async task attached to `ctx.waitUntil`, so the isolate stays alive until the block ends.
241 242 243 244 |
# File 'lib/cloudflare_workers/stream.rb', line 241 def sse(headers: nil, &block) ctx = env['cloudflare.ctx'] ::Cloudflare::SSEStream.new(headers: headers, ctx: ctx, &block) end |
#stream(keep_open: false, type: :plain, headers: nil, &block) ⇒ Object
Sinatra本家の ‘stream do |out| … end` 互換 DSL。本家の `Sinatra::Helpers#stream` は Rack EM/Thin で動くスケジューラを前提にするが、Workers では EventMachine も Thread も無いので代わりに Cloudflare の `ReadableStream` パスを使う。
Call sites look identical to upstream Sinatra:
get '/numbers' do
stream do |out|
10.times do |i|
out << "chunk #{i}\n"
out.sleep(0.1).__await__
end
end
end
SSE 用に ‘stream(type: :sse) do |out| … end` もサポート。type のデフォルトは `:plain` (text/plain) で、SSE-like ヘッダは付かない。:sse / :event_stream を指定すると Cloudflare::SSEStream::DEFAULT_HEADERS がマージされる(`sse do |out|` と同じ挙動)。
‘keep_open:` は本家互換だが Workers では意味がない(EM 前提)ため受け取るだけで使わない。
269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/cloudflare_workers/stream.rb', line 269 def stream(keep_open: false, type: :plain, headers: nil, &block) ctx = env['cloudflare.ctx'] extra_headers = headers || {} merged = case type when :sse, :event_stream extra_headers # SSE defaults は SSEStream 側で入る else # Plain streaming — start from an empty default set so # the SSE headers don't get force-injected into e.g. a # log-tailing or chunked-JSON endpoint. { 'content-type' => 'text/plain; charset=utf-8' }.merge(extra_headers) end ::Cloudflare::SSEStream.new(headers: merged, ctx: ctx, &block) end |