Skip to content

Commit b8fc51d

Browse files
committed
logs
1 parent cd7183a commit b8fc51d

File tree

1 file changed

+63
-27
lines changed

1 file changed

+63
-27
lines changed

vitest.browser.config.mts

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,68 @@ export default defineConfig({
268268
// tsconfigPaths({
269269
// projects: ['./tsconfig.spec.json'],
270270
// }),
271+
{
272+
name: 'request-response-logger',
273+
enforce: 'pre',
274+
configureServer(server) {
275+
console.log('[Request/Response Logger] Enabled');
276+
277+
let requestCounter = 0;
278+
server.middlewares.use((req, res, next) => {
279+
const url = req.url || '';
280+
const method = req.method || '';
281+
const requestTime = new Date().toISOString();
282+
const requestId = ++requestCounter;
283+
284+
// Log incoming request
285+
console.log('→'.repeat(40));
286+
console.log(`[INCOMING REQUEST #${requestId}] ${method} ${url}`);
287+
console.log(`Time: ${requestTime}`);
288+
console.log('→'.repeat(40));
289+
290+
const originalWrite = res.write;
291+
const originalEnd = res.end;
292+
const chunks: any[] = [];
293+
294+
// @ts-ignore
295+
res.write = function(chunk: any, ..._args: any[]) {
296+
chunks.push(Buffer.from(chunk));
297+
return true;
298+
};
299+
300+
// @ts-ignore
301+
res.end = function(chunk: any, ...args: any[]) {
302+
if (chunk) {
303+
chunks.push(Buffer.from(chunk));
304+
}
305+
306+
const buffer = Buffer.concat(chunks);
307+
const body = buffer.toString('utf8');
308+
309+
// Log outgoing response
310+
const contentType = res.getHeader('content-type')?.toString() || 'unknown';
311+
const statusCode = res.statusCode;
312+
const contentLength = res.getHeader('content-length') || buffer.length;
313+
const responseTime = new Date().toISOString();
314+
315+
console.log('←'.repeat(40));
316+
console.log(`[OUTGOING RESPONSE #${requestId}] ${method} ${url}`);
317+
console.log(`Status: ${statusCode}`);
318+
console.log(`Content-Type: ${contentType}`);
319+
console.log(`Content-Length: ${contentLength}`);
320+
console.log(`Time: ${responseTime}`);
321+
console.log('←'.repeat(40));
322+
323+
// Restore original methods and send response
324+
res.write = originalWrite;
325+
res.end = originalEnd;
326+
res.end(body, ...args);
327+
};
328+
329+
next();
330+
});
331+
},
332+
},
271333
{
272334
name: 'console-capture-plugin',
273335
enforce: 'pre', // Run before other plugins
@@ -307,19 +369,7 @@ export default defineConfig({
307369
});
308370

309371
// Add middleware to inject console-capture script into HTML responses
310-
let requestCounter = 0;
311-
server.middlewares.use((req, res, next) => {
312-
const url = req.url || '';
313-
const method = req.method || '';
314-
const requestTime = new Date().toISOString();
315-
const requestId = ++requestCounter;
316-
317-
// Log incoming request
318-
console.log('→'.repeat(40));
319-
console.log(`[INCOMING REQUEST #${requestId}] ${method} ${url}`);
320-
console.log(`Time: ${requestTime}`);
321-
console.log('→'.repeat(40));
322-
372+
server.middlewares.use((_req, res, next) => {
323373
const originalWrite = res.write;
324374
const originalEnd = res.end;
325375
const chunks: any[] = [];
@@ -348,20 +398,6 @@ export default defineConfig({
348398
}
349399
}
350400

351-
// Log outgoing response
352-
const contentType = res.getHeader('content-type')?.toString() || 'unknown';
353-
const statusCode = res.statusCode;
354-
const contentLength = res.getHeader('content-length') || buffer.length;
355-
const responseTime = new Date().toISOString();
356-
357-
console.log('←'.repeat(40));
358-
console.log(`[OUTGOING RESPONSE #${requestId}] ${method} ${url}`);
359-
console.log(`Status: ${statusCode}`);
360-
console.log(`Content-Type: ${contentType}`);
361-
console.log(`Content-Length: ${contentLength}`);
362-
console.log(`Time: ${responseTime}`);
363-
console.log('←'.repeat(40));
364-
365401
// Restore original methods and send response
366402
res.write = originalWrite;
367403
res.end = originalEnd;

0 commit comments

Comments
 (0)