@bigbrain-work/mcp-connect 1.3.4 → 1.3.5
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/package.json +1 -1
- package/src/keyring-worker.js +40 -0
- package/src/token-store.js +72 -1
package/package.json
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { stdin, stdout } from "node:process";
|
|
2
|
+
|
|
3
|
+
async function readRequest() {
|
|
4
|
+
let input = "";
|
|
5
|
+
stdin.setEncoding("utf8");
|
|
6
|
+
for await (const chunk of stdin) input += chunk;
|
|
7
|
+
return JSON.parse(input);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function validateRequest(request) {
|
|
11
|
+
if (
|
|
12
|
+
!request ||
|
|
13
|
+
!["get", "set", "delete"].includes(request.operation) ||
|
|
14
|
+
typeof request.service !== "string" ||
|
|
15
|
+
typeof request.account !== "string" ||
|
|
16
|
+
(request.operation === "set" && typeof request.password !== "string")
|
|
17
|
+
) {
|
|
18
|
+
throw new Error("凭据库请求无效");
|
|
19
|
+
}
|
|
20
|
+
return request;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const request = validateRequest(await readRequest());
|
|
25
|
+
const { Entry } = await import("@napi-rs/keyring");
|
|
26
|
+
const entry = new Entry(request.service, request.account);
|
|
27
|
+
|
|
28
|
+
if (request.operation === "get") {
|
|
29
|
+
stdout.write(JSON.stringify({ value: await entry.getPassword() }));
|
|
30
|
+
} else if (request.operation === "set") {
|
|
31
|
+
await entry.setPassword(request.password);
|
|
32
|
+
stdout.write(JSON.stringify({ ok: true }));
|
|
33
|
+
} else {
|
|
34
|
+
await entry.deletePassword();
|
|
35
|
+
stdout.write(JSON.stringify({ ok: true }));
|
|
36
|
+
}
|
|
37
|
+
} catch (error) {
|
|
38
|
+
process.stderr.write(error?.message || String(error));
|
|
39
|
+
process.exitCode = 1;
|
|
40
|
+
}
|
package/src/token-store.js
CHANGED
|
@@ -3,6 +3,12 @@ import {
|
|
|
3
3
|
KEYRING_SERVICE,
|
|
4
4
|
PENDING_KEYRING_ACCOUNT,
|
|
5
5
|
} from "./constants.js";
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
|
|
9
|
+
const KEYRING_WORKER_PATH = fileURLToPath(
|
|
10
|
+
new URL("./keyring-worker.js", import.meta.url),
|
|
11
|
+
);
|
|
6
12
|
|
|
7
13
|
function validateTokenSet(value) {
|
|
8
14
|
if (
|
|
@@ -17,7 +23,72 @@ function validateTokenSet(value) {
|
|
|
17
23
|
return value;
|
|
18
24
|
}
|
|
19
25
|
|
|
20
|
-
|
|
26
|
+
export function runKeyringWorker(request, { spawnImpl = spawn } = {}) {
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
const child = spawnImpl(process.execPath, [KEYRING_WORKER_PATH], {
|
|
29
|
+
windowsHide: true,
|
|
30
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
31
|
+
});
|
|
32
|
+
let stdout = "";
|
|
33
|
+
let stderr = "";
|
|
34
|
+
child.stdout.setEncoding("utf8");
|
|
35
|
+
child.stderr.setEncoding("utf8");
|
|
36
|
+
child.stdout.on("data", (chunk) => {
|
|
37
|
+
stdout += chunk;
|
|
38
|
+
});
|
|
39
|
+
child.stderr.on("data", (chunk) => {
|
|
40
|
+
stderr += chunk;
|
|
41
|
+
});
|
|
42
|
+
child.on("error", reject);
|
|
43
|
+
child.on("close", (code) => {
|
|
44
|
+
if (code !== 0) {
|
|
45
|
+
reject(new Error(stderr.trim() || `凭据库子进程返回退出码 ${code}`));
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
resolve(JSON.parse(stdout));
|
|
50
|
+
} catch {
|
|
51
|
+
reject(new Error("凭据库子进程返回了无效响应"));
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
child.stdin.end(JSON.stringify(request));
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function createSystemEntry(
|
|
59
|
+
account,
|
|
60
|
+
{
|
|
61
|
+
platform = process.platform,
|
|
62
|
+
runWorker = runKeyringWorker,
|
|
63
|
+
} = {},
|
|
64
|
+
) {
|
|
65
|
+
if (platform === "win32") {
|
|
66
|
+
return {
|
|
67
|
+
async setPassword(password) {
|
|
68
|
+
await runWorker({
|
|
69
|
+
operation: "set",
|
|
70
|
+
service: KEYRING_SERVICE,
|
|
71
|
+
account,
|
|
72
|
+
password,
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
async getPassword() {
|
|
76
|
+
const result = await runWorker({
|
|
77
|
+
operation: "get",
|
|
78
|
+
service: KEYRING_SERVICE,
|
|
79
|
+
account,
|
|
80
|
+
});
|
|
81
|
+
return result.value;
|
|
82
|
+
},
|
|
83
|
+
async deletePassword() {
|
|
84
|
+
await runWorker({
|
|
85
|
+
operation: "delete",
|
|
86
|
+
service: KEYRING_SERVICE,
|
|
87
|
+
account,
|
|
88
|
+
});
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
21
92
|
let module;
|
|
22
93
|
try {
|
|
23
94
|
module = await import("@napi-rs/keyring");
|