@dovetail-ts/server 0.0.0-stage → 1.0.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.
- package/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +149 -2
- package/dist/browser.d.ts +6 -0
- package/dist/browser.js +3 -0
- package/dist/browser.js.map +1 -0
- package/dist/chunk-YXFPYQRL.js +685 -0
- package/dist/chunk-YXFPYQRL.js.map +1 -0
- package/dist/http.d.ts +29 -0
- package/dist/in-page.d.ts +22 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +213 -0
- package/dist/index.js.map +1 -0
- package/dist/server.d.ts +155 -0
- package/dist/socket.d.ts +28 -0
- package/dist/stores.d.ts +66 -0
- package/package.json +56 -3
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { JsonApiDocument, JsonApiError, JsonObject, Limits, NotificationResource, ResourceIdentifier, ServerTransport, StoreOp } from '@dovetail-ts/protocol';
|
|
2
|
+
import type { DocPersistence, NotificationStore } from './stores';
|
|
3
|
+
/** A user, as the host `authenticate` hook gives it. */
|
|
4
|
+
export interface User {
|
|
5
|
+
id: string;
|
|
6
|
+
}
|
|
7
|
+
/** The actions that the host `authorize` hook decides (ADR 0007). */
|
|
8
|
+
export type RoomAction = 'join' | 'write';
|
|
9
|
+
/** The answer of `handleWrite`: an ack (with the server document), or a reject with a 4xx status. */
|
|
10
|
+
export type WriteResult = {
|
|
11
|
+
ok: true;
|
|
12
|
+
document?: JsonApiDocument;
|
|
13
|
+
} | {
|
|
14
|
+
ok: false;
|
|
15
|
+
status: number;
|
|
16
|
+
errors: JsonApiError[];
|
|
17
|
+
};
|
|
18
|
+
export interface WriteContext<U extends User = User> {
|
|
19
|
+
user: U | null;
|
|
20
|
+
session: string;
|
|
21
|
+
/** The id of the write. A resend after a reconnect has the same id, so a host can skip it. */
|
|
22
|
+
id: string;
|
|
23
|
+
room: string;
|
|
24
|
+
op: StoreOp;
|
|
25
|
+
}
|
|
26
|
+
/** Where a doc update goes, for `validateUpdate`. */
|
|
27
|
+
export interface UpdateContext<U extends User = User> {
|
|
28
|
+
user: U | null;
|
|
29
|
+
session: string;
|
|
30
|
+
room: string;
|
|
31
|
+
doc: string;
|
|
32
|
+
}
|
|
33
|
+
export interface ServerLimits extends Limits {
|
|
34
|
+
/** The rooms that one connection can join. Default 100. */
|
|
35
|
+
maxRooms: number;
|
|
36
|
+
/** The feed ops that each room keeps for a join with `since`. Default 1000. */
|
|
37
|
+
feedHistory: number;
|
|
38
|
+
/** The bytes (of JSON) of the feed ops that each room keeps. Default 1 MiB. */
|
|
39
|
+
feedHistoryBytes: number;
|
|
40
|
+
/** The time in ms after which the server forgets a room with no members. Default 600000. */
|
|
41
|
+
roomIdleMs: number;
|
|
42
|
+
/** The open connections. A new one over it closes with 1013. Default 10000. */
|
|
43
|
+
maxConnections: number;
|
|
44
|
+
/** The frames of one connection that wait for the frame in work. Default 1000. */
|
|
45
|
+
maxQueue: number;
|
|
46
|
+
/** The frames per second of one connection (a burst of the same size is allowed). Default 200. */
|
|
47
|
+
maxMessagesPerSecond: number;
|
|
48
|
+
/** The time in ms for the `hello` of a new connection. Default 10000. */
|
|
49
|
+
helloTimeoutMs: number;
|
|
50
|
+
/** The time in ms between two WebSocket pings. A socket with no pong closes. Default 30000. */
|
|
51
|
+
pingMs: number;
|
|
52
|
+
/** The bytes that wait in the send buffer of a connection. Over it, it closes with 1013. Default 4 MiB. */
|
|
53
|
+
maxBufferedBytes: number;
|
|
54
|
+
}
|
|
55
|
+
/** The timers of the server. A FakeClock of `@dovetail-ts/test-support` fits. */
|
|
56
|
+
export interface ServerClock {
|
|
57
|
+
now(): number;
|
|
58
|
+
setTimeout(run: () => void, ms: number): unknown;
|
|
59
|
+
clearTimeout(id: unknown): void;
|
|
60
|
+
}
|
|
61
|
+
export interface DovetailServerOptions<U extends User = User> {
|
|
62
|
+
/**
|
|
63
|
+
* Gives the user of a `hello` token, or null to refuse it. Without it, each user is null. The
|
|
64
|
+
* server refuses a missing or blank token before it calls this hook, unless `allowAnonymous`.
|
|
65
|
+
*/
|
|
66
|
+
authenticate?(token: string | undefined): U | null | Promise<U | null>;
|
|
67
|
+
/** Gives a missing or blank token to `authenticate` as `undefined`. Default false. */
|
|
68
|
+
allowAnonymous?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Decides each join and each write. Only `true` allows. Without it, each one is allowed, and
|
|
71
|
+
* the server warns at the start when it has `authenticate`.
|
|
72
|
+
*/
|
|
73
|
+
authorize?(user: U | null, room: string, action: RoomAction): boolean | Promise<boolean>;
|
|
74
|
+
/**
|
|
75
|
+
* Checks an awareness state before the relay: gives the state to relay (the same or a new one),
|
|
76
|
+
* or false to refuse it with `forbidden`. Without it, the server sets `state.user.id` to the
|
|
77
|
+
* id of the user of the session, when the session has a user and `state.user` is an object.
|
|
78
|
+
*/
|
|
79
|
+
authorizeAwareness?(user: U | null, room: string, state: JsonObject): JsonObject | false | Promise<JsonObject | false>;
|
|
80
|
+
/** Gets each warning of the server. Default: `console.warn`. */
|
|
81
|
+
onWarning?(message: string): void;
|
|
82
|
+
/** Applies a write. Without it, the server acks each write and feeds the op as it is. */
|
|
83
|
+
handleWrite?(write: WriteContext<U>): WriteResult | Promise<WriteResult>;
|
|
84
|
+
/** Where the doc updates stay. Default: memory. */
|
|
85
|
+
docs?: DocPersistence;
|
|
86
|
+
/**
|
|
87
|
+
* Merges doc updates (for example `Y.mergeUpdates`), so a doc sync sends one step 2. When it
|
|
88
|
+
* throws, the error goes to `onError` and the sync sends the stored updates one by one.
|
|
89
|
+
*/
|
|
90
|
+
mergeUpdates?(updates: Uint8Array[]): Uint8Array;
|
|
91
|
+
/**
|
|
92
|
+
* Checks a doc update before the server stores and relays it. False (or an exception) refuses
|
|
93
|
+
* it with an `invalid-message` error. Without it, the server stores each update.
|
|
94
|
+
*/
|
|
95
|
+
validateUpdate?(update: Uint8Array, context: UpdateContext<U>): boolean | Promise<boolean>;
|
|
96
|
+
/** Where the notifications stay. Default: memory. */
|
|
97
|
+
notifications?: NotificationStore;
|
|
98
|
+
limits?: Partial<ServerLimits>;
|
|
99
|
+
/** The time in ms after which an awareness state with no update goes. Default 30000. */
|
|
100
|
+
awarenessTimeout?: number;
|
|
101
|
+
clock?: ServerClock;
|
|
102
|
+
/** Gets each exception of a hook. The client gets only a safe `internal` error. */
|
|
103
|
+
onError?(error: unknown): void;
|
|
104
|
+
/** Called once when a connection closes, with the rooms that the session had joined. */
|
|
105
|
+
onSessionEnd?(session: SessionInfo<U>): void | Promise<void>;
|
|
106
|
+
}
|
|
107
|
+
/** A session that ended: its id (as in the `welcome`), its user and the rooms it had joined. */
|
|
108
|
+
export interface SessionInfo<U extends User = User> {
|
|
109
|
+
id: string;
|
|
110
|
+
user: U | null;
|
|
111
|
+
rooms: string[];
|
|
112
|
+
}
|
|
113
|
+
/** The fields of a new notification. The server sets the times. */
|
|
114
|
+
export interface NotifyPayload {
|
|
115
|
+
verb: string;
|
|
116
|
+
actor: ResourceIdentifier;
|
|
117
|
+
target: ResourceIdentifier;
|
|
118
|
+
/** Default: a random UUID. */
|
|
119
|
+
id?: string;
|
|
120
|
+
/** More attributes, as a `body` or a `url`. */
|
|
121
|
+
attributes?: JsonObject;
|
|
122
|
+
}
|
|
123
|
+
export declare class DovetailServer<U extends User = User> {
|
|
124
|
+
#private;
|
|
125
|
+
readonly limits: ServerLimits;
|
|
126
|
+
readonly notifications: NotificationStore;
|
|
127
|
+
constructor(options?: DovetailServerOptions<U>);
|
|
128
|
+
/** The open connections. */
|
|
129
|
+
get connections(): number;
|
|
130
|
+
/** The rooms that the server keeps in memory. */
|
|
131
|
+
get rooms(): number;
|
|
132
|
+
/** Serves one connection. The transport must parse each frame with parseMessage. */
|
|
133
|
+
accept(transport: ServerTransport): void;
|
|
134
|
+
/**
|
|
135
|
+
* The user of a token, with the host `authenticate` hook. Null without the hook, for a token
|
|
136
|
+
* that is not valid, or when the hook throws.
|
|
137
|
+
*/
|
|
138
|
+
authenticate(token: string | undefined): Promise<U | null>;
|
|
139
|
+
/** Sends a document to the store feed of a room. Gives the cursor after it. */
|
|
140
|
+
publish(room: string, document: JsonApiDocument): string;
|
|
141
|
+
/** Sends the delete of one record to the store feed of a room. Gives the cursor after it. */
|
|
142
|
+
remove(room: string, ref: ResourceIdentifier): string;
|
|
143
|
+
/** Makes a notification for a user, stores it and sends it to the per-user room (ADR 0009). */
|
|
144
|
+
notify(userId: string, payload: NotifyPayload): Promise<NotificationResource>;
|
|
145
|
+
/** Marks one notification as read. Null when the user has no such notification. */
|
|
146
|
+
markRead(userId: string, id: string): Promise<NotificationResource | null>;
|
|
147
|
+
/** Archives one notification. Null when the user has no such notification. */
|
|
148
|
+
archive(userId: string, id: string): Promise<NotificationResource | null>;
|
|
149
|
+
/** Marks each unread notification of the user as read. Gives the count that changed. */
|
|
150
|
+
markAllRead(userId: string): Promise<number>;
|
|
151
|
+
/** Closes each connection. */
|
|
152
|
+
close(): void;
|
|
153
|
+
}
|
|
154
|
+
/** Makes a Dovetail server. Give each connection to `accept`, or use `dovetailRoutes`. */
|
|
155
|
+
export declare function createDovetailServer<U extends User = User>(options?: DovetailServerOptions<U>): DovetailServer<U>;
|
package/dist/socket.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ClientMessage, CloseInfo, Limits, ServerMessage, ServerTransport } from '@dovetail-ts/protocol';
|
|
2
|
+
/** The wire under a TextSocket. */
|
|
3
|
+
export interface TextSink {
|
|
4
|
+
send(text: string): void;
|
|
5
|
+
close(code: number, reason: string): void;
|
|
6
|
+
/** The bytes that wait to go. Without it, the socket does not check the backpressure. */
|
|
7
|
+
buffered?(): number;
|
|
8
|
+
}
|
|
9
|
+
export interface TextSocketLimits extends Limits {
|
|
10
|
+
/** The bytes that can wait in the sink. Over it, the socket closes with 1013. */
|
|
11
|
+
maxBufferedBytes: number;
|
|
12
|
+
}
|
|
13
|
+
export declare class TextSocket implements ServerTransport {
|
|
14
|
+
#private;
|
|
15
|
+
readonly sink: TextSink;
|
|
16
|
+
readonly limits: Partial<TextSocketLimits>;
|
|
17
|
+
constructor(sink: TextSink, limits?: Partial<TextSocketLimits>);
|
|
18
|
+
get isOpen(): boolean;
|
|
19
|
+
open(): Promise<void>;
|
|
20
|
+
send(message: ServerMessage): void;
|
|
21
|
+
close(code?: number, reason?: string): void;
|
|
22
|
+
onMessage(listener: (message: ClientMessage) => void): () => void;
|
|
23
|
+
onClose(listener: (info: CloseInfo) => void): () => void;
|
|
24
|
+
/** Gives one frame from the wire. */
|
|
25
|
+
receive(text: string): void;
|
|
26
|
+
/** Tells the socket that the wire closed. */
|
|
27
|
+
closed(info: CloseInfo): void;
|
|
28
|
+
}
|
package/dist/stores.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { NotificationResource } from '@dovetail-ts/protocol';
|
|
2
|
+
/** The stored CRDT updates of each document. The bytes are base64, and the server does not read them. */
|
|
3
|
+
export interface DocPersistence {
|
|
4
|
+
/** Adds an update at the end of the updates of the document. */
|
|
5
|
+
append(room: string, doc: string, update: string): Promise<void> | void;
|
|
6
|
+
/** The updates of the document, in the order of `append`. */
|
|
7
|
+
load(room: string, doc: string): Promise<string[]> | string[];
|
|
8
|
+
}
|
|
9
|
+
/** A store refuses a change over its size limit. The server answers it with `too-large`. */
|
|
10
|
+
export declare class TooLargeError extends Error {
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A DocPersistence in memory, for development and tests only. The updates go when the process
|
|
14
|
+
* stops, and the documents stay until then. `maxDocBytes` (default 16 MiB of base64) limits each
|
|
15
|
+
* document.
|
|
16
|
+
*/
|
|
17
|
+
export declare class MemoryDocPersistence implements DocPersistence {
|
|
18
|
+
#private;
|
|
19
|
+
readonly maxDocBytes: number;
|
|
20
|
+
constructor(options?: {
|
|
21
|
+
maxDocBytes?: number;
|
|
22
|
+
});
|
|
23
|
+
append(room: string, doc: string, update: string): void;
|
|
24
|
+
load(room: string, doc: string): string[];
|
|
25
|
+
}
|
|
26
|
+
/** The fields of a notification that its user can change. */
|
|
27
|
+
export type NotificationChanges = Partial<Pick<NotificationResource['attributes'], 'readAt' | 'seenAt' | 'archivedAt'>>;
|
|
28
|
+
export interface NotificationQuery {
|
|
29
|
+
/** `all` (the default) is each notification that is not archived. `unread` has no `readAt`. */
|
|
30
|
+
filter?: 'unread' | 'all' | 'archived';
|
|
31
|
+
/** The `next` of the page before. */
|
|
32
|
+
before?: string;
|
|
33
|
+
/** The size of a page. Default 20, at most 100. */
|
|
34
|
+
limit?: number;
|
|
35
|
+
}
|
|
36
|
+
/** One page of notifications, the newest first. `next` gets the older page, or is null. */
|
|
37
|
+
export interface NotificationPage {
|
|
38
|
+
data: NotificationResource[];
|
|
39
|
+
next: string | null;
|
|
40
|
+
}
|
|
41
|
+
/** Where the notifications of each user stay (ADR 0009). */
|
|
42
|
+
export interface NotificationStore {
|
|
43
|
+
add(userId: string, notification: NotificationResource): Promise<void>;
|
|
44
|
+
get(userId: string, id: string): Promise<NotificationResource | null>;
|
|
45
|
+
list(userId: string, query: NotificationQuery): Promise<NotificationPage>;
|
|
46
|
+
/** Changes the read, seen or archived time. Null when the user has no such notification. */
|
|
47
|
+
update(userId: string, id: string, changes: NotificationChanges): Promise<NotificationResource | null>;
|
|
48
|
+
/** Sets `readAt` on each unread notification. Gives the notifications that changed. */
|
|
49
|
+
markAllRead(userId: string, at: string): Promise<NotificationResource[]>;
|
|
50
|
+
unreadCount(userId: string): Promise<number>;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A NotificationStore in memory, for development and tests only. The notifications go when the
|
|
54
|
+
* process stops.
|
|
55
|
+
*/
|
|
56
|
+
export declare class MemoryNotificationStore implements NotificationStore {
|
|
57
|
+
#private;
|
|
58
|
+
/** The users that have notifications. */
|
|
59
|
+
get users(): number;
|
|
60
|
+
add(userId: string, notification: NotificationResource): Promise<void>;
|
|
61
|
+
get(userId: string, id: string): Promise<NotificationResource | null>;
|
|
62
|
+
list(userId: string, query: NotificationQuery): Promise<NotificationPage>;
|
|
63
|
+
update(userId: string, id: string, changes: NotificationChanges): Promise<NotificationResource | null>;
|
|
64
|
+
markAllRead(userId: string, at: string): Promise<NotificationResource[]>;
|
|
65
|
+
unreadCount(userId: string): Promise<number>;
|
|
66
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dovetail-ts/server",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "The Node server: rooms, relay, presence, the store feed and the auth hooks. On Hono and ws.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Joe Gaudet <joe@joegaudet.com>",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/joegaudet/dovetail.git",
|
|
10
|
+
"directory": "packages/server"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://joegaudet.com/dovetail/",
|
|
13
|
+
"bugs": "https://github.com/joegaudet/dovetail/issues",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"realtime",
|
|
16
|
+
"offline",
|
|
17
|
+
"sync",
|
|
18
|
+
"crdt"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./browser": {
|
|
28
|
+
"types": "./dist/browser.d.ts",
|
|
29
|
+
"default": "./dist/browser.js"
|
|
30
|
+
},
|
|
31
|
+
"./package.json": "./package.json"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist",
|
|
35
|
+
"CHANGELOG.md",
|
|
36
|
+
"README.md",
|
|
37
|
+
"LICENSE"
|
|
38
|
+
],
|
|
39
|
+
"sideEffects": false,
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@dovetail-ts/protocol": "1.0.0",
|
|
42
|
+
"@hono/node-server": "^2.1.11",
|
|
43
|
+
"hono": "^4.13.23",
|
|
44
|
+
"ws": "^8.23.1"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/ws": "^8.18.2",
|
|
48
|
+
"tsup": "^8.5.1"
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=22.12.0"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "tsc --build --force && tsup"
|
|
58
|
+
}
|
|
6
59
|
}
|