boto-rb-web 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +17 -0
- data/assets/boto-web/boot.js +16 -0
- data/assets/boto-web/boto-web.css +32 -0
- data/assets/boto-web/dom/animate.js +49 -0
- data/assets/boto-web/dom/flip.js +41 -0
- data/assets/boto-web/dom/keyed.js +15 -0
- data/assets/boto-web/dom/morph.js +112 -0
- data/assets/boto-web/dom/navigation.js +72 -0
- data/assets/boto-web/dom/sync.js +42 -0
- data/assets/boto-web/net/base.js +5 -0
- data/assets/boto-web/net/protocol.js +46 -0
- data/assets/boto-web/net/sse.js +45 -0
- data/assets/boto-web/net/sse_stream.js +84 -0
- data/assets/boto-web/net/token.js +9 -0
- data/assets/boto-web/net/transport.js +99 -0
- data/assets/boto-web/runtime/component.js +138 -0
- data/assets/boto-web/runtime/events.js +54 -0
- data/assets/boto-web/runtime/hooks.js +82 -0
- data/assets/boto-web/runtime/live.js +12 -0
- data/assets/boto-web/runtime/on.js +36 -0
- data/assets/boto-web/runtime/runtime.js +100 -0
- data/assets/boto-web/runtime/scope.js +112 -0
- data/assets/boto-web/runtime/signals.js +123 -0
- data/assets/boto-web/template/decimal.js +93 -0
- data/assets/boto-web/template/helpers.js +31 -0
- data/assets/boto-web/template/html.js +61 -0
- data/assets/boto-web/template/hydrate.js +25 -0
- data/assets/boto-web/template/ruby/core.js +72 -0
- data/assets/boto-web/template/ruby/lists.js +63 -0
- data/assets/boto-web/template/ruby.js +5 -0
- data/lib/boto/component/arguments.rb +58 -0
- data/lib/boto/component/definition.rb +114 -0
- data/lib/boto/component.rb +107 -0
- data/lib/boto/helper.rb +14 -0
- data/lib/boto/view.rb +63 -0
- data/lib/boto/web/app_extension.rb +78 -0
- data/lib/boto/web/assets.rb +59 -0
- data/lib/boto/web/child_path.rb +38 -0
- data/lib/boto/web/code_class.rb +16 -0
- data/lib/boto/web/compiler/analysis/deps.rb +46 -0
- data/lib/boto/web/compiler/analysis/handlers.rb +47 -0
- data/lib/boto/web/compiler/analysis/walker.rb +174 -0
- data/lib/boto/web/compiler/analysis.rb +44 -0
- data/lib/boto/web/compiler/client_helper.rb +34 -0
- data/lib/boto/web/compiler/computed_body.rb +40 -0
- data/lib/boto/web/compiler/directives.rb +86 -0
- data/lib/boto/web/compiler/expression/calls.rb +149 -0
- data/lib/boto/web/compiler/expression/collections.rb +116 -0
- data/lib/boto/web/compiler/expression/literals.rb +75 -0
- data/lib/boto/web/compiler/expression/logic.rb +53 -0
- data/lib/boto/web/compiler/expression/operators.rb +85 -0
- data/lib/boto/web/compiler/expression/strings.rb +31 -0
- data/lib/boto/web/compiler/expression.rb +93 -0
- data/lib/boto/web/compiler/module_writer/handlers_js.rb +75 -0
- data/lib/boto/web/compiler/module_writer/js.rb +24 -0
- data/lib/boto/web/compiler/module_writer/tag_js.rb +132 -0
- data/lib/boto/web/compiler/module_writer/template_js.rb +89 -0
- data/lib/boto/web/compiler/module_writer/type_shape.rb +28 -0
- data/lib/boto/web/compiler/module_writer.rb +79 -0
- data/lib/boto/web/compiler/template.rb +136 -0
- data/lib/boto/web/compiler/type_map.rb +43 -0
- data/lib/boto/web/compiler/type_scope.rb +68 -0
- data/lib/boto/web/compiler.rb +9 -0
- data/lib/boto/web/dispatcher.rb +113 -0
- data/lib/boto/web/events_endpoint.rb +43 -0
- data/lib/boto/web/failure.rb +30 -0
- data/lib/boto/web/head.rb +24 -0
- data/lib/boto/web/helpers.rb +40 -0
- data/lib/boto/web/hooks.rb +39 -0
- data/lib/boto/web/html.rb +65 -0
- data/lib/boto/web/layouts/context.rb +22 -0
- data/lib/boto/web/layouts/template.rb +31 -0
- data/lib/boto/web/layouts.rb +69 -0
- data/lib/boto/web/modules.rb +37 -0
- data/lib/boto/web/page_endpoint.rb +65 -0
- data/lib/boto/web/project_helpers.rb +54 -0
- data/lib/boto/web/protocol.rb +85 -0
- data/lib/boto/web/registry.rb +86 -0
- data/lib/boto/web/registry_error.rb +8 -0
- data/lib/boto/web/renderer/erb_source.rb +199 -0
- data/lib/boto/web/renderer/render_context.rb +37 -0
- data/lib/boto/web/renderer.rb +94 -0
- data/lib/boto/web/runtime.rb +45 -0
- data/lib/boto/web/scoped_css.rb +113 -0
- data/lib/boto/web/secret_key.rb +8 -0
- data/lib/boto/web/settings.rb +27 -0
- data/lib/boto/web/snapshot/changes.rb +26 -0
- data/lib/boto/web/snapshot/inputs.rb +50 -0
- data/lib/boto/web/snapshot/token.rb +42 -0
- data/lib/boto/web/snapshot.rb +111 -0
- data/lib/boto/web/static_file.rb +35 -0
- data/lib/boto/web/stylesheet.rb +53 -0
- data/lib/boto/web/tailwind.rb +69 -0
- data/lib/boto/web/template_preview.rb +31 -0
- data/lib/boto/web/transports/http.rb +36 -0
- data/lib/boto/web/transports/sse.rb +45 -0
- data/lib/boto/web/transports.rb +21 -0
- data/lib/boto/web/version.rb +7 -0
- data/lib/boto/web/view_loader.rb +21 -0
- data/lib/boto/web.rb +17 -0
- metadata +172 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "erb"
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
6
|
+
module Boto
|
|
7
|
+
module Web
|
|
8
|
+
# Renders a component's HTML on the server, from the same template the browser runs.
|
|
9
|
+
# Each class is analyzed and compiled to ERB once; each block gets its own ERB, for the "block" request.
|
|
10
|
+
class Renderer
|
|
11
|
+
Compiled = Data.define(:result, :context)
|
|
12
|
+
|
|
13
|
+
attr_reader :helpers
|
|
14
|
+
|
|
15
|
+
# registry finds the child components; scope maps a class to its CSS scope attribute (nil: none);
|
|
16
|
+
# helpers are modules whose methods the templates can call, next to the series helpers;
|
|
17
|
+
# client_helper_names are the ones that also run in the browser, so the analysis splits blocks the same way.
|
|
18
|
+
def initialize(snapshot:, registry: Registry.new, scope: nil, helpers: [], client_helper_names: [])
|
|
19
|
+
@snapshot = snapshot
|
|
20
|
+
@registry = registry
|
|
21
|
+
@scope = scope
|
|
22
|
+
@helpers = helpers.freeze
|
|
23
|
+
@client_helper_names = client_helper_names.freeze
|
|
24
|
+
@compiled = {}
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def render(instance)
|
|
28
|
+
context_class(instance.class).new(instance, self).__render__
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def render_root(instance, tag:, path:, attrs: "")
|
|
32
|
+
snapshot = Html.escape(@snapshot.dump(instance))
|
|
33
|
+
%(<#{tag} data-component="#{instance.class.name}" data-path="#{Html.escape(path)}" data-snapshot="#{snapshot}"#{attrs}>) +
|
|
34
|
+
render(instance) + "</#{tag}>"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# The child's inputs ride in data-inputs, so the browser refreshes them after each morph of the parent.
|
|
38
|
+
def render_child(klass, inputs:, tag:, path:, attrs:)
|
|
39
|
+
instance = klass.build(**inputs)
|
|
40
|
+
render_root(instance, tag:, path:, attrs: attrs + Html.attr("data-inputs", inputs_json(instance)))
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def render_block(instance, id)
|
|
44
|
+
check_block!(instance.class, id)
|
|
45
|
+
context_class(instance.class).new(instance, self).public_send(block_method(id))
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def component_for(selector)
|
|
49
|
+
@registry.component_for(selector) or raise ArgumentError, "no component for #{selector}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# The analyzed tree and blocks of a class, cached: the dispatcher reads blocks and child tags from it.
|
|
53
|
+
def analysis(klass) = compiled(klass).result
|
|
54
|
+
|
|
55
|
+
# The class the templates of klass run in: built on the first use, then only instantiated.
|
|
56
|
+
def context_class(klass) = compiled(klass).context
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def compiled(klass)
|
|
61
|
+
@compiled[klass] ||= begin
|
|
62
|
+
result = Compiler::Analysis.new(klass, registry: @registry, scope: scope_for(klass),
|
|
63
|
+
helpers: @client_helper_names).call
|
|
64
|
+
Compiled.new(result:, context: build_context(klass, result))
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def build_context(klass, result)
|
|
69
|
+
RenderContext.subclass(klass, helpers: @helpers, file: klass.name.to_s, codes: template_codes(result))
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# The whole component is __render__; each server block has its own method, so a block renders alone.
|
|
73
|
+
def template_codes(result)
|
|
74
|
+
codes = { __render__: ERB.new(ErbSource.new(result).component).src }
|
|
75
|
+
result.blocks.values.each_with_object(codes) do |block, all|
|
|
76
|
+
all[block_method(block.id)] = ERB.new(ErbSource.block(result, block)).src
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def block_method(id) = :"__render_block_#{id}__"
|
|
81
|
+
|
|
82
|
+
def check_block!(klass, id)
|
|
83
|
+
return if compiled(klass).result.blocks.values.any? { _1.id == id }
|
|
84
|
+
|
|
85
|
+
raise ArgumentError, "unknown block #{id} in #{klass.name}"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def scope_for(klass) = @scope&.call(klass)
|
|
89
|
+
|
|
90
|
+
# The same values the snapshot's "i" holds, so the browser reads them back with the same types.
|
|
91
|
+
def inputs_json(instance) = JSON.generate(Snapshot::Inputs.dump(instance))
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# The objects one project shares across its views, built once at boot: the same registry, snapshot key and
|
|
6
|
+
# renderer for every request. In development the snapshot key is generated per process (see Snapshot.build).
|
|
7
|
+
class Runtime
|
|
8
|
+
attr_reader :registry, :views, :renderer, :modules, :dispatcher, :layouts, :project_name
|
|
9
|
+
|
|
10
|
+
# layouts_dir: the project's _layouts folder (nil when it has none); its layouts are checked against the views.
|
|
11
|
+
def self.build(namespace:, env:, environment:, options:, layouts_dir: nil, clock: -> { Time.now })
|
|
12
|
+
settings = Settings.new(**options)
|
|
13
|
+
helpers = project_helpers(namespace)
|
|
14
|
+
scope = ->(klass) { ScopedCss.attribute(klass) }
|
|
15
|
+
registry = Registry.from_namespace(namespace)
|
|
16
|
+
snapshot = Snapshot.build(env: env, environment: environment, ttl: settings.snapshot_ttl, clock: clock)
|
|
17
|
+
renderer = Renderer.new(snapshot: snapshot, registry: registry, scope: scope, helpers: helpers.modules,
|
|
18
|
+
client_helper_names: helpers.client_helper_names)
|
|
19
|
+
layouts = Layouts.load(dir: layouts_dir, helpers: renderer.helpers).tap { _1.check!(registry.views) }
|
|
20
|
+
new(registry: registry, views: ViewLoader.new(snapshot: snapshot, registry: registry), renderer: renderer,
|
|
21
|
+
modules: Modules.new(registry: registry, scope: scope, client_helpers: helpers.client_helpers),
|
|
22
|
+
dispatcher: Dispatcher.new(snapshot: snapshot, renderer: renderer, registry: registry),
|
|
23
|
+
layouts: layouts, project_name: namespace.name.freeze)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# The helper modules of Projeto::Helpers, when the project has them.
|
|
27
|
+
def self.project_helpers(namespace)
|
|
28
|
+
return ProjectHelpers.new([]) unless namespace.const_defined?(:Helpers, false)
|
|
29
|
+
|
|
30
|
+
ProjectHelpers.from(namespace.const_get(:Helpers, false))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def initialize(registry:, views:, renderer:, modules:, dispatcher:, layouts:, project_name:)
|
|
34
|
+
@registry = registry
|
|
35
|
+
@views = views
|
|
36
|
+
@renderer = renderer
|
|
37
|
+
@modules = modules
|
|
38
|
+
@dispatcher = dispatcher
|
|
39
|
+
@layouts = layouts
|
|
40
|
+
@project_name = project_name
|
|
41
|
+
freeze
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Web
|
|
7
|
+
# The .css next to a component's .rb: each selector gets [data-<scope_id>], so its rules reach only that component.
|
|
8
|
+
module ScopedCss
|
|
9
|
+
NESTED_AT_RULE = /\A@(media|supports|layer)\b/
|
|
10
|
+
COMMENTS = %r{\A(?:\s*/\*.*?\*/)*\s*}m
|
|
11
|
+
SELECTOR = /\A(\s*)(.*?)(\s*)\z/m
|
|
12
|
+
COMBINATORS = [" ", ">", "+", "~"].freeze
|
|
13
|
+
|
|
14
|
+
def self.scope_id(klass) = "s#{Digest::SHA256.hexdigest(klass.name)[0, 8]}"
|
|
15
|
+
|
|
16
|
+
def self.attribute(klass) = css_path(klass) ? "data-#{scope_id(klass)}" : nil
|
|
17
|
+
|
|
18
|
+
def self.css_path(klass)
|
|
19
|
+
path = Compiler::Template.path_for(klass).delete_suffix(".html.erb") + ".css"
|
|
20
|
+
File.exist?(path) ? path : nil
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.component_css(klass)
|
|
24
|
+
path = css_path(klass)
|
|
25
|
+
path && File.read(path, encoding: Encoding::UTF_8)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.rewrite(css, scope_id) = rewrite_rules(css, "data-#{scope_id}")
|
|
29
|
+
|
|
30
|
+
def self.rewrite_rules(css, attr)
|
|
31
|
+
out = +""
|
|
32
|
+
pos = 0
|
|
33
|
+
while (stop = css.index(/[;{]/, pos))
|
|
34
|
+
if css[stop] == ";"
|
|
35
|
+
out << css[pos..stop]
|
|
36
|
+
pos = stop + 1
|
|
37
|
+
else
|
|
38
|
+
close = closing_brace(css, stop)
|
|
39
|
+
out << rewrite_block(css[pos...stop], css[(stop + 1)...close], attr)
|
|
40
|
+
pos = close + 1
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
out << css[pos..].to_s
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Statements without a block (@import, @reference) and at-rules other than the nested ones stay as they are.
|
|
47
|
+
def self.rewrite_block(prelude, body, attr)
|
|
48
|
+
head = prelude.strip
|
|
49
|
+
return "#{prelude}{#{rewrite_rules(body, attr)}}" if NESTED_AT_RULE.match?(head)
|
|
50
|
+
return "#{prelude}{#{body}}" if head.start_with?("@")
|
|
51
|
+
|
|
52
|
+
"#{scope_prelude(prelude, attr)}{#{body}}"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def self.scope_prelude(prelude, attr)
|
|
56
|
+
comments = prelude[COMMENTS]
|
|
57
|
+
selectors = split_selectors(prelude[comments.size..])
|
|
58
|
+
comments + selectors.map { scope_selector(_1, attr) }.join(",")
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def self.split_selectors(prelude)
|
|
62
|
+
cuts = top_level(prelude).filter_map { |char, index| index if char == "," }
|
|
63
|
+
bounds = [-1, *cuts, prelude.length]
|
|
64
|
+
bounds.each_cons(2).map { |from, to| prelude[(from + 1)...to] }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def self.scope_selector(selector, attr)
|
|
68
|
+
lead, body, tail = selector.match(SELECTOR).captures
|
|
69
|
+
return selector if body.empty?
|
|
70
|
+
|
|
71
|
+
cut = insertion_point(body)
|
|
72
|
+
"#{lead}#{body[0...cut]}[#{attr}]#{body[cut..]}#{tail}"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Before the first pseudo of the last compound: a:hover becomes a[data-x]:hover, .a::before becomes .a[data-x]::before.
|
|
76
|
+
def self.insertion_point(selector)
|
|
77
|
+
colon = nil
|
|
78
|
+
top_level(selector).each do |char, index|
|
|
79
|
+
colon = nil if COMBINATORS.include?(char)
|
|
80
|
+
colon ||= index if char == ":"
|
|
81
|
+
end
|
|
82
|
+
colon || selector.length
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# The characters outside escapes and outside () and [], with their index.
|
|
86
|
+
def self.top_level(text)
|
|
87
|
+
depth = 0
|
|
88
|
+
escaped = false
|
|
89
|
+
text.each_char.with_index.filter_map do |char, index|
|
|
90
|
+
next escaped = false if escaped
|
|
91
|
+
next escaped = true if char == "\\"
|
|
92
|
+
|
|
93
|
+
depth += 1 if "([".include?(char)
|
|
94
|
+
depth -= 1 if ")]".include?(char)
|
|
95
|
+
[char, index] if depth.zero? && !")]".include?(char)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def self.closing_brace(css, open)
|
|
100
|
+
depth = 0
|
|
101
|
+
css[open..].each_char.with_index do |char, offset|
|
|
102
|
+
depth += 1 if char == "{"
|
|
103
|
+
depth -= 1 if char == "}"
|
|
104
|
+
return open + offset if depth.zero?
|
|
105
|
+
end
|
|
106
|
+
raise ArgumentError, "unbalanced braces in component css"
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
private_class_method :rewrite_rules, :rewrite_block, :scope_prelude, :split_selectors, :scope_selector,
|
|
110
|
+
:insertion_point, :top_level, :closing_brace
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# The validated app.web settings of one project; the core hands over the raw options.
|
|
6
|
+
class Settings
|
|
7
|
+
DEFAULT_SNAPSHOT_TTL = 12 * 60 * 60
|
|
8
|
+
SNAPSHOT_TTL_MESSAGE = "web: snapshot_ttl must be a positive Integer (seconds)"
|
|
9
|
+
# Reserved for the WebSocket transport (proposal 0050); HTTP with SSE is the only one today.
|
|
10
|
+
TRANSPORTS = %i[http].freeze
|
|
11
|
+
TRANSPORT_MESSAGE = "web: transport must be one of #{TRANSPORTS.map(&:inspect).join(", ")}".freeze
|
|
12
|
+
|
|
13
|
+
attr_reader :snapshot_ttl, :transport
|
|
14
|
+
|
|
15
|
+
def self.from(config) = new(**config.web)
|
|
16
|
+
|
|
17
|
+
def initialize(snapshot_ttl: DEFAULT_SNAPSHOT_TTL, transport: TRANSPORTS.first)
|
|
18
|
+
raise ArgumentError, SNAPSHOT_TTL_MESSAGE unless snapshot_ttl.is_a?(Integer) && snapshot_ttl.positive?
|
|
19
|
+
raise ArgumentError, TRANSPORT_MESSAGE unless TRANSPORTS.include?(transport)
|
|
20
|
+
|
|
21
|
+
@snapshot_ttl = snapshot_ttl
|
|
22
|
+
@transport = transport
|
|
23
|
+
freeze
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
class Snapshot
|
|
6
|
+
# The browser's changes go through the state writers, so they get the same coercion as the server's own writes.
|
|
7
|
+
module Changes
|
|
8
|
+
def self.apply(instance, changed)
|
|
9
|
+
errors = changed.flat_map { |name, value| apply_one(instance, name.to_s, value) }
|
|
10
|
+
raise InvalidInput.new(errors) if errors.any?
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.apply_one(instance, name, value)
|
|
14
|
+
state = instance.class.definition.states.each_value.find { |item| item.name.to_s == name }
|
|
15
|
+
return [{ path: name, message: "is not a state" }] unless state
|
|
16
|
+
raise ReadonlyViolation, "#{name} is readonly" if state.readonly
|
|
17
|
+
|
|
18
|
+
instance.public_send("#{name}=", value)
|
|
19
|
+
[]
|
|
20
|
+
rescue Boto::Types::Invalid => e
|
|
21
|
+
e.errors.map { |error| error.merge(path: Boto::Types.join_path(name, error[:path])) }
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
class Snapshot
|
|
6
|
+
# The inputs of a snapshot: a component's inputs, and a view's params (they are read the same way).
|
|
7
|
+
# A child's refresh sends its inputs again, coerced by the same types.
|
|
8
|
+
module Inputs
|
|
9
|
+
def self.items(klass)
|
|
10
|
+
inputs = klass.definition.inputs.values
|
|
11
|
+
klass.respond_to?(:params) ? inputs + klass.params.definitions : inputs
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.restore(klass, dumped)
|
|
15
|
+
items(klass).to_h { |item| [item.name, coerce_value(item, dumped.fetch(item.name.to_s))] }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# The values as the snapshot's "i" holds them: each one dumped by its type, nil when missing.
|
|
19
|
+
def self.dump(instance)
|
|
20
|
+
items(instance.class).to_h do |item|
|
|
21
|
+
value = instance.public_send(item.name)
|
|
22
|
+
[item.name.to_s, value.nil? ? nil : item.type.dump(value)]
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# A missing value stays nil, as the constructor leaves it.
|
|
27
|
+
def self.coerce_value(item, value) = value.nil? ? nil : item.type.coerce(value)
|
|
28
|
+
|
|
29
|
+
def self.coerce(klass, given)
|
|
30
|
+
errors = []
|
|
31
|
+
values = given.each_with_object({}) do |(name, value), coerced|
|
|
32
|
+
coerce_one(klass, name.to_s, value, coerced, errors)
|
|
33
|
+
end
|
|
34
|
+
raise InvalidInput.new(errors) if errors.any?
|
|
35
|
+
|
|
36
|
+
values
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.coerce_one(klass, name, value, coerced, errors)
|
|
40
|
+
item = items(klass).find { |candidate| candidate.name.to_s == name }
|
|
41
|
+
return errors << { path: name, message: "is not an input" } unless item
|
|
42
|
+
|
|
43
|
+
coerced[item.name] = coerce_value(item, value)
|
|
44
|
+
rescue Boto::Types::Invalid => e
|
|
45
|
+
errors.concat(e.errors.map { |error| error.merge(path: Boto::Types.join_path(name, error[:path])) })
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Web
|
|
7
|
+
class Snapshot
|
|
8
|
+
# Token: unpadded urlsafe base64 of the JSON, a dot, then base64 of its HMAC-SHA256.
|
|
9
|
+
module Token
|
|
10
|
+
SEPARATOR = "."
|
|
11
|
+
DIGEST = "SHA256"
|
|
12
|
+
|
|
13
|
+
def self.encode(json, signature) = "#{pack(json)}#{SEPARATOR}#{pack(signature)}"
|
|
14
|
+
|
|
15
|
+
def self.sign(json, key) = OpenSSL::HMAC.digest(DIGEST, key, json)
|
|
16
|
+
|
|
17
|
+
# Returns the JSON only when the signature matches; the signature is checked before anything is parsed.
|
|
18
|
+
def self.verify(token, key)
|
|
19
|
+
json_part, signature_part, extra = token.to_s.split(SEPARATOR, -1)
|
|
20
|
+
raise Invalid, "malformed token" if extra || signature_part.nil?
|
|
21
|
+
|
|
22
|
+
json = unpack(json_part)
|
|
23
|
+
signature = unpack(signature_part)
|
|
24
|
+
raise Invalid, "bad signature" unless Boto::Security::Secrets.equal?(signature, sign(json, key))
|
|
25
|
+
|
|
26
|
+
json.force_encoding(Encoding::UTF_8)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# pack("m0") is strict base64 with no line breaks; the urlsafe form drops the padding.
|
|
30
|
+
def self.pack(bytes) = [bytes].pack("m0").tr("+/", "-_").delete("=")
|
|
31
|
+
|
|
32
|
+
def self.unpack(text)
|
|
33
|
+
raise Invalid, "malformed token" unless text
|
|
34
|
+
|
|
35
|
+
text.tr("-_", "+/").concat("=" * ((4 - text.size % 4) % 4)).unpack1("m0")
|
|
36
|
+
rescue ArgumentError
|
|
37
|
+
raise Invalid, "malformed token"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Web
|
|
7
|
+
# A component's state as a signed token: the browser keeps it, and the server trusts only what the signature proves.
|
|
8
|
+
class Snapshot
|
|
9
|
+
class Error < StandardError; end
|
|
10
|
+
class Invalid < Error; end
|
|
11
|
+
class Expired < Error; end
|
|
12
|
+
class ReadonlyViolation < Error; end
|
|
13
|
+
|
|
14
|
+
class InvalidInput < Error
|
|
15
|
+
attr_reader :errors
|
|
16
|
+
|
|
17
|
+
def initialize(errors)
|
|
18
|
+
@errors = errors.freeze
|
|
19
|
+
super(errors.map { |error| "#{error[:path]}: #{error[:message]}" }.join("; "))
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# The snapshot of a project: its key comes from the env (or the development fallback), its lifetime from app.web.
|
|
24
|
+
def self.build(env:, environment:, ttl:, clock: -> { Time.now })
|
|
25
|
+
key = SecretKey.subkey(:snapshot, secret: SecretKey.secret(env:, environment:))
|
|
26
|
+
new(key:, ttl:, clock:)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def initialize(key:, ttl:, clock: -> { Time.now })
|
|
30
|
+
@key = key
|
|
31
|
+
@ttl = ttl
|
|
32
|
+
@clock = clock
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def dump(instance)
|
|
36
|
+
json = JSON.generate(
|
|
37
|
+
"c" => instance.class.name,
|
|
38
|
+
"i" => Inputs.dump(instance),
|
|
39
|
+
"s" => dump_values(instance, instance.class.definition.states.values),
|
|
40
|
+
"t" => @clock.call.to_i
|
|
41
|
+
)
|
|
42
|
+
Token.encode(json, Token.sign(json, @key))
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# component: refuses a snapshot of another class; inputs: a child's refresh, coerced by the input types.
|
|
46
|
+
def load(token, component: nil, changed: {}, inputs: {})
|
|
47
|
+
payload = parse(Token.verify(token, @key))
|
|
48
|
+
klass = component_class(payload["c"])
|
|
49
|
+
check_component!(klass, component)
|
|
50
|
+
check_expiry!(payload["t"])
|
|
51
|
+
instance = rebuild(klass, payload, inputs)
|
|
52
|
+
Changes.apply(instance, changed)
|
|
53
|
+
instance.clear_changes!
|
|
54
|
+
instance
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def dump_values(instance, items)
|
|
60
|
+
items.to_h do |item|
|
|
61
|
+
value = instance.public_send(item.name)
|
|
62
|
+
[item.name.to_s, value.nil? ? nil : item.type.dump(value)]
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def parse(json)
|
|
67
|
+
payload = JSON.parse(json)
|
|
68
|
+
raise Invalid, "payload is not an object" unless payload.is_a?(Hash)
|
|
69
|
+
|
|
70
|
+
payload
|
|
71
|
+
rescue JSON::ParserError
|
|
72
|
+
raise Invalid, "payload is not JSON"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def component_class(name)
|
|
76
|
+
klass = Object.const_get(name.to_s)
|
|
77
|
+
return klass if klass.is_a?(Class) && klass < Boto::Component
|
|
78
|
+
|
|
79
|
+
raise Invalid, "#{name} is not a component"
|
|
80
|
+
rescue NameError
|
|
81
|
+
raise Invalid, "#{name} is not a component"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def check_component!(klass, expected)
|
|
85
|
+
return if expected.nil? || klass == expected
|
|
86
|
+
|
|
87
|
+
raise Invalid, "snapshot is from another component"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def check_expiry!(issued_at)
|
|
91
|
+
raise Expired, "snapshot expired" if @clock.call.to_i - issued_at > @ttl
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Builds the instance without mount: the dumped values are the truth, not a fresh render.
|
|
95
|
+
def rebuild(klass, payload, inputs)
|
|
96
|
+
values = restored_inputs(klass, payload).merge(Inputs.coerce(klass, inputs))
|
|
97
|
+
klass.new(values, restored_states(klass, payload))
|
|
98
|
+
rescue KeyError, Boto::Types::Invalid => e
|
|
99
|
+
raise Invalid, e.message
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def restored_inputs(klass, payload) = Inputs.restore(klass, payload.fetch("i"))
|
|
103
|
+
|
|
104
|
+
def restored_states(klass, payload)
|
|
105
|
+
klass.definition.states.each_value.to_h do |state|
|
|
106
|
+
[state.name, payload.fetch("s").fetch(state.name.to_s)]
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# A file served as it is: the project's stylesheet, written by Stylesheet.build. Read at boot; with reload: (development)
|
|
6
|
+
# read again on each request whose file has a newer mtime, so a rewritten file shows without a restart.
|
|
7
|
+
class StaticFile
|
|
8
|
+
def initialize(path:, content_type:, reload: false, mtime: File.method(:mtime))
|
|
9
|
+
@path = path
|
|
10
|
+
@content_type = content_type
|
|
11
|
+
@reload = reload
|
|
12
|
+
@mtime = mtime
|
|
13
|
+
@stamp = mtime.call(path)
|
|
14
|
+
@body = read
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def call(_env)
|
|
18
|
+
refresh if @reload
|
|
19
|
+
[200, { "content-type" => @content_type }, [@body]]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def refresh
|
|
25
|
+
stamp = @mtime.call(@path)
|
|
26
|
+
return if stamp == @stamp
|
|
27
|
+
|
|
28
|
+
@stamp = stamp
|
|
29
|
+
@body = read
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def read = File.read(@path, encoding: Encoding::UTF_8).freeze
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Web
|
|
7
|
+
# The CSS of a project in one file: application.css first, then the .css of each view and component, scoped.
|
|
8
|
+
module Stylesheet
|
|
9
|
+
Error = Class.new(StandardError)
|
|
10
|
+
|
|
11
|
+
FILE = "app.css"
|
|
12
|
+
# Only Tailwind can read these: @apply, or the @import that loads it.
|
|
13
|
+
TAILWIND_ONLY = /@apply\b|@import\s+["']tailwindcss["']/
|
|
14
|
+
|
|
15
|
+
# Writes the file in out_dir and returns its path. app_css: the project's file, or nil for none (the file is
|
|
16
|
+
# then only the views' CSS, or empty). tailwind: is a Boto::Web::Tailwind, or nil to skip it.
|
|
17
|
+
def self.build(app_css:, registry:, out_dir:, tailwind: nil)
|
|
18
|
+
blocks = [*(compile(app_css, tailwind) if app_css), *scoped_blocks(registry, tailwind)]
|
|
19
|
+
FileUtils.mkdir_p(out_dir)
|
|
20
|
+
path = File.join(out_dir, FILE)
|
|
21
|
+
File.write(path, blocks.join("\n"))
|
|
22
|
+
path
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.scoped_blocks(registry, tailwind)
|
|
26
|
+
(registry.components + registry.views).sort_by(&:name).filter_map { scoped(_1, tailwind) }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# The .css next to the class's .rb, with each selector scoped to the class.
|
|
30
|
+
def self.scoped(klass, tailwind)
|
|
31
|
+
path = ScopedCss.css_path(klass)
|
|
32
|
+
return nil unless path
|
|
33
|
+
|
|
34
|
+
ScopedCss.rewrite(compile(path, tailwind), ScopedCss.scope_id(klass))
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.compile(path, tailwind)
|
|
38
|
+
return tailwind.compile(path) if tailwind
|
|
39
|
+
|
|
40
|
+
css = File.read(path, encoding: Encoding::UTF_8)
|
|
41
|
+
raise Error, missing_tailwind(path) if TAILWIND_ONLY.match?(css)
|
|
42
|
+
|
|
43
|
+
css
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.missing_tailwind(path)
|
|
47
|
+
"#{path} needs Tailwind: add gem \"tailwindcss-ruby\" to the project's Gemfile and run `boto bundle`"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private_class_method :scoped_blocks, :scoped, :compile, :missing_tailwind
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "open3"
|
|
4
|
+
require "tmpdir"
|
|
5
|
+
|
|
6
|
+
module Boto
|
|
7
|
+
module Web
|
|
8
|
+
# The Tailwind 4 binary of the project's tailwindcss-ruby gem, run on one file. The process goes through
|
|
9
|
+
# runner:, so the tests run without the binary.
|
|
10
|
+
class Tailwind
|
|
11
|
+
Error = Class.new(StandardError)
|
|
12
|
+
|
|
13
|
+
GEM_NAME = "tailwindcss-ruby"
|
|
14
|
+
MINIFY_FLAG = "--minify"
|
|
15
|
+
|
|
16
|
+
# Runs the command in chdir; returns its stderr and whether it succeeded.
|
|
17
|
+
class ProcessRunner
|
|
18
|
+
def call(args, chdir:)
|
|
19
|
+
_stdout, stderr, status = Open3.capture3(*args, chdir:)
|
|
20
|
+
[stderr, status.success?]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# nil when the project does not bring tailwindcss-ruby: the CSS then goes without Tailwind.
|
|
25
|
+
def self.for_project(spec: Gem::Specification, runner: ProcessRunner.new, executable: method(:gem_executable),
|
|
26
|
+
minify: false)
|
|
27
|
+
return nil unless spec.find_all_by_name(GEM_NAME).any?
|
|
28
|
+
|
|
29
|
+
new(executable: executable.call, runner:, minify:)
|
|
30
|
+
rescue LoadError
|
|
31
|
+
nil
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.gem_executable
|
|
35
|
+
require "tailwindcss/ruby"
|
|
36
|
+
Tailwindcss::Ruby.executable
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
attr_reader :executable, :runner, :minify
|
|
40
|
+
|
|
41
|
+
# minify: true in production, where the CSS ships as it is.
|
|
42
|
+
def initialize(executable:, runner: ProcessRunner.new, minify: false)
|
|
43
|
+
@executable = executable
|
|
44
|
+
@runner = runner
|
|
45
|
+
@minify = minify
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The compiled CSS of one file. The binary runs in the file's folder, so its relative imports resolve.
|
|
49
|
+
def compile(input)
|
|
50
|
+
Dir.mktmpdir do |dir|
|
|
51
|
+
output = File.join(dir, "compiled.css")
|
|
52
|
+
run(input, output)
|
|
53
|
+
File.read(output, encoding: Encoding::UTF_8)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def run(input, output)
|
|
60
|
+
stderr, success = runner.call(command(input, output), chdir: File.dirname(input))
|
|
61
|
+
raise Error, "tailwind failed on #{input}: #{stderr}" unless success
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def command(input, output)
|
|
65
|
+
[executable, "-i", input, "-o", output, *(MINIFY_FLAG if minify)]
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|