@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 +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31 -29
- package/package.json +4 -4
- package/src/index.ts +62 -56
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
8
|
+
send(message: Message): void;
|
|
9
9
|
announceConnection(peerId: PeerId): void;
|
|
10
10
|
join(): void;
|
|
11
11
|
leave(): void;
|
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,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
|
|
23
|
-
if (
|
|
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
|
-
|
|
30
|
-
|
|
30
|
+
senderId: this.peerId,
|
|
31
|
+
targetId: senderId,
|
|
31
32
|
type: "welcome",
|
|
32
33
|
});
|
|
33
|
-
this.announceConnection(
|
|
34
|
+
this.announceConnection(senderId);
|
|
34
35
|
break;
|
|
35
36
|
case "welcome":
|
|
36
|
-
this.announceConnection(
|
|
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
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
17
|
+
"@automerge/automerge-repo": "^1.0.0-alpha.3"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@automerge/automerge": "^2.1.0-alpha.
|
|
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": "
|
|
33
|
+
"gitHead": "0ed108273084319aeea64ceccb49c3d58709f107"
|
|
34
34
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
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(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
e.data
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
|
|
105
|
+
senderId: this.peerId,
|
|
100
106
|
type: "arrive",
|
|
101
107
|
})
|
|
102
108
|
}
|