apiforgejs 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 +117 -0
- package/package.json +33 -0
- package/src/aggregator.js +91 -0
- package/src/dashboard.js +186 -0
- package/src/database.js +298 -0
- package/src/index.js +60 -0
- package/src/insights.js +185 -0
- package/src/interceptor.js +105 -0
- package/src/transport.js +33 -0
- package/src/ui.html +1648 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 APIForge Organisation
|
|
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
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# apiforgejs
|
|
2
|
+
|
|
3
|
+
API observability & intelligence SDK for Express.js — local-first, privacy-first.
|
|
4
|
+
|
|
5
|
+
> Track latency, error rates, and behavioral trends of your APIs. Everything stays on your machine.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install apiforgejs
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
const express = require('express');
|
|
17
|
+
const { apiforge } = require('apiforgejs');
|
|
18
|
+
|
|
19
|
+
const app = express();
|
|
20
|
+
|
|
21
|
+
// Add the middleware — one line, zero config required
|
|
22
|
+
app.use(apiforge({ mode: 'local' }));
|
|
23
|
+
|
|
24
|
+
app.get('/users/:id', (req, res) => res.json({ id: req.params.id }));
|
|
25
|
+
|
|
26
|
+
app.listen(3000, () => {
|
|
27
|
+
console.log('App running on :3000');
|
|
28
|
+
// Dashboard auto-starts at http://localhost:4242
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Dashboard
|
|
33
|
+
|
|
34
|
+
Open **http://localhost:4242** after starting your app. The dashboard shows:
|
|
35
|
+
|
|
36
|
+
- **Health Score** (0–100) — global API health at a glance
|
|
37
|
+
- **Latency percentiles** — P50 / P90 / P99 per route
|
|
38
|
+
- **Error rates** — 4xx and 5xx breakdown
|
|
39
|
+
- **Automatic insights** — latency anomalies, dead endpoints, release regressions
|
|
40
|
+
- **Time series chart** — click any route to see its latency over time
|
|
41
|
+
|
|
42
|
+
Data is collected locally in `.apiforge.db` (SQLite). Nothing leaves your machine.
|
|
43
|
+
|
|
44
|
+
## Configuration
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
app.use(apiforge({
|
|
48
|
+
mode: 'local', // only supported mode in v0.x
|
|
49
|
+
dbPath: '.apiforge.db', // SQLite file location
|
|
50
|
+
dashboardPort: 4242, // set to 0 to disable dashboard
|
|
51
|
+
flushInterval: 60_000, // flush to SQLite every 60s (ms)
|
|
52
|
+
env: process.env.NODE_ENV, // 'production' | 'staging' | 'development'
|
|
53
|
+
release: process.env.APP_VERSION, // enables release regression detection
|
|
54
|
+
service: 'my-api', // label for multi-service setups
|
|
55
|
+
sampling: 1.0, // 0.0–1.0 sample rate
|
|
56
|
+
ignorePaths: ['/health', '/ping'], // paths to skip
|
|
57
|
+
}));
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Release tracking
|
|
61
|
+
|
|
62
|
+
Pass your release version to enable before/after deployment comparison:
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
app.use(apiforge({
|
|
66
|
+
mode: 'local',
|
|
67
|
+
release: process.env.npm_package_version, // or 'v1.4.0', git SHA, etc.
|
|
68
|
+
}));
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
When a new release is detected, APIForge automatically compares P90 latency before vs. after and surfaces regressions as insights.
|
|
72
|
+
|
|
73
|
+
## Graceful shutdown
|
|
74
|
+
|
|
75
|
+
```javascript
|
|
76
|
+
const forge = apiforge({ mode: 'local' });
|
|
77
|
+
app.use(forge);
|
|
78
|
+
|
|
79
|
+
process.on('SIGTERM', () => {
|
|
80
|
+
forge.shutdown(); // flush remaining buffer to SQLite
|
|
81
|
+
process.exit(0);
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Privacy by design
|
|
86
|
+
|
|
87
|
+
The SDK **never** collects:
|
|
88
|
+
- Request or response bodies
|
|
89
|
+
- HTTP headers (Authorization, Cookie, etc.)
|
|
90
|
+
- Query string values
|
|
91
|
+
- Route parameter values (`/users/12345` → only `/users/:id` is stored)
|
|
92
|
+
- IP addresses or User-Agent strings
|
|
93
|
+
|
|
94
|
+
Collected fields: route pattern, HTTP method, status code, latency (ms), timestamp, and optional env/release/service labels.
|
|
95
|
+
|
|
96
|
+
## Data collected
|
|
97
|
+
|
|
98
|
+
```json
|
|
99
|
+
{
|
|
100
|
+
"route": "GET /users/:id",
|
|
101
|
+
"method": "GET",
|
|
102
|
+
"status": 200,
|
|
103
|
+
"duration_ms": 134.7,
|
|
104
|
+
"timestamp": "2026-05-13T10:00:00.000Z",
|
|
105
|
+
"env": "production",
|
|
106
|
+
"release": "v1.4.0"
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Requirements
|
|
111
|
+
|
|
112
|
+
- Node.js >= 18
|
|
113
|
+
- Express >= 4
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "apiforgejs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "API observability & intelligence SDK for Express.js — local-first, privacy-first",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"api", "observability", "monitoring", "express", "metrics",
|
|
8
|
+
"performance", "analytics", "middleware", "latency", "local-first"
|
|
9
|
+
],
|
|
10
|
+
"author": "APIForge",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "node --test test/**/*.test.js",
|
|
14
|
+
"test:smoke": "node --test test/smoke.test.js"
|
|
15
|
+
},
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=22.5.0"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"react": "^18.0.0",
|
|
21
|
+
"react-dom": "^18.0.0"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"express": ">=4.0.0"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"src/"
|
|
28
|
+
],
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/APIForge-Organisation/sdk-nodejs"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class Aggregator {
|
|
4
|
+
constructor(transport, flushIntervalMs = 60_000) {
|
|
5
|
+
this.transport = transport;
|
|
6
|
+
this.flushIntervalMs = flushIntervalMs;
|
|
7
|
+
this.buffer = new Map();
|
|
8
|
+
this.timer = null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
start() {
|
|
12
|
+
this.timer = setInterval(() => this._flush(), this.flushIntervalMs);
|
|
13
|
+
// Don't prevent the process from exiting if nothing else is running
|
|
14
|
+
if (this.timer.unref) this.timer.unref();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
stop() {
|
|
18
|
+
if (this.timer) {
|
|
19
|
+
clearInterval(this.timer);
|
|
20
|
+
this.timer = null;
|
|
21
|
+
}
|
|
22
|
+
this._flush();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
record(event) {
|
|
26
|
+
const key = `${event.method}|${event.route}|${event.env}|${event.release || ''}`;
|
|
27
|
+
let bucket = this.buffer.get(key);
|
|
28
|
+
|
|
29
|
+
if (!bucket) {
|
|
30
|
+
bucket = {
|
|
31
|
+
method: event.method,
|
|
32
|
+
route: event.route,
|
|
33
|
+
env: event.env,
|
|
34
|
+
release: event.release,
|
|
35
|
+
durations: [],
|
|
36
|
+
status_2xx: 0,
|
|
37
|
+
status_4xx: 0,
|
|
38
|
+
status_5xx: 0,
|
|
39
|
+
};
|
|
40
|
+
this.buffer.set(key, bucket);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
bucket.durations.push(event.duration_ms);
|
|
44
|
+
|
|
45
|
+
const s = event.status;
|
|
46
|
+
if (s >= 200 && s < 300) bucket.status_2xx++;
|
|
47
|
+
else if (s >= 400 && s < 500) bucket.status_4xx++;
|
|
48
|
+
else if (s >= 500) bucket.status_5xx++;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
_flush() {
|
|
52
|
+
if (this.buffer.size === 0) return;
|
|
53
|
+
|
|
54
|
+
// Round down to the current minute as the bucket timestamp
|
|
55
|
+
const bucketTs = Math.floor(Date.now() / 60_000) * 60;
|
|
56
|
+
const rows = [];
|
|
57
|
+
|
|
58
|
+
for (const bucket of this.buffer.values()) {
|
|
59
|
+
const sorted = bucket.durations.slice().sort((a, b) => a - b);
|
|
60
|
+
const n = sorted.length;
|
|
61
|
+
|
|
62
|
+
rows.push({
|
|
63
|
+
bucket_ts: bucketTs,
|
|
64
|
+
route: bucket.route,
|
|
65
|
+
method: bucket.method,
|
|
66
|
+
env: bucket.env,
|
|
67
|
+
release_tag: bucket.release,
|
|
68
|
+
status_2xx: bucket.status_2xx,
|
|
69
|
+
status_4xx: bucket.status_4xx,
|
|
70
|
+
status_5xx: bucket.status_5xx,
|
|
71
|
+
total_calls: n,
|
|
72
|
+
lat_p50: percentile(sorted, 0.50),
|
|
73
|
+
lat_p90: percentile(sorted, 0.90),
|
|
74
|
+
lat_p99: percentile(sorted, 0.99),
|
|
75
|
+
lat_min: sorted[0] ?? 0,
|
|
76
|
+
lat_max: sorted[n - 1] ?? 0,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
this.buffer.clear();
|
|
81
|
+
this.transport.write(rows);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function percentile(sorted, p) {
|
|
86
|
+
if (sorted.length === 0) return 0;
|
|
87
|
+
const idx = Math.min(Math.ceil(p * sorted.length) - 1, sorted.length - 1);
|
|
88
|
+
return sorted[Math.max(0, idx)];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
module.exports = { Aggregator };
|
package/src/dashboard.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const http = require('http');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const { getInsights, computeHealthScore } = require('./insights');
|
|
7
|
+
|
|
8
|
+
const UI_HTML = path.join(__dirname, 'ui.html');
|
|
9
|
+
|
|
10
|
+
// Resolve React / ReactDOM UMD bundles from node_modules.
|
|
11
|
+
// These are added as direct dependencies of apiforgejs so they are always present.
|
|
12
|
+
function resolveAsset(pkg, file) {
|
|
13
|
+
try {
|
|
14
|
+
const main = require.resolve(pkg);
|
|
15
|
+
const pkgDir = path.dirname(main);
|
|
16
|
+
const candidate = path.join(pkgDir, 'umd', file);
|
|
17
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
18
|
+
const alt = path.join(pkgDir, file);
|
|
19
|
+
if (fs.existsSync(alt)) return alt;
|
|
20
|
+
} catch (_) {}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const REACT_PATH = resolveAsset('react', 'react.production.min.js');
|
|
25
|
+
const REACT_DOM_PATH = resolveAsset('react-dom', 'react-dom.production.min.js');
|
|
26
|
+
|
|
27
|
+
function startDashboard(db, port) {
|
|
28
|
+
const server = http.createServer((req, res) => {
|
|
29
|
+
const url = new URL(req.url, `http://localhost:${port}`);
|
|
30
|
+
try {
|
|
31
|
+
route(req, res, url, db);
|
|
32
|
+
} catch (err) {
|
|
33
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
34
|
+
res.end(JSON.stringify({ error: err.message }));
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
server.listen(port, '127.0.0.1', () => {
|
|
39
|
+
console.log(`[apiforgejs] Dashboard → http://localhost:${port}`);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
server.on('error', (err) => {
|
|
43
|
+
if (err.code === 'EADDRINUSE') {
|
|
44
|
+
console.warn(`[apiforgejs] Port ${port} already in use — dashboard not started.`);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return server;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function route(req, res, url, db) {
|
|
52
|
+
const pathname = url.pathname;
|
|
53
|
+
|
|
54
|
+
// ── HTML shell ────────────────────────────────────────────────────────────
|
|
55
|
+
if (req.method === 'GET' && pathname === '/') {
|
|
56
|
+
serveFile(res, UI_HTML, 'text/html; charset=utf-8');
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ── Static assets (React UMD) ─────────────────────────────────────────────
|
|
61
|
+
if (req.method === 'GET' && pathname === '/assets/react.js') {
|
|
62
|
+
if (REACT_PATH) {
|
|
63
|
+
serveFile(res, REACT_PATH, 'application/javascript');
|
|
64
|
+
} else {
|
|
65
|
+
res.writeHead(404);
|
|
66
|
+
res.end('// react not found — run npm install inside apiforgejs');
|
|
67
|
+
}
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (req.method === 'GET' && pathname === '/assets/react-dom.js') {
|
|
72
|
+
if (REACT_DOM_PATH) {
|
|
73
|
+
serveFile(res, REACT_DOM_PATH, 'application/javascript');
|
|
74
|
+
} else {
|
|
75
|
+
res.writeHead(404);
|
|
76
|
+
res.end('// react-dom not found — run npm install inside apiforgejs');
|
|
77
|
+
}
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ── API: summary ──────────────────────────────────────────────────────────
|
|
82
|
+
if (req.method === 'GET' && pathname === '/api/summary') {
|
|
83
|
+
const data = db.getSummary();
|
|
84
|
+
const healthScore = computeHealthScore(db);
|
|
85
|
+
const insights = getInsights(db);
|
|
86
|
+
|
|
87
|
+
const total = data.recent?.calls_total || 0;
|
|
88
|
+
const errors = (data.recent?.calls_4xx || 0) + (data.recent?.calls_5xx || 0);
|
|
89
|
+
const errorRate = total > 0 ? (errors / total) * 100 : 0;
|
|
90
|
+
|
|
91
|
+
sendJson(res, {
|
|
92
|
+
health_score: healthScore,
|
|
93
|
+
calls_24h: total,
|
|
94
|
+
error_rate_24h: parseFloat(errorRate.toFixed(2)),
|
|
95
|
+
avg_p90_24h: round(data.recent?.avg_p90),
|
|
96
|
+
avg_p99_24h: round(data.recent?.avg_p99),
|
|
97
|
+
active_routes: data.activeRoutes,
|
|
98
|
+
total_routes: data.totalRoutes,
|
|
99
|
+
insights_count: insights.length,
|
|
100
|
+
insights,
|
|
101
|
+
});
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ── API: routes ───────────────────────────────────────────────────────────
|
|
106
|
+
if (req.method === 'GET' && pathname === '/api/routes') {
|
|
107
|
+
const hours = parseInt(url.searchParams.get('hours') || '24', 10);
|
|
108
|
+
const routes = db.getRoutes(hours);
|
|
109
|
+
|
|
110
|
+
const untracked = db.getUntrackedRoutes().map(r => ({
|
|
111
|
+
route: r.route,
|
|
112
|
+
method: r.method,
|
|
113
|
+
calls: 0,
|
|
114
|
+
calls_2xx: 0,
|
|
115
|
+
calls_4xx: 0,
|
|
116
|
+
calls_5xx: 0,
|
|
117
|
+
p50: null,
|
|
118
|
+
p90: null,
|
|
119
|
+
p99: null,
|
|
120
|
+
lat_max: null,
|
|
121
|
+
untracked: true,
|
|
122
|
+
}));
|
|
123
|
+
|
|
124
|
+
sendJson(res, [...routes, ...untracked]);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ── API: timeseries (per route) ───────────────────────────────────────────
|
|
129
|
+
if (req.method === 'GET' && pathname === '/api/timeseries') {
|
|
130
|
+
const r = url.searchParams.get('route');
|
|
131
|
+
const m = url.searchParams.get('method');
|
|
132
|
+
const hours = parseInt(url.searchParams.get('hours') || '24', 10);
|
|
133
|
+
|
|
134
|
+
if (!r || !m) {
|
|
135
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
136
|
+
res.end(JSON.stringify({ error: 'route and method are required' }));
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
sendJson(res, db.getTimeSeries(r, m, hours));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ── API: global timeseries (all routes aggregated) ────────────────────────
|
|
145
|
+
if (req.method === 'GET' && pathname === '/api/global-timeseries') {
|
|
146
|
+
const hours = parseInt(url.searchParams.get('hours') || '24', 10);
|
|
147
|
+
sendJson(res, db.getGlobalTimeSeries(hours));
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ── API: releases ─────────────────────────────────────────────────────────
|
|
152
|
+
if (req.method === 'GET' && pathname === '/api/releases') {
|
|
153
|
+
sendJson(res, db.getReleases());
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
res.writeHead(404);
|
|
158
|
+
res.end('Not found');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function serveFile(res, filePath, contentType) {
|
|
162
|
+
fs.readFile(filePath, (err, data) => {
|
|
163
|
+
if (err) {
|
|
164
|
+
res.writeHead(404);
|
|
165
|
+
res.end('File not found');
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
res.writeHead(200, { 'Content-Type': contentType, 'Cache-Control': 'no-cache' });
|
|
169
|
+
res.end(data);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function sendJson(res, data) {
|
|
174
|
+
const body = JSON.stringify(data);
|
|
175
|
+
res.writeHead(200, {
|
|
176
|
+
'Content-Type': 'application/json',
|
|
177
|
+
'Access-Control-Allow-Origin': '*',
|
|
178
|
+
});
|
|
179
|
+
res.end(body);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function round(v) {
|
|
183
|
+
return v != null ? parseFloat(v.toFixed(2)) : null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
module.exports = { startDashboard };
|