@limiq/dashboard 0.0.0-stage → 0.1.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 -0
- package/README.md +18 -2
- package/dist/express.d.ts +21 -0
- package/dist/express.d.ts.map +1 -0
- package/dist/express.js +34 -0
- package/dist/express.js.map +1 -0
- package/dist/html.d.ts +7 -0
- package/dist/html.d.ts.map +1 -0
- package/dist/html.js +123 -0
- package/dist/html.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/router.d.ts +32 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +45 -0
- package/dist/router.js.map +1 -0
- package/package.json +50 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sandeep Vaid
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @limiq/dashboard
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Lightweight embedded metrics dashboard + JSON API for @limiq/core. Single-file UI, framework-agnostic router + Express mount.
|
|
4
|
+
|
|
5
|
+
Part of the **[@limiq](https://github.com/sandeepvaid/limiq)** rate limiter toolkit — a framework-agnostic, plug-and-play rate limiter for Node.js/TypeScript.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
```bash
|
|
9
|
+
npm install @limiq/dashboard
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { expressDashboard } from '@limiq/dashboard';
|
|
14
|
+
app.use(expressDashboard({ collector: limiter.getMetrics(), limiter }));
|
|
15
|
+
// UI at /__ratelimit/
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## License
|
|
19
|
+
MIT © Sandeep Vaid
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type DashboardOptions } from "./router.js";
|
|
2
|
+
interface ReqLike {
|
|
3
|
+
method: string;
|
|
4
|
+
path?: string;
|
|
5
|
+
url?: string;
|
|
6
|
+
originalUrl?: string;
|
|
7
|
+
}
|
|
8
|
+
interface ResLike {
|
|
9
|
+
setHeader(name: string, value: string): void;
|
|
10
|
+
status(code: number): ResLike;
|
|
11
|
+
send(body: string): void;
|
|
12
|
+
}
|
|
13
|
+
type Next = () => void;
|
|
14
|
+
/**
|
|
15
|
+
* Express middleware that serves the dashboard + JSON API. Mount it directly:
|
|
16
|
+
* app.use(expressDashboard({ collector: limiter.getMetrics()!, limiter }))
|
|
17
|
+
* Requests outside basePath are passed through to the next handler.
|
|
18
|
+
*/
|
|
19
|
+
export declare function expressDashboard(options: DashboardOptions): (req: ReqLike, res: ResLike, next: Next) => void;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=express.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"express.d.ts","sourceRoot":"","sources":["../src/express.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpE,UAAU,OAAO;IACf,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AACD,UAAU,OAAO;IACf,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7C,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAC9B,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AACD,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;AAEvB;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,gBAAgB,IAIpB,KAAK,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,IAAI,KAAG,IAAI,CA4BlF"}
|
package/dist/express.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { routeDashboard } from "./router.js";
|
|
2
|
+
/**
|
|
3
|
+
* Express middleware that serves the dashboard + JSON API. Mount it directly:
|
|
4
|
+
* app.use(expressDashboard({ collector: limiter.getMetrics()!, limiter }))
|
|
5
|
+
* Requests outside basePath are passed through to the next handler.
|
|
6
|
+
*/
|
|
7
|
+
export function expressDashboard(options) {
|
|
8
|
+
const basePath = options.basePath ?? "/__ratelimit";
|
|
9
|
+
const isAdmin = options.isAdmin ?? (() => false);
|
|
10
|
+
return function dashboardMiddleware(req, res, next) {
|
|
11
|
+
const rawUrl = req.originalUrl ?? req.url ?? req.path ?? "/";
|
|
12
|
+
const url = new URL(rawUrl, "http://internal");
|
|
13
|
+
const path = req.path ?? url.pathname;
|
|
14
|
+
if (!path.startsWith(basePath)) {
|
|
15
|
+
next();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
routeDashboard({ ...options, basePath }, {
|
|
19
|
+
method: req.method,
|
|
20
|
+
path,
|
|
21
|
+
query: url.searchParams,
|
|
22
|
+
isAdmin: isAdmin(req),
|
|
23
|
+
})
|
|
24
|
+
.then((r) => {
|
|
25
|
+
res.setHeader("Content-Type", r.contentType);
|
|
26
|
+
res.status(r.status).send(r.body);
|
|
27
|
+
})
|
|
28
|
+
.catch(() => {
|
|
29
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
30
|
+
res.status(500).send(JSON.stringify({ error: "Dashboard error" }));
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=express.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"express.js","sourceRoot":"","sources":["../src/express.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAyB,MAAM,aAAa,CAAC;AAepE;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAyB;IACxD,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,cAAc,CAAC;IACpD,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC;IAEjD,OAAO,SAAS,mBAAmB,CAAC,GAAY,EAAE,GAAY,EAAE,IAAU;QACxE,MAAM,MAAM,GAAG,GAAG,CAAC,WAAW,IAAI,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC;QAC7D,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC;QAC/C,MAAM,IAAI,GAAG,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,QAAQ,CAAC;QAEtC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC/B,IAAI,EAAE,CAAC;YACP,OAAO;QACT,CAAC;QAED,cAAc,CACZ,EAAE,GAAG,OAAO,EAAE,QAAQ,EAAE,EACxB;YACE,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,IAAI;YACJ,KAAK,EAAE,GAAG,CAAC,YAAY;YACvB,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC;SACtB,CACF;aACE,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE;YACV,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,CAAC,CAAC,WAAW,CAAC,CAAC;YAC7C,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QACpC,CAAC,CAAC;aACD,KAAK,CAAC,GAAG,EAAE;YACV,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,iCAAiC,CAAC,CAAC;YACjE,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,iBAAiB,EAAE,CAAC,CAAC,CAAC;QACrE,CAAC,CAAC,CAAC;IACP,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/html.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The embedded dashboard — a single, dependency-free HTML page. It polls the
|
|
3
|
+
* JSON API and renders counters, overhead percentiles, per-route load and the
|
|
4
|
+
* top throttled keys. Works in light and dark.
|
|
5
|
+
*/
|
|
6
|
+
export declare function dashboardHtml(basePath: string, pollMs: number): string;
|
|
7
|
+
//# sourceMappingURL=html.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"html.d.ts","sourceRoot":"","sources":["../src/html.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAoHtE"}
|
package/dist/html.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The embedded dashboard — a single, dependency-free HTML page. It polls the
|
|
3
|
+
* JSON API and renders counters, overhead percentiles, per-route load and the
|
|
4
|
+
* top throttled keys. Works in light and dark.
|
|
5
|
+
*/
|
|
6
|
+
export function dashboardHtml(basePath, pollMs) {
|
|
7
|
+
return `<!doctype html>
|
|
8
|
+
<html lang="en">
|
|
9
|
+
<head>
|
|
10
|
+
<meta charset="utf-8" />
|
|
11
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
12
|
+
<title>Rate Limiter</title>
|
|
13
|
+
<style>
|
|
14
|
+
:root {
|
|
15
|
+
--bg: #ffffff; --fg: #1a1a1a; --muted: #666; --card: #f6f7f9;
|
|
16
|
+
--border: #e3e5e8; --accent: #2563eb; --ok: #16a34a; --bad: #dc2626;
|
|
17
|
+
}
|
|
18
|
+
@media (prefers-color-scheme: dark) {
|
|
19
|
+
:root {
|
|
20
|
+
--bg: #0f1115; --fg: #e8eaed; --muted: #9aa0a6; --card: #181b20;
|
|
21
|
+
--border: #272b31; --accent: #60a5fa; --ok: #4ade80; --bad: #f87171;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
* { box-sizing: border-box; }
|
|
25
|
+
body { margin: 0; background: var(--bg); color: var(--fg);
|
|
26
|
+
font: 14px/1.5 system-ui, -apple-system, Segoe UI, Roboto, sans-serif; }
|
|
27
|
+
header { padding: 16px; border-bottom: 1px solid var(--border);
|
|
28
|
+
display: flex; align-items: center; gap: 12px; }
|
|
29
|
+
header h1 { font-size: 16px; margin: 0; }
|
|
30
|
+
.status { margin-left: auto; color: var(--muted); font-size: 12px; }
|
|
31
|
+
main { padding: 16px; max-width: 980px; margin: 0 auto; }
|
|
32
|
+
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
|
|
33
|
+
gap: 12px; margin-bottom: 16px; }
|
|
34
|
+
.card { background: var(--card); border: 1px solid var(--border);
|
|
35
|
+
border-radius: 10px; padding: 14px; }
|
|
36
|
+
.card .label { color: var(--muted); font-size: 12px; text-transform: uppercase;
|
|
37
|
+
letter-spacing: .04em; }
|
|
38
|
+
.card .value { font-size: 26px; font-weight: 600; margin-top: 4px; }
|
|
39
|
+
.ok { color: var(--ok); } .bad { color: var(--bad); }
|
|
40
|
+
.bar { height: 8px; border-radius: 4px; background: var(--border);
|
|
41
|
+
overflow: hidden; margin-top: 10px; display: flex; }
|
|
42
|
+
.bar > span { display: block; height: 100%; }
|
|
43
|
+
section h2 { font-size: 13px; color: var(--muted); text-transform: uppercase;
|
|
44
|
+
letter-spacing: .04em; margin: 20px 0 8px; }
|
|
45
|
+
table { width: 100%; border-collapse: collapse; }
|
|
46
|
+
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border);
|
|
47
|
+
font-size: 13px; }
|
|
48
|
+
th { color: var(--muted); font-weight: 500; }
|
|
49
|
+
td.num { text-align: right; font-variant-numeric: tabular-nums; }
|
|
50
|
+
code { background: var(--border); padding: 1px 5px; border-radius: 4px; font-size: 12px; }
|
|
51
|
+
.empty { color: var(--muted); padding: 10px; }
|
|
52
|
+
</style>
|
|
53
|
+
</head>
|
|
54
|
+
<body>
|
|
55
|
+
<header>
|
|
56
|
+
<h1>Rate Limiter Dashboard</h1>
|
|
57
|
+
<span class="status" id="status">connecting…</span>
|
|
58
|
+
</header>
|
|
59
|
+
<main>
|
|
60
|
+
<div class="grid" id="counters"></div>
|
|
61
|
+
<div class="card">
|
|
62
|
+
<div class="label">Allowed vs Blocked</div>
|
|
63
|
+
<div class="bar"><span id="barOk" style="background:var(--ok)"></span><span id="barBad" style="background:var(--bad)"></span></div>
|
|
64
|
+
</div>
|
|
65
|
+
<div class="grid" id="latency" style="margin-top:16px"></div>
|
|
66
|
+
<section>
|
|
67
|
+
<h2>Top throttled keys</h2>
|
|
68
|
+
<table><thead><tr><th>Key</th><th class="num">Blocked</th></tr></thead>
|
|
69
|
+
<tbody id="topBody"></tbody></table>
|
|
70
|
+
</section>
|
|
71
|
+
<section>
|
|
72
|
+
<h2>By route</h2>
|
|
73
|
+
<table><thead><tr><th>Route</th><th class="num">Allowed</th><th class="num">Blocked</th></tr></thead>
|
|
74
|
+
<tbody id="routeBody"></tbody></table>
|
|
75
|
+
</section>
|
|
76
|
+
</main>
|
|
77
|
+
<script>
|
|
78
|
+
const BASE = ${JSON.stringify(basePath)};
|
|
79
|
+
const POLL = ${pollMs};
|
|
80
|
+
const el = (id) => document.getElementById(id);
|
|
81
|
+
function card(label, value, cls) {
|
|
82
|
+
return '<div class="card"><div class="label">' + label + '</div><div class="value ' +
|
|
83
|
+
(cls||'') + '">' + value + '</div></div>';
|
|
84
|
+
}
|
|
85
|
+
function esc(s) { return String(s).replace(/[&<>]/g, c => ({'&':'&','<':'<','>':'>'}[c])); }
|
|
86
|
+
async function tick() {
|
|
87
|
+
try {
|
|
88
|
+
const res = await fetch(BASE + '/api/metrics', { headers: { accept: 'application/json' } });
|
|
89
|
+
if (!res.ok) throw new Error('HTTP ' + res.status);
|
|
90
|
+
const m = await res.json();
|
|
91
|
+
el('status').textContent = 'updated ' + new Date().toLocaleTimeString();
|
|
92
|
+
const c = m.counters;
|
|
93
|
+
el('counters').innerHTML =
|
|
94
|
+
card('Allowed', c.allowed, 'ok') + card('Blocked', c.blocked, 'bad') +
|
|
95
|
+
card('Degraded', c.degraded) + card('Store errors', c.storeErrors);
|
|
96
|
+
const total = c.allowed + c.blocked || 1;
|
|
97
|
+
el('barOk').style.width = (100 * c.allowed / total) + '%';
|
|
98
|
+
el('barBad').style.width = (100 * c.blocked / total) + '%';
|
|
99
|
+
el('latency').innerHTML =
|
|
100
|
+
card('p50 overhead', m.latency.p50 + ' ms') +
|
|
101
|
+
card('p95 overhead', m.latency.p95 + ' ms') +
|
|
102
|
+
card('p99 overhead', m.latency.p99 + ' ms') +
|
|
103
|
+
card('samples', m.latency.count);
|
|
104
|
+
el('topBody').innerHTML = (m.topThrottled || []).length
|
|
105
|
+
? m.topThrottled.map(t => '<tr><td><code>' + esc(t.key) + '</code></td><td class="num">' +
|
|
106
|
+
t.blocked + '</td></tr>').join('')
|
|
107
|
+
: '<tr><td class="empty" colspan="2">No throttled keys yet</td></tr>';
|
|
108
|
+
const routes = Object.entries(m.byRoute || {});
|
|
109
|
+
el('routeBody').innerHTML = routes.length
|
|
110
|
+
? routes.map(([r, v]) => '<tr><td><code>' + esc(r) + '</code></td><td class="num">' +
|
|
111
|
+
v.allowed + '</td><td class="num">' + v.blocked + '</td></tr>').join('')
|
|
112
|
+
: '<tr><td class="empty" colspan="3">No traffic yet</td></tr>';
|
|
113
|
+
} catch (e) {
|
|
114
|
+
el('status').textContent = 'error: ' + e.message;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
tick();
|
|
118
|
+
setInterval(tick, POLL);
|
|
119
|
+
</script>
|
|
120
|
+
</body>
|
|
121
|
+
</html>`;
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=html.js.map
|
package/dist/html.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"html.js","sourceRoot":"","sources":["../src/html.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,QAAgB,EAAE,MAAc;IAC5D,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuEQ,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC;iBACxB,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QA0Cf,CAAC;AACT,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @limiq/dashboard — embedded metrics UI + JSON API.
|
|
3
|
+
*/
|
|
4
|
+
export { routeDashboard } from "./router.js";
|
|
5
|
+
export type { DashboardOptions, DashboardRequest, DashboardResponse, } from "./router.js";
|
|
6
|
+
export { expressDashboard } from "./express.js";
|
|
7
|
+
export { dashboardHtml } from "./html.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,YAAY,EACV,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAM7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { MetricsCollector, RateLimiter } from "@limiq/core";
|
|
2
|
+
export interface DashboardOptions {
|
|
3
|
+
/** Metrics source (usually `limiter.getMetrics()`). */
|
|
4
|
+
collector: MetricsCollector;
|
|
5
|
+
/** Limiter — enables `/api/health` and admin `/api/reset`. */
|
|
6
|
+
limiter?: RateLimiter;
|
|
7
|
+
/** Mount path (default: "/__ratelimit"). No trailing slash. */
|
|
8
|
+
basePath?: string;
|
|
9
|
+
/** UI poll interval in ms (default: 2000). */
|
|
10
|
+
pollMs?: number;
|
|
11
|
+
/** Guards admin endpoints (reset). Defaults to deny-all. */
|
|
12
|
+
isAdmin?: (req: unknown) => boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface DashboardRequest {
|
|
15
|
+
method: string;
|
|
16
|
+
/** Full request path (we strip basePath ourselves). */
|
|
17
|
+
path: string;
|
|
18
|
+
query: URLSearchParams;
|
|
19
|
+
/** Result of options.isAdmin(nativeReq), computed by the adapter. */
|
|
20
|
+
isAdmin: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface DashboardResponse {
|
|
23
|
+
status: number;
|
|
24
|
+
contentType: string;
|
|
25
|
+
body: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Framework-agnostic dashboard router. Returns the response to send; adapters
|
|
29
|
+
* (e.g. Express) translate it to the native response.
|
|
30
|
+
*/
|
|
31
|
+
export declare function routeDashboard(options: DashboardOptions, req: DashboardRequest): Promise<DashboardResponse>;
|
|
32
|
+
//# sourceMappingURL=router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAGjE,MAAM,WAAW,gBAAgB;IAC/B,uDAAuD;IACvD,SAAS,EAAE,gBAAgB,CAAC;IAC5B,8DAA8D;IAC9D,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8CAA8C;IAC9C,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,4DAA4D;IAC5D,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC;CACrC;AAED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,uDAAuD;IACvD,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,eAAe,CAAC;IACvB,qEAAqE;IACrE,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;CACd;AAQD;;;GAGG;AACH,wBAAsB,cAAc,CAClC,OAAO,EAAE,gBAAgB,EACzB,GAAG,EAAE,gBAAgB,GACpB,OAAO,CAAC,iBAAiB,CAAC,CAoC5B"}
|
package/dist/router.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { dashboardHtml } from "./html.js";
|
|
2
|
+
const json = (status, data) => ({
|
|
3
|
+
status,
|
|
4
|
+
contentType: "application/json; charset=utf-8",
|
|
5
|
+
body: JSON.stringify(data),
|
|
6
|
+
});
|
|
7
|
+
/**
|
|
8
|
+
* Framework-agnostic dashboard router. Returns the response to send; adapters
|
|
9
|
+
* (e.g. Express) translate it to the native response.
|
|
10
|
+
*/
|
|
11
|
+
export async function routeDashboard(options, req) {
|
|
12
|
+
const basePath = options.basePath ?? "/__ratelimit";
|
|
13
|
+
const pollMs = options.pollMs ?? 2000;
|
|
14
|
+
if (!req.path.startsWith(basePath)) {
|
|
15
|
+
return json(404, { error: "Not found" });
|
|
16
|
+
}
|
|
17
|
+
const sub = req.path.slice(basePath.length) || "/";
|
|
18
|
+
if (req.method === "GET" && (sub === "/" || sub === "/index.html")) {
|
|
19
|
+
return {
|
|
20
|
+
status: 200,
|
|
21
|
+
contentType: "text/html; charset=utf-8",
|
|
22
|
+
body: dashboardHtml(basePath, pollMs),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (req.method === "GET" && sub === "/api/metrics") {
|
|
26
|
+
return json(200, options.collector.snapshot());
|
|
27
|
+
}
|
|
28
|
+
if (req.method === "GET" && sub === "/api/health") {
|
|
29
|
+
const store = options.limiter ? await options.limiter.health() : null;
|
|
30
|
+
return json(200, { ok: true, store });
|
|
31
|
+
}
|
|
32
|
+
if (req.method === "POST" && sub === "/api/reset") {
|
|
33
|
+
if (!req.isAdmin)
|
|
34
|
+
return json(403, { error: "Forbidden" });
|
|
35
|
+
if (!options.limiter)
|
|
36
|
+
return json(400, { error: "No limiter configured" });
|
|
37
|
+
const key = req.query.get("key");
|
|
38
|
+
if (!key)
|
|
39
|
+
return json(400, { error: "Missing `key` query param" });
|
|
40
|
+
await options.limiter.resetByKey(key);
|
|
41
|
+
return json(200, { ok: true, reset: key });
|
|
42
|
+
}
|
|
43
|
+
return json(404, { error: "Not found" });
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.js","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AA8B1C,MAAM,IAAI,GAAG,CAAC,MAAc,EAAE,IAAa,EAAqB,EAAE,CAAC,CAAC;IAClE,MAAM;IACN,WAAW,EAAE,iCAAiC;IAC9C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;CAC3B,CAAC,CAAC;AAEH;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,OAAyB,EACzB,GAAqB;IAErB,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,cAAc,CAAC;IACpD,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC;IAEtC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QACnC,OAAO,IAAI,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,CAAC;IAC3C,CAAC;IACD,MAAM,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC;IAEnD,IAAI,GAAG,CAAC,MAAM,KAAK,KAAK,IAAI,CAAC,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,aAAa,CAAC,EAAE,CAAC;QACnE,OAAO;YACL,MAAM,EAAE,GAAG;YACX,WAAW,EAAE,0BAA0B;YACvC,IAAI,EAAE,aAAa,CAAC,QAAQ,EAAE,MAAM,CAAC;SACtC,CAAC;IACJ,CAAC;IAED,IAAI,GAAG,CAAC,MAAM,KAAK,KAAK,IAAI,GAAG,KAAK,cAAc,EAAE,CAAC;QACnD,OAAO,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAC;IACjD,CAAC;IAED,IAAI,GAAG,CAAC,MAAM,KAAK,KAAK,IAAI,GAAG,KAAK,aAAa,EAAE,CAAC;QAClD,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;QACtE,OAAO,IAAI,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;IACxC,CAAC;IAED,IAAI,GAAG,CAAC,MAAM,KAAK,MAAM,IAAI,GAAG,KAAK,YAAY,EAAE,CAAC;QAClD,IAAI,CAAC,GAAG,CAAC,OAAO;YAAE,OAAO,IAAI,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,CAAC;QAC3D,IAAI,CAAC,OAAO,CAAC,OAAO;YAAE,OAAO,IAAI,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,uBAAuB,EAAE,CAAC,CAAC;QAC3E,MAAM,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACjC,IAAI,CAAC,GAAG;YAAE,OAAO,IAAI,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,2BAA2B,EAAE,CAAC,CAAC;QACnE,MAAM,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QACtC,OAAO,IAAI,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;IAC7C,CAAC;IAED,OAAO,IAAI,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,CAAC;AAC3C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@limiq/dashboard",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Lightweight embedded metrics dashboard + JSON API for @limiq/core",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@limiq/core": "0.1.0"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "Sandeep Vaid",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"keywords": [
|
|
26
|
+
"rate-limit",
|
|
27
|
+
"rate-limiter",
|
|
28
|
+
"ratelimit",
|
|
29
|
+
"throttle",
|
|
30
|
+
"throttling",
|
|
31
|
+
"redis",
|
|
32
|
+
"typescript",
|
|
33
|
+
"nodejs",
|
|
34
|
+
"dashboard",
|
|
35
|
+
"metrics",
|
|
36
|
+
"observability"
|
|
37
|
+
],
|
|
38
|
+
"homepage": "https://github.com/sandeepvaid/limiq#readme",
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/sandeepvaid/limiq/issues"
|
|
41
|
+
},
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/sandeepvaid/limiq.git",
|
|
45
|
+
"directory": "packages/dashboard"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsc -b"
|
|
52
|
+
}
|
|
6
53
|
}
|