Skip to content

Commit

Permalink
Refactor out base middleware from BaseViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
dalehamel committed Oct 23, 2024
1 parent 99d23ca commit ad67c69
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 144 deletions.
1 change: 1 addition & 0 deletions lib/app_profiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Storage
module Viewer
autoload :BaseViewer, "app_profiler/viewer/base_viewer"
autoload :SpeedscopeViewer, "app_profiler/viewer/speedscope"
autoload :BaseMiddleware, "app_profiler/viewer/middleware/base"
autoload :SpeedscopeRemoteViewer, "app_profiler/viewer/remote/speedscope"
autoload :FirefoxRemoteViewer, "app_profiler/viewer/remote/firefox"
end
Expand Down
142 changes: 0 additions & 142 deletions lib/app_profiler/viewer/base_viewer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

gem "rails-html-sanitizer", ">= 1.6.0"
require "rails-html-sanitizer"

module AppProfiler
module Viewer
class BaseViewer
Expand All @@ -11,145 +8,6 @@ def view(profile, params = {})
new(profile).view(**params)
end
end

def view(_params = {})
raise NotImplementedError
end

class BaseMiddleware
class Sanitizer < Rails::HTML::Sanitizer.best_supported_vendor.safe_list_sanitizer
self.allowed_tags = Set.new([
"strong",
"em",
"b",
"i",
"p",
"code",
"pre",
"tt",
"samp",
"kbd",
"var",
"sub",
"sup",
"dfn",
"cite",
"big",
"small",
"address",
"hr",
"br",
"div",
"span",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"ul",
"ol",
"li",
"dl",
"dt",
"dd",
"abbr",
"acronym",
"a",
"img",
"blockquote",
"del",
"ins",
"script",
])
end

private_constant(:Sanitizer)

class << self
def id(file)
file.basename.to_s
end
end

def initialize(app)
@app = app
end

def call(env)
request = Rack::Request.new(env)

return index(env) if %r(\A/app_profiler/?\z).match?(request.path_info)

@app.call(env)
end

protected

def id(file)
self.class.id(file)
end

def profile_files
AppProfiler.profile_root.glob("**/*.json")
end

def render(html)
[
200,
{ "Content-Type" => "text/html" },
[
+<<~HTML,
<!doctype html>
<html>
<head>
<title>App Profiler</title>
</head>
<body>
#{sanitizer.sanitize(html)}
</body>
</html>
HTML
],
]
end

def sanitizer
@sanitizer ||= Sanitizer.new
end

def viewer(_env, path)
raise NotImplementedError
end

def show(env, id)
raise NotImplementedError
end

def index(_env)
render(
(+"").tap do |content|
content << "<h1>Profiles</h1>"
profile_files.each do |file|
viewer = if file.to_s.end_with?(AppProfiler::VernierProfile::FILE_EXTENSION)
AppProfiler::Viewer::FirefoxRemoteViewer::NAME
else
AppProfiler::Viewer::SpeedscopeRemoteViewer::NAME
end
content << <<~HTML
<p>
<a href="/app_profiler/#{viewer}/viewer/#{id(file)}">
#{id(file)}
</a>
</p>
HTML
end
end,
)
end
end

private_constant(:BaseMiddleware)
end
end
end
141 changes: 141 additions & 0 deletions lib/app_profiler/viewer/middleware/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# frozen_string_literal: true

gem "rails-html-sanitizer", ">= 1.6.0"
require "rails-html-sanitizer"

module AppProfiler
module Viewer
class BaseMiddleware
class Sanitizer < Rails::HTML::Sanitizer.best_supported_vendor.safe_list_sanitizer
self.allowed_tags = Set.new([
"strong",
"em",
"b",
"i",
"p",
"code",
"pre",
"tt",
"samp",
"kbd",
"var",
"sub",
"sup",
"dfn",
"cite",
"big",
"small",
"address",
"hr",
"br",
"div",
"span",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"ul",
"ol",
"li",
"dl",
"dt",
"dd",
"abbr",
"acronym",
"a",
"img",
"blockquote",
"del",
"ins",
"script",
])
end

private_constant(:Sanitizer)

class << self
def id(file)
file.basename.to_s
end
end

def initialize(app)
@app = app
end

def call(env)
request = Rack::Request.new(env)

return index(env) if %r(\A/app_profiler/?\z).match?(request.path_info)

@app.call(env)
end

protected

def id(file)
self.class.id(file)
end

def profile_files
AppProfiler.profile_root.glob("**/*.json")
end

def render(html)
[
200,
{ "Content-Type" => "text/html" },
[
+<<~HTML,
<!doctype html>
<html>
<head>
<title>App Profiler</title>
</head>
<body>
#{sanitizer.sanitize(html)}
</body>
</html>
HTML
],
]
end

def sanitizer
@sanitizer ||= Sanitizer.new
end

def viewer(_env, path)
raise NotImplementedError
end

def show(env, id)
raise NotImplementedError
end

def index(_env)
render(
(+"").tap do |content|
content << "<h1>Profiles</h1>"
profile_files.each do |file|
viewer = if file.to_s.end_with?(AppProfiler::VernierProfile::FILE_EXTENSION)
AppProfiler::Viewer::FirefoxRemoteViewer::NAME
else
AppProfiler::Viewer::SpeedscopeRemoteViewer::NAME
end
content << <<~HTML
<p>
<a href="/app_profiler/#{viewer}/viewer/#{id(file)}">
#{id(file)}
</a>
</p>
HTML
end
end,
)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/app_profiler/viewer/middleware/firefox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
module AppProfiler
module Viewer
class FirefoxRemoteViewer < BaseViewer
class Middleware < BaseMiddleware
class Middleware < AppProfiler::Viewer::BaseMiddleware
include Yarn::WithFirefoxProfile

def initialize(app)
Expand Down
2 changes: 1 addition & 1 deletion lib/app_profiler/viewer/middleware/speedscope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
module AppProfiler
module Viewer
class SpeedscopeRemoteViewer < BaseViewer
class Middleware < BaseMiddleware
class Middleware < AppProfiler::Viewer::BaseMiddleware
include Yarn::WithSpeedscope

def initialize(app)
Expand Down

0 comments on commit ad67c69

Please sign in to comment.