Class: Sinatra::Base
- Inherits:
-
Object
- Object
- Sinatra::Base
- Defined in:
- lib/sinatra_opal_patches.rb,
lib/sinatra_opal_patches.rb
Overview
-
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
-
.force_encoding(data, encoding = default_encoding) ⇒ Object
———————————————————– 12.
- .new!(*args, &block) ⇒ Object
-
.setup_default_middleware(builder) ⇒ Object
———————————————————– 11.
Instance Method Summary collapse
- #content_type(type = nil, params = {}) ⇒ Object
- #halt(*halt_response) ⇒ Object
- #redirect(uri_value, *args) ⇒ Object
- #uri(addr = nil, absolute = true, add_script_name = true) ⇒ Object (also: #url, #to)
Class Method Details
.force_encoding(data, encoding = default_encoding) ⇒ Object
-
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! and guard raw JS null/undefined values before sending Ruby messages.
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 |
# File 'lib/sinatra_opal_patches.rb', line 671 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
620 621 622 623 624 |
# File 'lib/sinatra_opal_patches.rb', line 620 def new!(*args, &block) instance = allocate instance.send(:initialize, *args, &block) instance end |
.setup_default_middleware(builder) ⇒ Object
-
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.
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 |
# File 'lib/sinatra_opal_patches.rb', line 634 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 |
Instance Method Details
#content_type(type = nil, params = {}) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/sinatra_opal_patches.rb', line 345 def content_type(type = nil, params = {}) return response['content-type'] unless type default = params.delete :default mime_type = mime_type(type) || default raise format('Unknown media type: %p', type) if mime_type.nil? mime_type = mime_type.dup unless params.include?(:charset) || settings.add_charset.all? { |p| !(p === mime_type) } params[:charset] = params.delete('charset') || settings.default_encoding end params.delete(:charset) if mime_type.include?('charset') unless params.empty? mime_type += (mime_type.include?(';') ? ', ' : ';') mime_type += params.map do |key, val| val = val.inspect if val =~ /[";,]/ "#{key}=#{val}" end.join(', ') end response['content-type'] = mime_type end |
#halt(*halt_response) ⇒ Object
367 368 369 |
# File 'lib/sinatra_opal_patches.rb', line 367 def halt(*halt_response) raise HaltResponse.new(materialize_halt_payload(*halt_response)) end |
#redirect(uri_value, *args) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/sinatra_opal_patches.rb', line 333 def redirect(uri_value, *args) http_version = env['SERVER_PROTOCOL'] || env['HTTP_VERSION'] if (http_version == 'HTTP/1.1') && (env['REQUEST_METHOD'] != 'GET') status 303 else status 302 end response['Location'] = uri(uri_value.to_s, settings.absolute_redirects?, settings.prefixed_redirects?) raise HaltResponse.new(materialize_halt_payload(*args)) end |
#uri(addr = nil, absolute = true, add_script_name = true) ⇒ Object Also known as: url, to
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/sinatra_opal_patches.rb', line 313 def uri(addr = nil, absolute = true, add_script_name = true) return addr if addr.to_s =~ /\A[a-z][a-z0-9+.\-]*:/i host = '' if absolute host = host + "http#{'s' if request.secure?}://" host = host + if request.forwarded? || (request.port != (request.secure? ? 443 : 80)) request.host_with_port else request.host end end uri = [host] uri << request.script_name.to_s if add_script_name uri << (addr || request.path_info).to_s File.join(uri) end |