@mastra/fastify 1.3.25 → 1.3.26-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
- import { MastraServer as MastraServer$1, redactStreamChunk, normalizeQueryParams, checkRouteFGA } from '@mastra/server/server-adapter';
2
+ import { MastraServer as MastraServer$1, redactStreamChunk, normalizeQueryParams, isZodError, checkRouteFGA } from '@mastra/server/server-adapter';
3
3
  import { RequestContext } from '@mastra/core/request-context';
4
4
  import { coreAuthMiddleware } from '@mastra/server/auth';
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(request) {
176
8
  const protocol = request.protocol || "http";
177
9
  const host = request.headers.host || "localhost";
@@ -352,6 +184,9 @@ var MastraServer = class extends MastraServer$1 {
352
184
  ...sseHeaders,
353
185
  "Transfer-Encoding": "chunked"
354
186
  });
187
+ if (streamFormat === "sse" && route.sseFlushOnConnect) {
188
+ reply.raw.write(": connected\n\n");
189
+ }
355
190
  const readableStream = result instanceof ReadableStream ? result : result.fullStream;
356
191
  const reader = readableStream.getReader();
357
192
  let readerCanceled = false;
@@ -373,6 +208,10 @@ var MastraServer = class extends MastraServer$1 {
373
208
  const { done, value } = await reader.read();
374
209
  if (done) break;
375
210
  if (value) {
211
+ if (streamFormat === "sse" && typeof value === "string" && value.startsWith(":")) {
212
+ reply.raw.write(value);
213
+ continue;
214
+ }
376
215
  const shouldRedact = this.streamOptions?.redact ?? true;
377
216
  const outputValue = shouldRedact ? redactStreamChunk(value) : value;
378
217
  if (streamFormat === "sse") {
@@ -636,7 +475,7 @@ var MastraServer = class extends MastraServer$1 {
636
475
  this.mastra.getLogger()?.error("Error parsing query params", {
637
476
  error: error instanceof Error ? { message: error.message, stack: error.stack } : error
638
477
  });
639
- if (error instanceof ZodError) {
478
+ if (isZodError(error)) {
640
479
  const { status, body } = this.resolveValidationError(route, error, "query");
641
480
  return reply.status(status).send(body);
642
481
  }
@@ -653,7 +492,7 @@ var MastraServer = class extends MastraServer$1 {
653
492
  this.mastra.getLogger()?.error("Error parsing body", {
654
493
  error: error instanceof Error ? { message: error.message, stack: error.stack } : error
655
494
  });
656
- if (error instanceof ZodError) {
495
+ if (isZodError(error)) {
657
496
  const { status, body } = this.resolveValidationError(route, error, "body");
658
497
  return reply.status(status).send(body);
659
498
  }
@@ -670,7 +509,7 @@ var MastraServer = class extends MastraServer$1 {
670
509
  this.mastra.getLogger()?.error("Error parsing path params", {
671
510
  error: error instanceof Error ? { message: error.message, stack: error.stack } : error
672
511
  });
673
- if (error instanceof ZodError) {
512
+ if (isZodError(error)) {
674
513
  const { status, body } = this.resolveValidationError(route, error, "path");
675
514
  return reply.status(status).send(body);
676
515
  }