@mastra/deployer 0.16.3 → 0.16.4-alpha.0

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.
@@ -15,6 +15,7 @@ type Variables = {
15
15
  taskStore: InMemoryTaskStore;
16
16
  playground: boolean;
17
17
  isDev: boolean;
18
+ customRouteAuthConfig?: Map<string, boolean>;
18
19
  };
19
20
  export declare function getToolExports(tools: Record<string, Function>[]): Record<string, Function> | undefined;
20
21
  export declare function createHonoServer(mastra: Mastra, options?: ServerBundleOptions): Promise<Hono<{
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAE9D,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAE7D,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAuB5B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAGnD,KAAK,QAAQ,GAAG,EAAE,CAAC;AAEnB,KAAK,SAAS,GAAG;IACf,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,cAAc,CAAC;IAC/B,OAAO,EAAE,GAAG,CAAC;QAAE,UAAU,EAAE,+BAA+B,CAAA;KAAE,CAAC,CAAC;IAC9D,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC5B,SAAS,EAAE,iBAAiB,CAAC;IAC7B,UAAU,EAAE,OAAO,CAAC;IACpB,KAAK,EAAE,OAAO,CAAC;CAChB,CAAC;AAEF,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,EAAE,wCAmB/D;AAED,wBAAs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MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,mBAAmC,mDAkClG"}
@@ -5,7 +5,7 @@ import { createServer } from 'http';
5
5
  import { Http2ServerRequest } from 'http2';
6
6
  import { Writable, Readable } from 'stream';
7
7
  import { getMimeType } from 'hono/utils/mime';
8
- import { createReadStream, lstatSync } from 'fs';
8
+ import { existsSync, createReadStream, lstatSync } from 'fs';
9
9
  import { join as join$1 } from 'path';
10
10
  import { html } from 'hono/html';
11
11
  import { RuntimeContext } from '@mastra/core/runtime-context';
@@ -65,8 +65,7 @@ var Request = class extends GlobalRequest {
65
65
  super(input, options);
66
66
  }
67
67
  };
68
- var wrapBodyStream = Symbol("wrapBodyStream");
69
- var newRequestFromIncoming = (method, url, incoming, abortController) => {
68
+ var newHeadersFromIncoming = (incoming) => {
70
69
  const headerRecord = [];
71
70
  const rawHeaders = incoming.rawHeaders;
72
71
  for (let i2 = 0; i2 < rawHeaders.length; i2 += 2) {
@@ -76,9 +75,13 @@ var newRequestFromIncoming = (method, url, incoming, abortController) => {
76
75
  headerRecord.push([key, value]);
77
76
  }
78
77
  }
78
+ return new Headers(headerRecord);
79
+ };
80
+ var wrapBodyStream = Symbol("wrapBodyStream");
81
+ var newRequestFromIncoming = (method, url, headers, incoming, abortController) => {
79
82
  const init = {
80
83
  method,
81
- headers: headerRecord,
84
+ headers,
82
85
  signal: abortController.signal
83
86
  };
84
87
  if (method === "TRACE") {
@@ -126,6 +129,7 @@ var getRequestCache = Symbol("getRequestCache");
126
129
  var requestCache = Symbol("requestCache");
127
130
  var incomingKey = Symbol("incomingKey");
128
131
  var urlKey = Symbol("urlKey");
132
+ var headersKey = Symbol("headersKey");
129
133
  var abortControllerKey = Symbol("abortControllerKey");
130
134
  var getAbortController = Symbol("getAbortController");
131
135
  var requestPrototype = {
@@ -135,6 +139,9 @@ var requestPrototype = {
135
139
  get url() {
136
140
  return this[urlKey];
137
141
  },
142
+ get headers() {
143
+ return this[headersKey] ||= newHeadersFromIncoming(this[incomingKey]);
144
+ },
138
145
  [getAbortController]() {
139
146
  this[getRequestCache]();
140
147
  return this[abortControllerKey];
@@ -144,6 +151,7 @@ var requestPrototype = {
144
151
  return this[requestCache] ||= newRequestFromIncoming(
145
152
  this.method,
146
153
  this[urlKey],
154
+ this.headers,
147
155
  this[incomingKey],
148
156
  this[abortControllerKey]
149
157
  );
@@ -155,7 +163,6 @@ var requestPrototype = {
155
163
  "cache",
156
164
  "credentials",
157
165
  "destination",
158
- "headers",
159
166
  "integrity",
160
167
  "mode",
161
168
  "redirect",
@@ -281,17 +288,14 @@ var Response2 = class _Response {
281
288
  });
282
289
  Object.setPrototypeOf(Response2, GlobalResponse);
283
290
  Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
284
- function writeFromReadableStream(stream7, writable) {
285
- if (stream7.locked) {
286
- throw new TypeError("ReadableStream is locked.");
287
- } else if (writable.destroyed) {
288
- return;
289
- }
290
- const reader = stream7.getReader();
291
+ async function readWithoutBlocking(readPromise) {
292
+ return Promise.race([readPromise, Promise.resolve().then(() => Promise.resolve(void 0))]);
293
+ }
294
+ function writeFromReadableStreamDefaultReader(reader, writable, currentReadPromise) {
291
295
  const handleError2 = () => {
292
296
  };
293
297
  writable.on("error", handleError2);
294
- reader.read().then(flow, handleStreamError);
298
+ (currentReadPromise ?? reader.read()).then(flow, handleStreamError);
295
299
  return reader.closed.finally(() => {
296
300
  writable.off("error", handleError2);
297
301
  });
@@ -317,6 +321,14 @@ function writeFromReadableStream(stream7, writable) {
317
321
  }
318
322
  }
319
323
  }
324
+ function writeFromReadableStream(stream7, writable) {
325
+ if (stream7.locked) {
326
+ throw new TypeError("ReadableStream is locked.");
327
+ } else if (writable.destroyed) {
328
+ return;
329
+ }
330
+ return writeFromReadableStreamDefaultReader(stream7.getReader(), writable);
331
+ }
320
332
  var buildOutgoingHttpHeaders = (headers) => {
321
333
  const res = {};
322
334
  if (!(headers instanceof Headers)) {
@@ -351,8 +363,6 @@ global.fetch = (info, init) => {
351
363
  return webFetch(info, init);
352
364
  };
353
365
  var outgoingEnded = Symbol("outgoingEnded");
354
- var regBuffer = /^no$/i;
355
- var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
356
366
  var handleRequestError = () => new Response(null, {
357
367
  status: 400
358
368
  });
@@ -423,23 +433,50 @@ var responseViaResponseObject = async (res, outgoing, options = {}) => {
423
433
  }
424
434
  const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
425
435
  if (res.body) {
426
- const {
427
- "transfer-encoding": transferEncoding,
428
- "content-encoding": contentEncoding,
429
- "content-length": contentLength,
430
- "x-accel-buffering": accelBuffering,
431
- "content-type": contentType
432
- } = resHeaderRecord;
433
- if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
434
- accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
435
- outgoing.writeHead(res.status, resHeaderRecord);
436
- flushHeaders(outgoing);
437
- await writeFromReadableStream(res.body, outgoing);
436
+ const reader = res.body.getReader();
437
+ const values = [];
438
+ let done = false;
439
+ let currentReadPromise = void 0;
440
+ if (resHeaderRecord["transfer-encoding"] !== "chunked") {
441
+ let maxReadCount = 2;
442
+ for (let i2 = 0; i2 < maxReadCount; i2++) {
443
+ currentReadPromise ||= reader.read();
444
+ const chunk = await readWithoutBlocking(currentReadPromise).catch((e2) => {
445
+ console.error(e2);
446
+ done = true;
447
+ });
448
+ if (!chunk) {
449
+ if (i2 === 1) {
450
+ await new Promise((resolve) => setTimeout(resolve));
451
+ maxReadCount = 3;
452
+ continue;
453
+ }
454
+ break;
455
+ }
456
+ currentReadPromise = void 0;
457
+ if (chunk.value) {
458
+ values.push(chunk.value);
459
+ }
460
+ if (chunk.done) {
461
+ done = true;
462
+ break;
463
+ }
464
+ }
465
+ if (done && !("content-length" in resHeaderRecord)) {
466
+ resHeaderRecord["content-length"] = values.reduce((acc, value) => acc + value.length, 0);
467
+ }
468
+ }
469
+ outgoing.writeHead(res.status, resHeaderRecord);
470
+ values.forEach((value) => {
471
+ outgoing.write(value);
472
+ });
473
+ if (done) {
474
+ outgoing.end();
438
475
  } else {
439
- const buffer = await res.arrayBuffer();
440
- resHeaderRecord["content-length"] = buffer.byteLength;
441
- outgoing.writeHead(res.status, resHeaderRecord);
442
- outgoing.end(new Uint8Array(buffer));
476
+ if (values.length === 0) {
477
+ flushHeaders(outgoing);
478
+ }
479
+ await writeFromReadableStreamDefaultReader(reader, outgoing, currentReadPromise);
443
480
  }
444
481
  } else if (resHeaderRecord[X_ALREADY_SENT]) ; else {
445
482
  outgoing.writeHead(res.status, resHeaderRecord);
@@ -581,6 +618,9 @@ var getStats = (path) => {
581
618
  var serveStatic = (options = { root: "" }) => {
582
619
  const root = options.root || "";
583
620
  const optionPath = options.path;
621
+ if (root !== "" && !existsSync(root)) {
622
+ console.error(`serveStatic: root path '${root}' is not found, are you sure it's correct?`);
623
+ }
584
624
  return async (c2, next) => {
585
625
  if (c2.finalized) {
586
626
  return next();
@@ -648,8 +688,8 @@ var serveStatic = (options = { root: "" }) => {
648
688
  c2.header("Accept-Ranges", "bytes");
649
689
  c2.header("Date", stats.birthtime.toUTCString());
650
690
  const parts = range.replace(/bytes=/, "").split("-", 2);
651
- const start = parts[0] ? parseInt(parts[0], 10) : 0;
652
- let end = parts[1] ? parseInt(parts[1], 10) : stats.size - 1;
691
+ const start = parseInt(parts[0], 10) || 0;
692
+ let end = parseInt(parts[1], 10) || size - 1;
653
693
  if (size < end - start + 1) {
654
694
  end = size - 1;
655
695
  }
@@ -1004,6 +1044,20 @@ var defaultAuthConfig = {
1004
1044
  var isDevPlaygroundRequest = (req) => {
1005
1045
  return req.header("x-mastra-dev-playground") === "true" && process.env.MASTRA_DEV === "true";
1006
1046
  };
1047
+ var isCustomRoutePublic = (path, method, customRouteAuthConfig) => {
1048
+ if (!customRouteAuthConfig) {
1049
+ return false;
1050
+ }
1051
+ const routeKey = `${method}:${path}`;
1052
+ if (customRouteAuthConfig.has(routeKey)) {
1053
+ return !customRouteAuthConfig.get(routeKey);
1054
+ }
1055
+ const allRouteKey = `ALL:${path}`;
1056
+ if (customRouteAuthConfig.has(allRouteKey)) {
1057
+ return !customRouteAuthConfig.get(allRouteKey);
1058
+ }
1059
+ return false;
1060
+ };
1007
1061
  var isProtectedPath = (path, method, authConfig) => {
1008
1062
  const protectedAccess = [...defaultAuthConfig.protected || [], ...authConfig.protected || []];
1009
1063
  return isAnyMatch(path, method, protectedAccess);
@@ -1090,12 +1144,16 @@ var checkRules = async (rules, path, method, user) => {
1090
1144
  var authenticationMiddleware = async (c2, next) => {
1091
1145
  const mastra = c2.get("mastra");
1092
1146
  const authConfig = mastra.getServer()?.experimental_auth;
1147
+ const customRouteAuthConfig = c2.get("customRouteAuthConfig");
1093
1148
  if (!authConfig) {
1094
1149
  return next();
1095
1150
  }
1096
1151
  if (isDevPlaygroundRequest(c2.req)) {
1097
1152
  return next();
1098
1153
  }
1154
+ if (isCustomRoutePublic(c2.req.path, c2.req.method, customRouteAuthConfig)) {
1155
+ return next();
1156
+ }
1099
1157
  if (!isProtectedPath(c2.req.path, c2.req.method, authConfig)) {
1100
1158
  return next();
1101
1159
  }
@@ -1130,6 +1188,7 @@ var authenticationMiddleware = async (c2, next) => {
1130
1188
  var authorizationMiddleware = async (c2, next) => {
1131
1189
  const mastra = c2.get("mastra");
1132
1190
  const authConfig = mastra.getServer()?.experimental_auth;
1191
+ const customRouteAuthConfig = c2.get("customRouteAuthConfig");
1133
1192
  if (!authConfig) {
1134
1193
  return next();
1135
1194
  }
@@ -1138,6 +1197,9 @@ var authorizationMiddleware = async (c2, next) => {
1138
1197
  if (isDevPlaygroundRequest(c2.req)) {
1139
1198
  return next();
1140
1199
  }
1200
+ if (isCustomRoutePublic(path, method, customRouteAuthConfig)) {
1201
+ return next();
1202
+ }
1141
1203
  if (!isProtectedPath(c2.req.path, c2.req.method, authConfig)) {
1142
1204
  return next();
1143
1205
  }
@@ -10971,6 +11033,15 @@ async function createHonoServer(mastra, options = {
10971
11033
  const app = new Hono();
10972
11034
  const server = mastra.getServer();
10973
11035
  const a2aTaskStore = new InMemoryTaskStore();
11036
+ const routes = server?.apiRoutes;
11037
+ const customRouteAuthConfig = /* @__PURE__ */ new Map();
11038
+ if (routes) {
11039
+ for (const route of routes) {
11040
+ const requiresAuth = route.requiresAuth !== false;
11041
+ const routeKey = `${route.method}:${route.path}`;
11042
+ customRouteAuthConfig.set(routeKey, requiresAuth);
11043
+ }
11044
+ }
10974
11045
  app.use("*", async function setTelemetryInfo(c2, next) {
10975
11046
  const requestId = c2.req.header("x-request-id") ?? randomUUID();
10976
11047
  const span = Telemetry.getActiveSpan();
@@ -11033,6 +11104,7 @@ async function createHonoServer(mastra, options = {
11033
11104
  c2.set("taskStore", a2aTaskStore);
11034
11105
  c2.set("playground", options.playground === true);
11035
11106
  c2.set("isDev", options.isDev === true);
11107
+ c2.set("customRouteAuthConfig", customRouteAuthConfig);
11036
11108
  return next();
11037
11109
  });
11038
11110
  const serverMiddleware = mastra.getServerMiddleware?.();
@@ -11062,7 +11134,6 @@ async function createHonoServer(mastra, options = {
11062
11134
  // 4.5 MB,
11063
11135
  onError: (c2) => c2.json({ error: "Request body too large" }, 413)
11064
11136
  };
11065
- const routes = server?.apiRoutes;
11066
11137
  if (server?.middleware) {
11067
11138
  const normalizedMiddlewares = Array.isArray(server.middleware) ? server.middleware : [server.middleware];
11068
11139
  const middlewares = normalizedMiddlewares.map((middleware2) => {