@dovetail-ts/server 0.0.0-stage → 1.0.1

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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ # @dovetail-ts/server
2
+
3
+ ## 1.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 21b3dbf: Publish through OIDC trusted publishing only, with the Ember Workbench release kit. Each version from this release on has npm provenance.
8
+ - Updated dependencies [21b3dbf]
9
+ - @dovetail-ts/protocol@1.0.1
10
+
11
+ ## 1.0.0
12
+
13
+ ### Major Changes
14
+
15
+ - 480744b: First release of Dovetail: real-time and offline sync for Ember.
16
+
17
+ - `@dovetail-ts/core`: the connection life cycle, live store subscriptions, the optimistic ledger with rebase and rollback, the offline write queue in IndexedDB with a leader tab and dead letters, `DocSession` and `PresenceRoom`.
18
+ - `@dovetail-ts/warp-drive`: the store feed into WarpDrive.
19
+ - `@dovetail-ts/crdt`, `crdt-yjs`, `crdt-loro` and `crdt-automerge`: one `DocEngine` interface with three engines.
20
+ - `@dovetail-ts/tiptap`: co-editing in Tiptap 3 with carets, per-user undo, versions and comment threads.
21
+ - `@dovetail-ts/ember`, `notifications` and `inspector`: presence and status components, the inbox, toasts and activity feeds, and a dev panel.
22
+ - `@dovetail-ts/server`, the transports (`transport-ws`, `transport-sse`, `transport-cable`) and `conformance`: a Node server and one wire protocol that the Rails gems also pass.
23
+ - Adapters for Hocuspocus, Liveblocks, PartyKit and Supabase, and integrations for Switchyard, Millwork, Benchdog and Lattice. The Neon adapter is not in 1.0: it waits for a stable Neon Realtime client (1.1).
24
+
25
+ ### Patch Changes
26
+
27
+ - Updated dependencies [480744b]
28
+ - @dovetail-ts/protocol@1.0.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Joe Gaudet
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.
package/README.md CHANGED
@@ -1,3 +1,150 @@
1
- # Temporary Holding Version
1
+ # @dovetail-ts/server
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The Node server: rooms, relay, presence, the store feed, notifications and the auth hooks. On
4
+ Hono and ws. It passes `@dovetail-ts/conformance` over WebSocket and SSE.
5
+
6
+ Part of [Dovetail](https://joegaudet.com/dovetail/). Not released yet.
7
+
8
+ ```ts
9
+ import { createDovetailServer, listen } from '@dovetail-ts/server'
10
+
11
+ const server = createDovetailServer({
12
+ // Never look up a blank token. Compare a digest of the token in constant time; the guide
13
+ // page "Auth hooks" has a full example. Give back { id } or null.
14
+ authenticate: async token => (token ? users.findByTokenDigest(token) : null),
15
+ // Only `true` allows the action. Action is 'join' or 'write'.
16
+ authorize: async (user, room, action) => (await canAccess(user, room, action)) === true,
17
+ handleWrite: async ({ user, room, op }) => {
18
+ const document = await save(user, op)
19
+ return document
20
+ ? { ok: true, document }
21
+ : { ok: false, status: 422, errors: [{ title: 'Invalid' }] }
22
+ },
23
+ })
24
+ const { url, close } = await listen(server, { port: 3000, path: '/dovetail' })
25
+
26
+ server.publish('note:1', { data: { type: 'note', id: '1', attributes: { title: 'Plan' } } })
27
+ server.remove('note:1', { type: 'note', id: '2' })
28
+ await server.notify('u2', {
29
+ verb: 'commented',
30
+ actor: { type: 'user', id: 'u1' },
31
+ target: { type: 'note', id: '1' },
32
+ })
33
+ ```
34
+
35
+ ## The server
36
+
37
+ `createDovetailServer(options)` gives a `DovetailServer`.
38
+
39
+ | Option | Default | |
40
+ | -------------------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
41
+ | `authenticate` | none (user null) | `(token) => user \| null`. Null refuses the `hello`. |
42
+ | `allowAnonymous` | false | Gives a missing or blank token to `authenticate` as `undefined`. Without it, the server refuses such a token before the hook. |
43
+ | `authorize` | allow | `(user, room, action) => boolean`. Only `true` allows. Called for each join, and with `write` for each write, doc update and doc sync step 2. |
44
+ | `authorizeAwareness` | set `state.user.id` | `(user, room, state) => state \| false`. Gives the state to relay, or false to refuse it (`forbidden`). Without it, the server sets `state.user.id` to the id of the user. |
45
+ | `onWarning` | `console.warn` | Gets each warning, as the one for `authenticate` with no `authorize`. |
46
+ | `handleWrite` | ack, feed the op | `({ user, session, id, room, op }) => WriteResult`. `id` is the write id, so a host can skip a resend. |
47
+ | `docs` | memory | A `DocPersistence`: the doc updates of each document, as base64. |
48
+ | `mergeUpdates` | none | Merges doc updates (as `Y.mergeUpdates`), so a sync sends one step 2. When it throws, the sync sends each update. |
49
+ | `validateUpdate` | none | `(update, { user, session, room, doc }) => boolean`. False refuses a doc update with `invalid-message`. |
50
+ | `notifications` | memory | A `NotificationStore`. |
51
+ | `limits` | protocol defaults | The limits of `parseMessage` and of the server. Read [Limits](#limits). |
52
+ | `awarenessTimeout` | 30000 ms | The time after which an awareness state with no update goes. |
53
+ | `clock` | the system clock | `now`, `setTimeout`, `clearTimeout`. A `FakeClock` fits. |
54
+ | `onError` | none | Gets each exception of a hook. The client gets a safe `internal` error. |
55
+ | `onSessionEnd` | none | Called once when a connection closes, with `{ id, user, rooms }`. |
56
+
57
+ ### Limits
58
+
59
+ `limits` takes these fields. Each one has a safe default.
60
+
61
+ | Limit | Default | What it limits |
62
+ | ---------------------- | ------- | --------------------------------------------------------------------------------------------------------------- |
63
+ | `maxMessageBytes` | 1 MiB | The size of a frame (`parseMessage`). |
64
+ | `maxNameLength` | 256 | A room, a doc name, an id or a cursor (`parseMessage`). |
65
+ | `maxOps` | 1000 | The ops of one feed message (`parseMessage`). |
66
+ | `maxDepth` | 32 | The nesting of JSON (`parseMessage`). |
67
+ | `maxRooms` | 100 | The rooms that one connection can join. |
68
+ | `feedHistory` | 1000 | The feed ops that each room keeps for a join with `since`. |
69
+ | `feedHistoryBytes` | 1 MiB | The size (JSON) of the feed ops that each room keeps. |
70
+ | `roomIdleMs` | 600000 | The time after which the server forgets a room with no members, and its feed history. |
71
+ | `maxConnections` | 10000 | The open connections. A new WebSocket closes with 1013; a new SSE stream gets 503. |
72
+ | `maxQueue` | 1000 | The frames of one connection that wait. Over it, a frame gets `rate-limited`. |
73
+ | `maxMessagesPerSecond` | 200 | The frames per second of one connection. Over it, a frame gets `rate-limited`. |
74
+ | `helloTimeoutMs` | 10000 | The time for the `hello` of a new connection. Then it closes with 1008. |
75
+ | `pingMs` | 30000 | The time between two WebSocket pings. A socket with no pong in that time ends. |
76
+ | `maxBufferedBytes` | 4 MiB | The bytes that wait to go to a client (WebSocket buffer, SSE stream). Over it, the connection closes with 1013. |
77
+
78
+ `dovetailRoutes` checks the size of each WebSocket frame, also when you mount it with your own
79
+ `ws` server: a frame over `maxMessageBytes` closes the socket with 1009. Give your `ws` server
80
+ `maxPayload: maxMessageBytes` too, so that it does not read a large frame into memory.
81
+
82
+ When the history of a room does not have each op after the `since` of a join (the cursor is
83
+ older, the room was forgotten, or the server restarted), the `ack` has `reset: true` and no feed
84
+ follows. The client must load the room again. The first cursor of a room comes from the clock,
85
+ so a cursor of an earlier process never matches a new op.
86
+
87
+ The memory stores (`MemoryDocPersistence`, `MemoryNotificationStore`) are for development and
88
+ tests only. `MemoryDocPersistence({ maxDocBytes })` refuses an update that makes a document larger
89
+ than `maxDocBytes` (default 16 MiB of base64) with a `too-large` error.
90
+
91
+ A user can join only the own per-user room (`user:<id>`), whatever `authorize` answers.
92
+
93
+ | Method | |
94
+ | ------------------------- | -------------------------------------------------------------------------------------------------------------------- |
95
+ | `accept(transport)` | Serves one connection: a `ServerTransport` (a `TextSocket`, or the server end of a `FakeHub`). |
96
+ | `publish(room, document)` | Sends a JSON:API document to the store feed of a room. Gives the cursor. |
97
+ | `remove(room, ref)` | Sends the delete of one record. Gives the cursor. |
98
+ | `notify(userId, payload)` | Makes, stores and sends a notification. `payload`: `verb`, `actor`, `target`, and an optional `id` and `attributes`. |
99
+ | `markRead(userId, id)` | Sets `readAt`, and feeds the change to the per-user room. |
100
+ | `archive(userId, id)` | Sets `archivedAt`, and feeds the change. |
101
+ | `markAllRead(userId)` | Marks each unread notification as read. Gives the count. |
102
+ | `authenticate(token)` | The user of a token, with the hook. Null on a refusal or an error. |
103
+ | `notifications` | The `NotificationStore`. |
104
+ | `close()` | Closes each connection. |
105
+
106
+ ## In a browser
107
+
108
+ `@dovetail-ts/server/browser` has no Node HTTP code and no `ws`. It exports
109
+ `createDovetailServer`, `DovetailServer`, the memory stores, and `inPageServer(options)`: a
110
+ server core and the client transports of the same page. A demo or a story runs a full server
111
+ with it.
112
+
113
+ ```ts
114
+ import { Connection } from '@dovetail-ts/core'
115
+ import { inPageServer } from '@dovetail-ts/server/browser'
116
+
117
+ const page = inPageServer({ authenticate: token => (token === 't1' ? { id: 'u1' } : null) })
118
+ const connection = new Connection({
119
+ transport: page.connect,
120
+ network: page.network,
121
+ getToken: () => 't1',
122
+ })
123
+ connection.connect()
124
+ page.network.set('offline') // the open transports close, and the client sees offline
125
+ ```
126
+
127
+ - `inPageServer(options)` takes the options of `createDovetailServer`. It gives `server`,
128
+ `connect()` (a new `ClientTransport`) and `network`.
129
+ - Each message goes as text through `parseMessage`, in a new task. With `options.clock`, the
130
+ messages wait for the clock, so a test with a `FakeClock` moves them with `tick`.
131
+ - `network` has `isOnline`, `subscribe(listener)` and `set('online' | 'offline')`. Offline, the
132
+ open transports close with the code 1006, and a new one does not open.
133
+ - The tests of Dovetail use `FakeHub` of `@dovetail-ts/test-support`. It has the slow and
134
+ flaky modes too.
135
+
136
+ ## The HTTP routes
137
+
138
+ `dovetailRoutes(server)` gives a Hono app. `listen(server, { port, hostname, path })` serves it
139
+ on Node with `ws`, and gives `{ port, url, close() }`.
140
+
141
+ | Route | |
142
+ | ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
143
+ | `GET /ws` | The WebSocket endpoint. |
144
+ | `GET /sse` | The SSE stream. Its first event is `open` with `{ connection }`. |
145
+ | `POST /sse` | One client frame, with the `Dovetail-Connection` header (the id of the `open` event) and the `Authorization` header of the stream. 202, or 404 (unknown id), 403 (other `Authorization`). The body limit is `maxMessageBytes`. |
146
+ | `GET /notifications` | A page of the notifications of the user of `Authorization: Bearer <token>`. Query: `filter` (`unread`, `all`, `archived`), `before` (the `meta.next` of the page before), `limit`. The answer has `data` and `meta: { next, unread }`. |
147
+ | `POST /notifications/read-all` | Marks each unread notification as read. The answer is `{ meta: { updated } }`. |
148
+
149
+ The rules of the server (the order of messages, the doc sync of an opaque server, the writes
150
+ to a per-user room) are in `docs/protocol.md` and ADR 0012.
@@ -0,0 +1,6 @@
1
+ export { DovetailServer, createDovetailServer } from './server';
2
+ export type { DovetailServerOptions, NotifyPayload, RoomAction, ServerClock, ServerLimits, User, WriteContext, WriteResult, } from './server';
3
+ export { MemoryDocPersistence, MemoryNotificationStore, TooLargeError } from './stores';
4
+ export type { DocPersistence, NotificationChanges, NotificationPage, NotificationQuery, NotificationStore, } from './stores';
5
+ export { inPageServer } from './in-page';
6
+ export type { InPageNetwork, InPageServer } from './in-page';
@@ -0,0 +1,3 @@
1
+ export { DovetailServer, MemoryDocPersistence, MemoryNotificationStore, TooLargeError, createDovetailServer, inPageServer } from './chunk-YXFPYQRL.js';
2
+ //# sourceMappingURL=browser.js.map
3
+ //# sourceMappingURL=browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"browser.js"}