@automerge/automerge-repo-network-broadcastchannel 1.0.18 → 1.1.0-alpha.1
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 +35 -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 StorageId } 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, storageId: StorageId | undefined, isEphemeral: boolean): 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,SAAS,EACf,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,CACL,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,SAAS,GAAG,SAAS,EAChC,WAAW,EAAE,OAAO;IA0DtB,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, storageId, isEphemeral) {
|
|
25
25
|
this.peerId = peerId;
|
|
26
|
+
this.storageId = storageId;
|
|
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, storageId, isEphemeral);
|
|
41
42
|
break;
|
|
42
43
|
case "welcome":
|
|
43
|
-
this.#announceConnection(senderId);
|
|
44
|
+
this.#announceConnection(senderId, storageId, isEphemeral);
|
|
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, storageId, isEphemeral) {
|
|
66
|
+
this.emit("peer-candidate", { peerId, storageId, isEphemeral });
|
|
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.1",
|
|
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.1"
|
|
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": "11805d698f860bd6ffb3ca028d3b57e718690b5a"
|
|
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 StorageId,
|
|
22
|
+
} from "@automerge/automerge-repo"
|
|
18
23
|
|
|
19
24
|
export type BroadcastChannelNetworkAdapterOptions = {
|
|
20
25
|
channelName: string
|
|
@@ -27,11 +32,16 @@ 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(
|
|
38
|
+
connect(
|
|
39
|
+
peerId: PeerId,
|
|
40
|
+
storageId: StorageId | undefined,
|
|
41
|
+
isEphemeral: boolean
|
|
42
|
+
) {
|
|
34
43
|
this.peerId = peerId
|
|
44
|
+
this.storageId = storageId
|
|
35
45
|
this.#broadcastChannel = new BroadcastChannel(this.#options.channelName)
|
|
36
46
|
|
|
37
47
|
this.#broadcastChannel.addEventListener(
|
|
@@ -51,10 +61,10 @@ export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
|
51
61
|
targetId: senderId,
|
|
52
62
|
type: "welcome",
|
|
53
63
|
})
|
|
54
|
-
this.#announceConnection(senderId)
|
|
64
|
+
this.#announceConnection(senderId, storageId, isEphemeral)
|
|
55
65
|
break
|
|
56
66
|
case "welcome":
|
|
57
|
-
this.#announceConnection(senderId)
|
|
67
|
+
this.#announceConnection(senderId, storageId, isEphemeral)
|
|
58
68
|
break
|
|
59
69
|
default:
|
|
60
70
|
if (!("data" in message)) {
|
|
@@ -78,8 +88,12 @@ export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
|
78
88
|
this.emit("ready", { network: this })
|
|
79
89
|
}
|
|
80
90
|
|
|
81
|
-
#announceConnection(
|
|
82
|
-
|
|
91
|
+
#announceConnection(
|
|
92
|
+
peerId: PeerId,
|
|
93
|
+
storageId: StorageId | undefined,
|
|
94
|
+
isEphemeral: boolean
|
|
95
|
+
) {
|
|
96
|
+
this.emit("peer-candidate", { peerId, storageId, isEphemeral })
|
|
83
97
|
}
|
|
84
98
|
|
|
85
99
|
send(message: Message) {
|
|
@@ -109,6 +123,13 @@ type ArriveMessage = {
|
|
|
109
123
|
/** The peer ID of the sender of this message */
|
|
110
124
|
senderId: PeerId
|
|
111
125
|
|
|
126
|
+
/** Unique ID of the storage that the sender peer is using, is persistent across sessions */
|
|
127
|
+
storageId?: StorageId
|
|
128
|
+
|
|
129
|
+
/** Indicates whether other peers should persist the sync state of the sender peer.
|
|
130
|
+
* Sync state is only persisted for non-ephemeral peers */
|
|
131
|
+
isEphemeral: boolean
|
|
132
|
+
|
|
112
133
|
/** Arrive messages don't have a targetId */
|
|
113
134
|
targetId: never
|
|
114
135
|
}
|
|
@@ -120,6 +141,13 @@ type WelcomeMessage = {
|
|
|
120
141
|
/** The peer ID of the recipient sender this message */
|
|
121
142
|
senderId: PeerId
|
|
122
143
|
|
|
144
|
+
/** Unique ID of the storage that the sender peer is using, is persistent across sessions */
|
|
145
|
+
storageId?: StorageId
|
|
146
|
+
|
|
147
|
+
/** Indicates whether other peers should persist the sync state of the sender peer.
|
|
148
|
+
* Sync state is only persisted for non-ephemeral peers */
|
|
149
|
+
isEphemeral: boolean
|
|
150
|
+
|
|
123
151
|
/** The peer ID of the recipient of this message */
|
|
124
152
|
targetId: PeerId
|
|
125
153
|
}
|
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
|
})
|