live_cable 0.3.0 → 0.5.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 +4 -4
- data/CHANGELOG.md +739 -0
- data/README.md +101 -56
- data/app/assets/javascript/controllers/live_controller.js +61 -7
- data/app/assets/javascript/live_cable.js +2 -0
- data/app/assets/javascript/loading.js +61 -9
- data/app/assets/javascript/subscriptions.js +389 -42
- data/app/channels/live_channel.rb +51 -29
- data/app/helpers/live_cable_helper.rb +45 -2
- data/lib/live_cable/action_cable_connection.rb +4 -1
- data/lib/live_cable/arel_predications.rb +15 -0
- data/lib/live_cable/component/broadcasting.rb +47 -8
- data/lib/live_cable/component/dispatching.rb +37 -0
- data/lib/live_cable/component/events.rb +2 -1
- data/lib/live_cable/component/lifecycle.rb +1 -1
- data/lib/live_cable/component/reactive_variables.rb +33 -5
- data/lib/live_cable/component/rendering.rb +54 -17
- data/lib/live_cable/component/streaming.rb +35 -4
- data/lib/live_cable/component.rb +4 -3
- data/lib/live_cable/connection/broadcasting.rb +56 -12
- data/lib/live_cable/connection/component_management.rb +12 -8
- data/lib/live_cable/connection/error_handling.rb +28 -7
- data/lib/live_cable/connection/messaging.rb +54 -51
- data/lib/live_cable/connection/state_management.rb +13 -16
- data/lib/live_cable/connection.rb +33 -0
- data/lib/live_cable/container.rb +17 -11
- data/lib/live_cable/defaults_signer.rb +155 -0
- data/lib/live_cable/delegation/array.rb +27 -1
- data/lib/live_cable/delegation/hash.rb +60 -1
- data/lib/live_cable/delegation/methods.rb +25 -1
- data/lib/live_cable/delegation/model.rb +17 -0
- data/lib/live_cable/delegator.rb +68 -12
- data/lib/live_cable/dispatch.rb +54 -0
- data/lib/live_cable/engine.rb +8 -1
- data/lib/live_cable/forbidden.rb +7 -0
- data/lib/live_cable/identity_digest.rb +27 -0
- data/lib/live_cable/model_observer.rb +82 -0
- data/lib/live_cable/observer_tracking.rb +20 -2
- data/lib/live_cable/predicate_builder.rb +31 -0
- data/lib/live_cable/record_ids.rb +37 -0
- data/lib/live_cable/render_context.rb +134 -8
- data/lib/live_cable/rendering/controller_renderer.rb +30 -0
- data/lib/live_cable/rendering/dependency_visitor.rb +101 -24
- data/lib/live_cable/rendering/handler.rb +1 -1
- data/lib/live_cable/rendering/method_analyzer.rb +18 -4
- data/lib/live_cable/rendering/method_collector.rb +13 -5
- data/lib/live_cable/rendering/partial.rb +28 -12
- data/lib/live_cable/rendering/partial_renderer.rb +55 -38
- data/lib/live_cable/rendering/render_result.rb +5 -0
- data/lib/live_cable/rendering/renderer.rb +35 -15
- data/lib/live_cable/rendering/view_buffer.rb +22 -0
- data/lib/live_cable/testing/render_state.rb +6 -4
- data/lib/live_cable/testing/test_channel.rb +6 -0
- data/lib/live_cable/testing/test_component.rb +5 -5
- data/lib/live_cable/testing.rb +13 -7
- data/lib/live_cable/version.rb +1 -1
- data/lib/live_cable.rb +32 -3
- metadata +19 -9
|
@@ -8,37 +8,81 @@ module LiveCable
|
|
|
8
8
|
# @return [Array<LiveCable::Component>] Components that were broadcast to
|
|
9
9
|
# (rendered or errored), including children rendered by their parents
|
|
10
10
|
def broadcast_changeset
|
|
11
|
-
|
|
11
|
+
synchronize { broadcast_changeset_unsynchronized }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def broadcast_changeset_unsynchronized
|
|
17
|
+
rendered = Set.new.compare_by_identity
|
|
12
18
|
shared_changeset = containers[SHARED_CONTAINER]&.changeset
|
|
19
|
+
changed = lambda do |component|
|
|
20
|
+
containers[component.live_id]&.changed? || component.shared_reactive_variables.intersect?(shared_changeset)
|
|
21
|
+
end
|
|
13
22
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
23
|
+
list = components.values
|
|
24
|
+
list = list.any?(&changed) ? parents_first(list) : []
|
|
25
|
+
|
|
26
|
+
list.each do |component|
|
|
17
27
|
# Component may have already been re-rendered by a parent, so don't render it again
|
|
18
28
|
next if rendered.include?(component)
|
|
29
|
+
next unless changed.call(component)
|
|
19
30
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
next unless container&.changed? || component.shared_reactive_variables.intersect?(shared_changeset)
|
|
31
|
+
# Still last cycle's: a render that a callback halts doesn't replace it
|
|
32
|
+
component.rendered_children.clear
|
|
23
33
|
|
|
24
34
|
begin
|
|
25
35
|
component.broadcast_render
|
|
26
36
|
rescue StandardError => error
|
|
27
37
|
handle_error(component, error)
|
|
38
|
+
add_tree(component, :owned_children, rendered)
|
|
28
39
|
end
|
|
29
40
|
|
|
30
|
-
|
|
41
|
+
add_tree(component, :rendered_children, rendered)
|
|
31
42
|
end
|
|
32
43
|
|
|
33
44
|
# Deliver events from components that didn't broadcast a render this
|
|
34
45
|
# cycle (no state change, or rendered inline by a parent) - rendered
|
|
35
|
-
# components already flushed their events with the refresh
|
|
46
|
+
# components already flushed their events with the refresh, and the
|
|
47
|
+
# component awaiting its reply sends them with its _ack.
|
|
48
|
+
#
|
|
49
|
+
# A component rendered inline by a parent has no channel of its own
|
|
50
|
+
# yet, so it can't deliver anything. Leave its events queued (don't
|
|
51
|
+
# flush) so they're delivered when its own subscription connects,
|
|
52
|
+
# rather than silently dropped here.
|
|
36
53
|
components.each_value do |component|
|
|
37
|
-
|
|
38
|
-
|
|
54
|
+
next unless component.subscribed?
|
|
55
|
+
next if awaiting_reply?(component)
|
|
56
|
+
|
|
57
|
+
component.broadcast_events
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
rendered.to_a
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Parents come before the children they own, so a child its parent
|
|
64
|
+
# renders inline is already handled when the loop reaches it.
|
|
65
|
+
def parents_first(list)
|
|
66
|
+
children = list.each_with_object({}.compare_by_identity) do |component, by_parent|
|
|
67
|
+
by_parent[component] = component.owned_children
|
|
68
|
+
end
|
|
69
|
+
owned = Set.new.compare_by_identity.merge(children.values.flatten)
|
|
70
|
+
ordered = {}.compare_by_identity
|
|
71
|
+
visit = lambda do |component|
|
|
72
|
+
next if ordered.key?(component) || !children.key?(component)
|
|
73
|
+
|
|
74
|
+
ordered[component] = true
|
|
75
|
+
children[component].each(&visit)
|
|
39
76
|
end
|
|
77
|
+
list.each { |component| visit.call(component) unless owned.include?(component) }
|
|
78
|
+
|
|
79
|
+
ordered.keys.concat(list.reject { |component| ordered.key?(component) })
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def add_tree(component, children, seen)
|
|
83
|
+
return unless seen.add?(component)
|
|
40
84
|
|
|
41
|
-
|
|
85
|
+
component.public_send(children).each { |child| add_tree(child, children, seen) }
|
|
42
86
|
end
|
|
43
87
|
end
|
|
44
88
|
end
|
|
@@ -10,19 +10,23 @@ module LiveCable
|
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
def add_component(component)
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
synchronize do
|
|
14
|
+
component.live_connection = self
|
|
15
|
+
components[component.live_id] = component
|
|
16
|
+
end
|
|
15
17
|
end
|
|
16
18
|
|
|
17
19
|
def remove_component(component)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
synchronize do
|
|
21
|
+
# Clean up the container to break observer reference chains
|
|
22
|
+
container = containers.delete(component.live_id)
|
|
23
|
+
container&.cleanup
|
|
21
24
|
|
|
22
|
-
|
|
25
|
+
components.delete(component.live_id)
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
# Clean up shared container if no components remain
|
|
28
|
+
cleanup_shared_container
|
|
29
|
+
end
|
|
26
30
|
end
|
|
27
31
|
|
|
28
32
|
private
|
|
@@ -5,6 +5,24 @@ module LiveCable
|
|
|
5
5
|
module ErrorHandling
|
|
6
6
|
extend ActiveSupport::Concern
|
|
7
7
|
|
|
8
|
+
# Offer an error raised by the component's own code - an action, a
|
|
9
|
+
# reactive write, a stream callback - to its rescue_from handlers, and
|
|
10
|
+
# fall back to #handle_error when none matches.
|
|
11
|
+
#
|
|
12
|
+
# A handled error is a processed message, not a failed one: no _error
|
|
13
|
+
# goes out, so the caller must still answer with a render or an _ack or
|
|
14
|
+
# the client's loading state never clears.
|
|
15
|
+
#
|
|
16
|
+
# @param component [LiveCable::Component]
|
|
17
|
+
# @param error [Exception]
|
|
18
|
+
# @return [Boolean] true when a handler took the error
|
|
19
|
+
def rescue_error(component, error)
|
|
20
|
+
return true if component.rescue_with_handler(error)
|
|
21
|
+
|
|
22
|
+
handle_error(component, error)
|
|
23
|
+
false
|
|
24
|
+
end
|
|
25
|
+
|
|
8
26
|
# Report an error and replace the component on the client with an
|
|
9
27
|
# error box.
|
|
10
28
|
#
|
|
@@ -16,16 +34,19 @@ module LiveCable
|
|
|
16
34
|
# through; needed when the component failed before it connected to
|
|
17
35
|
# one, or when there is no component at all
|
|
18
36
|
def handle_error(component, error, channel: nil)
|
|
19
|
-
|
|
37
|
+
synchronize do
|
|
38
|
+
take_reply(component)
|
|
39
|
+
Rails.error.report(error)
|
|
20
40
|
|
|
21
|
-
|
|
41
|
+
html = error_html(component, error)
|
|
22
42
|
|
|
23
|
-
|
|
24
|
-
|
|
43
|
+
# Destroy children first so their _status:destroy messages arrive before _error
|
|
44
|
+
component&.owned_children&.each(&:destroy)
|
|
25
45
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
46
|
+
# Broadcast the error - JS replaces the DOM and calls unsubscribe(),
|
|
47
|
+
# which triggers LiveChannel#unsubscribed -> component.disconnect for server cleanup
|
|
48
|
+
(channel || component)&.broadcast(_error: html)
|
|
49
|
+
end
|
|
29
50
|
end
|
|
30
51
|
|
|
31
52
|
private
|
|
@@ -6,79 +6,82 @@ module LiveCable
|
|
|
6
6
|
extend ActiveSupport::Concern
|
|
7
7
|
|
|
8
8
|
def receive(component, data)
|
|
9
|
-
|
|
9
|
+
synchronize do
|
|
10
|
+
reset_changeset
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
return unless data['messages'].present?
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
errored = false
|
|
16
|
-
data['messages'].each do |message|
|
|
17
|
-
errored = true unless action(component, message)
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
rendered = broadcast_changeset
|
|
21
|
-
|
|
22
|
-
# Guarantee exactly one response per message batch so the client can
|
|
23
|
-
# clear its loading state even when nothing changed
|
|
24
|
-
component.broadcast_ack unless errored || rendered.include?(component)
|
|
25
|
-
end
|
|
14
|
+
@reply_to = component
|
|
15
|
+
@carried = Set.new
|
|
26
16
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
params = parse_params(data)
|
|
17
|
+
data['messages'].each do |message|
|
|
18
|
+
action(component, message)
|
|
19
|
+
end
|
|
31
20
|
|
|
32
|
-
|
|
33
|
-
action = data['_action']&.to_sym
|
|
21
|
+
broadcast_changeset
|
|
34
22
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
23
|
+
# Guarantee exactly one reply per message batch so the client can
|
|
24
|
+
# clear its loading state: the component's own render, an _error,
|
|
25
|
+
# or this
|
|
26
|
+
component.broadcast_ack(rendered: @carried.include?(component.live_id)) if take_reply(component)
|
|
27
|
+
ensure
|
|
28
|
+
@reply_to = nil
|
|
29
|
+
@carried = nil
|
|
30
|
+
end
|
|
31
|
+
end
|
|
38
32
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
33
|
+
# Notes the children a frame sent during a message batch rendered inline
|
|
34
|
+
#
|
|
35
|
+
# @param children [Hash{String => Hash}, nil] the frame's c
|
|
36
|
+
def carried(children)
|
|
37
|
+
@carried&.merge(children.keys) if children
|
|
38
|
+
end
|
|
42
39
|
|
|
43
|
-
|
|
40
|
+
# @return [Boolean] whether the component sent the message batch being
|
|
41
|
+
# processed and no frame has answered it yet
|
|
42
|
+
def awaiting_reply?(component)
|
|
43
|
+
@reply_to.equal?(component)
|
|
44
|
+
end
|
|
44
45
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
end
|
|
50
|
-
end
|
|
46
|
+
# @return [Boolean] true, once, for the frame that answers the message
|
|
47
|
+
# batch the component sent; never while one of its messages still runs
|
|
48
|
+
def take_reply(component)
|
|
49
|
+
return false unless component && @reply_to.equal?(component) && !component.current_dispatch
|
|
51
50
|
|
|
51
|
+
@reply_to = nil
|
|
52
52
|
true
|
|
53
|
-
rescue StandardError => e
|
|
54
|
-
handle_error(component, e)
|
|
55
|
-
false
|
|
56
53
|
end
|
|
57
54
|
|
|
58
|
-
# @return [Boolean] true when
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
55
|
+
# @return [Boolean] true when the message was processed (including one a
|
|
56
|
+
# before_dispatch callback halted, or whose error the component's
|
|
57
|
+
# rescue_from took), false when an _error was broadcast in its place
|
|
58
|
+
def action(component, data)
|
|
59
|
+
return true unless data['_action']
|
|
63
60
|
|
|
64
|
-
|
|
61
|
+
dispatch = Dispatch.from_message(data)
|
|
62
|
+
ensure_exposed(component, dispatch)
|
|
63
|
+
component.perform_dispatch(dispatch)
|
|
65
64
|
|
|
66
65
|
true
|
|
67
|
-
rescue
|
|
66
|
+
rescue LiveCable::Forbidden => e
|
|
68
67
|
handle_error(component, e)
|
|
69
68
|
false
|
|
69
|
+
rescue StandardError => e
|
|
70
|
+
rescue_error(component, e)
|
|
70
71
|
end
|
|
71
72
|
|
|
72
73
|
private
|
|
73
74
|
|
|
74
|
-
def
|
|
75
|
-
|
|
75
|
+
def ensure_exposed(component, dispatch)
|
|
76
|
+
if dispatch.reactive?
|
|
77
|
+
return if component.class.client_writable?(dispatch.name)
|
|
78
|
+
|
|
79
|
+
raise LiveCable::Forbidden, "Non-writable reactive variable: #{dispatch.name}"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
return if component.class.allowed_actions.include?(dispatch.name)
|
|
76
83
|
|
|
77
|
-
|
|
78
|
-
ActionDispatch::ParamBuilder.from_pairs(
|
|
79
|
-
ActionDispatch::QueryParser.each_pair(params)
|
|
80
|
-
)
|
|
81
|
-
)
|
|
84
|
+
raise LiveCable::Forbidden, "Unauthorized action: #{dispatch.name}"
|
|
82
85
|
end
|
|
83
86
|
end
|
|
84
87
|
end
|
|
@@ -10,7 +10,8 @@ module LiveCable
|
|
|
10
10
|
|
|
11
11
|
return container[variable] if container.key?(variable)
|
|
12
12
|
|
|
13
|
-
container[variable] = process_initial_value(component,
|
|
13
|
+
container[variable] = process_initial_value(component, initial_value)
|
|
14
|
+
container[variable]
|
|
14
15
|
end
|
|
15
16
|
|
|
16
17
|
def set(container_name, variable, value)
|
|
@@ -31,25 +32,21 @@ module LiveCable
|
|
|
31
32
|
shared_changeset = containers[SHARED_CONTAINER]&.changeset
|
|
32
33
|
container = containers[component.live_id]
|
|
33
34
|
|
|
34
|
-
container.changeset | component.shared_reactive_variables.intersection(shared_changeset)
|
|
35
|
+
changeset = container.changeset | component.shared_reactive_variables.intersection(shared_changeset)
|
|
36
|
+
return changeset if changeset.empty?
|
|
37
|
+
|
|
38
|
+
changeset | component.shared_variables
|
|
35
39
|
end
|
|
36
40
|
|
|
37
41
|
private
|
|
38
42
|
|
|
39
|
-
def process_initial_value(component,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
initial_value.call(*args)
|
|
48
|
-
else
|
|
49
|
-
raise LiveCable::Error, "Initial value for \":#{variable}\" must be a proc or nil"
|
|
50
|
-
end
|
|
51
|
-
rescue StandardError => e
|
|
52
|
-
handle_error(component, e)
|
|
43
|
+
def process_initial_value(component, initial_value)
|
|
44
|
+
return if initial_value.nil?
|
|
45
|
+
|
|
46
|
+
args = []
|
|
47
|
+
args << component if initial_value.arity.positive?
|
|
48
|
+
|
|
49
|
+
initial_value.call(*args)
|
|
53
50
|
end
|
|
54
51
|
end
|
|
55
52
|
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'monitor'
|
|
4
|
+
|
|
3
5
|
module LiveCable
|
|
4
6
|
class Connection
|
|
5
7
|
include ComponentManagement
|
|
@@ -14,6 +16,37 @@ module LiveCable
|
|
|
14
16
|
@request = request
|
|
15
17
|
@containers = Hash.new { |hash, key| hash[key] = Container.new }
|
|
16
18
|
@components = {}
|
|
19
|
+
@monitor = Monitor.new
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Run a block holding this connection's lock.
|
|
23
|
+
#
|
|
24
|
+
# One Connection is shared by every component on a socket, but ActionCable
|
|
25
|
+
# runs a socket's work - each incoming message, each subscribe and
|
|
26
|
+
# unsubscribe, each stream_from callback - as separate jobs on a worker
|
|
27
|
+
# pool shared by the whole server, with nothing keeping two jobs for the
|
|
28
|
+
# same socket apart. Every such entry point holds this lock for its whole
|
|
29
|
+
# duration, action and render included, so a stream callback can't land
|
|
30
|
+
# between an action's write and the push that follows it.
|
|
31
|
+
#
|
|
32
|
+
# A Monitor rather than a Mutex because the calls nest: receive ->
|
|
33
|
+
# broadcast_changeset -> add_component all take it again on one thread.
|
|
34
|
+
# Only ever one connection's lock per job - never take another
|
|
35
|
+
# connection's while holding this one.
|
|
36
|
+
def synchronize(&)
|
|
37
|
+
@monitor.synchronize(&)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# A fresh controller and view per socket render, on a request and response
|
|
41
|
+
# kept until a code reload replaces ApplicationController.
|
|
42
|
+
#
|
|
43
|
+
# @return [ActionView::Base]
|
|
44
|
+
def view_context
|
|
45
|
+
unless ApplicationController.equal?(@renderer&.controller)
|
|
46
|
+
@renderer = Rendering::ControllerRenderer.from_defaults(ApplicationController)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
@renderer.build_controller.view_context
|
|
17
50
|
end
|
|
18
51
|
|
|
19
52
|
private
|
data/lib/live_cable/container.rb
CHANGED
|
@@ -18,7 +18,7 @@ module LiveCable
|
|
|
18
18
|
# - Shared reactive variables use a special SHARED_CONTAINER
|
|
19
19
|
# - During message processing, mutations are tracked in a changeset
|
|
20
20
|
# - After message processing, components with dirty changesets are re-rendered
|
|
21
|
-
# - Changesets are reset
|
|
21
|
+
# - Changesets are reset when the next message or stream callback starts
|
|
22
22
|
#
|
|
23
23
|
# Change Tracking:
|
|
24
24
|
# 1. Value is stored via []=
|
|
@@ -46,11 +46,12 @@ module LiveCable
|
|
|
46
46
|
# container[:user] = User.new # ActiveRecord - wrapped in Delegator
|
|
47
47
|
def []=(key, value)
|
|
48
48
|
# Remove observer from the old value so it stops notifying this container
|
|
49
|
-
self[key]
|
|
49
|
+
remove_observer_from(self[key], key)
|
|
50
50
|
|
|
51
|
-
# ActiveRecord models get observers attached directly
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
# ActiveRecord models get observers attached directly, even when stored wrapped
|
|
52
|
+
record = value.is_a?(Delegator) ? value.__getobj__ : value
|
|
53
|
+
if record.class < ModelObserver
|
|
54
|
+
record.add_live_cable_observer(observer, key)
|
|
54
55
|
end
|
|
55
56
|
|
|
56
57
|
# If value is already a Delegator, add observer and store as-is
|
|
@@ -59,7 +60,7 @@ module LiveCable
|
|
|
59
60
|
super
|
|
60
61
|
else
|
|
61
62
|
# Wrap supported types in Delegators for change tracking
|
|
62
|
-
super(key, Delegator.create_if_supported(value, key, observer))
|
|
63
|
+
super(key, Delegator.create_if_supported(Delegator.unwrap(value), key, observer))
|
|
63
64
|
end
|
|
64
65
|
end
|
|
65
66
|
|
|
@@ -87,8 +88,8 @@ module LiveCable
|
|
|
87
88
|
!@changeset.empty?
|
|
88
89
|
end
|
|
89
90
|
|
|
90
|
-
# Clear the changeset
|
|
91
|
-
# Called by Connection
|
|
91
|
+
# Clear the changeset before tracking a new round of changes.
|
|
92
|
+
# Called by Connection before each message or stream callback.
|
|
92
93
|
#
|
|
93
94
|
# @return [void]
|
|
94
95
|
def reset_changeset
|
|
@@ -114,9 +115,7 @@ module LiveCable
|
|
|
114
115
|
# Note: We only remove this specific observer, not all observers,
|
|
115
116
|
# because the same object might be shared across multiple containers
|
|
116
117
|
each do |variable, value|
|
|
117
|
-
|
|
118
|
-
value.remove_live_cable_observer(observer, variable)
|
|
119
|
-
end
|
|
118
|
+
remove_observer_from(value, variable)
|
|
120
119
|
end
|
|
121
120
|
|
|
122
121
|
# Clear the container's data
|
|
@@ -124,5 +123,12 @@ module LiveCable
|
|
|
124
123
|
@changeset&.clear
|
|
125
124
|
@observer = nil
|
|
126
125
|
end
|
|
126
|
+
|
|
127
|
+
private
|
|
128
|
+
|
|
129
|
+
def remove_observer_from(value, key)
|
|
130
|
+
value.try(:remove_live_cable_observer, observer, key)
|
|
131
|
+
value.__getobj__.try(:remove_live_cable_observer, observer, key) if value.is_a?(Delegator)
|
|
132
|
+
end
|
|
127
133
|
end
|
|
128
134
|
end
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LiveCable
|
|
4
|
+
# Signs and verifies the reactive-variable defaults that travel from a
|
|
5
|
+
# server-rendered page to the client and back on subscribe.
|
|
6
|
+
#
|
|
7
|
+
# Defaults originate on the server in the `live(...)` helper, but they are
|
|
8
|
+
# emitted into the DOM as a `live-defaults` attribute and re-sent by the
|
|
9
|
+
# client when its ActionCable subscription connects. Because that value
|
|
10
|
+
# round-trips through the browser, it cannot be trusted: without signing, a
|
|
11
|
+
# user could edit the attribute (or craft the subscribe frame) to set *any*
|
|
12
|
+
# reactive variable at subscribe time - including ones deliberately not
|
|
13
|
+
# marked `writable:`, such as a record id, price, or tenant key.
|
|
14
|
+
#
|
|
15
|
+
# The blob is signed with a key derived from secret_key_base and bound to the
|
|
16
|
+
# component's live_id, so a tampered or replayed value verifies as invalid
|
|
17
|
+
# and is treated as no defaults at all.
|
|
18
|
+
module DefaultsSigner
|
|
19
|
+
PURPOSE = 'live_cable/defaults'
|
|
20
|
+
JSON_SAFE = [String, Symbol, Numeric, TrueClass, FalseClass, NilClass, Date, Time].freeze
|
|
21
|
+
|
|
22
|
+
module_function
|
|
23
|
+
|
|
24
|
+
# Equal defaults always sign to the same blob, so the client can compare
|
|
25
|
+
# blobs to tell whether a component's defaults have changed.
|
|
26
|
+
#
|
|
27
|
+
# @param defaults [Hash] The reactive-variable defaults to sign
|
|
28
|
+
# @param live_id [String] The component instance the defaults belong to
|
|
29
|
+
# @return [String] A signed, tamper-evident blob
|
|
30
|
+
def sign(defaults, live_id)
|
|
31
|
+
defaults = (defaults || {}).as_json.sort.to_h
|
|
32
|
+
|
|
33
|
+
verifier.generate({ 'live_id' => live_id, 'defaults' => defaults }, purpose: PURPOSE)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# The defaults as the component gets them back from the page on
|
|
37
|
+
# subscribe, so a prerender can render with the same values. In
|
|
38
|
+
# development and test, warns about any default that changes on the way.
|
|
39
|
+
#
|
|
40
|
+
# @param defaults [Hash]
|
|
41
|
+
# @param component_class [Class] Named in warnings and errors
|
|
42
|
+
# @return [Hash] String-keyed, JSON-native defaults
|
|
43
|
+
# @raise [ArgumentError] if a default is or holds anything but strings,
|
|
44
|
+
# symbols, numbers, booleans, nil, times, dates and permitted params,
|
|
45
|
+
# whose JSON form could put a record's attributes or an object's state
|
|
46
|
+
# in the page
|
|
47
|
+
def round_trip(defaults, component_class)
|
|
48
|
+
defaults ||= {}
|
|
49
|
+
reject_unsafe(defaults, component_class)
|
|
50
|
+
json = JSON.parse(JSON.generate(defaults.as_json))
|
|
51
|
+
|
|
52
|
+
warn_about_changed(defaults, json, component_class) if Rails.env.local?
|
|
53
|
+
|
|
54
|
+
json
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# @param blob [String, nil] A blob previously produced by {sign}
|
|
58
|
+
# @param live_id [String] The component instance the blob must be bound to
|
|
59
|
+
# @return [Hash] The verified defaults, or an empty hash when the blob is
|
|
60
|
+
# blank, not a string, tampered with, or bound to a different component
|
|
61
|
+
def verify(blob, live_id)
|
|
62
|
+
return {} unless blob.is_a?(String) && blob.present?
|
|
63
|
+
|
|
64
|
+
data = verifier.verify(blob, purpose: PURPOSE)
|
|
65
|
+
return {} unless data.is_a?(Hash) && data['live_id'] == live_id
|
|
66
|
+
|
|
67
|
+
data['defaults'] || {}
|
|
68
|
+
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
|
69
|
+
{}
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def reject_unsafe(defaults, component_class)
|
|
73
|
+
defaults.each do |key, value|
|
|
74
|
+
value = undelegated(value)
|
|
75
|
+
unsafe = unsafe_part(value) or next
|
|
76
|
+
|
|
77
|
+
raise ArgumentError, "#{component_class.name} default :#{key} (#{value.class}) #{unsafe_reason(unsafe)}"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def unsafe_reason(unsafe)
|
|
82
|
+
if record?(unsafe)
|
|
83
|
+
'is or holds an ActiveRecord record, which would be signed into the page as JSON with every one of its ' \
|
|
84
|
+
'attributes. Pass its id and load the record in the component.'
|
|
85
|
+
elsif params?(unsafe)
|
|
86
|
+
"is or holds ActionController::Parameters that aren't permitted. Permit the keys it may take, " \
|
|
87
|
+
'such as params.permit(:status).'
|
|
88
|
+
else
|
|
89
|
+
"is or holds an object (#{unsafe.class}) that isn't JSON-native, which would be signed into the page as " \
|
|
90
|
+
'its JSON form, often every one of its instance variables. Pass strings, numbers, booleans, nil, times, ' \
|
|
91
|
+
'or arrays and hashes of those, and load anything else in the component.'
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# The first thing in value that JSON can't carry as itself, or nil.
|
|
96
|
+
# is_a? rather than case, which TimeWithZone and Duration don't answer.
|
|
97
|
+
def unsafe_part(value)
|
|
98
|
+
value = undelegated(value)
|
|
99
|
+
|
|
100
|
+
if JSON_SAFE.any? { |type| value.is_a?(type) }
|
|
101
|
+
nil
|
|
102
|
+
elsif params?(value)
|
|
103
|
+
value.permitted? ? unsafe_part(value.to_h) : value
|
|
104
|
+
elsif value.is_a?(Hash)
|
|
105
|
+
value.each_value.lazy.filter_map { |item| unsafe_part(item) }.first
|
|
106
|
+
elsif value.is_a?(Array)
|
|
107
|
+
value.lazy.filter_map { |item| unsafe_part(item) }.first
|
|
108
|
+
else
|
|
109
|
+
value
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def params?(value)
|
|
114
|
+
defined?(ActionController::Parameters) && value.is_a?(ActionController::Parameters)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def record?(value)
|
|
118
|
+
defined?(ActiveRecord) && (value.is_a?(ActiveRecord::Base) || value.is_a?(ActiveRecord::Relation))
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def undelegated(value)
|
|
122
|
+
value = value.__getobj__ while value.is_a?(::Delegator)
|
|
123
|
+
value
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def warn_about_changed(defaults, json, component_class)
|
|
127
|
+
defaults.each do |key, value|
|
|
128
|
+
next if json[key.to_s] == value
|
|
129
|
+
|
|
130
|
+
LiveCable.warn_once(
|
|
131
|
+
"[LiveCable] #{component_class.name} default :#{key} (#{value.class}) is not JSON-native, so it " \
|
|
132
|
+
'reaches the component as its JSON form. Pass strings, numbers, booleans, nil, or arrays and ' \
|
|
133
|
+
'string-keyed hashes of those.'
|
|
134
|
+
)
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# @return [ActiveSupport::MessageVerifier]
|
|
139
|
+
def verifier
|
|
140
|
+
# JSON serializer keeps the payload to simple, safe types (no Marshal).
|
|
141
|
+
ActiveSupport::MessageVerifier.new(secret, serializer: JSON, url_safe: true)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# A key derived for this purpose alone, as Rails' own verifiers do, rather
|
|
145
|
+
# than secret_key_base itself - which would make these signatures
|
|
146
|
+
# interchangeable with anything else HMAC'd under the raw secret.
|
|
147
|
+
def secret
|
|
148
|
+
if Rails.application&.secret_key_base.blank?
|
|
149
|
+
raise LiveCable::Error, 'secret_key_base is required to sign LiveCable defaults'
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
Rails.application.key_generator.generate_key(PURPOSE)
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|