@automerge/automerge-repo-network-messagechannel 1.0.19 → 1.1.0-alpha.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/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  *
6
6
  * @module
7
7
  */
8
- import { type RepoMessage, NetworkAdapter, type PeerId } from "@automerge/automerge-repo";
8
+ import { type RepoMessage, NetworkAdapter, type PeerId, type StorageId } from "@automerge/automerge-repo";
9
9
  import { MessagePortRef } from "./MessagePortRef.js";
10
10
  export declare class MessageChannelNetworkAdapter extends NetworkAdapter {
11
11
  #private;
@@ -13,9 +13,9 @@ export declare class MessageChannelNetworkAdapter extends NetworkAdapter {
13
13
  /** @hidden */
14
14
  messagePortRef: MessagePortRef;
15
15
  constructor(messagePort: MessagePort, config?: MessageChannelNetworkAdapterConfig);
16
- connect(peerId: PeerId): void;
16
+ connect(peerId: PeerId, storageId: StorageId | undefined, isEphemeral: boolean): void;
17
17
  send(message: RepoMessage): void;
18
- announceConnection(peerId: PeerId): void;
18
+ announceConnection(peerId: PeerId, storageId: StorageId | undefined, isEphemeral: boolean): void;
19
19
  disconnect(): void;
20
20
  }
21
21
  export interface MessageChannelNetworkAdapterConfig {
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EACL,KAAK,WAAW,EAChB,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;IACd,cAAc,EAAE,cAAc,CAAA;gBAI5B,WAAW,EAAE,WAAW,EACxB,MAAM,GAAE,kCAAuC;IAWjD,OAAO,CAAC,MAAM,EAAE,MAAM;IAgEtB,IAAI,CAAC,OAAO,EAAE,WAAW;IAmBzB,kBAAkB,CAAC,MAAM,EAAE,MAAM;IAQjC,UAAU;CAIX;AAED,MAAM,WAAW,kCAAkC;IACjD;;;;;;;OAOG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EACL,KAAK,WAAW,EAChB,cAAc,EACd,KAAK,MAAM,EAEX,KAAK,SAAS,EACf,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAOpD,qBAAa,4BAA6B,SAAQ,cAAc;;IAC9D,QAAQ,KAAK;IACb,cAAc;IACd,cAAc,EAAE,cAAc,CAAA;gBAI5B,WAAW,EAAE,WAAW,EACxB,MAAM,GAAE,kCAAuC;IAWjD,OAAO,CACL,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,SAAS,GAAG,SAAS,EAChC,WAAW,EAAE,OAAO;IA2EtB,IAAI,CAAC,OAAO,EAAE,WAAW;IAmBzB,kBAAkB,CAChB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,SAAS,GAAG,SAAS,EAChC,WAAW,EAAE,OAAO;IAStB,UAAU;CAIX;AAED,MAAM,WAAW,kCAAkC;IACjD;;;;;;;OAOG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB"}
package/dist/index.js CHANGED
@@ -22,9 +22,11 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
22
22
  ? new WeakMessagePortRef(messagePort)
23
23
  : new StrongMessagePortRef(messagePort);
24
24
  }
25
- connect(peerId) {
25
+ connect(peerId, storageId, isEphemeral) {
26
26
  log("messageport connecting");
27
27
  this.peerId = peerId;
28
+ this.storageId = storageId;
29
+ this.isEphemeral = isEphemeral;
28
30
  this.messagePortRef.start();
29
31
  this.messagePortRef.addListener("message", (e) => {
30
32
  log("message port received", e.data);
@@ -35,15 +37,23 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
35
37
  const { senderId, type } = message;
36
38
  switch (type) {
37
39
  case "arrive":
38
- this.messagePortRef.postMessage({
39
- senderId: this.peerId,
40
- targetId: senderId,
41
- type: "welcome",
42
- });
43
- this.announceConnection(senderId);
40
+ {
41
+ const { storageId, isEphemeral } = message;
42
+ this.messagePortRef.postMessage({
43
+ senderId: this.peerId,
44
+ storageId: this.storageId,
45
+ isEphemeral: this.isEphemeral,
46
+ targetId: senderId,
47
+ type: "welcome",
48
+ });
49
+ this.announceConnection(senderId, storageId, isEphemeral);
50
+ }
44
51
  break;
45
52
  case "welcome":
46
- this.announceConnection(senderId);
53
+ {
54
+ const { storageId, isEphemeral } = message;
55
+ this.announceConnection(senderId, storageId, isEphemeral);
56
+ }
47
57
  break;
48
58
  default:
49
59
  if (!("data" in message)) {
@@ -87,12 +97,12 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
87
97
  this.messagePortRef.postMessage(message);
88
98
  }
89
99
  }
90
- announceConnection(peerId) {
100
+ announceConnection(peerId, storageId, isEphemeral) {
91
101
  if (!this.#startupComplete) {
92
102
  this.#startupComplete = true;
93
103
  this.emit("ready", { network: this });
94
104
  }
95
- this.emit("peer-candidate", { peerId });
105
+ this.emit("peer-candidate", { peerId, storageId, isEphemeral });
96
106
  }
97
107
  disconnect() {
98
108
  // TODO
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automerge/automerge-repo-network-messagechannel",
3
- "version": "1.0.19",
3
+ "version": "1.1.0-alpha.1",
4
4
  "description": "MessageChannel network adapter for Automerge Repo",
5
5
  "repository": "https://github.com/automerge/automerge-repo/tree/master/packages/automerge-repo-network-messagechannel",
6
6
  "author": "Peter van Hardenberg <pvh@pvh.ca>",
@@ -13,7 +13,7 @@
13
13
  "test": "vitest"
14
14
  },
15
15
  "dependencies": {
16
- "@automerge/automerge-repo": "^1.0.19"
16
+ "@automerge/automerge-repo": "^1.1.0-alpha.1"
17
17
  },
18
18
  "watch": {
19
19
  "build": {
@@ -26,5 +26,5 @@
26
26
  "publishConfig": {
27
27
  "access": "public"
28
28
  },
29
- "gitHead": "7d28ca50dfa437ac6f7b1722b89b3f6844b90de7"
29
+ "gitHead": "11805d698f860bd6ffb3ca028d3b57e718690b5a"
30
30
  }
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@ import {
10
10
  NetworkAdapter,
11
11
  type PeerId,
12
12
  type Message,
13
+ type StorageId,
13
14
  } from "@automerge/automerge-repo"
14
15
  import { MessagePortRef } from "./MessagePortRef.js"
15
16
  import { StrongMessagePortRef } from "./StrongMessagePortRef.js"
@@ -37,9 +38,15 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
37
38
  : new StrongMessagePortRef(messagePort)
38
39
  }
39
40
 
40
- connect(peerId: PeerId) {
41
+ connect(
42
+ peerId: PeerId,
43
+ storageId: StorageId | undefined,
44
+ isEphemeral: boolean
45
+ ) {
41
46
  log("messageport connecting")
42
47
  this.peerId = peerId
48
+ this.storageId = storageId
49
+ this.isEphemeral = isEphemeral
43
50
  this.messagePortRef.start()
44
51
  this.messagePortRef.addListener(
45
52
  "message",
@@ -57,15 +64,23 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
57
64
 
58
65
  switch (type) {
59
66
  case "arrive":
60
- this.messagePortRef.postMessage({
61
- senderId: this.peerId,
62
- targetId: senderId,
63
- type: "welcome",
64
- })
65
- this.announceConnection(senderId)
67
+ {
68
+ const { storageId, isEphemeral } = message
69
+ this.messagePortRef.postMessage({
70
+ senderId: this.peerId,
71
+ storageId: this.storageId,
72
+ isEphemeral: this.isEphemeral,
73
+ targetId: senderId,
74
+ type: "welcome",
75
+ })
76
+ this.announceConnection(senderId, storageId, isEphemeral)
77
+ }
66
78
  break
67
79
  case "welcome":
68
- this.announceConnection(senderId)
80
+ {
81
+ const { storageId, isEphemeral } = message
82
+ this.announceConnection(senderId, storageId, isEphemeral)
83
+ }
69
84
  break
70
85
  default:
71
86
  if (!("data" in message)) {
@@ -120,12 +135,16 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
120
135
  }
121
136
  }
122
137
 
123
- announceConnection(peerId: PeerId) {
138
+ announceConnection(
139
+ peerId: PeerId,
140
+ storageId: StorageId | undefined,
141
+ isEphemeral: boolean
142
+ ) {
124
143
  if (!this.#startupComplete) {
125
144
  this.#startupComplete = true
126
145
  this.emit("ready", { network: this })
127
146
  }
128
- this.emit("peer-candidate", { peerId })
147
+ this.emit("peer-candidate", { peerId, storageId, isEphemeral })
129
148
  }
130
149
 
131
150
  disconnect() {
@@ -153,6 +172,13 @@ type ArriveMessage = {
153
172
  /** The peer ID of the sender of this message */
154
173
  senderId: PeerId
155
174
 
175
+ /** Unique ID of the storage that the sender peer is using, is persistent across sessions */
176
+ storageId?: StorageId
177
+
178
+ /** Indicates whether other peers should persist the sync state of the sender peer.
179
+ * Sync state is only persisted for non-ephemeral peers */
180
+ isEphemeral: boolean
181
+
156
182
  /** Arrive messages don't have a targetId */
157
183
  targetId: never
158
184
  }
@@ -164,6 +190,13 @@ type WelcomeMessage = {
164
190
  /** The peer ID of the recipient sender this message */
165
191
  senderId: PeerId
166
192
 
193
+ /** Unique ID of the storage that the sender peer is using, is persistent across sessions */
194
+ storageId?: StorageId
195
+
196
+ /** Indicates whether other peers should persist the sync state of the sender peer.
197
+ * Sync state is only persisted for non-ephemeral peers */
198
+ isEphemeral: boolean
199
+
167
200
  /** The peer ID of the recipient of this message */
168
201
  targetId: PeerId
169
202
  }