@automerge/automerge-repo-network-websocket 1.1.0 → 1.1.2
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/README.md
CHANGED
|
@@ -17,9 +17,7 @@ is the server.
|
|
|
17
17
|
|
|
18
18
|
### Overview
|
|
19
19
|
|
|
20
|
-
The websocket wire protocol consists of a handshake where each peer tells the
|
|
21
|
-
other what their peer ID is followed by the sync loop where each peer can send
|
|
22
|
-
the other sync messages and ephemeral messages.
|
|
20
|
+
The websocket wire protocol consists of a handshake where each peer tells the other what their peer ID - and some other metadata - is followed by the sync loop where each peer can send the other sync messages and ephemeral messages.
|
|
23
21
|
|
|
24
22
|
### Handshake
|
|
25
23
|
|
|
@@ -54,6 +52,10 @@ Handshake is the following steps:
|
|
|
54
52
|
peer sends an [error](#error) message and terminates the connection
|
|
55
53
|
* it begins the sync phase
|
|
56
54
|
|
|
55
|
+
#### Peer IDs and storage IDs
|
|
56
|
+
|
|
57
|
+
The peer ID is an ephemeral ID which is assumed to only live for the lifetime of the process which advertises the given ID (e.g. a browser tab). Peers may optionally advertise a storage ID in the `join` and `peer` messages, this is an ID which is assumed to be tied to a persistent storage of some kind (e.g. an IndexedDB in a browser). Many peer IDs can advertise the same storage ID (as in the case of many browser tabs). The use of a storage ID allows other peers to know whether to save and reload sync states for a given peer (if the peer advertises a storage ID, then save and reload the sync state attached to that storage ID).
|
|
58
|
+
|
|
57
59
|
|
|
58
60
|
### Sync Phase
|
|
59
61
|
|
|
@@ -65,6 +67,22 @@ and receiving is emitting the [corresponding
|
|
|
65
67
|
event](https://automerge.org/automerge-repo/interfaces/_automerge_automerge_repo.NetworkAdapterEvents.html)
|
|
66
68
|
from the `NetworkAdapter` on receipt.
|
|
67
69
|
|
|
70
|
+
#### Remote heads gossiping
|
|
71
|
+
|
|
72
|
+
In some cases peers wish to know about the state of peers who are separated from them by several intermediate peers. For example, a tab running a text editor may wish to show whether the contents of the editor are up to date with respect to a tab running in a browser on another users device. This is achieved by gossiping remote heads across intermediate nodes. The logic for this is the following:
|
|
73
|
+
|
|
74
|
+
* For a given connection each peer maintains a list of the storage IDs the remote peer is interested in (note this is storage IDs, not peer IDs)
|
|
75
|
+
* Any peer can send a [`remote-subscription-changed`](#remote-subscription-changed) message to change the set of storage IDs they want the recipient to watch on the sender's behalf
|
|
76
|
+
* Any time a peer receives a sync message it checks:
|
|
77
|
+
* Is the sync message from a peer with a storage ID which some other remote peer has registered interest in
|
|
78
|
+
* Is the remote peer permitted access to the document which the message pertains to (i.e. either the `sharePolicy` return `true` or the local peer is already syncing the document with the remote)
|
|
79
|
+
* The local peer sends a [`remote-heads-changed`](#remote-heads-changed) message to each remote peer who passes these checks
|
|
80
|
+
* Additionally, whenever the local peer receives a `remote-heads-changed` message it performs the same checks and additionally checks if the timestamp on the `remote-heads-changed` message is greater than the last timestamp for the same storage ID/document combination and if so it forwards it.
|
|
81
|
+
|
|
82
|
+
In the `browser <-> sync server <-> browser` text editor example above each browser tab would send a `remote-subscription-changed` message to the sync server adding the other browsers storage ID (presumably communicated out of band) to their subscriptions with the sync server. The sync server will then send `remote-heads-changed` messages to each tab when their heads change.
|
|
83
|
+
|
|
84
|
+
In a more elaborate example such as `browser <-> sync server <-> sync server <-> browser` the intermediate sync servers could be configured to have their `sharePolicy` return `true` for every document when syncing with each other so that `remote-heads-changed` messages are forwarded between them unconditionally, allowing the browser tabs to still learn of each others heads.
|
|
85
|
+
|
|
68
86
|
### Message Types
|
|
69
87
|
|
|
70
88
|
All messages are encoded using CBOR and are described in this document using
|
|
@@ -77,12 +95,21 @@ These type definitions are used in every message type
|
|
|
77
95
|
```cddl
|
|
78
96
|
; The base64 encoded bytes of a Peer ID
|
|
79
97
|
peer_id = str
|
|
98
|
+
; The base64 encoded bytes of a Storage ID
|
|
99
|
+
storage_id = str
|
|
80
100
|
; The possible protocol versions (currently always the string "1")
|
|
81
101
|
protocol_version = "1"
|
|
82
102
|
; The bytes of an automerge sync message
|
|
83
103
|
sync_message = bstr
|
|
84
104
|
; The base58check encoded bytes of a document ID
|
|
85
105
|
document_id = str
|
|
106
|
+
; Metadata sent in either the join or peer message types
|
|
107
|
+
peer_metadata = {
|
|
108
|
+
; The storage ID of this peer
|
|
109
|
+
? storageId: storage_id,
|
|
110
|
+
; Whether the sender expects to connect again with this storage ID
|
|
111
|
+
isEphemeral: bool
|
|
112
|
+
}
|
|
86
113
|
```
|
|
87
114
|
|
|
88
115
|
#### Join
|
|
@@ -94,6 +121,7 @@ Sent by the initiating peer in the [handshake](#handshake) phase.
|
|
|
94
121
|
type: "join",
|
|
95
122
|
senderId: peer_id,
|
|
96
123
|
supportedProtocolVersions: protocol_version
|
|
124
|
+
? metadata: peer_metadata,
|
|
97
125
|
}
|
|
98
126
|
```
|
|
99
127
|
|
|
@@ -108,6 +136,7 @@ Sent by the receiving peer in response to the join message in the
|
|
|
108
136
|
senderId: peer_id,
|
|
109
137
|
selectedProtocolVersion: protocol_version,
|
|
110
138
|
targetId: peer_id,
|
|
139
|
+
? metadata: peer_metadata,
|
|
111
140
|
}
|
|
112
141
|
```
|
|
113
142
|
|
|
@@ -195,3 +224,48 @@ connection will close
|
|
|
195
224
|
message: str,
|
|
196
225
|
}
|
|
197
226
|
```
|
|
227
|
+
|
|
228
|
+
#### Remote subscription changed
|
|
229
|
+
|
|
230
|
+
Sent when the sender wishes to change the set of storage IDs they wish to be notified of when the given remotes heads change.
|
|
231
|
+
|
|
232
|
+
```cddl
|
|
233
|
+
{
|
|
234
|
+
type: "remote-subscription-change"
|
|
235
|
+
senderId: peer_id
|
|
236
|
+
targetId: peer_id
|
|
237
|
+
|
|
238
|
+
; The storage IDs to add to the subscription
|
|
239
|
+
? add: [* storage_id]
|
|
240
|
+
|
|
241
|
+
; The storage IDs to remove from the subscription
|
|
242
|
+
remove: [* storage_id]
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
#### Remote heads changed
|
|
247
|
+
|
|
248
|
+
Sent when the sender wishes to inform the receiver that a peer with a storage ID in the receivers remote heads subscription has changed heads. This is either sent when the local peer receives a new sync message directly from the listened-to peer, or when the local peer receives a `remote-heads-changed` message relating to the listened-to peer from another peer.
|
|
249
|
+
|
|
250
|
+
```cddl
|
|
251
|
+
{
|
|
252
|
+
type: "remote-heads-changed"
|
|
253
|
+
senderId: peer_id
|
|
254
|
+
targetId: peer_id
|
|
255
|
+
|
|
256
|
+
; The document ID of the document that has changed
|
|
257
|
+
documentId: document_id
|
|
258
|
+
|
|
259
|
+
; A map from storage ID to the heads advertised for a given storage ID
|
|
260
|
+
newHeads: {
|
|
261
|
+
* storage_id => {
|
|
262
|
+
; The heads of the new document for the given storage ID as
|
|
263
|
+
; a list of base64 encoded SHA2 hashes
|
|
264
|
+
heads: [* string]
|
|
265
|
+
; The local time on the node which initially sent the remote-heads-changed
|
|
266
|
+
; message as milliseconds since the unix epoch
|
|
267
|
+
timestamp: uint
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
```
|
|
@@ -11,7 +11,7 @@ export declare class NodeWSServerAdapter extends NetworkAdapter {
|
|
|
11
11
|
[peerId: PeerId]: WebSocket;
|
|
12
12
|
};
|
|
13
13
|
constructor(server: WebSocketServer, keepAliveInterval?: number);
|
|
14
|
-
connect(peerId: PeerId, peerMetadata
|
|
14
|
+
connect(peerId: PeerId, peerMetadata?: PeerMetadata): void;
|
|
15
15
|
disconnect(): void;
|
|
16
16
|
send(message: FromServerMessage): void;
|
|
17
17
|
receiveMessage(messageBytes: Uint8Array, socket: WebSocket): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeWSServerAdapter.d.ts","sourceRoot":"","sources":["../src/NodeWSServerAdapter.ts"],"names":[],"mappings":";AAAA,OAAO,SAAS,MAAM,eAAe,CAAA;AACrC,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,eAAe,CAAA;AAKpD,OAAO,EAEL,cAAc,EACd,KAAK,YAAY,EACjB,KAAK,MAAM,EACZ,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAEL,iBAAiB,EAGlB,MAAM,eAAe,CAAA;AAOtB,qBAAa,mBAAoB,SAAQ,cAAc;;IAInD,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,iBAAiB;IAJ3B,OAAO,EAAE;QAAE,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAK;gBAGnC,MAAM,EAAE,eAAe,EACvB,iBAAiB,SAAO;IAKlC,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY;
|
|
1
|
+
{"version":3,"file":"NodeWSServerAdapter.d.ts","sourceRoot":"","sources":["../src/NodeWSServerAdapter.ts"],"names":[],"mappings":";AAAA,OAAO,SAAS,MAAM,eAAe,CAAA;AACrC,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,eAAe,CAAA;AAKpD,OAAO,EAEL,cAAc,EACd,KAAK,YAAY,EACjB,KAAK,MAAM,EACZ,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAEL,iBAAiB,EAGlB,MAAM,eAAe,CAAA;AAOtB,qBAAa,mBAAoB,SAAQ,cAAc;;IAInD,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,iBAAiB;IAJ3B,OAAO,EAAE;QAAE,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAK;gBAGnC,MAAM,EAAE,eAAe,EACvB,iBAAiB,SAAO;IAKlC,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,YAAY;IAyCnD,UAAU;IAQV,IAAI,CAAC,OAAO,EAAE,iBAAiB;IAqB/B,cAAc,CAAC,YAAY,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS;CA0E3D"}
|
|
@@ -4,7 +4,7 @@ const log = debug("WebsocketServer");
|
|
|
4
4
|
import { cbor as cborHelpers, NetworkAdapter, } from "@automerge/automerge-repo";
|
|
5
5
|
import { isJoinMessage, isLeaveMessage, } from "./messages.js";
|
|
6
6
|
import { ProtocolV1 } from "./protocolVersion.js";
|
|
7
|
-
import assert from "assert";
|
|
7
|
+
import { assert } from "./assert.js";
|
|
8
8
|
import { toArrayBuffer } from "./toArrayBuffer.js";
|
|
9
9
|
const { encode, decode } = cborHelpers;
|
|
10
10
|
export class NodeWSServerAdapter extends NetworkAdapter {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automerge/automerge-repo-network-websocket",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "isomorphic node/browser Websocket network adapter for Automerge Repo",
|
|
5
5
|
"repository": "https://github.com/automerge/automerge-repo/tree/master/packages/automerge-repo-network-websocket",
|
|
6
6
|
"author": "Peter van Hardenberg <pvh@pvh.ca>",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"test": "vitest"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@automerge/automerge-repo": "1.1.
|
|
16
|
+
"@automerge/automerge-repo": "1.1.2",
|
|
17
17
|
"cbor-x": "^1.3.0",
|
|
18
18
|
"debug": "^4.3.4",
|
|
19
19
|
"eventemitter3": "^5.0.1",
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "8bcba031ae952b1f3302cd41d1066cd2db5b677f"
|
|
35
35
|
}
|
|
@@ -24,7 +24,7 @@ abstract class WebSocketNetworkAdapter extends NetworkAdapter {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export class BrowserWebSocketClientAdapter extends WebSocketNetworkAdapter {
|
|
27
|
-
#isReady
|
|
27
|
+
#isReady = false
|
|
28
28
|
#retryIntervalId?: TimeoutId
|
|
29
29
|
#log = debug("automerge-repo:websocket:browser")
|
|
30
30
|
|
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
isLeaveMessage,
|
|
18
18
|
} from "./messages.js"
|
|
19
19
|
import { ProtocolV1, ProtocolVersion } from "./protocolVersion.js"
|
|
20
|
-
import assert from "assert"
|
|
20
|
+
import { assert } from "./assert.js"
|
|
21
21
|
import { toArrayBuffer } from "./toArrayBuffer.js"
|
|
22
22
|
|
|
23
23
|
const { encode, decode } = cborHelpers
|
|
@@ -32,7 +32,7 @@ export class NodeWSServerAdapter extends NetworkAdapter {
|
|
|
32
32
|
super()
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
connect(peerId: PeerId, peerMetadata
|
|
35
|
+
connect(peerId: PeerId, peerMetadata?: PeerMetadata) {
|
|
36
36
|
this.peerId = peerId
|
|
37
37
|
this.peerMetadata = peerMetadata
|
|
38
38
|
|