3
3
*/
4
4
5
5
import { EventEmitter } from 'node:events' ;
6
+ import { inspect } from 'node:util' ;
6
7
7
8
import {
8
9
encode ,
@@ -12,6 +13,14 @@ import {
12
13
} from '@msgpack/msgpack' ;
13
14
import { Metadata } from '../api/types' ;
14
15
16
+ export let exportsForTesting : any ; // eslint-disable-line import/no-mutable-exports
17
+ // jest sets NODE_ENV=test.
18
+ if ( process . env . NODE_ENV === 'test' ) {
19
+ exportsForTesting = {
20
+ onTransportFail : new EventEmitter ( ) ,
21
+ } ;
22
+ }
23
+
15
24
class Response {
16
25
private requestId : number ;
17
26
@@ -111,12 +120,35 @@ class Transport extends EventEmitter {
111
120
iter . next ( ) . then ( resolved => {
112
121
if ( ! resolved . done ) {
113
122
if ( ! Array . isArray ( resolved . value ) ) {
114
- throw new TypeError ( 'expected msgpack result to be array' ) ;
123
+ let valstr = '?' ;
124
+ try {
125
+ valstr = inspect ( resolved . value , {
126
+ sorted : true ,
127
+ maxArrayLength : 10 ,
128
+ maxStringLength : 500 ,
129
+ compact : true ,
130
+ breakLength : 500 ,
131
+ } ) ;
132
+ } catch ( error ) {
133
+ // Do nothing.
134
+ }
135
+
136
+ const errMsg = `invalid msgpack-RPC message: expected array, got: ${ valstr } ` ;
137
+ const onFail : EventEmitter = exportsForTesting ?. onTransportFail ;
138
+ if ( onFail ) {
139
+ // HACK: for testing only.
140
+ // TODO(justinmk): let the tests explicitly drive the messages.
141
+ onFail . emit ( 'fail' , errMsg ) ;
142
+ return ;
143
+ }
144
+ throw new TypeError ( errMsg ) ;
115
145
}
146
+
116
147
this . parseMessage ( resolved . value ) ;
117
- return resolveGeneratorRecursively ( iter ) ;
148
+ resolveGeneratorRecursively ( iter ) ;
149
+ return ;
118
150
}
119
- return Promise . resolve ( ) ;
151
+ Promise . resolve ( ) ;
120
152
} ) ;
121
153
} ;
122
154
0 commit comments