class WebConsole::Middleware

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/web_console/middleware.rbs

class WebConsole::Middleware
  def call: (Hash env) -> Array[Integer]
  def create_regular_or_whiny_request: (Hash env) -> WebConsole::WhinyRequest
end

def acceptable_content_type?(headers)

def acceptable_content_type?(headers)
  headers["Content-Type"].to_s.include?("html")
end

def binding_change_re

def binding_change_re
  @_binding_change_re ||= %r{#{repl_sessions_re}/trace\z}
end

def call(env)

Experimental RBS support (using type sampling data from the type_fusion project).

def call: (rack.version | Integer | rack.errors | IO | rack.multithread | TrueClass | rack.multiprocess | FalseClass | rack.run_once | FalseClass | rack.url_scheme | String | SCRIPT_NAME | String | QUERY_STRING | String | SERVER_SOFTWARE | String | GATEWAY_INTERFACE | String | REQUEST_METHOD | String | REQUEST_PATH | String | REQUEST_URI | String | SERVER_PROTOCOL | String | HTTP_HOST | String | HTTP_SEC_FETCH_SITE | String | HTTP_ACCEPT_ENCODING | String | HTTP_COOKIE | String | HTTP_CONNECTION | String | HTTP_SEC_FETCH_MODE | String | HTTP_ACCEPT | String | HTTP_USER_AGENT | String | HTTP_REFERER | String | HTTP_SEC_FETCH_DEST | String | HTTP_ACCEPT_LANGUAGE | String | puma.request_body_wait | Float | SERVER_NAME | String | SERVER_PORT | String | PATH_INFO | String | REMOTE_ADDR | String | HTTP_VERSION | String | puma.socket | TCPSocket | rack.hijack? | TrueClass | rack.hijack | Puma::Client | rack.input | Puma::NullIO | rack.after_reply |  | puma.config | Puma::Configuration | action_dispatch.parameter_filter | Symbol | Symbol | Symbol | Symbol | Symbol | Symbol | Symbol | Symbol | action_dispatch.redirect_filter |  | action_dispatch.secret_key_base | String | action_dispatch.show_exceptions | TrueClass | action_dispatch.show_detailed_exceptions | TrueClass | action_dispatch.log_rescued_responses | TrueClass | action_dispatch.logger | ActiveSupport::Logger | action_dispatch.backtrace_cleaner | Rails::BacktraceCleaner | action_dispatch.key_generator | ActiveSupport::CachingKeyGenerator | action_dispatch.http_auth_salt | String | action_dispatch.signed_cookie_salt | String | action_dispatch.encrypted_cookie_salt | String | action_dispatch.encrypted_signed_cookie_salt | String | action_dispatch.authenticated_encrypted_cookie_salt | String | action_dispatch.use_authenticated_cookie_encryption | TrueClass | action_dispatch.encrypted_cookie_cipher | NilClass | action_dispatch.signed_cookie_digest | NilClass | action_dispatch.cookies_serializer | Symbol | action_dispatch.cookies_digest | NilClass | action_dispatch.cookies_rotations | ActiveSupport::Messages::RotationConfiguration | action_dispatch.cookies_same_site_protection | Proc | action_dispatch.use_cookies_with_metadata | TrueClass | action_dispatch.content_security_policy | NilClass | action_dispatch.content_security_policy_report_only | FalseClass | action_dispatch.content_security_policy_nonce_generator | NilClass | action_dispatch.content_security_policy_nonce_directives | NilClass | action_dispatch.permissions_policy | NilClass | action_dispatch.routes | ActionDispatch::Routing::RouteSet | ROUTES_14080_SCRIPT_NAME | String | ORIGINAL_FULLPATH | String | ORIGINAL_SCRIPT_NAME | String | action_dispatch.authorized_host | String | action_dispatch.request_id | String | action_dispatch.remote_ip | ActionDispatch::RemoteIp::GetIp | rack.session | ActionDispatch::Request::Session | rack.session.options | ActionDispatch::Request::Session::Options | rack.tempfiles |  | action_dispatch.request.path_parameters | controller | String | action | String | action_controller.instance | SearchController | action_dispatch.request.content_type | NilClass | action_dispatch.request.request_parameters |  | rack.request.query_string | String | rack.request.query_hash | name | String | action_dispatch.request.query_parameters | name | String | action_dispatch.request.parameters | name | String | controller | String | action | String | action_dispatch.request.accepts | Mime::Type | action_dispatch.request.formats | Mime::Type | rack.request.cookie_hash | _gemsh_session | String | _skillit_session | String | token | String | rack.request.cookie_string | String | action_dispatch.cookies | ActionDispatch::Cookies::CookieJar | action_dispatch.request.unsigned_session_cookie | session_id | String | _csrf_token | String env) -> Hash | Content-Type | String | Content-Length | String | X-Web-Console-Session-Id | String | X-Web-Console-Mount-Point | String | Array | String

This signature was generated using 1 sample from 1 application.

def call(env)
  app_exception = catch :app_exception do
    request = create_regular_or_whiny_request(env)
    return call_app(env) unless request.permitted?
    if id = id_for_repl_session_update(request)
      return update_repl_session(id, request)
    elsif id = id_for_repl_session_stack_frame_change(request)
      return change_stack_trace(id, request)
    end
    status, headers, body = call_app(env)
    if (session = Session.from(Thread.current)) && acceptable_content_type?(headers)
      headers["X-Web-Console-Session-Id"] = session.id
      headers["X-Web-Console-Mount-Point"] = mount_point
      template = Template.new(env, session)
      body, headers = Injector.new(body, headers).inject(template.render("index"))
    end
    [ status, headers, body ]
  end
rescue => e
  WebConsole.logger.error("\n#{e.class}: #{e}\n\tfrom #{e.backtrace.join("\n\tfrom ")}")
  raise e
ensure
  # Clean up the fiber locals after the session creation. Object#console
  # uses those to communicate the current binding or exception to the middleware.
  Thread.current[:__web_console_exception] = nil
  Thread.current[:__web_console_binding] = nil
  raise app_exception if Exception === app_exception
end

def call_app(env)

def call_app(env)
  @app.call(env)
rescue => e
  throw :app_exception, e
end

def change_stack_trace(id, request)

def change_stack_trace(id, request)
  json_response_with_session(id, request) do |session|
    session.switch_binding_to(request.params[:frame_id], request.params[:exception_object_id])
    { ok: true }
  end
end

def create_regular_or_whiny_request(env)

Experimental RBS support (using type sampling data from the type_fusion project).

def create_regular_or_whiny_request: (rack.version | Integer | rack.errors | IO | rack.multithread | TrueClass | rack.multiprocess | FalseClass | rack.run_once | FalseClass | rack.url_scheme | String | SCRIPT_NAME | String | QUERY_STRING | String | SERVER_SOFTWARE | String | GATEWAY_INTERFACE | String | REQUEST_METHOD | String | REQUEST_PATH | String | REQUEST_URI | String | SERVER_PROTOCOL | String | HTTP_HOST | String | HTTP_USER_AGENT | String | CONTENT_TYPE | String | HTTP_ACCEPT | String | HTTP_ACCEPT_CHARSET | String | CONTENT_LENGTH | String | puma.request_body_wait | Float | SERVER_NAME | String | SERVER_PORT | String | PATH_INFO | String | REMOTE_ADDR | String | HTTP_VERSION | String | puma.socket | TCPSocket | rack.hijack? | TrueClass | rack.hijack | Puma::Client | rack.input | StringIO | rack.after_reply |  | puma.config | Puma::Configuration | action_dispatch.parameter_filter | Symbol | Symbol | Symbol | Symbol | Symbol | Symbol | Symbol | Symbol | action_dispatch.redirect_filter |  | action_dispatch.secret_key_base | String | action_dispatch.show_exceptions | TrueClass | action_dispatch.show_detailed_exceptions | TrueClass | action_dispatch.log_rescued_responses | TrueClass | action_dispatch.logger | ActiveSupport::Logger | action_dispatch.backtrace_cleaner | Rails::BacktraceCleaner | action_dispatch.key_generator | ActiveSupport::CachingKeyGenerator | action_dispatch.http_auth_salt | String | action_dispatch.signed_cookie_salt | String | action_dispatch.encrypted_cookie_salt | String | action_dispatch.encrypted_signed_cookie_salt | String | action_dispatch.authenticated_encrypted_cookie_salt | String | action_dispatch.use_authenticated_cookie_encryption | TrueClass | action_dispatch.encrypted_cookie_cipher | NilClass | action_dispatch.signed_cookie_digest | NilClass | action_dispatch.cookies_serializer | Symbol | action_dispatch.cookies_digest | NilClass | action_dispatch.cookies_rotations | ActiveSupport::Messages::RotationConfiguration | action_dispatch.cookies_same_site_protection | Proc | action_dispatch.use_cookies_with_metadata | TrueClass | action_dispatch.content_security_policy | NilClass | action_dispatch.content_security_policy_report_only | FalseClass | action_dispatch.content_security_policy_nonce_generator | NilClass | action_dispatch.content_security_policy_nonce_directives | NilClass | action_dispatch.permissions_policy | NilClass | action_dispatch.routes | ActionDispatch::Routing::RouteSet | ROUTES_13460_SCRIPT_NAME | String | ORIGINAL_FULLPATH | String | ORIGINAL_SCRIPT_NAME | String | action_dispatch.authorized_host | String | action_dispatch.request_id | String | action_dispatch.remote_ip | ActionDispatch::RemoteIp::GetIp env) -> WebConsole::WhinyRequest

This signature was generated using 1 sample from 1 application.

def create_regular_or_whiny_request(env)
  request = Request.new(env)
  whiny_requests ? WhinyRequest.new(request) : request
end

def id_for_repl_session_stack_frame_change(request)

def id_for_repl_session_stack_frame_change(request)
  if request.xhr? && request.post?
    binding_change_re.match(request.path) { |m| m[:id] }
  end
end

def id_for_repl_session_update(request)

def id_for_repl_session_update(request)
  if request.xhr? && request.put?
    update_re.match(request.path) { |m| m[:id] }
  end
end

def initialize(app)

def initialize(app)
  @app = app
end

def json_response(opts = {})

def json_response(opts = {})
  status  = opts.fetch(:status, 200)
  headers = { "Content-Type" => "application/json; charset = utf-8" }
  body    = yield.to_json
  [ status, headers, [ body ] ]
end

def json_response_with_session(id, request, opts = {})

def json_response_with_session(id, request, opts = {})
  return respond_with_unavailable_session(id) unless session = Session.find(id)
  json_response(opts) { yield session }
end

def repl_sessions_re

def repl_sessions_re
  @_repl_sessions_re ||= %r{#{mount_point}/repl_sessions/(?<id>[^/]+)}
end

def respond_with_unacceptable_request

def respond_with_unacceptable_request
  json_response(status: 406) do
    { output: I18n.t("errors.unacceptable_request") }
  end
end

def respond_with_unavailable_session(id)

def respond_with_unavailable_session(id)
  json_response(status: 404) do
    { output: format(I18n.t("errors.unavailable_session"), id: id) }
  end
end

def update_re

def update_re
  @_update_re ||= %r{#{repl_sessions_re}\z}
end

def update_repl_session(id, request)

def update_repl_session(id, request)
  json_response_with_session(id, request) do |session|
    if input = request.params[:input]
      { output: session.eval(input) }
    elsif input = request.params[:context]
      { context: session.context(input) }
    end
  end
end