@@ -24,7 +24,7 @@ import { FirebaseApp, initializeApp } from '@firebase/app';
24
24
25
25
import { Firestore , initializeFirestore , setLogLevel } from '../../../src' ;
26
26
import { PrivateSettings } from '../../../src/lite-api/settings' ;
27
- import { logDebug } from '../../../src/util/log' ;
27
+ import { logDebug , logError } from '../../../src/util/log' ;
28
28
import { generateUniqueDebugId } from '../../../src/util/debug_uid' ;
29
29
30
30
// TODO(dimond): Right now we create a new app and Firestore instance for
@@ -92,12 +92,12 @@ class ReadableStreamSpy<Uint8Array> {
92
92
}
93
93
}
94
94
95
- class RequestInfo { }
96
-
97
95
globalThis . fetch = async function ( requestOrUrl , options ) {
98
96
let url = '' ;
99
97
let verb = 'GET' ;
100
98
let request = requestOrUrl ;
99
+ let bodyPromise : Promise < string > = Promise . resolve ( 'none' ) ;
100
+ let contentType : string = '' ;
101
101
const id = generateUniqueDebugId ( ) ;
102
102
103
103
if ( typeof requestOrUrl === 'string' ) {
@@ -107,31 +107,9 @@ globalThis.fetch = async function (requestOrUrl, options) {
107
107
} else {
108
108
url = requestOrUrl . url ;
109
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
- }
110
+ bodyPromise = requestOrUrl . clone ( ) . text ( ) ;
111
+ contentType = requestOrUrl . headers . get ( 'Content-Type' ) ?? 'empty' ;
112
+ request = requestOrUrl ;
135
113
}
136
114
137
115
if (
@@ -143,6 +121,20 @@ globalThis.fetch = async function (requestOrUrl, options) {
143
121
144
122
const response = await originalFetch ( request , options ) ;
145
123
124
+ bodyPromise . then ( bodyText => {
125
+ if ( contentType === 'application/x-www-form-urlencoded' ) {
126
+ const decodedData : Record < string , string > = { } ;
127
+
128
+ bodyText . split ( '&' ) . forEach ( pair => {
129
+ const [ key , value ] = pair . split ( '=' ) ;
130
+ decodedData [ key ] = decodeURIComponent ( value ) ;
131
+ } ) ;
132
+ logDebug ( `(fetch: ${ id } ) BODY: ${ JSON . stringify ( decodedData ) } ` ) ;
133
+ } else {
134
+ logDebug ( `(fetch: ${ id } ) BODY: ${ bodyText } ` ) ;
135
+ }
136
+ } ) ;
137
+
146
138
if ( response . body ) {
147
139
const spy = new ReadableStreamSpy ( 'response' , response . body , id ) ;
148
140
0 commit comments