File tree 2 files changed +25
-1
lines changed
2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -63,6 +63,22 @@ describe("protocol tests", () => {
63
63
expect ( oncloseMock ) . toHaveBeenCalled ( ) ;
64
64
} ) ;
65
65
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
+
66
82
describe ( "progress notification timeout behavior" , ( ) => {
67
83
beforeEach ( ( ) => {
68
84
jest . useFakeTimers ( ) ;
Original file line number Diff line number Diff line change @@ -279,23 +279,31 @@ export abstract class Protocol<
279
279
*/
280
280
async connect ( transport : Transport ) : Promise < void > {
281
281
this . _transport = transport ;
282
+ const _onclose = this . transport ?. onclose ;
282
283
this . _transport . onclose = ( ) => {
284
+ _onclose ?.( ) ;
283
285
this . _onclose ( ) ;
284
286
} ;
285
287
288
+ const _onerror = this . transport ?. onerror ;
286
289
this . _transport . onerror = ( error : Error ) => {
290
+ _onerror ?.( error ) ;
287
291
this . _onerror ( error ) ;
288
292
} ;
289
293
294
+ const _onmessage = this . _transport ?. onmessage ;
290
295
this . _transport . onmessage = ( message , extra ) => {
296
+ _onmessage ?.( message , extra ) ;
291
297
if ( isJSONRPCResponse ( message ) || isJSONRPCError ( message ) ) {
292
298
this . _onresponse ( message ) ;
293
299
} else if ( isJSONRPCRequest ( message ) ) {
294
300
this . _onrequest ( message , extra ) ;
295
301
} else if ( isJSONRPCNotification ( message ) ) {
296
302
this . _onnotification ( message ) ;
297
303
} 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
+ ) ;
299
307
}
300
308
} ;
301
309
You can’t perform that action at this time.
0 commit comments