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,406 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "uri"
|
|
3
|
+
|
|
4
|
+
module InstantRecord
|
|
5
|
+
module Mcp
|
|
6
|
+
# Coordinates the compiled native `host` process, HTTP sync exchange (with
|
|
7
|
+
# configurable token/cookie/header/scope auth), and the <1ms read-only
|
|
8
|
+
# SQLite replica reader under a single serial mutex.
|
|
9
|
+
class Runtime
|
|
10
|
+
attr_reader :database_path, :endpoint, :screens, :exchange
|
|
11
|
+
|
|
12
|
+
def initialize(
|
|
13
|
+
host_binary: nil,
|
|
14
|
+
database_path:,
|
|
15
|
+
endpoint: nil,
|
|
16
|
+
token: nil,
|
|
17
|
+
cookie: nil,
|
|
18
|
+
headers: {},
|
|
19
|
+
scope: nil,
|
|
20
|
+
offline: false,
|
|
21
|
+
timeout: HttpExchange::DEFAULT_TIMEOUT,
|
|
22
|
+
models: nil,
|
|
23
|
+
screens: [],
|
|
24
|
+
exchange: nil,
|
|
25
|
+
host_process: nil,
|
|
26
|
+
err: nil
|
|
27
|
+
)
|
|
28
|
+
@database_path = File.expand_path(database_path.to_s)
|
|
29
|
+
@endpoint = normalize_endpoint(endpoint)
|
|
30
|
+
@screens = Array(screens).map(&:to_s).sort.uniq
|
|
31
|
+
@models = models
|
|
32
|
+
@mutex = Mutex.new
|
|
33
|
+
|
|
34
|
+
@exchange = exchange || HttpExchange.new(
|
|
35
|
+
token: token,
|
|
36
|
+
cookie: cookie,
|
|
37
|
+
headers: headers,
|
|
38
|
+
scope: scope,
|
|
39
|
+
offline: offline,
|
|
40
|
+
timeout: timeout
|
|
41
|
+
)
|
|
42
|
+
@host = host_process || HostProcess.new(
|
|
43
|
+
binary: host_binary,
|
|
44
|
+
database: @database_path,
|
|
45
|
+
exchange: @exchange,
|
|
46
|
+
err: err
|
|
47
|
+
)
|
|
48
|
+
@replica = Replica.new(database_path: @database_path, models: @models)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def offline?
|
|
52
|
+
@exchange.respond_to?(:offline?) ? @exchange.offline? : false
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def offline=(value)
|
|
56
|
+
@exchange.offline = !!value if @exchange.respond_to?(:offline=)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def status
|
|
60
|
+
@mutex.synchronize do
|
|
61
|
+
started = monotonic_now
|
|
62
|
+
pos = parse_cursor(@host.call("cursor", "").out)
|
|
63
|
+
models_summary = @replica.models["models"]
|
|
64
|
+
{
|
|
65
|
+
"database" => @database_path,
|
|
66
|
+
"endpoint" => @endpoint,
|
|
67
|
+
"offline" => offline?,
|
|
68
|
+
"cursor" => pos["cursor"].to_i,
|
|
69
|
+
"pending" => pos["pending"].to_i,
|
|
70
|
+
"bootstrap" => pos["bootstrap"] || "needed",
|
|
71
|
+
"identity" => pos["identity"].to_s,
|
|
72
|
+
"screens" => @screens,
|
|
73
|
+
"models" => models_summary,
|
|
74
|
+
"elapsed_ms" => elapsed_ms(started)
|
|
75
|
+
}
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def sync(endpoint: nil)
|
|
80
|
+
@mutex.synchronize do
|
|
81
|
+
started = monotonic_now
|
|
82
|
+
target = normalize_endpoint(endpoint) || @endpoint
|
|
83
|
+
raise ArgumentError, "No sync endpoint configured. Pass endpoint or set INSTANT_RECORD_ENDPOINT." if target.nil?
|
|
84
|
+
|
|
85
|
+
if offline?
|
|
86
|
+
pos = parse_cursor(@host.call("cursor", "").out)
|
|
87
|
+
return {
|
|
88
|
+
"ok" => false,
|
|
89
|
+
"offline" => true,
|
|
90
|
+
"endpoint" => target,
|
|
91
|
+
"error" => "Client is in offline mode",
|
|
92
|
+
"cursor" => pos["cursor"].to_i,
|
|
93
|
+
"pending" => pos["pending"].to_i,
|
|
94
|
+
"bootstrap" => pos["bootstrap"] || "needed",
|
|
95
|
+
"identity" => pos["identity"].to_s,
|
|
96
|
+
"elapsed_ms" => elapsed_ms(started)
|
|
97
|
+
}
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
@exchange.reset_trace! if @exchange.respond_to?(:reset_trace!)
|
|
101
|
+
result = @host.call("sync", target)
|
|
102
|
+
pos = parse_cursor(@host.call("cursor", "").out)
|
|
103
|
+
@replica.refresh_schema!
|
|
104
|
+
|
|
105
|
+
went_offline = @exchange.respond_to?(:any_offline?) && @exchange.any_offline?
|
|
106
|
+
http_err = @exchange.respond_to?(:last_error_status) ? @exchange.last_error_status : nil
|
|
107
|
+
last_http = @exchange.respond_to?(:last_status) ? @exchange.last_status : nil
|
|
108
|
+
changed = result.out.start_with?("changed")
|
|
109
|
+
ok = !went_offline && http_err.nil?
|
|
110
|
+
|
|
111
|
+
payload = {
|
|
112
|
+
"ok" => ok,
|
|
113
|
+
"offline" => went_offline,
|
|
114
|
+
"endpoint" => target,
|
|
115
|
+
"changed" => changed,
|
|
116
|
+
"summary" => result.out,
|
|
117
|
+
"notice" => result.notice,
|
|
118
|
+
"cursor" => pos["cursor"].to_i,
|
|
119
|
+
"pending" => pos["pending"].to_i,
|
|
120
|
+
"bootstrap" => pos["bootstrap"] || "needed",
|
|
121
|
+
"identity" => pos["identity"].to_s,
|
|
122
|
+
"http_status" => last_http,
|
|
123
|
+
"elapsed_ms" => elapsed_ms(started)
|
|
124
|
+
}
|
|
125
|
+
if went_offline
|
|
126
|
+
payload["error"] = "Sync server unreachable (offline)"
|
|
127
|
+
elsif http_err
|
|
128
|
+
payload["error"] = "Sync server returned HTTP #{http_err}"
|
|
129
|
+
end
|
|
130
|
+
payload
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def list_models
|
|
135
|
+
@mutex.synchronize { @replica.models }
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def query_records(model:, where: {}, order: nil, limit: Replica::DEFAULT_LIMIT, offset: 0, columns: nil)
|
|
139
|
+
@mutex.synchronize do
|
|
140
|
+
@replica.query(model: model, where: where, order: order, limit: limit, offset: offset, columns: columns)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def get_record(model:, id:)
|
|
145
|
+
@mutex.synchronize { @replica.find(model: model, id: id) }
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def search_records(query:, models: nil, columns: nil, limit: Replica::DEFAULT_LIMIT)
|
|
149
|
+
@mutex.synchronize { @replica.search(query: query, models: models, columns: columns, limit: limit) }
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def outbox(limit: 100)
|
|
153
|
+
@mutex.synchronize { @replica.outbox(limit: limit) }
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def render_screen(screen:, params: {})
|
|
157
|
+
@mutex.synchronize do
|
|
158
|
+
started = monotonic_now
|
|
159
|
+
input = JSON.generate("screen" => screen.to_s, "params" => stringify_map(params))
|
|
160
|
+
result = @host.call("render", input)
|
|
161
|
+
{
|
|
162
|
+
"screen" => screen.to_s,
|
|
163
|
+
"params" => stringify_map(params),
|
|
164
|
+
"tree" => JSON.parse(result.out),
|
|
165
|
+
"elapsed_ms" => elapsed_ms(started)
|
|
166
|
+
}
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def perform_action(screen:, action:, params: {}, args: {}, values: {}, render: false, sync: false)
|
|
171
|
+
payload = @mutex.synchronize do
|
|
172
|
+
started = monotonic_now
|
|
173
|
+
str_params = stringify_map(params)
|
|
174
|
+
str_args = stringify_map(args)
|
|
175
|
+
str_values = stringify_map(values)
|
|
176
|
+
input = JSON.generate(
|
|
177
|
+
"screen" => screen.to_s,
|
|
178
|
+
"params" => str_params,
|
|
179
|
+
"action" => action.to_s,
|
|
180
|
+
"args" => str_args,
|
|
181
|
+
"values" => str_values
|
|
182
|
+
)
|
|
183
|
+
result = @host.call("action", input)
|
|
184
|
+
pos = parse_cursor(@host.call("cursor", "").out)
|
|
185
|
+
rendered =
|
|
186
|
+
if render
|
|
187
|
+
JSON.parse(@host.call("render", JSON.generate("screen" => screen.to_s, "params" => str_params)).out)
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
{
|
|
191
|
+
"ok" => true,
|
|
192
|
+
"screen" => screen.to_s,
|
|
193
|
+
"action" => action.to_s,
|
|
194
|
+
"pending" => pos["pending"].to_i,
|
|
195
|
+
"notice" => result.notice,
|
|
196
|
+
"rendered" => rendered,
|
|
197
|
+
"elapsed_ms" => elapsed_ms(started)
|
|
198
|
+
}
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
payload["sync"] = self.sync if sync
|
|
202
|
+
payload
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def app_request(method:, path:, params: nil, body: nil, headers: {}, content_type: nil, sync: false)
|
|
206
|
+
payload = @mutex.synchronize do
|
|
207
|
+
started = monotonic_now
|
|
208
|
+
verb = method.to_s.upcase
|
|
209
|
+
raise ArgumentError, "HTTP method is required" if verb.empty?
|
|
210
|
+
|
|
211
|
+
request_path = path.to_s
|
|
212
|
+
request_path = "/#{request_path}" unless request_path.start_with?("/")
|
|
213
|
+
|
|
214
|
+
raw_body, resolved_content_type = encode_request_body(verb, params, body, content_type)
|
|
215
|
+
if verb == "GET" && params.is_a?(Hash) && !params.empty?
|
|
216
|
+
sep = request_path.include?("?") ? "&" : "?"
|
|
217
|
+
request_path = "#{request_path}#{sep}#{encode_form_params(params)}"
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
raw_http = build_raw_http_request(verb, request_path, raw_body, resolved_content_type, headers)
|
|
221
|
+
result = @host.call("request", raw_http)
|
|
222
|
+
parsed = parse_raw_http_response(result.out)
|
|
223
|
+
pos = parse_cursor(@host.call("cursor", "").out)
|
|
224
|
+
|
|
225
|
+
parsed_json = nil
|
|
226
|
+
if parsed[:headers]["content-type"].to_s.include?("json") && !parsed[:body].empty?
|
|
227
|
+
parsed_json = JSON.parse(parsed[:body]) rescue nil
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
{
|
|
231
|
+
"ok" => parsed[:status] >= 200 && parsed[:status] < 400,
|
|
232
|
+
"status" => parsed[:status],
|
|
233
|
+
"headers" => parsed[:headers],
|
|
234
|
+
"body" => parsed[:body],
|
|
235
|
+
"json" => parsed_json,
|
|
236
|
+
"pending" => pos["pending"].to_i,
|
|
237
|
+
"notice" => result.notice,
|
|
238
|
+
"elapsed_ms" => elapsed_ms(started)
|
|
239
|
+
}
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
payload["sync"] = self.sync if sync && payload["ok"]
|
|
243
|
+
payload
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def fetch_history(key:, query: nil, endpoint: nil)
|
|
247
|
+
@mutex.synchronize do
|
|
248
|
+
started = monotonic_now
|
|
249
|
+
if query.nil? || query.to_s.strip.empty?
|
|
250
|
+
result = @host.call("history", JSON.generate("key" => key.to_s))
|
|
251
|
+
parsed = JSON.parse(result.out)
|
|
252
|
+
return parsed.merge("key" => key.to_s, "elapsed_ms" => elapsed_ms(started))
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
target = normalize_endpoint(endpoint) || @endpoint
|
|
256
|
+
raise ArgumentError, "No sync endpoint configured for history fetch." if target.nil?
|
|
257
|
+
raise Error, "Client is in offline mode" if offline?
|
|
258
|
+
|
|
259
|
+
raw_reply = @exchange.call("GET #{target}/records?#{query}\n")
|
|
260
|
+
status_line, body = raw_reply.to_s.split("\n", 2)
|
|
261
|
+
status = status_line.to_i
|
|
262
|
+
if status < 200 || status >= 300
|
|
263
|
+
return {
|
|
264
|
+
"ok" => false,
|
|
265
|
+
"key" => key.to_s,
|
|
266
|
+
"offline" => status == 0,
|
|
267
|
+
"http_status" => status,
|
|
268
|
+
"error" => status == 0 ? "History server unreachable (offline)" : "History request returned HTTP #{status}",
|
|
269
|
+
"elapsed_ms" => elapsed_ms(started)
|
|
270
|
+
}
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
result = @host.call("history", JSON.generate("key" => key.to_s, "response" => body.to_s))
|
|
274
|
+
parsed = JSON.parse(result.out)
|
|
275
|
+
parsed.merge("key" => key.to_s, "http_status" => status, "elapsed_ms" => elapsed_ms(started))
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def open_database(path:, endpoint: nil)
|
|
280
|
+
@mutex.synchronize do
|
|
281
|
+
started = monotonic_now
|
|
282
|
+
expanded = File.expand_path(path.to_s)
|
|
283
|
+
@host.open(expanded)
|
|
284
|
+
@database_path = expanded
|
|
285
|
+
@endpoint = normalize_endpoint(endpoint) if endpoint
|
|
286
|
+
@replica.reopen(expanded)
|
|
287
|
+
pos = parse_cursor(@host.call("cursor", "").out)
|
|
288
|
+
{
|
|
289
|
+
"ok" => true,
|
|
290
|
+
"database" => @database_path,
|
|
291
|
+
"endpoint" => @endpoint,
|
|
292
|
+
"cursor" => pos["cursor"].to_i,
|
|
293
|
+
"pending" => pos["pending"].to_i,
|
|
294
|
+
"bootstrap" => pos["bootstrap"] || "needed",
|
|
295
|
+
"identity" => pos["identity"].to_s,
|
|
296
|
+
"elapsed_ms" => elapsed_ms(started)
|
|
297
|
+
}
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def close
|
|
302
|
+
@mutex.synchronize do
|
|
303
|
+
@replica.close
|
|
304
|
+
@host.close
|
|
305
|
+
end
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
private
|
|
309
|
+
|
|
310
|
+
def parse_cursor(raw)
|
|
311
|
+
raw.to_s.split.each_with_object({}) do |pair, acc|
|
|
312
|
+
key, value = pair.split("=", 2)
|
|
313
|
+
acc[key] = value.to_s if key
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def stringify_map(hash)
|
|
318
|
+
return {} unless hash.is_a?(Hash)
|
|
319
|
+
|
|
320
|
+
hash.each_with_object({}) { |(k, v), acc| acc[k.to_s] = v.to_s }
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
def normalize_endpoint(endpoint)
|
|
324
|
+
str = endpoint.to_s.strip.sub(%r{/+\z}, "")
|
|
325
|
+
str.empty? ? nil : str
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
def encode_request_body(verb, params, body, content_type)
|
|
329
|
+
return ["", nil] if verb == "GET" || verb == "HEAD"
|
|
330
|
+
|
|
331
|
+
if !body.nil?
|
|
332
|
+
payload = body.is_a?(String) ? body : JSON.generate(body)
|
|
333
|
+
ct = content_type || (body.is_a?(String) ? "application/x-www-form-urlencoded" : "application/json")
|
|
334
|
+
return [payload, ct]
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
if params.is_a?(Hash) && !params.empty?
|
|
338
|
+
if content_type.to_s.include?("json")
|
|
339
|
+
[JSON.generate(params), content_type]
|
|
340
|
+
else
|
|
341
|
+
[encode_form_params(params), content_type || "application/x-www-form-urlencoded"]
|
|
342
|
+
end
|
|
343
|
+
else
|
|
344
|
+
["", content_type]
|
|
345
|
+
end
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
# Nested Rails-style form encoding: { "issue" => { "title" => "Hi" } } -> "issue[title]=Hi"
|
|
349
|
+
def encode_form_params(params, prefix = nil)
|
|
350
|
+
pairs = []
|
|
351
|
+
params.each do |key, value|
|
|
352
|
+
full_key = prefix ? "#{prefix}[#{key}]" : key.to_s
|
|
353
|
+
case value
|
|
354
|
+
when Hash
|
|
355
|
+
pairs << encode_form_params(value, full_key)
|
|
356
|
+
when Array
|
|
357
|
+
value.each { |item| pairs << "#{URI.encode_www_form_component("#{full_key}[]")}=#{URI.encode_www_form_component(item.to_s)}" }
|
|
358
|
+
else
|
|
359
|
+
pairs << "#{URI.encode_www_form_component(full_key)}=#{URI.encode_www_form_component(value.to_s)}"
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
pairs.reject(&:empty?).join("&")
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
def build_raw_http_request(verb, path, body, content_type, headers)
|
|
366
|
+
bytes = body.to_s.b
|
|
367
|
+
lines = ["#{verb} #{path} HTTP/1.1", "Host: localhost"]
|
|
368
|
+
custom = headers.is_a?(Hash) ? headers : {}
|
|
369
|
+
custom.each do |key, value|
|
|
370
|
+
next if key.to_s.casecmp?("host") || key.to_s.casecmp?("content-length")
|
|
371
|
+
|
|
372
|
+
lines << "#{key}: #{value}"
|
|
373
|
+
end
|
|
374
|
+
if bytes.bytesize > 0
|
|
375
|
+
unless custom.keys.any? { |k| k.to_s.casecmp?("content-type") }
|
|
376
|
+
lines << "Content-Type: #{content_type || 'application/x-www-form-urlencoded'}"
|
|
377
|
+
end
|
|
378
|
+
lines << "Content-Length: #{bytes.bytesize}"
|
|
379
|
+
end
|
|
380
|
+
"#{lines.join("\r\n")}\r\n\r\n".b + bytes
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
def parse_raw_http_response(raw)
|
|
384
|
+
head, body = raw.to_s.split("\n\n", 2)
|
|
385
|
+
lines = head.to_s.split("\n")
|
|
386
|
+
status = lines.first.to_i
|
|
387
|
+
headers = {}
|
|
388
|
+
Array(lines[1..]).each do |line|
|
|
389
|
+
next unless line.include?(":")
|
|
390
|
+
|
|
391
|
+
name, value = line.split(":", 2)
|
|
392
|
+
headers[name.strip.downcase] = value.to_s.strip
|
|
393
|
+
end
|
|
394
|
+
{ status: status, headers: headers, body: body.to_s }
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
def monotonic_now
|
|
398
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
def elapsed_ms(started)
|
|
402
|
+
((monotonic_now - started) * 1000.0).round(3)
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
end
|
|
406
|
+
end
|