@mastra/hono 1.1.7 → 1.1.8-alpha.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/CHANGELOG.md +39 -0
- package/LICENSE.md +15 -0
- package/dist/index.cjs +110 -119
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +110 -119
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
- package/dist/auth-middleware.d.ts +0 -11
- package/dist/auth-middleware.d.ts.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
1
|
# @mastra/hono
|
|
2
2
|
|
|
3
|
+
## 1.1.8-alpha.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Added RBAC permission enforcement to all server adapters. When an auth provider is configured, each route's required permission is checked against the authenticated user's permissions before the handler runs. Permissions are derived automatically from route paths and HTTP methods using the convention-based system from `@mastra/server`. ([#13163](https://github.com/mastra-ai/mastra/pull/13163))
|
|
8
|
+
|
|
9
|
+
- Added HTTP request logging middleware. Enable with `apiReqLogs: true` for default settings, or pass a configuration object for fine-grained control. ([#11907](https://github.com/mastra-ai/mastra/pull/11907))
|
|
10
|
+
|
|
11
|
+
**Simple activation**
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
const mastra = new Mastra({
|
|
15
|
+
server: { build: { apiReqLogs: true } },
|
|
16
|
+
});
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Advanced configuration**
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
const mastra = new Mastra({
|
|
23
|
+
server: {
|
|
24
|
+
build: {
|
|
25
|
+
apiReqLogs: {
|
|
26
|
+
enabled: true,
|
|
27
|
+
level: 'debug',
|
|
28
|
+
excludePaths: ['/health'],
|
|
29
|
+
includeHeaders: true,
|
|
30
|
+
includeQueryParams: true,
|
|
31
|
+
redactHeaders: ['authorization', 'cookie'],
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
- Updated dependencies [[`504fc8b`](https://github.com/mastra-ai/mastra/commit/504fc8b9d0ddab717577ad3bf9c95ea4bd5377bd), [`f9c150b`](https://github.com/mastra-ai/mastra/commit/f9c150b7595ad05ad9cc9a11098e2944361e8c22), [`88de7e8`](https://github.com/mastra-ai/mastra/commit/88de7e8dfe4b7e1951a9e441bb33136e705ce24e), [`6dbeeb9`](https://github.com/mastra-ai/mastra/commit/6dbeeb94a8b1eebb727300d1a98961f882180794), [`edee4b3`](https://github.com/mastra-ai/mastra/commit/edee4b37dff0af515fc7cc0e8d71ee39e6a762f0), [`3790c75`](https://github.com/mastra-ai/mastra/commit/3790c7578cc6a47d854eb12d89e6b1912867fe29), [`e7a235b`](https://github.com/mastra-ai/mastra/commit/e7a235be6472e0c870ed6c791ddb17c492dc188b), [`d51d298`](https://github.com/mastra-ai/mastra/commit/d51d298953967aab1f58ec965b644d109214f085), [`6dbeeb9`](https://github.com/mastra-ai/mastra/commit/6dbeeb94a8b1eebb727300d1a98961f882180794), [`d5f0d8d`](https://github.com/mastra-ai/mastra/commit/d5f0d8d6a03e515ddaa9b5da19b7e44b8357b07b), [`09c3b18`](https://github.com/mastra-ai/mastra/commit/09c3b1802ff14e243a8a8baea327440bc8cc2e32), [`b896379`](https://github.com/mastra-ai/mastra/commit/b8963791c6afa79484645fcec596a201f936b9a2), [`85c84eb`](https://github.com/mastra-ai/mastra/commit/85c84ebb78aebfcba9d209c8e152b16d7a00cb71), [`a89272a`](https://github.com/mastra-ai/mastra/commit/a89272a5d71939b9fcd284e6a6dc1dd091a6bdcf), [`ee9c8df`](https://github.com/mastra-ai/mastra/commit/ee9c8df644f19d055af5f496bf4942705f5a47b7), [`77b4a25`](https://github.com/mastra-ai/mastra/commit/77b4a254e51907f8ff3a3ba95596a18e93ae4b35), [`276246e`](https://github.com/mastra-ai/mastra/commit/276246e0b9066a1ea48bbc70df84dbe528daaf99), [`08ecfdb`](https://github.com/mastra-ai/mastra/commit/08ecfdbdad6fb8285deef86a034bdf4a6047cfca), [`d5f628c`](https://github.com/mastra-ai/mastra/commit/d5f628ca86c6f6f3ff1035d52f635df32dd81cab), [`524c0f3`](https://github.com/mastra-ai/mastra/commit/524c0f3c434c3d9d18f66338dcef383d6161b59c), [`c18a0e9`](https://github.com/mastra-ai/mastra/commit/c18a0e9cef1e4ca004b2963d35e4cfc031971eac), [`4bd21ea`](https://github.com/mastra-ai/mastra/commit/4bd21ea43d44d0a0427414fc047577f9f0aa3bec), [`115a7a4`](https://github.com/mastra-ai/mastra/commit/115a7a47db5e9896fec12ae6507501adb9ec89bf), [`22a48ae`](https://github.com/mastra-ai/mastra/commit/22a48ae2513eb54d8d79dad361fddbca97a155e8), [`3c6ef79`](https://github.com/mastra-ai/mastra/commit/3c6ef798481e00d6d22563be2de98818fd4dd5e0), [`9311c17`](https://github.com/mastra-ai/mastra/commit/9311c17d7a0640d9c4da2e71b814dc67c57c6369), [`7edf78f`](https://github.com/mastra-ai/mastra/commit/7edf78f80422c43e84585f08ba11df0d4d0b73c5), [`1c4221c`](https://github.com/mastra-ai/mastra/commit/1c4221cf6032ec98d0e094d4ee11da3e48490d96), [`6dbeeb9`](https://github.com/mastra-ai/mastra/commit/6dbeeb94a8b1eebb727300d1a98961f882180794), [`d25b9ea`](https://github.com/mastra-ai/mastra/commit/d25b9eabd400167255a97b690ffbc4ee4097ded5), [`fe1ce5c`](https://github.com/mastra-ai/mastra/commit/fe1ce5c9211c03d561606fda95cbfe7df1d9a9b5), [`b03c0e0`](https://github.com/mastra-ai/mastra/commit/b03c0e0389a799523929a458b0509c9e4244d562), [`0a8366b`](https://github.com/mastra-ai/mastra/commit/0a8366b0a692fcdde56c4d526e4cf03c502ae4ac), [`85664e9`](https://github.com/mastra-ai/mastra/commit/85664e9fd857320fbc245e301f764f45f66f32a3), [`bc79650`](https://github.com/mastra-ai/mastra/commit/bc796500c6e0334faa158a96077e3fb332274869), [`9257d01`](https://github.com/mastra-ai/mastra/commit/9257d01d1366d81f84c582fe02b5e200cf9621f4), [`3a3a59e`](https://github.com/mastra-ai/mastra/commit/3a3a59e8ffaa6a985fe3d9a126a3f5ade11a6724), [`3108d4e`](https://github.com/mastra-ai/mastra/commit/3108d4e649c9fddbf03253a6feeb388a5fa9fa5a), [`0c33b2c`](https://github.com/mastra-ai/mastra/commit/0c33b2c9db537f815e1c59e2c898ffce2e395a79), [`191e5bd`](https://github.com/mastra-ai/mastra/commit/191e5bd29b82f5bda35243945790da7bc7b695c2), [`f77cd94`](https://github.com/mastra-ai/mastra/commit/f77cd94c44eabed490384e7d19232a865e13214c), [`e8135c7`](https://github.com/mastra-ai/mastra/commit/e8135c7e300dac5040670eec7eab896ac6092e30), [`daca48f`](https://github.com/mastra-ai/mastra/commit/daca48f0fb17b7ae0b62a2ac40cf0e491b2fd0b7), [`bc79650`](https://github.com/mastra-ai/mastra/commit/bc796500c6e0334faa158a96077e3fb332274869), [`257d14f`](https://github.com/mastra-ai/mastra/commit/257d14faca5931f2e4186fc165b6f0b1f915deee), [`352f25d`](https://github.com/mastra-ai/mastra/commit/352f25da316b24cdd5b410fd8dddf6a8b763da2a), [`93477d0`](https://github.com/mastra-ai/mastra/commit/93477d0769b8a13ea5ed73d508d967fb23eaeed9), [`31c78b3`](https://github.com/mastra-ai/mastra/commit/31c78b3eb28f58a8017f1dcc795c33214d87feac), [`0bc0720`](https://github.com/mastra-ai/mastra/commit/0bc07201095791858087cc56f353fcd65e87ab54), [`36516ac`](https://github.com/mastra-ai/mastra/commit/36516aca1021cbeb42e74751b46a2614101f37c8), [`e947652`](https://github.com/mastra-ai/mastra/commit/e9476527fdecb4449e54570e80dfaf8466901254), [`3c6ef79`](https://github.com/mastra-ai/mastra/commit/3c6ef798481e00d6d22563be2de98818fd4dd5e0), [`9257d01`](https://github.com/mastra-ai/mastra/commit/9257d01d1366d81f84c582fe02b5e200cf9621f4), [`ec248f6`](https://github.com/mastra-ai/mastra/commit/ec248f6b56e8a037c066c49b2178e2507471d988)]:
|
|
39
|
+
- @mastra/core@1.9.0-alpha.0
|
|
40
|
+
- @mastra/server@1.9.0-alpha.0
|
|
41
|
+
|
|
3
42
|
## 1.1.7
|
|
4
43
|
|
|
5
44
|
### Patch Changes
|
package/LICENSE.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
Portions of this software are licensed as follows:
|
|
2
|
+
|
|
3
|
+
- All content that resides under any directory named "ee/" within this
|
|
4
|
+
repository, including but not limited to:
|
|
5
|
+
- `packages/core/src/auth/ee/`
|
|
6
|
+
- `packages/server/src/server/auth/ee/`
|
|
7
|
+
is licensed under the license defined in `ee/LICENSE`.
|
|
8
|
+
|
|
9
|
+
- All third-party components incorporated into the Mastra Software are
|
|
10
|
+
licensed under the original license provided by the owner of the
|
|
11
|
+
applicable component.
|
|
12
|
+
|
|
13
|
+
- Content outside of the above-mentioned directories or restrictions is
|
|
14
|
+
available under the "Apache License 2.0" as defined below.
|
|
15
|
+
|
|
1
16
|
# Apache License 2.0
|
|
2
17
|
|
|
3
18
|
Copyright (c) 2025 Kepler Software, Inc.
|
package/dist/index.cjs
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
var error = require('@mastra/server/handlers/error');
|
|
4
4
|
var serverAdapter = require('@mastra/server/server-adapter');
|
|
5
5
|
var fetchToNode = require('fetch-to-node');
|
|
6
|
-
var auth = require('@mastra/server/auth');
|
|
7
6
|
|
|
8
7
|
// src/index.ts
|
|
9
8
|
|
|
@@ -420,119 +419,20 @@ ZodError.create = (issues) => {
|
|
|
420
419
|
const error = new ZodError(issues);
|
|
421
420
|
return error;
|
|
422
421
|
};
|
|
423
|
-
var authenticationMiddleware = async (c, next) => {
|
|
424
|
-
const mastra = c.get("mastra");
|
|
425
|
-
const authConfig = mastra.getServer()?.auth;
|
|
426
|
-
const customRouteAuthConfig = c.get("customRouteAuthConfig");
|
|
427
|
-
if (!authConfig) {
|
|
428
|
-
return next();
|
|
429
|
-
}
|
|
430
|
-
const path = c.req.path;
|
|
431
|
-
const method = c.req.method;
|
|
432
|
-
const getHeader = (name) => c.req.header(name);
|
|
433
|
-
if (auth.isDevPlaygroundRequest(path, method, getHeader, authConfig, customRouteAuthConfig)) {
|
|
434
|
-
return next();
|
|
435
|
-
}
|
|
436
|
-
if (!auth.isProtectedPath(c.req.path, c.req.method, authConfig, customRouteAuthConfig)) {
|
|
437
|
-
return next();
|
|
438
|
-
}
|
|
439
|
-
if (auth.canAccessPublicly(c.req.path, c.req.method, authConfig)) {
|
|
440
|
-
return next();
|
|
441
|
-
}
|
|
442
|
-
const authHeader = c.req.header("Authorization");
|
|
443
|
-
let token = authHeader ? authHeader.replace("Bearer ", "") : null;
|
|
444
|
-
if (!token && c.req.query("apiKey")) {
|
|
445
|
-
token = c.req.query("apiKey") || null;
|
|
446
|
-
}
|
|
447
|
-
if (!token) {
|
|
448
|
-
return c.json({ error: "Authentication required" }, 401);
|
|
449
|
-
}
|
|
450
|
-
try {
|
|
451
|
-
let user;
|
|
452
|
-
if (typeof authConfig.authenticateToken === "function") {
|
|
453
|
-
user = await authConfig.authenticateToken(token, c.req);
|
|
454
|
-
} else {
|
|
455
|
-
throw new Error("No token verification method configured");
|
|
456
|
-
}
|
|
457
|
-
if (!user) {
|
|
458
|
-
return c.json({ error: "Invalid or expired token" }, 401);
|
|
459
|
-
}
|
|
460
|
-
c.get("requestContext").set("user", user);
|
|
461
|
-
return next();
|
|
462
|
-
} catch (err) {
|
|
463
|
-
mastra.getLogger()?.error("Authentication error", {
|
|
464
|
-
error: err instanceof Error ? { message: err.message, stack: err.stack } : err
|
|
465
|
-
});
|
|
466
|
-
return c.json({ error: "Invalid or expired token" }, 401);
|
|
467
|
-
}
|
|
468
|
-
};
|
|
469
|
-
var authorizationMiddleware = async (c, next) => {
|
|
470
|
-
const mastra = c.get("mastra");
|
|
471
|
-
const authConfig = mastra.getServer()?.auth;
|
|
472
|
-
const customRouteAuthConfig = c.get("customRouteAuthConfig");
|
|
473
|
-
if (!authConfig) {
|
|
474
|
-
return next();
|
|
475
|
-
}
|
|
476
|
-
const path = c.req.path;
|
|
477
|
-
const method = c.req.method;
|
|
478
|
-
const getHeader = (name) => c.req.header(name);
|
|
479
|
-
if (auth.isDevPlaygroundRequest(path, method, getHeader, authConfig, customRouteAuthConfig)) {
|
|
480
|
-
return next();
|
|
481
|
-
}
|
|
482
|
-
if (!auth.isProtectedPath(c.req.path, c.req.method, authConfig, customRouteAuthConfig)) {
|
|
483
|
-
return next();
|
|
484
|
-
}
|
|
485
|
-
if (auth.canAccessPublicly(path, method, authConfig)) {
|
|
486
|
-
return next();
|
|
487
|
-
}
|
|
488
|
-
const user = c.get("requestContext").get("user");
|
|
489
|
-
if ("authorizeUser" in authConfig && typeof authConfig.authorizeUser === "function") {
|
|
490
|
-
try {
|
|
491
|
-
const isAuthorized = await authConfig.authorizeUser(user, c.req);
|
|
492
|
-
if (isAuthorized) {
|
|
493
|
-
return next();
|
|
494
|
-
}
|
|
495
|
-
return c.json({ error: "Access denied" }, 403);
|
|
496
|
-
} catch (err) {
|
|
497
|
-
mastra.getLogger()?.error("Authorization error in authorizeUser", {
|
|
498
|
-
error: err instanceof Error ? { message: err.message, stack: err.stack } : err
|
|
499
|
-
});
|
|
500
|
-
return c.json({ error: "Authorization error" }, 500);
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
if ("authorize" in authConfig && typeof authConfig.authorize === "function") {
|
|
504
|
-
try {
|
|
505
|
-
const isAuthorized = await authConfig.authorize(path, method, user, c);
|
|
506
|
-
if (isAuthorized) {
|
|
507
|
-
return next();
|
|
508
|
-
}
|
|
509
|
-
return c.json({ error: "Access denied" }, 403);
|
|
510
|
-
} catch (err) {
|
|
511
|
-
mastra.getLogger()?.error("Authorization error in authorize", {
|
|
512
|
-
error: err instanceof Error ? { message: err.message, stack: err.stack } : err,
|
|
513
|
-
path,
|
|
514
|
-
method
|
|
515
|
-
});
|
|
516
|
-
return c.json({ error: "Authorization error" }, 500);
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
if ("rules" in authConfig && authConfig.rules && authConfig.rules.length > 0) {
|
|
520
|
-
const isAuthorized = await auth.checkRules(authConfig.rules, path, method, user);
|
|
521
|
-
if (isAuthorized) {
|
|
522
|
-
return next();
|
|
523
|
-
}
|
|
524
|
-
return c.json({ error: "Access denied" }, 403);
|
|
525
|
-
}
|
|
526
|
-
if (auth.defaultAuthConfig.rules && auth.defaultAuthConfig.rules.length > 0) {
|
|
527
|
-
const isAuthorized = await auth.checkRules(auth.defaultAuthConfig.rules, path, method, user);
|
|
528
|
-
if (isAuthorized) {
|
|
529
|
-
return next();
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
return c.json({ error: "Access denied" }, 403);
|
|
533
|
-
};
|
|
534
422
|
|
|
535
423
|
// src/index.ts
|
|
424
|
+
var _hasPermissionPromise;
|
|
425
|
+
function loadHasPermission() {
|
|
426
|
+
if (!_hasPermissionPromise) {
|
|
427
|
+
_hasPermissionPromise = import('@mastra/core/auth/ee').then((m) => m.hasPermission).catch(() => {
|
|
428
|
+
console.error(
|
|
429
|
+
"[@mastra/hono] Auth features require @mastra/core >= 1.6.0. Please upgrade: npm install @mastra/core@latest"
|
|
430
|
+
);
|
|
431
|
+
return void 0;
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
return _hasPermissionPromise;
|
|
435
|
+
}
|
|
536
436
|
var MastraServer = class extends serverAdapter.MastraServer {
|
|
537
437
|
createContextMiddleware() {
|
|
538
438
|
return async (c, next) => {
|
|
@@ -551,7 +451,8 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
551
451
|
let paramsRequestContext;
|
|
552
452
|
if (c.req.method === "POST" || c.req.method === "PUT") {
|
|
553
453
|
const contentType = c.req.header("content-type");
|
|
554
|
-
|
|
454
|
+
const contentLength = c.req.header("content-length");
|
|
455
|
+
if (contentType?.includes("application/json") && contentLength !== "0") {
|
|
555
456
|
try {
|
|
556
457
|
const body = await c.req.raw.clone().json();
|
|
557
458
|
if (body.requestContext) {
|
|
@@ -777,7 +678,9 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
777
678
|
method: c.req.method,
|
|
778
679
|
getHeader: (name) => c.req.header(name),
|
|
779
680
|
getQuery: (name) => c.req.query(name),
|
|
780
|
-
requestContext: c.get("requestContext")
|
|
681
|
+
requestContext: c.get("requestContext"),
|
|
682
|
+
request: c.req.raw,
|
|
683
|
+
buildAuthorizeContext: () => c
|
|
781
684
|
});
|
|
782
685
|
if (authError) {
|
|
783
686
|
return c.json({ error: authError.error }, authError.status);
|
|
@@ -858,8 +761,27 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
858
761
|
registeredTools: c.get("registeredTools"),
|
|
859
762
|
taskStore: c.get("taskStore"),
|
|
860
763
|
abortSignal: c.get("abortSignal"),
|
|
861
|
-
routePrefix: prefix
|
|
764
|
+
routePrefix: prefix,
|
|
765
|
+
request: c.req.raw
|
|
766
|
+
// Standard Request object with headers/cookies
|
|
862
767
|
};
|
|
768
|
+
const authConfig = this.mastra.getServer()?.auth;
|
|
769
|
+
if (authConfig) {
|
|
770
|
+
const hasPermission = await loadHasPermission();
|
|
771
|
+
if (hasPermission) {
|
|
772
|
+
const userPermissions = c.get("requestContext").get("userPermissions");
|
|
773
|
+
const permissionError = this.checkRoutePermission(route, userPermissions, hasPermission);
|
|
774
|
+
if (permissionError) {
|
|
775
|
+
return c.json(
|
|
776
|
+
{
|
|
777
|
+
error: permissionError.error,
|
|
778
|
+
message: permissionError.message
|
|
779
|
+
},
|
|
780
|
+
permissionError.status
|
|
781
|
+
);
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
}
|
|
863
785
|
try {
|
|
864
786
|
const result = await route.handler(handlerParams);
|
|
865
787
|
return this.sendResponse(route, c, result, prefix);
|
|
@@ -905,6 +827,43 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
905
827
|
const handler = "handler" in route && route.handler ? route.handler : "createHandler" in route ? await route.createHandler({ mastra: this.mastra }) : void 0;
|
|
906
828
|
if (!handler) continue;
|
|
907
829
|
const middlewares = [];
|
|
830
|
+
const serverRoute = {
|
|
831
|
+
method: route.method,
|
|
832
|
+
path: route.path,
|
|
833
|
+
responseType: "json",
|
|
834
|
+
handler: async () => {
|
|
835
|
+
},
|
|
836
|
+
requiresAuth: route.requiresAuth
|
|
837
|
+
};
|
|
838
|
+
middlewares.push(async (c, next) => {
|
|
839
|
+
const authError = await this.checkRouteAuth(serverRoute, {
|
|
840
|
+
path: c.req.path,
|
|
841
|
+
method: c.req.method,
|
|
842
|
+
getHeader: (name) => c.req.header(name),
|
|
843
|
+
getQuery: (name) => c.req.query(name),
|
|
844
|
+
requestContext: c.get("requestContext"),
|
|
845
|
+
request: c.req.raw,
|
|
846
|
+
buildAuthorizeContext: () => c
|
|
847
|
+
});
|
|
848
|
+
if (authError) {
|
|
849
|
+
return c.json({ error: authError.error }, authError.status);
|
|
850
|
+
}
|
|
851
|
+
const authConfig = this.mastra.getServer()?.auth;
|
|
852
|
+
if (authConfig) {
|
|
853
|
+
const hasPermission = await loadHasPermission();
|
|
854
|
+
if (hasPermission) {
|
|
855
|
+
const userPermissions = c.get("requestContext").get("userPermissions");
|
|
856
|
+
const permissionError = this.checkRoutePermission(serverRoute, userPermissions, hasPermission);
|
|
857
|
+
if (permissionError) {
|
|
858
|
+
return c.json(
|
|
859
|
+
{ error: permissionError.error, message: permissionError.message },
|
|
860
|
+
permissionError.status
|
|
861
|
+
);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
return next();
|
|
866
|
+
});
|
|
908
867
|
if (route.middleware) {
|
|
909
868
|
middlewares.push(...Array.isArray(route.middleware) ? route.middleware : [route.middleware]);
|
|
910
869
|
}
|
|
@@ -917,12 +876,44 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
917
876
|
this.app.use("*", this.createContextMiddleware());
|
|
918
877
|
}
|
|
919
878
|
registerAuthMiddleware() {
|
|
920
|
-
|
|
921
|
-
|
|
879
|
+
}
|
|
880
|
+
registerHttpLoggingMiddleware() {
|
|
881
|
+
if (!this.httpLoggingConfig?.enabled) {
|
|
922
882
|
return;
|
|
923
883
|
}
|
|
924
|
-
this.app.use("*",
|
|
925
|
-
|
|
884
|
+
this.app.use("*", async (c, next) => {
|
|
885
|
+
if (!this.shouldLogRequest(c.req.path)) {
|
|
886
|
+
return next();
|
|
887
|
+
}
|
|
888
|
+
const start = Date.now();
|
|
889
|
+
const method = c.req.method;
|
|
890
|
+
const path = c.req.path;
|
|
891
|
+
await next();
|
|
892
|
+
const duration = Date.now() - start;
|
|
893
|
+
const status = c.res.status;
|
|
894
|
+
const level = this.httpLoggingConfig?.level || "info";
|
|
895
|
+
const logData = {
|
|
896
|
+
method,
|
|
897
|
+
path,
|
|
898
|
+
status,
|
|
899
|
+
duration: `${duration}ms`
|
|
900
|
+
};
|
|
901
|
+
if (this.httpLoggingConfig?.includeQueryParams) {
|
|
902
|
+
logData.query = c.req.query();
|
|
903
|
+
}
|
|
904
|
+
if (this.httpLoggingConfig?.includeHeaders) {
|
|
905
|
+
const headers = Object.fromEntries(c.req.raw.headers.entries());
|
|
906
|
+
const redactHeaders = this.httpLoggingConfig.redactHeaders || [];
|
|
907
|
+
redactHeaders.forEach((h) => {
|
|
908
|
+
const key = h.toLowerCase();
|
|
909
|
+
if (headers[key] !== void 0) {
|
|
910
|
+
headers[key] = "[REDACTED]";
|
|
911
|
+
}
|
|
912
|
+
});
|
|
913
|
+
logData.headers = headers;
|
|
914
|
+
}
|
|
915
|
+
this.logger[level](`${method} ${path} ${status} ${duration}ms`, logData);
|
|
916
|
+
});
|
|
926
917
|
}
|
|
927
918
|
};
|
|
928
919
|
|