-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
142 lines (125 loc) · 5.5 KB
/
index.html
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous">
</head>
<body class="container-fluid">
<h1>Mercure Debugger</h1>
<div class="form-group row">
<label for="huburl" class="col-sm-2 col-form-label">Hub url</label>
<div class="col-sm-10">
<input type="text" id="huburl" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="topics" class="col-sm-2 col-form-label">Topics (one per line)</label>
<div class="col-sm-10">
<textarea id="topics" class="form-control"></textarea>
</div>
</div>
<div class="form-group row">
<label for="events" class="col-sm-2 col-form-label">Events (one per line)</label>
<div class="col-sm-10">
<textarea id="events" class="form-control"></textarea>
</div>
</div>
<div class="form-group row">
<label for="jwt" class="col-sm-2 col-form-label">JWT</label>
<div class="col-sm-10">
<textarea id="jwt" class="form-control"></textarea>
</div>
</div>
<div class="form-group row">
<label for="jwt" class="col-sm-2 col-form-label">Subscription URL</label>
<div class="col-sm-10">
<div id="finalurl" class="alert alert-primary"></div>
</div>
</div>
<h2>Events</h2>
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Type</th>
<th>Data</th>
</tr>
</thead>
<tbody id="body">
</tbody>
</table>
<script src="eventsource-polyfill.js" type="text/javascript"></script>
<script>
(function () {
const debounce = (delay, callback) => {
let timerId = null;
return function () {
if (null !== timerId) {
clearTimeout(timerId);
}
timerId = setTimeout(callback.bind(this, ...arguments), delay);
}
};
const jwtInput = document.getElementById('jwt');
const hubUrlInput = document.getElementById('huburl');
const topicsInput = document.getElementById('topics');
const eventsInput = document.getElementById('events');
const finalUrl = document.getElementById('finalurl');
const tableBody = document.getElementById('body');
let es = null;
const init = () => {
const headers = {};
const jwt = jwtInput.value;
const topics = topicsInput.value.split("\n").filter(t => t);
const events = eventsInput.value.split("\n").filter(t => t);
const hubUrl = hubUrlInput.value;
if (jwt) {
headers.Authorization = `Bearer ${jwt}`;
}
if (null !== es) {
es.close();
}
let url = null;
try {
url = new URL(hubUrl)
} catch (e) {
finalUrl.innerText = 'Invalid URL';
return;
}
for (let topic of topics) {
url.searchParams.append('topic', topic);
}
finalUrl.innerText = url.toString();
es = new EventSourcePolyfill(url.toString(), {headers});
for (let type of events) {
es.addEventListener(type, (e) => {
const tr = document.createElement('tr');
const dateCol = document.createElement('td');
const typeCol = document.createElement('td');
const dataCol = document.createElement('td');
tr.append(dateCol);
tr.append(typeCol);
tr.append(dataCol);
dateCol.innerText = new Date().toISOString();
typeCol.innerText = e.type;
dataCol.innerText = e.data;
tableBody.append(tr);
console.log(e);
});
}
};
jwtInput.addEventListener('input', debounce(300, () => init()));
hubUrlInput.addEventListener('input', debounce(300, () => init()));
topicsInput.addEventListener('input', debounce(300, () => init()));
eventsInput.addEventListener('input', debounce(300, () => init()));
init();
})();
</script>
</body>
</html>