@mastra/koa 1.5.7-alpha.4 → 1.5.7-alpha.8
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 +51 -0
- package/dist/index.cjs +125 -144
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +125 -144
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
package/dist/index.js
CHANGED
|
@@ -4,6 +4,9 @@ import { MastraServer as MastraServer$1, checkRouteFGA, redactStreamChunk, norma
|
|
|
4
4
|
import { RequestContext } from '@mastra/core/request-context';
|
|
5
5
|
|
|
6
6
|
// src/index.ts
|
|
7
|
+
|
|
8
|
+
// ../../node_modules/.pnpm/zod@4.4.3/node_modules/zod/v4/core/core.js
|
|
9
|
+
var _a;
|
|
7
10
|
// @__NO_SIDE_EFFECTS__
|
|
8
11
|
function $constructor(name, initializer3, params) {
|
|
9
12
|
function init(inst, def) {
|
|
@@ -56,15 +59,16 @@ function $constructor(name, initializer3, params) {
|
|
|
56
59
|
Object.defineProperty(_, "name", { value: name });
|
|
57
60
|
return _;
|
|
58
61
|
}
|
|
62
|
+
(_a = globalThis).__zod_globalConfig ?? (_a.__zod_globalConfig = {});
|
|
59
63
|
|
|
60
|
-
// ../../node_modules/.pnpm/zod@4.3
|
|
64
|
+
// ../../node_modules/.pnpm/zod@4.4.3/node_modules/zod/v4/core/util.js
|
|
61
65
|
function jsonStringifyReplacer(_, value) {
|
|
62
66
|
if (typeof value === "bigint")
|
|
63
67
|
return value.toString();
|
|
64
68
|
return value;
|
|
65
69
|
}
|
|
66
70
|
|
|
67
|
-
// ../../node_modules/.pnpm/zod@4.3
|
|
71
|
+
// ../../node_modules/.pnpm/zod@4.4.3/node_modules/zod/v4/core/errors.js
|
|
68
72
|
var initializer = (inst, def) => {
|
|
69
73
|
inst.name = "$ZodError";
|
|
70
74
|
Object.defineProperty(inst, "_zod", {
|
|
@@ -82,7 +86,7 @@ var initializer = (inst, def) => {
|
|
|
82
86
|
});
|
|
83
87
|
};
|
|
84
88
|
var $ZodError = $constructor("$ZodError", initializer);
|
|
85
|
-
function flattenError(error, mapper = (
|
|
89
|
+
function flattenError(error, mapper = (issue) => issue.message) {
|
|
86
90
|
const fieldErrors = {};
|
|
87
91
|
const formErrors = [];
|
|
88
92
|
for (const sub of error.issues) {
|
|
@@ -95,32 +99,35 @@ function flattenError(error, mapper = (issue2) => issue2.message) {
|
|
|
95
99
|
}
|
|
96
100
|
return { formErrors, fieldErrors };
|
|
97
101
|
}
|
|
98
|
-
function formatError(error, mapper = (
|
|
102
|
+
function formatError(error, mapper = (issue) => issue.message) {
|
|
99
103
|
const fieldErrors = { _errors: [] };
|
|
100
|
-
const processError = (error2) => {
|
|
101
|
-
for (const
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
} else if (
|
|
105
|
-
processError({ issues:
|
|
106
|
-
} else if (
|
|
107
|
-
processError({ issues:
|
|
108
|
-
} else if (issue2.path.length === 0) {
|
|
109
|
-
fieldErrors._errors.push(mapper(issue2));
|
|
104
|
+
const processError = (error2, path = []) => {
|
|
105
|
+
for (const issue of error2.issues) {
|
|
106
|
+
if (issue.code === "invalid_union" && issue.errors.length) {
|
|
107
|
+
issue.errors.map((issues) => processError({ issues }, [...path, ...issue.path]));
|
|
108
|
+
} else if (issue.code === "invalid_key") {
|
|
109
|
+
processError({ issues: issue.issues }, [...path, ...issue.path]);
|
|
110
|
+
} else if (issue.code === "invalid_element") {
|
|
111
|
+
processError({ issues: issue.issues }, [...path, ...issue.path]);
|
|
110
112
|
} else {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
113
|
+
const fullpath = [...path, ...issue.path];
|
|
114
|
+
if (fullpath.length === 0) {
|
|
115
|
+
fieldErrors._errors.push(mapper(issue));
|
|
116
|
+
} else {
|
|
117
|
+
let curr = fieldErrors;
|
|
118
|
+
let i = 0;
|
|
119
|
+
while (i < fullpath.length) {
|
|
120
|
+
const el = fullpath[i];
|
|
121
|
+
const terminal = i === fullpath.length - 1;
|
|
122
|
+
if (!terminal) {
|
|
123
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
124
|
+
} else {
|
|
125
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
126
|
+
curr[el]._errors.push(mapper(issue));
|
|
127
|
+
}
|
|
128
|
+
curr = curr[el];
|
|
129
|
+
i++;
|
|
121
130
|
}
|
|
122
|
-
curr = curr[el];
|
|
123
|
-
i++;
|
|
124
131
|
}
|
|
125
132
|
}
|
|
126
133
|
}
|
|
@@ -129,54 +136,7 @@ function formatError(error, mapper = (issue2) => issue2.message) {
|
|
|
129
136
|
return fieldErrors;
|
|
130
137
|
}
|
|
131
138
|
|
|
132
|
-
// ../../node_modules/.pnpm/zod@4.3
|
|
133
|
-
var _a;
|
|
134
|
-
var $ZodRegistry = class {
|
|
135
|
-
constructor() {
|
|
136
|
-
this._map = /* @__PURE__ */ new WeakMap();
|
|
137
|
-
this._idmap = /* @__PURE__ */ new Map();
|
|
138
|
-
}
|
|
139
|
-
add(schema, ..._meta) {
|
|
140
|
-
const meta = _meta[0];
|
|
141
|
-
this._map.set(schema, meta);
|
|
142
|
-
if (meta && typeof meta === "object" && "id" in meta) {
|
|
143
|
-
this._idmap.set(meta.id, schema);
|
|
144
|
-
}
|
|
145
|
-
return this;
|
|
146
|
-
}
|
|
147
|
-
clear() {
|
|
148
|
-
this._map = /* @__PURE__ */ new WeakMap();
|
|
149
|
-
this._idmap = /* @__PURE__ */ new Map();
|
|
150
|
-
return this;
|
|
151
|
-
}
|
|
152
|
-
remove(schema) {
|
|
153
|
-
const meta = this._map.get(schema);
|
|
154
|
-
if (meta && typeof meta === "object" && "id" in meta) {
|
|
155
|
-
this._idmap.delete(meta.id);
|
|
156
|
-
}
|
|
157
|
-
this._map.delete(schema);
|
|
158
|
-
return this;
|
|
159
|
-
}
|
|
160
|
-
get(schema) {
|
|
161
|
-
const p = schema._zod.parent;
|
|
162
|
-
if (p) {
|
|
163
|
-
const pm = { ...this.get(p) ?? {} };
|
|
164
|
-
delete pm.id;
|
|
165
|
-
const f = { ...pm, ...this._map.get(schema) };
|
|
166
|
-
return Object.keys(f).length ? f : void 0;
|
|
167
|
-
}
|
|
168
|
-
return this._map.get(schema);
|
|
169
|
-
}
|
|
170
|
-
has(schema) {
|
|
171
|
-
return this._map.has(schema);
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
function registry() {
|
|
175
|
-
return new $ZodRegistry();
|
|
176
|
-
}
|
|
177
|
-
(_a = globalThis).__zod_globalRegistry ?? (_a.__zod_globalRegistry = registry());
|
|
178
|
-
|
|
179
|
-
// ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/classic/errors.js
|
|
139
|
+
// ../../node_modules/.pnpm/zod@4.4.3/node_modules/zod/v4/classic/errors.js
|
|
180
140
|
var initializer2 = (inst, issues) => {
|
|
181
141
|
$ZodError.init(inst, issues);
|
|
182
142
|
inst.name = "ZodError";
|
|
@@ -190,8 +150,8 @@ var initializer2 = (inst, issues) => {
|
|
|
190
150
|
// enumerable: false,
|
|
191
151
|
},
|
|
192
152
|
addIssue: {
|
|
193
|
-
value: (
|
|
194
|
-
inst.issues.push(
|
|
153
|
+
value: (issue) => {
|
|
154
|
+
inst.issues.push(issue);
|
|
195
155
|
inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
|
|
196
156
|
}
|
|
197
157
|
// enumerable: false,
|
|
@@ -211,7 +171,7 @@ var initializer2 = (inst, issues) => {
|
|
|
211
171
|
}
|
|
212
172
|
});
|
|
213
173
|
};
|
|
214
|
-
var ZodError = $constructor("ZodError", initializer2);
|
|
174
|
+
var ZodError = /* @__PURE__ */ $constructor("ZodError", initializer2);
|
|
215
175
|
function toWebRequest(ctx) {
|
|
216
176
|
const protocol = ctx.protocol || "http";
|
|
217
177
|
const host = ctx.host || "localhost";
|
|
@@ -674,6 +634,10 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
674
634
|
const { done, value } = await reader.read();
|
|
675
635
|
if (done) break;
|
|
676
636
|
if (value) {
|
|
637
|
+
if (streamFormat === "sse" && typeof value === "string" && value.startsWith(":")) {
|
|
638
|
+
ctx.res.write(value);
|
|
639
|
+
continue;
|
|
640
|
+
}
|
|
677
641
|
const shouldRedact = this.streamOptions?.redact ?? true;
|
|
678
642
|
const outputValue = shouldRedact ? redactStreamChunk(value) : value;
|
|
679
643
|
if (streamFormat === "sse") {
|
|
@@ -913,77 +877,94 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
913
877
|
);
|
|
914
878
|
const shouldRunCustomRouteAuth = isProtectedCustomRoute(path, method, server.customRouteAuthConfig);
|
|
915
879
|
const shouldRunCustomRouteFGA = !!matchedRoute?.route.fga;
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
method,
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
880
|
+
const customRouteAbortController = new AbortController();
|
|
881
|
+
const abortCustomRoute = () => {
|
|
882
|
+
customRouteAbortController.abort();
|
|
883
|
+
};
|
|
884
|
+
const abortCustomRouteIfOpen = () => {
|
|
885
|
+
if (!ctx.res.writableEnded) {
|
|
886
|
+
abortCustomRoute();
|
|
887
|
+
}
|
|
888
|
+
};
|
|
889
|
+
ctx.res.once("close", abortCustomRouteIfOpen);
|
|
890
|
+
ctx.res.once("error", abortCustomRouteIfOpen);
|
|
891
|
+
try {
|
|
892
|
+
if (shouldRunCustomRouteAuth || shouldRunCustomRouteFGA) {
|
|
893
|
+
const serverRoute = {
|
|
894
|
+
method: matchedRoute?.route.method ?? method,
|
|
895
|
+
path: matchedRoute?.route.path ?? path,
|
|
896
|
+
responseType: "json",
|
|
897
|
+
handler: async () => {
|
|
898
|
+
},
|
|
899
|
+
requiresAuth: matchedRoute?.route.requiresAuth,
|
|
900
|
+
requiresPermission: matchedRoute?.route.requiresPermission,
|
|
901
|
+
fga: matchedRoute?.route.fga
|
|
902
|
+
};
|
|
903
|
+
if (shouldRunCustomRouteAuth) {
|
|
904
|
+
const authError = await server.checkRouteAuth(serverRoute, {
|
|
905
|
+
path,
|
|
906
|
+
method,
|
|
907
|
+
getHeader: (name) => ctx.headers[name.toLowerCase()],
|
|
908
|
+
getQuery: (name) => ctx.query[name],
|
|
909
|
+
requestContext: ctx.state.requestContext,
|
|
910
|
+
request: toWebRequest2(ctx),
|
|
911
|
+
buildAuthorizeContext: () => toWebRequest2(ctx)
|
|
912
|
+
});
|
|
913
|
+
if (authError) {
|
|
914
|
+
if (authError.headers) {
|
|
915
|
+
for (const [key, value] of Object.entries(authError.headers)) {
|
|
916
|
+
ctx.set(key, value);
|
|
917
|
+
}
|
|
941
918
|
}
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
ctx.body = { error: authError.error };
|
|
946
|
-
return;
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
const authConfig = server.mastra.getServer()?.auth;
|
|
950
|
-
if (authConfig) {
|
|
951
|
-
const hasPermission = await loadHasPermission();
|
|
952
|
-
if (hasPermission) {
|
|
953
|
-
const userPermissions = ctx.state.requestContext.get("mastra__userPermissions");
|
|
954
|
-
const permissionError = server.checkRoutePermission(serverRoute, userPermissions, hasPermission);
|
|
955
|
-
if (permissionError) {
|
|
956
|
-
ctx.status = permissionError.status;
|
|
957
|
-
ctx.body = {
|
|
958
|
-
error: permissionError.error,
|
|
959
|
-
message: permissionError.message
|
|
960
|
-
};
|
|
919
|
+
if (authError.error) {
|
|
920
|
+
ctx.status = authError.status;
|
|
921
|
+
ctx.body = { error: authError.error };
|
|
961
922
|
return;
|
|
962
923
|
}
|
|
963
924
|
}
|
|
925
|
+
const authConfig = server.mastra.getServer()?.auth;
|
|
926
|
+
if (authConfig) {
|
|
927
|
+
const hasPermission = await loadHasPermission();
|
|
928
|
+
if (hasPermission) {
|
|
929
|
+
const userPermissions = ctx.state.requestContext.get("mastra__userPermissions");
|
|
930
|
+
const permissionError = server.checkRoutePermission(serverRoute, userPermissions, hasPermission);
|
|
931
|
+
if (permissionError) {
|
|
932
|
+
ctx.status = permissionError.status;
|
|
933
|
+
ctx.body = {
|
|
934
|
+
error: permissionError.error,
|
|
935
|
+
message: permissionError.message
|
|
936
|
+
};
|
|
937
|
+
return;
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
const fgaError = await checkRouteFGA(server.mastra, serverRoute, ctx.state.requestContext, {
|
|
943
|
+
...matchedRoute?.params ?? {},
|
|
944
|
+
...ctx.query,
|
|
945
|
+
...typeof ctx.request.body === "object" && ctx.request.body !== null ? ctx.request.body : {}
|
|
946
|
+
});
|
|
947
|
+
if (fgaError) {
|
|
948
|
+
ctx.status = fgaError.status;
|
|
949
|
+
ctx.body = { error: fgaError.error, message: fgaError.message };
|
|
950
|
+
return;
|
|
964
951
|
}
|
|
965
952
|
}
|
|
966
|
-
const
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
953
|
+
const response = await server.handleCustomRouteRequest(
|
|
954
|
+
`${ctx.protocol}://${ctx.host}${ctx.originalUrl || ctx.url}`,
|
|
955
|
+
ctx.method,
|
|
956
|
+
ctx.headers,
|
|
957
|
+
ctx.request.body,
|
|
958
|
+
ctx.state.requestContext,
|
|
959
|
+
customRouteAbortController.signal
|
|
960
|
+
);
|
|
961
|
+
if (!response) return next();
|
|
962
|
+
ctx.respond = false;
|
|
963
|
+
await server.writeCustomRouteResponse(response, ctx.res, customRouteAbortController.signal);
|
|
964
|
+
} finally {
|
|
965
|
+
ctx.res.off("close", abortCustomRouteIfOpen);
|
|
966
|
+
ctx.res.off("error", abortCustomRouteIfOpen);
|
|
976
967
|
}
|
|
977
|
-
const response = await server.handleCustomRouteRequest(
|
|
978
|
-
`${ctx.protocol}://${ctx.host}${ctx.originalUrl || ctx.url}`,
|
|
979
|
-
ctx.method,
|
|
980
|
-
ctx.headers,
|
|
981
|
-
ctx.request.body,
|
|
982
|
-
ctx.state.requestContext
|
|
983
|
-
);
|
|
984
|
-
if (!response) return next();
|
|
985
|
-
ctx.respond = false;
|
|
986
|
-
await server.writeCustomRouteResponse(response, ctx.res);
|
|
987
968
|
});
|
|
988
969
|
}
|
|
989
970
|
registerContextMiddleware() {
|
|
@@ -1004,14 +985,14 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
1004
985
|
const method = ctx.method;
|
|
1005
986
|
const path = ctx.path;
|
|
1006
987
|
await next();
|
|
1007
|
-
const
|
|
988
|
+
const duration = Date.now() - start;
|
|
1008
989
|
const status = ctx.status;
|
|
1009
990
|
const level = server.httpLoggingConfig?.level || "info";
|
|
1010
991
|
const logData = {
|
|
1011
992
|
method,
|
|
1012
993
|
path,
|
|
1013
994
|
status,
|
|
1014
|
-
duration: `${
|
|
995
|
+
duration: `${duration}ms`
|
|
1015
996
|
};
|
|
1016
997
|
if (server.httpLoggingConfig?.includeQueryParams) {
|
|
1017
998
|
logData.query = ctx.query;
|
|
@@ -1027,7 +1008,7 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
1027
1008
|
});
|
|
1028
1009
|
logData.headers = headers;
|
|
1029
1010
|
}
|
|
1030
|
-
server.logger[level](`${method} ${path} ${status} ${
|
|
1011
|
+
server.logger[level](`${method} ${path} ${status} ${duration}ms`, logData);
|
|
1031
1012
|
});
|
|
1032
1013
|
}
|
|
1033
1014
|
};
|