@mastra/fastify 1.1.9 → 1.2.0-alpha.1
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/CHANGELOG.md +38 -0
- package/dist/index.cjs +208 -205
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +196 -193
- package/dist/index.js.map +1 -1
- package/package.json +11 -11
package/dist/index.js
CHANGED
|
@@ -1,215 +1,215 @@
|
|
|
1
1
|
import { Busboy } from '@fastify/busboy';
|
|
2
|
-
import { formatZodError } from '@mastra/server/handlers/error';
|
|
3
2
|
import { MastraServer as MastraServer$1, redactStreamChunk, normalizeQueryParams } from '@mastra/server/server-adapter';
|
|
4
3
|
|
|
5
4
|
// src/index.ts
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
util2.assertNever = assertNever;
|
|
19
|
-
util2.arrayToEnum = (items) => {
|
|
20
|
-
const obj = {};
|
|
21
|
-
for (const item of items) {
|
|
22
|
-
obj[item] = item;
|
|
5
|
+
// @__NO_SIDE_EFFECTS__
|
|
6
|
+
function $constructor(name, initializer3, params) {
|
|
7
|
+
function init(inst, def) {
|
|
8
|
+
if (!inst._zod) {
|
|
9
|
+
Object.defineProperty(inst, "_zod", {
|
|
10
|
+
value: {
|
|
11
|
+
def,
|
|
12
|
+
constr: _,
|
|
13
|
+
traits: /* @__PURE__ */ new Set()
|
|
14
|
+
},
|
|
15
|
+
enumerable: false
|
|
16
|
+
});
|
|
23
17
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
util2.getValidEnumValues = (obj) => {
|
|
27
|
-
const validKeys = util2.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
28
|
-
const filtered = {};
|
|
29
|
-
for (const k of validKeys) {
|
|
30
|
-
filtered[k] = obj[k];
|
|
18
|
+
if (inst._zod.traits.has(name)) {
|
|
19
|
+
return;
|
|
31
20
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const keys = [];
|
|
41
|
-
for (const key in object) {
|
|
42
|
-
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
43
|
-
keys.push(key);
|
|
21
|
+
inst._zod.traits.add(name);
|
|
22
|
+
initializer3(inst, def);
|
|
23
|
+
const proto = _.prototype;
|
|
24
|
+
const keys = Object.keys(proto);
|
|
25
|
+
for (let i = 0; i < keys.length; i++) {
|
|
26
|
+
const k = keys[i];
|
|
27
|
+
if (!(k in inst)) {
|
|
28
|
+
inst[k] = proto[k].bind(inst);
|
|
44
29
|
}
|
|
45
30
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
31
|
+
}
|
|
32
|
+
const Parent = params?.Parent ?? Object;
|
|
33
|
+
class Definition extends Parent {
|
|
34
|
+
}
|
|
35
|
+
Object.defineProperty(Definition, "name", { value: name });
|
|
36
|
+
function _(def) {
|
|
37
|
+
var _a2;
|
|
38
|
+
const inst = params?.Parent ? new Definition() : this;
|
|
39
|
+
init(inst, def);
|
|
40
|
+
(_a2 = inst._zod).deferred ?? (_a2.deferred = []);
|
|
41
|
+
for (const fn of inst._zod.deferred) {
|
|
42
|
+
fn();
|
|
52
43
|
}
|
|
53
|
-
return
|
|
54
|
-
};
|
|
55
|
-
util2.isInteger = typeof Number.isInteger === "function" ? (val) => Number.isInteger(val) : (val) => typeof val === "number" && Number.isFinite(val) && Math.floor(val) === val;
|
|
56
|
-
function joinValues(array, separator = " | ") {
|
|
57
|
-
return array.map((val) => typeof val === "string" ? `'${val}'` : val).join(separator);
|
|
44
|
+
return inst;
|
|
58
45
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
46
|
+
Object.defineProperty(_, "init", { value: init });
|
|
47
|
+
Object.defineProperty(_, Symbol.hasInstance, {
|
|
48
|
+
value: (inst) => {
|
|
49
|
+
if (params?.Parent && inst instanceof params.Parent)
|
|
50
|
+
return true;
|
|
51
|
+
return inst?._zod?.traits?.has(name);
|
|
63
52
|
}
|
|
64
|
-
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
(function(objectUtil2) {
|
|
69
|
-
objectUtil2.mergeShapes = (first, second) => {
|
|
70
|
-
return {
|
|
71
|
-
...first,
|
|
72
|
-
...second
|
|
73
|
-
// second overwrites first
|
|
74
|
-
};
|
|
75
|
-
};
|
|
76
|
-
})(objectUtil || (objectUtil = {}));
|
|
77
|
-
util.arrayToEnum([
|
|
78
|
-
"string",
|
|
79
|
-
"nan",
|
|
80
|
-
"number",
|
|
81
|
-
"integer",
|
|
82
|
-
"float",
|
|
83
|
-
"boolean",
|
|
84
|
-
"date",
|
|
85
|
-
"bigint",
|
|
86
|
-
"symbol",
|
|
87
|
-
"function",
|
|
88
|
-
"undefined",
|
|
89
|
-
"null",
|
|
90
|
-
"array",
|
|
91
|
-
"object",
|
|
92
|
-
"unknown",
|
|
93
|
-
"promise",
|
|
94
|
-
"void",
|
|
95
|
-
"never",
|
|
96
|
-
"map",
|
|
97
|
-
"set"
|
|
98
|
-
]);
|
|
53
|
+
});
|
|
54
|
+
Object.defineProperty(_, "name", { value: name });
|
|
55
|
+
return _;
|
|
56
|
+
}
|
|
99
57
|
|
|
100
|
-
// ../../node_modules/.pnpm/zod@3.
|
|
101
|
-
|
|
102
|
-
"
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
"
|
|
110
|
-
"
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
"
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const actualProto = new.target.prototype;
|
|
133
|
-
if (Object.setPrototypeOf) {
|
|
134
|
-
Object.setPrototypeOf(this, actualProto);
|
|
58
|
+
// ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/core/util.js
|
|
59
|
+
function jsonStringifyReplacer(_, value) {
|
|
60
|
+
if (typeof value === "bigint")
|
|
61
|
+
return value.toString();
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/core/errors.js
|
|
66
|
+
var initializer = (inst, def) => {
|
|
67
|
+
inst.name = "$ZodError";
|
|
68
|
+
Object.defineProperty(inst, "_zod", {
|
|
69
|
+
value: inst._zod,
|
|
70
|
+
enumerable: false
|
|
71
|
+
});
|
|
72
|
+
Object.defineProperty(inst, "issues", {
|
|
73
|
+
value: def,
|
|
74
|
+
enumerable: false
|
|
75
|
+
});
|
|
76
|
+
inst.message = JSON.stringify(def, jsonStringifyReplacer, 2);
|
|
77
|
+
Object.defineProperty(inst, "toString", {
|
|
78
|
+
value: () => inst.message,
|
|
79
|
+
enumerable: false
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
var $ZodError = $constructor("$ZodError", initializer);
|
|
83
|
+
function flattenError(error, mapper = (issue2) => issue2.message) {
|
|
84
|
+
const fieldErrors = {};
|
|
85
|
+
const formErrors = [];
|
|
86
|
+
for (const sub of error.issues) {
|
|
87
|
+
if (sub.path.length > 0) {
|
|
88
|
+
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
89
|
+
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
135
90
|
} else {
|
|
136
|
-
|
|
91
|
+
formErrors.push(mapper(sub));
|
|
137
92
|
}
|
|
138
|
-
this.name = "ZodError";
|
|
139
|
-
this.issues = issues;
|
|
140
93
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
curr[el]._errors.push(mapper(issue));
|
|
167
|
-
}
|
|
168
|
-
curr = curr[el];
|
|
169
|
-
i++;
|
|
94
|
+
return { formErrors, fieldErrors };
|
|
95
|
+
}
|
|
96
|
+
function formatError(error, mapper = (issue2) => issue2.message) {
|
|
97
|
+
const fieldErrors = { _errors: [] };
|
|
98
|
+
const processError = (error2) => {
|
|
99
|
+
for (const issue2 of error2.issues) {
|
|
100
|
+
if (issue2.code === "invalid_union" && issue2.errors.length) {
|
|
101
|
+
issue2.errors.map((issues) => processError({ issues }));
|
|
102
|
+
} else if (issue2.code === "invalid_key") {
|
|
103
|
+
processError({ issues: issue2.issues });
|
|
104
|
+
} else if (issue2.code === "invalid_element") {
|
|
105
|
+
processError({ issues: issue2.issues });
|
|
106
|
+
} else if (issue2.path.length === 0) {
|
|
107
|
+
fieldErrors._errors.push(mapper(issue2));
|
|
108
|
+
} else {
|
|
109
|
+
let curr = fieldErrors;
|
|
110
|
+
let i = 0;
|
|
111
|
+
while (i < issue2.path.length) {
|
|
112
|
+
const el = issue2.path[i];
|
|
113
|
+
const terminal = i === issue2.path.length - 1;
|
|
114
|
+
if (!terminal) {
|
|
115
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
116
|
+
} else {
|
|
117
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
118
|
+
curr[el]._errors.push(mapper(issue2));
|
|
170
119
|
}
|
|
120
|
+
curr = curr[el];
|
|
121
|
+
i++;
|
|
171
122
|
}
|
|
172
123
|
}
|
|
173
|
-
};
|
|
174
|
-
processError(this);
|
|
175
|
-
return fieldErrors;
|
|
176
|
-
}
|
|
177
|
-
static assert(value) {
|
|
178
|
-
if (!(value instanceof _ZodError)) {
|
|
179
|
-
throw new Error(`Not a ZodError: ${value}`);
|
|
180
124
|
}
|
|
125
|
+
};
|
|
126
|
+
processError(error);
|
|
127
|
+
return fieldErrors;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/core/registries.js
|
|
131
|
+
var _a;
|
|
132
|
+
var $ZodRegistry = class {
|
|
133
|
+
constructor() {
|
|
134
|
+
this._map = /* @__PURE__ */ new WeakMap();
|
|
135
|
+
this._idmap = /* @__PURE__ */ new Map();
|
|
181
136
|
}
|
|
182
|
-
|
|
183
|
-
|
|
137
|
+
add(schema, ..._meta) {
|
|
138
|
+
const meta = _meta[0];
|
|
139
|
+
this._map.set(schema, meta);
|
|
140
|
+
if (meta && typeof meta === "object" && "id" in meta) {
|
|
141
|
+
this._idmap.set(meta.id, schema);
|
|
142
|
+
}
|
|
143
|
+
return this;
|
|
184
144
|
}
|
|
185
|
-
|
|
186
|
-
|
|
145
|
+
clear() {
|
|
146
|
+
this._map = /* @__PURE__ */ new WeakMap();
|
|
147
|
+
this._idmap = /* @__PURE__ */ new Map();
|
|
148
|
+
return this;
|
|
187
149
|
}
|
|
188
|
-
|
|
189
|
-
|
|
150
|
+
remove(schema) {
|
|
151
|
+
const meta = this._map.get(schema);
|
|
152
|
+
if (meta && typeof meta === "object" && "id" in meta) {
|
|
153
|
+
this._idmap.delete(meta.id);
|
|
154
|
+
}
|
|
155
|
+
this._map.delete(schema);
|
|
156
|
+
return this;
|
|
190
157
|
}
|
|
191
|
-
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
fieldErrors[firstEl].push(mapper(sub));
|
|
199
|
-
} else {
|
|
200
|
-
formErrors.push(mapper(sub));
|
|
201
|
-
}
|
|
158
|
+
get(schema) {
|
|
159
|
+
const p = schema._zod.parent;
|
|
160
|
+
if (p) {
|
|
161
|
+
const pm = { ...this.get(p) ?? {} };
|
|
162
|
+
delete pm.id;
|
|
163
|
+
const f = { ...pm, ...this._map.get(schema) };
|
|
164
|
+
return Object.keys(f).length ? f : void 0;
|
|
202
165
|
}
|
|
203
|
-
return
|
|
166
|
+
return this._map.get(schema);
|
|
204
167
|
}
|
|
205
|
-
|
|
206
|
-
return this.
|
|
168
|
+
has(schema) {
|
|
169
|
+
return this._map.has(schema);
|
|
207
170
|
}
|
|
208
171
|
};
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
172
|
+
function registry() {
|
|
173
|
+
return new $ZodRegistry();
|
|
174
|
+
}
|
|
175
|
+
(_a = globalThis).__zod_globalRegistry ?? (_a.__zod_globalRegistry = registry());
|
|
176
|
+
|
|
177
|
+
// ../../node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/classic/errors.js
|
|
178
|
+
var initializer2 = (inst, issues) => {
|
|
179
|
+
$ZodError.init(inst, issues);
|
|
180
|
+
inst.name = "ZodError";
|
|
181
|
+
Object.defineProperties(inst, {
|
|
182
|
+
format: {
|
|
183
|
+
value: (mapper) => formatError(inst, mapper)
|
|
184
|
+
// enumerable: false,
|
|
185
|
+
},
|
|
186
|
+
flatten: {
|
|
187
|
+
value: (mapper) => flattenError(inst, mapper)
|
|
188
|
+
// enumerable: false,
|
|
189
|
+
},
|
|
190
|
+
addIssue: {
|
|
191
|
+
value: (issue2) => {
|
|
192
|
+
inst.issues.push(issue2);
|
|
193
|
+
inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
|
|
194
|
+
}
|
|
195
|
+
// enumerable: false,
|
|
196
|
+
},
|
|
197
|
+
addIssues: {
|
|
198
|
+
value: (issues2) => {
|
|
199
|
+
inst.issues.push(...issues2);
|
|
200
|
+
inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
|
|
201
|
+
}
|
|
202
|
+
// enumerable: false,
|
|
203
|
+
},
|
|
204
|
+
isEmpty: {
|
|
205
|
+
get() {
|
|
206
|
+
return inst.issues.length === 0;
|
|
207
|
+
}
|
|
208
|
+
// enumerable: false,
|
|
209
|
+
}
|
|
210
|
+
});
|
|
212
211
|
};
|
|
212
|
+
var ZodError = $constructor("ZodError", initializer2);
|
|
213
213
|
|
|
214
214
|
// src/index.ts
|
|
215
215
|
var _hasPermissionPromise;
|
|
@@ -538,7 +538,8 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
538
538
|
error: error instanceof Error ? { message: error.message, stack: error.stack } : error
|
|
539
539
|
});
|
|
540
540
|
if (error instanceof ZodError) {
|
|
541
|
-
|
|
541
|
+
const { status, body } = this.resolveValidationError(route, error, "query");
|
|
542
|
+
return reply.status(status).send(body);
|
|
542
543
|
}
|
|
543
544
|
return reply.status(400).send({
|
|
544
545
|
error: "Invalid query parameters",
|
|
@@ -554,7 +555,8 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
554
555
|
error: error instanceof Error ? { message: error.message, stack: error.stack } : error
|
|
555
556
|
});
|
|
556
557
|
if (error instanceof ZodError) {
|
|
557
|
-
|
|
558
|
+
const { status, body } = this.resolveValidationError(route, error, "body");
|
|
559
|
+
return reply.status(status).send(body);
|
|
558
560
|
}
|
|
559
561
|
return reply.status(400).send({
|
|
560
562
|
error: "Invalid request body",
|
|
@@ -570,7 +572,8 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
570
572
|
error: error instanceof Error ? { message: error.message, stack: error.stack } : error
|
|
571
573
|
});
|
|
572
574
|
if (error instanceof ZodError) {
|
|
573
|
-
|
|
575
|
+
const { status, body } = this.resolveValidationError(route, error, "path");
|
|
576
|
+
return reply.status(status).send(body);
|
|
574
577
|
}
|
|
575
578
|
return reply.status(400).send({
|
|
576
579
|
error: "Invalid path parameters",
|
|
@@ -625,7 +628,7 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
625
628
|
};
|
|
626
629
|
const shouldApplyBodyLimit = this.bodyLimitOptions && ["POST", "PUT", "PATCH"].includes(route.method.toUpperCase());
|
|
627
630
|
const maxSize = route.maxBodySize ?? this.bodyLimitOptions?.maxSize;
|
|
628
|
-
const
|
|
631
|
+
const config2 = shouldApplyBodyLimit && maxSize ? { bodyLimit: maxSize } : void 0;
|
|
629
632
|
if (route.method.toUpperCase() === "ALL") {
|
|
630
633
|
const methods = ["GET", "POST", "PUT", "DELETE", "PATCH"];
|
|
631
634
|
for (const method of methods) {
|
|
@@ -634,7 +637,7 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
634
637
|
method,
|
|
635
638
|
url: fastifyPath,
|
|
636
639
|
handler,
|
|
637
|
-
config
|
|
640
|
+
config: config2
|
|
638
641
|
});
|
|
639
642
|
} catch (err) {
|
|
640
643
|
if (err instanceof Error && err.message.includes("already declared")) {
|
|
@@ -648,7 +651,7 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
648
651
|
method: route.method,
|
|
649
652
|
url: fastifyPath,
|
|
650
653
|
handler,
|
|
651
|
-
config
|
|
654
|
+
config: config2
|
|
652
655
|
});
|
|
653
656
|
}
|
|
654
657
|
}
|
|
@@ -760,14 +763,14 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
760
763
|
const method = request.method;
|
|
761
764
|
const path = urlPath;
|
|
762
765
|
reply.raw.once("finish", () => {
|
|
763
|
-
const
|
|
766
|
+
const duration2 = Date.now() - start;
|
|
764
767
|
const status = reply.statusCode;
|
|
765
768
|
const level = this.httpLoggingConfig?.level || "info";
|
|
766
769
|
const logData = {
|
|
767
770
|
method,
|
|
768
771
|
path,
|
|
769
772
|
status,
|
|
770
|
-
duration: `${
|
|
773
|
+
duration: `${duration2}ms`
|
|
771
774
|
};
|
|
772
775
|
if (this.httpLoggingConfig?.includeQueryParams) {
|
|
773
776
|
logData.query = request.query;
|
|
@@ -783,7 +786,7 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
783
786
|
});
|
|
784
787
|
logData.headers = headers;
|
|
785
788
|
}
|
|
786
|
-
this.logger[level](`${method} ${path} ${status} ${
|
|
789
|
+
this.logger[level](`${method} ${path} ${status} ${duration2}ms`, logData);
|
|
787
790
|
});
|
|
788
791
|
});
|
|
789
792
|
}
|