@nuasite/cms-sidecar 0.70.4 → 0.71.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 +6 -0
- package/dist/cli.bundle.js +205 -70
- package/dist/concurrency.d.ts +7 -3
- package/dist/concurrency.d.ts.map +1 -1
- package/dist/concurrency.js +8 -4
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +74 -45
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +8 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/concurrency.ts +8 -5
- package/src/server.ts +68 -41
- package/src/types.ts +9 -2
package/README.md
CHANGED
|
@@ -52,6 +52,12 @@ version. `commitChangeset(fs)` is a reference implementation; it is atomic only
|
|
|
52
52
|
else writes between its awaits, so a host with concurrent writers runs the same check
|
|
53
53
|
(`versionOf`, `versionMatches`) and the writes inside its own critical section.
|
|
54
54
|
|
|
55
|
+
Every entry read (`GET …/entries/:slug`) carries `sourceHash`, the digest of the bytes it
|
|
56
|
+
parsed. An entry mutation returns the digest of the bytes it wrote, or for a rename of the
|
|
57
|
+
bytes it moved, taken from those bytes rather than by reading the file again. `PATCH`,
|
|
58
|
+
`DELETE` and `POST …/rename` take that value back as an optional `baseHash` and answer `409`
|
|
59
|
+
when the file changed since.
|
|
60
|
+
|
|
55
61
|
## Collections the scan cannot vouch for
|
|
56
62
|
|
|
57
63
|
`GET /cms/v1/collections` lists the collections the scan could read. The scan is a walk
|
package/dist/cli.bundle.js
CHANGED
|
@@ -24987,10 +24987,12 @@ async function scanCollection(fs3, collectionPath, collectionName, contentDir, f
|
|
|
24987
24987
|
return null;
|
|
24988
24988
|
return await buildCollectionDefinition(fs3, collectionPath, sources, collectionName, contentDir, frontmatterMemo);
|
|
24989
24989
|
}
|
|
24990
|
+
function isPrivateEntryPath(relToBase) {
|
|
24991
|
+
return relToBase.split("/").some((seg) => seg.startsWith("_") || seg.startsWith("."));
|
|
24992
|
+
}
|
|
24990
24993
|
async function globLoaderFiles(fs3, baseRel, pattern, options) {
|
|
24991
|
-
const
|
|
24992
|
-
|
|
24993
|
-
return matches.map((rel) => path5.relative(baseRel, rel)).filter((relToBase) => !isPrivate(relToBase));
|
|
24994
|
+
const matches = await fs3.glob(path5.join(baseRel, pattern), { ...options, skipDirectory: (dir) => isPrivateEntryPath(path5.relative(baseRel, dir)) });
|
|
24995
|
+
return matches.map((rel) => path5.relative(baseRel, rel)).filter((relToBase) => !isPrivateEntryPath(relToBase));
|
|
24994
24996
|
}
|
|
24995
24997
|
async function scanGlobCollection(fs3, collectionName, baseRel, pattern, contentDir, frontmatterMemo) {
|
|
24996
24998
|
const rels = await globLoaderFiles(fs3, baseRel, pattern);
|
|
@@ -25002,7 +25004,36 @@ async function scanGlobCollection(fs3, collectionName, baseRel, pattern, content
|
|
|
25002
25004
|
if (dataSources.length > 0) {
|
|
25003
25005
|
return await buildDataCollectionDefinition(fs3, baseRel, dataSources, collectionName, contentDir);
|
|
25004
25006
|
}
|
|
25005
|
-
|
|
25007
|
+
if (!portRunsLoaderPattern(pattern))
|
|
25008
|
+
return null;
|
|
25009
|
+
const fileExtension = entryExtensionOfPattern(pattern);
|
|
25010
|
+
if (!fileExtension)
|
|
25011
|
+
return null;
|
|
25012
|
+
const isData = fileExtension === "json" || fileExtension === "yaml" || fileExtension === "yml";
|
|
25013
|
+
if (!loadsCreatedEntry(pattern, fileExtension, isData))
|
|
25014
|
+
return null;
|
|
25015
|
+
return assembleCollectionDefinition(collectionName, contentDir, new Map, [], 0, {
|
|
25016
|
+
path: getCollectionSourceBasePath(baseRel, collectionName, contentDir),
|
|
25017
|
+
fileExtension,
|
|
25018
|
+
...isData ? { type: "data" } : {}
|
|
25019
|
+
});
|
|
25020
|
+
}
|
|
25021
|
+
function loadsCreatedEntry(pattern, fileExtension, isData) {
|
|
25022
|
+
const matcher = globToRegExp(path5.posix.normalize(pattern));
|
|
25023
|
+
const indexLayout = !isData && isIndexStyleGlobPattern(pattern);
|
|
25024
|
+
return ["entry", "x-1"].every((slug) => matcher.test(indexLayout ? `${slug}/index.${fileExtension}` : `${slug}.${fileExtension}`));
|
|
25025
|
+
}
|
|
25026
|
+
function isIndexStyleGlobPattern(pattern) {
|
|
25027
|
+
return pattern.includes("index.{") || pattern.includes("*/index") || pattern.includes("**/index");
|
|
25028
|
+
}
|
|
25029
|
+
var PATTERN_ENTRY_EXTENSIONS = ["md", "mdx", "json", "yaml", "yml"];
|
|
25030
|
+
function entryExtensionOfPattern(pattern) {
|
|
25031
|
+
const fileName = pattern.slice(pattern.lastIndexOf("/") + 1);
|
|
25032
|
+
const match = fileName.match(/\.(?:\{([^{}]*)\}|([A-Za-z0-9]+))$/);
|
|
25033
|
+
if (!match)
|
|
25034
|
+
return "md";
|
|
25035
|
+
const named = new Set((match[1] ?? match[2] ?? "").split(",").map((ext) => ext.trim().replace(/^\./, "")));
|
|
25036
|
+
return PATTERN_ENTRY_EXTENSIONS.find((ext) => named.has(ext)) ?? null;
|
|
25006
25037
|
}
|
|
25007
25038
|
function applyParsedConfig(collections, parsed) {
|
|
25008
25039
|
for (const [collectionName, parsedColl] of parsed) {
|
|
@@ -25480,6 +25511,9 @@ function collectionScanReport(collections) {
|
|
|
25480
25511
|
return scanReports.get(collections);
|
|
25481
25512
|
}
|
|
25482
25513
|
var UNSUPPORTED_GLOB_SYNTAX = /[[\]()\\|]|\}\+|\{[^}]*(?:\{|\.\.|[*?]|,,)|\{,|,\}|\{[^,}]*\}|(?:^|[/{},])\.\.(?:[/{},]|$)|\/$|^(?:\.\/)+!|.\/\*\*$/;
|
|
25514
|
+
function portRunsLoaderPattern(pattern) {
|
|
25515
|
+
return !UNSUPPORTED_GLOB_SYNTAX.test(pattern) && !UNSUPPORTED_GLOB_SYNTAX.test(path5.posix.normalize(pattern));
|
|
25516
|
+
}
|
|
25483
25517
|
var LEAVES_THE_BASE = /(?:^|[/{},])\.\.(?:[/{},]|$)/;
|
|
25484
25518
|
var REPORT_LINKED_DIRECTORY_LIMIT = 20000;
|
|
25485
25519
|
var ENTRY_FILE = /\.(?:md|mdx|mdoc|markdown|mdown|mkdn|mkd|mdwn|json|ya?ml|toml)$/i;
|
|
@@ -25525,7 +25559,7 @@ async function declaredEntryFiles(fs3, contentDir, declaration) {
|
|
|
25525
25559
|
const patterns = pattern !== undefined ? [pattern] : declaration.loaderPatterns;
|
|
25526
25560
|
if (patterns === undefined)
|
|
25527
25561
|
return null;
|
|
25528
|
-
const exact = pattern !== undefined &&
|
|
25562
|
+
const exact = pattern !== undefined && portRunsLoaderPattern(pattern);
|
|
25529
25563
|
if (!exact && (base === "." || patterns.some((one) => LEAVES_THE_BASE.test(one))))
|
|
25530
25564
|
return null;
|
|
25531
25565
|
const matches = await fs3.glob(path5.join(base, exact ? pattern : "**/*"), {
|
|
@@ -26067,11 +26101,68 @@ async function versionOf(fs3, path7) {
|
|
|
26067
26101
|
return { kind: "present" };
|
|
26068
26102
|
}
|
|
26069
26103
|
}
|
|
26104
|
+
function changesetDigest(changeset, path7) {
|
|
26105
|
+
const target = normalize(path7);
|
|
26106
|
+
const changed = changeset.changes.findLast((change) => change.path === target);
|
|
26107
|
+
if (changed !== undefined)
|
|
26108
|
+
return changed.kind === "write" ? digestBytes(changed.content) : undefined;
|
|
26109
|
+
const read = changeset.reads.get(target);
|
|
26110
|
+
return read?.kind === "content" ? read.digest : undefined;
|
|
26111
|
+
}
|
|
26112
|
+
function recordVersions(base) {
|
|
26113
|
+
const versions = new Map;
|
|
26114
|
+
const encoder = new TextEncoder;
|
|
26115
|
+
return {
|
|
26116
|
+
readFile: (path7) => base.readFile(path7),
|
|
26117
|
+
async readBytes(path7) {
|
|
26118
|
+
const content = await base.readBytes(path7);
|
|
26119
|
+
const key = normalize(path7);
|
|
26120
|
+
if (!versions.has(key))
|
|
26121
|
+
versions.set(key, digestBytes(content));
|
|
26122
|
+
return content;
|
|
26123
|
+
},
|
|
26124
|
+
async writeFile(path7, content) {
|
|
26125
|
+
await base.writeFile(path7, content);
|
|
26126
|
+
versions.set(normalize(path7), digestBytes(encoder.encode(content)));
|
|
26127
|
+
},
|
|
26128
|
+
async writeBytes(path7, content) {
|
|
26129
|
+
await base.writeBytes(path7, content);
|
|
26130
|
+
versions.set(normalize(path7), digestBytes(content));
|
|
26131
|
+
},
|
|
26132
|
+
async remove(path7) {
|
|
26133
|
+
await base.remove(path7);
|
|
26134
|
+
versions.delete(normalize(path7));
|
|
26135
|
+
},
|
|
26136
|
+
async rename(rawFrom, rawTo) {
|
|
26137
|
+
await base.rename(rawFrom, rawTo);
|
|
26138
|
+
const from = normalize(rawFrom);
|
|
26139
|
+
const to = normalize(rawTo);
|
|
26140
|
+
for (const path7 of [...versions.keys()]) {
|
|
26141
|
+
if (path7 === to || isBelow(path7, to))
|
|
26142
|
+
versions.delete(path7);
|
|
26143
|
+
}
|
|
26144
|
+
for (const [path7, digest] of [...versions]) {
|
|
26145
|
+
if (path7 !== from && !isBelow(path7, from))
|
|
26146
|
+
continue;
|
|
26147
|
+
versions.delete(path7);
|
|
26148
|
+
versions.set(to + path7.slice(from.length), digest);
|
|
26149
|
+
}
|
|
26150
|
+
},
|
|
26151
|
+
exists: (path7) => base.exists(path7),
|
|
26152
|
+
list: (dir) => base.list(dir),
|
|
26153
|
+
glob: (pattern, options) => base.glob(pattern, options),
|
|
26154
|
+
stat: (path7) => base.stat(path7),
|
|
26155
|
+
digest(path7) {
|
|
26156
|
+
return versions.get(normalize(path7));
|
|
26157
|
+
}
|
|
26158
|
+
};
|
|
26159
|
+
}
|
|
26070
26160
|
|
|
26071
26161
|
// ../cms-core/src/handlers/entry-ops.ts
|
|
26072
26162
|
var import_yaml3 = __toESM(require_dist(), 1);
|
|
26073
26163
|
import path7 from "path";
|
|
26074
26164
|
var MARKDOWN_EXTENSIONS = ["md", "mdx"];
|
|
26165
|
+
var UTF8 = new TextDecoder("utf-8", { ignoreBOM: true });
|
|
26075
26166
|
function fileExtension(filePath) {
|
|
26076
26167
|
const idx = filePath.lastIndexOf(".");
|
|
26077
26168
|
return idx >= 0 ? filePath.slice(idx + 1).toLowerCase() : "";
|
|
@@ -26246,13 +26337,14 @@ async function inferLayoutFromExistingEntries(deps, collection) {
|
|
|
26246
26337
|
const dirEntries = await deps.fs.list(collectionPath);
|
|
26247
26338
|
if (dirEntries.length === 0)
|
|
26248
26339
|
return null;
|
|
26340
|
+
const loads = await loaderPatternTest(deps, collection);
|
|
26249
26341
|
let flatCount = 0;
|
|
26250
26342
|
const flatSlugs = new Set;
|
|
26251
26343
|
for (const entry of dirEntries) {
|
|
26252
26344
|
if (entry.isDirectory)
|
|
26253
26345
|
continue;
|
|
26254
26346
|
const match = entry.name.match(/^(.+)\.(md|mdx)$/);
|
|
26255
|
-
if (!match)
|
|
26347
|
+
if (!match || !loads(entry.name))
|
|
26256
26348
|
continue;
|
|
26257
26349
|
flatCount++;
|
|
26258
26350
|
flatSlugs.add(match[1]);
|
|
@@ -26262,7 +26354,8 @@ async function inferLayoutFromExistingEntries(deps, collection) {
|
|
|
26262
26354
|
if (flatSlugs.has(dir.name))
|
|
26263
26355
|
return false;
|
|
26264
26356
|
for (const ext of MARKDOWN_EXTENSIONS) {
|
|
26265
|
-
|
|
26357
|
+
const relPath = path7.posix.join(dir.name, `index.${ext}`);
|
|
26358
|
+
if (loads(relPath) && await deps.fs.exists(path7.posix.join(collectionPath, relPath)))
|
|
26266
26359
|
return true;
|
|
26267
26360
|
}
|
|
26268
26361
|
return false;
|
|
@@ -26274,6 +26367,13 @@ async function inferLayoutFromExistingEntries(deps, collection) {
|
|
|
26274
26367
|
return "flat";
|
|
26275
26368
|
return null;
|
|
26276
26369
|
}
|
|
26370
|
+
async function loaderPatternTest(deps, collection) {
|
|
26371
|
+
const pattern = (await parseContentConfig(deps.fs, deps.parseCache)).get(collection)?.loaderPattern;
|
|
26372
|
+
if (!pattern || !portRunsLoaderPattern(pattern))
|
|
26373
|
+
return () => true;
|
|
26374
|
+
const matcher = globToRegExp(path7.posix.normalize(pattern));
|
|
26375
|
+
return (relPath) => !isPrivateEntryPath(relPath) && matcher.test(relPath);
|
|
26376
|
+
}
|
|
26277
26377
|
async function inferLayoutFromContentConfig(deps, collection) {
|
|
26278
26378
|
const parsed = await parseContentConfig(deps.fs, deps.parseCache);
|
|
26279
26379
|
const pattern = parsed.get(collection)?.loaderPattern;
|
|
@@ -26281,9 +26381,6 @@ async function inferLayoutFromContentConfig(deps, collection) {
|
|
|
26281
26381
|
return null;
|
|
26282
26382
|
return isIndexStyleGlobPattern(pattern) ? "index" : "flat";
|
|
26283
26383
|
}
|
|
26284
|
-
function isIndexStyleGlobPattern(pattern) {
|
|
26285
|
-
return pattern.includes("index.{") || pattern.includes("*/index") || pattern.includes("**/index");
|
|
26286
|
-
}
|
|
26287
26384
|
async function applyDerivedFields(deps, collection, frontmatter, patch) {
|
|
26288
26385
|
const parsed = await parseContentConfig(deps.fs, deps.parseCache);
|
|
26289
26386
|
const parsedCollection = parsed.get(collection);
|
|
@@ -26330,17 +26427,20 @@ async function getEntry(deps, collection, slug) {
|
|
|
26330
26427
|
const sourcePath = await resolveEntryPath(deps, collection, slug);
|
|
26331
26428
|
if (!sourcePath)
|
|
26332
26429
|
return null;
|
|
26333
|
-
const
|
|
26430
|
+
const bytes = await deps.fs.readBytes(sourcePath);
|
|
26431
|
+
const raw = UTF8.decode(bytes);
|
|
26432
|
+
const sourceHash = digestBytes(bytes);
|
|
26334
26433
|
if (isDataFile(sourcePath)) {
|
|
26335
26434
|
const data = sourcePath.endsWith(".json") ? JSON.parse(raw) : import_yaml3.default.parse(raw);
|
|
26336
26435
|
return {
|
|
26337
26436
|
content: "",
|
|
26338
26437
|
frontmatter: data && typeof data === "object" ? data : {},
|
|
26339
|
-
sourcePath
|
|
26438
|
+
sourcePath,
|
|
26439
|
+
sourceHash
|
|
26340
26440
|
};
|
|
26341
26441
|
}
|
|
26342
26442
|
const { frontmatter, content } = parseFrontmatter2(raw);
|
|
26343
|
-
return { content, frontmatter, sourcePath };
|
|
26443
|
+
return { content, frontmatter, sourcePath, sourceHash };
|
|
26344
26444
|
}
|
|
26345
26445
|
async function createEntry(deps, input) {
|
|
26346
26446
|
const { collection, slug, body = "" } = input;
|
|
@@ -26446,6 +26546,11 @@ async function renameEntry(deps, collection, from, to) {
|
|
|
26446
26546
|
if (!normalizedSlug) {
|
|
26447
26547
|
return { success: false, error: "Invalid slug" };
|
|
26448
26548
|
}
|
|
26549
|
+
try {
|
|
26550
|
+
await deps.fs.readBytes(sourcePath);
|
|
26551
|
+
} catch {
|
|
26552
|
+
return { success: false, error: `Entry not found: ${collection}/${from}` };
|
|
26553
|
+
}
|
|
26449
26554
|
const { moveFrom, moveTo, previousSlug, newSourcePath } = renameTarget(sourcePath, from, normalizedSlug);
|
|
26450
26555
|
if (moveFrom === moveTo) {
|
|
26451
26556
|
return { success: true, sourcePath: newSourcePath };
|
|
@@ -26965,6 +27070,25 @@ function createCmsCore(fs3, opts = {}) {
|
|
|
26965
27070
|
await commit(staged.changeset());
|
|
26966
27071
|
return result;
|
|
26967
27072
|
}
|
|
27073
|
+
async function mutateEntry(operation) {
|
|
27074
|
+
let digestOf;
|
|
27075
|
+
let result;
|
|
27076
|
+
if (commit === undefined) {
|
|
27077
|
+
const recording = recordVersions(fs3);
|
|
27078
|
+
result = await operation(recording);
|
|
27079
|
+
digestOf = (path8) => recording.digest(path8);
|
|
27080
|
+
} else {
|
|
27081
|
+
const staged = createStagedFileSystem(fs3);
|
|
27082
|
+
result = await operation(staged);
|
|
27083
|
+
const changeset = staged.changeset();
|
|
27084
|
+
await commit(changeset);
|
|
27085
|
+
digestOf = (path8) => changesetDigest(changeset, path8);
|
|
27086
|
+
}
|
|
27087
|
+
if (!result.success || result.sourcePath === undefined)
|
|
27088
|
+
return result;
|
|
27089
|
+
const sourceHash = digestOf(result.sourcePath);
|
|
27090
|
+
return sourceHash === undefined ? result : { ...result, sourceHash };
|
|
27091
|
+
}
|
|
26968
27092
|
return {
|
|
26969
27093
|
scanCollections(options) {
|
|
26970
27094
|
return scanCollections(fs3, contentDir, parseCache, { report: options?.report ?? false, frontmatterCache });
|
|
@@ -26983,22 +27107,22 @@ function createCmsCore(fs3, opts = {}) {
|
|
|
26983
27107
|
return getProjectAsset(readDeps, assetPath);
|
|
26984
27108
|
},
|
|
26985
27109
|
createEntry(input) {
|
|
26986
|
-
return
|
|
27110
|
+
return mutateEntry((over) => createEntry(entryDeps(over), input));
|
|
26987
27111
|
},
|
|
26988
27112
|
updateEntry(input) {
|
|
26989
|
-
return
|
|
27113
|
+
return mutateEntry((over) => updateEntry(entryDeps(over), input));
|
|
26990
27114
|
},
|
|
26991
27115
|
deleteEntry(collection, slug) {
|
|
26992
|
-
return
|
|
27116
|
+
return mutateEntry((over) => deleteEntry(entryDeps(over), collection, slug));
|
|
26993
27117
|
},
|
|
26994
27118
|
renameEntry(collection, from, to) {
|
|
26995
|
-
return
|
|
27119
|
+
return mutateEntry((over) => renameEntry(entryDeps(over), collection, from, to));
|
|
26996
27120
|
},
|
|
26997
27121
|
addArrayItem(input) {
|
|
26998
|
-
return
|
|
27122
|
+
return mutateEntry((over) => addArrayItem(entryDeps(over), input));
|
|
26999
27123
|
},
|
|
27000
27124
|
removeArrayItem(input) {
|
|
27001
|
-
return
|
|
27125
|
+
return mutateEntry((over) => removeArrayItem(entryDeps(over), input));
|
|
27002
27126
|
},
|
|
27003
27127
|
createPage(input) {
|
|
27004
27128
|
return mutate((over) => createPage({ fs: over }, input));
|
|
@@ -27519,15 +27643,10 @@ function mediaFromEnv(env = process.env) {
|
|
|
27519
27643
|
}
|
|
27520
27644
|
|
|
27521
27645
|
// src/concurrency.ts
|
|
27522
|
-
import { createHash as createHash2 } from "crypto";
|
|
27523
|
-
function hashContent(content) {
|
|
27524
|
-
return `sha256:${createHash2("sha256").update(content, "utf-8").digest("hex")}`;
|
|
27525
|
-
}
|
|
27526
27646
|
async function hashSource(fs3, sourcePath) {
|
|
27527
27647
|
if (!await fs3.exists(sourcePath))
|
|
27528
27648
|
return null;
|
|
27529
|
-
|
|
27530
|
-
return hashContent(raw);
|
|
27649
|
+
return digestBytes(await fs3.readBytes(sourcePath));
|
|
27531
27650
|
}
|
|
27532
27651
|
|
|
27533
27652
|
class SharedRun {
|
|
@@ -27652,6 +27771,23 @@ async function parseJson(req) {
|
|
|
27652
27771
|
return { ok: false, response: error("validation", "Invalid JSON body") };
|
|
27653
27772
|
}
|
|
27654
27773
|
}
|
|
27774
|
+
async function parseOptionalJson(req) {
|
|
27775
|
+
const text = await req.text();
|
|
27776
|
+
if (text.trim() === "")
|
|
27777
|
+
return { ok: true, value: undefined };
|
|
27778
|
+
try {
|
|
27779
|
+
const value = JSON.parse(text);
|
|
27780
|
+
return { ok: true, value };
|
|
27781
|
+
} catch {
|
|
27782
|
+
return { ok: false, response: error("validation", "Invalid JSON body") };
|
|
27783
|
+
}
|
|
27784
|
+
}
|
|
27785
|
+
function isOptionalHash(value) {
|
|
27786
|
+
return value === undefined || typeof value === "string" && value !== "";
|
|
27787
|
+
}
|
|
27788
|
+
function isBodyObject(value) {
|
|
27789
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
27790
|
+
}
|
|
27655
27791
|
function parseEntriesQuery(url) {
|
|
27656
27792
|
const fields = url.searchParams.get("fields") ?? undefined;
|
|
27657
27793
|
const rawDraft = url.searchParams.get("draft");
|
|
@@ -27969,7 +28105,8 @@ function createServer(opts) {
|
|
|
27969
28105
|
sourcePath: result.sourcePath,
|
|
27970
28106
|
frontmatter,
|
|
27971
28107
|
body: result.content,
|
|
27972
|
-
bodyStartLine: 0
|
|
28108
|
+
bodyStartLine: 0,
|
|
28109
|
+
sourceHash: result.sourceHash
|
|
27973
28110
|
};
|
|
27974
28111
|
return json(entry);
|
|
27975
28112
|
}
|
|
@@ -27996,23 +28133,9 @@ function createServer(opts) {
|
|
|
27996
28133
|
if (!existing)
|
|
27997
28134
|
return error("not_found", `Entry not found: ${collection}/${slug}`);
|
|
27998
28135
|
const sourcePath = existing.sourcePath;
|
|
27999
|
-
const existingFrontmatter = existing.frontmatter;
|
|
28000
|
-
async function conflictResponse(serverHash) {
|
|
28001
|
-
const current = await core.getEntry(collection, slug);
|
|
28002
|
-
const conflict = {
|
|
28003
|
-
code: "conflict",
|
|
28004
|
-
serverHash,
|
|
28005
|
-
serverFrontmatter: current?.frontmatter ?? existingFrontmatter
|
|
28006
|
-
};
|
|
28007
|
-
if (current && current.content !== "")
|
|
28008
|
-
conflict.serverBody = current.content;
|
|
28009
|
-
return json(conflict, STATUS_BY_CODE.conflict);
|
|
28010
|
-
}
|
|
28011
28136
|
return mutex.runExclusive(sourcePath, async () => {
|
|
28012
|
-
|
|
28013
|
-
|
|
28014
|
-
return conflictResponse(serverHash);
|
|
28015
|
-
}
|
|
28137
|
+
if (await isStale(sourcePath, body.baseHash))
|
|
28138
|
+
return conflictResponse(collection, slug);
|
|
28016
28139
|
let result;
|
|
28017
28140
|
try {
|
|
28018
28141
|
result = await core.updateEntry({
|
|
@@ -28024,20 +28147,32 @@ function createServer(opts) {
|
|
|
28024
28147
|
} catch (cause) {
|
|
28025
28148
|
if (!(cause instanceof CmsCommitConflictError))
|
|
28026
28149
|
throw cause;
|
|
28027
|
-
|
|
28028
|
-
if (currentHash === null)
|
|
28150
|
+
if (await hashSource(fs3, sourcePath) === null)
|
|
28029
28151
|
throw cause;
|
|
28030
|
-
return conflictResponse(
|
|
28031
|
-
}
|
|
28032
|
-
|
|
28033
|
-
return mutationResponse(result);
|
|
28034
|
-
const newHash = await hashSource(fs3, result.sourcePath ?? sourcePath);
|
|
28035
|
-
const enriched = { ...result };
|
|
28036
|
-
if (newHash !== null)
|
|
28037
|
-
enriched.sourceHash = newHash;
|
|
28038
|
-
return json(enriched);
|
|
28152
|
+
return conflictResponse(collection, slug);
|
|
28153
|
+
}
|
|
28154
|
+
return mutationResponse(result);
|
|
28039
28155
|
});
|
|
28040
28156
|
}
|
|
28157
|
+
async function isStale(sourcePath, baseHash) {
|
|
28158
|
+
if (baseHash === undefined)
|
|
28159
|
+
return false;
|
|
28160
|
+
const serverHash = await hashSource(fs3, sourcePath);
|
|
28161
|
+
return serverHash !== null && serverHash !== baseHash;
|
|
28162
|
+
}
|
|
28163
|
+
async function conflictResponse(collection, slug) {
|
|
28164
|
+
const current = await core.getEntry(collection, slug);
|
|
28165
|
+
if (current === null)
|
|
28166
|
+
return error("conflict", `Entry changed and was removed meanwhile: ${collection}/${slug}`);
|
|
28167
|
+
const conflict = {
|
|
28168
|
+
code: "conflict",
|
|
28169
|
+
serverHash: current.sourceHash,
|
|
28170
|
+
serverFrontmatter: current.frontmatter
|
|
28171
|
+
};
|
|
28172
|
+
if (current.content !== "")
|
|
28173
|
+
conflict.serverBody = current.content;
|
|
28174
|
+
return json(conflict, STATUS_BY_CODE.conflict);
|
|
28175
|
+
}
|
|
28041
28176
|
async function fetchHandler(req) {
|
|
28042
28177
|
const url = new URL(req.url);
|
|
28043
28178
|
let pathname = url.pathname;
|
|
@@ -28133,7 +28268,7 @@ function createServer(opts) {
|
|
|
28133
28268
|
if (method === "PATCH")
|
|
28134
28269
|
return updateEntryRoute(collection, slug, req);
|
|
28135
28270
|
if (method === "DELETE")
|
|
28136
|
-
return deleteEntryRoute(collection, slug);
|
|
28271
|
+
return deleteEntryRoute(collection, slug, req);
|
|
28137
28272
|
return error("unsupported", `Unsupported: ${method} /collections/${collection}/entries/${slug}`);
|
|
28138
28273
|
}
|
|
28139
28274
|
if (action === "rename" && method === "POST")
|
|
@@ -28184,13 +28319,7 @@ function createServer(opts) {
|
|
|
28184
28319
|
body: body.body,
|
|
28185
28320
|
fileExtension: body.fileExtension
|
|
28186
28321
|
});
|
|
28187
|
-
|
|
28188
|
-
return mutationResponse(result);
|
|
28189
|
-
const newHash = result.sourcePath ? await hashSource(fs3, result.sourcePath) : null;
|
|
28190
|
-
const enriched = { ...result };
|
|
28191
|
-
if (newHash !== null)
|
|
28192
|
-
enriched.sourceHash = newHash;
|
|
28193
|
-
return json(enriched);
|
|
28322
|
+
return mutationResponse(result);
|
|
28194
28323
|
}
|
|
28195
28324
|
async function updateEntryRoute(collection, slug, req) {
|
|
28196
28325
|
const parsed = await parseJson(req);
|
|
@@ -28198,11 +28327,20 @@ function createServer(opts) {
|
|
|
28198
28327
|
return parsed.response;
|
|
28199
28328
|
return patchEntry(collection, slug, parsed.value);
|
|
28200
28329
|
}
|
|
28201
|
-
async function deleteEntryRoute(collection, slug) {
|
|
28330
|
+
async function deleteEntryRoute(collection, slug, req) {
|
|
28331
|
+
const parsed = await parseOptionalJson(req);
|
|
28332
|
+
if (!parsed.ok)
|
|
28333
|
+
return parsed.response;
|
|
28334
|
+
if (parsed.value !== undefined && !isBodyObject(parsed.value))
|
|
28335
|
+
return error("validation", "The body must be a JSON object");
|
|
28336
|
+
if (!isOptionalHash(parsed.value?.baseHash))
|
|
28337
|
+
return error("validation", '"baseHash" must be a non-empty string');
|
|
28202
28338
|
const existing = await core.getEntry(collection, slug);
|
|
28203
28339
|
if (!existing)
|
|
28204
28340
|
return error("not_found", `Entry not found: ${collection}/${slug}`);
|
|
28205
28341
|
return mutex.runExclusive(existing.sourcePath, async () => {
|
|
28342
|
+
if (await isStale(existing.sourcePath, parsed.value?.baseHash))
|
|
28343
|
+
return conflictResponse(collection, slug);
|
|
28206
28344
|
const result = await core.deleteEntry(collection, slug);
|
|
28207
28345
|
return mutationResponse(result);
|
|
28208
28346
|
});
|
|
@@ -28214,18 +28352,15 @@ function createServer(opts) {
|
|
|
28214
28352
|
if (typeof parsed.value.to !== "string" || parsed.value.to.trim() === "") {
|
|
28215
28353
|
return error("validation", 'A non-empty "to" slug is required');
|
|
28216
28354
|
}
|
|
28355
|
+
if (!isOptionalHash(parsed.value.baseHash))
|
|
28356
|
+
return error("validation", '"baseHash" must be a non-empty string');
|
|
28217
28357
|
const existing = await core.getEntry(collection, slug);
|
|
28218
28358
|
if (!existing)
|
|
28219
28359
|
return error("not_found", `Entry not found: ${collection}/${slug}`);
|
|
28220
28360
|
return mutex.runExclusive(existing.sourcePath, async () => {
|
|
28221
|
-
|
|
28222
|
-
|
|
28223
|
-
|
|
28224
|
-
const newHash = result.sourcePath ? await hashSource(fs3, result.sourcePath) : null;
|
|
28225
|
-
const enriched = { ...result };
|
|
28226
|
-
if (newHash !== null)
|
|
28227
|
-
enriched.sourceHash = newHash;
|
|
28228
|
-
return json(enriched);
|
|
28361
|
+
if (await isStale(existing.sourcePath, parsed.value.baseHash))
|
|
28362
|
+
return conflictResponse(collection, slug);
|
|
28363
|
+
return mutationResponse(await core.renameEntry(collection, slug, parsed.value.to));
|
|
28229
28364
|
});
|
|
28230
28365
|
}
|
|
28231
28366
|
async function addArrayRoute(collection, slug, req) {
|
package/dist/concurrency.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type CmsFileSystem } from '@nuasite/cms-core';
|
|
2
2
|
/**
|
|
3
3
|
* Hashing, per-file serialization and shared reads for the sidecar layer.
|
|
4
4
|
*
|
|
@@ -7,11 +7,15 @@ import type { CmsFileSystem } from '@nuasite/cms-core';
|
|
|
7
7
|
* through the same `CmsFileSystem` port cms-core uses, so the hash reflects exactly
|
|
8
8
|
* the bytes a subsequent `getEntry` would parse.
|
|
9
9
|
*/
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Stable content hash of a UTF-8 string: `sha256:<hex>`. Equal to the `sourceHash` of a file only when
|
|
12
|
+
* the file is valid UTF-8 that decodes to `content`; the server hashes the bytes (`hashSource`).
|
|
13
|
+
*/
|
|
11
14
|
export declare function hashContent(content: string): string;
|
|
12
15
|
/**
|
|
13
16
|
* Hash the current on-disk source at `sourcePath`, or `null` when the file does
|
|
14
|
-
* not exist.
|
|
17
|
+
* not exist. Hashes the bytes, as cms-core does for a read and a write, so the
|
|
18
|
+
* values compare exactly.
|
|
15
19
|
*/
|
|
16
20
|
export declare function hashSource(fs: CmsFileSystem, sourcePath: string): Promise<string | null>;
|
|
17
21
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"concurrency.d.ts","sourceRoot":"","sources":["../src/concurrency.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"concurrency.d.ts","sourceRoot":"","sources":["../src/concurrency.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAe,MAAM,mBAAmB,CAAA;AAGnE;;;;;;;GAOG;AAEH;;;GAGG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED;;;;GAIG;AACH,wBAAsB,UAAU,CAAC,EAAE,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAG9F;AAED;;;;GAIG;AACH,qBAAa,SAAS,CAAC,CAAC;IACvB,OAAO,CAAC,OAAO,CAA2D;IAC1E,OAAO,CAAC,UAAU,CAAI;IAEtB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAkB;gBAE1B,GAAG,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC;IAIjC,GAAG,IAAI,OAAO,CAAC,CAAC,CAAC;IAWjB,UAAU,IAAI,IAAI;CAGlB;AAED;;;;GAIG;AACH,qBAAa,UAAU;IACtB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAsC;IAEtD,YAAY,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAetE"}
|
package/dist/concurrency.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { digestBytes } from '@nuasite/cms-core';
|
|
1
2
|
import { createHash } from 'node:crypto';
|
|
2
3
|
/**
|
|
3
4
|
* Hashing, per-file serialization and shared reads for the sidecar layer.
|
|
@@ -7,19 +8,22 @@ import { createHash } from 'node:crypto';
|
|
|
7
8
|
* through the same `CmsFileSystem` port cms-core uses, so the hash reflects exactly
|
|
8
9
|
* the bytes a subsequent `getEntry` would parse.
|
|
9
10
|
*/
|
|
10
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* Stable content hash of a UTF-8 string: `sha256:<hex>`. Equal to the `sourceHash` of a file only when
|
|
13
|
+
* the file is valid UTF-8 that decodes to `content`; the server hashes the bytes (`hashSource`).
|
|
14
|
+
*/
|
|
11
15
|
export function hashContent(content) {
|
|
12
16
|
return `sha256:${createHash('sha256').update(content, 'utf-8').digest('hex')}`;
|
|
13
17
|
}
|
|
14
18
|
/**
|
|
15
19
|
* Hash the current on-disk source at `sourcePath`, or `null` when the file does
|
|
16
|
-
* not exist.
|
|
20
|
+
* not exist. Hashes the bytes, as cms-core does for a read and a write, so the
|
|
21
|
+
* values compare exactly.
|
|
17
22
|
*/
|
|
18
23
|
export async function hashSource(fs, sourcePath) {
|
|
19
24
|
if (!(await fs.exists(sourcePath)))
|
|
20
25
|
return null;
|
|
21
|
-
|
|
22
|
-
return hashContent(raw);
|
|
26
|
+
return digestBytes(await fs.readBytes(sourcePath));
|
|
23
27
|
}
|
|
24
28
|
/**
|
|
25
29
|
* One run of an expensive read at a time, shared by every caller that asks while it is in flight.
|
package/dist/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,OAAO,EACZ,KAAK,aAAa,EAiBlB,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,OAAO,EACZ,KAAK,aAAa,EAiBlB,MAAM,mBAAmB,CAAA;AAoC1B,qFAAqF;AACrF,eAAO,MAAM,gBAAgB,EAAE,SAAS,MAAM,EAoB7C,CAAA;AAMD,MAAM,WAAW,mBAAmB;IACnC,IAAI,EAAE,OAAO,CAAA;IACb,kGAAkG;IAClG,EAAE,EAAE,aAAa,CAAA;IACjB,+DAA+D;IAC/D,IAAI,EAAE,MAAM,CAAA;IACZ,wEAAwE;IACxE,WAAW,EAAE,MAAM,CAAA;IACnB,kFAAkF;IAClF,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,6DAA6D;IAC7D,aAAa,CAAC,EAAE,MAAM,CAAA;CACtB;AAohBD,MAAM,WAAW,gBAAgB;IAChC,6EAA6E;IAC7E,KAAK,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;CACtC;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,mBAAmB,GAAG,gBAAgB,CA+nBxE"}
|