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