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 @@ -63,6 +63,22 @@ describe("protocol tests", () => {
6363 expect ( oncloseMock ) . toHaveBeenCalled ( ) ;
6464 } ) ;
6565
66+ it ( "should not overwrite existing hooks when connecting transports" , async ( ) => {
67+ const oncloseMock = jest . fn ( ) ;
68+ const onerrorMock = jest . fn ( ) ;
69+ const onmessageMock = jest . fn ( ) ;
70+ transport . onclose = oncloseMock ;
71+ transport . onerror = onerrorMock ;
72+ transport . onmessage = onmessageMock ;
73+ await protocol . connect ( transport ) ;
74+ transport . onclose ( ) ;
75+ transport . onerror ( new Error ( ) ) ;
76+ transport . onmessage ( "" ) ;
77+ expect ( oncloseMock ) . toHaveBeenCalled ( ) ;
78+ expect ( onerrorMock ) . toHaveBeenCalled ( ) ;
79+ expect ( onmessageMock ) . toHaveBeenCalled ( ) ;
80+ } ) ;
81+
6682 describe ( "progress notification timeout behavior" , ( ) => {
6783 beforeEach ( ( ) => {
6884 jest . useFakeTimers ( ) ;
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