Skip to content

Commit f5d43bf

Browse files
committed
get request body
1 parent a03b666 commit f5d43bf

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

packages/firestore/src/util/log.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,14 @@ const bufferingLogHandler: LogHandler = (instance, logType, ...args): void => {
213213
}
214214

215215
// Buffer any messages less than the current logLevel
216-
if (logType < instance.logLevel) {
216+
//if (logType < instance.logLevel) {
217+
let codeFound = false;
218+
args.forEach(v => {
219+
if (typeof v === 'string' && /ca9/.test(v)) {
220+
codeFound = true;
221+
}
222+
});
223+
if (!codeFound) {
217224
logBuffer!.add({ level: logType, now, args });
218225
return;
219226
}

packages/firestore/test/integration/util/firebase_export.ts

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ class ReadableStreamSpy<Uint8Array> {
4949
* @param inputReadableStream The ReadableStream to spy on.
5050
* @param id id for logging
5151
*/
52-
constructor(inputReadableStream: ReadableStream<Uint8Array>, id: string) {
52+
constructor(
53+
type: 'request' | 'response',
54+
inputReadableStream: ReadableStream<Uint8Array>,
55+
id: string
56+
) {
5357
if (!(inputReadableStream instanceof ReadableStream)) {
5458
throw new Error('Input must be a ReadableStream.');
5559
}
@@ -63,7 +67,7 @@ class ReadableStreamSpy<Uint8Array> {
6367
controller: TransformStreamDefaultController<Uint8Array>
6468
) => {
6569
// @ts-ignore
66-
logDebug(`(fetch: ${id}) ${this.decoder.decode(chunk)}`);
70+
logDebug(`${type}: (fetch: ${id}) ${this.decoder.decode(chunk)}`);
6771

6872
controller.enqueue(chunk); // Pass the chunk along
6973
},
@@ -88,26 +92,59 @@ class ReadableStreamSpy<Uint8Array> {
8892
}
8993
}
9094

95+
class RequestInfo {}
96+
9197
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+
}
98136

99137
if (
100138
url.startsWith(
101139
'https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel'
102140
)
103141
) {
104-
const response = await originalFetch(requestOrUrl, options);
142+
logDebug(`(fetch: ${id}) FETCH FOR ${verb} ${url}`);
105143

106-
if (response.body) {
107-
const id = generateUniqueDebugId();
108-
logDebug(`(fetch: ${id}) FETCH FOR ${url}`);
144+
const response = await originalFetch(request, options);
109145

110-
const spy = new ReadableStreamSpy(response.body, id);
146+
if (response.body) {
147+
const spy = new ReadableStreamSpy('response', response.body, id);
111148

112149
return Promise.resolve(
113150
new Response(spy.readableStream, {
@@ -125,7 +162,7 @@ globalThis.fetch = async function (requestOrUrl, options) {
125162
};
126163

127164
// enable contextual debug logging
128-
setLogLevel('error', 200);
165+
setLogLevel('error', 400);
129166

130167
export function newTestApp(projectId: string, appName?: string): FirebaseApp {
131168
if (appName === undefined) {

0 commit comments

Comments
 (0)