File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -65,6 +65,22 @@ describe("protocol tests", () => {
6565 expect ( oncloseMock ) . toHaveBeenCalled ( ) ;
6666 } ) ;
6767
68+ test ( "should not overwrite existing hooks when connecting transports" , async ( ) => {
69+ const oncloseMock = jest . fn ( ) ;
70+ const onerrorMock = jest . fn ( ) ;
71+ const onmessageMock = jest . fn ( ) ;
72+ transport . onclose = oncloseMock ;
73+ transport . onerror = onerrorMock ;
74+ transport . onmessage = onmessageMock ;
75+ await protocol . connect ( transport ) ;
76+ transport . onclose ( ) ;
77+ transport . onerror ( new Error ( ) ) ;
78+ transport . onmessage ( "" ) ;
79+ expect ( oncloseMock ) . toHaveBeenCalled ( ) ;
80+ expect ( onerrorMock ) . toHaveBeenCalled ( ) ;
81+ expect ( onmessageMock ) . toHaveBeenCalled ( ) ;
82+ } ) ;
83+
6884 describe ( "_meta preservation with onprogress" , ( ) => {
6985 test ( "should preserve existing _meta when adding progressToken" , async ( ) => {
7086 await protocol . connect ( transport ) ;
Original file line number Diff line number Diff line change @@ -279,23 +279,31 @@ export abstract class Protocol<
279279 */
280280 async connect ( transport : Transport ) : Promise < void > {
281281 this . _transport = transport ;
282+ const _onclose = this . transport ?. onclose ;
282283 this . _transport . onclose = ( ) => {
284+ _onclose ?.( ) ;
283285 this . _onclose ( ) ;
284286 } ;
285287
288+ const _onerror = this . transport ?. onerror ;
286289 this . _transport . onerror = ( error : Error ) => {
290+ _onerror ?.( error ) ;
287291 this . _onerror ( error ) ;
288292 } ;
289293
294+ const _onmessage = this . _transport ?. onmessage ;
290295 this . _transport . onmessage = ( message , extra ) => {
296+ _onmessage ?.( message , extra ) ;
291297 if ( isJSONRPCResponse ( message ) || isJSONRPCError ( message ) ) {
292298 this . _onresponse ( message ) ;
293299 } else if ( isJSONRPCRequest ( message ) ) {
294300 this . _onrequest ( message , extra ) ;
295301 } else if ( isJSONRPCNotification ( message ) ) {
296302 this . _onnotification ( message ) ;
297303 } else {
298- this . _onerror ( new Error ( `Unknown message type: ${ JSON . stringify ( message ) } ` ) ) ;
304+ this . _onerror (
305+ new Error ( `Unknown message type: ${ JSON . stringify ( message ) } ` ) ,
306+ ) ;
299307 }
300308 } ;
301309
You can’t perform that action at this time.
0 commit comments