@automerge/automerge-repo-network-websocket 2.0.0-collectionsync-alpha.1 → 2.0.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/{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} +1 -1
- 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} +1 -1
- package/src/index.ts +11 -4
- package/test/Websocket.test.ts +20 -18
- package/dist/BrowserWebSocketClientAdapter.d.ts.map +0 -1
- package/dist/NodeWSServerAdapter.d.ts.map +0 -1
- package/dist/WSShared.d.ts +0 -22
- package/dist/WSShared.d.ts.map +0 -1
- package/dist/WSShared.js +0 -83
- package/dist/encoder.d.ts +0 -4
- package/dist/encoder.d.ts.map +0 -1
- package/dist/encoder.js +0 -2
|
@@ -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 = {};
|
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,QAAQ,EAAE,MAAM,CAAA;IAChB,gCAAgC;IAChC,OAAO,EAAE,MAAM,CAAA;IACf,+BAA+B;IAC/B,QAAQ,EAAE,MAAM,CAAA;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,
|
|
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,QAAQ,EAAE,MAAM,CAAA;IAChB,gCAAgC;IAChC,OAAO,EAAE,MAAM,CAAA;IACf,+BAA+B;IAC/B,QAAQ,EAAE,MAAM,CAAA;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.
|
|
3
|
+
"version": "2.0.1",
|
|
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.
|
|
16
|
+
"@automerge/automerge-repo": "2.0.1",
|
|
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": "6e8d13f3aebcbc31ff3dd478be85191eee1ad120"
|
|
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
|
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],
|
|
@@ -347,7 +349,7 @@ describe("Websocket adapters", () => {
|
|
|
347
349
|
await new Promise<void>(resolve => server.listen(port, resolve))
|
|
348
350
|
|
|
349
351
|
// Create a repo listening on the socket
|
|
350
|
-
const serverAdapter = new
|
|
352
|
+
const serverAdapter = new WebSocketServerAdapter(serverSocket)
|
|
351
353
|
const serverRepo = new Repo({
|
|
352
354
|
network: [serverAdapter],
|
|
353
355
|
peerId: serverPeerId,
|
|
@@ -510,7 +512,7 @@ describe("Websocket adapters", () => {
|
|
|
510
512
|
clientDoc = A.change(clientDoc, d => (d.foo = "qux"))
|
|
511
513
|
|
|
512
514
|
// Now create a websocket sync server with the original document in it's storage
|
|
513
|
-
const adapter = new
|
|
515
|
+
const adapter = new WebSocketServerAdapter(socket)
|
|
514
516
|
const repo = new Repo({
|
|
515
517
|
network: [adapter],
|
|
516
518
|
storage,
|
|
@@ -518,8 +520,7 @@ describe("Websocket adapters", () => {
|
|
|
518
520
|
})
|
|
519
521
|
|
|
520
522
|
// make a change to the handle on the sync server
|
|
521
|
-
const handle = repo.find<{ foo: string }>(url)
|
|
522
|
-
await handle.whenReady()
|
|
523
|
+
const handle = await repo.find<{ foo: string }>(url)
|
|
523
524
|
handle.change(d => (d.foo = "baz"))
|
|
524
525
|
|
|
525
526
|
// Okay, so now there is a document on both the client and the server
|
|
@@ -567,7 +568,7 @@ describe("Websocket adapters", () => {
|
|
|
567
568
|
// Now, assume either the network or the server is going slow, so the
|
|
568
569
|
// server thinks it has sent the response above, but for whatever reason
|
|
569
570
|
// it never gets to the client. In that case the reconnect timer in the
|
|
570
|
-
//
|
|
571
|
+
// WebSocketClientAdapter will fire and we'll create a new
|
|
571
572
|
// websocket and connect it. To simulate this we drop the above response
|
|
572
573
|
// on the floor and start connecting again.
|
|
573
574
|
|
|
@@ -619,9 +620,10 @@ describe("Websocket adapters", () => {
|
|
|
619
620
|
await pause(50)
|
|
620
621
|
}
|
|
621
622
|
|
|
623
|
+
// we encode localHeads for consistency with URL formatted heads
|
|
622
624
|
let localHeads = A.getHeads(clientDoc)
|
|
623
625
|
let remoteHeads = handle.heads()
|
|
624
|
-
if (!headsAreSame(localHeads, remoteHeads)) {
|
|
626
|
+
if (!headsAreSame(encodeHeads(localHeads), remoteHeads)) {
|
|
625
627
|
throw new Error("heads not equal")
|
|
626
628
|
}
|
|
627
629
|
})
|
|
@@ -658,7 +660,7 @@ const setupServer = async (options: SetupOptions = {}) => {
|
|
|
658
660
|
const server = http.createServer()
|
|
659
661
|
const serverSocket = new WebSocket.Server({ server })
|
|
660
662
|
await new Promise<void>(resolve => server.listen(port, resolve))
|
|
661
|
-
const serverAdapter = new
|
|
663
|
+
const serverAdapter = new WebSocketServerAdapter(serverSocket, retryInterval)
|
|
662
664
|
return { server, serverAdapter, serverSocket, serverUrl }
|
|
663
665
|
}
|
|
664
666
|
|
|
@@ -669,7 +671,7 @@ const setupClient = async (options: SetupOptions = {}) => {
|
|
|
669
671
|
port = await getPort(),
|
|
670
672
|
} = options
|
|
671
673
|
const serverUrl = `ws://localhost:${port}`
|
|
672
|
-
return new
|
|
674
|
+
return new WebSocketClientAdapter(serverUrl, retryInterval)
|
|
673
675
|
}
|
|
674
676
|
|
|
675
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;IAiBV,IAAI,CAAC,OAAO,EAAE,iBAAiB;IAe/B,aAAa,CAAC,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY;IAU9D,cAAc,CAAC,YAAY,EAAE,UAAU;CAiBxC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"NodeWSServerAdapter.d.ts","sourceRoot":"","sources":["../src/NodeWSServerAdapter.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,mBAAoB,SAAQ,cAAc;;IAyBnD,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"}
|
package/dist/WSShared.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/// <reference types="ws" />
|
|
2
|
-
import { NetworkAdapter } from "automerge-repo"
|
|
3
|
-
import WebSocket from "isomorphic-ws"
|
|
4
|
-
export interface WebSocketNetworkAdapter extends NetworkAdapter {
|
|
5
|
-
client?: WebSocket
|
|
6
|
-
}
|
|
7
|
-
export declare function sendMessage(
|
|
8
|
-
socket: WebSocket,
|
|
9
|
-
channelId: string,
|
|
10
|
-
senderId: string,
|
|
11
|
-
message: Uint8Array
|
|
12
|
-
): void
|
|
13
|
-
export declare function receiveMessageClient(
|
|
14
|
-
message: Uint8Array,
|
|
15
|
-
self: WebSocketNetworkAdapter
|
|
16
|
-
): void
|
|
17
|
-
export declare function receiveMessageServer(
|
|
18
|
-
message: Uint8Array,
|
|
19
|
-
socket: WebSocket,
|
|
20
|
-
self: WebSocketNetworkAdapter
|
|
21
|
-
): void
|
|
22
|
-
//# sourceMappingURL=WSShared.d.ts.map
|
package/dist/WSShared.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"WSShared.d.ts","sourceRoot":"","sources":["../src/WSShared.ts"],"names":[],"mappings":";AACA,OAAO,EAEL,cAAc,EAEf,MAAM,gBAAgB,CAAA;AACvB,OAAO,SAAS,MAAM,eAAe,CAAA;AAErC,MAAM,WAAW,uBAAwB,SAAQ,cAAc;IAC7D,MAAM,CAAC,EAAE,SAAS,CAAA;CACnB;AAED,wBAAgB,WAAW,CACzB,MAAM,EAAE,SAAS,EACjB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,UAAU,QAoBpB;AAsBD,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,UAAU,EACnB,IAAI,EAAE,uBAAuB,QA0B9B;AAED,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,UAAU,EACnB,MAAM,EAAE,SAAS,EACjB,IAAI,EAAE,uBAAuB,QA0B9B"}
|
package/dist/WSShared.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import * as CBOR from "cbor-x"
|
|
2
|
-
import WebSocket from "isomorphic-ws"
|
|
3
|
-
export function sendMessage(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(
|
|
17
|
-
encoded.byteOffset,
|
|
18
|
-
encoded.byteOffset + encoded.byteLength
|
|
19
|
-
)
|
|
20
|
-
socket.send(arrayBuf)
|
|
21
|
-
}
|
|
22
|
-
function announceConnection(channelId, peerId, socket, self) {
|
|
23
|
-
// return a peer object
|
|
24
|
-
const myPeerId = self.peerId
|
|
25
|
-
if (!myPeerId) {
|
|
26
|
-
throw new Error("we should have a peer ID by now")
|
|
27
|
-
}
|
|
28
|
-
const connection = {
|
|
29
|
-
close: () => socket.close(),
|
|
30
|
-
isOpen: () => socket.readyState === WebSocket.OPEN,
|
|
31
|
-
send: message => sendMessage(socket, channelId, myPeerId, message),
|
|
32
|
-
}
|
|
33
|
-
self.emit("peer-candidate", { peerId, channelId, connection })
|
|
34
|
-
}
|
|
35
|
-
export function receiveMessageClient(message, self) {
|
|
36
|
-
const decoded = CBOR.decode(new Uint8Array(message))
|
|
37
|
-
const { type, senderId, channelId, data } = decoded
|
|
38
|
-
const client = self.client
|
|
39
|
-
if (!client) {
|
|
40
|
-
throw new Error("Missing client at receiveMessage")
|
|
41
|
-
}
|
|
42
|
-
if (message.byteLength === 0) {
|
|
43
|
-
throw new Error("received a zero-length message")
|
|
44
|
-
}
|
|
45
|
-
switch (type) {
|
|
46
|
-
case "peer":
|
|
47
|
-
// console.log(`peer: ${senderId}, ${channelId}`)
|
|
48
|
-
announceConnection(channelId, senderId, client, self)
|
|
49
|
-
break
|
|
50
|
-
default:
|
|
51
|
-
self.emit("message", {
|
|
52
|
-
channelId,
|
|
53
|
-
senderId,
|
|
54
|
-
message: new Uint8Array(data),
|
|
55
|
-
})
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
export function receiveMessageServer(message, socket, self) {
|
|
59
|
-
const cbor = CBOR.decode(message)
|
|
60
|
-
console.log("received: ", cbor)
|
|
61
|
-
const { type, channelId, senderId, data } = cbor
|
|
62
|
-
switch (type) {
|
|
63
|
-
case "join":
|
|
64
|
-
announceConnection(channelId, senderId, socket, self)
|
|
65
|
-
socket.send(
|
|
66
|
-
CBOR.encode({ type: "peer", senderId: self.peerId, channelId })
|
|
67
|
-
)
|
|
68
|
-
break
|
|
69
|
-
case "leave":
|
|
70
|
-
// ?
|
|
71
|
-
break
|
|
72
|
-
case "sync":
|
|
73
|
-
self.emit("message", {
|
|
74
|
-
senderId,
|
|
75
|
-
channelId,
|
|
76
|
-
message: new Uint8Array(data),
|
|
77
|
-
})
|
|
78
|
-
break
|
|
79
|
-
default:
|
|
80
|
-
// console.log("unrecognized message type")
|
|
81
|
-
break
|
|
82
|
-
}
|
|
83
|
-
}
|
package/dist/encoder.d.ts
DELETED
package/dist/encoder.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"encoder.d.ts","sourceRoot":"","sources":["../src/encoder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAA;;AAE9B,wBAAuD"}
|
package/dist/encoder.js
DELETED