@dovetail-ts/partykit 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 +21 -0
- package/LICENSE +21 -0
- package/README.md +91 -2
- package/dist/engine-error.d.ts +6 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +126 -0
- package/dist/index.js.map +1 -0
- package/dist/transport.d.ts +17 -0
- package/dist/yjs.d.ts +4 -0
- package/package.json +54 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# @dovetail-ts/partykit
|
|
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/crdt@1.0.0
|
|
21
|
+
- @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,92 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dovetail-ts/partykit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The PartyKit and PartyServer adapter: doc sync, presence and the store feed.
|
|
4
|
+
|
|
5
|
+
Part of [Dovetail](https://joegaudet.com/dovetail/). Not released yet.
|
|
6
|
+
|
|
7
|
+
| Kind | Supported | How |
|
|
8
|
+
| ----------- | --------- | ------------------------------------------------------- |
|
|
9
|
+
| `StoreFeed` | Yes | `PartySocketTransport`, to a party that runs Dovetail |
|
|
10
|
+
| `DocSync` | Yes | `partykitSync`, to a y-partykit party (Yjs only) |
|
|
11
|
+
| `Presence` | Yes | `partykitSync` (the Yjs awareness), or the Dovetail one |
|
|
12
|
+
|
|
13
|
+
`kinds` has the same list: `['StoreFeed', 'DocSync', 'Presence']`.
|
|
14
|
+
|
|
15
|
+
## Doc sync and presence
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { yjsEngine } from '@dovetail-ts/crdt-yjs'
|
|
19
|
+
import { partykitSync } from '@dovetail-ts/partykit'
|
|
20
|
+
|
|
21
|
+
const handle = yjsEngine().create()
|
|
22
|
+
const provider = partykitSync(handle, { host: 'my-app.me.partykit.dev', room: 'notes-42' })
|
|
23
|
+
|
|
24
|
+
provider.on('sync', (synced: boolean) => console.log(synced))
|
|
25
|
+
provider.destroy()
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`partykitSync(handle, options)` takes the options of `YPartyKitProvider` (`party`, `params`,
|
|
29
|
+
`connect` and the other options), without `awareness`, and the `host` and the `room`. It gives
|
|
30
|
+
back the `YPartyKitProvider`.
|
|
31
|
+
|
|
32
|
+
- The provider uses the Yjs document and the awareness of the handle. Presence thus needs no
|
|
33
|
+
bridge, and the Tiptap carets work.
|
|
34
|
+
- `provider.destroy()` stops the sync. The handle and its awareness stay usable.
|
|
35
|
+
- y-partykit supports Yjs only. A handle of another engine gives an `EngineError` (ADR 0002).
|
|
36
|
+
|
|
37
|
+
## The store feed
|
|
38
|
+
|
|
39
|
+
`PartySocketTransport` is a Dovetail `Transport` over `partysocket`. Give it to the
|
|
40
|
+
`Connection` of `@dovetail-ts/core` as the transport factory:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { PartySocketTransport } from '@dovetail-ts/partykit'
|
|
44
|
+
|
|
45
|
+
const transport = () =>
|
|
46
|
+
new PartySocketTransport({ host: 'my-app.me.partykit.dev', party: 'dovetail', room: 'app' })
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
- The transport opens once and does not reconnect. The `Connection` makes a new transport for
|
|
50
|
+
each attempt, with its own backoff (ADR 0011). A `close()` while the socket opens closes the
|
|
51
|
+
socket and rejects `open()`, so no socket stays open.
|
|
52
|
+
- Each frame of the party goes through `parseMessage`. A frame that is not valid closes the
|
|
53
|
+
transport with the code 1007.
|
|
54
|
+
- Through this connection, the store feed, doc sync and presence all use the Dovetail
|
|
55
|
+
protocol, with any engine.
|
|
56
|
+
|
|
57
|
+
## The party side
|
|
58
|
+
|
|
59
|
+
The party must speak the Dovetail protocol. Use the server core of `@dovetail-ts/server`: a
|
|
60
|
+
PartyServer connection has `send(text)` and `close(code, reason)`, so it is a `TextSink`.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { Server } from 'partyserver'
|
|
64
|
+
import type { Connection, WSMessage } from 'partyserver'
|
|
65
|
+
import { TextSocket, createDovetailServer } from '@dovetail-ts/server'
|
|
66
|
+
|
|
67
|
+
const dovetail = createDovetailServer({/* hooks */})
|
|
68
|
+
const sockets = new Map<string, TextSocket>()
|
|
69
|
+
|
|
70
|
+
export class Dovetail extends Server {
|
|
71
|
+
onConnect(connection: Connection) {
|
|
72
|
+
const socket = new TextSocket(connection)
|
|
73
|
+
sockets.set(connection.id, socket)
|
|
74
|
+
dovetail.accept(socket)
|
|
75
|
+
}
|
|
76
|
+
onMessage(connection: Connection, message: WSMessage) {
|
|
77
|
+
if (typeof message === 'string') sockets.get(connection.id)?.receive(message)
|
|
78
|
+
}
|
|
79
|
+
onClose(connection: Connection, code: number, reason: string, wasClean: boolean) {
|
|
80
|
+
sockets.get(connection.id)?.closed({ code, reason, wasClean })
|
|
81
|
+
sockets.delete(connection.id)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
This package does not ship this helper, because it needs `@dovetail-ts/server` and
|
|
87
|
+
`partyserver` as dependencies.
|
|
88
|
+
|
|
89
|
+
## Tests
|
|
90
|
+
|
|
91
|
+
The unit tests do not open a socket. The live test runs only when `PARTYKIT_HOST` names a host
|
|
92
|
+
with a y-partykit party, for example `127.0.0.1:1999`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import YPartyKitProvider from 'y-partykit/provider';
|
|
2
|
+
import type { DocHandle } from '@dovetail-ts/crdt';
|
|
3
|
+
export { EngineError } from './engine-error';
|
|
4
|
+
export { PartySocketTransport } from './transport';
|
|
5
|
+
export type { PartySocketTransportOptions } from './transport';
|
|
6
|
+
/** The name of this package. */
|
|
7
|
+
export declare const PACKAGE_NAME = "@dovetail-ts/partykit";
|
|
8
|
+
/** A kind of adapter (ADR 0003). */
|
|
9
|
+
export type AdapterKind = 'StoreFeed' | 'DocSync' | 'Presence';
|
|
10
|
+
/** The kinds that this adapter supports. */
|
|
11
|
+
export declare const kinds: readonly ["StoreFeed", "DocSync", "Presence"];
|
|
12
|
+
type ProviderOptions = NonNullable<ConstructorParameters<typeof YPartyKitProvider>[3]>;
|
|
13
|
+
/** The options of `YPartyKitProvider`, without `awareness`, and its host and room. */
|
|
14
|
+
export interface PartykitSyncOptions extends Omit<ProviderOptions, 'awareness'> {
|
|
15
|
+
/** The host of the party, as `my-app.me.partykit.dev` or `127.0.0.1:1999`. */
|
|
16
|
+
host: string;
|
|
17
|
+
room: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Syncs a Yjs document and its awareness with a y-partykit party. Throws an `EngineError` for a
|
|
21
|
+
* document of another engine. `provider.destroy()` stops the sync, and the handle stays usable.
|
|
22
|
+
*/
|
|
23
|
+
export declare function partykitSync(handle: DocHandle, options: PartykitSyncOptions): YPartyKitProvider;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import YPartyKitProvider from 'y-partykit/provider';
|
|
2
|
+
import * as Y from 'yjs';
|
|
3
|
+
import { PartySocket } from 'partysocket';
|
|
4
|
+
import { encodeMessage, parseMessage } from '@dovetail-ts/protocol';
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
|
|
8
|
+
// src/engine-error.ts
|
|
9
|
+
var EngineError = class extends Error {
|
|
10
|
+
constructor(adapter, engine) {
|
|
11
|
+
super(
|
|
12
|
+
`The ${adapter} adapter supports the Yjs engine only. This document uses the "${engine}" engine.`
|
|
13
|
+
);
|
|
14
|
+
this.adapter = adapter;
|
|
15
|
+
this.engine = engine;
|
|
16
|
+
this.name = "EngineError";
|
|
17
|
+
}
|
|
18
|
+
adapter;
|
|
19
|
+
engine;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/yjs.ts
|
|
23
|
+
function requireYjs(handle, adapter) {
|
|
24
|
+
if (isYjs(handle)) return handle;
|
|
25
|
+
throw new EngineError(adapter, handle.engine.name);
|
|
26
|
+
}
|
|
27
|
+
function isYjs(handle) {
|
|
28
|
+
return handle.engine.name === "yjs" && "doc" in handle && handle.doc instanceof Y.Doc && "yAwareness" in handle;
|
|
29
|
+
}
|
|
30
|
+
var PartySocketTransport = class {
|
|
31
|
+
#options;
|
|
32
|
+
#socket;
|
|
33
|
+
#open = false;
|
|
34
|
+
/** Rejects the `open()` in progress, or undefined. */
|
|
35
|
+
#opening;
|
|
36
|
+
#messages = /* @__PURE__ */ new Set();
|
|
37
|
+
#closes = /* @__PURE__ */ new Set();
|
|
38
|
+
constructor(options) {
|
|
39
|
+
this.#options = options;
|
|
40
|
+
}
|
|
41
|
+
get isOpen() {
|
|
42
|
+
return this.#open;
|
|
43
|
+
}
|
|
44
|
+
/** Opens the socket. A transport opens once. */
|
|
45
|
+
open() {
|
|
46
|
+
if (this.#socket) return Promise.reject(new Error("PartySocketTransport: open only once"));
|
|
47
|
+
const { limits: _limits, ...options } = this.#options;
|
|
48
|
+
const socket = new PartySocket({
|
|
49
|
+
...options,
|
|
50
|
+
maxRetries: 1,
|
|
51
|
+
shouldReconnectOnClose: () => false
|
|
52
|
+
});
|
|
53
|
+
this.#socket = socket;
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
this.#opening = reject;
|
|
56
|
+
socket.addEventListener("open", () => {
|
|
57
|
+
if (this.#opening !== reject) return;
|
|
58
|
+
this.#opening = void 0;
|
|
59
|
+
this.#open = true;
|
|
60
|
+
resolve();
|
|
61
|
+
});
|
|
62
|
+
socket.addEventListener("error", () => {
|
|
63
|
+
if (this.#opening !== reject) return;
|
|
64
|
+
this.#opening = void 0;
|
|
65
|
+
reject(new Error("PartySocketTransport: could not open the socket"));
|
|
66
|
+
});
|
|
67
|
+
socket.addEventListener("message", (event) => this.#receive(event.data));
|
|
68
|
+
socket.addEventListener(
|
|
69
|
+
"close",
|
|
70
|
+
(event) => this.#closed({ code: event.code, reason: event.reason, wasClean: event.wasClean })
|
|
71
|
+
);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
send(message) {
|
|
75
|
+
if (!this.#open || !this.#socket)
|
|
76
|
+
throw new Error("PartySocketTransport: the socket is not open");
|
|
77
|
+
this.#socket.send(encodeMessage(message));
|
|
78
|
+
}
|
|
79
|
+
close(code = 1e3, reason = "") {
|
|
80
|
+
const opening = this.#opening;
|
|
81
|
+
if (opening) {
|
|
82
|
+
this.#opening = void 0;
|
|
83
|
+
this.#socket?.close();
|
|
84
|
+
opening(new Error("PartySocketTransport: closed while it opened"));
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (!this.#open) return;
|
|
88
|
+
this.#closed({ code, reason, wasClean: true });
|
|
89
|
+
this.#socket?.close(code === 1e3 || code >= 3e3 ? code : 4e3 + code % 1e3, reason);
|
|
90
|
+
}
|
|
91
|
+
onMessage(listener) {
|
|
92
|
+
this.#messages.add(listener);
|
|
93
|
+
return () => this.#messages.delete(listener);
|
|
94
|
+
}
|
|
95
|
+
onClose(listener) {
|
|
96
|
+
this.#closes.add(listener);
|
|
97
|
+
return () => this.#closes.delete(listener);
|
|
98
|
+
}
|
|
99
|
+
#receive(data) {
|
|
100
|
+
if (!this.#open) return;
|
|
101
|
+
const result = parseMessage(typeof data === "string" ? data : "", {
|
|
102
|
+
from: "server",
|
|
103
|
+
limits: this.#options.limits
|
|
104
|
+
});
|
|
105
|
+
if (!result.ok) return this.close(1007, result.error.message.slice(0, 120));
|
|
106
|
+
for (const listener of [...this.#messages]) listener(result.message);
|
|
107
|
+
}
|
|
108
|
+
#closed(info) {
|
|
109
|
+
if (!this.#open) return;
|
|
110
|
+
this.#open = false;
|
|
111
|
+
for (const listener of [...this.#closes]) listener(info);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// src/index.ts
|
|
116
|
+
var PACKAGE_NAME = "@dovetail-ts/partykit";
|
|
117
|
+
var kinds = ["StoreFeed", "DocSync", "Presence"];
|
|
118
|
+
function partykitSync(handle, options) {
|
|
119
|
+
const yjs = requireYjs(handle, "PartyKit");
|
|
120
|
+
const { host, room, ...rest } = options;
|
|
121
|
+
return new YPartyKitProvider(host, room, yjs.doc, { ...rest, awareness: yjs.yAwareness });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export { EngineError, PACKAGE_NAME, PartySocketTransport, kinds, partykitSync };
|
|
125
|
+
//# sourceMappingURL=index.js.map
|
|
126
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/engine-error.ts","../src/yjs.ts","../src/transport.ts","../src/index.ts"],"names":[],"mappings":";;;;;;;;AAGO,IAAM,WAAA,GAAN,cAA0B,KAAA,CAAM;AAAA,EACrC,WAAA,CACW,SACA,MAAA,EACT;AACA,IAAA,KAAA;AAAA,MACE,CAAA,IAAA,EAAO,OAAO,CAAA,+DAAA,EACe,MAAM,CAAA,SAAA;AAAA,KACrC;AANS,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA;AACA,IAAA,IAAA,CAAA,MA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O,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;;;ACxGO,IAAM,YAAA,GAAe;AAMrB,IAAM,KAAA,GAAQ,CAAC,WAAA,EAAa,SAAA,EAAW,UAAU;AAejD,SAAS,YAAA,CAAa,QAAmB,OAAA,EAAiD;AAC/F,EAAA,MAAM,GAAA,GAAM,UAAA,CAAW,MAAA,EAAQ,UAAU,CAAA;AACzC,EAAA,MAAM,EAAE,IAAA,EAAM,IAAA,EAAM,GAAG,MAAK,GAAI,OAAA;AAChC,EAAA,OAAO,IAAI,iBAAA,CAAkB,IAAA,EAAM,IAAA,EAAM,GAAA,CAAI,GAAA,EAAK,EAAE,GAAG,IAAA,EAAM,SAAA,EAAW,GAAA,CAAI,UAAA,EAAY,CAAA;AAC1F","file":"index.js","sourcesContent":["// The error of an adapter that supports the Yjs engine only (ADR 0002).\n\n/** The error for a document of an engine that the adapter does not support. */\nexport class EngineError extends Error {\n constructor(\n readonly adapter: string,\n readonly engine: string,\n ) {\n super(\n `The ${adapter} adapter supports the Yjs engine only. ` +\n `This document uses the \"${engine}\" engine.`,\n )\n this.name = 'EngineError'\n }\n}\n","// The Yjs check of the adapters that support the Yjs engine only (ADR 0002).\nimport * as Y from 'yjs'\nimport type { DocHandle } from '@dovetail-ts/crdt'\nimport type { YjsDocHandle } from '@dovetail-ts/crdt-yjs'\nimport { EngineError } from './engine-error'\n\n/** Gives the handle as a Yjs handle, or throws an `EngineError`. */\nexport function requireYjs(handle: DocHandle, adapter: string): YjsDocHandle {\n if (isYjs(handle)) return handle\n throw new EngineError(adapter, handle.engine.name)\n}\n\nfunction isYjs(handle: DocHandle): handle is YjsDocHandle {\n return (\n handle.engine.name === 'yjs' &&\n 'doc' in handle &&\n handle.doc instanceof Y.Doc &&\n 'yAwareness' in handle\n )\n}\n","// A Dovetail `Transport` over PartySocket. It does not reconnect: the `Connection` of\n// `@dovetail-ts/core` makes a new transport for each attempt (ADR 0011).\nimport { PartySocket } from 'partysocket'\nimport type { PartySocketOptions } from 'partysocket'\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 type PartySocketTransportOptions = Omit<\n PartySocketOptions,\n 'maxRetries' | 'shouldReconnectOnClose' | 'startClosed'\n> & {\n /** The limits for the frames of the party. */\n limits?: Partial<Limits>\n}\n\nexport class PartySocketTransport implements ClientTransport {\n readonly #options: PartySocketTransportOptions\n #socket: PartySocket | 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: PartySocketTransportOptions) {\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('PartySocketTransport: open only once'))\n const { limits: _limits, ...options } = this.#options\n // One attempt only. `maxRetries` counts the first attempt too.\n const socket = new PartySocket({\n ...options,\n maxRetries: 1,\n shouldReconnectOnClose: () => false,\n })\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('PartySocketTransport: 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 if (!this.#open || !this.#socket)\n throw new Error('PartySocketTransport: the socket is not open')\n this.#socket.send(encodeMessage(message))\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('PartySocketTransport: closed while it opened'))\n return\n }\n if (!this.#open) return\n // PartySocket sends its close event at once, so the listeners get the protocol code first.\n this.#closed({ code, reason, wasClean: true })\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 }\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","// The PartyKit and PartyServer adapter (ADR 0003, ADR 0017). Doc sync and presence go through\n// y-partykit, with the Yjs document and the awareness of the handle. The store feed goes\n// through a party that speaks the Dovetail protocol: `PartySocketTransport` is a `Transport`\n// for the `Connection` of `@dovetail-ts/core`.\nimport YPartyKitProvider from 'y-partykit/provider'\nimport type { DocHandle } from '@dovetail-ts/crdt'\nimport { requireYjs } from './yjs'\n\nexport { EngineError } from './engine-error'\nexport { PartySocketTransport } from './transport'\nexport type { PartySocketTransportOptions } from './transport'\n\n/** The name of this package. */\nexport const PACKAGE_NAME = '@dovetail-ts/partykit'\n\n/** A kind of adapter (ADR 0003). */\nexport type AdapterKind = 'StoreFeed' | 'DocSync' | 'Presence'\n\n/** The kinds that this adapter supports. */\nexport const kinds = ['StoreFeed', 'DocSync', 'Presence'] as const satisfies readonly AdapterKind[]\n\ntype ProviderOptions = NonNullable<ConstructorParameters<typeof YPartyKitProvider>[3]>\n\n/** The options of `YPartyKitProvider`, without `awareness`, and its host and room. */\nexport interface PartykitSyncOptions extends Omit<ProviderOptions, 'awareness'> {\n /** The host of the party, as `my-app.me.partykit.dev` or `127.0.0.1:1999`. */\n host: string\n room: string\n}\n\n/**\n * Syncs a Yjs document and its awareness with a y-partykit party. Throws an `EngineError` for a\n * document of another engine. `provider.destroy()` stops the sync, and the handle stays usable.\n */\nexport function partykitSync(handle: DocHandle, options: PartykitSyncOptions): YPartyKitProvider {\n const yjs = requireYjs(handle, 'PartyKit')\n const { host, room, ...rest } = options\n return new YPartyKitProvider(host, room, yjs.doc, { ...rest, awareness: yjs.yAwareness })\n}\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { PartySocketOptions } from 'partysocket';
|
|
2
|
+
import type { ClientMessage, ClientTransport, CloseInfo, Limits, ServerMessage } from '@dovetail-ts/protocol';
|
|
3
|
+
export type PartySocketTransportOptions = Omit<PartySocketOptions, 'maxRetries' | 'shouldReconnectOnClose' | 'startClosed'> & {
|
|
4
|
+
/** The limits for the frames of the party. */
|
|
5
|
+
limits?: Partial<Limits>;
|
|
6
|
+
};
|
|
7
|
+
export declare class PartySocketTransport implements ClientTransport {
|
|
8
|
+
#private;
|
|
9
|
+
constructor(options: PartySocketTransportOptions);
|
|
10
|
+
get isOpen(): boolean;
|
|
11
|
+
/** Opens the socket. A transport opens once. */
|
|
12
|
+
open(): Promise<void>;
|
|
13
|
+
send(message: ClientMessage): void;
|
|
14
|
+
close(code?: number, reason?: string): void;
|
|
15
|
+
onMessage(listener: (message: ServerMessage) => void): () => void;
|
|
16
|
+
onClose(listener: (info: CloseInfo) => void): () => void;
|
|
17
|
+
}
|
package/dist/yjs.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dovetail-ts/partykit",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "The PartyKit and PartyServer adapter: doc sync, presence and the store feed.",
|
|
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/partykit"
|
|
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/crdt": "1.0.0",
|
|
38
|
+
"@dovetail-ts/protocol": "1.0.0",
|
|
39
|
+
"partysocket": "^1.3.0",
|
|
40
|
+
"y-partykit": "^0.0.33",
|
|
41
|
+
"yjs": "^13.6.33"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@dovetail-ts/crdt-loro": "1.0.0",
|
|
45
|
+
"@dovetail-ts/crdt-yjs": "1.0.0",
|
|
46
|
+
"tsup": "^8.5.1"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=22.12.0"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "tsc --build --force && tsup"
|
|
56
|
+
}
|
|
6
57
|
}
|