@mastra/hono 0.0.0-scorers-logs-20251208123838 → 0.0.0-standard-schema-20260123120255

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.js CHANGED
@@ -1,18 +1,29 @@
1
- import { MastraServer as MastraServer$1, redactStreamChunk } from '@mastra/server/server-adapter';
1
+ import { formatZodError } from '@mastra/server/handlers/error';
2
+ import { MastraServer as MastraServer$1, normalizeQueryParams, redactStreamChunk } from '@mastra/server/server-adapter';
2
3
  import { toReqRes, toFetchResponse } from 'fetch-to-node';
3
4
  import { isDevPlaygroundRequest, isProtectedPath, canAccessPublicly, checkRules, defaultAuthConfig } from '@mastra/server/auth';
4
5
 
5
6
  // src/index.ts
6
7
 
7
- // ../../node_modules/.pnpm/hono@4.10.6/node_modules/hono/dist/http-exception.js
8
+ // ../../node_modules/.pnpm/hono@4.11.3/node_modules/hono/dist/http-exception.js
8
9
  var HTTPException = class extends Error {
9
10
  res;
10
11
  status;
12
+ /**
13
+ * Creates an instance of `HTTPException`.
14
+ * @param status - HTTP status code for the exception. Defaults to 500.
15
+ * @param options - Additional options for the exception.
16
+ */
11
17
  constructor(status = 500, options) {
12
18
  super(options?.message, { cause: options?.cause });
13
19
  this.res = options?.res;
14
20
  this.status = status;
15
21
  }
22
+ /**
23
+ * Returns the response object associated with the exception.
24
+ * If a response object is not provided, a new response is created with the error message and status code.
25
+ * @returns The response object.
26
+ */
16
27
  getResponse() {
17
28
  if (this.res) {
18
29
  const newResponse = new Response(this.res.body, {
@@ -27,7 +38,7 @@ var HTTPException = class extends Error {
27
38
  }
28
39
  };
29
40
 
30
- // ../../node_modules/.pnpm/hono@4.10.6/node_modules/hono/dist/middleware/body-limit/index.js
41
+ // ../../node_modules/.pnpm/hono@4.11.3/node_modules/hono/dist/middleware/body-limit/index.js
31
42
  var ERROR_MESSAGE = "Payload Too Large";
32
43
  var BodyLimitError = class extends Error {
33
44
  constructor(message) {
@@ -84,14 +95,20 @@ var bodyLimit = (options) => {
84
95
  };
85
96
  };
86
97
 
87
- // ../../node_modules/.pnpm/hono@4.10.6/node_modules/hono/dist/utils/stream.js
98
+ // ../../node_modules/.pnpm/hono@4.11.3/node_modules/hono/dist/utils/stream.js
88
99
  var StreamingApi = class {
89
100
  writer;
90
101
  encoder;
91
102
  writable;
92
103
  abortSubscribers = [];
93
104
  responseReadable;
105
+ /**
106
+ * Whether the stream has been aborted.
107
+ */
94
108
  aborted = false;
109
+ /**
110
+ * Whether the stream has been closed normally.
111
+ */
95
112
  closed = false;
96
113
  constructor(writable, _readable) {
97
114
  this.writable = writable;
@@ -143,6 +160,10 @@ var StreamingApi = class {
143
160
  onAbort(listener) {
144
161
  this.abortSubscribers.push(listener);
145
162
  }
163
+ /**
164
+ * Abort the stream.
165
+ * You can call this method when stream is aborted by external event.
166
+ */
146
167
  abort() {
147
168
  if (!this.aborted) {
148
169
  this.aborted = true;
@@ -151,7 +172,7 @@ var StreamingApi = class {
151
172
  }
152
173
  };
153
174
 
154
- // ../../node_modules/.pnpm/hono@4.10.6/node_modules/hono/dist/helper/streaming/utils.js
175
+ // ../../node_modules/.pnpm/hono@4.11.3/node_modules/hono/dist/helper/streaming/utils.js
155
176
  var isOldBunVersion = () => {
156
177
  const version = typeof Bun !== "undefined" ? Bun.version : void 0;
157
178
  if (version === void 0) {
@@ -162,7 +183,7 @@ var isOldBunVersion = () => {
162
183
  return result;
163
184
  };
164
185
 
165
- // ../../node_modules/.pnpm/hono@4.10.6/node_modules/hono/dist/helper/streaming/stream.js
186
+ // ../../node_modules/.pnpm/hono@4.11.3/node_modules/hono/dist/helper/streaming/stream.js
166
187
  var contextStash = /* @__PURE__ */ new WeakMap();
167
188
  var stream = (c, cb, onError) => {
168
189
  const { readable, writable } = new TransformStream();
@@ -190,6 +211,213 @@ var stream = (c, cb, onError) => {
190
211
  })();
191
212
  return c.newResponse(stream2.responseReadable);
192
213
  };
214
+
215
+ // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/helpers/util.js
216
+ var util;
217
+ (function(util2) {
218
+ util2.assertEqual = (_) => {
219
+ };
220
+ function assertIs(_arg) {
221
+ }
222
+ util2.assertIs = assertIs;
223
+ function assertNever(_x) {
224
+ throw new Error();
225
+ }
226
+ util2.assertNever = assertNever;
227
+ util2.arrayToEnum = (items) => {
228
+ const obj = {};
229
+ for (const item of items) {
230
+ obj[item] = item;
231
+ }
232
+ return obj;
233
+ };
234
+ util2.getValidEnumValues = (obj) => {
235
+ const validKeys = util2.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
236
+ const filtered = {};
237
+ for (const k of validKeys) {
238
+ filtered[k] = obj[k];
239
+ }
240
+ return util2.objectValues(filtered);
241
+ };
242
+ util2.objectValues = (obj) => {
243
+ return util2.objectKeys(obj).map(function(e) {
244
+ return obj[e];
245
+ });
246
+ };
247
+ util2.objectKeys = typeof Object.keys === "function" ? (obj) => Object.keys(obj) : (object) => {
248
+ const keys = [];
249
+ for (const key in object) {
250
+ if (Object.prototype.hasOwnProperty.call(object, key)) {
251
+ keys.push(key);
252
+ }
253
+ }
254
+ return keys;
255
+ };
256
+ util2.find = (arr, checker) => {
257
+ for (const item of arr) {
258
+ if (checker(item))
259
+ return item;
260
+ }
261
+ return void 0;
262
+ };
263
+ util2.isInteger = typeof Number.isInteger === "function" ? (val) => Number.isInteger(val) : (val) => typeof val === "number" && Number.isFinite(val) && Math.floor(val) === val;
264
+ function joinValues(array, separator = " | ") {
265
+ return array.map((val) => typeof val === "string" ? `'${val}'` : val).join(separator);
266
+ }
267
+ util2.joinValues = joinValues;
268
+ util2.jsonStringifyReplacer = (_, value) => {
269
+ if (typeof value === "bigint") {
270
+ return value.toString();
271
+ }
272
+ return value;
273
+ };
274
+ })(util || (util = {}));
275
+ var objectUtil;
276
+ (function(objectUtil2) {
277
+ objectUtil2.mergeShapes = (first, second) => {
278
+ return {
279
+ ...first,
280
+ ...second
281
+ // second overwrites first
282
+ };
283
+ };
284
+ })(objectUtil || (objectUtil = {}));
285
+ util.arrayToEnum([
286
+ "string",
287
+ "nan",
288
+ "number",
289
+ "integer",
290
+ "float",
291
+ "boolean",
292
+ "date",
293
+ "bigint",
294
+ "symbol",
295
+ "function",
296
+ "undefined",
297
+ "null",
298
+ "array",
299
+ "object",
300
+ "unknown",
301
+ "promise",
302
+ "void",
303
+ "never",
304
+ "map",
305
+ "set"
306
+ ]);
307
+
308
+ // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/ZodError.js
309
+ util.arrayToEnum([
310
+ "invalid_type",
311
+ "invalid_literal",
312
+ "custom",
313
+ "invalid_union",
314
+ "invalid_union_discriminator",
315
+ "invalid_enum_value",
316
+ "unrecognized_keys",
317
+ "invalid_arguments",
318
+ "invalid_return_type",
319
+ "invalid_date",
320
+ "invalid_string",
321
+ "too_small",
322
+ "too_big",
323
+ "invalid_intersection_types",
324
+ "not_multiple_of",
325
+ "not_finite"
326
+ ]);
327
+ var ZodError = class _ZodError extends Error {
328
+ get errors() {
329
+ return this.issues;
330
+ }
331
+ constructor(issues) {
332
+ super();
333
+ this.issues = [];
334
+ this.addIssue = (sub) => {
335
+ this.issues = [...this.issues, sub];
336
+ };
337
+ this.addIssues = (subs = []) => {
338
+ this.issues = [...this.issues, ...subs];
339
+ };
340
+ const actualProto = new.target.prototype;
341
+ if (Object.setPrototypeOf) {
342
+ Object.setPrototypeOf(this, actualProto);
343
+ } else {
344
+ this.__proto__ = actualProto;
345
+ }
346
+ this.name = "ZodError";
347
+ this.issues = issues;
348
+ }
349
+ format(_mapper) {
350
+ const mapper = _mapper || function(issue) {
351
+ return issue.message;
352
+ };
353
+ const fieldErrors = { _errors: [] };
354
+ const processError = (error) => {
355
+ for (const issue of error.issues) {
356
+ if (issue.code === "invalid_union") {
357
+ issue.unionErrors.map(processError);
358
+ } else if (issue.code === "invalid_return_type") {
359
+ processError(issue.returnTypeError);
360
+ } else if (issue.code === "invalid_arguments") {
361
+ processError(issue.argumentsError);
362
+ } else if (issue.path.length === 0) {
363
+ fieldErrors._errors.push(mapper(issue));
364
+ } else {
365
+ let curr = fieldErrors;
366
+ let i = 0;
367
+ while (i < issue.path.length) {
368
+ const el = issue.path[i];
369
+ const terminal = i === issue.path.length - 1;
370
+ if (!terminal) {
371
+ curr[el] = curr[el] || { _errors: [] };
372
+ } else {
373
+ curr[el] = curr[el] || { _errors: [] };
374
+ curr[el]._errors.push(mapper(issue));
375
+ }
376
+ curr = curr[el];
377
+ i++;
378
+ }
379
+ }
380
+ }
381
+ };
382
+ processError(this);
383
+ return fieldErrors;
384
+ }
385
+ static assert(value) {
386
+ if (!(value instanceof _ZodError)) {
387
+ throw new Error(`Not a ZodError: ${value}`);
388
+ }
389
+ }
390
+ toString() {
391
+ return this.message;
392
+ }
393
+ get message() {
394
+ return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
395
+ }
396
+ get isEmpty() {
397
+ return this.issues.length === 0;
398
+ }
399
+ flatten(mapper = (issue) => issue.message) {
400
+ const fieldErrors = {};
401
+ const formErrors = [];
402
+ for (const sub of this.issues) {
403
+ if (sub.path.length > 0) {
404
+ const firstEl = sub.path[0];
405
+ fieldErrors[firstEl] = fieldErrors[firstEl] || [];
406
+ fieldErrors[firstEl].push(mapper(sub));
407
+ } else {
408
+ formErrors.push(mapper(sub));
409
+ }
410
+ }
411
+ return { formErrors, fieldErrors };
412
+ }
413
+ get formErrors() {
414
+ return this.flatten();
415
+ }
416
+ };
417
+ ZodError.create = (issues) => {
418
+ const error = new ZodError(issues);
419
+ return error;
420
+ };
193
421
  var authenticationMiddleware = async (c, next) => {
194
422
  const mastra = c.get("mastra");
195
423
  const authConfig = mastra.getServer()?.auth;
@@ -333,10 +561,8 @@ var MastraServer = class extends MastraServer$1 {
333
561
  const requestContext = this.mergeRequestContext({ paramsRequestContext, bodyRequestContext });
334
562
  c.set("requestContext", requestContext);
335
563
  c.set("mastra", this.mastra);
336
- c.set("tools", this.tools || {});
564
+ c.set("registeredTools", this.tools || {});
337
565
  c.set("taskStore", this.taskStore);
338
- c.set("playground", this.playground === true);
339
- c.set("isDev", this.isDev === true);
340
566
  c.set("abortSignal", c.req.raw.signal);
341
567
  c.set("customRouteAuthConfig", this.customRouteAuthConfig);
342
568
  return next();
@@ -384,16 +610,51 @@ var MastraServer = class extends MastraServer$1 {
384
610
  }
385
611
  async getParams(route, request) {
386
612
  const urlParams = request.param();
387
- const queryParams = request.query();
613
+ const queryParams = normalizeQueryParams(request.queries());
388
614
  let body;
389
615
  if (route.method === "POST" || route.method === "PUT" || route.method === "PATCH") {
390
- try {
391
- body = await request.json();
392
- } catch {
616
+ const contentType = request.header("content-type") || "";
617
+ if (contentType.includes("multipart/form-data")) {
618
+ try {
619
+ const formData = await request.formData();
620
+ body = await this.parseFormData(formData);
621
+ } catch (error) {
622
+ console.error("Failed to parse multipart form data:", error);
623
+ if (error instanceof Error && error.message.toLowerCase().includes("size")) {
624
+ throw error;
625
+ }
626
+ }
627
+ } else {
628
+ try {
629
+ body = await request.json();
630
+ } catch (error) {
631
+ console.error("Failed to parse JSON body:", error);
632
+ }
393
633
  }
394
634
  }
395
635
  return { urlParams, queryParams, body };
396
636
  }
637
+ /**
638
+ * Parse FormData into a plain object, converting File objects to Buffers.
639
+ */
640
+ async parseFormData(formData) {
641
+ const result = {};
642
+ for (const [key, value] of formData.entries()) {
643
+ if (value instanceof File) {
644
+ const arrayBuffer = await value.arrayBuffer();
645
+ result[key] = Buffer.from(arrayBuffer);
646
+ } else if (typeof value === "string") {
647
+ try {
648
+ result[key] = JSON.parse(value);
649
+ } catch {
650
+ result[key] = value;
651
+ }
652
+ } else {
653
+ result[key] = value;
654
+ }
655
+ }
656
+ return result;
657
+ }
397
658
  async sendResponse(route, response, result) {
398
659
  if (route.responseType === "json") {
399
660
  return response.json(result, 200);
@@ -408,7 +669,7 @@ var MastraServer = class extends MastraServer$1 {
408
669
  try {
409
670
  await server.startHTTP({
410
671
  url: new URL(response.req.url),
411
- httpPath,
672
+ httpPath: `${this.prefix ?? ""}${httpPath}`,
412
673
  req,
413
674
  res
414
675
  });
@@ -432,8 +693,8 @@ var MastraServer = class extends MastraServer$1 {
432
693
  try {
433
694
  return await server.startHonoSSE({
434
695
  url: new URL(response.req.url),
435
- ssePath,
436
- messagePath,
696
+ ssePath: `${this.prefix ?? ""}${ssePath}`,
697
+ messagePath: `${this.prefix ?? ""}${messagePath}`,
437
698
  context: response
438
699
  });
439
700
  } catch {
@@ -459,16 +720,29 @@ var MastraServer = class extends MastraServer$1 {
459
720
  `${prefix}${route.path}`,
460
721
  ...middlewares,
461
722
  async (c) => {
723
+ const authError = await this.checkRouteAuth(route, {
724
+ path: c.req.path,
725
+ method: c.req.method,
726
+ getHeader: (name) => c.req.header(name),
727
+ getQuery: (name) => c.req.query(name),
728
+ requestContext: c.get("requestContext")
729
+ });
730
+ if (authError) {
731
+ return c.json({ error: authError.error }, authError.status);
732
+ }
462
733
  const params = await this.getParams(route, c.req);
463
734
  if (params.queryParams) {
464
735
  try {
465
736
  params.queryParams = await this.parseQueryParams(route, params.queryParams);
466
737
  } catch (error) {
467
738
  console.error("Error parsing query params", error);
739
+ if (error instanceof ZodError) {
740
+ return c.json(formatZodError(error, "query parameters"), 400);
741
+ }
468
742
  return c.json(
469
743
  {
470
744
  error: "Invalid query parameters",
471
- details: error instanceof Error ? error.message : "Unknown error"
745
+ issues: [{ field: "unknown", message: error instanceof Error ? error.message : "Unknown error" }]
472
746
  },
473
747
  400
474
748
  );
@@ -479,10 +753,13 @@ var MastraServer = class extends MastraServer$1 {
479
753
  params.body = await this.parseBody(route, params.body);
480
754
  } catch (error) {
481
755
  console.error("Error parsing body:", error instanceof Error ? error.message : String(error));
756
+ if (error instanceof ZodError) {
757
+ return c.json(formatZodError(error, "request body"), 400);
758
+ }
482
759
  return c.json(
483
760
  {
484
761
  error: "Invalid request body",
485
- details: error instanceof Error ? error.message : "Unknown error"
762
+ issues: [{ field: "unknown", message: error instanceof Error ? error.message : "Unknown error" }]
486
763
  },
487
764
  400
488
765
  );
@@ -494,9 +771,10 @@ var MastraServer = class extends MastraServer$1 {
494
771
  ...typeof params.body === "object" ? params.body : {},
495
772
  requestContext: c.get("requestContext"),
496
773
  mastra: this.mastra,
497
- tools: c.get("tools"),
774
+ registeredTools: c.get("registeredTools"),
498
775
  taskStore: c.get("taskStore"),
499
- abortSignal: c.get("abortSignal")
776
+ abortSignal: c.get("abortSignal"),
777
+ routePrefix: this.prefix
500
778
  };
501
779
  try {
502
780
  const result = await route.handler(handlerParams);