ruact 0.0.1 → 0.0.3

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 (129) hide show
  1. checksums.yaml +4 -4
  2. data/.codecov.yml +31 -0
  3. data/.github/workflows/ci.yml +160 -94
  4. data/.github/workflows/server-functions-bench.yml +54 -0
  5. data/.rubocop.yml +19 -1
  6. data/.rubocop_todo.yml +175 -0
  7. data/CHANGELOG.md +86 -5
  8. data/README.md +2 -0
  9. data/RELEASING.md +9 -3
  10. data/bench/server_functions_dispatch_bench.rb +309 -0
  11. data/bench/server_functions_dispatch_bench.results.md +121 -0
  12. data/docs/internal/README.md +9 -0
  13. data/docs/internal/decisions/server-functions-api.md +1680 -0
  14. data/lib/generators/ruact/install/install_generator.rb +43 -0
  15. data/lib/generators/ruact/install/templates/application.jsx.tt +1 -1
  16. data/lib/generators/ruact/install/templates/initializer.rb.tt +1 -1
  17. data/lib/ruact/client_manifest.rb +125 -12
  18. data/lib/ruact/configuration.rb +264 -23
  19. data/lib/ruact/controller.rb +459 -32
  20. data/lib/ruact/doctor.rb +34 -2
  21. data/lib/ruact/erb_preprocessor.rb +6 -6
  22. data/lib/ruact/errors.rb +89 -0
  23. data/lib/ruact/flight/serializer.rb +2 -2
  24. data/lib/ruact/html_converter.rb +131 -31
  25. data/lib/ruact/query.rb +107 -0
  26. data/lib/ruact/railtie.rb +220 -3
  27. data/lib/ruact/render_context.rb +30 -0
  28. data/lib/ruact/render_pipeline.rb +201 -59
  29. data/lib/ruact/routing.rb +81 -0
  30. data/lib/ruact/serializable.rb +11 -11
  31. data/lib/ruact/server.rb +341 -0
  32. data/lib/ruact/server_action.rb +131 -0
  33. data/lib/ruact/server_functions/backtrace_cleaner.rb +32 -0
  34. data/lib/ruact/server_functions/bucket_two_payload.rb +109 -0
  35. data/lib/ruact/server_functions/codegen.rb +330 -0
  36. data/lib/ruact/server_functions/codegen_v2.rb +176 -0
  37. data/lib/ruact/server_functions/endpoint_controller.rb +237 -0
  38. data/lib/ruact/server_functions/error_payload.rb +93 -0
  39. data/lib/ruact/server_functions/error_rendering.rb +188 -0
  40. data/lib/ruact/server_functions/error_suggestion.rb +38 -0
  41. data/lib/ruact/server_functions/name_bridge.rb +113 -0
  42. data/lib/ruact/server_functions/query_context.rb +62 -0
  43. data/lib/ruact/server_functions/query_dispatch.rb +248 -0
  44. data/lib/ruact/server_functions/registry.rb +148 -0
  45. data/lib/ruact/server_functions/registry_entry.rb +26 -0
  46. data/lib/ruact/server_functions/route_source.rb +201 -0
  47. data/lib/ruact/server_functions/snapshot.rb +195 -0
  48. data/lib/ruact/server_functions/snapshot_writer.rb +65 -0
  49. data/lib/ruact/server_functions/standalone_context.rb +103 -0
  50. data/lib/ruact/server_functions/standalone_dispatcher.rb +178 -0
  51. data/lib/ruact/server_functions.rb +75 -0
  52. data/lib/ruact/version.rb +1 -1
  53. data/lib/ruact/view_helper.rb +17 -9
  54. data/lib/ruact.rb +85 -6
  55. data/lib/rubocop/cop/ruact/no_shared_state.rb +1 -1
  56. data/lib/tasks/benchmark.rake +15 -11
  57. data/lib/tasks/ruact.rake +81 -0
  58. data/spec/benchmarks/render_pipeline_benchmark_spec.rb +1 -1
  59. data/spec/fixtures/flight/README.md +55 -7
  60. data/spec/fixtures/flight/bigint.txt +1 -0
  61. data/spec/fixtures/flight/infinity.txt +1 -0
  62. data/spec/fixtures/flight/nan.txt +1 -0
  63. data/spec/fixtures/flight/negative_infinity.txt +1 -0
  64. data/spec/fixtures/flight/undefined.txt +1 -0
  65. data/spec/fixtures/story_7_9_views/controller_request_spec_support/demo/show.html.erb +3 -0
  66. data/spec/ruact/client_manifest_spec.rb +108 -0
  67. data/spec/ruact/configuration_spec.rb +501 -0
  68. data/spec/ruact/controller_request_spec.rb +204 -0
  69. data/spec/ruact/controller_spec.rb +427 -39
  70. data/spec/ruact/doctor_spec.rb +118 -0
  71. data/spec/ruact/erb_preprocessor_hook_spec.rb +3 -3
  72. data/spec/ruact/erb_preprocessor_spec.rb +7 -7
  73. data/spec/ruact/errors_spec.rb +95 -0
  74. data/spec/ruact/flight/renderer_spec.rb +14 -3
  75. data/spec/ruact/flight/serializer_spec.rb +129 -88
  76. data/spec/ruact/html_converter_spec.rb +183 -5
  77. data/spec/ruact/install_generator_spec.rb +93 -0
  78. data/spec/ruact/query_request_spec.rb +446 -0
  79. data/spec/ruact/query_spec.rb +105 -0
  80. data/spec/ruact/railtie_spec.rb +2 -3
  81. data/spec/ruact/render_context_spec.rb +58 -0
  82. data/spec/ruact/render_pipeline_concurrency_spec.rb +78 -0
  83. data/spec/ruact/render_pipeline_spec.rb +784 -330
  84. data/spec/ruact/serializable_spec.rb +8 -8
  85. data/spec/ruact/server_bucket_request_spec.rb +352 -0
  86. data/spec/ruact/server_function_name_spec.rb +53 -0
  87. data/spec/ruact/server_functions/backtrace_cleaner_spec.rb +63 -0
  88. data/spec/ruact/server_functions/bucket_two_payload_spec.rb +200 -0
  89. data/spec/ruact/server_functions/codegen_spec.rb +429 -0
  90. data/spec/ruact/server_functions/csrf_request_spec.rb +380 -0
  91. data/spec/ruact/server_functions/dispatch_request_spec.rb +819 -0
  92. data/spec/ruact/server_functions/error_payload_spec.rb +222 -0
  93. data/spec/ruact/server_functions/error_suggestion_spec.rb +79 -0
  94. data/spec/ruact/server_functions/name_bridge_spec.rb +188 -0
  95. data/spec/ruact/server_functions/query_context_spec.rb +72 -0
  96. data/spec/ruact/server_functions/railtie_integration_spec.rb +345 -0
  97. data/spec/ruact/server_functions/rake_spec.rb +86 -0
  98. data/spec/ruact/server_functions/registry_spec.rb +199 -0
  99. data/spec/ruact/server_functions/route_source_spec.rb +202 -0
  100. data/spec/ruact/server_functions/snapshot_spec.rb +256 -0
  101. data/spec/ruact/server_functions/snapshot_writer_spec.rb +71 -0
  102. data/spec/ruact/server_functions/standalone_action_spec.rb +224 -0
  103. data/spec/ruact/server_functions/standalone_context_spec.rb +142 -0
  104. data/spec/ruact/server_functions/standalone_dispatcher_spec.rb +273 -0
  105. data/spec/ruact/server_rescue_request_spec.rb +416 -0
  106. data/spec/ruact/server_spec.rb +180 -0
  107. data/spec/ruact/server_upload_request_spec.rb +311 -0
  108. data/spec/ruact/view_helper_spec.rb +23 -17
  109. data/spec/spec_helper.rb +52 -1
  110. data/spec/support/fixtures/pixel.png +0 -0
  111. data/spec/support/flight_wire_parser.rb +135 -0
  112. data/spec/support/flight_wire_parser_spec.rb +93 -0
  113. data/spec/support/matchers/flight_fixture_matcher.rb +356 -0
  114. data/spec/support/matchers/flight_fixture_matcher_spec.rb +250 -0
  115. data/spec/support/rails_stub.rb +75 -5
  116. data/vendor/javascript/ruact-server-functions-runtime/index.d.ts +139 -0
  117. data/vendor/javascript/ruact-server-functions-runtime/index.js +438 -0
  118. data/vendor/javascript/ruact-server-functions-runtime/index.test.mjs +827 -0
  119. data/vendor/javascript/ruact-server-functions-runtime/package.json +22 -0
  120. data/vendor/javascript/vite-plugin-ruact/index.js +3 -2
  121. data/vendor/javascript/vite-plugin-ruact/package-lock.json +1429 -0
  122. data/vendor/javascript/vite-plugin-ruact/package.json +15 -0
  123. data/vendor/javascript/vite-plugin-ruact/server-functions-codegen.mjs +761 -0
  124. data/vendor/javascript/vite-plugin-ruact/server-functions-codegen.test.mjs +866 -0
  125. data/vendor/javascript/vite-plugin-ruact/vitest.config.mjs +15 -0
  126. metadata +87 -6
  127. data/Users/luiz/workspace/rails-rsc/gem/vendor/javascript/vite-plugin-ruact/index.js +0 -163
  128. data/lib/ruact/component_registry.rb +0 -31
  129. data/lib/tasks/rsc.rake +0 -9
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruact
4
+ module ServerFunctions
5
+ # Story 9.4 (D3) — per-request execution context injected into a
6
+ # {Ruact::Query} subclass by the internal query dispatch controller. A
7
+ # fresh instance is built per request (NFR8) wrapping the DISPATCHING
8
+ # controller instance; because that controller inherits
9
+ # `Ruact.config.query_parent_controller`, every delegated reader resolves
10
+ # to the host's own machinery — `current_user` IS the host's method
11
+ # (Devise / Pundit / hand-rolled), not a gem-side resolver lambda.
12
+ #
13
+ # Mirrors the SHAPE of the Story-8.3 `StandaloneContext` (plain accessors,
14
+ # per-request instance) while sourcing everything from the controller —
15
+ # the resolver-lambda pattern is superseded by the 2026-06-02 ADR
16
+ # addendum (Decision 2).
17
+ class QueryContext
18
+ # @param controller [ActionController::Base] the dispatching controller
19
+ # instance (a generated subclass of `Ruact.config.query_parent_controller`).
20
+ def initialize(controller:)
21
+ @controller = controller
22
+ end
23
+
24
+ # The host's authenticated user. Resolved through the controller's own
25
+ # `current_user` — public OR private (hand-rolled apps commonly define
26
+ # it `private`). When the host chain defines no `current_user` at all,
27
+ # raises a NoMethodError that names the fix instead of a bare
28
+ # "undefined method" from deep inside a query body.
29
+ #
30
+ # @return [Object, nil]
31
+ # @raise [NoMethodError] when the parent controller chain defines no
32
+ # `current_user`.
33
+ def current_user
34
+ unless @controller.respond_to?(:current_user, true)
35
+ raise NoMethodError,
36
+ "ruact: the query dispatch controller (inheriting " \
37
+ "#{@controller.class.superclass.name}, via Ruact.config.query_parent_controller) " \
38
+ "does not define `current_user`. Define it on the parent controller, or point " \
39
+ "`query_parent_controller` at a controller that does."
40
+ end
41
+
42
+ @controller.send(:current_user)
43
+ end
44
+
45
+ # @return [ActionController::Parameters] the request params (query-string
46
+ # parameters on a GET query route).
47
+ def params
48
+ @controller.params
49
+ end
50
+
51
+ # @return [ActionDispatch::Request] the live request.
52
+ def request
53
+ @controller.request
54
+ end
55
+
56
+ # @return [ActionDispatch::Request::Session] the host middleware's session.
57
+ def session
58
+ @controller.session
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,248 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "action_controller"
4
+
5
+ require_relative "error_rendering"
6
+ require_relative "bucket_two_payload"
7
+ require_relative "query_context"
8
+
9
+ module Ruact
10
+ module ServerFunctions
11
+ # Story 9.4 (D2) — generates the INTERNAL dispatch controller backing the
12
+ # routes the `ruact_queries` macro draws: one controller subclass PER
13
+ # {Ruact::Query} subclass, with one action per public query method. The
14
+ # per-class shape is what makes the AC4 callback opt-out scopable — a
15
+ # `ruact_skip_before_action` on one query class lands on that class's
16
+ # controller only, and `only:`/`except:` options scope it further to
17
+ # individual query methods (each method is one action).
18
+ #
19
+ # The controller inherits `Ruact.config.query_parent_controller`
20
+ # (default `ApplicationController`), resolved LAZILY here — at route-draw
21
+ # time, when the host's constants exist — never at gem-load or configure
22
+ # time. The host's REAL callback chain therefore runs before the query
23
+ # class is instantiated (FR89). The dev never sees this controller; it is
24
+ # named under this module (e.g.
25
+ # `Ruact::ServerFunctions::QueryDispatch::CatalogQueryController`) only so
26
+ # Rails' string-based route resolution (`to: "…/catalog_query#categories"`)
27
+ # and `rails routes` output stay legible.
28
+ #
29
+ # Regeneration is idempotent: every `ruact_queries` evaluation (boot AND
30
+ # every dev-mode routes reload) rebuilds the constant from the query
31
+ # class's CURRENT state, and the query class itself is re-constantized per
32
+ # request, so code reloading never serves a stale class.
33
+ module QueryDispatch
34
+ # Instance-level dispatch plumbing shared by every generated controller.
35
+ # Included into the generated subclass, so these definitions override
36
+ # anything the parent chain provides (notably the {Ruact::Server} gates,
37
+ # when the host's ApplicationController happens to include the mutation
38
+ # concern).
39
+ module Dispatching
40
+ # The Method#parameters types that mark keyword arguments (D7).
41
+ KEYWORD_PARAM_TYPES = %i[key keyreq].freeze
42
+
43
+ private
44
+
45
+ # The action body of every query action: fresh context + fresh query
46
+ # instance per request (NFR8), return value serialized through the
47
+ # SAME policy Bucket 2 applies to ivars (D6). Encoded explicitly so a
48
+ # scalar String/nil return still renders valid JSON (`"hi"` / `null`),
49
+ # which `render json:` alone would pass through raw.
50
+ def __ruact_dispatch_query(query_method)
51
+ query_class = self.class.__ruact_query_class
52
+ query = query_class.new(QueryContext.new(controller: self))
53
+ result = query.public_send(query_method, **__ruact_query_kwargs(query, query_method))
54
+ serialized = BucketTwoPayload.serialize_value(result, strict: Ruact.config.strict_serialization)
55
+ render json: ActiveSupport::JSON.encode(serialized)
56
+ end
57
+
58
+ # D7 — minimal, best-effort param passing: only the keyword arguments
59
+ # the query method declares, read by name from the GET query params
60
+ # (values arrive as Strings). The strict FR88 sanitization contract
61
+ # (primitive allowlist, reject objects, 400 on invalid) is Story 9.5,
62
+ # coupled to the `useQuery` wire format.
63
+ def __ruact_query_kwargs(query, query_method)
64
+ query.method(query_method).parameters.each_with_object({}) do |(type, name), kwargs|
65
+ next unless KEYWORD_PARAM_TYPES.include?(type)
66
+
67
+ kwargs[name] = params[name.to_s] if params.key?(name.to_s)
68
+ end
69
+ end
70
+
71
+ # D5 — the {Ruact::Server} mutation gate returns false for GET/HEAD so
72
+ # GET *pages* keep stock Rails errors; query dispatch requests are GET
73
+ # *function calls*, so the structured 8.4 payload must render here.
74
+ # `Ruact::ConfigurationError` still re-raises — a misconfiguration is
75
+ # a loud setup failure, never a disguised runtime 500 (the same rule
76
+ # the mutation concern enforces).
77
+ def __ruact_render_structured_error?(error)
78
+ !error.is_a?(Ruact::ConfigurationError)
79
+ end
80
+ end
81
+
82
+ class << self
83
+ # Builds (or rebuilds) the dispatch controller for +query_class+ and
84
+ # installs it under this module's namespace, where the route target
85
+ # string from {.route_target_for} resolves to it.
86
+ #
87
+ # @param query_class [Class] a {Ruact::Query} subclass
88
+ # @return [Class] the generated controller
89
+ # @raise [Ruact::ConfigurationError] when the parent controller cannot
90
+ # be resolved or +query_class+ is anonymous
91
+ def controller_for(query_class)
92
+ *namespace_segments, base = constant_segments(query_class)
93
+ namespace = ensure_namespace(namespace_segments)
94
+ const_name = "#{base}Controller"
95
+ namespace.send(:remove_const, const_name) if namespace.const_defined?(const_name, false)
96
+ namespace.const_set(const_name, build_controller(query_class))
97
+ end
98
+
99
+ # The `to:` route target for +query_class+'s generated controller —
100
+ # the underscored constant path Rails camelizes back at dispatch time.
101
+ # The query class's namespace is PRESERVED (review round 4): a nested
102
+ # path, never flattened, so two classes whose names differ only in
103
+ # namespace boundary (`Admin::CatalogQuery` vs `AdminCatalogQuery`)
104
+ # map to DISTINCT controllers and can never cross-wire — collision is
105
+ # impossible by construction, across any number of RouteSets / engines.
106
+ #
107
+ # @param query_class [Class] a {Ruact::Query} subclass
108
+ # @return [String] e.g. `"ruact/server_functions/query_dispatch/admin/catalog_query"`
109
+ def route_target_for(query_class)
110
+ "ruact/server_functions/query_dispatch/#{path_segments(query_class).join('/')}"
111
+ end
112
+
113
+ private
114
+
115
+ # The underscored route-path segments for +query_class+
116
+ # (`Admin::CatalogQuery` → `["admin", "catalog_query"]`).
117
+ def path_segments(query_class)
118
+ base_segments(query_class).map(&:underscore)
119
+ end
120
+
121
+ # The generated controller's constant-name segments — derived from the
122
+ # SAME underscored path the route target uses, then `camelize`d
123
+ # (review round 5). Deriving both directions from one underscored form
124
+ # via the shared global inflector guarantees the route target Rails
125
+ # `camelize`s at dispatch time resolves to EXACTLY this constant,
126
+ # regardless of how the query class spelled an acronym or how the host
127
+ # configured `inflect.acronym` (`APIProbe::CatalogQuery` and the route
128
+ # `.../api_probe/catalog_query` both canonicalize identically). Using
129
+ # the raw class spelling instead would 404 acronym constants with the
130
+ # default inflector.
131
+ def constant_segments(query_class)
132
+ path_segments(query_class).map(&:camelize)
133
+ end
134
+
135
+ # The query class's fully-qualified name split into constant segments
136
+ # (`Admin::CatalogQuery` → `["Admin", "CatalogQuery"]`). The namespace
137
+ # is preserved so the generated controller lives at a nested,
138
+ # collision-free constant path under {QueryDispatch}.
139
+ def base_segments(query_class)
140
+ name = query_class.name
141
+ unless name
142
+ raise Ruact::ConfigurationError,
143
+ "ruact_queries cannot mount an anonymous Ruact::Query subclass — " \
144
+ "assign it to a constant (e.g. `class CatalogQuery < ApplicationQuery`)."
145
+ end
146
+
147
+ name.split("::")
148
+ end
149
+
150
+ # Walks (creating as needed) the nested module path under {QueryDispatch}
151
+ # that mirrors the query class's namespace, returning the innermost
152
+ # module the controller constant is set on. Idempotent — reuses existing
153
+ # modules so repeated draws (boot + dev reloads) never duplicate them.
154
+ def ensure_namespace(segments)
155
+ segments.reduce(self) do |mod, segment|
156
+ if mod.const_defined?(segment, false)
157
+ mod.const_get(segment, false)
158
+ else
159
+ mod.const_set(segment, Module.new)
160
+ end
161
+ end
162
+ end
163
+
164
+ # Lazy resolution of `Ruact.config.query_parent_controller` (AC2). Both
165
+ # failure shapes are configuration-time errors raised at route-draw —
166
+ # a typo'd name or a non-controller class must never reach a request.
167
+ def resolve_parent_controller
168
+ name = Ruact.config.query_parent_controller
169
+ parent = begin
170
+ name.constantize
171
+ rescue NameError
172
+ raise Ruact::ConfigurationError,
173
+ "ruact_queries: Ruact.config.query_parent_controller = #{name.inspect} does not " \
174
+ "resolve to a constant. Define that controller, or point query_parent_controller " \
175
+ "at an existing one in config/initializers/ruact.rb."
176
+ end
177
+
178
+ unless parent.is_a?(Class) && parent <= ActionController::Metal
179
+ raise Ruact::ConfigurationError,
180
+ "ruact_queries: Ruact.config.query_parent_controller = #{name.inspect} resolved to " \
181
+ "#{parent.inspect}, which is not an ActionController class."
182
+ end
183
+
184
+ parent
185
+ end
186
+
187
+ def build_controller(query_class)
188
+ query_class_name = query_class.name
189
+
190
+ # Mixins applied on the built class (not inside a `Class.new do … end`
191
+ # block) so YARD's static MixinHandler does not emit "Undocumentable
192
+ # mixin … for class" for an anonymous class body — the
193
+ # `--fail-on-warning` docs gate treats that as an error. Runtime is
194
+ # identical.
195
+ controller = Class.new(resolve_parent_controller)
196
+ controller.include(Ruact::ServerFunctions::ErrorRendering)
197
+ controller.include(Dispatching)
198
+
199
+ controller.define_singleton_method(:__ruact_query_class) { query_class_name.constantize }
200
+
201
+ # AC5 — the salvaged 8.4 error chain, with the same front-loading
202
+ # trick as Ruact::Server: handlers the parent chain registered
203
+ # (inherited OR declared later) stay more recent and keep precedence;
204
+ # the structured renderer only catches what the host did not.
205
+ inherited_handlers = controller.rescue_handlers
206
+ controller.rescue_from(StandardError, with: :__ruact_render_action_error)
207
+ controller.rescue_handlers = (controller.rescue_handlers - inherited_handlers) + inherited_handlers
208
+
209
+ define_query_actions(controller, query_class)
210
+ apply_skips(controller, query_class)
211
+ controller
212
+ end
213
+
214
+ # Review round 1 (finding 1) — a query method whose name already exists
215
+ # anywhere on the generated controller chain (`params`, `render`,
216
+ # `session`, `process`, the gem's own `__ruact_*` plumbing, …) would
217
+ # OVERRIDE that method when installed as an action, corrupting request
218
+ # handling (e.g. `def params` shadows `ActionController#params` and
219
+ # recurses through the dispatch path). Reject at route-draw with a
220
+ # legible error instead of failing at the first request.
221
+ def define_query_actions(controller, query_class)
222
+ query_class.public_instance_methods(false).each do |query_method|
223
+ if controller.method_defined?(query_method) || controller.private_method_defined?(query_method)
224
+ raise Ruact::ConfigurationError,
225
+ "ruact_queries: query method :#{query_method} on #{query_class.name} is already " \
226
+ "defined on the dispatch controller chain (#{controller.superclass.name} / " \
227
+ "ActionController / ruact plumbing) and would shadow it — rename the query method."
228
+ end
229
+
230
+ controller.define_method(query_method) do
231
+ __ruact_dispatch_query(query_method)
232
+ end
233
+ end
234
+ end
235
+
236
+ # AC4 / D1 — forwards every recorded `ruact_skip_before_action` to
237
+ # Rails' own `skip_before_action` on the generated controller. An
238
+ # unknown callback raises here (route-draw time) unless the query
239
+ # passed `raise: false`, mirroring stock Rails behavior.
240
+ def apply_skips(controller, query_class)
241
+ query_class.__ruact_skipped_callbacks.each do |callbacks, options|
242
+ controller.skip_before_action(*callbacks, **options)
243
+ end
244
+ end
245
+ end
246
+ end
247
+ end
248
+ end
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruact
4
+ module ServerFunctions
5
+ # In-memory storage for server-function entries. One instance backs
6
+ # `Ruact.action_registry`; another backs `Ruact.query_registry` — kept
7
+ # separate so the JSON snapshot can emit a `kind` field per entry without
8
+ # the call sites having to thread an extra parameter through. Cross-registry
9
+ # JS-identifier collisions are detected by {Ruact::ServerFunctions::Snapshot}
10
+ # at snapshot time (a single registry only sees its own entries).
11
+ #
12
+ # Thread-safety: not thread-safe by design. Registration happens at
13
+ # controller-class load time (`config.to_prepare` in dev, eager-load in
14
+ # production), single-threaded. Reads from {#entries} return a frozen
15
+ # snapshot of the internal hash so concurrent readers cannot observe a
16
+ # partial registration.
17
+ class Registry
18
+ # The only kinds the codegen knows how to emit. Story 8.1 owns `:action`,
19
+ # Story 9.1 owns `:query`. Any other value is rejected at registration
20
+ # time — silent acceptance would otherwise let an unknown kind fall
21
+ # through and be emitted as an action signature.
22
+ ALLOWED_KINDS = %i[action query].freeze
23
+
24
+ def initialize
25
+ @entries = {}
26
+ end
27
+
28
+ # Adds +symbol+ to the registry.
29
+ #
30
+ # @param symbol [Symbol] the Ruby identifier (snake_case).
31
+ # @param kind [Symbol] `:action` or `:query`. Other values raise.
32
+ # @param controller [Class, nil] the controller class registering the
33
+ # function. Used in collision-error messages.
34
+ # @yield the implementation body; stored verbatim for Story 8.1 / 9.1 to
35
+ # invoke. May be nil for Story 8.0a's bootstrap (registries are empty
36
+ # until 8.1 and 9.1 land).
37
+ # @return [Ruact::ServerFunctions::RegistryEntry] the entry just inserted.
38
+ # @raise [Ruact::ConfigurationError] when +symbol+ fails the naming-bridge
39
+ # rule, when +kind+ is not in {ALLOWED_KINDS}, or when a different Ruby
40
+ # symbol already maps to the same JS identifier in THIS registry. Cross-
41
+ # registry collisions (one action + one query sharing a JS identifier)
42
+ # are detected later by {Ruact::ServerFunctions::Snapshot.functions_payload}.
43
+ def register(symbol, kind:, controller: nil, &block)
44
+ validate_kind!(symbol, kind, controller)
45
+ js_identifier = translate_symbol(symbol, controller)
46
+ detect_collision!(symbol, js_identifier, controller)
47
+
48
+ entry = RegistryEntry.new(
49
+ ruby_symbol: symbol,
50
+ js_identifier: js_identifier,
51
+ kind: kind,
52
+ controller: controller,
53
+ block: block
54
+ )
55
+ @entries[symbol] = entry
56
+ entry
57
+ end
58
+
59
+ # @return [Hash{Symbol => Ruact::ServerFunctions::RegistryEntry}] frozen
60
+ # snapshot of the current entries, ordered by insertion.
61
+ def entries
62
+ @entries.dup.freeze
63
+ end
64
+
65
+ # Wipes the registry. Used by `config.to_prepare` (between dev reloads) and
66
+ # by tests that need a clean slate.
67
+ #
68
+ # @return [self]
69
+ def clear!
70
+ @entries.clear
71
+ self
72
+ end
73
+
74
+ # @return [Integer] number of registered entries.
75
+ def size
76
+ @entries.size
77
+ end
78
+
79
+ # @return [Boolean] whether the registry has no entries.
80
+ def empty?
81
+ @entries.empty?
82
+ end
83
+
84
+ private
85
+
86
+ def validate_kind!(symbol, kind, controller)
87
+ return if ALLOWED_KINDS.include?(kind)
88
+
89
+ raise Ruact::ConfigurationError,
90
+ "invalid server-function symbol :#{symbol} in #{describe_controller(controller)}: " \
91
+ "kind #{kind.inspect} is not one of #{ALLOWED_KINDS.inspect}"
92
+ end
93
+
94
+ # Wraps the NameBridge call to attach controller context to the raised
95
+ # error (the AC7 "invalid server-function symbol :SYMBOL in CONTROLLER"
96
+ # shape). NameBridge itself is controller-agnostic; the wrap lives at the
97
+ # registry boundary because that is where controller context exists.
98
+ def translate_symbol(symbol, controller)
99
+ NameBridge.to_js_identifier(symbol)
100
+ rescue Ruact::ConfigurationError => e
101
+ raise Ruact::ConfigurationError,
102
+ "invalid server-function symbol :#{symbol} in #{describe_controller(controller)} — #{e.message}"
103
+ end
104
+
105
+ def detect_collision!(symbol, js_identifier, controller)
106
+ # Re-run-3 (2026-05-15) — TWO failure shapes:
107
+ #
108
+ # (a) Different Ruby symbols, same JS identifier (`:foo_bar` and
109
+ # `:fooBar` both → "fooBar"). Filtered by `js_identifier ==`.
110
+ # (b) Same Ruby symbol declared on TWO different controllers
111
+ # (e.g., `ruact_action :create_post` in both `PostsController`
112
+ # AND `AdminPostsController`). Pre-batch this silently
113
+ # overwrote `@entries[symbol]` with the last-loaded one, so
114
+ # dispatch routed to whichever controller Zeitwerk happened
115
+ # to load last — non-deterministic in dev, surprise breakage
116
+ # when refactoring. Detect by checking the existing entry's
117
+ # `controller` against the one trying to register.
118
+ existing = @entries[symbol]
119
+ if existing && existing.controller != controller
120
+ raise Ruact::ConfigurationError,
121
+ "server-function naming collision: " \
122
+ ":#{symbol} is declared in BOTH " \
123
+ "#{describe_controller(existing.controller)} and " \
124
+ "#{describe_controller(controller)}. Each `ruact_action` " \
125
+ "symbol must be unique across the whole registry — pick a " \
126
+ "more specific name (e.g. :admin_create_post) on one side."
127
+ end
128
+
129
+ collision = @entries.values.find do |e|
130
+ e.js_identifier == js_identifier && e.ruby_symbol != symbol
131
+ end
132
+ return unless collision
133
+
134
+ raise Ruact::ConfigurationError,
135
+ "server-function naming collision: " \
136
+ ":#{symbol} (in #{describe_controller(controller)}) and " \
137
+ ":#{collision.ruby_symbol} (in #{describe_controller(collision.controller)}) " \
138
+ "both map to JS identifier \"#{js_identifier}\""
139
+ end
140
+
141
+ def describe_controller(controller)
142
+ return "unknown controller" if controller.nil?
143
+
144
+ controller.respond_to?(:name) && controller.name ? controller.name : controller.inspect
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruact
4
+ module ServerFunctions
5
+ # Immutable record describing a single registered server function. Stored by
6
+ # {Ruact::ServerFunctions::Registry}; serialized into the JSON snapshot by
7
+ # {Ruact::ServerFunctions::Snapshot}.
8
+ #
9
+ # @!attribute [r] ruby_symbol
10
+ # @return [Symbol] the symbol the controller registered (e.g. `:create_post`).
11
+ # @!attribute [r] js_identifier
12
+ # @return [String] result of {Ruact::ServerFunctions::NameBridge.to_js_identifier}
13
+ # — cached at registration time so the snapshot writer never re-derives it.
14
+ # @!attribute [r] kind
15
+ # @return [Symbol] `:action` or `:query`. Informational at codegen time
16
+ # (Story 8.0 decision 2A-i: both kinds POST through the same accessor).
17
+ # @!attribute [r] controller
18
+ # @return [Class, nil] the controller class that registered the function;
19
+ # used for collision-error messages and downstream tooling. Nil is allowed
20
+ # for tests / Rails-console registrations.
21
+ # @!attribute [r] block
22
+ # @return [Proc, nil] the implementation block supplied by the controller
23
+ # macro. Story 8.0a stores it untouched; Stories 8.1 / 9.1 invoke it.
24
+ RegistryEntry = Data.define(:ruby_symbol, :js_identifier, :kind, :controller, :block)
25
+ end
26
+ end