-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
62 lines (53 loc) · 1.53 KB
/
index.js
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
import XHRInterceptor from 'react-native/Libraries/Network/XHRInterceptor';
function convertURLStringToHash(data) {
const formattedData = {};
const dataArr = data.split('&');
dataArr.forEach(d => {
const item = d.split('=');
const key = item[0];
const value = item[1];
formattedData[key] = value;
});
return formattedData;
}
export function start(config = {
host: 'localhost',
port: 6969,
}) {
const ws = new WebSocket(`ws://${config.host}:${config.port}`);
const defaultMessage = {
requestHeaders: {},
responseHeaders: {},
};
let message = defaultMessage;
XHRInterceptor.setOpenCallback((protocol, url) => {
message.protocol = protocol;
message.url = url;
});
XHRInterceptor.setSendCallback((d) => {
try {
message.data = convertURLStringToHash(d);
} catch (e) {
if (d) {
message.data = d;
}
}
});
XHRInterceptor.setRequestHeaderCallback((header, value) => {
message.requestHeaders[header] = value;
});
XHRInterceptor.setHeaderReceivedCallback((header, value) => {
message.responseHeaders[header] = value;
});
XHRInterceptor.setResponseCallback((status, _, payload) => {
message.response = JSON.parse(payload);
message.requestHeaders = JSON.stringify(message.requestHeaders);
message.responseHeaders = JSON.stringify(message.responseHeaders);
ws.send(JSON.stringify(message));
message = defaultMessage;
});
XHRInterceptor.enableInterception();
}
export function stop() {
XHRInterceptor.disableInterception();
}