express-sequelize-traffic 0.3.0 → 0.4.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 +34 -1
- package/dist/index.cjs +28 -6
- package/dist/index.js +28 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -65,6 +65,7 @@ const traffic = createTrafficTracker({
|
|
|
65
65
|
getUserId: (req) => req.user?.id || req.headers["x-user-id"],
|
|
66
66
|
getSessionId: (req) => req.sessionID || req.headers["x-session-id"],
|
|
67
67
|
slowRouteThresholdMs: 1000,
|
|
68
|
+
includeRoutes: ["/api/*"],
|
|
68
69
|
trackIp: false,
|
|
69
70
|
trackUserAgent: true,
|
|
70
71
|
ignoredRoutes: ["/health", "/favicon.ico"],
|
|
@@ -108,6 +109,7 @@ async function start() {
|
|
|
108
109
|
getUserId: (req) => req.user?.id || req.headers["x-user-id"] || null,
|
|
109
110
|
getSessionId: (req) => req.sessionID || req.headers["x-session-id"] || null,
|
|
110
111
|
slowRouteThresholdMs: 1000,
|
|
112
|
+
includeRoutes: ["/api/*"],
|
|
111
113
|
ignoredRoutes: ["/health", "/favicon.ico"],
|
|
112
114
|
trackIp: false,
|
|
113
115
|
trackUserAgent: true,
|
|
@@ -190,7 +192,37 @@ Internally, the tracker records the route from `req.route?.path`, then falls bac
|
|
|
190
192
|
|
|
191
193
|
## How To Track Only Application Routes
|
|
192
194
|
|
|
193
|
-
If you want to limit tracking to business endpoints,
|
|
195
|
+
If you want to limit tracking to business endpoints, start with `includeRoutes`. It is usually easier than maintaining a long `ignoredRoutes` list.
|
|
196
|
+
|
|
197
|
+
Track only route families you want:
|
|
198
|
+
|
|
199
|
+
```js
|
|
200
|
+
const traffic = createTrafficTracker({
|
|
201
|
+
sequelize,
|
|
202
|
+
includeRoutes: ["/api/*", "/admin/*"],
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
String matchers support:
|
|
207
|
+
|
|
208
|
+
- exact matches like `"/health"`
|
|
209
|
+
- prefix-style wildcards like `"/api/*"`
|
|
210
|
+
- regex matchers like `/^\/v[0-9]+\//`
|
|
211
|
+
- custom functions
|
|
212
|
+
|
|
213
|
+
If `includeRoutes` is provided, only matching requests are tracked. You can still use `ignoredRoutes` to exclude specific subsets from a broader include rule.
|
|
214
|
+
|
|
215
|
+
Example:
|
|
216
|
+
|
|
217
|
+
```js
|
|
218
|
+
const traffic = createTrafficTracker({
|
|
219
|
+
sequelize,
|
|
220
|
+
includeRoutes: ["/api/*"],
|
|
221
|
+
ignoredRoutes: ["/api/internal/*"],
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
You can also use one or more of these mounting patterns.
|
|
194
226
|
|
|
195
227
|
Mount the tracker only on the route prefix you care about:
|
|
196
228
|
|
|
@@ -252,6 +284,7 @@ npm run build:dashboard
|
|
|
252
284
|
| `getUserId` | `(req) => string \| null` | `undefined` | Custom resolver for `userId` |
|
|
253
285
|
| `getSessionId` | `(req) => string \| null` | `undefined` | Custom resolver for `sessionId` |
|
|
254
286
|
| `slowRouteThresholdMs` | `number` | `1000` | Marks requests as slow when duration meets or exceeds this value |
|
|
287
|
+
| `includeRoutes` | `Array<string \| RegExp \| Function>` | `[]` | If set, only matching routes or URLs are tracked |
|
|
255
288
|
| `ignoredRoutes` | `Array<string \| RegExp \| Function>` | `[]` | Routes or URLs to skip |
|
|
256
289
|
| `trackIp` | `boolean` | `false` | Persist `req.ip` when enabled |
|
|
257
290
|
| `trackUserAgent` | `boolean` | `true` | Persist the request `user-agent` header when enabled |
|
package/dist/index.cjs
CHANGED
|
@@ -147,7 +147,7 @@ function resolveTrackedRoute(req) {
|
|
|
147
147
|
}
|
|
148
148
|
return routePath || req.path || req.originalUrl || "/";
|
|
149
149
|
}
|
|
150
|
-
function
|
|
150
|
+
function matchesRouteMatcher(matcher, candidate) {
|
|
151
151
|
if (!candidate) {
|
|
152
152
|
return false;
|
|
153
153
|
}
|
|
@@ -158,18 +158,32 @@ function matchesIgnoredRoute(matcher, candidate) {
|
|
|
158
158
|
return Boolean(matcher(candidate));
|
|
159
159
|
}
|
|
160
160
|
if (typeof matcher === "string") {
|
|
161
|
+
if (matcher.endsWith("/*")) {
|
|
162
|
+
const prefix = matcher.slice(0, -2);
|
|
163
|
+
if (!prefix || prefix === "/") {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
return candidate === prefix || candidate.startsWith(`${prefix}/`) || candidate.startsWith(`${prefix}?`);
|
|
167
|
+
}
|
|
161
168
|
return candidate === matcher || candidate.startsWith(`${matcher}?`);
|
|
162
169
|
}
|
|
163
170
|
return false;
|
|
164
171
|
}
|
|
165
|
-
function
|
|
172
|
+
function matchesRouteList(matchers, route, originalUrl) {
|
|
173
|
+
return matchers.some(
|
|
174
|
+
(matcher) => matchesRouteMatcher(matcher, route) || matchesRouteMatcher(matcher, originalUrl)
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
function shouldTrackRoute({
|
|
166
178
|
route,
|
|
167
179
|
originalUrl,
|
|
180
|
+
includeRoutes = [],
|
|
168
181
|
ignoredRoutes = []
|
|
169
182
|
}) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
183
|
+
if (includeRoutes.length > 0 && !matchesRouteList(includeRoutes, route, originalUrl)) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
return !matchesRouteList(ignoredRoutes, route, originalUrl);
|
|
173
187
|
}
|
|
174
188
|
|
|
175
189
|
// src/middleware.js
|
|
@@ -223,6 +237,7 @@ function createTrackingMiddleware({
|
|
|
223
237
|
getUserId,
|
|
224
238
|
getSessionId,
|
|
225
239
|
slowRouteThresholdMs = 1e3,
|
|
240
|
+
includeRoutes = [],
|
|
226
241
|
ignoredRoutes = [],
|
|
227
242
|
trackIp = false,
|
|
228
243
|
trackUserAgent = true,
|
|
@@ -235,7 +250,12 @@ function createTrackingMiddleware({
|
|
|
235
250
|
const endedAt = /* @__PURE__ */ new Date();
|
|
236
251
|
const route = resolveTrackedRoute(req);
|
|
237
252
|
const originalUrl = req.originalUrl || route;
|
|
238
|
-
if (
|
|
253
|
+
if (!shouldTrackRoute({
|
|
254
|
+
route,
|
|
255
|
+
originalUrl,
|
|
256
|
+
includeRoutes,
|
|
257
|
+
ignoredRoutes
|
|
258
|
+
})) {
|
|
239
259
|
return;
|
|
240
260
|
}
|
|
241
261
|
void safeAsync(
|
|
@@ -828,6 +848,7 @@ function createTrafficTracker(options = {}) {
|
|
|
828
848
|
getUserId,
|
|
829
849
|
getSessionId,
|
|
830
850
|
slowRouteThresholdMs = 1e3,
|
|
851
|
+
includeRoutes = [],
|
|
831
852
|
ignoredRoutes = [],
|
|
832
853
|
trackIp = false,
|
|
833
854
|
trackUserAgent = true,
|
|
@@ -848,6 +869,7 @@ function createTrafficTracker(options = {}) {
|
|
|
848
869
|
getUserId,
|
|
849
870
|
getSessionId,
|
|
850
871
|
slowRouteThresholdMs,
|
|
872
|
+
includeRoutes,
|
|
851
873
|
ignoredRoutes,
|
|
852
874
|
trackIp,
|
|
853
875
|
trackUserAgent,
|
package/dist/index.js
CHANGED
|
@@ -112,7 +112,7 @@ function resolveTrackedRoute(req) {
|
|
|
112
112
|
}
|
|
113
113
|
return routePath || req.path || req.originalUrl || "/";
|
|
114
114
|
}
|
|
115
|
-
function
|
|
115
|
+
function matchesRouteMatcher(matcher, candidate) {
|
|
116
116
|
if (!candidate) {
|
|
117
117
|
return false;
|
|
118
118
|
}
|
|
@@ -123,18 +123,32 @@ function matchesIgnoredRoute(matcher, candidate) {
|
|
|
123
123
|
return Boolean(matcher(candidate));
|
|
124
124
|
}
|
|
125
125
|
if (typeof matcher === "string") {
|
|
126
|
+
if (matcher.endsWith("/*")) {
|
|
127
|
+
const prefix = matcher.slice(0, -2);
|
|
128
|
+
if (!prefix || prefix === "/") {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
return candidate === prefix || candidate.startsWith(`${prefix}/`) || candidate.startsWith(`${prefix}?`);
|
|
132
|
+
}
|
|
126
133
|
return candidate === matcher || candidate.startsWith(`${matcher}?`);
|
|
127
134
|
}
|
|
128
135
|
return false;
|
|
129
136
|
}
|
|
130
|
-
function
|
|
137
|
+
function matchesRouteList(matchers, route, originalUrl) {
|
|
138
|
+
return matchers.some(
|
|
139
|
+
(matcher) => matchesRouteMatcher(matcher, route) || matchesRouteMatcher(matcher, originalUrl)
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
function shouldTrackRoute({
|
|
131
143
|
route,
|
|
132
144
|
originalUrl,
|
|
145
|
+
includeRoutes = [],
|
|
133
146
|
ignoredRoutes = []
|
|
134
147
|
}) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
148
|
+
if (includeRoutes.length > 0 && !matchesRouteList(includeRoutes, route, originalUrl)) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
return !matchesRouteList(ignoredRoutes, route, originalUrl);
|
|
138
152
|
}
|
|
139
153
|
|
|
140
154
|
// src/middleware.js
|
|
@@ -188,6 +202,7 @@ function createTrackingMiddleware({
|
|
|
188
202
|
getUserId,
|
|
189
203
|
getSessionId,
|
|
190
204
|
slowRouteThresholdMs = 1e3,
|
|
205
|
+
includeRoutes = [],
|
|
191
206
|
ignoredRoutes = [],
|
|
192
207
|
trackIp = false,
|
|
193
208
|
trackUserAgent = true,
|
|
@@ -200,7 +215,12 @@ function createTrackingMiddleware({
|
|
|
200
215
|
const endedAt = /* @__PURE__ */ new Date();
|
|
201
216
|
const route = resolveTrackedRoute(req);
|
|
202
217
|
const originalUrl = req.originalUrl || route;
|
|
203
|
-
if (
|
|
218
|
+
if (!shouldTrackRoute({
|
|
219
|
+
route,
|
|
220
|
+
originalUrl,
|
|
221
|
+
includeRoutes,
|
|
222
|
+
ignoredRoutes
|
|
223
|
+
})) {
|
|
204
224
|
return;
|
|
205
225
|
}
|
|
206
226
|
void safeAsync(
|
|
@@ -793,6 +813,7 @@ function createTrafficTracker(options = {}) {
|
|
|
793
813
|
getUserId,
|
|
794
814
|
getSessionId,
|
|
795
815
|
slowRouteThresholdMs = 1e3,
|
|
816
|
+
includeRoutes = [],
|
|
796
817
|
ignoredRoutes = [],
|
|
797
818
|
trackIp = false,
|
|
798
819
|
trackUserAgent = true,
|
|
@@ -813,6 +834,7 @@ function createTrafficTracker(options = {}) {
|
|
|
813
834
|
getUserId,
|
|
814
835
|
getSessionId,
|
|
815
836
|
slowRouteThresholdMs,
|
|
837
|
+
includeRoutes,
|
|
816
838
|
ignoredRoutes,
|
|
817
839
|
trackIp,
|
|
818
840
|
trackUserAgent,
|
package/package.json
CHANGED