From 54efcc250c84a3bf5fd688e5df2bd91f5860b511 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 6 May 2025 18:12:03 +0000 Subject: [PATCH 1/3] chore: update onClose callback to include logs --- src/synapse.ts | 18 ++++++++++++++---- tests/synapse.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/synapse.ts b/src/synapse.ts index 9fa1729..6f79630 100644 --- a/src/synapse.ts +++ b/src/synapse.ts @@ -22,7 +22,12 @@ export type MessageHandler = ( message: MessagePayload, connectionId: string, ) => void; -export type ConnectionCallback = (connectionId: string) => void; +export type ConnectionCallback = ( + connectionId: string, + code?: number, + reason?: string, + wasClean?: boolean, +) => void; export type ErrorCallback = (error: Error, connectionId: string) => void; export type ServerConnectionCallback = (connectionId: string) => void; export type Logger = (message: string) => void; @@ -112,8 +117,13 @@ class Synapse { ws.onmessage = (event) => this.handleMessage(event, connectionId); - ws.onclose = () => { - this.connectionListeners.onClose(connectionId); + ws.onclose = (event) => { + this.connectionListeners.onClose( + connectionId, + event.code, + event.reason, + event.wasClean, + ); this.attemptReconnect(connectionId); }; @@ -440,7 +450,7 @@ class Synapse { /** * Registers a callback for when a WebSocket connection is closed - * @param callback - Function to be called when connection closes + * @param callback - Function to be called when connection closes. Receives (connectionId, code, reason) * @returns The Synapse instance for method chaining */ onClose(callback: ConnectionCallback): Synapse { diff --git a/tests/synapse.test.ts b/tests/synapse.test.ts index cb6f0ca..33c9056 100644 --- a/tests/synapse.test.ts +++ b/tests/synapse.test.ts @@ -62,6 +62,28 @@ describe("Synapse", () => { await expect(connectPromise).rejects.toThrow("WebSocket error"); }); + + it("should call onClose with code, reason, and wasClean", () => { + const mockWs = createMockWebSocket(); + (WebSocket as unknown as jest.Mock).mockImplementation(() => mockWs); + + const onCloseMock = jest.fn(); + synapse.onClose(onCloseMock); + + // Use the real setup method so event handlers are set + (synapse as any).setupWebSocket(mockWs, { url: "/" }, "conn1"); + + // Simulate close event + const closeEvent = { code: 4001, reason: "Test reason", wasClean: true }; + mockWs.onclose && mockWs.onclose(closeEvent as any); + + expect(onCloseMock).toHaveBeenCalledWith( + "conn1", + 4001, + "Test reason", + true, + ); + }); }); describe("message handling", () => { From 4eb7b415397deeac41cd3e5c37eee129f3cbabdc Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 6 May 2025 18:13:48 +0000 Subject: [PATCH 2/3] chore: fix typing --- src/synapse.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/synapse.ts b/src/synapse.ts index 6f79630..16b7c4d 100644 --- a/src/synapse.ts +++ b/src/synapse.ts @@ -22,12 +22,16 @@ export type MessageHandler = ( message: MessagePayload, connectionId: string, ) => void; -export type ConnectionCallback = ( + +export type ConnectionCallback = (connectionId: string) => void; + +export type ConnectionCloseCallback = ( connectionId: string, code?: number, reason?: string, wasClean?: boolean, ) => void; + export type ErrorCallback = (error: Error, connectionId: string) => void; export type ServerConnectionCallback = (connectionId: string) => void; export type Logger = (message: string) => void; @@ -38,7 +42,7 @@ class Synapse { private messageHandlers: Record = {}; private connectionListeners = { onOpen: (() => {}) as ConnectionCallback, - onClose: (() => {}) as ConnectionCallback, + onClose: (() => {}) as ConnectionCloseCallback, onError: (() => {}) as ErrorCallback, }; @@ -453,7 +457,7 @@ class Synapse { * @param callback - Function to be called when connection closes. Receives (connectionId, code, reason) * @returns The Synapse instance for method chaining */ - onClose(callback: ConnectionCallback): Synapse { + onClose(callback: ConnectionCloseCallback): Synapse { this.connectionListeners.onClose = callback; return this; } From 35fbcb71f07f346ef3fa77e99bb4f6ebc44d35ae Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 6 May 2025 18:14:54 +0000 Subject: [PATCH 3/3] chore: update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cc26a59..f9dbef2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@appwrite.io/synapse", - "version": "0.4.0", + "version": "0.4.1", "description": "Operating system gateway for remote serverless environments", "main": "dist/index.js", "types": "dist/index.d.ts",