@automerge/automerge-repo-network-messagechannel 1.0.12 → 1.0.15
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/dist/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +61 -34
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EACL,KAAK,WAAW,EAChB,cAAc,EACd,KAAK,MAAM,EAEZ,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAOpD,qBAAa,4BAA6B,SAAQ,cAAc;;IAC9D,QAAQ,KAAK;IACb,cAAc;IACd,cAAc,EAAE,cAAc,CAAA;gBAI5B,WAAW,EAAE,WAAW,EACxB,MAAM,GAAE,kCAAuC;IAWjD,OAAO,CAAC,MAAM,EAAE,MAAM;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EACL,KAAK,WAAW,EAChB,cAAc,EACd,KAAK,MAAM,EAEZ,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAOpD,qBAAa,4BAA6B,SAAQ,cAAc;;IAC9D,QAAQ,KAAK;IACb,cAAc;IACd,cAAc,EAAE,cAAc,CAAA;gBAI5B,WAAW,EAAE,WAAW,EACxB,MAAM,GAAE,kCAAuC;IAWjD,OAAO,CAAC,MAAM,EAAE,MAAM;IAgEtB,IAAI,CAAC,OAAO,EAAE,WAAW;IAmBzB,kBAAkB,CAAC,MAAM,EAAE,MAAM;IAQjC,UAAU;CAIX;AAED,MAAM,WAAW,kCAAkC;IACjD;;;;;;;OAOG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automerge/automerge-repo-network-messagechannel",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.15",
|
|
4
4
|
"description": "MessageChannel network adapter for Automerge Repo",
|
|
5
5
|
"repository": "https://github.com/automerge/automerge-repo/tree/master/packages/automerge-repo-network-messagechannel",
|
|
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.0.
|
|
16
|
+
"@automerge/automerge-repo": "^1.0.15"
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
19
19
|
"@automerge/automerge": "^2.1.5"
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "f60bd2a4594cc23b0ee6debed62aea61fb48ea56"
|
|
33
33
|
}
|
package/src/index.ts
CHANGED
|
@@ -41,42 +41,45 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
|
|
|
41
41
|
log("messageport connecting")
|
|
42
42
|
this.peerId = peerId
|
|
43
43
|
this.messagePortRef.start()
|
|
44
|
-
this.messagePortRef.addListener(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
break
|
|
65
|
-
case "welcome":
|
|
66
|
-
this.announceConnection(senderId)
|
|
67
|
-
break
|
|
68
|
-
default:
|
|
69
|
-
if (!("data" in message)) {
|
|
70
|
-
this.emit("message", message)
|
|
71
|
-
} else {
|
|
72
|
-
this.emit("message", {
|
|
73
|
-
...message,
|
|
74
|
-
data: new Uint8Array(message.data),
|
|
44
|
+
this.messagePortRef.addListener(
|
|
45
|
+
"message",
|
|
46
|
+
(e: { data: MessageChannelMessage }) => {
|
|
47
|
+
log("message port received", e.data)
|
|
48
|
+
|
|
49
|
+
const message = e.data
|
|
50
|
+
if ("targetId" in message && message.targetId !== this.peerId) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
"MessagePortNetwork should never receive messages for a different peer."
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const { senderId, type } = message
|
|
57
|
+
|
|
58
|
+
switch (type) {
|
|
59
|
+
case "arrive":
|
|
60
|
+
this.messagePortRef.postMessage({
|
|
61
|
+
senderId: this.peerId,
|
|
62
|
+
targetId: senderId,
|
|
63
|
+
type: "welcome",
|
|
75
64
|
})
|
|
76
|
-
|
|
77
|
-
|
|
65
|
+
this.announceConnection(senderId)
|
|
66
|
+
break
|
|
67
|
+
case "welcome":
|
|
68
|
+
this.announceConnection(senderId)
|
|
69
|
+
break
|
|
70
|
+
default:
|
|
71
|
+
if (!("data" in message)) {
|
|
72
|
+
this.emit("message", message)
|
|
73
|
+
} else {
|
|
74
|
+
this.emit("message", {
|
|
75
|
+
...message,
|
|
76
|
+
data: new Uint8Array(message.data),
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
break
|
|
80
|
+
}
|
|
78
81
|
}
|
|
79
|
-
|
|
82
|
+
)
|
|
80
83
|
|
|
81
84
|
this.messagePortRef.addListener("close", () => {
|
|
82
85
|
this.emit("close")
|
|
@@ -142,3 +145,27 @@ export interface MessageChannelNetworkAdapterConfig {
|
|
|
142
145
|
*/
|
|
143
146
|
useWeakRef?: boolean
|
|
144
147
|
}
|
|
148
|
+
|
|
149
|
+
/** Notify the network that we have arrived so everyone knows our peer ID */
|
|
150
|
+
type ArriveMessage = {
|
|
151
|
+
type: "arrive"
|
|
152
|
+
|
|
153
|
+
/** The peer ID of the sender of this message */
|
|
154
|
+
senderId: PeerId
|
|
155
|
+
|
|
156
|
+
/** Arrive messages don't have a targetId */
|
|
157
|
+
targetId: never
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Respond to an arriving peer with our peer ID */
|
|
161
|
+
type WelcomeMessage = {
|
|
162
|
+
type: "welcome"
|
|
163
|
+
|
|
164
|
+
/** The peer ID of the recipient sender this message */
|
|
165
|
+
senderId: PeerId
|
|
166
|
+
|
|
167
|
+
/** The peer ID of the recipient of this message */
|
|
168
|
+
targetId: PeerId
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
type MessageChannelMessage = ArriveMessage | WelcomeMessage | Message
|