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