-
Notifications
You must be signed in to change notification settings - Fork 53
/
index.ts
215 lines (184 loc) · 5.83 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* A `NetworkAdapter` which uses [`MessageChannel`](https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel)
* to communicate with other peers. This is useful for communicating between
* browser tabs and web workers (including shared workers).
*
* @module
*/
import {
type RepoMessage,
NetworkAdapter,
type PeerId,
type Message,
PeerMetadata,
} from "@automerge/automerge-repo/slim"
import { MessagePortRef } from "./MessagePortRef.js"
import { StrongMessagePortRef } from "./StrongMessagePortRef.js"
import { WeakMessagePortRef } from "./WeakMessagePortRef.js"
import debug from "debug"
const log = debug("automerge-repo:messagechannel")
export class MessageChannelNetworkAdapter extends NetworkAdapter {
channels = {}
/** @hidden */
messagePortRef: MessagePortRef
#ready = false
#readyResolver?: () => void
#readyPromise: Promise<void> = new Promise<void>(resolve => {
this.#readyResolver = resolve
})
#remotePeerId?: PeerId
isReady() {
return this.#ready
}
whenReady() {
return this.#readyPromise
}
#forceReady() {
if (!this.#ready) {
this.#ready = true
this.#readyResolver?.()
}
}
constructor(
messagePort: MessagePort,
config: MessageChannelNetworkAdapterConfig = {}
) {
super()
const useWeakRef = config.useWeakRef ?? false
this.messagePortRef = useWeakRef
? new WeakMessagePortRef(messagePort)
: new StrongMessagePortRef(messagePort)
}
connect(peerId: PeerId, peerMetadata?: PeerMetadata) {
log("messageport connecting")
this.peerId = peerId
this.peerMetadata = peerMetadata
this.messagePortRef.start()
this.messagePortRef.addListener(
"message",
(e: { data: MessageChannelMessage }) => {
log("message port received %o", e.data)
const message = e.data
if ("targetId" in message && message.targetId !== this.peerId) {
throw new Error(
"MessagePortNetwork should never receive messages for a different peer."
)
}
const { senderId, type } = message
switch (type) {
case "arrive":
{
const { peerMetadata } = message as ArriveMessage
this.messagePortRef.postMessage({
type: "welcome",
senderId: this.peerId,
peerMetadata: this.peerMetadata,
targetId: senderId,
})
this.announceConnection(senderId, peerMetadata)
}
break
case "welcome":
{
const { peerMetadata } = message as WelcomeMessage
this.announceConnection(senderId, peerMetadata)
}
break
case "leave":
if (this.#remotePeerId === senderId) {
this.emit("peer-disconnected", { peerId: senderId })
}
break
default:
if (!("data" in message)) {
this.emit("message", message)
} else {
this.emit("message", {
...message,
data: message.data ? new Uint8Array(message.data) : undefined,
})
}
break
}
}
)
this.messagePortRef.addListener("close", () => {
this.emit("close")
})
this.messagePortRef.postMessage({
senderId: this.peerId,
type: "arrive",
peerMetadata,
})
// Mark this messagechannel as ready after 50 ms, at this point there
// must be something weird going on on the other end to cause us to receive
// no response
setTimeout(() => {
this.#forceReady()
}, 100)
}
send(message: RepoMessage) {
if ("data" in message) {
const data = message.data.buffer.slice(
message.data.byteOffset,
message.data.byteOffset + message.data.byteLength
)
this.messagePortRef.postMessage(
{
...message,
data,
},
[data]
)
} else {
this.messagePortRef.postMessage(message)
}
}
announceConnection(peerId: PeerId, peerMetadata: PeerMetadata) {
this.#remotePeerId = peerId
this.#forceReady()
this.emit("peer-candidate", { peerId, peerMetadata })
}
disconnect() {
if (this.#remotePeerId && this.peerId) {
this.messagePortRef.postMessage({
type: "leave",
senderId: this.peerId,
})
this.emit("peer-disconnected", { peerId: this.#remotePeerId })
}
this.messagePortRef.stop()
}
}
export interface MessageChannelNetworkAdapterConfig {
/**
* This is an optional parameter to use a weak ref to reference the message port that is passed to
* the adapter. This option is useful when using a message channel with a shared worker. If you
* use a network adapter with `useWeakRef = true` in the shared worker and in the main thread
* network adapters with strong refs the network adapter will be automatically garbage collected
* if you close a page. The garbage collection doesn't happen immediately; there might be some
* time in between when the page is closed and when the port is garbage collected
*/
useWeakRef?: boolean
}
/** Notify the network that we have arrived so everyone knows our peer ID */
type ArriveMessage = {
type: "arrive"
/** The peer ID of the sender of this message */
senderId: PeerId
/** The peer metadata of the sender of this message */
peerMetadata: PeerMetadata
/** Arrive messages don't have a targetId */
targetId: never
}
/** Respond to an arriving peer with our peer ID */
type WelcomeMessage = {
type: "welcome"
/** The peer ID of the recipient sender this message */
senderId: PeerId
/** The peer metadata of the sender of this message */
peerMetadata: PeerMetadata
/** The peer ID of the recipient of this message */
targetId: PeerId
}
type MessageChannelMessage = ArriveMessage | WelcomeMessage | Message