-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
111 lines (87 loc) · 2.65 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
import {LogEntry, LogLevel, LogType} from "./interfaces"
import { createInterceptor } from '@mswjs/interceptors'
import nodeInterceptors from '@mswjs/interceptors/lib/presets/node'
import axios from "axios"
import * as fs from "fs"
import {type} from "os";
import {timeStamp} from "console";
const port = 1234;
const interceptor = createInterceptor({
modules: nodeInterceptors,
resolver(request, ref) {},
})
const log = console.log
console.log = (...params) => {
const body = params.map((param) => {
try{ return JSON.parse(param)} catch {
return param.toString()
}})
const logEntry: LogEntry = {
body: body,
duration: 0,
level: LogLevel.INFO,
timestamp: new Date(),
project: process.cwd(),
type: LogType.NETWORK,
stack: ""
}
sendToAllClients(logEntry)
log(...params)
}
interceptor.on("request", (request) => {
log('[%s] %s', request.method, request.url.toString())
})
interceptor.on("response",async (response) => {
const logEntry: LogEntry = {
body: [JSON.stringify({
response
})],
level: LogLevel.INFO,
duration: 0,
timestamp: new Date(),
project: process.cwd(),
type: LogType.NETWORK,
stack: ""
}
sendToAllClients(logEntry)
})
interceptor.apply()
const sendToAllClients = (newLog: LogEntry) => clients.forEach((client) => client.response.write(`data: ${JSON.stringify(newLog)}\n\n`))
let clients = [];
const logs: LogEntry[] = [];
app.use(cors())
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: false }))
.post('/log-entry', (request, response )=> {
const newLog = request.body;
logs.push(newLog);
response.json(newLog);
return ;
})
.get('/', (request, response) => {
const headers = {
'Content-Type': 'text/event-stream',
Connection: 'keep-alive',
'Cache-Control': 'no-cache',
};
response.writeHead(200, headers);
const data = `data: ${JSON.stringify(logs)}\n\n`;
response.write(data);
const clientId = Date.now();
const newClient = {
id: clientId,
response,
};
clients.push(newClient);
request.on('close', () => {
log(`${clientId} Connection closed`);
clients = clients.filter((client) => client.id !== clientId);
});
});
app.listen(port, () => {
log(`Example app listening on port ${port}`);
});