@open-document/core 0.13.0 → 0.13.2
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/dist/{build-CbgIk6bA.js → build-CvsOak5B.js} +1 -1
- package/dist/{check-BTroj-5Z.js → check-CbUBwYmf.js} +3 -3
- package/dist/cli/bin.js +9 -9
- package/dist/{config-DKKNIbvO.js → config-CltJJVsw.js} +337 -180
- package/dist/{context-DXNP8v9I.js → context-CWgfsHoE.js} +1 -1
- package/dist/{dev-YXumw5iX.js → dev-B8H2qT6G.js} +1 -1
- package/dist/{diff-CTFfU4Tz.js → diff-BvIjjgnc.js} +2 -2
- package/dist/{export-D2QLtF84.js → export-DX30_s2q.js} +3 -3
- package/dist/{import-CcmrFetU.js → import-DgsdJT5e.js} +2 -2
- package/dist/index.js +3 -0
- package/dist/ops/index.d.ts +96 -11
- package/dist/ops/index.js +3 -3
- package/dist/{ops-DmEecq1k.js → ops-CSR5RlrE.js} +1 -1
- package/dist/{preview-_mHpBhMk.js → preview-DYMUNjXL.js} +1 -1
- package/dist/{templates-MMblHwmT.js → templates-B5Tz0AKb.js} +3 -3
- package/dist/vite/index.js +1 -1
- package/package.json +1 -1
- package/src/app/components/code-block.tsx +4 -0
- package/src/app/lib/docx/from-dom.ts +292 -15
- package/src/app/lib/docx/ooxml.test.ts +88 -0
- package/src/app/lib/docx/ooxml.ts +118 -27
- package/src/app/lib/export-docx.ts +31 -2
- package/src/app/lib/export-dom.ts +10 -1
|
@@ -647,53 +647,67 @@ async function listDocuments(ctx) {
|
|
|
647
647
|
async function readDocument(ctx, docId) {
|
|
648
648
|
const entry = resolveEntry(ctx, docId);
|
|
649
649
|
if (!entry) throw new OpsError(404, `document not found: ${docId}`);
|
|
650
|
+
const source = await fs.readFile(entry, "utf8");
|
|
650
651
|
return {
|
|
651
652
|
id: docId,
|
|
652
653
|
entry: path.relative(ctx.userCwd, entry),
|
|
653
|
-
source
|
|
654
|
+
source,
|
|
655
|
+
hash: hashSource(source)
|
|
654
656
|
};
|
|
655
657
|
}
|
|
656
658
|
async function createDocument(ctx, docId, source) {
|
|
657
659
|
const dir = docDir(ctx, docId);
|
|
658
|
-
if (existsSync(dir)) throw new OpsError(409, `document already exists: ${docId}`);
|
|
659
|
-
await fs.mkdir(dir, { recursive: true });
|
|
660
660
|
const entry = path.join(dir, "index.tsx");
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
661
|
+
return withFileLock(entry, async () => {
|
|
662
|
+
if (existsSync(dir)) throw new OpsError(409, `document already exists: ${docId}`);
|
|
663
|
+
await fs.mkdir(dir, { recursive: true });
|
|
664
|
+
await fs.writeFile(entry, source, "utf8");
|
|
665
|
+
return {
|
|
666
|
+
id: docId,
|
|
667
|
+
entry: path.relative(ctx.userCwd, entry),
|
|
668
|
+
hash: hashSource(source)
|
|
669
|
+
};
|
|
670
|
+
});
|
|
666
671
|
}
|
|
667
672
|
/**
|
|
668
|
-
* Whole-file overwrite. `expected`
|
|
669
|
-
*
|
|
670
|
-
*
|
|
673
|
+
* Whole-file overwrite. `expected` (full source) or `expectedHash` (16-char
|
|
674
|
+
* SHA-256 digest from `readDocument`) turns a blind overwrite into an atomic
|
|
675
|
+
* conflict check inside a per-file write lock, keeping two agents (or an agent
|
|
676
|
+
* and a person on GCS FUSE) from clobbering each other.
|
|
671
677
|
*/
|
|
672
|
-
async function writeDocument(ctx, docId, source, expected) {
|
|
678
|
+
async function writeDocument(ctx, docId, source, expected, expectedHash) {
|
|
673
679
|
const entry = resolveEntry(ctx, docId);
|
|
674
680
|
if (!entry) throw new OpsError(404, `document not found: ${docId}`);
|
|
675
|
-
|
|
676
|
-
if (await fs.readFile(entry, "utf8")
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
681
|
+
return withFileLock(entry, async () => {
|
|
682
|
+
if (expected !== void 0 || expectedHash !== void 0) assertExpectedSource(await fs.readFile(entry, "utf8"), {
|
|
683
|
+
expected,
|
|
684
|
+
expectedHash
|
|
685
|
+
});
|
|
686
|
+
await fs.writeFile(entry, source, "utf8");
|
|
687
|
+
return {
|
|
688
|
+
id: docId,
|
|
689
|
+
entry: path.relative(ctx.userCwd, entry),
|
|
690
|
+
hash: hashSource(source)
|
|
691
|
+
};
|
|
692
|
+
});
|
|
683
693
|
}
|
|
684
|
-
async function renameDocument(ctx, docId, rawTitle) {
|
|
694
|
+
async function renameDocument(ctx, docId, rawTitle, opts = {}) {
|
|
685
695
|
const title = validateDocTitle(rawTitle);
|
|
686
696
|
if (!title) throw new OpsError(400, "invalid title");
|
|
687
697
|
const entry = resolveEntry(ctx, docId);
|
|
688
698
|
if (!entry) throw new OpsError(404, `document not found: ${docId}`);
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
699
|
+
return withFileLock(entry, async () => {
|
|
700
|
+
const source = await fs.readFile(entry, "utf8");
|
|
701
|
+
assertExpectedSource(source, opts);
|
|
702
|
+
const next = setMetaTitle(source, title);
|
|
703
|
+
if (next === null) throw new OpsError(422, "document has no `export const meta` object to rename");
|
|
704
|
+
if (next !== source) await fs.writeFile(entry, next, "utf8");
|
|
705
|
+
return {
|
|
706
|
+
id: docId,
|
|
707
|
+
title,
|
|
708
|
+
hash: hashSource(next)
|
|
709
|
+
};
|
|
710
|
+
});
|
|
697
711
|
}
|
|
698
712
|
async function duplicateDocument(ctx, docId, newId) {
|
|
699
713
|
const dir = docDir(ctx, docId);
|
|
@@ -707,17 +721,19 @@ async function duplicateDocument(ctx, docId, newId) {
|
|
|
707
721
|
} else targetId = nextCopyId(docId, taken);
|
|
708
722
|
await fs.cp(dir, docDir(ctx, targetId), { recursive: true });
|
|
709
723
|
const entry = resolveEntry(ctx, targetId);
|
|
710
|
-
if (entry) {
|
|
724
|
+
if (entry) await withFileLock(entry, async () => {
|
|
711
725
|
const source = await fs.readFile(entry, "utf8");
|
|
712
726
|
const renamed = setMetaTitle(source, copyTitle(readMetaTitle(source) ?? docId));
|
|
713
727
|
if (renamed && renamed !== source) await fs.writeFile(entry, renamed, "utf8");
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
728
|
+
});
|
|
729
|
+
await withFileLock(ctx.manifestPath, async () => {
|
|
730
|
+
const manifest = await readManifest(ctx.manifestPath);
|
|
731
|
+
const folderId = manifest.assignments[docId];
|
|
732
|
+
if (folderId) {
|
|
733
|
+
manifest.assignments[targetId] = folderId;
|
|
734
|
+
await writeManifest(ctx.manifestPath, manifest);
|
|
735
|
+
}
|
|
736
|
+
});
|
|
721
737
|
return { id: targetId };
|
|
722
738
|
}
|
|
723
739
|
async function deleteDocument(ctx, docId) {
|
|
@@ -727,10 +743,53 @@ async function deleteDocument(ctx, docId) {
|
|
|
727
743
|
recursive: true,
|
|
728
744
|
force: true
|
|
729
745
|
});
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
746
|
+
await withFileLock(ctx.manifestPath, async () => {
|
|
747
|
+
const manifest = await readManifest(ctx.manifestPath);
|
|
748
|
+
if (manifest.assignments[docId]) {
|
|
749
|
+
delete manifest.assignments[docId];
|
|
750
|
+
await writeManifest(ctx.manifestPath, manifest);
|
|
751
|
+
}
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
//#endregion
|
|
755
|
+
//#region src/ops/concurrency.ts
|
|
756
|
+
/**
|
|
757
|
+
* Deterministic 16-char SHA-256 hex digest of a document's UTF-8 source.
|
|
758
|
+
* Used as a lightweight optimistic concurrency token (`hash` / `expectedHash`)
|
|
759
|
+
* so clients and MCP agents do not need to re-send the full source string.
|
|
760
|
+
*/
|
|
761
|
+
function hashSource(source) {
|
|
762
|
+
return createHash("sha256").update(source, "utf8").digest("hex").slice(0, 16);
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Verifies that `current` on disk still matches what the caller last read,
|
|
766
|
+
* either via full-source comparison (`expected`) or SHA-256 digest (`expectedHash`).
|
|
767
|
+
*/
|
|
768
|
+
function assertExpectedSource(current, opts) {
|
|
769
|
+
if (opts?.expected !== void 0 && current !== opts.expected) throw new OpsError(409, "document changed on disk since it was read");
|
|
770
|
+
if (opts?.expectedHash !== void 0 && hashSource(current) !== opts.expectedHash) throw new OpsError(409, "document changed on disk since it was read (hash mismatch)");
|
|
771
|
+
}
|
|
772
|
+
const fileQueues = /* @__PURE__ */ new Map();
|
|
773
|
+
/**
|
|
774
|
+
* Serializes async read-modify-write operations on the same file path in FIFO order.
|
|
775
|
+
* Prevents concurrent edits (e.g. over high-latency GCS FUSE mounts or parallel
|
|
776
|
+
* MCP tool calls) from reading the same stale snapshot and overwriting each other.
|
|
777
|
+
*/
|
|
778
|
+
async function withFileLock(filePath, fn) {
|
|
779
|
+
const key = path.resolve(filePath);
|
|
780
|
+
const prev = fileQueues.get(key) ?? Promise.resolve();
|
|
781
|
+
let release;
|
|
782
|
+
const gate = new Promise((resolve) => {
|
|
783
|
+
release = resolve;
|
|
784
|
+
});
|
|
785
|
+
const tail = prev.then(() => gate);
|
|
786
|
+
fileQueues.set(key, tail);
|
|
787
|
+
await prev;
|
|
788
|
+
try {
|
|
789
|
+
return await fn();
|
|
790
|
+
} finally {
|
|
791
|
+
release();
|
|
792
|
+
if (fileQueues.get(key) === tail) fileQueues.delete(key);
|
|
734
793
|
}
|
|
735
794
|
}
|
|
736
795
|
//#endregion
|
|
@@ -855,11 +914,13 @@ async function revertChange(ctx, docId, opts) {
|
|
|
855
914
|
if (!file || !hunk) throw new OpsError(409, "that change is no longer there — reload the changes");
|
|
856
915
|
if (file.binary) throw new OpsError(422, "a binary file cannot be reverted here");
|
|
857
916
|
const target = path.join(ctx.docsRoot, docId, file.path);
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
917
|
+
return withFileLock(target, async () => {
|
|
918
|
+
const next = revertHunkIn(await fs.readFile(target, "utf8"), hunk);
|
|
919
|
+
if (next === null) throw new OpsError(409, "the file changed since the diff was read");
|
|
920
|
+
if (/\.(tsx?|jsx?)$/.test(file.path) && !parseStrict(next)) throw new OpsError(422, "reverting this change alone would break the source — revert the changes around it too");
|
|
921
|
+
await fs.writeFile(target, next, "utf8");
|
|
922
|
+
return { ok: true };
|
|
923
|
+
});
|
|
863
924
|
}
|
|
864
925
|
//#endregion
|
|
865
926
|
//#region src/files/assets.ts
|
|
@@ -1461,7 +1522,9 @@ async function writeAsset(ctx, scope, filename, data) {
|
|
|
1461
1522
|
await fs.mkdir(dir, { recursive: true });
|
|
1462
1523
|
const file = resolveScopedAssetFile(ctx.docsRoot, ctx.globalAssetsRoot, scope, name);
|
|
1463
1524
|
if (!file) throw new OpsError(400, "invalid filename");
|
|
1464
|
-
await
|
|
1525
|
+
await withFileLock(file, async () => {
|
|
1526
|
+
await fs.writeFile(file, data);
|
|
1527
|
+
});
|
|
1465
1528
|
return {
|
|
1466
1529
|
scope,
|
|
1467
1530
|
filename: name,
|
|
@@ -1473,7 +1536,9 @@ async function deleteAsset(ctx, scope, filename) {
|
|
|
1473
1536
|
const file = resolveScopedAssetFile(ctx.docsRoot, ctx.globalAssetsRoot, scope, filename);
|
|
1474
1537
|
if (!file) throw new OpsError(400, "invalid filename");
|
|
1475
1538
|
if (!existsSync(file)) throw new OpsError(404, `asset not found: ${scope}/${filename}`);
|
|
1476
|
-
await
|
|
1539
|
+
await withFileLock(file, async () => {
|
|
1540
|
+
await fs.rm(file);
|
|
1541
|
+
});
|
|
1477
1542
|
}
|
|
1478
1543
|
/** Which documents import this asset — check before deleting one. */
|
|
1479
1544
|
async function findAssetUsages(ctx, scope, filename) {
|
|
@@ -1507,23 +1572,27 @@ async function createFolder(ctx, rawName, rawIcon) {
|
|
|
1507
1572
|
if (!name) throw new OpsError(400, "invalid folder name");
|
|
1508
1573
|
const icon = validateIcon(rawIcon ?? DEFAULT_FOLDER_ICON);
|
|
1509
1574
|
if (!icon) throw new OpsError(400, "invalid icon");
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1575
|
+
return withFileLock(ctx.manifestPath, async () => {
|
|
1576
|
+
const manifest = await readManifest(ctx.manifestPath);
|
|
1577
|
+
const folder = {
|
|
1578
|
+
id: newFolderId(),
|
|
1579
|
+
name,
|
|
1580
|
+
icon
|
|
1581
|
+
};
|
|
1582
|
+
manifest.folders.push(folder);
|
|
1583
|
+
await writeManifest(ctx.manifestPath, manifest);
|
|
1584
|
+
return folder;
|
|
1585
|
+
});
|
|
1519
1586
|
}
|
|
1520
1587
|
/** Filing only edits assignments — a document id never moves on disk. */
|
|
1521
1588
|
async function fileDocument(ctx, docId, folderId) {
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1589
|
+
await withFileLock(ctx.manifestPath, async () => {
|
|
1590
|
+
const manifest = await readManifest(ctx.manifestPath);
|
|
1591
|
+
if (folderId !== null && !manifest.folders.some((f) => f.id === folderId)) throw new OpsError(404, `folder not found: ${folderId}`);
|
|
1592
|
+
if (folderId === null) delete manifest.assignments[docId];
|
|
1593
|
+
else manifest.assignments[docId] = folderId;
|
|
1594
|
+
await writeManifest(ctx.manifestPath, manifest);
|
|
1595
|
+
});
|
|
1527
1596
|
}
|
|
1528
1597
|
//#endregion
|
|
1529
1598
|
//#region src/ops/templates.ts
|
|
@@ -3157,21 +3226,31 @@ async function readText(ctx, docId, locs, shown, prop) {
|
|
|
3157
3226
|
const { source } = await sourceOf(ctx, docId);
|
|
3158
3227
|
const resolved = resolveTextTarget(source, locs, shown, prop);
|
|
3159
3228
|
if (!resolved) throw new OpsError(404, "no editable text at that location");
|
|
3160
|
-
return
|
|
3229
|
+
return {
|
|
3230
|
+
...resolved,
|
|
3231
|
+
hash: hashSource(source)
|
|
3232
|
+
};
|
|
3161
3233
|
}
|
|
3162
3234
|
/**
|
|
3163
3235
|
* `expected` is the text the caller believes is there; when it no longer
|
|
3164
3236
|
* matches the write is refused rather than silently overwriting someone else's
|
|
3165
|
-
* edit. `
|
|
3166
|
-
*
|
|
3167
|
-
* is refused rather than dropping the formatting.
|
|
3237
|
+
* edit. `expectedHash` optionally verifies the file-level SHA-256 digest.
|
|
3238
|
+
* All reads and writes on the same document are serialized via `withFileLock`.
|
|
3168
3239
|
*/
|
|
3169
3240
|
async function writeText(ctx, docId, loc, text, opts = {}) {
|
|
3170
|
-
const
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3241
|
+
const entry = resolveEntry(ctx, docId);
|
|
3242
|
+
if (!entry) throw new OpsError(404, `document not found: ${docId}`);
|
|
3243
|
+
return withFileLock(entry, async () => {
|
|
3244
|
+
const source = await fs.readFile(entry, "utf8");
|
|
3245
|
+
assertExpectedSource(source, { expectedHash: opts.expectedHash });
|
|
3246
|
+
const result = replaceTextAt(source, loc, text, opts);
|
|
3247
|
+
if (!result.ok) throw new OpsError(result.status, result.error);
|
|
3248
|
+
if (result.source !== source) await fs.writeFile(entry, result.source, "utf8");
|
|
3249
|
+
return {
|
|
3250
|
+
ok: true,
|
|
3251
|
+
hash: hashSource(result.source)
|
|
3252
|
+
};
|
|
3253
|
+
});
|
|
3175
3254
|
}
|
|
3176
3255
|
/**
|
|
3177
3256
|
* Several run replacements in one read and one write, so an editing session
|
|
@@ -3179,12 +3258,20 @@ async function writeText(ctx, docId, loc, text, opts = {}) {
|
|
|
3179
3258
|
* that each re-paginate the document. Per-edit failures are reported, not
|
|
3180
3259
|
* thrown: the edits that still apply are written.
|
|
3181
3260
|
*/
|
|
3182
|
-
async function writeTexts(ctx, docId, edits) {
|
|
3261
|
+
async function writeTexts(ctx, docId, edits, opts = {}) {
|
|
3183
3262
|
if (edits.length === 0) throw new OpsError(400, "at least one edit is required");
|
|
3184
|
-
const
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3263
|
+
const entry = resolveEntry(ctx, docId);
|
|
3264
|
+
if (!entry) throw new OpsError(404, `document not found: ${docId}`);
|
|
3265
|
+
return withFileLock(entry, async () => {
|
|
3266
|
+
const source = await fs.readFile(entry, "utf8");
|
|
3267
|
+
assertExpectedSource(source, opts);
|
|
3268
|
+
const result = replaceTextsAt(source, edits);
|
|
3269
|
+
if (result.source !== source) await fs.writeFile(entry, result.source, "utf8");
|
|
3270
|
+
return {
|
|
3271
|
+
results: result.results,
|
|
3272
|
+
hash: hashSource(result.source)
|
|
3273
|
+
};
|
|
3274
|
+
});
|
|
3188
3275
|
}
|
|
3189
3276
|
/** What the element at `loc` sets in its `style`, key by key, for the element panel's Format section. */
|
|
3190
3277
|
async function readStyle(ctx, docId, loc, tag) {
|
|
@@ -3196,24 +3283,38 @@ async function readStyle(ctx, docId, loc, tag) {
|
|
|
3196
3283
|
* against the same read of the file and written once. Per-edit failures are
|
|
3197
3284
|
* reported, not thrown, like `writeTexts`.
|
|
3198
3285
|
*/
|
|
3199
|
-
async function writeEdits(ctx, docId, texts, styles) {
|
|
3286
|
+
async function writeEdits(ctx, docId, texts, styles, opts = {}) {
|
|
3200
3287
|
if (texts.length + styles.length === 0) throw new OpsError(400, "at least one edit is required");
|
|
3201
|
-
const
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
|
|
3207
|
-
|
|
3288
|
+
const entry = resolveEntry(ctx, docId);
|
|
3289
|
+
if (!entry) throw new OpsError(404, `document not found: ${docId}`);
|
|
3290
|
+
return withFileLock(entry, async () => {
|
|
3291
|
+
const source = await fs.readFile(entry, "utf8");
|
|
3292
|
+
assertExpectedSource(source, opts);
|
|
3293
|
+
const result = replaceEditsAt(source, texts, styles);
|
|
3294
|
+
if (result.source !== source) await fs.writeFile(entry, result.source, "utf8");
|
|
3295
|
+
return {
|
|
3296
|
+
texts: result.texts,
|
|
3297
|
+
styles: result.styles,
|
|
3298
|
+
hash: hashSource(result.source)
|
|
3299
|
+
};
|
|
3300
|
+
});
|
|
3208
3301
|
}
|
|
3209
3302
|
/** Leaves a `@doc-comment` marker in the source for a later agent pass to act on. */
|
|
3210
|
-
async function addComment(ctx, docId, loc, note, hint) {
|
|
3303
|
+
async function addComment(ctx, docId, loc, note, hint, opts = {}) {
|
|
3211
3304
|
if (note.trim() === "") throw new OpsError(400, "note is empty");
|
|
3212
|
-
const
|
|
3213
|
-
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
|
|
3305
|
+
const entry = resolveEntry(ctx, docId);
|
|
3306
|
+
if (!entry) throw new OpsError(404, `document not found: ${docId}`);
|
|
3307
|
+
return withFileLock(entry, async () => {
|
|
3308
|
+
const source = await fs.readFile(entry, "utf8");
|
|
3309
|
+
assertExpectedSource(source, opts);
|
|
3310
|
+
const inserted = insertMarker(source, loc, note.trim(), hint);
|
|
3311
|
+
if (!inserted) throw new OpsError(422, "cannot anchor a comment here — pick the surrounding element");
|
|
3312
|
+
await fs.writeFile(entry, inserted.source, "utf8");
|
|
3313
|
+
return {
|
|
3314
|
+
id: inserted.id,
|
|
3315
|
+
hash: hashSource(inserted.source)
|
|
3316
|
+
};
|
|
3317
|
+
});
|
|
3217
3318
|
}
|
|
3218
3319
|
//#endregion
|
|
3219
3320
|
//#region src/vite/routes/edit.ts
|
|
@@ -3304,9 +3405,13 @@ function registerEditRoutes(server, ctx) {
|
|
|
3304
3405
|
column
|
|
3305
3406
|
}));
|
|
3306
3407
|
if (!entry || locs.length === 0) return json(res, 400, { error: "invalid target" });
|
|
3307
|
-
const
|
|
3408
|
+
const source = await fs.readFile(entry, "utf8");
|
|
3409
|
+
const resolved = resolveTextTarget(source, locs, url.searchParams.get("shown") ?? void 0, url.searchParams.get("prop") ?? void 0);
|
|
3308
3410
|
if (!resolved) return json(res, 404, { error: "element not found" });
|
|
3309
|
-
return json(res, 200,
|
|
3411
|
+
return json(res, 200, {
|
|
3412
|
+
...resolved,
|
|
3413
|
+
hash: hashSource(source)
|
|
3414
|
+
});
|
|
3310
3415
|
}
|
|
3311
3416
|
if (method === "PUT" && url.pathname === "/text") {
|
|
3312
3417
|
const check = validateMutationRequest(req, { requireJsonBody: true });
|
|
@@ -3317,19 +3422,15 @@ function registerEditRoutes(server, ctx) {
|
|
|
3317
3422
|
if (!loc || typeof text !== "string") return json(res, 400, { error: "invalid payload" });
|
|
3318
3423
|
const entry = entryFor(loc.docId);
|
|
3319
3424
|
if (!entry) return json(res, 404, { error: "document not found" });
|
|
3320
|
-
const
|
|
3321
|
-
const result = replaceTextAt(source, loc, text, {
|
|
3425
|
+
const out = await writeText(ctx, loc.docId, loc, text, {
|
|
3322
3426
|
index: typeof body.index === "number" ? body.index : void 0,
|
|
3323
3427
|
expected: typeof body.expected === "string" ? body.expected : void 0,
|
|
3428
|
+
expectedHash: typeof body.expectedHash === "string" ? body.expectedHash : void 0,
|
|
3324
3429
|
shown: typeof body.shown === "string" ? body.shown : void 0,
|
|
3325
3430
|
prop: typeof body.prop === "string" ? body.prop : void 0
|
|
3326
3431
|
});
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
await fs.writeFile(entry, result.source, "utf8");
|
|
3330
|
-
server.watcher.emit("change", entry);
|
|
3331
|
-
}
|
|
3332
|
-
return json(res, 200, { ok: true });
|
|
3432
|
+
server.watcher.emit("change", entry);
|
|
3433
|
+
return json(res, 200, out);
|
|
3333
3434
|
}
|
|
3334
3435
|
if (method === "PUT" && url.pathname === "/texts") {
|
|
3335
3436
|
const check = validateMutationRequest(req, { requireJsonBody: true });
|
|
@@ -3338,7 +3439,7 @@ function registerEditRoutes(server, ctx) {
|
|
|
3338
3439
|
if (typeof body.docId !== "string" || !Array.isArray(body.edits)) return json(res, 400, { error: "invalid payload" });
|
|
3339
3440
|
const edits = readTextEdits(body.docId, body.edits);
|
|
3340
3441
|
if (!edits) return json(res, 400, { error: "invalid payload" });
|
|
3341
|
-
const out = await writeTexts(ctx, body.docId, edits);
|
|
3442
|
+
const out = await writeTexts(ctx, body.docId, edits, { expectedHash: typeof body.expectedHash === "string" ? body.expectedHash : void 0 });
|
|
3342
3443
|
const entry = entryFor(body.docId);
|
|
3343
3444
|
if (entry) server.watcher.emit("change", entry);
|
|
3344
3445
|
return json(res, 200, out);
|
|
@@ -3361,7 +3462,7 @@ function registerEditRoutes(server, ctx) {
|
|
|
3361
3462
|
const texts = readTextEdits(body.docId, body.texts ?? []);
|
|
3362
3463
|
const styles = readStyleEdits(body.docId, body.styles ?? []);
|
|
3363
3464
|
if (!texts || !styles) return json(res, 400, { error: "invalid payload" });
|
|
3364
|
-
const out = await writeEdits(ctx, body.docId, texts, styles);
|
|
3465
|
+
const out = await writeEdits(ctx, body.docId, texts, styles, { expectedHash: typeof body.expectedHash === "string" ? body.expectedHash : void 0 });
|
|
3365
3466
|
const entry = entryFor(body.docId);
|
|
3366
3467
|
if (entry) server.watcher.emit("change", entry);
|
|
3367
3468
|
return json(res, 200, out);
|
|
@@ -3375,13 +3476,12 @@ function registerEditRoutes(server, ctx) {
|
|
|
3375
3476
|
if (!loc || typeof note !== "string" || note.trim() === "") return json(res, 400, { error: "invalid payload" });
|
|
3376
3477
|
const entry = entryFor(loc.docId);
|
|
3377
3478
|
if (!entry) return json(res, 404, { error: "document not found" });
|
|
3378
|
-
const inserted =
|
|
3379
|
-
if (!inserted) return json(res, 422, { error: "cannot anchor a comment here — pick the surrounding element" });
|
|
3380
|
-
await fs.writeFile(entry, inserted.source, "utf8");
|
|
3479
|
+
const inserted = await addComment(ctx, loc.docId, loc, note, typeof body.hint === "string" ? body.hint : void 0, { expectedHash: typeof body.expectedHash === "string" ? body.expectedHash : void 0 });
|
|
3381
3480
|
server.watcher.emit("change", entry);
|
|
3382
3481
|
return json(res, 200, {
|
|
3383
3482
|
ok: true,
|
|
3384
|
-
id: inserted.id
|
|
3483
|
+
id: inserted.id,
|
|
3484
|
+
hash: inserted.hash
|
|
3385
3485
|
});
|
|
3386
3486
|
}
|
|
3387
3487
|
return next();
|
|
@@ -3406,9 +3506,12 @@ function registerEditRoutes(server, ctx) {
|
|
|
3406
3506
|
if (!check.ok) return json(res, check.status, { error: check.error });
|
|
3407
3507
|
const id = url.searchParams.get("id") ?? "";
|
|
3408
3508
|
if (!entry || !/^c-[a-f0-9]+$/.test(id)) return json(res, 400, { error: "invalid id" });
|
|
3409
|
-
|
|
3410
|
-
|
|
3411
|
-
|
|
3509
|
+
if (!await withFileLock(entry, async () => {
|
|
3510
|
+
const nextSource = removeMarker(await fs.readFile(entry, "utf8"), id);
|
|
3511
|
+
if (nextSource === null) return false;
|
|
3512
|
+
await fs.writeFile(entry, nextSource, "utf8");
|
|
3513
|
+
return true;
|
|
3514
|
+
})) return json(res, 404, { error: "comment not found" });
|
|
3412
3515
|
server.watcher.emit("change", entry);
|
|
3413
3516
|
return json(res, 200, { ok: true });
|
|
3414
3517
|
}
|
|
@@ -3434,15 +3537,17 @@ function registerFolderRoutes(server, ctx) {
|
|
|
3434
3537
|
if (!name) return json(res, 400, { error: "invalid name" });
|
|
3435
3538
|
const icon = validateIcon(body.icon);
|
|
3436
3539
|
if (!icon) return json(res, 400, { error: "invalid icon" });
|
|
3437
|
-
|
|
3438
|
-
|
|
3439
|
-
|
|
3440
|
-
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3540
|
+
return json(res, 200, await withFileLock(ctx.manifestPath, async () => {
|
|
3541
|
+
const manifest = await readManifest(ctx.manifestPath);
|
|
3542
|
+
const created = {
|
|
3543
|
+
id: newFolderId(),
|
|
3544
|
+
name,
|
|
3545
|
+
icon
|
|
3546
|
+
};
|
|
3547
|
+
manifest.folders.push(created);
|
|
3548
|
+
await writeManifest(ctx.manifestPath, manifest);
|
|
3549
|
+
return created;
|
|
3550
|
+
}));
|
|
3446
3551
|
}
|
|
3447
3552
|
if (method === "PUT" && url.pathname === "/assign") {
|
|
3448
3553
|
const requestCheck = validateMutationRequest(req, { requireJsonBody: true });
|
|
@@ -3454,23 +3559,29 @@ function registerFolderRoutes(server, ctx) {
|
|
|
3454
3559
|
if (body.folderId === null) folderId = null;
|
|
3455
3560
|
else if (typeof body.folderId === "string" && FOLDER_ID_RE.test(body.folderId)) folderId = body.folderId;
|
|
3456
3561
|
else return json(res, 400, { error: "invalid folderId" });
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3562
|
+
if (!await withFileLock(ctx.manifestPath, async () => {
|
|
3563
|
+
const manifest = await readManifest(ctx.manifestPath);
|
|
3564
|
+
if (folderId && !manifest.folders.some((f) => f.id === folderId)) return false;
|
|
3565
|
+
if (folderId === null) delete manifest.assignments[docId];
|
|
3566
|
+
else manifest.assignments[docId] = folderId;
|
|
3567
|
+
await writeManifest(ctx.manifestPath, manifest);
|
|
3568
|
+
return true;
|
|
3569
|
+
})) return json(res, 404, { error: "folder not found" });
|
|
3462
3570
|
return json(res, 200, { ok: true });
|
|
3463
3571
|
}
|
|
3464
3572
|
if (method === "PUT" && url.pathname === "/reorder") {
|
|
3465
3573
|
const requestCheck = validateMutationRequest(req, { requireJsonBody: true });
|
|
3466
3574
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
3467
3575
|
const body = await readBody(req);
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3576
|
+
if (!await withFileLock(ctx.manifestPath, async () => {
|
|
3577
|
+
const manifest = await readManifest(ctx.manifestPath);
|
|
3578
|
+
const ids = validateReorder(body.ids, manifest.folders);
|
|
3579
|
+
if (!ids) return false;
|
|
3580
|
+
const byId = new Map(manifest.folders.map((f) => [f.id, f]));
|
|
3581
|
+
manifest.folders = ids.map((id) => byId.get(id));
|
|
3582
|
+
await writeManifest(ctx.manifestPath, manifest);
|
|
3583
|
+
return true;
|
|
3584
|
+
})) return json(res, 400, { error: "invalid ids" });
|
|
3474
3585
|
return json(res, 200, { ok: true });
|
|
3475
3586
|
}
|
|
3476
3587
|
const idMatch = url.pathname.match(/^\/([^/]+)$/);
|
|
@@ -3481,31 +3592,50 @@ function registerFolderRoutes(server, ctx) {
|
|
|
3481
3592
|
const requestCheck = validateMutationRequest(req, { requireJsonBody: true });
|
|
3482
3593
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
3483
3594
|
const body = await readBody(req);
|
|
3484
|
-
const
|
|
3485
|
-
|
|
3486
|
-
|
|
3487
|
-
|
|
3488
|
-
|
|
3489
|
-
|
|
3490
|
-
|
|
3491
|
-
|
|
3492
|
-
|
|
3493
|
-
|
|
3494
|
-
|
|
3495
|
-
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
3595
|
+
const outcome = await withFileLock(ctx.manifestPath, async () => {
|
|
3596
|
+
const manifest = await readManifest(ctx.manifestPath);
|
|
3597
|
+
const folder = manifest.folders.find((f) => f.id === id);
|
|
3598
|
+
if (!folder) return {
|
|
3599
|
+
status: 404,
|
|
3600
|
+
error: "folder not found"
|
|
3601
|
+
};
|
|
3602
|
+
if (body.name !== void 0) {
|
|
3603
|
+
const name = validateName(body.name);
|
|
3604
|
+
if (!name) return {
|
|
3605
|
+
status: 400,
|
|
3606
|
+
error: "invalid name"
|
|
3607
|
+
};
|
|
3608
|
+
folder.name = name;
|
|
3609
|
+
}
|
|
3610
|
+
if (body.icon !== void 0) {
|
|
3611
|
+
const icon = validateIcon(body.icon);
|
|
3612
|
+
if (!icon) return {
|
|
3613
|
+
status: 400,
|
|
3614
|
+
error: "invalid icon"
|
|
3615
|
+
};
|
|
3616
|
+
folder.icon = icon;
|
|
3617
|
+
}
|
|
3618
|
+
await writeManifest(ctx.manifestPath, manifest);
|
|
3619
|
+
return {
|
|
3620
|
+
status: 200,
|
|
3621
|
+
folder
|
|
3622
|
+
};
|
|
3623
|
+
});
|
|
3624
|
+
if (outcome.status !== 200) return json(res, outcome.status, { error: outcome.error });
|
|
3625
|
+
return json(res, 200, outcome.folder);
|
|
3499
3626
|
}
|
|
3500
3627
|
if (method === "DELETE") {
|
|
3501
3628
|
const requestCheck = validateMutationRequest(req);
|
|
3502
3629
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
3503
|
-
|
|
3504
|
-
|
|
3505
|
-
|
|
3506
|
-
|
|
3507
|
-
|
|
3508
|
-
|
|
3630
|
+
if (!await withFileLock(ctx.manifestPath, async () => {
|
|
3631
|
+
const manifest = await readManifest(ctx.manifestPath);
|
|
3632
|
+
const before = manifest.folders.length;
|
|
3633
|
+
manifest.folders = manifest.folders.filter((f) => f.id !== id);
|
|
3634
|
+
if (manifest.folders.length === before) return false;
|
|
3635
|
+
for (const [docId, folderId] of Object.entries(manifest.assignments)) if (folderId === id) delete manifest.assignments[docId];
|
|
3636
|
+
await writeManifest(ctx.manifestPath, manifest);
|
|
3637
|
+
return true;
|
|
3638
|
+
})) return json(res, 404, { error: "folder not found" });
|
|
3509
3639
|
return json(res, 200, { ok: true });
|
|
3510
3640
|
}
|
|
3511
3641
|
}
|
|
@@ -3943,41 +4073,68 @@ function designPlugin(opts) {
|
|
|
3943
4073
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
3944
4074
|
const patch = (await readBody(req)).patch;
|
|
3945
4075
|
if (!patch || typeof patch !== "object") return json(res, 400, { error: "missing patch object" });
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
3954
|
-
|
|
3955
|
-
|
|
3956
|
-
|
|
3957
|
-
|
|
3958
|
-
|
|
3959
|
-
|
|
3960
|
-
|
|
3961
|
-
|
|
4076
|
+
const outcome = await withFileLock(file, async () => {
|
|
4077
|
+
let source;
|
|
4078
|
+
try {
|
|
4079
|
+
source = await fs.readFile(file, "utf8");
|
|
4080
|
+
} catch {
|
|
4081
|
+
return {
|
|
4082
|
+
status: 404,
|
|
4083
|
+
payload: { error: "document not found" }
|
|
4084
|
+
};
|
|
4085
|
+
}
|
|
4086
|
+
const parsed = parseDocDesign(source);
|
|
4087
|
+
if (!parsed.ok && parsed.exists) return {
|
|
4088
|
+
status: 422,
|
|
4089
|
+
payload: { error: parsed.error }
|
|
4090
|
+
};
|
|
4091
|
+
const merged = mergeDesign(parsed.ok ? parsed.design : defaultDesign, patch);
|
|
4092
|
+
const written = applyDesignWrite(source, merged);
|
|
4093
|
+
if (!written.ok) return {
|
|
4094
|
+
status: written.status,
|
|
4095
|
+
payload: { error: written.error }
|
|
4096
|
+
};
|
|
4097
|
+
if (written.source !== source) await fs.writeFile(file, written.source, "utf8");
|
|
4098
|
+
return {
|
|
4099
|
+
status: 200,
|
|
4100
|
+
payload: {
|
|
4101
|
+
ok: true,
|
|
4102
|
+
design: merged,
|
|
4103
|
+
created: written.created
|
|
4104
|
+
}
|
|
4105
|
+
};
|
|
3962
4106
|
});
|
|
4107
|
+
return json(res, outcome.status, outcome.payload);
|
|
3963
4108
|
}
|
|
3964
4109
|
if (method === "POST" && url.pathname === "/reset") {
|
|
3965
4110
|
const requestCheck = validateMutationRequest(req);
|
|
3966
4111
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
3967
|
-
|
|
3968
|
-
|
|
3969
|
-
|
|
3970
|
-
|
|
3971
|
-
|
|
3972
|
-
|
|
3973
|
-
|
|
3974
|
-
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
4112
|
+
const outcome = await withFileLock(file, async () => {
|
|
4113
|
+
let source;
|
|
4114
|
+
try {
|
|
4115
|
+
source = await fs.readFile(file, "utf8");
|
|
4116
|
+
} catch {
|
|
4117
|
+
return {
|
|
4118
|
+
status: 404,
|
|
4119
|
+
payload: { error: "document not found" }
|
|
4120
|
+
};
|
|
4121
|
+
}
|
|
4122
|
+
const written = applyDesignWrite(source, defaultDesign);
|
|
4123
|
+
if (!written.ok) return {
|
|
4124
|
+
status: written.status,
|
|
4125
|
+
payload: { error: written.error }
|
|
4126
|
+
};
|
|
4127
|
+
if (written.source !== source) await fs.writeFile(file, written.source, "utf8");
|
|
4128
|
+
return {
|
|
4129
|
+
status: 200,
|
|
4130
|
+
payload: {
|
|
4131
|
+
ok: true,
|
|
4132
|
+
design: defaultDesign,
|
|
4133
|
+
created: written.created
|
|
4134
|
+
}
|
|
4135
|
+
};
|
|
3980
4136
|
});
|
|
4137
|
+
return json(res, outcome.status, outcome.payload);
|
|
3981
4138
|
}
|
|
3982
4139
|
return next();
|
|
3983
4140
|
} catch (err) {
|
|
@@ -4248,4 +4405,4 @@ async function createViteConfig(opts) {
|
|
|
4248
4405
|
};
|
|
4249
4406
|
}
|
|
4250
4407
|
//#endregion
|
|
4251
|
-
export {
|
|
4408
|
+
export { duplicateDocument as A, assertExpectedSource as C, createDocument as D, OpsError as E, resolveEntry as F, writeDocument as I, makeContext as L, listDocuments as M, readDocument as N, deleteDocument as O, renameDocument as P, revertChange as S, withFileLock as T, readTheme as _, writeText as a, listRevisions as b, listTemplates as c, deleteAsset as d, fileDocument as f, listThemes as g, listFolders as h, writeEdits as i, listDocIds$1 as j, docDir as k, suggestDocId as l, listAssets as m, addComment as n, writeTexts as o, findAssetUsages as p, readText as r, createFromTemplate as s, createViteConfig as t, createFolder as u, writeAsset as v, hashSource as w, readChanges as x, validateAssetName as y };
|