instant_record 0.1.0 → 0.3.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 (198) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +58 -0
  3. data/README.md +267 -61
  4. data/app/controllers/instant_record/bootstraps_controller.rb +39 -12
  5. data/app/controllers/instant_record/events_controller.rb +58 -6
  6. data/app/controllers/instant_record/files_controller.rb +116 -0
  7. data/app/controllers/instant_record/mutations_controller.rb +84 -5
  8. data/app/controllers/instant_record/native_screens_controller.rb +60 -0
  9. data/app/controllers/instant_record/records_controller.rb +86 -8
  10. data/app/controllers/instant_record/shells_controller.rb +64 -0
  11. data/app/controllers/instant_record/sync_notices_controller.rb +81 -0
  12. data/app/helpers/instant_record/sync_notices_helper.rb +41 -0
  13. data/app/jobs/instant_record/shell_build_job.rb +16 -0
  14. data/app/models/instant_record/change.rb +36 -3
  15. data/app/models/instant_record/mutation_applier.rb +633 -50
  16. data/app/views/instant_record/sync_notices/_notices.html.erb +63 -0
  17. data/compiled/README.md +272 -0
  18. data/compiled/pipeline/browser_migrations.rb +18 -0
  19. data/compiled/pipeline/build.rb +13 -0
  20. data/compiled/pipeline/build_native.rb +14 -0
  21. data/compiled/pipeline/native/host.c +78 -0
  22. data/compiled/pipeline/native/instant_record_native.h +57 -0
  23. data/compiled/pipeline/native_shim.c +149 -0
  24. data/compiled/pipeline/reactor_shim.c +66 -0
  25. data/compiled/pipeline/sqlite/rh_shim.c +5 -0
  26. data/compiled/pipeline/sqlite_release.rb +43 -0
  27. data/compiled/toolchain.env.example +22 -0
  28. data/compiled/web/index.html +20 -0
  29. data/compiled/web/package-lock.json +884 -0
  30. data/compiled/web/package.json +17 -0
  31. data/compiled/web/src/app-host-core.js +556 -0
  32. data/compiled/web/src/app-host.js +16 -0
  33. data/compiled/web/src/branch-manager.js +140 -0
  34. data/compiled/web/src/branch-preview.js +397 -0
  35. data/compiled/web/src/changes.js +191 -0
  36. data/compiled/web/src/faults.js +172 -0
  37. data/compiled/web/src/flight-recorder.js +347 -0
  38. data/compiled/web/src/history-fill.js +68 -0
  39. data/compiled/web/src/http-headers.js +100 -0
  40. data/compiled/web/src/leader-election.js +705 -0
  41. data/compiled/web/src/main.js +608 -0
  42. data/compiled/web/src/manifest.js +112 -0
  43. data/compiled/web/src/opfs-wasi-core.js +109 -0
  44. data/compiled/web/src/opfs-wasi-file.js +8 -0
  45. data/compiled/web/src/reactor-runtime.js +1692 -0
  46. data/compiled/web/src/redactor.js +518 -0
  47. data/compiled/web/src/release-updates.js +48 -0
  48. data/compiled/web/src/replay.js +338 -0
  49. data/compiled/web/src/schema-recovery.js +204 -0
  50. data/compiled/web/src/server-request.js +68 -0
  51. data/compiled/web/src/sha256.js +98 -0
  52. data/compiled/web/src/shell.js +52 -0
  53. data/compiled/web/src/snapshot-store.js +38 -0
  54. data/compiled/web/src/sqlite-redactor.js +111 -0
  55. data/compiled/web/src/storage-backend.js +159 -0
  56. data/compiled/web/src/stream-leader.js +675 -0
  57. data/compiled/web/src/sync-context.js +91 -0
  58. data/compiled/web/src/sync-drain.js +104 -0
  59. data/compiled/web/src/sync-schedule.js +9 -0
  60. data/compiled/web/src/wasi-minimal.js +275 -0
  61. data/compiled/web/src/worker.js +39 -0
  62. data/compiled/web/vite.config.js +27 -0
  63. data/config/routes.rb +25 -0
  64. data/db/migrate/20261119000201_add_scope_to_instant_record_changes.rb +7 -0
  65. data/db/migrate/20261121000001_create_instant_record_sync_notices.rb +21 -0
  66. data/db/migrate/20261121000101_add_partition_to_instant_record_changes.rb +8 -0
  67. data/db/migrate/20261121230300_add_delay_line_to_instant_record_outbox.rb +10 -0
  68. data/exe/instant_record_mcp +6 -0
  69. data/frontend/LiveList.js +67 -0
  70. data/frontend/chord.js +79 -0
  71. data/frontend/file.js +65 -0
  72. data/frontend/files.js +738 -0
  73. data/frontend/focus.js +10 -0
  74. data/frontend/held.js +192 -0
  75. data/frontend/http.js +76 -0
  76. data/frontend/index.js +53 -0
  77. data/frontend/keyboard.js +56 -0
  78. data/frontend/live.js +298 -0
  79. data/frontend/notices.js +118 -0
  80. data/frontend/package.json +15 -0
  81. data/frontend/range.js +10 -0
  82. data/frontend/selection.js +47 -0
  83. data/frontend/shortcuts.js +25 -0
  84. data/frontend/sync-notices.js +130 -0
  85. data/frontend/undo.js +48 -0
  86. data/frontend/window.js +181 -0
  87. data/lib/generators/instant_record/install/install_generator.rb +18 -30
  88. data/lib/generators/instant_record/install/templates/instant_record.rb +34 -0
  89. data/lib/generators/instant_record/ios/ios_generator.rb +71 -0
  90. data/lib/generators/instant_record/ios/templates/App.swift.tt +35 -0
  91. data/lib/generators/instant_record/ios/templates/Info.plist +11 -0
  92. data/lib/generators/instant_record/ios/templates/LibraryEngine.swift +30 -0
  93. data/lib/instant_record/branch/sqlite_store.rb +142 -0
  94. data/lib/instant_record/branch.rb +388 -0
  95. data/lib/instant_record/client/notifier.rb +8 -12
  96. data/lib/instant_record/client/rebaser.rb +565 -0
  97. data/lib/instant_record/client/transport.rb +64 -42
  98. data/lib/instant_record/client.rb +1600 -169
  99. data/lib/instant_record/compiled_site.rb +85 -0
  100. data/lib/instant_record/compiler/app_stage.rb +212 -0
  101. data/lib/instant_record/compiler/ast_lowerer.rb +659 -0
  102. data/lib/instant_record/compiler/browser_migrations.rb +161 -0
  103. data/lib/instant_record/compiler/entry_builder.rb +832 -0
  104. data/lib/instant_record/compiler/fts5_smoke.rb +205 -0
  105. data/lib/instant_record/compiler/gem_vendor.rb +194 -0
  106. data/lib/instant_record/compiler/host_adapters.rb +506 -0
  107. data/lib/instant_record/compiler/kotlin_codegen.rb +209 -0
  108. data/lib/instant_record/compiler/manifest.rb +178 -0
  109. data/lib/instant_record/compiler/native_build.rb +586 -0
  110. data/lib/instant_record/compiler/native_contracts.rb +338 -0
  111. data/lib/instant_record/compiler/path_scrubber.rb +31 -0
  112. data/lib/instant_record/compiler/reachability_tracer.rb +1188 -0
  113. data/lib/instant_record/compiler/route_analyzer.rb +623 -0
  114. data/lib/instant_record/compiler/runner.rb +42 -0
  115. data/lib/instant_record/compiler/schema_lock.rb +606 -0
  116. data/lib/instant_record/compiler/source_pages.rb +77 -0
  117. data/lib/instant_record/compiler/static_linter.rb +770 -0
  118. data/lib/instant_record/compiler/swift_codegen.rb +341 -0
  119. data/lib/instant_record/compiler/toolchain.rb +219 -0
  120. data/lib/instant_record/compiler/wasm_build.rb +704 -0
  121. data/lib/instant_record/compiler/wasm_tree.rb +518 -0
  122. data/lib/instant_record/compiler.rb +277 -0
  123. data/lib/instant_record/configuration.rb +104 -0
  124. data/lib/instant_record/engine.rb +68 -18
  125. data/lib/instant_record/file_store.rb +162 -0
  126. data/lib/instant_record/files.rb +92 -0
  127. data/lib/instant_record/hlc.rb +387 -0
  128. data/lib/instant_record/host.rb +66 -0
  129. data/lib/instant_record/inertia/renderer.rb +52 -0
  130. data/lib/instant_record/inertia.rb +222 -0
  131. data/lib/instant_record/mcp/cli.rb +319 -0
  132. data/lib/instant_record/mcp/host_process.rb +229 -0
  133. data/lib/instant_record/mcp/http_exchange.rb +233 -0
  134. data/lib/instant_record/mcp/replica.rb +556 -0
  135. data/lib/instant_record/mcp/runtime.rb +406 -0
  136. data/lib/instant_record/mcp/server.rb +429 -0
  137. data/lib/instant_record/mcp.rb +47 -0
  138. data/lib/instant_record/native/contract.rb +367 -0
  139. data/lib/instant_record/native/node.rb +125 -0
  140. data/lib/instant_record/native/screen.rb +366 -0
  141. data/lib/instant_record/native/xcode_project.rb +344 -0
  142. data/lib/instant_record/native.rb +41 -0
  143. data/lib/instant_record/outbox_mutation.rb +81 -9
  144. data/lib/instant_record/outbox_upcaster.rb +161 -0
  145. data/lib/instant_record/read_tracker.rb +610 -0
  146. data/lib/instant_record/routing.rb +33 -0
  147. data/lib/instant_record/runtime_scoped.rb +4 -0
  148. data/lib/instant_record/shell.rb +486 -0
  149. data/lib/instant_record/sync_actions.rb +800 -0
  150. data/lib/instant_record/sync_metadata.rb +17 -2
  151. data/lib/instant_record/sync_notice.rb +234 -0
  152. data/lib/instant_record/sync_partition.rb +63 -0
  153. data/lib/instant_record/sync_scope.rb +300 -0
  154. data/lib/instant_record/sync_shape.rb +102 -0
  155. data/lib/instant_record/sync_window.rb +92 -15
  156. data/lib/instant_record/syncable.rb +313 -17
  157. data/lib/instant_record/testing/authoritative_server.rb +547 -0
  158. data/lib/instant_record/testing/chaos_simulator.rb +429 -0
  159. data/lib/instant_record/testing/cruby_host_bridge.rb +370 -0
  160. data/lib/instant_record/testing/fault_network.rb +140 -0
  161. data/lib/instant_record/testing/parity_suite.rb +435 -0
  162. data/lib/instant_record/testing/runtimes/base.rb +184 -0
  163. data/lib/instant_record/testing/runtimes/cruby_runtime.rb +255 -0
  164. data/lib/instant_record/testing/runtimes/native_runtime.rb +346 -0
  165. data/lib/instant_record/testing/runtimes/wasm_runtime.rb +371 -0
  166. data/lib/instant_record/testing/wasm_host_bridge.mjs +240 -0
  167. data/lib/instant_record/testing.rb +333 -0
  168. data/lib/instant_record/trace/redactor.rb +278 -0
  169. data/lib/instant_record/trace/replayer.rb +126 -0
  170. data/lib/instant_record/trace/schema_extractor.rb +37 -0
  171. data/lib/instant_record/trace.rb +22 -0
  172. data/lib/instant_record/version.rb +1 -1
  173. data/lib/instant_record.rb +107 -37
  174. data/lib/tasks/instant_record.rake +299 -47
  175. data/native/ios/InstantRecordNative/Package.swift +12 -0
  176. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/AppHost.swift +706 -0
  177. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Engine.swift +64 -0
  178. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/FileStore.swift +1048 -0
  179. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/HTTPEngine.swift +36 -0
  180. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Icons.swift +19 -0
  181. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Node.swift +64 -0
  182. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/ScreenView.swift +481 -0
  183. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Screens.swift +94 -0
  184. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/StreamClient.swift +74 -0
  185. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncNotice.swift +282 -0
  186. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncSchedule.swift +153 -0
  187. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Theme.swift +140 -0
  188. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Transport.swift +108 -0
  189. data/native/ios/InstantRecordNative/Sources/InstantRecordNative/TypedContracts.swift +237 -0
  190. data/native/ios/README.md +341 -0
  191. data/sig/instant_record/inertia.rbs +47 -0
  192. data/sig/instant_record/native.rbs +38 -0
  193. metadata +194 -18
  194. data/lib/generators/instant_record/install/templates/database.js +0 -32
  195. data/lib/generators/instant_record/install/templates/index.html +0 -69
  196. data/lib/generators/instant_record/install/templates/rails.sw.js +0 -750
  197. data/lib/instant_record/local_schema.rb +0 -75
  198. data/lib/instant_record/pglite_compat.rb +0 -40
@@ -0,0 +1,518 @@
1
+ require "fileutils"
2
+ require_relative "app_stage"
3
+ require_relative "runner"
4
+
5
+ module InstantRecord
6
+ class Compiler
7
+ # Copies an emitted Spinel project and adapts it into a single-threaded tree
8
+ # suitable for wasm32-wasi and the native static library.
9
+ class WasmTree
10
+ IGNORE_PATTERNS = %w[build storage node_modules e2e test].freeze
11
+ THREAD_CONSTANTS = %w[Thread Mutex Queue ConditionVariable Fiber].freeze
12
+ THREAD_CONSTANT_RE = /\b(?:#{THREAD_CONSTANTS.join('|')})\b/
13
+
14
+ WASM_THREADS_RB = <<~'RUBY'
15
+ # Hand-written (see WasmTree): single-threaded stand-ins for the threading classes the
16
+ # emitted runtime uses. One wasm run handles one request on one thread, so a lock has nothing to
17
+ # exclude, a thread-local is a plain slot, and a background thread is never started.
18
+ class WasmThread
19
+ def self.current
20
+ @current ||= WasmThread.new
21
+ end
22
+
23
+ def self.pass
24
+ nil
25
+ end
26
+
27
+ def initialize(a = nil, b = nil, &block)
28
+ @locals = {}
29
+ end
30
+
31
+ def [](key)
32
+ @locals[key]
33
+ end
34
+
35
+ def []=(key, value)
36
+ @locals[key] = value
37
+ end
38
+
39
+ def key?(key)
40
+ @locals.key?(key)
41
+ end
42
+
43
+ def join(limit = nil)
44
+ self
45
+ end
46
+
47
+ def alive?
48
+ false
49
+ end
50
+
51
+ def kill
52
+ self
53
+ end
54
+ end
55
+
56
+ # WASI has no stack switching, so a Fiber cannot be created. The runtime only builds them for its
57
+ # fiber-per-connection server, which a one-shot run never starts.
58
+ class WasmFiber
59
+ def self.yield(value = nil)
60
+ nil
61
+ end
62
+
63
+ def initialize(&block)
64
+ @started = false
65
+ end
66
+
67
+ def resume(value = nil)
68
+ nil
69
+ end
70
+
71
+ def alive?
72
+ false
73
+ end
74
+ end
75
+
76
+ class WasmMutex
77
+ def synchronize
78
+ yield
79
+ end
80
+
81
+ def lock
82
+ self
83
+ end
84
+
85
+ def unlock
86
+ self
87
+ end
88
+
89
+ def locked?
90
+ false
91
+ end
92
+
93
+ def owned?
94
+ true
95
+ end
96
+
97
+ def try_lock
98
+ true
99
+ end
100
+ end
101
+
102
+ class WasmConditionVariable
103
+ def wait(mutex = nil, timeout = nil)
104
+ self
105
+ end
106
+
107
+ def signal
108
+ self
109
+ end
110
+
111
+ def broadcast
112
+ self
113
+ end
114
+ end
115
+
116
+ module Timeout
117
+ class Error < RuntimeError
118
+ end
119
+
120
+ def self.timeout(sec, klass = nil, message = nil)
121
+ yield(sec)
122
+ end
123
+ end
124
+ RUBY
125
+
126
+ AES_STUB = <<~'RUBY'
127
+ def self.aes_256_gcm_encrypt(key, iv, plain)
128
+ raise NotImplementedError, "AES-256-GCM is not available in the wasm build"
129
+ end
130
+
131
+ def self.aes_256_gcm_decrypt(key, iv, ct, tag)
132
+ raise NotImplementedError, "AES-256-GCM is not available in the wasm build"
133
+ end
134
+ end
135
+ RUBY
136
+
137
+ CHANGED_METHOD = <<~'RUBY'
138
+ def changed
139
+ changes = changes_to_save
140
+ changes.keys.map { |name| name.to_s }
141
+ end
142
+
143
+ RUBY
144
+
145
+ PRUNED_STUBS = {
146
+ "runtime/action_text.rb" => "module ActionText\nend\n",
147
+ "runtime/action_text_json.rb" => "",
148
+ "runtime/nokogiri_spinel.rb" => "",
149
+ "runtime/rails_html_sanitizer_spinel.rb" => "",
150
+ "runtime/active_storage.rb" => <<~'RUBY',
151
+ module ActiveStorage
152
+ module Routes
153
+ def self.table
154
+ []
155
+ end
156
+
157
+ def self.instantiate_controller(_sym)
158
+ nil
159
+ end
160
+ end
161
+
162
+ module Previewer
163
+ class VideoPreviewer
164
+ end
165
+ end
166
+ end
167
+ RUBY
168
+ "runtime/active_storage_disk.rb" => "",
169
+ "runtime/active_storage_processor.rb" => "",
170
+ "runtime/active_storage_previewer.rb" => "",
171
+ "runtime/multipart.rb" => "",
172
+ "runtime/cable.rb" => "module Cable\n def self.upgrade(_req, _res)\n nil\n end\nend\n",
173
+ "runtime/action_cable.rb" => "",
174
+ "runtime/turbo_streams.rb" => "",
175
+ "runtime/redis_cable.rb" => "",
176
+ "runtime/faye_websocket.rb" => "module Faye\n class WebSocket\n end\nend\n",
177
+ "runtime/sidekiq_redis.rb" => "",
178
+ "runtime/sidekiq_envelope.rb" => "",
179
+ "runtime/sidekiq_worker.rb" => "",
180
+ "runtime/sidekiq_push.rb" => <<~'RUBY',
181
+ module SidekiqPush
182
+ def self.flush_held
183
+ nil
184
+ end
185
+
186
+ def self.drop_held
187
+ nil
188
+ end
189
+
190
+ def self.enqueue(_name, _queue, _at, _payload)
191
+ nil
192
+ end
193
+ end
194
+ RUBY
195
+ "runtime/solid_queue.rb" => "",
196
+ "runtime/solid_queue_standin.rb" => "",
197
+ "runtime/active_job_serialization.rb" => "",
198
+ "runtime/integration_session.rb" => "",
199
+ "runtime/active_record/tasks.rb" => <<~'RUBY',
200
+ module ActiveRecord
201
+ module Tasks
202
+ module DatabaseTasks
203
+ def self.current_db_config
204
+ nil
205
+ end
206
+
207
+ def self.replace_configs(_configs)
208
+ nil
209
+ end
210
+ end
211
+ end
212
+ end
213
+ RUBY
214
+ "runtime/active_record/migration_runner.rb" => "",
215
+ "runtime/active_record/schema_reader.rb" => "",
216
+ "runtime/rake/task.rb" => "",
217
+ "runtime/active_record/friendly_id.rb" => "",
218
+ "runtime/active_record/signed_id.rb" => "",
219
+ "runtime/active_record/token_for.rb" => "",
220
+ "runtime/action_controller_page_caching.rb" => <<~'RUBY',
221
+ module ActionController
222
+ class Base
223
+ def self.page_cache_directory
224
+ ""
225
+ end
226
+
227
+ def self.perform_caching
228
+ false
229
+ end
230
+ end
231
+ end
232
+ RUBY
233
+ "runtime/action_controller_fragment_caching.rb" => "",
234
+ "runtime/action_controller_json_render.rb" => "",
235
+ "runtime/fragment_cache.rb" => "",
236
+ "runtime/active_support_cache.rb" => "",
237
+ "runtime/active_support_reporters.rb" => "",
238
+ "runtime/i18n_locale.rb" => "",
239
+ "runtime/schematized_json.rb" => "",
240
+ "runtime/user_agent.rb" => <<~'RUBY',
241
+ module UserAgent
242
+ def self.parse(_ua)
243
+ nil
244
+ end
245
+
246
+ def self.version_below?(_reported, _floor)
247
+ false
248
+ end
249
+ end
250
+ RUBY
251
+ "runtime/active_support_number_helper.rb" => <<~'RUBY',
252
+ module ActiveSupport
253
+ module NumberHelper
254
+ def self.symbolize_options(opts = {})
255
+ opts
256
+ end
257
+
258
+ def self.number_to_currency(n, _opts = {}); n.to_s; end
259
+ def self.number_to_delimited(n, _opts = {}); n.to_s; end
260
+ def self.number_to_human(n, _opts = {}); n.to_s; end
261
+ def self.number_to_human_size(n, _opts = {}); n.to_s; end
262
+ def self.number_to_percentage(n, _opts = {}); n.to_s; end
263
+ def self.number_to_phone(n, _opts = {}); n.to_s; end
264
+ def self.number_to_rounded(n, _opts = {}); n.to_s; end
265
+ end
266
+ end
267
+ RUBY
268
+ "runtime/tep/server.rb" => "",
269
+ "runtime/tep/server_threaded.rb" => "",
270
+ "runtime/tep/server_scheduled.rb" => "",
271
+ "runtime/tep/unix_listener.rb" => "",
272
+ "runtime/tep/thruster_cache.rb" => <<~'RUBY',
273
+ module Tep
274
+ class ThrusterCache
275
+ def answer(_req, _res)
276
+ false
277
+ end
278
+
279
+ def store(_req, _res)
280
+ nil
281
+ end
282
+ end
283
+
284
+ THRUSTER_CACHE = ThrusterCache.new
285
+ end
286
+ RUBY
287
+ "runtime/tep/presence.rb" => "",
288
+ "runtime/tep/scheduler.rb" => <<~'RUBY',
289
+ module Tep
290
+ class FiberSlot
291
+ def initialize(_fiber)
292
+ end
293
+ end
294
+
295
+ module Scheduler
296
+ READ = 1
297
+
298
+ def self.io_wait(_fd, _mode, _timeout)
299
+ 0
300
+ end
301
+ end
302
+ end
303
+ RUBY
304
+ "runtime/tep/broadcast.rb" => <<~'RUBY',
305
+ module Tep
306
+ module Broadcast
307
+ def self.subscribers_for(_topic)
308
+ []
309
+ end
310
+ end
311
+ end
312
+ RUBY
313
+ "runtime/tep/websocket.rb" => <<~'RUBY'
314
+ module Tep
315
+ module WebSocket
316
+ class Driver
317
+ def initialize(_fd = 0)
318
+ end
319
+ end
320
+ end
321
+ end
322
+ RUBY
323
+ }.freeze
324
+
325
+ def self.build(source:, target:, demo: false)
326
+ new(source: source, target: target, demo: demo).run
327
+ end
328
+
329
+ def initialize(source:, target:, demo: false)
330
+ @source = source.to_s
331
+ @target = target.to_s
332
+ @demo = demo
333
+ end
334
+
335
+ def run
336
+ FileUtils.rm_rf(@target)
337
+ FileCopy.copy_tree(@source, @target, ignore: IGNORE_PATTERNS)
338
+
339
+ paths = runtime_paths
340
+ touched = adapt_threading_and_timeout(paths)
341
+ patch_message_digest
342
+ patch_sqlite_shims
343
+ patch_read_tracking
344
+ patch_model_datetime_casts
345
+ patch_model_remote_timestamps
346
+ patch_active_record_changed
347
+ patch_emitted_views
348
+ patch_slack_sidebar if @demo
349
+ prepend_wasm_threads_require
350
+ pruned_lines = @demo ? prune_dead_subsystems : 0
351
+
352
+ { touched: touched, pruned_lines: pruned_lines }
353
+ end
354
+
355
+ private
356
+
357
+ def runtime_paths
358
+ paths = %w[boot.rb main_wasm.rb].map { |name| File.join(@target, name) }
359
+ %w[runtime config].each do |folder|
360
+ dir = File.join(@target, folder)
361
+ next unless File.directory?(dir)
362
+
363
+ paths.concat(Dir.glob("**/*.rb", base: dir).sort.map { |rel| File.join(dir, rel) })
364
+ end
365
+ paths
366
+ end
367
+
368
+ def adapt_threading_and_timeout(paths)
369
+ touched = 0
370
+ paths.each do |path|
371
+ text = File.read(path)
372
+ renamed = text.gsub(THREAD_CONSTANT_RE, 'Wasm\0')
373
+ touched += 1 if renamed != text
374
+ updated = renamed.gsub('require "timeout"', '# require "timeout" (wasm: see runtime/wasm_threads.rb)')
375
+ File.write(path, updated) if updated != text
376
+ end
377
+ File.write(File.join(@target, "runtime/wasm_threads.rb"), WASM_THREADS_RB)
378
+ touched
379
+ end
380
+
381
+ def patch_message_digest
382
+ path = File.join(@target, "runtime/message_digest.rb")
383
+ text = File.read(path)
384
+ start = text.index(" def self.aes_256_gcm_encrypt")
385
+ raise BuildError, "Expected aes_256_gcm_encrypt in #{path}" unless start
386
+
387
+ head = text[0...start].gsub('require "openssl"', '# require "openssl" (wasm: no OpenSSL build)')
388
+ File.write(path, head + AES_STUB)
389
+ end
390
+
391
+ def patch_sqlite_shims
392
+ path = File.join(@target, "runtime/db.rb")
393
+ text = File.read(path)
394
+ %w[sqlite3_bind_int64 sqlite3_last_insert_rowid].each do |name|
395
+ text = text.gsub(name, "rh_#{name}")
396
+ end
397
+ File.write(path, text)
398
+ end
399
+
400
+ def patch_read_tracking
401
+ path = File.join(@target, "runtime/sqlite_adapter.rb")
402
+ return unless File.file?(path)
403
+
404
+ text = File.read(path)
405
+ hook = "InstantRecord::ReadTracker.record_sql(sql)"
406
+ return if text.include?(hook)
407
+
408
+ updated = text.gsub(
409
+ /^(\s*def (?:self\.)?(?:each_row|select_all|select_rows|select_value|select_values|query|exec_query)\(sql\b[^\n]*\)\n)/
410
+ ) do
411
+ line = Regexp.last_match(1)
412
+ indent = line[/\A(\s*)/, 1]
413
+ "#{line}#{indent} #{hook}\n"
414
+ end
415
+ File.write(path, updated) if updated != text
416
+ end
417
+
418
+ def patch_model_datetime_casts
419
+ models_dir = File.join(@target, "app/models")
420
+ return unless File.directory?(models_dir)
421
+
422
+ Dir.glob("**/*.rb", base: models_dir).sort.each do |rel|
423
+ path = File.join(models_dir, rel)
424
+ text = File.read(path)
425
+ fixed = text.gsub(
426
+ /(if name == "(\w+_at)"\n\s+if respond_to\?\(:\2=\)\n\s+self\.\2 = )value\n/,
427
+ '\1ActiveSupport.cast_datetime(value)' + "\n"
428
+ )
429
+ File.write(path, fixed) if fixed != text
430
+ end
431
+ end
432
+
433
+ def patch_model_remote_timestamps
434
+ models_dir = File.join(@target, "app/models")
435
+ return unless File.directory?(models_dir)
436
+
437
+ Dir.glob("**/*.rb", base: models_dir).sort.each do |rel|
438
+ path = File.join(models_dir, rel)
439
+ text = File.read(path)
440
+ # Roundhouse's generated fill_timestamps unconditionally stamps
441
+ # updated_at = db_now on every UPDATE, clobbering the server's
442
+ # authoritative timestamp during upsert_from_server. Preserve the
443
+ # incoming server timestamp while applying_remote? is active.
444
+ fixed = text.gsub(
445
+ "if !(creating) || @updated_at_raw.nil? || @updated_at_raw.empty?",
446
+ "if (!(creating) && !InstantRecord::Client.applying_remote?) || @updated_at_raw.nil? || @updated_at_raw.empty?"
447
+ )
448
+ File.write(path, fixed) if fixed != text
449
+ end
450
+ end
451
+
452
+ def patch_active_record_changed
453
+ path = File.join(@target, "runtime/active_record/base.rb")
454
+ text = File.read(path)
455
+ anchor = " def has_changes_to_save?\n"
456
+ raise BuildError, "Expected #{anchor.strip} in #{path}" unless text.include?(anchor)
457
+
458
+ File.write(path, text.sub(anchor) { CHANGED_METHOD + anchor })
459
+ end
460
+
461
+ def patch_emitted_views
462
+ views_dir = File.join(@target, "app/views")
463
+ return unless File.directory?(views_dir)
464
+
465
+ bogus = '#{ActionView::ViewHelpers.tag_attr("form", { data: { turbo: false, action: "submit->issues#submit" } })}'
466
+ Dir.glob("**/*.rb", base: views_dir).sort.each do |rel|
467
+ path = File.join(views_dir, rel)
468
+ text = File.read(path)
469
+ updated = text
470
+ if updated.include?("each_with_index") && updated.include?("ActionView::ViewHelpers.raw(_cap)")
471
+ updated = updated.gsub("ActionView::ViewHelpers.raw(_cap)", "io << _cap")
472
+ end
473
+ if updated.include?(bogus)
474
+ updated = updated.gsub(bogus, "")
475
+ updated = updated.gsub(
476
+ 'class=\"#{"button_to"}\">',
477
+ 'class=\"#{"button_to"}\" data-turbo=\"false\" data-action=\"submit-&gt;issues#submit\">'
478
+ )
479
+ end
480
+ File.write(path, updated) if updated != text
481
+ end
482
+ end
483
+
484
+ def patch_slack_sidebar
485
+ sidebar = File.join(@target, "app/views/slack/channels/_sidebar.rb")
486
+ text = File.read(sidebar)
487
+ start = text.index("channels.each do |channel|")
488
+ stop = start && text.index("dms.each do", start)
489
+ raise BuildError, "Expected channel loop in #{sidebar}" unless start && stop
490
+
491
+ loop_body = text[start...stop].gsub(/\bchannel\b/, "channel_row").gsub("channel_row == channel_row", "channel_row == channel")
492
+ File.write(sidebar, text[0...start] + loop_body + text[stop..])
493
+ end
494
+
495
+ def prepend_wasm_threads_require
496
+ boot = File.join(@target, "boot.rb")
497
+ text = File.read(boot)
498
+ match = text.match(/^require(?:_relative)? /)
499
+ raise BuildError, "Expected require line in #{boot}" unless match
500
+
501
+ first = match.begin(0)
502
+ File.write(boot, text[0...first] + "require_relative \"runtime/wasm_threads\"\n" + text[first..])
503
+ end
504
+
505
+ def prune_dead_subsystems
506
+ pruned_lines = 0
507
+ PRUNED_STUBS.each do |rel, stub|
508
+ path = File.join(@target, rel)
509
+ next unless File.file?(path)
510
+
511
+ pruned_lines += File.read(path).count("\n")
512
+ File.write(path, "# Pruned for browser wasm (see WasmTree)\n#{stub}")
513
+ end
514
+ pruned_lines
515
+ end
516
+ end
517
+ end
518
+ end