mankai-sockets 0.1.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.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +28 -0
  3. data/LICENSE +20 -0
  4. data/PROTOCOL.md +111 -0
  5. data/README.md +630 -0
  6. data/js/mankai-sockets.js +244 -0
  7. data/js/package.json +4 -0
  8. data/js/test/client.test.js +225 -0
  9. data/lib/mankai/sockets/authentication.rb +72 -0
  10. data/lib/mankai/sockets/backends/memory.rb +69 -0
  11. data/lib/mankai/sockets/broadcast.rb +22 -0
  12. data/lib/mankai/sockets/broadcaster.rb +43 -0
  13. data/lib/mankai/sockets/channel.rb +82 -0
  14. data/lib/mankai/sockets/channel_map.rb +130 -0
  15. data/lib/mankai/sockets/cli.rb +157 -0
  16. data/lib/mankai/sockets/close_code.rb +57 -0
  17. data/lib/mankai/sockets/context.rb +24 -0
  18. data/lib/mankai/sockets/endpoint.rb +60 -0
  19. data/lib/mankai/sockets/errors.rb +20 -0
  20. data/lib/mankai/sockets/executor.rb +61 -0
  21. data/lib/mankai/sockets/frames.rb +131 -0
  22. data/lib/mankai/sockets/hub.rb +85 -0
  23. data/lib/mankai/sockets/identity.rb +21 -0
  24. data/lib/mankai/sockets/instrumenter.rb +65 -0
  25. data/lib/mankai/sockets/limits.rb +37 -0
  26. data/lib/mankai/sockets/mailbox.rb +58 -0
  27. data/lib/mankai/sockets/message.rb +24 -0
  28. data/lib/mankai/sockets/origin_policy.rb +117 -0
  29. data/lib/mankai/sockets/protocol.rb +186 -0
  30. data/lib/mankai/sockets/provider_source.rb +224 -0
  31. data/lib/mankai/sockets/server.rb +106 -0
  32. data/lib/mankai/sockets/session.rb +331 -0
  33. data/lib/mankai/sockets/session_dependencies.rb +19 -0
  34. data/lib/mankai/sockets/session_factory.rb +50 -0
  35. data/lib/mankai/sockets/sessions.rb +49 -0
  36. data/lib/mankai/sockets/stream.rb +70 -0
  37. data/lib/mankai/sockets/subscription.rb +55 -0
  38. data/lib/mankai/sockets/subscriptions.rb +54 -0
  39. data/lib/mankai/sockets/testing/backend_examples.rb +69 -0
  40. data/lib/mankai/sockets/testing.rb +151 -0
  41. data/lib/mankai/sockets/tokens.rb +96 -0
  42. data/lib/mankai/sockets/transport/reactor.rb +152 -0
  43. data/lib/mankai/sockets/transport/socket.rb +150 -0
  44. data/lib/mankai/sockets/version.rb +9 -0
  45. data/lib/mankai/sockets.rb +40 -0
  46. data/lib/mankai-sockets.rb +3 -0
  47. data/mankai-sockets.gemspec +39 -0
  48. metadata +193 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3d382cad6c10128f0c53b6d7bf178bd2b4aa5708d604d063bfa95a1b3d02b9be
4
+ data.tar.gz: d433e52b0877780c4614808b6ea2b2b705ea94699b46d6a1f1341b6e76d42985
5
+ SHA512:
6
+ metadata.gz: e648ead6936f8822272204cb116caf4da390a77267b805c8fd435bc2f6de8d9563df781104a52f8712fb7c181b62f64ff2399a48bc6e3eaedc33d7bdf8b6a6d0
7
+ data.tar.gz: ac0181a7f181aa3283d9979b5e39a754adfedd5f170276c3af13e8bd713e0455d9df7507a81c6202ad0ae854949e45cefa8a011d9f120c50b9989935c9a79c1b
data/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Break Versioning](https://www.taoensso.com/break-versioning).
7
+
8
+ ## [0.1.0]
9
+
10
+ ### Added
11
+
12
+ - A websocket Rack endpoint, validated typed frames, channels with declared params and actions, cookie or token authentication, and a strict origin policy.
13
+ - A reactor/executor transport with a serial mailbox per session, a hub for stream fan-out, size and connection limits, and connection revocation by identity.
14
+ - A dry-system provider source registering `sockets.endpoint`, `sockets.server`, `sockets.broadcaster`, `sockets.backend`, `sockets.channels` and optional `sockets.tokens`.
15
+ - An in-process memory backend, shared backend examples, test helpers, events, and a `hanami sockets install` command.
16
+ - A dependency-free browser client with explicit connect, subscriptions, actions, heartbeats and injectable reconnect policy.
17
+ - `Mankai::Sockets::Testing::Matchers` adds `have_broadcast` and `assert_broadcast` as instance methods; `have_broadcast` accepts composable matchers such as `a_hash_including`, and `assert_broadcast` raises `Minitest::Assertion` when Minitest is loaded.
18
+
19
+ ### Fixed
20
+
21
+ - The `mankai.sockets.action` event is now emitted for every action, with `session_id`, `channel`, `subscription_id`, `action` and `outcome` (`:success`, `:failure` or `:error`).
22
+ - `Tokens#issue` accepts non-String subjects such as Integer ids and stores them as Strings, so `issue(current_user.id)` works; the authenticator always receives a String subject.
23
+ - The provider starts when a channel injects one of its own components, such as an operation that uses `sockets.broadcaster`; a channel that fails to load now reports the error instead of "is not registered".
24
+ - Slice-prefixed channel keys such as `"admin:channels.reports"` resolve from a provider registered in the app.
25
+ - `hanami sockets install` prints a provider that works as written (`allowed_origins = :same_origin`, a session authenticator, the SQL backend line for `--backend=sql`), and copies the client to `public/js` when hanami-assets is not bundled.
26
+ - The browser client calls `disconnected()` only for subscriptions the server accepted, resets its reconnect backoff after a successful connection, and no longer reconnects after close code 4429.
27
+ - The memory backend calls listeners outside its lock, so a broadcast can no longer deadlock with a subscription being added at the same time.
28
+ - With `allow_anonymous = true` and no authenticator, clients connect with anonymous identities instead of being closed with 4401.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Mankai contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/PROTOCOL.md ADDED
@@ -0,0 +1,111 @@
1
+ # mankai-sockets protocol, version 1
2
+
3
+ This document is the complete wire contract. A client in any language can be written from it alone.
4
+
5
+ ## Transport
6
+
7
+ The endpoint is mounted at `/sockets` and speaks the WebSocket protocol over a Rack full-hijack
8
+ upgrade. Clients must offer the subprotocol `mankai.sockets.v1`; a handshake without it is refused
9
+ with HTTP 400. After the upgrade, all frames are UTF-8 JSON objects. Every frame has a string
10
+ `type` field. Unknown fields are rejected.
11
+
12
+ A session runs in this order:
13
+
14
+ ```
15
+ client -> server hello
16
+ server -> client welcome
17
+ client -> server subscribe ... action ... unsubscribe ...
18
+ server -> client subscribed / rejected / message / ack / error
19
+ either ping <-> pong
20
+ either close (with a code below)
21
+ ```
22
+
23
+ No frame other than `hello` is accepted until `welcome` has been sent. A second `hello` closes the
24
+ connection.
25
+
26
+ ## Inbound frames
27
+
28
+ | type | Fields | Notes |
29
+ |---|---|---|
30
+ | `hello` | `token` (optional string, at most 2048 bytes) | The token is verified and passed to the app's authenticator. |
31
+ | `subscribe` | `id` (string, 1–64 bytes), `channel` (string, 1–128 bytes), `params` (object, default `{}`) | `id` is chosen by the client and unique on this connection. |
32
+ | `unsubscribe` | `id` | Stops delivery to that subscription. |
33
+ | `action` | `id`, `name` (string, 1–128 bytes), `data` (object, default `{}`), `ref` (optional string, 1–64 bytes) | Calls the channel's action; `ref` asks for a reply. |
34
+ | `ping` | — | Answered with `pong`. |
35
+ | `pong` | — | Liveness only. |
36
+
37
+ Examples:
38
+
39
+ ```json
40
+ {"type":"hello","token":"eyJ...sig"}
41
+ {"type":"subscribe","id":"s1","channel":"chat","params":{"room":42}}
42
+ {"type":"action","id":"s1","name":"speak","data":{"body":"hi"},"ref":"r1"}
43
+ {"type":"unsubscribe","id":"s1"}
44
+ {"type":"ping"}
45
+ ```
46
+
47
+ ## Outbound frames
48
+
49
+ | type | Fields | Notes |
50
+ |---|---|---|
51
+ | `welcome` | `session_id` | Sent once, after a successful `hello`. |
52
+ | `subscribed` | `id`, `data` (optional object) | `data` comes from the channel's `subscribe` and is for this subscriber only. |
53
+ | `rejected` | `id`, `reason` | `reason` is generic: `invalid`, `unauthorized` or `internal`. The subscription is gone. |
54
+ | `message` | `id`, `stream_id`, `data` (object) | One frame per matching subscription. `data` is the broadcast's JSON object. |
55
+ | `ack` | `ref`, `data` (optional object) | Only sent when the client supplied `ref`. |
56
+ | `error` | `ref`, `reason` | Only sent when the client supplied `ref`; `reason` is generic: `invalid`, `unauthorized` or `internal`. |
57
+ | `ping` | — | Answer with `pong`. Also sent by the server as a WebSocket ping for proxies. |
58
+ | `pong` | — | Reply to a client `ping`. |
59
+
60
+ Examples:
61
+
62
+ ```json
63
+ {"type":"welcome","session_id":"4f1..."}
64
+ {"type":"subscribed","id":"s1","data":{"room":42,"joined_at":"2026-01-01T12:00:00Z"}}
65
+ {"type":"message","id":"s1","stream_id":"chat:42","data":{"body":"hi","user_id":7}}
66
+ {"type":"ack","ref":"r1"}
67
+ {"type":"error","ref":"r1","reason":"unauthorized"}
68
+ {"type":"ping"}
69
+ ```
70
+
71
+ ## Close codes
72
+
73
+ Reasons are generic on the wire; details go to the app's logs and events.
74
+
75
+ | Code | Reason | When |
76
+ |---|---|---|
77
+ | 4400 | `invalid` | Not JSON, unknown type, a bad field, invalid UTF-8, or a frame before `hello`. |
78
+ | 4401 | `unauthorized` | Authentication failed, token invalid or expired, or the identity was disconnected. |
79
+ | 4408 | `hello timeout` | No `hello` within the configured timeout. |
80
+ | 4409 | `duplicate id` | `subscribe` used an `id` already active on this connection. |
81
+ | 4410 | `duplicate hello` | A second `hello`. |
82
+ | 4429 | `too many subscriptions` | Over the configured limit. |
83
+ | 4430 | `too many connections` | The identity has too many connections in this process. |
84
+ | 4008 | `slow consumer` | The write buffer is over its limit. |
85
+ | 4000 | `idle` | Nothing received for twice the heartbeat interval. |
86
+ | 1001 | `going away` | The server is stopping. |
87
+
88
+ An oversized frame is closed by the WebSocket driver with code 1009.
89
+
90
+ A client should not reconnect after 4400, 4401, 4429 or 4430: the next attempt would fail the same
91
+ way.
92
+
93
+ ## Handshake responses
94
+
95
+ Before the upgrade, the endpoint answers a plain HTTP request with 426, a disallowed `Origin` with
96
+ 403, a handshake without the `mankai.sockets.v1` subprotocol with 400, and a server without Rack
97
+ full hijack with 501.
98
+
99
+ ## Heartbeats
100
+
101
+ The server sends a JSON `ping` and a WebSocket ping at the configured interval, and closes idle
102
+ connections with 4000. Clients may send `ping` at any time after `welcome`; the server answers
103
+ `pong`. Browsers cannot send WebSocket control frames from JavaScript, which is why liveness also
104
+ uses JSON frames.
105
+
106
+ ## Streams
107
+
108
+ A stream is a colon-separated name such as `chat:42`. Clients never choose streams directly:
109
+ channels choose them when a subscription is accepted, using parts that cannot contain `:`. Names
110
+ starting with `mankai.` are reserved by the gem and cannot be used by apps. Messages are delivered
111
+ only to subscriptions whose channel selected that exact stream.