Module: Rack::Handler::CloudflareWorkers

Defined in:
lib/cloudflare_workers.rb

Constant Summary collapse

EMPTY_STRING_IO =
StringIO.new('').freeze

Class Method Summary collapse

Class Method Details

.appObject



132
133
134
# File 'lib/cloudflare_workers.rb', line 132

def self.app
  @app
end

.call(js_req, js_env, js_ctx, body_text = '') ⇒ Object

Entry point invoked from the Module Worker (src/worker.mjs) for every fetch event. ‘js_req` is a Cloudflare Workers Request, `js_env` is the bindings object (D1, KV, R2, secrets…), `js_ctx` is the ExecutionContext, `body_text` is the pre-resolved request body (the worker.mjs front awaits req.text() before handing control to Ruby because Opal runs synchronously).



142
143
144
145
146
147
148
149
150
# File 'lib/cloudflare_workers.rb', line 142

def self.call(js_req, js_env, js_ctx, body_text = '')
  raise '`run app` was never called from user code' if @app.nil?

  env = build_rack_env(js_req, js_env, js_ctx, body_text)
  status, headers, body = @app.call(env)
  build_js_response(status, headers, body)
ensure
  body.close if body.respond_to?(:close) && body
end

.run(app, **_options) ⇒ Object



126
127
128
129
130
# File 'lib/cloudflare_workers.rb', line 126

def self.run(app, **_options)
  @app = app
  install_dispatcher
  app
end