Class: Cloudflare::DurableObjectRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/homura/runtime/durable_object.rb

Overview

The incoming ‘request` argument passed to DO handlers. `body_text` is pre-awaited by the JS dispatcher because Ruby runs synchronously under Opal (same pattern as `Rack::Handler::Homura.call`).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(js_request, body_text = '') ⇒ DurableObjectRequest

Returns a new instance of DurableObjectRequest.



552
553
554
555
# File 'lib/homura/runtime/durable_object.rb', line 552

def initialize(js_request, body_text = '')
  @js_request = js_request
  @body = body_text.to_s
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



550
551
552
# File 'lib/homura/runtime/durable_object.rb', line 550

def body
  @body
end

#js_requestObject (readonly)

Returns the value of attribute js_request.



550
551
552
# File 'lib/homura/runtime/durable_object.rb', line 550

def js_request
  @js_request
end

Instance Method Details

#[](name) ⇒ Object



590
591
592
# File 'lib/homura/runtime/durable_object.rb', line 590

def [](name)
  headers[name.to_s.downcase]
end

#headersObject

Shallow header Hash with lowercased keys.



582
583
584
585
586
587
588
# File 'lib/homura/runtime/durable_object.rb', line 582

def headers
  return @headers if @headers
  h = {}
  js = @js_request
  `(#{js} && #{js}.headers && typeof #{js}.headers.forEach === 'function') && #{js}.headers.forEach(function(v, k) { #{h}.$store(String(k).toLowerCase(), String(v)); })`
  @headers = h
end

#jsonObject



594
595
596
597
598
# File 'lib/homura/runtime/durable_object.rb', line 594

def json
  JSON.parse(body)
rescue JSON::ParserError
  {}
end

#methodObject



557
558
559
560
# File 'lib/homura/runtime/durable_object.rb', line 557

def method
  js = @js_request
  `(#{js} ? String(#{js}.method || 'GET') : 'GET')`
end

#pathObject



567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/homura/runtime/durable_object.rb', line 567

def path
  u = url
  return '' if u.nil? || u.empty?
  begin
    # Extract pathname via URL() so relative paths aren't mangled.
    u_str = u
    `new URL(#{u_str}).pathname`
  rescue StandardError
    # Fallback regex — strip scheme+host, keep everything up to ?/#.
    m = u.match(%r{\Ahttps?://[^/]+([^?#]*)})
    m ? m[1] : u
  end
end

#urlObject



562
563
564
565
# File 'lib/homura/runtime/durable_object.rb', line 562

def url
  js = @js_request
  `(#{js} ? String(#{js}.url || '') : '')`
end