@automerge/automerge-repo-network-websocket 2.0.0-alpha.7 → 2.0.0-beta.2
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/README.md +3 -3
- package/dist/{BrowserWebSocketClientAdapter.d.ts → WebSocketClientAdapter.d.ts} +2 -2
- package/dist/WebSocketClientAdapter.d.ts.map +1 -0
- package/dist/{BrowserWebSocketClientAdapter.js → WebSocketClientAdapter.js} +1 -1
- package/dist/{NodeWSServerAdapter.d.ts → WebSocketServerAdapter.d.ts} +2 -2
- package/dist/WebSocketServerAdapter.d.ts.map +1 -0
- package/dist/{NodeWSServerAdapter.js → WebSocketServerAdapter.js} +10 -2
- package/dist/index.d.ts +8 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -4
- package/dist/messages.d.ts.map +1 -1
- package/dist/toArrayBuffer.d.ts +1 -1
- package/dist/toArrayBuffer.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/{BrowserWebSocketClientAdapter.ts → WebSocketClientAdapter.ts} +1 -1
- package/src/{NodeWSServerAdapter.ts → WebSocketServerAdapter.ts} +9 -2
- package/src/index.ts +11 -4
- package/test/Websocket.test.ts +56 -17
- package/dist/BrowserWebSocketClientAdapter.d.ts.map +0 -1
- package/dist/NodeWSServerAdapter.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -27,12 +27,12 @@ Handshake is the following steps:
|
|
|
27
27
|
|
|
28
28
|
- Once a connection is established the initiating peer sends a
|
|
29
29
|
[join](#join) message with the `senderId` set to the initiating peers ID and
|
|
30
|
-
|
|
30
|
+
a `supportedProtocolVersions` array containing "1"
|
|
31
31
|
- The receiving peer waits until it receives a message from the initiating
|
|
32
32
|
peer, if the initiating peer receives a message before sending the join message
|
|
33
33
|
the initiating peer SHOULD terminate the connection.
|
|
34
34
|
- When the receiving peer receives the join message
|
|
35
|
-
- if the `
|
|
35
|
+
- if the `supportedProtocolVersions` does not contain "1" the receiving peer sends an
|
|
36
36
|
[error](#error) message and terminates the connection
|
|
37
37
|
- otherwise
|
|
38
38
|
- store the `senderId` as the peer ID of the initiating peer
|
|
@@ -119,7 +119,7 @@ Sent by the initiating peer in the [handshake](#handshake) phase.
|
|
|
119
119
|
{
|
|
120
120
|
type: "join",
|
|
121
121
|
senderId: peer_id,
|
|
122
|
-
supportedProtocolVersions: protocol_version
|
|
122
|
+
supportedProtocolVersions: [protocol_version]
|
|
123
123
|
? metadata: peer_metadata,
|
|
124
124
|
}
|
|
125
125
|
```
|
|
@@ -4,7 +4,7 @@ import { FromClientMessage } from "./messages.js";
|
|
|
4
4
|
declare abstract class WebSocketNetworkAdapter extends NetworkAdapter {
|
|
5
5
|
socket?: WebSocket;
|
|
6
6
|
}
|
|
7
|
-
export declare class
|
|
7
|
+
export declare class WebSocketClientAdapter extends WebSocketNetworkAdapter {
|
|
8
8
|
#private;
|
|
9
9
|
readonly url: string;
|
|
10
10
|
readonly retryInterval: number;
|
|
@@ -25,4 +25,4 @@ export declare class BrowserWebSocketClientAdapter extends WebSocketNetworkAdapt
|
|
|
25
25
|
receiveMessage(messageBytes: Uint8Array): void;
|
|
26
26
|
}
|
|
27
27
|
export {};
|
|
28
|
-
//# sourceMappingURL=
|
|
28
|
+
//# sourceMappingURL=WebSocketClientAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebSocketClientAdapter.d.ts","sourceRoot":"","sources":["../src/WebSocketClientAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,MAAM,EACN,YAAY,EAEb,MAAM,gCAAgC,CAAA;AACvC,OAAO,SAAS,MAAM,eAAe,CAAA;AAIrC,OAAO,EACL,iBAAiB,EAKlB,MAAM,eAAe,CAAA;AAKtB,uBAAe,uBAAwB,SAAQ,cAAc;IAC3D,MAAM,CAAC,EAAE,SAAS,CAAA;CACnB;AAED,qBAAa,sBAAuB,SAAQ,uBAAuB;;aA4B/C,GAAG,EAAE,MAAM;aACX,aAAa;IAtB/B,OAAO;IAIP,SAAS;IAcT,YAAY,CAAC,EAAE,MAAM,CAAA;gBAGH,GAAG,EAAE,MAAM,EACX,aAAa,SAAO;IAMtC,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,YAAY;IAqCnD,MAAM,aAKL;IAGD,OAAO,aAWN;IAED,SAAS,GAAI,OAAO,SAAS,CAAC,YAAY,UAEzC;IAED,mFAAmF;IACnF,OAAO,GACL,OACI,KAAK,GACL,SAAS,CAAC,UAAU,UAezB;IAED,IAAI;IAUJ,UAAU;IAiBV,IAAI,CAAC,OAAO,EAAE,iBAAiB;IAe/B,aAAa,CAAC,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY;IAU9D,cAAc,CAAC,YAAY,EAAE,UAAU;CAiBxC"}
|
|
@@ -8,7 +8,7 @@ import { toArrayBuffer } from "./toArrayBuffer.js";
|
|
|
8
8
|
class WebSocketNetworkAdapter extends NetworkAdapter {
|
|
9
9
|
socket;
|
|
10
10
|
}
|
|
11
|
-
export class
|
|
11
|
+
export class WebSocketClientAdapter extends WebSocketNetworkAdapter {
|
|
12
12
|
url;
|
|
13
13
|
retryInterval;
|
|
14
14
|
#ready = false;
|
|
@@ -2,7 +2,7 @@ import WebSocket from "isomorphic-ws";
|
|
|
2
2
|
import { type WebSocketServer } from "isomorphic-ws";
|
|
3
3
|
import { NetworkAdapter, type PeerMetadata, type PeerId } from "@automerge/automerge-repo/slim";
|
|
4
4
|
import { FromServerMessage } from "./messages.js";
|
|
5
|
-
export declare class
|
|
5
|
+
export declare class WebSocketServerAdapter extends NetworkAdapter {
|
|
6
6
|
#private;
|
|
7
7
|
private server;
|
|
8
8
|
private keepAliveInterval;
|
|
@@ -17,4 +17,4 @@ export declare class NodeWSServerAdapter extends NetworkAdapter {
|
|
|
17
17
|
send(message: FromServerMessage): void;
|
|
18
18
|
receiveMessage(messageBytes: Uint8Array, socket: WebSocket): void;
|
|
19
19
|
}
|
|
20
|
-
//# sourceMappingURL=
|
|
20
|
+
//# sourceMappingURL=WebSocketServerAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebSocketServerAdapter.d.ts","sourceRoot":"","sources":["../src/WebSocketServerAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,eAAe,CAAA;AACrC,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,eAAe,CAAA;AAKpD,OAAO,EAEL,cAAc,EACd,KAAK,YAAY,EACjB,KAAK,MAAM,EACZ,MAAM,gCAAgC,CAAA;AACvC,OAAO,EAEL,iBAAiB,EAElB,MAAM,eAAe,CAAA;AAOtB,qBAAa,sBAAuB,SAAQ,cAAc;;IAyBtD,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,iBAAiB;IAzB3B,OAAO,EAAE;QAAE,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAK;IAQ7C,OAAO;IAIP,SAAS;gBAYC,MAAM,EAAE,eAAe,EACvB,iBAAiB,SAAO;IAKlC,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,YAAY;IAyCnD,UAAU;IAQV,IAAI,CAAC,OAAO,EAAE,iBAAiB;IAqB/B,cAAc,CAAC,YAAY,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS;CA2E3D"}
|
|
@@ -7,7 +7,7 @@ import { ProtocolV1 } from "./protocolVersion.js";
|
|
|
7
7
|
import { assert } from "./assert.js";
|
|
8
8
|
import { toArrayBuffer } from "./toArrayBuffer.js";
|
|
9
9
|
const { encode, decode } = cborHelpers;
|
|
10
|
-
export class
|
|
10
|
+
export class WebSocketServerAdapter extends NetworkAdapter {
|
|
11
11
|
server;
|
|
12
12
|
keepAliveInterval;
|
|
13
13
|
sockets = {};
|
|
@@ -89,7 +89,15 @@ export class NodeWSServerAdapter extends NetworkAdapter {
|
|
|
89
89
|
socket.send(arrayBuf);
|
|
90
90
|
}
|
|
91
91
|
receiveMessage(messageBytes, socket) {
|
|
92
|
-
|
|
92
|
+
let message;
|
|
93
|
+
try {
|
|
94
|
+
message = decode(messageBytes);
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
log("invalid message received, closing connection");
|
|
98
|
+
socket.close();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
93
101
|
const { type, senderId } = message;
|
|
94
102
|
const myPeerId = this.peerId;
|
|
95
103
|
assert(myPeerId);
|
package/dist/index.d.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* A `NetworkAdapter` which connects to a remote host via WebSockets
|
|
3
3
|
*
|
|
4
4
|
* The websocket protocol requires a server to be listening and a client to
|
|
5
|
-
* connect to the server. To that end the {@link
|
|
5
|
+
* connect to the server. To that end the {@link WebSocketServerAdapter} does not
|
|
6
6
|
* make outbound connections and instead listens on the provided socket for
|
|
7
|
-
* new connections whilst the {@link
|
|
7
|
+
* new connections whilst the {@link WebSocketClientAdapter} makes an
|
|
8
8
|
* outbound connection to the provided socket.
|
|
9
9
|
*
|
|
10
10
|
* Note that the "browser" and "node" naming is a bit misleading, both
|
|
@@ -12,8 +12,12 @@
|
|
|
12
12
|
*
|
|
13
13
|
* @module
|
|
14
14
|
* */
|
|
15
|
-
export {
|
|
16
|
-
export {
|
|
15
|
+
export { WebSocketClientAdapter } from "./WebSocketClientAdapter.js";
|
|
16
|
+
export { WebSocketServerAdapter } from "./WebSocketServerAdapter.js";
|
|
17
|
+
/** @hidden */
|
|
18
|
+
export { WebSocketClientAdapter as BrowserWebSocketClientAdapter } from "./WebSocketClientAdapter.js";
|
|
19
|
+
/** @hidden */
|
|
20
|
+
export { WebSocketServerAdapter as NodeWSServerAdapter } from "./WebSocketServerAdapter.js";
|
|
17
21
|
export type { FromClientMessage, FromServerMessage, JoinMessage, ErrorMessage, PeerMessage, } from "./messages.js";
|
|
18
22
|
export type { ProtocolVersion } from "./protocolVersion.js";
|
|
19
23
|
export { ProtocolV1 } from "./protocolVersion.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;KAaK;AACL,OAAO,EAAE,6BAA6B,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;KAaK;AACL,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAA;AACpE,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAA;AAEpE,cAAc;AACd,OAAO,EAAE,sBAAsB,IAAI,6BAA6B,EAAE,MAAM,6BAA6B,CAAA;AAErG,cAAc;AACd,OAAO,EAAE,sBAAsB,IAAI,mBAAmB,EAAE,MAAM,6BAA6B,CAAA;AAE3F,YAAY,EACV,iBAAiB,EACjB,iBAAiB,EACjB,WAAW,EACX,YAAY,EACZ,WAAW,GACZ,MAAM,eAAe,CAAA;AACtB,YAAY,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* A `NetworkAdapter` which connects to a remote host via WebSockets
|
|
3
3
|
*
|
|
4
4
|
* The websocket protocol requires a server to be listening and a client to
|
|
5
|
-
* connect to the server. To that end the {@link
|
|
5
|
+
* connect to the server. To that end the {@link WebSocketServerAdapter} does not
|
|
6
6
|
* make outbound connections and instead listens on the provided socket for
|
|
7
|
-
* new connections whilst the {@link
|
|
7
|
+
* new connections whilst the {@link WebSocketClientAdapter} makes an
|
|
8
8
|
* outbound connection to the provided socket.
|
|
9
9
|
*
|
|
10
10
|
* Note that the "browser" and "node" naming is a bit misleading, both
|
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
*
|
|
13
13
|
* @module
|
|
14
14
|
* */
|
|
15
|
-
export {
|
|
16
|
-
export {
|
|
15
|
+
export { WebSocketClientAdapter } from "./WebSocketClientAdapter.js";
|
|
16
|
+
export { WebSocketServerAdapter } from "./WebSocketServerAdapter.js";
|
|
17
|
+
/** @hidden */
|
|
18
|
+
export { WebSocketClientAdapter as BrowserWebSocketClientAdapter } from "./WebSocketClientAdapter.js";
|
|
19
|
+
/** @hidden */
|
|
20
|
+
export { WebSocketServerAdapter as NodeWSServerAdapter } from "./WebSocketServerAdapter.js";
|
|
17
21
|
export { ProtocolV1 } from "./protocolVersion.js";
|
package/dist/messages.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,MAAM,EACN,YAAY,EACb,MAAM,gCAAgC,CAAA;AACvC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAE3D,6EAA6E;AAC7E,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,+BAA+B;IAC/B,QAAQ,EAAE,MAAM,CAAA;IAEhB,sCAAsC;IACtC,YAAY,EAAE,YAAY,CAAA;IAE1B,+CAA+C;IAC/C,yBAAyB,EAAE,eAAe,EAAE,CAAA;CAC7C,CAAA;AAED,yFAAyF;AACzF,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,+BAA+B;IAC/B,QAAQ,EAAE,MAAM,CAAA;IAEhB,sCAAsC;IACtC,YAAY,EAAE,YAAY,CAAA;IAE1B,mEAAmE;IACnE,uBAAuB,EAAE,eAAe,CAAA;IACxC,+BAA+B;IAC/B,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,gGAAgG;AAChG,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,EAAE,OAAO,CAAA;IACb,mCAAmC;IACnC,QAA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CACjB,CAAA;AAED,8CAA8C;AAC9C,MAAM,MAAM,iBAAiB,GAAG,WAAW,GAAG,OAAO,CAAA;AAErD,8CAA8C;AAC9C,MAAM,MAAM,iBAAiB,GAAG,WAAW,GAAG,YAAY,GAAG,OAAO,CAAA;AAIpE,eAAO,MAAM,aAAa,GACxB,SAAS,iBAAiB,KACzB,OAAO,IAAI,WAAsC,CAAA;AAEpD,eAAO,MAAM,aAAa,GACxB,SAAS,iBAAiB,KACzB,OAAO,IAAI,WAAsC,CAAA;AAEpD,eAAO,MAAM,cAAc,GACzB,SAAS,iBAAiB,KACzB,OAAO,IAAI,YAAwC,CAAA"}
|
package/dist/toArrayBuffer.d.ts
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
* This incantation deals with websocket sending the whole underlying buffer even if we just have a
|
|
3
3
|
* uint8array view on it
|
|
4
4
|
*/
|
|
5
|
-
export declare const toArrayBuffer: (bytes: Uint8Array) => ArrayBuffer;
|
|
5
|
+
export declare const toArrayBuffer: (bytes: Uint8Array) => ArrayBuffer | SharedArrayBuffer;
|
|
6
6
|
//# sourceMappingURL=toArrayBuffer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toArrayBuffer.d.ts","sourceRoot":"","sources":["../src/toArrayBuffer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,aAAa,
|
|
1
|
+
{"version":3,"file":"toArrayBuffer.d.ts","sourceRoot":"","sources":["../src/toArrayBuffer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,aAAa,GAAI,OAAO,UAAU,oCAG9C,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automerge/automerge-repo-network-websocket",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-beta.2",
|
|
4
4
|
"description": "isomorphic node/browser Websocket network adapter for Automerge Repo",
|
|
5
5
|
"repository": "https://github.com/automerge/automerge-repo/tree/master/packages/automerge-repo-network-websocket",
|
|
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": "2.0.0-
|
|
16
|
+
"@automerge/automerge-repo": "2.0.0-beta.2",
|
|
17
17
|
"cbor-x": "^1.3.0",
|
|
18
18
|
"debug": "^4.3.4",
|
|
19
19
|
"eventemitter3": "^5.0.1",
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "70b1423aad5595782bb1ed6cfe21244e2693e226"
|
|
35
35
|
}
|
|
@@ -23,7 +23,7 @@ abstract class WebSocketNetworkAdapter extends NetworkAdapter {
|
|
|
23
23
|
socket?: WebSocket
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
export class
|
|
26
|
+
export class WebSocketClientAdapter extends WebSocketNetworkAdapter {
|
|
27
27
|
#ready = false
|
|
28
28
|
#readyResolver?: () => void
|
|
29
29
|
#readyPromise: Promise<void> = new Promise<void>(resolve => {
|
|
@@ -21,7 +21,7 @@ import { toArrayBuffer } from "./toArrayBuffer.js"
|
|
|
21
21
|
|
|
22
22
|
const { encode, decode } = cborHelpers
|
|
23
23
|
|
|
24
|
-
export class
|
|
24
|
+
export class WebSocketServerAdapter extends NetworkAdapter {
|
|
25
25
|
sockets: { [peerId: PeerId]: WebSocket } = {}
|
|
26
26
|
|
|
27
27
|
#ready = false
|
|
@@ -123,7 +123,14 @@ export class NodeWSServerAdapter extends NetworkAdapter {
|
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
receiveMessage(messageBytes: Uint8Array, socket: WebSocket) {
|
|
126
|
-
|
|
126
|
+
let message: FromClientMessage
|
|
127
|
+
try {
|
|
128
|
+
message = decode(messageBytes)
|
|
129
|
+
} catch (e) {
|
|
130
|
+
log("invalid message received, closing connection")
|
|
131
|
+
socket.close()
|
|
132
|
+
return
|
|
133
|
+
}
|
|
127
134
|
|
|
128
135
|
const { type, senderId } = message
|
|
129
136
|
|
package/src/index.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* A `NetworkAdapter` which connects to a remote host via WebSockets
|
|
3
3
|
*
|
|
4
4
|
* The websocket protocol requires a server to be listening and a client to
|
|
5
|
-
* connect to the server. To that end the {@link
|
|
5
|
+
* connect to the server. To that end the {@link WebSocketServerAdapter} does not
|
|
6
6
|
* make outbound connections and instead listens on the provided socket for
|
|
7
|
-
* new connections whilst the {@link
|
|
7
|
+
* new connections whilst the {@link WebSocketClientAdapter} makes an
|
|
8
8
|
* outbound connection to the provided socket.
|
|
9
9
|
*
|
|
10
10
|
* Note that the "browser" and "node" naming is a bit misleading, both
|
|
@@ -12,8 +12,15 @@
|
|
|
12
12
|
*
|
|
13
13
|
* @module
|
|
14
14
|
* */
|
|
15
|
-
export {
|
|
16
|
-
export {
|
|
15
|
+
export { WebSocketClientAdapter } from "./WebSocketClientAdapter.js"
|
|
16
|
+
export { WebSocketServerAdapter } from "./WebSocketServerAdapter.js"
|
|
17
|
+
|
|
18
|
+
/** @hidden */
|
|
19
|
+
export { WebSocketClientAdapter as BrowserWebSocketClientAdapter } from "./WebSocketClientAdapter.js"
|
|
20
|
+
|
|
21
|
+
/** @hidden */
|
|
22
|
+
export { WebSocketServerAdapter as NodeWSServerAdapter } from "./WebSocketServerAdapter.js"
|
|
23
|
+
|
|
17
24
|
export type {
|
|
18
25
|
FromClientMessage,
|
|
19
26
|
FromServerMessage,
|
package/test/Websocket.test.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { next as A } from "@automerge/automerge"
|
|
2
2
|
import {
|
|
3
3
|
AutomergeUrl,
|
|
4
|
+
DocHandle,
|
|
4
5
|
DocumentId,
|
|
5
6
|
PeerId,
|
|
6
7
|
Repo,
|
|
@@ -19,8 +20,9 @@ import http from "http"
|
|
|
19
20
|
import { getPortPromise as getAvailablePort } from "portfinder"
|
|
20
21
|
import { describe, it } from "vitest"
|
|
21
22
|
import WebSocket from "ws"
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
23
|
+
import { WebSocketClientAdapter } from "../src/WebSocketClientAdapter.js"
|
|
24
|
+
import { WebSocketServerAdapter } from "../src/WebSocketServerAdapter.js"
|
|
25
|
+
import { encodeHeads } from "../../automerge-repo/dist/AutomergeUrl.js"
|
|
24
26
|
|
|
25
27
|
describe("Websocket adapters", () => {
|
|
26
28
|
const browserPeerId = "browser" as PeerId
|
|
@@ -41,7 +43,7 @@ describe("Websocket adapters", () => {
|
|
|
41
43
|
return { adapters: [aliceAdapter, serverAdapter, bobAdapter], teardown }
|
|
42
44
|
})
|
|
43
45
|
|
|
44
|
-
describe("
|
|
46
|
+
describe("WebSocketClientAdapter", () => {
|
|
45
47
|
it("should advertise the protocol versions it supports in its join message", async () => {
|
|
46
48
|
const {
|
|
47
49
|
serverSocket: socket,
|
|
@@ -178,7 +180,7 @@ describe("Websocket adapters", () => {
|
|
|
178
180
|
const serverUrl = `ws://localhost:${port}`
|
|
179
181
|
|
|
180
182
|
const retry = 100
|
|
181
|
-
const browser = new
|
|
183
|
+
const browser = new WebSocketClientAdapter(serverUrl, retry)
|
|
182
184
|
|
|
183
185
|
const _browserRepo = new Repo({
|
|
184
186
|
network: [browser],
|
|
@@ -189,7 +191,7 @@ describe("Websocket adapters", () => {
|
|
|
189
191
|
const serverSocket = new WebSocket.Server({ server })
|
|
190
192
|
|
|
191
193
|
await new Promise<void>(resolve => server.listen(port, resolve))
|
|
192
|
-
const serverAdapter = new
|
|
194
|
+
const serverAdapter = new WebSocketServerAdapter(serverSocket, retry)
|
|
193
195
|
|
|
194
196
|
const serverRepo = new Repo({
|
|
195
197
|
network: [serverAdapter],
|
|
@@ -214,7 +216,7 @@ describe("Websocket adapters", () => {
|
|
|
214
216
|
})
|
|
215
217
|
|
|
216
218
|
it("should correctly clear event handlers on reconnect", async () => {
|
|
217
|
-
// This reproduces an issue where the
|
|
219
|
+
// This reproduces an issue where the WebSocketClientAdapter.connect
|
|
218
220
|
// call registered event handlers on the websocket but didn't clear them
|
|
219
221
|
// up again on a second call to connect. This combined with the reconnect
|
|
220
222
|
// timer to produce the following sequence of events:
|
|
@@ -245,12 +247,12 @@ describe("Websocket adapters", () => {
|
|
|
245
247
|
})
|
|
246
248
|
})
|
|
247
249
|
|
|
248
|
-
describe("
|
|
250
|
+
describe("WebSocketServerAdapter", () => {
|
|
249
251
|
const serverResponse = async (clientHello: Object) => {
|
|
250
252
|
const { serverSocket, serverUrl } = await setup({
|
|
251
253
|
clientCount: 0,
|
|
252
254
|
})
|
|
253
|
-
const server = new
|
|
255
|
+
const server = new WebSocketServerAdapter(serverSocket)
|
|
254
256
|
const _serverRepo = new Repo({
|
|
255
257
|
network: [server],
|
|
256
258
|
peerId: serverPeerId,
|
|
@@ -312,13 +314,13 @@ describe("Websocket adapters", () => {
|
|
|
312
314
|
const serverUrl = `ws://localhost:${port}`
|
|
313
315
|
|
|
314
316
|
const retry = 100
|
|
315
|
-
const browserAdapter = new
|
|
317
|
+
const browserAdapter = new WebSocketClientAdapter(serverUrl, retry)
|
|
316
318
|
|
|
317
319
|
const server = http.createServer()
|
|
318
320
|
const serverSocket = new WebSocket.Server({ server })
|
|
319
321
|
|
|
320
322
|
await new Promise<void>(resolve => server.listen(port, resolve))
|
|
321
|
-
const serverAdapter = new
|
|
323
|
+
const serverAdapter = new WebSocketServerAdapter(serverSocket, retry)
|
|
322
324
|
|
|
323
325
|
const _browserRepo = new Repo({
|
|
324
326
|
network: [browserAdapter],
|
|
@@ -338,6 +340,43 @@ describe("Websocket adapters", () => {
|
|
|
338
340
|
await eventPromise(serverAdapter, "peer-disconnected")
|
|
339
341
|
})
|
|
340
342
|
|
|
343
|
+
it("should disconnect from a client that sends an invalid CBOR message", async () => {
|
|
344
|
+
// Set up a server and wait for it to be ready
|
|
345
|
+
const port = await getPort()
|
|
346
|
+
const serverUrl = `ws://localhost:${port}`
|
|
347
|
+
const server = http.createServer()
|
|
348
|
+
const serverSocket = new WebSocket.Server({ server })
|
|
349
|
+
await new Promise<void>(resolve => server.listen(port, resolve))
|
|
350
|
+
|
|
351
|
+
// Create a repo listening on the socket
|
|
352
|
+
const serverAdapter = new WebSocketServerAdapter(serverSocket)
|
|
353
|
+
const serverRepo = new Repo({
|
|
354
|
+
network: [serverAdapter],
|
|
355
|
+
peerId: serverPeerId,
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
// Create a new socket connected to the repo
|
|
359
|
+
const browserSocket = new WebSocket(serverUrl)
|
|
360
|
+
await new Promise(resolve => browserSocket.on("open", resolve))
|
|
361
|
+
const disconnected = new Promise(resolve =>
|
|
362
|
+
browserSocket.on("close", resolve)
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
// Send an invalid CBOR message, in this case we use a definite length
|
|
366
|
+
// array with too many elements. This test should actually work for any
|
|
367
|
+
// invalid message but this reproduces a specific issue we were seeing on
|
|
368
|
+
// the sycn server
|
|
369
|
+
//
|
|
370
|
+
// 0x9 (1001) is major type 4, for an array
|
|
371
|
+
// 0xB (1011) indicates that the length will be encoded in the next 8 bytes
|
|
372
|
+
// 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 is 2**32, which is longer than allowed
|
|
373
|
+
const invalidLargeArray = new Uint8Array([
|
|
374
|
+
0x9b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
|
|
375
|
+
])
|
|
376
|
+
browserSocket.send(invalidLargeArray)
|
|
377
|
+
await disconnected
|
|
378
|
+
})
|
|
379
|
+
|
|
341
380
|
it("should send the negotiated protocol version in its hello message", async () => {
|
|
342
381
|
const response = await serverResponse({
|
|
343
382
|
type: "join",
|
|
@@ -473,7 +512,7 @@ describe("Websocket adapters", () => {
|
|
|
473
512
|
clientDoc = A.change(clientDoc, d => (d.foo = "qux"))
|
|
474
513
|
|
|
475
514
|
// Now create a websocket sync server with the original document in it's storage
|
|
476
|
-
const adapter = new
|
|
515
|
+
const adapter = new WebSocketServerAdapter(socket)
|
|
477
516
|
const repo = new Repo({
|
|
478
517
|
network: [adapter],
|
|
479
518
|
storage,
|
|
@@ -481,8 +520,7 @@ describe("Websocket adapters", () => {
|
|
|
481
520
|
})
|
|
482
521
|
|
|
483
522
|
// make a change to the handle on the sync server
|
|
484
|
-
const handle = repo.find<{ foo: string }>(url)
|
|
485
|
-
await handle.whenReady()
|
|
523
|
+
const handle = await repo.find<{ foo: string }>(url)
|
|
486
524
|
handle.change(d => (d.foo = "baz"))
|
|
487
525
|
|
|
488
526
|
// Okay, so now there is a document on both the client and the server
|
|
@@ -530,7 +568,7 @@ describe("Websocket adapters", () => {
|
|
|
530
568
|
// Now, assume either the network or the server is going slow, so the
|
|
531
569
|
// server thinks it has sent the response above, but for whatever reason
|
|
532
570
|
// it never gets to the client. In that case the reconnect timer in the
|
|
533
|
-
//
|
|
571
|
+
// WebSocketClientAdapter will fire and we'll create a new
|
|
534
572
|
// websocket and connect it. To simulate this we drop the above response
|
|
535
573
|
// on the floor and start connecting again.
|
|
536
574
|
|
|
@@ -582,9 +620,10 @@ describe("Websocket adapters", () => {
|
|
|
582
620
|
await pause(50)
|
|
583
621
|
}
|
|
584
622
|
|
|
623
|
+
// we encode localHeads for consistency with URL formatted heads
|
|
585
624
|
let localHeads = A.getHeads(clientDoc)
|
|
586
625
|
let remoteHeads = handle.heads()
|
|
587
|
-
if (!headsAreSame(localHeads, remoteHeads)) {
|
|
626
|
+
if (!headsAreSame(encodeHeads(localHeads), remoteHeads)) {
|
|
588
627
|
throw new Error("heads not equal")
|
|
589
628
|
}
|
|
590
629
|
})
|
|
@@ -621,7 +660,7 @@ const setupServer = async (options: SetupOptions = {}) => {
|
|
|
621
660
|
const server = http.createServer()
|
|
622
661
|
const serverSocket = new WebSocket.Server({ server })
|
|
623
662
|
await new Promise<void>(resolve => server.listen(port, resolve))
|
|
624
|
-
const serverAdapter = new
|
|
663
|
+
const serverAdapter = new WebSocketServerAdapter(serverSocket, retryInterval)
|
|
625
664
|
return { server, serverAdapter, serverSocket, serverUrl }
|
|
626
665
|
}
|
|
627
666
|
|
|
@@ -632,7 +671,7 @@ const setupClient = async (options: SetupOptions = {}) => {
|
|
|
632
671
|
port = await getPort(),
|
|
633
672
|
} = options
|
|
634
673
|
const serverUrl = `ws://localhost:${port}`
|
|
635
|
-
return new
|
|
674
|
+
return new WebSocketClientAdapter(serverUrl, retryInterval)
|
|
636
675
|
}
|
|
637
676
|
|
|
638
677
|
const pause = (t = 0) =>
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"BrowserWebSocketClientAdapter.d.ts","sourceRoot":"","sources":["../src/BrowserWebSocketClientAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,MAAM,EACN,YAAY,EAEb,MAAM,gCAAgC,CAAA;AACvC,OAAO,SAAS,MAAM,eAAe,CAAA;AAIrC,OAAO,EACL,iBAAiB,EAKlB,MAAM,eAAe,CAAA;AAKtB,uBAAe,uBAAwB,SAAQ,cAAc;IAC3D,MAAM,CAAC,EAAE,SAAS,CAAA;CACnB;AAED,qBAAa,6BAA8B,SAAQ,uBAAuB;;aA4BtD,GAAG,EAAE,MAAM;aACX,aAAa;IAtB/B,OAAO;IAIP,SAAS;IAcT,YAAY,CAAC,EAAE,MAAM,CAAA;gBAGH,GAAG,EAAE,MAAM,EACX,aAAa,SAAO;IAMtC,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,YAAY;IAqCnD,MAAM,aAKL;IAGD,OAAO,aAWN;IAED,SAAS,UAAW,SAAS,CAAC,YAAY,UAEzC;IAED,mFAAmF;IACnF,OAAO,UAED,KAAK,GACL,SAAS,CAAC,UAAU,UAezB;IAED,IAAI;IAUJ,UAAU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