@@ -49,7 +49,11 @@ class ReadableStreamSpy<Uint8Array> {
49
49
* @param inputReadableStream The ReadableStream to spy on.
50
50
* @param id id for logging
51
51
*/
52
- constructor ( inputReadableStream : ReadableStream < Uint8Array > , id : string ) {
52
+ constructor (
53
+ type : 'request' | 'response' ,
54
+ inputReadableStream : ReadableStream < Uint8Array > ,
55
+ id : string
56
+ ) {
53
57
if ( ! ( inputReadableStream instanceof ReadableStream ) ) {
54
58
throw new Error ( 'Input must be a ReadableStream.' ) ;
55
59
}
@@ -63,7 +67,7 @@ class ReadableStreamSpy<Uint8Array> {
63
67
controller : TransformStreamDefaultController < Uint8Array >
64
68
) => {
65
69
// @ts -ignore
66
- logDebug ( `(fetch: ${ id } ) ${ this . decoder . decode ( chunk ) } ` ) ;
70
+ logDebug ( `${ type } : (fetch: ${ id } ) ${ this . decoder . decode ( chunk ) } ` ) ;
67
71
68
72
controller . enqueue ( chunk ) ; // Pass the chunk along
69
73
} ,
@@ -88,26 +92,59 @@ class ReadableStreamSpy<Uint8Array> {
88
92
}
89
93
}
90
94
95
+ class RequestInfo { }
96
+
91
97
globalThis . fetch = async function ( requestOrUrl , options ) {
92
- const url =
93
- typeof requestOrUrl === 'string'
94
- ? requestOrUrl
95
- : requestOrUrl instanceof URL
96
- ? requestOrUrl . toString ( )
97
- : requestOrUrl . url ;
98
+ let url = '' ;
99
+ let verb = 'GET' ;
100
+ let request = requestOrUrl ;
101
+ const id = generateUniqueDebugId ( ) ;
102
+
103
+ if ( typeof requestOrUrl === 'string' ) {
104
+ url = requestOrUrl ;
105
+ } else if ( requestOrUrl instanceof URL ) {
106
+ url = requestOrUrl . toString ( ) ;
107
+ } else {
108
+ url = requestOrUrl . url ;
109
+ verb = requestOrUrl . method ;
110
+ if (
111
+ url . startsWith (
112
+ 'https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel'
113
+ )
114
+ ) {
115
+ try {
116
+ if ( requestOrUrl . body instanceof ReadableStream ) {
117
+ const requestSpy = new ReadableStreamSpy (
118
+ 'request' ,
119
+ requestOrUrl . body ,
120
+ id
121
+ ) ;
122
+ const requestInit : RequestInit = {
123
+ body : requestSpy . readableStream ,
124
+ // @ts -ignore
125
+ duplex : 'half'
126
+ } ;
127
+ request = new Request ( requestOrUrl , requestInit ) ;
128
+ } else {
129
+ logDebug ( JSON . stringify ( requestOrUrl . body ) ) ;
130
+ }
131
+ } catch ( e ) {
132
+ console . log ( e ) ;
133
+ }
134
+ }
135
+ }
98
136
99
137
if (
100
138
url . startsWith (
101
139
'https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel'
102
140
)
103
141
) {
104
- const response = await originalFetch ( requestOrUrl , options ) ;
142
+ logDebug ( `(fetch: ${ id } ) FETCH FOR ${ verb } ${ url } ` ) ;
105
143
106
- if ( response . body ) {
107
- const id = generateUniqueDebugId ( ) ;
108
- logDebug ( `(fetch: ${ id } ) FETCH FOR ${ url } ` ) ;
144
+ const response = await originalFetch ( request , options ) ;
109
145
110
- const spy = new ReadableStreamSpy ( response . body , id ) ;
146
+ if ( response . body ) {
147
+ const spy = new ReadableStreamSpy ( 'response' , response . body , id ) ;
111
148
112
149
return Promise . resolve (
113
150
new Response ( spy . readableStream , {
@@ -125,7 +162,7 @@ globalThis.fetch = async function (requestOrUrl, options) {
125
162
} ;
126
163
127
164
// enable contextual debug logging
128
- setLogLevel ( 'error' , 200 ) ;
165
+ setLogLevel ( 'error' , 400 ) ;
129
166
130
167
export function newTestApp ( projectId : string , appName ?: string ) : FirebaseApp {
131
168
if ( appName === undefined ) {
0 commit comments