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,136 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
module Compiler
|
|
6
|
+
# Reads a .html.erb into nodes: HTML text, <%= %>, if/elsif/else/unless and each blocks.
|
|
7
|
+
# Any other <% %> is an error, with the file and the line.
|
|
8
|
+
class Template
|
|
9
|
+
Text = Data.define(:html, :line)
|
|
10
|
+
Output = Data.define(:source, :line, :block)
|
|
11
|
+
If = Data.define(:branches, :negate, :block, :line)
|
|
12
|
+
Each = Data.define(:collection, :vars, :with_index, :children, :line, :block)
|
|
13
|
+
|
|
14
|
+
ERB_TAG = /<%(=|#)?(.*?)%>/m
|
|
15
|
+
# An open tag right before an ERB tag: the ERB would land inside the tag.
|
|
16
|
+
# shortcut: a '>' inside a quoted attribute value hides the open tag, so ERB there is not caught; upgrade when a template hits it.
|
|
17
|
+
OPEN_TAG = /<[a-zA-Z][^<>]*\z/
|
|
18
|
+
IF = /\A(if|unless) (.+)\z/
|
|
19
|
+
ELSIF = /\Aelsif (.+)\z/
|
|
20
|
+
EACH = /\A(.+)\.(each|each_with_index) do \|(\w+)(?:, (\w+))?\|\z/
|
|
21
|
+
|
|
22
|
+
# kind is :root, :if or :each. An :if keeps its branches; an :each keeps its vars and children.
|
|
23
|
+
Frame = Struct.new(:kind, :line, :branches, :negate, :collection, :vars, :with_index, :children)
|
|
24
|
+
|
|
25
|
+
def self.parse(source, path:) = new(source, path:).parse
|
|
26
|
+
|
|
27
|
+
def self.path_for(klass)
|
|
28
|
+
Object.const_source_location(klass.name).first.sub(/\.rb\z/, ".html.erb")
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def initialize(source, path:)
|
|
32
|
+
@source = source
|
|
33
|
+
@path = path
|
|
34
|
+
@stack = [Frame.new(:root, 0, nil, false, nil, nil, false, [])]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def parse
|
|
38
|
+
pos = 0
|
|
39
|
+
@source.to_enum(:scan, ERB_TAG).map { Regexp.last_match }.each do |match|
|
|
40
|
+
before = @source[pos...match.begin(0)]
|
|
41
|
+
if OPEN_TAG.match?(before)
|
|
42
|
+
raise error(line_at(match.begin(0)), "ERB inside an HTML tag (use [attr.x] or [class.x])")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
text(before, pos)
|
|
46
|
+
erb(match)
|
|
47
|
+
pos = match.end(0)
|
|
48
|
+
end
|
|
49
|
+
text(@source[pos..], pos)
|
|
50
|
+
raise error(@stack.last.line, "block opened without end") if @stack.size > 1
|
|
51
|
+
|
|
52
|
+
@stack.first.children
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def text(chunk, offset)
|
|
58
|
+
add(Text.new(html: chunk, line: line_at(offset))) unless chunk.empty?
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def erb(match)
|
|
62
|
+
line = line_at(match.begin(0))
|
|
63
|
+
body = match[2].strip
|
|
64
|
+
case match[1]
|
|
65
|
+
when "#" then nil
|
|
66
|
+
when "=" then add(Output.new(source: body, line:, block: nil))
|
|
67
|
+
else statement(body, line)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def statement(body, line)
|
|
72
|
+
case body
|
|
73
|
+
when "end" then close(line)
|
|
74
|
+
when "else" then branch(nil, "else", line)
|
|
75
|
+
else keyword(body, line)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def keyword(body, line)
|
|
80
|
+
if (found = ELSIF.match(body)) then branch(found[1], "elsif", line)
|
|
81
|
+
elsif (found = IF.match(body)) then open_if(found[1] == "unless", found[2], line)
|
|
82
|
+
elsif (found = EACH.match(body)) then open_each(found, line)
|
|
83
|
+
else raise error(line, "<% #{body} %> is not supported (only if, elsif, else, unless, each and end)")
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def open_if(negate, condition, line)
|
|
88
|
+
@stack.push(Frame.new(:if, line, [[condition, []]], negate, nil, nil, false, nil))
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def open_each(found, line)
|
|
92
|
+
vars = [found[3], found[4]].compact.map(&:to_sym)
|
|
93
|
+
with_index = found[2] == "each_with_index"
|
|
94
|
+
raise error(line, "each_with_index takes two params, each takes one") unless with_index == (vars.size == 2)
|
|
95
|
+
|
|
96
|
+
@stack.push(Frame.new(:each, line, nil, false, found[1], vars, with_index, []))
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def branch(condition, keyword, line)
|
|
100
|
+
frame = @stack.last
|
|
101
|
+
raise error(line, "#{keyword} outside an if") unless frame.kind == :if
|
|
102
|
+
raise error(line, "#{keyword} after else") if frame.branches.last[0].nil?
|
|
103
|
+
raise error(line, "elsif in an unless") if frame.negate && condition && keyword == "elsif"
|
|
104
|
+
|
|
105
|
+
frame.branches << [condition, []]
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def close(line)
|
|
109
|
+
raise error(line, "end without a block") if @stack.size == 1
|
|
110
|
+
|
|
111
|
+
frame = @stack.pop
|
|
112
|
+
add(frame.kind == :if ? build_if(frame) : build_each(frame))
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def build_if(frame)
|
|
116
|
+
If.new(branches: frame.branches, negate: frame.negate, block: nil, line: frame.line)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def build_each(frame)
|
|
120
|
+
Each.new(collection: frame.collection, vars: frame.vars, with_index: frame.with_index,
|
|
121
|
+
children: frame.children, line: frame.line, block: nil)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def add(node)
|
|
125
|
+
frame = @stack.last
|
|
126
|
+
children = frame.kind == :if ? frame.branches.last[1] : frame.children
|
|
127
|
+
children << node
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def line_at(offset) = @source[0, offset].count("\n") + 1
|
|
131
|
+
|
|
132
|
+
def error(line, message) = Error.new("#{@path}:#{line}: #{message}")
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The scalar constants below are read when the file loads, so their libraries are required here, not by the app.
|
|
4
|
+
require "bigdecimal"
|
|
5
|
+
require "date"
|
|
6
|
+
|
|
7
|
+
module Boto
|
|
8
|
+
module Web
|
|
9
|
+
module Compiler
|
|
10
|
+
# Declared Boto types (String, [Schema], ...) as the compiler's internal types:
|
|
11
|
+
# symbols for scalars, [:list, T] and [:schema, Klass]. :any and nil (unknown) are never operated on.
|
|
12
|
+
module TypeMap
|
|
13
|
+
SCALARS = {
|
|
14
|
+
String => :string, Integer => :integer, Float => :float, BigDecimal => :decimal,
|
|
15
|
+
Boto::Boolean => :boolean, Date => :date, Time => :time
|
|
16
|
+
}.freeze
|
|
17
|
+
SCALAR_TYPES = [*SCALARS.values, :nil].freeze
|
|
18
|
+
NOT_OPERABLE = [nil, :any, :server].freeze
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
def of(declared)
|
|
23
|
+
return [:list, of(declared.first)] if declared.is_a?(Array)
|
|
24
|
+
return [:schema, declared] if schema_class?(declared)
|
|
25
|
+
|
|
26
|
+
SCALARS[declared]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def schema_class?(declared) = declared.is_a?(Class) && declared < Boto::Schema
|
|
30
|
+
|
|
31
|
+
def schema_type?(type) = type.is_a?(Array) && type.first == :schema
|
|
32
|
+
|
|
33
|
+
def list_type?(type) = type.is_a?(Array) && type.first == :list
|
|
34
|
+
|
|
35
|
+
def element(type) = list_type?(type) ? type[1] : nil
|
|
36
|
+
|
|
37
|
+
def scalar?(type) = SCALAR_TYPES.include?(type)
|
|
38
|
+
|
|
39
|
+
def operable?(type) = !NOT_OPERABLE.include?(type)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
module Compiler
|
|
6
|
+
# The names an expression can read in a component: states, inputs, computeds and block locals.
|
|
7
|
+
# Locals win over component names. Computed types are inferred once and shared by the scopes of a component.
|
|
8
|
+
# helpers are the names of the project's client helpers, which the expression calls in the browser.
|
|
9
|
+
class TypeScope
|
|
10
|
+
# Placeholder while a computed is inferred: a computed that reads itself stays on the server.
|
|
11
|
+
INFERRING = :server
|
|
12
|
+
|
|
13
|
+
def initialize(component_class, locals: {}, cache: {}, helpers: [])
|
|
14
|
+
@component = component_class
|
|
15
|
+
@locals = locals
|
|
16
|
+
@cache = cache
|
|
17
|
+
@helpers = helpers
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# component_class is nil for a client helper, which reads its params only.
|
|
21
|
+
def lookup(name)
|
|
22
|
+
return { kind: :local, type: @locals[name] } if @locals.key?(name)
|
|
23
|
+
|
|
24
|
+
component_entry(name) if @component
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def client_helper?(name) = @helpers.include?(name)
|
|
28
|
+
|
|
29
|
+
def with_local(name, type)
|
|
30
|
+
TypeScope.new(@component, locals: @locals.merge(name => type), cache: @cache, helpers: @helpers)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def component_entry(name)
|
|
36
|
+
definition = @component.definition
|
|
37
|
+
return { kind: :state, type: declared_type(definition.states[name]) } if definition.states.key?(name)
|
|
38
|
+
return { kind: :input, type: declared_type(definition.inputs[name]) } if definition.inputs.key?(name)
|
|
39
|
+
return param_entry(name) if @component.declared_params.key?(name)
|
|
40
|
+
return { kind: :computed, type: computed_type(name) } if definition.computeds.include?(name)
|
|
41
|
+
|
|
42
|
+
nil
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def declared_type(item) = TypeMap.of(item.declared_type)
|
|
46
|
+
|
|
47
|
+
# A view's param is read like an input, typed by the declaration the view keeps.
|
|
48
|
+
def param_entry(name) = { kind: :input, type: TypeMap.of(@component.declared_params.fetch(name)) }
|
|
49
|
+
|
|
50
|
+
def computed_type(name)
|
|
51
|
+
return @cache[name] if @cache.key?(name)
|
|
52
|
+
|
|
53
|
+
@cache[name] = INFERRING
|
|
54
|
+
@cache[name] = infer(name)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Computed bodies see the component, never the block locals of the place that reads them.
|
|
58
|
+
def infer(name)
|
|
59
|
+
node = ComputedBody.of(@component, name)
|
|
60
|
+
return INFERRING unless node
|
|
61
|
+
|
|
62
|
+
result = Expression.compile_node(node, TypeScope.new(@component, cache: @cache, helpers: @helpers))
|
|
63
|
+
result.equal?(Expression::SERVER) ? INFERRING : result.type
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# Runs one request of the protocol over the snapshots. It knows nothing of Rack, so the tests call it directly.
|
|
6
|
+
# Every class it instantiates comes from the registry, never from a name the browser sent.
|
|
7
|
+
class Dispatcher
|
|
8
|
+
class UnknownAction < Boto::Error::BadRequest; end
|
|
9
|
+
|
|
10
|
+
def initialize(snapshot:, renderer:, registry:)
|
|
11
|
+
@snapshot = snapshot
|
|
12
|
+
@renderer = renderer
|
|
13
|
+
@views = ViewLoader.new(snapshot:, registry:)
|
|
14
|
+
@children = ChildPath.new(renderer:, registry:)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def call(envelope)
|
|
18
|
+
view, component = load_targets(envelope)
|
|
19
|
+
instance = component || view
|
|
20
|
+
blocks = run(instance, envelope)
|
|
21
|
+
body = Protocol.result(
|
|
22
|
+
view: @snapshot.dump(view), component: component && @snapshot.dump(component),
|
|
23
|
+
states: states_of(instance), events: events_of(instance), blocks:
|
|
24
|
+
)
|
|
25
|
+
[200, body]
|
|
26
|
+
rescue *Failure::HANDLED => error
|
|
27
|
+
Failure.response(error)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def load_targets(envelope)
|
|
33
|
+
return [@views.call(envelope["view"], changed: envelope["changed"]), nil] if envelope["path"].empty?
|
|
34
|
+
|
|
35
|
+
view = @views.call(envelope["view"])
|
|
36
|
+
[view, load_component(view, envelope)]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def load_component(view, envelope)
|
|
40
|
+
klass = @children.class_at(view.class, envelope["path"])
|
|
41
|
+
@snapshot.load(envelope["component"], component: klass, changed: envelope["changed"], inputs: refresh_inputs(envelope))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Only a child's refresh carries inputs: they are coerced by the input types before the refresh runs.
|
|
45
|
+
def refresh_inputs(envelope)
|
|
46
|
+
envelope["kind"] == "refresh" ? envelope.fetch("inputs", {}) : {}
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def run(instance, envelope)
|
|
50
|
+
return render_block(instance, envelope["block"]) if envelope["kind"] == "block"
|
|
51
|
+
|
|
52
|
+
envelope["kind"] == "action" ? invoke(instance, envelope) : instance.refresh
|
|
53
|
+
changed_blocks(instance, envelope["changed"])
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def invoke(instance, envelope)
|
|
57
|
+
method_name = action_name(instance.class, envelope["action"])
|
|
58
|
+
instance.public_send(method_name, **action_arguments(instance.class, method_name, envelope["args"]))
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def action_name(klass, name)
|
|
62
|
+
klass.definition.actions.find { _1.to_s == name } or raise UnknownAction, "unknown action #{name}"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Only the args the action declares are read from the browser: any other key is ignored, and no key becomes a Symbol.
|
|
66
|
+
def action_arguments(klass, action, given)
|
|
67
|
+
picked = klass.definition.action_args(action).keys.each_with_object({}) do |name, args|
|
|
68
|
+
args[name] = given[name.to_s] if given.key?(name.to_s)
|
|
69
|
+
end
|
|
70
|
+
klass.coerce_args(action, picked)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def render_block(instance, id)
|
|
74
|
+
raise Protocol::BadRequest, "unknown block #{id}" unless block_ids(instance).include?(id)
|
|
75
|
+
|
|
76
|
+
{ id => @renderer.render_block(instance, id) }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def block_ids(instance) = @renderer.analysis(instance.class).blocks.values.map(&:id)
|
|
80
|
+
|
|
81
|
+
# Re-renders the server blocks whose deps cross a changed state.
|
|
82
|
+
def changed_blocks(instance, browser_changed)
|
|
83
|
+
names = dependency_names(instance, browser_changed)
|
|
84
|
+
return {} if names.empty?
|
|
85
|
+
|
|
86
|
+
@renderer.analysis(instance.class).blocks.values
|
|
87
|
+
.select { |block| block.deps.any? { names.include?(_1.to_s) } }
|
|
88
|
+
.to_h { |block| [block.id, @renderer.render_block(instance, block.id)] }
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# When anything changed, the computeds count too: a block that reads one lists only its name, not the states behind it.
|
|
92
|
+
def dependency_names(instance, browser_changed)
|
|
93
|
+
changed = instance.changed_states.map(&:to_s) | browser_changed.keys
|
|
94
|
+
return changed if changed.empty?
|
|
95
|
+
|
|
96
|
+
changed | instance.class.definition.computeds.map(&:to_s)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def states_of(instance)
|
|
100
|
+
definition = instance.class.definition
|
|
101
|
+
instance.changed_states.to_h do |name|
|
|
102
|
+
state = definition.states.fetch(name)
|
|
103
|
+
value = instance.public_send(name)
|
|
104
|
+
[name.to_s, value.nil? ? nil : state.type.dump(value)]
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def events_of(instance)
|
|
109
|
+
instance.events.map { { "name" => _1[:name].to_s, "data" => _1[:data] } }
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# The live stream of one view. It takes the view's declarations (as PageEndpoint does), so its :auth, authorize
|
|
6
|
+
# and middlewares run before the call; the call subscribes to the view's channels and answers the stream. Each
|
|
7
|
+
# message becomes an event named after the message, whose data is the channel only: the publish data never reaches
|
|
8
|
+
# the browser, which refreshes the view instead.
|
|
9
|
+
class EventsEndpoint < Boto::Endpoint
|
|
10
|
+
NO_CHANNELS_MESSAGE = "this view has no channels for these params"
|
|
11
|
+
NO_SCHEMAS = {}.freeze
|
|
12
|
+
CHANNEL_FRAME = ->(message) { { channel: message.channel } }.freeze
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
attr_accessor :view
|
|
16
|
+
|
|
17
|
+
def name = view ? view.name : super
|
|
18
|
+
|
|
19
|
+
# Declared events are the view's business: the stream forwards whatever the channels publish, by its frame.
|
|
20
|
+
def events? = true
|
|
21
|
+
|
|
22
|
+
def event_schemas = NO_SCHEMAS
|
|
23
|
+
|
|
24
|
+
# A subclass per view, built at boot; the core compiles it and runs it through the builder, with no route.
|
|
25
|
+
def for(view)
|
|
26
|
+
klass = Class.new(self)
|
|
27
|
+
klass.view = view
|
|
28
|
+
PageEndpoint::DECLARATIONS.each { |ivar| klass.instance_variable_set(ivar, view.instance_variable_get(ivar)) }
|
|
29
|
+
klass
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The channels come from the params alone: the mount of the view (its data) does not run when a stream opens.
|
|
34
|
+
def call(**args)
|
|
35
|
+
channels = self.class.view.instance_for(**args).channels
|
|
36
|
+
bad_request(NO_CHANNELS_MESSAGE) if channels.empty?
|
|
37
|
+
|
|
38
|
+
subscription = subscribe(*channels)
|
|
39
|
+
stream(subscription) { |events| events.forward(subscription, after: events.last_id, frame: CHANNEL_FRAME) }
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# The errors of a request as the status and the error envelope of the protocol.
|
|
6
|
+
# Anything outside HANDLED is a bug, and it is not rescued here.
|
|
7
|
+
module Failure
|
|
8
|
+
HANDLED = [Boto::Error, Snapshot::Error, Boto::Types::Invalid].freeze
|
|
9
|
+
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def response(error)
|
|
13
|
+
status, reason, errors = describe(error)
|
|
14
|
+
[status, Protocol.error(status, reason, errors)]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def describe(error)
|
|
18
|
+
case error
|
|
19
|
+
when Protocol::UnsupportedVersion then [400, "unsupported_version", []]
|
|
20
|
+
when Dispatcher::UnknownAction then [400, "unknown_action", []]
|
|
21
|
+
when Protocol::BadRequest then [400, "bad_request", []]
|
|
22
|
+
when Snapshot::Invalid, Snapshot::ReadonlyViolation then [400, "invalid_snapshot", []]
|
|
23
|
+
when Snapshot::Expired then [400, "expired", []]
|
|
24
|
+
when Snapshot::InvalidInput, Boto::Types::Invalid then [422, "invalid_input", error.errors]
|
|
25
|
+
else [error.status, "error", []]
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# What a layout's boto_head writes: the runtime's CSS, the project's CSS, the csrf meta when the project has
|
|
6
|
+
# :csrf, and the boot script. script_name is the mount prefix of the app, so the files load under a gateway.
|
|
7
|
+
module Head
|
|
8
|
+
RUNTIME_CSS = "boto-web.css"
|
|
9
|
+
BOOT_SCRIPT = "boot.js"
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def html(script_name:, csrf_token: nil)
|
|
14
|
+
base = "#{Html.escape(script_name)}/boto-web"
|
|
15
|
+
[csrf(csrf_token), stylesheet("#{base}/#{RUNTIME_CSS}"), stylesheet("#{base}/#{Stylesheet::FILE}"),
|
|
16
|
+
%(<script type="module" src="#{base}/#{BOOT_SCRIPT}"></script>)].join
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def csrf(token) = token ? %(<meta name="csrf-token" content="#{Html.escape(token)}">) : ""
|
|
20
|
+
|
|
21
|
+
def stylesheet(href) = %(<link rel="stylesheet" href="#{href}">)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bigdecimal"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Web
|
|
7
|
+
# Display helpers for templates. The browser's helpers.js mirrors these rules: keep them in step.
|
|
8
|
+
module Helpers
|
|
9
|
+
# The series helpers: a project's helper cannot reuse one of these names.
|
|
10
|
+
NAMES = %i[number currency date time pluralize truncate].freeze
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
# Rounds half up, groups thousands with "." and writes the decimals with ",".
|
|
15
|
+
def number(value, precision: 0)
|
|
16
|
+
text = BigDecimal(value.to_s).round(precision, half: :up).to_s("F")
|
|
17
|
+
sign = text.start_with?("-") ? "-" : ""
|
|
18
|
+
whole, fraction = text.delete("-").split(".")
|
|
19
|
+
grouped = whole.reverse.scan(/\d{1,3}/).join(".").reverse
|
|
20
|
+
return "#{sign}#{grouped}" if precision.zero?
|
|
21
|
+
|
|
22
|
+
"#{sign}#{grouped},#{fraction.to_s.ljust(precision, "0")}"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def currency(value) = "R$ #{number(value, precision: 2)}"
|
|
26
|
+
|
|
27
|
+
# The ISO day, as the snapshot writes a date (2026-12-05).
|
|
28
|
+
def date(value) = value.strftime("%Y-%m-%d")
|
|
29
|
+
|
|
30
|
+
# The hour and minute on the clock of the value, as the snapshot writes it (14:30).
|
|
31
|
+
def time(value) = value.strftime("%H:%M")
|
|
32
|
+
|
|
33
|
+
def pluralize(count, word) = "#{count} #{count == 1 ? word : "#{word}s"}"
|
|
34
|
+
|
|
35
|
+
def truncate(text, length: 30)
|
|
36
|
+
text.length > length ? "#{text[0, length - 3]}..." : text
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# The hook scripts of a project (src/assets/hooks/<name>.js), served under /boto-web/hooks/. The name allows only
|
|
6
|
+
# lowercase letters, digits, "_" and "-" before ".js": no dots or slashes, so nothing outside the folder is read.
|
|
7
|
+
class Hooks
|
|
8
|
+
PATTERN = /\A([a-z0-9_-]+)\.js\z/
|
|
9
|
+
CONTENT_TYPE = "text/javascript; charset=utf-8"
|
|
10
|
+
NOT_FOUND = [404, { "content-type" => "text/plain" }, ["not found"]].freeze
|
|
11
|
+
|
|
12
|
+
# reload: read the file on each request (development, so an edit shows on reload). Otherwise each hook is read
|
|
13
|
+
# once, at its first request, and kept (production).
|
|
14
|
+
def initialize(directory:, reload: false)
|
|
15
|
+
@directory = directory
|
|
16
|
+
@reload = reload
|
|
17
|
+
@served = {}
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def call(env)
|
|
21
|
+
match = PATTERN.match(env[Boto::App::PATH_PARAMS_KEY].fetch(:name))
|
|
22
|
+
path = match && File.join(@directory, match[0])
|
|
23
|
+
return NOT_FOUND unless path && File.file?(path)
|
|
24
|
+
|
|
25
|
+
[200, { "content-type" => CONTENT_TYPE }, [source(match[1], path)]]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def source(name, path)
|
|
31
|
+
return read(path) if @reload
|
|
32
|
+
|
|
33
|
+
@served[name] ||= read(path).freeze
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def read(path) = File.read(path, encoding: Encoding::UTF_8)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bigdecimal"
|
|
4
|
+
require "erb"
|
|
5
|
+
require "json"
|
|
6
|
+
|
|
7
|
+
module Boto
|
|
8
|
+
module Web
|
|
9
|
+
# The HTML the server writes. Each attribute helper returns the text with its leading space, or "" to omit it.
|
|
10
|
+
module Html
|
|
11
|
+
URL_ATTRIBUTES = %w[href src].freeze
|
|
12
|
+
LOCALS_ENTITIES = { "'" => "'", "&" => "&", "<" => "<", ">" => ">" }.freeze
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
# A decimal is written in fixed notation (12.5), as the browser's R.str writes it.
|
|
17
|
+
def escape(value) = ERB::Util.html_escape(text(value))
|
|
18
|
+
|
|
19
|
+
def text(value) = value.is_a?(BigDecimal) ? value.to_s("F") : value.to_s
|
|
20
|
+
|
|
21
|
+
def attrs(*parts) = parts.join
|
|
22
|
+
|
|
23
|
+
def bool_attr(name, value) = value ? " #{name}" : ""
|
|
24
|
+
|
|
25
|
+
def attr(name, value)
|
|
26
|
+
return "" if value.nil? || value == false
|
|
27
|
+
return "" if URL_ATTRIBUTES.include?(name) && unsafe_url?(value)
|
|
28
|
+
|
|
29
|
+
%( #{name}="#{escape(value)}")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# The pairs are [class name, on?]; the static text keeps its own classes.
|
|
33
|
+
def class_attr(static, pairs)
|
|
34
|
+
classes = [static, *pairs.filter_map { |name, on| name if on }].compact.flat_map { _1.to_s.split }
|
|
35
|
+
classes.empty? ? "" : %( class="#{escape(classes.join(" "))}")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The pairs are [property, value]; nil and false leave a declaration out.
|
|
39
|
+
def style_attr(static, pairs)
|
|
40
|
+
declarations = [static&.strip, *pairs.reject { |_, value| value.nil? || value == false }.map { "#{_1}: #{text(_2)};" }]
|
|
41
|
+
declarations = declarations.compact.reject(&:empty?).map { _1.end_with?(";") ? _1 : "#{_1};" }
|
|
42
|
+
declarations.empty? ? "" : %( style="#{escape(declarations.join(" "))}")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# The block locals of a handler, for the browser: JSON in a single-quoted attribute.
|
|
46
|
+
def locals_attr(locals)
|
|
47
|
+
return "" if locals.empty?
|
|
48
|
+
|
|
49
|
+
json = JSON.generate(plain(locals.transform_keys(&:to_s)))
|
|
50
|
+
%( data-locals='#{json.gsub(/['&<>]/, LOCALS_ENTITIES)}')
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def unsafe_url?(value) = value.to_s.gsub(/\s/, "").match?(/\Ajavascript:/i)
|
|
54
|
+
|
|
55
|
+
def plain(value)
|
|
56
|
+
case value
|
|
57
|
+
when BigDecimal then value.to_s("F")
|
|
58
|
+
when Array then value.map { plain(_1) }
|
|
59
|
+
when Hash then value.transform_values { plain(_1) }
|
|
60
|
+
else value.class.respond_to?(:serialize) ? plain(value.class.serialize(value)) : value
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
class Layouts
|
|
6
|
+
# Where a layout's code runs: the page's title, boto_head and the helpers the project's templates can call.
|
|
7
|
+
# Layouts builds a subclass per project with the helpers and the compiled layouts (see Layouts#context).
|
|
8
|
+
class Context
|
|
9
|
+
include Helpers
|
|
10
|
+
|
|
11
|
+
attr_reader :title
|
|
12
|
+
|
|
13
|
+
def initialize(title:, head:)
|
|
14
|
+
@title = title
|
|
15
|
+
@head = head
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def boto_head = @head
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "erb"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Web
|
|
7
|
+
class Layouts
|
|
8
|
+
# A layout's ERB as Ruby code. <%= %> writes its value escaped, as the component renderer does; <%== %> writes it
|
|
9
|
+
# raw, for the values that are HTML already (yield, boto_head).
|
|
10
|
+
class Template
|
|
11
|
+
OUTPUT = /<%=(?<raw>=?)(?<expr>.*?)%>/m
|
|
12
|
+
|
|
13
|
+
attr_reader :code
|
|
14
|
+
|
|
15
|
+
def self.compile(source) = new(ERB.new(escape_outputs(source)).src)
|
|
16
|
+
|
|
17
|
+
def self.escape_outputs(source)
|
|
18
|
+
source.gsub(OUTPUT) do
|
|
19
|
+
match = Regexp.last_match
|
|
20
|
+
match[:raw].empty? ? "<%= Boto::Web::Html.escape((#{match[:expr]})) %>" : "<%= #{match[:expr]} %>"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def initialize(code)
|
|
25
|
+
@code = code.freeze
|
|
26
|
+
freeze
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|