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,209 @@
|
|
|
1
|
+
require_relative "../native/contract"
|
|
2
|
+
require_relative "swift_codegen"
|
|
3
|
+
|
|
4
|
+
module InstantRecord
|
|
5
|
+
class Compiler
|
|
6
|
+
# Emits strongly typed Kotlin data classes and sealed action interfaces from
|
|
7
|
+
# extracted `InstantRecord::Native::Contract` definitions (optional target
|
|
8
|
+
# alongside Swift for Android / Jetpack Compose hosts).
|
|
9
|
+
module KotlinCodegen
|
|
10
|
+
KEYWORDS = %w[
|
|
11
|
+
as break class continue do else false for fun if in interface is null object
|
|
12
|
+
package return super this throw true try typealias typeof val var when while
|
|
13
|
+
].freeze
|
|
14
|
+
|
|
15
|
+
SCALAR_TYPES = {
|
|
16
|
+
"string" => "String",
|
|
17
|
+
"int" => "Int",
|
|
18
|
+
"float" => "Double",
|
|
19
|
+
"bool" => "Boolean",
|
|
20
|
+
"date" => "String"
|
|
21
|
+
}.freeze
|
|
22
|
+
|
|
23
|
+
module_function
|
|
24
|
+
|
|
25
|
+
def generate(contracts, package: "dev.instantrecord.contracts", serializable: true)
|
|
26
|
+
lines = [
|
|
27
|
+
"// Generated by InstantRecord::Compiler::KotlinCodegen. Do not edit by hand.",
|
|
28
|
+
"package #{package}",
|
|
29
|
+
""
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
if serializable
|
|
33
|
+
needs_serial_name = contracts.any? { |c| tree_needs_serial_name?(c.fields) }
|
|
34
|
+
lines << "import kotlinx.serialization.SerialName" if needs_serial_name
|
|
35
|
+
lines << "import kotlinx.serialization.Serializable"
|
|
36
|
+
lines << ""
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
contracts.each_with_index do |contract, index|
|
|
40
|
+
lines << "" if index > 0
|
|
41
|
+
lines.concat(render_contract(contract, serializable: serializable))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
lines.join("\n") + "\n"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def render_contract(contract, serializable:)
|
|
48
|
+
lines = render_data_class(
|
|
49
|
+
name: "#{contract.component}Props",
|
|
50
|
+
component_name: contract.component,
|
|
51
|
+
fields: contract.fields,
|
|
52
|
+
indent: "",
|
|
53
|
+
top_level: true,
|
|
54
|
+
serializable: serializable
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
unless contract.actions.empty?
|
|
58
|
+
lines << ""
|
|
59
|
+
lines.concat(render_action_interface("#{contract.component}Action", contract.actions))
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
lines
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def render_data_class(name:, fields:, indent:, top_level:, serializable:, component_name: nil)
|
|
66
|
+
lines = []
|
|
67
|
+
lines << "#{indent}@Serializable" if serializable
|
|
68
|
+
|
|
69
|
+
nested_fields = fields.select { |f| f.one? || f.many? }
|
|
70
|
+
has_body = top_level || nested_fields.any?
|
|
71
|
+
|
|
72
|
+
if fields.empty?
|
|
73
|
+
lines << "#{indent}class #{name}#{has_body ? ' {' : ''}"
|
|
74
|
+
else
|
|
75
|
+
lines << "#{indent}data class #{name}("
|
|
76
|
+
fields.each do |field|
|
|
77
|
+
camel = SwiftCodegen.camel_case(field.name)
|
|
78
|
+
ident = kotlin_ident(camel)
|
|
79
|
+
annotation = serializable && camel != field.name ? %(@SerialName(#{kotlin_string(field.name)}) ) : ""
|
|
80
|
+
default_suffix = field.optional? ? " = null" : ""
|
|
81
|
+
lines << "#{indent} #{annotation}val #{ident}: #{kotlin_field_type(field)}#{default_suffix},"
|
|
82
|
+
end
|
|
83
|
+
lines << "#{indent})#{has_body ? ' {' : ''}"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
if has_body
|
|
87
|
+
inner = "#{indent} "
|
|
88
|
+
if top_level
|
|
89
|
+
lines << "#{inner}companion object {"
|
|
90
|
+
lines << %(#{inner} const val COMPONENT_NAME: String = #{kotlin_string(component_name)})
|
|
91
|
+
lines << "#{inner}}"
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
nested_fields.each do |field|
|
|
95
|
+
lines << "" if top_level || nested_fields.first != field
|
|
96
|
+
lines.concat(render_data_class(
|
|
97
|
+
name: SwiftCodegen.nested_type_name(field),
|
|
98
|
+
fields: field.fields,
|
|
99
|
+
indent: inner,
|
|
100
|
+
top_level: false,
|
|
101
|
+
serializable: serializable
|
|
102
|
+
))
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
lines << "#{indent}}"
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
lines
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def render_action_interface(interface_name, actions)
|
|
112
|
+
lines = [
|
|
113
|
+
"sealed interface #{interface_name} {",
|
|
114
|
+
" val actionName: String",
|
|
115
|
+
" val actionArgs: Map<String, String>",
|
|
116
|
+
" val actionValues: Map<String, String>"
|
|
117
|
+
]
|
|
118
|
+
|
|
119
|
+
actions.each do |action|
|
|
120
|
+
lines << ""
|
|
121
|
+
type_name = SwiftCodegen.pascal_case(action.name)
|
|
122
|
+
if action.params.empty?
|
|
123
|
+
lines << " data object #{type_name} : #{interface_name} {"
|
|
124
|
+
lines << " override val actionName: String = #{kotlin_string(action.name)}"
|
|
125
|
+
lines << " override val actionArgs: Map<String, String> = emptyMap()"
|
|
126
|
+
lines << " override val actionValues: Map<String, String> = emptyMap()"
|
|
127
|
+
lines << " }"
|
|
128
|
+
else
|
|
129
|
+
lines << " data class #{type_name}("
|
|
130
|
+
action.params.each do |param|
|
|
131
|
+
ident = kotlin_ident(SwiftCodegen.camel_case(param.name))
|
|
132
|
+
base = SCALAR_TYPES.fetch(param.type)
|
|
133
|
+
type = param.optional? ? "#{base}?" : base
|
|
134
|
+
default_suffix = param.optional? ? " = null" : ""
|
|
135
|
+
lines << " val #{ident}: #{type}#{default_suffix},"
|
|
136
|
+
end
|
|
137
|
+
lines << " ) : #{interface_name} {"
|
|
138
|
+
lines << " override val actionName: String = #{kotlin_string(action.name)}"
|
|
139
|
+
lines.concat(render_action_map("actionArgs", action.args))
|
|
140
|
+
lines.concat(render_action_map("actionValues", action.values))
|
|
141
|
+
lines << " }"
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
lines << "}"
|
|
146
|
+
lines
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def render_action_map(property_name, params)
|
|
150
|
+
if params.empty?
|
|
151
|
+
return [" override val #{property_name}: Map<String, String> = emptyMap()"]
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
if params.any?(&:optional?)
|
|
155
|
+
lines = [" override val #{property_name}: Map<String, String> = buildMap {"]
|
|
156
|
+
params.each do |param|
|
|
157
|
+
ident = kotlin_ident(SwiftCodegen.camel_case(param.name))
|
|
158
|
+
encoded = param.type == "string" ? "it" : "it.toString()"
|
|
159
|
+
if param.optional?
|
|
160
|
+
lines << " #{ident}?.let { put(#{kotlin_string(param.name)}, #{encoded}) }"
|
|
161
|
+
else
|
|
162
|
+
direct = param.type == "string" ? ident : "#{ident}.toString()"
|
|
163
|
+
lines << " put(#{kotlin_string(param.name)}, #{direct})"
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
lines << " }"
|
|
167
|
+
lines
|
|
168
|
+
else
|
|
169
|
+
entries = params.map do |param|
|
|
170
|
+
ident = kotlin_ident(SwiftCodegen.camel_case(param.name))
|
|
171
|
+
expr = param.type == "string" ? ident : "#{ident}.toString()"
|
|
172
|
+
"#{kotlin_string(param.name)} to #{expr}"
|
|
173
|
+
end
|
|
174
|
+
[" override val #{property_name}: Map<String, String> = mapOf(#{entries.join(', ')})"]
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def kotlin_field_type(field)
|
|
179
|
+
base =
|
|
180
|
+
case field.kind
|
|
181
|
+
when :scalar
|
|
182
|
+
SCALAR_TYPES.fetch(field.type)
|
|
183
|
+
when :one
|
|
184
|
+
SwiftCodegen.nested_type_name(field)
|
|
185
|
+
when :many
|
|
186
|
+
"List<#{SwiftCodegen.nested_type_name(field)}>"
|
|
187
|
+
when :many_scalar
|
|
188
|
+
"List<#{SCALAR_TYPES.fetch(field.type)}>"
|
|
189
|
+
end
|
|
190
|
+
field.optional? ? "#{base}?" : base
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def tree_needs_serial_name?(fields)
|
|
194
|
+
fields.any? do |field|
|
|
195
|
+
SwiftCodegen.camel_case(field.name) != field.name ||
|
|
196
|
+
((field.one? || field.many?) && tree_needs_serial_name?(field.fields))
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def kotlin_ident(name)
|
|
201
|
+
KEYWORDS.include?(name) ? "`#{name}`" : name
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def kotlin_string(value)
|
|
205
|
+
SwiftCodegen.swift_string(value).gsub("$", "\\$")
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
end
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "yaml"
|
|
3
|
+
require_relative "runner"
|
|
4
|
+
|
|
5
|
+
module InstantRecord
|
|
6
|
+
class Compiler
|
|
7
|
+
# Canonical manifest (`instant_record.manifest.json`) emitted during staging
|
|
8
|
+
# and consumed by EntryBuilder, WasmBuild, NativeBuild, worker.js, and main.js.
|
|
9
|
+
class Manifest
|
|
10
|
+
SCHEMA_VERSION = 1
|
|
11
|
+
FILENAME = "instant_record.manifest.json".freeze
|
|
12
|
+
CONFIG_FILE = "config/instant_record.yml".freeze
|
|
13
|
+
INCLUDE_KEYS = %w[models controllers views partials helpers migrations files].freeze
|
|
14
|
+
|
|
15
|
+
attr_reader :data
|
|
16
|
+
|
|
17
|
+
def self.from_json(json_text)
|
|
18
|
+
new(JSON.parse(json_text))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.read(path)
|
|
22
|
+
from_json(File.read(path))
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.empty_includes
|
|
26
|
+
INCLUDE_KEYS.to_h { |key| [key, []] }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Load optional declarative overrides from config/instant_record.yml.
|
|
30
|
+
def self.load_config(source_root, island: nil)
|
|
31
|
+
path = File.join(source_root.to_s, CONFIG_FILE)
|
|
32
|
+
return { include: empty_includes, server_routes: [] } unless File.file?(path)
|
|
33
|
+
|
|
34
|
+
raw = YAML.safe_load_file(path, permitted_classes: [Symbol]) || {}
|
|
35
|
+
raise BuildError, "#{CONFIG_FILE} must be a YAML mapping" unless raw.is_a?(Hash)
|
|
36
|
+
|
|
37
|
+
base = normalize_overrides(raw)
|
|
38
|
+
if island && raw["islands"].is_a?(Hash)
|
|
39
|
+
island_section = raw["islands"][island.to_s] || raw["islands"][island.to_sym]
|
|
40
|
+
base = merge_overrides(base, normalize_overrides(island_section)) if island_section.is_a?(Hash)
|
|
41
|
+
end
|
|
42
|
+
base
|
|
43
|
+
rescue Psych::SyntaxError => e
|
|
44
|
+
raise BuildError, "Invalid YAML in #{CONFIG_FILE}: #{e.message}"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.normalize_overrides(raw)
|
|
48
|
+
result = { include: empty_includes, server_routes: [] }
|
|
49
|
+
return result unless raw.is_a?(Hash)
|
|
50
|
+
|
|
51
|
+
inc = raw["include"] || raw[:include]
|
|
52
|
+
if inc.nil? && (raw.keys.map(&:to_s) & INCLUDE_KEYS).any?
|
|
53
|
+
inc = raw
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
if inc.is_a?(Hash)
|
|
57
|
+
INCLUDE_KEYS.each do |key|
|
|
58
|
+
values = inc[key] || inc[key.to_sym]
|
|
59
|
+
result[:include][key] = Array(values).map(&:to_s).reject(&:empty?).uniq
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
routes = raw["server_routes"] || raw[:server_routes]
|
|
64
|
+
if routes
|
|
65
|
+
result[:server_routes] = Array(routes).map { |spec| parse_server_route_spec(spec) }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
result
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def self.merge_overrides(*list)
|
|
72
|
+
merged = { include: empty_includes, server_routes: [] }
|
|
73
|
+
list.compact.each do |entry|
|
|
74
|
+
norm = normalize_overrides(entry)
|
|
75
|
+
INCLUDE_KEYS.each do |key|
|
|
76
|
+
merged[:include][key] = (merged[:include][key] + Array(norm[:include][key])).uniq
|
|
77
|
+
end
|
|
78
|
+
merged[:server_routes] = deduplicate_routes(merged[:server_routes] + Array(norm[:server_routes]))
|
|
79
|
+
end
|
|
80
|
+
merged
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def self.deduplicate_routes(routes)
|
|
84
|
+
seen = {}
|
|
85
|
+
routes.each_with_object([]) do |route, acc|
|
|
86
|
+
key = [route["method"], route["path"]]
|
|
87
|
+
next if seen[key]
|
|
88
|
+
|
|
89
|
+
seen[key] = true
|
|
90
|
+
acc << route
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def self.parse_server_route_spec(spec)
|
|
95
|
+
case spec
|
|
96
|
+
when String
|
|
97
|
+
trimmed = spec.strip
|
|
98
|
+
raise BuildError, "server_routes entry must not be blank" if trimmed.empty?
|
|
99
|
+
|
|
100
|
+
parts = trimmed.split(/\s+/, 2)
|
|
101
|
+
if parts.length == 2
|
|
102
|
+
method_name, path = parts
|
|
103
|
+
else
|
|
104
|
+
method_name = "GET"
|
|
105
|
+
path = parts.first
|
|
106
|
+
end
|
|
107
|
+
{
|
|
108
|
+
"method" => method_name.upcase,
|
|
109
|
+
"path" => normalize_route_path(path),
|
|
110
|
+
"controller" => nil,
|
|
111
|
+
"action" => nil
|
|
112
|
+
}
|
|
113
|
+
when Hash
|
|
114
|
+
method_name = (spec["method"] || spec[:method] || "GET").to_s.upcase
|
|
115
|
+
path = (spec["path"] || spec[:path]).to_s.strip
|
|
116
|
+
raise BuildError, "server_routes entry must include a path: #{spec.inspect}" if path.empty?
|
|
117
|
+
|
|
118
|
+
{
|
|
119
|
+
"method" => method_name,
|
|
120
|
+
"path" => normalize_route_path(path),
|
|
121
|
+
"controller" => (spec["controller"] || spec[:controller])&.to_s,
|
|
122
|
+
"action" => (spec["action"] || spec[:action])&.to_s
|
|
123
|
+
}
|
|
124
|
+
else
|
|
125
|
+
raise BuildError, "Unsupported server_routes entry: #{spec.inspect}"
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def self.normalize_route_path(path)
|
|
130
|
+
cleaned = path.to_s.strip
|
|
131
|
+
cleaned = "/#{cleaned}" unless cleaned.start_with?("/")
|
|
132
|
+
cleaned = cleaned.gsub(%r{/+}, "/")
|
|
133
|
+
cleaned.length > 1 ? cleaned.delete_suffix("/") : cleaned
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def initialize(data)
|
|
137
|
+
@data = data
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def schema_version = @data["schema_version"]
|
|
141
|
+
def mode = @data["mode"]
|
|
142
|
+
def island = @data["island"]
|
|
143
|
+
def island? = mode == "island"
|
|
144
|
+
def models = Array(@data["models"])
|
|
145
|
+
def local_routes = Array(@data["local_routes"])
|
|
146
|
+
def server_routes = Array(@data["server_routes"])
|
|
147
|
+
def staged = @data["staged"] || {}
|
|
148
|
+
def includes = @data["includes"] || {}
|
|
149
|
+
def diagnostics = Array(@data["diagnostics"])
|
|
150
|
+
|
|
151
|
+
def synced_model_names
|
|
152
|
+
models.map { |entry| entry["name"] }
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def sync_windows
|
|
156
|
+
models.each_with_object({}) do |entry, map|
|
|
157
|
+
map[entry["name"]] = entry["sync_window"] if entry["sync_window"]
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def sync_scopes
|
|
162
|
+
models.each_with_object({}) do |entry, map|
|
|
163
|
+
map[entry["name"]] = entry["sync_scope"] if entry["sync_scope"]
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def to_h = @data
|
|
168
|
+
|
|
169
|
+
def to_json(pretty: true)
|
|
170
|
+
pretty ? "#{JSON.pretty_generate(@data)}\n" : JSON.generate(@data)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def to_compact_json
|
|
174
|
+
JSON.generate(@data)
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|