@automerge/automerge-repo-network-websocket 2.0.5 → 2.0.7

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.
@@ -1 +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"}
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;CAuBxC"}
@@ -158,7 +158,14 @@ export class WebSocketClientAdapter extends WebSocketNetworkAdapter {
158
158
  });
159
159
  }
160
160
  receiveMessage(messageBytes) {
161
- const message = cbor.decode(new Uint8Array(messageBytes));
161
+ let message;
162
+ try {
163
+ message = cbor.decode(new Uint8Array(messageBytes));
164
+ }
165
+ catch (e) {
166
+ this.#log("error decoding message:", e);
167
+ return;
168
+ }
162
169
  assert(this.socket);
163
170
  if (messageBytes.byteLength === 0)
164
171
  throw new Error("received a zero-length message");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automerge/automerge-repo-network-websocket",
3
- "version": "2.0.5",
3
+ "version": "2.0.7",
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.5",
16
+ "@automerge/automerge-repo": "2.0.7",
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": "9d24ea3db7c1f459fc66ce43b794a9ed4bef1341"
34
+ "gitHead": "fe2b8e6e8b4d945ac7d89aff966e38a2892af891"
35
35
  }
@@ -194,7 +194,13 @@ export class WebSocketClientAdapter extends WebSocketNetworkAdapter {
194
194
  }
195
195
 
196
196
  receiveMessage(messageBytes: Uint8Array) {
197
- const message: FromServerMessage = cbor.decode(new Uint8Array(messageBytes))
197
+ let message: FromServerMessage
198
+ try {
199
+ message = cbor.decode(new Uint8Array(messageBytes))
200
+ } catch (e) {
201
+ this.#log("error decoding message:", e)
202
+ return
203
+ }
198
204
 
199
205
  assert(this.socket)
200
206
  if (messageBytes.byteLength === 0)
@@ -245,6 +245,40 @@ describe("Websocket adapters", () => {
245
245
  // are cleaned up correctly we shouldn't throw
246
246
  await pause(1)
247
247
  })
248
+
249
+ it("should gracefully handle a server sending invalid cbor", async () => {
250
+ const port = await getPort()
251
+ const serverUrl = `ws://localhost:${port}`
252
+ const server = http.createServer()
253
+ const serverSocket = new WebSocket.Server({ server })
254
+
255
+ // This listener will be added in addition to the listener which the server adds
256
+ // so we know that at some point we will send garbage. Because we only send the
257
+ // garbage once, the reconnect logic in the client should recover
258
+ let garbageSent = false
259
+ serverSocket.on("connection", ws => {
260
+ // send garbage to the client
261
+ if (garbageSent) return
262
+ ws.send(new Uint8Array([0x00, 0x01, 0x02]))
263
+ })
264
+ await new Promise<void>(resolve => server.listen(port, resolve))
265
+ const serverAdapter = new WebSocketServerAdapter(serverSocket)
266
+
267
+ const serverRepo = new Repo({
268
+ network: [serverAdapter],
269
+ peerId: "server" as PeerId,
270
+ })
271
+ const browser = new WebSocketClientAdapter(serverUrl, 1000)
272
+
273
+ const browserRepo = new Repo({
274
+ network: [browser],
275
+ peerId: browserPeerId,
276
+ })
277
+
278
+ await browserRepo.networkSubsystem.whenReady()
279
+
280
+ assert.deepStrictEqual(browserRepo.peers, ["server" as PeerId])
281
+ })
248
282
  })
249
283
 
250
284
  describe("WebSocketServerAdapter", () => {