@mastra/deployer 1.0.0-beta.5 → 1.0.0-beta.7
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 +78 -0
- package/dist/arktype-aI7TBD0R-JG4WETGU.js +8 -0
- package/dist/arktype-aI7TBD0R-JG4WETGU.js.map +1 -0
- package/dist/arktype-aI7TBD0R-RMRB5ZNY.cjs +10 -0
- package/dist/arktype-aI7TBD0R-RMRB5ZNY.cjs.map +1 -0
- package/dist/build/analyze/analyzeEntry.d.ts +4 -1
- package/dist/build/analyze/analyzeEntry.d.ts.map +1 -1
- package/dist/build/analyze/bundleExternals.d.ts.map +1 -1
- package/dist/build/analyze.cjs +2 -2
- package/dist/build/analyze.d.ts +1 -1
- package/dist/build/analyze.d.ts.map +1 -1
- package/dist/build/analyze.js +1 -1
- package/dist/build/bundler.cjs +3 -3
- package/dist/build/bundler.d.ts.map +1 -1
- package/dist/build/bundler.js +1 -1
- package/dist/build/index.cjs +13 -13
- package/dist/build/index.js +4 -4
- package/dist/build/plugins/esm-shim.d.ts +14 -0
- package/dist/build/plugins/esm-shim.d.ts.map +1 -0
- package/dist/build/plugins/module-resolve-map.d.ts +3 -0
- package/dist/build/plugins/module-resolve-map.d.ts.map +1 -0
- package/dist/build/plugins/node-gyp-detector.d.ts +3 -0
- package/dist/build/plugins/node-gyp-detector.d.ts.map +1 -0
- package/dist/build/plugins/subpath-externals-resolver.d.ts +3 -0
- package/dist/build/plugins/subpath-externals-resolver.d.ts.map +1 -0
- package/dist/build/plugins/tsconfig-paths.d.ts.map +1 -1
- package/dist/build/utils.d.ts +1 -0
- package/dist/build/utils.d.ts.map +1 -1
- package/dist/bundler/index.cjs +2 -2
- package/dist/bundler/index.d.ts +1 -1
- package/dist/bundler/index.js +1 -1
- package/dist/chunk-2NO5NO4J.js +93 -0
- package/dist/chunk-2NO5NO4J.js.map +1 -0
- package/dist/chunk-2OTEFUER.cjs +69 -0
- package/dist/chunk-2OTEFUER.cjs.map +1 -0
- package/dist/chunk-46SXOVGO.js +159 -0
- package/dist/chunk-46SXOVGO.js.map +1 -0
- package/dist/chunk-4YYZKC2P.cjs +4 -0
- package/dist/chunk-4YYZKC2P.cjs.map +1 -0
- package/dist/chunk-5CWPEG6R.js +62 -0
- package/dist/chunk-5CWPEG6R.js.map +1 -0
- package/dist/{chunk-QBQY35KA.js → chunk-7G2FA6KY.js} +6 -5
- package/dist/chunk-7G2FA6KY.js.map +1 -0
- package/dist/chunk-BJURVMEW.cjs +16 -0
- package/dist/chunk-BJURVMEW.cjs.map +1 -0
- package/dist/{chunk-5QFG6SCM.js → chunk-DPDWM7HQ.js} +2 -2
- package/dist/chunk-DPDWM7HQ.js.map +1 -0
- package/dist/chunk-HXYLK6CH.js +3 -0
- package/dist/chunk-HXYLK6CH.js.map +1 -0
- package/dist/chunk-IL36YSXF.cjs +8 -0
- package/dist/chunk-IL36YSXF.cjs.map +1 -0
- package/dist/chunk-IR2ZJ7CP.cjs +95 -0
- package/dist/chunk-IR2ZJ7CP.cjs.map +1 -0
- package/dist/{chunk-3JYRSECU.cjs → chunk-JVBCN3RH.cjs} +4 -4
- package/dist/chunk-JVBCN3RH.cjs.map +1 -0
- package/dist/chunk-L4SN27ZE.js +14 -0
- package/dist/chunk-L4SN27ZE.js.map +1 -0
- package/dist/{chunk-WFL4V5AW.cjs → chunk-MC37X2IH.cjs} +565 -192
- package/dist/chunk-MC37X2IH.cjs.map +1 -0
- package/dist/{chunk-Z7TBDRZ7.cjs → chunk-N6GC4R2P.cjs} +13 -23
- package/dist/chunk-N6GC4R2P.cjs.map +1 -0
- package/dist/chunk-NZ47MQSD.cjs +162 -0
- package/dist/chunk-NZ47MQSD.cjs.map +1 -0
- package/dist/{chunk-EM3VETMX.js → chunk-O2KU7J7Q.js} +5 -4
- package/dist/chunk-O2KU7J7Q.js.map +1 -0
- package/dist/{chunk-NVCHGD24.cjs → chunk-ORHTXAPI.cjs} +15 -14
- package/dist/chunk-ORHTXAPI.cjs.map +1 -0
- package/dist/chunk-PDVYEX76.js +6 -0
- package/dist/chunk-PDVYEX76.js.map +1 -0
- package/dist/{chunk-6S5GQUS6.cjs → chunk-PMRHMMAQ.cjs} +16 -15
- package/dist/chunk-PMRHMMAQ.cjs.map +1 -0
- package/dist/{chunk-Z7KK37HD.js → chunk-SRCHVFND.js} +6 -15
- package/dist/chunk-SRCHVFND.js.map +1 -0
- package/dist/{chunk-NK3AYZ6D.js → chunk-UEFVUIPU.js} +549 -175
- package/dist/chunk-UEFVUIPU.js.map +1 -0
- package/dist/default-u_dwuiYb-MRYQILW4.js +3 -0
- package/dist/default-u_dwuiYb-MRYQILW4.js.map +1 -0
- package/dist/default-u_dwuiYb-WPBHZH2R.cjs +9 -0
- package/dist/default-u_dwuiYb-WPBHZH2R.cjs.map +1 -0
- package/dist/deploy/log.d.ts +1 -1
- package/dist/deploy/log.d.ts.map +1 -1
- package/dist/effect-QlVUlMFu-N7VAYT2B.cjs +17 -0
- package/dist/effect-QlVUlMFu-N7VAYT2B.cjs.map +1 -0
- package/dist/effect-QlVUlMFu-NH625H3V.js +15 -0
- package/dist/effect-QlVUlMFu-NH625H3V.js.map +1 -0
- package/dist/esm-BZAOWJTI.cjs +1336 -0
- package/dist/esm-BZAOWJTI.cjs.map +1 -0
- package/dist/esm-T7FR74HN.js +1289 -0
- package/dist/esm-T7FR74HN.js.map +1 -0
- package/dist/index.cjs +12 -12
- package/dist/index.js +4 -4
- package/dist/server/index.cjs +1113 -1508
- package/dist/server/index.cjs.map +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +1101 -1496
- package/dist/server/index.js.map +1 -1
- package/dist/services/index.cjs +4 -4
- package/dist/services/index.js +1 -1
- package/dist/sury-CWZTCd75-E5BDQIXB.cjs +17 -0
- package/dist/sury-CWZTCd75-E5BDQIXB.cjs.map +1 -0
- package/dist/sury-CWZTCd75-HWBNYVQW.js +15 -0
- package/dist/sury-CWZTCd75-HWBNYVQW.js.map +1 -0
- package/dist/typebox-Dei93FPO-42ICWLXF.cjs +10 -0
- package/dist/typebox-Dei93FPO-42ICWLXF.cjs.map +1 -0
- package/dist/typebox-Dei93FPO-XXPZXWWK.js +8 -0
- package/dist/typebox-Dei93FPO-XXPZXWWK.js.map +1 -0
- package/dist/valibot--1zFm7rT-ALWQ34TP.cjs +17 -0
- package/dist/valibot--1zFm7rT-ALWQ34TP.cjs.map +1 -0
- package/dist/valibot--1zFm7rT-W6DSEVCL.js +15 -0
- package/dist/valibot--1zFm7rT-W6DSEVCL.js.map +1 -0
- package/dist/valibot-D_HTw1Gn-LVFT5X5F.js +43 -0
- package/dist/valibot-D_HTw1Gn-LVFT5X5F.js.map +1 -0
- package/dist/valibot-D_HTw1Gn-PN7GEV4F.cjs +45 -0
- package/dist/valibot-D_HTw1Gn-PN7GEV4F.cjs.map +1 -0
- package/dist/validator/custom-resolver.cjs +20 -12
- package/dist/validator/custom-resolver.cjs.map +1 -1
- package/dist/validator/custom-resolver.d.ts.map +1 -1
- package/dist/validator/custom-resolver.js +18 -10
- package/dist/validator/custom-resolver.js.map +1 -1
- package/dist/validator/validate.d.ts +15 -1
- package/dist/validator/validate.d.ts.map +1 -1
- package/dist/zod-Bwrt9trS-C5ATNTZG.cjs +31 -0
- package/dist/zod-Bwrt9trS-C5ATNTZG.cjs.map +1 -0
- package/dist/zod-Bwrt9trS-UDAV5VDV.js +29 -0
- package/dist/zod-Bwrt9trS-UDAV5VDV.js.map +1 -0
- package/dist/zod-DSgpEGAE-AVWGGTUD.js +37 -0
- package/dist/zod-DSgpEGAE-AVWGGTUD.js.map +1 -0
- package/dist/zod-DSgpEGAE-EH7N2QK7.cjs +39 -0
- package/dist/zod-DSgpEGAE-EH7N2QK7.cjs.map +1 -0
- package/package.json +8 -6
- package/dist/chunk-3JYRSECU.cjs.map +0 -1
- package/dist/chunk-5QFG6SCM.js.map +0 -1
- package/dist/chunk-6S5GQUS6.cjs.map +0 -1
- package/dist/chunk-EM3VETMX.js.map +0 -1
- package/dist/chunk-NK3AYZ6D.js.map +0 -1
- package/dist/chunk-NVCHGD24.cjs.map +0 -1
- package/dist/chunk-QBQY35KA.js.map +0 -1
- package/dist/chunk-WFL4V5AW.cjs.map +0 -1
- package/dist/chunk-Z7KK37HD.js.map +0 -1
- package/dist/chunk-Z7TBDRZ7.cjs.map +0 -1
- package/dist/server/handlers/auth/defaults.d.ts +0 -3
- package/dist/server/handlers/auth/defaults.d.ts.map +0 -1
- package/dist/server/handlers/auth/helpers.d.ts +0 -11
- package/dist/server/handlers/auth/helpers.d.ts.map +0 -1
- package/dist/server/handlers/auth/index.d.ts +0 -11
- package/dist/server/handlers/auth/index.d.ts.map +0 -1
- package/dist/server/handlers/mcp.d.ts +0 -86
- package/dist/server/handlers/mcp.d.ts.map +0 -1
- package/dist/server/schemas/mcp.d.ts +0 -230
- package/dist/server/schemas/mcp.d.ts.map +0 -1
package/dist/server/index.js
CHANGED
|
@@ -10,29 +10,28 @@ import { existsSync, createReadStream, lstatSync } from 'fs';
|
|
|
10
10
|
import { join as join$1 } from 'path';
|
|
11
11
|
import { html } from 'hono/html';
|
|
12
12
|
import { Tool } from '@mastra/core/tools';
|
|
13
|
+
import { MastraServer as MastraServer$1, redactStreamChunk } from '@mastra/server/server-adapter';
|
|
14
|
+
import util from 'util';
|
|
15
|
+
import { Buffer as Buffer$1 } from 'buffer';
|
|
16
|
+
import { isDevPlaygroundRequest, isProtectedPath, canAccessPublicly, checkRules, defaultAuthConfig } from '@mastra/server/auth';
|
|
13
17
|
import { InMemoryTaskStore } from '@mastra/server/a2a/store';
|
|
14
|
-
import { createRoute, MastraServerAdapter } from '@mastra/server/server-adapter';
|
|
15
18
|
import { Hono } from 'hono';
|
|
16
19
|
import { cors } from 'hono/cors';
|
|
17
20
|
import { logger } from 'hono/logger';
|
|
18
21
|
import { timeout } from 'hono/timeout';
|
|
19
22
|
import { HTTPException as HTTPException$1 } from 'hono/http-exception';
|
|
20
|
-
import util from 'util';
|
|
21
|
-
import { Buffer as Buffer$1 } from 'buffer';
|
|
22
|
-
import { z } from 'zod';
|
|
23
23
|
|
|
24
|
-
// src/server/index.ts
|
|
25
24
|
var RequestError = class extends Error {
|
|
26
25
|
constructor(message, options) {
|
|
27
26
|
super(message, options);
|
|
28
27
|
this.name = "RequestError";
|
|
29
28
|
}
|
|
30
29
|
};
|
|
31
|
-
var toRequestError = (
|
|
32
|
-
if (
|
|
33
|
-
return
|
|
30
|
+
var toRequestError = (e) => {
|
|
31
|
+
if (e instanceof RequestError) {
|
|
32
|
+
return e;
|
|
34
33
|
}
|
|
35
|
-
return new RequestError(
|
|
34
|
+
return new RequestError(e.message, { cause: e });
|
|
36
35
|
};
|
|
37
36
|
var GlobalRequest = global.Request;
|
|
38
37
|
var Request2 = class extends GlobalRequest {
|
|
@@ -178,8 +177,8 @@ var newRequest = (incoming, defaultHostname) => {
|
|
|
178
177
|
try {
|
|
179
178
|
const url2 = new URL(incomingUrl);
|
|
180
179
|
req[urlKey] = url2.href;
|
|
181
|
-
} catch (
|
|
182
|
-
throw new RequestError("Invalid absolute URL", { cause:
|
|
180
|
+
} catch (e) {
|
|
181
|
+
throw new RequestError("Invalid absolute URL", { cause: e });
|
|
183
182
|
}
|
|
184
183
|
return req;
|
|
185
184
|
}
|
|
@@ -301,8 +300,8 @@ function writeFromReadableStreamDefaultReader(reader, writable, currentReadPromi
|
|
|
301
300
|
} else {
|
|
302
301
|
return reader.read().then(flow, handleStreamError);
|
|
303
302
|
}
|
|
304
|
-
} catch (
|
|
305
|
-
handleStreamError(
|
|
303
|
+
} catch (e) {
|
|
304
|
+
handleStreamError(e);
|
|
306
305
|
}
|
|
307
306
|
}
|
|
308
307
|
}
|
|
@@ -351,15 +350,15 @@ var outgoingEnded = Symbol("outgoingEnded");
|
|
|
351
350
|
var handleRequestError = () => new Response(null, {
|
|
352
351
|
status: 400
|
|
353
352
|
});
|
|
354
|
-
var handleFetchError = (
|
|
355
|
-
status:
|
|
353
|
+
var handleFetchError = (e) => new Response(null, {
|
|
354
|
+
status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
|
|
356
355
|
});
|
|
357
|
-
var handleResponseError = (
|
|
358
|
-
const err =
|
|
356
|
+
var handleResponseError = (e, outgoing) => {
|
|
357
|
+
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
359
358
|
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
|
|
360
359
|
console.info("The user aborted a request.");
|
|
361
360
|
} else {
|
|
362
|
-
console.error(
|
|
361
|
+
console.error(e);
|
|
363
362
|
if (!outgoing.headersSent) {
|
|
364
363
|
outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
365
364
|
}
|
|
@@ -392,7 +391,7 @@ var responseViaCache = async (res, outgoing) => {
|
|
|
392
391
|
} else {
|
|
393
392
|
flushHeaders(outgoing);
|
|
394
393
|
await writeFromReadableStream(body, outgoing)?.catch(
|
|
395
|
-
(
|
|
394
|
+
(e) => handleResponseError(e, outgoing)
|
|
396
395
|
);
|
|
397
396
|
}
|
|
398
397
|
outgoing[outgoingEnded]?.();
|
|
@@ -427,8 +426,8 @@ var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
|
427
426
|
let maxReadCount = 2;
|
|
428
427
|
for (let i = 0; i < maxReadCount; i++) {
|
|
429
428
|
currentReadPromise ||= reader.read();
|
|
430
|
-
const chunk = await readWithoutBlocking(currentReadPromise).catch((
|
|
431
|
-
console.error(
|
|
429
|
+
const chunk = await readWithoutBlocking(currentReadPromise).catch((e) => {
|
|
430
|
+
console.error(e);
|
|
432
431
|
done = true;
|
|
433
432
|
});
|
|
434
433
|
if (!chunk) {
|
|
@@ -528,26 +527,26 @@ var getRequestListener = (fetchCallback, options = {}) => {
|
|
|
528
527
|
if (cacheKey in res) {
|
|
529
528
|
return responseViaCache(res, outgoing);
|
|
530
529
|
}
|
|
531
|
-
} catch (
|
|
530
|
+
} catch (e) {
|
|
532
531
|
if (!res) {
|
|
533
532
|
if (options.errorHandler) {
|
|
534
|
-
res = await options.errorHandler(req ?
|
|
533
|
+
res = await options.errorHandler(req ? e : toRequestError(e));
|
|
535
534
|
if (!res) {
|
|
536
535
|
return;
|
|
537
536
|
}
|
|
538
537
|
} else if (!req) {
|
|
539
538
|
res = handleRequestError();
|
|
540
539
|
} else {
|
|
541
|
-
res = handleFetchError(
|
|
540
|
+
res = handleFetchError(e);
|
|
542
541
|
}
|
|
543
542
|
} else {
|
|
544
|
-
return handleResponseError(
|
|
543
|
+
return handleResponseError(e, outgoing);
|
|
545
544
|
}
|
|
546
545
|
}
|
|
547
546
|
try {
|
|
548
547
|
return await responseViaResponseObject(res, outgoing, options);
|
|
549
|
-
} catch (
|
|
550
|
-
return handleResponseError(
|
|
548
|
+
} catch (e) {
|
|
549
|
+
return handleResponseError(e, outgoing);
|
|
551
550
|
}
|
|
552
551
|
};
|
|
553
552
|
};
|
|
@@ -808,1123 +807,412 @@ var middleware = (options) => async (c) => {
|
|
|
808
807
|
`
|
|
809
808
|
);
|
|
810
809
|
};
|
|
811
|
-
var
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
810
|
+
var classRegExp = /^([A-Z][a-z0-9]*)+$/;
|
|
811
|
+
var kTypes = [
|
|
812
|
+
"string",
|
|
813
|
+
"function",
|
|
814
|
+
"number",
|
|
815
|
+
"object",
|
|
816
|
+
// Accept 'Function' and 'Object' as alternative to the lower cased version.
|
|
817
|
+
"Function",
|
|
818
|
+
"Object",
|
|
819
|
+
"boolean",
|
|
820
|
+
"bigint",
|
|
821
|
+
"symbol"
|
|
822
|
+
];
|
|
823
|
+
function determineSpecificType(value) {
|
|
824
|
+
if (value == null) {
|
|
825
|
+
return "" + value;
|
|
818
826
|
}
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
}
|
|
825
|
-
return newResponse;
|
|
827
|
+
if (typeof value === "function" && value.name) {
|
|
828
|
+
return `function ${value.name}`;
|
|
829
|
+
}
|
|
830
|
+
if (typeof value === "object") {
|
|
831
|
+
if (value.constructor?.name) {
|
|
832
|
+
return `an instance of ${value.constructor.name}`;
|
|
826
833
|
}
|
|
827
|
-
return
|
|
828
|
-
status: this.status
|
|
829
|
-
});
|
|
834
|
+
return `${util.inspect(value, { depth: -1 })}`;
|
|
830
835
|
}
|
|
831
|
-
};
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
836
|
+
let inspected = util.inspect(value, { colors: false });
|
|
837
|
+
if (inspected.length > 28) {
|
|
838
|
+
inspected = `${inspected.slice(0, 25)}...`;
|
|
839
|
+
}
|
|
840
|
+
return `type ${typeof value} (${inspected})`;
|
|
841
|
+
}
|
|
842
|
+
var ERR_HTTP_BODY_NOT_ALLOWED = class extends Error {
|
|
843
|
+
constructor() {
|
|
844
|
+
super("Adding content for this request method or response status is not allowed.");
|
|
837
845
|
}
|
|
838
846
|
};
|
|
839
|
-
var
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
});
|
|
844
|
-
throw new HTTPException(413, { res });
|
|
845
|
-
});
|
|
846
|
-
const maxSize = options.maxSize;
|
|
847
|
-
return async function bodyLimit2(c, next) {
|
|
848
|
-
if (!c.req.raw.body) {
|
|
849
|
-
return next();
|
|
850
|
-
}
|
|
851
|
-
const hasTransferEncoding = c.req.raw.headers.has("transfer-encoding");
|
|
852
|
-
const hasContentLength = c.req.raw.headers.has("content-length");
|
|
853
|
-
if (hasContentLength && !hasTransferEncoding) {
|
|
854
|
-
const contentLength = parseInt(c.req.raw.headers.get("content-length") || "0", 10);
|
|
855
|
-
return contentLength > maxSize ? onError3(c) : next();
|
|
856
|
-
}
|
|
857
|
-
let size = 0;
|
|
858
|
-
const rawReader = c.req.raw.body.getReader();
|
|
859
|
-
const reader = new ReadableStream({
|
|
860
|
-
async start(controller) {
|
|
861
|
-
try {
|
|
862
|
-
for (; ; ) {
|
|
863
|
-
const { done, value } = await rawReader.read();
|
|
864
|
-
if (done) {
|
|
865
|
-
break;
|
|
866
|
-
}
|
|
867
|
-
size += value.length;
|
|
868
|
-
if (size > maxSize) {
|
|
869
|
-
controller.error(new BodyLimitError(ERROR_MESSAGE));
|
|
870
|
-
break;
|
|
871
|
-
}
|
|
872
|
-
controller.enqueue(value);
|
|
873
|
-
}
|
|
874
|
-
} finally {
|
|
875
|
-
controller.close();
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
});
|
|
879
|
-
const requestInit = { body: reader, duplex: "half" };
|
|
880
|
-
c.req.raw = new Request(c.req.raw, requestInit);
|
|
881
|
-
await next();
|
|
882
|
-
if (c.error instanceof BodyLimitError) {
|
|
883
|
-
c.res = await onError3(c);
|
|
884
|
-
}
|
|
885
|
-
};
|
|
847
|
+
var ERR_HTTP_CONTENT_LENGTH_MISMATCH = class extends Error {
|
|
848
|
+
constructor(actual, expected) {
|
|
849
|
+
super(`Response body's content-length of ${actual} byte(s) does not match the content-length of ${expected} byte(s) set in header`);
|
|
850
|
+
}
|
|
886
851
|
};
|
|
887
|
-
var
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
writable;
|
|
891
|
-
abortSubscribers = [];
|
|
892
|
-
responseReadable;
|
|
893
|
-
aborted = false;
|
|
894
|
-
closed = false;
|
|
895
|
-
constructor(writable, _readable) {
|
|
896
|
-
this.writable = writable;
|
|
897
|
-
this.writer = writable.getWriter();
|
|
898
|
-
this.encoder = new TextEncoder();
|
|
899
|
-
const reader = _readable.getReader();
|
|
900
|
-
this.abortSubscribers.push(async () => {
|
|
901
|
-
await reader.cancel();
|
|
902
|
-
});
|
|
903
|
-
this.responseReadable = new ReadableStream({
|
|
904
|
-
async pull(controller) {
|
|
905
|
-
const { done, value } = await reader.read();
|
|
906
|
-
done ? controller.close() : controller.enqueue(value);
|
|
907
|
-
},
|
|
908
|
-
cancel: () => {
|
|
909
|
-
this.abort();
|
|
910
|
-
}
|
|
911
|
-
});
|
|
852
|
+
var ERR_HTTP_HEADERS_SENT = class extends Error {
|
|
853
|
+
constructor(arg) {
|
|
854
|
+
super(`Cannot ${arg} headers after they are sent to the client`);
|
|
912
855
|
}
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
} catch {
|
|
856
|
+
};
|
|
857
|
+
var ERR_INVALID_ARG_VALUE = class extends TypeError {
|
|
858
|
+
constructor(name, value, reason = "is invalid") {
|
|
859
|
+
let inspected = util.inspect(value);
|
|
860
|
+
if (inspected.length > 128) {
|
|
861
|
+
inspected = `${inspected.slice(0, 128)}...`;
|
|
920
862
|
}
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
async writeln(input) {
|
|
924
|
-
await this.write(input + "\n");
|
|
925
|
-
return this;
|
|
926
|
-
}
|
|
927
|
-
sleep(ms) {
|
|
928
|
-
return new Promise((res) => setTimeout(res, ms));
|
|
863
|
+
const type = name.includes(".") ? "property" : "argument";
|
|
864
|
+
super(`The ${type} '${name}' ${reason}. Received ${inspected}`);
|
|
929
865
|
}
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
866
|
+
};
|
|
867
|
+
var ERR_INVALID_CHAR = class extends TypeError {
|
|
868
|
+
constructor(name, field) {
|
|
869
|
+
let msg = `Invalid character in ${name}`;
|
|
870
|
+
if (field !== void 0) {
|
|
871
|
+
msg += ` ["${field}"]`;
|
|
934
872
|
}
|
|
935
|
-
|
|
936
|
-
}
|
|
937
|
-
async pipe(body) {
|
|
938
|
-
this.writer.releaseLock();
|
|
939
|
-
await body.pipeTo(this.writable, { preventClose: true });
|
|
940
|
-
this.writer = this.writable.getWriter();
|
|
941
|
-
}
|
|
942
|
-
onAbort(listener) {
|
|
943
|
-
this.abortSubscribers.push(listener);
|
|
873
|
+
super(msg);
|
|
944
874
|
}
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
}
|
|
875
|
+
};
|
|
876
|
+
var ERR_HTTP_INVALID_HEADER_VALUE = class extends TypeError {
|
|
877
|
+
constructor(value, name) {
|
|
878
|
+
super(`Invalid value "${value}" for header "${name}"`);
|
|
950
879
|
}
|
|
951
880
|
};
|
|
952
|
-
var
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
881
|
+
var ERR_HTTP_INVALID_STATUS_CODE = class extends RangeError {
|
|
882
|
+
originalStatusCode;
|
|
883
|
+
constructor(originalStatusCode) {
|
|
884
|
+
super(`Invalid status code: ${originalStatusCode}`);
|
|
885
|
+
this.originalStatusCode = originalStatusCode;
|
|
956
886
|
}
|
|
957
|
-
const result = version.startsWith("1.1") || version.startsWith("1.0") || version.startsWith("0.");
|
|
958
|
-
isOldBunVersion = () => result;
|
|
959
|
-
return result;
|
|
960
887
|
};
|
|
961
|
-
var
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
const stream2 = new StreamingApi(writable, readable);
|
|
965
|
-
if (isOldBunVersion()) {
|
|
966
|
-
c.req.raw.signal.addEventListener("abort", () => {
|
|
967
|
-
if (!stream2.closed) {
|
|
968
|
-
stream2.abort();
|
|
969
|
-
}
|
|
970
|
-
});
|
|
888
|
+
var ERR_HTTP_TRAILER_INVALID = class extends Error {
|
|
889
|
+
constructor() {
|
|
890
|
+
super(`Trailers are invalid with this transfer encoding`);
|
|
971
891
|
}
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
892
|
+
};
|
|
893
|
+
var ERR_INVALID_ARG_TYPE = class extends TypeError {
|
|
894
|
+
constructor(name, expected, actual) {
|
|
895
|
+
if (!Array.isArray(expected)) {
|
|
896
|
+
expected = [expected];
|
|
897
|
+
}
|
|
898
|
+
let msg = "The ";
|
|
899
|
+
if (name.endsWith(" argument")) {
|
|
900
|
+
msg += `${name} `;
|
|
901
|
+
} else {
|
|
902
|
+
const type = name.includes(".") ? "property" : "argument";
|
|
903
|
+
msg += `"${name}" ${type} `;
|
|
904
|
+
}
|
|
905
|
+
msg += "must be ";
|
|
906
|
+
const types = [];
|
|
907
|
+
const instances = [];
|
|
908
|
+
const other = [];
|
|
909
|
+
for (const value of expected) {
|
|
910
|
+
if (kTypes.includes(value)) {
|
|
911
|
+
types.push(value.toLowerCase());
|
|
912
|
+
} else if (classRegExp.exec(value) !== null) {
|
|
913
|
+
instances.push(value);
|
|
980
914
|
} else {
|
|
981
|
-
|
|
915
|
+
other.push(value);
|
|
982
916
|
}
|
|
983
|
-
} finally {
|
|
984
|
-
stream2.close();
|
|
985
917
|
}
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
customRouteAuthConfig;
|
|
992
|
-
playground;
|
|
993
|
-
isDev;
|
|
994
|
-
constructor({
|
|
995
|
-
mastra,
|
|
996
|
-
tools,
|
|
997
|
-
taskStore,
|
|
998
|
-
customRouteAuthConfig,
|
|
999
|
-
playground,
|
|
1000
|
-
isDev,
|
|
1001
|
-
bodyLimitOptions
|
|
1002
|
-
}) {
|
|
1003
|
-
super({ mastra, bodyLimitOptions, tools });
|
|
1004
|
-
this.taskStore = taskStore || new InMemoryTaskStore();
|
|
1005
|
-
this.customRouteAuthConfig = customRouteAuthConfig;
|
|
1006
|
-
this.playground = playground;
|
|
1007
|
-
this.isDev = isDev;
|
|
1008
|
-
}
|
|
1009
|
-
createContextMiddleware() {
|
|
1010
|
-
return async (c, next) => {
|
|
1011
|
-
let bodyRequestContext;
|
|
1012
|
-
let paramsRequestContext;
|
|
1013
|
-
if (c.req.method === "POST" || c.req.method === "PUT") {
|
|
1014
|
-
const contentType = c.req.header("content-type");
|
|
1015
|
-
if (contentType?.includes("application/json")) {
|
|
1016
|
-
try {
|
|
1017
|
-
const clonedReq = c.req.raw.clone();
|
|
1018
|
-
const body = await clonedReq.json();
|
|
1019
|
-
if (body.requestContext) {
|
|
1020
|
-
bodyRequestContext = body.requestContext;
|
|
1021
|
-
}
|
|
1022
|
-
} catch {
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
if (c.req.method === "GET") {
|
|
1027
|
-
try {
|
|
1028
|
-
const encodedRequestContext = c.req.query("requestContext");
|
|
1029
|
-
if (encodedRequestContext) {
|
|
1030
|
-
try {
|
|
1031
|
-
paramsRequestContext = JSON.parse(encodedRequestContext);
|
|
1032
|
-
} catch {
|
|
1033
|
-
try {
|
|
1034
|
-
const json = Buffer.from(encodedRequestContext, "base64").toString("utf-8");
|
|
1035
|
-
paramsRequestContext = JSON.parse(json);
|
|
1036
|
-
} catch {
|
|
1037
|
-
}
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
} catch {
|
|
1041
|
-
}
|
|
1042
|
-
}
|
|
1043
|
-
const requestContext = this.mergeRequestContext({ paramsRequestContext, bodyRequestContext });
|
|
1044
|
-
c.set("requestContext", requestContext);
|
|
1045
|
-
c.set("mastra", this.mastra);
|
|
1046
|
-
c.set("tools", this.tools || {});
|
|
1047
|
-
c.set("taskStore", this.taskStore);
|
|
1048
|
-
c.set("playground", this.playground === true);
|
|
1049
|
-
c.set("isDev", this.isDev === true);
|
|
1050
|
-
c.set("customRouteAuthConfig", this.customRouteAuthConfig);
|
|
1051
|
-
c.set("abortSignal", c.req.raw.signal);
|
|
1052
|
-
return next();
|
|
1053
|
-
};
|
|
1054
|
-
}
|
|
1055
|
-
async stream(route, res, result) {
|
|
1056
|
-
res.header("Content-Type", "text/plain");
|
|
1057
|
-
res.header("Transfer-Encoding", "chunked");
|
|
1058
|
-
const streamFormat = route.streamFormat || "stream";
|
|
1059
|
-
return stream(
|
|
1060
|
-
res,
|
|
1061
|
-
async (stream2) => {
|
|
1062
|
-
const readableStream = result instanceof ReadableStream ? result : result.fullStream;
|
|
1063
|
-
const reader = readableStream.getReader();
|
|
1064
|
-
stream2.onAbort(() => {
|
|
1065
|
-
void reader.cancel("request aborted");
|
|
1066
|
-
});
|
|
1067
|
-
try {
|
|
1068
|
-
while (true) {
|
|
1069
|
-
const { done, value } = await reader.read();
|
|
1070
|
-
if (done) break;
|
|
1071
|
-
if (value) {
|
|
1072
|
-
if (streamFormat === "sse") {
|
|
1073
|
-
await stream2.write(`data: ${JSON.stringify(value)}
|
|
1074
|
-
|
|
1075
|
-
`);
|
|
1076
|
-
} else {
|
|
1077
|
-
await stream2.write(JSON.stringify(value) + "");
|
|
1078
|
-
}
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
await stream2.write("data: [DONE]\n\n");
|
|
1082
|
-
} catch (error) {
|
|
1083
|
-
console.error(error);
|
|
1084
|
-
} finally {
|
|
1085
|
-
await stream2.close();
|
|
1086
|
-
}
|
|
1087
|
-
},
|
|
1088
|
-
async (err) => {
|
|
1089
|
-
console.error(err);
|
|
1090
|
-
}
|
|
1091
|
-
);
|
|
1092
|
-
}
|
|
1093
|
-
async getParams(route, request) {
|
|
1094
|
-
const urlParams = request.param();
|
|
1095
|
-
const queryParams = request.query();
|
|
1096
|
-
let body;
|
|
1097
|
-
if (route.method === "POST" || route.method === "PUT" || route.method === "PATCH") {
|
|
1098
|
-
try {
|
|
1099
|
-
body = await request.json();
|
|
1100
|
-
} catch {
|
|
918
|
+
if (instances.length > 0) {
|
|
919
|
+
const pos = types.indexOf("object");
|
|
920
|
+
if (pos !== -1) {
|
|
921
|
+
types.splice(pos, 1);
|
|
922
|
+
instances.push("Object");
|
|
1101
923
|
}
|
|
1102
924
|
}
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
const fetchResponse = result;
|
|
1112
|
-
return fetchResponse;
|
|
1113
|
-
} else {
|
|
1114
|
-
return response.status(500);
|
|
1115
|
-
}
|
|
1116
|
-
}
|
|
1117
|
-
async registerRoute(app, route, { prefix }) {
|
|
1118
|
-
const shouldApplyBodyLimit = this.bodyLimitOptions && ["POST", "PUT", "PATCH"].includes(route.method.toUpperCase());
|
|
1119
|
-
const maxSize = route.maxBodySize ?? this.bodyLimitOptions?.maxSize;
|
|
1120
|
-
const middlewares = [];
|
|
1121
|
-
if (shouldApplyBodyLimit && maxSize && this.bodyLimitOptions) {
|
|
1122
|
-
middlewares.push(
|
|
1123
|
-
bodyLimit({
|
|
1124
|
-
maxSize,
|
|
1125
|
-
onError: this.bodyLimitOptions.onError
|
|
1126
|
-
})
|
|
1127
|
-
);
|
|
1128
|
-
}
|
|
1129
|
-
app[route.method.toLowerCase()](
|
|
1130
|
-
`${prefix}${route.path}`,
|
|
1131
|
-
...middlewares,
|
|
1132
|
-
async (c) => {
|
|
1133
|
-
const params = await this.getParams(route, c.req);
|
|
1134
|
-
if (params.queryParams) {
|
|
1135
|
-
try {
|
|
1136
|
-
params.queryParams = await this.parseQueryParams(route, params.queryParams);
|
|
1137
|
-
} catch (error) {
|
|
1138
|
-
console.error("Error parsing query params", error);
|
|
1139
|
-
return c.json(
|
|
1140
|
-
{
|
|
1141
|
-
error: "Invalid query parameters",
|
|
1142
|
-
details: error instanceof Error ? error.message : "Unknown error"
|
|
1143
|
-
},
|
|
1144
|
-
400
|
|
1145
|
-
);
|
|
1146
|
-
}
|
|
1147
|
-
}
|
|
1148
|
-
if (params.body) {
|
|
1149
|
-
try {
|
|
1150
|
-
params.body = await this.parseBody(route, params.body);
|
|
1151
|
-
} catch (error) {
|
|
1152
|
-
console.error("Error parsing body", error);
|
|
1153
|
-
return c.json(
|
|
1154
|
-
{
|
|
1155
|
-
error: "Invalid request body",
|
|
1156
|
-
details: error instanceof Error ? error.message : "Unknown error"
|
|
1157
|
-
},
|
|
1158
|
-
400
|
|
1159
|
-
);
|
|
1160
|
-
}
|
|
1161
|
-
}
|
|
1162
|
-
const handlerParams = {
|
|
1163
|
-
...params.urlParams,
|
|
1164
|
-
...params.queryParams,
|
|
1165
|
-
...typeof params.body === "object" ? params.body : {},
|
|
1166
|
-
requestContext: c.get("requestContext"),
|
|
1167
|
-
mastra: this.mastra,
|
|
1168
|
-
tools: c.get("tools"),
|
|
1169
|
-
taskStore: c.get("taskStore"),
|
|
1170
|
-
abortSignal: c.get("abortSignal")
|
|
1171
|
-
};
|
|
1172
|
-
try {
|
|
1173
|
-
const result = await route.handler(handlerParams);
|
|
1174
|
-
return this.sendResponse(route, c, result);
|
|
1175
|
-
} catch (error) {
|
|
1176
|
-
console.error("Error calling handler", error);
|
|
1177
|
-
if (error && typeof error === "object") {
|
|
1178
|
-
if ("status" in error) {
|
|
1179
|
-
const status = error.status;
|
|
1180
|
-
return c.json({ error: error instanceof Error ? error.message : "Unknown error" }, status);
|
|
1181
|
-
}
|
|
1182
|
-
if ("details" in error && error.details && typeof error.details === "object" && "status" in error.details) {
|
|
1183
|
-
const status = error.details.status;
|
|
1184
|
-
return c.json({ error: error instanceof Error ? error.message : "Unknown error" }, status);
|
|
1185
|
-
}
|
|
1186
|
-
}
|
|
1187
|
-
return c.json({ error: error instanceof Error ? error.message : "Unknown error" }, 500);
|
|
1188
|
-
}
|
|
925
|
+
if (types.length > 0) {
|
|
926
|
+
if (types.length > 2) {
|
|
927
|
+
const last = types.pop();
|
|
928
|
+
msg += `one of type ${types.join(", ")}, or ${last}`;
|
|
929
|
+
} else if (types.length === 2) {
|
|
930
|
+
msg += `one of type ${types[0]} or ${types[1]}`;
|
|
931
|
+
} else {
|
|
932
|
+
msg += `of type ${types[0]}`;
|
|
1189
933
|
}
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
}
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
var e = Symbol("openapi");
|
|
1202
|
-
function w(n) {
|
|
1203
|
-
const { validateResponse: s2, ...o } = n;
|
|
1204
|
-
return Object.assign((async (e2, o2) => {
|
|
1205
|
-
if (await o2(), s2 && n.responses) {
|
|
1206
|
-
const o3 = e2.res.status, a = e2.res.headers.get("content-type");
|
|
1207
|
-
if (o3 && a) {
|
|
1208
|
-
const r = n.responses[o3];
|
|
1209
|
-
if (r && "content" in r && r.content) {
|
|
1210
|
-
const n2 = a.split(";")[0], o4 = r.content[n2];
|
|
1211
|
-
if (o4?.schema && "validator" in o4.schema) try {
|
|
1212
|
-
let t2;
|
|
1213
|
-
const s3 = e2.res.clone();
|
|
1214
|
-
if ("application/json" === n2 ? t2 = await s3.json() : "text/plain" === n2 && (t2 = await s3.text()), !t2) throw new Error("No data to validate!");
|
|
1215
|
-
await o4.schema.validator(t2);
|
|
1216
|
-
} catch (e3) {
|
|
1217
|
-
let n3 = { status: 500, message: "Response validation failed!" };
|
|
1218
|
-
throw "object" == typeof s2 && (n3 = { ...n3, ...s2 }), new HTTPException$1(n3.status, { message: n3.message, cause: e3 });
|
|
1219
|
-
}
|
|
934
|
+
if (instances.length > 0 || other.length > 0)
|
|
935
|
+
msg += " or ";
|
|
936
|
+
}
|
|
937
|
+
if (instances.length > 0) {
|
|
938
|
+
if (instances.length > 2) {
|
|
939
|
+
const last = instances.pop();
|
|
940
|
+
msg += `an instance of ${instances.join(", ")}, or ${last}`;
|
|
941
|
+
} else {
|
|
942
|
+
msg += `an instance of ${instances[0]}`;
|
|
943
|
+
if (instances.length === 2) {
|
|
944
|
+
msg += ` or ${instances[1]}`;
|
|
1220
945
|
}
|
|
1221
946
|
}
|
|
947
|
+
if (other.length > 0)
|
|
948
|
+
msg += " or ";
|
|
1222
949
|
}
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
const t5 = await o2.schema.builder(e2);
|
|
1234
|
-
o2.schema = t5.schema, t5.components && (s2 = { ...s2, ...t5.components });
|
|
950
|
+
if (other.length > 0) {
|
|
951
|
+
if (other.length > 2) {
|
|
952
|
+
const last = other.pop();
|
|
953
|
+
msg += `one of ${other.join(", ")}, or ${last}`;
|
|
954
|
+
} else if (other.length === 2) {
|
|
955
|
+
msg += `one of ${other[0]} or ${other[1]}`;
|
|
956
|
+
} else {
|
|
957
|
+
if (other[0].toLowerCase() !== other[0])
|
|
958
|
+
msg += "an ";
|
|
959
|
+
msg += `${other[0]}`;
|
|
1235
960
|
}
|
|
1236
961
|
}
|
|
962
|
+
msg += `. Received ${determineSpecificType(actual)}`;
|
|
963
|
+
super(msg);
|
|
1237
964
|
}
|
|
1238
|
-
return { docs: o, components: s2 };
|
|
1239
|
-
}
|
|
1240
|
-
|
|
1241
|
-
// src/server/handlers/auth/defaults.ts
|
|
1242
|
-
var defaultAuthConfig = {
|
|
1243
|
-
protected: ["/api/*"],
|
|
1244
|
-
public: ["/api"],
|
|
1245
|
-
// Simple rule system
|
|
1246
|
-
rules: [
|
|
1247
|
-
// Admin users can do anything
|
|
1248
|
-
{
|
|
1249
|
-
condition: (user) => {
|
|
1250
|
-
if (typeof user === "object" && user !== null) {
|
|
1251
|
-
if ("isAdmin" in user) {
|
|
1252
|
-
return !!user.isAdmin;
|
|
1253
|
-
}
|
|
1254
|
-
if ("role" in user) {
|
|
1255
|
-
return user.role === "admin";
|
|
1256
|
-
}
|
|
1257
|
-
}
|
|
1258
|
-
return false;
|
|
1259
|
-
},
|
|
1260
|
-
allow: true
|
|
1261
|
-
}
|
|
1262
|
-
]
|
|
1263
|
-
};
|
|
1264
|
-
|
|
1265
|
-
// src/server/handlers/auth/helpers.ts
|
|
1266
|
-
var isDevPlaygroundRequest = (req) => {
|
|
1267
|
-
return req.header("x-mastra-dev-playground") === "true" && process.env.MASTRA_DEV === "true";
|
|
1268
965
|
};
|
|
1269
|
-
var
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
}
|
|
1273
|
-
const routeKey = `${method}:${path}`;
|
|
1274
|
-
if (customRouteAuthConfig.has(routeKey)) {
|
|
1275
|
-
return !customRouteAuthConfig.get(routeKey);
|
|
1276
|
-
}
|
|
1277
|
-
const allRouteKey = `ALL:${path}`;
|
|
1278
|
-
if (customRouteAuthConfig.has(allRouteKey)) {
|
|
1279
|
-
return !customRouteAuthConfig.get(allRouteKey);
|
|
966
|
+
var ERR_INVALID_HTTP_TOKEN = class extends TypeError {
|
|
967
|
+
constructor(name, field) {
|
|
968
|
+
super(`${name} must be a valid HTTP token ["${field}"]`);
|
|
1280
969
|
}
|
|
1281
|
-
return false;
|
|
1282
|
-
};
|
|
1283
|
-
var isProtectedPath = (path, method, authConfig, customRouteAuthConfig) => {
|
|
1284
|
-
const protectedAccess = [...defaultAuthConfig.protected || [], ...authConfig.protected || []];
|
|
1285
|
-
return isAnyMatch(path, method, protectedAccess) || !isCustomRoutePublic(path, method, customRouteAuthConfig);
|
|
1286
970
|
};
|
|
1287
|
-
var
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
};
|
|
1291
|
-
var isAnyMatch = (path, method, patterns) => {
|
|
1292
|
-
if (!patterns) {
|
|
1293
|
-
return false;
|
|
1294
|
-
}
|
|
1295
|
-
for (const patternPathOrMethod of patterns) {
|
|
1296
|
-
if (patternPathOrMethod instanceof RegExp) {
|
|
1297
|
-
if (patternPathOrMethod.test(path)) {
|
|
1298
|
-
return true;
|
|
1299
|
-
}
|
|
1300
|
-
}
|
|
1301
|
-
if (typeof patternPathOrMethod === "string" && pathMatchesPattern(path, patternPathOrMethod)) {
|
|
1302
|
-
return true;
|
|
1303
|
-
}
|
|
1304
|
-
if (Array.isArray(patternPathOrMethod) && patternPathOrMethod.length === 2) {
|
|
1305
|
-
const [pattern, methodOrMethods] = patternPathOrMethod;
|
|
1306
|
-
if (pathMatchesPattern(path, pattern) && matchesOrIncludes(methodOrMethods, method)) {
|
|
1307
|
-
return true;
|
|
1308
|
-
}
|
|
1309
|
-
}
|
|
971
|
+
var ERR_METHOD_NOT_IMPLEMENTED = class extends Error {
|
|
972
|
+
constructor(methodName) {
|
|
973
|
+
super(`The ${methodName} method is not implemented`);
|
|
1310
974
|
}
|
|
1311
|
-
return false;
|
|
1312
975
|
};
|
|
1313
|
-
var
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
return path.startsWith(prefix);
|
|
976
|
+
var ERR_STREAM_ALREADY_FINISHED = class extends Error {
|
|
977
|
+
constructor(methodName) {
|
|
978
|
+
super(`Cannot call ${methodName} after a stream was finished`);
|
|
1317
979
|
}
|
|
1318
|
-
return path === pattern;
|
|
1319
980
|
};
|
|
1320
|
-
var
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
return pathMatchesPattern(path, rulePath);
|
|
1324
|
-
}
|
|
1325
|
-
if (rulePath instanceof RegExp) {
|
|
1326
|
-
return rulePath.test(path);
|
|
1327
|
-
}
|
|
1328
|
-
if (Array.isArray(rulePath)) {
|
|
1329
|
-
return rulePath.some((p) => pathMatchesPattern(path, p));
|
|
981
|
+
var ERR_STREAM_CANNOT_PIPE = class extends Error {
|
|
982
|
+
constructor() {
|
|
983
|
+
super(`Cannot pipe, not readable`);
|
|
1330
984
|
}
|
|
1331
|
-
return false;
|
|
1332
985
|
};
|
|
1333
|
-
var
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
}
|
|
1337
|
-
if (Array.isArray(values)) {
|
|
1338
|
-
return values.includes(value);
|
|
986
|
+
var ERR_STREAM_DESTROYED = class extends Error {
|
|
987
|
+
constructor(methodName) {
|
|
988
|
+
super(`Cannot call ${methodName} after a stream was destroyed`);
|
|
1339
989
|
}
|
|
1340
|
-
return false;
|
|
1341
990
|
};
|
|
1342
|
-
var
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
if (!pathMatchesRule(path, rule.path)) {
|
|
1346
|
-
continue;
|
|
1347
|
-
}
|
|
1348
|
-
if (rule.methods && !matchesOrIncludes(rule.methods, method)) {
|
|
1349
|
-
continue;
|
|
1350
|
-
}
|
|
1351
|
-
const condition = rule.condition;
|
|
1352
|
-
if (typeof condition === "function") {
|
|
1353
|
-
const allowed = await Promise.resolve().then(() => condition(user)).catch(() => false);
|
|
1354
|
-
if (allowed) {
|
|
1355
|
-
return true;
|
|
1356
|
-
}
|
|
1357
|
-
} else if (rule.allow) {
|
|
1358
|
-
return true;
|
|
1359
|
-
}
|
|
991
|
+
var ERR_STREAM_NULL_VALUES = class extends TypeError {
|
|
992
|
+
constructor() {
|
|
993
|
+
super(`May not write null values to stream`);
|
|
1360
994
|
}
|
|
1361
|
-
return false;
|
|
1362
995
|
};
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
const mastra = c.get("mastra");
|
|
1367
|
-
const authConfig = mastra.getServer()?.auth;
|
|
1368
|
-
const customRouteAuthConfig = c.get("customRouteAuthConfig");
|
|
1369
|
-
if (!authConfig) {
|
|
1370
|
-
return next();
|
|
996
|
+
var ERR_STREAM_WRITE_AFTER_END = class extends Error {
|
|
997
|
+
constructor() {
|
|
998
|
+
super(`write after end`);
|
|
1371
999
|
}
|
|
1372
|
-
|
|
1373
|
-
|
|
1000
|
+
};
|
|
1001
|
+
|
|
1002
|
+
// ../../node_modules/.pnpm/fetch-to-node@2.1.0/node_modules/fetch-to-node/dist/fetch-to-node/http-incoming.js
|
|
1003
|
+
var kHeaders = Symbol("kHeaders");
|
|
1004
|
+
var kHeadersDistinct = Symbol("kHeadersDistinct");
|
|
1005
|
+
var kHeadersCount = Symbol("kHeadersCount");
|
|
1006
|
+
var kTrailers = Symbol("kTrailers");
|
|
1007
|
+
var kTrailersDistinct = Symbol("kTrailersDistinct");
|
|
1008
|
+
var kTrailersCount = Symbol("kTrailersCount");
|
|
1009
|
+
var FetchIncomingMessage = class extends Readable {
|
|
1010
|
+
get socket() {
|
|
1011
|
+
return null;
|
|
1374
1012
|
}
|
|
1375
|
-
|
|
1376
|
-
|
|
1013
|
+
set socket(_val) {
|
|
1014
|
+
throw new ERR_METHOD_NOT_IMPLEMENTED("socket");
|
|
1377
1015
|
}
|
|
1378
|
-
|
|
1379
|
-
|
|
1016
|
+
httpVersionMajor;
|
|
1017
|
+
httpVersionMinor;
|
|
1018
|
+
httpVersion;
|
|
1019
|
+
complete = false;
|
|
1020
|
+
[kHeaders] = null;
|
|
1021
|
+
[kHeadersDistinct] = null;
|
|
1022
|
+
[kHeadersCount] = 0;
|
|
1023
|
+
rawHeaders = [];
|
|
1024
|
+
[kTrailers] = null;
|
|
1025
|
+
[kTrailersDistinct] = null;
|
|
1026
|
+
[kTrailersCount] = 0;
|
|
1027
|
+
rawTrailers = [];
|
|
1028
|
+
joinDuplicateHeaders = false;
|
|
1029
|
+
aborted = false;
|
|
1030
|
+
upgrade = false;
|
|
1031
|
+
// request (server) only
|
|
1032
|
+
url = "";
|
|
1033
|
+
method;
|
|
1034
|
+
// TODO: Support ClientRequest
|
|
1035
|
+
// statusCode = null;
|
|
1036
|
+
// statusMessage = null;
|
|
1037
|
+
// client = socket;
|
|
1038
|
+
_consuming;
|
|
1039
|
+
_dumped;
|
|
1040
|
+
// The underlying ReadableStream
|
|
1041
|
+
_stream = null;
|
|
1042
|
+
constructor() {
|
|
1043
|
+
const streamOptions = {};
|
|
1044
|
+
super(streamOptions);
|
|
1045
|
+
this._readableState.readingMore = true;
|
|
1046
|
+
this._consuming = false;
|
|
1047
|
+
this._dumped = false;
|
|
1380
1048
|
}
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
if (!token && c.req.query("apiKey")) {
|
|
1384
|
-
token = c.req.query("apiKey") || null;
|
|
1049
|
+
get connection() {
|
|
1050
|
+
return null;
|
|
1385
1051
|
}
|
|
1386
|
-
|
|
1387
|
-
|
|
1052
|
+
set connection(_socket) {
|
|
1053
|
+
console.error("No support for IncomingMessage.connection");
|
|
1388
1054
|
}
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
return c.json({ error: "Invalid or expired token" }, 401);
|
|
1055
|
+
get headers() {
|
|
1056
|
+
if (!this[kHeaders]) {
|
|
1057
|
+
this[kHeaders] = {};
|
|
1058
|
+
const src = this.rawHeaders;
|
|
1059
|
+
const dst = this[kHeaders];
|
|
1060
|
+
for (let n = 0; n < this[kHeadersCount]; n += 2) {
|
|
1061
|
+
this._addHeaderLine(src[n], src[n + 1], dst);
|
|
1062
|
+
}
|
|
1398
1063
|
}
|
|
1399
|
-
|
|
1400
|
-
return next();
|
|
1401
|
-
} catch (err) {
|
|
1402
|
-
console.error(err);
|
|
1403
|
-
return c.json({ error: "Invalid or expired token" }, 401);
|
|
1404
|
-
}
|
|
1405
|
-
};
|
|
1406
|
-
var authorizationMiddleware = async (c, next) => {
|
|
1407
|
-
const mastra = c.get("mastra");
|
|
1408
|
-
const authConfig = mastra.getServer()?.auth;
|
|
1409
|
-
const customRouteAuthConfig = c.get("customRouteAuthConfig");
|
|
1410
|
-
if (!authConfig) {
|
|
1411
|
-
return next();
|
|
1064
|
+
return this[kHeaders];
|
|
1412
1065
|
}
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
if (isDevPlaygroundRequest(c.req)) {
|
|
1416
|
-
return next();
|
|
1066
|
+
set headers(val) {
|
|
1067
|
+
this[kHeaders] = val;
|
|
1417
1068
|
}
|
|
1418
|
-
|
|
1419
|
-
|
|
1069
|
+
get headersDistinct() {
|
|
1070
|
+
if (!this[kHeadersDistinct]) {
|
|
1071
|
+
this[kHeadersDistinct] = {};
|
|
1072
|
+
const src = this.rawHeaders;
|
|
1073
|
+
const dst = this[kHeadersDistinct];
|
|
1074
|
+
for (let n = 0; n < this[kHeadersCount]; n += 2) {
|
|
1075
|
+
this._addHeaderLineDistinct(src[n], src[n + 1], dst);
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
return this[kHeadersDistinct];
|
|
1420
1079
|
}
|
|
1421
|
-
|
|
1422
|
-
|
|
1080
|
+
set headersDistinct(val) {
|
|
1081
|
+
this[kHeadersDistinct] = val;
|
|
1423
1082
|
}
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
const
|
|
1428
|
-
|
|
1429
|
-
|
|
1083
|
+
get trailers() {
|
|
1084
|
+
if (!this[kTrailers]) {
|
|
1085
|
+
this[kTrailers] = {};
|
|
1086
|
+
const src = this.rawTrailers;
|
|
1087
|
+
const dst = this[kTrailers];
|
|
1088
|
+
for (let n = 0; n < this[kTrailersCount]; n += 2) {
|
|
1089
|
+
this._addHeaderLine(src[n], src[n + 1], dst);
|
|
1430
1090
|
}
|
|
1431
|
-
return c.json({ error: "Access denied" }, 403);
|
|
1432
|
-
} catch (err) {
|
|
1433
|
-
console.error(err);
|
|
1434
|
-
return c.json({ error: "Authorization error" }, 500);
|
|
1435
1091
|
}
|
|
1092
|
+
return this[kTrailers];
|
|
1436
1093
|
}
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1094
|
+
set trailers(val) {
|
|
1095
|
+
this[kTrailers] = val;
|
|
1096
|
+
}
|
|
1097
|
+
get trailersDistinct() {
|
|
1098
|
+
if (!this[kTrailersDistinct]) {
|
|
1099
|
+
this[kTrailersDistinct] = {};
|
|
1100
|
+
const src = this.rawTrailers;
|
|
1101
|
+
const dst = this[kTrailersDistinct];
|
|
1102
|
+
for (let n = 0; n < this[kTrailersCount]; n += 2) {
|
|
1103
|
+
this._addHeaderLineDistinct(src[n], src[n + 1], dst);
|
|
1442
1104
|
}
|
|
1443
|
-
return c.json({ error: "Access denied" }, 403);
|
|
1444
|
-
} catch (err) {
|
|
1445
|
-
console.error(err);
|
|
1446
|
-
return c.json({ error: "Authorization error" }, 500);
|
|
1447
1105
|
}
|
|
1106
|
+
return this[kTrailersDistinct];
|
|
1448
1107
|
}
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
if (isAuthorized) {
|
|
1452
|
-
return next();
|
|
1453
|
-
}
|
|
1454
|
-
return c.json({ error: "Access denied" }, 403);
|
|
1108
|
+
set trailersDistinct(val) {
|
|
1109
|
+
this[kTrailersDistinct] = val;
|
|
1455
1110
|
}
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
if (isAuthorized) {
|
|
1459
|
-
return next();
|
|
1460
|
-
}
|
|
1111
|
+
setTimeout(msecs, callback) {
|
|
1112
|
+
return this;
|
|
1461
1113
|
}
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
var clients = /* @__PURE__ */ new Set();
|
|
1467
|
-
var hotReloadDisabled = false;
|
|
1468
|
-
function handleClientsRefresh(c) {
|
|
1469
|
-
const stream2 = new ReadableStream({
|
|
1470
|
-
start(controller) {
|
|
1471
|
-
clients.add(controller);
|
|
1472
|
-
controller.enqueue("data: connected\n\n");
|
|
1473
|
-
c.req.raw.signal.addEventListener("abort", () => {
|
|
1474
|
-
clients.delete(controller);
|
|
1475
|
-
});
|
|
1114
|
+
async _read(n) {
|
|
1115
|
+
if (!this._consuming) {
|
|
1116
|
+
this._readableState.readingMore = false;
|
|
1117
|
+
this._consuming = true;
|
|
1476
1118
|
}
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
"Cache-Control": "no-cache",
|
|
1482
|
-
Connection: "keep-alive",
|
|
1483
|
-
"Access-Control-Allow-Origin": "*"
|
|
1119
|
+
if (this._stream == null) {
|
|
1120
|
+
this.complete = true;
|
|
1121
|
+
this.push(null);
|
|
1122
|
+
return;
|
|
1484
1123
|
}
|
|
1485
|
-
|
|
1486
|
-
}
|
|
1487
|
-
function handleTriggerClientsRefresh(c) {
|
|
1488
|
-
clients.forEach((controller) => {
|
|
1124
|
+
const reader = this._stream.getReader();
|
|
1489
1125
|
try {
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1126
|
+
const data = await reader.read();
|
|
1127
|
+
if (data.done) {
|
|
1128
|
+
this.complete = true;
|
|
1129
|
+
this.push(null);
|
|
1130
|
+
} else {
|
|
1131
|
+
this.push(data.value);
|
|
1132
|
+
}
|
|
1133
|
+
} catch (e) {
|
|
1134
|
+
this.destroy(e);
|
|
1135
|
+
} finally {
|
|
1136
|
+
reader.releaseLock();
|
|
1493
1137
|
}
|
|
1494
|
-
}
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
}
|
|
1500
|
-
function handleError(error, defaultMessage) {
|
|
1501
|
-
const apiError = error;
|
|
1502
|
-
throw new HTTPException$1(apiError.status || 500, {
|
|
1503
|
-
message: apiError.message || defaultMessage,
|
|
1504
|
-
cause: apiError.cause
|
|
1505
|
-
});
|
|
1506
|
-
}
|
|
1507
|
-
function errorHandler(err, c, isDev) {
|
|
1508
|
-
if (err instanceof HTTPException$1) {
|
|
1509
|
-
if (isDev) {
|
|
1510
|
-
return c.json({ error: err.message, cause: err.cause, stack: err.stack }, err.status);
|
|
1138
|
+
}
|
|
1139
|
+
_destroy(err, cb) {
|
|
1140
|
+
if (!this.readableEnded || !this.complete) {
|
|
1141
|
+
this.aborted = true;
|
|
1142
|
+
this.emit("aborted");
|
|
1511
1143
|
}
|
|
1512
|
-
|
|
1144
|
+
setTimeout(onError, 0, this, err, cb);
|
|
1513
1145
|
}
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
}
|
|
1522
|
-
var classRegExp = /^([A-Z][a-z0-9]*)+$/;
|
|
1523
|
-
var kTypes = [
|
|
1524
|
-
"string",
|
|
1525
|
-
"function",
|
|
1526
|
-
"number",
|
|
1527
|
-
"object",
|
|
1528
|
-
// Accept 'Function' and 'Object' as alternative to the lower cased version.
|
|
1529
|
-
"Function",
|
|
1530
|
-
"Object",
|
|
1531
|
-
"boolean",
|
|
1532
|
-
"bigint",
|
|
1533
|
-
"symbol"
|
|
1534
|
-
];
|
|
1535
|
-
function determineSpecificType(value) {
|
|
1536
|
-
if (value == null) {
|
|
1537
|
-
return "" + value;
|
|
1538
|
-
}
|
|
1539
|
-
if (typeof value === "function" && value.name) {
|
|
1540
|
-
return `function ${value.name}`;
|
|
1541
|
-
}
|
|
1542
|
-
if (typeof value === "object") {
|
|
1543
|
-
if (value.constructor?.name) {
|
|
1544
|
-
return `an instance of ${value.constructor.name}`;
|
|
1545
|
-
}
|
|
1546
|
-
return `${util.inspect(value, { depth: -1 })}`;
|
|
1547
|
-
}
|
|
1548
|
-
let inspected = util.inspect(value, { colors: false });
|
|
1549
|
-
if (inspected.length > 28) {
|
|
1550
|
-
inspected = `${inspected.slice(0, 25)}...`;
|
|
1551
|
-
}
|
|
1552
|
-
return `type ${typeof value} (${inspected})`;
|
|
1553
|
-
}
|
|
1554
|
-
var ERR_HTTP_BODY_NOT_ALLOWED = class extends Error {
|
|
1555
|
-
constructor() {
|
|
1556
|
-
super("Adding content for this request method or response status is not allowed.");
|
|
1557
|
-
}
|
|
1558
|
-
};
|
|
1559
|
-
var ERR_HTTP_CONTENT_LENGTH_MISMATCH = class extends Error {
|
|
1560
|
-
constructor(actual, expected) {
|
|
1561
|
-
super(`Response body's content-length of ${actual} byte(s) does not match the content-length of ${expected} byte(s) set in header`);
|
|
1562
|
-
}
|
|
1563
|
-
};
|
|
1564
|
-
var ERR_HTTP_HEADERS_SENT = class extends Error {
|
|
1565
|
-
constructor(arg) {
|
|
1566
|
-
super(`Cannot ${arg} headers after they are sent to the client`);
|
|
1567
|
-
}
|
|
1568
|
-
};
|
|
1569
|
-
var ERR_INVALID_ARG_VALUE = class extends TypeError {
|
|
1570
|
-
constructor(name, value, reason = "is invalid") {
|
|
1571
|
-
let inspected = util.inspect(value);
|
|
1572
|
-
if (inspected.length > 128) {
|
|
1573
|
-
inspected = `${inspected.slice(0, 128)}...`;
|
|
1574
|
-
}
|
|
1575
|
-
const type = name.includes(".") ? "property" : "argument";
|
|
1576
|
-
super(`The ${type} '${name}' ${reason}. Received ${inspected}`);
|
|
1577
|
-
}
|
|
1578
|
-
};
|
|
1579
|
-
var ERR_INVALID_CHAR = class extends TypeError {
|
|
1580
|
-
constructor(name, field) {
|
|
1581
|
-
let msg = `Invalid character in ${name}`;
|
|
1582
|
-
if (field !== void 0) {
|
|
1583
|
-
msg += ` ["${field}"]`;
|
|
1584
|
-
}
|
|
1585
|
-
super(msg);
|
|
1586
|
-
}
|
|
1587
|
-
};
|
|
1588
|
-
var ERR_HTTP_INVALID_HEADER_VALUE = class extends TypeError {
|
|
1589
|
-
constructor(value, name) {
|
|
1590
|
-
super(`Invalid value "${value}" for header "${name}"`);
|
|
1591
|
-
}
|
|
1592
|
-
};
|
|
1593
|
-
var ERR_HTTP_INVALID_STATUS_CODE = class extends RangeError {
|
|
1594
|
-
originalStatusCode;
|
|
1595
|
-
constructor(originalStatusCode) {
|
|
1596
|
-
super(`Invalid status code: ${originalStatusCode}`);
|
|
1597
|
-
this.originalStatusCode = originalStatusCode;
|
|
1598
|
-
}
|
|
1599
|
-
};
|
|
1600
|
-
var ERR_HTTP_TRAILER_INVALID = class extends Error {
|
|
1601
|
-
constructor() {
|
|
1602
|
-
super(`Trailers are invalid with this transfer encoding`);
|
|
1603
|
-
}
|
|
1604
|
-
};
|
|
1605
|
-
var ERR_INVALID_ARG_TYPE = class extends TypeError {
|
|
1606
|
-
constructor(name, expected, actual) {
|
|
1607
|
-
if (!Array.isArray(expected)) {
|
|
1608
|
-
expected = [expected];
|
|
1609
|
-
}
|
|
1610
|
-
let msg = "The ";
|
|
1611
|
-
if (name.endsWith(" argument")) {
|
|
1612
|
-
msg += `${name} `;
|
|
1613
|
-
} else {
|
|
1614
|
-
const type = name.includes(".") ? "property" : "argument";
|
|
1615
|
-
msg += `"${name}" ${type} `;
|
|
1616
|
-
}
|
|
1617
|
-
msg += "must be ";
|
|
1618
|
-
const types = [];
|
|
1619
|
-
const instances = [];
|
|
1620
|
-
const other = [];
|
|
1621
|
-
for (const value of expected) {
|
|
1622
|
-
if (kTypes.includes(value)) {
|
|
1623
|
-
types.push(value.toLowerCase());
|
|
1624
|
-
} else if (classRegExp.exec(value) !== null) {
|
|
1625
|
-
instances.push(value);
|
|
1146
|
+
_addHeaderLines(headers, n) {
|
|
1147
|
+
if (headers?.length) {
|
|
1148
|
+
let dest;
|
|
1149
|
+
if (this.complete) {
|
|
1150
|
+
this.rawTrailers = headers;
|
|
1151
|
+
this[kTrailersCount] = n;
|
|
1152
|
+
dest = this[kTrailers];
|
|
1626
1153
|
} else {
|
|
1627
|
-
|
|
1154
|
+
this.rawHeaders = headers;
|
|
1155
|
+
this[kHeadersCount] = n;
|
|
1156
|
+
dest = this[kHeaders];
|
|
1628
1157
|
}
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
types.splice(pos, 1);
|
|
1634
|
-
instances.push("Object");
|
|
1158
|
+
if (dest) {
|
|
1159
|
+
for (let i = 0; i < n; i += 2) {
|
|
1160
|
+
this._addHeaderLine(headers[i], headers[i + 1], dest);
|
|
1161
|
+
}
|
|
1635
1162
|
}
|
|
1636
1163
|
}
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1164
|
+
}
|
|
1165
|
+
// Add the given (field, value) pair to the message
|
|
1166
|
+
//
|
|
1167
|
+
// Per RFC2616, section 4.2 it is acceptable to join multiple instances of the
|
|
1168
|
+
// same header with a ', ' if the header in question supports specification of
|
|
1169
|
+
// multiple values this way. The one exception to this is the Cookie header,
|
|
1170
|
+
// which has multiple values joined with a '; ' instead. If a header's values
|
|
1171
|
+
// cannot be joined in either of these ways, we declare the first instance the
|
|
1172
|
+
// winner and drop the second. Extended header fields (those beginning with
|
|
1173
|
+
// 'x-') are always joined.
|
|
1174
|
+
_addHeaderLine(field, value, dest) {
|
|
1175
|
+
field = matchKnownFields(field);
|
|
1176
|
+
const flag = field.charCodeAt(0);
|
|
1177
|
+
if (flag === 0 || flag === 2) {
|
|
1178
|
+
field = field.slice(1);
|
|
1179
|
+
if (typeof dest[field] === "string") {
|
|
1180
|
+
dest[field] += (flag === 0 ? ", " : "; ") + value;
|
|
1643
1181
|
} else {
|
|
1644
|
-
|
|
1182
|
+
dest[field] = value;
|
|
1645
1183
|
}
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
if (instances.length > 0) {
|
|
1650
|
-
if (instances.length > 2) {
|
|
1651
|
-
const last = instances.pop();
|
|
1652
|
-
msg += `an instance of ${instances.join(", ")}, or ${last}`;
|
|
1184
|
+
} else if (flag === 1) {
|
|
1185
|
+
if (dest["set-cookie"] !== void 0) {
|
|
1186
|
+
dest["set-cookie"].push(value);
|
|
1653
1187
|
} else {
|
|
1654
|
-
|
|
1655
|
-
if (instances.length === 2) {
|
|
1656
|
-
msg += ` or ${instances[1]}`;
|
|
1657
|
-
}
|
|
1188
|
+
dest["set-cookie"] = [value];
|
|
1658
1189
|
}
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
if (other.length > 0) {
|
|
1663
|
-
if (other.length > 2) {
|
|
1664
|
-
const last = other.pop();
|
|
1665
|
-
msg += `one of ${other.join(", ")}, or ${last}`;
|
|
1666
|
-
} else if (other.length === 2) {
|
|
1667
|
-
msg += `one of ${other[0]} or ${other[1]}`;
|
|
1190
|
+
} else if (this.joinDuplicateHeaders) {
|
|
1191
|
+
if (dest[field] === void 0) {
|
|
1192
|
+
dest[field] = value;
|
|
1668
1193
|
} else {
|
|
1669
|
-
|
|
1670
|
-
msg += "an ";
|
|
1671
|
-
msg += `${other[0]}`;
|
|
1194
|
+
dest[field] += ", " + value;
|
|
1672
1195
|
}
|
|
1196
|
+
} else if (dest[field] === void 0) {
|
|
1197
|
+
dest[field] = value;
|
|
1673
1198
|
}
|
|
1674
|
-
msg += `. Received ${determineSpecificType(actual)}`;
|
|
1675
|
-
super(msg);
|
|
1676
|
-
}
|
|
1677
|
-
};
|
|
1678
|
-
var ERR_INVALID_HTTP_TOKEN = class extends TypeError {
|
|
1679
|
-
constructor(name, field) {
|
|
1680
|
-
super(`${name} must be a valid HTTP token ["${field}"]`);
|
|
1681
|
-
}
|
|
1682
|
-
};
|
|
1683
|
-
var ERR_METHOD_NOT_IMPLEMENTED = class extends Error {
|
|
1684
|
-
constructor(methodName) {
|
|
1685
|
-
super(`The ${methodName} method is not implemented`);
|
|
1686
|
-
}
|
|
1687
|
-
};
|
|
1688
|
-
var ERR_STREAM_ALREADY_FINISHED = class extends Error {
|
|
1689
|
-
constructor(methodName) {
|
|
1690
|
-
super(`Cannot call ${methodName} after a stream was finished`);
|
|
1691
|
-
}
|
|
1692
|
-
};
|
|
1693
|
-
var ERR_STREAM_CANNOT_PIPE = class extends Error {
|
|
1694
|
-
constructor() {
|
|
1695
|
-
super(`Cannot pipe, not readable`);
|
|
1696
1199
|
}
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
constructor() {
|
|
1705
|
-
super(`May not write null values to stream`);
|
|
1200
|
+
_addHeaderLineDistinct(field, value, dest) {
|
|
1201
|
+
field = field.toLowerCase();
|
|
1202
|
+
if (!dest[field]) {
|
|
1203
|
+
dest[field] = [value];
|
|
1204
|
+
} else {
|
|
1205
|
+
dest[field].push(value);
|
|
1206
|
+
}
|
|
1706
1207
|
}
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
var kHeaders = Symbol("kHeaders");
|
|
1716
|
-
var kHeadersDistinct = Symbol("kHeadersDistinct");
|
|
1717
|
-
var kHeadersCount = Symbol("kHeadersCount");
|
|
1718
|
-
var kTrailers = Symbol("kTrailers");
|
|
1719
|
-
var kTrailersDistinct = Symbol("kTrailersDistinct");
|
|
1720
|
-
var kTrailersCount = Symbol("kTrailersCount");
|
|
1721
|
-
var FetchIncomingMessage = class extends Readable {
|
|
1722
|
-
get socket() {
|
|
1723
|
-
return null;
|
|
1724
|
-
}
|
|
1725
|
-
set socket(_val) {
|
|
1726
|
-
throw new ERR_METHOD_NOT_IMPLEMENTED("socket");
|
|
1727
|
-
}
|
|
1728
|
-
httpVersionMajor;
|
|
1729
|
-
httpVersionMinor;
|
|
1730
|
-
httpVersion;
|
|
1731
|
-
complete = false;
|
|
1732
|
-
[kHeaders] = null;
|
|
1733
|
-
[kHeadersDistinct] = null;
|
|
1734
|
-
[kHeadersCount] = 0;
|
|
1735
|
-
rawHeaders = [];
|
|
1736
|
-
[kTrailers] = null;
|
|
1737
|
-
[kTrailersDistinct] = null;
|
|
1738
|
-
[kTrailersCount] = 0;
|
|
1739
|
-
rawTrailers = [];
|
|
1740
|
-
joinDuplicateHeaders = false;
|
|
1741
|
-
aborted = false;
|
|
1742
|
-
upgrade = false;
|
|
1743
|
-
// request (server) only
|
|
1744
|
-
url = "";
|
|
1745
|
-
method;
|
|
1746
|
-
// TODO: Support ClientRequest
|
|
1747
|
-
// statusCode = null;
|
|
1748
|
-
// statusMessage = null;
|
|
1749
|
-
// client = socket;
|
|
1750
|
-
_consuming;
|
|
1751
|
-
_dumped;
|
|
1752
|
-
// The underlying ReadableStream
|
|
1753
|
-
_stream = null;
|
|
1754
|
-
constructor() {
|
|
1755
|
-
const streamOptions = {};
|
|
1756
|
-
super(streamOptions);
|
|
1757
|
-
this._readableState.readingMore = true;
|
|
1758
|
-
this._consuming = false;
|
|
1759
|
-
this._dumped = false;
|
|
1760
|
-
}
|
|
1761
|
-
get connection() {
|
|
1762
|
-
return null;
|
|
1763
|
-
}
|
|
1764
|
-
set connection(_socket) {
|
|
1765
|
-
console.error("No support for IncomingMessage.connection");
|
|
1766
|
-
}
|
|
1767
|
-
get headers() {
|
|
1768
|
-
if (!this[kHeaders]) {
|
|
1769
|
-
this[kHeaders] = {};
|
|
1770
|
-
const src = this.rawHeaders;
|
|
1771
|
-
const dst = this[kHeaders];
|
|
1772
|
-
for (let n = 0; n < this[kHeadersCount]; n += 2) {
|
|
1773
|
-
this._addHeaderLine(src[n], src[n + 1], dst);
|
|
1774
|
-
}
|
|
1775
|
-
}
|
|
1776
|
-
return this[kHeaders];
|
|
1777
|
-
}
|
|
1778
|
-
set headers(val) {
|
|
1779
|
-
this[kHeaders] = val;
|
|
1780
|
-
}
|
|
1781
|
-
get headersDistinct() {
|
|
1782
|
-
if (!this[kHeadersDistinct]) {
|
|
1783
|
-
this[kHeadersDistinct] = {};
|
|
1784
|
-
const src = this.rawHeaders;
|
|
1785
|
-
const dst = this[kHeadersDistinct];
|
|
1786
|
-
for (let n = 0; n < this[kHeadersCount]; n += 2) {
|
|
1787
|
-
this._addHeaderLineDistinct(src[n], src[n + 1], dst);
|
|
1788
|
-
}
|
|
1789
|
-
}
|
|
1790
|
-
return this[kHeadersDistinct];
|
|
1791
|
-
}
|
|
1792
|
-
set headersDistinct(val) {
|
|
1793
|
-
this[kHeadersDistinct] = val;
|
|
1794
|
-
}
|
|
1795
|
-
get trailers() {
|
|
1796
|
-
if (!this[kTrailers]) {
|
|
1797
|
-
this[kTrailers] = {};
|
|
1798
|
-
const src = this.rawTrailers;
|
|
1799
|
-
const dst = this[kTrailers];
|
|
1800
|
-
for (let n = 0; n < this[kTrailersCount]; n += 2) {
|
|
1801
|
-
this._addHeaderLine(src[n], src[n + 1], dst);
|
|
1802
|
-
}
|
|
1803
|
-
}
|
|
1804
|
-
return this[kTrailers];
|
|
1805
|
-
}
|
|
1806
|
-
set trailers(val) {
|
|
1807
|
-
this[kTrailers] = val;
|
|
1808
|
-
}
|
|
1809
|
-
get trailersDistinct() {
|
|
1810
|
-
if (!this[kTrailersDistinct]) {
|
|
1811
|
-
this[kTrailersDistinct] = {};
|
|
1812
|
-
const src = this.rawTrailers;
|
|
1813
|
-
const dst = this[kTrailersDistinct];
|
|
1814
|
-
for (let n = 0; n < this[kTrailersCount]; n += 2) {
|
|
1815
|
-
this._addHeaderLineDistinct(src[n], src[n + 1], dst);
|
|
1816
|
-
}
|
|
1817
|
-
}
|
|
1818
|
-
return this[kTrailersDistinct];
|
|
1819
|
-
}
|
|
1820
|
-
set trailersDistinct(val) {
|
|
1821
|
-
this[kTrailersDistinct] = val;
|
|
1822
|
-
}
|
|
1823
|
-
setTimeout(msecs, callback) {
|
|
1824
|
-
return this;
|
|
1825
|
-
}
|
|
1826
|
-
async _read(n) {
|
|
1827
|
-
if (!this._consuming) {
|
|
1828
|
-
this._readableState.readingMore = false;
|
|
1829
|
-
this._consuming = true;
|
|
1830
|
-
}
|
|
1831
|
-
if (this._stream == null) {
|
|
1832
|
-
this.complete = true;
|
|
1833
|
-
this.push(null);
|
|
1834
|
-
return;
|
|
1835
|
-
}
|
|
1836
|
-
const reader = this._stream.getReader();
|
|
1837
|
-
try {
|
|
1838
|
-
const data = await reader.read();
|
|
1839
|
-
if (data.done) {
|
|
1840
|
-
this.complete = true;
|
|
1841
|
-
this.push(null);
|
|
1842
|
-
} else {
|
|
1843
|
-
this.push(data.value);
|
|
1844
|
-
}
|
|
1845
|
-
} catch (e2) {
|
|
1846
|
-
this.destroy(e2);
|
|
1847
|
-
} finally {
|
|
1848
|
-
reader.releaseLock();
|
|
1849
|
-
}
|
|
1850
|
-
}
|
|
1851
|
-
_destroy(err, cb) {
|
|
1852
|
-
if (!this.readableEnded || !this.complete) {
|
|
1853
|
-
this.aborted = true;
|
|
1854
|
-
this.emit("aborted");
|
|
1855
|
-
}
|
|
1856
|
-
setTimeout(onError, 0, this, err, cb);
|
|
1857
|
-
}
|
|
1858
|
-
_addHeaderLines(headers, n) {
|
|
1859
|
-
if (headers?.length) {
|
|
1860
|
-
let dest;
|
|
1861
|
-
if (this.complete) {
|
|
1862
|
-
this.rawTrailers = headers;
|
|
1863
|
-
this[kTrailersCount] = n;
|
|
1864
|
-
dest = this[kTrailers];
|
|
1865
|
-
} else {
|
|
1866
|
-
this.rawHeaders = headers;
|
|
1867
|
-
this[kHeadersCount] = n;
|
|
1868
|
-
dest = this[kHeaders];
|
|
1869
|
-
}
|
|
1870
|
-
if (dest) {
|
|
1871
|
-
for (let i = 0; i < n; i += 2) {
|
|
1872
|
-
this._addHeaderLine(headers[i], headers[i + 1], dest);
|
|
1873
|
-
}
|
|
1874
|
-
}
|
|
1875
|
-
}
|
|
1876
|
-
}
|
|
1877
|
-
// Add the given (field, value) pair to the message
|
|
1878
|
-
//
|
|
1879
|
-
// Per RFC2616, section 4.2 it is acceptable to join multiple instances of the
|
|
1880
|
-
// same header with a ', ' if the header in question supports specification of
|
|
1881
|
-
// multiple values this way. The one exception to this is the Cookie header,
|
|
1882
|
-
// which has multiple values joined with a '; ' instead. If a header's values
|
|
1883
|
-
// cannot be joined in either of these ways, we declare the first instance the
|
|
1884
|
-
// winner and drop the second. Extended header fields (those beginning with
|
|
1885
|
-
// 'x-') are always joined.
|
|
1886
|
-
_addHeaderLine(field, value, dest) {
|
|
1887
|
-
field = matchKnownFields(field);
|
|
1888
|
-
const flag = field.charCodeAt(0);
|
|
1889
|
-
if (flag === 0 || flag === 2) {
|
|
1890
|
-
field = field.slice(1);
|
|
1891
|
-
if (typeof dest[field] === "string") {
|
|
1892
|
-
dest[field] += (flag === 0 ? ", " : "; ") + value;
|
|
1893
|
-
} else {
|
|
1894
|
-
dest[field] = value;
|
|
1895
|
-
}
|
|
1896
|
-
} else if (flag === 1) {
|
|
1897
|
-
if (dest["set-cookie"] !== void 0) {
|
|
1898
|
-
dest["set-cookie"].push(value);
|
|
1899
|
-
} else {
|
|
1900
|
-
dest["set-cookie"] = [value];
|
|
1901
|
-
}
|
|
1902
|
-
} else if (this.joinDuplicateHeaders) {
|
|
1903
|
-
if (dest[field] === void 0) {
|
|
1904
|
-
dest[field] = value;
|
|
1905
|
-
} else {
|
|
1906
|
-
dest[field] += ", " + value;
|
|
1907
|
-
}
|
|
1908
|
-
} else if (dest[field] === void 0) {
|
|
1909
|
-
dest[field] = value;
|
|
1910
|
-
}
|
|
1911
|
-
}
|
|
1912
|
-
_addHeaderLineDistinct(field, value, dest) {
|
|
1913
|
-
field = field.toLowerCase();
|
|
1914
|
-
if (!dest[field]) {
|
|
1915
|
-
dest[field] = [value];
|
|
1916
|
-
} else {
|
|
1917
|
-
dest[field].push(value);
|
|
1918
|
-
}
|
|
1919
|
-
}
|
|
1920
|
-
// Call this instead of resume() if we want to just
|
|
1921
|
-
// dump all the data to /dev/null
|
|
1922
|
-
_dump() {
|
|
1923
|
-
if (!this._dumped) {
|
|
1924
|
-
this._dumped = true;
|
|
1925
|
-
this.removeAllListeners("data");
|
|
1926
|
-
this.resume();
|
|
1927
|
-
}
|
|
1208
|
+
// Call this instead of resume() if we want to just
|
|
1209
|
+
// dump all the data to /dev/null
|
|
1210
|
+
_dump() {
|
|
1211
|
+
if (!this._dumped) {
|
|
1212
|
+
this._dumped = true;
|
|
1213
|
+
this.removeAllListeners("data");
|
|
1214
|
+
this.resume();
|
|
1215
|
+
}
|
|
1928
1216
|
}
|
|
1929
1217
|
};
|
|
1930
1218
|
function matchKnownFields(field, lowercased = false) {
|
|
@@ -2115,11 +1403,11 @@ var kRejectNonStandardBodyWrites = Symbol("kRejectNonStandardBodyWrites");
|
|
|
2115
1403
|
var nop = () => {
|
|
2116
1404
|
};
|
|
2117
1405
|
var RE_CONN_CLOSE = /(?:^|\W)close(?:$|\W)/i;
|
|
2118
|
-
function isCookieField(
|
|
2119
|
-
return
|
|
1406
|
+
function isCookieField(s) {
|
|
1407
|
+
return s.length === 6 && s.toLowerCase() === "cookie";
|
|
2120
1408
|
}
|
|
2121
|
-
function isContentDispositionField(
|
|
2122
|
-
return
|
|
1409
|
+
function isContentDispositionField(s) {
|
|
1410
|
+
return s.length === 19 && s.toLowerCase() === "content-disposition";
|
|
2123
1411
|
}
|
|
2124
1412
|
var WrittenDataBuffer = class {
|
|
2125
1413
|
[kCorked] = 0;
|
|
@@ -3169,22 +2457,22 @@ var FetchServerResponse = class _FetchServerResponse extends FetchOutgoingMessag
|
|
|
3169
2457
|
finished = true;
|
|
3170
2458
|
});
|
|
3171
2459
|
const initialDataChunks = [];
|
|
3172
|
-
const initialDataWrittenHandler = (
|
|
2460
|
+
const initialDataWrittenHandler = (e) => {
|
|
3173
2461
|
if (finished) {
|
|
3174
2462
|
return;
|
|
3175
2463
|
}
|
|
3176
|
-
initialDataChunks[
|
|
2464
|
+
initialDataChunks[e.index] = this.dataFromDataWrittenEvent(e);
|
|
3177
2465
|
};
|
|
3178
2466
|
this.on("_dataWritten", initialDataWrittenHandler);
|
|
3179
|
-
this.on("_headersSent", (
|
|
2467
|
+
this.on("_headersSent", (e) => {
|
|
3180
2468
|
this.off("_dataWritten", initialDataWrittenHandler);
|
|
3181
|
-
const { statusCode, statusMessage, headers } =
|
|
2469
|
+
const { statusCode, statusMessage, headers } = e;
|
|
3182
2470
|
resolve(this._toFetchResponse(statusCode, statusMessage, headers, initialDataChunks, finished));
|
|
3183
2471
|
});
|
|
3184
2472
|
});
|
|
3185
2473
|
}
|
|
3186
|
-
dataFromDataWrittenEvent(
|
|
3187
|
-
const { index, entry } =
|
|
2474
|
+
dataFromDataWrittenEvent(e) {
|
|
2475
|
+
const { index, entry } = e;
|
|
3188
2476
|
let { data, encoding } = entry;
|
|
3189
2477
|
if (index === 0) {
|
|
3190
2478
|
if (typeof data !== "string") {
|
|
@@ -3197,449 +2485,771 @@ var FetchServerResponse = class _FetchServerResponse extends FetchOutgoingMessag
|
|
|
3197
2485
|
if (encoding === void 0 || encoding === "utf8" || encoding === "utf-8") {
|
|
3198
2486
|
data = _FetchServerResponse.encoder.encode(data);
|
|
3199
2487
|
} else {
|
|
3200
|
-
data = Buffer$1.from(data, encoding ?? void 0);
|
|
2488
|
+
data = Buffer$1.from(data, encoding ?? void 0);
|
|
2489
|
+
}
|
|
2490
|
+
}
|
|
2491
|
+
return data ?? Buffer$1.from([]);
|
|
2492
|
+
}
|
|
2493
|
+
_finish() {
|
|
2494
|
+
super._finish();
|
|
2495
|
+
}
|
|
2496
|
+
assignSocket(socket) {
|
|
2497
|
+
throw new ERR_METHOD_NOT_IMPLEMENTED("assignSocket");
|
|
2498
|
+
}
|
|
2499
|
+
detachSocket(socket) {
|
|
2500
|
+
throw new ERR_METHOD_NOT_IMPLEMENTED("detachSocket");
|
|
2501
|
+
}
|
|
2502
|
+
writeContinue(callback) {
|
|
2503
|
+
this._writeRaw("HTTP/1.1 100 Continue\r\n\r\n", "ascii", callback);
|
|
2504
|
+
this._sent100 = true;
|
|
2505
|
+
}
|
|
2506
|
+
writeProcessing(callback) {
|
|
2507
|
+
this._writeRaw("HTTP/1.1 102 Processing\r\n\r\n", "ascii", callback);
|
|
2508
|
+
}
|
|
2509
|
+
writeEarlyHints(hints, callback) {
|
|
2510
|
+
let head = "HTTP/1.1 103 Early Hints\r\n";
|
|
2511
|
+
if (hints.link === null || hints.link === void 0) {
|
|
2512
|
+
return;
|
|
2513
|
+
}
|
|
2514
|
+
const link = validateLinkHeaderValue(hints.link);
|
|
2515
|
+
if (link.length === 0) {
|
|
2516
|
+
return;
|
|
2517
|
+
}
|
|
2518
|
+
head += "Link: " + link + "\r\n";
|
|
2519
|
+
for (const key of Object.keys(hints)) {
|
|
2520
|
+
if (key !== "link") {
|
|
2521
|
+
head += key + ": " + hints[key] + "\r\n";
|
|
2522
|
+
}
|
|
2523
|
+
}
|
|
2524
|
+
head += "\r\n";
|
|
2525
|
+
this._writeRaw(head, "ascii", callback);
|
|
2526
|
+
}
|
|
2527
|
+
_implicitHeader() {
|
|
2528
|
+
this.writeHead(this.statusCode);
|
|
2529
|
+
}
|
|
2530
|
+
writeHead(statusCode, reason, obj) {
|
|
2531
|
+
if (this._header) {
|
|
2532
|
+
throw new ERR_HTTP_HEADERS_SENT("write");
|
|
2533
|
+
}
|
|
2534
|
+
const originalStatusCode = statusCode;
|
|
2535
|
+
statusCode |= 0;
|
|
2536
|
+
if (statusCode < 100 || statusCode > 999) {
|
|
2537
|
+
throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
|
|
2538
|
+
}
|
|
2539
|
+
if (typeof reason === "string") {
|
|
2540
|
+
this.statusMessage = reason;
|
|
2541
|
+
} else {
|
|
2542
|
+
this.statusMessage ||= STATUS_CODES[statusCode] || "unknown";
|
|
2543
|
+
obj ??= reason;
|
|
2544
|
+
}
|
|
2545
|
+
this.statusCode = statusCode;
|
|
2546
|
+
let headers;
|
|
2547
|
+
if (this[kOutHeaders]) {
|
|
2548
|
+
let k;
|
|
2549
|
+
if (Array.isArray(obj)) {
|
|
2550
|
+
if (obj.length % 2 !== 0) {
|
|
2551
|
+
throw new ERR_INVALID_ARG_VALUE("headers", obj);
|
|
2552
|
+
}
|
|
2553
|
+
for (let n = 0; n < obj.length; n += 2) {
|
|
2554
|
+
k = obj[n + 0];
|
|
2555
|
+
this.removeHeader(String(k));
|
|
2556
|
+
}
|
|
2557
|
+
for (let n = 0; n < obj.length; n += 2) {
|
|
2558
|
+
k = obj[n];
|
|
2559
|
+
if (k) {
|
|
2560
|
+
this.appendHeader(String(k), obj[n + 1]);
|
|
2561
|
+
}
|
|
2562
|
+
}
|
|
2563
|
+
} else if (obj) {
|
|
2564
|
+
const keys = Object.keys(obj);
|
|
2565
|
+
for (let i = 0; i < keys.length; i++) {
|
|
2566
|
+
k = keys[i];
|
|
2567
|
+
if (k) {
|
|
2568
|
+
this.setHeader(k, obj[k]);
|
|
2569
|
+
}
|
|
2570
|
+
}
|
|
2571
|
+
}
|
|
2572
|
+
headers = this[kOutHeaders];
|
|
2573
|
+
} else {
|
|
2574
|
+
headers = obj;
|
|
2575
|
+
}
|
|
2576
|
+
if (checkInvalidHeaderChar2(this.statusMessage)) {
|
|
2577
|
+
throw new ERR_INVALID_CHAR("statusMessage");
|
|
2578
|
+
}
|
|
2579
|
+
const statusLine = `HTTP/1.1 ${statusCode} ${this.statusMessage}\r
|
|
2580
|
+
`;
|
|
2581
|
+
if (statusCode === 204 || statusCode === 304 || statusCode >= 100 && statusCode <= 199) {
|
|
2582
|
+
this._hasBody = false;
|
|
2583
|
+
}
|
|
2584
|
+
if (this._expect_continue && !this._sent100) {
|
|
2585
|
+
this.shouldKeepAlive = false;
|
|
2586
|
+
}
|
|
2587
|
+
const convertedHeaders = headers && !Array.isArray(headers) ? headers : headers;
|
|
2588
|
+
this._storeHeader(statusLine, convertedHeaders ?? null);
|
|
2589
|
+
return this;
|
|
2590
|
+
}
|
|
2591
|
+
// Docs-only deprecated: DEP0063
|
|
2592
|
+
writeHeader = this.writeHead;
|
|
2593
|
+
fetchResponse;
|
|
2594
|
+
_toFetchResponse(status, statusText, sentHeaders, initialDataChunks, finished) {
|
|
2595
|
+
const headers = new Headers();
|
|
2596
|
+
for (const [header, value] of sentHeaders) {
|
|
2597
|
+
headers.append(header, value);
|
|
2598
|
+
}
|
|
2599
|
+
const _this = this;
|
|
2600
|
+
let body = this._hasBody ? new ReadableStream({
|
|
2601
|
+
start(controller) {
|
|
2602
|
+
for (const dataChunk of initialDataChunks) {
|
|
2603
|
+
controller.enqueue(dataChunk);
|
|
2604
|
+
}
|
|
2605
|
+
if (finished) {
|
|
2606
|
+
controller.close();
|
|
2607
|
+
} else {
|
|
2608
|
+
_this.on("finish", () => {
|
|
2609
|
+
finished = true;
|
|
2610
|
+
controller.close();
|
|
2611
|
+
});
|
|
2612
|
+
_this.on("_dataWritten", (e) => {
|
|
2613
|
+
if (finished) {
|
|
2614
|
+
return;
|
|
2615
|
+
}
|
|
2616
|
+
const data = _this.dataFromDataWrittenEvent(e);
|
|
2617
|
+
controller.enqueue(data);
|
|
2618
|
+
});
|
|
2619
|
+
}
|
|
2620
|
+
}
|
|
2621
|
+
}) : null;
|
|
2622
|
+
if (body != null && typeof FixedLengthStream !== "undefined") {
|
|
2623
|
+
const contentLength = parseInt(headers.get("content-length") ?? "", 10);
|
|
2624
|
+
if (contentLength >= 0) {
|
|
2625
|
+
body = body.pipeThrough(new FixedLengthStream(contentLength));
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
return new Response(body, {
|
|
2629
|
+
status,
|
|
2630
|
+
statusText,
|
|
2631
|
+
headers
|
|
2632
|
+
});
|
|
2633
|
+
}
|
|
2634
|
+
};
|
|
2635
|
+
function toReqRes(req, options) {
|
|
2636
|
+
const { createIncomingMessage = () => new FetchIncomingMessage(), createServerResponse = (incoming2) => new FetchServerResponse(incoming2), ctx } = {};
|
|
2637
|
+
const incoming = createIncomingMessage(ctx);
|
|
2638
|
+
const serverResponse = createServerResponse(incoming, ctx);
|
|
2639
|
+
const reqUrl = new URL(req.url);
|
|
2640
|
+
const versionMajor = 1;
|
|
2641
|
+
const versionMinor = 1;
|
|
2642
|
+
incoming.httpVersionMajor = versionMajor;
|
|
2643
|
+
incoming.httpVersionMinor = versionMinor;
|
|
2644
|
+
incoming.httpVersion = `${versionMajor}.${versionMinor}`;
|
|
2645
|
+
incoming.url = reqUrl.pathname + reqUrl.search;
|
|
2646
|
+
incoming.upgrade = false;
|
|
2647
|
+
const headers = [];
|
|
2648
|
+
for (const [headerName, headerValue] of req.headers) {
|
|
2649
|
+
headers.push(headerName);
|
|
2650
|
+
headers.push(headerValue);
|
|
2651
|
+
}
|
|
2652
|
+
incoming._addHeaderLines(headers, headers.length);
|
|
2653
|
+
incoming.method = req.method;
|
|
2654
|
+
incoming._stream = req.body;
|
|
2655
|
+
return {
|
|
2656
|
+
req: incoming,
|
|
2657
|
+
res: serverResponse
|
|
2658
|
+
};
|
|
2659
|
+
}
|
|
2660
|
+
function toFetchResponse(res) {
|
|
2661
|
+
if (!(res instanceof FetchServerResponse)) {
|
|
2662
|
+
throw new Error("toFetchResponse must be called on a ServerResponse generated by toReqRes");
|
|
2663
|
+
}
|
|
2664
|
+
return res.fetchResponse;
|
|
2665
|
+
}
|
|
2666
|
+
var HTTPException = class extends Error {
|
|
2667
|
+
res;
|
|
2668
|
+
status;
|
|
2669
|
+
constructor(status = 500, options) {
|
|
2670
|
+
super(options?.message, { cause: options?.cause });
|
|
2671
|
+
this.res = options?.res;
|
|
2672
|
+
this.status = status;
|
|
2673
|
+
}
|
|
2674
|
+
getResponse() {
|
|
2675
|
+
if (this.res) {
|
|
2676
|
+
const newResponse = new Response(this.res.body, {
|
|
2677
|
+
status: this.status,
|
|
2678
|
+
headers: this.res.headers
|
|
2679
|
+
});
|
|
2680
|
+
return newResponse;
|
|
2681
|
+
}
|
|
2682
|
+
return new Response(this.message, {
|
|
2683
|
+
status: this.status
|
|
2684
|
+
});
|
|
2685
|
+
}
|
|
2686
|
+
};
|
|
2687
|
+
var ERROR_MESSAGE = "Payload Too Large";
|
|
2688
|
+
var BodyLimitError = class extends Error {
|
|
2689
|
+
constructor(message) {
|
|
2690
|
+
super(message);
|
|
2691
|
+
this.name = "BodyLimitError";
|
|
2692
|
+
}
|
|
2693
|
+
};
|
|
2694
|
+
var bodyLimit = (options) => {
|
|
2695
|
+
const onError3 = options.onError || (() => {
|
|
2696
|
+
const res = new Response(ERROR_MESSAGE, {
|
|
2697
|
+
status: 413
|
|
2698
|
+
});
|
|
2699
|
+
throw new HTTPException(413, { res });
|
|
2700
|
+
});
|
|
2701
|
+
const maxSize = options.maxSize;
|
|
2702
|
+
return async function bodyLimit2(c, next) {
|
|
2703
|
+
if (!c.req.raw.body) {
|
|
2704
|
+
return next();
|
|
2705
|
+
}
|
|
2706
|
+
const hasTransferEncoding = c.req.raw.headers.has("transfer-encoding");
|
|
2707
|
+
const hasContentLength = c.req.raw.headers.has("content-length");
|
|
2708
|
+
if (hasContentLength && !hasTransferEncoding) {
|
|
2709
|
+
const contentLength = parseInt(c.req.raw.headers.get("content-length") || "0", 10);
|
|
2710
|
+
return contentLength > maxSize ? onError3(c) : next();
|
|
2711
|
+
}
|
|
2712
|
+
let size = 0;
|
|
2713
|
+
const rawReader = c.req.raw.body.getReader();
|
|
2714
|
+
const reader = new ReadableStream({
|
|
2715
|
+
async start(controller) {
|
|
2716
|
+
try {
|
|
2717
|
+
for (; ; ) {
|
|
2718
|
+
const { done, value } = await rawReader.read();
|
|
2719
|
+
if (done) {
|
|
2720
|
+
break;
|
|
2721
|
+
}
|
|
2722
|
+
size += value.length;
|
|
2723
|
+
if (size > maxSize) {
|
|
2724
|
+
controller.error(new BodyLimitError(ERROR_MESSAGE));
|
|
2725
|
+
break;
|
|
2726
|
+
}
|
|
2727
|
+
controller.enqueue(value);
|
|
2728
|
+
}
|
|
2729
|
+
} finally {
|
|
2730
|
+
controller.close();
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
});
|
|
2734
|
+
const requestInit = { body: reader, duplex: "half" };
|
|
2735
|
+
c.req.raw = new Request(c.req.raw, requestInit);
|
|
2736
|
+
await next();
|
|
2737
|
+
if (c.error instanceof BodyLimitError) {
|
|
2738
|
+
c.res = await onError3(c);
|
|
2739
|
+
}
|
|
2740
|
+
};
|
|
2741
|
+
};
|
|
2742
|
+
var StreamingApi = class {
|
|
2743
|
+
writer;
|
|
2744
|
+
encoder;
|
|
2745
|
+
writable;
|
|
2746
|
+
abortSubscribers = [];
|
|
2747
|
+
responseReadable;
|
|
2748
|
+
aborted = false;
|
|
2749
|
+
closed = false;
|
|
2750
|
+
constructor(writable, _readable) {
|
|
2751
|
+
this.writable = writable;
|
|
2752
|
+
this.writer = writable.getWriter();
|
|
2753
|
+
this.encoder = new TextEncoder();
|
|
2754
|
+
const reader = _readable.getReader();
|
|
2755
|
+
this.abortSubscribers.push(async () => {
|
|
2756
|
+
await reader.cancel();
|
|
2757
|
+
});
|
|
2758
|
+
this.responseReadable = new ReadableStream({
|
|
2759
|
+
async pull(controller) {
|
|
2760
|
+
const { done, value } = await reader.read();
|
|
2761
|
+
done ? controller.close() : controller.enqueue(value);
|
|
2762
|
+
},
|
|
2763
|
+
cancel: () => {
|
|
2764
|
+
this.abort();
|
|
2765
|
+
}
|
|
2766
|
+
});
|
|
2767
|
+
}
|
|
2768
|
+
async write(input) {
|
|
2769
|
+
try {
|
|
2770
|
+
if (typeof input === "string") {
|
|
2771
|
+
input = this.encoder.encode(input);
|
|
2772
|
+
}
|
|
2773
|
+
await this.writer.write(input);
|
|
2774
|
+
} catch {
|
|
2775
|
+
}
|
|
2776
|
+
return this;
|
|
2777
|
+
}
|
|
2778
|
+
async writeln(input) {
|
|
2779
|
+
await this.write(input + "\n");
|
|
2780
|
+
return this;
|
|
2781
|
+
}
|
|
2782
|
+
sleep(ms) {
|
|
2783
|
+
return new Promise((res) => setTimeout(res, ms));
|
|
2784
|
+
}
|
|
2785
|
+
async close() {
|
|
2786
|
+
try {
|
|
2787
|
+
await this.writer.close();
|
|
2788
|
+
} catch {
|
|
2789
|
+
}
|
|
2790
|
+
this.closed = true;
|
|
2791
|
+
}
|
|
2792
|
+
async pipe(body) {
|
|
2793
|
+
this.writer.releaseLock();
|
|
2794
|
+
await body.pipeTo(this.writable, { preventClose: true });
|
|
2795
|
+
this.writer = this.writable.getWriter();
|
|
2796
|
+
}
|
|
2797
|
+
onAbort(listener) {
|
|
2798
|
+
this.abortSubscribers.push(listener);
|
|
2799
|
+
}
|
|
2800
|
+
abort() {
|
|
2801
|
+
if (!this.aborted) {
|
|
2802
|
+
this.aborted = true;
|
|
2803
|
+
this.abortSubscribers.forEach((subscriber) => subscriber());
|
|
2804
|
+
}
|
|
2805
|
+
}
|
|
2806
|
+
};
|
|
2807
|
+
var isOldBunVersion = () => {
|
|
2808
|
+
const version = typeof Bun !== "undefined" ? Bun.version : void 0;
|
|
2809
|
+
if (version === void 0) {
|
|
2810
|
+
return false;
|
|
2811
|
+
}
|
|
2812
|
+
const result = version.startsWith("1.1") || version.startsWith("1.0") || version.startsWith("0.");
|
|
2813
|
+
isOldBunVersion = () => result;
|
|
2814
|
+
return result;
|
|
2815
|
+
};
|
|
2816
|
+
var contextStash = /* @__PURE__ */ new WeakMap();
|
|
2817
|
+
var stream = (c, cb, onError3) => {
|
|
2818
|
+
const { readable, writable } = new TransformStream();
|
|
2819
|
+
const stream2 = new StreamingApi(writable, readable);
|
|
2820
|
+
if (isOldBunVersion()) {
|
|
2821
|
+
c.req.raw.signal.addEventListener("abort", () => {
|
|
2822
|
+
if (!stream2.closed) {
|
|
2823
|
+
stream2.abort();
|
|
2824
|
+
}
|
|
2825
|
+
});
|
|
2826
|
+
}
|
|
2827
|
+
contextStash.set(stream2.responseReadable, c);
|
|
2828
|
+
(async () => {
|
|
2829
|
+
try {
|
|
2830
|
+
await cb(stream2);
|
|
2831
|
+
} catch (e) {
|
|
2832
|
+
if (e === void 0) ;
|
|
2833
|
+
else if (e instanceof Error && onError3) {
|
|
2834
|
+
await onError3(e, stream2);
|
|
2835
|
+
} else {
|
|
2836
|
+
console.error(e);
|
|
3201
2837
|
}
|
|
2838
|
+
} finally {
|
|
2839
|
+
stream2.close();
|
|
3202
2840
|
}
|
|
3203
|
-
|
|
2841
|
+
})();
|
|
2842
|
+
return c.newResponse(stream2.responseReadable);
|
|
2843
|
+
};
|
|
2844
|
+
var authenticationMiddleware = async (c, next) => {
|
|
2845
|
+
const mastra = c.get("mastra");
|
|
2846
|
+
const authConfig = mastra.getServer()?.auth;
|
|
2847
|
+
const customRouteAuthConfig = c.get("customRouteAuthConfig");
|
|
2848
|
+
if (!authConfig) {
|
|
2849
|
+
return next();
|
|
3204
2850
|
}
|
|
3205
|
-
|
|
3206
|
-
|
|
2851
|
+
const path = c.req.path;
|
|
2852
|
+
const method = c.req.method;
|
|
2853
|
+
const getHeader = (name) => c.req.header(name);
|
|
2854
|
+
if (isDevPlaygroundRequest(path, method, getHeader, authConfig)) {
|
|
2855
|
+
return next();
|
|
3207
2856
|
}
|
|
3208
|
-
|
|
3209
|
-
|
|
2857
|
+
if (!isProtectedPath(c.req.path, c.req.method, authConfig, customRouteAuthConfig)) {
|
|
2858
|
+
return next();
|
|
3210
2859
|
}
|
|
3211
|
-
|
|
3212
|
-
|
|
2860
|
+
if (canAccessPublicly(c.req.path, c.req.method, authConfig)) {
|
|
2861
|
+
return next();
|
|
3213
2862
|
}
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
|
|
2863
|
+
const authHeader = c.req.header("Authorization");
|
|
2864
|
+
let token = authHeader ? authHeader.replace("Bearer ", "") : null;
|
|
2865
|
+
if (!token && c.req.query("apiKey")) {
|
|
2866
|
+
token = c.req.query("apiKey") || null;
|
|
3217
2867
|
}
|
|
3218
|
-
|
|
3219
|
-
|
|
2868
|
+
if (!token) {
|
|
2869
|
+
return c.json({ error: "Authentication required" }, 401);
|
|
3220
2870
|
}
|
|
3221
|
-
|
|
3222
|
-
let
|
|
3223
|
-
if (
|
|
3224
|
-
|
|
2871
|
+
try {
|
|
2872
|
+
let user;
|
|
2873
|
+
if (typeof authConfig.authenticateToken === "function") {
|
|
2874
|
+
user = await authConfig.authenticateToken(token, c.req);
|
|
2875
|
+
} else {
|
|
2876
|
+
throw new Error("No token verification method configured");
|
|
3225
2877
|
}
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
return;
|
|
2878
|
+
if (!user) {
|
|
2879
|
+
return c.json({ error: "Invalid or expired token" }, 401);
|
|
3229
2880
|
}
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
2881
|
+
c.get("requestContext").set("user", user);
|
|
2882
|
+
return next();
|
|
2883
|
+
} catch (err) {
|
|
2884
|
+
console.error(err);
|
|
2885
|
+
return c.json({ error: "Invalid or expired token" }, 401);
|
|
2886
|
+
}
|
|
2887
|
+
};
|
|
2888
|
+
var authorizationMiddleware = async (c, next) => {
|
|
2889
|
+
const mastra = c.get("mastra");
|
|
2890
|
+
const authConfig = mastra.getServer()?.auth;
|
|
2891
|
+
const customRouteAuthConfig = c.get("customRouteAuthConfig");
|
|
2892
|
+
if (!authConfig) {
|
|
2893
|
+
return next();
|
|
2894
|
+
}
|
|
2895
|
+
const path = c.req.path;
|
|
2896
|
+
const method = c.req.method;
|
|
2897
|
+
const getHeader = (name) => c.req.header(name);
|
|
2898
|
+
if (isDevPlaygroundRequest(path, method, getHeader, authConfig)) {
|
|
2899
|
+
return next();
|
|
2900
|
+
}
|
|
2901
|
+
if (!isProtectedPath(c.req.path, c.req.method, authConfig, customRouteAuthConfig)) {
|
|
2902
|
+
return next();
|
|
2903
|
+
}
|
|
2904
|
+
if (canAccessPublicly(path, method, authConfig)) {
|
|
2905
|
+
return next();
|
|
2906
|
+
}
|
|
2907
|
+
const user = c.get("requestContext").get("user");
|
|
2908
|
+
if ("authorizeUser" in authConfig && typeof authConfig.authorizeUser === "function") {
|
|
2909
|
+
try {
|
|
2910
|
+
const isAuthorized = await authConfig.authorizeUser(user, c.req);
|
|
2911
|
+
if (isAuthorized) {
|
|
2912
|
+
return next();
|
|
3234
2913
|
}
|
|
2914
|
+
return c.json({ error: "Access denied" }, 403);
|
|
2915
|
+
} catch (err) {
|
|
2916
|
+
console.error(err);
|
|
2917
|
+
return c.json({ error: "Authorization error" }, 500);
|
|
3235
2918
|
}
|
|
3236
|
-
head += "\r\n";
|
|
3237
|
-
this._writeRaw(head, "ascii", callback);
|
|
3238
|
-
}
|
|
3239
|
-
_implicitHeader() {
|
|
3240
|
-
this.writeHead(this.statusCode);
|
|
3241
2919
|
}
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
2920
|
+
if ("authorize" in authConfig && typeof authConfig.authorize === "function") {
|
|
2921
|
+
try {
|
|
2922
|
+
const isAuthorized = await authConfig.authorize(path, method, user, c);
|
|
2923
|
+
if (isAuthorized) {
|
|
2924
|
+
return next();
|
|
2925
|
+
}
|
|
2926
|
+
return c.json({ error: "Access denied" }, 403);
|
|
2927
|
+
} catch (err) {
|
|
2928
|
+
console.error(err);
|
|
2929
|
+
return c.json({ error: "Authorization error" }, 500);
|
|
3245
2930
|
}
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
2931
|
+
}
|
|
2932
|
+
if ("rules" in authConfig && authConfig.rules && authConfig.rules.length > 0) {
|
|
2933
|
+
const isAuthorized = await checkRules(authConfig.rules, path, method, user);
|
|
2934
|
+
if (isAuthorized) {
|
|
2935
|
+
return next();
|
|
3250
2936
|
}
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3255
|
-
|
|
2937
|
+
return c.json({ error: "Access denied" }, 403);
|
|
2938
|
+
}
|
|
2939
|
+
if (defaultAuthConfig.rules && defaultAuthConfig.rules.length > 0) {
|
|
2940
|
+
const isAuthorized = await checkRules(defaultAuthConfig.rules, path, method, user);
|
|
2941
|
+
if (isAuthorized) {
|
|
2942
|
+
return next();
|
|
3256
2943
|
}
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
3261
|
-
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
2944
|
+
}
|
|
2945
|
+
return c.json({ error: "Access denied" }, 403);
|
|
2946
|
+
};
|
|
2947
|
+
var MastraServer = class extends MastraServer$1 {
|
|
2948
|
+
createContextMiddleware() {
|
|
2949
|
+
return async (c, next) => {
|
|
2950
|
+
let bodyRequestContext;
|
|
2951
|
+
let paramsRequestContext;
|
|
2952
|
+
if (c.req.method === "POST" || c.req.method === "PUT") {
|
|
2953
|
+
const contentType = c.req.header("content-type");
|
|
2954
|
+
if (contentType?.includes("application/json")) {
|
|
2955
|
+
try {
|
|
2956
|
+
const clonedReq = c.req.raw.clone();
|
|
2957
|
+
const body = await clonedReq.json();
|
|
2958
|
+
if (body.requestContext) {
|
|
2959
|
+
bodyRequestContext = body.requestContext;
|
|
2960
|
+
}
|
|
2961
|
+
} catch {
|
|
2962
|
+
}
|
|
3268
2963
|
}
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
2964
|
+
}
|
|
2965
|
+
if (c.req.method === "GET") {
|
|
2966
|
+
try {
|
|
2967
|
+
const encodedRequestContext = c.req.query("requestContext");
|
|
2968
|
+
if (encodedRequestContext) {
|
|
2969
|
+
try {
|
|
2970
|
+
paramsRequestContext = JSON.parse(encodedRequestContext);
|
|
2971
|
+
} catch {
|
|
2972
|
+
try {
|
|
2973
|
+
const json = Buffer.from(encodedRequestContext, "base64").toString("utf-8");
|
|
2974
|
+
paramsRequestContext = JSON.parse(json);
|
|
2975
|
+
} catch {
|
|
2976
|
+
}
|
|
2977
|
+
}
|
|
3273
2978
|
}
|
|
2979
|
+
} catch {
|
|
3274
2980
|
}
|
|
3275
|
-
}
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3280
|
-
|
|
2981
|
+
}
|
|
2982
|
+
const requestContext = this.mergeRequestContext({ paramsRequestContext, bodyRequestContext });
|
|
2983
|
+
c.set("requestContext", requestContext);
|
|
2984
|
+
c.set("mastra", this.mastra);
|
|
2985
|
+
c.set("tools", this.tools || {});
|
|
2986
|
+
c.set("taskStore", this.taskStore);
|
|
2987
|
+
c.set("playground", this.playground === true);
|
|
2988
|
+
c.set("isDev", this.isDev === true);
|
|
2989
|
+
c.set("abortSignal", c.req.raw.signal);
|
|
2990
|
+
c.set("customRouteAuthConfig", this.customRouteAuthConfig);
|
|
2991
|
+
return next();
|
|
2992
|
+
};
|
|
2993
|
+
}
|
|
2994
|
+
async stream(route, res, result) {
|
|
2995
|
+
res.header("Content-Type", "text/plain");
|
|
2996
|
+
res.header("Transfer-Encoding", "chunked");
|
|
2997
|
+
const streamFormat = route.streamFormat || "stream";
|
|
2998
|
+
return stream(
|
|
2999
|
+
res,
|
|
3000
|
+
async (stream2) => {
|
|
3001
|
+
const readableStream = result instanceof ReadableStream ? result : result.fullStream;
|
|
3002
|
+
const reader = readableStream.getReader();
|
|
3003
|
+
stream2.onAbort(() => {
|
|
3004
|
+
void reader.cancel("request aborted");
|
|
3005
|
+
});
|
|
3006
|
+
try {
|
|
3007
|
+
while (true) {
|
|
3008
|
+
const { done, value } = await reader.read();
|
|
3009
|
+
if (done) break;
|
|
3010
|
+
if (value) {
|
|
3011
|
+
const shouldRedact = this.streamOptions?.redact ?? true;
|
|
3012
|
+
const outputValue = shouldRedact ? redactStreamChunk(value) : value;
|
|
3013
|
+
if (streamFormat === "sse") {
|
|
3014
|
+
await stream2.write(`data: ${JSON.stringify(outputValue)}
|
|
3015
|
+
|
|
3016
|
+
`);
|
|
3017
|
+
} else {
|
|
3018
|
+
await stream2.write(JSON.stringify(outputValue) + "");
|
|
3019
|
+
}
|
|
3020
|
+
}
|
|
3281
3021
|
}
|
|
3022
|
+
await stream2.write("data: [DONE]\n\n");
|
|
3023
|
+
} catch (error) {
|
|
3024
|
+
console.error(error);
|
|
3025
|
+
} finally {
|
|
3026
|
+
await stream2.close();
|
|
3027
|
+
}
|
|
3028
|
+
},
|
|
3029
|
+
async (err) => {
|
|
3030
|
+
console.error(err);
|
|
3031
|
+
}
|
|
3032
|
+
);
|
|
3033
|
+
}
|
|
3034
|
+
async getParams(route, request) {
|
|
3035
|
+
const urlParams = request.param();
|
|
3036
|
+
const queryParams = request.query();
|
|
3037
|
+
let body;
|
|
3038
|
+
if (route.method === "POST" || route.method === "PUT" || route.method === "PATCH") {
|
|
3039
|
+
try {
|
|
3040
|
+
body = await request.json();
|
|
3041
|
+
} catch {
|
|
3042
|
+
}
|
|
3043
|
+
}
|
|
3044
|
+
return { urlParams, queryParams, body };
|
|
3045
|
+
}
|
|
3046
|
+
async sendResponse(route, response, result) {
|
|
3047
|
+
if (route.responseType === "json") {
|
|
3048
|
+
return response.json(result, 200);
|
|
3049
|
+
} else if (route.responseType === "stream") {
|
|
3050
|
+
return this.stream(route, response, result);
|
|
3051
|
+
} else if (route.responseType === "datastream-response") {
|
|
3052
|
+
const fetchResponse = result;
|
|
3053
|
+
return fetchResponse;
|
|
3054
|
+
} else if (route.responseType === "mcp-http") {
|
|
3055
|
+
const { server, httpPath } = result;
|
|
3056
|
+
const { req, res } = toReqRes(response.req.raw);
|
|
3057
|
+
try {
|
|
3058
|
+
await server.startHTTP({
|
|
3059
|
+
url: new URL(response.req.url),
|
|
3060
|
+
httpPath,
|
|
3061
|
+
req,
|
|
3062
|
+
res
|
|
3063
|
+
});
|
|
3064
|
+
return await toFetchResponse(res);
|
|
3065
|
+
} catch {
|
|
3066
|
+
if (!res.headersSent) {
|
|
3067
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
3068
|
+
res.end(
|
|
3069
|
+
JSON.stringify({
|
|
3070
|
+
jsonrpc: "2.0",
|
|
3071
|
+
error: { code: -32603, message: "Internal server error" },
|
|
3072
|
+
id: null
|
|
3073
|
+
})
|
|
3074
|
+
);
|
|
3075
|
+
return await toFetchResponse(res);
|
|
3282
3076
|
}
|
|
3077
|
+
return await toFetchResponse(res);
|
|
3078
|
+
}
|
|
3079
|
+
} else if (route.responseType === "mcp-sse") {
|
|
3080
|
+
const { server, ssePath, messagePath } = result;
|
|
3081
|
+
try {
|
|
3082
|
+
return await server.startHonoSSE({
|
|
3083
|
+
url: new URL(response.req.url),
|
|
3084
|
+
ssePath,
|
|
3085
|
+
messagePath,
|
|
3086
|
+
context: response
|
|
3087
|
+
});
|
|
3088
|
+
} catch {
|
|
3089
|
+
return response.json({ error: "Error handling MCP SSE request" }, 500);
|
|
3283
3090
|
}
|
|
3284
|
-
headers = this[kOutHeaders];
|
|
3285
3091
|
} else {
|
|
3286
|
-
|
|
3287
|
-
}
|
|
3288
|
-
if (checkInvalidHeaderChar2(this.statusMessage)) {
|
|
3289
|
-
throw new ERR_INVALID_CHAR("statusMessage");
|
|
3290
|
-
}
|
|
3291
|
-
const statusLine = `HTTP/1.1 ${statusCode} ${this.statusMessage}\r
|
|
3292
|
-
`;
|
|
3293
|
-
if (statusCode === 204 || statusCode === 304 || statusCode >= 100 && statusCode <= 199) {
|
|
3294
|
-
this._hasBody = false;
|
|
3295
|
-
}
|
|
3296
|
-
if (this._expect_continue && !this._sent100) {
|
|
3297
|
-
this.shouldKeepAlive = false;
|
|
3092
|
+
return response.status(500);
|
|
3298
3093
|
}
|
|
3299
|
-
const convertedHeaders = headers && !Array.isArray(headers) ? headers : headers;
|
|
3300
|
-
this._storeHeader(statusLine, convertedHeaders ?? null);
|
|
3301
|
-
return this;
|
|
3302
3094
|
}
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
|
|
3095
|
+
async registerRoute(app, route, { prefix }) {
|
|
3096
|
+
const shouldApplyBodyLimit = this.bodyLimitOptions && ["POST", "PUT", "PATCH"].includes(route.method.toUpperCase());
|
|
3097
|
+
const maxSize = route.maxBodySize ?? this.bodyLimitOptions?.maxSize;
|
|
3098
|
+
const middlewares = [];
|
|
3099
|
+
if (shouldApplyBodyLimit && maxSize && this.bodyLimitOptions) {
|
|
3100
|
+
middlewares.push(
|
|
3101
|
+
bodyLimit({
|
|
3102
|
+
maxSize,
|
|
3103
|
+
onError: this.bodyLimitOptions.onError
|
|
3104
|
+
})
|
|
3105
|
+
);
|
|
3310
3106
|
}
|
|
3311
|
-
|
|
3312
|
-
|
|
3313
|
-
|
|
3314
|
-
|
|
3315
|
-
|
|
3107
|
+
app[route.method.toLowerCase()](
|
|
3108
|
+
`${prefix}${route.path}`,
|
|
3109
|
+
...middlewares,
|
|
3110
|
+
async (c) => {
|
|
3111
|
+
const params = await this.getParams(route, c.req);
|
|
3112
|
+
if (params.queryParams) {
|
|
3113
|
+
try {
|
|
3114
|
+
params.queryParams = await this.parseQueryParams(route, params.queryParams);
|
|
3115
|
+
} catch (error) {
|
|
3116
|
+
console.error("Error parsing query params", error);
|
|
3117
|
+
return c.json(
|
|
3118
|
+
{
|
|
3119
|
+
error: "Invalid query parameters",
|
|
3120
|
+
details: error instanceof Error ? error.message : "Unknown error"
|
|
3121
|
+
},
|
|
3122
|
+
400
|
|
3123
|
+
);
|
|
3124
|
+
}
|
|
3316
3125
|
}
|
|
3317
|
-
if (
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
|
|
3126
|
+
if (params.body) {
|
|
3127
|
+
try {
|
|
3128
|
+
params.body = await this.parseBody(route, params.body);
|
|
3129
|
+
} catch (error) {
|
|
3130
|
+
console.error("Error parsing body:", error instanceof Error ? error.message : String(error));
|
|
3131
|
+
return c.json(
|
|
3132
|
+
{
|
|
3133
|
+
error: "Invalid request body",
|
|
3134
|
+
details: error instanceof Error ? error.message : "Unknown error"
|
|
3135
|
+
},
|
|
3136
|
+
400
|
|
3137
|
+
);
|
|
3138
|
+
}
|
|
3139
|
+
}
|
|
3140
|
+
const handlerParams = {
|
|
3141
|
+
...params.urlParams,
|
|
3142
|
+
...params.queryParams,
|
|
3143
|
+
...typeof params.body === "object" ? params.body : {},
|
|
3144
|
+
requestContext: c.get("requestContext"),
|
|
3145
|
+
mastra: this.mastra,
|
|
3146
|
+
tools: c.get("tools"),
|
|
3147
|
+
taskStore: c.get("taskStore"),
|
|
3148
|
+
abortSignal: c.get("abortSignal")
|
|
3149
|
+
};
|
|
3150
|
+
try {
|
|
3151
|
+
const result = await route.handler(handlerParams);
|
|
3152
|
+
return this.sendResponse(route, c, result);
|
|
3153
|
+
} catch (error) {
|
|
3154
|
+
console.error("Error calling handler", error);
|
|
3155
|
+
if (error && typeof error === "object") {
|
|
3156
|
+
if ("status" in error) {
|
|
3157
|
+
const status = error.status;
|
|
3158
|
+
return c.json({ error: error instanceof Error ? error.message : "Unknown error" }, status);
|
|
3327
3159
|
}
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
|
|
3160
|
+
if ("details" in error && error.details && typeof error.details === "object" && "status" in error.details) {
|
|
3161
|
+
const status = error.details.status;
|
|
3162
|
+
return c.json({ error: error instanceof Error ? error.message : "Unknown error" }, status);
|
|
3163
|
+
}
|
|
3164
|
+
}
|
|
3165
|
+
return c.json({ error: error instanceof Error ? error.message : "Unknown error" }, 500);
|
|
3331
3166
|
}
|
|
3332
3167
|
}
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
|
|
3168
|
+
);
|
|
3169
|
+
}
|
|
3170
|
+
registerContextMiddleware() {
|
|
3171
|
+
this.app.use("*", this.createContextMiddleware());
|
|
3172
|
+
}
|
|
3173
|
+
registerAuthMiddleware() {
|
|
3174
|
+
const authConfig = this.mastra.getServer()?.auth;
|
|
3175
|
+
if (!authConfig) {
|
|
3176
|
+
return;
|
|
3339
3177
|
}
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
statusText,
|
|
3343
|
-
headers
|
|
3344
|
-
});
|
|
3178
|
+
this.app.use("*", authenticationMiddleware);
|
|
3179
|
+
this.app.use("*", authorizationMiddleware);
|
|
3345
3180
|
}
|
|
3346
3181
|
};
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
const
|
|
3352
|
-
|
|
3353
|
-
const versionMinor = 1;
|
|
3354
|
-
incoming.httpVersionMajor = versionMajor;
|
|
3355
|
-
incoming.httpVersionMinor = versionMinor;
|
|
3356
|
-
incoming.httpVersion = `${versionMajor}.${versionMinor}`;
|
|
3357
|
-
incoming.url = reqUrl.pathname + reqUrl.search;
|
|
3358
|
-
incoming.upgrade = false;
|
|
3359
|
-
const headers = [];
|
|
3360
|
-
for (const [headerName, headerValue] of req.headers) {
|
|
3361
|
-
headers.push(headerName);
|
|
3362
|
-
headers.push(headerValue);
|
|
3363
|
-
}
|
|
3364
|
-
incoming._addHeaderLines(headers, headers.length);
|
|
3365
|
-
incoming.method = req.method;
|
|
3366
|
-
incoming._stream = req.body;
|
|
3367
|
-
return {
|
|
3368
|
-
req: incoming,
|
|
3369
|
-
res: serverResponse
|
|
3182
|
+
|
|
3183
|
+
// ../../node_modules/.pnpm/hono-openapi@1.1.1_@hono+standard-validator@0.2.0_@standard-schema+spec@1.0.0_hono@4.10_dee62e986c04f5a510cb45dc6cf3f0e3/node_modules/hono-openapi/dist/index.js
|
|
3184
|
+
var uniqueSymbol = Symbol("openapi");
|
|
3185
|
+
function describeRoute(spec) {
|
|
3186
|
+
const middleware2 = async (_c, next) => {
|
|
3187
|
+
await next();
|
|
3370
3188
|
};
|
|
3189
|
+
return Object.assign(middleware2, {
|
|
3190
|
+
[uniqueSymbol]: {
|
|
3191
|
+
spec
|
|
3192
|
+
}
|
|
3193
|
+
});
|
|
3371
3194
|
}
|
|
3372
|
-
function toFetchResponse(res) {
|
|
3373
|
-
if (!(res instanceof FetchServerResponse)) {
|
|
3374
|
-
throw new Error("toFetchResponse must be called on a ServerResponse generated by toReqRes");
|
|
3375
|
-
}
|
|
3376
|
-
return res.fetchResponse;
|
|
3377
|
-
}
|
|
3378
|
-
var mcpServerIdPathParams = z.object({
|
|
3379
|
-
serverId: z.string().describe("MCP server ID")
|
|
3380
|
-
});
|
|
3381
|
-
var mcpServerDetailPathParams = z.object({
|
|
3382
|
-
id: z.string().describe("MCP server ID")
|
|
3383
|
-
});
|
|
3384
|
-
var mcpServerToolPathParams = z.object({
|
|
3385
|
-
serverId: z.string().describe("MCP server ID"),
|
|
3386
|
-
toolId: z.string().describe("Tool ID")
|
|
3387
|
-
});
|
|
3388
|
-
var executeToolBodySchema = z.object({
|
|
3389
|
-
data: z.unknown().optional()
|
|
3390
|
-
});
|
|
3391
|
-
var listMcpServersQuerySchema = z.object({
|
|
3392
|
-
limit: z.coerce.number().optional(),
|
|
3393
|
-
offset: z.coerce.number().optional()
|
|
3394
|
-
});
|
|
3395
|
-
var getMcpServerDetailQuerySchema = z.object({
|
|
3396
|
-
version: z.string().optional()
|
|
3397
|
-
});
|
|
3398
|
-
var versionDetailSchema = z.object({
|
|
3399
|
-
version: z.string(),
|
|
3400
|
-
release_date: z.string(),
|
|
3401
|
-
is_latest: z.boolean()
|
|
3402
|
-
});
|
|
3403
|
-
var serverInfoSchema = z.object({
|
|
3404
|
-
id: z.string(),
|
|
3405
|
-
name: z.string(),
|
|
3406
|
-
version_detail: versionDetailSchema
|
|
3407
|
-
});
|
|
3408
|
-
var listMcpServersResponseSchema = z.object({
|
|
3409
|
-
servers: z.array(serverInfoSchema),
|
|
3410
|
-
total_count: z.number(),
|
|
3411
|
-
next: z.string().nullable()
|
|
3412
|
-
});
|
|
3413
|
-
var serverDetailSchema = z.object({
|
|
3414
|
-
id: z.string(),
|
|
3415
|
-
name: z.string(),
|
|
3416
|
-
description: z.string().optional(),
|
|
3417
|
-
version_detail: versionDetailSchema,
|
|
3418
|
-
package_canonical: z.string().optional(),
|
|
3419
|
-
packages: z.array(z.unknown()).optional(),
|
|
3420
|
-
remotes: z.array(z.unknown()).optional()
|
|
3421
|
-
});
|
|
3422
|
-
z.object({
|
|
3423
|
-
jsonrpc: z.literal("2.0"),
|
|
3424
|
-
error: z.object({
|
|
3425
|
-
code: z.number(),
|
|
3426
|
-
message: z.string()
|
|
3427
|
-
}),
|
|
3428
|
-
id: z.null()
|
|
3429
|
-
});
|
|
3430
3195
|
|
|
3431
|
-
// src/server/handlers/
|
|
3432
|
-
var
|
|
3433
|
-
var
|
|
3434
|
-
|
|
3435
|
-
const
|
|
3436
|
-
|
|
3437
|
-
|
|
3438
|
-
|
|
3439
|
-
|
|
3440
|
-
|
|
3441
|
-
return await toFetchResponse(res);
|
|
3442
|
-
}
|
|
3443
|
-
try {
|
|
3444
|
-
await server.startHTTP({
|
|
3445
|
-
url: new URL(c.req.url),
|
|
3446
|
-
httpPath: `/api/mcp/${serverId}/mcp`,
|
|
3447
|
-
req,
|
|
3448
|
-
res
|
|
3449
|
-
});
|
|
3450
|
-
return await toFetchResponse(res);
|
|
3451
|
-
} catch (error) {
|
|
3452
|
-
if (!res.headersSent) {
|
|
3453
|
-
res.writeHead(500, { "Content-Type": "application/json" });
|
|
3454
|
-
res.end(
|
|
3455
|
-
JSON.stringify({
|
|
3456
|
-
jsonrpc: "2.0",
|
|
3457
|
-
error: {
|
|
3458
|
-
code: -32603,
|
|
3459
|
-
message: "Internal server error"
|
|
3460
|
-
},
|
|
3461
|
-
id: null
|
|
3462
|
-
// Cannot determine original request ID in catch
|
|
3463
|
-
})
|
|
3464
|
-
);
|
|
3465
|
-
return await toFetchResponse(res);
|
|
3466
|
-
} else {
|
|
3467
|
-
c.get("logger")?.error("Error after headers sent:", error);
|
|
3468
|
-
return await toFetchResponse(res);
|
|
3469
|
-
}
|
|
3470
|
-
}
|
|
3471
|
-
};
|
|
3472
|
-
var getMcpServerSseHandler = async (c) => {
|
|
3473
|
-
const mastra = getMastra(c);
|
|
3474
|
-
const serverId = c.req.param("serverId");
|
|
3475
|
-
const server = mastra.getMCPServerById(serverId);
|
|
3476
|
-
if (!server) {
|
|
3477
|
-
return c.json({ error: `MCP server '${serverId}' not found` }, 404);
|
|
3478
|
-
}
|
|
3479
|
-
const requestUrl = new URL(c.req.url);
|
|
3480
|
-
const sseConnectionPath = `/api/mcp/${serverId}/sse`;
|
|
3481
|
-
const sseMessagePath = `/api/mcp/${serverId}/messages`;
|
|
3482
|
-
try {
|
|
3483
|
-
return await server.startHonoSSE({
|
|
3484
|
-
url: requestUrl,
|
|
3485
|
-
ssePath: sseConnectionPath,
|
|
3486
|
-
messagePath: sseMessagePath,
|
|
3487
|
-
context: c
|
|
3488
|
-
});
|
|
3489
|
-
} catch (error) {
|
|
3490
|
-
c.get("logger")?.error({ err: error, serverId, path: requestUrl.pathname }, "Error in MCP SSE route handler");
|
|
3491
|
-
return c.json({ error: "Error handling MCP SSE request" }, 500);
|
|
3492
|
-
}
|
|
3493
|
-
};
|
|
3494
|
-
var LIST_MCP_SERVERS_ROUTE = createRoute({
|
|
3495
|
-
method: "GET",
|
|
3496
|
-
path: "/api/mcp/v0/servers",
|
|
3497
|
-
responseType: "json",
|
|
3498
|
-
queryParamSchema: listMcpServersQuerySchema,
|
|
3499
|
-
responseSchema: listMcpServersResponseSchema,
|
|
3500
|
-
summary: "List MCP servers",
|
|
3501
|
-
description: "Returns a list of registered MCP servers with pagination support",
|
|
3502
|
-
tags: ["MCP"],
|
|
3503
|
-
handler: async ({ mastra, limit, offset }) => {
|
|
3504
|
-
if (!mastra || typeof mastra.listMCPServers !== "function") {
|
|
3505
|
-
throw new HTTPException$1(500, { message: "Mastra instance or listMCPServers method not available" });
|
|
3506
|
-
}
|
|
3507
|
-
const servers = mastra.listMCPServers();
|
|
3508
|
-
if (!servers) {
|
|
3509
|
-
return { servers: [], total_count: 0, next: null };
|
|
3510
|
-
}
|
|
3511
|
-
const serverList = Object.values(servers);
|
|
3512
|
-
const totalCount = serverList.length;
|
|
3513
|
-
const actualOffset = offset ?? 0;
|
|
3514
|
-
let paginatedServers = serverList;
|
|
3515
|
-
let nextUrl = null;
|
|
3516
|
-
if (limit !== void 0) {
|
|
3517
|
-
paginatedServers = serverList.slice(actualOffset, actualOffset + limit);
|
|
3518
|
-
if (actualOffset + limit < totalCount) {
|
|
3519
|
-
nextUrl = `/api/mcp/v0/servers?limit=${limit}&offset=${actualOffset + limit}`;
|
|
3520
|
-
}
|
|
3521
|
-
}
|
|
3522
|
-
const serverInfoList = paginatedServers.map((server) => server.getServerInfo());
|
|
3523
|
-
return {
|
|
3524
|
-
servers: serverInfoList,
|
|
3525
|
-
total_count: totalCount,
|
|
3526
|
-
next: nextUrl
|
|
3527
|
-
};
|
|
3528
|
-
}
|
|
3529
|
-
});
|
|
3530
|
-
var GET_MCP_SERVER_DETAIL_ROUTE = createRoute({
|
|
3531
|
-
method: "GET",
|
|
3532
|
-
path: "/api/mcp/v0/servers/:id",
|
|
3533
|
-
responseType: "json",
|
|
3534
|
-
pathParamSchema: mcpServerDetailPathParams,
|
|
3535
|
-
queryParamSchema: getMcpServerDetailQuerySchema,
|
|
3536
|
-
responseSchema: serverDetailSchema,
|
|
3537
|
-
summary: "Get MCP server details",
|
|
3538
|
-
description: "Returns detailed information about a specific MCP server",
|
|
3539
|
-
tags: ["MCP"],
|
|
3540
|
-
handler: async ({ mastra, id, version }) => {
|
|
3541
|
-
if (!mastra || typeof mastra.getMCPServerById !== "function") {
|
|
3542
|
-
throw new HTTPException$1(500, { message: "Mastra instance or getMCPServerById method not available" });
|
|
3543
|
-
}
|
|
3544
|
-
const server = mastra.getMCPServerById(id);
|
|
3545
|
-
if (!server) {
|
|
3546
|
-
throw new HTTPException$1(404, { message: `MCP server with ID '${id}' not found` });
|
|
3547
|
-
}
|
|
3548
|
-
const serverDetail = server.getServerDetail();
|
|
3549
|
-
if (version && serverDetail.version_detail.version !== version) {
|
|
3550
|
-
throw new HTTPException$1(404, {
|
|
3551
|
-
message: `MCP server with ID '${id}' found, but not version '${version}'. Available version: ${serverDetail.version_detail.version}`
|
|
3196
|
+
// src/server/handlers/client.ts
|
|
3197
|
+
var clients = /* @__PURE__ */ new Set();
|
|
3198
|
+
var hotReloadDisabled = false;
|
|
3199
|
+
function handleClientsRefresh(c) {
|
|
3200
|
+
const stream2 = new ReadableStream({
|
|
3201
|
+
start(controller) {
|
|
3202
|
+
clients.add(controller);
|
|
3203
|
+
controller.enqueue("data: connected\n\n");
|
|
3204
|
+
c.req.raw.signal.addEventListener("abort", () => {
|
|
3205
|
+
clients.delete(controller);
|
|
3552
3206
|
});
|
|
3553
3207
|
}
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
pathParamSchema: mcpServerIdPathParams,
|
|
3562
|
-
summary: "List MCP server tools",
|
|
3563
|
-
description: "Returns a list of tools available on the specified MCP server",
|
|
3564
|
-
tags: ["MCP"],
|
|
3565
|
-
handler: async ({ mastra, serverId }) => {
|
|
3566
|
-
if (!mastra || typeof mastra.getMCPServerById !== "function") {
|
|
3567
|
-
throw new HTTPException$1(500, { message: "Mastra instance or getMCPServerById method not available" });
|
|
3568
|
-
}
|
|
3569
|
-
const server = mastra.getMCPServerById(serverId);
|
|
3570
|
-
if (!server) {
|
|
3571
|
-
throw new HTTPException$1(404, { message: `MCP server with ID '${serverId}' not found` });
|
|
3572
|
-
}
|
|
3573
|
-
if (typeof server.getToolListInfo !== "function") {
|
|
3574
|
-
throw new HTTPException$1(501, { message: `Server '${serverId}' cannot list tools in this way.` });
|
|
3575
|
-
}
|
|
3576
|
-
return server.getToolListInfo();
|
|
3577
|
-
}
|
|
3578
|
-
});
|
|
3579
|
-
var GET_MCP_SERVER_TOOL_DETAIL_ROUTE = createRoute({
|
|
3580
|
-
method: "GET",
|
|
3581
|
-
path: "/api/mcp/:serverId/tools/:toolId",
|
|
3582
|
-
responseType: "json",
|
|
3583
|
-
pathParamSchema: mcpServerToolPathParams,
|
|
3584
|
-
summary: "Get MCP server tool details",
|
|
3585
|
-
description: "Returns detailed information about a specific tool on the MCP server",
|
|
3586
|
-
tags: ["MCP"],
|
|
3587
|
-
handler: async ({ mastra, serverId, toolId }) => {
|
|
3588
|
-
if (!mastra || typeof mastra.getMCPServerById !== "function") {
|
|
3589
|
-
throw new HTTPException$1(500, { message: "Mastra instance or getMCPServerById method not available" });
|
|
3590
|
-
}
|
|
3591
|
-
const server = mastra.getMCPServerById(serverId);
|
|
3592
|
-
if (!server) {
|
|
3593
|
-
throw new HTTPException$1(404, { message: `MCP server with ID '${serverId}' not found` });
|
|
3594
|
-
}
|
|
3595
|
-
if (typeof server.getToolInfo !== "function") {
|
|
3596
|
-
throw new HTTPException$1(501, { message: `Server '${serverId}' cannot provide tool details in this way.` });
|
|
3597
|
-
}
|
|
3598
|
-
const toolInfo = server.getToolInfo(toolId);
|
|
3599
|
-
if (!toolInfo) {
|
|
3600
|
-
throw new HTTPException$1(404, { message: `Tool with ID '${toolId}' not found on MCP server '${serverId}'` });
|
|
3601
|
-
}
|
|
3602
|
-
return toolInfo;
|
|
3603
|
-
}
|
|
3604
|
-
});
|
|
3605
|
-
var EXECUTE_MCP_SERVER_TOOL_ROUTE = createRoute({
|
|
3606
|
-
method: "POST",
|
|
3607
|
-
path: "/api/mcp/:serverId/tools/:toolId/execute",
|
|
3608
|
-
responseType: "json",
|
|
3609
|
-
pathParamSchema: mcpServerToolPathParams,
|
|
3610
|
-
bodySchema: executeToolBodySchema,
|
|
3611
|
-
summary: "Execute MCP server tool",
|
|
3612
|
-
description: "Executes a tool on the specified MCP server with the provided arguments",
|
|
3613
|
-
tags: ["MCP"],
|
|
3614
|
-
handler: async ({
|
|
3615
|
-
mastra,
|
|
3616
|
-
serverId,
|
|
3617
|
-
toolId,
|
|
3618
|
-
data
|
|
3619
|
-
}) => {
|
|
3620
|
-
if (!mastra || typeof mastra.getMCPServerById !== "function") {
|
|
3621
|
-
throw new HTTPException$1(500, { message: "Mastra instance or getMCPServerById method not available" });
|
|
3208
|
+
});
|
|
3209
|
+
return new Response(stream2, {
|
|
3210
|
+
headers: {
|
|
3211
|
+
"Content-Type": "text/event-stream",
|
|
3212
|
+
"Cache-Control": "no-cache",
|
|
3213
|
+
Connection: "keep-alive",
|
|
3214
|
+
"Access-Control-Allow-Origin": "*"
|
|
3622
3215
|
}
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
|
|
3216
|
+
});
|
|
3217
|
+
}
|
|
3218
|
+
function handleTriggerClientsRefresh(c) {
|
|
3219
|
+
clients.forEach((controller) => {
|
|
3220
|
+
try {
|
|
3221
|
+
controller.enqueue("data: refresh\n\n");
|
|
3222
|
+
} catch {
|
|
3223
|
+
clients.delete(controller);
|
|
3626
3224
|
}
|
|
3627
|
-
|
|
3628
|
-
|
|
3225
|
+
});
|
|
3226
|
+
return c.json({ success: true, clients: clients.size });
|
|
3227
|
+
}
|
|
3228
|
+
function isHotReloadDisabled() {
|
|
3229
|
+
return hotReloadDisabled;
|
|
3230
|
+
}
|
|
3231
|
+
function handleError(error, defaultMessage) {
|
|
3232
|
+
const apiError = error;
|
|
3233
|
+
throw new HTTPException$1(apiError.status || 500, {
|
|
3234
|
+
message: apiError.message || defaultMessage,
|
|
3235
|
+
cause: apiError.cause
|
|
3236
|
+
});
|
|
3237
|
+
}
|
|
3238
|
+
function errorHandler(err, c, isDev) {
|
|
3239
|
+
if (err instanceof HTTPException$1) {
|
|
3240
|
+
if (isDev) {
|
|
3241
|
+
return c.json({ error: err.message, cause: err.cause, stack: err.stack }, err.status);
|
|
3629
3242
|
}
|
|
3630
|
-
|
|
3631
|
-
return { result };
|
|
3243
|
+
return c.json({ error: err.message }, err.status);
|
|
3632
3244
|
}
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
|
|
3640
|
-
|
|
3641
|
-
// because they require direct access to raw Hono context.
|
|
3642
|
-
];
|
|
3245
|
+
c.get("mastra").getLogger().error(err);
|
|
3246
|
+
return c.json({ error: "Internal Server Error" }, 500);
|
|
3247
|
+
}
|
|
3248
|
+
|
|
3249
|
+
// src/server/handlers/health.ts
|
|
3250
|
+
async function healthHandler(c) {
|
|
3251
|
+
return c.json({ success: true }, 200);
|
|
3252
|
+
}
|
|
3643
3253
|
|
|
3644
3254
|
// src/server/handlers/restart-active-runs.ts
|
|
3645
3255
|
async function restartAllActiveWorkflowRunsHandler(c) {
|
|
@@ -3801,16 +3411,18 @@ async function createHonoServer(mastra, options = {
|
|
|
3801
3411
|
// 4.5 MB,
|
|
3802
3412
|
onError: () => ({ error: "Request body too large" })
|
|
3803
3413
|
};
|
|
3804
|
-
const honoServerAdapter = new
|
|
3414
|
+
const honoServerAdapter = new MastraServer({
|
|
3415
|
+
app,
|
|
3805
3416
|
mastra,
|
|
3806
3417
|
tools: options.tools,
|
|
3807
3418
|
taskStore: a2aTaskStore,
|
|
3808
|
-
customRouteAuthConfig,
|
|
3809
3419
|
playground: options.playground,
|
|
3810
3420
|
isDev: options.isDev,
|
|
3811
|
-
bodyLimitOptions
|
|
3421
|
+
bodyLimitOptions,
|
|
3422
|
+
openapiPath: "/openapi.json",
|
|
3423
|
+
customRouteAuthConfig
|
|
3812
3424
|
});
|
|
3813
|
-
honoServerAdapter.registerContextMiddleware(
|
|
3425
|
+
honoServerAdapter.registerContextMiddleware();
|
|
3814
3426
|
const serverMiddleware = mastra.getServerMiddleware?.();
|
|
3815
3427
|
if (serverMiddleware && serverMiddleware.length > 0) {
|
|
3816
3428
|
for (const m of serverMiddleware) {
|
|
@@ -3833,7 +3445,7 @@ async function createHonoServer(mastra, options = {
|
|
|
3833
3445
|
}
|
|
3834
3446
|
app.get(
|
|
3835
3447
|
"/health",
|
|
3836
|
-
|
|
3448
|
+
describeRoute({
|
|
3837
3449
|
description: "Health check endpoint",
|
|
3838
3450
|
tags: ["system"],
|
|
3839
3451
|
responses: {
|
|
@@ -3844,8 +3456,7 @@ async function createHonoServer(mastra, options = {
|
|
|
3844
3456
|
}),
|
|
3845
3457
|
healthHandler
|
|
3846
3458
|
);
|
|
3847
|
-
|
|
3848
|
-
app.use("*", authorizationMiddleware);
|
|
3459
|
+
honoServerAdapter.registerAuthMiddleware();
|
|
3849
3460
|
if (server?.middleware) {
|
|
3850
3461
|
const normalizedMiddlewares = Array.isArray(server.middleware) ? server.middleware : [server.middleware];
|
|
3851
3462
|
const middlewares = normalizedMiddlewares.map((middleware2) => {
|
|
@@ -3868,7 +3479,7 @@ async function createHonoServer(mastra, options = {
|
|
|
3868
3479
|
middlewares.push(...Array.isArray(route.middleware) ? route.middleware : [route.middleware]);
|
|
3869
3480
|
}
|
|
3870
3481
|
if (route.openapi) {
|
|
3871
|
-
middlewares.push(
|
|
3482
|
+
middlewares.push(describeRoute(route.openapi));
|
|
3872
3483
|
}
|
|
3873
3484
|
const handler = "handler" in route ? route.handler : await route.createHandler({ mastra });
|
|
3874
3485
|
if (route.method === "GET") {
|
|
@@ -3889,17 +3500,11 @@ async function createHonoServer(mastra, options = {
|
|
|
3889
3500
|
if (server?.build?.apiReqLogs) {
|
|
3890
3501
|
app.use(logger());
|
|
3891
3502
|
}
|
|
3892
|
-
await honoServerAdapter.registerRoutes(
|
|
3893
|
-
for (const route of MCP_ROUTES) {
|
|
3894
|
-
await honoServerAdapter.registerRoute(app, route, { prefix: "" });
|
|
3895
|
-
}
|
|
3896
|
-
app.all("/api/mcp/:serverId/mcp", getMcpServerMessageHandler);
|
|
3897
|
-
app.get("/api/mcp/:serverId/sse", getMcpServerSseHandler);
|
|
3898
|
-
app.post("/api/mcp/:serverId/messages", getMcpServerSseHandler);
|
|
3503
|
+
await honoServerAdapter.registerRoutes();
|
|
3899
3504
|
if (options?.isDev || server?.build?.swaggerUI) {
|
|
3900
3505
|
app.get(
|
|
3901
3506
|
"/swagger-ui",
|
|
3902
|
-
|
|
3507
|
+
describeRoute({
|
|
3903
3508
|
hide: true
|
|
3904
3509
|
}),
|
|
3905
3510
|
middleware({ url: "/openapi.json" })
|
|
@@ -3908,7 +3513,7 @@ async function createHonoServer(mastra, options = {
|
|
|
3908
3513
|
if (options?.isDev) {
|
|
3909
3514
|
app.post(
|
|
3910
3515
|
"/__restart-active-workflow-runs",
|
|
3911
|
-
|
|
3516
|
+
describeRoute({
|
|
3912
3517
|
hide: true
|
|
3913
3518
|
}),
|
|
3914
3519
|
restartAllActiveWorkflowRunsHandler
|
|
@@ -3917,21 +3522,21 @@ async function createHonoServer(mastra, options = {
|
|
|
3917
3522
|
if (options?.playground) {
|
|
3918
3523
|
app.get(
|
|
3919
3524
|
"/refresh-events",
|
|
3920
|
-
|
|
3525
|
+
describeRoute({
|
|
3921
3526
|
hide: true
|
|
3922
3527
|
}),
|
|
3923
3528
|
handleClientsRefresh
|
|
3924
3529
|
);
|
|
3925
3530
|
app.post(
|
|
3926
3531
|
"/__refresh",
|
|
3927
|
-
|
|
3532
|
+
describeRoute({
|
|
3928
3533
|
hide: true
|
|
3929
3534
|
}),
|
|
3930
3535
|
handleTriggerClientsRefresh
|
|
3931
3536
|
);
|
|
3932
3537
|
app.get(
|
|
3933
3538
|
"/__hot-reload-status",
|
|
3934
|
-
|
|
3539
|
+
describeRoute({
|
|
3935
3540
|
hide: true
|
|
3936
3541
|
}),
|
|
3937
3542
|
(c) => {
|