obsidian-sync-mcp 0.10.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.
@@ -5,11 +5,16 @@ import {
5
5
  deriveContent
6
6
  } from "./chunk-GSVMRHWP.js";
7
7
  import {
8
+ AttachmentExistsError,
9
+ AttachmentMoveIncompleteError,
10
+ AttachmentTooLargeError,
8
11
  __reExport,
12
+ isValidAttachmentPath,
9
13
  isValidNotePath,
10
14
  parseFrontmatterAndLinks,
15
+ validateAttachmentPath,
11
16
  validateNotePath
12
- } from "./chunk-VR63UUCB.js";
17
+ } from "./chunk-XZ4HUASL.js";
13
18
 
14
19
  // lib/livesync-commonlib/src/common/types.ts
15
20
  import {
@@ -1728,6 +1733,18 @@ function getPath(entry) {
1728
1733
  function stripPrefix(prefixedPath) {
1729
1734
  return expandFilePathPrefix(prefixedPath)[1];
1730
1735
  }
1736
+ function isPlainText(filename) {
1737
+ if (filename.endsWith(".md")) return true;
1738
+ if (filename.endsWith(".txt")) return true;
1739
+ if (filename.endsWith(".svg")) return true;
1740
+ if (filename.endsWith(".html")) return true;
1741
+ if (filename.endsWith(".csv")) return true;
1742
+ if (filename.endsWith(".css")) return true;
1743
+ if (filename.endsWith(".js")) return true;
1744
+ if (filename.endsWith(".xml")) return true;
1745
+ if (filename.endsWith(".canvas")) return true;
1746
+ return false;
1747
+ }
1731
1748
  function shouldSplitAsPlainText(filename) {
1732
1749
  if (filename.endsWith(".md")) return true;
1733
1750
  if (filename.endsWith(".txt")) return true;
@@ -2017,6 +2034,19 @@ function createBlob(data) {
2017
2034
  if (data instanceof Uint8Array || data instanceof ArrayBuffer) return createBinaryBlob(data);
2018
2035
  return createTextBlob(data);
2019
2036
  }
2037
+ function isTextDocument(doc) {
2038
+ if (doc.type == "plain") return true;
2039
+ if (doc.datatype == "plain") return true;
2040
+ if (isPlainText(doc.path)) return true;
2041
+ return false;
2042
+ }
2043
+ function readContent(doc) {
2044
+ if (isTextDocument(doc)) {
2045
+ return getDocData(doc.data);
2046
+ } else {
2047
+ return decodeBinary(doc.data);
2048
+ }
2049
+ }
2020
2050
  var isIndexDBCmpExist = typeof compatGlobal?.indexedDB?.cmp !== "undefined";
2021
2051
  function isObfuscatedEntry(doc) {
2022
2052
  if (doc._id.startsWith(PREFIX_OBFUSCATED)) {
@@ -12037,6 +12067,38 @@ var DirectFileManipulator = class {
12037
12067
  // src/vault.ts
12038
12068
  import { isPathProbablyObfuscated as isPathProbablyObfuscated2, decrypt as decrypt4 } from "octagonal-wheels/encryption/encryption";
12039
12069
 
12070
+ // src/keyed-lock.ts
12071
+ var KeyedLock = class {
12072
+ tails = /* @__PURE__ */ new Map();
12073
+ /** Run `fn` once every earlier holder of any of `keys` has finished. */
12074
+ async run(keys, fn) {
12075
+ const unique5 = [...new Set(keys)].sort();
12076
+ const releases = [];
12077
+ try {
12078
+ for (const key of unique5) releases.push(await this.acquire(key));
12079
+ return await fn();
12080
+ } finally {
12081
+ for (const release of releases.reverse()) release();
12082
+ }
12083
+ }
12084
+ async acquire(key) {
12085
+ const previous = this.tails.get(key) ?? Promise.resolve();
12086
+ let release;
12087
+ const current = new Promise((r) => release = r);
12088
+ const tail = previous.then(() => current);
12089
+ this.tails.set(key, tail);
12090
+ await previous;
12091
+ return () => {
12092
+ release();
12093
+ if (this.tails.get(key) === tail) this.tails.delete(key);
12094
+ };
12095
+ }
12096
+ /** Keys currently held or queued (for tests). */
12097
+ get size() {
12098
+ return this.tails.size;
12099
+ }
12100
+ };
12101
+
12040
12102
  // src/id-derivation-config.ts
12041
12103
  function idDerivationOptions(obfuscatePaths, idDerivationKey) {
12042
12104
  if (obfuscatePaths && idDerivationKey) {
@@ -12118,6 +12180,8 @@ var Vault = class _Vault {
12118
12180
  // Obfuscation actually in effect after reconciling with the database, which
12119
12181
  // can differ from the configured value.
12120
12182
  obfuscatePathsEffective = false;
12183
+ // Serializes attachment writes per document id (see writeAttachmentUnlocked).
12184
+ attachmentLock = new KeyedLock();
12121
12185
  constructor(config) {
12122
12186
  this.config = config;
12123
12187
  this.passphrase = config.passphrase;
@@ -12244,7 +12308,7 @@ var Vault = class _Vault {
12244
12308
  const content = deriveContent(doc);
12245
12309
  callback(path, content, content === null ? void 0 : doc.mtime, seq);
12246
12310
  }
12247
- async catchUp(since, callback, onBatch, stats) {
12311
+ async catchUp(since, callback, onBatch, stats, onAttachment) {
12248
12312
  const BATCH_SIZE2 = 50;
12249
12313
  const db = this.manipulator.liveSyncLocalDB.localDatabase;
12250
12314
  let currentSince = since;
@@ -12271,6 +12335,10 @@ var Vault = class _Vault {
12271
12335
  continue;
12272
12336
  }
12273
12337
  }
12338
+ if (onAttachment && isValidAttachmentPath(path)) {
12339
+ onAttachment(path, meta.deleted ? null : { size: meta.size ?? 0, mtime: meta.mtime ?? 0 });
12340
+ continue;
12341
+ }
12274
12342
  if (meta.deleted) {
12275
12343
  if (isValidNotePath(path)) callback(path, null);
12276
12344
  continue;
@@ -12294,12 +12362,29 @@ var Vault = class _Vault {
12294
12362
  this.manipulator.since = currentSince;
12295
12363
  return currentSince;
12296
12364
  }
12297
- watchChanges(callback) {
12365
+ watchChanges(callback, onAttachment) {
12366
+ const filter = (meta) => {
12367
+ if (onAttachment) this.reportAttachment(meta, onAttachment);
12368
+ return _Vault.mdFilter(meta);
12369
+ };
12298
12370
  this.manipulator.beginWatch(
12299
12371
  (doc, seq) => _Vault.docToChange(doc, callback, seq),
12300
- _Vault.mdFilter
12372
+ filter
12301
12373
  );
12302
12374
  }
12375
+ reportAttachment(meta, onAttachment) {
12376
+ const emit = (path2) => {
12377
+ if (!isValidAttachmentPath(path2)) return;
12378
+ onAttachment(path2, meta.deleted ? null : { size: meta.size ?? 0, mtime: meta.mtime ?? 0 });
12379
+ };
12380
+ const path = meta.path ?? "";
12381
+ if (isPathProbablyObfuscated2(path) && this.passphrase) {
12382
+ decrypt4(path, this.passphrase, false).then(emit, () => {
12383
+ });
12384
+ } else {
12385
+ emit(path);
12386
+ }
12387
+ }
12303
12388
  validatePath(path) {
12304
12389
  validateNotePath(path);
12305
12390
  }
@@ -12337,8 +12422,11 @@ var Vault = class _Vault {
12337
12422
  this.validatePath(to);
12338
12423
  const content = await this.readNote(from);
12339
12424
  if (content === null) return false;
12425
+ if (from === to) return true;
12340
12426
  const wrote = await this.writeNote(to, content);
12341
12427
  if (!wrote) return false;
12428
+ const fromId = await this.manipulator.path2id(from);
12429
+ if (fromId === await this.manipulator.path2id(to)) return true;
12342
12430
  return await this.deleteNote(from);
12343
12431
  }
12344
12432
  async getMetadata(path) {
@@ -12371,8 +12459,83 @@ var Vault = class _Vault {
12371
12459
  }
12372
12460
  return results.sort((a, b) => a.path.localeCompare(b.path));
12373
12461
  }
12462
+ async writeAttachment(path, data, options = {}) {
12463
+ validateAttachmentPath(path);
12464
+ const id = await this.manipulator.path2id(path);
12465
+ return await this.attachmentLock.run([id], () => this.writeAttachmentUnlocked(path, data, options));
12466
+ }
12467
+ /**
12468
+ * The library's put always writes over the current revision, so "create
12469
+ * unless it exists" can't be one CouchDB request. Callers hold the
12470
+ * document's lock instead, which covers writes made by this server; a
12471
+ * concurrent write from Obsidian is left to LiveSync's conflict handling.
12472
+ */
12473
+ async writeAttachmentUnlocked(path, data, { overwrite = false }) {
12474
+ clearHandlers();
12475
+ let ctime = Date.now();
12476
+ const existing = await this.manipulator.get(path, true);
12477
+ if (existing && !overwrite) throw new AttachmentExistsError(path);
12478
+ if (existing && "ctime" in existing) {
12479
+ ctime = existing.ctime;
12480
+ }
12481
+ const blob = createBinaryBlob(new Uint8Array(data));
12482
+ return await this.manipulator.put(path, blob, {
12483
+ ctime,
12484
+ mtime: Date.now(),
12485
+ size: data.byteLength
12486
+ });
12487
+ }
12488
+ async deleteAttachment(path) {
12489
+ validateAttachmentPath(path);
12490
+ const id = await this.manipulator.path2id(path);
12491
+ return await this.attachmentLock.run([id], async () => {
12492
+ clearHandlers();
12493
+ return await this.manipulator.delete(path);
12494
+ });
12495
+ }
12496
+ async moveAttachment(from, to, { maxBytes, ...options } = {}) {
12497
+ validateAttachmentPath(from);
12498
+ validateAttachmentPath(to);
12499
+ const fromId = await this.manipulator.path2id(from);
12500
+ const toId = await this.manipulator.path2id(to);
12501
+ return await this.attachmentLock.run([fromId, toId], async () => {
12502
+ const meta = await this.manipulator.get(from, true);
12503
+ if (!meta) return false;
12504
+ if (from === to) return true;
12505
+ if (maxBytes !== void 0 && !(Number.isFinite(meta.size) && meta.size <= maxBytes)) {
12506
+ throw new AttachmentTooLargeError(from, meta.size, maxBytes);
12507
+ }
12508
+ if (fromId !== toId && !options.overwrite && await this.manipulator.get(to, true)) {
12509
+ throw new AttachmentExistsError(to);
12510
+ }
12511
+ const entry = await this.manipulator.get(from);
12512
+ if (!entry || !("data" in entry)) return false;
12513
+ const content = readContent(entry);
12514
+ const bytes = typeof content === "string" ? new TextEncoder().encode(content) : new Uint8Array(content);
12515
+ if (maxBytes !== void 0 && bytes.byteLength > maxBytes) throw new AttachmentTooLargeError(from, bytes.byteLength, maxBytes);
12516
+ if (fromId === toId) {
12517
+ return await this.writeAttachmentUnlocked(to, bytes, { overwrite: true });
12518
+ }
12519
+ const wrote = await this.writeAttachmentUnlocked(to, bytes, options);
12520
+ if (!wrote) return false;
12521
+ clearHandlers();
12522
+ let deleted = false;
12523
+ try {
12524
+ deleted = await this.manipulator.delete(from);
12525
+ } catch {
12526
+ }
12527
+ if (!deleted) throw new AttachmentMoveIncompleteError(from, to);
12528
+ return true;
12529
+ });
12530
+ }
12531
+ async getAttachmentInfo(path) {
12532
+ validateAttachmentPath(path);
12533
+ const entry = await this.manipulator.get(path, true);
12534
+ if (!entry) return null;
12535
+ return { path, size: entry.size, ctime: entry.ctime, mtime: entry.mtime };
12536
+ }
12374
12537
  };
12375
12538
  export {
12376
12539
  Vault
12377
12540
  };
12378
- //# sourceMappingURL=vault-6H64Y46A.js.map
12541
+ //# sourceMappingURL=vault-7FFBJ5QT.js.map