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", () => {
65
65
expect ( oncloseMock ) . toHaveBeenCalled ( ) ;
66
66
} ) ;
67
67
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
+
68
84
describe ( "_meta preservation with onprogress" , ( ) => {
69
85
test ( "should preserve existing _meta when adding progressToken" , async ( ) => {
70
86
await protocol . connect ( transport ) ;
Original file line number Diff line number Diff line change @@ -286,23 +286,31 @@ export abstract class Protocol<
286
286
*/
287
287
async connect ( transport : Transport ) : Promise < void > {
288
288
this . _transport = transport ;
289
+ const _onclose = this . transport ?. onclose ;
289
290
this . _transport . onclose = ( ) => {
291
+ _onclose ?.( ) ;
290
292
this . _onclose ( ) ;
291
293
} ;
292
294
295
+ const _onerror = this . transport ?. onerror ;
293
296
this . _transport . onerror = ( error : Error ) => {
297
+ _onerror ?.( error ) ;
294
298
this . _onerror ( error ) ;
295
299
} ;
296
300
301
+ const _onmessage = this . _transport ?. onmessage ;
297
302
this . _transport . onmessage = ( message , extra ) => {
303
+ _onmessage ?.( message , extra ) ;
298
304
if ( isJSONRPCResponse ( message ) || isJSONRPCError ( message ) ) {
299
305
this . _onresponse ( message ) ;
300
306
} else if ( isJSONRPCRequest ( message ) ) {
301
307
this . _onrequest ( message , extra ) ;
302
308
} else if ( isJSONRPCNotification ( message ) ) {
303
309
this . _onnotification ( message ) ;
304
310
} else {
305
- this . _onerror ( new Error ( `Unknown message type: ${ JSON . stringify ( message ) } ` ) ) ;
311
+ this . _onerror (
312
+ new Error ( `Unknown message type: ${ JSON . stringify ( message ) } ` ) ,
313
+ ) ;
306
314
}
307
315
} ;
308
316
You can’t perform that action at this time.
0 commit comments