cabby-rails 0.1.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.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +27 -0
  3. data/LICENSE +21 -0
  4. data/README.md +190 -0
  5. data/lib/action_cable/subscription_adapter/cabby.rb +29 -0
  6. data/lib/cabby/broadcast/http.rb +103 -0
  7. data/lib/cabby/broadcast/nats.rb +45 -0
  8. data/lib/cabby/broadcast/postgres.rb +52 -0
  9. data/lib/cabby/broadcast/redis.rb +25 -0
  10. data/lib/cabby/broadcast/redis_connection.rb +54 -0
  11. data/lib/cabby/broadcast/redisx.rb +25 -0
  12. data/lib/cabby/broadcast.rb +53 -0
  13. data/lib/cabby/config.rb +159 -0
  14. data/lib/cabby/errors.rb +16 -0
  15. data/lib/cabby/rails/helper.rb +10 -0
  16. data/lib/cabby/rails/identifiers.rb +47 -0
  17. data/lib/cabby/rails/railtie.rb +34 -0
  18. data/lib/cabby/rails/remote_connections.rb +34 -0
  19. data/lib/cabby/rails/rpc/channel_streams.rb +19 -0
  20. data/lib/cabby/rails/rpc/command.rb +122 -0
  21. data/lib/cabby/rails/rpc/compat.rb +25 -0
  22. data/lib/cabby/rails/rpc/connect.rb +30 -0
  23. data/lib/cabby/rails/rpc/disconnect.rb +41 -0
  24. data/lib/cabby/rails/rpc/handler.rb +71 -0
  25. data/lib/cabby/rails/rpc/log_tags.rb +27 -0
  26. data/lib/cabby/rails/rpc/metadata.rb +26 -0
  27. data/lib/cabby/rails/rpc/rack_env.rb +73 -0
  28. data/lib/cabby/rails/rpc/server.rb +79 -0
  29. data/lib/cabby/rails/rpc/socket.rb +37 -0
  30. data/lib/cabby/rails/rpc.rb +113 -0
  31. data/lib/cabby/rails/serializer.rb +58 -0
  32. data/lib/cabby/rails/state.rb +70 -0
  33. data/lib/cabby/rails/version.rb +5 -0
  34. data/lib/cabby/rails/wiring.rb +30 -0
  35. data/lib/cabby/rails.rb +30 -0
  36. data/lib/cabby/secrets.rb +18 -0
  37. data/lib/cabby/signed_streams.rb +48 -0
  38. data/lib/cabby-rails.rb +2 -0
  39. data/lib/cabby.rb +57 -0
  40. data/lib/generators/cabby/setup/setup_generator.rb +138 -0
  41. data/lib/generators/cabby/setup/templates/cabby.yml +24 -0
  42. data/lib/generators/cabby/setup/templates/cable.yml +10 -0
  43. metadata +165 -0
@@ -0,0 +1,138 @@
1
+ require "rails/generators"
2
+ require "yaml"
3
+
4
+ module Cabby
5
+ module Generators
6
+ # bin/rails g cabby:setup: config/cabby.yml, adapter: cabby, the /_cabby endpoint, and the
7
+ # cabby command that matches. Running it again changes nothing.
8
+ class SetupGenerator < ::Rails::Generators::Base
9
+ source_root File.expand_path("templates", __dir__)
10
+
11
+ ENVIRONMENTS = %w[development production].freeze
12
+ MOUNT = %( mount Cabby::Rails::RPC => "/_cabby"\n).freeze
13
+
14
+ def create_config
15
+ copy_file "cabby.yml", "config/cabby.yml", skip: true
16
+ end
17
+
18
+ def use_the_cabby_adapter
19
+ path = File.join(destination_root, "config/cable.yml")
20
+ return copy_file("cable.yml", "config/cable.yml") unless File.exist?(path)
21
+
22
+ original = File.read(path)
23
+ edited = CableYaml.new(original).with_adapter(ENVIRONMENTS)
24
+ if edited.nil?
25
+ @cable_instructions = true
26
+ elsif edited == original
27
+ say_status :identical, "config/cable.yml"
28
+ else
29
+ File.write(path, edited) unless options[:pretend]
30
+ say_status :update, "config/cable.yml"
31
+ end
32
+ end
33
+
34
+ def mount_the_endpoint
35
+ path = File.join(destination_root, "config/routes.rb")
36
+ return @routes_instructions = true unless File.exist?(path) && File.read(path).match?(/\.routes\.draw do/)
37
+ return say_status(:identical, "config/routes.rb") if File.read(path).include?("Cabby::Rails::RPC")
38
+
39
+ inject_into_file "config/routes.rb", MOUNT, after: /\.routes\.draw do[^\n]*\n/
40
+ end
41
+
42
+ def next_steps
43
+ say ""
44
+ if @cable_instructions
45
+ say "config/cable.yml was left as it is: set `adapter: cabby` for development and production in " \
46
+ "config/cable.yml.", :yellow
47
+ end
48
+ if @routes_instructions
49
+ say "config/routes.rb was not changed: add `#{MOUNT.strip}` inside the routes block.", :yellow
50
+ end
51
+ say <<~TEXT
52
+ Next:
53
+
54
+ 1. Give the app and the server the same secret, for example:
55
+ export CABBY_SECRET=$(bin/rails secret)
56
+ (or put it in the credentials as cabby: { secret: ... }; config/cabby.yml reads either).
57
+
58
+ 2. Start the server next to the app:
59
+ #{cabby_command}
60
+
61
+ 3. Browsers connect to ws://localhost:8080/cable (url in config/cabby.yml).
62
+ TEXT
63
+ end
64
+
65
+ private
66
+
67
+ def cabby_command
68
+ flags = ["--rpc_host=http://localhost:3000/_cabby", "--secret=$CABBY_SECRET"]
69
+ # The server confirms Turbo Stream subscriptions itself with the key the gem gives Turbo.
70
+ flags << "--turbo_streams" if defined?(::Turbo)
71
+ "cabby #{flags.join(" ")}"
72
+ end
73
+
74
+ # Sets `adapter: cabby` in some sections of a cable.yml, line by line so comments, anchors
75
+ # and other keys stay. nil when the file has a shape this does not handle.
76
+ class CableYaml
77
+ def initialize(text)
78
+ @text = text
79
+ end
80
+
81
+ def with_adapter(environments)
82
+ lines = environments.inject(@text.lines) { |current, env| current && set_adapter(current, env) }
83
+ edited = lines&.join
84
+ edited if edited && adapters_ok?(edited, environments)
85
+ end
86
+
87
+ private
88
+
89
+ def set_adapter(lines, env)
90
+ start = lines.index { |line| line.match?(/\A#{env}:\s*(#.*)?\Z/) }
91
+ return append_section(lines, env) if start.nil? && lines.none? { |line| line.start_with?("#{env}:") }
92
+
93
+ start && set_in_section(lines.dup, start)
94
+ end
95
+
96
+ def set_in_section(lines, start)
97
+ block = section_lines(lines, start)
98
+ indent = block.lazy.map { |i| lines[i][/\A( +)\S/, 1] }.find(&:itself) || " "
99
+ adapter = block.find { |i| lines[i].match?(/\A#{indent}adapter:/) }
100
+ if adapter
101
+ lines[adapter] = "#{indent}adapter: cabby\n" unless lines[adapter].match?(/adapter:\s*cabby\s*(#.*)?\Z/)
102
+ else
103
+ # After a merge key, so the section's own adapter wins over the merged one.
104
+ merge = block.select { |i| lines[i].match?(/\A#{indent}<<:/) }.last
105
+ lines.insert((merge || start) + 1, "#{indent}adapter: cabby\n")
106
+ end
107
+ lines
108
+ end
109
+
110
+ # The indented (or blank) lines after a section's key.
111
+ def section_lines(lines, start)
112
+ finish = start + 1
113
+ finish += 1 while finish < lines.size && lines[finish].match?(/\A(\s|\z)/)
114
+ ((start + 1)...finish)
115
+ end
116
+
117
+ def append_section(lines, env)
118
+ separator = lines.last.to_s.end_with?("\n\n") || lines.empty? ? "" : "\n"
119
+ lines + ["#{separator}#{env}:\n", " adapter: cabby\n"]
120
+ end
121
+
122
+ # The result must parse with the adapter set and the other sections as they were.
123
+ def adapters_ok?(edited, environments)
124
+ before = parse(@text)
125
+ after = parse(edited)
126
+ return false unless before.is_a?(Hash) && after.is_a?(Hash)
127
+
128
+ environments.all? { |env| after.dig(env, "adapter") == "cabby" } &&
129
+ (before.keys - environments).all? { |key| before[key] == after[key] }
130
+ rescue Psych::Exception
131
+ false
132
+ end
133
+
134
+ def parse(text) = YAML.safe_load(text.gsub(/<%.*?%>/m, "erb"), aliases: true)
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,24 @@
1
+ # Cabby settings, per environment. CABBY_* environment variables override these, and a
2
+ # Cabby.configure block in an initializer overrides both. The cabby-rails README lists every
3
+ # setting with its variable and default.
4
+ default: &default
5
+ # The secret the app shares with the server (cabby --secret). It never goes in this file:
6
+ # it comes from CABBY_SECRET or from the credentials (bin/rails credentials:edit, then
7
+ # cabby: { secret: ... }).
8
+ secret: <%= ENV.fetch("CABBY_SECRET") { Rails.application.credentials.dig(:cabby, :secret) } %>
9
+
10
+ # How the app sends broadcasts to the server: http, postgres, redisx, redis or nats.
11
+ # broadcast_adapter: http
12
+
13
+ development:
14
+ <<: *default
15
+ # Where browsers connect; action_cable_meta_tag and Turbo use it.
16
+ url: ws://localhost:8080/cable
17
+
18
+ test:
19
+ <<: *default
20
+
21
+ production:
22
+ <<: *default
23
+ # Where browsers connect, for example wss://cable.example.com/cable (or CABBY_URL).
24
+ # url:
@@ -0,0 +1,10 @@
1
+ # The Cabby server holds the WebSocket connections; adapter: cabby sends the app's broadcasts
2
+ # to it (see config/cabby.yml).
3
+ development:
4
+ adapter: cabby
5
+
6
+ test:
7
+ adapter: test
8
+
9
+ production:
10
+ adapter: cabby
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cabby-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniele Pestilli
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: actioncable
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '8.0'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '9'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '8.0'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '9'
32
+ - !ruby/object:Gem::Dependency
33
+ name: activesupport
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '8.0'
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '9'
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '8.0'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '9'
52
+ - !ruby/object:Gem::Dependency
53
+ name: globalid
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '1.1'
59
+ - - "<"
60
+ - !ruby/object:Gem::Version
61
+ version: '2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ - - "<"
70
+ - !ruby/object:Gem::Version
71
+ version: '2'
72
+ - !ruby/object:Gem::Dependency
73
+ name: railties
74
+ requirement: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '8.0'
79
+ - - "<"
80
+ - !ruby/object:Gem::Version
81
+ version: '9'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '8.0'
89
+ - - "<"
90
+ - !ruby/object:Gem::Version
91
+ version: '9'
92
+ description: Runs a Rails app's Action Cable connections and channels for Cabby over
93
+ HTTP RPC, in pure Ruby.
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - CHANGELOG.md
99
+ - LICENSE
100
+ - README.md
101
+ - lib/action_cable/subscription_adapter/cabby.rb
102
+ - lib/cabby-rails.rb
103
+ - lib/cabby.rb
104
+ - lib/cabby/broadcast.rb
105
+ - lib/cabby/broadcast/http.rb
106
+ - lib/cabby/broadcast/nats.rb
107
+ - lib/cabby/broadcast/postgres.rb
108
+ - lib/cabby/broadcast/redis.rb
109
+ - lib/cabby/broadcast/redis_connection.rb
110
+ - lib/cabby/broadcast/redisx.rb
111
+ - lib/cabby/config.rb
112
+ - lib/cabby/errors.rb
113
+ - lib/cabby/rails.rb
114
+ - lib/cabby/rails/helper.rb
115
+ - lib/cabby/rails/identifiers.rb
116
+ - lib/cabby/rails/railtie.rb
117
+ - lib/cabby/rails/remote_connections.rb
118
+ - lib/cabby/rails/rpc.rb
119
+ - lib/cabby/rails/rpc/channel_streams.rb
120
+ - lib/cabby/rails/rpc/command.rb
121
+ - lib/cabby/rails/rpc/compat.rb
122
+ - lib/cabby/rails/rpc/connect.rb
123
+ - lib/cabby/rails/rpc/disconnect.rb
124
+ - lib/cabby/rails/rpc/handler.rb
125
+ - lib/cabby/rails/rpc/log_tags.rb
126
+ - lib/cabby/rails/rpc/metadata.rb
127
+ - lib/cabby/rails/rpc/rack_env.rb
128
+ - lib/cabby/rails/rpc/server.rb
129
+ - lib/cabby/rails/rpc/socket.rb
130
+ - lib/cabby/rails/serializer.rb
131
+ - lib/cabby/rails/state.rb
132
+ - lib/cabby/rails/version.rb
133
+ - lib/cabby/rails/wiring.rb
134
+ - lib/cabby/secrets.rb
135
+ - lib/cabby/signed_streams.rb
136
+ - lib/generators/cabby/setup/setup_generator.rb
137
+ - lib/generators/cabby/setup/templates/cabby.yml
138
+ - lib/generators/cabby/setup/templates/cable.yml
139
+ homepage: https://cabbyhq.com
140
+ licenses:
141
+ - MIT
142
+ metadata:
143
+ rubygems_mfa_required: 'true'
144
+ source_code_uri: https://github.com/DaniG2k/cabby-rails
145
+ changelog_uri: https://github.com/DaniG2k/cabby-rails/blob/main/CHANGELOG.md
146
+ bug_tracker_uri: https://github.com/DaniG2k/cabby-rails/issues
147
+ documentation_uri: https://docs.cabbyhq.com/rails
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '3.2'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubygems_version: 4.0.20
163
+ specification_version: 4
164
+ summary: Rails integration for Cabby, the real-time server
165
+ test_files: []