obsidian-sync-mcp 0.9.0 → 0.11.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/README.md +31 -4
- package/dist/{chunk-VR63UUCB.js → chunk-XZ4HUASL.js} +150 -47
- package/dist/chunk-XZ4HUASL.js.map +1 -0
- package/dist/main.js +568 -18
- package/dist/main.js.map +1 -1
- package/dist/{vault-6H64Y46A.js → vault-7FFBJ5QT.js} +168 -5
- package/dist/vault-7FFBJ5QT.js.map +1 -0
- package/dist/vault-local-MF65KDVB.js +309 -0
- package/dist/vault-local-MF65KDVB.js.map +1 -0
- package/package.json +2 -2
- package/dist/chunk-VR63UUCB.js.map +0 -1
- package/dist/vault-6H64Y46A.js.map +0 -1
- package/dist/vault-local-OURGENR5.js +0 -154
- package/dist/vault-local-OURGENR5.js.map +0 -1
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Node polyfills for livesync-commonlib browser globals
|
|
3
|
+
if(!("navigator" in globalThis)){globalThis.navigator={language:"en"};}
|
|
4
|
+
import {
|
|
5
|
+
AttachmentExistsError,
|
|
6
|
+
AttachmentMoveIncompleteError,
|
|
7
|
+
isValidAttachmentPath,
|
|
8
|
+
isValidNotePath,
|
|
9
|
+
parseFrontmatterAndLinks,
|
|
10
|
+
validateAttachmentPath,
|
|
11
|
+
validateNotePath
|
|
12
|
+
} from "./chunk-XZ4HUASL.js";
|
|
13
|
+
|
|
14
|
+
// src/vault-local.ts
|
|
15
|
+
import { readFile, writeFile, unlink, mkdir, stat, lstat, realpath, rename, link, copyFile } from "fs/promises";
|
|
16
|
+
import { basename, dirname, join, relative, resolve, sep } from "path";
|
|
17
|
+
import { realpathSync, constants } from "fs";
|
|
18
|
+
import { glob } from "fs/promises";
|
|
19
|
+
var NOFOLLOW = constants.O_NOFOLLOW ?? 0;
|
|
20
|
+
var LocalVault = class {
|
|
21
|
+
root;
|
|
22
|
+
constructor(vaultPath) {
|
|
23
|
+
this.root = realpathSync(resolve(vaultPath));
|
|
24
|
+
}
|
|
25
|
+
async safePath(path, kind = "folder", { noSymlink = false } = {}) {
|
|
26
|
+
if (kind === "note") validateNotePath(path);
|
|
27
|
+
if (kind === "attachment") validateAttachmentPath(path);
|
|
28
|
+
const full = resolve(this.root, path);
|
|
29
|
+
if (!full.startsWith(this.root + sep)) {
|
|
30
|
+
throw new Error("Path traversal blocked");
|
|
31
|
+
}
|
|
32
|
+
const real = await this.resolveExisting(full);
|
|
33
|
+
if (!real.startsWith(this.root + sep)) {
|
|
34
|
+
throw new Error("Path traversal blocked");
|
|
35
|
+
}
|
|
36
|
+
if (kind !== "folder" && !this.isValidKind(relative(this.root, real), kind)) {
|
|
37
|
+
throw new Error("Path traversal blocked");
|
|
38
|
+
}
|
|
39
|
+
if (await this.hasDanglingSymlink(full)) {
|
|
40
|
+
throw new Error("Path traversal blocked");
|
|
41
|
+
}
|
|
42
|
+
if (noSymlink && await this.isSymlink(full)) {
|
|
43
|
+
throw new Error(`Refusing to modify a symlink: ${path}`);
|
|
44
|
+
}
|
|
45
|
+
return real;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* mkdir for an attachment write to `fullPath` (from safePath), then check
|
|
49
|
+
* the path again: a folder swapped for a symlink since would redirect the
|
|
50
|
+
* write. Returns false when mkdir fails.
|
|
51
|
+
*/
|
|
52
|
+
async mkdirChecked(path, fullPath) {
|
|
53
|
+
try {
|
|
54
|
+
await mkdir(dirname(fullPath), { recursive: true });
|
|
55
|
+
} catch {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
if (await this.safePath(path, "attachment", { noSymlink: true }) !== fullPath) {
|
|
59
|
+
throw new Error("Path traversal blocked");
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
isValidKind(rel, kind) {
|
|
64
|
+
const path = rel.split(sep).join("/");
|
|
65
|
+
return kind === "note" ? isValidNotePath(path) : isValidAttachmentPath(path);
|
|
66
|
+
}
|
|
67
|
+
async isSymlink(full) {
|
|
68
|
+
try {
|
|
69
|
+
return (await lstat(full)).isSymbolicLink();
|
|
70
|
+
} catch (e) {
|
|
71
|
+
if (e.code === "ENOENT") return false;
|
|
72
|
+
throw e;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* realpath of `full`, or for a path that doesn't exist yet the realpath of
|
|
77
|
+
* its nearest existing ancestor joined with the rest.
|
|
78
|
+
*/
|
|
79
|
+
async resolveExisting(full) {
|
|
80
|
+
const rest = [];
|
|
81
|
+
let current = full;
|
|
82
|
+
for (; ; ) {
|
|
83
|
+
try {
|
|
84
|
+
return join(await realpath(current), ...rest);
|
|
85
|
+
} catch (e) {
|
|
86
|
+
if (e.code !== "ENOENT") throw e;
|
|
87
|
+
const parent = dirname(current);
|
|
88
|
+
if (parent === current) return full;
|
|
89
|
+
rest.unshift(basename(current));
|
|
90
|
+
current = parent;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** Whether `full` or one of its ancestors below the root is a symlink to nothing. */
|
|
95
|
+
async hasDanglingSymlink(full) {
|
|
96
|
+
for (let current = full; current !== this.root; current = dirname(current)) {
|
|
97
|
+
let s;
|
|
98
|
+
try {
|
|
99
|
+
s = await lstat(current);
|
|
100
|
+
} catch (e) {
|
|
101
|
+
if (e.code === "ENOENT") continue;
|
|
102
|
+
throw e;
|
|
103
|
+
}
|
|
104
|
+
if (!s.isSymbolicLink()) return false;
|
|
105
|
+
try {
|
|
106
|
+
await realpath(current);
|
|
107
|
+
return false;
|
|
108
|
+
} catch (e) {
|
|
109
|
+
if (e.code === "ENOENT") return true;
|
|
110
|
+
throw e;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
async init() {
|
|
116
|
+
}
|
|
117
|
+
async close() {
|
|
118
|
+
}
|
|
119
|
+
async readNote(path) {
|
|
120
|
+
const fullPath = await this.safePath(path, "note");
|
|
121
|
+
try {
|
|
122
|
+
return await readFile(fullPath, "utf-8");
|
|
123
|
+
} catch {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async writeNote(path, content) {
|
|
128
|
+
const fullPath = await this.safePath(path, "note");
|
|
129
|
+
try {
|
|
130
|
+
await mkdir(dirname(fullPath), { recursive: true });
|
|
131
|
+
await writeFile(fullPath, content, "utf-8");
|
|
132
|
+
return true;
|
|
133
|
+
} catch {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
async deleteNote(path) {
|
|
138
|
+
const fullPath = await this.safePath(path, "note");
|
|
139
|
+
try {
|
|
140
|
+
await unlink(fullPath);
|
|
141
|
+
return true;
|
|
142
|
+
} catch {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async moveNote(from, to) {
|
|
147
|
+
const fromPath = await this.safePath(from, "note");
|
|
148
|
+
const toPath = await this.safePath(to, "note");
|
|
149
|
+
if (fromPath === toPath) {
|
|
150
|
+
try {
|
|
151
|
+
await rename(fromPath, resolve(this.root, to));
|
|
152
|
+
return true;
|
|
153
|
+
} catch {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
await mkdir(dirname(toPath), { recursive: true });
|
|
159
|
+
await rename(fromPath, toPath);
|
|
160
|
+
return true;
|
|
161
|
+
} catch (e) {
|
|
162
|
+
if (e.code === "EXDEV") {
|
|
163
|
+
const content = await this.readNote(from);
|
|
164
|
+
if (content === null) return false;
|
|
165
|
+
const wrote = await this.writeNote(to, content);
|
|
166
|
+
if (!wrote) return false;
|
|
167
|
+
return await this.deleteNote(from);
|
|
168
|
+
}
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async getMetadata(path) {
|
|
173
|
+
const fullPath = await this.safePath(path, "note");
|
|
174
|
+
try {
|
|
175
|
+
const [content, s] = await Promise.all([
|
|
176
|
+
readFile(fullPath, "utf-8"),
|
|
177
|
+
stat(fullPath)
|
|
178
|
+
]);
|
|
179
|
+
return {
|
|
180
|
+
path,
|
|
181
|
+
size: s.size,
|
|
182
|
+
ctime: s.birthtimeMs,
|
|
183
|
+
mtime: s.mtimeMs,
|
|
184
|
+
...parseFrontmatterAndLinks(content)
|
|
185
|
+
};
|
|
186
|
+
} catch {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
async listNotes(folder) {
|
|
191
|
+
const notes = await this.listNotesWithMtime(folder);
|
|
192
|
+
return notes.map((n) => n.path);
|
|
193
|
+
}
|
|
194
|
+
async listNotesWithMtime(folder) {
|
|
195
|
+
if (folder && !folder.endsWith("/") && !folder.endsWith("\\")) folder += "/";
|
|
196
|
+
const searchDir = folder ? await this.safePath(folder) : this.root;
|
|
197
|
+
const entries = [];
|
|
198
|
+
try {
|
|
199
|
+
for await (const entry of glob("**/*.md", { cwd: searchDir })) {
|
|
200
|
+
const full = folder ? `${folder}${entry}` : entry;
|
|
201
|
+
if (!isValidNotePath(full)) continue;
|
|
202
|
+
entries.push(full);
|
|
203
|
+
}
|
|
204
|
+
} catch {
|
|
205
|
+
return [];
|
|
206
|
+
}
|
|
207
|
+
const results = await Promise.all(
|
|
208
|
+
entries.map(async (p) => {
|
|
209
|
+
try {
|
|
210
|
+
const s = await stat(resolve(this.root, p));
|
|
211
|
+
return { path: p, mtime: s.mtimeMs };
|
|
212
|
+
} catch {
|
|
213
|
+
return { path: p, mtime: 0 };
|
|
214
|
+
}
|
|
215
|
+
})
|
|
216
|
+
);
|
|
217
|
+
return results.sort((a, b) => a.path.localeCompare(b.path));
|
|
218
|
+
}
|
|
219
|
+
async writeAttachment(path, data, { overwrite = false } = {}) {
|
|
220
|
+
const fullPath = await this.safePath(path, "attachment", { noSymlink: true });
|
|
221
|
+
if (!await this.mkdirChecked(path, fullPath)) return false;
|
|
222
|
+
try {
|
|
223
|
+
const flag = constants.O_WRONLY | constants.O_CREAT | (overwrite ? constants.O_TRUNC : constants.O_EXCL) | NOFOLLOW;
|
|
224
|
+
await writeFile(fullPath, data, { flag });
|
|
225
|
+
return true;
|
|
226
|
+
} catch (e) {
|
|
227
|
+
if (e.code === "EEXIST") throw new AttachmentExistsError(path);
|
|
228
|
+
if (e.code === "ELOOP") throw new Error(`Refusing to modify a symlink: ${path}`);
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
async deleteAttachment(path) {
|
|
233
|
+
const fullPath = await this.safePath(path, "attachment", { noSymlink: true });
|
|
234
|
+
try {
|
|
235
|
+
await unlink(fullPath);
|
|
236
|
+
return true;
|
|
237
|
+
} catch {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
async moveAttachment(from, to, { overwrite = false } = {}) {
|
|
242
|
+
const fromPath = await this.safePath(from, "attachment", { noSymlink: true });
|
|
243
|
+
const toPath = await this.safePath(to, "attachment", { noSymlink: true });
|
|
244
|
+
if (fromPath === toPath) {
|
|
245
|
+
try {
|
|
246
|
+
await rename(fromPath, resolve(this.root, to));
|
|
247
|
+
return true;
|
|
248
|
+
} catch {
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (!await this.mkdirChecked(to, toPath)) return false;
|
|
253
|
+
try {
|
|
254
|
+
if (overwrite) {
|
|
255
|
+
await rename(fromPath, toPath);
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
await link(fromPath, toPath);
|
|
259
|
+
} catch (e) {
|
|
260
|
+
if (e.code === "EEXIST") throw new AttachmentExistsError(to);
|
|
261
|
+
if (!["EXDEV", "EPERM", "ENOTSUP", "EOPNOTSUPP"].includes(e.code)) return false;
|
|
262
|
+
try {
|
|
263
|
+
await copyFile(fromPath, toPath, overwrite ? 0 : constants.COPYFILE_EXCL);
|
|
264
|
+
} catch (copyErr) {
|
|
265
|
+
if (copyErr.code === "EEXIST") throw new AttachmentExistsError(to);
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
try {
|
|
270
|
+
await unlink(fromPath);
|
|
271
|
+
return true;
|
|
272
|
+
} catch {
|
|
273
|
+
throw new AttachmentMoveIncompleteError(from, to);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
async getAttachmentInfo(path) {
|
|
277
|
+
const fullPath = await this.safePath(path, "attachment");
|
|
278
|
+
try {
|
|
279
|
+
const s = await stat(fullPath);
|
|
280
|
+
if (!s.isFile()) return null;
|
|
281
|
+
return { path, size: s.size, ctime: s.birthtimeMs, mtime: s.mtimeMs };
|
|
282
|
+
} catch {
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
async listAttachments() {
|
|
287
|
+
const results = [];
|
|
288
|
+
try {
|
|
289
|
+
for await (const entry of glob("**/*", { cwd: this.root, exclude: (p) => String(typeof p === "string" ? p : p.name).split(/[/\\]/).some((seg) => seg.startsWith(".")) })) {
|
|
290
|
+
const path = entry.replace(/\\/g, "/");
|
|
291
|
+
if (!isValidAttachmentPath(path)) continue;
|
|
292
|
+
try {
|
|
293
|
+
const real = await realpath(resolve(this.root, path));
|
|
294
|
+
if (!real.startsWith(this.root + sep) || !this.isValidKind(relative(this.root, real), "attachment")) continue;
|
|
295
|
+
const s = await stat(real);
|
|
296
|
+
if (s.isFile()) results.push({ path, size: s.size, ctime: s.birthtimeMs, mtime: s.mtimeMs });
|
|
297
|
+
} catch {
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
} catch {
|
|
301
|
+
return [];
|
|
302
|
+
}
|
|
303
|
+
return results.sort((a, b) => a.path.localeCompare(b.path));
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
export {
|
|
307
|
+
LocalVault
|
|
308
|
+
};
|
|
309
|
+
//# sourceMappingURL=vault-local-MF65KDVB.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/vault-local.ts"],"sourcesContent":["import { readFile, writeFile, unlink, mkdir, stat, lstat, realpath, rename, link, copyFile } from \"fs/promises\";\nimport { basename, dirname, join, relative, resolve, sep } from \"path\";\nimport { realpathSync, constants } from \"fs\";\nimport { glob } from \"fs/promises\";\nimport { parseFrontmatterAndLinks } from \"./parse.js\";\nimport { validateNotePath, isValidNotePath } from \"./note-path.js\";\nimport { validateAttachmentPath, isValidAttachmentPath } from \"./attachment-path.js\";\nimport { AttachmentExistsError, AttachmentMoveIncompleteError, type VaultBackend, type NoteInfo, type NoteListing, type AttachmentInfo, type WriteOptions, type MoveOptions } from \"./vault-backend.js\";\n\n// Not defined on Windows, which has no symlinks a non-admin can create anyway.\nconst NOFOLLOW = constants.O_NOFOLLOW ?? 0;\n\nexport class LocalVault implements VaultBackend {\n private root: string;\n\n constructor(vaultPath: string) {\n this.root = realpathSync(resolve(vaultPath));\n }\n\n private async safePath(path: string, kind: \"note\" | \"attachment\" | \"folder\" = \"folder\", { noSymlink = false } = {}): Promise<string> {\n // Note operations must target a real note (.md, no dot-folders, etc.)\n // and attachment operations an allowed media file; folder listing\n // passes a directory and skips this.\n if (kind === \"note\") validateNotePath(path);\n if (kind === \"attachment\") validateAttachmentPath(path);\n const full = resolve(this.root, path);\n // Lexical check first (catches ../ without hitting disk)\n if (!full.startsWith(this.root + sep)) {\n throw new Error(\"Path traversal blocked\");\n }\n // Resolve symlinks and re-check (catches symlink escapes). A path\n // that doesn't exist yet resolves through its nearest existing\n // ancestor, so a symlinked parent can't redirect a write either.\n const real = await this.resolveExisting(full);\n if (!real.startsWith(this.root + sep)) {\n throw new Error(\"Path traversal blocked\");\n }\n // The name was validated, but a symlink inside the vault can point at\n // a note, a dot-folder or another type, so the target must pass too.\n if (kind !== \"folder\" && !this.isValidKind(relative(this.root, real), kind)) {\n throw new Error(\"Path traversal blocked\");\n }\n // realpath fails with ENOENT on a dangling symlink, so it resolves as\n // a new file, but a write would follow it and create outside the vault.\n if (await this.hasDanglingSymlink(full)) {\n throw new Error(\"Path traversal blocked\");\n }\n // Writing or deleting through a symlink would act on its target, a\n // different path from the one the caller named.\n if (noSymlink && (await this.isSymlink(full))) {\n throw new Error(`Refusing to modify a symlink: ${path}`);\n }\n return real;\n }\n\n /**\n * mkdir for an attachment write to `fullPath` (from safePath), then check\n * the path again: a folder swapped for a symlink since would redirect the\n * write. Returns false when mkdir fails.\n */\n private async mkdirChecked(path: string, fullPath: string): Promise<boolean> {\n try {\n await mkdir(dirname(fullPath), { recursive: true });\n } catch {\n return false;\n }\n if ((await this.safePath(path, \"attachment\", { noSymlink: true })) !== fullPath) {\n throw new Error(\"Path traversal blocked\");\n }\n return true;\n }\n\n private isValidKind(rel: string, kind: \"note\" | \"attachment\"): boolean {\n const path = rel.split(sep).join(\"/\");\n return kind === \"note\" ? isValidNotePath(path) : isValidAttachmentPath(path);\n }\n\n private async isSymlink(full: string): Promise<boolean> {\n try {\n return (await lstat(full)).isSymbolicLink();\n } catch (e: any) {\n if (e.code === \"ENOENT\") return false;\n throw e;\n }\n }\n\n /**\n * realpath of `full`, or for a path that doesn't exist yet the realpath of\n * its nearest existing ancestor joined with the rest.\n */\n private async resolveExisting(full: string): Promise<string> {\n const rest: string[] = [];\n let current = full;\n for (;;) {\n try {\n return join(await realpath(current), ...rest);\n } catch (e: any) {\n if (e.code !== \"ENOENT\") throw e;\n const parent = dirname(current);\n if (parent === current) return full; // reached filesystem root\n rest.unshift(basename(current));\n current = parent;\n }\n }\n }\n\n /** Whether `full` or one of its ancestors below the root is a symlink to nothing. */\n private async hasDanglingSymlink(full: string): Promise<boolean> {\n for (let current = full; current !== this.root; current = dirname(current)) {\n let s;\n try {\n s = await lstat(current);\n } catch (e: any) {\n if (e.code === \"ENOENT\") continue;\n throw e;\n }\n // The nearest existing entry decides: anything but a symlink is\n // real, and a resolvable symlink was checked by resolveExisting.\n if (!s.isSymbolicLink()) return false;\n try {\n await realpath(current);\n return false;\n } catch (e: any) {\n if (e.code === \"ENOENT\") return true;\n throw e;\n }\n }\n return false;\n }\n\n async init(): Promise<void> {}\n\n async close(): Promise<void> {}\n\n async readNote(path: string): Promise<string | null> {\n const fullPath = await this.safePath(path, \"note\");\n try {\n return await readFile(fullPath, \"utf-8\");\n } catch {\n return null;\n }\n }\n\n async writeNote(path: string, content: string): Promise<boolean> {\n const fullPath = await this.safePath(path, \"note\");\n try {\n await mkdir(dirname(fullPath), { recursive: true });\n await writeFile(fullPath, content, \"utf-8\");\n return true;\n } catch {\n return false;\n }\n }\n\n async deleteNote(path: string): Promise<boolean> {\n const fullPath = await this.safePath(path, \"note\");\n try {\n await unlink(fullPath);\n return true;\n } catch {\n return false;\n }\n }\n\n async moveNote(from: string, to: string): Promise<boolean> {\n const fromPath = await this.safePath(from, \"note\");\n const toPath = await this.safePath(to, \"note\");\n if (fromPath === toPath) {\n // Same file: a no-op, or a case-only rename on a case-insensitive\n // filesystem, where toPath is the realpath and has the old case.\n try {\n await rename(fromPath, resolve(this.root, to));\n return true;\n } catch {\n return false;\n }\n }\n try {\n await mkdir(dirname(toPath), { recursive: true });\n await rename(fromPath, toPath);\n return true;\n } catch (e: any) {\n if (e.code === \"EXDEV\") {\n // Cross-device: fall back to copy-delete\n const content = await this.readNote(from);\n if (content === null) return false;\n const wrote = await this.writeNote(to, content);\n if (!wrote) return false;\n return await this.deleteNote(from);\n }\n return false;\n }\n }\n\n async getMetadata(path: string): Promise<NoteInfo | null> {\n const fullPath = await this.safePath(path, \"note\");\n try {\n const [content, s] = await Promise.all([\n readFile(fullPath, \"utf-8\"),\n stat(fullPath),\n ]);\n return {\n path,\n size: s.size,\n ctime: s.birthtimeMs,\n mtime: s.mtimeMs,\n ...parseFrontmatterAndLinks(content),\n };\n } catch {\n return null;\n }\n }\n\n async listNotes(folder?: string): Promise<string[]> {\n const notes = await this.listNotesWithMtime(folder);\n return notes.map((n) => n.path);\n }\n\n async listNotesWithMtime(folder?: string): Promise<NoteListing[]> {\n if (folder && !folder.endsWith(\"/\") && !folder.endsWith(\"\\\\\")) folder += \"/\";\n const searchDir = folder ? await this.safePath(folder) : this.root;\n const entries: string[] = [];\n try {\n for await (const entry of glob(\"**/*.md\", { cwd: searchDir })) {\n const full = folder ? `${folder}${entry}` : entry;\n if (!isValidNotePath(full)) continue;\n entries.push(full);\n }\n } catch {\n return [];\n }\n const results = await Promise.all(\n entries.map(async (p) => {\n try {\n const s = await stat(resolve(this.root, p));\n return { path: p, mtime: s.mtimeMs };\n } catch {\n return { path: p, mtime: 0 };\n }\n }),\n );\n return results.sort((a, b) => a.path.localeCompare(b.path));\n }\n\n async writeAttachment(path: string, data: Uint8Array, { overwrite = false }: WriteOptions = {}): Promise<boolean> {\n const fullPath = await this.safePath(path, \"attachment\", { noSymlink: true });\n if (!(await this.mkdirChecked(path, fullPath))) return false;\n try {\n // O_EXCL fails if the file exists, in the same step as the create,\n // so two concurrent creates can't both succeed. O_NOFOLLOW refuses\n // a symlink put in place since safePath checked.\n const flag = constants.O_WRONLY | constants.O_CREAT | (overwrite ? constants.O_TRUNC : constants.O_EXCL) | NOFOLLOW;\n await writeFile(fullPath, data, { flag });\n return true;\n } catch (e: any) {\n if (e.code === \"EEXIST\") throw new AttachmentExistsError(path);\n if (e.code === \"ELOOP\") throw new Error(`Refusing to modify a symlink: ${path}`);\n return false;\n }\n }\n\n async deleteAttachment(path: string): Promise<boolean> {\n const fullPath = await this.safePath(path, \"attachment\", { noSymlink: true });\n try {\n await unlink(fullPath);\n return true;\n } catch {\n return false;\n }\n }\n\n async moveAttachment(from: string, to: string, { overwrite = false }: MoveOptions = {}): Promise<boolean> {\n const fromPath = await this.safePath(from, \"attachment\", { noSymlink: true });\n const toPath = await this.safePath(to, \"attachment\", { noSymlink: true });\n if (fromPath === toPath) {\n // Same file, which link below would refuse with EEXIST. rename is a\n // no-op here, or the case-only rename on a case-insensitive filesystem.\n try {\n await rename(fromPath, resolve(this.root, to));\n return true;\n } catch {\n return false;\n }\n }\n if (!(await this.mkdirChecked(to, toPath))) return false;\n try {\n if (overwrite) {\n await rename(fromPath, toPath);\n return true;\n }\n // rename would replace an existing destination; link fails with\n // EEXIST instead.\n await link(fromPath, toPath);\n } catch (e: any) {\n if (e.code === \"EEXIST\") throw new AttachmentExistsError(to);\n // Cross-device, or a filesystem without hard links: copy, then delete.\n if (![\"EXDEV\", \"EPERM\", \"ENOTSUP\", \"EOPNOTSUPP\"].includes(e.code)) return false;\n try {\n await copyFile(fromPath, toPath, overwrite ? 0 : constants.COPYFILE_EXCL);\n } catch (copyErr: any) {\n if (copyErr.code === \"EEXIST\") throw new AttachmentExistsError(to);\n return false;\n }\n }\n // The destination now exists, by link or copy; only the source is left.\n try {\n await unlink(fromPath);\n return true;\n } catch {\n throw new AttachmentMoveIncompleteError(from, to);\n }\n }\n\n async getAttachmentInfo(path: string): Promise<AttachmentInfo | null> {\n const fullPath = await this.safePath(path, \"attachment\");\n try {\n const s = await stat(fullPath);\n if (!s.isFile()) return null;\n return { path, size: s.size, ctime: s.birthtimeMs, mtime: s.mtimeMs };\n } catch {\n return null;\n }\n }\n\n async listAttachments(): Promise<AttachmentInfo[]> {\n const results: AttachmentInfo[] = [];\n try {\n // Dot-folders (.obsidian, .trash, .git) are never attachments; skip them while walking.\n for await (const entry of glob(\"**/*\", { cwd: this.root, exclude: (p: any) => String(typeof p === \"string\" ? p : p.name).split(/[/\\\\]/).some((seg: string) => seg.startsWith(\".\")) })) {\n const path = entry.replace(/\\\\/g, \"/\");\n if (!isValidAttachmentPath(path)) continue;\n try {\n // A symlink out of the vault would leak the target's name and\n // size, and one to a note or dot-folder isn't an attachment.\n const real = await realpath(resolve(this.root, path));\n if (!real.startsWith(this.root + sep) || !this.isValidKind(relative(this.root, real), \"attachment\")) continue;\n const s = await stat(real);\n if (s.isFile()) results.push({ path, size: s.size, ctime: s.birthtimeMs, mtime: s.mtimeMs });\n } catch {\n // Vanished between listing and stat, or a dangling symlink.\n }\n }\n } catch {\n return [];\n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|
|
4
|
+
"version": "0.11.0",
|
|
5
5
|
"description": "Give any AI agent access to your Obsidian vault over MCP",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"pouchdb-utils": "^9.0.0",
|
|
54
54
|
"transform-pouch": "^2.0.0",
|
|
55
55
|
"xxhash-wasm": "^1.0.2",
|
|
56
|
-
"zod": "^
|
|
56
|
+
"zod": "^4.6.7"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@types/node": "^22",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/parse.ts","../src/note-path.ts"],"sourcesContent":["/**\n * Parse Obsidian markdown content for frontmatter, tags, and links.\n */\n\nexport interface NoteMetadata {\n frontmatter: Record<string, string>;\n tags: string[];\n links: string[];\n}\n\nexport function parseFrontmatterAndLinks(content: string): NoteMetadata {\n const frontmatter: Record<string, any> = {};\n const tags = new Set<string>();\n const links: string[] = [];\n\n // Parse YAML frontmatter\n if (content.startsWith(\"---\\n\")) {\n const end = content.indexOf(\"\\n---\", 4);\n if (end !== -1) {\n const yaml = content.slice(4, end);\n let inTagsList = false;\n for (const line of yaml.split(\"\\n\")) {\n // Multi-line tags list item: \" - tagname\"\n if (inTagsList) {\n const listItem = line.match(/^\\s+-\\s+(.+)/);\n if (listItem) {\n const trimmed = listItem[1].trim();\n if (trimmed) tags.add(trimmed);\n continue;\n }\n inTagsList = false;\n }\n\n const match = line.match(/^([\\p{L}\\p{N}_][\\p{L}\\p{M}\\p{N}_-]*)\\s*:\\s*(.+)/u);\n if (match) {\n frontmatter[match[1]] = match[2].trim();\n }\n // Frontmatter tags (inline: [a, b] or start of multi-line list)\n if (/^tags\\s*:/.test(line)) {\n const value = line.replace(/^tags\\s*:\\s*/, \"\").trim();\n if (value) {\n // Inline: tags: [a, b] or tags: a, b\n const tagValues = value.replace(/[[\\]]/g, \"\");\n for (const t of tagValues.split(\",\")) {\n const trimmed = t.trim();\n if (trimmed) tags.add(trimmed);\n }\n } else {\n // Multi-line list follows\n inTagsList = true;\n }\n }\n }\n }\n }\n\n // Inline #tags. Obsidian does not read tags out of code, and a tag must\n // contain at least one non-numerical character (\"#1984 isn't a valid tag,\n // but #y1984 is\" — obsidian.md/help/tags), so code spans and fenced blocks\n // are masked out first and all-digit matches are dropped.\n for (const match of maskCode(content).matchAll(/(^|\\s)#([\\p{L}\\p{N}_/-][\\p{L}\\p{M}\\p{N}_/-]*)/gu)) {\n if (/^\\p{Nd}+$/u.test(match[2])) continue;\n tags.add(match[2]);\n }\n\n // [[wikilinks]]\n for (const match of content.matchAll(/\\[\\[([^\\]|]+)(?:\\|[^\\]]+)?\\]\\]/g)) {\n links.push(match[1]);\n }\n\n // [markdown links](path.md)\n for (const match of content.matchAll(/\\[([^\\]]+)\\]\\(([^)]+\\.md)\\)/g)) {\n links.push(match[2]);\n }\n\n return { frontmatter, tags: [...tags], links: [...new Set(links)] };\n}\n\n/**\n * Replace fenced code blocks (``` or ~~~, opener at line start with up to three\n * spaces of indent, closer of the same character and at least the same length;\n * an unclosed fence runs to the end) and inline code spans (a backtick run of\n * length n closes at the next run of exactly n within the same paragraph — a\n * span never crosses a blank line; an unmatched run is literal)\n * with dots, so offsets are preserved and nothing inside can start or extend a\n * tag. Indented code blocks, %% comments and math are not masked.\n */\nexport function maskCode(content: string): string {\n const lines = content.split(\"\\n\");\n const out: string[] = [];\n let fence: { char: string; len: number } | null = null;\n const inline: string[] = [];\n const flushInline = () => {\n if (inline.length === 0) return;\n out.push(...maskInlineSpans(inline.join(\"\\n\")).split(\"\\n\"));\n inline.length = 0;\n };\n for (const line of lines) {\n const open = line.match(/^ {0,3}(`{3,}|~{3,})/);\n if (fence) {\n const close = open && open[1][0] === fence.char && open[1].length >= fence.len && line.trim() === open[1];\n out.push(\".\".repeat(line.length));\n if (close) fence = null;\n continue;\n }\n if (open && (open[1][0] === \"~\" || !line.slice(open[0].length).includes(\"`\"))) {\n flushInline();\n fence = { char: open[1][0], len: open[1].length };\n out.push(\".\".repeat(line.length));\n continue;\n }\n if (line.trim() === \"\") {\n flushInline();\n out.push(line);\n continue;\n }\n inline.push(line);\n }\n flushInline();\n return out.join(\"\\n\");\n}\n\nfunction maskInlineSpans(text: string): string {\n // Collect backtick runs in one pass, then link each run to the next run of\n // the same length (scanning right to left) so matching stays linear.\n const runs: { at: number; len: number }[] = [];\n for (let i = 0; i < text.length; ) {\n if (text[i] !== \"`\") { i++; continue; }\n let n = 0;\n while (text[i + n] === \"`\") n++;\n runs.push({ at: i, len: n });\n i += n;\n }\n const next: number[] = [];\n const seen = new Map<number, number>();\n for (let r = runs.length - 1; r >= 0; r--) {\n next[r] = seen.get(runs[r].len) ?? -1;\n seen.set(runs[r].len, r);\n }\n let result = \"\";\n let from = 0;\n for (let r = 0; r < runs.length; ) {\n const c = next[r];\n if (c === -1) { r++; continue; }\n const start = runs[r].at;\n const end = runs[c].at + runs[c].len;\n result += text.slice(from, start) + text.slice(start, end).replace(/[^\\n]/g, \".\");\n from = end;\n r = c + 1;\n }\n return result + text.slice(from);\n}\n\nexport function extractSnippet(content: string, query: string, context = 80): string {\n const lower = content.toLowerCase();\n\n // Try exact phrase first\n let idx = lower.indexOf(query.toLowerCase());\n\n // Try to find the smallest span containing all query words\n if (idx === -1) {\n const words = query.split(/\\s+/).filter((w) => w.length >= 3).map((w) => w.toLowerCase());\n if (words.length > 1) {\n let bestStart = -1;\n let bestLen = Infinity;\n // For each occurrence of the first word, find the nearest span containing all words\n const first = words[0];\n let pos = 0;\n while (pos < lower.length) {\n const start = lower.indexOf(first, pos);\n if (start === -1) break;\n // Find last position needed to include all words from this start\n let spanEnd = start + first.length;\n let allFound = true;\n for (let i = 1; i < words.length; i++) {\n const wi = lower.indexOf(words[i], Math.max(0, start - 200));\n if (wi === -1) { allFound = false; break; }\n spanEnd = Math.max(spanEnd, wi + words[i].length);\n }\n if (allFound) {\n const spanStart = Math.min(start, ...words.map((w) => lower.indexOf(w, Math.max(0, start - 200))).filter((i) => i >= 0));\n const len = spanEnd - spanStart;\n if (len < bestLen) { bestStart = spanStart; bestLen = len; }\n }\n pos = start + 1;\n }\n if (bestStart >= 0 && bestLen <= 500) idx = bestStart;\n }\n }\n\n // Fall back to longest matching word\n if (idx === -1) {\n const words = query.split(/\\s+/).filter((w) => w.length >= 3).sort((a, b) => b.length - a.length);\n for (const word of words) {\n idx = lower.indexOf(word.toLowerCase());\n if (idx !== -1) break;\n }\n }\n\n if (idx === -1) {\n return content.slice(0, 160) + (content.length > 160 ? \"...\" : \"\");\n }\n const start = Math.max(0, idx - context);\n const end = Math.min(content.length, idx + query.length + context);\n return (start > 0 ? \"...\" : \"\") + content.slice(start, end) + (end < content.length ? \"...\" : \"\");\n}\n","/**\n * Shared validation for note paths, applied by both the CouchDB and local\n * backends before any read, write, delete or move.\n *\n * The note model is markdown-only: `list_notes` only ever surfaces `*.md`\n * files, and binary attachments are not supported. Enforcing that here keeps\n * the tools from reaching anything that isn't a note, which closes two real\n * problems:\n * - CouchDB mode: a path like `obsydian_livesync_version` or a `ix:`/`ps:`\n * prefixed id would otherwise become a document id and overwrite LiveSync's\n * own control documents, stopping sync on every device.\n * - Local mode: a path like `.obsidian/plugins/x/main.js` would otherwise\n * write executable plugin code into the vault, and `.obsidian/.../data.json`\n * would expose LiveSync credentials.\n *\n * Rules: vault-relative, ends in `.md`, no `..`, no leading `/`, no `:` (its\n * only use here is LiveSync's internal id prefixes), no NUL, and no path\n * segment (split on either slash) that starts with `.` (hidden files and the\n * `.obsidian` config dir). Root-level LiveSync control flag files are also\n * rejected even though they are `.md`.\n */\n\n// LiveSync's note-shaped control files, all at the vault root. Writing one and\n// letting it sync triggers a vault-wide rebuild/fetch on every device, so they\n// are not writable through the note tools. Compared lowercase because LiveSync\n// lowercases ids when case-insensitive handling is on.\nconst RESERVED_ROOT_FILES = new Set([\n \"redflag.md\",\n \"redflag2.md\",\n \"redflag3.md\",\n \"flag_rebuild.md\",\n \"flag_fetch.md\",\n]);\n\nexport function validateNotePath(path: string): void {\n if (!path || path.length > 1000) {\n throw new Error(\"Invalid note path\");\n }\n if (path.startsWith(\"/\") || path.includes(\"\\0\") || path.includes(\"..\")) {\n throw new Error(\"Invalid note path\");\n }\n if (path.includes(\":\")) {\n throw new Error(\"Invalid note path: ':' is not allowed\");\n }\n if (!path.endsWith(\".md\")) {\n throw new Error(\"Invalid note path: must be a vault-relative path ending in .md\");\n }\n for (const segment of path.split(/[/\\\\]/)) {\n if (segment === \"\") {\n throw new Error(\"Invalid note path: empty path segment\");\n }\n if (segment.startsWith(\".\")) {\n throw new Error(\"Invalid note path: dot-folders and hidden files (e.g. .obsidian) are not allowed\");\n }\n }\n // Root-level control flag files (no folder segment).\n if (!path.includes(\"/\") && !path.includes(\"\\\\\") && RESERVED_ROOT_FILES.has(path.toLowerCase())) {\n throw new Error(\"Invalid note path: reserved LiveSync control file\");\n }\n}\n\n/** Boolean form of validateNotePath, for filtering listings and the index. */\nexport function isValidNotePath(path: string): boolean {\n try {\n validateNotePath(path);\n return true;\n } catch {\n return false;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAUO,SAAS,yBAAyB,SAA+B;AACpE,QAAM,cAAmC,CAAC;AAC1C,QAAM,OAAO,oBAAI,IAAY;AAC7B,QAAM,QAAkB,CAAC;AAGzB,MAAI,QAAQ,WAAW,OAAO,GAAG;AAC7B,UAAM,MAAM,QAAQ,QAAQ,SAAS,CAAC;AACtC,QAAI,QAAQ,IAAI;AACZ,YAAM,OAAO,QAAQ,MAAM,GAAG,GAAG;AACjC,UAAI,aAAa;AACjB,iBAAW,QAAQ,KAAK,MAAM,IAAI,GAAG;AAEjC,YAAI,YAAY;AACZ,gBAAM,WAAW,KAAK,MAAM,cAAc;AAC1C,cAAI,UAAU;AACV,kBAAM,UAAU,SAAS,CAAC,EAAE,KAAK;AACjC,gBAAI,QAAS,MAAK,IAAI,OAAO;AAC7B;AAAA,UACJ;AACA,uBAAa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