@automerge/automerge-repo-network-broadcastchannel 1.0.0-alpha.0 → 1.0.0-alpha.3

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 CHANGED
@@ -1,8 +1,8 @@
1
- import { ChannelId, NetworkAdapter, PeerId } from "@automerge/automerge-repo";
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
- sendMessage(peerId: PeerId, channelId: ChannelId, uint8message: Uint8Array, broadcast: boolean): void;
5
+ send(message: Message): void;
6
6
  join(): void;
7
7
  leave(): void;
8
8
  }
@@ -1 +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;IAQJ,KAAK;CAIN"}
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;IA6CtB,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 { senderId, targetId, type, channelId, message, broadcast } = e.data;
9
- if (targetId && targetId !== this.peerId && !broadcast) {
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,38 @@ 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
- throw new Error("unhandled message from network");
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
  });
37
38
  }
38
39
  #announceConnection(peerId) {
39
40
  this.emit("peer-candidate", { peerId });
40
41
  }
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
- });
42
+ send(message) {
43
+ if ("data" in message) {
44
+ this.#broadcastChannel.postMessage({
45
+ ...message,
46
+ data: message.data.buffer.slice(message.data.byteOffset, message.data.byteOffset + message.data.byteLength),
47
+ });
48
+ }
49
+ else {
50
+ this.#broadcastChannel.postMessage(message);
51
+ }
51
52
  }
52
53
  join() {
53
54
  this.#broadcastChannel.postMessage({
54
55
  senderId: this.peerId,
55
56
  type: "arrive",
56
- broadcast: true,
57
57
  });
58
58
  }
59
59
  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.0",
3
+ "version": "1.0.0-alpha.3",
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.9"
17
+ "@automerge/automerge": "^2.1.0-alpha.10"
18
18
  },
19
19
  "dependencies": {
20
- "@automerge/automerge-repo": "^1.0.0-alpha.0"
20
+ "@automerge/automerge-repo": "^1.0.0-alpha.3"
21
21
  },
22
22
  "watch": {
23
23
  "build": {
@@ -30,5 +30,5 @@
30
30
  "publishConfig": {
31
31
  "access": "public"
32
32
  },
33
- "gitHead": "38c0c32796ddca5f86a2e55ab0f1202a2ce107c8"
33
+ "gitHead": "0ed108273084319aeea64ceccb49c3d58709f107"
34
34
  }
package/src/index.ts CHANGED
@@ -1,4 +1,9 @@
1
- import { ChannelId, NetworkAdapter, PeerId } from "@automerge/automerge-repo"
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,65 @@ export class BroadcastChannelNetworkAdapter extends NetworkAdapter {
7
12
  this.peerId = peerId
8
13
  this.#broadcastChannel = new BroadcastChannel(`broadcast`)
9
14
 
10
- this.#broadcastChannel.addEventListener("message", e => {
11
- const { senderId, targetId, type, channelId, message, broadcast } = e.data
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
- if (targetId && targetId !== this.peerId && !broadcast) {
14
- return
15
- }
23
+ const { senderId, type } = message
16
24
 
17
- switch (type) {
18
- case "arrive":
19
- this.#broadcastChannel.postMessage({
20
- senderId: this.peerId,
21
- targetId: senderId,
22
- type: "welcome",
23
- })
24
- this.#announceConnection(senderId)
25
- break
26
- case "welcome":
27
- this.#announceConnection(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")
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
+ )
42
50
  }
43
51
 
44
52
  #announceConnection(peerId: PeerId) {
45
53
  this.emit("peer-candidate", { peerId })
46
54
  }
47
55
 
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
- })
56
+ send(message: Message) {
57
+ if ("data" in message) {
58
+ this.#broadcastChannel.postMessage({
59
+ ...message,
60
+ data: message.data.buffer.slice(
61
+ message.data.byteOffset,
62
+ message.data.byteOffset + message.data.byteLength
63
+ ),
64
+ })
65
+ } else {
66
+ this.#broadcastChannel.postMessage(message)
67
+ }
66
68
  }
67
69
 
68
70
  join() {
69
71
  this.#broadcastChannel.postMessage({
70
72
  senderId: this.peerId,
71
73
  type: "arrive",
72
- broadcast: true,
73
74
  })
74
75
  }
75
76