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
|
@@ -9,8 +9,23 @@ module InstantRecord
|
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
def self.set(key, value)
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
if InstantRecord.browser?
|
|
13
|
+
record = find_or_initialize_by(key: key)
|
|
14
|
+
record.update!(value: value.to_s)
|
|
15
|
+
else
|
|
16
|
+
begin
|
|
17
|
+
transaction(requires_new: true) do
|
|
18
|
+
record = find_or_initialize_by(key: key)
|
|
19
|
+
record.update!(value: value.to_s)
|
|
20
|
+
end
|
|
21
|
+
rescue ActiveRecord::RecordNotUnique
|
|
22
|
+
where(key: key).update_all(value: value.to_s, updated_at: Time.now.utc)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.delete(key)
|
|
28
|
+
where(key: key).delete_all
|
|
14
29
|
end
|
|
15
30
|
end
|
|
16
31
|
end
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
require "securerandom"
|
|
2
|
+
|
|
3
|
+
module InstantRecord
|
|
4
|
+
# Local non-destructive quarantine ledger for offline writes the server
|
|
5
|
+
# rejected or last-write-wins overwrote. A notice keeps the user's attempted
|
|
6
|
+
# changes alongside the winning server attributes and human-readable reason so
|
|
7
|
+
# the write can be retried, forked into a new record, or dismissed explicitly.
|
|
8
|
+
class SyncNotice < ActiveRecord::Base
|
|
9
|
+
self.table_name = "instant_record_sync_notices"
|
|
10
|
+
|
|
11
|
+
KINDS = %w[rejected conflict].freeze
|
|
12
|
+
NON_WRITABLE_COLUMNS = %w[id server_version sync_state created_at updated_at].freeze
|
|
13
|
+
|
|
14
|
+
serialize :attempted_changes, coder: JSON
|
|
15
|
+
serialize :server_attributes, coder: JSON
|
|
16
|
+
|
|
17
|
+
before_create { self.id = SecureRandom.uuid if id.blank? }
|
|
18
|
+
|
|
19
|
+
scope :active, -> { where(dismissed_at: nil) }
|
|
20
|
+
scope :recent, -> { order(created_at: :desc, id: :desc) }
|
|
21
|
+
scope :for_record, ->(type, record_id) { where(record_type: type.to_s, record_id: record_id.to_s) }
|
|
22
|
+
|
|
23
|
+
# Record a notice once per mutation_id (when present), preserving attempted
|
|
24
|
+
# local changes and winning server attributes as plain string-keyed hashes.
|
|
25
|
+
def self.record!(kind:, record_type:, record_id:, operation:, reason:, attempted_changes: {}, server_attributes: {}, server_version: 0, mutation_id: nil)
|
|
26
|
+
mid = mutation_id.to_s
|
|
27
|
+
if mid.length > 0
|
|
28
|
+
existing = find_by(mutation_id: mid)
|
|
29
|
+
return existing if existing
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
create!(
|
|
33
|
+
mutation_id: mid.length > 0 ? mid : nil,
|
|
34
|
+
kind: kind.to_s,
|
|
35
|
+
record_type: record_type.to_s,
|
|
36
|
+
record_id: record_id.to_s,
|
|
37
|
+
operation: operation.to_s,
|
|
38
|
+
reason: reason.to_s,
|
|
39
|
+
attempted_changes: normalize_hash(attempted_changes),
|
|
40
|
+
server_attributes: normalize_hash(server_attributes),
|
|
41
|
+
server_version: server_version.to_i
|
|
42
|
+
)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Dispatch one user resolution ("retry", "fork", "dismiss") by id and return
|
|
46
|
+
# a JSON-ready status hash for bridge and HTTP callers.
|
|
47
|
+
def self.resolve(id, action:, overrides: {})
|
|
48
|
+
notice = find_by(id: id.to_s)
|
|
49
|
+
raise ArgumentError, "Sync notice not found: #{id}" unless notice
|
|
50
|
+
|
|
51
|
+
clean_overrides = normalize_hash(overrides)
|
|
52
|
+
record =
|
|
53
|
+
case action.to_s
|
|
54
|
+
when "dismiss"
|
|
55
|
+
notice.dismiss!
|
|
56
|
+
nil
|
|
57
|
+
when "retry"
|
|
58
|
+
notice.retry!(clean_overrides)
|
|
59
|
+
when "fork"
|
|
60
|
+
notice.fork!(clean_overrides)
|
|
61
|
+
else
|
|
62
|
+
raise ArgumentError, "Unknown sync notice action: #{action}"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
{
|
|
66
|
+
"ok" => true,
|
|
67
|
+
"action" => action.to_s,
|
|
68
|
+
"notice" => notice.as_notice,
|
|
69
|
+
"record_id" => record ? record.id.to_s : nil,
|
|
70
|
+
"notices" => active.recent.map(&:as_notice)
|
|
71
|
+
}
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.normalize_hash(value)
|
|
75
|
+
return {} unless value.is_a?(Hash)
|
|
76
|
+
|
|
77
|
+
out = {}
|
|
78
|
+
value.each { |k, v| out[k.to_s] = v }
|
|
79
|
+
out
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def active?
|
|
83
|
+
dismissed_at.nil?
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def conflict?
|
|
87
|
+
kind == "conflict"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def rejected?
|
|
91
|
+
kind == "rejected"
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def dismiss!
|
|
95
|
+
update!(dismissed_at: Time.now.utc)
|
|
96
|
+
self
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Re-apply the quarantined local write so Syncable enqueues a fresh outbox
|
|
100
|
+
# mutation with a new timestamp and mutation_id, then mark this notice done.
|
|
101
|
+
def retry!(overrides = {})
|
|
102
|
+
model = synced_model!
|
|
103
|
+
record = nil
|
|
104
|
+
|
|
105
|
+
self.class.transaction do
|
|
106
|
+
case operation
|
|
107
|
+
when "create"
|
|
108
|
+
attrs = writable_attributes(model, attempted_changes, overrides)
|
|
109
|
+
record = model.find_by(id: record_id)
|
|
110
|
+
if record
|
|
111
|
+
record.update!(attrs)
|
|
112
|
+
else
|
|
113
|
+
record = model.create!(attrs.merge("id" => record_id))
|
|
114
|
+
end
|
|
115
|
+
when "update"
|
|
116
|
+
record = model.find_by(id: record_id)
|
|
117
|
+
if record
|
|
118
|
+
attrs = writable_attributes(model, attempted_changes, overrides)
|
|
119
|
+
record.assign_attributes(attrs)
|
|
120
|
+
bump_updated_at!(record)
|
|
121
|
+
record.save!
|
|
122
|
+
else
|
|
123
|
+
attrs = writable_attributes(model, server_attributes, attempted_changes, overrides)
|
|
124
|
+
record = model.create!(attrs.merge("id" => record_id))
|
|
125
|
+
end
|
|
126
|
+
when "destroy"
|
|
127
|
+
record = model.find_by(id: record_id)
|
|
128
|
+
record.destroy! if record
|
|
129
|
+
else
|
|
130
|
+
raise ArgumentError, "Unknown operation: #{operation}"
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
dismiss!
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
record
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Create a brand-new record (fresh UUID) carrying the user's attempted local
|
|
140
|
+
# changes on top of the server/current base attributes, leaving the winning
|
|
141
|
+
# server record untouched.
|
|
142
|
+
def fork!(overrides = {})
|
|
143
|
+
model = synced_model!
|
|
144
|
+
base = server_attributes.is_a?(Hash) && !server_attributes.empty? ? server_attributes : current_record_attributes(model)
|
|
145
|
+
attrs = writable_attributes(model, base, attempted_changes, overrides)
|
|
146
|
+
record = nil
|
|
147
|
+
|
|
148
|
+
self.class.transaction do
|
|
149
|
+
record = model.create!(attrs)
|
|
150
|
+
dismiss!
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
record
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Field-level comparison between attempted local values and winning server
|
|
157
|
+
# attributes, excluding gem bookkeeping columns.
|
|
158
|
+
def field_diffs
|
|
159
|
+
attempted = self.class.normalize_hash(attempted_changes)
|
|
160
|
+
server = self.class.normalize_hash(server_attributes)
|
|
161
|
+
keys =
|
|
162
|
+
if operation == "destroy"
|
|
163
|
+
server.keys - NON_WRITABLE_COLUMNS
|
|
164
|
+
else
|
|
165
|
+
attempted.keys - NON_WRITABLE_COLUMNS
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
diffs = []
|
|
169
|
+
keys.each do |field|
|
|
170
|
+
local_val = operation == "destroy" ? nil : attempted[field]
|
|
171
|
+
server_val = server[field]
|
|
172
|
+
next if local_val == server_val && !server.empty?
|
|
173
|
+
|
|
174
|
+
diffs << { "field" => field, "local" => local_val, "server" => server_val }
|
|
175
|
+
end
|
|
176
|
+
diffs
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def as_notice
|
|
180
|
+
{
|
|
181
|
+
"id" => id.to_s,
|
|
182
|
+
"mutation_id" => mutation_id.to_s,
|
|
183
|
+
"kind" => kind.to_s,
|
|
184
|
+
"record_type" => record_type.to_s,
|
|
185
|
+
"record_id" => record_id.to_s,
|
|
186
|
+
"operation" => operation.to_s,
|
|
187
|
+
"reason" => reason.to_s,
|
|
188
|
+
"attempted_changes" => self.class.normalize_hash(attempted_changes),
|
|
189
|
+
"server_attributes" => self.class.normalize_hash(server_attributes),
|
|
190
|
+
"server_version" => server_version.to_i,
|
|
191
|
+
"dismissed_at" => dismissed_at ? dismissed_at.utc.iso8601(TIME_PRECISION) : nil,
|
|
192
|
+
"created_at" => created_at ? created_at.utc.iso8601(TIME_PRECISION) : nil
|
|
193
|
+
}
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
private
|
|
197
|
+
|
|
198
|
+
def synced_model!
|
|
199
|
+
model = InstantRecord.synced_model(record_type)
|
|
200
|
+
raise ArgumentError, "Unknown synced model: #{record_type}" unless model
|
|
201
|
+
|
|
202
|
+
model
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def current_record_attributes(model)
|
|
206
|
+
existing = model.find_by(id: record_id)
|
|
207
|
+
existing ? InstantRecord.wire_attributes(existing.attributes) : {}
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# Ensure a retried update always carries a timestamp strictly newer than
|
|
211
|
+
# the winning server row (even under clock skew) so LWW accepts the retry.
|
|
212
|
+
def bump_updated_at!(record)
|
|
213
|
+
return unless record.respond_to?(:updated_at=)
|
|
214
|
+
|
|
215
|
+
now = Time.now.utc
|
|
216
|
+
floor = record.updated_at ? record.updated_at.to_time.utc + 0.000001 : now
|
|
217
|
+
record.updated_at = now > floor ? now : floor
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def writable_attributes(model, *hashes)
|
|
221
|
+
allowed = model.column_names - NON_WRITABLE_COLUMNS
|
|
222
|
+
merged = {}
|
|
223
|
+
hashes.each do |hash|
|
|
224
|
+
next unless hash.is_a?(Hash)
|
|
225
|
+
|
|
226
|
+
hash.each do |key, value|
|
|
227
|
+
name = key.to_s
|
|
228
|
+
merged[name] = value if allowed.include?(name)
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
merged
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
module InstantRecord
|
|
2
|
+
# Multi-tenant partition declaration on a Syncable model (`sync_partition :column`).
|
|
3
|
+
# Partitioned models sync only when a request selects a matching partition,
|
|
4
|
+
# keeping per-account / per-workspace rows and change feeds isolated.
|
|
5
|
+
class SyncPartition
|
|
6
|
+
attr_reader :model, :column, :name
|
|
7
|
+
|
|
8
|
+
def initialize(model, column, name: nil)
|
|
9
|
+
@model = model
|
|
10
|
+
@column = column.to_s
|
|
11
|
+
@name = (name || column).to_s
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Extracts the partition value for this model from a client-supplied
|
|
15
|
+
# specifier, or returns nil when the specifier does not apply to this model.
|
|
16
|
+
#
|
|
17
|
+
# Accepted forms:
|
|
18
|
+
# "support" -> "support"
|
|
19
|
+
# "mailbox_id:support" -> "support" (matches name or column)
|
|
20
|
+
# "mailbox_id=support" -> "support" (matches name or column)
|
|
21
|
+
# { "mailbox_id" => "support" } -> "support" (Hash keyed by name or column)
|
|
22
|
+
def extract(spec)
|
|
23
|
+
return nil if spec.nil?
|
|
24
|
+
|
|
25
|
+
if spec.is_a?(Hash)
|
|
26
|
+
value = spec[@name]
|
|
27
|
+
value = spec[@name.to_sym] if value.nil?
|
|
28
|
+
value = spec[@column] if value.nil?
|
|
29
|
+
value = spec[@column.to_sym] if value.nil?
|
|
30
|
+
text = value.to_s
|
|
31
|
+
return text.empty? ? nil : text
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
raw = spec.to_s.strip
|
|
35
|
+
return nil if raw.empty?
|
|
36
|
+
|
|
37
|
+
if raw.include?(":")
|
|
38
|
+
prefix, value = raw.split(":", 2)
|
|
39
|
+
return (prefix == @name || prefix == @column) && !value.to_s.empty? ? value : nil
|
|
40
|
+
elsif raw.include?("=")
|
|
41
|
+
prefix, value = raw.split("=", 2)
|
|
42
|
+
return (prefix == @name || prefix == @column) && !value.to_s.empty? ? value : nil
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
raw
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Canonical key stored on InstantRecord::Change#partition so two models
|
|
49
|
+
# partitioned by different columns never collide.
|
|
50
|
+
def change_key(value)
|
|
51
|
+
text = value.to_s
|
|
52
|
+
text.empty? ? nil : "#{@name}:#{text}"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def change_partition_for(record)
|
|
56
|
+
change_key(record[@column])
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def scope_relation(relation, value)
|
|
60
|
+
relation.where(@column => value)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
require "digest"
|
|
2
|
+
require "instant_record/syncable"
|
|
3
|
+
require "instant_record/sync_partition"
|
|
4
|
+
require "instant_record/sync_shape"
|
|
5
|
+
|
|
6
|
+
module InstantRecord
|
|
7
|
+
module Syncable
|
|
8
|
+
class_methods do
|
|
9
|
+
# Which records the asker may be sent, for rules a single column cannot
|
|
10
|
+
# say (`sync_scope` is the short form for "its owner's, or everyone's").
|
|
11
|
+
# The block runs on the model, on the server only, with whatever
|
|
12
|
+
# `InstantRecord.config.scope_from` returned for the request, and returns
|
|
13
|
+
# a relation. Declare it inside `server_only`.
|
|
14
|
+
#
|
|
15
|
+
# server_only do
|
|
16
|
+
# syncable_to { |user| user ? where(team_id: user.team_ids) : none }
|
|
17
|
+
# end
|
|
18
|
+
def syncable_to(&block)
|
|
19
|
+
@instant_record_visibility = block
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Which records the asker may create, change and delete. Without it,
|
|
23
|
+
# what may be seen may be written. Pass `only:` (e.g. `only: :create`) to
|
|
24
|
+
# restrict allowed mutation operations.
|
|
25
|
+
def writable_by(only: nil, &block)
|
|
26
|
+
@instant_record_writability = block if block
|
|
27
|
+
sync_operations(*Array(only)) if only
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Restrict which mutation operations clients may apply to this model
|
|
31
|
+
# (e.g. `sync_operations :create` for append-only models).
|
|
32
|
+
def sync_operations(*operations)
|
|
33
|
+
@instant_record_writable_operations = operations.flatten.map(&:to_s).freeze
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Declare a server-side query shape for windowed bootstrap, keyset history
|
|
37
|
+
# paging, related rows, search widening, single-record lookup, and
|
|
38
|
+
# background history-fill. Declare inside `server_only`.
|
|
39
|
+
def sync_shape(**options)
|
|
40
|
+
@instant_record_sync_shape = InstantRecord::SyncShape.new(self, **options)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Authorize (and optionally prepare) a client-selected partition for this
|
|
44
|
+
# model on the server. Runs with `(partition_value, identity)` or
|
|
45
|
+
# `(partition_value, identity, request)`. Return falsey to forbid (403)
|
|
46
|
+
# or raise `InstantRecord::Unauthorized` / `InstantRecord::Forbidden`.
|
|
47
|
+
def authorize_partition(&block)
|
|
48
|
+
@instant_record_partition_authorizer = block
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def instant_record_visibility = @instant_record_visibility
|
|
52
|
+
def instant_record_writability = @instant_record_writability || @instant_record_visibility
|
|
53
|
+
def instant_record_writable_operations = @instant_record_writable_operations
|
|
54
|
+
def instant_record_sync_shape = @instant_record_sync_shape
|
|
55
|
+
def instant_record_partition_authorizer = @instant_record_partition_authorizer
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Who may be sent what, and who may write what, on the four sync endpoints.
|
|
60
|
+
# A model says so with `sync_scope :column`, `sync_partition :column`, or
|
|
61
|
+
# `syncable_to` and `writable_by`; all are read through here. Server only:
|
|
62
|
+
# none of this is in the browser build, which holds whatever the server chose
|
|
63
|
+
# to send.
|
|
64
|
+
module SyncScope
|
|
65
|
+
RESET = "reset".freeze
|
|
66
|
+
|
|
67
|
+
class << self
|
|
68
|
+
# Run the host app's optional request authenticator (may raise
|
|
69
|
+
# Unauthorized or Forbidden).
|
|
70
|
+
def authenticate!(request)
|
|
71
|
+
InstantRecord.config.authenticate_request&.call(request)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Whoever `config.scope_from` says is asking. A string or nil suits
|
|
75
|
+
# `sync_scope` columns; `syncable_to` blocks are handed it as it is.
|
|
76
|
+
def identity(request)
|
|
77
|
+
InstantRecord.config.scope_from.call(request)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Which multi-tenant partition the request targets, or nil for an
|
|
81
|
+
# unpartitioned sync session.
|
|
82
|
+
def partition(request, identity = nil)
|
|
83
|
+
resolver = InstantRecord.config.partition_from
|
|
84
|
+
return nil unless resolver
|
|
85
|
+
|
|
86
|
+
value = resolver.arity == 1 ? resolver.call(request) : resolver.call(request, identity)
|
|
87
|
+
value.is_a?(String) ? value.presence : value
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# The asker as a `sync_scope` column holds it.
|
|
91
|
+
def token(identity)
|
|
92
|
+
identity.to_s.presence
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# A name for the asker that changes when the person behind a browser
|
|
96
|
+
# does (sign in, sign out) or when the active partition changes.
|
|
97
|
+
# Not a secret and not a credential: the server always scopes by the
|
|
98
|
+
# request's own identity and partition.
|
|
99
|
+
def key(identity, partition: nil)
|
|
100
|
+
name =
|
|
101
|
+
if identity.respond_to?(:sync_identity_key) then identity.sync_identity_key
|
|
102
|
+
elsif identity.respond_to?(:id) then "#{identity.class.name}:#{identity.id}"
|
|
103
|
+
else identity
|
|
104
|
+
end
|
|
105
|
+
base = name.to_s.presence || "anonymous"
|
|
106
|
+
base = "#{base}|partition=#{partition}" if partition.present?
|
|
107
|
+
Digest::SHA256.hexdigest(base)[0, 16]
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Whether the key a client bootstrapped under is no longer this request's
|
|
111
|
+
# identity. A request that names no key is judged by who it is now: it is
|
|
112
|
+
# a client that synced before keys existed, or no sync client at all.
|
|
113
|
+
def stale?(sent_key, identity, partition: nil)
|
|
114
|
+
sent_key.present? && sent_key != key(identity, partition: partition)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def partitioned?(model)
|
|
118
|
+
!model.instant_record_sync_partition.nil?
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def partition_value(model, partition_spec)
|
|
122
|
+
model.instant_record_sync_partition&.extract(partition_spec)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def scoped?(model)
|
|
126
|
+
!model.instant_record_sync_scope.nil? || !model.instant_record_visibility.nil? || partitioned?(model)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Models participating in a sync pass for `partition_spec`:
|
|
130
|
+
# - Unpartitioned session (nil): all models without `sync_partition`.
|
|
131
|
+
# - Partitioned session: only `sync_partition` models matching `partition_spec`.
|
|
132
|
+
def models_for(partition_spec)
|
|
133
|
+
if partition_spec.nil?
|
|
134
|
+
InstantRecord.syncable_models.reject { |model| partitioned?(model) }
|
|
135
|
+
else
|
|
136
|
+
InstantRecord.syncable_models.select do |model|
|
|
137
|
+
partitioned?(model) && !partition_value(model, partition_spec).nil?
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Authorize a client's access to `partition_value` on `model`.
|
|
143
|
+
# Raises `InstantRecord::Forbidden` when the model's `authorize_partition`
|
|
144
|
+
# block returns falsey.
|
|
145
|
+
def authorize_partition!(model, partition_value, identity, request = nil)
|
|
146
|
+
authorizer = model.instant_record_partition_authorizer
|
|
147
|
+
return true unless authorizer
|
|
148
|
+
|
|
149
|
+
allowed =
|
|
150
|
+
case authorizer.arity
|
|
151
|
+
when 1 then model.instance_exec(partition_value, &authorizer)
|
|
152
|
+
when 2 then model.instance_exec(partition_value, identity, &authorizer)
|
|
153
|
+
else model.instance_exec(partition_value, identity, request, &authorizer)
|
|
154
|
+
end
|
|
155
|
+
raise InstantRecord::Forbidden, "partition not permitted" unless allowed
|
|
156
|
+
|
|
157
|
+
true
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Authorize all partitioned models in `models` for `partition_spec`.
|
|
161
|
+
def authorize_models!(models, partition_spec, identity, request = nil)
|
|
162
|
+
return true if partition_spec.nil?
|
|
163
|
+
|
|
164
|
+
models.each do |model|
|
|
165
|
+
next unless partitioned?(model)
|
|
166
|
+
|
|
167
|
+
value = partition_value(model, partition_spec)
|
|
168
|
+
raise InstantRecord::Forbidden, "partition not permitted" if value.nil?
|
|
169
|
+
|
|
170
|
+
authorize_partition!(model, value, identity, request)
|
|
171
|
+
end
|
|
172
|
+
true
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# Change-log partition keys for `models` under `partition_spec`.
|
|
176
|
+
# Empty array (or nil) selects unpartitioned changes (`partition IS NULL`).
|
|
177
|
+
def change_partitions_for(models, partition_spec)
|
|
178
|
+
return nil if partition_spec.nil?
|
|
179
|
+
|
|
180
|
+
models.filter_map do |model|
|
|
181
|
+
part = model.instant_record_sync_partition
|
|
182
|
+
next unless part
|
|
183
|
+
|
|
184
|
+
value = part.extract(partition_spec)
|
|
185
|
+
part.change_key(value) if value
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def visible(model, identity, partition: nil)
|
|
190
|
+
rows = model.instant_record_visible_to(token(identity))
|
|
191
|
+
rows = apply_partition(model, rows, partition)
|
|
192
|
+
both(model, rows, model.instant_record_visibility, identity)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def writable(model, identity, partition: nil)
|
|
196
|
+
column = model.instant_record_sync_scope
|
|
197
|
+
rows = column ? model.where(column => token(identity)) : model.all
|
|
198
|
+
rows = apply_partition(model, rows, partition)
|
|
199
|
+
both(model, rows, model.instant_record_writability, identity)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def scoped_types
|
|
203
|
+
InstantRecord.syncable_models.select { |model| scoped?(model) }.map(&:name)
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# The models a change of this identity's key empties in a browser: every
|
|
207
|
+
# scoped one, unless the identity names them. An identity whose key
|
|
208
|
+
# leaves part of itself out (the demo's leaves out the browser's own
|
|
209
|
+
# token) says which models the rest decides.
|
|
210
|
+
def reset_types(identity, partition: nil)
|
|
211
|
+
return models_for(partition).map(&:name) if partition.present?
|
|
212
|
+
|
|
213
|
+
identity.respond_to?(:sync_reset_types) ? identity.sync_reset_types : scoped_types
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# The one event a stale client is sent. It names the types because the
|
|
217
|
+
# browser build cannot know them.
|
|
218
|
+
def reset_event(identity, partition: nil)
|
|
219
|
+
{ operation: RESET, types: reset_types(identity, partition: partition), identity: key(identity, partition: partition) }
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# Change-log rows as the events one asker may receive, as [id, event]
|
|
223
|
+
# pairs. The log already keeps a `sync_scope` model's changes to their
|
|
224
|
+
# owner (Change.visible_to) and `sync_partition` changes to their
|
|
225
|
+
# partition (Change.in_partition). A `syncable_to` model's rule can
|
|
226
|
+
# depend on anything, so it is asked now: a change is sent with the
|
|
227
|
+
# record's state as it stands, never the logged payload, which may
|
|
228
|
+
# predate the record being shared. A record the asker may not see is sent
|
|
229
|
+
# as an empty destroy: nothing to a client that never held it, a
|
|
230
|
+
# revocation to one that did.
|
|
231
|
+
def events_for(changes, identity, partition: nil)
|
|
232
|
+
current = visible_records(changes, identity, partition: partition)
|
|
233
|
+
changes.map do |change|
|
|
234
|
+
model = ruled_model(change.record_type)
|
|
235
|
+
next [change.id, change.as_event] if change.operation == "destroy" || model.nil?
|
|
236
|
+
|
|
237
|
+
record = current[[change.record_type, change.record_id]]
|
|
238
|
+
event =
|
|
239
|
+
if record
|
|
240
|
+
InstantRecord.record_payload(record).merge(operation: change.operation)
|
|
241
|
+
else
|
|
242
|
+
{ type: change.record_type, id: change.record_id, operation: "destroy", version: 0, attributes: {} }
|
|
243
|
+
end
|
|
244
|
+
[change.id, event]
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
private
|
|
249
|
+
|
|
250
|
+
def apply_partition(model, rows, partition_spec)
|
|
251
|
+
part = model.instant_record_sync_partition
|
|
252
|
+
return rows unless part
|
|
253
|
+
|
|
254
|
+
value = part.extract(partition_spec)
|
|
255
|
+
return model.none if value.nil?
|
|
256
|
+
|
|
257
|
+
part.scope_relation(rows, value)
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# A column rule and a block rule both hold. As a subquery, since merging
|
|
261
|
+
# two conditions on one column keeps only the second.
|
|
262
|
+
def both(model, rows, block, identity)
|
|
263
|
+
return rows unless block
|
|
264
|
+
|
|
265
|
+
ruled = model.instance_exec(identity, &block)
|
|
266
|
+
if model.instant_record_sync_scope || model.instant_record_sync_partition
|
|
267
|
+
rows.where(model.primary_key => ruled.select(model.arel_table[model.primary_key]))
|
|
268
|
+
else
|
|
269
|
+
ruled
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# The model behind a logged type, when it has a `syncable_to` rule. Asked
|
|
274
|
+
# of the class itself too: a model left out of `InstantRecord.sync` still
|
|
275
|
+
# logs its changes, and its rule still decides who reads them.
|
|
276
|
+
def ruled_model(type)
|
|
277
|
+
model = InstantRecord.synced_model(type) || type.to_s.safe_constantize
|
|
278
|
+
model if model.respond_to?(:instant_record_visibility) && model.instant_record_visibility
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# One query per `syncable_to` model in the batch.
|
|
282
|
+
def visible_records(changes, identity, partition: nil)
|
|
283
|
+
found = {}
|
|
284
|
+
changes.reject { |change| change.operation == "destroy" }.group_by(&:record_type).each do |type, list|
|
|
285
|
+
model = ruled_model(type)
|
|
286
|
+
next unless model
|
|
287
|
+
|
|
288
|
+
model.connection_pool.with_connection do
|
|
289
|
+
model.uncached do
|
|
290
|
+
visible(model, identity, partition: partition).where(model.primary_key => list.map(&:record_id).uniq).each do |record|
|
|
291
|
+
found[[type, record.id]] = record
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
found
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
end
|