@automerge/automerge-repo-network-websocket 1.0.19 → 1.1.0-alpha.13

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/src/messages.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Message, PeerId } from "@automerge/automerge-repo"
1
+ import type { Message, PeerId, PeerMetadata } from "@automerge/automerge-repo"
2
2
  import type { ProtocolVersion } from "./protocolVersion.js"
3
3
 
4
4
  /** The sender is disconnecting */
@@ -12,6 +12,10 @@ export type JoinMessage = {
12
12
  type: "join"
13
13
  /** The PeerID of the client */
14
14
  senderId: PeerId
15
+
16
+ /** Metadata presented by the peer */
17
+ peerMetadata: PeerMetadata
18
+
15
19
  /** The protocol version the client supports */
16
20
  supportedProtocolVersions: ProtocolVersion[]
17
21
  }
@@ -21,6 +25,10 @@ export type PeerMessage = {
21
25
  type: "peer"
22
26
  /** The PeerID of the server */
23
27
  senderId: PeerId
28
+
29
+ /** Metadata presented by the peer */
30
+ peerMetadata: PeerMetadata
31
+
24
32
  /** The protocol version the server selected for this connection */
25
33
  selectedProtocolVersion: ProtocolVersion
26
34
  /** The PeerID of the client */
@@ -38,11 +46,26 @@ export type ErrorMessage = {
38
46
  targetId: PeerId
39
47
  }
40
48
 
41
- // This adapter doesn't use the network adapter Message types, it has its own idea of how to handle
42
- // join/leave
43
-
44
49
  /** A message from the client to the server */
45
50
  export type FromClientMessage = JoinMessage | LeaveMessage | Message
46
51
 
47
52
  /** A message from the server to the client */
48
53
  export type FromServerMessage = PeerMessage | ErrorMessage | Message
54
+
55
+ // TYPE GUARDS
56
+
57
+ export const isJoinMessage = (
58
+ message: FromClientMessage
59
+ ): message is JoinMessage => message.type === "join"
60
+
61
+ export const isLeaveMessage = (
62
+ message: FromClientMessage
63
+ ): message is LeaveMessage => message.type === "leave"
64
+
65
+ export const isPeerMessage = (
66
+ message: FromServerMessage
67
+ ): message is PeerMessage => message.type === "peer"
68
+
69
+ export const isErrorMessage = (
70
+ message: FromServerMessage
71
+ ): message is ErrorMessage => message.type === "error"
@@ -0,0 +1,8 @@
1
+ /**
2
+ * This incantation deals with websocket sending the whole underlying buffer even if we just have a
3
+ * uint8array view on it
4
+ */
5
+ export const toArrayBuffer = (bytes: Uint8Array) => {
6
+ const { buffer, byteOffset, byteLength } = bytes
7
+ return buffer.slice(byteOffset, byteOffset + byteLength)
8
+ }