express-sequelize-traffic 0.1.0 → 0.3.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/README.md +155 -4
- package/dist/index.cjs +880 -0
- package/dist/index.js +844 -0
- package/package.json +13 -6
- package/examples/express-app.js +0 -106
- package/src/dashboard.js +0 -132
- package/src/index.js +0 -67
- package/src/middleware.js +0 -111
- package/src/models/TrafficLog.js +0 -83
- package/src/realtime.js +0 -71
- package/src/routes/analyticsRoutes.js +0 -83
- package/src/services/analyticsService.js +0 -286
- package/src/utils/dashboardAuth.js +0 -71
- package/src/utils/routeMatcher.js +0 -53
- package/src/utils/safeAsync.js +0 -13
- /package/{src → dist}/dashboard-public/assets/index-CaWHQ-tp.js +0 -0
- /package/{src → dist}/dashboard-public/assets/index-iT93XJlh.css +0 -0
- /package/{src → dist}/dashboard-public/index.html +0 -0
package/README.md
CHANGED
|
@@ -27,12 +27,12 @@ If you are working on this repository directly:
|
|
|
27
27
|
|
|
28
28
|
```bash
|
|
29
29
|
npm install
|
|
30
|
-
npm run build
|
|
30
|
+
npm run build
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
-
##
|
|
33
|
+
## Quick Setup
|
|
34
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.
|
|
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. `traffic.sync()` wraps Sequelize model sync and creates the `traffic_logs` table if it does not already exist.
|
|
36
36
|
|
|
37
37
|
```js
|
|
38
38
|
import { Sequelize } from "sequelize";
|
|
@@ -46,7 +46,9 @@ await sequelize.authenticate();
|
|
|
46
46
|
await traffic.sync();
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
-
##
|
|
49
|
+
## ES Module Setup
|
|
50
|
+
|
|
51
|
+
Use this form when the target app uses `"type": "module"` or native ESM imports.
|
|
50
52
|
|
|
51
53
|
```js
|
|
52
54
|
import express from "express";
|
|
@@ -83,6 +85,155 @@ app.use("/traffic-dashboard", traffic.dashboard);
|
|
|
83
85
|
server.listen(3000);
|
|
84
86
|
```
|
|
85
87
|
|
|
88
|
+
## CommonJS Setup
|
|
89
|
+
|
|
90
|
+
The package also publishes a CommonJS entry, so `require(...)` works directly.
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
const express = require("express");
|
|
94
|
+
const http = require("node:http");
|
|
95
|
+
const { Sequelize } = require("sequelize");
|
|
96
|
+
const { createTrafficTracker } = require("express-sequelize-traffic");
|
|
97
|
+
|
|
98
|
+
const app = express();
|
|
99
|
+
const server = http.createServer(app);
|
|
100
|
+
|
|
101
|
+
const sequelize = new Sequelize(process.env.DB_URL, {
|
|
102
|
+
logging: false,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
async function start() {
|
|
106
|
+
const traffic = createTrafficTracker({
|
|
107
|
+
sequelize,
|
|
108
|
+
getUserId: (req) => req.user?.id || req.headers["x-user-id"] || null,
|
|
109
|
+
getSessionId: (req) => req.sessionID || req.headers["x-session-id"] || null,
|
|
110
|
+
slowRouteThresholdMs: 1000,
|
|
111
|
+
ignoredRoutes: ["/health", "/favicon.ico"],
|
|
112
|
+
trackIp: false,
|
|
113
|
+
trackUserAgent: true,
|
|
114
|
+
dashboard: {
|
|
115
|
+
enabled: true,
|
|
116
|
+
mountPath: "/traffic-dashboard",
|
|
117
|
+
username: process.env.TRAFFIC_ADMIN_USER,
|
|
118
|
+
password: process.env.TRAFFIC_ADMIN_PASS,
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
await sequelize.authenticate();
|
|
123
|
+
await traffic.sync();
|
|
124
|
+
|
|
125
|
+
app.use(traffic.middleware);
|
|
126
|
+
traffic.attachRealtime(server);
|
|
127
|
+
app.use("/traffic-dashboard", traffic.dashboard);
|
|
128
|
+
|
|
129
|
+
app.get("/health", (_req, res) => {
|
|
130
|
+
res.json({ ok: true });
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
server.listen(3000);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
start().catch((error) => {
|
|
137
|
+
console.error("Startup failed:", error);
|
|
138
|
+
process.exit(1);
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Passing User Details
|
|
143
|
+
|
|
144
|
+
The package does not assume how authentication works in your app. You pass user and session details through resolver functions.
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
const traffic = createTrafficTracker({
|
|
148
|
+
sequelize,
|
|
149
|
+
getUserId: (req) =>
|
|
150
|
+
req.user?.id || req.auth?.sub || req.headers["x-user-id"] || null,
|
|
151
|
+
getSessionId: (req) =>
|
|
152
|
+
req.sessionID || req.cookies?.sessionId || req.headers["x-session-id"] || null,
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Common sources are:
|
|
157
|
+
|
|
158
|
+
- `req.user.id` from Passport or your own auth middleware
|
|
159
|
+
- `req.auth.sub` from JWT middleware
|
|
160
|
+
- `req.sessionID` from `express-session`
|
|
161
|
+
- custom request headers in internal systems
|
|
162
|
+
|
|
163
|
+
Rules to follow:
|
|
164
|
+
|
|
165
|
+
- Register auth and session middleware before `traffic.middleware`
|
|
166
|
+
- Return `null` when user or session data is unavailable
|
|
167
|
+
- If you omit `getUserId` or `getSessionId`, those fields are stored as `NULL`
|
|
168
|
+
|
|
169
|
+
## Why It Tracks More Than Application Routes
|
|
170
|
+
|
|
171
|
+
This package is request middleware. If you mount it globally with:
|
|
172
|
+
|
|
173
|
+
```js
|
|
174
|
+
app.use(traffic.middleware);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
it will see every request that reaches that point in the Express stack, not just your business endpoints.
|
|
178
|
+
|
|
179
|
+
That includes:
|
|
180
|
+
|
|
181
|
+
- API routes
|
|
182
|
+
- page routes
|
|
183
|
+
- health checks
|
|
184
|
+
- static files
|
|
185
|
+
- 404 requests
|
|
186
|
+
- dashboard API requests
|
|
187
|
+
- dashboard frontend asset requests
|
|
188
|
+
|
|
189
|
+
Internally, the tracker records the route from `req.route?.path`, then falls back to `req.path`, then `req.originalUrl`. That is why it can still log requests even when Express does not resolve them to a named application route.
|
|
190
|
+
|
|
191
|
+
## How To Track Only Application Routes
|
|
192
|
+
|
|
193
|
+
If you want to limit tracking to business endpoints, use one or more of these patterns.
|
|
194
|
+
|
|
195
|
+
Mount the tracker only on the route prefix you care about:
|
|
196
|
+
|
|
197
|
+
```js
|
|
198
|
+
app.use("/api", traffic.middleware);
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Register static assets or health checks before the tracker:
|
|
202
|
+
|
|
203
|
+
```js
|
|
204
|
+
app.use("/public", express.static("public"));
|
|
205
|
+
app.get("/health", (_req, res) => res.json({ ok: true }));
|
|
206
|
+
|
|
207
|
+
app.use(traffic.middleware);
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
Ignore route groups with regex:
|
|
211
|
+
|
|
212
|
+
```js
|
|
213
|
+
const traffic = createTrafficTracker({
|
|
214
|
+
sequelize,
|
|
215
|
+
ignoredRoutes: [
|
|
216
|
+
"/health",
|
|
217
|
+
"/favicon.ico",
|
|
218
|
+
/^\/traffic-dashboard(\/|$)/,
|
|
219
|
+
/^\/public(\/|$)/,
|
|
220
|
+
],
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Use a function when the skip logic depends on route naming conventions:
|
|
225
|
+
|
|
226
|
+
```js
|
|
227
|
+
const traffic = createTrafficTracker({
|
|
228
|
+
sequelize,
|
|
229
|
+
ignoredRoutes: [
|
|
230
|
+
(route) => route.startsWith("/internal/"),
|
|
231
|
+
],
|
|
232
|
+
});
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
If you mount the tracker globally, global tracking is expected behavior.
|
|
236
|
+
|
|
86
237
|
## Dashboard Usage
|
|
87
238
|
|
|
88
239
|
The dashboard is optional. Set `dashboard.enabled` to `true`, build the frontend assets, attach realtime support, and mount the provided router.
|