@mastra/koa 1.5.8 → 1.5.9-alpha.2

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,177 +1,9 @@
1
1
  import { Busboy } from '@fastify/busboy';
2
2
  import { coreAuthMiddleware, findMatchingCustomRoute, isProtectedCustomRoute } from '@mastra/server/auth';
3
- import { MastraServer as MastraServer$1, checkRouteFGA, redactStreamChunk, normalizeQueryParams } from '@mastra/server/server-adapter';
3
+ import { MastraServer as MastraServer$1, isZodError, checkRouteFGA, redactStreamChunk, normalizeQueryParams } from '@mastra/server/server-adapter';
4
4
  import { RequestContext } from '@mastra/core/request-context';
5
5
 
6
6
  // src/index.ts
7
-
8
- // ../../node_modules/.pnpm/zod@4.4.3/node_modules/zod/v4/core/core.js
9
- var _a;
10
- // @__NO_SIDE_EFFECTS__
11
- function $constructor(name, initializer3, params) {
12
- function init(inst, def) {
13
- if (!inst._zod) {
14
- Object.defineProperty(inst, "_zod", {
15
- value: {
16
- def,
17
- constr: _,
18
- traits: /* @__PURE__ */ new Set()
19
- },
20
- enumerable: false
21
- });
22
- }
23
- if (inst._zod.traits.has(name)) {
24
- return;
25
- }
26
- inst._zod.traits.add(name);
27
- initializer3(inst, def);
28
- const proto = _.prototype;
29
- const keys = Object.keys(proto);
30
- for (let i = 0; i < keys.length; i++) {
31
- const k = keys[i];
32
- if (!(k in inst)) {
33
- inst[k] = proto[k].bind(inst);
34
- }
35
- }
36
- }
37
- const Parent = params?.Parent ?? Object;
38
- class Definition extends Parent {
39
- }
40
- Object.defineProperty(Definition, "name", { value: name });
41
- function _(def) {
42
- var _a2;
43
- const inst = params?.Parent ? new Definition() : this;
44
- init(inst, def);
45
- (_a2 = inst._zod).deferred ?? (_a2.deferred = []);
46
- for (const fn of inst._zod.deferred) {
47
- fn();
48
- }
49
- return inst;
50
- }
51
- Object.defineProperty(_, "init", { value: init });
52
- Object.defineProperty(_, Symbol.hasInstance, {
53
- value: (inst) => {
54
- if (params?.Parent && inst instanceof params.Parent)
55
- return true;
56
- return inst?._zod?.traits?.has(name);
57
- }
58
- });
59
- Object.defineProperty(_, "name", { value: name });
60
- return _;
61
- }
62
- (_a = globalThis).__zod_globalConfig ?? (_a.__zod_globalConfig = {});
63
-
64
- // ../../node_modules/.pnpm/zod@4.4.3/node_modules/zod/v4/core/util.js
65
- function jsonStringifyReplacer(_, value) {
66
- if (typeof value === "bigint")
67
- return value.toString();
68
- return value;
69
- }
70
-
71
- // ../../node_modules/.pnpm/zod@4.4.3/node_modules/zod/v4/core/errors.js
72
- var initializer = (inst, def) => {
73
- inst.name = "$ZodError";
74
- Object.defineProperty(inst, "_zod", {
75
- value: inst._zod,
76
- enumerable: false
77
- });
78
- Object.defineProperty(inst, "issues", {
79
- value: def,
80
- enumerable: false
81
- });
82
- inst.message = JSON.stringify(def, jsonStringifyReplacer, 2);
83
- Object.defineProperty(inst, "toString", {
84
- value: () => inst.message,
85
- enumerable: false
86
- });
87
- };
88
- var $ZodError = $constructor("$ZodError", initializer);
89
- function flattenError(error, mapper = (issue) => issue.message) {
90
- const fieldErrors = {};
91
- const formErrors = [];
92
- for (const sub of error.issues) {
93
- if (sub.path.length > 0) {
94
- fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
95
- fieldErrors[sub.path[0]].push(mapper(sub));
96
- } else {
97
- formErrors.push(mapper(sub));
98
- }
99
- }
100
- return { formErrors, fieldErrors };
101
- }
102
- function formatError(error, mapper = (issue) => issue.message) {
103
- const fieldErrors = { _errors: [] };
104
- const processError = (error2, path = []) => {
105
- for (const issue of error2.issues) {
106
- if (issue.code === "invalid_union" && issue.errors.length) {
107
- issue.errors.map((issues) => processError({ issues }, [...path, ...issue.path]));
108
- } else if (issue.code === "invalid_key") {
109
- processError({ issues: issue.issues }, [...path, ...issue.path]);
110
- } else if (issue.code === "invalid_element") {
111
- processError({ issues: issue.issues }, [...path, ...issue.path]);
112
- } else {
113
- const fullpath = [...path, ...issue.path];
114
- if (fullpath.length === 0) {
115
- fieldErrors._errors.push(mapper(issue));
116
- } else {
117
- let curr = fieldErrors;
118
- let i = 0;
119
- while (i < fullpath.length) {
120
- const el = fullpath[i];
121
- const terminal = i === fullpath.length - 1;
122
- if (!terminal) {
123
- curr[el] = curr[el] || { _errors: [] };
124
- } else {
125
- curr[el] = curr[el] || { _errors: [] };
126
- curr[el]._errors.push(mapper(issue));
127
- }
128
- curr = curr[el];
129
- i++;
130
- }
131
- }
132
- }
133
- }
134
- };
135
- processError(error);
136
- return fieldErrors;
137
- }
138
-
139
- // ../../node_modules/.pnpm/zod@4.4.3/node_modules/zod/v4/classic/errors.js
140
- var initializer2 = (inst, issues) => {
141
- $ZodError.init(inst, issues);
142
- inst.name = "ZodError";
143
- Object.defineProperties(inst, {
144
- format: {
145
- value: (mapper) => formatError(inst, mapper)
146
- // enumerable: false,
147
- },
148
- flatten: {
149
- value: (mapper) => flattenError(inst, mapper)
150
- // enumerable: false,
151
- },
152
- addIssue: {
153
- value: (issue) => {
154
- inst.issues.push(issue);
155
- inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
156
- }
157
- // enumerable: false,
158
- },
159
- addIssues: {
160
- value: (issues2) => {
161
- inst.issues.push(...issues2);
162
- inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
163
- }
164
- // enumerable: false,
165
- },
166
- isEmpty: {
167
- get() {
168
- return inst.issues.length === 0;
169
- }
170
- // enumerable: false,
171
- }
172
- });
173
- };
174
- var ZodError = /* @__PURE__ */ $constructor("ZodError", initializer2);
175
7
  function toWebRequest(ctx) {
176
8
  const protocol = ctx.protocol || "http";
177
9
  const host = ctx.host || "localhost";
@@ -494,7 +326,7 @@ var MastraServer = class extends MastraServer$1 {
494
326
  this.mastra.getLogger()?.error("Error parsing query params", {
495
327
  error: error instanceof Error ? { message: error.message, stack: error.stack } : error
496
328
  });
497
- if (error instanceof ZodError) {
329
+ if (isZodError(error)) {
498
330
  const resolved = this.resolveValidationError(route, error, "query");
499
331
  ctx.status = resolved.status;
500
332
  ctx.body = resolved.body;
@@ -515,7 +347,7 @@ var MastraServer = class extends MastraServer$1 {
515
347
  this.mastra.getLogger()?.error("Error parsing body", {
516
348
  error: error instanceof Error ? { message: error.message, stack: error.stack } : error
517
349
  });
518
- if (error instanceof ZodError) {
350
+ if (isZodError(error)) {
519
351
  const resolved = this.resolveValidationError(route, error, "body");
520
352
  ctx.status = resolved.status;
521
353
  ctx.body = resolved.body;
@@ -536,7 +368,7 @@ var MastraServer = class extends MastraServer$1 {
536
368
  this.mastra.getLogger()?.error("Error parsing path params", {
537
369
  error: error instanceof Error ? { message: error.message, stack: error.stack } : error
538
370
  });
539
- if (error instanceof ZodError) {
371
+ if (isZodError(error)) {
540
372
  const resolved = this.resolveValidationError(route, error, "path");
541
373
  ctx.status = resolved.status;
542
374
  ctx.body = resolved.body;
@@ -624,6 +456,9 @@ var MastraServer = class extends MastraServer$1 {
624
456
  ...sseHeaders,
625
457
  "Transfer-Encoding": "chunked"
626
458
  });
459
+ if (streamFormat === "sse" && route.sseFlushOnConnect) {
460
+ ctx.res.write(": connected\n\n");
461
+ }
627
462
  const readableStream = result instanceof ReadableStream ? result : result.fullStream;
628
463
  const reader = readableStream.getReader();
629
464
  ctx.res.on("close", () => {