@automerge/automerge-repo-network-websocket 0.1.4-alpha.1 → 0.2.0

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.
@@ -0,0 +1,10 @@
1
+ /// <reference types="ws" />
2
+ import { ChannelId, NetworkAdapter, PeerId } from "automerge-repo";
3
+ import WebSocket from "isomorphic-ws";
4
+ export interface WebSocketNetworkAdapter extends NetworkAdapter {
5
+ client?: WebSocket;
6
+ }
7
+ export declare function sendMessage(destinationId: PeerId, socket: WebSocket, channelId: ChannelId, senderId: PeerId, message: Uint8Array): void;
8
+ export declare function receiveMessageClient(message: Uint8Array, self: WebSocketNetworkAdapter): void;
9
+ export declare function receiveMessageServer(message: Uint8Array, socket: WebSocket, self: WebSocketNetworkAdapter): void;
10
+ //# sourceMappingURL=WSShared.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WSShared.d.ts","sourceRoot":"","sources":["../src/WSShared.ts"],"names":[],"mappings":";AACA,OAAO,EACL,SAAS,EAET,cAAc,EAEd,MAAM,EACP,MAAM,gBAAgB,CAAA;AAEvB,OAAO,SAAS,MAAM,eAAe,CAAA;AAErC,MAAM,WAAW,uBAAwB,SAAQ,cAAc;IAC7D,MAAM,CAAC,EAAE,SAAS,CAAA;CACnB;AAED,wBAAgB,WAAW,CACzB,aAAa,EAAE,MAAM,EACrB,MAAM,EAAE,SAAS,EACjB,SAAS,EAAE,SAAS,EACpB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,UAAU,QAwBpB;AAiBD,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,UAAU,EACnB,IAAI,EAAE,uBAAuB,QAqC9B;AAED,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,UAAU,EACnB,MAAM,EAAE,SAAS,EACjB,IAAI,EAAE,uBAAuB,QAwC9B"}
@@ -0,0 +1,86 @@
1
+ import * as CBOR from "cbor-x";
2
+ import WebSocket from "isomorphic-ws";
3
+ export function sendMessage(destinationId, socket, channelId, senderId, message) {
4
+ if (message.byteLength === 0) {
5
+ throw new Error("tried to send a zero-length message");
6
+ }
7
+ const decoded = {
8
+ senderId,
9
+ channelId,
10
+ type: "sync",
11
+ data: message,
12
+ };
13
+ const encoded = CBOR.encode(decoded);
14
+ // This incantation deals with websocket sending the whole
15
+ // underlying buffer even if we just have a uint8array view on it
16
+ const arrayBuf = encoded.buffer.slice(encoded.byteOffset, encoded.byteOffset + encoded.byteLength);
17
+ console.log(`[${senderId}->${destinationId}@${channelId}] "sync" | ${arrayBuf.byteLength} bytes`);
18
+ socket.send(arrayBuf);
19
+ }
20
+ function prepareConnection(channelId, destinationId, socket, sourceId) {
21
+ const connection = {
22
+ close: () => socket.close(),
23
+ isOpen: () => socket.readyState === WebSocket.OPEN,
24
+ send: (message) => sendMessage(destinationId, socket, channelId, sourceId, message),
25
+ };
26
+ return connection;
27
+ }
28
+ export function receiveMessageClient(message, self) {
29
+ const decoded = CBOR.decode(new Uint8Array(message));
30
+ const { type, senderId, channelId, data } = decoded;
31
+ const socket = self.client;
32
+ if (!socket) {
33
+ throw new Error("Missing client at receiveMessage");
34
+ }
35
+ if (message.byteLength === 0) {
36
+ throw new Error("received a zero-length message");
37
+ }
38
+ switch (type) {
39
+ case "peer":
40
+ // console.log(`peer: ${senderId}, ${channelId}`)
41
+ const myPeerId = self.peerId;
42
+ if (!myPeerId) {
43
+ throw new Error("Local peer ID not set!");
44
+ }
45
+ const connection = prepareConnection(channelId, senderId, socket, myPeerId);
46
+ self.emit("peer-candidate", { peerId: senderId, channelId, connection });
47
+ break;
48
+ default:
49
+ self.emit("message", {
50
+ channelId,
51
+ senderId,
52
+ message: new Uint8Array(data),
53
+ });
54
+ }
55
+ }
56
+ export function receiveMessageServer(message, socket, self) {
57
+ const cbor = CBOR.decode(message);
58
+ const { type, channelId, senderId, data } = cbor;
59
+ const myPeerId = self.peerId;
60
+ if (!myPeerId) {
61
+ throw new Error("Missing my peer ID.");
62
+ }
63
+ console.log(`[${senderId}->${myPeerId}@${channelId}] ${type} | ${message.byteLength} bytes`);
64
+ switch (type) {
65
+ case "join":
66
+ // Let the rest of the system know that we have a new connection.
67
+ const connection = prepareConnection(channelId, senderId, socket, myPeerId);
68
+ self.emit("peer-candidate", { peerId: senderId, channelId, connection });
69
+ // In this client-server connection, there's only ever one peer: us!
70
+ socket.send(CBOR.encode({ type: "peer", senderId: self.peerId, channelId }));
71
+ break;
72
+ case "leave":
73
+ // ?
74
+ break;
75
+ case "sync":
76
+ self.emit("message", {
77
+ senderId,
78
+ channelId,
79
+ message: new Uint8Array(data),
80
+ });
81
+ break;
82
+ default:
83
+ // console.log("unrecognized message type")
84
+ break;
85
+ }
86
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automerge/automerge-repo-network-websocket",
3
- "version": "0.1.4-alpha.1",
3
+ "version": "0.2.0",
4
4
  "description": "isomorphic node/browser Websocket network adapter for Automerge Repo",
5
5
  "repository": "https://github.com/automerge/automerge-repo",
6
6
  "author": "Peter van Hardenberg <pvh@pvh.ca>",
@@ -14,7 +14,7 @@
14
14
  "test": "mocha --no-warnings --experimental-specifier-resolution=node --exit"
15
15
  },
16
16
  "dependencies": {
17
- "@automerge/automerge-repo": "^0.1.4-alpha.1",
17
+ "@automerge/automerge-repo": "^0.2.0",
18
18
  "cbor-x": "^1.3.0",
19
19
  "eventemitter3": "^4.0.7",
20
20
  "isomorphic-ws": "^5.0.0"
@@ -30,5 +30,5 @@
30
30
  "publishConfig": {
31
31
  "access": "public"
32
32
  },
33
- "gitHead": "10312fcdf52f56a4834236a45f7814886cd3cc56"
33
+ "gitHead": "b17ea68dce605c06e57e4fcd6e6295ef968a8b6d"
34
34
  }