obsidian-sync-mcp 0.7.2 → 0.8.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.
@@ -3,11 +3,13 @@
3
3
  if(!("navigator" in globalThis)){globalThis.navigator={language:"en"};}
4
4
  import {
5
5
  deriveContent
6
- } from "./chunk-SGLPTZJA.js";
6
+ } from "./chunk-GSVMRHWP.js";
7
7
  import {
8
8
  __reExport,
9
- parseFrontmatterAndLinks
10
- } from "./chunk-43MP6CJE.js";
9
+ isValidNotePath,
10
+ parseFrontmatterAndLinks,
11
+ validateNotePath
12
+ } from "./chunk-VR63UUCB.js";
11
13
 
12
14
  // lib/livesync-commonlib/src/common/types.ts
13
15
  import {
@@ -10000,15 +10002,63 @@ function classifyIds(ids) {
10000
10002
  return obfuscated > 0 ? "obfuscated" : "plain";
10001
10003
  }
10002
10004
 
10005
+ // src/couchdb-preflight.ts
10006
+ function databaseUrl(url, database) {
10007
+ const u = new URL(url);
10008
+ u.username = "";
10009
+ u.password = "";
10010
+ u.hash = "";
10011
+ u.pathname = `${u.pathname.replace(/\/+$/, "")}/${encodeURIComponent(database)}`;
10012
+ return u.toString();
10013
+ }
10014
+ function networkReason(err) {
10015
+ if (err instanceof Error) {
10016
+ if (err.name === "TimeoutError") return "no response (timed out)";
10017
+ const cause = err.cause;
10018
+ if (cause instanceof Error) return cause.message || String(cause.code ?? cause.name);
10019
+ return err.message;
10020
+ }
10021
+ return String(err);
10022
+ }
10023
+ async function checkDatabase(target, options = {}) {
10024
+ const { fetchImpl = fetch, timeoutMs = 1e4 } = options;
10025
+ if (!target.database) {
10026
+ throw new Error("COUCHDB_DATABASE is empty. Set it to the name of your LiveSync database.");
10027
+ }
10028
+ const url = databaseUrl(target.url, target.database);
10029
+ const token = Buffer.from(`${target.username}:${target.password}`).toString("base64");
10030
+ const headers = { Accept: "application/json", Authorization: `Basic ${token}` };
10031
+ let res;
10032
+ try {
10033
+ res = await fetchImpl(url, { headers, signal: AbortSignal.timeout(timeoutMs) });
10034
+ } catch (err) {
10035
+ throw new Error(`Cannot reach CouchDB at ${url}: ${networkReason(err)}.`);
10036
+ }
10037
+ await res.body?.cancel();
10038
+ if (res.ok) return;
10039
+ if (res.status === 404) {
10040
+ throw new Error(
10041
+ `CouchDB database "${target.database}" does not exist at ${url}. This server never creates databases; check COUCHDB_DATABASE.`
10042
+ );
10043
+ }
10044
+ if (res.status === 401 || res.status === 403) {
10045
+ throw new Error(
10046
+ `CouchDB at ${url} refused access to database "${target.database}" (HTTP ${res.status}). Check COUCHDB_USER and COUCHDB_PASSWORD, and that the user is a member of the database.`
10047
+ );
10048
+ }
10049
+ throw new Error(`CouchDB at ${url} returned HTTP ${res.status} for database "${target.database}".`);
10050
+ }
10051
+
10003
10052
  // src/vault.ts
10004
10053
  var Vault = class _Vault {
10054
+ // Created in init(): constructing it starts connecting, and PouchDB would
10055
+ // create a missing database, so the existence check has to come first.
10005
10056
  manipulator;
10006
10057
  passphrase;
10007
10058
  config;
10008
10059
  constructor(config) {
10009
10060
  this.config = config;
10010
10061
  this.passphrase = config.passphrase;
10011
- this.manipulator = new DirectFileManipulator(_Vault.buildOptions(config, !!config.obfuscatePaths));
10012
10062
  }
10013
10063
  static buildOptions(config, obfuscatePaths) {
10014
10064
  return {
@@ -10025,6 +10075,13 @@ var Vault = class _Vault {
10025
10075
  };
10026
10076
  }
10027
10077
  async init() {
10078
+ await checkDatabase({
10079
+ url: this.config.couchdbUrl,
10080
+ database: this.config.database,
10081
+ username: this.config.couchdbUser,
10082
+ password: this.config.couchdbPassword
10083
+ });
10084
+ this.manipulator = new DirectFileManipulator(_Vault.buildOptions(this.config, !!this.config.obfuscatePaths));
10028
10085
  await this.manipulator.ready.promise;
10029
10086
  await this.reconcileObfuscation();
10030
10087
  }
@@ -10088,15 +10145,15 @@ var Vault = class _Vault {
10088
10145
  await this.manipulator.close();
10089
10146
  }
10090
10147
  static mdFilter(meta) {
10091
- return (meta.path ?? "").endsWith(".md");
10148
+ return isValidNotePath(meta.path ?? "");
10092
10149
  }
10093
10150
  static docToChange(doc, callback, seq) {
10094
10151
  const path = doc.path ?? "";
10095
- if (!path.endsWith(".md")) return;
10152
+ if (!isValidNotePath(path)) return;
10096
10153
  const content = deriveContent(doc);
10097
10154
  callback(path, content, content === null ? void 0 : doc.mtime, seq);
10098
10155
  }
10099
- async catchUp(since, callback, onBatch) {
10156
+ async catchUp(since, callback, onBatch, stats) {
10100
10157
  const BATCH_SIZE2 = 50;
10101
10158
  const db = this.manipulator.liveSyncLocalDB.localDatabase;
10102
10159
  let currentSince = since;
@@ -10119,12 +10176,21 @@ var Vault = class _Vault {
10119
10176
  try {
10120
10177
  path = await decrypt4(path, this.passphrase, false);
10121
10178
  } catch {
10179
+ if (stats) stats.unreadable++;
10122
10180
  continue;
10123
10181
  }
10124
10182
  }
10125
- if (!path.endsWith(".md") && !meta.deleted) continue;
10183
+ if (meta.deleted) {
10184
+ if (isValidNotePath(path)) callback(path, null);
10185
+ continue;
10186
+ }
10187
+ if (!isValidNotePath(path)) continue;
10126
10188
  const doc = await this.manipulator.getByMeta(meta).catch(() => null);
10127
- if (doc) _Vault.docToChange(doc, callback);
10189
+ if (doc) {
10190
+ _Vault.docToChange(doc, callback);
10191
+ } else if (stats) {
10192
+ stats.unreadable++;
10193
+ }
10128
10194
  }
10129
10195
  totalProcessed += result.results.length;
10130
10196
  currentSince = String(result.last_seq);
@@ -10144,9 +10210,7 @@ var Vault = class _Vault {
10144
10210
  );
10145
10211
  }
10146
10212
  validatePath(path) {
10147
- if (!path || path.startsWith("/") || path.includes("\0") || path.includes("..") || path.length > 1e3) {
10148
- throw new Error("Invalid path");
10149
- }
10213
+ validateNotePath(path);
10150
10214
  }
10151
10215
  async readNote(path) {
10152
10216
  this.validatePath(path);
@@ -10210,7 +10274,7 @@ var Vault = class _Vault {
10210
10274
  const entry = doc;
10211
10275
  if (entry.deleted) continue;
10212
10276
  const notePath = entry.path ?? "";
10213
- if (!notePath.endsWith(".md")) continue;
10277
+ if (!isValidNotePath(notePath)) continue;
10214
10278
  if (folder && !notePath.startsWith(folder)) continue;
10215
10279
  results.push({ path: notePath, mtime: entry.mtime ?? 0 });
10216
10280
  }
@@ -10220,4 +10284,4 @@ var Vault = class _Vault {
10220
10284
  export {
10221
10285
  Vault
10222
10286
  };
10223
- //# sourceMappingURL=vault-YGHHZZZQ.js.map
10287
+ //# sourceMappingURL=vault-IAH37QM4.js.map