@mastra/elysia 0.1.19-alpha.9 → 0.1.20-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/dist/__tests__/helpers.d.ts.map +1 -1
- package/dist/index.cjs +125 -80
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +126 -81
- package/dist/index.js.map +1 -1
- package/package.json +14 -12
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/__tests__/helpers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAuB,MAAM,EAAE,MAAM,WAAW,CAAC;AAE7D,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;IAC5D,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC,
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/__tests__/helpers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAuB,MAAM,EAAE,MAAM,WAAW,CAAC;AAE7D,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;IAC5D,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC,CAwFD"}
|
package/dist/index.cjs
CHANGED
|
@@ -204,6 +204,50 @@ function clearMastraOpenAPICache(server) {
|
|
|
204
204
|
}
|
|
205
205
|
//#endregion
|
|
206
206
|
//#region src/index.ts
|
|
207
|
+
const BODY_TOO_LARGE = Symbol("mastra.elysia.bodyTooLarge");
|
|
208
|
+
async function readBodyWithLimit(body, maxSize) {
|
|
209
|
+
const reader = body.getReader();
|
|
210
|
+
const chunks = [];
|
|
211
|
+
let size = 0;
|
|
212
|
+
while (true) {
|
|
213
|
+
const { done, value } = await reader.read();
|
|
214
|
+
if (done) break;
|
|
215
|
+
size += value.byteLength;
|
|
216
|
+
if (size > maxSize) {
|
|
217
|
+
await reader.cancel().catch(() => {});
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
chunks.push(value);
|
|
221
|
+
}
|
|
222
|
+
const bytes = new Uint8Array(size);
|
|
223
|
+
let offset = 0;
|
|
224
|
+
for (const chunk of chunks) {
|
|
225
|
+
bytes.set(chunk, offset);
|
|
226
|
+
offset += chunk.byteLength;
|
|
227
|
+
}
|
|
228
|
+
return bytes;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Route-level Elysia parse hook that enforces the body limit before Elysia's
|
|
232
|
+
* default parsers buffer the request. Oversized bodies are marked with
|
|
233
|
+
* BODY_TOO_LARGE (rejected with 413 by the route handler) without being read in full.
|
|
234
|
+
*/
|
|
235
|
+
function createBodyLimitParser(maxSize) {
|
|
236
|
+
return async (ctx) => {
|
|
237
|
+
const request = ctx.request;
|
|
238
|
+
const contentLength = request.headers.get("content-length");
|
|
239
|
+
if (contentLength !== null && parseInt(contentLength, 10) > maxSize) return BODY_TOO_LARGE;
|
|
240
|
+
if (!request.body) return void 0;
|
|
241
|
+
const bytes = await readBodyWithLimit(request.body, maxSize);
|
|
242
|
+
if (!bytes) return BODY_TOO_LARGE;
|
|
243
|
+
ctx.request = new Request(request.url, {
|
|
244
|
+
method: request.method,
|
|
245
|
+
headers: request.headers,
|
|
246
|
+
body: bytes,
|
|
247
|
+
signal: request.signal
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
}
|
|
207
251
|
/**
|
|
208
252
|
* Normalizes route path parameters to position-based names (:p0, :p1, ...)
|
|
209
253
|
* to avoid Elysia router conflicts when different routes use different
|
|
@@ -240,12 +284,13 @@ async function createForwardRequest(ctx) {
|
|
|
240
284
|
if (method === "GET" || method === "HEAD") return request;
|
|
241
285
|
const headers = new Headers(request.headers);
|
|
242
286
|
let body;
|
|
243
|
-
if (ctx.body !== void 0)
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
287
|
+
if (ctx.body !== void 0) {
|
|
288
|
+
if (typeof ctx.body === "string" || ctx.body instanceof FormData || ctx.body instanceof Blob) body = ctx.body;
|
|
289
|
+
else {
|
|
290
|
+
body = JSON.stringify(ctx.body);
|
|
291
|
+
if (!headers.has("content-type")) headers.set("content-type", "application/json");
|
|
292
|
+
}
|
|
293
|
+
} else if (!request.bodyUsed) {
|
|
249
294
|
const bodyBuffer = await request.clone().arrayBuffer();
|
|
250
295
|
if (bodyBuffer.byteLength > 0) body = bodyBuffer;
|
|
251
296
|
}
|
|
@@ -330,44 +375,53 @@ var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
|
|
|
330
375
|
Connection: "keep-alive",
|
|
331
376
|
"X-Accel-Buffering": "no"
|
|
332
377
|
} : { "Content-Type": "text/plain" };
|
|
333
|
-
const
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
if (
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
378
|
+
const reader = (result instanceof ReadableStream ? result : result.fullStream).getReader();
|
|
379
|
+
let cancelled = false;
|
|
380
|
+
const stream = new ReadableStream({
|
|
381
|
+
start: async (controller) => {
|
|
382
|
+
try {
|
|
383
|
+
if (streamFormat === "sse" && route.sseFlushOnConnect) controller.enqueue(encoder.encode(": connected\n\n"));
|
|
384
|
+
while (true) {
|
|
385
|
+
const { done, value } = await reader.read();
|
|
386
|
+
if (done || cancelled) break;
|
|
387
|
+
if (value) {
|
|
388
|
+
if (streamFormat === "sse" && typeof value === "string" && value.startsWith(":")) {
|
|
389
|
+
controller.enqueue(encoder.encode(value));
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
const outputValue = this.streamOptions?.redact ?? true ? (0, _mastra_server_server_adapter.redactStreamChunk)(value) : value;
|
|
393
|
+
const serialized = (0, _mastra_server_server_adapter.serializeStreamChunk)(outputValue);
|
|
394
|
+
if (!serialized.ok) {
|
|
395
|
+
this.mastra.getLogger()?.error("Failed to serialize stream chunk, skipping", {
|
|
396
|
+
path: route.path,
|
|
397
|
+
chunkType: outputValue?.type,
|
|
398
|
+
error: serialized.error.message
|
|
399
|
+
});
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
if (streamFormat === "sse") controller.enqueue(encoder.encode(`data: ${serialized.json}\n\n`));
|
|
403
|
+
else controller.enqueue(encoder.encode(serialized.json + ""));
|
|
354
404
|
}
|
|
355
|
-
if (streamFormat === "sse") controller.enqueue(encoder.encode(`data: ${serialized.json}\n\n`));
|
|
356
|
-
else controller.enqueue(encoder.encode(serialized.json + ""));
|
|
357
405
|
}
|
|
406
|
+
if (cancelled) return;
|
|
407
|
+
if (streamFormat === "sse") controller.enqueue(encoder.encode("data: [DONE]\n\n"));
|
|
408
|
+
controller.close();
|
|
409
|
+
} catch (error) {
|
|
410
|
+
if (cancelled) return;
|
|
411
|
+
this.mastra.getLogger()?.error("Error in stream processing", { error: error instanceof Error ? {
|
|
412
|
+
message: error.message,
|
|
413
|
+
stack: error.stack
|
|
414
|
+
} : error });
|
|
415
|
+
controller.error(error);
|
|
416
|
+
} finally {
|
|
417
|
+
await reader.cancel();
|
|
358
418
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
message: error.message,
|
|
364
|
-
stack: error.stack
|
|
365
|
-
} : error });
|
|
366
|
-
controller.error(error);
|
|
367
|
-
} finally {
|
|
368
|
-
await reader.cancel();
|
|
419
|
+
},
|
|
420
|
+
cancel: async (reason) => {
|
|
421
|
+
cancelled = true;
|
|
422
|
+
await reader.cancel(reason);
|
|
369
423
|
}
|
|
370
|
-
}
|
|
424
|
+
});
|
|
371
425
|
return new Response(stream, { headers });
|
|
372
426
|
}
|
|
373
427
|
async getParams(route, request) {
|
|
@@ -473,7 +527,8 @@ var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
|
|
|
473
527
|
});
|
|
474
528
|
} else if (route.responseType === "mcp-http") {
|
|
475
529
|
const { server, httpPath, mcpOptions: routeMcpOptions } = result;
|
|
476
|
-
const
|
|
530
|
+
const request = response;
|
|
531
|
+
const forwardRequest = await createForwardRequest(request);
|
|
477
532
|
const { req, res } = (0, fetch_to_node.toReqRes)(forwardRequest);
|
|
478
533
|
const options = {
|
|
479
534
|
...this.mcpOptions,
|
|
@@ -491,6 +546,7 @@ var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
|
|
|
491
546
|
stack: e.stack
|
|
492
547
|
} : e });
|
|
493
548
|
});
|
|
549
|
+
(0, _mastra_server_server_adapter.propagateRequestAbort)(request.request.signal, res);
|
|
494
550
|
const mcpResponse = await (0, fetch_to_node.toFetchResponse)(res);
|
|
495
551
|
const headers = new Headers(mcpResponse.headers);
|
|
496
552
|
headers.delete("Transfer-Encoding");
|
|
@@ -529,6 +585,16 @@ var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
|
|
|
529
585
|
const prefix = prefixParam ?? this.prefix ?? "";
|
|
530
586
|
const { normalizedPath, paramNames } = normalizeRouteParams(`${prefix}${route.path}`);
|
|
531
587
|
const handler = async (ctx) => {
|
|
588
|
+
if (ctx.body === BODY_TOO_LARGE) {
|
|
589
|
+
let errorResponse = { error: "Request body too large" };
|
|
590
|
+
if (route.maxBodySize === void 0 && this.bodyLimitOptions) try {
|
|
591
|
+
errorResponse = this.bodyLimitOptions.onError(errorResponse);
|
|
592
|
+
} catch {}
|
|
593
|
+
return new Response(JSON.stringify(errorResponse), {
|
|
594
|
+
status: 413,
|
|
595
|
+
headers: { "Content-Type": "application/json" }
|
|
596
|
+
});
|
|
597
|
+
}
|
|
532
598
|
const authError = await this.checkRouteAuth(route, {
|
|
533
599
|
path: ctx.request.url ? new URL(ctx.request.url).pathname : ctx.path,
|
|
534
600
|
method: ctx.request.method,
|
|
@@ -545,11 +611,11 @@ var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
|
|
|
545
611
|
...authError.headers
|
|
546
612
|
}
|
|
547
613
|
});
|
|
548
|
-
if (this.mastra.getServer()?.auth) {
|
|
614
|
+
if (this.mastra.getStudio?.()?.auth || this.mastra.getServer()?.auth) {
|
|
549
615
|
const hasPermission = await loadHasPermission();
|
|
550
616
|
if (hasPermission) {
|
|
551
617
|
const userPermissions = ctx.requestContext.get("userPermissions");
|
|
552
|
-
const permissionError = this.checkRoutePermission(route, userPermissions, hasPermission);
|
|
618
|
+
const permissionError = this.checkRoutePermission(route, userPermissions, hasPermission, ctx.requestContext);
|
|
553
619
|
if (permissionError) return new Response(JSON.stringify({
|
|
554
620
|
error: permissionError.error,
|
|
555
621
|
message: permissionError.message
|
|
@@ -565,21 +631,7 @@ var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
|
|
|
565
631
|
let body;
|
|
566
632
|
let bodyParseError;
|
|
567
633
|
if (route.method === "POST" || route.method === "PUT" || route.method === "PATCH" || route.method === "DELETE") {
|
|
568
|
-
const maxSize = route.maxBodySize ?? this.bodyLimitOptions?.maxSize;
|
|
569
|
-
const contentLength = ctx.request.headers.get("content-length");
|
|
570
634
|
const contentType = ctx.request.headers.get("content-type") || "";
|
|
571
|
-
const exceedsDeclaredLimit = maxSize !== void 0 && contentLength !== null && parseInt(contentLength, 10) > maxSize;
|
|
572
|
-
const exceedsStreamedJsonLimit = maxSize !== void 0 && contentLength === null && contentType.includes("application/json") && ctx.body !== void 0 && new TextEncoder().encode(JSON.stringify(ctx.body)).byteLength > maxSize;
|
|
573
|
-
if (exceedsDeclaredLimit || exceedsStreamedJsonLimit) {
|
|
574
|
-
let errorResponse = { error: "Request body too large" };
|
|
575
|
-
if (route.maxBodySize === void 0 && this.bodyLimitOptions) try {
|
|
576
|
-
errorResponse = this.bodyLimitOptions.onError(errorResponse);
|
|
577
|
-
} catch {}
|
|
578
|
-
return new Response(JSON.stringify(errorResponse), {
|
|
579
|
-
status: 413,
|
|
580
|
-
headers: { "Content-Type": "application/json" }
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
635
|
if (contentType.includes("multipart/form-data")) try {
|
|
584
636
|
body = ctx.body ? await this.parseFormData(ctx.body) : void 0;
|
|
585
637
|
} catch (error) {
|
|
@@ -736,29 +788,22 @@ var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
|
|
|
736
788
|
});
|
|
737
789
|
const customResponse = (0, _mastra_server_server_adapter.getCustomHTTPExceptionResponse)(error);
|
|
738
790
|
if (customResponse) return customResponse;
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
return new Response(JSON.stringify({ error: error instanceof Error ? error.message : "Unknown error" }), {
|
|
743
|
-
status,
|
|
744
|
-
headers: { "Content-Type": "application/json" }
|
|
745
|
-
});
|
|
746
|
-
}
|
|
747
|
-
if ("details" in error && error.details && typeof error.details === "object" && "status" in error.details) {
|
|
748
|
-
const status = error.details.status;
|
|
749
|
-
return new Response(JSON.stringify({ error: error instanceof Error ? error.message : "Unknown error" }), {
|
|
750
|
-
status,
|
|
751
|
-
headers: { "Content-Type": "application/json" }
|
|
752
|
-
});
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
return new Response(JSON.stringify({ error: error instanceof Error ? error.message : "Unknown error" }), {
|
|
756
|
-
status: 500,
|
|
791
|
+
const { status, body } = (0, _mastra_server_server_adapter.getHTTPErrorResponse)(error);
|
|
792
|
+
return new Response(JSON.stringify(body), {
|
|
793
|
+
status,
|
|
757
794
|
headers: { "Content-Type": "application/json" }
|
|
758
795
|
});
|
|
759
796
|
}
|
|
760
797
|
};
|
|
761
|
-
|
|
798
|
+
const method = route.method.toLowerCase();
|
|
799
|
+
const maxSize = route.maxBodySize ?? this.bodyLimitOptions?.maxSize;
|
|
800
|
+
if ([
|
|
801
|
+
"POST",
|
|
802
|
+
"PUT",
|
|
803
|
+
"PATCH",
|
|
804
|
+
"DELETE"
|
|
805
|
+
].includes(route.method) && maxSize !== void 0) app[method](normalizedPath, handler, { parse: createBodyLimitParser(maxSize) });
|
|
806
|
+
else app[method](normalizedPath, handler);
|
|
762
807
|
}
|
|
763
808
|
async registerCustomApiRoutes() {
|
|
764
809
|
const routes = await this.registerSchemaApiRoutes();
|
|
@@ -799,11 +844,11 @@ var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
|
|
|
799
844
|
...authError.headers
|
|
800
845
|
}
|
|
801
846
|
});
|
|
802
|
-
if (this.mastra.getServer()?.auth) {
|
|
847
|
+
if (this.mastra.getStudio?.()?.auth || this.mastra.getServer()?.auth) {
|
|
803
848
|
const hasPermission = await loadHasPermission();
|
|
804
849
|
if (hasPermission) {
|
|
805
850
|
const userPermissions = ctx.requestContext.get("userPermissions");
|
|
806
|
-
const permissionError = this.checkRoutePermission(serverRoute, userPermissions, hasPermission);
|
|
851
|
+
const permissionError = this.checkRoutePermission(serverRoute, userPermissions, hasPermission, ctx.requestContext);
|
|
807
852
|
if (permissionError) return new Response(JSON.stringify({
|
|
808
853
|
error: permissionError.error,
|
|
809
854
|
message: permissionError.message
|
|
@@ -815,9 +860,9 @@ var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
|
|
|
815
860
|
}
|
|
816
861
|
}
|
|
817
862
|
const fgaError = await (0, _mastra_server_server_adapter.checkRouteFGA)(this.mastra, serverRoute, ctx.requestContext, {
|
|
818
|
-
...matchedRoute?.params ?? {},
|
|
819
863
|
...ctx.query,
|
|
820
|
-
...typeof ctx.body === "object" && ctx.body !== null ? ctx.body : {}
|
|
864
|
+
...typeof ctx.body === "object" && ctx.body !== null ? ctx.body : {},
|
|
865
|
+
...matchedRoute?.params ?? {}
|
|
821
866
|
});
|
|
822
867
|
if (fgaError) return new Response(JSON.stringify({
|
|
823
868
|
error: fgaError.error,
|