@mastra/fastify 1.2.6 → 1.3.0-alpha.1
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 +26 -0
- package/dist/auth-middleware.d.ts +8 -0
- package/dist/auth-middleware.d.ts.map +1 -0
- package/dist/index.cjs +67 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +67 -6
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# @mastra/fastify
|
|
2
2
|
|
|
3
|
+
## 1.3.0-alpha.1
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Added adapter auth middleware helpers for raw framework routes. ([#14458](https://github.com/mastra-ai/mastra/pull/14458))
|
|
8
|
+
|
|
9
|
+
Use `createAuthMiddleware({ mastra })` when you mount routes directly on a Hono, Express, Fastify, or Koa app and still want Mastra auth to run. Set `requiresAuth: false` when you need to reuse the same helper chain on a public route.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
app.get('/custom/protected', createAuthMiddleware({ mastra }), handler);
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Updated dependencies [[`7dbd611`](https://github.com/mastra-ai/mastra/commit/7dbd611a85cb1e0c0a1581c57564268cb183d86e), [`8a738d8`](https://github.com/mastra-ai/mastra/commit/8a738d844772e14be381d54b22fd5d548ee4af42), [`41aee84`](https://github.com/mastra-ai/mastra/commit/41aee84561ceebe28bad1ecba8702d92838f67f0)]:
|
|
18
|
+
- @mastra/core@1.16.0-alpha.1
|
|
19
|
+
- @mastra/server@1.16.0-alpha.1
|
|
20
|
+
|
|
21
|
+
## 1.2.7-alpha.0
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- Updated dependencies [[`68ed4e9`](https://github.com/mastra-ai/mastra/commit/68ed4e9f118e8646b60a6112dabe854d0ef53902), [`085c1da`](https://github.com/mastra-ai/mastra/commit/085c1daf71b55a97b8ebad26623089e40055021c), [`4a75e10`](https://github.com/mastra-ai/mastra/commit/4a75e106bd31c283a1b3fe74c923610dcc46415b), [`085c1da`](https://github.com/mastra-ai/mastra/commit/085c1daf71b55a97b8ebad26623089e40055021c), [`085c1da`](https://github.com/mastra-ai/mastra/commit/085c1daf71b55a97b8ebad26623089e40055021c)]:
|
|
26
|
+
- @mastra/core@1.16.0-alpha.0
|
|
27
|
+
- @mastra/server@1.16.0-alpha.0
|
|
28
|
+
|
|
3
29
|
## 1.2.6
|
|
4
30
|
|
|
5
31
|
### Patch Changes
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Mastra } from '@mastra/core/mastra';
|
|
2
|
+
import type { preHandlerHookHandler } from 'fastify';
|
|
3
|
+
export interface FastifyAuthMiddlewareOptions {
|
|
4
|
+
mastra: Mastra;
|
|
5
|
+
requiresAuth?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function createAuthMiddleware({ mastra, requiresAuth, }: FastifyAuthMiddlewareOptions): preHandlerHookHandler;
|
|
8
|
+
//# sourceMappingURL=auth-middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-middleware.d.ts","sourceRoot":"","sources":["../src/auth-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAGlD,OAAO,KAAK,EAAgC,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAEnF,MAAM,WAAW,4BAA4B;IAC3C,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAuBD,wBAAgB,oBAAoB,CAAC,EACnC,MAAM,EACN,YAAmB,GACpB,EAAE,4BAA4B,GAAG,qBAAqB,CA2CtD"}
|
package/dist/index.cjs
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
var busboy = require('@fastify/busboy');
|
|
4
4
|
var serverAdapter = require('@mastra/server/server-adapter');
|
|
5
|
+
var requestContext = require('@mastra/core/request-context');
|
|
6
|
+
var auth = require('@mastra/server/auth');
|
|
5
7
|
|
|
6
8
|
// src/index.ts
|
|
7
9
|
// @__NO_SIDE_EFFECTS__
|
|
@@ -212,6 +214,65 @@ var initializer2 = (inst, issues) => {
|
|
|
212
214
|
});
|
|
213
215
|
};
|
|
214
216
|
var ZodError = $constructor("ZodError", initializer2);
|
|
217
|
+
function toWebRequest(request) {
|
|
218
|
+
const protocol = request.protocol || "http";
|
|
219
|
+
const host = request.headers.host || "localhost";
|
|
220
|
+
const url = `${protocol}://${host}${request.url}`;
|
|
221
|
+
const headers = new Headers();
|
|
222
|
+
for (const [key, value] of Object.entries(request.headers)) {
|
|
223
|
+
if (!value) continue;
|
|
224
|
+
if (Array.isArray(value)) {
|
|
225
|
+
value.forEach((v) => headers.append(key, v));
|
|
226
|
+
} else {
|
|
227
|
+
headers.set(key, value);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return new globalThis.Request(url, {
|
|
231
|
+
method: request.method,
|
|
232
|
+
headers
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
function createAuthMiddleware({
|
|
236
|
+
mastra,
|
|
237
|
+
requiresAuth = true
|
|
238
|
+
}) {
|
|
239
|
+
return async (request, reply) => {
|
|
240
|
+
if (!requiresAuth) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
const authConfig = mastra.getServer()?.auth;
|
|
244
|
+
if (!authConfig) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
request.requestContext ??= new requestContext.RequestContext();
|
|
248
|
+
request.mastra ??= mastra;
|
|
249
|
+
const path = String(request.url.split("?")[0] || "/");
|
|
250
|
+
const method = String(request.method || "GET");
|
|
251
|
+
const customRouteAuthConfig = new Map(request.customRouteAuthConfig ?? []);
|
|
252
|
+
customRouteAuthConfig.set(`${method}:${path}`, true);
|
|
253
|
+
const authHeader = request.headers.authorization;
|
|
254
|
+
let token = authHeader ? authHeader.replace("Bearer ", "") : null;
|
|
255
|
+
const query = request.query;
|
|
256
|
+
if (!token && query.apiKey) {
|
|
257
|
+
token = query.apiKey || null;
|
|
258
|
+
}
|
|
259
|
+
const result = await auth.coreAuthMiddleware({
|
|
260
|
+
path,
|
|
261
|
+
method,
|
|
262
|
+
getHeader: (name) => request.headers[name.toLowerCase()],
|
|
263
|
+
mastra,
|
|
264
|
+
authConfig,
|
|
265
|
+
customRouteAuthConfig,
|
|
266
|
+
requestContext: request.requestContext,
|
|
267
|
+
rawRequest: toWebRequest(request),
|
|
268
|
+
token,
|
|
269
|
+
buildAuthorizeContext: () => toWebRequest(request)
|
|
270
|
+
});
|
|
271
|
+
if (result.action === "error") {
|
|
272
|
+
return reply.status(result.status).send(result.body);
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
}
|
|
215
276
|
|
|
216
277
|
// src/index.ts
|
|
217
278
|
var _hasPermissionPromise;
|
|
@@ -226,7 +287,7 @@ function loadHasPermission() {
|
|
|
226
287
|
}
|
|
227
288
|
return _hasPermissionPromise;
|
|
228
289
|
}
|
|
229
|
-
function
|
|
290
|
+
function toWebRequest2(request) {
|
|
230
291
|
const protocol = request.protocol || "http";
|
|
231
292
|
const host = request.headers.host || "localhost";
|
|
232
293
|
const url = `${protocol}://${host}${request.url}`;
|
|
@@ -519,8 +580,8 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
519
580
|
getHeader: (name) => request.headers[name.toLowerCase()],
|
|
520
581
|
getQuery: (name) => request.query[name],
|
|
521
582
|
requestContext: request.requestContext,
|
|
522
|
-
request:
|
|
523
|
-
buildAuthorizeContext: () =>
|
|
583
|
+
request: toWebRequest2(request),
|
|
584
|
+
buildAuthorizeContext: () => toWebRequest2(request)
|
|
524
585
|
});
|
|
525
586
|
if (authError) {
|
|
526
587
|
return reply.status(authError.status).send({ error: authError.error });
|
|
@@ -676,8 +737,8 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
676
737
|
getHeader: (name) => request.headers[name.toLowerCase()],
|
|
677
738
|
getQuery: (name) => request.query[name],
|
|
678
739
|
requestContext: request.requestContext,
|
|
679
|
-
request:
|
|
680
|
-
buildAuthorizeContext: () =>
|
|
740
|
+
request: toWebRequest2(request),
|
|
741
|
+
buildAuthorizeContext: () => toWebRequest2(request)
|
|
681
742
|
});
|
|
682
743
|
if (authError) {
|
|
683
744
|
return reply.status(authError.status).send({ error: authError.error });
|
|
@@ -795,5 +856,6 @@ var MastraServer = class extends serverAdapter.MastraServer {
|
|
|
795
856
|
};
|
|
796
857
|
|
|
797
858
|
exports.MastraServer = MastraServer;
|
|
859
|
+
exports.createAuthMiddleware = createAuthMiddleware;
|
|
798
860
|
//# sourceMappingURL=index.cjs.map
|
|
799
861
|
//# sourceMappingURL=index.cjs.map
|