@mastra/hono 1.7.29-alpha.5 → 1.7.29-alpha.7
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/dist/index.cjs +85 -55
- package/dist/index.js +85 -55
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -462,6 +462,18 @@ function cancelUnreadBodyOnAbort(response, signal) {
|
|
|
462
462
|
if (signal.aborted) cancel();
|
|
463
463
|
else signal.addEventListener("abort", cancel, { once: true });
|
|
464
464
|
}
|
|
465
|
+
/**
|
|
466
|
+
* Releases an adapter-owned body branch that will not be forwarded. `Request.clone()` tees the body, so an unread
|
|
467
|
+
* branch retains the whole body until GC. Cancelling the unused branch frees it while the forwarded branch streams.
|
|
468
|
+
*
|
|
469
|
+
* The cancel is intentionally fire-and-forget: fetch tee cancellation only settles once the sibling branch drains (or
|
|
470
|
+
* is otherwise consumed), so awaiting it here would stall dispatch. Rejections are swallowed so cancellation failure
|
|
471
|
+
* cannot turn into an unhandled rejection or replace the route response.
|
|
472
|
+
*/
|
|
473
|
+
function releaseUnreadBody(body) {
|
|
474
|
+
if (!body || body.locked) return;
|
|
475
|
+
body.cancel("unused request body clone").catch(() => {});
|
|
476
|
+
}
|
|
465
477
|
const requestClones = /* @__PURE__ */ new WeakMap();
|
|
466
478
|
/**
|
|
467
479
|
* Clones a request and keeps the clone alive for as long as the request itself. Node 22 makes a request's body
|
|
@@ -478,7 +490,7 @@ function cloneRequest(request) {
|
|
|
478
490
|
var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
|
|
479
491
|
createContextMiddleware() {
|
|
480
492
|
return async (c, next) => {
|
|
481
|
-
if (this.hasCustomRouteHandler && BODY_METHODS.has(c.req.method) && c.req.raw.body) c.set(MASTRA_PRISTINE_REQUEST_KEY, c.req.raw.clone());
|
|
493
|
+
if (this.hasCustomRouteHandler && BODY_METHODS.has(c.req.method) && c.req.raw.body && this.matchCustomRoute(c.req.url, c.req.method)) c.set(MASTRA_PRISTINE_REQUEST_KEY, c.req.raw.clone());
|
|
482
494
|
const originalJson = c.req.json.bind(c.req);
|
|
483
495
|
let jsonPromise;
|
|
484
496
|
c.req.json = () => {
|
|
@@ -867,64 +879,82 @@ var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
|
|
|
867
879
|
if (!await this.buildCustomRouteHandler(routes)) return;
|
|
868
880
|
for (const route of routes) {
|
|
869
881
|
const routeHandler = async (c) => {
|
|
870
|
-
const
|
|
871
|
-
const
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
}, permissionError.status);
|
|
890
|
-
const pristineRequest = c.get(MASTRA_PRISTINE_REQUEST_KEY) ?? c.req.raw;
|
|
891
|
-
let bodyParams = {};
|
|
892
|
-
if ((0, _mastra_server_server_adapter.getFGAProvider)(this.mastra, c.get("requestContext"))) {
|
|
893
|
-
const contentType = c.req.header("content-type");
|
|
894
|
-
if (contentType?.includes("application/json")) try {
|
|
895
|
-
const body = await pristineRequest.clone().json();
|
|
896
|
-
if (body && typeof body === "object" && !Array.isArray(body)) bodyParams = body;
|
|
897
|
-
} catch {
|
|
898
|
-
bodyParams = {};
|
|
882
|
+
const originalRequest = c.req.raw;
|
|
883
|
+
const pristineRequest = c.get(MASTRA_PRISTINE_REQUEST_KEY) ?? originalRequest;
|
|
884
|
+
const hasPristine = pristineRequest !== originalRequest;
|
|
885
|
+
let releasePristineBody = hasPristine;
|
|
886
|
+
try {
|
|
887
|
+
const { match, serverRoute, customRouteAuthConfig } = this.resolveCustomRouteAuth(c.req.url, c.req.method, c.req.path, route);
|
|
888
|
+
const authError = await this.checkRouteAuth(serverRoute, {
|
|
889
|
+
path: c.req.path,
|
|
890
|
+
method: c.req.method,
|
|
891
|
+
getHeader: (name) => c.req.header(name),
|
|
892
|
+
getQuery: (name) => c.req.query(name),
|
|
893
|
+
requestContext: c.get("requestContext"),
|
|
894
|
+
request: c.req.raw,
|
|
895
|
+
buildAuthorizeContext: () => c,
|
|
896
|
+
customRouteAuthConfig
|
|
897
|
+
});
|
|
898
|
+
if (authError) {
|
|
899
|
+
if (authError.headers) for (const [key, value] of Object.entries(authError.headers)) c.header(key, value);
|
|
900
|
+
if (authError.error) return c.json({ error: authError.error }, authError.status);
|
|
899
901
|
}
|
|
900
|
-
|
|
901
|
-
|
|
902
|
+
const permissionError = this.checkRouteRBAC(serverRoute, c.get("requestContext"));
|
|
903
|
+
if (permissionError) return c.json({
|
|
904
|
+
error: permissionError.error,
|
|
905
|
+
message: permissionError.message
|
|
906
|
+
}, permissionError.status);
|
|
907
|
+
let bodyParams = {};
|
|
908
|
+
if ((0, _mastra_server_server_adapter.getFGAProvider)(this.mastra, c.get("requestContext"))) {
|
|
909
|
+
const contentType = c.req.header("content-type");
|
|
910
|
+
if (contentType?.includes("application/json")) try {
|
|
911
|
+
const body = await pristineRequest.clone().json();
|
|
912
|
+
if (body && typeof body === "object" && !Array.isArray(body)) bodyParams = body;
|
|
913
|
+
} catch {
|
|
914
|
+
bodyParams = {};
|
|
915
|
+
}
|
|
916
|
+
else if (contentType?.includes("application/x-www-form-urlencoded") || contentType?.includes("multipart/form-data")) try {
|
|
917
|
+
bodyParams = Object.fromEntries(await pristineRequest.clone().formData());
|
|
918
|
+
} catch {
|
|
919
|
+
bodyParams = {};
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
const fgaError = await (0, _mastra_server_server_adapter.checkRouteFGA)(this.mastra, serverRoute, c.get("requestContext"), {
|
|
923
|
+
...Object.fromEntries(new URL(c.req.url).searchParams.entries()),
|
|
924
|
+
...bodyParams,
|
|
925
|
+
...match ? match.params : c.req.param()
|
|
926
|
+
});
|
|
927
|
+
if (fgaError) return c.json({
|
|
928
|
+
error: fgaError.error,
|
|
929
|
+
message: fgaError.message
|
|
930
|
+
}, fgaError.status);
|
|
931
|
+
const reqHeaders = {};
|
|
932
|
+
c.req.raw.headers.forEach((v, k) => {
|
|
933
|
+
reqHeaders[k] = v;
|
|
934
|
+
});
|
|
935
|
+
let executionCtx;
|
|
936
|
+
try {
|
|
937
|
+
executionCtx = c.executionCtx;
|
|
902
938
|
} catch {
|
|
903
|
-
|
|
939
|
+
executionCtx = void 0;
|
|
904
940
|
}
|
|
941
|
+
const originalUntouched = originalRequest.body !== null && !originalRequest.bodyUsed && !originalRequest.body.locked;
|
|
942
|
+
let requestBody;
|
|
943
|
+
if (!hasPristine) requestBody = originalRequest.body;
|
|
944
|
+
else if (originalUntouched) {
|
|
945
|
+
requestBody = originalRequest.body;
|
|
946
|
+
releasePristineBody = false;
|
|
947
|
+
releaseUnreadBody(pristineRequest.body);
|
|
948
|
+
} else {
|
|
949
|
+
requestBody = pristineRequest.body;
|
|
950
|
+
releasePristineBody = false;
|
|
951
|
+
}
|
|
952
|
+
const response = await this.handleCustomRouteRequest(c.req.url, c.req.method, reqHeaders, requestBody, c.get("requestContext"), c.req.raw.signal, executionCtx);
|
|
953
|
+
if (!response) return c.json({ error: "Not Found" }, 404);
|
|
954
|
+
return response;
|
|
955
|
+
} finally {
|
|
956
|
+
if (releasePristineBody) releaseUnreadBody(pristineRequest.body);
|
|
905
957
|
}
|
|
906
|
-
const fgaError = await (0, _mastra_server_server_adapter.checkRouteFGA)(this.mastra, serverRoute, c.get("requestContext"), {
|
|
907
|
-
...Object.fromEntries(new URL(c.req.url).searchParams.entries()),
|
|
908
|
-
...bodyParams,
|
|
909
|
-
...match ? match.params : c.req.param()
|
|
910
|
-
});
|
|
911
|
-
if (fgaError) return c.json({
|
|
912
|
-
error: fgaError.error,
|
|
913
|
-
message: fgaError.message
|
|
914
|
-
}, fgaError.status);
|
|
915
|
-
const reqHeaders = {};
|
|
916
|
-
c.req.raw.headers.forEach((v, k) => {
|
|
917
|
-
reqHeaders[k] = v;
|
|
918
|
-
});
|
|
919
|
-
let executionCtx;
|
|
920
|
-
try {
|
|
921
|
-
executionCtx = c.executionCtx;
|
|
922
|
-
} catch {
|
|
923
|
-
executionCtx = void 0;
|
|
924
|
-
}
|
|
925
|
-
const response = await this.handleCustomRouteRequest(c.req.url, c.req.method, reqHeaders, pristineRequest.body, c.get("requestContext"), c.req.raw.signal, executionCtx);
|
|
926
|
-
if (!response) return c.json({ error: "Not Found" }, 404);
|
|
927
|
-
return response;
|
|
928
958
|
};
|
|
929
959
|
const method = route.method.toLowerCase();
|
|
930
960
|
this.app[method](route.path, routeHandler);
|
package/dist/index.js
CHANGED
|
@@ -461,6 +461,18 @@ function cancelUnreadBodyOnAbort(response, signal) {
|
|
|
461
461
|
if (signal.aborted) cancel();
|
|
462
462
|
else signal.addEventListener("abort", cancel, { once: true });
|
|
463
463
|
}
|
|
464
|
+
/**
|
|
465
|
+
* Releases an adapter-owned body branch that will not be forwarded. `Request.clone()` tees the body, so an unread
|
|
466
|
+
* branch retains the whole body until GC. Cancelling the unused branch frees it while the forwarded branch streams.
|
|
467
|
+
*
|
|
468
|
+
* The cancel is intentionally fire-and-forget: fetch tee cancellation only settles once the sibling branch drains (or
|
|
469
|
+
* is otherwise consumed), so awaiting it here would stall dispatch. Rejections are swallowed so cancellation failure
|
|
470
|
+
* cannot turn into an unhandled rejection or replace the route response.
|
|
471
|
+
*/
|
|
472
|
+
function releaseUnreadBody(body) {
|
|
473
|
+
if (!body || body.locked) return;
|
|
474
|
+
body.cancel("unused request body clone").catch(() => {});
|
|
475
|
+
}
|
|
464
476
|
const requestClones = /* @__PURE__ */ new WeakMap();
|
|
465
477
|
/**
|
|
466
478
|
* Clones a request and keeps the clone alive for as long as the request itself. Node 22 makes a request's body
|
|
@@ -477,7 +489,7 @@ function cloneRequest(request) {
|
|
|
477
489
|
var MastraServer = class extends MastraServer$1 {
|
|
478
490
|
createContextMiddleware() {
|
|
479
491
|
return async (c, next) => {
|
|
480
|
-
if (this.hasCustomRouteHandler && BODY_METHODS.has(c.req.method) && c.req.raw.body) c.set(MASTRA_PRISTINE_REQUEST_KEY, c.req.raw.clone());
|
|
492
|
+
if (this.hasCustomRouteHandler && BODY_METHODS.has(c.req.method) && c.req.raw.body && this.matchCustomRoute(c.req.url, c.req.method)) c.set(MASTRA_PRISTINE_REQUEST_KEY, c.req.raw.clone());
|
|
481
493
|
const originalJson = c.req.json.bind(c.req);
|
|
482
494
|
let jsonPromise;
|
|
483
495
|
c.req.json = () => {
|
|
@@ -866,64 +878,82 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
866
878
|
if (!await this.buildCustomRouteHandler(routes)) return;
|
|
867
879
|
for (const route of routes) {
|
|
868
880
|
const routeHandler = async (c) => {
|
|
869
|
-
const
|
|
870
|
-
const
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
}, permissionError.status);
|
|
889
|
-
const pristineRequest = c.get(MASTRA_PRISTINE_REQUEST_KEY) ?? c.req.raw;
|
|
890
|
-
let bodyParams = {};
|
|
891
|
-
if (getFGAProvider(this.mastra, c.get("requestContext"))) {
|
|
892
|
-
const contentType = c.req.header("content-type");
|
|
893
|
-
if (contentType?.includes("application/json")) try {
|
|
894
|
-
const body = await pristineRequest.clone().json();
|
|
895
|
-
if (body && typeof body === "object" && !Array.isArray(body)) bodyParams = body;
|
|
896
|
-
} catch {
|
|
897
|
-
bodyParams = {};
|
|
881
|
+
const originalRequest = c.req.raw;
|
|
882
|
+
const pristineRequest = c.get(MASTRA_PRISTINE_REQUEST_KEY) ?? originalRequest;
|
|
883
|
+
const hasPristine = pristineRequest !== originalRequest;
|
|
884
|
+
let releasePristineBody = hasPristine;
|
|
885
|
+
try {
|
|
886
|
+
const { match, serverRoute, customRouteAuthConfig } = this.resolveCustomRouteAuth(c.req.url, c.req.method, c.req.path, route);
|
|
887
|
+
const authError = await this.checkRouteAuth(serverRoute, {
|
|
888
|
+
path: c.req.path,
|
|
889
|
+
method: c.req.method,
|
|
890
|
+
getHeader: (name) => c.req.header(name),
|
|
891
|
+
getQuery: (name) => c.req.query(name),
|
|
892
|
+
requestContext: c.get("requestContext"),
|
|
893
|
+
request: c.req.raw,
|
|
894
|
+
buildAuthorizeContext: () => c,
|
|
895
|
+
customRouteAuthConfig
|
|
896
|
+
});
|
|
897
|
+
if (authError) {
|
|
898
|
+
if (authError.headers) for (const [key, value] of Object.entries(authError.headers)) c.header(key, value);
|
|
899
|
+
if (authError.error) return c.json({ error: authError.error }, authError.status);
|
|
898
900
|
}
|
|
899
|
-
|
|
900
|
-
|
|
901
|
+
const permissionError = this.checkRouteRBAC(serverRoute, c.get("requestContext"));
|
|
902
|
+
if (permissionError) return c.json({
|
|
903
|
+
error: permissionError.error,
|
|
904
|
+
message: permissionError.message
|
|
905
|
+
}, permissionError.status);
|
|
906
|
+
let bodyParams = {};
|
|
907
|
+
if (getFGAProvider(this.mastra, c.get("requestContext"))) {
|
|
908
|
+
const contentType = c.req.header("content-type");
|
|
909
|
+
if (contentType?.includes("application/json")) try {
|
|
910
|
+
const body = await pristineRequest.clone().json();
|
|
911
|
+
if (body && typeof body === "object" && !Array.isArray(body)) bodyParams = body;
|
|
912
|
+
} catch {
|
|
913
|
+
bodyParams = {};
|
|
914
|
+
}
|
|
915
|
+
else if (contentType?.includes("application/x-www-form-urlencoded") || contentType?.includes("multipart/form-data")) try {
|
|
916
|
+
bodyParams = Object.fromEntries(await pristineRequest.clone().formData());
|
|
917
|
+
} catch {
|
|
918
|
+
bodyParams = {};
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
const fgaError = await checkRouteFGA(this.mastra, serverRoute, c.get("requestContext"), {
|
|
922
|
+
...Object.fromEntries(new URL(c.req.url).searchParams.entries()),
|
|
923
|
+
...bodyParams,
|
|
924
|
+
...match ? match.params : c.req.param()
|
|
925
|
+
});
|
|
926
|
+
if (fgaError) return c.json({
|
|
927
|
+
error: fgaError.error,
|
|
928
|
+
message: fgaError.message
|
|
929
|
+
}, fgaError.status);
|
|
930
|
+
const reqHeaders = {};
|
|
931
|
+
c.req.raw.headers.forEach((v, k) => {
|
|
932
|
+
reqHeaders[k] = v;
|
|
933
|
+
});
|
|
934
|
+
let executionCtx;
|
|
935
|
+
try {
|
|
936
|
+
executionCtx = c.executionCtx;
|
|
901
937
|
} catch {
|
|
902
|
-
|
|
938
|
+
executionCtx = void 0;
|
|
903
939
|
}
|
|
940
|
+
const originalUntouched = originalRequest.body !== null && !originalRequest.bodyUsed && !originalRequest.body.locked;
|
|
941
|
+
let requestBody;
|
|
942
|
+
if (!hasPristine) requestBody = originalRequest.body;
|
|
943
|
+
else if (originalUntouched) {
|
|
944
|
+
requestBody = originalRequest.body;
|
|
945
|
+
releasePristineBody = false;
|
|
946
|
+
releaseUnreadBody(pristineRequest.body);
|
|
947
|
+
} else {
|
|
948
|
+
requestBody = pristineRequest.body;
|
|
949
|
+
releasePristineBody = false;
|
|
950
|
+
}
|
|
951
|
+
const response = await this.handleCustomRouteRequest(c.req.url, c.req.method, reqHeaders, requestBody, c.get("requestContext"), c.req.raw.signal, executionCtx);
|
|
952
|
+
if (!response) return c.json({ error: "Not Found" }, 404);
|
|
953
|
+
return response;
|
|
954
|
+
} finally {
|
|
955
|
+
if (releasePristineBody) releaseUnreadBody(pristineRequest.body);
|
|
904
956
|
}
|
|
905
|
-
const fgaError = await checkRouteFGA(this.mastra, serverRoute, c.get("requestContext"), {
|
|
906
|
-
...Object.fromEntries(new URL(c.req.url).searchParams.entries()),
|
|
907
|
-
...bodyParams,
|
|
908
|
-
...match ? match.params : c.req.param()
|
|
909
|
-
});
|
|
910
|
-
if (fgaError) return c.json({
|
|
911
|
-
error: fgaError.error,
|
|
912
|
-
message: fgaError.message
|
|
913
|
-
}, fgaError.status);
|
|
914
|
-
const reqHeaders = {};
|
|
915
|
-
c.req.raw.headers.forEach((v, k) => {
|
|
916
|
-
reqHeaders[k] = v;
|
|
917
|
-
});
|
|
918
|
-
let executionCtx;
|
|
919
|
-
try {
|
|
920
|
-
executionCtx = c.executionCtx;
|
|
921
|
-
} catch {
|
|
922
|
-
executionCtx = void 0;
|
|
923
|
-
}
|
|
924
|
-
const response = await this.handleCustomRouteRequest(c.req.url, c.req.method, reqHeaders, pristineRequest.body, c.get("requestContext"), c.req.raw.signal, executionCtx);
|
|
925
|
-
if (!response) return c.json({ error: "Not Found" }, 404);
|
|
926
|
-
return response;
|
|
927
957
|
};
|
|
928
958
|
const method = route.method.toLowerCase();
|
|
929
959
|
this.app[method](route.path, routeHandler);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/hono",
|
|
3
|
-
"version": "1.7.29-alpha.
|
|
3
|
+
"version": "1.7.29-alpha.7",
|
|
4
4
|
"description": "Mastra Hono adapter for the server",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@hono/node-ws": "^1.3.1",
|
|
24
24
|
"fetch-to-node": "^2.1.0",
|
|
25
25
|
"ws": "^8.23.0",
|
|
26
|
-
"@mastra/server": "1.87.0-alpha.
|
|
26
|
+
"@mastra/server": "1.87.0-alpha.7"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@ai-sdk/openai": "^2.0.139",
|
|
@@ -41,11 +41,11 @@
|
|
|
41
41
|
"@internal/lint": "0.0.152",
|
|
42
42
|
"@internal/storage-test-utils": "0.0.148",
|
|
43
43
|
"@internal/types-builder": "0.0.127",
|
|
44
|
-
"@mastra/client-js": "1.63.0-alpha.
|
|
44
|
+
"@mastra/client-js": "1.63.0-alpha.7",
|
|
45
|
+
"@mastra/core": "1.87.0-alpha.7",
|
|
45
46
|
"@mastra/duckdb": "1.19.3-alpha.0",
|
|
46
|
-
"@mastra/core": "1.87.0-alpha.5",
|
|
47
47
|
"@mastra/libsql": "1.33.0-alpha.2",
|
|
48
|
-
"@mastra/mcp": "2.11.1-alpha.
|
|
48
|
+
"@mastra/mcp": "2.11.1-alpha.1",
|
|
49
49
|
"@mastra/mcp-apps-components": "0.3.0",
|
|
50
50
|
"@mastra/memory": "1.41.4-alpha.0",
|
|
51
51
|
"@mastra/observability": "1.24.4-alpha.1",
|