tricache 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +144 -1428
- package/bin/tricache.js +8 -8
- package/dashboards/tricache-alerts.yaml +54 -0
- package/dashboards/tricache-grafana.json +548 -0
- package/dist/cache-service-5MZXFIGK.js +12 -0
- package/dist/cache-service-CyLdpo77.d.ts +871 -0
- package/dist/{chunk-3XQUQBOZ.js → chunk-6PQ5MOWR.js} +2950 -523
- package/dist/chunk-7RV5ZVBB.js +1023 -0
- package/dist/{chunk-PFB5FMEY.js → chunk-LJ5QJB5O.js} +116 -20
- package/dist/chunk-QNU7B43J.js +687 -0
- package/dist/chunk-UGD4VEPD.js +138 -0
- package/dist/chunk-XZL45ABR.js +108 -0
- package/dist/chunk-ZTSWCFYO.js +135 -0
- package/dist/cli.cjs +2894 -518
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +103 -5
- package/dist/dashboard/index.cjs +1065 -0
- package/dist/dashboard/index.d.ts +183 -0
- package/dist/dashboard/index.js +22 -0
- package/dist/drizzle/index.cjs +2648 -508
- package/dist/drizzle/index.d.ts +3 -1
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +1498 -0
- package/dist/edge/index.d.ts +127 -0
- package/dist/edge/index.js +1109 -0
- package/dist/hono-DQRS1Xd7.d.ts +370 -0
- package/dist/http/index.cjs +3038 -589
- package/dist/http/index.d.ts +109 -23
- package/dist/http/index.js +238 -62
- package/dist/index.cjs +6962 -2487
- package/dist/index.d.ts +547 -30
- package/dist/index.js +104 -5
- package/dist/murmur3-CMGw9PbV.d.ts +98 -0
- package/dist/nestjs/index.cjs +2586 -498
- package/dist/nestjs/index.d.ts +3 -1
- package/dist/nestjs/index.js +6 -3
- package/dist/next/index.cjs +2633 -519
- package/dist/next/index.d.ts +13 -7
- package/dist/next/index.js +45 -18
- package/dist/prisma/index.cjs +2 -1
- package/dist/prisma/index.d.ts +3 -1
- package/dist/prisma/index.js +2 -1
- package/dist/serialize-worker.cjs +31 -19
- package/dist/serialize-worker.js +1 -1
- package/dist/{cache-service-BblHuF3i.d.ts → types-B6krw64X.d.ts} +605 -646
- package/package.json +37 -7
- package/src/adapters/auto-pipeliner.ts +206 -0
- package/src/adapters/node-redis.ts +370 -0
- package/src/cache-service.ts +4231 -3226
- package/src/cli.ts +261 -143
- package/src/codec.ts +72 -0
- package/src/compression.ts +56 -63
- package/src/cross-region.ts +245 -0
- package/src/dashboard/handler.ts +392 -0
- package/src/dashboard/html.ts +594 -0
- package/src/dashboard/index.ts +195 -0
- package/src/dashboard/types.ts +96 -0
- package/src/disk-tier.ts +1223 -821
- package/src/drizzle/index.ts +86 -86
- package/src/edge/adapters/cloudflare-do.ts +130 -0
- package/src/edge/adapters/cloudflare-kv.ts +115 -0
- package/src/edge/adapters/upstash.ts +143 -0
- package/src/edge/cache.ts +797 -0
- package/src/edge/crypto.ts +182 -0
- package/src/edge/hono.ts +203 -0
- package/src/edge/index.ts +37 -0
- package/src/edge/types.ts +230 -0
- package/src/edge/utils/base64.ts +63 -0
- package/src/edge/utils/murmur3.ts +179 -0
- package/src/encryption.ts +635 -493
- package/src/http/express.ts +195 -0
- package/src/http/fastify.ts +260 -0
- package/src/http/index.ts +46 -223
- package/src/http/utils.ts +92 -0
- package/src/index.ts +149 -47
- package/src/ipc-telemetry.ts +481 -0
- package/src/latency-watchdog.ts +282 -0
- package/src/nestjs/decorators.ts +161 -161
- package/src/nestjs/index.ts +23 -23
- package/src/nestjs/tricache.module.ts +104 -104
- package/src/nestjs/tricache.store.ts +97 -97
- package/src/nestjs/types.ts +79 -79
- package/src/next/cache-handler.ts +271 -244
- package/src/next/index.ts +37 -37
- package/src/next/isr-handler.ts +105 -105
- package/src/next/types.ts +130 -130
- package/src/prisma/index.ts +154 -151
- package/src/remote-snapshot.ts +153 -0
- package/src/serialize-worker.ts +120 -120
- package/src/sigv4-snapshot-adapter.ts +260 -0
- package/src/smart-memory-cache.ts +1109 -1024
- package/src/types.ts +1199 -902
- package/src/utils/cgroup.ts +100 -0
- package/src/utils/tracing.ts +54 -0
- package/src/wasm/bloom-filter-wasm.ts +109 -102
- package/src/worker-pool.ts +299 -299
- package/src/wtiny-lfu.ts +624 -0
- package/dist/cache-service-AJASH5FX.js +0 -7
- package/dist/chunk-UBSR5UYA.js +0 -0
|
@@ -0,0 +1,1023 @@
|
|
|
1
|
+
// src/dashboard/index.ts
|
|
2
|
+
import http from "http";
|
|
3
|
+
|
|
4
|
+
// src/dashboard/handler.ts
|
|
5
|
+
import { createHash, timingSafeEqual } from "crypto";
|
|
6
|
+
import os from "os";
|
|
7
|
+
|
|
8
|
+
// src/dashboard/html.ts
|
|
9
|
+
import { gzipSync } from "zlib";
|
|
10
|
+
var DASHBOARD_HTML = `<!DOCTYPE html>
|
|
11
|
+
<html lang="en">
|
|
12
|
+
<head>
|
|
13
|
+
<meta charset="UTF-8" />
|
|
14
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
15
|
+
<title>TriCache Observability</title>
|
|
16
|
+
<style>
|
|
17
|
+
:root {
|
|
18
|
+
--bg: #0b0f17;
|
|
19
|
+
--card-bg: rgba(18, 24, 38, 0.75);
|
|
20
|
+
--card-border: rgba(255, 255, 255, 0.08);
|
|
21
|
+
--card-hover: rgba(255, 255, 255, 0.12);
|
|
22
|
+
--text: #f1f5f9;
|
|
23
|
+
--text-muted: #94a3b8;
|
|
24
|
+
--primary: #38bdf8;
|
|
25
|
+
--primary-glow: rgba(56, 189, 248, 0.25);
|
|
26
|
+
--success: #10b981;
|
|
27
|
+
--warning: #f59e0b;
|
|
28
|
+
--danger: #ef4444;
|
|
29
|
+
--l1-color: #38bdf8;
|
|
30
|
+
--disk-color: #a855f7;
|
|
31
|
+
--l2-color: #f97316;
|
|
32
|
+
--db-color: #ef4444;
|
|
33
|
+
--font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
|
|
34
|
+
}
|
|
35
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
36
|
+
body {
|
|
37
|
+
background-color: var(--bg);
|
|
38
|
+
color: var(--text);
|
|
39
|
+
font-family: var(--font);
|
|
40
|
+
line-height: 1.5;
|
|
41
|
+
padding: 1.5rem;
|
|
42
|
+
min-height: 100vh;
|
|
43
|
+
-webkit-font-smoothing: antialiased;
|
|
44
|
+
}
|
|
45
|
+
header {
|
|
46
|
+
display: flex;
|
|
47
|
+
flex-wrap: wrap;
|
|
48
|
+
justify-content: space-between;
|
|
49
|
+
align-items: center;
|
|
50
|
+
gap: 1rem;
|
|
51
|
+
margin-bottom: 2rem;
|
|
52
|
+
padding-bottom: 1.25rem;
|
|
53
|
+
border-bottom: 1px solid var(--card-border);
|
|
54
|
+
}
|
|
55
|
+
.brand {
|
|
56
|
+
display: flex;
|
|
57
|
+
align-items: center;
|
|
58
|
+
gap: 0.85rem;
|
|
59
|
+
}
|
|
60
|
+
.brand-logo {
|
|
61
|
+
width: 36px;
|
|
62
|
+
height: 36px;
|
|
63
|
+
border-radius: 8px;
|
|
64
|
+
background: linear-gradient(135deg, #0284c7, #818cf8);
|
|
65
|
+
display: flex;
|
|
66
|
+
align-items: center;
|
|
67
|
+
justify-content: center;
|
|
68
|
+
font-weight: 800;
|
|
69
|
+
color: #fff;
|
|
70
|
+
font-size: 1.1rem;
|
|
71
|
+
box-shadow: 0 0 16px var(--primary-glow);
|
|
72
|
+
}
|
|
73
|
+
.brand-text h1 {
|
|
74
|
+
font-size: 1.35rem;
|
|
75
|
+
font-weight: 700;
|
|
76
|
+
letter-spacing: -0.02em;
|
|
77
|
+
}
|
|
78
|
+
.brand-text p {
|
|
79
|
+
font-size: 0.8rem;
|
|
80
|
+
color: var(--text-muted);
|
|
81
|
+
}
|
|
82
|
+
.header-meta {
|
|
83
|
+
display: flex;
|
|
84
|
+
flex-wrap: wrap;
|
|
85
|
+
align-items: center;
|
|
86
|
+
gap: 0.75rem;
|
|
87
|
+
}
|
|
88
|
+
.badge {
|
|
89
|
+
display: inline-flex;
|
|
90
|
+
align-items: center;
|
|
91
|
+
gap: 0.4rem;
|
|
92
|
+
padding: 0.3rem 0.65rem;
|
|
93
|
+
border-radius: 9999px;
|
|
94
|
+
font-size: 0.75rem;
|
|
95
|
+
font-weight: 600;
|
|
96
|
+
background: rgba(255, 255, 255, 0.05);
|
|
97
|
+
border: 1px solid var(--card-border);
|
|
98
|
+
color: var(--text-muted);
|
|
99
|
+
}
|
|
100
|
+
.badge-live {
|
|
101
|
+
color: var(--success);
|
|
102
|
+
border-color: rgba(16, 185, 129, 0.3);
|
|
103
|
+
background: rgba(16, 185, 129, 0.1);
|
|
104
|
+
}
|
|
105
|
+
.pulse-dot {
|
|
106
|
+
width: 7px;
|
|
107
|
+
height: 7px;
|
|
108
|
+
border-radius: 50%;
|
|
109
|
+
background-color: currentColor;
|
|
110
|
+
box-shadow: 0 0 8px currentColor;
|
|
111
|
+
animation: pulse 2s infinite ease-in-out;
|
|
112
|
+
}
|
|
113
|
+
@keyframes pulse { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: 0.4; transform: scale(0.85); } }
|
|
114
|
+
.peer-select {
|
|
115
|
+
background: rgba(255, 255, 255, 0.06);
|
|
116
|
+
border: 1px solid var(--card-border);
|
|
117
|
+
color: var(--text);
|
|
118
|
+
padding: 0.3rem 0.65rem;
|
|
119
|
+
border-radius: 6px;
|
|
120
|
+
font-size: 0.8rem;
|
|
121
|
+
outline: none;
|
|
122
|
+
cursor: pointer;
|
|
123
|
+
}
|
|
124
|
+
.grid {
|
|
125
|
+
display: grid;
|
|
126
|
+
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
|
|
127
|
+
gap: 1.25rem;
|
|
128
|
+
margin-bottom: 2rem;
|
|
129
|
+
}
|
|
130
|
+
.card {
|
|
131
|
+
background: var(--card-bg);
|
|
132
|
+
backdrop-filter: blur(12px);
|
|
133
|
+
border: 1px solid var(--card-border);
|
|
134
|
+
border-radius: 12px;
|
|
135
|
+
padding: 1.25rem;
|
|
136
|
+
transition: border-color 0.2s, transform 0.2s;
|
|
137
|
+
}
|
|
138
|
+
.card:hover {
|
|
139
|
+
border-color: var(--card-hover);
|
|
140
|
+
}
|
|
141
|
+
.card-label {
|
|
142
|
+
font-size: 0.75rem;
|
|
143
|
+
font-weight: 600;
|
|
144
|
+
color: var(--text-muted);
|
|
145
|
+
text-transform: uppercase;
|
|
146
|
+
letter-spacing: 0.05em;
|
|
147
|
+
margin-bottom: 0.4rem;
|
|
148
|
+
display: flex;
|
|
149
|
+
justify-content: space-between;
|
|
150
|
+
align-items: center;
|
|
151
|
+
}
|
|
152
|
+
.card-value {
|
|
153
|
+
font-size: 1.85rem;
|
|
154
|
+
font-weight: 800;
|
|
155
|
+
letter-spacing: -0.03em;
|
|
156
|
+
color: var(--text);
|
|
157
|
+
}
|
|
158
|
+
.card-sub {
|
|
159
|
+
font-size: 0.8rem;
|
|
160
|
+
color: var(--text-muted);
|
|
161
|
+
margin-top: 0.35rem;
|
|
162
|
+
}
|
|
163
|
+
.tier-bar {
|
|
164
|
+
height: 10px;
|
|
165
|
+
border-radius: 5px;
|
|
166
|
+
background: rgba(255, 255, 255, 0.06);
|
|
167
|
+
overflow: hidden;
|
|
168
|
+
display: flex;
|
|
169
|
+
margin: 0.75rem 0;
|
|
170
|
+
}
|
|
171
|
+
.tier-segment { height: 100%; transition: width 0.4s ease; }
|
|
172
|
+
.tier-legend {
|
|
173
|
+
display: flex;
|
|
174
|
+
flex-wrap: wrap;
|
|
175
|
+
gap: 0.85rem;
|
|
176
|
+
font-size: 0.75rem;
|
|
177
|
+
color: var(--text-muted);
|
|
178
|
+
}
|
|
179
|
+
.tier-item { display: flex; align-items: center; gap: 0.35rem; }
|
|
180
|
+
.tier-color-dot { width: 8px; height: 8px; border-radius: 2px; }
|
|
181
|
+
.actions-panel {
|
|
182
|
+
display: flex;
|
|
183
|
+
flex-wrap: wrap;
|
|
184
|
+
gap: 0.75rem;
|
|
185
|
+
align-items: center;
|
|
186
|
+
margin-top: 1rem;
|
|
187
|
+
}
|
|
188
|
+
.btn {
|
|
189
|
+
background: rgba(255, 255, 255, 0.08);
|
|
190
|
+
border: 1px solid var(--card-border);
|
|
191
|
+
color: var(--text);
|
|
192
|
+
font-family: inherit;
|
|
193
|
+
font-size: 0.8rem;
|
|
194
|
+
font-weight: 600;
|
|
195
|
+
padding: 0.45rem 0.9rem;
|
|
196
|
+
border-radius: 6px;
|
|
197
|
+
cursor: pointer;
|
|
198
|
+
display: inline-flex;
|
|
199
|
+
align-items: center;
|
|
200
|
+
gap: 0.4rem;
|
|
201
|
+
transition: background 0.15s, border-color 0.15s;
|
|
202
|
+
}
|
|
203
|
+
.btn:hover:not(:disabled) {
|
|
204
|
+
background: rgba(255, 255, 255, 0.15);
|
|
205
|
+
border-color: rgba(255, 255, 255, 0.25);
|
|
206
|
+
}
|
|
207
|
+
.btn:disabled {
|
|
208
|
+
opacity: 0.4;
|
|
209
|
+
cursor: not-allowed;
|
|
210
|
+
}
|
|
211
|
+
.btn-danger {
|
|
212
|
+
background: rgba(239, 68, 68, 0.15);
|
|
213
|
+
border-color: rgba(239, 68, 68, 0.3);
|
|
214
|
+
color: #fca5a5;
|
|
215
|
+
}
|
|
216
|
+
.btn-danger:hover:not(:disabled) {
|
|
217
|
+
background: rgba(239, 68, 68, 0.25);
|
|
218
|
+
}
|
|
219
|
+
.dialog-overlay {
|
|
220
|
+
position: fixed;
|
|
221
|
+
inset: 0;
|
|
222
|
+
background: rgba(0, 0, 0, 0.7);
|
|
223
|
+
backdrop-filter: blur(4px);
|
|
224
|
+
display: none;
|
|
225
|
+
place-items: center;
|
|
226
|
+
z-index: 100;
|
|
227
|
+
}
|
|
228
|
+
.dialog-overlay.active { display: grid; }
|
|
229
|
+
.dialog {
|
|
230
|
+
background: #141c2e;
|
|
231
|
+
border: 1px solid var(--card-border);
|
|
232
|
+
border-radius: 12px;
|
|
233
|
+
padding: 1.5rem;
|
|
234
|
+
width: 100%;
|
|
235
|
+
max-width: 420px;
|
|
236
|
+
box-shadow: 0 20px 40px rgba(0,0,0,0.5);
|
|
237
|
+
}
|
|
238
|
+
.dialog h3 { font-size: 1.15rem; margin-bottom: 0.5rem; }
|
|
239
|
+
.dialog p { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 1rem; }
|
|
240
|
+
.dialog input {
|
|
241
|
+
width: 100%;
|
|
242
|
+
background: rgba(0, 0, 0, 0.3);
|
|
243
|
+
border: 1px solid var(--card-border);
|
|
244
|
+
border-radius: 6px;
|
|
245
|
+
padding: 0.5rem 0.75rem;
|
|
246
|
+
color: var(--text);
|
|
247
|
+
font-size: 0.9rem;
|
|
248
|
+
margin-bottom: 1rem;
|
|
249
|
+
outline: none;
|
|
250
|
+
}
|
|
251
|
+
.dialog-actions {
|
|
252
|
+
display: flex;
|
|
253
|
+
justify-content: flex-end;
|
|
254
|
+
gap: 0.5rem;
|
|
255
|
+
}
|
|
256
|
+
#toast {
|
|
257
|
+
position: fixed;
|
|
258
|
+
bottom: 1.5rem;
|
|
259
|
+
right: 1.5rem;
|
|
260
|
+
background: #1e293b;
|
|
261
|
+
border: 1px solid var(--card-border);
|
|
262
|
+
padding: 0.75rem 1.25rem;
|
|
263
|
+
border-radius: 8px;
|
|
264
|
+
font-size: 0.85rem;
|
|
265
|
+
box-shadow: 0 10px 25px rgba(0,0,0,0.4);
|
|
266
|
+
display: none;
|
|
267
|
+
z-index: 200;
|
|
268
|
+
animation: slideIn 0.2s ease-out;
|
|
269
|
+
}
|
|
270
|
+
@keyframes slideIn { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
|
|
271
|
+
</style>
|
|
272
|
+
</head>
|
|
273
|
+
<body>
|
|
274
|
+
<header>
|
|
275
|
+
<div class="brand">
|
|
276
|
+
<div class="brand-logo">\u0394</div>
|
|
277
|
+
<div class="brand-text">
|
|
278
|
+
<h1 id="ui-title">TriCache Observability</h1>
|
|
279
|
+
<p id="ui-subtitle">Connecting to live metric stream...</p>
|
|
280
|
+
</div>
|
|
281
|
+
</div>
|
|
282
|
+
<div class="header-meta">
|
|
283
|
+
<div class="badge" id="pod-badge">Pod: ...</div>
|
|
284
|
+
<div class="badge" id="uptime-badge">Uptime: 0s</div>
|
|
285
|
+
<div class="badge" id="cb-badge">L2: ...</div>
|
|
286
|
+
<div class="badge badge-live" id="stream-badge">
|
|
287
|
+
<span class="pulse-dot"></span>
|
|
288
|
+
<span id="stream-text">SSE Live</span>
|
|
289
|
+
</div>
|
|
290
|
+
<select id="peer-select" class="peer-select" style="display:none;" onchange="if(this.value) window.location.href=this.value;">
|
|
291
|
+
<option value="">Switch Pod...</option>
|
|
292
|
+
</select>
|
|
293
|
+
</div>
|
|
294
|
+
</header>
|
|
295
|
+
|
|
296
|
+
<section class="grid">
|
|
297
|
+
<!-- Stat 1: Overall Hit Ratio -->
|
|
298
|
+
<div class="card">
|
|
299
|
+
<div class="card-label">Hit Ratio <span>Global</span></div>
|
|
300
|
+
<div class="card-value" id="val-hit-ratio">0.0%</div>
|
|
301
|
+
<div class="card-sub" id="sub-hit-ratio">0 total gets</div>
|
|
302
|
+
<div class="tier-bar">
|
|
303
|
+
<div class="tier-segment" id="bar-l1" style="background:var(--l1-color); width:0%;"></div>
|
|
304
|
+
<div class="tier-segment" id="bar-disk" style="background:var(--disk-color); width:0%;"></div>
|
|
305
|
+
<div class="tier-segment" id="bar-l2" style="background:var(--l2-color); width:0%;"></div>
|
|
306
|
+
<div class="tier-segment" id="bar-db" style="background:var(--db-color); width:0%;"></div>
|
|
307
|
+
</div>
|
|
308
|
+
<div class="tier-legend">
|
|
309
|
+
<div class="tier-item"><span class="tier-color-dot" style="background:var(--l1-color);"></span> L1 RAM (<span id="pct-l1">0%</span>)</div>
|
|
310
|
+
<div class="tier-item"><span class="tier-color-dot" style="background:var(--disk-color);"></span> Disk (<span id="pct-disk">0%</span>)</div>
|
|
311
|
+
<div class="tier-item"><span class="tier-color-dot" style="background:var(--l2-color);"></span> L2 (<span id="pct-l2">0%</span>)</div>
|
|
312
|
+
<div class="tier-item"><span class="tier-color-dot" style="background:var(--db-color);"></span> DB (<span id="pct-db">0%</span>)</div>
|
|
313
|
+
</div>
|
|
314
|
+
</div>
|
|
315
|
+
|
|
316
|
+
<!-- Stat 2: Singleflight & Stampedes -->
|
|
317
|
+
<div class="card">
|
|
318
|
+
<div class="card-label">Stampedes Prevented <span>Singleflight</span></div>
|
|
319
|
+
<div class="card-value" id="val-stampedes">0</div>
|
|
320
|
+
<div class="card-sub" id="sub-stampedes">0 concurrent coalesced</div>
|
|
321
|
+
</div>
|
|
322
|
+
|
|
323
|
+
<!-- Stat 3: Memory Footprint & OOM Guard -->
|
|
324
|
+
<div class="card">
|
|
325
|
+
<div class="card-label">L1 Heap Memory <span>OOM Guard</span></div>
|
|
326
|
+
<div class="card-value" id="val-memory">0 MB</div>
|
|
327
|
+
<div class="card-sub" id="sub-memory">0 entries | 0 OOM rounds</div>
|
|
328
|
+
</div>
|
|
329
|
+
|
|
330
|
+
<!-- Stat 4: Invalidation Mesh -->
|
|
331
|
+
<div class="card">
|
|
332
|
+
<div class="card-label">Invalidation Mesh <span>Backplane</span></div>
|
|
333
|
+
<div class="card-value" id="val-backplane">0 / 0</div>
|
|
334
|
+
<div class="card-sub" id="sub-backplane">Sent / Received</div>
|
|
335
|
+
</div>
|
|
336
|
+
</section>
|
|
337
|
+
|
|
338
|
+
<!-- Actions & Control Bar -->
|
|
339
|
+
<div class="card">
|
|
340
|
+
<div class="card-label">Operational Controls & Invalidation</div>
|
|
341
|
+
<div class="actions-panel">
|
|
342
|
+
<button class="btn" id="btn-invalidate" onclick="openInvalidateModal()">\u{1F3F7}\uFE0F Invalidate Tag</button>
|
|
343
|
+
<button class="btn btn-danger" id="btn-clear" onclick="openClearModal()">\u{1F9F9} Clear Cache</button>
|
|
344
|
+
<button class="btn" onclick="fetchMetricsSnapshot()">\u{1F504} Refresh Snapshot</button>
|
|
345
|
+
</div>
|
|
346
|
+
</div>
|
|
347
|
+
|
|
348
|
+
<!-- Invalidate Modal -->
|
|
349
|
+
<div class="dialog-overlay" id="invalidate-modal">
|
|
350
|
+
<div class="dialog">
|
|
351
|
+
<h3>Invalidate Tag</h3>
|
|
352
|
+
<p>Increments generational tag version across local tiers and broadcast backplane.</p>
|
|
353
|
+
<input type="text" id="tag-input" placeholder="e.g. products, tenant-123" />
|
|
354
|
+
<div class="dialog-actions">
|
|
355
|
+
<button class="btn" onclick="closeModals()">Cancel</button>
|
|
356
|
+
<button class="btn btn-danger" onclick="submitTagInvalidation()">Invalidate</button>
|
|
357
|
+
</div>
|
|
358
|
+
</div>
|
|
359
|
+
</div>
|
|
360
|
+
|
|
361
|
+
<!-- Clear Modal -->
|
|
362
|
+
<div class="dialog-overlay" id="clear-modal">
|
|
363
|
+
<div class="dialog">
|
|
364
|
+
<h3>Clear Cache</h3>
|
|
365
|
+
<p>Evict all cached keys from the local L1 memory and NVMe disk tiers.</p>
|
|
366
|
+
<div class="dialog-actions">
|
|
367
|
+
<button class="btn" onclick="closeModals()">Cancel</button>
|
|
368
|
+
<button class="btn btn-danger" onclick="submitClear()">Confirm Clear</button>
|
|
369
|
+
</div>
|
|
370
|
+
</div>
|
|
371
|
+
</div>
|
|
372
|
+
|
|
373
|
+
<div id="toast"></div>
|
|
374
|
+
|
|
375
|
+
<script>
|
|
376
|
+
(function() {
|
|
377
|
+
let isReadOnly = false;
|
|
378
|
+
let sseFailed = false;
|
|
379
|
+
|
|
380
|
+
function showToast(msg, duration = 3000) {
|
|
381
|
+
const t = document.getElementById('toast');
|
|
382
|
+
t.innerText = msg;
|
|
383
|
+
t.style.display = 'block';
|
|
384
|
+
setTimeout(() => { t.style.display = 'none'; }, duration);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function formatBytes(bytes) {
|
|
388
|
+
if (!bytes || bytes < 1024) return (bytes || 0) + ' B';
|
|
389
|
+
if (bytes < 1048576) return (bytes / 1024).toFixed(1) + ' KB';
|
|
390
|
+
return (bytes / 1048576).toFixed(1) + ' MB';
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function updateUI(data) {
|
|
394
|
+
if (!data || !data.metrics) return;
|
|
395
|
+
const m = data.metrics;
|
|
396
|
+
isReadOnly = !!data.readOnly;
|
|
397
|
+
|
|
398
|
+
if (data.title) document.getElementById('ui-title').innerText = data.title;
|
|
399
|
+
if (data.instanceId) {
|
|
400
|
+
document.getElementById('pod-badge').innerText = 'Pod: ' + data.instanceId;
|
|
401
|
+
document.getElementById('ui-subtitle').innerText = 'Cluster Instance: ' + data.instanceId + ' | Node ' + (data.nodeVersion || '');
|
|
402
|
+
}
|
|
403
|
+
if (m.uptimeMs) {
|
|
404
|
+
const s = Math.floor(m.uptimeMs / 1000);
|
|
405
|
+
document.getElementById('uptime-badge').innerText = 'Uptime: ' + (s > 3600 ? (s/3600).toFixed(1) + 'h' : s + 's');
|
|
406
|
+
}
|
|
407
|
+
if (m.l2CircuitBreaker) {
|
|
408
|
+
const cb = m.l2CircuitBreaker.state || 'closed';
|
|
409
|
+
const el = document.getElementById('cb-badge');
|
|
410
|
+
el.innerText = 'L2: ' + cb.toUpperCase();
|
|
411
|
+
el.style.color = cb === 'closed' ? 'var(--success)' : 'var(--danger)';
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// Peer dropdown
|
|
415
|
+
if (data.peerInstances && data.peerInstances.length) {
|
|
416
|
+
const ps = document.getElementById('peer-select');
|
|
417
|
+
ps.style.display = 'inline-block';
|
|
418
|
+
if (ps.options.length <= 1) {
|
|
419
|
+
data.peerInstances.forEach(p => {
|
|
420
|
+
const opt = document.createElement('option');
|
|
421
|
+
opt.value = p.url;
|
|
422
|
+
opt.innerText = p.name;
|
|
423
|
+
ps.appendChild(opt);
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// Read-only toggle
|
|
429
|
+
if (isReadOnly) {
|
|
430
|
+
document.getElementById('btn-invalidate').disabled = true;
|
|
431
|
+
document.getElementById('btn-clear').disabled = true;
|
|
432
|
+
document.getElementById('btn-invalidate').title = 'Read-Only Mode Enabled';
|
|
433
|
+
document.getElementById('btn-clear').title = 'Read-Only Mode Enabled';
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// Hit Ratio
|
|
437
|
+
const gets = m.gets ? m.gets.total : 0;
|
|
438
|
+
const l1 = m.gets ? m.gets.l1Hits : 0;
|
|
439
|
+
const disk = m.gets ? m.gets.diskHits : 0;
|
|
440
|
+
const l2 = m.gets ? m.gets.l2Hits : 0;
|
|
441
|
+
const db = m.gets ? m.gets.fetches : 0;
|
|
442
|
+
const hits = l1 + disk + l2;
|
|
443
|
+
const hitRatio = gets > 0 ? (hits / gets * 100).toFixed(1) : '0.0';
|
|
444
|
+
|
|
445
|
+
document.getElementById('val-hit-ratio').innerText = hitRatio + '%';
|
|
446
|
+
document.getElementById('sub-hit-ratio').innerText = hits.toLocaleString() + ' hits / ' + gets.toLocaleString() + ' gets';
|
|
447
|
+
|
|
448
|
+
const pL1 = gets > 0 ? Math.round(l1 / gets * 100) : 0;
|
|
449
|
+
const pDisk = gets > 0 ? Math.round(disk / gets * 100) : 0;
|
|
450
|
+
const pL2 = gets > 0 ? Math.round(l2 / gets * 100) : 0;
|
|
451
|
+
const pDb = gets > 0 ? Math.round(db / gets * 100) : 0;
|
|
452
|
+
|
|
453
|
+
document.getElementById('bar-l1').style.width = pL1 + '%';
|
|
454
|
+
document.getElementById('bar-disk').style.width = pDisk + '%';
|
|
455
|
+
document.getElementById('bar-l2').style.width = pL2 + '%';
|
|
456
|
+
document.getElementById('bar-db').style.width = pDb + '%';
|
|
457
|
+
|
|
458
|
+
document.getElementById('pct-l1').innerText = pL1 + '%';
|
|
459
|
+
document.getElementById('pct-disk').innerText = pDisk + '%';
|
|
460
|
+
document.getElementById('pct-l2').innerText = pL2 + '%';
|
|
461
|
+
document.getElementById('pct-db').innerText = pDb + '%';
|
|
462
|
+
|
|
463
|
+
// Stampedes
|
|
464
|
+
const st = m.gets ? m.gets.stampedePrevented : 0;
|
|
465
|
+
document.getElementById('val-stampedes').innerText = st.toLocaleString();
|
|
466
|
+
document.getElementById('sub-stampedes').innerText = (m.revalidations ? m.revalidations.total : 0) + ' SWR refreshes';
|
|
467
|
+
|
|
468
|
+
// Memory
|
|
469
|
+
const mem = m.l1 ? m.l1.sizeBytes : 0;
|
|
470
|
+
const entries = m.l1 ? m.l1.entries : 0;
|
|
471
|
+
const oom = m.oom ? m.oom.evictions : 0;
|
|
472
|
+
document.getElementById('val-memory').innerText = formatBytes(mem);
|
|
473
|
+
document.getElementById('sub-memory').innerText = entries.toLocaleString() + ' entries | ' + oom + ' OOM purges';
|
|
474
|
+
|
|
475
|
+
// Backplane & Mesh
|
|
476
|
+
const bpSent = m.backplane ? m.backplane.sent : 0;
|
|
477
|
+
const bpRecv = m.backplane ? m.backplane.received : 0;
|
|
478
|
+
document.getElementById('val-backplane').innerText = bpSent + ' / ' + bpRecv;
|
|
479
|
+
if (m.crossRegion && m.crossRegion.enabled) {
|
|
480
|
+
document.getElementById('sub-backplane').innerText = 'Cross-Region: ' + m.crossRegion.sent + ' sent, ' + m.crossRegion.received + ' recv';
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
window.fetchMetricsSnapshot = function() {
|
|
485
|
+
fetch('api/metrics')
|
|
486
|
+
.then(r => r.json())
|
|
487
|
+
.then(data => updateUI(data))
|
|
488
|
+
.catch(err => showToast('Failed to fetch snapshot: ' + err.message));
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
function connectSSE() {
|
|
492
|
+
if (typeof EventSource === 'undefined') {
|
|
493
|
+
fallbackPolling();
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
const sse = new EventSource('api/stream');
|
|
497
|
+
sse.onmessage = function(e) {
|
|
498
|
+
try {
|
|
499
|
+
const data = JSON.parse(e.data);
|
|
500
|
+
updateUI(data);
|
|
501
|
+
} catch(err) { /* ignore */ }
|
|
502
|
+
};
|
|
503
|
+
sse.onerror = function() {
|
|
504
|
+
sse.close();
|
|
505
|
+
if (!sseFailed) {
|
|
506
|
+
sseFailed = true;
|
|
507
|
+
document.getElementById('stream-badge').className = 'badge';
|
|
508
|
+
document.getElementById('stream-text').innerText = 'Polling Active';
|
|
509
|
+
fallbackPolling();
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function fallbackPolling() {
|
|
515
|
+
fetchMetricsSnapshot();
|
|
516
|
+
setInterval(fetchMetricsSnapshot, 3000);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
window.openInvalidateModal = function() {
|
|
520
|
+
if (isReadOnly) return;
|
|
521
|
+
document.getElementById('invalidate-modal').classList.add('active');
|
|
522
|
+
document.getElementById('tag-input').focus();
|
|
523
|
+
};
|
|
524
|
+
window.openClearModal = function() {
|
|
525
|
+
if (isReadOnly) return;
|
|
526
|
+
document.getElementById('clear-modal').classList.add('active');
|
|
527
|
+
};
|
|
528
|
+
window.closeModals = function() {
|
|
529
|
+
document.getElementById('invalidate-modal').classList.remove('active');
|
|
530
|
+
document.getElementById('clear-modal').classList.remove('active');
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
window.submitTagInvalidation = function() {
|
|
534
|
+
const tag = document.getElementById('tag-input').value.trim();
|
|
535
|
+
if (!tag) return;
|
|
536
|
+
fetch('api/actions/invalidate-tag', {
|
|
537
|
+
method: 'POST',
|
|
538
|
+
headers: {
|
|
539
|
+
'Content-Type': 'application/json',
|
|
540
|
+
'X-TriCache-Action': '1'
|
|
541
|
+
},
|
|
542
|
+
body: JSON.stringify({ tag })
|
|
543
|
+
})
|
|
544
|
+
.then(r => r.json())
|
|
545
|
+
.then(res => {
|
|
546
|
+
closeModals();
|
|
547
|
+
document.getElementById('tag-input').value = '';
|
|
548
|
+
if (res.ok) {
|
|
549
|
+
showToast('Tag "' + tag + '" invalidated across fleet');
|
|
550
|
+
fetchMetricsSnapshot();
|
|
551
|
+
} else {
|
|
552
|
+
showToast('Error: ' + (res.error || 'Failed'));
|
|
553
|
+
}
|
|
554
|
+
})
|
|
555
|
+
.catch(e => showToast('Request failed: ' + e.message));
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
window.submitClear = function() {
|
|
559
|
+
fetch('api/actions/clear', {
|
|
560
|
+
method: 'POST',
|
|
561
|
+
headers: {
|
|
562
|
+
'Content-Type': 'application/json',
|
|
563
|
+
'X-TriCache-Action': '1'
|
|
564
|
+
}
|
|
565
|
+
})
|
|
566
|
+
.then(r => r.json())
|
|
567
|
+
.then(res => {
|
|
568
|
+
closeModals();
|
|
569
|
+
if (res.ok) {
|
|
570
|
+
showToast('Local cache tiers cleared');
|
|
571
|
+
fetchMetricsSnapshot();
|
|
572
|
+
} else {
|
|
573
|
+
showToast('Error: ' + (res.error || 'Failed'));
|
|
574
|
+
}
|
|
575
|
+
})
|
|
576
|
+
.catch(e => showToast('Clear failed: ' + e.message));
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
// Bootstrap on load
|
|
580
|
+
fetchMetricsSnapshot();
|
|
581
|
+
connectSSE();
|
|
582
|
+
})();
|
|
583
|
+
</script>
|
|
584
|
+
</body>
|
|
585
|
+
</html>`;
|
|
586
|
+
var DASHBOARD_GZIP_BUFFER = gzipSync(Buffer.from(DASHBOARD_HTML, "utf-8"));
|
|
587
|
+
|
|
588
|
+
// src/dashboard/handler.ts
|
|
589
|
+
function safeCompare(a, b) {
|
|
590
|
+
if (typeof a !== "string" || typeof b !== "string") return false;
|
|
591
|
+
const hashA = createHash("sha256").update(a).digest();
|
|
592
|
+
const hashB = createHash("sha256").update(b).digest();
|
|
593
|
+
return timingSafeEqual(hashA, hashB);
|
|
594
|
+
}
|
|
595
|
+
var SECURITY_HEADERS = {
|
|
596
|
+
"Content-Security-Policy": "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'",
|
|
597
|
+
"X-Frame-Options": "DENY",
|
|
598
|
+
"X-Content-Type-Options": "nosniff",
|
|
599
|
+
"Referrer-Policy": "same-origin",
|
|
600
|
+
"Cache-Control": "no-store, no-cache, must-revalidate"
|
|
601
|
+
};
|
|
602
|
+
function resolveInstanceId(custom) {
|
|
603
|
+
if (custom && custom.trim()) return custom.trim();
|
|
604
|
+
if (process.env.POD_NAME) return process.env.POD_NAME;
|
|
605
|
+
if (process.env.HOSTNAME) return process.env.HOSTNAME;
|
|
606
|
+
try {
|
|
607
|
+
return os.hostname();
|
|
608
|
+
} catch {
|
|
609
|
+
return "tricache-node";
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function buildMetricsPayload(options) {
|
|
613
|
+
const metrics = options.cache.metrics();
|
|
614
|
+
return {
|
|
615
|
+
title: options.title || "TriCache Observability",
|
|
616
|
+
instanceId: resolveInstanceId(options.instanceId),
|
|
617
|
+
nodeVersion: process.version,
|
|
618
|
+
readOnly: !!options.readOnly,
|
|
619
|
+
peerInstances: options.peerInstances || [],
|
|
620
|
+
metrics: {
|
|
621
|
+
...metrics,
|
|
622
|
+
health: {
|
|
623
|
+
status: metrics.l2CircuitBreaker?.state === "open" ? "degraded" : "healthy",
|
|
624
|
+
circuitBreaker: metrics.l2CircuitBreaker?.state || "closed",
|
|
625
|
+
uptimeMs: metrics.uptimeMs
|
|
626
|
+
}
|
|
627
|
+
},
|
|
628
|
+
timestamp: Date.now()
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
function verifyAuth(req, options) {
|
|
632
|
+
const url = new URL(req.url, "http://localhost");
|
|
633
|
+
if (options.auth) {
|
|
634
|
+
const authHeader = req.headers.get("authorization") || "";
|
|
635
|
+
if (!authHeader.toLowerCase().startsWith("basic ")) {
|
|
636
|
+
return { authorized: false };
|
|
637
|
+
}
|
|
638
|
+
const b64 = authHeader.slice(6).trim();
|
|
639
|
+
let decoded = "";
|
|
640
|
+
try {
|
|
641
|
+
decoded = Buffer.from(b64, "base64").toString("utf-8");
|
|
642
|
+
} catch {
|
|
643
|
+
return { authorized: false };
|
|
644
|
+
}
|
|
645
|
+
const colonIdx = decoded.indexOf(":");
|
|
646
|
+
if (colonIdx === -1) return { authorized: false };
|
|
647
|
+
const username = decoded.slice(0, colonIdx);
|
|
648
|
+
const password = decoded.slice(colonIdx + 1);
|
|
649
|
+
const userMatch = safeCompare(username, options.auth.username);
|
|
650
|
+
const passMatch = safeCompare(password, options.auth.password);
|
|
651
|
+
if (userMatch && passMatch) {
|
|
652
|
+
return { authorized: true, user: username };
|
|
653
|
+
}
|
|
654
|
+
return { authorized: false };
|
|
655
|
+
}
|
|
656
|
+
if (options.authSecret) {
|
|
657
|
+
const authHeader = req.headers.get("authorization") || "";
|
|
658
|
+
if (authHeader.toLowerCase().startsWith("bearer ")) {
|
|
659
|
+
const token = authHeader.slice(7).trim();
|
|
660
|
+
if (safeCompare(token, options.authSecret)) {
|
|
661
|
+
return { authorized: true, user: "token-client" };
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
const queryToken = url.searchParams.get("token");
|
|
665
|
+
if (queryToken && safeCompare(queryToken, options.authSecret)) {
|
|
666
|
+
return { authorized: true, user: "token-client" };
|
|
667
|
+
}
|
|
668
|
+
return { authorized: false };
|
|
669
|
+
}
|
|
670
|
+
return { authorized: true, user: "anonymous" };
|
|
671
|
+
}
|
|
672
|
+
function verifyCsrf(req) {
|
|
673
|
+
const customHeader = req.headers.get("x-tricache-action");
|
|
674
|
+
if (customHeader !== "1") {
|
|
675
|
+
return false;
|
|
676
|
+
}
|
|
677
|
+
const origin = req.headers.get("origin");
|
|
678
|
+
const host = req.headers.get("host");
|
|
679
|
+
if (origin && host) {
|
|
680
|
+
try {
|
|
681
|
+
const originHost = new URL(origin).host;
|
|
682
|
+
if (originHost !== host) return false;
|
|
683
|
+
} catch {
|
|
684
|
+
return false;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
return true;
|
|
688
|
+
}
|
|
689
|
+
async function handleDashboardRequest(req, options) {
|
|
690
|
+
const authResult = verifyAuth(req, options);
|
|
691
|
+
if (!authResult.authorized) {
|
|
692
|
+
return new Response(JSON.stringify({ error: "Unauthorized" }), {
|
|
693
|
+
status: 401,
|
|
694
|
+
headers: {
|
|
695
|
+
"Content-Type": "application/json",
|
|
696
|
+
...options.auth ? { "WWW-Authenticate": 'Basic realm="TriCache Dashboard"' } : {}
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
const url = new URL(req.url, "http://localhost");
|
|
701
|
+
let pathname = url.pathname;
|
|
702
|
+
const basePath = (options.basePath || "").replace(/\/+$/, "");
|
|
703
|
+
if (basePath && pathname.startsWith(basePath)) {
|
|
704
|
+
pathname = pathname.slice(basePath.length) || "/";
|
|
705
|
+
}
|
|
706
|
+
if (!pathname.startsWith("/")) {
|
|
707
|
+
pathname = "/" + pathname;
|
|
708
|
+
}
|
|
709
|
+
if (pathname === "/api/stream" && req.method === "GET") {
|
|
710
|
+
const encoder = new TextEncoder();
|
|
711
|
+
const intervalMs = Math.max(500, options.streamIntervalMs || 2e3);
|
|
712
|
+
let ticker = null;
|
|
713
|
+
const stream = new ReadableStream({
|
|
714
|
+
start(controller) {
|
|
715
|
+
try {
|
|
716
|
+
const initial = buildMetricsPayload(options);
|
|
717
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(initial)}
|
|
718
|
+
|
|
719
|
+
`));
|
|
720
|
+
} catch {
|
|
721
|
+
}
|
|
722
|
+
ticker = setInterval(() => {
|
|
723
|
+
try {
|
|
724
|
+
const data = buildMetricsPayload(options);
|
|
725
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}
|
|
726
|
+
|
|
727
|
+
`));
|
|
728
|
+
} catch {
|
|
729
|
+
if (ticker) clearInterval(ticker);
|
|
730
|
+
try {
|
|
731
|
+
controller.close();
|
|
732
|
+
} catch {
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}, intervalMs);
|
|
736
|
+
if (req.signal) {
|
|
737
|
+
req.signal.addEventListener("abort", () => {
|
|
738
|
+
if (ticker) {
|
|
739
|
+
clearInterval(ticker);
|
|
740
|
+
ticker = null;
|
|
741
|
+
}
|
|
742
|
+
try {
|
|
743
|
+
controller.close();
|
|
744
|
+
} catch {
|
|
745
|
+
}
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
},
|
|
749
|
+
cancel() {
|
|
750
|
+
if (ticker) {
|
|
751
|
+
clearInterval(ticker);
|
|
752
|
+
ticker = null;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
});
|
|
756
|
+
return new Response(stream, {
|
|
757
|
+
status: 200,
|
|
758
|
+
headers: {
|
|
759
|
+
"Content-Type": "text/event-stream",
|
|
760
|
+
"Cache-Control": "no-cache, no-transform",
|
|
761
|
+
"Connection": "keep-alive",
|
|
762
|
+
"X-Accel-Buffering": "no"
|
|
763
|
+
}
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
if (pathname === "/api/metrics" && req.method === "GET") {
|
|
767
|
+
const payload = buildMetricsPayload(options);
|
|
768
|
+
return new Response(JSON.stringify(payload), {
|
|
769
|
+
status: 200,
|
|
770
|
+
headers: {
|
|
771
|
+
"Content-Type": "application/json",
|
|
772
|
+
"Cache-Control": "no-store, no-cache, must-revalidate"
|
|
773
|
+
}
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
if (pathname === "/api/actions/invalidate-tag" && req.method === "POST") {
|
|
777
|
+
const clientIp = req.headers.get("x-forwarded-for") || void 0;
|
|
778
|
+
if (options.readOnly) {
|
|
779
|
+
return new Response(JSON.stringify({ ok: false, error: "Dashboard is in read-only mode" }), {
|
|
780
|
+
status: 403,
|
|
781
|
+
headers: { "Content-Type": "application/json" }
|
|
782
|
+
});
|
|
783
|
+
}
|
|
784
|
+
if (!verifyCsrf(req)) {
|
|
785
|
+
return new Response(JSON.stringify({ ok: false, error: "Invalid or missing CSRF headers" }), {
|
|
786
|
+
status: 403,
|
|
787
|
+
headers: { "Content-Type": "application/json" }
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
try {
|
|
791
|
+
const body = await req.json();
|
|
792
|
+
const tag = body?.tag?.trim();
|
|
793
|
+
if (!tag) {
|
|
794
|
+
return new Response(JSON.stringify({ ok: false, error: "Tag parameter is required" }), {
|
|
795
|
+
status: 400,
|
|
796
|
+
headers: { "Content-Type": "application/json" }
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
await options.cache.invalidateTag(tag);
|
|
800
|
+
options.onAction?.({
|
|
801
|
+
action: "invalidate-tag",
|
|
802
|
+
target: tag,
|
|
803
|
+
user: authResult.user,
|
|
804
|
+
ip: clientIp,
|
|
805
|
+
timestamp: Date.now(),
|
|
806
|
+
success: true
|
|
807
|
+
});
|
|
808
|
+
return new Response(JSON.stringify({ ok: true, tag }), {
|
|
809
|
+
status: 200,
|
|
810
|
+
headers: { "Content-Type": "application/json" }
|
|
811
|
+
});
|
|
812
|
+
} catch (err) {
|
|
813
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
814
|
+
options.onAction?.({
|
|
815
|
+
action: "invalidate-tag",
|
|
816
|
+
user: authResult.user,
|
|
817
|
+
ip: clientIp,
|
|
818
|
+
timestamp: Date.now(),
|
|
819
|
+
success: false,
|
|
820
|
+
error: errMsg
|
|
821
|
+
});
|
|
822
|
+
return new Response(JSON.stringify({ ok: false, error: errMsg }), {
|
|
823
|
+
status: 500,
|
|
824
|
+
headers: { "Content-Type": "application/json" }
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
if (pathname === "/api/actions/clear" && req.method === "POST") {
|
|
829
|
+
const clientIp = req.headers.get("x-forwarded-for") || void 0;
|
|
830
|
+
if (options.readOnly) {
|
|
831
|
+
return new Response(JSON.stringify({ ok: false, error: "Dashboard is in read-only mode" }), {
|
|
832
|
+
status: 403,
|
|
833
|
+
headers: { "Content-Type": "application/json" }
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
if (!verifyCsrf(req)) {
|
|
837
|
+
return new Response(JSON.stringify({ ok: false, error: "Invalid or missing CSRF headers" }), {
|
|
838
|
+
status: 403,
|
|
839
|
+
headers: { "Content-Type": "application/json" }
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
try {
|
|
843
|
+
await options.cache.clear();
|
|
844
|
+
options.onAction?.({
|
|
845
|
+
action: "clear",
|
|
846
|
+
user: authResult.user,
|
|
847
|
+
ip: clientIp,
|
|
848
|
+
timestamp: Date.now(),
|
|
849
|
+
success: true
|
|
850
|
+
});
|
|
851
|
+
return new Response(JSON.stringify({ ok: true }), {
|
|
852
|
+
status: 200,
|
|
853
|
+
headers: { "Content-Type": "application/json" }
|
|
854
|
+
});
|
|
855
|
+
} catch (err) {
|
|
856
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
857
|
+
options.onAction?.({
|
|
858
|
+
action: "clear",
|
|
859
|
+
user: authResult.user,
|
|
860
|
+
ip: clientIp,
|
|
861
|
+
timestamp: Date.now(),
|
|
862
|
+
success: false,
|
|
863
|
+
error: errMsg
|
|
864
|
+
});
|
|
865
|
+
return new Response(JSON.stringify({ ok: false, error: errMsg }), {
|
|
866
|
+
status: 500,
|
|
867
|
+
headers: { "Content-Type": "application/json" }
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
if (pathname === "/" || pathname === "/index.html" || pathname === "") {
|
|
872
|
+
const acceptEncoding = req.headers.get("accept-encoding") || "";
|
|
873
|
+
if (acceptEncoding.includes("gzip")) {
|
|
874
|
+
return new Response(new Uint8Array(DASHBOARD_GZIP_BUFFER), {
|
|
875
|
+
status: 200,
|
|
876
|
+
headers: {
|
|
877
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
878
|
+
"Content-Encoding": "gzip",
|
|
879
|
+
"Content-Length": String(DASHBOARD_GZIP_BUFFER.length),
|
|
880
|
+
...SECURITY_HEADERS
|
|
881
|
+
}
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
return new Response(DASHBOARD_HTML, {
|
|
885
|
+
status: 200,
|
|
886
|
+
headers: {
|
|
887
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
888
|
+
...SECURITY_HEADERS
|
|
889
|
+
}
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
return new Response(JSON.stringify({ error: "Not Found" }), {
|
|
893
|
+
status: 404,
|
|
894
|
+
headers: { "Content-Type": "application/json" }
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
// src/dashboard/index.ts
|
|
899
|
+
async function nodeToWebRequest(req) {
|
|
900
|
+
const protocol = req.socket.encrypted ? "https" : "http";
|
|
901
|
+
const host = req.headers.host || "localhost";
|
|
902
|
+
const url = new URL(req.url || "/", `${protocol}://${host}`);
|
|
903
|
+
const headers = new Headers();
|
|
904
|
+
for (const [key, val] of Object.entries(req.headers)) {
|
|
905
|
+
if (val === void 0) continue;
|
|
906
|
+
if (Array.isArray(val)) {
|
|
907
|
+
for (const item of val) headers.append(key, item);
|
|
908
|
+
} else {
|
|
909
|
+
headers.set(key, val);
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
let body = null;
|
|
913
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
914
|
+
const chunks = [];
|
|
915
|
+
for await (const chunk of req) {
|
|
916
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
917
|
+
}
|
|
918
|
+
if (chunks.length > 0) {
|
|
919
|
+
body = Buffer.concat(chunks);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
const controller = new AbortController();
|
|
923
|
+
req.on("close", () => {
|
|
924
|
+
if (req.destroyed || !req.complete) {
|
|
925
|
+
controller.abort();
|
|
926
|
+
}
|
|
927
|
+
});
|
|
928
|
+
const init = {
|
|
929
|
+
method: req.method,
|
|
930
|
+
headers,
|
|
931
|
+
body: body ? body : null,
|
|
932
|
+
signal: controller.signal
|
|
933
|
+
};
|
|
934
|
+
if (body) {
|
|
935
|
+
init.duplex = "half";
|
|
936
|
+
}
|
|
937
|
+
return new Request(url.toString(), init);
|
|
938
|
+
}
|
|
939
|
+
async function pipeWebResponseToNode(webRes, nodeRes) {
|
|
940
|
+
nodeRes.statusCode = webRes.status;
|
|
941
|
+
webRes.headers.forEach((val, key) => {
|
|
942
|
+
nodeRes.setHeader(key, val);
|
|
943
|
+
});
|
|
944
|
+
if (!webRes.body) {
|
|
945
|
+
nodeRes.end();
|
|
946
|
+
return;
|
|
947
|
+
}
|
|
948
|
+
const reader = webRes.body.getReader();
|
|
949
|
+
try {
|
|
950
|
+
while (true) {
|
|
951
|
+
const { done, value } = await reader.read();
|
|
952
|
+
if (done) break;
|
|
953
|
+
nodeRes.write(value);
|
|
954
|
+
}
|
|
955
|
+
} finally {
|
|
956
|
+
nodeRes.end();
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
function tricacheDashboard(options) {
|
|
960
|
+
return async (req, res, next) => {
|
|
961
|
+
try {
|
|
962
|
+
const webReq = await nodeToWebRequest(req);
|
|
963
|
+
const webRes = await handleDashboardRequest(webReq, options);
|
|
964
|
+
await pipeWebResponseToNode(webRes, res);
|
|
965
|
+
} catch (err) {
|
|
966
|
+
if (typeof next === "function") {
|
|
967
|
+
next(err);
|
|
968
|
+
} else {
|
|
969
|
+
res.statusCode = 500;
|
|
970
|
+
res.end(JSON.stringify({ error: "Internal Server Error" }));
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
};
|
|
974
|
+
}
|
|
975
|
+
function createNextDashboardHandlers(options) {
|
|
976
|
+
const handler = async (req) => {
|
|
977
|
+
return handleDashboardRequest(req, options);
|
|
978
|
+
};
|
|
979
|
+
return {
|
|
980
|
+
GET: handler,
|
|
981
|
+
POST: handler
|
|
982
|
+
};
|
|
983
|
+
}
|
|
984
|
+
async function startDashboardServer(options) {
|
|
985
|
+
const port = options.port || 9090;
|
|
986
|
+
const host = options.host || "127.0.0.1";
|
|
987
|
+
const middleware = tricacheDashboard(options);
|
|
988
|
+
const server = http.createServer((req, res) => {
|
|
989
|
+
middleware(req, res).catch((err) => {
|
|
990
|
+
res.statusCode = 500;
|
|
991
|
+
res.end(String(err));
|
|
992
|
+
});
|
|
993
|
+
});
|
|
994
|
+
await new Promise((resolve, reject) => {
|
|
995
|
+
server.once("error", reject);
|
|
996
|
+
server.listen(port, host, () => {
|
|
997
|
+
server.off("error", reject);
|
|
998
|
+
resolve();
|
|
999
|
+
});
|
|
1000
|
+
});
|
|
1001
|
+
const actualAddress = server.address();
|
|
1002
|
+
const actualPort = typeof actualAddress === "object" && actualAddress ? actualAddress.port : port;
|
|
1003
|
+
return {
|
|
1004
|
+
server,
|
|
1005
|
+
port: actualPort,
|
|
1006
|
+
host,
|
|
1007
|
+
close: () => new Promise((resolve, reject) => {
|
|
1008
|
+
server.close((err) => err ? reject(err) : resolve());
|
|
1009
|
+
})
|
|
1010
|
+
};
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
export {
|
|
1014
|
+
DASHBOARD_HTML,
|
|
1015
|
+
DASHBOARD_GZIP_BUFFER,
|
|
1016
|
+
safeCompare,
|
|
1017
|
+
resolveInstanceId,
|
|
1018
|
+
buildMetricsPayload,
|
|
1019
|
+
handleDashboardRequest,
|
|
1020
|
+
tricacheDashboard,
|
|
1021
|
+
createNextDashboardHandlers,
|
|
1022
|
+
startDashboardServer
|
|
1023
|
+
};
|