@@ -18,6 +18,25 @@ const HOST_SESSION_STATES = {
1818 CLIENT_CLOSED : 'CLIENT_CLOSED'
1919}
2020
21+
22+ function truncateString ( str , num ) {
23+
24+ if ( ! str ) { return '' }
25+
26+ if ( typeof str != 'string' ) {
27+ str = str . toString ( )
28+ }
29+
30+ let length = str . length
31+
32+ if ( str . length <= num ) {
33+ return str
34+ }
35+ return str . slice ( 0 , num ) + '...' + ( length - num ) + 'more bytes'
36+ }
37+
38+
39+
2140class PeerComms extends SocketComms {
2241 constructor ( { remoteIdentity, discoverRemoteIdentity, host, party, socket, ...options } ) {
2342 super ( { remoteIdentity, discoverRemoteIdentity, party, ...options } )
@@ -58,7 +77,7 @@ class PeerComms extends SocketComms {
5877
5978 let response = null
6079 let request = await this . decrypt ( { data : message } , this . remoteIdentity )
61- debug ( 'handleHostCall' , request )
80+ debug ( 'handleHostCall' , truncateString ( request , 1024 ) )
6281
6382 let inputValidated
6483
@@ -85,8 +104,8 @@ class PeerComms extends SocketComms {
85104 throw inputValidated . error
86105 }
87106
88- debug ( 'original input ->' , typeof request , request )
89- debug ( 'validated input ->' , inputValidated )
107+ // debug('original input ->', typeof request, request)
108+ // debug('validated input ->', inputValidated)
90109 const op = new HostOp ( { msg : message , input : inputValidated . value } )
91110
92111 /*debug('session id : ', op.input.session, this.session)
@@ -109,6 +128,8 @@ class PeerComms extends SocketComms {
109128 ...op . result
110129 }
111130
131+ debug ( response . id , response . state , response . stats . duration_ms , 'ms' )
132+
112133 this . send ( response )
113134 } )
114135
@@ -133,6 +154,8 @@ class PeerComms extends SocketComms {
133154 )
134155 }
135156
157+
158+
136159 async handleAuthTimeout ( ) {
137160 clearTimeout ( this . _host_auth_timeout )
138161 this . _host_auth_timeout = null
@@ -144,7 +167,7 @@ class PeerComms extends SocketComms {
144167 }
145168
146169 async handleMessage ( message ) {
147- debug ( 'handleMessage' , message . toString ( ) )
170+ debug ( 'handleMessage' , truncateString ( message . toString ( ) , 1024 ) )
148171
149172 this . onmessage ( { data : message } )
150173 }
@@ -159,7 +182,7 @@ class PeerComms extends SocketComms {
159182
160183 let callOp = new SocketOp ( 'peer-call' , { endpoint : path , data } , this )
161184
162- debug ( 'running peer-call endpoint =' , path , data )
185+ debug ( 'running peer-call endpoint =' , path , truncateString ( data , 1024 ) )
163186
164187 const reply = await callOp . run ( )
165188
@@ -212,11 +235,11 @@ class PeerComms extends SocketComms {
212235
213236 async authorizeOperation ( op ) {
214237
215- debug ( 'Here\'s op' , op )
238+ // debug('Here\'s op', op)
216239
217- debug ( 'Here state : ' , this . state )
240+ // debug('state : ', this.state)
218241
219- console . log ( op . input )
242+ // console.log(op.input)
220243
221244 if ( op . op === 'auth' && this . state === PeerComms . STATES . AUTH_REQUIRED ) {
222245
@@ -231,24 +254,40 @@ class PeerComms extends SocketComms {
231254
232255 if ( this . party . topics ) {
233256 await this . party . topics . advertise ( this , op . input . topic )
257+ op . setState ( HostOp . STATES . Finished_Success )
258+ }
259+ else {
260+ op . setState ( HostOp . STATES . Finished_Fail )
234261 }
235262
236263 } else if ( op . op === 'subscribe' && this . state === PeerComms . STATES . AUTHED ) {
237264
238265 if ( this . party . topics ) {
239266 await this . party . topics . subscribe . bind ( this . party . topics ) ( this , op . input . topic )
267+ op . setState ( HostOp . STATES . Finished_Success )
268+ }
269+ else {
270+ op . setState ( HostOp . STATES . Finished_Fail )
240271 }
241272
242273 } else if ( op . op === 'unsubscribe' && this . state === PeerComms . STATES . AUTHED ) {
243274
244275 if ( this . party . topics ) {
245276 await this . party . topics . unsubscribe ( this , op . input . topic )
277+ op . setState ( HostOp . STATES . Finished_Success )
278+ }
279+ else {
280+ op . setState ( HostOp . STATES . Finished_Fail )
246281 }
247282
248283 } else if ( op . op === 'publish' && this . state === PeerComms . STATES . AUTHED ) {
249284
250285 if ( this . party . topics ) {
251286 await this . party . topics . publish ( this , op . input . topic , op . input . msg )
287+ op . setState ( HostOp . STATES . Finished_Success )
288+ }
289+ else {
290+ op . setState ( HostOp . STATES . Finished_Fail )
252291 }
253292
254293 } else {
@@ -283,7 +322,7 @@ class PeerComms extends SocketComms {
283322 debug ( 'calling runner' )
284323
285324 if ( op . input . endpoint == 'api-v2-peer-bouncer' ) {
286- debug ( 'ask->' , op . input . data )
325+ debug ( 'ask->' , truncateString ( op . input . data , 1024 ) )
287326 op . result = { result : await this . party . handleCall ( op . input . data ) }
288327
289328 op . setState ( HostOp . STATES . Finished_Success )
0 commit comments