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,63 @@
|
|
|
1
|
+
<section id="<%= dom_id %>"
|
|
2
|
+
class="ir-sync-notices"
|
|
3
|
+
data-instant-record-sync-notices="true"
|
|
4
|
+
data-notice-count="<%= notices.size %>"
|
|
5
|
+
aria-live="polite"
|
|
6
|
+
<%= "hidden" if notices.empty? %>>
|
|
7
|
+
<% notices.each do |notice| %>
|
|
8
|
+
<% diffs = instant_record_notice_diff(notice) %>
|
|
9
|
+
<article class="ir-sync-notice ir-sync-notice--<%= notice.kind %>"
|
|
10
|
+
data-sync-notice-id="<%= notice.id %>"
|
|
11
|
+
data-sync-notice-kind="<%= notice.kind %>"
|
|
12
|
+
data-sync-notice-record-type="<%= notice.record_type %>"
|
|
13
|
+
data-sync-notice-record-id="<%= notice.record_id %>">
|
|
14
|
+
<header class="ir-sync-notice__header">
|
|
15
|
+
<span class="ir-sync-notice__badge"><%= notice.conflict? ? "Conflict" : "Rejected" %></span>
|
|
16
|
+
<strong class="ir-sync-notice__target"><%= notice.record_type %> (<%= notice.operation %>)</strong>
|
|
17
|
+
<p class="ir-sync-notice__reason"><%= notice.reason %></p>
|
|
18
|
+
</header>
|
|
19
|
+
|
|
20
|
+
<% if diffs.any? %>
|
|
21
|
+
<dl class="ir-sync-notice__diffs">
|
|
22
|
+
<% diffs.each do |diff| %>
|
|
23
|
+
<div class="ir-sync-notice__diff">
|
|
24
|
+
<dt><%= diff["field"] %></dt>
|
|
25
|
+
<dd>
|
|
26
|
+
<span class="ir-sync-notice__local" data-side="local"><%= diff["local"].nil? ? "(removed)" : diff["local"].to_s %></span>
|
|
27
|
+
<% unless diff["server"].nil? %>
|
|
28
|
+
<span class="ir-sync-notice__server" data-side="server"><%= diff["server"].to_s %></span>
|
|
29
|
+
<% end %>
|
|
30
|
+
</dd>
|
|
31
|
+
</div>
|
|
32
|
+
<% end %>
|
|
33
|
+
</dl>
|
|
34
|
+
<% end %>
|
|
35
|
+
|
|
36
|
+
<div class="ir-sync-notice__actions">
|
|
37
|
+
<form action="<%= base_path %>/<%= notice.id %>/retry" method="post" data-sync-notice-action="retry">
|
|
38
|
+
<% if respond_to?(:form_authenticity_token) && respond_to?(:protect_against_forgery?) && protect_against_forgery? %>
|
|
39
|
+
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
|
|
40
|
+
<% end %>
|
|
41
|
+
<input type="hidden" name="return_to" value="<%= return_to %>">
|
|
42
|
+
<button type="submit" class="ir-sync-notice__btn ir-sync-notice__btn--retry">Retry</button>
|
|
43
|
+
</form>
|
|
44
|
+
|
|
45
|
+
<form action="<%= base_path %>/<%= notice.id %>/fork" method="post" data-sync-notice-action="fork">
|
|
46
|
+
<% if respond_to?(:form_authenticity_token) && respond_to?(:protect_against_forgery?) && protect_against_forgery? %>
|
|
47
|
+
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
|
|
48
|
+
<% end %>
|
|
49
|
+
<input type="hidden" name="return_to" value="<%= return_to %>">
|
|
50
|
+
<button type="submit" class="ir-sync-notice__btn ir-sync-notice__btn--fork">Fork as new</button>
|
|
51
|
+
</form>
|
|
52
|
+
|
|
53
|
+
<form action="<%= base_path %>/<%= notice.id %>/dismiss" method="post" data-sync-notice-action="dismiss">
|
|
54
|
+
<% if respond_to?(:form_authenticity_token) && respond_to?(:protect_against_forgery?) && protect_against_forgery? %>
|
|
55
|
+
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
|
|
56
|
+
<% end %>
|
|
57
|
+
<input type="hidden" name="return_to" value="<%= return_to %>">
|
|
58
|
+
<button type="submit" class="ir-sync-notice__btn ir-sync-notice__btn--dismiss">Dismiss</button>
|
|
59
|
+
</form>
|
|
60
|
+
</div>
|
|
61
|
+
</article>
|
|
62
|
+
<% end %>
|
|
63
|
+
</section>
|
data/compiled/README.md
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# Compiling InstantRecord apps
|
|
2
|
+
|
|
3
|
+
`bin/rails instant_record:compile` builds a static browser site from a Rails app.
|
|
4
|
+
Roundhouse translates the Rails source, Spinel compiles it to WebAssembly, and a
|
|
5
|
+
dedicated worker serves local requests from a persistent SQLite connection. The
|
|
6
|
+
worker saves database snapshots in IndexedDB and sends pending mutations to the
|
|
7
|
+
Rails sync endpoint. This backend uses an experimental compiler toolchain.
|
|
8
|
+
|
|
9
|
+
## Install the toolchain
|
|
10
|
+
|
|
11
|
+
Use Ruby 3.3+, Node and npm, a C toolchain, and the following build inputs:
|
|
12
|
+
|
|
13
|
+
- [Spinel](https://github.com/matz/spinel) at `7c56323d2`, including its WASI runtime and bundled packages. Build it with `make` and `make wasm-rt`.
|
|
14
|
+
- [Roundhouse](https://github.com/rubys/roundhouse) at `b937108ce`. Build the release binary with `cargo build --release`.
|
|
15
|
+
- [WASI SDK](https://github.com/WebAssembly/wasi-sdk) 34 and the SQLite 3.54.1 [amalgamation](https://www.sqlite.org/download.html).
|
|
16
|
+
- The npm dependencies pinned in the gem’s `compiled/web/package-lock.json`. Binaryen’s `wasm-opt` is optional.
|
|
17
|
+
|
|
18
|
+
Copy the example configuration (or run `bin/rails generate instant_record:install`) and install the JavaScript dependencies in your app:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
gem_dir="$(bundle show instant_record)"
|
|
22
|
+
mkdir -p tmp/instant_record/js config
|
|
23
|
+
cp "$gem_dir"/compiled/web/package*.json tmp/instant_record/js/
|
|
24
|
+
npm ci --prefix tmp/instant_record/js
|
|
25
|
+
bin/rails generate instant_record:install
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Fill in absolute paths in `config/instant_record_toolchain.env`. Set `WEB_NODE_MODULES` to the directory created by `npm ci` (`tmp/instant_record/js/node_modules`) and `CACHE` to a writable build cache. `RAILS_GEMS` is optional in consumer apps using Bundler — `turbo-rails` and `stimulus-rails` assets are resolved from `Gem.loaded_specs` automatically. `PLAYWRIGHT_NODE_MODULES` is only needed for this repository's benchmarks. Point `INSTANT_RECORD_TOOLCHAIN` at another config if desired.
|
|
29
|
+
|
|
30
|
+
## Build and serve
|
|
31
|
+
|
|
32
|
+
The gem includes SQLite and Prism for compiling migrations on the build machine.
|
|
33
|
+
The Rails sync server keeps its existing database.
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
bin/rails db:migrate
|
|
37
|
+
bin/rails instant_record:doctor
|
|
38
|
+
bin/rails instant_record:compile
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The task discovers named models using `InstantRecord.syncable_models`. It reads
|
|
42
|
+
your routes, ERB views, models, controllers, helpers, assets and `db/schema.rb`,
|
|
43
|
+
then stages the browser build under `tmp/instant_record/compiled`. Run it again
|
|
44
|
+
after changing the app. Keep server secrets and server-only logic out of code
|
|
45
|
+
shipped to the browser.
|
|
46
|
+
|
|
47
|
+
Serve `tmp/instant_record/compiled/web/dist` from the root of a static origin.
|
|
48
|
+
Return its `index.html` for page routes and serve Wasm with the
|
|
49
|
+
`application/wasm` content type. Route `/instant_record` to your Rails server
|
|
50
|
+
or build with `SYNC_ENDPOINT=https://your-rails.example/instant_record`.
|
|
51
|
+
The standalone compile task stages sources outside the gem.
|
|
52
|
+
|
|
53
|
+
### Ship on a Rails deploy
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
# config/application.rb
|
|
57
|
+
config.instant_record.build_on_precompile = :compiled
|
|
58
|
+
config.instant_record.compiled_host = "browser.example.com"
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Point that hostname at Rails and allow it in your Rails host configuration.
|
|
62
|
+
Run `bin/rails assets:precompile` with the compiler toolchain available. Include
|
|
63
|
+
`public/instant_record_compiled` from the build in the deployed Rails image.
|
|
64
|
+
The hook publishes an immutable release directory and atomically switches the
|
|
65
|
+
`current` symlink after a successful build. The gem serves browser navigation and
|
|
66
|
+
compiled assets on the configured hostname. Sync requests continue through Rails.
|
|
67
|
+
Keep the previous release directory in the image or shared asset store during a
|
|
68
|
+
rollout so a tab that just received the old HTML can still fetch its worker and Wasm.
|
|
69
|
+
`build_on_precompile = true` also selects the compiled build.
|
|
70
|
+
|
|
71
|
+
Open compiled tabs check the release manifest on focus, on reconnect, and once
|
|
72
|
+
a minute while visible. An idle tab reloads. After input, the tab offers a
|
|
73
|
+
reload button so the reader can finish editing. Applications with custom draft
|
|
74
|
+
state can cancel the `instant-record:before-update` window event. When adopting
|
|
75
|
+
this mechanism from an older bundle, reload those tabs once.
|
|
76
|
+
|
|
77
|
+
### Migrate an existing browser database
|
|
78
|
+
|
|
79
|
+
Commit the Rails migration and its updated `db/schema.rb` together. The
|
|
80
|
+
compiler reads the configured Rails migration paths. Literal `create_table`,
|
|
81
|
+
`add_column`, `add_index`, and `execute` operations in `change` or `up` are
|
|
82
|
+
compiled to SQLite SQL. For example, a release can add a column and backfill
|
|
83
|
+
it with a SQL UPDATE that runs against each browser's existing rows.
|
|
84
|
+
|
|
85
|
+
For a Ruby backfill, adapter-specific SQL, or another unsupported operation,
|
|
86
|
+
provide `db/browser_migrate/<same migration filename>.sql`. This file contains
|
|
87
|
+
the SQLite equivalent for the whole migration. For renames and deletions,
|
|
88
|
+
handle any queued mutations that refer to the old fields and keep the sync
|
|
89
|
+
server compatible with older clients. An unsupported declaration without a
|
|
90
|
+
companion file fails the build before publication.
|
|
91
|
+
|
|
92
|
+
New profiles start at the dumped schema. Returning profiles run only pending
|
|
93
|
+
steps before rendering, with each compiled migration version wrapped in an
|
|
94
|
+
atomic SQLite transaction (`BEGIN IMMEDIATE` / `COMMIT` / `ROLLBACK`). The
|
|
95
|
+
database and migration version commit together in one IndexedDB transaction. If
|
|
96
|
+
a browser encounters schema drift, a release rollback, or a failed migration,
|
|
97
|
+
the worker automatically extracts pending `instant_record_outbox` rows, rebuilds
|
|
98
|
+
a clean database at the target schema, re-inserts the preserved outbox rows,
|
|
99
|
+
and drains + re-bootstraps against the server.
|
|
100
|
+
|
|
101
|
+
Check in `db/instant_record_lock.json` (generated by `bin/rails instant_record:schema:lock`
|
|
102
|
+
or on first compile) and run `bin/rails instant_record:schema:verify` (or
|
|
103
|
+
`bin/rails instant_record:doctor`) in CI. Verification asserts that published
|
|
104
|
+
migrations have not been mutated and replays every historical schema version
|
|
105
|
+
populated with sample rows and pending `OutboxMutation` rows up to `HEAD`.
|
|
106
|
+
|
|
107
|
+
For column renames, drops, or rolling-deploy schema skew (N-1 or N+1 clients),
|
|
108
|
+
declare `upcast_outbox` on your syncable model (or globally via
|
|
109
|
+
`InstantRecord.upcast_outbox`) so queued mutations transform cleanly on the
|
|
110
|
+
server before `MutationApplier` writes them.
|
|
111
|
+
|
|
112
|
+
If an earlier compiled app already stored **unversioned** SQLite snapshots,
|
|
113
|
+
set `config.instant_record.compiled_migration_baseline` to the numeric Rails
|
|
114
|
+
schema version that bundle actually shipped. Publish a versioned baseline
|
|
115
|
+
release before changing the schema. Existing profiles with differing unrecorded
|
|
116
|
+
schemas require an app-specific migration strategy. A fresh app needs no baseline.
|
|
117
|
+
|
|
118
|
+
## Demo and compatibility
|
|
119
|
+
|
|
120
|
+
From this repository, copy the config to `compiled/toolchain.env` and run:
|
|
121
|
+
|
|
122
|
+
```sh
|
|
123
|
+
bundle exec rake compile # or: ruby compiled/pipeline/build.rb
|
|
124
|
+
node compiled/bench/serve.mjs
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Open the catalog at `/` or `/home`. Todo and Chat use local writes and background
|
|
128
|
+
sync. Ship v2 alters the live SQLite schema and backfills notes. Console offers
|
|
129
|
+
compiled ActiveRecord queries. Receipts measures this browser’s boot, module size
|
|
130
|
+
and local/server round trips. The source drawer loads the source for the selected
|
|
131
|
+
page on demand. The Sync inspector shows local and network traffic and simulates
|
|
132
|
+
Offline and Slow connections.
|
|
133
|
+
|
|
134
|
+
Spinel compiles code ahead of time. Arbitrary Ruby evaluation, multipart form file uploads (use `sync_file` with `frontend/files.js` instead),
|
|
135
|
+
encrypted attributes, and server-only Rails subsystems are outside this browser
|
|
136
|
+
backend. Tabs on the same origin and browser profile share
|
|
137
|
+
one durable SQLite snapshot. Each write picks up the latest revision and saves
|
|
138
|
+
the changed records and outbox together before the browser confirms the write.
|
|
139
|
+
Pending changes survive closing every tab and sync when the app reopens online.
|
|
140
|
+
The demo's Ship v2 page runs its teaching migration on demand. Consumer apps
|
|
141
|
+
apply their release migrations at boot. The sync server remains authoritative across clients.
|
|
142
|
+
|
|
143
|
+
## A native library (experimental)
|
|
144
|
+
|
|
145
|
+
The same models can run outside a browser. `build_native.rb` (or `bundle exec rake compile:native`) compiles the demo,
|
|
146
|
+
with the screens in `demo/app/screens`, to a C library and a small host program:
|
|
147
|
+
|
|
148
|
+
```sh
|
|
149
|
+
bundle exec rake compile:native # or: ruby compiled/pipeline/build_native.rb
|
|
150
|
+
cd demo && INSTANT_RECORD_NATIVE_HOST=../compiled/.build-native/host bin/rails test test/integration/native_inbox_test.rb
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
A screen is a Ruby class (`InstantRecord::Native::Screen`) whose `body` describes
|
|
154
|
+
what to draw and whose `perform` answers a tap. The library has six functions,
|
|
155
|
+
declared in `pipeline/native/instant_record_native.h`: `ir_boot` starts the
|
|
156
|
+
app on a SQLite file, once in a process, `ir_open` closes that file and opens
|
|
157
|
+
another, and `ir_call` runs `render`, `action`, `sync`, `stream` and the rest
|
|
158
|
+
the header lists, each taking and returning a string. `render` and `action`
|
|
159
|
+
take one JSON object (the screen's name and its params, and for an action its
|
|
160
|
+
name, args and a form's values) and a render returns the screen as JSON. The
|
|
161
|
+
library never opens a connection: when it needs the server it calls the host
|
|
162
|
+
back with one request and the host answers. A host that must not wait on the
|
|
163
|
+
network inside a call asks `sync_plan` and `cursor` what a pass will request,
|
|
164
|
+
fetches those itself, and answers the pass from what it fetched.
|
|
165
|
+
|
|
166
|
+
Both builds end with the same entry, built in Ruby by
|
|
167
|
+
`InstantRecord::Compiler::EntryBuilder`. The class a screen's file holds is
|
|
168
|
+
decided in Ruby alone (`InstantRecord::Native.class_name`).
|
|
169
|
+
|
|
170
|
+
`pipeline/native/host.c` is that host with no screen, driven over standard
|
|
171
|
+
input. The test starts it, answers its requests from the Rails app, and checks
|
|
172
|
+
that the mailbox renders the same bytes as under CRuby, that an archive made
|
|
173
|
+
with no connection waits in the outbox and reaches the server later, that the
|
|
174
|
+
database survives a restart, that a rejected write is undone, that a pass
|
|
175
|
+
answered from replies fetched beforehand asks for nothing else, that a typed
|
|
176
|
+
reply keeps every character, that a page of older mail handed to the
|
|
177
|
+
`history` entry lengthens the list, and that opening another inbox's file
|
|
178
|
+
closes the first.
|
|
179
|
+
|
|
180
|
+
A list that goes on at the server ends in a `more` element, which names what
|
|
181
|
+
to ask the sync address's records door for. The host fetches that page and
|
|
182
|
+
hands it to `history`; the screen reads back whether more is left. Code that
|
|
183
|
+
runs only in the compiled app is marked `client_only` (the same thing as
|
|
184
|
+
`browser_only`, by the name that fits a phone).
|
|
185
|
+
|
|
186
|
+
Another Rails app builds its own library with `bin/rails instant_record:native`
|
|
187
|
+
(add `[iphonesimulator]` or `[iphoneos]` for iOS), which stages the app with
|
|
188
|
+
its `app/screens` as `instant_record:compile` stages it for the browser.
|
|
189
|
+
`bin/rails generate instant_record:ios Name --screen first` writes a thin
|
|
190
|
+
Xcode project that links the result. While developing, the phone can ask Rails
|
|
191
|
+
for its screens and skip the build: with `config.instant_record.native_dev`
|
|
192
|
+
on, in a local environment, `POST <mount>/native/render` and `/native/action`
|
|
193
|
+
take what the library takes and answer from the same screen classes under
|
|
194
|
+
CRuby. They answer only requests from the same machine, since a screen's
|
|
195
|
+
action writes as whoever its params name.
|
|
196
|
+
|
|
197
|
+
A screen can also hand props to a view written in SwiftUI (`swift "Name" do
|
|
198
|
+
prop ... end`), as a Rails action hands a React page its props.
|
|
199
|
+
|
|
200
|
+
### On iPhone
|
|
201
|
+
|
|
202
|
+
```sh
|
|
203
|
+
bundle exec rake "compile:native[iphonesimulator]" # or: ruby compiled/pipeline/build_native.rb iphonesimulator
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
builds the same library for the iOS simulator, with Spinel's runtime and the
|
|
207
|
+
packages the app uses compiled from Spinel's sources beside it
|
|
208
|
+
(`.build-native/iphonesimulator/libinstant_record_app.a` and
|
|
209
|
+
`libspinel_rt.a`). `native/ios` holds a SwiftUI renderer and the Inbox as an
|
|
210
|
+
app that links them. [native/ios/README.md](../native/ios/README.md) has the
|
|
211
|
+
build steps and the two ways to draw a screen.
|
|
212
|
+
|
|
213
|
+
What ran, on macOS arm64 with Xcode 27.2 and the iPhone 17 simulator on iOS
|
|
214
|
+
26.5:
|
|
215
|
+
|
|
216
|
+
- The library and host build on macOS with no change, and the test above
|
|
217
|
+
passes there (9 runs).
|
|
218
|
+
- Spinel's generated C, its runtime and SQLite compile and link for the
|
|
219
|
+
simulator with one change: the iOS SDK has no `<sys/random.h>`, so the shim
|
|
220
|
+
uses `arc4random_buf` on Apple platforms.
|
|
221
|
+
- In the simulator the Inbox lists mail from its local database, opens a
|
|
222
|
+
conversation and marks it read, archives with a swipe, switches inbox and
|
|
223
|
+
filter from its bar menus, and archives with the server stopped. The
|
|
224
|
+
archive reached the server on the next launch after it came back.
|
|
225
|
+
|
|
226
|
+
`build_native.rb iphoneos` builds the same two libraries for a device, and the
|
|
227
|
+
Inbox builds against them unsigned. What did not run: nothing was signed or
|
|
228
|
+
installed on a phone.
|
|
229
|
+
|
|
230
|
+
## Deploying it
|
|
231
|
+
|
|
232
|
+
`deploy/` serves the built folder as a static site: nginx in a container, deployed with Kamal.
|
|
233
|
+
|
|
234
|
+
SYNC_ENDPOINT=https://your-rails-app.example/instant_record ruby pipeline/build.rb
|
|
235
|
+
cp -R web/dist deploy/site
|
|
236
|
+
ruby deploy/scrub_path.rb deploy/site/app-*.wasm "$PWD/.build/wasm-tree"
|
|
237
|
+
cd deploy
|
|
238
|
+
cp deploy.env.example .kamal/deploy.env # fill in, then:
|
|
239
|
+
source .kamal/deploy.env
|
|
240
|
+
kamal deploy --version <a tag for this build>
|
|
241
|
+
|
|
242
|
+
- The sync server must allow cross-origin requests from the site; the gem's endpoints do.
|
|
243
|
+
- Give it its own hostname, or replace the ruby.wasm build on its host (below). It cannot live
|
|
244
|
+
under a path next to that build: that build's service worker takes over every request on its
|
|
245
|
+
origin.
|
|
246
|
+
- The module contains the absolute path of the folder it was built in; `scrub_path.rb` replaces
|
|
247
|
+
it with a neutral one of the same length.
|
|
248
|
+
- Every path that is not a file serves the shell page, so deep links and reloads work.
|
|
249
|
+
- The build copies the app's `app/views/pwa/service-worker.js` to `/service-worker.js`, and the
|
|
250
|
+
demo's `application.js` registers it. It caches the shell (the page, `/harness`, `/assets`, the
|
|
251
|
+
module, the fonts), so a reopened app starts with no connection; it leaves every sync request
|
|
252
|
+
and the change stream alone. It keeps the two newest modules. Pending changes are still sent
|
|
253
|
+
only while the app is open: a service worker cannot drain the outbox, which lives in the
|
|
254
|
+
app's database. When hosting through Rails (`config.instant_record.compiled_host`), `InstantRecord::CompiledSite` serves `/service-worker.js` with `cache-control: no-cache` whenever the compiled build includes one.
|
|
255
|
+
|
|
256
|
+
### Replacing the ruby.wasm build on its host
|
|
257
|
+
|
|
258
|
+
A browser that visited the ruby.wasm build still has its service worker. Three things make the
|
|
259
|
+
switch clean for it:
|
|
260
|
+
|
|
261
|
+
- Keep `/instant_record` on that host going to the Rails app and send everything else here.
|
|
262
|
+
With kamal-proxy that is the Rails service deployed with `--path-prefix /instant_record
|
|
263
|
+
--strip-path-prefix=false` and no TLS options; the service on the root path carries TLS for
|
|
264
|
+
the host.
|
|
265
|
+
- Copy `deploy/rails.sw.js` into `deploy/site/` before building the image. The old worker's
|
|
266
|
+
browser asks for that path on its next visit; this file takes the old worker's place, removes
|
|
267
|
+
itself, reloads the tab and leaves the old local database intact.
|
|
268
|
+
- `nginx.conf` answers `/app.wasm` with 404. That stops the old worker from starting its app,
|
|
269
|
+
and from opening its change stream, on that one last visit.
|
|
270
|
+
|
|
271
|
+
Measured in Chrome: a returning browser shows the old worker's error text for about three
|
|
272
|
+
seconds, reloads by itself, and is on this build after about four. A first visit is not affected.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Compatibility shim: InstantRecord::Compiler::BrowserMigrations lives in
|
|
2
|
+
# lib/instant_record/compiler/browser_migrations.rb.
|
|
3
|
+
$LOAD_PATH.unshift(File.expand_path("../../lib", __dir__)) unless $LOAD_PATH.include?(File.expand_path("../../lib", __dir__))
|
|
4
|
+
require "instant_record/compiler/browser_migrations"
|
|
5
|
+
|
|
6
|
+
BrowserMigrations = InstantRecord::Compiler::BrowserMigrations unless defined?(::BrowserMigrations)
|
|
7
|
+
|
|
8
|
+
if $PROGRAM_NAME == __FILE__
|
|
9
|
+
root, output = ARGV
|
|
10
|
+
raw_paths = ENV["INSTANT_RECORD_MIGRATION_PATHS"]
|
|
11
|
+
migration_paths = raw_paths && !raw_paths.empty? ? JSON.parse(raw_paths) : nil
|
|
12
|
+
plan = InstantRecord::Compiler::BrowserMigrations.plan(
|
|
13
|
+
root: root,
|
|
14
|
+
baseline: ENV["INSTANT_RECORD_MIGRATION_BASELINE"],
|
|
15
|
+
migration_paths: migration_paths
|
|
16
|
+
)
|
|
17
|
+
File.write(output, JSON.generate(plan))
|
|
18
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# Build the compiled browser demo into compiled/web/dist.
|
|
3
|
+
#
|
|
4
|
+
# ruby compiled/pipeline/build.rb
|
|
5
|
+
# SYNC_ENDPOINT=http://localhost:3000/instant_record ruby compiled/pipeline/build.rb
|
|
6
|
+
$LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
|
|
7
|
+
require "instant_record/compiler"
|
|
8
|
+
|
|
9
|
+
begin
|
|
10
|
+
InstantRecord::Compiler.build_demo
|
|
11
|
+
rescue InstantRecord::Compiler::BuildError, ArgumentError => e
|
|
12
|
+
abort e.message
|
|
13
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# Build the demo app as a native library into compiled/.build-native[/target].
|
|
3
|
+
#
|
|
4
|
+
# ruby compiled/pipeline/build_native.rb
|
|
5
|
+
# ruby compiled/pipeline/build_native.rb iphonesimulator
|
|
6
|
+
# ruby compiled/pipeline/build_native.rb iphoneos
|
|
7
|
+
$LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
|
|
8
|
+
require "instant_record/compiler"
|
|
9
|
+
|
|
10
|
+
begin
|
|
11
|
+
InstantRecord::Compiler.build_demo_native(target: ARGV[0])
|
|
12
|
+
rescue InstantRecord::Compiler::BuildError, ArgumentError => e
|
|
13
|
+
abort e.message
|
|
14
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/* A host with no screen: it runs the compiled app for a driver on the other end
|
|
2
|
+
of its standard input and output, so a test can stand where the phone and
|
|
3
|
+
the network would be.
|
|
4
|
+
|
|
5
|
+
usage: host <database file> boot on that file
|
|
6
|
+
host - do not boot: the driver sends boot itself
|
|
7
|
+
|
|
8
|
+
The driver sends call <entry> <bytes>\n<input>
|
|
9
|
+
and reads ok <out bytes> <notice bytes>\n<out><notice>
|
|
10
|
+
or error <bytes>\n<message>
|
|
11
|
+
|
|
12
|
+
`boot <bytes>\n<path>` and `open <bytes>\n<path>` are ir_boot and ir_open,
|
|
13
|
+
answered the same way with nothing in <out>.
|
|
14
|
+
|
|
15
|
+
While a call runs the app may ask for the network. The host then writes
|
|
16
|
+
exchange <bytes>\n<request> and reads <bytes>\n<response> from the driver. */
|
|
17
|
+
#include "instant_record_native.h"
|
|
18
|
+
#include <stdio.h>
|
|
19
|
+
#include <stdlib.h>
|
|
20
|
+
#include <string.h>
|
|
21
|
+
|
|
22
|
+
static char *read_bytes(size_t size) {
|
|
23
|
+
char *bytes = malloc(size + 1);
|
|
24
|
+
if (!bytes || fread(bytes, 1, size, stdin) != size) exit(2);
|
|
25
|
+
bytes[size] = 0;
|
|
26
|
+
return bytes;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static const char *ask_driver(const char *request, void *context) {
|
|
30
|
+
static char *response;
|
|
31
|
+
size_t size;
|
|
32
|
+
(void)context;
|
|
33
|
+
printf("exchange %zu\n%s", strlen(request), request);
|
|
34
|
+
fflush(stdout);
|
|
35
|
+
if (scanf("%zu", &size) != 1 || getchar() != '\n') exit(2);
|
|
36
|
+
free(response);
|
|
37
|
+
response = read_bytes(size);
|
|
38
|
+
return response;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static void fail(void) {
|
|
42
|
+
printf("error %zu\n%s", strlen(ir_error()), ir_error());
|
|
43
|
+
fflush(stdout);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static void done(int failed) {
|
|
47
|
+
if (failed) { fail(); return; }
|
|
48
|
+
printf("ok 0 0\n");
|
|
49
|
+
fflush(stdout);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
int main(int argc, char **argv) {
|
|
53
|
+
char verb[8], entry[32];
|
|
54
|
+
size_t size;
|
|
55
|
+
if (argc != 2) return 64;
|
|
56
|
+
ir_set_exchange(ask_driver, NULL);
|
|
57
|
+
if (strcmp(argv[1], "-") == 0) done(0);
|
|
58
|
+
else if (ir_boot(argv[1])) { fail(); return 1; }
|
|
59
|
+
else done(0);
|
|
60
|
+
while (scanf(" %7s", verb) == 1) {
|
|
61
|
+
int call = strcmp(verb, "call") == 0;
|
|
62
|
+
if (call && scanf(" %31s", entry) != 1) return 2;
|
|
63
|
+
if (scanf(" %zu", &size) != 1 || getchar() != '\n') return 2;
|
|
64
|
+
char *input = read_bytes(size);
|
|
65
|
+
if (call) {
|
|
66
|
+
const char *out = ir_call(entry, input);
|
|
67
|
+
if (!out) fail();
|
|
68
|
+
else {
|
|
69
|
+
printf("ok %zu %zu\n%s%s", strlen(out), strlen(ir_notice()), out, ir_notice());
|
|
70
|
+
fflush(stdout);
|
|
71
|
+
}
|
|
72
|
+
} else if (strcmp(verb, "boot") == 0) done(ir_boot(input));
|
|
73
|
+
else if (strcmp(verb, "open") == 0) done(ir_open(input));
|
|
74
|
+
else return 2;
|
|
75
|
+
free(input);
|
|
76
|
+
}
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/* A compiled InstantRecord app, as a native host calls it.
|
|
2
|
+
|
|
3
|
+
Every call runs Ruby on the calling thread and none may overlap: give the
|
|
4
|
+
app one serial queue. Strings are UTF-8 and NUL-terminated. A returned
|
|
5
|
+
string belongs to the library and holds until the next call. */
|
|
6
|
+
#ifndef INSTANT_RECORD_NATIVE_H
|
|
7
|
+
#define INSTANT_RECORD_NATIVE_H
|
|
8
|
+
|
|
9
|
+
/* The app asks for the network through this during a "sync" call. `request`
|
|
10
|
+
is "METHOD URL\n" and then the body. Answer with the status on the first
|
|
11
|
+
line and then the response body, or "0\n" when the network failed. The
|
|
12
|
+
call waits for the answer, so give the request a timeout. */
|
|
13
|
+
typedef const char *(*ir_exchange_fn)(const char *request, void *context);
|
|
14
|
+
void ir_set_exchange(ir_exchange_fn exchange, void *context);
|
|
15
|
+
|
|
16
|
+
/* Start the app on the SQLite file at `database_path`, creating it and its
|
|
17
|
+
tables when it is new. Call once, before anything else. Returns 0 when the
|
|
18
|
+
app is up. A second call is refused, and so is every other call until this
|
|
19
|
+
one has succeeded (ir_error says which). */
|
|
20
|
+
int ir_boot(const char *database_path);
|
|
21
|
+
|
|
22
|
+
/* Close the database and work in the file at `database_path` from here on,
|
|
23
|
+
creating it and its tables when it is new. A database syncs with one
|
|
24
|
+
address, so an app that changes account opens that account's file. Writes
|
|
25
|
+
still queued in the file that is closed stay in it and go out when it is
|
|
26
|
+
opened again and synced. Returns 0 when the file is open. */
|
|
27
|
+
int ir_open(const char *database_path);
|
|
28
|
+
|
|
29
|
+
/* Run one entry point with `input` and return what it wrote, or NULL when
|
|
30
|
+
Ruby raised (ir_error says what).
|
|
31
|
+
|
|
32
|
+
"render" input {"screen":"mailbox","params":{...}} the screen as JSON
|
|
33
|
+
"action" input the same, with "action", "args" and "values"
|
|
34
|
+
(each an object of strings); render again after
|
|
35
|
+
"sync" input the sync endpoint's address one pass: push, then pull
|
|
36
|
+
"stream" input a text/event-stream chunk changes the host fetched
|
|
37
|
+
"open" input a database path what ir_open calls
|
|
38
|
+
"notices" input {"action":"list"|"retry"|"fork"|"dismiss",...} quarantined sync notices
|
|
39
|
+
"branch_outbox" input {"exclude_ids":[...]} staged OutboxMutations summary
|
|
40
|
+
"branch_replay" input {"mutations":[...],"conflict":"lww"} replay onto active DB
|
|
41
|
+
"sync_plan", "cursor", "history", "request" as the browser host uses them */
|
|
42
|
+
const char *ir_call(const char *entry, const char *input);
|
|
43
|
+
|
|
44
|
+
/* What changed during the last call: empty, "records_changed" when anything
|
|
45
|
+
may have changed, or "records_changed" followed by one compact JSON delta
|
|
46
|
+
per line: {"type":...,"id":...,"op":...,"changed_columns":[...],"attrs":{...}},
|
|
47
|
+
plus any "sync_notice:<json>" lines for quarantined rejections or conflicts. */
|
|
48
|
+
const char *ir_notice(void);
|
|
49
|
+
|
|
50
|
+
/* Tables and partitions read during the last "render" or "request" call,
|
|
51
|
+
as compact JSON {"tables":[...],"models":[...],"partitions":{...}}, or
|
|
52
|
+
empty when nothing was tracked. */
|
|
53
|
+
const char *ir_reads(void);
|
|
54
|
+
|
|
55
|
+
const char *ir_error(void);
|
|
56
|
+
|
|
57
|
+
#endif
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/* Native reactor bridge for Spinel (--ext-init app_boot): reactor_shim.c for a
|
|
2
|
+
host that links the app as a library. The browser's host trades strings with
|
|
3
|
+
Ruby through a WASI directory. Here they pass through named slots in memory,
|
|
4
|
+
which Ruby reaches as NativeHost (EntryBuilder). */
|
|
5
|
+
#include "app.h"
|
|
6
|
+
#include "instant_record_native.h"
|
|
7
|
+
#include <stdio.h>
|
|
8
|
+
#include <stdlib.h>
|
|
9
|
+
#include <string.h>
|
|
10
|
+
#include <unistd.h>
|
|
11
|
+
#ifndef __APPLE__
|
|
12
|
+
#include <sys/random.h>
|
|
13
|
+
#endif
|
|
14
|
+
|
|
15
|
+
enum { IN, OUT, NOTIFY, READS, REQUEST, RESPONSE, SLOTS };
|
|
16
|
+
static const char *const names[SLOTS] = { "in", "out", "notify", "reads", "request", "response" };
|
|
17
|
+
static char *slots[SLOTS];
|
|
18
|
+
static char error[512];
|
|
19
|
+
static ir_exchange_fn exchange;
|
|
20
|
+
static void *exchange_context;
|
|
21
|
+
/* The app boots once in a process: Spinel's app_boot() may not run twice. */
|
|
22
|
+
static enum { ASLEEP, UP, BROKEN } state;
|
|
23
|
+
|
|
24
|
+
static void put(int slot, const char *value) {
|
|
25
|
+
char *copy = strdup(value ? value : "");
|
|
26
|
+
if (!copy) abort();
|
|
27
|
+
free(slots[slot]);
|
|
28
|
+
slots[slot] = copy;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static int slot_named(const char *name) {
|
|
32
|
+
for (int slot = 0; slot < SLOTS; slot++)
|
|
33
|
+
if (strcmp(names[slot], name) == 0) return slot;
|
|
34
|
+
return -1;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const char *ir_host_take(const char *name) {
|
|
38
|
+
int slot = slot_named(name);
|
|
39
|
+
return slot >= 0 && slots[slot] ? slots[slot] : "";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
void ir_host_give(const char *name, const char *value) {
|
|
43
|
+
int slot = slot_named(name);
|
|
44
|
+
if (slot < 0) return;
|
|
45
|
+
put(slot, value);
|
|
46
|
+
if (slot == REQUEST) put(RESPONSE, exchange ? exchange(slots[REQUEST], exchange_context) : "0\n");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static void call_request(void *ctx) { (void)ctx; sp_NativeBridge_s_handle_request(); }
|
|
50
|
+
static void call_plan(void *ctx) { (void)ctx; sp_NativeBridge_s_sync_plan(); }
|
|
51
|
+
static void call_sync(void *ctx) { (void)ctx; sp_NativeBridge_s_handle_sync(); }
|
|
52
|
+
static void call_stream(void *ctx) { (void)ctx; sp_NativeBridge_s_handle_stream(); }
|
|
53
|
+
static void call_history(void *ctx) { (void)ctx; sp_NativeBridge_s_history(); }
|
|
54
|
+
static void call_cursor(void *ctx) { (void)ctx; sp_NativeBridge_s_cursor(); }
|
|
55
|
+
static void call_export_outbox(void *ctx) { (void)ctx; sp_NativeBridge_s_export_outbox(); }
|
|
56
|
+
static void call_import_outbox(void *ctx) { (void)ctx; sp_NativeBridge_s_import_outbox(); }
|
|
57
|
+
static void call_notices(void *ctx) { (void)ctx; sp_NativeBridge_s_notices(); }
|
|
58
|
+
static void call_branch_outbox(void *ctx) { (void)ctx; sp_NativeBridge_s_branch_outbox(); }
|
|
59
|
+
static void call_branch_replay(void *ctx) { (void)ctx; sp_NativeBridge_s_branch_replay(); }
|
|
60
|
+
static void call_render(void *ctx) { (void)ctx; sp_NativeBridge_s_render(); }
|
|
61
|
+
static void call_action(void *ctx) { (void)ctx; sp_NativeBridge_s_action(); }
|
|
62
|
+
static void call_open(void *ctx) { (void)ctx; sp_NativeBridge_s_open(); }
|
|
63
|
+
static void call_boot(void *ctx) { (void)ctx; app_boot(); }
|
|
64
|
+
|
|
65
|
+
static const struct { const char *name; void (*fn)(void *); } entries[] = {
|
|
66
|
+
{ "request", call_request }, { "sync_plan", call_plan }, { "sync", call_sync },
|
|
67
|
+
{ "stream", call_stream }, { "history", call_history }, { "cursor", call_cursor },
|
|
68
|
+
{ "export_outbox", call_export_outbox }, { "import_outbox", call_import_outbox },
|
|
69
|
+
{ "notices", call_notices },
|
|
70
|
+
{ "branch_outbox", call_branch_outbox }, { "branch_replay", call_branch_replay },
|
|
71
|
+
{ "render", call_render }, { "action", call_action }, { "open", call_open },
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
static int run_guarded(void (*fn)(void *)) {
|
|
75
|
+
const char *cls = NULL;
|
|
76
|
+
const char *msg = NULL;
|
|
77
|
+
error[0] = 0;
|
|
78
|
+
if (app_boot_try(fn, NULL, &cls, &msg)) {
|
|
79
|
+
snprintf(error, sizeof error, "%s: %s", cls ? cls : "Error", msg ? msg : "");
|
|
80
|
+
return 1;
|
|
81
|
+
}
|
|
82
|
+
return 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
void ir_set_exchange(ir_exchange_fn fn, void *context) {
|
|
86
|
+
exchange = fn;
|
|
87
|
+
exchange_context = context;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/* The iOS SDK has no <sys/random.h>, and arc4random_buf cannot fail. */
|
|
91
|
+
static int random_bytes(unsigned char *bytes, size_t size) {
|
|
92
|
+
#ifdef __APPLE__
|
|
93
|
+
arc4random_buf(bytes, size);
|
|
94
|
+
return 0;
|
|
95
|
+
#else
|
|
96
|
+
return getentropy(bytes, size);
|
|
97
|
+
#endif
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* The runtime signs cookies with SECRET_KEY_BASE and, when there is none, keeps
|
|
101
|
+
one in a file under the working directory. A native host serves no cookies
|
|
102
|
+
and may not be able to write there, so each boot gets a key of its own. */
|
|
103
|
+
static void fresh_secret(void) {
|
|
104
|
+
unsigned char bytes[32];
|
|
105
|
+
char hex[sizeof bytes * 2 + 1];
|
|
106
|
+
if (getenv("SECRET_KEY_BASE") || random_bytes(bytes, sizeof bytes) != 0) return;
|
|
107
|
+
for (size_t i = 0; i < sizeof bytes; i++) snprintf(hex + i * 2, 3, "%02x", bytes[i]);
|
|
108
|
+
setenv("SECRET_KEY_BASE", hex, 1);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
static int refuse(const char *why) {
|
|
112
|
+
snprintf(error, sizeof error, "%s", why);
|
|
113
|
+
return 1;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static int asleep(void) {
|
|
117
|
+
if (state == UP) return 0;
|
|
118
|
+
return refuse(state == BROKEN ? "The app did not boot, and cannot boot again in this process" : "Call ir_boot first");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
int ir_boot(const char *database_path) {
|
|
122
|
+
if (state != ASLEEP) return refuse("The app boots once. Open another database with ir_open");
|
|
123
|
+
setenv("BLOG_DB", database_path, 1);
|
|
124
|
+
fresh_secret();
|
|
125
|
+
state = run_guarded(call_boot) ? BROKEN : UP;
|
|
126
|
+
return state != UP;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
int ir_open(const char *database_path) {
|
|
130
|
+
return ir_call("open", database_path) ? 0 : 1;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const char *ir_call(const char *entry, const char *input) {
|
|
134
|
+
if (asleep()) return NULL;
|
|
135
|
+
for (size_t i = 0; i < sizeof entries / sizeof entries[0]; i++) {
|
|
136
|
+
if (strcmp(entries[i].name, entry) != 0) continue;
|
|
137
|
+
put(IN, input);
|
|
138
|
+
put(OUT, "");
|
|
139
|
+
put(NOTIFY, "");
|
|
140
|
+
put(READS, "");
|
|
141
|
+
return run_guarded(entries[i].fn) ? NULL : slots[OUT];
|
|
142
|
+
}
|
|
143
|
+
snprintf(error, sizeof error, "No entry named %s", entry);
|
|
144
|
+
return NULL;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const char *ir_notice(void) { return slots[NOTIFY] ? slots[NOTIFY] : ""; }
|
|
148
|
+
const char *ir_reads(void) { return slots[READS] ? slots[READS] : ""; }
|
|
149
|
+
const char *ir_error(void) { return error; }
|