@dovetail-ts/transport-ws 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 ADDED
@@ -0,0 +1,20 @@
1
+ # @dovetail-ts/transport-ws
2
+
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 480744b: First release of Dovetail: real-time and offline sync for Ember.
8
+
9
+ - `@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`.
10
+ - `@dovetail-ts/warp-drive`: the store feed into WarpDrive.
11
+ - `@dovetail-ts/crdt`, `crdt-yjs`, `crdt-loro` and `crdt-automerge`: one `DocEngine` interface with three engines.
12
+ - `@dovetail-ts/tiptap`: co-editing in Tiptap 3 with carets, per-user undo, versions and comment threads.
13
+ - `@dovetail-ts/ember`, `notifications` and `inspector`: presence and status components, the inbox, toasts and activity feeds, and a dev panel.
14
+ - `@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.
15
+ - 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).
16
+
17
+ ### Patch Changes
18
+
19
+ - Updated dependencies [480744b]
20
+ - @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,20 @@
1
- # Temporary Holding Version
1
+ # @dovetail-ts/transport-ws
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 WebSocket transport. It implements the `Transport` interface of `@dovetail-ts/protocol`.
4
+
5
+ Part of [Dovetail](https://joegaudet.com/dovetail/). Not released yet.
6
+
7
+ ```ts
8
+ import { WebSocketTransport } from '@dovetail-ts/transport-ws'
9
+
10
+ const transport = new WebSocketTransport({ url: 'wss://example.com/dovetail/ws' })
11
+ await transport.open()
12
+ transport.onMessage(message => console.log(message))
13
+ transport.send({ type: 'hello', versions: [1], token })
14
+ ```
15
+
16
+ Options: `url`, `protocols`, `WebSocket` (default: the global one) and `limits`. A transport
17
+ opens once and does not reconnect: the Connection makes a new transport. A server frame that
18
+ is not valid closes the transport with the code 1007. A `close()` while the socket opens closes
19
+ the socket and rejects `open()`, so no socket stays open. `sendText(text)` sends a frame as it is,
20
+ for tests and proxies.
@@ -0,0 +1,23 @@
1
+ import type { ClientMessage, ClientTransport, CloseInfo, Limits, ServerMessage } from '@dovetail-ts/protocol';
2
+ export interface WebSocketTransportOptions {
3
+ /** The WebSocket endpoint, as `wss://example.com/dovetail/ws`. */
4
+ url: string | URL;
5
+ protocols?: string | string[];
6
+ /** The WebSocket class. Default: the global `WebSocket` (browsers and Node 22+). */
7
+ WebSocket?: typeof WebSocket;
8
+ /** The limits for the frames of the server. */
9
+ limits?: Partial<Limits>;
10
+ }
11
+ export declare class WebSocketTransport implements ClientTransport {
12
+ #private;
13
+ constructor(options: WebSocketTransportOptions);
14
+ get isOpen(): boolean;
15
+ /** Opens the socket. A transport opens once. */
16
+ open(): Promise<void>;
17
+ send(message: ClientMessage): void;
18
+ /** Sends one frame as it is. For tests and proxies: `send` is the normal way. */
19
+ sendText(text: string): void;
20
+ close(code?: number, reason?: string): void;
21
+ onMessage(listener: (message: ServerMessage) => void): () => void;
22
+ onClose(listener: (info: CloseInfo) => void): () => void;
23
+ }
package/dist/index.js ADDED
@@ -0,0 +1,90 @@
1
+ import { encodeMessage, parseMessage } from '@dovetail-ts/protocol';
2
+
3
+ // src/index.ts
4
+ var WebSocketTransport = class {
5
+ #options;
6
+ #socket;
7
+ #open = false;
8
+ /** Rejects the `open()` in progress, or undefined. */
9
+ #opening;
10
+ #messages = /* @__PURE__ */ new Set();
11
+ #closes = /* @__PURE__ */ new Set();
12
+ constructor(options) {
13
+ this.#options = options;
14
+ }
15
+ get isOpen() {
16
+ return this.#open;
17
+ }
18
+ /** Opens the socket. A transport opens once. */
19
+ open() {
20
+ if (this.#socket) return Promise.reject(new Error("WebSocketTransport: open only once"));
21
+ const Socket = this.#options.WebSocket ?? globalThis.WebSocket;
22
+ const socket = new Socket(this.#options.url, this.#options.protocols);
23
+ this.#socket = socket;
24
+ return new Promise((resolve, reject) => {
25
+ this.#opening = reject;
26
+ socket.addEventListener("open", () => {
27
+ if (this.#opening !== reject) return;
28
+ this.#opening = void 0;
29
+ this.#open = true;
30
+ resolve();
31
+ });
32
+ socket.addEventListener("error", () => {
33
+ if (this.#opening !== reject) return;
34
+ this.#opening = void 0;
35
+ reject(new Error("WebSocketTransport: could not open the socket"));
36
+ });
37
+ socket.addEventListener("message", (event) => this.#receive(event.data));
38
+ socket.addEventListener(
39
+ "close",
40
+ (event) => this.#closed({ code: event.code, reason: event.reason, wasClean: event.wasClean })
41
+ );
42
+ });
43
+ }
44
+ send(message) {
45
+ this.sendText(encodeMessage(message));
46
+ }
47
+ /** Sends one frame as it is. For tests and proxies: `send` is the normal way. */
48
+ sendText(text) {
49
+ if (!this.#open || !this.#socket) throw new Error("WebSocketTransport: the socket is not open");
50
+ this.#socket.send(text);
51
+ }
52
+ close(code = 1e3, reason = "") {
53
+ const opening = this.#opening;
54
+ if (opening) {
55
+ this.#opening = void 0;
56
+ this.#socket?.close();
57
+ opening(new Error("WebSocketTransport: closed while it opened"));
58
+ return;
59
+ }
60
+ if (!this.#open) return;
61
+ this.#socket?.close(code === 1e3 || code >= 3e3 ? code : 4e3 + code % 1e3, reason);
62
+ this.#closed({ code, reason, wasClean: true });
63
+ }
64
+ onMessage(listener) {
65
+ this.#messages.add(listener);
66
+ return () => this.#messages.delete(listener);
67
+ }
68
+ onClose(listener) {
69
+ this.#closes.add(listener);
70
+ return () => this.#closes.delete(listener);
71
+ }
72
+ #receive(data) {
73
+ if (!this.#open) return;
74
+ const result = parseMessage(typeof data === "string" ? data : "", {
75
+ from: "server",
76
+ limits: this.#options.limits
77
+ });
78
+ if (!result.ok) return this.close(1007, result.error.message.slice(0, 120));
79
+ for (const listener of [...this.#messages]) listener(result.message);
80
+ }
81
+ #closed(info) {
82
+ if (!this.#open) return;
83
+ this.#open = false;
84
+ for (const listener of [...this.#closes]) listener(info);
85
+ }
86
+ };
87
+
88
+ export { WebSocketTransport };
89
+ //# sourceMappingURL=index.js.map
90
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAqBO,IAAM,qBAAN,MAAoD;AAAA,EAChD,QAAA;AAAA,EACT,OAAA;AAAA,EACA,KAAA,GAAQ,KAAA;AAAA;AAAA,EAER,QAAA;AAAA,EACA,SAAA,uBAAgB,GAAA,EAAsC;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AU,QAAA,EAAwD;AAChE,IAAA,IAAA,CAAK,SAAA,CAAU,IAAI,QAAQ,CAAA;AAC3B,IAAA,OAAO,MAAM,IAAA,CAAK,SAAA,CAAU,MAAA,CAAO,QAAQ,CAAA;AAAA,EAC7C;AAAA,EAEA,QAAQ,QAAA,EAAiD;AACvD,IAAA,IAAA,CAAK,OAAA,CAAQ,IAAI,QAAQ,CAAA;AACzB,IAAA,OAAO,MAAM,IAAA,CAAK,OAAA,CAAQ,MAAA,CAAO,QAAQ,CAAA;AAAA,EAC3C;AAAA,EAEA,SAAS,IAAA,EAAqB;AAC5B,IAAA,IAAI,CAAC,KAAK,KAAA,EAAO;AACjB,IAAA,MAAM,SAAS,YAAA,CAAa,OAAO,IAAA,KAAS,QAAA,GAAW,OAAO,EAAA,EAAI;AAAA,MAChE,IAAA,EAAM,QAAA;AAAA,MACN,MAAA,EAAQ,KAAK,QAAA,CAAS;AAAA,KACvB,CAAA;AACD,IAAA,IAAI,CAAC,MAAA,CAAO,EAAA,EAAI,OAAO,IAAA,CAAK,KAAA,CAAM,IAAA,EAAM,MAAA,CAAO,KAAA,CAAM,OAAA,CAAQ,KAAA,CAAM,CAAA,EAAG,GAAG,CAAC,CAAA;AAC1E,IAAA,KAAA,MAAW,QAAA,IAAY,CAAC,GAAG,IAAA,CAAK,SAAS,CAAA,EAAG,QAAA,CAAS,OAAO,OAAO,CAAA;AAAA,EACrE;AAAA,EAEA,QAAQ,IAAA,EAAuB;AAC7B,IAAA,IAAI,CAAC,KAAK,KAAA,EAAO;AACjB,IAAA,IAAA,CAAK,KAAA,GAAQ,KAAA;AACb,IAAA,KAAA,MAAW,YAAY,CAAC,GAAG,KAAK,OAAO,CAAA,WAAY,IAAI,CAAA;AAAA,EACzD;AACF","file":"index.js","sourcesContent":["// The WebSocket transport (ADR 0001). It moves protocol frames and does not change them. It\n// does not reconnect: the Connection of @dovetail-ts/core makes a new transport for that.\nimport { encodeMessage, parseMessage } from '@dovetail-ts/protocol'\nimport type {\n ClientMessage,\n ClientTransport,\n CloseInfo,\n Limits,\n ServerMessage,\n} from '@dovetail-ts/protocol'\n\nexport interface WebSocketTransportOptions {\n /** The WebSocket endpoint, as `wss://example.com/dovetail/ws`. */\n url: string | URL\n protocols?: string | string[]\n /** The WebSocket class. Default: the global `WebSocket` (browsers and Node 22+). */\n WebSocket?: typeof WebSocket\n /** The limits for the frames of the server. */\n limits?: Partial<Limits>\n}\n\nexport class WebSocketTransport implements ClientTransport {\n readonly #options: WebSocketTransportOptions\n #socket: WebSocket | undefined\n #open = false\n /** Rejects the `open()` in progress, or undefined. */\n #opening: ((error: Error) => void) | undefined\n #messages = new Set<(message: ServerMessage) => void>()\n #closes = new Set<(info: CloseInfo) => void>()\n\n constructor(options: WebSocketTransportOptions) {\n this.#options = options\n }\n\n get isOpen(): boolean {\n return this.#open\n }\n\n /** Opens the socket. A transport opens once. */\n open(): Promise<void> {\n if (this.#socket) return Promise.reject(new Error('WebSocketTransport: open only once'))\n const Socket = this.#options.WebSocket ?? globalThis.WebSocket\n const socket = new Socket(this.#options.url, this.#options.protocols)\n this.#socket = socket\n return new Promise((resolve, reject) => {\n this.#opening = reject\n socket.addEventListener('open', () => {\n // After a close() in the open, the socket is closed: it does not open the transport.\n if (this.#opening !== reject) return\n this.#opening = undefined\n this.#open = true\n resolve()\n })\n socket.addEventListener('error', () => {\n if (this.#opening !== reject) return\n this.#opening = undefined\n reject(new Error('WebSocketTransport: could not open the socket'))\n })\n socket.addEventListener('message', event => this.#receive(event.data))\n socket.addEventListener('close', event =>\n this.#closed({ code: event.code, reason: event.reason, wasClean: event.wasClean }),\n )\n })\n }\n\n send(message: ClientMessage): void {\n this.sendText(encodeMessage(message))\n }\n\n /** Sends one frame as it is. For tests and proxies: `send` is the normal way. */\n sendText(text: string): void {\n if (!this.#open || !this.#socket) throw new Error('WebSocketTransport: the socket is not open')\n this.#socket.send(text)\n }\n\n close(code = 1000, reason = ''): void {\n const opening = this.#opening\n if (opening) {\n // The socket is still opening. Stop it, so that no socket stays open with no user.\n this.#opening = undefined\n this.#socket?.close()\n opening(new Error('WebSocketTransport: closed while it opened'))\n return\n }\n if (!this.#open) return\n // A browser socket takes only 1000 and 3000-4999, so a protocol code as 1007 goes as 4007.\n this.#socket?.close(code === 1000 || code >= 3000 ? code : 4000 + (code % 1000), reason)\n this.#closed({ code, reason, wasClean: true })\n }\n\n onMessage(listener: (message: ServerMessage) => void): () => void {\n this.#messages.add(listener)\n return () => this.#messages.delete(listener)\n }\n\n onClose(listener: (info: CloseInfo) => void): () => void {\n this.#closes.add(listener)\n return () => this.#closes.delete(listener)\n }\n\n #receive(data: unknown): void {\n if (!this.#open) return\n const result = parseMessage(typeof data === 'string' ? data : '', {\n from: 'server',\n limits: this.#options.limits,\n })\n if (!result.ok) return this.close(1007, result.error.message.slice(0, 120))\n for (const listener of [...this.#messages]) listener(result.message)\n }\n\n #closed(info: CloseInfo): void {\n if (!this.#open) return\n this.#open = false\n for (const listener of [...this.#closes]) listener(info)\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,53 @@
1
1
  {
2
2
  "name": "@dovetail-ts/transport-ws",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0",
4
+ "description": "The WebSocket transport.",
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/transport-ws"
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
+ "./package.json": "./package.json"
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "CHANGELOG.md",
32
+ "README.md",
33
+ "LICENSE"
34
+ ],
35
+ "sideEffects": false,
36
+ "dependencies": {
37
+ "@dovetail-ts/protocol": "1.0.0"
38
+ },
39
+ "devDependencies": {
40
+ "@types/ws": "^8.18.2",
41
+ "tsup": "^8.5.1",
42
+ "ws": "^8.23.1"
43
+ },
44
+ "publishConfig": {
45
+ "access": "public"
46
+ },
47
+ "engines": {
48
+ "node": ">=22.12.0"
49
+ },
50
+ "scripts": {
51
+ "build": "tsc --build --force && tsup"
52
+ }
6
53
  }