Class: Sinatra::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra_opal_patches.rb

Overview


  1. Base#static! (upstream base.rb:1147)

Upstream: URI_INSTANCE.unescape — URI_INSTANCE is defined in upstream as URI::RFC2396_PARSER (populated in opal_patches.rb with a CGI-backed stand-in), so this works today. The homura port also dropped the ‘static_headers` setting (not used on Workers — static files are served from R2 or a CDN, not from per-request Ruby response headers). Keeping parity here.


Class Method Summary collapse

Class Method Details

.force_encoding(data, encoding = default_encoding) ⇒ Object


  1. Base.force_encoding (upstream base.rb:1942)

Upstream calls ‘.force_encoding(encoding).encode!`. Opal’s String#encode! raises NotImplementedError (JS Strings are immutable UTF-16). force_encoding alone is a no-op that returns the same String object. Drop encode!.




368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/sinatra_opal_patches.rb', line 368

def force_encoding(data, encoding = default_encoding)
  return if data == settings || data.is_a?(Tempfile)

  present = `#{data} !== null && #{data} !== undefined && #{data} !== Opal.nil`

  if present && data.respond_to?(:force_encoding)
    data.force_encoding(encoding)
  elsif present && data.respond_to?(:each_value)
    data.each_value { |v| force_encoding(v, encoding) }
  elsif present && data.respond_to?(:each)
    data.each { |v| force_encoding(v, encoding) }
  end

  data
end

.new!(*args, &block) ⇒ Object



318
319
320
321
322
# File 'lib/sinatra_opal_patches.rb', line 318

def new!(*args, &block)
  instance = allocate
  instance.send(:initialize, *args, &block)
  instance
end

.setup_default_middleware(builder) ⇒ Object


  1. Base.setup_default_middleware (upstream base.rb:1846)

Upstream invokes ‘setup_host_authorization` which uses IPAddr. homura does not use host_authorization on Workers (the request host is always whitelisted by the Worker binding itself; permitted_hosts is moot). Skip that middleware.




332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/sinatra_opal_patches.rb', line 332

def setup_default_middleware(builder)
  builder.use ExtendedRack
  builder.use ShowExceptions       if show_exceptions?
  builder.use ::Rack::MethodOverride if method_override?
  builder.use ::Rack::Head
  setup_logging(builder)
  setup_sessions(builder)
  setup_protection(builder)
  # NOTE: upstream calls `setup_host_authorization builder` here.
  # On Cloudflare Workers the host whitelist is enforced by the
  # wrangler.toml binding configuration itself, and the
  # `Rack::Protection::HostAuthorization` middleware is not vendored
  # (would require IPAddr, also stubbed in homura). Skipping it
  # is functionally equivalent in our deployment model.
end