@automerge/automerge-repo-network-messagechannel 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,11 +1,11 @@
1
- import { ChannelId, NetworkAdapter, PeerId } from "@automerge/automerge-repo";
1
+ import { type Message, NetworkAdapter, type PeerId } from "@automerge/automerge-repo";
2
2
  import { MessagePortRef } from "./MessagePortRef.js";
3
3
  export declare class MessageChannelNetworkAdapter extends NetworkAdapter {
4
4
  channels: {};
5
5
  messagePortRef: MessagePortRef;
6
6
  constructor(messagePort: MessagePort, config?: MessageChannelNetworkAdapterConfig);
7
7
  connect(peerId: PeerId): void;
8
- sendMessage(peerId: PeerId, channelId: ChannelId, uint8message: Uint8Array, broadcast: boolean): void;
8
+ send(message: Message): void;
9
9
  announceConnection(peerId: PeerId): void;
10
10
  join(): void;
11
11
  leave(): void;
@@ -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;AAC7E,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAOpD,qBAAa,4BAA6B,SAAQ,cAAc;IAC9D,QAAQ,KAAK;IACb,cAAc,EAAE,cAAc,CAAA;gBAG5B,WAAW,EAAE,WAAW,EACxB,MAAM,GAAE,kCAAuC;IAWjD,OAAO,CAAC,MAAM,EAAE,MAAM;IA4CtB,WAAW,CACT,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,SAAS,EACpB,YAAY,EAAE,UAAU,EACxB,SAAS,EAAE,OAAO;IAmBpB,kBAAkB,CAAC,MAAM,EAAE,MAAM;IAIjC,IAAI;IAOJ,KAAK;CAIN;AAED,UAAU,kCAAkC;IAC1C;;;;;;;OAOG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,OAAO,EACZ,cAAc,EACd,KAAK,MAAM,EAEZ,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAOpD,qBAAa,4BAA6B,SAAQ,cAAc;IAC9D,QAAQ,KAAK;IACb,cAAc,EAAE,cAAc,CAAA;gBAG5B,WAAW,EAAE,WAAW,EACxB,MAAM,GAAE,kCAAuC;IAWjD,OAAO,CAAC,MAAM,EAAE,MAAM;IAiDtB,IAAI,CAAC,OAAO,EAAE,OAAO;IAmBrB,kBAAkB,CAAC,MAAM,EAAE,MAAM;IAIjC,IAAI;IAOJ,KAAK;CAIN;AAED,UAAU,kCAAkC;IAC1C;;;;;;;OAOG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB"}
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { NetworkAdapter } from "@automerge/automerge-repo";
1
+ import { NetworkAdapter, } from "@automerge/automerge-repo";
2
2
  import { StrongMessagePortRef } from "./StrongMessagePortRef.js";
3
3
  import { WeakMessagePortRef } from "./WeakMessagePortRef.js";
4
4
  import debug from "debug";
@@ -17,58 +17,60 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
17
17
  log("messageport connecting");
18
18
  this.peerId = peerId;
19
19
  this.messagePortRef.start();
20
- this.messagePortRef.addListener("message", e => {
20
+ this.messagePortRef.addListener("message", (e) => {
21
21
  log("message port received", e.data);
22
- const { origin, destination, type, channelId, message, broadcast } = e.data;
23
- if (destination && !(destination === this.peerId || broadcast)) {
22
+ const message = e.data;
23
+ if ("targetId" in message && message.targetId !== this.peerId) {
24
24
  throw new Error("MessagePortNetwork should never receive messages for a different peer.");
25
25
  }
26
+ const { senderId, type } = message;
26
27
  switch (type) {
27
28
  case "arrive":
28
29
  this.messagePortRef.postMessage({
29
- origin: this.peerId,
30
- destination: origin,
30
+ senderId: this.peerId,
31
+ targetId: senderId,
31
32
  type: "welcome",
32
33
  });
33
- this.announceConnection(origin);
34
+ this.announceConnection(senderId);
34
35
  break;
35
36
  case "welcome":
36
- this.announceConnection(origin);
37
- break;
38
- case "message":
39
- this.emit("message", {
40
- senderId: origin,
41
- targetId: destination,
42
- channelId,
43
- message: new Uint8Array(message),
44
- broadcast,
45
- });
37
+ this.announceConnection(senderId);
46
38
  break;
47
39
  default:
48
- throw new Error("unhandled message from network");
40
+ if (!("data" in message)) {
41
+ this.emit("message", message);
42
+ }
43
+ else {
44
+ this.emit("message", {
45
+ ...message,
46
+ data: new Uint8Array(message.data),
47
+ });
48
+ }
49
+ break;
49
50
  }
50
51
  });
51
52
  this.messagePortRef.addListener("close", () => {
52
53
  this.emit("close");
53
54
  });
54
55
  }
55
- sendMessage(peerId, channelId, uint8message, broadcast) {
56
- const message = uint8message.buffer.slice(uint8message.byteOffset, uint8message.byteOffset + uint8message.byteLength);
57
- this.messagePortRef.postMessage({
58
- origin: this.peerId,
59
- destination: peerId,
60
- channelId: channelId,
61
- type: "message",
62
- message,
63
- broadcast,
64
- }, [message]);
56
+ send(message) {
57
+ if ("data" in message) {
58
+ const data = message.data.buffer.slice(message.data.byteOffset, message.data.byteOffset + message.data.byteLength);
59
+ this.messagePortRef.postMessage({
60
+ ...message,
61
+ data,
62
+ }, [data]);
63
+ }
64
+ else {
65
+ this.messagePortRef.postMessage(message);
66
+ }
65
67
  }
66
68
  announceConnection(peerId) {
67
69
  this.emit("peer-candidate", { peerId });
68
70
  }
69
71
  join() {
70
72
  this.messagePortRef.postMessage({
71
- origin: this.peerId,
73
+ senderId: this.peerId,
72
74
  type: "arrive",
73
75
  });
74
76
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automerge/automerge-repo-network-messagechannel",
3
- "version": "1.0.0-alpha.0",
3
+ "version": "1.0.0-alpha.3",
4
4
  "description": "MessageChannel 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
  "dependencies": {
17
- "@automerge/automerge-repo": "^1.0.0-alpha.0"
17
+ "@automerge/automerge-repo": "^1.0.0-alpha.3"
18
18
  },
19
19
  "peerDependencies": {
20
- "@automerge/automerge": "^2.1.0-alpha.9"
20
+ "@automerge/automerge": "^2.1.0-alpha.10"
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
+ type Message,
3
+ NetworkAdapter,
4
+ type PeerId,
5
+ NetworkAdapterMessage,
6
+ } from "@automerge/automerge-repo"
2
7
  import { MessagePortRef } from "./MessagePortRef.js"
3
8
  import { StrongMessagePortRef } from "./StrongMessagePortRef.js"
4
9
  import { WeakMessagePortRef } from "./WeakMessagePortRef.js"
@@ -27,67 +32,68 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
27
32
  log("messageport connecting")
28
33
  this.peerId = peerId
29
34
  this.messagePortRef.start()
30
- this.messagePortRef.addListener("message", e => {
31
- log("message port received", e.data)
32
- const { origin, destination, type, channelId, message, broadcast } =
33
- e.data
34
- if (destination && !(destination === this.peerId || broadcast)) {
35
- throw new Error(
36
- "MessagePortNetwork should never receive messages for a different peer."
37
- )
38
- }
39
- switch (type) {
40
- case "arrive":
41
- this.messagePortRef.postMessage({
42
- origin: this.peerId,
43
- destination: origin,
44
- type: "welcome",
45
- })
46
- this.announceConnection(origin)
47
- break
48
- case "welcome":
49
- this.announceConnection(origin)
50
- break
51
- case "message":
52
- this.emit("message", {
53
- senderId: origin,
54
- targetId: destination,
55
- channelId,
56
- message: new Uint8Array(message),
57
- broadcast,
58
- })
59
- break
60
- default:
61
- throw new Error("unhandled message from network")
35
+ this.messagePortRef.addListener(
36
+ "message",
37
+ (e: { data: NetworkAdapterMessage }) => {
38
+ log("message port received", e.data)
39
+
40
+ const message = e.data
41
+ if ("targetId" in message && message.targetId !== this.peerId) {
42
+ throw new Error(
43
+ "MessagePortNetwork should never receive messages for a different peer."
44
+ )
45
+ }
46
+
47
+ const { senderId, type } = message
48
+
49
+ switch (type) {
50
+ case "arrive":
51
+ this.messagePortRef.postMessage({
52
+ senderId: this.peerId,
53
+ targetId: senderId,
54
+ type: "welcome",
55
+ })
56
+ this.announceConnection(senderId)
57
+ break
58
+ case "welcome":
59
+ this.announceConnection(senderId)
60
+ break
61
+ default:
62
+ if (!("data" in message)) {
63
+ this.emit("message", message)
64
+ } else {
65
+ this.emit("message", {
66
+ ...message,
67
+ data: new Uint8Array(message.data),
68
+ })
69
+ }
70
+ break
71
+ }
62
72
  }
63
- })
73
+ )
64
74
 
65
75
  this.messagePortRef.addListener("close", () => {
66
76
  this.emit("close")
67
77
  })
68
78
  }
69
79
 
70
- sendMessage(
71
- peerId: PeerId,
72
- channelId: ChannelId,
73
- uint8message: Uint8Array,
74
- broadcast: boolean
75
- ) {
76
- const message = uint8message.buffer.slice(
77
- uint8message.byteOffset,
78
- uint8message.byteOffset + uint8message.byteLength
79
- )
80
- this.messagePortRef.postMessage(
81
- {
82
- origin: this.peerId,
83
- destination: peerId,
84
- channelId: channelId,
85
- type: "message",
86
- message,
87
- broadcast,
88
- },
89
- [message]
90
- )
80
+ send(message: Message) {
81
+ if ("data" in message) {
82
+ const data = message.data.buffer.slice(
83
+ message.data.byteOffset,
84
+ message.data.byteOffset + message.data.byteLength
85
+ )
86
+
87
+ this.messagePortRef.postMessage(
88
+ {
89
+ ...message,
90
+ data,
91
+ },
92
+ [data]
93
+ )
94
+ } else {
95
+ this.messagePortRef.postMessage(message)
96
+ }
91
97
  }
92
98
 
93
99
  announceConnection(peerId: PeerId) {
@@ -96,7 +102,7 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
96
102
 
97
103
  join() {
98
104
  this.messagePortRef.postMessage({
99
- origin: this.peerId,
105
+ senderId: this.peerId,
100
106
  type: "arrive",
101
107
  })
102
108
  }