@automerge/automerge-repo-network-broadcastchannel 1.0.0-alpha.2 → 1.0.0-alpha.4
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 +26 -25
- package/package.json +4 -4
- package/src/index.ts +51 -49
- package/test/index.test.ts +2 -2
- package/tsconfig.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NetworkAdapter, PeerId, Message } from "@automerge/automerge-repo";
|
|
2
2
|
export declare class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
3
3
|
#private;
|
|
4
4
|
connect(peerId: PeerId): void;
|
|
5
|
-
|
|
5
|
+
send(message: Message): void;
|
|
6
6
|
join(): void;
|
|
7
7
|
leave(): void;
|
|
8
8
|
}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,cAAc,EACd,MAAM,EACN,OAAO,EACR,MAAM,2BAA2B,CAAA;AAElC,qBAAa,8BAA+B,SAAQ,cAAc;;IAGhE,OAAO,CAAC,MAAM,EAAE,MAAM;IA8CtB,IAAI,CAAC,OAAO,EAAE,OAAO;IAcrB,IAAI;IAOJ,KAAK;CAIN"}
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { NetworkAdapter } from "@automerge/automerge-repo";
|
|
1
|
+
import { NetworkAdapter, } from "@automerge/automerge-repo";
|
|
2
2
|
export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
3
3
|
#broadcastChannel;
|
|
4
4
|
connect(peerId) {
|
|
5
5
|
this.peerId = peerId;
|
|
6
6
|
this.#broadcastChannel = new BroadcastChannel(`broadcast`);
|
|
7
|
-
this.#broadcastChannel.addEventListener("message", e => {
|
|
8
|
-
const
|
|
9
|
-
if (targetId && targetId !== this.peerId
|
|
7
|
+
this.#broadcastChannel.addEventListener("message", (e) => {
|
|
8
|
+
const message = e.data;
|
|
9
|
+
if ("targetId" in message && message.targetId !== this.peerId) {
|
|
10
10
|
return;
|
|
11
11
|
}
|
|
12
|
+
const { senderId, type } = message;
|
|
12
13
|
switch (type) {
|
|
13
14
|
case "arrive":
|
|
14
15
|
this.#broadcastChannel.postMessage({
|
|
@@ -21,39 +22,39 @@ export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
|
21
22
|
case "welcome":
|
|
22
23
|
this.#announceConnection(senderId);
|
|
23
24
|
break;
|
|
24
|
-
case "message":
|
|
25
|
-
this.emit("message", {
|
|
26
|
-
senderId,
|
|
27
|
-
targetId,
|
|
28
|
-
channelId,
|
|
29
|
-
message: new Uint8Array(message),
|
|
30
|
-
broadcast,
|
|
31
|
-
});
|
|
32
|
-
break;
|
|
33
25
|
default:
|
|
34
|
-
|
|
26
|
+
if (!("data" in message)) {
|
|
27
|
+
this.emit("message", message);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this.emit("message", {
|
|
31
|
+
...message,
|
|
32
|
+
data: new Uint8Array(message.data),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
break;
|
|
35
36
|
}
|
|
36
37
|
});
|
|
38
|
+
this.emit("ready", { network: this });
|
|
37
39
|
}
|
|
38
40
|
#announceConnection(peerId) {
|
|
39
41
|
this.emit("peer-candidate", { peerId });
|
|
40
42
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
43
|
+
send(message) {
|
|
44
|
+
if ("data" in message) {
|
|
45
|
+
this.#broadcastChannel.postMessage({
|
|
46
|
+
...message,
|
|
47
|
+
data: message.data.buffer.slice(message.data.byteOffset, message.data.byteOffset + message.data.byteLength),
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
this.#broadcastChannel.postMessage(message);
|
|
52
|
+
}
|
|
51
53
|
}
|
|
52
54
|
join() {
|
|
53
55
|
this.#broadcastChannel.postMessage({
|
|
54
56
|
senderId: this.peerId,
|
|
55
57
|
type: "arrive",
|
|
56
|
-
broadcast: true,
|
|
57
58
|
});
|
|
58
59
|
}
|
|
59
60
|
leave() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automerge/automerge-repo-network-broadcastchannel",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.4",
|
|
4
4
|
"description": "BroadcastChannel network adapter for Automerge Repo",
|
|
5
5
|
"repository": "https://github.com/automerge/automerge-repo",
|
|
6
6
|
"author": "Peter van Hardenberg <pvh@pvh.ca>",
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
"test": "mocha --no-warnings --experimental-specifier-resolution=node --exit"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"@automerge/automerge": "^2.1.0-alpha.
|
|
17
|
+
"@automerge/automerge": "^2.1.0-alpha.12"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@automerge/automerge-repo": "^1.0.0-alpha.
|
|
20
|
+
"@automerge/automerge-repo": "^1.0.0-alpha.4"
|
|
21
21
|
},
|
|
22
22
|
"watch": {
|
|
23
23
|
"build": {
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "fbf71f0c3aaa2786a4e279f336f01d665f53ce5b"
|
|
34
34
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
NetworkAdapterMessage,
|
|
3
|
+
NetworkAdapter,
|
|
4
|
+
PeerId,
|
|
5
|
+
Message,
|
|
6
|
+
} from "@automerge/automerge-repo"
|
|
2
7
|
|
|
3
8
|
export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
4
9
|
#broadcastChannel: BroadcastChannel
|
|
@@ -7,69 +12,66 @@ export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
|
7
12
|
this.peerId = peerId
|
|
8
13
|
this.#broadcastChannel = new BroadcastChannel(`broadcast`)
|
|
9
14
|
|
|
10
|
-
this.#broadcastChannel.addEventListener(
|
|
11
|
-
|
|
15
|
+
this.#broadcastChannel.addEventListener(
|
|
16
|
+
"message",
|
|
17
|
+
(e: { data: NetworkAdapterMessage }) => {
|
|
18
|
+
const message = e.data
|
|
19
|
+
if ("targetId" in message && message.targetId !== this.peerId) {
|
|
20
|
+
return
|
|
21
|
+
}
|
|
12
22
|
|
|
13
|
-
|
|
14
|
-
return
|
|
15
|
-
}
|
|
23
|
+
const { senderId, type } = message
|
|
16
24
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
25
|
+
switch (type) {
|
|
26
|
+
case "arrive":
|
|
27
|
+
this.#broadcastChannel.postMessage({
|
|
28
|
+
senderId: this.peerId,
|
|
29
|
+
targetId: senderId,
|
|
30
|
+
type: "welcome",
|
|
31
|
+
})
|
|
32
|
+
this.#announceConnection(senderId)
|
|
33
|
+
break
|
|
34
|
+
case "welcome":
|
|
35
|
+
this.#announceConnection(senderId)
|
|
36
|
+
break
|
|
37
|
+
default:
|
|
38
|
+
if (!("data" in message)) {
|
|
39
|
+
this.emit("message", message)
|
|
40
|
+
} else {
|
|
41
|
+
this.emit("message", {
|
|
42
|
+
...message,
|
|
43
|
+
data: new Uint8Array(message.data),
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
break
|
|
47
|
+
}
|
|
40
48
|
}
|
|
41
|
-
|
|
49
|
+
)
|
|
50
|
+
this.emit("ready", { network: this })
|
|
42
51
|
}
|
|
43
52
|
|
|
44
53
|
#announceConnection(peerId: PeerId) {
|
|
45
54
|
this.emit("peer-candidate", { peerId })
|
|
46
55
|
}
|
|
47
56
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
targetId: peerId,
|
|
61
|
-
type: "message",
|
|
62
|
-
channelId,
|
|
63
|
-
message,
|
|
64
|
-
broadcast,
|
|
65
|
-
})
|
|
57
|
+
send(message: Message) {
|
|
58
|
+
if ("data" in message) {
|
|
59
|
+
this.#broadcastChannel.postMessage({
|
|
60
|
+
...message,
|
|
61
|
+
data: message.data.buffer.slice(
|
|
62
|
+
message.data.byteOffset,
|
|
63
|
+
message.data.byteOffset + message.data.byteLength
|
|
64
|
+
),
|
|
65
|
+
})
|
|
66
|
+
} else {
|
|
67
|
+
this.#broadcastChannel.postMessage(message)
|
|
68
|
+
}
|
|
66
69
|
}
|
|
67
70
|
|
|
68
71
|
join() {
|
|
69
72
|
this.#broadcastChannel.postMessage({
|
|
70
73
|
senderId: this.peerId,
|
|
71
74
|
type: "arrive",
|
|
72
|
-
broadcast: true,
|
|
73
75
|
})
|
|
74
76
|
}
|
|
75
77
|
|
package/test/index.test.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { BroadcastChannelNetworkAdapter } from "../src"
|
|
1
|
+
import { BroadcastChannelNetworkAdapter } from "../src/index.js"
|
|
2
2
|
import {
|
|
3
3
|
type SetupFn,
|
|
4
4
|
runAdapterTests,
|
|
5
|
-
} from "../../automerge-repo/src/helpers/tests/network-adapter-tests"
|
|
5
|
+
} from "../../automerge-repo/src/helpers/tests/network-adapter-tests.js"
|
|
6
6
|
|
|
7
7
|
describe("BroadcastChannel", () => {
|
|
8
8
|
const setup: SetupFn = async () => {
|
package/tsconfig.json
CHANGED