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.
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 |
# File 'lib/sinatra_opal_patches.rb', line 569 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
518 519 520 521 522 |
# File 'lib/sinatra_opal_patches.rb', line 518 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.
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
# File 'lib/sinatra_opal_patches.rb', line 532 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
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/sinatra_opal_patches.rb', line 291 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
313 314 315 |
# File 'lib/sinatra_opal_patches.rb', line 313 def halt(*halt_response) raise HaltResponse.new(materialize_halt_payload(*halt_response)) end |
#redirect(uri_value, *args) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/sinatra_opal_patches.rb', line 279 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
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/sinatra_opal_patches.rb', line 259 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 |