@mastra/koa 1.4.16 → 1.5.0-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 +14 -0
- package/dist/index.cjs +223 -182
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +223 -182
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @mastra/koa
|
|
2
2
|
|
|
3
|
+
## 1.5.0-alpha.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Improved the Koa adapter to make request routing more efficient as route counts grow. ([#16050](https://github.com/mastra-ai/mastra/pull/16050))
|
|
8
|
+
|
|
9
|
+
Requests now move through a leaner routing path with lower middleware overhead, which helps Koa-based Mastra servers stay faster and produce cleaner request traces without changing the public API.
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Updated dependencies [[`6dcd65f`](https://github.com/mastra-ai/mastra/commit/6dcd65f2a34069e6dc43ba35f1d11119b9b40bef), [`1c2dda8`](https://github.com/mastra-ai/mastra/commit/1c2dda805fbfccc0abf55d4cb20cc34402dc3f0c)]:
|
|
14
|
+
- @mastra/core@1.31.1-alpha.0
|
|
15
|
+
- @mastra/server@1.31.1-alpha.0
|
|
16
|
+
|
|
3
17
|
## 1.4.16
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -308,6 +308,7 @@ function toWebRequest2(ctx) {
|
|
|
308
308
|
});
|
|
309
309
|
}
|
|
310
310
|
var MastraServer = class extends serverAdapter.MastraServer {
|
|
311
|
+
activeRouteDispatchers = /* @__PURE__ */ new WeakMap();
|
|
311
312
|
async init() {
|
|
312
313
|
this.registerErrorMiddleware();
|
|
313
314
|
await super.init();
|
|
@@ -325,11 +326,12 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
325
326
|
* should use `server.onError` or register their own middleware between this and the routes.
|
|
326
327
|
*/
|
|
327
328
|
registerErrorMiddleware() {
|
|
328
|
-
|
|
329
|
+
const server = this;
|
|
330
|
+
this.app.use(async function mastraErrorBoundary(ctx, next) {
|
|
329
331
|
try {
|
|
330
332
|
await next();
|
|
331
333
|
} catch (err) {
|
|
332
|
-
if (await
|
|
334
|
+
if (await server.handleOnError(err, ctx)) {
|
|
333
335
|
return;
|
|
334
336
|
}
|
|
335
337
|
const error = err instanceof Error ? err : new Error(String(err));
|
|
@@ -393,7 +395,8 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
393
395
|
}
|
|
394
396
|
}
|
|
395
397
|
createContextMiddleware() {
|
|
396
|
-
|
|
398
|
+
const server = this;
|
|
399
|
+
return async function mastraRequestContext(ctx, next) {
|
|
397
400
|
let bodyRequestContext;
|
|
398
401
|
let paramsRequestContext;
|
|
399
402
|
if (ctx.method === "POST" || ctx.method === "PUT") {
|
|
@@ -423,14 +426,14 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
423
426
|
} catch {
|
|
424
427
|
}
|
|
425
428
|
}
|
|
426
|
-
const requestContext =
|
|
429
|
+
const requestContext = server.mergeRequestContext({ paramsRequestContext, bodyRequestContext });
|
|
427
430
|
ctx.state.requestContext = requestContext;
|
|
428
|
-
ctx.state.mastra =
|
|
429
|
-
ctx.state.tools =
|
|
430
|
-
if (
|
|
431
|
-
ctx.state.taskStore =
|
|
431
|
+
ctx.state.mastra = server.mastra;
|
|
432
|
+
ctx.state.tools = server.tools || {};
|
|
433
|
+
if (server.taskStore) {
|
|
434
|
+
ctx.state.taskStore = server.taskStore;
|
|
432
435
|
}
|
|
433
|
-
ctx.state.customRouteAuthConfig =
|
|
436
|
+
ctx.state.customRouteAuthConfig = server.customRouteAuthConfig;
|
|
434
437
|
const controller = new AbortController();
|
|
435
438
|
ctx.req.on("close", () => {
|
|
436
439
|
if (!ctx.res.writableEnded) {
|
|
@@ -441,6 +444,193 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
441
444
|
await next();
|
|
442
445
|
};
|
|
443
446
|
}
|
|
447
|
+
getRouteDispatcherGroup(app) {
|
|
448
|
+
const activeGroup = this.activeRouteDispatchers.get(app);
|
|
449
|
+
if (activeGroup && app.middleware.length === activeGroup.stackLengthAfterRegistration) {
|
|
450
|
+
return activeGroup;
|
|
451
|
+
}
|
|
452
|
+
const group = {
|
|
453
|
+
routes: [],
|
|
454
|
+
stackLengthAfterRegistration: 0
|
|
455
|
+
};
|
|
456
|
+
app.use(this.createRouteDispatcherMiddleware(group));
|
|
457
|
+
group.stackLengthAfterRegistration = app.middleware.length;
|
|
458
|
+
this.activeRouteDispatchers.set(app, group);
|
|
459
|
+
return group;
|
|
460
|
+
}
|
|
461
|
+
createRouteDispatcherMiddleware(group) {
|
|
462
|
+
const server = this;
|
|
463
|
+
return async function mastraRouteDispatcher(ctx, next) {
|
|
464
|
+
const matchedRoute = server.findRegisteredRoute(group.routes, ctx);
|
|
465
|
+
if (!matchedRoute) {
|
|
466
|
+
await next();
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
await server.handleMatchedRoute(matchedRoute, ctx);
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
findRegisteredRoute(routes, ctx) {
|
|
473
|
+
const method = ctx.method.toUpperCase();
|
|
474
|
+
for (const registeredRoute of routes) {
|
|
475
|
+
if (registeredRoute.route.method.toUpperCase() !== "ALL" && method !== registeredRoute.route.method.toUpperCase()) {
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
const match = registeredRoute.pathRegex.exec(ctx.path);
|
|
479
|
+
if (!match) {
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
ctx.params = {};
|
|
483
|
+
registeredRoute.paramNames.forEach((name, index) => {
|
|
484
|
+
ctx.params[name] = match[index + 1];
|
|
485
|
+
});
|
|
486
|
+
return registeredRoute;
|
|
487
|
+
}
|
|
488
|
+
return void 0;
|
|
489
|
+
}
|
|
490
|
+
async handleMatchedRoute(registeredRoute, ctx) {
|
|
491
|
+
const { route, prefix } = registeredRoute;
|
|
492
|
+
const authError = await this.checkRouteAuth(route, {
|
|
493
|
+
path: String(ctx.path || "/"),
|
|
494
|
+
method: String(ctx.method || "GET"),
|
|
495
|
+
getHeader: (name) => ctx.headers[name.toLowerCase()],
|
|
496
|
+
getQuery: (name) => ctx.query[name],
|
|
497
|
+
requestContext: ctx.state.requestContext,
|
|
498
|
+
request: toWebRequest2(ctx),
|
|
499
|
+
buildAuthorizeContext: () => toWebRequest2(ctx)
|
|
500
|
+
});
|
|
501
|
+
if (authError) {
|
|
502
|
+
if (authError.headers) {
|
|
503
|
+
for (const [key, value] of Object.entries(authError.headers)) {
|
|
504
|
+
ctx.set(key, value);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
if (authError.error) {
|
|
508
|
+
ctx.status = authError.status;
|
|
509
|
+
ctx.body = { error: authError.error };
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
const params = await this.getParams(route, ctx);
|
|
514
|
+
if (params.bodyParseError) {
|
|
515
|
+
ctx.status = 400;
|
|
516
|
+
ctx.body = {
|
|
517
|
+
error: "Invalid request body",
|
|
518
|
+
issues: [{ field: "body", message: params.bodyParseError.message }]
|
|
519
|
+
};
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
if (params.queryParams) {
|
|
523
|
+
try {
|
|
524
|
+
params.queryParams = await this.parseQueryParams(route, params.queryParams);
|
|
525
|
+
} catch (error) {
|
|
526
|
+
this.mastra.getLogger()?.error("Error parsing query params", {
|
|
527
|
+
error: error instanceof Error ? { message: error.message, stack: error.stack } : error
|
|
528
|
+
});
|
|
529
|
+
if (error instanceof ZodError) {
|
|
530
|
+
const resolved = this.resolveValidationError(route, error, "query");
|
|
531
|
+
ctx.status = resolved.status;
|
|
532
|
+
ctx.body = resolved.body;
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
ctx.status = 400;
|
|
536
|
+
ctx.body = {
|
|
537
|
+
error: "Invalid query parameters",
|
|
538
|
+
issues: [{ field: "unknown", message: error instanceof Error ? error.message : "Unknown error" }]
|
|
539
|
+
};
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
if (params.body) {
|
|
544
|
+
try {
|
|
545
|
+
params.body = await this.parseBody(route, params.body);
|
|
546
|
+
} catch (error) {
|
|
547
|
+
this.mastra.getLogger()?.error("Error parsing body", {
|
|
548
|
+
error: error instanceof Error ? { message: error.message, stack: error.stack } : error
|
|
549
|
+
});
|
|
550
|
+
if (error instanceof ZodError) {
|
|
551
|
+
const resolved = this.resolveValidationError(route, error, "body");
|
|
552
|
+
ctx.status = resolved.status;
|
|
553
|
+
ctx.body = resolved.body;
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
ctx.status = 400;
|
|
557
|
+
ctx.body = {
|
|
558
|
+
error: "Invalid request body",
|
|
559
|
+
issues: [{ field: "unknown", message: error instanceof Error ? error.message : "Unknown error" }]
|
|
560
|
+
};
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
if (params.urlParams) {
|
|
565
|
+
try {
|
|
566
|
+
params.urlParams = await this.parsePathParams(route, params.urlParams);
|
|
567
|
+
} catch (error) {
|
|
568
|
+
this.mastra.getLogger()?.error("Error parsing path params", {
|
|
569
|
+
error: error instanceof Error ? { message: error.message, stack: error.stack } : error
|
|
570
|
+
});
|
|
571
|
+
if (error instanceof ZodError) {
|
|
572
|
+
const resolved = this.resolveValidationError(route, error, "path");
|
|
573
|
+
ctx.status = resolved.status;
|
|
574
|
+
ctx.body = resolved.body;
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
ctx.status = 400;
|
|
578
|
+
ctx.body = {
|
|
579
|
+
error: "Invalid path parameters",
|
|
580
|
+
issues: [{ field: "unknown", message: error instanceof Error ? error.message : "Unknown error" }]
|
|
581
|
+
};
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
const handlerParams = {
|
|
586
|
+
...params.urlParams,
|
|
587
|
+
...params.queryParams,
|
|
588
|
+
...typeof params.body === "object" ? params.body : {},
|
|
589
|
+
requestContext: ctx.state.requestContext,
|
|
590
|
+
mastra: this.mastra,
|
|
591
|
+
tools: ctx.state.tools,
|
|
592
|
+
taskStore: ctx.state.taskStore,
|
|
593
|
+
abortSignal: ctx.state.abortSignal,
|
|
594
|
+
routePrefix: prefix
|
|
595
|
+
};
|
|
596
|
+
const authConfig = this.mastra.getServer()?.auth;
|
|
597
|
+
if (authConfig) {
|
|
598
|
+
const hasPermission = await loadHasPermission();
|
|
599
|
+
if (hasPermission) {
|
|
600
|
+
const userPermissions = ctx.state.requestContext.get("userPermissions");
|
|
601
|
+
const permissionError = this.checkRoutePermission(route, userPermissions, hasPermission);
|
|
602
|
+
if (permissionError) {
|
|
603
|
+
ctx.status = permissionError.status;
|
|
604
|
+
ctx.body = {
|
|
605
|
+
error: permissionError.error,
|
|
606
|
+
message: permissionError.message
|
|
607
|
+
};
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
try {
|
|
613
|
+
const result = await route.handler(handlerParams);
|
|
614
|
+
await this.sendResponse(route, ctx, result, prefix);
|
|
615
|
+
} catch (error) {
|
|
616
|
+
this.mastra.getLogger()?.error("Error calling handler", {
|
|
617
|
+
error: error instanceof Error ? { message: error.message, stack: error.stack } : error,
|
|
618
|
+
path: route.path,
|
|
619
|
+
method: route.method
|
|
620
|
+
});
|
|
621
|
+
if (error && typeof error === "object") {
|
|
622
|
+
if (!("status" in error)) {
|
|
623
|
+
if ("details" in error && error.details && typeof error.details === "object" && "status" in error.details) {
|
|
624
|
+
error.status = error.details.status;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
if (await this.handleOnError(error, ctx)) {
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
throw error;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
444
634
|
async stream(route, ctx, result) {
|
|
445
635
|
ctx.respond = false;
|
|
446
636
|
const streamFormat = route.streamFormat || "stream";
|
|
@@ -666,165 +856,14 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
666
856
|
const prefix = prefixParam ?? this.prefix ?? "";
|
|
667
857
|
const fullPath = `${prefix}${route.path}`;
|
|
668
858
|
const koaPath = fullPath;
|
|
669
|
-
const
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
await next();
|
|
678
|
-
return;
|
|
679
|
-
}
|
|
680
|
-
const paramNames = this.extractParamNames(koaPath);
|
|
681
|
-
ctx.params = {};
|
|
682
|
-
paramNames.forEach((name, index) => {
|
|
683
|
-
ctx.params[name] = match[index + 1];
|
|
684
|
-
});
|
|
685
|
-
const authError = await this.checkRouteAuth(route, {
|
|
686
|
-
path: String(ctx.path || "/"),
|
|
687
|
-
method: String(ctx.method || "GET"),
|
|
688
|
-
getHeader: (name) => ctx.headers[name.toLowerCase()],
|
|
689
|
-
getQuery: (name) => ctx.query[name],
|
|
690
|
-
requestContext: ctx.state.requestContext,
|
|
691
|
-
request: toWebRequest2(ctx),
|
|
692
|
-
buildAuthorizeContext: () => toWebRequest2(ctx)
|
|
693
|
-
});
|
|
694
|
-
if (authError) {
|
|
695
|
-
if (authError.headers) {
|
|
696
|
-
for (const [key, value] of Object.entries(authError.headers)) {
|
|
697
|
-
ctx.set(key, value);
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
if (authError.error) {
|
|
701
|
-
ctx.status = authError.status;
|
|
702
|
-
ctx.body = { error: authError.error };
|
|
703
|
-
return;
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
const params = await this.getParams(route, ctx);
|
|
707
|
-
if (params.bodyParseError) {
|
|
708
|
-
ctx.status = 400;
|
|
709
|
-
ctx.body = {
|
|
710
|
-
error: "Invalid request body",
|
|
711
|
-
issues: [{ field: "body", message: params.bodyParseError.message }]
|
|
712
|
-
};
|
|
713
|
-
return;
|
|
714
|
-
}
|
|
715
|
-
if (params.queryParams) {
|
|
716
|
-
try {
|
|
717
|
-
params.queryParams = await this.parseQueryParams(route, params.queryParams);
|
|
718
|
-
} catch (error) {
|
|
719
|
-
this.mastra.getLogger()?.error("Error parsing query params", {
|
|
720
|
-
error: error instanceof Error ? { message: error.message, stack: error.stack } : error
|
|
721
|
-
});
|
|
722
|
-
if (error instanceof ZodError) {
|
|
723
|
-
const resolved = this.resolveValidationError(route, error, "query");
|
|
724
|
-
ctx.status = resolved.status;
|
|
725
|
-
ctx.body = resolved.body;
|
|
726
|
-
return;
|
|
727
|
-
}
|
|
728
|
-
ctx.status = 400;
|
|
729
|
-
ctx.body = {
|
|
730
|
-
error: "Invalid query parameters",
|
|
731
|
-
issues: [{ field: "unknown", message: error instanceof Error ? error.message : "Unknown error" }]
|
|
732
|
-
};
|
|
733
|
-
return;
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
if (params.body) {
|
|
737
|
-
try {
|
|
738
|
-
params.body = await this.parseBody(route, params.body);
|
|
739
|
-
} catch (error) {
|
|
740
|
-
this.mastra.getLogger()?.error("Error parsing body", {
|
|
741
|
-
error: error instanceof Error ? { message: error.message, stack: error.stack } : error
|
|
742
|
-
});
|
|
743
|
-
if (error instanceof ZodError) {
|
|
744
|
-
const resolved = this.resolveValidationError(route, error, "body");
|
|
745
|
-
ctx.status = resolved.status;
|
|
746
|
-
ctx.body = resolved.body;
|
|
747
|
-
return;
|
|
748
|
-
}
|
|
749
|
-
ctx.status = 400;
|
|
750
|
-
ctx.body = {
|
|
751
|
-
error: "Invalid request body",
|
|
752
|
-
issues: [{ field: "unknown", message: error instanceof Error ? error.message : "Unknown error" }]
|
|
753
|
-
};
|
|
754
|
-
return;
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
if (params.urlParams) {
|
|
758
|
-
try {
|
|
759
|
-
params.urlParams = await this.parsePathParams(route, params.urlParams);
|
|
760
|
-
} catch (error) {
|
|
761
|
-
this.mastra.getLogger()?.error("Error parsing path params", {
|
|
762
|
-
error: error instanceof Error ? { message: error.message, stack: error.stack } : error
|
|
763
|
-
});
|
|
764
|
-
if (error instanceof ZodError) {
|
|
765
|
-
const resolved = this.resolveValidationError(route, error, "path");
|
|
766
|
-
ctx.status = resolved.status;
|
|
767
|
-
ctx.body = resolved.body;
|
|
768
|
-
return;
|
|
769
|
-
}
|
|
770
|
-
ctx.status = 400;
|
|
771
|
-
ctx.body = {
|
|
772
|
-
error: "Invalid path parameters",
|
|
773
|
-
issues: [{ field: "unknown", message: error instanceof Error ? error.message : "Unknown error" }]
|
|
774
|
-
};
|
|
775
|
-
return;
|
|
776
|
-
}
|
|
777
|
-
}
|
|
778
|
-
const handlerParams = {
|
|
779
|
-
...params.urlParams,
|
|
780
|
-
...params.queryParams,
|
|
781
|
-
...typeof params.body === "object" ? params.body : {},
|
|
782
|
-
requestContext: ctx.state.requestContext,
|
|
783
|
-
mastra: this.mastra,
|
|
784
|
-
tools: ctx.state.tools,
|
|
785
|
-
taskStore: ctx.state.taskStore,
|
|
786
|
-
abortSignal: ctx.state.abortSignal,
|
|
787
|
-
routePrefix: prefix
|
|
788
|
-
};
|
|
789
|
-
const authConfig = this.mastra.getServer()?.auth;
|
|
790
|
-
if (authConfig) {
|
|
791
|
-
const hasPermission = await loadHasPermission();
|
|
792
|
-
if (hasPermission) {
|
|
793
|
-
const userPermissions = ctx.state.requestContext.get("userPermissions");
|
|
794
|
-
const permissionError = this.checkRoutePermission(route, userPermissions, hasPermission);
|
|
795
|
-
if (permissionError) {
|
|
796
|
-
ctx.status = permissionError.status;
|
|
797
|
-
ctx.body = {
|
|
798
|
-
error: permissionError.error,
|
|
799
|
-
message: permissionError.message
|
|
800
|
-
};
|
|
801
|
-
return;
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
try {
|
|
806
|
-
const result = await route.handler(handlerParams);
|
|
807
|
-
await this.sendResponse(route, ctx, result, prefix);
|
|
808
|
-
} catch (error) {
|
|
809
|
-
this.mastra.getLogger()?.error("Error calling handler", {
|
|
810
|
-
error: error instanceof Error ? { message: error.message, stack: error.stack } : error,
|
|
811
|
-
path: route.path,
|
|
812
|
-
method: route.method
|
|
813
|
-
});
|
|
814
|
-
if (error && typeof error === "object") {
|
|
815
|
-
if (!("status" in error)) {
|
|
816
|
-
if ("details" in error && error.details && typeof error.details === "object" && "status" in error.details) {
|
|
817
|
-
error.status = error.details.status;
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
}
|
|
821
|
-
if (await this.handleOnError(error, ctx)) {
|
|
822
|
-
return;
|
|
823
|
-
}
|
|
824
|
-
throw error;
|
|
825
|
-
}
|
|
826
|
-
};
|
|
827
|
-
app.use(handler);
|
|
859
|
+
const group = this.getRouteDispatcherGroup(app);
|
|
860
|
+
group.routes.push({
|
|
861
|
+
route,
|
|
862
|
+
prefix,
|
|
863
|
+
koaPath,
|
|
864
|
+
pathRegex: this.pathToRegex(koaPath),
|
|
865
|
+
paramNames: this.extractParamNames(koaPath)
|
|
866
|
+
});
|
|
828
867
|
}
|
|
829
868
|
/**
|
|
830
869
|
* Convert Express-style path to regex for matching
|
|
@@ -845,10 +884,11 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
845
884
|
}
|
|
846
885
|
async registerCustomApiRoutes() {
|
|
847
886
|
if (!await this.buildCustomRouteHandler()) return;
|
|
848
|
-
|
|
887
|
+
const server = this;
|
|
888
|
+
this.app.use(async function mastraCustomRouteDispatcher(ctx, next) {
|
|
849
889
|
const path = String(ctx.path || "/");
|
|
850
890
|
const method = String(ctx.method || "GET");
|
|
851
|
-
if (auth.isProtectedCustomRoute(path, method,
|
|
891
|
+
if (auth.isProtectedCustomRoute(path, method, server.customRouteAuthConfig)) {
|
|
852
892
|
const serverRoute = {
|
|
853
893
|
method,
|
|
854
894
|
path,
|
|
@@ -856,7 +896,7 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
856
896
|
handler: async () => {
|
|
857
897
|
}
|
|
858
898
|
};
|
|
859
|
-
const authError = await
|
|
899
|
+
const authError = await server.checkRouteAuth(serverRoute, {
|
|
860
900
|
path,
|
|
861
901
|
method,
|
|
862
902
|
getHeader: (name) => ctx.headers[name.toLowerCase()],
|
|
@@ -877,7 +917,7 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
877
917
|
return;
|
|
878
918
|
}
|
|
879
919
|
}
|
|
880
|
-
const authConfig =
|
|
920
|
+
const authConfig = server.mastra.getServer()?.auth;
|
|
881
921
|
if (authConfig) {
|
|
882
922
|
let hasPermission;
|
|
883
923
|
try {
|
|
@@ -889,7 +929,7 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
889
929
|
}
|
|
890
930
|
if (hasPermission) {
|
|
891
931
|
const userPermissions = ctx.state.requestContext.get("userPermissions");
|
|
892
|
-
const permissionError =
|
|
932
|
+
const permissionError = server.checkRoutePermission(serverRoute, userPermissions, hasPermission);
|
|
893
933
|
if (permissionError) {
|
|
894
934
|
ctx.status = permissionError.status;
|
|
895
935
|
ctx.body = {
|
|
@@ -901,7 +941,7 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
901
941
|
}
|
|
902
942
|
}
|
|
903
943
|
}
|
|
904
|
-
const response = await
|
|
944
|
+
const response = await server.handleCustomRouteRequest(
|
|
905
945
|
`${ctx.protocol}://${ctx.host}${ctx.originalUrl || ctx.url}`,
|
|
906
946
|
ctx.method,
|
|
907
947
|
ctx.headers,
|
|
@@ -910,7 +950,7 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
910
950
|
);
|
|
911
951
|
if (!response) return next();
|
|
912
952
|
ctx.respond = false;
|
|
913
|
-
await
|
|
953
|
+
await server.writeCustomRouteResponse(response, ctx.res);
|
|
914
954
|
});
|
|
915
955
|
}
|
|
916
956
|
registerContextMiddleware() {
|
|
@@ -922,8 +962,9 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
922
962
|
if (!this.httpLoggingConfig?.enabled) {
|
|
923
963
|
return;
|
|
924
964
|
}
|
|
925
|
-
|
|
926
|
-
|
|
965
|
+
const server = this;
|
|
966
|
+
this.app.use(async function mastraHttpLogger(ctx, next) {
|
|
967
|
+
if (!server.shouldLogRequest(ctx.path)) {
|
|
927
968
|
return next();
|
|
928
969
|
}
|
|
929
970
|
const start = Date.now();
|
|
@@ -932,19 +973,19 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
932
973
|
await next();
|
|
933
974
|
const duration2 = Date.now() - start;
|
|
934
975
|
const status = ctx.status;
|
|
935
|
-
const level =
|
|
976
|
+
const level = server.httpLoggingConfig?.level || "info";
|
|
936
977
|
const logData = {
|
|
937
978
|
method,
|
|
938
979
|
path,
|
|
939
980
|
status,
|
|
940
981
|
duration: `${duration2}ms`
|
|
941
982
|
};
|
|
942
|
-
if (
|
|
983
|
+
if (server.httpLoggingConfig?.includeQueryParams) {
|
|
943
984
|
logData.query = ctx.query;
|
|
944
985
|
}
|
|
945
|
-
if (
|
|
986
|
+
if (server.httpLoggingConfig?.includeHeaders) {
|
|
946
987
|
const headers = { ...ctx.headers };
|
|
947
|
-
const redactHeaders =
|
|
988
|
+
const redactHeaders = server.httpLoggingConfig.redactHeaders || [];
|
|
948
989
|
redactHeaders.forEach((h) => {
|
|
949
990
|
const key = h.toLowerCase();
|
|
950
991
|
if (headers[key] !== void 0) {
|
|
@@ -953,7 +994,7 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
953
994
|
});
|
|
954
995
|
logData.headers = headers;
|
|
955
996
|
}
|
|
956
|
-
|
|
997
|
+
server.logger[level](`${method} ${path} ${status} ${duration2}ms`, logData);
|
|
957
998
|
});
|
|
958
999
|
}
|
|
959
1000
|
};
|