@@ -41,12 +41,13 @@ import {
41
41
} from '../../remote/rpc_error' ;
42
42
import { StreamBridge } from '../../remote/stream_bridge' ;
43
43
import { fail , hardAssert } from '../../util/assert' ;
44
+ import { generateUniqueDebugId } from '../../util/debug_uid' ;
44
45
import { Code , FirestoreError } from '../../util/error' ;
45
46
import { logDebug , logWarn } from '../../util/log' ;
46
47
import { Rejecter , Resolver } from '../../util/promise' ;
47
48
import { StringMap } from '../../util/types' ;
48
49
49
- const LOG_TAG = 'Connection ' ;
50
+ const LOG_TAG = 'WebChannelConnection ' ;
50
51
51
52
const RPC_STREAM_SERVICE = 'google.firestore.v1.Firestore' ;
52
53
@@ -70,6 +71,7 @@ export class WebChannelConnection extends RestConnection {
70
71
headers : StringMap ,
71
72
body : Req
72
73
) : Promise < Resp > {
74
+ const streamId = generateUniqueDebugId ( ) ;
73
75
return new Promise ( ( resolve : Resolver < Resp > , reject : Rejecter ) => {
74
76
const xhr = new XhrIo ( ) ;
75
77
xhr . setWithCredentials ( true ) ;
@@ -78,11 +80,15 @@ export class WebChannelConnection extends RestConnection {
78
80
switch ( xhr . getLastErrorCode ( ) ) {
79
81
case ErrorCode . NO_ERROR :
80
82
const json = xhr . getResponseJson ( ) as Resp ;
81
- logDebug ( LOG_TAG , 'XHR received:' , JSON . stringify ( json ) ) ;
83
+ logDebug (
84
+ LOG_TAG ,
85
+ `XHR for RPC '${ rpcName } ' ${ streamId } received:` ,
86
+ JSON . stringify ( json )
87
+ ) ;
82
88
resolve ( json ) ;
83
89
break ;
84
90
case ErrorCode . TIMEOUT :
85
- logDebug ( LOG_TAG , ' RPC "' + rpcName + '" timed out' ) ;
91
+ logDebug ( LOG_TAG , ` RPC ' ${ rpcName } ' ${ streamId } timed out` ) ;
86
92
reject (
87
93
new FirestoreError ( Code . DEADLINE_EXCEEDED , 'Request time out' )
88
94
) ;
@@ -91,7 +97,7 @@ export class WebChannelConnection extends RestConnection {
91
97
const status = xhr . getStatus ( ) ;
92
98
logDebug (
93
99
LOG_TAG ,
94
- ' RPC "' + rpcName + '" failed with status:' ,
100
+ ` RPC ' ${ rpcName } ' ${ streamId } failed with status:` ,
95
101
status ,
96
102
'response text:' ,
97
103
xhr . getResponseText ( )
@@ -134,22 +140,21 @@ export class WebChannelConnection extends RestConnection {
134
140
break ;
135
141
default :
136
142
fail (
137
- 'RPC "' +
138
- rpcName +
139
- '" failed with unanticipated ' +
140
- 'webchannel error ' +
143
+ `RPC '${ rpcName } ' ${ streamId } ` +
144
+ 'failed with unanticipated webchannel error: ' +
141
145
xhr . getLastErrorCode ( ) +
142
146
': ' +
143
147
xhr . getLastError ( ) +
144
148
', giving up.'
145
149
) ;
146
150
}
147
151
} finally {
148
- logDebug ( LOG_TAG , ' RPC "' + rpcName + '" completed.' ) ;
152
+ logDebug ( LOG_TAG , ` RPC ' ${ rpcName } ' ${ streamId } completed.` ) ;
149
153
}
150
154
} ) ;
151
155
152
156
const requestString = JSON . stringify ( body ) ;
157
+ logDebug ( LOG_TAG , `RPC '${ rpcName } ' ${ streamId } sending request:` , body ) ;
153
158
xhr . send ( url , 'POST' , requestString , headers , XHR_TIMEOUT_SECS ) ;
154
159
} ) ;
155
160
}
@@ -159,6 +164,7 @@ export class WebChannelConnection extends RestConnection {
159
164
authToken : Token | null ,
160
165
appCheckToken : Token | null
161
166
) : Stream < Req , Resp > {
167
+ const streamId = generateUniqueDebugId ( ) ;
162
168
const urlParts = [
163
169
this . baseUrl ,
164
170
'/' ,
@@ -217,7 +223,11 @@ export class WebChannelConnection extends RestConnection {
217
223
request . encodeInitMessageHeaders = true ;
218
224
219
225
const url = urlParts . join ( '' ) ;
220
- logDebug ( LOG_TAG , 'Creating WebChannel: ' + url , request ) ;
226
+ logDebug (
227
+ LOG_TAG ,
228
+ `Creating RPC '${ rpcName } ' stream ${ streamId } : ${ url } ` ,
229
+ request
230
+ ) ;
221
231
const channel = webchannelTransport . createWebChannel ( url , request ) ;
222
232
223
233
// WebChannel supports sending the first message with the handshake - saving
@@ -236,14 +246,26 @@ export class WebChannelConnection extends RestConnection {
236
246
sendFn : ( msg : Req ) => {
237
247
if ( ! closed ) {
238
248
if ( ! opened ) {
239
- logDebug ( LOG_TAG , 'Opening WebChannel transport.' ) ;
249
+ logDebug (
250
+ LOG_TAG ,
251
+ `Opening RPC '${ rpcName } ' stream ${ streamId } transport.`
252
+ ) ;
240
253
channel . open ( ) ;
241
254
opened = true ;
242
255
}
243
- logDebug ( LOG_TAG , 'WebChannel sending:' , msg ) ;
256
+ logDebug (
257
+ LOG_TAG ,
258
+ `RPC '${ rpcName } ' stream ${ streamId } sending:` ,
259
+ msg
260
+ ) ;
244
261
channel . send ( msg ) ;
245
262
} else {
246
- logDebug ( LOG_TAG , 'Not sending because WebChannel is closed:' , msg ) ;
263
+ logDebug (
264
+ LOG_TAG ,
265
+ `Not sending because RPC '${ rpcName } ' stream ${ streamId } ` +
266
+ 'is closed:' ,
267
+ msg
268
+ ) ;
247
269
}
248
270
} ,
249
271
closeFn : ( ) => channel . close ( )
@@ -273,22 +295,32 @@ export class WebChannelConnection extends RestConnection {
273
295
274
296
unguardedEventListen ( channel , WebChannel . EventType . OPEN , ( ) => {
275
297
if ( ! closed ) {
276
- logDebug ( LOG_TAG , 'WebChannel transport opened.' ) ;
298
+ logDebug (
299
+ LOG_TAG ,
300
+ `RPC '${ rpcName } ' stream ${ streamId } transport opened.`
301
+ ) ;
277
302
}
278
303
} ) ;
279
304
280
305
unguardedEventListen ( channel , WebChannel . EventType . CLOSE , ( ) => {
281
306
if ( ! closed ) {
282
307
closed = true ;
283
- logDebug ( LOG_TAG , 'WebChannel transport closed' ) ;
308
+ logDebug (
309
+ LOG_TAG ,
310
+ `RPC '${ rpcName } ' stream ${ streamId } transport closed`
311
+ ) ;
284
312
streamBridge . callOnClose ( ) ;
285
313
}
286
314
} ) ;
287
315
288
316
unguardedEventListen < Error > ( channel , WebChannel . EventType . ERROR , err => {
289
317
if ( ! closed ) {
290
318
closed = true ;
291
- logWarn ( LOG_TAG , 'WebChannel transport errored:' , err ) ;
319
+ logWarn (
320
+ LOG_TAG ,
321
+ `RPC '${ rpcName } ' stream ${ streamId } transport errored:` ,
322
+ err
323
+ ) ;
292
324
streamBridge . callOnClose (
293
325
new FirestoreError (
294
326
Code . UNAVAILABLE ,
@@ -322,7 +354,11 @@ export class WebChannelConnection extends RestConnection {
322
354
msgDataOrError . error ||
323
355
( msgDataOrError as WebChannelError [ ] ) [ 0 ] ?. error ;
324
356
if ( error ) {
325
- logDebug ( LOG_TAG , 'WebChannel received error:' , error ) ;
357
+ logDebug (
358
+ LOG_TAG ,
359
+ `RPC '${ rpcName } ' stream ${ streamId } received error:` ,
360
+ error
361
+ ) ;
326
362
// error.status will be a string like 'OK' or 'NOT_FOUND'.
327
363
const status : string = error . status ;
328
364
let code = mapCodeFromRpcStatus ( status ) ;
@@ -340,7 +376,11 @@ export class WebChannelConnection extends RestConnection {
340
376
streamBridge . callOnClose ( new FirestoreError ( code , message ) ) ;
341
377
channel . close ( ) ;
342
378
} else {
343
- logDebug ( LOG_TAG , 'WebChannel received:' , msgData ) ;
379
+ logDebug (
380
+ LOG_TAG ,
381
+ `RPC '${ rpcName } ' stream ${ streamId } received:` ,
382
+ msgData
383
+ ) ;
344
384
streamBridge . callOnMessage ( msgData ) ;
345
385
}
346
386
}
@@ -349,9 +389,15 @@ export class WebChannelConnection extends RestConnection {
349
389
350
390
unguardedEventListen < StatEvent > ( requestStats , Event . STAT_EVENT , event => {
351
391
if ( event . stat === Stat . PROXY ) {
352
- logDebug ( LOG_TAG , 'Detected buffering proxy' ) ;
392
+ logDebug (
393
+ LOG_TAG ,
394
+ `RPC '${ rpcName } ' stream ${ streamId } detected buffering proxy`
395
+ ) ;
353
396
} else if ( event . stat === Stat . NOPROXY ) {
354
- logDebug ( LOG_TAG , 'Detected no buffering proxy' ) ;
397
+ logDebug (
398
+ LOG_TAG ,
399
+ `RPC '${ rpcName } ' stream ${ streamId } detected no buffering proxy`
400
+ ) ;
355
401
}
356
402
} ) ;
357
403
0 commit comments