@automerge/automerge-repo-network-broadcastchannel 1.0.19 → 1.1.0-alpha.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/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -7
- package/package.json +3 -3
- package/src/index.ts +19 -7
- package/test/index.test.ts +4 -3
package/dist/index.d.ts
CHANGED
|
@@ -13,14 +13,14 @@
|
|
|
13
13
|
* @module
|
|
14
14
|
*
|
|
15
15
|
*/
|
|
16
|
-
import {
|
|
16
|
+
import { NetworkAdapter, type Message, type PeerId, type PeerMetadata } from "@automerge/automerge-repo";
|
|
17
17
|
export type BroadcastChannelNetworkAdapterOptions = {
|
|
18
18
|
channelName: string;
|
|
19
19
|
};
|
|
20
20
|
export declare class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
21
21
|
#private;
|
|
22
22
|
constructor(options?: BroadcastChannelNetworkAdapterOptions);
|
|
23
|
-
connect(peerId: PeerId): void;
|
|
23
|
+
connect(peerId: PeerId, peerMetadata: PeerMetadata): void;
|
|
24
24
|
send(message: Message): void;
|
|
25
25
|
disconnect(): void;
|
|
26
26
|
}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EACL,cAAc,EACd,KAAK,OAAO,EACZ,KAAK,MAAM,EACX,KAAK,YAAY,EAClB,MAAM,2BAA2B,CAAA;AAElC,MAAM,MAAM,qCAAqC,GAAG;IAClD,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED,qBAAa,8BAA+B,SAAQ,cAAc;;gBAKpD,OAAO,CAAC,EAAE,qCAAqC;IAK3D,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY;IAqDlD,IAAI,CAAC,OAAO,EAAE,OAAO;IAcrB,UAAU;CAIX"}
|
package/dist/index.js
CHANGED
|
@@ -13,16 +13,17 @@
|
|
|
13
13
|
* @module
|
|
14
14
|
*
|
|
15
15
|
*/
|
|
16
|
-
import { NetworkAdapter } from "@automerge/automerge-repo";
|
|
16
|
+
import { NetworkAdapter, } from "@automerge/automerge-repo";
|
|
17
17
|
export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
18
18
|
#broadcastChannel;
|
|
19
19
|
#options;
|
|
20
20
|
constructor(options) {
|
|
21
21
|
super();
|
|
22
|
-
this.#options = { ...(options ?? {})
|
|
22
|
+
this.#options = { channelName: "broadcast", ...(options ?? {}) };
|
|
23
23
|
}
|
|
24
|
-
connect(peerId) {
|
|
24
|
+
connect(peerId, peerMetadata) {
|
|
25
25
|
this.peerId = peerId;
|
|
26
|
+
this.peerMetadata = peerMetadata;
|
|
26
27
|
this.#broadcastChannel = new BroadcastChannel(this.#options.channelName);
|
|
27
28
|
this.#broadcastChannel.addEventListener("message", (e) => {
|
|
28
29
|
const message = e.data;
|
|
@@ -37,10 +38,10 @@ export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
|
37
38
|
targetId: senderId,
|
|
38
39
|
type: "welcome",
|
|
39
40
|
});
|
|
40
|
-
this.#announceConnection(senderId);
|
|
41
|
+
this.#announceConnection(senderId, peerMetadata);
|
|
41
42
|
break;
|
|
42
43
|
case "welcome":
|
|
43
|
-
this.#announceConnection(senderId);
|
|
44
|
+
this.#announceConnection(senderId, peerMetadata);
|
|
44
45
|
break;
|
|
45
46
|
default:
|
|
46
47
|
if (!("data" in message)) {
|
|
@@ -61,8 +62,8 @@ export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
|
61
62
|
});
|
|
62
63
|
this.emit("ready", { network: this });
|
|
63
64
|
}
|
|
64
|
-
#announceConnection(peerId) {
|
|
65
|
-
this.emit("peer-candidate", { peerId });
|
|
65
|
+
#announceConnection(peerId, peerMetadata) {
|
|
66
|
+
this.emit("peer-candidate", { peerId, peerMetadata });
|
|
66
67
|
}
|
|
67
68
|
send(message) {
|
|
68
69
|
if ("data" in message) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automerge/automerge-repo-network-broadcastchannel",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.1.0-alpha.2",
|
|
4
4
|
"description": "BroadcastChannel network adapter for Automerge Repo",
|
|
5
5
|
"repository": "https://github.com/automerge/automerge-repo/tree/master/packages/automerge-repo-network-broadcastchannel",
|
|
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.1.0-alpha.2"
|
|
17
17
|
},
|
|
18
18
|
"watch": {
|
|
19
19
|
"build": {
|
|
@@ -26,5 +26,5 @@
|
|
|
26
26
|
"publishConfig": {
|
|
27
27
|
"access": "public"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "d73e71588c3835a172fdf4d19e56a1f946c041ed"
|
|
30
30
|
}
|
package/src/index.ts
CHANGED
|
@@ -14,7 +14,12 @@
|
|
|
14
14
|
*
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
NetworkAdapter,
|
|
19
|
+
type Message,
|
|
20
|
+
type PeerId,
|
|
21
|
+
type PeerMetadata,
|
|
22
|
+
} from "@automerge/automerge-repo"
|
|
18
23
|
|
|
19
24
|
export type BroadcastChannelNetworkAdapterOptions = {
|
|
20
25
|
channelName: string
|
|
@@ -27,11 +32,12 @@ export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
|
27
32
|
|
|
28
33
|
constructor(options?: BroadcastChannelNetworkAdapterOptions) {
|
|
29
34
|
super()
|
|
30
|
-
this.#options = { ...(options ?? {})
|
|
35
|
+
this.#options = { channelName: "broadcast", ...(options ?? {}) }
|
|
31
36
|
}
|
|
32
37
|
|
|
33
|
-
connect(peerId: PeerId) {
|
|
38
|
+
connect(peerId: PeerId, peerMetadata: PeerMetadata) {
|
|
34
39
|
this.peerId = peerId
|
|
40
|
+
this.peerMetadata = peerMetadata
|
|
35
41
|
this.#broadcastChannel = new BroadcastChannel(this.#options.channelName)
|
|
36
42
|
|
|
37
43
|
this.#broadcastChannel.addEventListener(
|
|
@@ -51,10 +57,10 @@ export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
|
51
57
|
targetId: senderId,
|
|
52
58
|
type: "welcome",
|
|
53
59
|
})
|
|
54
|
-
this.#announceConnection(senderId)
|
|
60
|
+
this.#announceConnection(senderId, peerMetadata)
|
|
55
61
|
break
|
|
56
62
|
case "welcome":
|
|
57
|
-
this.#announceConnection(senderId)
|
|
63
|
+
this.#announceConnection(senderId, peerMetadata)
|
|
58
64
|
break
|
|
59
65
|
default:
|
|
60
66
|
if (!("data" in message)) {
|
|
@@ -78,8 +84,8 @@ export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
|
78
84
|
this.emit("ready", { network: this })
|
|
79
85
|
}
|
|
80
86
|
|
|
81
|
-
#announceConnection(peerId: PeerId) {
|
|
82
|
-
this.emit("peer-candidate", { peerId })
|
|
87
|
+
#announceConnection(peerId: PeerId, peerMetadata: PeerMetadata) {
|
|
88
|
+
this.emit("peer-candidate", { peerId, peerMetadata })
|
|
83
89
|
}
|
|
84
90
|
|
|
85
91
|
send(message: Message) {
|
|
@@ -109,6 +115,9 @@ type ArriveMessage = {
|
|
|
109
115
|
/** The peer ID of the sender of this message */
|
|
110
116
|
senderId: PeerId
|
|
111
117
|
|
|
118
|
+
/** The peer metadata of the sender of this message */
|
|
119
|
+
peerMetadata: PeerMetadata
|
|
120
|
+
|
|
112
121
|
/** Arrive messages don't have a targetId */
|
|
113
122
|
targetId: never
|
|
114
123
|
}
|
|
@@ -120,6 +129,9 @@ type WelcomeMessage = {
|
|
|
120
129
|
/** The peer ID of the recipient sender this message */
|
|
121
130
|
senderId: PeerId
|
|
122
131
|
|
|
132
|
+
/** The peer metadata of the sender of this message */
|
|
133
|
+
peerMetadata: PeerMetadata
|
|
134
|
+
|
|
123
135
|
/** The peer ID of the recipient of this message */
|
|
124
136
|
targetId: PeerId
|
|
125
137
|
}
|
package/test/index.test.ts
CHANGED
|
@@ -18,11 +18,11 @@ describe("BroadcastChannel", () => {
|
|
|
18
18
|
runAdapterTests(setup)
|
|
19
19
|
|
|
20
20
|
it("allows a channel name to be specified in the options and limits messages to that channel", async () => {
|
|
21
|
-
const a = new BroadcastChannelNetworkAdapter(
|
|
22
|
-
const b = new BroadcastChannelNetworkAdapter(
|
|
21
|
+
const a = new BroadcastChannelNetworkAdapter()
|
|
22
|
+
const b = new BroadcastChannelNetworkAdapter()
|
|
23
23
|
|
|
24
24
|
// this adapter should never connect
|
|
25
|
-
const c = new BroadcastChannelNetworkAdapter()
|
|
25
|
+
const c = new BroadcastChannelNetworkAdapter({ channelName: "other" })
|
|
26
26
|
|
|
27
27
|
const aConnect = new Promise<void>(resolve => {
|
|
28
28
|
a.once("peer-candidate", () => resolve())
|
|
@@ -38,6 +38,7 @@ describe("BroadcastChannel", () => {
|
|
|
38
38
|
|
|
39
39
|
a.connect("a" as PeerId)
|
|
40
40
|
b.connect("b" as PeerId)
|
|
41
|
+
c.connect("c" as PeerId)
|
|
41
42
|
|
|
42
43
|
return Promise.all([aConnect, cShouldNotConnect])
|
|
43
44
|
})
|