@automerge/automerge-repo-network-broadcastchannel 0.0.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/.mocharc.json +5 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +64 -0
- package/package.json +31 -0
- package/src/index.ts +83 -0
- package/test/index.test.ts +14 -0
- package/tsconfig.json +16 -0
package/.mocharc.json
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ChannelId, NetworkAdapter, PeerId } from "@automerge/automerge-repo";
|
|
2
|
+
export declare class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
3
|
+
#private;
|
|
4
|
+
connect(peerId: PeerId): void;
|
|
5
|
+
sendMessage(peerId: PeerId, channelId: ChannelId, uint8message: Uint8Array, broadcast: boolean): void;
|
|
6
|
+
join(joinChannelId: ChannelId): void;
|
|
7
|
+
leave(channelId: ChannelId): void;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAE7E,qBAAa,8BAA+B,SAAQ,cAAc;;IAGhE,OAAO,CAAC,MAAM,EAAE,MAAM;IA0CtB,WAAW,CACT,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,SAAS,EACpB,YAAY,EAAE,UAAU,EACxB,SAAS,EAAE,OAAO;IAgBpB,IAAI,CAAC,aAAa,EAAE,SAAS;IAS7B,KAAK,CAAC,SAAS,EAAE,SAAS;CAM3B"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { NetworkAdapter } from "@automerge/automerge-repo";
|
|
2
|
+
export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
3
|
+
#broadcastChannel;
|
|
4
|
+
connect(peerId) {
|
|
5
|
+
this.peerId = peerId;
|
|
6
|
+
this.#broadcastChannel = new BroadcastChannel(`broadcast`);
|
|
7
|
+
this.#broadcastChannel.addEventListener("message", e => {
|
|
8
|
+
const { senderId, targetId, type, channelId, message, broadcast } = e.data;
|
|
9
|
+
if (targetId && targetId !== this.peerId && !broadcast) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
switch (type) {
|
|
13
|
+
case "arrive":
|
|
14
|
+
this.#broadcastChannel.postMessage({
|
|
15
|
+
senderId: this.peerId,
|
|
16
|
+
targetId: senderId,
|
|
17
|
+
type: "welcome",
|
|
18
|
+
});
|
|
19
|
+
this.#announceConnection(channelId, senderId);
|
|
20
|
+
break;
|
|
21
|
+
case "welcome":
|
|
22
|
+
this.#announceConnection(channelId, senderId);
|
|
23
|
+
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
|
+
default:
|
|
34
|
+
throw new Error("unhandled message from network");
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
#announceConnection(channelId, peerId) {
|
|
39
|
+
this.emit("peer-candidate", { peerId, channelId });
|
|
40
|
+
}
|
|
41
|
+
sendMessage(peerId, channelId, uint8message, broadcast) {
|
|
42
|
+
const message = uint8message.buffer.slice(uint8message.byteOffset, uint8message.byteOffset + uint8message.byteLength);
|
|
43
|
+
this.#broadcastChannel.postMessage({
|
|
44
|
+
senderId: this.peerId,
|
|
45
|
+
targetId: peerId,
|
|
46
|
+
type: "message",
|
|
47
|
+
channelId,
|
|
48
|
+
message,
|
|
49
|
+
broadcast,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
join(joinChannelId) {
|
|
53
|
+
this.#broadcastChannel.postMessage({
|
|
54
|
+
senderId: this.peerId,
|
|
55
|
+
channelId: joinChannelId,
|
|
56
|
+
type: "arrive",
|
|
57
|
+
broadcast: true,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
leave(channelId) {
|
|
61
|
+
// TODO
|
|
62
|
+
throw new Error("Unimplemented: leave on BroadcastChannelNetworkAdapter: " + channelId);
|
|
63
|
+
}
|
|
64
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@automerge/automerge-repo-network-broadcastchannel",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "BroadcastChannel network adapter for Automerge Repo",
|
|
5
|
+
"repository": "https://github.com/pvh/automerge-repo",
|
|
6
|
+
"author": "Peter van Hardenberg <pvh@pvh.ca>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"private": false,
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "dist/index.js",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"watch": "npm-watch",
|
|
14
|
+
"test": "mocha --no-warnings --experimental-specifier-resolution=node --exit"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@automerge/automerge-repo": "^0.0.1"
|
|
18
|
+
},
|
|
19
|
+
"watch": {
|
|
20
|
+
"build": {
|
|
21
|
+
"patterns": "./src/**/*",
|
|
22
|
+
"extensions": [
|
|
23
|
+
".ts"
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"gitHead": "e572f26ae416140b025c3ba557d9f781abbdada1"
|
|
31
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { ChannelId, NetworkAdapter, PeerId } from "@automerge/automerge-repo"
|
|
2
|
+
|
|
3
|
+
export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
|
|
4
|
+
#broadcastChannel: BroadcastChannel
|
|
5
|
+
|
|
6
|
+
connect(peerId: PeerId) {
|
|
7
|
+
this.peerId = peerId
|
|
8
|
+
this.#broadcastChannel = new BroadcastChannel(`broadcast`)
|
|
9
|
+
|
|
10
|
+
this.#broadcastChannel.addEventListener("message", e => {
|
|
11
|
+
const { senderId, targetId, type, channelId, message, broadcast } = e.data
|
|
12
|
+
|
|
13
|
+
if (targetId && targetId !== this.peerId && !broadcast) {
|
|
14
|
+
return
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
switch (type) {
|
|
18
|
+
case "arrive":
|
|
19
|
+
this.#broadcastChannel.postMessage({
|
|
20
|
+
senderId: this.peerId,
|
|
21
|
+
targetId: senderId,
|
|
22
|
+
type: "welcome",
|
|
23
|
+
})
|
|
24
|
+
this.#announceConnection(channelId, senderId)
|
|
25
|
+
break
|
|
26
|
+
case "welcome":
|
|
27
|
+
this.#announceConnection(channelId, senderId)
|
|
28
|
+
break
|
|
29
|
+
case "message":
|
|
30
|
+
this.emit("message", {
|
|
31
|
+
senderId,
|
|
32
|
+
targetId,
|
|
33
|
+
channelId,
|
|
34
|
+
message: new Uint8Array(message),
|
|
35
|
+
broadcast,
|
|
36
|
+
})
|
|
37
|
+
break
|
|
38
|
+
default:
|
|
39
|
+
throw new Error("unhandled message from network")
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#announceConnection(channelId: ChannelId, peerId: PeerId) {
|
|
45
|
+
this.emit("peer-candidate", { peerId, channelId })
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
sendMessage(
|
|
49
|
+
peerId: PeerId,
|
|
50
|
+
channelId: ChannelId,
|
|
51
|
+
uint8message: Uint8Array,
|
|
52
|
+
broadcast: boolean
|
|
53
|
+
) {
|
|
54
|
+
const message = uint8message.buffer.slice(
|
|
55
|
+
uint8message.byteOffset,
|
|
56
|
+
uint8message.byteOffset + uint8message.byteLength
|
|
57
|
+
)
|
|
58
|
+
this.#broadcastChannel.postMessage({
|
|
59
|
+
senderId: this.peerId,
|
|
60
|
+
targetId: peerId,
|
|
61
|
+
type: "message",
|
|
62
|
+
channelId,
|
|
63
|
+
message,
|
|
64
|
+
broadcast,
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
join(joinChannelId: ChannelId) {
|
|
69
|
+
this.#broadcastChannel.postMessage({
|
|
70
|
+
senderId: this.peerId,
|
|
71
|
+
channelId: joinChannelId,
|
|
72
|
+
type: "arrive",
|
|
73
|
+
broadcast: true,
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
leave(channelId: ChannelId) {
|
|
78
|
+
// TODO
|
|
79
|
+
throw new Error(
|
|
80
|
+
"Unimplemented: leave on BroadcastChannelNetworkAdapter: " + channelId
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BroadcastChannelNetworkAdapter } from "../src"
|
|
2
|
+
import { type SetupFn, runAdapterTests } from "@automerge/automerge-repo"
|
|
3
|
+
|
|
4
|
+
describe("BroadcastChannel", () => {
|
|
5
|
+
const setup: SetupFn = async () => {
|
|
6
|
+
const a = new BroadcastChannelNetworkAdapter()
|
|
7
|
+
const b = new BroadcastChannelNetworkAdapter()
|
|
8
|
+
const c = new BroadcastChannelNetworkAdapter()
|
|
9
|
+
|
|
10
|
+
return { adapters: [a, b, c] }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
runAdapterTests(setup)
|
|
14
|
+
})
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext",
|
|
4
|
+
"jsx": "react",
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"moduleResolution": "node",
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"declarationMap": true,
|
|
9
|
+
"outDir": "./dist",
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"strict": false,
|
|
13
|
+
"skipLibCheck": true
|
|
14
|
+
},
|
|
15
|
+
"include": ["src/**/*.ts"]
|
|
16
|
+
}
|