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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +58 -0
- data/README.md +267 -61
- data/app/controllers/instant_record/bootstraps_controller.rb +39 -12
- data/app/controllers/instant_record/events_controller.rb +58 -6
- data/app/controllers/instant_record/files_controller.rb +116 -0
- data/app/controllers/instant_record/mutations_controller.rb +84 -5
- data/app/controllers/instant_record/native_screens_controller.rb +60 -0
- data/app/controllers/instant_record/records_controller.rb +86 -8
- data/app/controllers/instant_record/shells_controller.rb +64 -0
- data/app/controllers/instant_record/sync_notices_controller.rb +81 -0
- data/app/helpers/instant_record/sync_notices_helper.rb +41 -0
- data/app/jobs/instant_record/shell_build_job.rb +16 -0
- data/app/models/instant_record/change.rb +36 -3
- data/app/models/instant_record/mutation_applier.rb +633 -50
- data/app/views/instant_record/sync_notices/_notices.html.erb +63 -0
- data/compiled/README.md +272 -0
- data/compiled/pipeline/browser_migrations.rb +18 -0
- data/compiled/pipeline/build.rb +13 -0
- data/compiled/pipeline/build_native.rb +14 -0
- data/compiled/pipeline/native/host.c +78 -0
- data/compiled/pipeline/native/instant_record_native.h +57 -0
- data/compiled/pipeline/native_shim.c +149 -0
- data/compiled/pipeline/reactor_shim.c +66 -0
- data/compiled/pipeline/sqlite/rh_shim.c +5 -0
- data/compiled/pipeline/sqlite_release.rb +43 -0
- data/compiled/toolchain.env.example +22 -0
- data/compiled/web/index.html +20 -0
- data/compiled/web/package-lock.json +884 -0
- data/compiled/web/package.json +17 -0
- data/compiled/web/src/app-host-core.js +556 -0
- data/compiled/web/src/app-host.js +16 -0
- data/compiled/web/src/branch-manager.js +140 -0
- data/compiled/web/src/branch-preview.js +397 -0
- data/compiled/web/src/changes.js +191 -0
- data/compiled/web/src/faults.js +172 -0
- data/compiled/web/src/flight-recorder.js +347 -0
- data/compiled/web/src/history-fill.js +68 -0
- data/compiled/web/src/http-headers.js +100 -0
- data/compiled/web/src/leader-election.js +705 -0
- data/compiled/web/src/main.js +608 -0
- data/compiled/web/src/manifest.js +112 -0
- data/compiled/web/src/opfs-wasi-core.js +109 -0
- data/compiled/web/src/opfs-wasi-file.js +8 -0
- data/compiled/web/src/reactor-runtime.js +1692 -0
- data/compiled/web/src/redactor.js +518 -0
- data/compiled/web/src/release-updates.js +48 -0
- data/compiled/web/src/replay.js +338 -0
- data/compiled/web/src/schema-recovery.js +204 -0
- data/compiled/web/src/server-request.js +68 -0
- data/compiled/web/src/sha256.js +98 -0
- data/compiled/web/src/shell.js +52 -0
- data/compiled/web/src/snapshot-store.js +38 -0
- data/compiled/web/src/sqlite-redactor.js +111 -0
- data/compiled/web/src/storage-backend.js +159 -0
- data/compiled/web/src/stream-leader.js +675 -0
- data/compiled/web/src/sync-context.js +91 -0
- data/compiled/web/src/sync-drain.js +104 -0
- data/compiled/web/src/sync-schedule.js +9 -0
- data/compiled/web/src/wasi-minimal.js +275 -0
- data/compiled/web/src/worker.js +39 -0
- data/compiled/web/vite.config.js +27 -0
- data/config/routes.rb +25 -0
- data/db/migrate/20261119000201_add_scope_to_instant_record_changes.rb +7 -0
- data/db/migrate/20261121000001_create_instant_record_sync_notices.rb +21 -0
- data/db/migrate/20261121000101_add_partition_to_instant_record_changes.rb +8 -0
- data/db/migrate/20261121230300_add_delay_line_to_instant_record_outbox.rb +10 -0
- data/exe/instant_record_mcp +6 -0
- data/frontend/LiveList.js +67 -0
- data/frontend/chord.js +79 -0
- data/frontend/file.js +65 -0
- data/frontend/files.js +738 -0
- data/frontend/focus.js +10 -0
- data/frontend/held.js +192 -0
- data/frontend/http.js +76 -0
- data/frontend/index.js +53 -0
- data/frontend/keyboard.js +56 -0
- data/frontend/live.js +298 -0
- data/frontend/notices.js +118 -0
- data/frontend/package.json +15 -0
- data/frontend/range.js +10 -0
- data/frontend/selection.js +47 -0
- data/frontend/shortcuts.js +25 -0
- data/frontend/sync-notices.js +130 -0
- data/frontend/undo.js +48 -0
- data/frontend/window.js +181 -0
- data/lib/generators/instant_record/install/install_generator.rb +18 -30
- data/lib/generators/instant_record/install/templates/instant_record.rb +34 -0
- data/lib/generators/instant_record/ios/ios_generator.rb +71 -0
- data/lib/generators/instant_record/ios/templates/App.swift.tt +35 -0
- data/lib/generators/instant_record/ios/templates/Info.plist +11 -0
- data/lib/generators/instant_record/ios/templates/LibraryEngine.swift +30 -0
- data/lib/instant_record/branch/sqlite_store.rb +142 -0
- data/lib/instant_record/branch.rb +388 -0
- data/lib/instant_record/client/notifier.rb +8 -12
- data/lib/instant_record/client/rebaser.rb +565 -0
- data/lib/instant_record/client/transport.rb +64 -42
- data/lib/instant_record/client.rb +1600 -169
- data/lib/instant_record/compiled_site.rb +85 -0
- data/lib/instant_record/compiler/app_stage.rb +212 -0
- data/lib/instant_record/compiler/ast_lowerer.rb +659 -0
- data/lib/instant_record/compiler/browser_migrations.rb +161 -0
- data/lib/instant_record/compiler/entry_builder.rb +832 -0
- data/lib/instant_record/compiler/fts5_smoke.rb +205 -0
- data/lib/instant_record/compiler/gem_vendor.rb +194 -0
- data/lib/instant_record/compiler/host_adapters.rb +506 -0
- data/lib/instant_record/compiler/kotlin_codegen.rb +209 -0
- data/lib/instant_record/compiler/manifest.rb +178 -0
- data/lib/instant_record/compiler/native_build.rb +586 -0
- data/lib/instant_record/compiler/native_contracts.rb +338 -0
- data/lib/instant_record/compiler/path_scrubber.rb +31 -0
- data/lib/instant_record/compiler/reachability_tracer.rb +1188 -0
- data/lib/instant_record/compiler/route_analyzer.rb +623 -0
- data/lib/instant_record/compiler/runner.rb +42 -0
- data/lib/instant_record/compiler/schema_lock.rb +606 -0
- data/lib/instant_record/compiler/source_pages.rb +77 -0
- data/lib/instant_record/compiler/static_linter.rb +770 -0
- data/lib/instant_record/compiler/swift_codegen.rb +341 -0
- data/lib/instant_record/compiler/toolchain.rb +219 -0
- data/lib/instant_record/compiler/wasm_build.rb +704 -0
- data/lib/instant_record/compiler/wasm_tree.rb +518 -0
- data/lib/instant_record/compiler.rb +277 -0
- data/lib/instant_record/configuration.rb +104 -0
- data/lib/instant_record/engine.rb +68 -18
- data/lib/instant_record/file_store.rb +162 -0
- data/lib/instant_record/files.rb +92 -0
- data/lib/instant_record/hlc.rb +387 -0
- data/lib/instant_record/host.rb +66 -0
- data/lib/instant_record/inertia/renderer.rb +52 -0
- data/lib/instant_record/inertia.rb +222 -0
- data/lib/instant_record/mcp/cli.rb +319 -0
- data/lib/instant_record/mcp/host_process.rb +229 -0
- data/lib/instant_record/mcp/http_exchange.rb +233 -0
- data/lib/instant_record/mcp/replica.rb +556 -0
- data/lib/instant_record/mcp/runtime.rb +406 -0
- data/lib/instant_record/mcp/server.rb +429 -0
- data/lib/instant_record/mcp.rb +47 -0
- data/lib/instant_record/native/contract.rb +367 -0
- data/lib/instant_record/native/node.rb +125 -0
- data/lib/instant_record/native/screen.rb +366 -0
- data/lib/instant_record/native/xcode_project.rb +344 -0
- data/lib/instant_record/native.rb +41 -0
- data/lib/instant_record/outbox_mutation.rb +81 -9
- data/lib/instant_record/outbox_upcaster.rb +161 -0
- data/lib/instant_record/read_tracker.rb +610 -0
- data/lib/instant_record/routing.rb +33 -0
- data/lib/instant_record/runtime_scoped.rb +4 -0
- data/lib/instant_record/shell.rb +486 -0
- data/lib/instant_record/sync_actions.rb +800 -0
- data/lib/instant_record/sync_metadata.rb +17 -2
- data/lib/instant_record/sync_notice.rb +234 -0
- data/lib/instant_record/sync_partition.rb +63 -0
- data/lib/instant_record/sync_scope.rb +300 -0
- data/lib/instant_record/sync_shape.rb +102 -0
- data/lib/instant_record/sync_window.rb +92 -15
- data/lib/instant_record/syncable.rb +313 -17
- data/lib/instant_record/testing/authoritative_server.rb +547 -0
- data/lib/instant_record/testing/chaos_simulator.rb +429 -0
- data/lib/instant_record/testing/cruby_host_bridge.rb +370 -0
- data/lib/instant_record/testing/fault_network.rb +140 -0
- data/lib/instant_record/testing/parity_suite.rb +435 -0
- data/lib/instant_record/testing/runtimes/base.rb +184 -0
- data/lib/instant_record/testing/runtimes/cruby_runtime.rb +255 -0
- data/lib/instant_record/testing/runtimes/native_runtime.rb +346 -0
- data/lib/instant_record/testing/runtimes/wasm_runtime.rb +371 -0
- data/lib/instant_record/testing/wasm_host_bridge.mjs +240 -0
- data/lib/instant_record/testing.rb +333 -0
- data/lib/instant_record/trace/redactor.rb +278 -0
- data/lib/instant_record/trace/replayer.rb +126 -0
- data/lib/instant_record/trace/schema_extractor.rb +37 -0
- data/lib/instant_record/trace.rb +22 -0
- data/lib/instant_record/version.rb +1 -1
- data/lib/instant_record.rb +107 -37
- data/lib/tasks/instant_record.rake +299 -47
- data/native/ios/InstantRecordNative/Package.swift +12 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/AppHost.swift +706 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Engine.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/FileStore.swift +1048 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/HTTPEngine.swift +36 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Icons.swift +19 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Node.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/ScreenView.swift +481 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Screens.swift +94 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/StreamClient.swift +74 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncNotice.swift +282 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncSchedule.swift +153 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Theme.swift +140 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Transport.swift +108 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/TypedContracts.swift +237 -0
- data/native/ios/README.md +341 -0
- data/sig/instant_record/inertia.rbs +47 -0
- data/sig/instant_record/native.rbs +38 -0
- metadata +194 -18
- data/lib/generators/instant_record/install/templates/database.js +0 -32
- data/lib/generators/instant_record/install/templates/index.html +0 -69
- data/lib/generators/instant_record/install/templates/rails.sw.js +0 -750
- data/lib/instant_record/local_schema.rb +0 -75
- data/lib/instant_record/pglite_compat.rb +0 -40
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
require "digest"
|
|
2
|
+
require "fileutils"
|
|
3
|
+
require "pathname"
|
|
4
|
+
require "prism"
|
|
5
|
+
require_relative "ast_lowerer"
|
|
6
|
+
require_relative "runner"
|
|
7
|
+
|
|
8
|
+
module InstantRecord
|
|
9
|
+
class Compiler
|
|
10
|
+
# Swappable target host-adapter modules installed by GemVendor when staging
|
|
11
|
+
# an app for AOT compilation. Each adapter provides the compiled
|
|
12
|
+
# `InstantRecord::Host`, `InstantRecord::RuntimeScoped`, and
|
|
13
|
+
# `InstantRecord::Native` implementations for its target runtime so the gem's
|
|
14
|
+
# own Ruby files need zero literal string edits.
|
|
15
|
+
module HostAdapters
|
|
16
|
+
RUNTIME_SCOPED_SEAM = <<~RUBY
|
|
17
|
+
module InstantRecord
|
|
18
|
+
# Host adapter seam: AstLowerer statically strips server_only blocks and
|
|
19
|
+
# unwraps browser_only / client_only blocks before Roundhouse & Spinel run.
|
|
20
|
+
module RuntimeScoped
|
|
21
|
+
def server_only(&block)
|
|
22
|
+
nil
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def browser_only(&block)
|
|
26
|
+
nil
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def client_only(&block)
|
|
30
|
+
nil
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
RUBY
|
|
35
|
+
|
|
36
|
+
NATIVE_SEAM = <<~RUBY
|
|
37
|
+
require "instant_record/native/node"
|
|
38
|
+
require "instant_record/native/screen"
|
|
39
|
+
RUBY
|
|
40
|
+
|
|
41
|
+
SPINEL_SYNC_ACTIONS_STUB = <<~'RUBY'
|
|
42
|
+
def server_frame = nil
|
|
43
|
+
def enforcing_server_mutator? = false
|
|
44
|
+
def server_authorize_create!(_record) = nil
|
|
45
|
+
def server_finalize_create!(_record) = nil
|
|
46
|
+
def server_authorize_update_before!(_record) = nil
|
|
47
|
+
def server_finalize_update!(_record) = nil
|
|
48
|
+
def server_authorize_destroy!(_record) = nil
|
|
49
|
+
def server_finalize_destroy!(_record) = nil
|
|
50
|
+
def server_note_read(_record) = nil
|
|
51
|
+
def execute_on_server(_name, _args, _mutation_id) = nil
|
|
52
|
+
def register(_klass, _action_name, _block = nil) = nil
|
|
53
|
+
|
|
54
|
+
def dispatch(name, _args)
|
|
55
|
+
raise ArgumentError, "Unknown sync_action: " + name
|
|
56
|
+
end
|
|
57
|
+
RUBY
|
|
58
|
+
|
|
59
|
+
CRUBY_SYNC_ACTION_METHODS = %i[
|
|
60
|
+
server_frame enforcing_server_mutator?
|
|
61
|
+
server_authorize_create! server_finalize_create!
|
|
62
|
+
server_authorize_update_before! server_finalize_update!
|
|
63
|
+
server_authorize_destroy! server_finalize_destroy!
|
|
64
|
+
server_note_read with_server_frame execute_on_server
|
|
65
|
+
registry reset_registry! register resolve dispatch
|
|
66
|
+
defer_active_job? install_active_job_guard! invoke_callable
|
|
67
|
+
].freeze
|
|
68
|
+
|
|
69
|
+
# Shared installation logic for compiled host adapters.
|
|
70
|
+
module Base
|
|
71
|
+
def install(gem_src:, app_dir:)
|
|
72
|
+
File.write(File.join(app_dir, "lib/instant_record/host.rb"), host_source)
|
|
73
|
+
File.write(File.join(app_dir, "lib/instant_record/runtime_scoped.rb"), RUNTIME_SCOPED_SEAM)
|
|
74
|
+
File.write(File.join(app_dir, "lib/instant_record/native.rb"), NATIVE_SEAM)
|
|
75
|
+
|
|
76
|
+
FileUtils.mkdir_p(File.join(app_dir, "sig/instant_record"))
|
|
77
|
+
%w[native.rbs inertia.rbs].each do |rbs|
|
|
78
|
+
FileUtils.cp(
|
|
79
|
+
File.join(gem_src, "sig/instant_record", rbs),
|
|
80
|
+
File.join(app_dir, "sig/instant_record", rbs)
|
|
81
|
+
)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
adapt_compiled_mutate_signatures(app_dir)
|
|
85
|
+
adapt_sync_actions(app_dir)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
# Spinel does not support `**kwargs` splats; rewrite `InstantRecord.mutate`
|
|
91
|
+
# via Prism AST to accept a plain `args = {}` hash.
|
|
92
|
+
def adapt_compiled_mutate_signatures(app_dir)
|
|
93
|
+
path = File.join(app_dir, "lib/instant_record.rb")
|
|
94
|
+
return unless File.file?(path)
|
|
95
|
+
|
|
96
|
+
source = File.read(path, encoding: Encoding::UTF_8)
|
|
97
|
+
parsed = Prism.parse(source)
|
|
98
|
+
return unless parsed.success?
|
|
99
|
+
|
|
100
|
+
edits = []
|
|
101
|
+
walk_ast(parsed.value) do |node|
|
|
102
|
+
next unless node.is_a?(Prism::DefNode) && node.name == :mutate && node.parameters&.keyword_rest
|
|
103
|
+
|
|
104
|
+
indent = AstLowerer.leading_indent(source, node.location.start_offset)
|
|
105
|
+
replacement = <<~RUBY.chomp
|
|
106
|
+
def mutate(mutator_name, args = {}, mutation_id: nil)
|
|
107
|
+
#{indent} SyncActions.mutate(mutator_name, args, mutation_id: mutation_id)
|
|
108
|
+
#{indent}end
|
|
109
|
+
RUBY
|
|
110
|
+
edits << AstLowerer::Edit.new(
|
|
111
|
+
start_offset: node.location.start_offset,
|
|
112
|
+
end_offset: node.location.end_offset,
|
|
113
|
+
replacement: replacement
|
|
114
|
+
)
|
|
115
|
+
end
|
|
116
|
+
File.write(path, AstLowerer.apply_edits(source, edits)) unless edits.empty?
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Strip CRuby-only server frame, ActiveJob guard, and dynamic dispatch from
|
|
120
|
+
# `lib/instant_record/sync_actions.rb` using Prism's AST; EntryBuilder emits
|
|
121
|
+
# the static `case` dispatcher in `main_*.rb`.
|
|
122
|
+
def adapt_sync_actions(app_dir)
|
|
123
|
+
path = File.join(app_dir, "lib/instant_record/sync_actions.rb")
|
|
124
|
+
return unless File.file?(path)
|
|
125
|
+
|
|
126
|
+
source = File.read(path, encoding: Encoding::UTF_8)
|
|
127
|
+
parsed = Prism.parse(source)
|
|
128
|
+
raise BuildError, "Cannot parse lib/instant_record/sync_actions.rb" unless parsed.success?
|
|
129
|
+
|
|
130
|
+
edits = []
|
|
131
|
+
stub_inserted = false
|
|
132
|
+
walk_ast(parsed.value) do |node|
|
|
133
|
+
if node.is_a?(Prism::ClassNode) && AstLowerer.constant_name(node.constant_path) == "ServerFrame"
|
|
134
|
+
edits << delete_lines_edit(source, node)
|
|
135
|
+
next :skip
|
|
136
|
+
elsif node.is_a?(Prism::ModuleNode) && AstLowerer.constant_name(node.constant_path) == "ActiveJobCommitGuard"
|
|
137
|
+
edits << delete_lines_edit(source, node)
|
|
138
|
+
next :skip
|
|
139
|
+
elsif node.is_a?(Prism::DefNode) && node.name == :perform_sync_action && node.parameters&.keyword_rest
|
|
140
|
+
indent = AstLowerer.leading_indent(source, node.location.start_offset)
|
|
141
|
+
replacement = <<~RUBY.chomp
|
|
142
|
+
def perform_sync_action(action_name, args = {}, mutation_id: nil)
|
|
143
|
+
#{indent} InstantRecord::SyncActions.mutate("\#{name}.\#{action_name}", args, mutation_id: mutation_id)
|
|
144
|
+
#{indent}end
|
|
145
|
+
RUBY
|
|
146
|
+
edits << AstLowerer::Edit.new(start_offset: node.location.start_offset, end_offset: node.location.end_offset, replacement: replacement)
|
|
147
|
+
next :skip
|
|
148
|
+
elsif node.is_a?(Prism::DefNode) && node.name == :mutate && node.parameters&.keyword_rest
|
|
149
|
+
indent = AstLowerer.leading_indent(source, node.location.start_offset)
|
|
150
|
+
# Rewrite parameter list and remove `merged = ...` line inside body.
|
|
151
|
+
sig_end = node.rparen_loc ? node.rparen_loc.end_offset : node.parameters.location.end_offset
|
|
152
|
+
edits << AstLowerer::Edit.new(
|
|
153
|
+
start_offset: node.location.start_offset,
|
|
154
|
+
end_offset: sig_end,
|
|
155
|
+
replacement: "def mutate(mutator_name, args = {}, mutation_id: nil)"
|
|
156
|
+
)
|
|
157
|
+
Array(node.body&.body).each do |stmt|
|
|
158
|
+
if stmt.is_a?(Prism::LocalVariableWriteNode) && stmt.name == :merged
|
|
159
|
+
edits << delete_lines_edit(source, stmt)
|
|
160
|
+
elsif stmt.is_a?(Prism::LocalVariableWriteNode) && stmt.name == :normalized
|
|
161
|
+
edits << AstLowerer::Edit.new(
|
|
162
|
+
start_offset: stmt.location.start_offset,
|
|
163
|
+
end_offset: stmt.location.end_offset,
|
|
164
|
+
replacement: "normalized = normalize_args(args)"
|
|
165
|
+
)
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
next :skip
|
|
169
|
+
elsif node.is_a?(Prism::DefNode) && CRUBY_SYNC_ACTION_METHODS.include?(node.name)
|
|
170
|
+
if !stub_inserted
|
|
171
|
+
stub_inserted = true
|
|
172
|
+
indent = AstLowerer.leading_indent(source, node.location.start_offset)
|
|
173
|
+
stub = SPINEL_SYNC_ACTIONS_STUB.lines.map { |l| l.strip.empty? ? "\n" : "#{indent}#{l}" }.join
|
|
174
|
+
edits << AstLowerer::Edit.new(
|
|
175
|
+
start_offset: AstLowerer.line_start_of(source, node.location.start_offset),
|
|
176
|
+
end_offset: AstLowerer.line_end_after(source, node.location.end_offset),
|
|
177
|
+
replacement: stub
|
|
178
|
+
)
|
|
179
|
+
else
|
|
180
|
+
edits << delete_lines_edit(source, node)
|
|
181
|
+
end
|
|
182
|
+
next :skip
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
adapted = AstLowerer.apply_edits(source, edits)
|
|
187
|
+
raise BuildError, "Invalid Ruby after adapting sync_actions.rb" unless Prism.parse(adapted).success?
|
|
188
|
+
|
|
189
|
+
File.write(path, adapted)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def delete_lines_edit(source, node)
|
|
193
|
+
AstLowerer::Edit.new(
|
|
194
|
+
start_offset: AstLowerer.line_start_of(source, node.location.start_offset),
|
|
195
|
+
end_offset: AstLowerer.line_end_after(source, node.location.end_offset),
|
|
196
|
+
replacement: ""
|
|
197
|
+
)
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def walk_ast(node, &block)
|
|
201
|
+
return if node.nil?
|
|
202
|
+
|
|
203
|
+
return if yield(node) == :skip
|
|
204
|
+
|
|
205
|
+
node.compact_child_nodes.each { |child| walk_ast(child, &block) }
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# Browser wasm32-wasi reactor host: communicates with the dedicated worker
|
|
210
|
+
# through virtual `/host/*` files.
|
|
211
|
+
module Wasm
|
|
212
|
+
extend Base
|
|
213
|
+
|
|
214
|
+
HOST_SOURCE = <<~RUBY
|
|
215
|
+
module InstantRecord
|
|
216
|
+
# Compiled wasm32-wasi host adapter (see Compiler::HostAdapters::Wasm).
|
|
217
|
+
module Host
|
|
218
|
+
module_function
|
|
219
|
+
|
|
220
|
+
def browser? = true
|
|
221
|
+
|
|
222
|
+
def load_engine = nil
|
|
223
|
+
|
|
224
|
+
def load_inertia_renderer = nil
|
|
225
|
+
|
|
226
|
+
def load_branch_store = nil
|
|
227
|
+
|
|
228
|
+
def install_active_job_guard = nil
|
|
229
|
+
|
|
230
|
+
def with_savepoint(&block)
|
|
231
|
+
@savepoint_seq = (@savepoint_seq || 0) + 1
|
|
232
|
+
sp = "ir_sp_" + @savepoint_seq.to_s
|
|
233
|
+
Db.exec("SAVEPOINT " + sp)
|
|
234
|
+
begin
|
|
235
|
+
result = block.call
|
|
236
|
+
Db.exec("RELEASE SAVEPOINT " + sp)
|
|
237
|
+
result
|
|
238
|
+
rescue StandardError => error
|
|
239
|
+
Db.exec("ROLLBACK TO SAVEPOINT " + sp)
|
|
240
|
+
Db.exec("RELEASE SAVEPOINT " + sp)
|
|
241
|
+
raise error
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def default_mount_path = InstantRecord::DEFAULT_MOUNT_PATH
|
|
246
|
+
|
|
247
|
+
def resolve_syncable_model(_record_type) = nil
|
|
248
|
+
|
|
249
|
+
def discover_syncable_models = []
|
|
250
|
+
|
|
251
|
+
# Compiled models initialize a new record's id to "", not nil.
|
|
252
|
+
def blank_id?(id)
|
|
253
|
+
id.blank?
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
def read_channel(name)
|
|
257
|
+
File.read("/host/\#{name}")
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def write_channel(name, body)
|
|
261
|
+
File.write("/host/\#{name}", body)
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
RUBY
|
|
266
|
+
|
|
267
|
+
module_function
|
|
268
|
+
|
|
269
|
+
def target = :wasm
|
|
270
|
+
def host_source = HOST_SOURCE
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Native static-library host (macOS host binary / iOS simulator / iPhone):
|
|
274
|
+
# communicates through NativeHost FFI calls (`native_shim.c`).
|
|
275
|
+
module Native
|
|
276
|
+
extend Base
|
|
277
|
+
|
|
278
|
+
HOST_SOURCE = <<~RUBY
|
|
279
|
+
module InstantRecord
|
|
280
|
+
# Compiled native host adapter (see Compiler::HostAdapters::Native).
|
|
281
|
+
module Host
|
|
282
|
+
module_function
|
|
283
|
+
|
|
284
|
+
def browser? = true
|
|
285
|
+
|
|
286
|
+
def load_engine = nil
|
|
287
|
+
|
|
288
|
+
def load_inertia_renderer = nil
|
|
289
|
+
|
|
290
|
+
def load_branch_store = nil
|
|
291
|
+
|
|
292
|
+
def install_active_job_guard = nil
|
|
293
|
+
|
|
294
|
+
def with_savepoint(&block)
|
|
295
|
+
@savepoint_seq = (@savepoint_seq || 0) + 1
|
|
296
|
+
sp = "ir_sp_" + @savepoint_seq.to_s
|
|
297
|
+
Db.exec("SAVEPOINT " + sp)
|
|
298
|
+
begin
|
|
299
|
+
result = block.call
|
|
300
|
+
Db.exec("RELEASE SAVEPOINT " + sp)
|
|
301
|
+
result
|
|
302
|
+
rescue StandardError => error
|
|
303
|
+
Db.exec("ROLLBACK TO SAVEPOINT " + sp)
|
|
304
|
+
Db.exec("RELEASE SAVEPOINT " + sp)
|
|
305
|
+
raise error
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
def default_mount_path = InstantRecord::DEFAULT_MOUNT_PATH
|
|
310
|
+
|
|
311
|
+
def resolve_syncable_model(_record_type) = nil
|
|
312
|
+
|
|
313
|
+
def discover_syncable_models = []
|
|
314
|
+
|
|
315
|
+
# Compiled models initialize a new record's id to "", not nil.
|
|
316
|
+
def blank_id?(id)
|
|
317
|
+
id.blank?
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
def read_channel(name)
|
|
321
|
+
NativeHost.ir_host_take(name)
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def write_channel(name, body)
|
|
325
|
+
NativeHost.ir_host_give(name, body)
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
end
|
|
329
|
+
RUBY
|
|
330
|
+
|
|
331
|
+
module_function
|
|
332
|
+
|
|
333
|
+
def target = :native
|
|
334
|
+
def host_source = HOST_SOURCE
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
# Demo-specific seams isolated from GemVendor core vendoring. Ruby edits use
|
|
338
|
+
# Prism AST rewrites rather than literal string substitution.
|
|
339
|
+
module Demo
|
|
340
|
+
SERVER_URL_ROUTES = {
|
|
341
|
+
slack_reset_path: "/slack/reset",
|
|
342
|
+
auth_session_path: "/auth/session",
|
|
343
|
+
auth_session_sign_out_path: "/auth/session/sign_out"
|
|
344
|
+
}.freeze
|
|
345
|
+
|
|
346
|
+
module_function
|
|
347
|
+
|
|
348
|
+
def apply(app_dir:)
|
|
349
|
+
fingerprint_stylesheets(app_dir)
|
|
350
|
+
version_inbox_bundle(app_dir)
|
|
351
|
+
adapt_demo_views(app_dir)
|
|
352
|
+
rewrite_server_url_helpers(app_dir)
|
|
353
|
+
coerce_slack_thread_replies(app_dir)
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
def fingerprint_stylesheets(app_dir)
|
|
357
|
+
stylesheets_dir = File.join(app_dir, "app/assets/stylesheets")
|
|
358
|
+
return unless File.directory?(stylesheets_dir)
|
|
359
|
+
|
|
360
|
+
Dir.glob("*.css", base: stylesheets_dir).sort.each do |name|
|
|
361
|
+
sheet = File.join(stylesheets_dir, name)
|
|
362
|
+
digest = Digest::SHA256.file(sheet).hexdigest[0, 10]
|
|
363
|
+
ext = File.extname(sheet)
|
|
364
|
+
stem = File.basename(sheet, ext)
|
|
365
|
+
File.rename(sheet, File.join(stylesheets_dir, "#{stem}-#{digest}#{ext}"))
|
|
366
|
+
end
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
def version_inbox_bundle(app_dir)
|
|
370
|
+
bundle = File.join(app_dir, "app/assets/builds/inbox.js")
|
|
371
|
+
view = File.join(app_dir, "app/views/email/mailboxes/index.html.erb")
|
|
372
|
+
return unless File.file?(bundle) && File.file?(view)
|
|
373
|
+
|
|
374
|
+
digest = Digest::SHA256.file(bundle).hexdigest[0, 10]
|
|
375
|
+
source = File.read(view)
|
|
376
|
+
target = '<%= asset_path("inbox.js") %>'
|
|
377
|
+
replacement = "/assets/inbox.js?v=#{digest}"
|
|
378
|
+
if source.include?(target)
|
|
379
|
+
File.write(view, source.sub(target) { replacement })
|
|
380
|
+
elsif !source.include?(replacement)
|
|
381
|
+
raise BuildError, "demo seam did not apply: app/views/email/mailboxes/index.html.erb"
|
|
382
|
+
end
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
def adapt_demo_views(app_dir)
|
|
386
|
+
layout_path = File.join(app_dir, "app/views/layouts/application.html.erb")
|
|
387
|
+
if File.file?(layout_path)
|
|
388
|
+
source = File.read(layout_path)
|
|
389
|
+
source = source.gsub('<span class="micro-label">Local-first Rails demos —', '<span class="micro-label"><a href="/">All demos</a> ·')
|
|
390
|
+
source = source.gsub('<%= source_path(page: controller_path, view: action_name) %>', "/source")
|
|
391
|
+
source = source.gsub("Read live out of app.wasm — not a snippet", "View the source for this build")
|
|
392
|
+
source = source.gsub('demo-<%= controller_path.split("/").first %>', "demo-<%= content_for(:demo_scope) %>")
|
|
393
|
+
source = source.gsub('<% demo = controller_path.split("/").first %>', "<% demo = content_for(:demo_scope).to_s %>")
|
|
394
|
+
File.write(layout_path, source)
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
views_dir = File.join(app_dir, "app/views")
|
|
398
|
+
views_root = Pathname(views_dir)
|
|
399
|
+
Dir.glob("**/*.html.erb", base: views_dir).sort.each do |rel|
|
|
400
|
+
view = views_root.join(rel)
|
|
401
|
+
next if view.basename.to_s.start_with?("_") || view.parent.basename.to_s == "layouts"
|
|
402
|
+
|
|
403
|
+
scope = Pathname(rel).each_filename.first
|
|
404
|
+
view.write("<% content_for(:demo_scope, \"#{scope}\") %>\n" + view.read)
|
|
405
|
+
end
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
# Rewrites `InstantRecord.server_url(...)` calls in demo helpers into static
|
|
409
|
+
# network-marked path literals using Prism's AST.
|
|
410
|
+
def rewrite_server_url_helpers(app_dir)
|
|
411
|
+
%w[app/helpers/slack_helper.rb app/helpers/auth_helper.rb].each do |rel|
|
|
412
|
+
path = File.join(app_dir, rel)
|
|
413
|
+
next unless File.file?(path)
|
|
414
|
+
|
|
415
|
+
source = File.read(path, encoding: Encoding::UTF_8)
|
|
416
|
+
parsed = Prism.parse(source)
|
|
417
|
+
raise BuildError, "Cannot parse #{rel}" unless parsed.success?
|
|
418
|
+
|
|
419
|
+
edits = []
|
|
420
|
+
walk_nodes(parsed.value) do |node|
|
|
421
|
+
next unless node.is_a?(Prism::CallNode) && node.name == :server_url
|
|
422
|
+
next unless %w[InstantRecord ::InstantRecord].include?(AstLowerer.constant_name(node.receiver))
|
|
423
|
+
|
|
424
|
+
args = node.arguments&.arguments
|
|
425
|
+
next unless args&.length == 1
|
|
426
|
+
|
|
427
|
+
resolved = resolve_demo_route(args.first)
|
|
428
|
+
next unless resolved
|
|
429
|
+
|
|
430
|
+
edits << AstLowerer::Edit.new(
|
|
431
|
+
start_offset: node.location.start_offset,
|
|
432
|
+
end_offset: node.location.end_offset,
|
|
433
|
+
replacement: %("#{resolved}?#{InstantRecord::NETWORK_MARKER}")
|
|
434
|
+
)
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
raise BuildError, "demo seam did not apply: #{rel}: no InstantRecord.server_url calls rewritten" if edits.empty?
|
|
438
|
+
|
|
439
|
+
File.write(path, AstLowerer.apply_edits(source, edits))
|
|
440
|
+
end
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
# Compiled code needs a single array type across both branches of the
|
|
444
|
+
# `@thread_parent ? <relation> : []` ternary.
|
|
445
|
+
def coerce_slack_thread_replies(app_dir)
|
|
446
|
+
rel = "app/controllers/slack/channels_controller.rb"
|
|
447
|
+
path = File.join(app_dir, rel)
|
|
448
|
+
return unless File.file?(path)
|
|
449
|
+
|
|
450
|
+
source = File.read(path, encoding: Encoding::UTF_8)
|
|
451
|
+
parsed = Prism.parse(source)
|
|
452
|
+
raise BuildError, "Cannot parse #{rel}" unless parsed.success?
|
|
453
|
+
|
|
454
|
+
edits = []
|
|
455
|
+
walk_nodes(parsed.value) do |node|
|
|
456
|
+
next unless node.is_a?(Prism::IfNode) && node.if_keyword_loc.nil?
|
|
457
|
+
next unless node.predicate.is_a?(Prism::InstanceVariableReadNode) && node.predicate.name == :@thread_parent
|
|
458
|
+
|
|
459
|
+
else_body = node.subsequent&.statements&.body
|
|
460
|
+
next unless else_body&.length == 1 && else_body.first.is_a?(Prism::ArrayNode) && else_body.first.elements.empty?
|
|
461
|
+
|
|
462
|
+
then_body = node.statements&.body
|
|
463
|
+
next unless then_body&.length == 1 && then_body.first.is_a?(Prism::CallNode)
|
|
464
|
+
next if then_body.first.name == :to_a
|
|
465
|
+
|
|
466
|
+
end_off = then_body.first.location.end_offset
|
|
467
|
+
edits << AstLowerer::Edit.new(start_offset: end_off, end_offset: end_off, replacement: ".to_a")
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
raise BuildError, "demo seam did not apply: #{rel}: @thread_parent ternary not found" if edits.empty?
|
|
471
|
+
|
|
472
|
+
File.write(path, AstLowerer.apply_edits(source, edits))
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
def resolve_demo_route(arg)
|
|
476
|
+
case arg
|
|
477
|
+
when Prism::StringNode
|
|
478
|
+
arg.unescaped
|
|
479
|
+
when Prism::CallNode
|
|
480
|
+
SERVER_URL_ROUTES[arg.name] if arg.receiver.nil? && arg.arguments.nil?
|
|
481
|
+
end
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
def walk_nodes(node, &block)
|
|
485
|
+
return if node.nil?
|
|
486
|
+
|
|
487
|
+
yield node
|
|
488
|
+
node.compact_child_nodes.each { |child| walk_nodes(child, &block) }
|
|
489
|
+
end
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
module_function
|
|
493
|
+
|
|
494
|
+
def for(target)
|
|
495
|
+
return target if target.respond_to?(:install) && target.respond_to?(:host_source)
|
|
496
|
+
|
|
497
|
+
case target.to_sym
|
|
498
|
+
when :wasm then Wasm
|
|
499
|
+
when :native, :iphonesimulator, :iphoneos then Native
|
|
500
|
+
else
|
|
501
|
+
raise ArgumentError, "Unknown host adapter target: #{target.inspect}"
|
|
502
|
+
end
|
|
503
|
+
end
|
|
504
|
+
end
|
|
505
|
+
end
|
|
506
|
+
end
|