ultra-mcp-toolkit 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/skills/ultra-mcp-toolkit/SKILL.md +65 -0
- package/.claude/skills/ultra-mcp-toolkit/references/dispatcher.md +144 -0
- package/.claude/skills/ultra-mcp-toolkit/references/manifest.md +100 -0
- package/.claude/skills/ultra-mcp-toolkit/references/server-boot.md +260 -0
- package/.claude/skills/ultra-mcp-toolkit/references/skills.md +94 -0
- package/.claude/skills/ultra-mcp-toolkit/references/trim.md +115 -0
- package/LICENSE +21 -0
- package/README.md +271 -0
- package/build/agent-safety/index.d.ts +3 -0
- package/build/agent-safety/index.d.ts.map +1 -0
- package/build/agent-safety/index.js +3 -0
- package/build/agent-safety/index.js.map +1 -0
- package/build/agent-safety/safe-path.d.ts +67 -0
- package/build/agent-safety/safe-path.d.ts.map +1 -0
- package/build/agent-safety/safe-path.js +157 -0
- package/build/agent-safety/safe-path.js.map +1 -0
- package/build/agent-safety/safe-path.test.d.ts +2 -0
- package/build/agent-safety/safe-path.test.d.ts.map +1 -0
- package/build/agent-safety/safe-path.test.js +134 -0
- package/build/agent-safety/safe-path.test.js.map +1 -0
- package/build/agent-safety/safety-checker.d.ts +52 -0
- package/build/agent-safety/safety-checker.d.ts.map +1 -0
- package/build/agent-safety/safety-checker.js +218 -0
- package/build/agent-safety/safety-checker.js.map +1 -0
- package/build/agent-safety/safety-checker.test.d.ts +2 -0
- package/build/agent-safety/safety-checker.test.d.ts.map +1 -0
- package/build/agent-safety/safety-checker.test.js +138 -0
- package/build/agent-safety/safety-checker.test.js.map +1 -0
- package/build/agent-tools/file-tools.d.ts +93 -0
- package/build/agent-tools/file-tools.d.ts.map +1 -0
- package/build/agent-tools/file-tools.js +164 -0
- package/build/agent-tools/file-tools.js.map +1 -0
- package/build/agent-tools/file-tools.test.d.ts +2 -0
- package/build/agent-tools/file-tools.test.d.ts.map +1 -0
- package/build/agent-tools/file-tools.test.js +186 -0
- package/build/agent-tools/file-tools.test.js.map +1 -0
- package/build/agent-tools/git-tools.d.ts +106 -0
- package/build/agent-tools/git-tools.d.ts.map +1 -0
- package/build/agent-tools/git-tools.js +202 -0
- package/build/agent-tools/git-tools.js.map +1 -0
- package/build/agent-tools/github-tools.d.ts +151 -0
- package/build/agent-tools/github-tools.d.ts.map +1 -0
- package/build/agent-tools/github-tools.js +293 -0
- package/build/agent-tools/github-tools.js.map +1 -0
- package/build/agent-tools/index.d.ts +5 -0
- package/build/agent-tools/index.d.ts.map +1 -0
- package/build/agent-tools/index.js +5 -0
- package/build/agent-tools/index.js.map +1 -0
- package/build/agent-tools/test-tools.d.ts +79 -0
- package/build/agent-tools/test-tools.d.ts.map +1 -0
- package/build/agent-tools/test-tools.js +113 -0
- package/build/agent-tools/test-tools.js.map +1 -0
- package/build/boot/http.d.ts +31 -0
- package/build/boot/http.d.ts.map +1 -0
- package/build/boot/http.js +301 -0
- package/build/boot/http.js.map +1 -0
- package/build/boot/http.test.d.ts +2 -0
- package/build/boot/http.test.d.ts.map +1 -0
- package/build/boot/http.test.js +514 -0
- package/build/boot/http.test.js.map +1 -0
- package/build/boot/stdio.d.ts +14 -0
- package/build/boot/stdio.d.ts.map +1 -0
- package/build/boot/stdio.js +48 -0
- package/build/boot/stdio.js.map +1 -0
- package/build/bridge/client.d.ts +10 -0
- package/build/bridge/client.d.ts.map +1 -0
- package/build/bridge/client.js +85 -0
- package/build/bridge/client.js.map +1 -0
- package/build/bridge/discovery.d.ts +29 -0
- package/build/bridge/discovery.d.ts.map +1 -0
- package/build/bridge/discovery.js +202 -0
- package/build/bridge/discovery.js.map +1 -0
- package/build/bridge/discovery.test.d.ts +2 -0
- package/build/bridge/discovery.test.d.ts.map +1 -0
- package/build/bridge/discovery.test.js +140 -0
- package/build/bridge/discovery.test.js.map +1 -0
- package/build/bridge/index.d.ts +6 -0
- package/build/bridge/index.d.ts.map +1 -0
- package/build/bridge/index.js +10 -0
- package/build/bridge/index.js.map +1 -0
- package/build/bridge/resolve.d.ts +14 -0
- package/build/bridge/resolve.d.ts.map +1 -0
- package/build/bridge/resolve.js +22 -0
- package/build/bridge/resolve.js.map +1 -0
- package/build/bridge/server.d.ts +39 -0
- package/build/bridge/server.d.ts.map +1 -0
- package/build/bridge/server.js +237 -0
- package/build/bridge/server.js.map +1 -0
- package/build/bridge/wire.d.ts +23 -0
- package/build/bridge/wire.d.ts.map +1 -0
- package/build/bridge/wire.js +17 -0
- package/build/bridge/wire.js.map +1 -0
- package/build/cli/help-select.test.d.ts +2 -0
- package/build/cli/help-select.test.d.ts.map +1 -0
- package/build/cli/help-select.test.js +215 -0
- package/build/cli/help-select.test.js.map +1 -0
- package/build/cli/help.d.ts +17 -0
- package/build/cli/help.d.ts.map +1 -0
- package/build/cli/help.js +91 -0
- package/build/cli/help.js.map +1 -0
- package/build/cli/index.d.ts +31 -0
- package/build/cli/index.d.ts.map +1 -0
- package/build/cli/index.js +387 -0
- package/build/cli/index.js.map +1 -0
- package/build/cli/install-skill.d.ts +20 -0
- package/build/cli/install-skill.d.ts.map +1 -0
- package/build/cli/install-skill.js +98 -0
- package/build/cli/install-skill.js.map +1 -0
- package/build/cli/install-skill.test.d.ts +2 -0
- package/build/cli/install-skill.test.d.ts.map +1 -0
- package/build/cli/install-skill.test.js +159 -0
- package/build/cli/install-skill.test.js.map +1 -0
- package/build/cli/parse-argv.d.ts +10 -0
- package/build/cli/parse-argv.d.ts.map +1 -0
- package/build/cli/parse-argv.js +95 -0
- package/build/cli/parse-argv.js.map +1 -0
- package/build/cli/select.d.ts +4 -0
- package/build/cli/select.d.ts.map +1 -0
- package/build/cli/select.js +53 -0
- package/build/cli/select.js.map +1 -0
- package/build/client/http.d.ts +32 -0
- package/build/client/http.d.ts.map +1 -0
- package/build/client/http.js +183 -0
- package/build/client/http.js.map +1 -0
- package/build/client/http.test.d.ts +2 -0
- package/build/client/http.test.d.ts.map +1 -0
- package/build/client/http.test.js +76 -0
- package/build/client/http.test.js.map +1 -0
- package/build/client/index.d.ts +9 -0
- package/build/client/index.d.ts.map +1 -0
- package/build/client/index.js +19 -0
- package/build/client/index.js.map +1 -0
- package/build/client/streaming.d.ts +29 -0
- package/build/client/streaming.d.ts.map +1 -0
- package/build/client/streaming.js +133 -0
- package/build/client/streaming.js.map +1 -0
- package/build/client/streaming.test.d.ts +2 -0
- package/build/client/streaming.test.d.ts.map +1 -0
- package/build/client/streaming.test.js +139 -0
- package/build/client/streaming.test.js.map +1 -0
- package/build/client/transport.d.ts +24 -0
- package/build/client/transport.d.ts.map +1 -0
- package/build/client/transport.js +119 -0
- package/build/client/transport.js.map +1 -0
- package/build/client/transport.test.d.ts +2 -0
- package/build/client/transport.test.d.ts.map +1 -0
- package/build/client/transport.test.js +145 -0
- package/build/client/transport.test.js.map +1 -0
- package/build/code-api/boot.d.ts +28 -0
- package/build/code-api/boot.d.ts.map +1 -0
- package/build/code-api/boot.js +57 -0
- package/build/code-api/boot.js.map +1 -0
- package/build/code-api/index.d.ts +4 -0
- package/build/code-api/index.d.ts.map +1 -0
- package/build/code-api/index.js +5 -0
- package/build/code-api/index.js.map +1 -0
- package/build/code-api/script.d.ts +17 -0
- package/build/code-api/script.d.ts.map +1 -0
- package/build/code-api/script.js +57 -0
- package/build/code-api/script.js.map +1 -0
- package/build/code-api/tool.d.ts +36 -0
- package/build/code-api/tool.d.ts.map +1 -0
- package/build/code-api/tool.js +91 -0
- package/build/code-api/tool.js.map +1 -0
- package/build/config/index.d.ts +23 -0
- package/build/config/index.d.ts.map +1 -0
- package/build/config/index.js +55 -0
- package/build/config/index.js.map +1 -0
- package/build/config/index.test.d.ts +2 -0
- package/build/config/index.test.d.ts.map +1 -0
- package/build/config/index.test.js +84 -0
- package/build/config/index.test.js.map +1 -0
- package/build/confirm/index.d.ts +57 -0
- package/build/confirm/index.d.ts.map +1 -0
- package/build/confirm/index.js +198 -0
- package/build/confirm/index.js.map +1 -0
- package/build/core/disk-cache.d.ts +8 -0
- package/build/core/disk-cache.d.ts.map +1 -0
- package/build/core/disk-cache.js +70 -0
- package/build/core/disk-cache.js.map +1 -0
- package/build/core/disk-cache.test.d.ts +2 -0
- package/build/core/disk-cache.test.d.ts.map +1 -0
- package/build/core/disk-cache.test.js +62 -0
- package/build/core/disk-cache.test.js.map +1 -0
- package/build/core/lru.d.ts +18 -0
- package/build/core/lru.d.ts.map +1 -0
- package/build/core/lru.js +57 -0
- package/build/core/lru.js.map +1 -0
- package/build/core/lru.test.d.ts +2 -0
- package/build/core/lru.test.d.ts.map +1 -0
- package/build/core/lru.test.js +86 -0
- package/build/core/lru.test.js.map +1 -0
- package/build/core/manifest.d.ts +63 -0
- package/build/core/manifest.d.ts.map +1 -0
- package/build/core/manifest.js +193 -0
- package/build/core/manifest.js.map +1 -0
- package/build/core/manifest.test.d.ts +2 -0
- package/build/core/manifest.test.d.ts.map +1 -0
- package/build/core/manifest.test.js +234 -0
- package/build/core/manifest.test.js.map +1 -0
- package/build/core/page-cache.d.ts +21 -0
- package/build/core/page-cache.d.ts.map +1 -0
- package/build/core/page-cache.js +152 -0
- package/build/core/page-cache.js.map +1 -0
- package/build/core/page-cache.test.d.ts +2 -0
- package/build/core/page-cache.test.d.ts.map +1 -0
- package/build/core/page-cache.test.js +93 -0
- package/build/core/page-cache.test.js.map +1 -0
- package/build/core/sandbox.d.ts +26 -0
- package/build/core/sandbox.d.ts.map +1 -0
- package/build/core/sandbox.js +150 -0
- package/build/core/sandbox.js.map +1 -0
- package/build/core/sandbox.test.d.ts +2 -0
- package/build/core/sandbox.test.d.ts.map +1 -0
- package/build/core/sandbox.test.js +80 -0
- package/build/core/sandbox.test.js.map +1 -0
- package/build/core/trim-registry.d.ts +5 -0
- package/build/core/trim-registry.d.ts.map +1 -0
- package/build/core/trim-registry.js +27 -0
- package/build/core/trim-registry.js.map +1 -0
- package/build/core/trim.d.ts +26 -0
- package/build/core/trim.d.ts.map +1 -0
- package/build/core/trim.js +99 -0
- package/build/core/trim.js.map +1 -0
- package/build/core/trim.test.d.ts +2 -0
- package/build/core/trim.test.d.ts.map +1 -0
- package/build/core/trim.test.js +131 -0
- package/build/core/trim.test.js.map +1 -0
- package/build/index.d.ts +25 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +49 -0
- package/build/index.js.map +1 -0
- package/build/schemas/index.d.ts +6 -0
- package/build/schemas/index.d.ts.map +1 -0
- package/build/schemas/index.js +20 -0
- package/build/schemas/index.js.map +1 -0
- package/build/schemas/index.test.d.ts +2 -0
- package/build/schemas/index.test.d.ts.map +1 -0
- package/build/schemas/index.test.js +48 -0
- package/build/schemas/index.test.js.map +1 -0
- package/build/skills/drift.d.ts +18 -0
- package/build/skills/drift.d.ts.map +1 -0
- package/build/skills/drift.js +158 -0
- package/build/skills/drift.js.map +1 -0
- package/build/skills/drift.test.d.ts +2 -0
- package/build/skills/drift.test.d.ts.map +1 -0
- package/build/skills/drift.test.js +126 -0
- package/build/skills/drift.test.js.map +1 -0
- package/build/skills/files.d.ts +14 -0
- package/build/skills/files.d.ts.map +1 -0
- package/build/skills/files.js +80 -0
- package/build/skills/files.js.map +1 -0
- package/build/skills/files.test.d.ts +2 -0
- package/build/skills/files.test.d.ts.map +1 -0
- package/build/skills/files.test.js +54 -0
- package/build/skills/files.test.js.map +1 -0
- package/build/skills/index.d.ts +3 -0
- package/build/skills/index.d.ts.map +1 -0
- package/build/skills/index.js +8 -0
- package/build/skills/index.js.map +1 -0
- package/build/skills/serve.test.d.ts +2 -0
- package/build/skills/serve.test.d.ts.map +1 -0
- package/build/skills/serve.test.js +67 -0
- package/build/skills/serve.test.js.map +1 -0
- package/build/tool/dispatcher.d.ts +48 -0
- package/build/tool/dispatcher.d.ts.map +1 -0
- package/build/tool/dispatcher.js +219 -0
- package/build/tool/dispatcher.js.map +1 -0
- package/build/tool/dispatcher.test.d.ts +2 -0
- package/build/tool/dispatcher.test.d.ts.map +1 -0
- package/build/tool/dispatcher.test.js +269 -0
- package/build/tool/dispatcher.test.js.map +1 -0
- package/build/tool/schema-validation.test.d.ts +2 -0
- package/build/tool/schema-validation.test.d.ts.map +1 -0
- package/build/tool/schema-validation.test.js +56 -0
- package/build/tool/schema-validation.test.js.map +1 -0
- package/build/trim/mutation-ack.d.ts +11 -0
- package/build/trim/mutation-ack.d.ts.map +1 -0
- package/build/trim/mutation-ack.js +52 -0
- package/build/trim/mutation-ack.js.map +1 -0
- package/build/trim/mutation-ack.test.d.ts +2 -0
- package/build/trim/mutation-ack.test.d.ts.map +1 -0
- package/build/trim/mutation-ack.test.js +67 -0
- package/build/trim/mutation-ack.test.js.map +1 -0
- package/build/types/refs.d.ts +14 -0
- package/build/types/refs.d.ts.map +1 -0
- package/build/types/refs.js +9 -0
- package/build/types/refs.js.map +1 -0
- package/package.json +181 -0
- package/scripts/install-skill.mjs +128 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// Generic HTTP client factory.
|
|
2
|
+
//
|
|
3
|
+
// Wraps `undici.request` with the patterns every MCP server's API
|
|
4
|
+
// client needs but routinely re-implements: redirect following (the
|
|
5
|
+
// /diff endpoint on Bitbucket 302s to a signed CDN URL — a footgun
|
|
6
|
+
// that breaks naive clients), 204 → `{}` for JSON / `""` for raw,
|
|
7
|
+
// best-effort JSON parse on the error path so structured errors
|
|
8
|
+
// surface in the thrown Error, and a separate `getText()` for text
|
|
9
|
+
// endpoints.
|
|
10
|
+
//
|
|
11
|
+
// Auth is pluggable: Basic, Bearer, or a fully custom header
|
|
12
|
+
// generator (for OAuth flows, signed requests, etc.).
|
|
13
|
+
import { request } from "undici";
|
|
14
|
+
import { httpRequest } from "./transport.js";
|
|
15
|
+
export class HttpClientError extends Error {
|
|
16
|
+
statusCode;
|
|
17
|
+
response;
|
|
18
|
+
constructor(message, statusCode,
|
|
19
|
+
// Best-effort JSON-parsed response body. Undefined if the body
|
|
20
|
+
// was empty or not parseable as JSON.
|
|
21
|
+
response) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.statusCode = statusCode;
|
|
24
|
+
this.response = response;
|
|
25
|
+
this.name = "HttpClientError";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function createHttpClient(opts) {
|
|
29
|
+
const baseUrl = opts.baseUrl;
|
|
30
|
+
const userAgent = opts.userAgent;
|
|
31
|
+
const maxRedirections = opts.maxRedirections ?? 5;
|
|
32
|
+
const retry = opts.retry;
|
|
33
|
+
const authResolver = makeAuthResolver(opts.auth);
|
|
34
|
+
function buildUrl(path, queryParams) {
|
|
35
|
+
const url = new URL(`${baseUrl}${path}`);
|
|
36
|
+
if (queryParams) {
|
|
37
|
+
for (const [key, value] of Object.entries(queryParams)) {
|
|
38
|
+
if (value === undefined)
|
|
39
|
+
continue;
|
|
40
|
+
url.searchParams.append(key, String(value));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return url.toString();
|
|
44
|
+
}
|
|
45
|
+
async function doRequest(reqOpts) {
|
|
46
|
+
const url = buildUrl(reqOpts.path, reqOpts.queryParams);
|
|
47
|
+
const headers = {
|
|
48
|
+
Authorization: authResolver(),
|
|
49
|
+
Accept: reqOpts.accept ?? "application/json",
|
|
50
|
+
"User-Agent": userAgent,
|
|
51
|
+
};
|
|
52
|
+
let bodyPayload;
|
|
53
|
+
if (reqOpts.body !== undefined &&
|
|
54
|
+
reqOpts.method !== "GET" &&
|
|
55
|
+
reqOpts.method !== "DELETE") {
|
|
56
|
+
headers["Content-Type"] = "application/json";
|
|
57
|
+
bodyPayload = JSON.stringify(reqOpts.body);
|
|
58
|
+
}
|
|
59
|
+
let statusCode;
|
|
60
|
+
let text;
|
|
61
|
+
if (retry) {
|
|
62
|
+
// Retry+pool path. Note: the retry transport doesn't currently
|
|
63
|
+
// follow redirects — endpoints that 302 to signed CDN URLs
|
|
64
|
+
// (e.g. bitbucket's /diff) should stay on the single-shot path
|
|
65
|
+
// until the transport grows redirect handling.
|
|
66
|
+
const res = await httpRequest(url, { method: reqOpts.method, headers, body: bodyPayload }, retry);
|
|
67
|
+
statusCode = res.statusCode;
|
|
68
|
+
text = await res.text();
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
const res = await request(url, {
|
|
72
|
+
method: reqOpts.method,
|
|
73
|
+
headers,
|
|
74
|
+
body: bodyPayload,
|
|
75
|
+
// Critical for endpoints that 302 to a signed CDN URL
|
|
76
|
+
// (e.g. bitbucket's /diff). Without this the response body is
|
|
77
|
+
// the redirect notice, not the actual payload.
|
|
78
|
+
maxRedirections,
|
|
79
|
+
});
|
|
80
|
+
statusCode = res.statusCode;
|
|
81
|
+
text = await res.body.text();
|
|
82
|
+
}
|
|
83
|
+
if (statusCode === 204) {
|
|
84
|
+
return reqOpts.raw ? "" : {};
|
|
85
|
+
}
|
|
86
|
+
// Best-effort JSON parse for the error path even when raw=true,
|
|
87
|
+
// so we can surface a structured error message.
|
|
88
|
+
let parsed = undefined;
|
|
89
|
+
let parseError = null;
|
|
90
|
+
if (text) {
|
|
91
|
+
try {
|
|
92
|
+
parsed = JSON.parse(text);
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
parseError = err;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const ok = statusCode >= 200 && statusCode < 300;
|
|
99
|
+
if (!ok) {
|
|
100
|
+
const msg = extractErrorMessage(parsed) ?? text ?? `HTTP ${statusCode}`;
|
|
101
|
+
throw new HttpClientError(msg, statusCode, parsed);
|
|
102
|
+
}
|
|
103
|
+
// raw mode: return the body verbatim regardless of content type.
|
|
104
|
+
if (reqOpts.raw) {
|
|
105
|
+
return text;
|
|
106
|
+
}
|
|
107
|
+
// Successful non-JSON response (rare for JSON endpoints).
|
|
108
|
+
if (parseError) {
|
|
109
|
+
return text;
|
|
110
|
+
}
|
|
111
|
+
return parsed ?? {};
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
async get(path, queryParams) {
|
|
115
|
+
return doRequest({ method: "GET", path, queryParams });
|
|
116
|
+
},
|
|
117
|
+
async getText(path, queryParams, opts) {
|
|
118
|
+
return doRequest({
|
|
119
|
+
method: "GET",
|
|
120
|
+
path,
|
|
121
|
+
queryParams,
|
|
122
|
+
accept: opts?.accept ?? "text/plain",
|
|
123
|
+
raw: true,
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
async post(path, body, queryParams) {
|
|
127
|
+
return doRequest({ method: "POST", path, body, queryParams });
|
|
128
|
+
},
|
|
129
|
+
async put(path, body, queryParams) {
|
|
130
|
+
return doRequest({ method: "PUT", path, body, queryParams });
|
|
131
|
+
},
|
|
132
|
+
async delete(path, queryParams) {
|
|
133
|
+
return doRequest({ method: "DELETE", path, queryParams });
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
// --- Internal helpers --------------------------------------------------
|
|
138
|
+
function makeAuthResolver(auth) {
|
|
139
|
+
switch (auth.kind) {
|
|
140
|
+
case "basic": {
|
|
141
|
+
const credentials = Buffer.from(`${auth.username}:${auth.password}`).toString("base64");
|
|
142
|
+
const header = `Basic ${credentials}`;
|
|
143
|
+
return () => header;
|
|
144
|
+
}
|
|
145
|
+
case "bearer": {
|
|
146
|
+
const header = `Bearer ${auth.token}`;
|
|
147
|
+
return () => header;
|
|
148
|
+
}
|
|
149
|
+
case "custom":
|
|
150
|
+
return auth.header;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// Best-effort extraction of a human-readable message from a parsed
|
|
154
|
+
// error response. Covers the common shapes:
|
|
155
|
+
// - Bitbucket: { error: { message, detail } }
|
|
156
|
+
// - Jira / Atlassian: { errorMessages: ["..."], errors: {...} }
|
|
157
|
+
// - Plain { message: "..." }
|
|
158
|
+
// Returns undefined if nothing recognizable found; caller falls back
|
|
159
|
+
// to raw body text or "HTTP <status>".
|
|
160
|
+
function extractErrorMessage(parsed) {
|
|
161
|
+
if (!parsed || typeof parsed !== "object")
|
|
162
|
+
return undefined;
|
|
163
|
+
const p = parsed;
|
|
164
|
+
// { error: { message?, detail? } }
|
|
165
|
+
if (p.error && typeof p.error === "object") {
|
|
166
|
+
const err = p.error;
|
|
167
|
+
if (typeof err.message === "string")
|
|
168
|
+
return err.message;
|
|
169
|
+
if (typeof err.detail === "string")
|
|
170
|
+
return err.detail;
|
|
171
|
+
}
|
|
172
|
+
// { errorMessages: ["..."] }
|
|
173
|
+
if (Array.isArray(p.errorMessages) && p.errorMessages.length > 0) {
|
|
174
|
+
const first = p.errorMessages[0];
|
|
175
|
+
if (typeof first === "string")
|
|
176
|
+
return first;
|
|
177
|
+
}
|
|
178
|
+
// { message: "..." }
|
|
179
|
+
if (typeof p.message === "string")
|
|
180
|
+
return p.message;
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
//# sourceMappingURL=http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/client/http.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,EAAE;AACF,kEAAkE;AAClE,oEAAoE;AACpE,mEAAmE;AACnE,kEAAkE;AAClE,gEAAgE;AAChE,mEAAmE;AACnE,aAAa;AACb,EAAE;AACF,6DAA6D;AAC7D,sDAAsD;AAEtD,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAGjC,OAAO,EAAE,WAAW,EAAqB,MAAM,gBAAgB,CAAC;AAwBhE,MAAM,OAAO,eAAgB,SAAQ,KAAK;IAGtB;IAGA;IALlB,YACE,OAAe,EACC,UAAkB;IAClC,+DAA+D;IAC/D,sCAAsC;IACtB,QAAkB;QAElC,KAAK,CAAC,OAAO,CAAC,CAAC;QALC,eAAU,GAAV,UAAU,CAAQ;QAGlB,aAAQ,GAAR,QAAQ,CAAU;QAGlC,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AAyBD,MAAM,UAAU,gBAAgB,CAAC,IAAoB;IACnD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;IAC7B,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,C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xD,IAAI,OAAO,GAAG,CAAC,MAAM,KAAK,QAAQ;YAAE,OAAO,GAAG,CAAC,MAAM,CAAC;IACxD,CAAC;IACD,6BAA6B;IAC7B,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACjE,MAAM,KAAK,GAAG,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;QACjC,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE,OAAO,KAAK,CAAC;IAC9C,CAAC;IACD,qBAAqB;IACrB,IAAI,OAAO,CAAC,CAAC,OAAO,KAAK,QAAQ;QAAE,OAAO,CAAC,CAAC,OAAO,CAAC;IACpD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.test.d.ts","sourceRoot":"","sources":["../../src/client/http.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { HttpClientError, createHttpClient } from "./http.js";
|
|
3
|
+
// Note: end-to-end request tests would need either undici's
|
|
4
|
+
// MockAgent or a real server; both are heavier than is justified for
|
|
5
|
+
// this lift. These tests cover the configuration plumbing and the
|
|
6
|
+
// HttpClientError shape; the body behavior (redirects, 204, error
|
|
7
|
+
// path) is exercised by the bitbucket-mcp consumer tests against
|
|
8
|
+
// recorded fixtures.
|
|
9
|
+
describe("createHttpClient", () => {
|
|
10
|
+
it("returns a client with the Client interface methods + getText", () => {
|
|
11
|
+
const c = createHttpClient({
|
|
12
|
+
baseUrl: "https://example.test",
|
|
13
|
+
auth: { kind: "bearer", token: "xx" },
|
|
14
|
+
userAgent: "test/1.0",
|
|
15
|
+
});
|
|
16
|
+
expect(typeof c.get).toBe("function");
|
|
17
|
+
expect(typeof c.post).toBe("function");
|
|
18
|
+
expect(typeof c.put).toBe("function");
|
|
19
|
+
expect(typeof c.delete).toBe("function");
|
|
20
|
+
expect(typeof c.getText).toBe("function");
|
|
21
|
+
});
|
|
22
|
+
it("accepts basic / bearer / custom auth without throwing", () => {
|
|
23
|
+
expect(() => createHttpClient({
|
|
24
|
+
baseUrl: "https://x",
|
|
25
|
+
auth: { kind: "basic", username: "u", password: "p" },
|
|
26
|
+
userAgent: "ua",
|
|
27
|
+
})).not.toThrow();
|
|
28
|
+
expect(() => createHttpClient({
|
|
29
|
+
baseUrl: "https://x",
|
|
30
|
+
auth: { kind: "bearer", token: "t" },
|
|
31
|
+
userAgent: "ua",
|
|
32
|
+
})).not.toThrow();
|
|
33
|
+
expect(() => createHttpClient({
|
|
34
|
+
baseUrl: "https://x",
|
|
35
|
+
auth: { kind: "custom", header: () => "X-Auth abc" },
|
|
36
|
+
userAgent: "ua",
|
|
37
|
+
})).not.toThrow();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
describe("HttpClientError", () => {
|
|
41
|
+
it("carries status code and response payload", () => {
|
|
42
|
+
const e = new HttpClientError("nope", 404, { error: { message: "nope" } });
|
|
43
|
+
expect(e.name).toBe("HttpClientError");
|
|
44
|
+
expect(e.statusCode).toBe(404);
|
|
45
|
+
expect(e.response).toEqual({ error: { message: "nope" } });
|
|
46
|
+
expect(e instanceof Error).toBe(true);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
describe("getText Accept header", () => {
|
|
50
|
+
it("sends text/plain by default and the caller's accept when given", async () => {
|
|
51
|
+
const { createServer } = await import("node:http");
|
|
52
|
+
const seen = [];
|
|
53
|
+
const server = createServer((req, res) => {
|
|
54
|
+
seen.push(req.headers.accept);
|
|
55
|
+
// Mirrors Bitbucket's step-log endpoint: 406 unless the client
|
|
56
|
+
// accepts octet-stream.
|
|
57
|
+
if (!req.headers.accept?.includes("octet-stream")) {
|
|
58
|
+
res.writeHead(406, { "Content-Type": "application/json" }).end('{"error":"not acceptable"}');
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
res.writeHead(200, { "Content-Type": "application/octet-stream" }).end("line 1\nline 2\n");
|
|
62
|
+
});
|
|
63
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
64
|
+
const { port } = server.address();
|
|
65
|
+
try {
|
|
66
|
+
const c = createHttpClient({ baseUrl: `http://127.0.0.1:${port}`, userAgent: "test", auth: { kind: "bearer", token: "t" } });
|
|
67
|
+
await expect(c.getText("/log")).rejects.toMatchObject({ statusCode: 406 });
|
|
68
|
+
await expect(c.getText("/log", undefined, { accept: "application/octet-stream" })).resolves.toBe("line 1\nline 2\n");
|
|
69
|
+
expect(seen).toEqual(["text/plain", "application/octet-stream"]);
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
await new Promise((resolve) => server.close(() => resolve()));
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
//# sourceMappingURL=http.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.test.js","sourceRoot":"","sources":["../../src/client/http.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAE9C,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAE9D,4DAA4D;AAC5D,qEAAqE;AACrE,kEAAkE;AAClE,kEAAkE;AAClE,iEAAiE;AACjE,qBAAqB;AAErB,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE;IAChC,EAAE,CAAC,8DAA8D,EAAE,GAAG,EAAE;QACtE,MAAM,CAAC,GAAG,gBAAgB,CAAC;YACzB,OAAO,EAAE,sBAAsB;YAC/B,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE;YACrC,SAAS,EAAE,UAAU;SACtB,CAAC,CAAC;QACH,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CA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|
2
|
+
export type QueryParams = Record<string, QueryValue>;
|
|
3
|
+
export interface Client {
|
|
4
|
+
get(path: string, queryParams?: QueryParams): Promise<unknown>;
|
|
5
|
+
post(path: string, body?: unknown, queryParams?: QueryParams): Promise<unknown>;
|
|
6
|
+
put(path: string, body?: unknown, queryParams?: QueryParams): Promise<unknown>;
|
|
7
|
+
delete(path: string, queryParams?: QueryParams): Promise<unknown>;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAkBA,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAAC;AAC/D,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAErD,MAAM,WAAW,MAAM;IACrB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC/D,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAChF,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC/E,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACnE"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Generic HTTP client interface for SDK consumers.
|
|
2
|
+
//
|
|
3
|
+
// Each MCP server (jira-mcp, confluence-mcp, bitbucket-mcp) builds its
|
|
4
|
+
// own concrete client because the auth model differs (Basic vs Bearer,
|
|
5
|
+
// API token vs scoped token, OAuth flows, etc.). The SDK's only
|
|
6
|
+
// requirement is the four-verb shape below.
|
|
7
|
+
//
|
|
8
|
+
// Returns `Promise<unknown>` — callers narrow as needed. (A generic
|
|
9
|
+
// `<T>` return wouldn't be soundly implementable: the implementation
|
|
10
|
+
// has only one concrete return shape, but a generic position implies
|
|
11
|
+
// it can return any T the caller picks. Operations validate response
|
|
12
|
+
// shape via trim projections, not via the Client interface.)
|
|
13
|
+
//
|
|
14
|
+
// Servers with multi-API routing (e.g. jira's platform-vs-agile split)
|
|
15
|
+
// implement Client once and use the `ExecuteFn` hook on
|
|
16
|
+
// `invokeOperation` to decide per-operation which downstream method to
|
|
17
|
+
// call.
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,EAAE;AACF,uEAAuE;AACvE,uEAAuE;AACvE,gEAAgE;AAChE,4CAA4C;AAC5C,EAAE;AACF,oEAAoE;AACpE,qEAAqE;AACrE,qEAAqE;AACrE,qEAAqE;AACrE,6DAA6D;AAC7D,EAAE;AACF,uEAAuE;AACvE,wDAAwD;AACxD,uEAAuE;AACvE,QAAQ"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Readable } from "node:stream";
|
|
2
|
+
export declare function sanitizeFilename(name: string): string;
|
|
3
|
+
export interface SingleConsumptionResponse {
|
|
4
|
+
statusCode: number;
|
|
5
|
+
body: Readable;
|
|
6
|
+
bodyText: () => Promise<string>;
|
|
7
|
+
}
|
|
8
|
+
export declare function guardSingleConsumption(statusCode: number, underlying: {
|
|
9
|
+
stream: () => Readable;
|
|
10
|
+
text: () => Promise<string>;
|
|
11
|
+
}): SingleConsumptionResponse;
|
|
12
|
+
export type DownloadTransport = (url: string, init: {
|
|
13
|
+
method: "GET";
|
|
14
|
+
headers: Record<string, string>;
|
|
15
|
+
}) => Promise<SingleConsumptionResponse>;
|
|
16
|
+
export interface DownloadRef {
|
|
17
|
+
absolutePath: string;
|
|
18
|
+
size: number;
|
|
19
|
+
sha256: string;
|
|
20
|
+
}
|
|
21
|
+
export interface DownloadToFileOpts {
|
|
22
|
+
url: string;
|
|
23
|
+
headers?: Record<string, string>;
|
|
24
|
+
targetDir: string;
|
|
25
|
+
filename: string;
|
|
26
|
+
transport?: DownloadTransport;
|
|
27
|
+
}
|
|
28
|
+
export declare function downloadToFile(opts: DownloadToFileOpts): Promise<DownloadRef>;
|
|
29
|
+
//# sourceMappingURL=streaming.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["../../src/client/streaming.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAYvC,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAoBrD;AASD,MAAM,WAAW,yBAAyB;IACxC,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,QAAQ,CAAC;IACf,QAAQ,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;CACjC;AAED,wBAAgB,sBAAsB,CACpC,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE;IACV,MAAM,EAAE,MAAM,QAAQ,CAAC;IACvB,IAAI,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;CAC7B,GACA,yBAAyB,CA6B3B;AAID,MAAM,MAAM,iBAAiB,GAAG,CAC9B,GAAG,EAAE,MAAM,EACX,IAAI,EAAE;IAAE,MAAM,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,KACrD,OAAO,CAAC,yBAAyB,CAAC,CAAC;AAexC,MAAM,WAAW,WAAW;IAC1B,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,kBAAkB;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IAEjB,SAAS,CAAC,EAAE,iBAAiB,CAAC;CAC/B;AAMD,wBAAsB,cAAc,CAAC,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,WAAW,CAAC,CA6CnF"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Generic streaming download primitive.
|
|
2
|
+
//
|
|
3
|
+
// Tools that fetch large binaries (attachments, generated reports,
|
|
4
|
+
// build artifacts) want to hand the agent a local path, not the raw
|
|
5
|
+
// bytes — a 50 MB attachment must never hit Node's heap. This module
|
|
6
|
+
// streams a URL to disk, returns metadata + checksum, and protects
|
|
7
|
+
// against double-consumption / path traversal.
|
|
8
|
+
//
|
|
9
|
+
// Vendor-neutral on purpose. The toolkit ships no "issue" or
|
|
10
|
+
// "attachment" concept; consumers pass a `targetDir` and an arbitrary
|
|
11
|
+
// `filename`, and the primitive guarantees the resulting path is
|
|
12
|
+
// under `targetDir`.
|
|
13
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
14
|
+
import { createReadStream, createWriteStream } from "node:fs";
|
|
15
|
+
import * as fs from "node:fs/promises";
|
|
16
|
+
import * as path from "node:path";
|
|
17
|
+
import { Readable } from "node:stream";
|
|
18
|
+
import { pipeline } from "node:stream/promises";
|
|
19
|
+
import { request as undiciRequest } from "undici";
|
|
20
|
+
// --- Filename sanitization --------------------------------------------
|
|
21
|
+
// Filenames become path segments, so we strip anything that could
|
|
22
|
+
// break out of the intended directory or trip the filesystem.
|
|
23
|
+
const UNSAFE_CHARS = /[/\\\x00]/g;
|
|
24
|
+
const LEADING_DOTS = /^\.+/;
|
|
25
|
+
export function sanitizeFilename(name) {
|
|
26
|
+
// Drop any path components a caller might smuggle in.
|
|
27
|
+
let base = name.split(/[/\\]/).pop() ?? "";
|
|
28
|
+
base = base.replace(UNSAFE_CHARS, "_");
|
|
29
|
+
// Also strip other ASCII control characters (\x01-\x1F, \x7F).
|
|
30
|
+
base = base.replace(/[\x01-\x1F\x7F]/g, "_");
|
|
31
|
+
base = base.replace(LEADING_DOTS, "");
|
|
32
|
+
base = base.replace(/\s+/g, " ").trim();
|
|
33
|
+
// Cap length to keep ext4/APFS/NTFS happy (all cap at 255).
|
|
34
|
+
if (base.length > 200) {
|
|
35
|
+
const dot = base.lastIndexOf(".");
|
|
36
|
+
if (dot > 0 && base.length - dot <= 16) {
|
|
37
|
+
const ext = base.slice(dot);
|
|
38
|
+
base = `${base.slice(0, 200 - ext.length)}${ext}`;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
base = base.slice(0, 200);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (base.length === 0)
|
|
45
|
+
base = "download";
|
|
46
|
+
return base;
|
|
47
|
+
}
|
|
48
|
+
export function guardSingleConsumption(statusCode, underlying) {
|
|
49
|
+
let consumed = "none";
|
|
50
|
+
return {
|
|
51
|
+
statusCode,
|
|
52
|
+
get body() {
|
|
53
|
+
if (consumed === "text") {
|
|
54
|
+
throw new Error("Response body already consumed via bodyText()");
|
|
55
|
+
}
|
|
56
|
+
if (consumed === "stream") {
|
|
57
|
+
throw new Error("Response body already consumed via body getter");
|
|
58
|
+
}
|
|
59
|
+
consumed = "stream";
|
|
60
|
+
return underlying.stream();
|
|
61
|
+
},
|
|
62
|
+
bodyText: () => {
|
|
63
|
+
if (consumed === "stream") {
|
|
64
|
+
return Promise.reject(new Error("Response body already consumed via stream"));
|
|
65
|
+
}
|
|
66
|
+
if (consumed === "text") {
|
|
67
|
+
return Promise.reject(new Error("Response body already consumed via bodyText()"));
|
|
68
|
+
}
|
|
69
|
+
consumed = "text";
|
|
70
|
+
return underlying.text();
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const defaultTransport = async (url, init) => {
|
|
75
|
+
const res = await undiciRequest(url, {
|
|
76
|
+
method: init.method,
|
|
77
|
+
headers: init.headers,
|
|
78
|
+
});
|
|
79
|
+
return guardSingleConsumption(res.statusCode, {
|
|
80
|
+
stream: () => Readable.from(res.body),
|
|
81
|
+
text: () => res.body.text(),
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
// Stream `url` to `targetDir/sanitize(filename)`. Atomic: writes to a
|
|
85
|
+
// `.partial` file and renames on success; the rename means a partial
|
|
86
|
+
// file from an interrupted download never poses as the real one.
|
|
87
|
+
// Returns absolute path, byte length, and sha256 of the bytes written.
|
|
88
|
+
export async function downloadToFile(opts) {
|
|
89
|
+
const transport = opts.transport ?? defaultTransport;
|
|
90
|
+
const filename = sanitizeFilename(opts.filename);
|
|
91
|
+
const targetPath = path.join(opts.targetDir, filename);
|
|
92
|
+
// Defense in depth: even after sanitizeFilename strips separators,
|
|
93
|
+
// assert the resolved path is under targetDir. A future regression
|
|
94
|
+
// in sanitizeFilename can't escape.
|
|
95
|
+
const resolvedTarget = path.resolve(targetPath);
|
|
96
|
+
const resolvedDir = path.resolve(opts.targetDir);
|
|
97
|
+
if (!resolvedTarget.startsWith(resolvedDir + path.sep) && resolvedTarget !== resolvedDir) {
|
|
98
|
+
throw new Error(`Refusing to download outside targetDir: ${resolvedTarget}`);
|
|
99
|
+
}
|
|
100
|
+
await fs.mkdir(opts.targetDir, { recursive: true });
|
|
101
|
+
const res = await transport(opts.url, {
|
|
102
|
+
method: "GET",
|
|
103
|
+
headers: opts.headers ?? {},
|
|
104
|
+
});
|
|
105
|
+
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
106
|
+
const errText = await res.bodyText().catch(() => "");
|
|
107
|
+
throw new Error(`Download failed for ${opts.url}: HTTP ${res.statusCode} ${errText.slice(0, 200)}`);
|
|
108
|
+
}
|
|
109
|
+
// Temp + rename for atomicity. Random suffix so two concurrent
|
|
110
|
+
// downloads of the same target don't corrupt each other.
|
|
111
|
+
const tempPath = `${targetPath}.${process.pid}.${randomBytes(6).toString("hex")}.partial`;
|
|
112
|
+
try {
|
|
113
|
+
await pipeline(res.body, createWriteStream(tempPath));
|
|
114
|
+
await fs.rename(tempPath, targetPath);
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
await fs.rm(tempPath, { force: true });
|
|
118
|
+
throw err;
|
|
119
|
+
}
|
|
120
|
+
const stat = await fs.stat(targetPath);
|
|
121
|
+
const sha256 = await hashFile(targetPath);
|
|
122
|
+
return {
|
|
123
|
+
absolutePath: targetPath,
|
|
124
|
+
size: stat.size,
|
|
125
|
+
sha256,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
async function hashFile(p) {
|
|
129
|
+
const hash = createHash("sha256");
|
|
130
|
+
await pipeline(createReadStream(p), hash);
|
|
131
|
+
return hash.digest("hex");
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=streaming.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streaming.js","sourceRoot":"","sources":["../../src/client/streaming.ts"],"names":[],"mappings":"AAAA,wCAAwC;AACxC,EAAE;AACF,mEAAmE;AACnE,oEAAoE;AACpE,qEAAqE;AACrE,mEAAmE;AACnE,+CAA+C;AAC/C,EAAE;AACF,6DAA6D;AAC7D,sEAAsE;AACtE,iEAAiE;AACjE,qBAAqB;AAErB,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAC9D,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,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C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streaming.test.d.ts","sourceRoot":"","sources":["../../src/client/streaming.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import * as fs from "node:fs/promises";
|
|
4
|
+
import * as os from "node:os";
|
|
5
|
+
import * as path from "node:path";
|
|
6
|
+
import { Readable } from "node:stream";
|
|
7
|
+
import { downloadToFile, guardSingleConsumption, sanitizeFilename, } from "./streaming.js";
|
|
8
|
+
describe("sanitizeFilename", () => {
|
|
9
|
+
it("strips path separators", () => {
|
|
10
|
+
expect(sanitizeFilename("../../etc/passwd")).toBe("passwd");
|
|
11
|
+
expect(sanitizeFilename("a\\b\\c.txt")).toBe("c.txt");
|
|
12
|
+
});
|
|
13
|
+
it("strips null bytes and control chars", () => {
|
|
14
|
+
expect(sanitizeFilename("file\x00name")).toBe("file_name");
|
|
15
|
+
expect(sanitizeFilename("a\x01b\x1fc")).toBe("a_b_c");
|
|
16
|
+
});
|
|
17
|
+
it("strips leading dots (no hidden files)", () => {
|
|
18
|
+
expect(sanitizeFilename(".env")).toBe("env");
|
|
19
|
+
expect(sanitizeFilename("..hidden")).toBe("hidden");
|
|
20
|
+
});
|
|
21
|
+
it("substitutes a default for fully-empty results", () => {
|
|
22
|
+
expect(sanitizeFilename("")).toBe("download");
|
|
23
|
+
expect(sanitizeFilename("...")).toBe("download");
|
|
24
|
+
expect(sanitizeFilename("/")).toBe("download");
|
|
25
|
+
});
|
|
26
|
+
it("caps length while preserving the last short extension", () => {
|
|
27
|
+
// lastIndexOf("."), so multi-segment extensions reduce to the
|
|
28
|
+
// trailing piece — ".tar.gz" → ".gz" at the cap.
|
|
29
|
+
const long = "x".repeat(300) + ".gz";
|
|
30
|
+
const r = sanitizeFilename(long);
|
|
31
|
+
expect(r.length).toBeLessThanOrEqual(200);
|
|
32
|
+
expect(r.endsWith(".gz")).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
it("caps length when there is no short extension", () => {
|
|
35
|
+
const long = "y".repeat(300);
|
|
36
|
+
expect(sanitizeFilename(long).length).toBe(200);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
describe("guardSingleConsumption", () => {
|
|
40
|
+
it("permits one stream read, then rejects further access", () => {
|
|
41
|
+
const wrapped = guardSingleConsumption(200, {
|
|
42
|
+
stream: () => Readable.from(["body"]),
|
|
43
|
+
text: async () => "body",
|
|
44
|
+
});
|
|
45
|
+
// First read OK.
|
|
46
|
+
const _stream = wrapped.body;
|
|
47
|
+
expect(_stream).toBeDefined();
|
|
48
|
+
// Second access throws.
|
|
49
|
+
expect(() => wrapped.body).toThrow(/already consumed/);
|
|
50
|
+
return expect(wrapped.bodyText()).rejects.toThrow(/already consumed/);
|
|
51
|
+
});
|
|
52
|
+
it("permits one text read, then rejects further access", async () => {
|
|
53
|
+
const wrapped = guardSingleConsumption(200, {
|
|
54
|
+
stream: () => Readable.from(["body"]),
|
|
55
|
+
text: async () => "body",
|
|
56
|
+
});
|
|
57
|
+
await expect(wrapped.bodyText()).resolves.toBe("body");
|
|
58
|
+
await expect(wrapped.bodyText()).rejects.toThrow(/already consumed/);
|
|
59
|
+
expect(() => wrapped.body).toThrow(/already consumed/);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
describe("downloadToFile", () => {
|
|
63
|
+
let tmpDir;
|
|
64
|
+
beforeEach(async () => {
|
|
65
|
+
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "ultra-mcp-toolkit-stream-"));
|
|
66
|
+
});
|
|
67
|
+
afterEach(async () => {
|
|
68
|
+
await fs.rm(tmpDir, { recursive: true, force: true });
|
|
69
|
+
});
|
|
70
|
+
function fakeTransport(body, statusCode = 200) {
|
|
71
|
+
return async () => guardSingleConsumption(statusCode, {
|
|
72
|
+
stream: () => Readable.from(body),
|
|
73
|
+
text: async () => body.toString("utf8"),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
it("writes bytes to disk and returns absolutePath/size/sha256", async () => {
|
|
77
|
+
const payload = Buffer.from("hello world");
|
|
78
|
+
const expectedHash = createHash("sha256").update(payload).digest("hex");
|
|
79
|
+
const ref = await downloadToFile({
|
|
80
|
+
url: "https://x/y",
|
|
81
|
+
filename: "out.txt",
|
|
82
|
+
targetDir: tmpDir,
|
|
83
|
+
transport: fakeTransport(payload),
|
|
84
|
+
});
|
|
85
|
+
expect(ref.absolutePath).toBe(path.join(tmpDir, "out.txt"));
|
|
86
|
+
expect(ref.size).toBe(payload.length);
|
|
87
|
+
expect(ref.sha256).toBe(expectedHash);
|
|
88
|
+
const onDisk = await fs.readFile(ref.absolutePath);
|
|
89
|
+
expect(onDisk.equals(payload)).toBe(true);
|
|
90
|
+
});
|
|
91
|
+
it("produces stable sha256 across calls with identical payloads", async () => {
|
|
92
|
+
const payload = Buffer.from("repeatable");
|
|
93
|
+
const a = await downloadToFile({
|
|
94
|
+
url: "https://x/a",
|
|
95
|
+
filename: "a.bin",
|
|
96
|
+
targetDir: tmpDir,
|
|
97
|
+
transport: fakeTransport(payload),
|
|
98
|
+
});
|
|
99
|
+
const b = await downloadToFile({
|
|
100
|
+
url: "https://x/b",
|
|
101
|
+
filename: "b.bin",
|
|
102
|
+
targetDir: tmpDir,
|
|
103
|
+
transport: fakeTransport(payload),
|
|
104
|
+
});
|
|
105
|
+
expect(a.sha256).toBe(b.sha256);
|
|
106
|
+
});
|
|
107
|
+
it("sanitizes the filename before writing", async () => {
|
|
108
|
+
const ref = await downloadToFile({
|
|
109
|
+
url: "https://x/y",
|
|
110
|
+
filename: "../../etc/passwd",
|
|
111
|
+
targetDir: tmpDir,
|
|
112
|
+
transport: fakeTransport(Buffer.from("safe")),
|
|
113
|
+
});
|
|
114
|
+
expect(ref.absolutePath).toBe(path.join(tmpDir, "passwd"));
|
|
115
|
+
const stat = await fs.stat(ref.absolutePath);
|
|
116
|
+
expect(stat.isFile()).toBe(true);
|
|
117
|
+
});
|
|
118
|
+
it("writes atomically (no .partial files remain on success)", async () => {
|
|
119
|
+
await downloadToFile({
|
|
120
|
+
url: "https://x/y",
|
|
121
|
+
filename: "atomic.bin",
|
|
122
|
+
targetDir: tmpDir,
|
|
123
|
+
transport: fakeTransport(Buffer.from("done")),
|
|
124
|
+
});
|
|
125
|
+
const entries = await fs.readdir(tmpDir);
|
|
126
|
+
expect(entries.every((e) => !e.includes(".partial"))).toBe(true);
|
|
127
|
+
expect(entries).toContain("atomic.bin");
|
|
128
|
+
});
|
|
129
|
+
it("throws on non-2xx status with the server message", async () => {
|
|
130
|
+
const transport = fakeTransport(Buffer.from("not found"), 404);
|
|
131
|
+
await expect(downloadToFile({
|
|
132
|
+
url: "https://x/y",
|
|
133
|
+
filename: "missing.bin",
|
|
134
|
+
targetDir: tmpDir,
|
|
135
|
+
transport,
|
|
136
|
+
})).rejects.toThrow(/HTTP 404/);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
//# sourceMappingURL=streaming.test.js.map
|