yrb-lite 0.1.0.beta2-x64-mingw-ucrt

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 87fcef57f93415ad050f6a09e1d9347f0d5bfc58333c8c96b11aedefc2854ff2
4
+ data.tar.gz: 7a646a789b33291d01bf9088ed7486b7260489384015a9679d49b306709fce74
5
+ SHA512:
6
+ metadata.gz: b348fed53d5d9baf799c41a7aed61edfc8f15c9ef973fd26254c96af4f8175072fa27b6440969dbbc442056337e9950d5c5a39b5657c67746fa4adbed7966934
7
+ data.tar.gz: 0ac25fc4fece992c32569a3da0c03c18bff23e839b7e373f056713fbe4abd88d25d790cdb7eb02455d1808a7e08f264fe49e524ec531f88544c0f2f2dc822c54
data/CHANGELOG.md ADDED
@@ -0,0 +1,59 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. The format is based on
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project aims
5
+ to follow [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0.beta2] - 2026-06-16
10
+
11
+ ### Added
12
+
13
+ - Reliable delivery (opt-in, client-driven). A client may tag a document update
14
+ with an `"id"`; the server replies `{ "ack": <id> }` once the update has been
15
+ accepted (recorded in audit mode, applied in fast mode). This lets an
16
+ ack-aware client retain and retransmit an update until delivery is confirmed,
17
+ so an edit can't be silently lost on a flaky connection. Stock clients send no
18
+ `"id"`, never get acks, and behave exactly as before.
19
+ - A vendored, ack-aware `@y-rb/actioncable` provider in the demo
20
+ (`reliable_actioncable_provider.mjs`) that adds reliable delivery with
21
+ "sync-since-last-ack" framing (the unacknowledged tail is sent as one merged,
22
+ causally-complete delta), plus a minimal reference client and an intensive
23
+ message-loss stress test.
24
+
25
+ ### Fixed
26
+
27
+ - Causal-gap protection. The authoritative, fast, and store paths now reject a
28
+ document update that isn't causally ready -- one whose dependencies are
29
+ missing because an earlier update was lost in transit or its durable record
30
+ failed -- and ask the client to resync, instead of recording or relaying an
31
+ un-integrable update that would leave the log permanently pending. Adds native
32
+ `Doc#update_ready?`/`#pending?` (cheap, read-only checks) used to gate the
33
+ record-before-distribute path.
34
+
35
+ ## [0.1.0.beta1]
36
+
37
+ ### Added
38
+
39
+ - Thread-safe `YrbLite::Doc` and `YrbLite::Awareness` over `yrs` (magnus/rb-sys
40
+ native extension). The GVL is released during CRDT work so docs can run in
41
+ parallel on MRI.
42
+ - `YrbLite::Sync` ActionCable channel concern implementing the y-websocket
43
+ protocol (document sync plus awareness/presence). It's wire-compatible with
44
+ the [`@y-rb/actioncable`](https://www.npmjs.com/package/@y-rb/actioncable)
45
+ browser provider, and accepts its `{ update: ... }` envelope and `{ m: ... }`.
46
+ - A "record-before-distribute" mode via an `on_change` hook, so every change is
47
+ recorded durably before it's applied or relayed.
48
+ - Presence cleanup on disconnect, and idle-document eviction.
49
+ - Two backends: `sync_backend :memory` (default, classic ActionCable) and
50
+ `sync_backend :store` (stateless, AnyCable-ready, multi-process).
51
+ - Hardening against bad input: malformed or multi-message frames are dropped
52
+ before processing or relay, and native panics are contained at the FFI
53
+ boundary.
54
+ - Precompiled native gems for common platforms (no Rust toolchain needed to
55
+ install) via the cross-gem workflow.
56
+
57
+ [Unreleased]: https://github.com/jpcamara/yrb-lite/compare/v0.1.0.beta2...main
58
+ [0.1.0.beta2]: https://github.com/jpcamara/yrb-lite/compare/v0.1.0.beta1...v0.1.0.beta2
59
+ [0.1.0.beta1]: https://github.com/jpcamara/yrb-lite/releases/tag/v0.1.0.beta1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,477 @@
1
+ # yrb-lite
2
+
3
+ [![CI](https://github.com/jpcamara/yrb-lite/actions/workflows/ci.yml/badge.svg)](https://github.com/jpcamara/yrb-lite/actions/workflows/ci.yml)
4
+
5
+ Collaborative editing for Rails, backed by [y-crdt](https://github.com/y-crdt/y-crdt)
6
+ (the Rust library behind Y.js). Your Rails server speaks the y-websocket sync
7
+ protocol directly, so there's no separate Node process hosting the Y.js
8
+ documents.
9
+
10
+ ```ruby
11
+ class DocumentChannel < ApplicationCable::Channel
12
+ include YrbLite::Sync
13
+
14
+ def subscribed = sync_for(params[:id])
15
+ def receive(data) = sync_receive(data)
16
+ def unsubscribed = sync_clear_presence
17
+ end
18
+ ```
19
+
20
+ On the browser, use the [`@y-rb/actioncable`](https://www.npmjs.com/package/@y-rb/actioncable)
21
+ provider as-is. Tiptap, ProseMirror, and BlockNote all sync through it.
22
+
23
+ ## What you get
24
+
25
+ - Thread-safe `Doc` and `Awareness`. You can share them across Puma threads,
26
+ and the GVL is released while yrs does the actual work.
27
+ - The y-websocket protocol (document sync plus awareness/presence) as a
28
+ one-include ActionCable concern.
29
+ - A store-backed mode for AnyCable and multi-process deployments.
30
+ - An optional authoritative mode that records each change durably before it
31
+ goes out to anyone.
32
+
33
+ What it doesn't do: auth, read-only connections, rate limiting, webhooks,
34
+ metrics. Hocuspocus ships extensions for those; here you'd build them with
35
+ Rails.
36
+
37
+ ## Testing
38
+
39
+ Ruby and Rust unit tests cover the core, and an end-to-end suite runs the real
40
+ stack: it fuzzes the protocol, throws garbage and chaos at the server, kills the
41
+ server mid-write to check crash recovery, and drives real browsers under load.
42
+ The benchmark numbers below are from a single laptop. Issues and PRs are
43
+ welcome.
44
+
45
+ ## Install
46
+
47
+ ```ruby
48
+ gem "yrb-lite"
49
+ ```
50
+
51
+ Requires Ruby 3.4 or newer. Precompiled gems ship for Linux and macOS on Ruby
52
+ 3.4 and 4.0, so installing there needs no Rust. Other platforms (and any other
53
+ Ruby version) build from source, which needs [Rust](https://rustup.rs).
54
+
55
+ To work on the gem itself:
56
+
57
+ ```bash
58
+ git clone https://github.com/jpcamara/yrb-lite
59
+ cd yrb-lite
60
+ bundle install
61
+ bundle exec rake compile test
62
+ ```
63
+
64
+ The rest of the dev setup, plus the demo, is in [CONTRIBUTING.md](CONTRIBUTING.md).
65
+
66
+ ## Docs
67
+
68
+ - The ActionCable concern and a quickstart are [below](#actioncable-integration).
69
+ - [`examples/actioncable-demo`](examples/actioncable-demo): a runnable Rails +
70
+ Tiptap app with collaborative cursors, the AnyCable setup, a Postgres store,
71
+ and the test/load suites.
72
+ - [CHANGELOG.md](CHANGELOG.md) and [CONTRIBUTING.md](CONTRIBUTING.md).
73
+
74
+ ## Usage
75
+
76
+ ### Doc (Low-Level Document Sync)
77
+
78
+ ```ruby
79
+ require "yrb_lite"
80
+
81
+ # Create docs
82
+ doc = YrbLite::Doc.new # random client ID
83
+ doc = YrbLite::Doc.new(12345) # specific client ID
84
+
85
+ # Get document info
86
+ doc.client_id # => unique client identifier
87
+ doc.guid # => document GUID
88
+
89
+ # Encoding
90
+ doc.encode_state_vector # => current state vector
91
+ doc.encode_state_as_update # => full update
92
+ doc.encode_state_as_update(sv) # => update diff against state vector
93
+
94
+ # Applying updates
95
+ doc.apply_update(update_bytes) # apply raw V1 update
96
+
97
+ # Sync protocol messages
98
+ doc.sync_step1 # => SyncStep1 message (contains state vector)
99
+ doc.sync_step2(state_vector) # => SyncStep2 message (contains update)
100
+ doc.handle_sync_message(data) # => [msg_type, sync_type, response]
101
+ doc.encode_update_message(update) # => wrap update as sync Update message
102
+ ```
103
+
104
+ ### Awareness (Document + Presence)
105
+
106
+ ```ruby
107
+ # Create awareness instances (each contains a Doc)
108
+ awareness = YrbLite::Awareness.new # random client ID
109
+ awareness = YrbLite::Awareness.new(12345) # specific client ID
110
+
111
+ # Get document info
112
+ awareness.client_id # => unique client identifier
113
+ awareness.guid # => document GUID
114
+ ```
115
+
116
+ ### Handling Sync Messages
117
+
118
+ ```ruby
119
+ # When connection opens, send initial sync messages
120
+ initial_message = awareness.start
121
+ # Send initial_message to peer via WebSocket
122
+
123
+ # When receiving messages from peer
124
+ response = awareness.handle(incoming_data)
125
+ # Send response back to peer if not empty
126
+ send_to_peer(response) unless response.empty?
127
+ ```
128
+
129
+ ### ActionCable Integration
130
+
131
+ `YrbLite::Sync` is a channel concern that implements the full y-websocket
132
+ protocol (document sync + awareness/presence) over ActionCable:
133
+
134
+ ```ruby
135
+ # app/channels/document_channel.rb
136
+ class DocumentChannel < ApplicationCable::Channel
137
+ include YrbLite::Sync
138
+
139
+ # Optional persistence:
140
+ # on_load { |key| Document.find_by(key: key)&.content }
141
+ # on_save { |key, update| Document.find_by(key: key)&.update!(content: update) }
142
+
143
+ def subscribed
144
+ sync_for params[:id]
145
+ end
146
+
147
+ def receive(data)
148
+ sync_receive(data)
149
+ end
150
+
151
+ def unsubscribed
152
+ sync_clear_presence
153
+ end
154
+ end
155
+ ```
156
+
157
+ One `YrbLite::Awareness` is shared per document key. Creating it is
158
+ mutex-serialized; after that everything runs lock-free on the thread-safe
159
+ native types. The concern answers SyncStep1 directly, relays document and
160
+ awareness changes to the other subscribers (not back to the sender), and calls
161
+ `on_save` after any message that changed the document.
162
+
163
+ `sync_unsubscribed` clears the connection's presence, so a closed tab doesn't
164
+ leave a stale cursor hanging until the client-side timeout. It also unloads the
165
+ document from memory once the last subscriber disconnects, which keeps the
166
+ process from holding onto every document it ever served. That unload only
167
+ happens when `on_load` is set and the document can be reloaded later; without
168
+ it, the in-memory copy is the only one and stays put.
169
+
170
+ Incoming frames are validated as a single well-formed protocol message before
171
+ anything processes or relays them. Malformed, truncated, multi-message,
172
+ oversized, or unknown frames are dropped. A bad frame can't crash the process: a
173
+ Rust panic is caught at the FFI boundary and re-raised as a Ruby exception. And
174
+ no single client can relay garbage that breaks the others in a room.
175
+
176
+ #### Multi-process deployments
177
+
178
+ Most Rails apps run several processes (Puma workers, multiple dynos), and any of
179
+ them might serve a given document. Two pieces keep them in step.
180
+
181
+ Broadcasts cross processes through the Action Cable adapter, so it needs to be a
182
+ real one (`redis` or `solid_cable`, not `async`). With that in place, a change
183
+ on one process reaches clients on all of them.
184
+
185
+ Each process also keeps its own copy of the document and applies broadcasts from
186
+ the others. The merge is an ordinary CRDT apply, idempotent and
187
+ order-independent, which keeps server reads and new-client handshakes current on
188
+ every process. Each broadcast carries a per-process id (`Sync.process_id`) that
189
+ tells a process to skip its own.
190
+
191
+ A cold process (no copy yet) rebuilds from the durable store through `on_load`.
192
+ In authoritative mode the store is always current, since changes are recorded
193
+ before they go out. Record-before-distribute therefore holds across processes:
194
+ whichever process receives a change records it to the shared store before
195
+ anyone, anywhere, sees it.
196
+
197
+ `bun multiprocess.mjs` in the demo runs clients across two processes and checks
198
+ the lot: convergence, fresh copies on both, presence across processes, and one
199
+ shared log.
200
+
201
+ ##### AnyCable (`sync_backend :store`)
202
+
203
+ The default backend keeps that warm in-memory copy and relies on a `stream_from`
204
+ block running in Ruby for each broadcast. AnyCable breaks both assumptions.
205
+ anycable-go delivers broadcasts outside Ruby, so the block never runs. Each RPC
206
+ gets a fresh channel instance, which means ivars set in `subscribed` are gone by
207
+ `receive`. And there's no fixed worker-to-document mapping to lean on.
208
+
209
+ `sync_backend :store` is the path for that: stateless per message, no warm
210
+ copy.
211
+
212
+ ```ruby
213
+ class DocumentChannel < ApplicationCable::Channel
214
+ include YrbLite::Sync
215
+ sync_backend :store
216
+
217
+ on_load { |key| MyStore.load(key) } # required: source of truth
218
+ on_change { |key, update| MyStore.append(key, update) } # required: record
219
+
220
+ def subscribed = sync_for(params[:id])
221
+ def receive(data) = sync_receive(data, params[:id]) # pass the key each call
222
+ def unsubscribed = sync_unsubscribed(params[:id])
223
+ end
224
+ ```
225
+
226
+ - `stream_from` is registered without a block; anycable-go does the relaying.
227
+ - A handshake (SyncStep1) is answered from the store. Changes are recorded, then
228
+ broadcast. Nothing is held in Ruby between calls, so any worker can handle any
229
+ message.
230
+ - Pass `params[:id]` into `sync_receive`/`sync_unsubscribed` so the document key
231
+ survives AnyCable's per-command instances.
232
+ - The sender gets its own updates echoed back (no Ruby callback to filter them).
233
+ That's a no-op, since applying an update twice does nothing.
234
+
235
+ The demo checks this against a real anycable-go + RPC server
236
+ (`frontend/anycable_probe.mjs`, `anycable_concurrent.mjs`): liveness, the
237
+ `@y-rb/actioncable` provider, cross-process reads, and concurrent convergence.
238
+
239
+ The wire format is the standard y-protocols binary messages, base64-encoded in
240
+ the ActionCable envelope. The server accepts the `@y-rb/actioncable` provider's
241
+ `{ "update" => ... }` envelope (and its own `{ "m" => ... }`) and sends one
242
+ message per frame, so the off-the-shelf provider works with no custom client
243
+ code:
244
+
245
+ ```js
246
+ import { createConsumer } from "@rails/actioncable"
247
+ import { WebsocketProvider } from "@y-rb/actioncable"
248
+
249
+ const provider = new WebsocketProvider(ydoc, createConsumer(), "DocumentChannel", { id: docId })
250
+ ```
251
+
252
+ [`examples/actioncable-demo`](examples/actioncable-demo) is a full Rails + Tiptap
253
+ app using that provider, with end-to-end tests.
254
+
255
+ #### Authoritative audit mode (record before distribute)
256
+
257
+ By default a change is applied and broadcast immediately (the fast path). If you
258
+ need to durably record every change before anyone else sees it, whether for
259
+ auditing or to guarantee nothing is distributed until it's stored, register an
260
+ `on_change` recorder:
261
+
262
+ ```ruby
263
+ class DocumentChannel < ApplicationCable::Channel
264
+ include YrbLite::Sync
265
+
266
+ on_change do |key, update|
267
+ # Synchronous, durable write. `update` is the exact CRDT delta.
268
+ AuditLog.append!(key, update) # raise to REJECT the change
269
+ end
270
+
271
+ def subscribed = sync_for(params[:id])
272
+ def receive(data) = sync_receive(data)
273
+ def unsubscribed = sync_clear_presence
274
+ end
275
+ ```
276
+
277
+ With `on_change` registered, a change is recorded before it goes anywhere. The
278
+ recorder writes the raw CRDT delta synchronously; only then is the change
279
+ applied to the shared document and broadcast. The whole sequence runs under a
280
+ per-document lock, so every change to a document is recorded in the same order
281
+ it's applied. That's what makes the log authoritative. Replay the deltas onto a
282
+ fresh `Y.Doc` and you get the document back exactly.
283
+
284
+ If the recorder raises (say the store is down), the change is rejected: not
285
+ applied, not sent to anyone. The cost is a synchronous durable write per change,
286
+ which serializes that document's writes. Other documents use other locks and run
287
+ in parallel.
288
+
289
+ `on_change` and `on_save` are separate. `on_save` snapshots the whole document
290
+ when it gets a chance; `on_change` is the per-change log. The demo's `AUDIT=1`
291
+ mode (in [`examples/actioncable-demo`](examples/actioncable-demo)) wires
292
+ `on_change` to an fsync'd append-only log and checks, end to end, that the log
293
+ alone rebuilds the document.
294
+
295
+ #### Reliable delivery (acks)
296
+
297
+ The y-websocket protocol is fire-and-forget. If a client's update is lost in
298
+ transit (a flaky socket, a send that never lands) and the client makes no
299
+ further edits, the server stays idle and never asks anyone to resync, so that
300
+ edit is gone -- even though the client believes it was saved. CRDTs converge the
301
+ state everyone *has*; they don't recover an update that never arrived.
302
+
303
+ yrb-lite closes that gap with an opt-in, client-driven acknowledgement. If an
304
+ incoming frame carries an `"id"`, the server replies `{ "ack": <id> }` once the
305
+ update has been **accepted** -- recorded in audit mode, applied in fast mode. A
306
+ causally-gapped update is not acked (it gets a resync instead), so the client
307
+ knows it hasn't landed yet.
308
+
309
+ ```
310
+ client -> server { "m": "<base64 update>", "id": 42 }
311
+ server -> client { "ack": 42 } # update accepted; safe to forget
312
+ ```
313
+
314
+ That's the whole server side. A reliable client tags each outgoing update with
315
+ an incrementing id, keeps it in a pending buffer, and retransmits on a timer (and
316
+ on reconnect) until the matching ack returns. Because CRDT apply is idempotent, a
317
+ resend that already landed is a harmless no-op that just re-acks. An update lost
318
+ in transit is recovered by the client's own retransmit -- no reconnect required,
319
+ and no dependence on a later edit happening to trigger a resync.
320
+
321
+ This is entirely **self-gating**: stock clients send no `"id"`, so they never get
322
+ acks and behave exactly as before. Only a client that opts in by tagging its
323
+ frames participates.
324
+
325
+ Two client examples ship in the demo:
326
+
327
+ - [`frontend/reliable.mjs`](examples/actioncable-demo/frontend/reliable.mjs) — a
328
+ minimal reference client showing the raw mechanism (tag with an id, retain,
329
+ retransmit on a timer, drain on ack), with an end-to-end test that loses an
330
+ update mid-flight and recovers it purely by retransmit.
331
+ - [`frontend/provider/reliable_actioncable_provider.mjs`](examples/actioncable-demo/frontend/provider/reliable_actioncable_provider.mjs)
332
+ — the standard `@y-rb/actioncable` `WebsocketProvider`, vendored and augmented
333
+ for production use. It's a drop-in replacement that speaks the same protocol
334
+ and envelope, and adds reliability with **sync-since-last-ack** framing: rather
335
+ than retransmitting updates one by one, it keeps the unacknowledged local
336
+ updates in a queue and sends their *merge* as a single causally-complete delta,
337
+ with the id being the highest sequence in the batch (so one `{ ack: id }`
338
+ cumulatively confirms everything up to it). Because the whole unacked tail goes
339
+ as one self-contained delta, the server never sees an internal gap and never
340
+ has to round-trip a resync for a lost middle update — the next edit, or the
341
+ next timer tick, carries it. Awareness stays fire-and-forget; against a server
342
+ that doesn't implement acks it warns once and falls back to plain delivery; and
343
+ `reliable: false` opts out entirely. The demo's editor uses this provider.
344
+
345
+ ### User Awareness/Presence
346
+
347
+ ```ruby
348
+ # Set local user state (cursor position, name, etc.)
349
+ awareness.set_local_state('{"user": {"name": "Alice", "color": "#ff0000"}}')
350
+
351
+ # Get local state
352
+ awareness.local_state # => '{"user": {"name": "Alice", "color": "#ff0000"}}'
353
+
354
+ # Clear local state (e.g., when disconnecting)
355
+ awareness.clear_local_state
356
+
357
+ # Encode awareness update for broadcasting
358
+ update = awareness.encode_awareness_update
359
+ ```
360
+
361
+ ### Low-Level Access
362
+
363
+ ```ruby
364
+ # Get state vector for manual sync
365
+ sv = awareness.encode_state_vector
366
+
367
+ # Get update diffed against a state vector
368
+ update = awareness.encode_state_as_update(remote_state_vector)
369
+
370
+ # Apply raw update to the document
371
+ awareness.apply_update(update_bytes)
372
+
373
+ # Wrap raw update data in a sync message
374
+ message = awareness.encode_update(update_bytes)
375
+ ```
376
+
377
+ ## Thread Safety
378
+
379
+ Unlike the official `y-rb` gem, yrb-lite is safe to share across Ruby threads. A
380
+ `Doc` or `Awareness` can be used concurrently from Puma workers, ActionCable
381
+ connection threads, or background jobs without external locking.
382
+
383
+ That comes from how the underlying types work, not from locking on top:
384
+
385
+ - `yrs::Doc` is `Send + Sync`. Every operation takes the document's internal
386
+ RwLock with blocking semantics (`read_blocking`/`write_blocking`), so
387
+ concurrent access serializes instead of erroring or corrupting state.
388
+ - `yrs::sync::Awareness` is built for multi-threaded servers: client states
389
+ live in a `DashMap` and the whole API is `&self`.
390
+ - The extension adds no interior-mutability tricks. There's no `RefCell`, where
391
+ a re-entrant borrow would panic and take the Ruby process down with it.
392
+ Each native method opens and closes its transaction in one call, so no lock
393
+ or borrow outlives a call and there's nothing to deadlock on.
394
+ - A `Send + Sync` static assertion for both wrapped types lives in `lib.rs`. If
395
+ a yrs upgrade regressed this, the gem would fail to compile instead of quietly
396
+ turning thread-unsafe.
397
+
398
+ `test/thread_safety_test.rb` runs shared docs, the full sync handshake, fan-in
399
+ sync, and awareness state across 8 threads at once, and checks the interleaving
400
+ doesn't change convergence.
401
+
402
+ ### Parallelism (GVL release)
403
+
404
+ Every method that does real CRDT work (applying updates, encoding state,
405
+ handling sync messages) releases Ruby's Global VM Lock
406
+ (`rb_thread_call_without_gvl`) while the native code runs. That buys two things.
407
+
408
+ CRDT work runs in parallel across Ruby threads on MRI, not just
409
+ JRuby/TruffleRuby. `bench/parallelism_bench.rb` measures over 2x wall-clock
410
+ speedup applying a ~900 KB update concurrently; native code that held the GVL
411
+ couldn't beat serial time.
412
+
413
+ A slow operation also can't stall the VM. A thread applying a large update holds
414
+ the doc's write lock without holding the GVL, so other Ruby threads keep running
415
+ instead of queuing behind it.
416
+
417
+ Each method has the same shape: copy the Ruby byte string, drop the GVL, do the
418
+ yrs work (taking and releasing the doc lock entirely inside the closure), take
419
+ the GVL back, then build Ruby objects. No Ruby API is touched without the GVL,
420
+ and the doc lock is never held across a GVL boundary, so the lock order can't
421
+ deadlock. Panics in native code are caught and re-raised as Ruby exceptions.
422
+
423
+ ## Message Type Constants
424
+
425
+ ```ruby
426
+ YrbLite::MSG_SYNC # 0 - Document sync messages
427
+ YrbLite::MSG_AWARENESS # 1 - User presence data
428
+ YrbLite::MSG_AUTH # 2 - Authentication
429
+ YrbLite::MSG_QUERY_AWARENESS # 3 - Request awareness state
430
+
431
+ YrbLite::MSG_SYNC_STEP1 # 0 - State vector request
432
+ YrbLite::MSG_SYNC_STEP2 # 1 - Update response
433
+ YrbLite::MSG_SYNC_UPDATE # 2 - Incremental update
434
+ ```
435
+
436
+ ## Sync Flow
437
+
438
+ ```
439
+ Client A Server
440
+ | |
441
+ |-------- start() --------------->|
442
+ | (SyncStep1 + Awareness) |
443
+ | |
444
+ |<------- handle() response ------|
445
+ | (SyncStep2) |
446
+ | |
447
+ | (Document synchronized!) |
448
+ | |
449
+ |<------- updates ----------------|
450
+ |-------- updates --------------->|
451
+ ```
452
+
453
+ ## Development
454
+
455
+ ```bash
456
+ # Setup
457
+ bundle install
458
+
459
+ # Build extension
460
+ rake compile
461
+
462
+ # Run tests
463
+ rake test
464
+
465
+ # Clean build artifacts
466
+ rake clean
467
+ ```
468
+
469
+ ## License
470
+
471
+ MIT License
472
+
473
+ ## Acknowledgments
474
+
475
+ - [y-crdt/yrs](https://github.com/y-crdt/y-crdt) - The Rust implementation of Y.js
476
+ - [Magnus](https://github.com/matsadler/magnus) - Ruby bindings for Rust
477
+ - [rb-sys](https://github.com/oxidize-rb/rb-sys) - Rust extensions for Ruby
data/lib/yrb-lite.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Entry point matching the gem name, so `Bundler.require` works out of the box.
4
+ require "yrb_lite"
Binary file
Binary file
@@ -0,0 +1,559 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require "securerandom"
5
+
6
+ module YrbLite
7
+ # y-websocket protocol over ActionCable.
8
+ #
9
+ # Include this module in an ActionCable channel to sync Y.js documents
10
+ # (and awareness/presence) with browser clients. Messages are the standard
11
+ # y-protocols binary messages, base64-encoded in a JSON envelope:
12
+ #
13
+ # { "m" => "<base64 bytes>" } # client -> server
14
+ # { "m" => "...", "origin" => "<id>" } # server -> subscribers
15
+ #
16
+ # Example:
17
+ # class DocumentChannel < ApplicationCable::Channel
18
+ # include YrbLite::Sync
19
+ #
20
+ # on_load { |key| Document.find_by(key: key)&.content }
21
+ # on_save { |key, update| Document.find_by(key: key)&.update!(content: update) }
22
+ #
23
+ # def subscribed
24
+ # sync_for params[:id]
25
+ # end
26
+ #
27
+ # def receive(data)
28
+ # sync_receive(data)
29
+ # end
30
+ #
31
+ # def unsubscribed
32
+ # sync_clear_presence
33
+ # end
34
+ # end
35
+ #
36
+ # The shared YrbLite::Awareness instances are safe to use from ActionCable's
37
+ # worker thread pool: the native types are Send + Sync and every operation
38
+ # releases the GVL, so concurrent clients sync in parallel.
39
+ module Sync
40
+ # Validated frame kinds from Awareness#message_kind. A frame only gets a
41
+ # non-DROP kind if it is exactly one well-formed message; anything
42
+ # malformed, truncated, multi-message, or unknown is dropped before it can
43
+ # be processed or relayed.
44
+ MSG_KIND_DROP = 0
45
+ MSG_KIND_SYNC_STEP1 = 1
46
+ MSG_KIND_UPDATE = 2
47
+ MSG_KIND_AWARENESS = 3
48
+ MSG_KIND_AWARENESS_QUERY = 4
49
+
50
+ def self.included(base)
51
+ base.extend(ClassMethods)
52
+ end
53
+
54
+ module ClassMethods
55
+ # Load persisted document state. Called once per key with (key);
56
+ # return a binary Y.js update (or nil for a fresh document).
57
+ def on_load(callable = nil, &block)
58
+ @on_load = callable || block if callable || block
59
+ @on_load
60
+ end
61
+
62
+ # Persist document state. Called with (key, update) after every
63
+ # message that modified the document.
64
+ def on_save(callable = nil, &block)
65
+ @on_save = callable || block if callable || block
66
+ @on_save
67
+ end
68
+
69
+ # Record every document change durably before it is applied or
70
+ # distributed (authoritative audit mode). Called synchronously with
71
+ # (key, update), where update is the exact CRDT delta, serialized per
72
+ # document so the recorded order is the apply order. If the block raises,
73
+ # the change is rejected: neither applied to the shared document nor
74
+ # broadcast to other subscribers.
75
+ #
76
+ # Registering an on_change switches that channel onto the strict path
77
+ # (record, apply, broadcast). Without it, the default fast path applies
78
+ # and broadcasts, with an optional on_save snapshot.
79
+ def on_change(callable = nil, &block)
80
+ @on_change = callable || block if callable || block
81
+ @on_change
82
+ end
83
+
84
+ # Select the document backend:
85
+ # :memory (default): keep a warm in-memory replica per process and keep
86
+ # it current via a custom stream_from callback. Fast, but it assumes
87
+ # classic ActionCable (the callback runs in Ruby) and
88
+ # process<->document affinity.
89
+ # :store: stateless per message, with no warm replica and no custom
90
+ # stream callback. Handshakes and reads are served from the durable
91
+ # store (`on_load`); changes are recorded (`on_change`) and relayed.
92
+ # Works under AnyCable (broadcasts handled outside Ruby, no worker
93
+ # affinity) and across processes. Requires `on_load` and `on_change`.
94
+ def sync_backend(mode = nil)
95
+ @sync_backend = mode if mode
96
+ @sync_backend || :memory
97
+ end
98
+ end
99
+
100
+ # Call from `subscribed`. Streams broadcasts for this document and
101
+ # transmits the server's opening handshake (SyncStep1 + awareness).
102
+ def sync_for(key)
103
+ @sync_key = key.to_s
104
+ @sync_origin = SecureRandom.hex(8)
105
+ @sync_clients = [] # awareness client IDs seen on this connection
106
+
107
+ return sync_for_store_backed if self.class.sync_backend == :store
108
+
109
+ Sync.subscribe(@sync_key)
110
+ awareness = sync_awareness
111
+
112
+ stream_from sync_stream_name, coder: ActiveSupport::JSON do |payload|
113
+ sync_on_broadcast(payload)
114
+ end
115
+
116
+ # Opening handshake: SyncStep1 then the current awareness, each as its
117
+ # own single-message frame, so providers that parse one message per frame
118
+ # (e.g. @y-rb/actioncable) handle both. The client replies SyncStep2 to
119
+ # the SyncStep1, delivering its state to the server.
120
+ sync_transmit(awareness.sync_step1)
121
+ sync_transmit(awareness.encode_awareness_update)
122
+ end
123
+
124
+ # Call from `receive`. Applies the client's message, replies directly
125
+ # when the protocol calls for it, and relays document/awareness changes
126
+ # to the other subscribers.
127
+ #
128
+ # If an `on_change` recorder is registered, document changes take the
129
+ # strict authoritative path (record -> apply -> broadcast, serialized per
130
+ # document); otherwise the fast path is used.
131
+ #
132
+ # Reliable delivery (opt-in, client-driven): if the frame carries an "id",
133
+ # the server replies `{ "ack" => id }` once the update has been accepted
134
+ # (recorded in audit mode, applied in fast mode). A causally-gapped update
135
+ # is not acked -- it gets a resync instead -- so an ack-aware client knows
136
+ # to retransmit until the update lands. Stock clients send no "id", never
137
+ # get acks, and are completely unaffected.
138
+ def sync_receive(data, key = nil)
139
+ # Pass `key` (params[:id]) when your transport doesn't keep the channel
140
+ # instance alive across actions. Under AnyCable each RPC command gets a
141
+ # fresh channel, so instance variables set in `subscribed` are gone here.
142
+ @sync_key = key.to_s if key
143
+
144
+ # Accept both envelope keys: "m" (yrb-lite's own clients) and "update"
145
+ # (the @y-rb/actioncable browser provider).
146
+ m = data.is_a?(Hash) ? (data["m"] || data["update"]) : nil
147
+ return unless m.is_a?(String)
148
+
149
+ # Optional client-supplied id for reliable delivery (see sync_send_ack).
150
+ id = data.is_a?(Hash) ? data["id"] : nil
151
+
152
+ begin
153
+ bytes = Base64.strict_decode64(m)
154
+ rescue ArgumentError
155
+ return # not valid base64; ignore the frame and keep the connection
156
+ end
157
+
158
+ sync_send_ack(id, sync_dispatch(m, bytes))
159
+ end
160
+
161
+ # Route a decoded frame to the backend/path that handles it and return the
162
+ # outcome symbol (:recorded/:applied/:gap/:noop) used by the reliable-
163
+ # delivery ack. A dropped frame returns nil (never acked).
164
+ def sync_dispatch(encoded, bytes)
165
+ return sync_receive_store_backed(encoded, bytes) if self.class.sync_backend == :store
166
+
167
+ awareness = sync_awareness
168
+ kind = awareness.message_kind(bytes)
169
+ # Malformed / truncated / multi-message / unknown frames are dropped
170
+ # before they can be processed or relayed to other clients.
171
+ return if kind == MSG_KIND_DROP
172
+
173
+ sync_track_clients(awareness, bytes) if kind == MSG_KIND_AWARENESS
174
+
175
+ if kind == MSG_KIND_UPDATE && self.class.on_change
176
+ sync_apply_authoritative(awareness, encoded, bytes)
177
+ else
178
+ sync_apply_fast(awareness, encoded, bytes, kind)
179
+ end
180
+ end
181
+
182
+ # Call from `unsubscribed`. Clears the presence states this connection
183
+ # introduced and tells the other subscribers to drop those cursors, so a
184
+ # closed tab or dropped socket doesn't leave a ghost cursor behind until
185
+ # the client-side timeout reaps it.
186
+ def sync_clear_presence
187
+ return if @sync_clients.nil? || @sync_clients.empty?
188
+
189
+ removal = sync_awareness.remove_clients(@sync_clients)
190
+ @sync_clients = []
191
+ return if removal.empty?
192
+
193
+ sync_distribute(Base64.strict_encode64(removal))
194
+ end
195
+
196
+ # Call from `unsubscribed`. Clears this connection's presence and, when the
197
+ # last subscriber for the document leaves, persists and unloads it from
198
+ # memory (only when an `on_load` is configured to bring it back; otherwise
199
+ # the in-memory document is the only copy and is kept). Prevents a
200
+ # long-running server from accumulating every document it has ever served.
201
+ def sync_unsubscribed(key = nil)
202
+ @sync_key = key.to_s if key
203
+ return if self.class.sync_backend == :store # nothing cached per process
204
+
205
+ sync_clear_presence
206
+ saver = self.class.on_save
207
+ Sync.release(@sync_key, evictable: !self.class.on_load.nil?) do |awareness|
208
+ saver&.call(@sync_key, awareness.encode_state_as_update)
209
+ end
210
+ end
211
+
212
+ # The shared Awareness (document + presence) for this channel's key.
213
+ # Also useful for server-side reads, e.g.:
214
+ # sync_awareness.encode_state_as_update
215
+ def sync_awareness
216
+ Sync.awareness_for(@sync_key, self.class.on_load)
217
+ end
218
+
219
+ private
220
+
221
+ # Default path: apply the message, answer direct requests, relay
222
+ # state-changing messages to the other subscribers. Routing comes from the
223
+ # native `kind` (from Awareness#message_kind) rather than peeking at bytes.
224
+ # Document changes (SyncStep2, Update) and awareness get relayed; requests
225
+ # (SyncStep1, awareness-query) are answered above and not relayed. An
226
+ # optional on_save snapshot is taken after a document change.
227
+ #
228
+ # Returns an outcome symbol for the reliable-delivery ack: :applied when a
229
+ # document update was integrated and relayed, :gap when it was rejected for
230
+ # a resync, :noop for everything else (requests, awareness, empty updates).
231
+ def sync_apply_fast(awareness, encoded, bytes, kind)
232
+ # A document update that isn't causally ready (an earlier one was lost in
233
+ # transit) would relay an un-integrable change to peers and stall the
234
+ # replica. Drop it and ask the client to resync instead, which re-delivers
235
+ # the missing piece. See sync_apply_authoritative for the durable variant.
236
+ if kind == MSG_KIND_UPDATE
237
+ update = awareness.update_from_message(bytes)
238
+ # A no-op message (e.g. the empty SyncStep2 in an opening handshake)
239
+ # carries no change, so there's nothing to relay, persist, or ack.
240
+ return :noop unless update
241
+
242
+ unless awareness.update_ready?(update)
243
+ sync_request_resync(awareness)
244
+ return :gap
245
+ end
246
+ end
247
+
248
+ response = awareness.handle(bytes)
249
+ sync_transmit(response) unless response.empty?
250
+
251
+ return :noop unless [MSG_KIND_UPDATE, MSG_KIND_AWARENESS].include?(kind)
252
+
253
+ sync_distribute(encoded)
254
+ return :noop unless kind == MSG_KIND_UPDATE
255
+
256
+ sync_persist
257
+ :applied
258
+ end
259
+
260
+ # Authoritative path: record the change durably, then apply it to the
261
+ # shared document, then distribute it. The sequence runs under a
262
+ # per-document lock so changes are recorded in a single total order that
263
+ # matches the order they're applied, and nothing is distributed (or applied)
264
+ # before it has been recorded. If the recorder raises, the change is
265
+ # rejected (not applied, not broadcast) and the exception propagates, so the
266
+ # channel can surface it and the client can resync.
267
+ #
268
+ # Before recording, the update must be causally ready: every dependency it
269
+ # references must already be in the doc. If an earlier update was lost in
270
+ # transit, or its record failed, a later update arrives with a gap. Recording
271
+ # it would write a permanently-pending entry to the log -- one that can never
272
+ # be replayed until the missing update shows up. Such an update is rejected
273
+ # (not recorded, not applied, not relayed) and the client is asked to resync,
274
+ # which re-delivers the missing range as one causally-complete delta.
275
+ def sync_apply_authoritative(awareness, encoded, bytes)
276
+ recorder = self.class.on_change
277
+
278
+ outcome = Sync.lock_for(@sync_key).synchronize do
279
+ update = awareness.update_from_message(bytes)
280
+ # A no-op message (e.g. the empty SyncStep2 in a client's opening
281
+ # handshake) carries no change, so there's nothing to record or relay.
282
+ next :noop unless update
283
+ next :gap unless awareness.update_ready?(update)
284
+
285
+ recorder.call(@sync_key, update) # durable write; raise to reject
286
+ awareness.apply_update(update) # only recorded changes reach the doc
287
+ sync_distribute(encoded) # ...and only then the wire
288
+ :recorded
289
+ end
290
+
291
+ case outcome
292
+ when :recorded then sync_persist
293
+ when :gap then sync_request_resync(awareness)
294
+ end
295
+
296
+ # Surface the outcome for the reliable-delivery ack: :recorded means the
297
+ # update is durably written (and will be acked); :gap triggered a resync
298
+ # (no ack); :noop carried no change.
299
+ outcome
300
+ end
301
+
302
+ # Ask this connection's client to resync: re-send SyncStep1 carrying the
303
+ # server's current (gap-free) state vector. The client replies SyncStep2
304
+ # with everything the server is missing, delivered as one causally-complete
305
+ # delta -- which heals the gap that triggered the resync.
306
+ def sync_request_resync(awareness)
307
+ sync_transmit(awareness.sync_step1)
308
+ end
309
+
310
+ # Reliable delivery: acknowledge an accepted update back to the sending
311
+ # connection. An ack-aware client tags each outgoing update with an "id"
312
+ # and retains it until the matching `{ "ack" => id }` returns, retransmitting
313
+ # on a timer or reconnect; idempotent CRDT apply makes resends free. We ack
314
+ # only when the client supplied an id (so stock clients are unaffected) and
315
+ # the update was actually accepted -- recorded in audit mode, applied in fast
316
+ # mode. A gapped update gets no ack (it got a resync), so the client keeps
317
+ # retransmitting until the missing range lands and the update can integrate.
318
+ def sync_send_ack(id, outcome)
319
+ return if id.nil?
320
+ return unless %i[recorded applied].include?(outcome)
321
+
322
+ # Braces are load-bearing: a bare hash would bind to transmit's `via:`
323
+ # keyword instead of its positional data argument.
324
+ transmit({ "ack" => id })
325
+ end
326
+
327
+ # Single broadcast point for both paths (and presence removal), so the
328
+ # relay semantics live in one place and tests can observe distribution.
329
+ # `origin` identifies the sending connection (don't echo to it); `pid`
330
+ # identifies the sending process (other processes apply it to their own
331
+ # replica; see sync_on_broadcast).
332
+ def sync_distribute(encoded)
333
+ ActionCable.server.broadcast(
334
+ sync_stream_name,
335
+ sync_envelope(encoded, "origin" => @sync_origin, "pid" => Sync.process_id)
336
+ )
337
+ end
338
+
339
+ # Transmit raw protocol bytes to this connection (base64, dual-key).
340
+ def sync_transmit(bytes)
341
+ transmit(sync_envelope(Base64.strict_encode64(bytes)))
342
+ end
343
+
344
+ # Build an outgoing envelope. We send the payload under both keys: "m"
345
+ # (yrb-lite's own clients) and "update" (the @y-rb/actioncable provider),
346
+ # so either client works against the same server.
347
+ def sync_envelope(encoded, extra = {})
348
+ { "m" => encoded, "update" => encoded }.merge(extra)
349
+ end
350
+
351
+ # Handle a broadcast delivered by the cable adapter. With a multi-process
352
+ # adapter (Redis, solid_cable), it may have come from another server
353
+ # process. Keep this process's in-memory replica current with changes that
354
+ # originated elsewhere, then relay to this connection's browser.
355
+ def sync_on_broadcast(payload)
356
+ sync_apply_remote(payload["m"]) if payload["pid"] != Sync.process_id
357
+ transmit(payload) unless payload["origin"] == @sync_origin
358
+ end
359
+
360
+ # Apply a change that originated on another process to this process's
361
+ # replica, without re-recording it (the origin process already recorded it
362
+ # before broadcasting). The CRDT merge is idempotent and commutative, so a
363
+ # cold replica converges regardless of ordering, and applying from several
364
+ # local connections is harmless.
365
+ def sync_apply_remote(encoded)
366
+ return unless encoded.is_a?(String)
367
+
368
+ begin
369
+ bytes = Base64.strict_decode64(encoded)
370
+ rescue ArgumentError
371
+ return
372
+ end
373
+
374
+ awareness = sync_awareness
375
+ case awareness.message_kind(bytes)
376
+ when MSG_KIND_UPDATE
377
+ update = awareness.update_from_message(bytes)
378
+ awareness.apply_update(update) if update
379
+ when MSG_KIND_AWARENESS
380
+ awareness.handle(bytes)
381
+ end
382
+ end
383
+
384
+ # -- Store-backed (AnyCable-native) path --------------------------------
385
+
386
+ # Subscribe without a custom block, so AnyCable (which delivers broadcasts
387
+ # outside Ruby) relays them directly. Send the opening SyncStep1 built from
388
+ # the durable store. No warm replica is kept.
389
+ def sync_for_store_backed
390
+ stream_from sync_stream_name
391
+ sync_transmit(sync_load_doc.sync_step1)
392
+ end
393
+
394
+ # Stateless per message: no warm replica, no assumptions about which process
395
+ # owns a document. A client's SyncStep1 is answered from the store, document
396
+ # changes are recorded durably before relay and then broadcast, and
397
+ # awareness is relayed best-effort. Echoing back to the sender is harmless,
398
+ # since the CRDT apply is idempotent.
399
+ #
400
+ # Returns an outcome symbol for the reliable-delivery ack: :recorded when a
401
+ # document update was durably recorded and relayed, :gap when it was
402
+ # rejected for a resync, :noop for everything else.
403
+ def sync_receive_store_backed(encoded, bytes)
404
+ case Sync.codec.message_kind(bytes)
405
+ when MSG_KIND_SYNC_STEP1
406
+ result = sync_load_doc.handle_sync_message(bytes)
407
+ sync_transmit(result[2]) if result
408
+ :noop
409
+ when MSG_KIND_UPDATE
410
+ update = Sync.codec.update_from_message(bytes)
411
+ return :noop unless update
412
+
413
+ # Store mode keeps no warm replica, so to tell whether this update is
414
+ # causally ready we rebuild the doc from the store and check against it.
415
+ # That's an O(history) load per update (mitigated by snapshotting the
416
+ # store on the load path). A gappy update -- an earlier one was lost or
417
+ # its record failed -- is rejected and the client asked to resync,
418
+ # rather than written to the log as a permanently-pending entry.
419
+ doc = sync_load_doc
420
+ unless doc.update_ready?(update)
421
+ sync_transmit(doc.sync_step1)
422
+ return :gap
423
+ end
424
+
425
+ self.class.on_change&.call(@sync_key, update) # record before relay
426
+ sync_distribute(encoded)
427
+ :recorded
428
+ when MSG_KIND_AWARENESS
429
+ sync_distribute(encoded)
430
+ :noop
431
+ else
432
+ :noop
433
+ end
434
+ end
435
+
436
+ # Build a fresh document from the durable store (on_load).
437
+ def sync_load_doc
438
+ doc = YrbLite::Doc.new
439
+ state = self.class.on_load&.call(@sync_key)
440
+ doc.apply_update(state) if state
441
+ doc
442
+ end
443
+
444
+ # Record the awareness client IDs carried by an incoming message (already
445
+ # known to be an awareness frame) so we can clear them when this connection
446
+ # closes.
447
+ def sync_track_clients(awareness, bytes)
448
+ awareness.awareness_client_ids(bytes).each do |id|
449
+ @sync_clients << id unless @sync_clients.include?(id)
450
+ end
451
+ end
452
+
453
+ def sync_stream_name
454
+ "yrb_lite:#{@sync_key}"
455
+ end
456
+
457
+ def sync_persist
458
+ return unless (saver = self.class.on_save)
459
+
460
+ saver.call(@sync_key, sync_awareness.encode_state_as_update)
461
+ end
462
+
463
+ # -- Shared document registry ------------------------------------------
464
+
465
+ @registry = {}
466
+ @locks = {}
467
+ @subscribers = Hash.new(0)
468
+ @registry_mutex = Mutex.new
469
+
470
+ class << self
471
+ # A stable id for this server process, stamped on every broadcast so
472
+ # other processes know to apply it to their replica and this process
473
+ # knows to skip its own. Survives for the life of the process.
474
+ def process_id
475
+ @process_id ||= SecureRandom.hex(8)
476
+ end
477
+
478
+ # A shared, stateless decoder for the store-backed path. message_kind and
479
+ # update_from_message only read their argument (they don't touch the
480
+ # instance's document), so one shared instance is safe across threads.
481
+ def codec
482
+ @codec ||= YrbLite::Awareness.new
483
+ end
484
+
485
+ # Get or create the shared Awareness for a key. Creation (including
486
+ # the on_load callback) is serialized under a mutex so concurrent
487
+ # subscribers can never observe two documents for one key; all
488
+ # subsequent operations run lock-free on the thread-safe native types.
489
+ def awareness_for(key, loader = nil)
490
+ @registry_mutex.synchronize do
491
+ @registry[key] ||= begin
492
+ awareness = YrbLite::Awareness.new
493
+ if loader && (state = loader.call(key))
494
+ awareness.apply_update(state)
495
+ end
496
+ awareness
497
+ end
498
+ end
499
+ end
500
+
501
+ # Per-document mutex serializing the authoritative record -> apply ->
502
+ # broadcast section, so a document's audit log is a single total order.
503
+ # Only briefly holds the registry mutex to fetch/create the lock; the
504
+ # durable write itself runs while holding only this per-key lock.
505
+ def lock_for(key)
506
+ @registry_mutex.synchronize { @locks[key] ||= Mutex.new }
507
+ end
508
+
509
+ # Count a new subscriber for a document.
510
+ def subscribe(key)
511
+ @registry_mutex.synchronize { @subscribers[key] += 1 }
512
+ end
513
+
514
+ # Drop a subscriber. When the last one leaves and the document is
515
+ # evictable (there's an on_load to bring it back, so unloading can't lose
516
+ # data), persist it via the given block and unload it from memory, so a
517
+ # long-running server doesn't accumulate every document and lock it has
518
+ # ever seen. Returns true if the document was evicted.
519
+ #
520
+ # The persist runs outside the registry lock (it may do I/O), and we
521
+ # re-check the subscriber count afterward: if someone reconnected while
522
+ # we were saving, eviction is aborted and the warm document is kept.
523
+ def release(key, evictable:)
524
+ awareness = @registry_mutex.synchronize do
525
+ @subscribers[key] -= 1 if @subscribers[key].positive?
526
+ next nil unless @subscribers[key].zero?
527
+
528
+ @subscribers.delete(key)
529
+ evictable ? @registry[key] : nil
530
+ end
531
+ return false unless awareness
532
+
533
+ yield awareness if block_given?
534
+
535
+ @registry_mutex.synchronize do
536
+ # A subscriber may have returned during the persist above.
537
+ next false unless @subscribers[key].zero?
538
+
539
+ @subscribers.delete(key)
540
+ @locks.delete(key)
541
+ !@registry.delete(key).nil?
542
+ end
543
+ end
544
+
545
+ def registry
546
+ @registry_mutex.synchronize { @registry.dup }
547
+ end
548
+
549
+ # Clear all documents (useful for testing).
550
+ def reset!
551
+ @registry_mutex.synchronize do
552
+ @registry = {}
553
+ @locks = {}
554
+ @subscribers = Hash.new(0)
555
+ end
556
+ end
557
+ end
558
+ end
559
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module YrbLite
4
+ VERSION = "0.1.0.beta2"
5
+ end
data/lib/yrb_lite.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "yrb_lite/version"
4
+
5
+ # Load the native extension. Precompiled gems ship it in a per-Ruby-version
6
+ # subdir (lib/yrb_lite/<major.minor>/yrb_lite.<ext>); a source build puts it
7
+ # flat at lib/yrb_lite/yrb_lite.<ext>. Try the versioned path first, fall back.
8
+ begin
9
+ RUBY_VERSION =~ /(\d+\.\d+)/
10
+ require_relative "yrb_lite/#{Regexp.last_match(1)}/yrb_lite"
11
+ rescue LoadError
12
+ require_relative "yrb_lite/yrb_lite"
13
+ end
14
+
15
+ module YrbLite
16
+ # Error class is defined in Rust extension
17
+
18
+ # Autoload Sync module - only loaded when ActionCable is available
19
+ autoload :Sync, "yrb_lite/sync"
20
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yrb-lite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.beta2
5
+ platform: x64-mingw-ucrt
6
+ authors:
7
+ - JP Camara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: base64
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake-compiler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ description: yrb-lite is a thread-safe Ruby binding over the Rust y-crdt (yrs) library
70
+ plus an ActionCable concern implementing the full y-websocket sync protocol and
71
+ awareness. It lets a Rails app be the collaboration server for Y.js editors (Tiptap,
72
+ ProseMirror, BlockNote) with no Node sidecar.
73
+ email:
74
+ - johnpcamara@gmail.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - CHANGELOG.md
80
+ - LICENSE
81
+ - README.md
82
+ - lib/yrb-lite.rb
83
+ - lib/yrb_lite.rb
84
+ - lib/yrb_lite/3.4/yrb_lite.so
85
+ - lib/yrb_lite/4.0/yrb_lite.so
86
+ - lib/yrb_lite/sync.rb
87
+ - lib/yrb_lite/version.rb
88
+ homepage: https://github.com/jpcamara/yrb-lite
89
+ licenses:
90
+ - MIT
91
+ metadata:
92
+ source_code_uri: https://github.com/jpcamara/yrb-lite
93
+ changelog_uri: https://github.com/jpcamara/yrb-lite/blob/main/CHANGELOG.md
94
+ bug_tracker_uri: https://github.com/jpcamara/yrb-lite/issues
95
+ rubygems_mfa_required: 'true'
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '3.4'
105
+ - - "<"
106
+ - !ruby/object:Gem::Version
107
+ version: 4.1.dev
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubygems_version: 3.5.23
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Thread-safe Ruby bindings for y-crdt (Y.js) with the y-websocket sync protocol
118
+ for ActionCable
119
+ test_files: []