express-sequelize-traffic 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 +190 -0
- package/examples/express-app.js +106 -0
- package/package.json +49 -0
- package/src/dashboard-public/assets/index-CaWHQ-tp.js +191 -0
- package/src/dashboard-public/assets/index-iT93XJlh.css +1 -0
- package/src/dashboard-public/index.html +13 -0
- package/src/dashboard.js +132 -0
- package/src/index.js +67 -0
- package/src/middleware.js +111 -0
- package/src/models/TrafficLog.js +83 -0
- package/src/realtime.js +71 -0
- package/src/routes/analyticsRoutes.js +83 -0
- package/src/services/analyticsService.js +286 -0
- package/src/utils/dashboardAuth.js +71 -0
- package/src/utils/routeMatcher.js +53 -0
- package/src/utils/safeAsync.js +13 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c)
|
|
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,190 @@
|
|
|
1
|
+
# express-sequelize-traffic
|
|
2
|
+
|
|
3
|
+
`express-sequelize-traffic` is a privacy-first, self-hosted npm package for Express.js applications. It records request analytics directly into your own Sequelize database and provides an optional realtime dashboard with REST APIs and Socket.IO updates.
|
|
4
|
+
|
|
5
|
+
No traffic data is sent to Google Analytics, Prometheus, Segment, Sentry, or any external service. The package only writes to the Sequelize connection you provide.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Express middleware that tracks every request without interrupting the main app on failure
|
|
10
|
+
- Sequelize `TrafficLog` model stored in the `traffic_logs` table
|
|
11
|
+
- Request tracking for user, session, route, status code, duration, slow routes, IP, user agent, start time, and end time
|
|
12
|
+
- Analytics REST API under the dashboard router
|
|
13
|
+
- Realtime `traffic:new-request` events over Socket.IO
|
|
14
|
+
- Optional built-in dashboard served from Express
|
|
15
|
+
- Simple HTTP basic authentication for the dashboard
|
|
16
|
+
- SQLite-backed example app for local testing
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
Install the package dependencies:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install express-sequelize-traffic
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
If you are working on this repository directly:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install
|
|
30
|
+
npm run build:dashboard
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Sequelize Setup
|
|
34
|
+
|
|
35
|
+
The package defines a `TrafficLog` model for you. You can either manage the table through your own migrations or call `traffic.sync()` for quick setup.
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
import { Sequelize } from "sequelize";
|
|
39
|
+
import { createTrafficTracker } from "express-sequelize-traffic";
|
|
40
|
+
|
|
41
|
+
const sequelize = new Sequelize(process.env.DB_URL);
|
|
42
|
+
|
|
43
|
+
const traffic = createTrafficTracker({ sequelize });
|
|
44
|
+
|
|
45
|
+
await sequelize.authenticate();
|
|
46
|
+
await traffic.sync();
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Express Usage
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
import express from "express";
|
|
53
|
+
import http from "node:http";
|
|
54
|
+
import { Sequelize } from "sequelize";
|
|
55
|
+
import { createTrafficTracker } from "express-sequelize-traffic";
|
|
56
|
+
|
|
57
|
+
const app = express();
|
|
58
|
+
const server = http.createServer(app);
|
|
59
|
+
const sequelize = new Sequelize(process.env.DB_URL);
|
|
60
|
+
|
|
61
|
+
const traffic = createTrafficTracker({
|
|
62
|
+
sequelize,
|
|
63
|
+
getUserId: (req) => req.user?.id || req.headers["x-user-id"],
|
|
64
|
+
getSessionId: (req) => req.sessionID || req.headers["x-session-id"],
|
|
65
|
+
slowRouteThresholdMs: 1000,
|
|
66
|
+
trackIp: false,
|
|
67
|
+
trackUserAgent: true,
|
|
68
|
+
ignoredRoutes: ["/health", "/favicon.ico"],
|
|
69
|
+
dashboard: {
|
|
70
|
+
enabled: true,
|
|
71
|
+
mountPath: "/traffic-dashboard",
|
|
72
|
+
username: process.env.TRAFFIC_ADMIN_USER,
|
|
73
|
+
password: process.env.TRAFFIC_ADMIN_PASS,
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
await traffic.sync();
|
|
78
|
+
|
|
79
|
+
app.use(traffic.middleware);
|
|
80
|
+
traffic.attachRealtime(server);
|
|
81
|
+
app.use("/traffic-dashboard", traffic.dashboard);
|
|
82
|
+
|
|
83
|
+
server.listen(3000);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Dashboard Usage
|
|
87
|
+
|
|
88
|
+
The dashboard is optional. Set `dashboard.enabled` to `true`, build the frontend assets, attach realtime support, and mount the provided router.
|
|
89
|
+
|
|
90
|
+
The dashboard expects the router mount path to match `dashboard.mountPath`. If you keep the default `/traffic-dashboard`, the client-side API and Socket.IO paths line up automatically.
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
npm run build:dashboard
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Options
|
|
97
|
+
|
|
98
|
+
| Option | Type | Default | Description |
|
|
99
|
+
| --- | --- | --- | --- |
|
|
100
|
+
| `sequelize` | `Sequelize` | required | Sequelize instance used for the `TrafficLog` model |
|
|
101
|
+
| `getUserId` | `(req) => string \| null` | `undefined` | Custom resolver for `userId` |
|
|
102
|
+
| `getSessionId` | `(req) => string \| null` | `undefined` | Custom resolver for `sessionId` |
|
|
103
|
+
| `slowRouteThresholdMs` | `number` | `1000` | Marks requests as slow when duration meets or exceeds this value |
|
|
104
|
+
| `ignoredRoutes` | `Array<string \| RegExp \| Function>` | `[]` | Routes or URLs to skip |
|
|
105
|
+
| `trackIp` | `boolean` | `false` | Persist `req.ip` when enabled |
|
|
106
|
+
| `trackUserAgent` | `boolean` | `true` | Persist the request `user-agent` header when enabled |
|
|
107
|
+
| `dashboard.enabled` | `boolean` | `false` | Enables dashboard APIs and static UI |
|
|
108
|
+
| `dashboard.mountPath` | `string` | `"/traffic-dashboard"` | Mount path used by the dashboard UI and realtime socket |
|
|
109
|
+
| `dashboard.username` | `string` | `undefined` | Basic auth username for the dashboard |
|
|
110
|
+
| `dashboard.password` | `string` | `undefined` | Basic auth password for the dashboard |
|
|
111
|
+
| `debug` | `boolean` | `false` | Logs tracking or analytics errors without throwing them |
|
|
112
|
+
|
|
113
|
+
## API Routes
|
|
114
|
+
|
|
115
|
+
When the dashboard router is mounted, these endpoints are available under `/<mountPath>/api`:
|
|
116
|
+
|
|
117
|
+
- `GET /api/overview`
|
|
118
|
+
- `GET /api/live`
|
|
119
|
+
- `GET /api/routes`
|
|
120
|
+
- `GET /api/users`
|
|
121
|
+
- `GET /api/errors`
|
|
122
|
+
|
|
123
|
+
`GET /api/overview` returns:
|
|
124
|
+
|
|
125
|
+
- `totalRequests`
|
|
126
|
+
- `averageDurationMs`
|
|
127
|
+
- `slowRequestCount`
|
|
128
|
+
- `errorRequestCount`
|
|
129
|
+
- `uniqueUsers`
|
|
130
|
+
- `topRoutes`
|
|
131
|
+
- `slowestRoutes`
|
|
132
|
+
- `statusCodeSummary`
|
|
133
|
+
|
|
134
|
+
The implementation also includes `latestRequests` and `requestsTimeline` to support the dashboard overview page.
|
|
135
|
+
|
|
136
|
+
## Realtime Events
|
|
137
|
+
|
|
138
|
+
Call `traffic.attachRealtime(server)` with your Node HTTP server. Each stored request emits:
|
|
139
|
+
|
|
140
|
+
- Event: `traffic:new-request`
|
|
141
|
+
|
|
142
|
+
Payload:
|
|
143
|
+
|
|
144
|
+
```json
|
|
145
|
+
{
|
|
146
|
+
"userId": "123",
|
|
147
|
+
"sessionId": "session-1",
|
|
148
|
+
"method": "GET",
|
|
149
|
+
"route": "/api/products",
|
|
150
|
+
"originalUrl": "/api/products?page=1",
|
|
151
|
+
"statusCode": 200,
|
|
152
|
+
"durationMs": 187,
|
|
153
|
+
"isSlow": false,
|
|
154
|
+
"createdAt": "2026-05-07T12:00:00.000Z"
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Example App
|
|
159
|
+
|
|
160
|
+
Run the SQLite example:
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
npm run example
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Example endpoints:
|
|
167
|
+
|
|
168
|
+
- `GET /api/products`
|
|
169
|
+
- `GET /api/slow`
|
|
170
|
+
- `GET /api/error`
|
|
171
|
+
- `GET /api/users/:id`
|
|
172
|
+
- `GET /traffic-dashboard/`
|
|
173
|
+
|
|
174
|
+
The example dashboard uses HTTP basic auth with:
|
|
175
|
+
|
|
176
|
+
- username: `admin`
|
|
177
|
+
- password: `password`
|
|
178
|
+
|
|
179
|
+
Override them with `TRAFFIC_ADMIN_USER` and `TRAFFIC_ADMIN_PASS`.
|
|
180
|
+
|
|
181
|
+
## Screenshot Placeholders
|
|
182
|
+
|
|
183
|
+
- Overview dashboard screenshot placeholder
|
|
184
|
+
- Live traffic dashboard screenshot placeholder
|
|
185
|
+
|
|
186
|
+
Capture real screenshots after building and running the example app.
|
|
187
|
+
|
|
188
|
+
## License
|
|
189
|
+
|
|
190
|
+
MIT
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import http from "node:http";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { Sequelize } from "sequelize";
|
|
6
|
+
import { createTrafficTracker } from "../src/index.js";
|
|
7
|
+
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
+
const __dirname = path.dirname(__filename);
|
|
10
|
+
|
|
11
|
+
const app = express();
|
|
12
|
+
const server = http.createServer(app);
|
|
13
|
+
|
|
14
|
+
const sequelize = new Sequelize({
|
|
15
|
+
dialect: "sqlite",
|
|
16
|
+
storage: path.join(__dirname, "traffic-demo.sqlite"),
|
|
17
|
+
logging: false,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const traffic = createTrafficTracker({
|
|
21
|
+
sequelize,
|
|
22
|
+
getUserId: (req) => req.user?.id || req.headers["x-user-id"] || null,
|
|
23
|
+
getSessionId: (req) => req.headers["x-session-id"] || null,
|
|
24
|
+
slowRouteThresholdMs: 700,
|
|
25
|
+
trackIp: false,
|
|
26
|
+
trackUserAgent: true,
|
|
27
|
+
ignoredRoutes: ["/health", "/favicon.ico"],
|
|
28
|
+
dashboard: {
|
|
29
|
+
enabled: true,
|
|
30
|
+
mountPath: "/traffic-dashboard",
|
|
31
|
+
username: process.env.TRAFFIC_ADMIN_USER || "admin",
|
|
32
|
+
password: process.env.TRAFFIC_ADMIN_PASS || "password",
|
|
33
|
+
},
|
|
34
|
+
debug: true,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
function wait(ms) {
|
|
38
|
+
return new Promise((resolve) => {
|
|
39
|
+
setTimeout(resolve, ms);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
app.use(express.json());
|
|
44
|
+
|
|
45
|
+
app.use((req, _res, next) => {
|
|
46
|
+
const userId = req.headers["x-user-id"];
|
|
47
|
+
|
|
48
|
+
req.user = userId ? { id: String(userId) } : null;
|
|
49
|
+
next();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
app.use(traffic.middleware);
|
|
53
|
+
traffic.attachRealtime(server);
|
|
54
|
+
app.use("/traffic-dashboard", traffic.dashboard);
|
|
55
|
+
|
|
56
|
+
app.get("/health", (_req, res) => {
|
|
57
|
+
res.json({ ok: true });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
app.get("/api/products", async (_req, res) => {
|
|
61
|
+
await wait(120);
|
|
62
|
+
|
|
63
|
+
res.json([
|
|
64
|
+
{ id: 1, name: "Signal Desk" },
|
|
65
|
+
{ id: 2, name: "Route Pulse" },
|
|
66
|
+
{ id: 3, name: "Latency Watch" },
|
|
67
|
+
]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
app.get("/api/slow", async (_req, res) => {
|
|
71
|
+
await wait(1200);
|
|
72
|
+
res.json({ ok: true, message: "This route should show up as slow." });
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
app.get("/api/error", async (_req, res) => {
|
|
76
|
+
await wait(320);
|
|
77
|
+
res.status(500).json({ error: "Synthetic example failure." });
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
app.get("/api/users/:id", async (req, res) => {
|
|
81
|
+
await wait(180);
|
|
82
|
+
res.json({
|
|
83
|
+
id: req.params.id,
|
|
84
|
+
name: `User ${req.params.id}`,
|
|
85
|
+
requestedAt: new Date().toISOString(),
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
async function start() {
|
|
90
|
+
await sequelize.authenticate();
|
|
91
|
+
await traffic.sync();
|
|
92
|
+
|
|
93
|
+
const port = Number(process.env.PORT || 3000);
|
|
94
|
+
|
|
95
|
+
server.listen(port, () => {
|
|
96
|
+
console.log(`Example app running on http://localhost:${port}`);
|
|
97
|
+
console.log(
|
|
98
|
+
`Dashboard available on http://localhost:${port}/traffic-dashboard/`,
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
start().catch((error) => {
|
|
104
|
+
console.error("Failed to start example app.", error);
|
|
105
|
+
process.exitCode = 1;
|
|
106
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "express-sequelize-traffic",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Privacy-first, self-hosted Express request analytics with Sequelize storage and an optional realtime dashboard.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"workspaces": [
|
|
8
|
+
"dashboard-app"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": "./src/index.js"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"examples",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"dev": "npm run example",
|
|
21
|
+
"build": "npm run build:dashboard",
|
|
22
|
+
"build:dashboard": "npm run build --workspace dashboard-app",
|
|
23
|
+
"example": "node examples/express-app.js",
|
|
24
|
+
"prepack": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"express",
|
|
28
|
+
"sequelize",
|
|
29
|
+
"analytics",
|
|
30
|
+
"traffic",
|
|
31
|
+
"dashboard",
|
|
32
|
+
"socket.io",
|
|
33
|
+
"privacy-first"
|
|
34
|
+
],
|
|
35
|
+
"author": "",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"express": ">=4.18.0",
|
|
39
|
+
"sequelize": ">=6.0.0"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"socket.io": "^4.8.1"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"express": "^4.21.2",
|
|
46
|
+
"sequelize": "^6.37.5",
|
|
47
|
+
"sqlite3": "^5.1.7"
|
|
48
|
+
}
|
|
49
|
+
}
|