inkmap-mcp 0.3.18 → 0.3.19

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.
@@ -1,6 +1,6 @@
1
1
  // packages/harness/src/host.mjs
2
2
  import { randomUUID as randomUUID2 } from "node:crypto";
3
- import { lstat as lstat3, realpath as realpath3 } from "node:fs/promises";
3
+ import { lstat as lstat3, realpath as realpath4 } from "node:fs/promises";
4
4
  import { isAbsolute as isAbsolute2, relative as relative3, sep as sep3 } from "node:path";
5
5
  import { fileURLToPath } from "node:url";
6
6
 
@@ -26,6 +26,52 @@ function hasAccess(state, now = Date.now()) {
26
26
  return state.pro || (state.trialUntil ?? 0) > now;
27
27
  }
28
28
 
29
+ // packages/core/dist/images.js
30
+ var MAX_IMAGE_BYTES = 10 * 1024 * 1024;
31
+ var startsWith = (bytes, signature, offset = 0) => [...signature].every((char, i) => bytes[offset + i] === char.charCodeAt(0));
32
+ function sniffImage(bytes) {
33
+ if (startsWith(bytes, "\x89PNG\r\n\n"))
34
+ return "png";
35
+ if (startsWith(bytes, "\xFF\xD8\xFF"))
36
+ return "jpg";
37
+ if (startsWith(bytes, "GIF87a") || startsWith(bytes, "GIF89a"))
38
+ return "gif";
39
+ if (startsWith(bytes, "RIFF") && startsWith(bytes, "WEBP", 8))
40
+ return "webp";
41
+ return void 0;
42
+ }
43
+ function decodeImageDataUrl(dataUrl) {
44
+ const match = typeof dataUrl === "string" ? /^data:[\w.+/-]*;base64,([A-Za-z0-9+/]*={0,2})$/.exec(dataUrl) : null;
45
+ if (!match)
46
+ return { problem: "type" };
47
+ if (match[1].length * 3 / 4 - 2 > MAX_IMAGE_BYTES)
48
+ return { problem: "size" };
49
+ let binary;
50
+ try {
51
+ binary = atob(match[1]);
52
+ } catch {
53
+ return { problem: "type" };
54
+ }
55
+ if (binary.length > MAX_IMAGE_BYTES)
56
+ return { problem: "size" };
57
+ const bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
58
+ const extension = sniffImage(bytes);
59
+ return extension ? { bytes, extension } : { problem: "type" };
60
+ }
61
+ function imageFolderName(documentFileName) {
62
+ const stem = documentFileName.replace(/\.(?:md|markdown)$/i, "").trim();
63
+ return `${/^\.*$/.test(stem) ? "images" : stem}.assets`;
64
+ }
65
+ function imageFileName(when, extension, attempt = 1) {
66
+ const two = (n) => String(n).padStart(2, "0");
67
+ const stamp = `${when.getFullYear()}${two(when.getMonth() + 1)}${two(when.getDate())}-${two(when.getHours())}${two(when.getMinutes())}${two(when.getSeconds())}`;
68
+ return `image-${stamp}${attempt > 1 ? `-${attempt}` : ""}.${extension}`;
69
+ }
70
+ function imageLinkPath(folder, file) {
71
+ const encode = (part) => part.replace(/[%\s()<>#?[\]\\"]/g, (char) => encodeURIComponent(char).replace(/\(/g, "%28").replace(/\)/g, "%29"));
72
+ return `${encode(folder)}/${encode(file)}`;
73
+ }
74
+
29
75
  // packages/core/dist/index.js
30
76
  function sanitizeViewState(raw) {
31
77
  if (!raw || typeof raw !== "object")
@@ -59,7 +105,7 @@ import { homedir, tmpdir } from "node:os";
59
105
  import { dirname, join } from "node:path";
60
106
 
61
107
  // packages/cli/dist/version.js
62
- var INKMAP_VERSION = "0.3.18";
108
+ var INKMAP_VERSION = "0.3.19";
63
109
 
64
110
  // packages/cli/dist/license.js
65
111
  var ZH_LOCALE_RE = /^zh(?:[-_.]|$)/i;
@@ -1137,48 +1183,20 @@ async function localTrialDeadline() {
1137
1183
  // packages/cli/dist/server.js
1138
1184
  import { createHash, randomBytes } from "node:crypto";
1139
1185
  import { watch } from "node:fs";
1140
- import { lstat as lstat2, mkdir as mkdir2, readFile as readFile3, realpath as realpath2, rename as rename2, rm as rm2, stat, writeFile as writeFile2 } from "node:fs/promises";
1186
+ import { lstat as lstat2, mkdir as mkdir3, readFile as readFile3, realpath as realpath3, rename as rename2, rm as rm2, stat, writeFile as writeFile3 } from "node:fs/promises";
1141
1187
  import { createServer } from "node:http";
1142
1188
  import { homedir as homedir3 } from "node:os";
1143
- import { basename as basename2, delimiter as delimiter2, dirname as dirname3, extname, join as join3, normalize, relative as relative2, resolve as resolve2, sep as sep2 } from "node:path";
1189
+ import { basename as basename2, delimiter as delimiter2, dirname as dirname3, extname, join as join4, normalize, relative as relative2, resolve as resolve2, sep as sep2 } from "node:path";
1144
1190
 
1145
- // packages/cli/dist/open-browser.js
1146
- import { spawn } from "node:child_process";
1147
- function run(cmd, args, launched = false) {
1148
- return new Promise((done) => {
1149
- const child = spawn(cmd, args, { stdio: "ignore", detached: true });
1150
- child.on("error", () => done(false));
1151
- if (launched)
1152
- child.on("spawn", () => done(true));
1153
- else
1154
- child.on("exit", (code) => done(code === 0));
1155
- child.unref();
1156
- });
1157
- }
1158
- async function openBrowserWindow(url) {
1159
- if (process.env.INKMAP_NO_OPEN === "1" || !/^http:\/\/(?:127\.0\.0\.1|localhost|\[::1\]):[0-9]+\//.test(url))
1160
- return;
1161
- if (process.platform === "darwin") {
1162
- if (await run("open", ["-a", "Google Chrome", url]))
1163
- return;
1164
- await run("open", [url]);
1165
- return;
1166
- }
1167
- if (process.platform === "win32") {
1168
- if (await run("cmd", ["/c", "start", "", "chrome", url]))
1169
- return;
1170
- await run("cmd", ["/c", "start", "", url]);
1171
- return;
1172
- }
1173
- if (await run("google-chrome", [url], true))
1174
- return;
1175
- await run("xdg-open", [url], true);
1176
- }
1191
+ // packages/cli/dist/image-assets.js
1192
+ import { mkdir as mkdir2, realpath as realpath2, writeFile as writeFile2 } from "node:fs/promises";
1193
+ import { join as join3 } from "node:path";
1177
1194
 
1178
1195
  // packages/cli/dist/workspace-fs.js
1179
1196
  import { lstat, readdir, readFile as readFile2, realpath } from "node:fs/promises";
1180
1197
  import { homedir as homedir2 } from "node:os";
1181
1198
  import { basename, delimiter, dirname as dirname2, isAbsolute, join as join2, posix, relative, resolve, sep, win32 } from "node:path";
1199
+ var say = (zh, en) => detectOsLocale() === "zh" ? zh : en;
1182
1200
  var MAX_MARKDOWN_BYTES = 5 * 1024 * 1024;
1183
1201
  var SKIP_SCAN_DIRS = /* @__PURE__ */ new Set([
1184
1202
  ".git",
@@ -1274,6 +1292,60 @@ async function scanMarkdownFiles(rootDir, { maxFiles = 200, maxDepth = 4, byRece
1274
1292
  return found;
1275
1293
  }
1276
1294
 
1295
+ // packages/cli/dist/image-assets.js
1296
+ async function saveImageAsset(documentDir, documentFileName, image, when = /* @__PURE__ */ new Date()) {
1297
+ const folder = imageFolderName(documentFileName);
1298
+ const target = join3(documentDir, folder);
1299
+ await mkdir2(target, { recursive: true });
1300
+ if (!insideOrEqual(await realpath2(documentDir), await realpath2(target))) {
1301
+ throw new Error(say("\u56FE\u7247\u76EE\u5F55\u6307\u5411\u6587\u6863\u76EE\u5F55\u4E4B\u5916\uFF0C\u672A\u4FDD\u5B58\u56FE\u7247\u3002", "The pictures folder leads outside the document's folder, so nothing was saved."));
1302
+ }
1303
+ for (let attempt = 1; attempt <= 100; attempt++) {
1304
+ const name2 = imageFileName(when, image.extension, attempt);
1305
+ try {
1306
+ await writeFile2(join3(target, name2), image.bytes, { flag: "wx" });
1307
+ return imageLinkPath(folder, name2);
1308
+ } catch (error) {
1309
+ if (error.code !== "EEXIST")
1310
+ throw error;
1311
+ }
1312
+ }
1313
+ throw new Error(say("\u65E0\u6CD5\u4E3A\u56FE\u7247\u751F\u6210\u65B0\u7684\u6587\u4EF6\u540D\u3002", "Could not find a free file name for the picture."));
1314
+ }
1315
+
1316
+ // packages/cli/dist/open-browser.js
1317
+ import { spawn } from "node:child_process";
1318
+ function run(cmd, args, launched = false) {
1319
+ return new Promise((done) => {
1320
+ const child = spawn(cmd, args, { stdio: "ignore", detached: true });
1321
+ child.on("error", () => done(false));
1322
+ if (launched)
1323
+ child.on("spawn", () => done(true));
1324
+ else
1325
+ child.on("exit", (code) => done(code === 0));
1326
+ child.unref();
1327
+ });
1328
+ }
1329
+ async function openBrowserWindow(url) {
1330
+ if (process.env.INKMAP_NO_OPEN === "1" || !/^http:\/\/(?:127\.0\.0\.1|localhost|\[::1\]):[0-9]+\//.test(url))
1331
+ return;
1332
+ if (process.platform === "darwin") {
1333
+ if (await run("open", ["-a", "Google Chrome", url]))
1334
+ return;
1335
+ await run("open", [url]);
1336
+ return;
1337
+ }
1338
+ if (process.platform === "win32") {
1339
+ if (await run("cmd", ["/c", "start", "", "chrome", url]))
1340
+ return;
1341
+ await run("cmd", ["/c", "start", "", url]);
1342
+ return;
1343
+ }
1344
+ if (await run("google-chrome", [url], true))
1345
+ return;
1346
+ await run("xdg-open", [url], true);
1347
+ }
1348
+
1277
1349
  // packages/cli/dist/server.js
1278
1350
  var MAX_BODY = 20 * 1024 * 1024;
1279
1351
  var TYPES = {
@@ -1307,8 +1379,8 @@ function docName(path) {
1307
1379
  return basename2(path).replace(/\.md$/i, "").replace(/\.markmap$/i, "");
1308
1380
  }
1309
1381
  function viewStateFile(key) {
1310
- const base = process.env.INKMAP_HOME || join3(homedir3(), ".inkmap");
1311
- return join3(base, "view-state", hash(key) + ".json");
1382
+ const base = process.env.INKMAP_HOME || join4(homedir3(), ".inkmap");
1383
+ return join4(base, "view-state", hash(key) + ".json");
1312
1384
  }
1313
1385
  async function readDiskViewState(key) {
1314
1386
  try {
@@ -1319,10 +1391,10 @@ async function readDiskViewState(key) {
1319
1391
  }
1320
1392
  async function writeDiskViewState(key, state) {
1321
1393
  const target = viewStateFile(key);
1322
- await mkdir2(dirname3(target), { recursive: true, mode: 448 });
1394
+ await mkdir3(dirname3(target), { recursive: true, mode: 448 });
1323
1395
  const temp = `${target}.${process.pid}.${Date.now()}`;
1324
1396
  try {
1325
- await writeFile2(temp, JSON.stringify(state), { mode: 384 });
1397
+ await writeFile3(temp, JSON.stringify(state), { mode: 384 });
1326
1398
  await rename2(temp, target);
1327
1399
  } finally {
1328
1400
  await rm2(temp, { force: true }).catch(() => {
@@ -1331,7 +1403,7 @@ async function writeDiskViewState(key, state) {
1331
1403
  }
1332
1404
  async function fileSource(file, log = () => {
1333
1405
  }) {
1334
- const path = await realpath2(resolve2(file)).catch(() => resolve2(file));
1406
+ const path = await realpath3(resolve2(file)).catch(() => resolve2(file));
1335
1407
  if (!(await stat(path)).isFile())
1336
1408
  throw new Error(`\u4E0D\u662F\u6587\u4EF6\uFF1A${path}`);
1337
1409
  const dir = dirname3(path);
@@ -1341,7 +1413,7 @@ async function fileSource(file, log = () => {
1341
1413
  dir,
1342
1414
  read: () => readFile3(path, "utf8"),
1343
1415
  async write(md) {
1344
- await writeFile2(path, md, "utf8");
1416
+ await writeFile3(path, md, "utf8");
1345
1417
  log(`\u5DF2\u5199\u56DE ${path}`);
1346
1418
  },
1347
1419
  watch(onChange) {
@@ -1371,7 +1443,7 @@ function within(root, rel, allowAbsoluteInRoot = false) {
1371
1443
  }
1372
1444
  const resolvedRoot = resolve2(root);
1373
1445
  const base = resolvedRoot.endsWith(sep2) ? resolvedRoot : resolvedRoot + sep2;
1374
- const full = allowAbsoluteInRoot && /^(?:[\\/]|[A-Za-z]:[\\/])/.test(decoded) ? normalize(decoded) : normalize(join3(resolvedRoot, decoded));
1446
+ const full = allowAbsoluteInRoot && /^(?:[\\/]|[A-Za-z]:[\\/])/.test(decoded) ? normalize(decoded) : normalize(join4(resolvedRoot, decoded));
1375
1447
  return full.startsWith(base) ? full : null;
1376
1448
  }
1377
1449
  async function serve({ source: initialSource, webRoot: webRoot2, port = 4377, host = "127.0.0.1", log = () => {
@@ -1429,17 +1501,17 @@ data: ${JSON.stringify(data)}
1429
1501
  }), 80);
1430
1502
  };
1431
1503
  let unwatch = activeSource.watch(scheduleRefresh);
1432
- const canonicalActiveKey = async () => activeSource.key ? await realpath2(activeSource.key).catch(() => activeSource.key) : void 0;
1504
+ const canonicalActiveKey = async () => activeSource.key ? await realpath3(activeSource.key).catch(() => activeSource.key) : void 0;
1433
1505
  async function collectWorkspaceFiles() {
1434
1506
  const roots = /* @__PURE__ */ new Map();
1435
1507
  if (activeSource.key && !confineToWorkspaceRoots) {
1436
- const realKey = await realpath2(activeSource.key).catch(() => activeSource.key);
1437
- const projRoot = await realpath2(await findProjectRoot(realKey)).catch(() => dirname3(realKey));
1508
+ const realKey = await realpath3(activeSource.key).catch(() => activeSource.key);
1509
+ const projRoot = await realpath3(await findProjectRoot(realKey)).catch(() => dirname3(realKey));
1438
1510
  roots.set(projRoot, { path: projRoot, name: basename2(projRoot), active: true });
1439
1511
  }
1440
1512
  const envRoots = [...workspaceRoots, ...(process.env.INKMAP_WORKSPACES || "").split(delimiter2)].filter(Boolean);
1441
1513
  for (const raw of envRoots) {
1442
- const real = await realpath2(resolve2(raw)).catch(() => null);
1514
+ const real = await realpath3(resolve2(raw)).catch(() => null);
1443
1515
  if (real && !roots.has(real)) {
1444
1516
  roots.set(real, { path: real, name: basename2(real), active: roots.size === 0 });
1445
1517
  }
@@ -1477,7 +1549,7 @@ data: ${JSON.stringify(data)}
1477
1549
  throw notMarkdown();
1478
1550
  const resolvedCandidate = resolve2(candidatePath);
1479
1551
  const rawStat = await lstat2(resolvedCandidate).catch(() => null);
1480
- const realTarget = await realpath2(resolvedCandidate).catch(() => null);
1552
+ const realTarget = await realpath3(resolvedCandidate).catch(() => null);
1481
1553
  const targetStat = realTarget ? await stat(realTarget).catch(() => null) : null;
1482
1554
  if (!rawStat || rawStat.isSymbolicLink() && !trustCaller || !realTarget || !targetStat?.isFile() || targetStat.size > MAX_MARKDOWN_BYTES) {
1483
1555
  throw new HttpError(403, zh() ? "\u8BF7\u9009\u62E9 5 MiB \u4EE5\u5185\u7684\u666E\u901A Markdown \u6587\u4EF6\u3002" : "Choose a regular Markdown file up to 5 MiB.");
@@ -1632,6 +1704,20 @@ data: ${JSON.stringify(data)}
1632
1704
  return json2(res, 200, { version });
1633
1705
  });
1634
1706
  }
1707
+ if (url.pathname === "/api/image" && req.method === "POST") {
1708
+ if (!sameOriginWrite())
1709
+ return json2(res, 403, { error: "forbidden" });
1710
+ const image = decodeImageDataUrl(JSON.parse(await readBody2(req)).dataUrl);
1711
+ if ("problem" in image) {
1712
+ return json2(res, image.problem === "size" ? 413 : 415, { error: image.problem });
1713
+ }
1714
+ if (!hasAccess(await license?.state() ?? { pro: false }))
1715
+ return json2(res, 403, { error: "Inkmap trial expired" });
1716
+ return await serialized(async () => {
1717
+ const name2 = activeSource.key ? basename2(activeSource.key) : activeSource.name;
1718
+ return json2(res, 200, { path: await saveImageAsset(activeSource.dir, name2, image) });
1719
+ });
1720
+ }
1635
1721
  if (url.pathname === "/api/license" && req.method === "GET")
1636
1722
  return json2(res, 200, await license?.state({ forUi: true }) ?? { pro: false });
1637
1723
  if (url.pathname === "/api/open" && req.method === "POST") {
@@ -1747,8 +1833,8 @@ data: ${JSON.stringify(data)}
1747
1833
  if (url.pathname.startsWith("/doc-assets/")) {
1748
1834
  let asset = within(activeSource.dir, url.pathname.slice("/doc-assets/".length), true);
1749
1835
  if (asset) {
1750
- const root = await realpath2(activeSource.dir).catch(() => null);
1751
- asset = await realpath2(asset).catch(() => null);
1836
+ const root = await realpath3(activeSource.dir).catch(() => null);
1837
+ asset = await realpath3(asset).catch(() => null);
1752
1838
  const allowedExt = sidebar ? /\.(?:png|jpe?g|gif|webp|avif|bmp|ico)$/i : /\.(?:png|jpe?g|gif|webp|avif|bmp|ico|svg)$/i;
1753
1839
  if (!root || !asset || !insideOrEqual(root, asset) || !allowedExt.test(asset))
1754
1840
  asset = null;
@@ -1842,7 +1928,7 @@ async function workspaceFile(ctx, address) {
1842
1928
  if (typeof hostPath !== "string") throw new FileUnavailable();
1843
1929
  const roundTrip = fs.processPathFromHostPath(hostPath);
1844
1930
  if (roundTrip === void 0 || (await fs.resolve(roundTrip)).targetKey !== target.targetKey) throw new FileUnavailable();
1845
- return realpath3(hostPath);
1931
+ return realpath4(hostPath);
1846
1932
  };
1847
1933
  const [root, path] = await Promise.all([local(header.cwd), local(file.path)]);
1848
1934
  const child = relative3(root, path);
@@ -1853,7 +1939,7 @@ async function workspaceFile(ctx, address) {
1853
1939
  }
1854
1940
  async function assertFile(path) {
1855
1941
  const info = await lstat3(path);
1856
- if (!info.isFile() || info.size > maxBytes || await realpath3(path) !== path) throw new FileUnavailable();
1942
+ if (!info.isFile() || info.size > maxBytes || await realpath4(path) !== path) throw new FileUnavailable();
1857
1943
  }
1858
1944
  async function confinedSource(path) {
1859
1945
  const source = await fileSource(path);
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-inkmap",
3
- "version": "0.3.18",
3
+ "version": "0.3.19",
4
4
  "description": "Inkmap 思维导图 — 在 DeepSeek Harness 中将本地 Markdown 实时渲染为可双向编辑的思维导图 (Markmap) 与 Mermaid 流程图 / Interactive two-way Markdown mind map & Mermaid flowchart editor for DeepSeek Harness.",
5
5
  "type": "module",
6
6
  "icon": "./icon.svg",