@conloca/content-api 0.1.16 → 0.2.1
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/asset-constraints.d.ts +16 -0
- package/dist/asset-constraints.d.ts.map +1 -0
- package/dist/asset-constraints.js +29 -0
- package/dist/asset-constraints.js.map +1 -0
- package/dist/asset-metadata-store.d.ts +60 -0
- package/dist/asset-metadata-store.d.ts.map +1 -0
- package/dist/asset-metadata-store.js +335 -0
- package/dist/asset-metadata-store.js.map +1 -0
- package/dist/asset-operations.d.ts +21 -28
- package/dist/asset-operations.d.ts.map +1 -1
- package/dist/asset-operations.js +268 -305
- package/dist/asset-operations.js.map +1 -0
- package/dist/asset-types.d.ts +23 -7
- package/dist/asset-types.d.ts.map +1 -1
- package/dist/asset-types.js +8 -2
- package/dist/asset-types.js.map +1 -0
- package/dist/asset-usage.d.ts +11 -0
- package/dist/asset-usage.d.ts.map +1 -0
- package/dist/asset-usage.js +50 -0
- package/dist/asset-usage.js.map +1 -0
- package/dist/base-content-index.d.ts +8 -2
- package/dist/base-content-index.d.ts.map +1 -1
- package/dist/base-content-index.js +24 -3
- package/dist/base-content-index.js.map +1 -0
- package/dist/cf-access.js +1 -0
- package/dist/cf-access.js.map +1 -0
- package/dist/component-registry.d.ts +1 -1
- package/dist/component-registry.d.ts.map +1 -1
- package/dist/component-registry.js +2 -1
- package/dist/component-registry.js.map +1 -0
- package/dist/component-registry.types.js +1 -0
- package/dist/component-registry.types.js.map +1 -0
- package/dist/component-vocabulary.d.ts +33 -0
- package/dist/component-vocabulary.d.ts.map +1 -0
- package/dist/component-vocabulary.js +105 -0
- package/dist/component-vocabulary.js.map +1 -0
- package/dist/content-api.interface.d.ts +5 -5
- package/dist/content-api.interface.d.ts.map +1 -1
- package/dist/content-api.interface.js +1 -0
- package/dist/content-api.interface.js.map +1 -0
- package/dist/content-filters.d.ts +2 -2
- package/dist/content-filters.d.ts.map +1 -1
- package/dist/content-filters.js +9 -8
- package/dist/content-filters.js.map +1 -0
- package/dist/content-index.d.ts +74 -16
- package/dist/content-index.d.ts.map +1 -1
- package/dist/content-index.js +183 -55
- package/dist/content-index.js.map +1 -0
- package/dist/content-operations.d.ts +71 -7
- package/dist/content-operations.d.ts.map +1 -1
- package/dist/content-operations.js +166 -82
- package/dist/content-operations.js.map +1 -0
- package/dist/content-path.d.ts +64 -0
- package/dist/content-path.d.ts.map +1 -0
- package/dist/content-path.js +95 -0
- package/dist/content-path.js.map +1 -0
- package/dist/content-reader.d.ts +2 -2
- package/dist/content-reader.d.ts.map +1 -1
- package/dist/content-reader.js +2 -1
- package/dist/content-reader.js.map +1 -0
- package/dist/content-utils.d.ts +53 -10
- package/dist/content-utils.d.ts.map +1 -1
- package/dist/content-utils.js +103 -34
- package/dist/content-utils.js.map +1 -0
- package/dist/content-watcher.d.ts +3 -3
- package/dist/content-watcher.d.ts.map +1 -1
- package/dist/content-watcher.js +13 -10
- package/dist/content-watcher.js.map +1 -0
- package/dist/data-index.d.ts +1 -1
- package/dist/data-index.d.ts.map +1 -1
- package/dist/data-index.js +2 -1
- package/dist/data-index.js.map +1 -0
- package/dist/data.d.ts +5 -13
- package/dist/data.d.ts.map +1 -1
- package/dist/data.js +7 -6
- package/dist/data.js.map +1 -0
- package/dist/editor-data-bindings.d.ts +10 -0
- package/dist/editor-data-bindings.d.ts.map +1 -0
- package/dist/editor-data-bindings.js +67 -0
- package/dist/editor-data-bindings.js.map +1 -0
- package/dist/etag-utils.d.ts.map +1 -1
- package/dist/etag-utils.js +5 -4
- package/dist/etag-utils.js.map +1 -0
- package/dist/filesystem-content-api.d.ts +45 -18
- package/dist/filesystem-content-api.d.ts.map +1 -1
- package/dist/filesystem-content-api.js +455 -286
- package/dist/filesystem-content-api.js.map +1 -0
- package/dist/filesystem-reader-backend.d.ts +46 -0
- package/dist/filesystem-reader-backend.d.ts.map +1 -0
- package/dist/filesystem-reader-backend.js +119 -0
- package/dist/filesystem-reader-backend.js.map +1 -0
- package/dist/fragment-index.d.ts +8 -0
- package/dist/fragment-index.d.ts.map +1 -0
- package/dist/fragment-index.js +8 -0
- package/dist/fragment-index.js.map +1 -0
- package/dist/fragments.d.ts +16 -0
- package/dist/fragments.d.ts.map +1 -0
- package/dist/fragments.js +104 -0
- package/dist/fragments.js.map +1 -0
- package/dist/git-operations.d.ts.map +1 -1
- package/dist/git-operations.js +5 -1
- package/dist/git-operations.js.map +1 -0
- package/dist/header-parser.d.ts +4 -4
- package/dist/header-parser.d.ts.map +1 -1
- package/dist/header-parser.js +8 -61
- package/dist/header-parser.js.map +1 -0
- package/dist/in-memory-content-api.d.ts +7 -7
- package/dist/in-memory-content-api.d.ts.map +1 -1
- package/dist/in-memory-content-api.js +74 -56
- package/dist/in-memory-content-api.js.map +1 -0
- package/dist/index.d.ts +19 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -9
- package/dist/index.js.map +1 -0
- package/dist/indexing.d.ts +4 -4
- package/dist/indexing.d.ts.map +1 -1
- package/dist/indexing.js +6 -5
- package/dist/indexing.js.map +1 -0
- package/dist/json-whitespace-view.js +1 -0
- package/dist/json-whitespace-view.js.map +1 -0
- package/dist/middleware.d.ts +8 -6
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +178 -142
- package/dist/middleware.js.map +1 -0
- package/dist/node.d.ts +24 -17
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +17 -14
- package/dist/node.js.map +1 -0
- package/dist/page-references.d.ts +21 -0
- package/dist/page-references.d.ts.map +1 -0
- package/dist/page-references.js +50 -0
- package/dist/page-references.js.map +1 -0
- package/dist/read-date.d.ts +3 -0
- package/dist/read-date.d.ts.map +1 -0
- package/dist/read-date.js +9 -0
- package/dist/read-date.js.map +1 -0
- package/dist/reader-backend.d.ts +54 -0
- package/dist/reader-backend.d.ts.map +1 -0
- package/dist/reader-backend.js +2 -0
- package/dist/reader-backend.js.map +1 -0
- package/dist/reader.d.ts +8 -8
- package/dist/reader.d.ts.map +1 -1
- package/dist/reader.js +8 -7
- package/dist/reader.js.map +1 -0
- package/dist/schemas/index.d.ts +6 -6
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +9 -8
- package/dist/schemas/index.js.map +1 -0
- package/dist/site-index.d.ts +3 -3
- package/dist/site-index.d.ts.map +1 -1
- package/dist/site-index.js +9 -5
- package/dist/site-index.js.map +1 -0
- package/dist/site.d.ts +5 -3
- package/dist/site.d.ts.map +1 -1
- package/dist/site.js +14 -2
- package/dist/site.js.map +1 -0
- package/dist/types.d.ts +203 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/url-safe-base64.js +1 -0
- package/dist/url-safe-base64.js.map +1 -0
- package/dist/url-validation.d.ts +14 -0
- package/dist/url-validation.d.ts.map +1 -1
- package/dist/url-validation.js +56 -14
- package/dist/url-validation.js.map +1 -0
- package/dist/utils/atomic-write.js +1 -0
- package/dist/utils/atomic-write.js.map +1 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +2 -1
- package/dist/utils.js.map +1 -0
- package/dist/vite-plugin.d.ts +2 -2
- package/dist/vite-plugin.js +5 -4
- package/dist/vite-plugin.js.map +1 -0
- package/dist/vxjson-serialize.d.ts +5 -6
- package/dist/vxjson-serialize.d.ts.map +1 -1
- package/dist/vxjson-serialize.js +13 -10
- package/dist/vxjson-serialize.js.map +1 -0
- package/dist/vxjson.d.ts +4 -3
- package/dist/vxjson.d.ts.map +1 -1
- package/dist/vxjson.js +10 -20
- package/dist/vxjson.js.map +1 -0
- package/dist/xxhash.d.ts.map +1 -1
- package/dist/xxhash.js +6 -2
- package/dist/xxhash.js.map +1 -0
- package/package.json +92 -56
- package/src/asset-constraints.ts +33 -0
- package/src/asset-metadata-store.ts +401 -0
- package/src/asset-operations.ts +814 -0
- package/src/asset-types.ts +74 -0
- package/src/asset-usage.ts +48 -0
- package/src/base-content-index.ts +227 -0
- package/src/cf-access.ts +85 -0
- package/src/component-registry.ts +145 -0
- package/src/component-registry.types.ts +12 -0
- package/src/component-vocabulary.ts +87 -0
- package/src/content-api.interface.ts +94 -0
- package/src/content-filters.ts +278 -0
- package/src/content-index.ts +642 -0
- package/src/content-operations.ts +497 -0
- package/src/content-path.ts +121 -0
- package/src/content-reader.ts +18 -0
- package/src/content-utils.ts +474 -0
- package/src/content-watcher.ts +122 -0
- package/src/data-index.ts +7 -0
- package/src/data.ts +134 -0
- package/src/editor-data-bindings.ts +66 -0
- package/src/etag-utils.ts +204 -0
- package/src/filesystem-content-api.ts +2740 -0
- package/src/filesystem-reader-backend.ts +135 -0
- package/src/fragment-index.ts +7 -0
- package/src/fragments.ts +113 -0
- package/src/git-operations.ts +340 -0
- package/src/header-parser.ts +272 -0
- package/src/in-memory-content-api.ts +760 -0
- package/src/index.ts +45 -0
- package/src/indexing.ts +8 -0
- package/src/json-whitespace-view.ts +155 -0
- package/src/middleware.ts +1322 -0
- package/src/node.ts +40 -0
- package/src/page-references.ts +76 -0
- package/src/read-date.ts +7 -0
- package/src/reader-backend.ts +54 -0
- package/src/reader.ts +15 -0
- package/src/schemas/index.ts +113 -0
- package/src/site-index.ts +330 -0
- package/src/site.ts +198 -0
- package/src/types.ts +759 -0
- package/src/url-safe-base64.ts +38 -0
- package/src/url-validation.ts +121 -0
- package/src/utils/atomic-write.ts +20 -0
- package/src/utils.ts +84 -0
- package/src/vite-plugin.ts +45 -0
- package/src/vxjson-serialize.ts +66 -0
- package/src/vxjson.ts +442 -0
- package/src/xxhash.ts +43 -0
- package/dist/asset-manifest.d.ts +0 -23
- package/dist/asset-manifest.d.ts.map +0 -1
- package/dist/asset-manifest.js +0 -73
- package/dist/block-index.d.ts +0 -9
- package/dist/block-index.d.ts.map +0 -1
- package/dist/block-index.js +0 -11
- package/dist/blocks.d.ts +0 -16
- package/dist/blocks.d.ts.map +0 -1
- package/dist/blocks.js +0 -103
- package/dist/component-registry.types-C0ctF9nh.d.mts +0 -71
- package/dist/component-registry.types-C0ctF9nh.d.mts.map +0 -1
- package/dist/content-index-D092_vk1.mjs +0 -629
- package/dist/content-index-D092_vk1.mjs.map +0 -1
- package/dist/content-index-D6IVMmAB.d.mts +0 -127
- package/dist/content-index-D6IVMmAB.d.mts.map +0 -1
- package/dist/content-operations-DIbsxG5A.d.mts +0 -354
- package/dist/content-operations-DIbsxG5A.d.mts.map +0 -1
- package/dist/content-utils-fWNHTrPy.mjs +0 -280
- package/dist/content-utils-fWNHTrPy.mjs.map +0 -1
- package/dist/data-DquNIEnQ.mjs +0 -62
- package/dist/data-DquNIEnQ.mjs.map +0 -1
- package/dist/filesystem-content-api-B1KIQJfk.mjs +0 -2
- package/dist/filesystem-content-api-C0HIhqZi.mjs +0 -1620
- package/dist/filesystem-content-api-C0HIhqZi.mjs.map +0 -1
- package/dist/filesystem-content-api-DCdNi-co.d.mts +0 -134
- package/dist/filesystem-content-api-DCdNi-co.d.mts.map +0 -1
- package/dist/index-DxVTFK66.d.mts +0 -105
- package/dist/index-DxVTFK66.d.mts.map +0 -1
- package/dist/index.d.mts +0 -80
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -78
- package/dist/index.mjs.map +0 -1
- package/dist/indexing.d.mts +0 -152
- package/dist/indexing.d.mts.map +0 -1
- package/dist/indexing.mjs +0 -255
- package/dist/indexing.mjs.map +0 -1
- package/dist/node.d.mts +0 -373
- package/dist/node.d.mts.map +0 -1
- package/dist/node.mjs +0 -2572
- package/dist/node.mjs.map +0 -1
- package/dist/reader.d.mts +0 -15
- package/dist/reader.d.mts.map +0 -1
- package/dist/reader.mjs +0 -22
- package/dist/reader.mjs.map +0 -1
- package/dist/schemas/index.d.mts +0 -2
- package/dist/schemas/index.mjs +0 -2
- package/dist/schemas-CFRD0FIQ.mjs +0 -84
- package/dist/schemas-CFRD0FIQ.mjs.map +0 -1
- package/dist/site-8oaj1Q60.mjs +0 -366
- package/dist/site-8oaj1Q60.mjs.map +0 -1
- package/dist/site-index-B_254bCc.d.mts +0 -136
- package/dist/site-index-B_254bCc.d.mts.map +0 -1
- package/dist/types-DOdldbfD.mjs +0 -34
- package/dist/types-DOdldbfD.mjs.map +0 -1
- package/dist/types-DjfhS272.d.mts +0 -456
- package/dist/types-DjfhS272.d.mts.map +0 -1
- package/dist/vxjson-qHrgLGip.mjs +0 -555
- package/dist/vxjson-qHrgLGip.mjs.map +0 -1
- package/dist/vxjson-serialize-geF6qvpn.mjs +0 -34
- package/dist/vxjson-serialize-geF6qvpn.mjs.map +0 -1
- package/dist/vxjson-serialize.d.mts +0 -17
- package/dist/vxjson-serialize.d.mts.map +0 -1
- package/dist/vxjson-serialize.mjs +0 -2
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import { mkdir, open, readdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
|
2
|
-
import {
|
|
1
|
+
import { access, mkdir, open, readdir, readFile, rename, rm, stat, unlink, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join, resolve } from 'node:path';
|
|
3
3
|
import matter from 'gray-matter';
|
|
4
|
-
import { dirname, join, resolve } from 'path';
|
|
5
4
|
import sortKeys from 'sort-keys';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { localesOf, mapSiteNames, resolvePublishDate, serializeMdxWithFrontmatter } from './content-utils';
|
|
11
|
-
import { Data } from './data';
|
|
12
|
-
import { calculateEtagsFromMdxBuffer, findMdxContentStartPosition, parseDualEtag } from './etag-utils';
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
5
|
+
import { filterContentWithIndexes } from './content-filters.js';
|
|
6
|
+
import { ContentIndex, duplicateIdMessage, unknownSiteMessage } from './content-index.js';
|
|
7
|
+
import { analyzeUpdateScope, buildLocaleVersion, buildPathnameHistory, checkEntryName, checkMdxPagesPathname, checkPagePathname, generateContentId, isContentPublished, shouldUpdateModifiedTimestamp, UPDATE_SCOPE, updatePublishDates, validateAndPrepareMetadata, validateCreateContent, } from './content-operations.js';
|
|
8
|
+
import { invalidLocaleMessage, isContentLocale, parseContentPathLayout, parseLocaleFilename, } from './content-path.js';
|
|
9
|
+
import { applyMetaPatch, localesOf, mapSiteNames, resolvePublishDate, serializeMdxWithFrontmatter, } from './content-utils.js';
|
|
10
|
+
import { Data } from './data.js';
|
|
11
|
+
import { calculateEtagsFromMdxBuffer, findMdxContentStartPosition, parseDualEtag } from './etag-utils.js';
|
|
12
|
+
import { Fragments } from './fragments.js';
|
|
13
|
+
import { Site } from './site.js';
|
|
14
|
+
import { atomicWriteFile } from './utils/atomic-write.js';
|
|
15
|
+
import { xxh64 } from './xxhash.js';
|
|
16
|
+
import { VXJSON } from './vxjson.js';
|
|
16
17
|
// Helper to ensure we have an Error object
|
|
17
18
|
function ensureError(error) {
|
|
18
19
|
if (error instanceof Error) {
|
|
@@ -22,10 +23,29 @@ function ensureError(error) {
|
|
|
22
23
|
}
|
|
23
24
|
// Reusable TextDecoder instance - safe to reuse as it's stateless
|
|
24
25
|
const textDecoder = new TextDecoder();
|
|
26
|
+
/**
|
|
27
|
+
* Where the read repair moves a fragment saved straight into fragments/: the
|
|
28
|
+
* folder the editor saves new fragments to, which every conlocaFragments(...)
|
|
29
|
+
* call in a site's content config declares, so the moved fragment still builds.
|
|
30
|
+
*/
|
|
31
|
+
const REPAIRED_FRAGMENT_COLLECTION = 'fragments';
|
|
32
|
+
async function readFilePrefix(filePath, buffer) {
|
|
33
|
+
const handle = await open(filePath, 'r');
|
|
34
|
+
try {
|
|
35
|
+
return (await handle.read(buffer, 0, buffer.byteLength, 0)).bytesRead;
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
await handle.close();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
25
41
|
// Parse 4KB MDX content - extracts frontmatter from buffer
|
|
26
42
|
function parse4KBMDX(buffer, bytesRead) {
|
|
27
43
|
// Only convert to string what we need
|
|
28
44
|
const partialContent = textDecoder.decode(buffer.subarray(0, bytesRead));
|
|
45
|
+
// Empty options opt out of gray-matter's module-level memo cache — a plain
|
|
46
|
+
// object keyed by the raw input, so inputs naming an Object.prototype member
|
|
47
|
+
// ('valueOf', 'toString', …) return the inherited member instead of a parse.
|
|
48
|
+
// Same at every matter() call below.
|
|
29
49
|
const { data: frontmatter } = matter(partialContent, {});
|
|
30
50
|
// Find where content starts (after second ---)
|
|
31
51
|
const delimiter = new TextEncoder().encode('---');
|
|
@@ -68,7 +88,7 @@ function parse4KBMDX(buffer, bytesRead) {
|
|
|
68
88
|
*
|
|
69
89
|
* Stores content as files on disk in a git-friendly format:
|
|
70
90
|
* - Pages: /{site}/pages/{filename}.{locale}.vxjson
|
|
71
|
-
* -
|
|
91
|
+
* - Fragments: /fragments/{collection}/{filename}.{locale}.{mdx|vxjson}
|
|
72
92
|
*
|
|
73
93
|
* ## Indexing
|
|
74
94
|
*
|
|
@@ -102,35 +122,88 @@ function parse4KBMDX(buffer, bytesRead) {
|
|
|
102
122
|
export async function readSitesConfig(contentRoot) {
|
|
103
123
|
const absoluteContentRoot = resolve(contentRoot);
|
|
104
124
|
const sitesPath = join(absoluteContentRoot, 'sites.json');
|
|
125
|
+
let content;
|
|
105
126
|
try {
|
|
106
|
-
|
|
107
|
-
const parsed = JSON.parse(content);
|
|
108
|
-
// Validate the structure
|
|
109
|
-
if (!parsed.sites || typeof parsed.sites !== 'object') {
|
|
110
|
-
console.warn(`Warning: sites.json has invalid structure at ${sitesPath}. Expected 'sites' property. Using default configuration.`);
|
|
111
|
-
return { sites: {}, globalLocales: ['en'] };
|
|
112
|
-
}
|
|
113
|
-
if (!parsed.globalLocales || !Array.isArray(parsed.globalLocales)) {
|
|
114
|
-
console.warn(`Warning: sites.json is missing 'globalLocales' array at ${sitesPath}. Using default global locales.`);
|
|
115
|
-
return {
|
|
116
|
-
sites: parsed.sites,
|
|
117
|
-
globalLocales: ['en'],
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
return parsed;
|
|
127
|
+
content = await readFile(sitesPath, 'utf-8');
|
|
121
128
|
}
|
|
122
129
|
catch {
|
|
123
|
-
//
|
|
130
|
+
// A genuinely absent sites.json is a normal starting state.
|
|
124
131
|
console.warn(`Warning: sites.json is missing at ${sitesPath}. Using empty configuration.`);
|
|
125
132
|
return { sites: {}, globalLocales: ['en'] };
|
|
126
133
|
}
|
|
134
|
+
// Tolerating a malformed sites.json is deliberate ("Handle invalid sites.json gracefully"). What
|
|
135
|
+
// was not deliberate is the diagnosis: this used to share the catch above
|
|
136
|
+
// and report the file as MISSING, naming a path where it plainly sits,
|
|
137
|
+
// while every page silently dropped out of every site. Say what is
|
|
138
|
+
// actually wrong; the tolerance itself is unchanged.
|
|
139
|
+
let parsed;
|
|
140
|
+
try {
|
|
141
|
+
parsed = JSON.parse(content);
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
console.warn(`Warning: sites.json at ${sitesPath} is not valid JSON (${messageOf(error)}). ` +
|
|
145
|
+
'Using empty configuration — every site will be empty until this is fixed.');
|
|
146
|
+
return { sites: {}, globalLocales: ['en'] };
|
|
147
|
+
}
|
|
148
|
+
// Validate the structure. `null` is valid JSON, so `parsed` itself needs
|
|
149
|
+
// the null check before anything reads a property off it.
|
|
150
|
+
if (!parsed?.sites || typeof parsed.sites !== 'object') {
|
|
151
|
+
console.warn(`Warning: sites.json has invalid structure at ${sitesPath}. Expected 'sites' property. Using default configuration.`);
|
|
152
|
+
return { sites: {}, globalLocales: ['en'] };
|
|
153
|
+
}
|
|
154
|
+
if (!parsed.globalLocales || !Array.isArray(parsed.globalLocales)) {
|
|
155
|
+
console.warn(`Warning: sites.json is missing 'globalLocales' array at ${sitesPath}. Using default global locales.`);
|
|
156
|
+
warnUnreadableLocales(sitesPath, parsed.sites, []);
|
|
157
|
+
return {
|
|
158
|
+
sites: parsed.sites,
|
|
159
|
+
globalLocales: ['en'],
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
warnUnreadableLocales(sitesPath, parsed.sites, parsed.globalLocales);
|
|
163
|
+
return parsed;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Why a page file's name gives no locale the content grammar reads: either it
|
|
167
|
+
* names one the grammar refuses ('about.pt-br.vxjson') or it names none.
|
|
168
|
+
*/
|
|
169
|
+
function unreadablePageLocaleMessage(filename) {
|
|
170
|
+
const written = /^.+\.([^.]+)\.(?:mdx|vxjson)$/.exec(filename)?.[1];
|
|
171
|
+
return written === undefined
|
|
172
|
+
? `Page file ${filename} has no locale in its name (<name>.<locale>.vxjson), so it is not indexed.`
|
|
173
|
+
: `Page file ${filename} is not indexed. ${invalidLocaleMessage(written)}`;
|
|
174
|
+
}
|
|
175
|
+
// One warning per sites.json and locale for the whole process: the file is
|
|
176
|
+
// read again by every content API created and every config reload.
|
|
177
|
+
const warnedLocales = new Set();
|
|
178
|
+
/**
|
|
179
|
+
* Warn about each locale sites.json declares that content file names cannot
|
|
180
|
+
* carry: its pages can be neither created nor read, and nothing else says so
|
|
181
|
+
* until a save is refused.
|
|
182
|
+
*/
|
|
183
|
+
function warnUnreadableLocales(sitesPath, sites, globalLocales) {
|
|
184
|
+
const declared = [...globalLocales];
|
|
185
|
+
for (const site of Object.values(sites)) {
|
|
186
|
+
if (Array.isArray(site?.locales))
|
|
187
|
+
declared.push(...site.locales);
|
|
188
|
+
declared.push(site?.defaultLocale);
|
|
189
|
+
}
|
|
190
|
+
for (const locale of declared) {
|
|
191
|
+
const key = `${sitesPath}\0${locale}`;
|
|
192
|
+
if (typeof locale !== 'string' || isContentLocale(locale) || warnedLocales.has(key))
|
|
193
|
+
continue;
|
|
194
|
+
warnedLocales.add(key);
|
|
195
|
+
console.warn(`Warning: sites.json at ${sitesPath} declares a locale no content file can carry, so its pages can be neither created nor read. ${invalidLocaleMessage(locale)}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function messageOf(error) {
|
|
199
|
+
return error instanceof Error ? error.message : String(error);
|
|
127
200
|
}
|
|
128
201
|
export class FileSystemContentAPI {
|
|
129
202
|
contentRoot;
|
|
130
203
|
absoluteContentRoot;
|
|
131
204
|
contentIndex;
|
|
132
205
|
sitesConfig;
|
|
133
|
-
|
|
206
|
+
fragments;
|
|
134
207
|
data;
|
|
135
208
|
sites;
|
|
136
209
|
// mdx-page support: optional second filesystem root for kind:'page'+type:'mdx'.
|
|
@@ -150,7 +223,7 @@ export class FileSystemContentAPI {
|
|
|
150
223
|
get normalizedContentRoot() {
|
|
151
224
|
return this.contentRoot;
|
|
152
225
|
}
|
|
153
|
-
constructor(contentRoot,
|
|
226
|
+
constructor(contentRoot, sitesConfig, contentIndex, mdxPagesOptions, localeOptions) {
|
|
154
227
|
// Normalize contentRoot: remove ./ prefix for relative paths, keep absolute paths as-is
|
|
155
228
|
if (contentRoot.startsWith('./')) {
|
|
156
229
|
this.contentRoot = contentRoot.slice(2);
|
|
@@ -165,7 +238,6 @@ export class FileSystemContentAPI {
|
|
|
165
238
|
}
|
|
166
239
|
// Store absolute path for file operations
|
|
167
240
|
this.absoluteContentRoot = resolve(this.contentRoot);
|
|
168
|
-
// TODO: Use canvasDir for canvas/unlinked components
|
|
169
241
|
this.sitesConfig = sitesConfig;
|
|
170
242
|
this.contentIndex = contentIndex;
|
|
171
243
|
// Resolve which site owns mdx-type pages. Resolution order:
|
|
@@ -217,8 +289,8 @@ export class FileSystemContentAPI {
|
|
|
217
289
|
}
|
|
218
290
|
merged.add(this.mdxPagesDefaultLocale);
|
|
219
291
|
this.availableLocales = merged;
|
|
220
|
-
// Initialize
|
|
221
|
-
this.
|
|
292
|
+
// Initialize fragments
|
|
293
|
+
this.fragments = new Fragments(this, this.contentIndex.getFragmentIndex());
|
|
222
294
|
// Initialize data
|
|
223
295
|
this.data = new Data(this, this.contentIndex.getDataIndex());
|
|
224
296
|
// Initialize all sites
|
|
@@ -231,13 +303,12 @@ export class FileSystemContentAPI {
|
|
|
231
303
|
});
|
|
232
304
|
}
|
|
233
305
|
static async create(options) {
|
|
234
|
-
const { canvasDir } = options;
|
|
235
306
|
// Keep contentRoot as provided (will be normalized in constructor)
|
|
236
307
|
const contentRoot = options.contentRoot;
|
|
237
308
|
// Load sites config (canonical home for per-site fields including mdxPages).
|
|
238
309
|
let sitesConfig = await readSitesConfig(contentRoot);
|
|
239
310
|
// Apply the top-level locales option as the authoritative source.
|
|
240
|
-
// We expand sitesConfig so that the
|
|
311
|
+
// We expand sitesConfig so that the FragmentIndex (which keys on
|
|
241
312
|
// globalLocales) and the resolved SiteIndex (which keys on the
|
|
242
313
|
// site's locales array) both have slots for every option-declared
|
|
243
314
|
// locale. Without this, the parser would correctly identify a file's
|
|
@@ -267,7 +338,7 @@ export class FileSystemContentAPI {
|
|
|
267
338
|
const normalizedContentRoot = contentRoot.startsWith('./') ? contentRoot.slice(2) : contentRoot;
|
|
268
339
|
const contentIndex = await ContentIndex.getCachedOrCreate(normalizedContentRoot, sitesConfig);
|
|
269
340
|
// Create instance (not cached)
|
|
270
|
-
const api = new FileSystemContentAPI(contentRoot,
|
|
341
|
+
const api = new FileSystemContentAPI(contentRoot, sitesConfig, contentIndex, {
|
|
271
342
|
root: options.mdxPagesRoot,
|
|
272
343
|
defaultLocale: options.mdxPagesDefaultLocale,
|
|
273
344
|
site: options.mdxPagesSite,
|
|
@@ -275,68 +346,119 @@ export class FileSystemContentAPI {
|
|
|
275
346
|
availableLocales: options.availableLocales,
|
|
276
347
|
defaultLocale: options.defaultLocale,
|
|
277
348
|
});
|
|
278
|
-
// Index all content files (only if index is empty)
|
|
349
|
+
// Index all content files (only if index is empty).
|
|
350
|
+
//
|
|
351
|
+
// FileSystemContentAPI continues to hydrate via parseFileHeaderWithRepair
|
|
352
|
+
// (legacy path), which performs read-repair side effects on disk (renaming
|
|
353
|
+
// missing-locale files, rewriting non-canonical VXJSON, adding missing
|
|
354
|
+
// id/created/modified). ContentIndex.indexAll(backend) is the newer,
|
|
355
|
+
// backend-agnostic hydration path for content that is not on disk;
|
|
356
|
+
// FileSystemContentAPI's behavior must stay identical, so this call site
|
|
357
|
+
// keeps the legacy path until the repair side effects move behind a backend.
|
|
279
358
|
if (contentIndex.entryCount === 0) {
|
|
280
359
|
await api.indexAllContent();
|
|
281
360
|
}
|
|
282
361
|
return api;
|
|
283
362
|
}
|
|
284
363
|
/**
|
|
285
|
-
*
|
|
364
|
+
* Hydrate the content index for this FileSystemContentAPI by scanning the
|
|
365
|
+
* content root and running parseFileHeaderWithRepair on each file. This is
|
|
366
|
+
* the legacy hydration path that preserves the filesystem read-repair
|
|
367
|
+
* behavior (file renames, canonical-form rewrites, id/timestamp backfill).
|
|
368
|
+
*
|
|
369
|
+
* ContentIndex.indexAll(backend) is the backend-agnostic replacement for
|
|
370
|
+
* content that is not on disk; this private method exists so FileSystemContentAPI
|
|
371
|
+
* can keep its repair-on-read side effects until they are lifted onto a
|
|
372
|
+
* dedicated FileSystemReaderBackend field.
|
|
286
373
|
*/
|
|
287
|
-
static clearCaches() {
|
|
288
|
-
ContentIndex.clearCache();
|
|
289
|
-
// TODO: CanvasIndex.clearCache() when implemented
|
|
290
|
-
}
|
|
291
374
|
async indexAllContent() {
|
|
292
|
-
//
|
|
375
|
+
// Also drops the previous problems: they describe an index that no longer exists.
|
|
293
376
|
this.contentIndex.clear();
|
|
294
|
-
// Scan content directory
|
|
295
377
|
const files = await this.scanContentFiles();
|
|
296
|
-
// Use optimized batch size based on benchmarks
|
|
297
378
|
const OPTIMAL_BATCH_SIZE = 1000;
|
|
298
379
|
console.debug(`Indexing ${files.length} content files...`);
|
|
299
|
-
//
|
|
300
|
-
//
|
|
301
|
-
|
|
302
|
-
|
|
380
|
+
// Which file claimed each id+locale — same guard ContentIndex.indexAll
|
|
381
|
+
// carries. Without it a duplicated file merges into the first one's
|
|
382
|
+
// locale slot and a page silently stops existing, and this legacy path
|
|
383
|
+
// is the one every local dev server and Astro build hydrates through.
|
|
384
|
+
const claimedBy = new Map();
|
|
385
|
+
// One 4KB buffer per file in a batch: the batch is read in parallel, so
|
|
386
|
+
// two files sharing a buffer overwrite each other's bytes mid-parse.
|
|
387
|
+
// parseFileHeaderWithRepair owns the buffer contract (reads up to 4096
|
|
388
|
+
// bytes from offset 0).
|
|
389
|
+
const buffers = Array.from({ length: Math.min(files.length, OPTIMAL_BATCH_SIZE) }, () => new Uint8Array(4096));
|
|
303
390
|
for (let i = 0; i < files.length; i += OPTIMAL_BATCH_SIZE) {
|
|
304
391
|
const batch = files.slice(i, i + OPTIMAL_BATCH_SIZE);
|
|
305
|
-
|
|
306
|
-
await Promise.all(batch.map(async (filePath, index) => {
|
|
392
|
+
await Promise.all(batch.map(async (filePath, batchIndex) => {
|
|
307
393
|
try {
|
|
308
|
-
const result = await this.parseFileHeaderWithRepair(filePath, buffers[
|
|
394
|
+
const result = await this.parseFileHeaderWithRepair(filePath, buffers[batchIndex]);
|
|
309
395
|
if (result) {
|
|
310
|
-
|
|
311
|
-
//
|
|
312
|
-
|
|
396
|
+
// Same reason as the shared indexAll loop: a page under a site
|
|
397
|
+
// sites.json does not declare would be filed nowhere and
|
|
398
|
+
// silently vanish from every lookup and build.
|
|
399
|
+
if (result.manifest.kind === 'page' &&
|
|
400
|
+
result.manifest.site &&
|
|
401
|
+
!this.contentIndex.hasSite(result.manifest.site)) {
|
|
402
|
+
this.recordIndexProblem(filePath, 'unknown-site', unknownSiteMessage(result.manifest.site));
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
const claim = `${result.manifest.id}::${result.localeVersion.locale}`;
|
|
406
|
+
const claimant = claimedBy.get(claim);
|
|
407
|
+
if (claimant !== undefined && claimant !== filePath) {
|
|
408
|
+
this.recordIndexProblem(filePath, 'duplicate-id', duplicateIdMessage(result.manifest.id, result.localeVersion.locale, claimant));
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
claimedBy.set(claim, filePath);
|
|
412
|
+
this.contentIndex.addContent(result.manifest, result.localeVersion, result.content);
|
|
313
413
|
}
|
|
314
414
|
}
|
|
315
415
|
catch (error) {
|
|
316
|
-
console.error(`Failed to index ${filePath}
|
|
416
|
+
console.error(`Failed to index ${filePath}: ${messageOf(error)}`);
|
|
417
|
+
this.recordIndexProblem(filePath, 'index-error', messageOf(error));
|
|
317
418
|
}
|
|
318
419
|
}));
|
|
319
420
|
}
|
|
320
421
|
}
|
|
422
|
+
/**
|
|
423
|
+
* Record a file the indexer could not read. Signature matches the `onError`
|
|
424
|
+
* callback of ContentIndex.indexAll so it can be handed over unchanged when
|
|
425
|
+
* this class migrates off the legacy repair-on-read hydration path. The
|
|
426
|
+
* problems live on the index itself, not on this instance: the index is
|
|
427
|
+
* cached per content root, and a later instance over the same root reuses it
|
|
428
|
+
* without re-indexing, so instance-local problems would vanish.
|
|
429
|
+
*/
|
|
430
|
+
recordIndexProblem(path, reason, message) {
|
|
431
|
+
this.contentIndex.recordProblem(path, reason, message);
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Files the indexer could not read. They are absent from listAllContent(),
|
|
435
|
+
* so any caller that treats that list as complete — `conloca doctor`, most
|
|
436
|
+
* of all — must consult this to avoid reporting success over broken content.
|
|
437
|
+
*/
|
|
438
|
+
getIndexProblems() {
|
|
439
|
+
return this.contentIndex.getProblems();
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Clear all caches - useful for testing
|
|
443
|
+
*/
|
|
444
|
+
static clearCaches() {
|
|
445
|
+
ContentIndex.clearCache();
|
|
446
|
+
// TODO: CanvasIndex.clearCache() when implemented
|
|
447
|
+
}
|
|
321
448
|
async parseFileHeaderWithRepair(originalFilePath, buffer) {
|
|
322
|
-
let handle;
|
|
323
449
|
let bytesRead = 0;
|
|
324
450
|
let filePath = originalFilePath; // Use a local variable that can be updated if file is renamed
|
|
325
451
|
try {
|
|
326
|
-
|
|
327
|
-
bytesRead = (await handle.read(buffer, 0, 4096, 0)).bytesRead;
|
|
452
|
+
bytesRead = await readFilePrefix(filePath, buffer);
|
|
328
453
|
}
|
|
329
454
|
catch (error) {
|
|
330
455
|
console.error(`Error reading ${filePath}:`, error);
|
|
331
456
|
throw error;
|
|
332
457
|
}
|
|
333
|
-
finally {
|
|
334
|
-
await handle?.close();
|
|
335
|
-
}
|
|
336
458
|
try {
|
|
337
459
|
// Now parse the buffer content
|
|
338
460
|
// Parse file to extract metadata
|
|
339
|
-
let relativePath = filePath.replace(this.absoluteContentRoot
|
|
461
|
+
let relativePath = filePath.replace(`${this.absoluteContentRoot}/`, '');
|
|
340
462
|
let parts = relativePath.split('/');
|
|
341
463
|
// Determine site/collection structure
|
|
342
464
|
let site;
|
|
@@ -351,11 +473,11 @@ export class FileSystemContentAPI {
|
|
|
351
473
|
// leading prefix is stripped here; locale lives as a directory segment
|
|
352
474
|
// ({locale}/{slug}.mdx for non-default locales, {slug}.mdx at the root
|
|
353
475
|
// for the default locale).
|
|
354
|
-
if (this.absoluteMdxPagesRoot && filePath.startsWith(this.absoluteMdxPagesRoot
|
|
476
|
+
if (this.absoluteMdxPagesRoot && filePath.startsWith(`${this.absoluteMdxPagesRoot}/`)) {
|
|
355
477
|
kind = 'page';
|
|
356
478
|
site = this.mdxPagesSite;
|
|
357
479
|
collection = this.mdxPagesCollection;
|
|
358
|
-
const mdxRelativePath = filePath.replace(this.absoluteMdxPagesRoot
|
|
480
|
+
const mdxRelativePath = filePath.replace(`${this.absoluteMdxPagesRoot}/`, '');
|
|
359
481
|
const mdxParts = mdxRelativePath.split('/');
|
|
360
482
|
const mdxFilename = mdxParts[mdxParts.length - 1];
|
|
361
483
|
const noLocaleMatch = mdxFilename.match(/^(.+)\.mdx$/);
|
|
@@ -370,77 +492,75 @@ export class FileSystemContentAPI {
|
|
|
370
492
|
const slugParts = mdxParts.slice(1);
|
|
371
493
|
pathname =
|
|
372
494
|
slugParts[slugParts.length - 1] === 'index'
|
|
373
|
-
?
|
|
374
|
-
:
|
|
495
|
+
? `/${slugParts.slice(0, -1).join('/')}`
|
|
496
|
+
: `/${slugParts.join('/')}`;
|
|
375
497
|
}
|
|
376
498
|
else {
|
|
377
499
|
locale = this.mdxPagesDefaultLocale;
|
|
378
500
|
pathname =
|
|
379
501
|
mdxParts[mdxParts.length - 1] === 'index'
|
|
380
|
-
?
|
|
381
|
-
:
|
|
502
|
+
? `/${mdxParts.slice(0, -1).join('/')}`
|
|
503
|
+
: `/${mdxParts.join('/')}`;
|
|
382
504
|
}
|
|
383
505
|
parsedData = parse4KBMDX(buffer, bytesRead);
|
|
384
506
|
}
|
|
385
|
-
else if (parts[0] === '
|
|
386
|
-
kind = '
|
|
387
|
-
// Check if
|
|
507
|
+
else if (parts[0] === 'fragments') {
|
|
508
|
+
kind = 'fragment';
|
|
509
|
+
// Check if fragment is directly in fragments/ directory (no collection subdirectory)
|
|
388
510
|
if (parts.length === 2) {
|
|
389
|
-
//
|
|
390
|
-
collection = '
|
|
511
|
+
// Keep the direct layout unless the repair move succeeds.
|
|
512
|
+
collection = '';
|
|
391
513
|
const filename = parts[1];
|
|
392
|
-
// Create the
|
|
393
|
-
const
|
|
514
|
+
// Create the repair folder if it doesn't exist
|
|
515
|
+
const repairDir = join(this.absoluteContentRoot, 'fragments', REPAIRED_FRAGMENT_COLLECTION);
|
|
394
516
|
try {
|
|
395
|
-
await mkdir(
|
|
517
|
+
await mkdir(repairDir, { recursive: true });
|
|
396
518
|
}
|
|
397
|
-
catch
|
|
519
|
+
catch {
|
|
398
520
|
// Directory might already exist, that's fine
|
|
399
521
|
}
|
|
400
|
-
// Move the file to the
|
|
401
|
-
const newFilePath = join(
|
|
522
|
+
// Move the file to the repair folder
|
|
523
|
+
const newFilePath = join(repairDir, filename);
|
|
402
524
|
try {
|
|
403
|
-
await handle?.close(); // Close the current file handle before moving
|
|
404
|
-
handle = undefined;
|
|
405
525
|
// Check if target file already exists
|
|
406
526
|
try {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
console.warn(`Cannot move ${filename} to blocks/general/: target file already exists`);
|
|
527
|
+
await access(newFilePath);
|
|
528
|
+
console.warn(`Cannot move ${filename} to fragments/${REPAIRED_FRAGMENT_COLLECTION}/: target file already exists`);
|
|
410
529
|
}
|
|
411
|
-
catch
|
|
530
|
+
catch {
|
|
412
531
|
// Target doesn't exist, safe to move
|
|
413
532
|
await rename(filePath, newFilePath);
|
|
414
|
-
console.log(`Repaired: Moved ${filename} to
|
|
415
|
-
//
|
|
533
|
+
console.log(`Repaired: Moved ${filename} to fragments/${REPAIRED_FRAGMENT_COLLECTION}/ collection`);
|
|
534
|
+
// The file now uses the repaired collection layout.
|
|
535
|
+
collection = REPAIRED_FRAGMENT_COLLECTION;
|
|
416
536
|
filePath = newFilePath;
|
|
417
537
|
// Update relativePath and parts for the new file path
|
|
418
|
-
relativePath = filePath.replace(this.absoluteContentRoot
|
|
538
|
+
relativePath = filePath.replace(`${this.absoluteContentRoot}/`, '');
|
|
419
539
|
parts = relativePath.split('/');
|
|
420
|
-
|
|
421
|
-
handle = await open(filePath, 'r');
|
|
422
|
-
bytesRead = (await handle.read(buffer, 0, 4096, 0)).bytesRead;
|
|
540
|
+
bytesRead = await readFilePrefix(filePath, buffer);
|
|
423
541
|
}
|
|
424
542
|
}
|
|
425
543
|
catch (moveError) {
|
|
426
|
-
console.error(`Failed to move ${filename} to
|
|
544
|
+
console.error(`Failed to move ${filename} to fragments/${REPAIRED_FRAGMENT_COLLECTION}/:`, moveError);
|
|
427
545
|
// Continue processing with original file
|
|
428
|
-
|
|
429
|
-
bytesRead = (await handle.read(buffer, 0, 4096, 0)).bytesRead;
|
|
546
|
+
bytesRead = await readFilePrefix(filePath, buffer);
|
|
430
547
|
}
|
|
431
548
|
}
|
|
432
549
|
else {
|
|
433
|
-
//
|
|
550
|
+
// Fragment is in a collection subdirectory
|
|
434
551
|
collection = parts[1];
|
|
435
552
|
}
|
|
436
553
|
const filename = parts[parts.length - 1];
|
|
437
554
|
// Extract name and locale from filename: hero.en.mdx -> name: hero, locale: en
|
|
438
|
-
// Also handle files without locale: hero.mdx -> name: hero, locale: default from sites.json
|
|
439
|
-
|
|
555
|
+
// Also handle files without locale: hero.mdx -> name: hero, locale: default from sites.json.
|
|
556
|
+
// The locale grammar is the shared one (content-path.ts): a suffix that is not
|
|
557
|
+
// a locale ('hero.v2.mdx') takes the rename repair below instead of being
|
|
558
|
+
// indexed under a made-up locale.
|
|
559
|
+
const localeSuffix = parseLocaleFilename(filename);
|
|
440
560
|
const matchWithoutLocale = filename.match(/^(.+)\.(mdx|vxjson)$/);
|
|
441
|
-
if (
|
|
442
|
-
name =
|
|
443
|
-
locale =
|
|
561
|
+
if (localeSuffix && localeSuffix.extension !== 'json') {
|
|
562
|
+
name = localeSuffix.base;
|
|
563
|
+
locale = localeSuffix.locale;
|
|
444
564
|
}
|
|
445
565
|
else if (matchWithoutLocale) {
|
|
446
566
|
name = matchWithoutLocale[1];
|
|
@@ -454,15 +574,11 @@ export class FileSystemContentAPI {
|
|
|
454
574
|
console.log(`Will rename to: ${newFilename} at ${newFilePath}`);
|
|
455
575
|
// Check if target file already exists
|
|
456
576
|
try {
|
|
457
|
-
await
|
|
458
|
-
handle = undefined;
|
|
459
|
-
// Try to open the target file to check if it exists
|
|
460
|
-
const targetHandle = await open(newFilePath, 'r');
|
|
461
|
-
await targetHandle.close();
|
|
577
|
+
await access(newFilePath);
|
|
462
578
|
// Target file exists, skip renaming to avoid conflict
|
|
463
579
|
console.warn(`Cannot rename ${filename} to ${newFilename}: target file already exists`);
|
|
464
580
|
}
|
|
465
|
-
catch
|
|
581
|
+
catch {
|
|
466
582
|
// Target file doesn't exist, safe to rename
|
|
467
583
|
console.log(`Target file doesn't exist, proceeding with rename`);
|
|
468
584
|
try {
|
|
@@ -471,17 +587,14 @@ export class FileSystemContentAPI {
|
|
|
471
587
|
// Update filePath to the new path for further processing
|
|
472
588
|
filePath = newFilePath;
|
|
473
589
|
// Update relativePath and parts for the new file path
|
|
474
|
-
relativePath = filePath.replace(this.absoluteContentRoot
|
|
590
|
+
relativePath = filePath.replace(`${this.absoluteContentRoot}/`, '');
|
|
475
591
|
parts = relativePath.split('/');
|
|
476
|
-
|
|
477
|
-
handle = await open(filePath, 'r');
|
|
478
|
-
bytesRead = (await handle.read(buffer, 0, 4096, 0)).bytesRead;
|
|
592
|
+
bytesRead = await readFilePrefix(filePath, buffer);
|
|
479
593
|
}
|
|
480
594
|
catch (renameError) {
|
|
481
595
|
console.error(`Failed to rename ${filename}:`, renameError);
|
|
482
596
|
// Continue processing with original file
|
|
483
|
-
|
|
484
|
-
bytesRead = (await handle.read(buffer, 0, 4096, 0)).bytesRead;
|
|
597
|
+
bytesRead = await readFilePrefix(filePath, buffer);
|
|
485
598
|
}
|
|
486
599
|
}
|
|
487
600
|
}
|
|
@@ -498,12 +611,12 @@ export class FileSystemContentAPI {
|
|
|
498
611
|
kind = 'data';
|
|
499
612
|
collection = parts[1];
|
|
500
613
|
const filename = parts[parts.length - 1];
|
|
501
|
-
// Extract name and locale from filename: testimonial.en.json -> name: testimonial, locale: en
|
|
502
|
-
//
|
|
503
|
-
const
|
|
504
|
-
if (
|
|
505
|
-
name =
|
|
506
|
-
locale =
|
|
614
|
+
// Extract name and locale from filename: testimonial.en.json -> name: testimonial, locale: en.
|
|
615
|
+
// The locale grammar is the shared one (content-path.ts).
|
|
616
|
+
const localeSuffix = parseLocaleFilename(filename);
|
|
617
|
+
if (localeSuffix?.extension === 'json') {
|
|
618
|
+
name = localeSuffix.base;
|
|
619
|
+
locale = localeSuffix.locale;
|
|
507
620
|
}
|
|
508
621
|
// Parse JSON to get ID, metadata, and data
|
|
509
622
|
const content = textDecoder.decode(buffer.subarray(0, bytesRead));
|
|
@@ -522,7 +635,8 @@ export class FileSystemContentAPI {
|
|
|
522
635
|
};
|
|
523
636
|
}
|
|
524
637
|
catch (error) {
|
|
525
|
-
console.error(`Failed to parse JSON data file ${filePath}
|
|
638
|
+
console.error(`Failed to parse JSON data file ${filePath}: ${messageOf(error)}`);
|
|
639
|
+
this.recordIndexProblem(filePath, 'malformed-json', messageOf(error));
|
|
526
640
|
return null;
|
|
527
641
|
}
|
|
528
642
|
}
|
|
@@ -539,17 +653,26 @@ export class FileSystemContentAPI {
|
|
|
539
653
|
// above) and won't reach this branch.
|
|
540
654
|
const pathParts = parts.slice(2); // Remove site/collection
|
|
541
655
|
const filename = pathParts[pathParts.length - 1];
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
656
|
+
// One grammar with the header parser and every other reader (content-path.ts).
|
|
657
|
+
// A page file with no parseable locale suffix is NOT content: indexing it
|
|
658
|
+
// under the fallback locale filed it with no pathname at all, invisible to
|
|
659
|
+
// every pathname lookup while still occupying that locale slot. parseFilePath
|
|
660
|
+
// (the reindex path) has always refused these — now both halves agree.
|
|
661
|
+
const localeSuffix = filename ? parseLocaleFilename(filename) : null;
|
|
662
|
+
if (!localeSuffix || localeSuffix.extension === 'json') {
|
|
663
|
+
const message = unreadablePageLocaleMessage(filename ?? relativePath);
|
|
664
|
+
console.warn(`Skipping ${filePath}: ${message}`);
|
|
665
|
+
this.recordIndexProblem(filePath, 'unsupported-locale-path', message);
|
|
666
|
+
return null;
|
|
552
667
|
}
|
|
668
|
+
locale = localeSuffix.locale;
|
|
669
|
+
// Replace filename with basename
|
|
670
|
+
pathParts[pathParts.length - 1] = localeSuffix.base;
|
|
671
|
+
// index files map to parent: /index -> /, /foo/index -> /foo
|
|
672
|
+
pathname =
|
|
673
|
+
pathParts[pathParts.length - 1] === 'index'
|
|
674
|
+
? `/${pathParts.slice(0, -1).join('/')}`
|
|
675
|
+
: `/${pathParts.join('/')}`;
|
|
553
676
|
// Parse format-appropriately. parse4KBMDX reads frontmatter from the
|
|
554
677
|
// MDX header; VXJSON.parse4KB reads the leading JSON metadata.
|
|
555
678
|
if (filePath.endsWith('.mdx')) {
|
|
@@ -575,6 +698,7 @@ export class FileSystemContentAPI {
|
|
|
575
698
|
...(parsedData.previousPathnames && { previousPathnames: parsedData.previousPathnames }),
|
|
576
699
|
...(name && { name }),
|
|
577
700
|
meta: parsedData.meta || { title: name || 'Untitled' },
|
|
701
|
+
sourcePath: filePath,
|
|
578
702
|
};
|
|
579
703
|
// Build ContentManifest (without content)
|
|
580
704
|
const manifest = {
|
|
@@ -592,7 +716,7 @@ export class FileSystemContentAPI {
|
|
|
592
716
|
// Check if we need to repair the file first (missing required fields)
|
|
593
717
|
if (needsRepair) {
|
|
594
718
|
// Perform repair and get the repaired content
|
|
595
|
-
const repairedData = await this.performReadRepair(filePath, manifest
|
|
719
|
+
const repairedData = await this.performReadRepair(filePath, manifest);
|
|
596
720
|
if (repairedData) {
|
|
597
721
|
content = repairedData.content;
|
|
598
722
|
localeVersion.etag = repairedData.etag;
|
|
@@ -601,6 +725,7 @@ export class FileSystemContentAPI {
|
|
|
601
725
|
}
|
|
602
726
|
// Repair failed, skip this file
|
|
603
727
|
console.error(`Failed to repair ${filePath}, skipping`);
|
|
728
|
+
this.recordIndexProblem(filePath, 'repair-failed', 'Read-repair failed');
|
|
604
729
|
return null;
|
|
605
730
|
}
|
|
606
731
|
if (bytesRead < 4096 ||
|
|
@@ -639,7 +764,7 @@ export class FileSystemContentAPI {
|
|
|
639
764
|
if (!etagResult.success) {
|
|
640
765
|
// Content field is not last or invalid JSON - repair immediately
|
|
641
766
|
// Perform repair and get the repaired content
|
|
642
|
-
const repairedData = await this.performReadRepair(filePath, manifest
|
|
767
|
+
const repairedData = await this.performReadRepair(filePath, manifest);
|
|
643
768
|
if (repairedData) {
|
|
644
769
|
content = repairedData.content;
|
|
645
770
|
localeVersion.etag = repairedData.etag;
|
|
@@ -648,6 +773,7 @@ export class FileSystemContentAPI {
|
|
|
648
773
|
}
|
|
649
774
|
// Repair failed, skip this file
|
|
650
775
|
console.error(`Failed to repair ${filePath}, skipping`);
|
|
776
|
+
this.recordIndexProblem(filePath, 'repair-failed', 'Read-repair failed');
|
|
651
777
|
return null;
|
|
652
778
|
}
|
|
653
779
|
localeVersion.etag = etagResult.contentEtag
|
|
@@ -659,7 +785,8 @@ export class FileSystemContentAPI {
|
|
|
659
785
|
return { manifest, locale, localeVersion, bytesRead, content };
|
|
660
786
|
}
|
|
661
787
|
catch (error) {
|
|
662
|
-
console.error(`Error indexing ${originalFilePath}
|
|
788
|
+
console.error(`Error indexing ${originalFilePath}: ${messageOf(error)}`);
|
|
789
|
+
this.recordIndexProblem(originalFilePath, 'parse-failed', messageOf(error));
|
|
663
790
|
return null;
|
|
664
791
|
}
|
|
665
792
|
}
|
|
@@ -667,7 +794,7 @@ export class FileSystemContentAPI {
|
|
|
667
794
|
* Perform read-repair by writing back the file with missing fields
|
|
668
795
|
* This ensures stable IDs and timestamps across restarts
|
|
669
796
|
*/
|
|
670
|
-
async performReadRepair(filePath, manifest
|
|
797
|
+
async performReadRepair(filePath, manifest) {
|
|
671
798
|
try {
|
|
672
799
|
const now = new Date().toISOString();
|
|
673
800
|
let needsWrite = false;
|
|
@@ -799,7 +926,7 @@ export class FileSystemContentAPI {
|
|
|
799
926
|
}
|
|
800
927
|
}
|
|
801
928
|
}
|
|
802
|
-
catch
|
|
929
|
+
catch {
|
|
803
930
|
// Directory doesn't exist yet
|
|
804
931
|
}
|
|
805
932
|
}
|
|
@@ -816,7 +943,7 @@ export class FileSystemContentAPI {
|
|
|
816
943
|
// practice.
|
|
817
944
|
if (this.absoluteMdxPagesRoot &&
|
|
818
945
|
this.absoluteMdxPagesRoot !== this.absoluteContentRoot &&
|
|
819
|
-
!this.absoluteMdxPagesRoot.startsWith(this.absoluteContentRoot
|
|
946
|
+
!this.absoluteMdxPagesRoot.startsWith(`${this.absoluteContentRoot}/`)) {
|
|
820
947
|
async function scanMdxPages(dir) {
|
|
821
948
|
try {
|
|
822
949
|
const entries = await readdir(dir, { withFileTypes: true });
|
|
@@ -830,7 +957,7 @@ export class FileSystemContentAPI {
|
|
|
830
957
|
}
|
|
831
958
|
}
|
|
832
959
|
}
|
|
833
|
-
catch
|
|
960
|
+
catch {
|
|
834
961
|
// Directory doesn't exist yet
|
|
835
962
|
}
|
|
836
963
|
}
|
|
@@ -838,10 +965,23 @@ export class FileSystemContentAPI {
|
|
|
838
965
|
}
|
|
839
966
|
return files;
|
|
840
967
|
}
|
|
968
|
+
/**
|
|
969
|
+
* Why a page of `type` cannot have `pathname` in `locale` where `getFilePath` puts it: an mdx
|
|
970
|
+
* page lives in the MDX pages folder when one is configured, where a default-language path may
|
|
971
|
+
* not start with a language folder (see `checkMdxPagesPathname`).
|
|
972
|
+
*/
|
|
973
|
+
mdxPagesPathnameRefusal(type, pathname, locale) {
|
|
974
|
+
if (type !== 'mdx' || !this.absoluteMdxPagesRoot)
|
|
975
|
+
return null;
|
|
976
|
+
return checkMdxPagesPathname(pathname, locale, {
|
|
977
|
+
defaultLocale: this.mdxPagesDefaultLocale,
|
|
978
|
+
locales: this.availableLocales,
|
|
979
|
+
});
|
|
980
|
+
}
|
|
841
981
|
getFilePath(manifest, { locale, name, pathname }) {
|
|
842
|
-
if (manifest.kind === '
|
|
982
|
+
if (manifest.kind === 'fragment') {
|
|
843
983
|
const ext = manifest.type === 'mdx' ? 'mdx' : 'vxjson';
|
|
844
|
-
return join(this.absoluteContentRoot, '
|
|
984
|
+
return join(this.absoluteContentRoot, 'fragments', manifest.collection, `${name}.${locale}.${ext}`);
|
|
845
985
|
}
|
|
846
986
|
if (manifest.kind === 'data') {
|
|
847
987
|
return join(this.absoluteContentRoot, 'data', manifest.collection, `${name}.${locale}.json`);
|
|
@@ -877,8 +1017,8 @@ export class FileSystemContentAPI {
|
|
|
877
1017
|
// their position relative to absoluteContentRoot. Mirror the parsing
|
|
878
1018
|
// performed in parseFileHeaderWithRepair so both code paths agree on
|
|
879
1019
|
// the derived (pathname, locale) tuple.
|
|
880
|
-
if (this.absoluteMdxPagesRoot && filePath.startsWith(this.absoluteMdxPagesRoot
|
|
881
|
-
const mdxRelativePath = filePath.replace(this.absoluteMdxPagesRoot
|
|
1020
|
+
if (this.absoluteMdxPagesRoot && filePath.startsWith(`${this.absoluteMdxPagesRoot}/`)) {
|
|
1021
|
+
const mdxRelativePath = filePath.replace(`${this.absoluteMdxPagesRoot}/`, '');
|
|
882
1022
|
const mdxParts = mdxRelativePath.split('/');
|
|
883
1023
|
const mdxFilename = mdxParts[mdxParts.length - 1];
|
|
884
1024
|
let locale;
|
|
@@ -894,15 +1034,13 @@ export class FileSystemContentAPI {
|
|
|
894
1034
|
const slugParts = mdxParts.slice(1);
|
|
895
1035
|
pathname =
|
|
896
1036
|
slugParts[slugParts.length - 1] === 'index'
|
|
897
|
-
?
|
|
898
|
-
:
|
|
1037
|
+
? `/${slugParts.slice(0, -1).join('/')}`
|
|
1038
|
+
: `/${slugParts.join('/')}`;
|
|
899
1039
|
}
|
|
900
1040
|
else {
|
|
901
1041
|
locale = this.mdxPagesDefaultLocale;
|
|
902
1042
|
pathname =
|
|
903
|
-
mdxParts[mdxParts.length - 1] === 'index'
|
|
904
|
-
? '/' + mdxParts.slice(0, -1).join('/') || '/'
|
|
905
|
-
: '/' + mdxParts.join('/');
|
|
1043
|
+
mdxParts[mdxParts.length - 1] === 'index' ? `/${mdxParts.slice(0, -1).join('/')}` : `/${mdxParts.join('/')}`;
|
|
906
1044
|
}
|
|
907
1045
|
return {
|
|
908
1046
|
kind: 'page',
|
|
@@ -912,55 +1050,12 @@ export class FileSystemContentAPI {
|
|
|
912
1050
|
locale,
|
|
913
1051
|
};
|
|
914
1052
|
}
|
|
915
|
-
//
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
//
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
if (parts[0] === 'data' && parts.length === 3) {
|
|
922
|
-
const dataMatch = filename.match(/^(.+)\.([a-z]{2}(?:-[A-Z]{2})?)\.json$/);
|
|
923
|
-
if (!dataMatch)
|
|
924
|
-
return null;
|
|
925
|
-
const [, name, locale] = dataMatch;
|
|
926
|
-
return {
|
|
927
|
-
kind: 'data',
|
|
928
|
-
collection: parts[1],
|
|
929
|
-
name,
|
|
930
|
-
locale,
|
|
931
|
-
};
|
|
932
|
-
}
|
|
933
|
-
// Match locale.ext pattern for non-data files (vxjson, mdx)
|
|
934
|
-
const match = filename.match(/^(.+)\.([a-z]{2}(?:-[A-Z]{2})?)\.(?:vxjson|mdx)$/);
|
|
935
|
-
if (!match)
|
|
936
|
-
return null;
|
|
937
|
-
const [, nameOrPath, locale] = match;
|
|
938
|
-
if (parts[0] === 'blocks' && parts.length === 3) {
|
|
939
|
-
// blocks/collection/name.locale.ext
|
|
940
|
-
return {
|
|
941
|
-
kind: 'block',
|
|
942
|
-
collection: parts[1],
|
|
943
|
-
name: nameOrPath,
|
|
944
|
-
locale,
|
|
945
|
-
};
|
|
946
|
-
}
|
|
947
|
-
if (parts.length >= 3 && parts[0] !== 'blocks') {
|
|
948
|
-
// site/collection/pathname.locale.vxjson
|
|
949
|
-
const site = parts[0];
|
|
950
|
-
const collection = parts[1];
|
|
951
|
-
// Reconstruct pathname from remaining parts
|
|
952
|
-
const pathParts = parts.slice(2, -1); // All dirs between collection and filename
|
|
953
|
-
pathParts.push(nameOrPath); // Add the filename part (without locale.ext)
|
|
954
|
-
const pathname = '/' + pathParts.join('/');
|
|
955
|
-
return {
|
|
956
|
-
kind: 'page',
|
|
957
|
-
site,
|
|
958
|
-
collection,
|
|
959
|
-
pathname,
|
|
960
|
-
locale,
|
|
961
|
-
};
|
|
962
|
-
}
|
|
963
|
-
return null;
|
|
1053
|
+
// Everything under the primary content root shares ONE grammar with the
|
|
1054
|
+
// header parser and every other reader (content-path.ts) — including the
|
|
1055
|
+
// `index` -> parent-pathname rule this method used to skip. Missing it made
|
|
1056
|
+
// reindex look up `/index`, never find the entry the indexer had filed under
|
|
1057
|
+
// `/`, and leave a deleted index page in the index forever.
|
|
1058
|
+
return parseContentPathLayout(filePath.replace(`${this.absoluteContentRoot}/`, ''));
|
|
964
1059
|
}
|
|
965
1060
|
async getContent(id) {
|
|
966
1061
|
// Get manifest for this ID
|
|
@@ -1003,7 +1098,7 @@ export class FileSystemContentAPI {
|
|
|
1003
1098
|
await this.reindex();
|
|
1004
1099
|
// Try again with updated index
|
|
1005
1100
|
const updatedManifest = this.contentIndex.getManifest(id);
|
|
1006
|
-
if (!updatedManifest
|
|
1101
|
+
if (!updatedManifest?.locales[locale]) {
|
|
1007
1102
|
return null;
|
|
1008
1103
|
}
|
|
1009
1104
|
return this.getLocalized(id, locale);
|
|
@@ -1162,21 +1257,30 @@ export class FileSystemContentAPI {
|
|
|
1162
1257
|
}
|
|
1163
1258
|
return result;
|
|
1164
1259
|
}
|
|
1165
|
-
catch
|
|
1260
|
+
catch {
|
|
1166
1261
|
return null;
|
|
1167
1262
|
}
|
|
1168
1263
|
}
|
|
1169
1264
|
async createContent(data) {
|
|
1170
1265
|
// Use shared validation
|
|
1171
|
-
const validation = validateCreateContent(data, this.sites, this.
|
|
1266
|
+
const validation = validateCreateContent(data, this.sites, this.fragments, this.data);
|
|
1172
1267
|
if (!validation.valid) {
|
|
1173
1268
|
return {
|
|
1174
1269
|
success: false,
|
|
1175
1270
|
reason: validation.reason,
|
|
1176
1271
|
error: validation.error ? new Error(validation.error) : undefined,
|
|
1177
1272
|
existingId: validation.existingId,
|
|
1273
|
+
field: validation.field,
|
|
1178
1274
|
};
|
|
1179
1275
|
}
|
|
1276
|
+
if (data.kind === 'page') {
|
|
1277
|
+
for (const [locale, localeData] of Object.entries(data.locales)) {
|
|
1278
|
+
const refused = this.mdxPagesPathnameRefusal(data.type, localeData.pathname ?? '', locale);
|
|
1279
|
+
if (refused) {
|
|
1280
|
+
return { success: false, reason: refused.reason, error: new Error(refused.error), field: 'pathname' };
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1180
1284
|
const id = generateContentId();
|
|
1181
1285
|
const now = data.created || new Date().toISOString();
|
|
1182
1286
|
try {
|
|
@@ -1299,7 +1403,7 @@ export class FileSystemContentAPI {
|
|
|
1299
1403
|
meta,
|
|
1300
1404
|
kind: data.kind,
|
|
1301
1405
|
pathname: data.kind === 'page' ? localeData.pathname : undefined,
|
|
1302
|
-
name: data.kind === '
|
|
1406
|
+
name: data.kind === 'fragment' || data.kind === 'data' ? data.name : undefined,
|
|
1303
1407
|
publishAt: localeData.publishAt && typeof localeData.publishAt === 'string' ? localeData.publishAt : undefined,
|
|
1304
1408
|
unpublishAt: localeData.unpublishAt && typeof localeData.unpublishAt === 'string' ? localeData.unpublishAt : undefined,
|
|
1305
1409
|
});
|
|
@@ -1408,7 +1512,7 @@ export class FileSystemContentAPI {
|
|
|
1408
1512
|
// No changes and no explicit timestamp, keep current
|
|
1409
1513
|
modified = current.modified;
|
|
1410
1514
|
}
|
|
1411
|
-
const { sortedMeta: newMeta, valid: metaValid, error: metaError, } = validateAndPrepareMetadata(
|
|
1515
|
+
const { sortedMeta: newMeta, valid: metaValid, error: metaError, } = validateAndPrepareMetadata(applyMetaPatch(current.meta, data.meta ?? {}));
|
|
1412
1516
|
// Validate new metadata
|
|
1413
1517
|
if (!newMeta || !metaValid) {
|
|
1414
1518
|
return {
|
|
@@ -1419,58 +1523,59 @@ export class FileSystemContentAPI {
|
|
|
1419
1523
|
}
|
|
1420
1524
|
// Merge updates
|
|
1421
1525
|
const newContent = data.content ? { ...current.content, ...data.content } : current.content;
|
|
1422
|
-
//
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
error: new Error(`${kindLabel} name "${data.name}" is already in use${existingTitle ? ` by "${existingTitle}"` : ''}`),
|
|
1451
|
-
};
|
|
1526
|
+
// A page's new path is tidied, and refused when it is malformed or another page has it, as a
|
|
1527
|
+
// create's is; the tidied form names the file and keys the page in the site index. An unchanged
|
|
1528
|
+
// path is left alone, so a page keeps a path from before the checks.
|
|
1529
|
+
let pathname = data.pathname;
|
|
1530
|
+
if (pathname !== undefined && pathname !== localeVersion.pathname && manifest.kind === 'page') {
|
|
1531
|
+
// Own-key only: the manifest's site name comes from the on-disk layout
|
|
1532
|
+
// and is never checked against the configured sites, so an inherited key
|
|
1533
|
+
// would reach the calls below.
|
|
1534
|
+
const site = manifest.site && Object.hasOwn(this.sites, manifest.site) ? this.sites[manifest.site] : undefined;
|
|
1535
|
+
const checked = checkPagePathname(site, pathname, locale, id);
|
|
1536
|
+
if (!checked.valid) {
|
|
1537
|
+
return { success: false, reason: checked.reason, error: new Error(checked.error) };
|
|
1538
|
+
}
|
|
1539
|
+
const refused = this.mdxPagesPathnameRefusal(manifest.type, checked.pathname, locale);
|
|
1540
|
+
if (refused) {
|
|
1541
|
+
return { success: false, reason: refused.reason, error: new Error(refused.error) };
|
|
1542
|
+
}
|
|
1543
|
+
pathname = checked.pathname;
|
|
1544
|
+
}
|
|
1545
|
+
// A new fragment or data name passes the create's checks. An unchanged name is left alone, so
|
|
1546
|
+
// an entry keeps a name from before the checks.
|
|
1547
|
+
if (data.name !== undefined &&
|
|
1548
|
+
data.name !== localeVersion.name &&
|
|
1549
|
+
(manifest.kind === 'fragment' || manifest.kind === 'data')) {
|
|
1550
|
+
const entries = manifest.kind === 'fragment' ? this.fragments : this.data;
|
|
1551
|
+
const refused = checkEntryName(manifest.kind, manifest.collection, data.name, (name) => manifest.kind === 'fragment' ? this.fragments.isFragmentNameValid(name) : this.data.isDataNameValid(name), (name) => entries.getNameConflict(manifest.collection, name, id));
|
|
1552
|
+
if (refused) {
|
|
1553
|
+
return { success: false, reason: refused.reason, error: new Error(refused.error) };
|
|
1452
1554
|
}
|
|
1453
1555
|
}
|
|
1454
1556
|
// Handle pathname changes - track history only for published content
|
|
1455
|
-
const pathnameHistory = buildPathnameHistory(localeVersion.pathname,
|
|
1557
|
+
const pathnameHistory = buildPathnameHistory(localeVersion.pathname, pathname, isContentPublished({
|
|
1456
1558
|
publishAt: resolvePublishDate(data.publishAt, current.publishAt),
|
|
1457
1559
|
unpublishAt: resolvePublishDate(data.unpublishAt, current.unpublishAt),
|
|
1458
1560
|
}), new Date().toISOString());
|
|
1459
1561
|
try {
|
|
1460
1562
|
// Determine the target file path (may be different if pathname or name changed)
|
|
1461
1563
|
let targetFilePath = filePath;
|
|
1462
|
-
if (
|
|
1564
|
+
if (pathname && localeVersion.pathname && pathname !== localeVersion.pathname && manifest.site) {
|
|
1463
1565
|
// For pages, calculate new file path based on new pathname
|
|
1464
1566
|
const newLocaleVersion = {
|
|
1465
1567
|
...localeVersion,
|
|
1466
|
-
pathname
|
|
1568
|
+
pathname,
|
|
1467
1569
|
};
|
|
1468
1570
|
targetFilePath = this.getFilePath(manifest, newLocaleVersion);
|
|
1469
1571
|
// Ensure target directory exists
|
|
1470
1572
|
await mkdir(dirname(targetFilePath), { recursive: true });
|
|
1471
1573
|
}
|
|
1472
|
-
else if (data.name &&
|
|
1473
|
-
|
|
1574
|
+
else if (data.name &&
|
|
1575
|
+
localeVersion.name &&
|
|
1576
|
+
data.name !== localeVersion.name &&
|
|
1577
|
+
(manifest.kind === 'fragment' || manifest.kind === 'data')) {
|
|
1578
|
+
// A fragment's or data entry's name is part of its file path, so a rename moves the file
|
|
1474
1579
|
const newLocaleVersion = {
|
|
1475
1580
|
...localeVersion,
|
|
1476
1581
|
name: data.name,
|
|
@@ -1564,7 +1669,7 @@ export class FileSystemContentAPI {
|
|
|
1564
1669
|
content: sortKeys(newContent, { deep: true }),
|
|
1565
1670
|
};
|
|
1566
1671
|
// Handle pathname changes and track history
|
|
1567
|
-
if (
|
|
1672
|
+
if (pathname && localeVersion.pathname && pathname !== localeVersion.pathname) {
|
|
1568
1673
|
// Only add to history if content is published
|
|
1569
1674
|
if (isContentPublished(current)) {
|
|
1570
1675
|
if (!updatedFile.previousPathnames) {
|
|
@@ -1636,7 +1741,7 @@ export class FileSystemContentAPI {
|
|
|
1636
1741
|
etag: newEtag,
|
|
1637
1742
|
modified,
|
|
1638
1743
|
meta: newMeta,
|
|
1639
|
-
...(
|
|
1744
|
+
...(pathname && { pathname }),
|
|
1640
1745
|
...(data.name && { name: data.name }),
|
|
1641
1746
|
};
|
|
1642
1747
|
// Handle publish dates using shared function
|
|
@@ -1668,16 +1773,21 @@ export class FileSystemContentAPI {
|
|
|
1668
1773
|
}
|
|
1669
1774
|
}
|
|
1670
1775
|
// Store the old locale manifest for rollback if needed
|
|
1671
|
-
const oldLocaleVersion =
|
|
1776
|
+
const oldLocaleVersion = localeVersion;
|
|
1672
1777
|
try {
|
|
1673
|
-
//
|
|
1674
|
-
|
|
1675
|
-
// Update index with the updated locale
|
|
1778
|
+
// Replace the locale so its old path/name mappings are removed first.
|
|
1779
|
+
this.contentIndex.removeLocale(id, locale);
|
|
1676
1780
|
this.contentIndex.addContent(manifest, updatedLocaleVersion, newContent);
|
|
1677
1781
|
// If pathname changed, delete the old file
|
|
1678
1782
|
if (targetFilePath !== filePath) {
|
|
1679
1783
|
try {
|
|
1680
|
-
|
|
1784
|
+
// A name changed only in letter case is the same file on a case-insensitive disk: the
|
|
1785
|
+
// write went into it, so deleting the old path would delete the entry. Renaming it
|
|
1786
|
+
// gives the file its new name's case.
|
|
1787
|
+
if (await isSameFile(filePath, targetFilePath))
|
|
1788
|
+
await rename(filePath, targetFilePath);
|
|
1789
|
+
else
|
|
1790
|
+
await unlink(filePath);
|
|
1681
1791
|
}
|
|
1682
1792
|
catch (error) {
|
|
1683
1793
|
// Log but don't fail if old file deletion fails
|
|
@@ -1686,10 +1796,12 @@ export class FileSystemContentAPI {
|
|
|
1686
1796
|
}
|
|
1687
1797
|
}
|
|
1688
1798
|
catch (error) {
|
|
1689
|
-
// Rollback on error to maintain
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1799
|
+
// Rollback on error to maintain the original locale and name mapping.
|
|
1800
|
+
this.contentIndex.removeLocale(id, locale);
|
|
1801
|
+
this.contentIndex.addContent(manifest, oldLocaleVersion, current.content);
|
|
1802
|
+
// If we wrote to a new file, try to clean it up. A name changed only in letter case wrote
|
|
1803
|
+
// into the entry's own file on a case-insensitive disk, and that file is kept.
|
|
1804
|
+
if (targetFilePath !== filePath && !(await isSameFile(filePath, targetFilePath))) {
|
|
1693
1805
|
try {
|
|
1694
1806
|
await unlink(targetFilePath);
|
|
1695
1807
|
}
|
|
@@ -1740,23 +1852,32 @@ export class FileSystemContentAPI {
|
|
|
1740
1852
|
error: new Error('Invalid etag - must match at least one locale'),
|
|
1741
1853
|
};
|
|
1742
1854
|
}
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1855
|
+
// Settled, not all: Promise.all let one rejection abandon the others
|
|
1856
|
+
// AFTER they had already unlinked, so the caller was told the delete
|
|
1857
|
+
// failed while the content was gone and the index still listed it. A file
|
|
1858
|
+
// already absent (git checkout, a manual rm) is the common cause, and for
|
|
1859
|
+
// a delete that is the desired end state, not an error.
|
|
1860
|
+
const localeVersions = Array.from(localesOf(manifest));
|
|
1861
|
+
const outcomes = await Promise.allSettled(localeVersions.map(async (localeVersion) => {
|
|
1862
|
+
await rm(this.getFilePath(manifest, localeVersion), { force: true });
|
|
1863
|
+
}));
|
|
1864
|
+
const failures = outcomes.filter((outcome) => outcome.status === 'rejected');
|
|
1865
|
+
if (failures.length === 0) {
|
|
1751
1866
|
this.contentIndex.removeEntry(id);
|
|
1752
1867
|
return { success: true };
|
|
1753
1868
|
}
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1869
|
+
// Some files are gone and some are not. Drop exactly what was removed so
|
|
1870
|
+
// the index still describes what is on disk; the survivors stay indexed
|
|
1871
|
+
// rather than becoming unreachable orphans.
|
|
1872
|
+
for (const [position, outcome] of outcomes.entries()) {
|
|
1873
|
+
if (outcome.status === 'fulfilled') {
|
|
1874
|
+
this.contentIndex.removeLocale(id, localeVersions[position].locale);
|
|
1875
|
+
}
|
|
1759
1876
|
}
|
|
1877
|
+
return {
|
|
1878
|
+
success: false,
|
|
1879
|
+
error: ensureError(failures[0].reason),
|
|
1880
|
+
};
|
|
1760
1881
|
}
|
|
1761
1882
|
async deleteLocalized(input) {
|
|
1762
1883
|
const { id, locale, etag } = input;
|
|
@@ -1787,19 +1908,27 @@ export class FileSystemContentAPI {
|
|
|
1787
1908
|
error: new Error('Invalid etag for locale'),
|
|
1788
1909
|
};
|
|
1789
1910
|
}
|
|
1790
|
-
// Don't allow deleting the last locale
|
|
1791
|
-
|
|
1911
|
+
// Don't allow deleting the last locale. Count actual locales, not
|
|
1912
|
+
// manifest keys — removeLocale clears a locale by assigning undefined
|
|
1913
|
+
// and keeping the key, so after an earlier translation delete the key
|
|
1914
|
+
// count still includes the dead slot and the guard would wave the
|
|
1915
|
+
// genuinely last locale through.
|
|
1916
|
+
if (Array.from(localesOf(manifest)).length === 1) {
|
|
1792
1917
|
return {
|
|
1793
1918
|
success: false,
|
|
1794
1919
|
error: new Error('Cannot delete the last locale. Use deleteContent to delete all content.'),
|
|
1795
1920
|
};
|
|
1796
1921
|
}
|
|
1797
1922
|
try {
|
|
1798
|
-
// Delete the locale file
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
//
|
|
1802
|
-
manifest
|
|
1923
|
+
// Delete the locale file. A file already absent (git checkout, a
|
|
1924
|
+
// manual rm) is the desired end state of a delete, not an error —
|
|
1925
|
+
// same stance deleteContent takes. Failing on it left the deleted
|
|
1926
|
+
// translation routable and cached while reporting failure.
|
|
1927
|
+
await rm(this.getFilePath(manifest, localeVersion), { force: true });
|
|
1928
|
+
// Remove locale from manifest and index. removeLocale clears the
|
|
1929
|
+
// manifest field itself, and returns early if it is already cleared —
|
|
1930
|
+
// clearing it here first skipped the pathname-index and content-cache
|
|
1931
|
+
// cleanup, leaving the deleted translation routable and cached.
|
|
1803
1932
|
this.contentIndex.removeLocale(manifest.id, locale);
|
|
1804
1933
|
return { success: true };
|
|
1805
1934
|
}
|
|
@@ -1811,11 +1940,12 @@ export class FileSystemContentAPI {
|
|
|
1811
1940
|
}
|
|
1812
1941
|
}
|
|
1813
1942
|
getSite(siteName) {
|
|
1814
|
-
if (siteName === '
|
|
1815
|
-
throw new Error('Use `
|
|
1943
|
+
if (siteName === 'fragments') {
|
|
1944
|
+
throw new Error('Use `fragments` property instead of getSite("fragments")');
|
|
1816
1945
|
}
|
|
1817
|
-
|
|
1818
|
-
|
|
1946
|
+
// Own-key only: the name is caller-supplied, and an inherited key like
|
|
1947
|
+
// `constructor` would resolve to a truthy non-Site.
|
|
1948
|
+
return Object.hasOwn(this.sites, siteName) ? this.sites[siteName] : null;
|
|
1819
1949
|
}
|
|
1820
1950
|
*listAllContent(filters) {
|
|
1821
1951
|
yield* filterContentWithIndexes(this.contentIndex, filters, this.sitesConfig);
|
|
@@ -1852,14 +1982,14 @@ export class FileSystemContentAPI {
|
|
|
1852
1982
|
try {
|
|
1853
1983
|
await readFile(filePath);
|
|
1854
1984
|
}
|
|
1855
|
-
catch
|
|
1985
|
+
catch {
|
|
1856
1986
|
// File doesn't exist - remove from index
|
|
1857
1987
|
const parsed = this.parseFilePath(filePath);
|
|
1858
1988
|
if (parsed) {
|
|
1859
1989
|
// Find the content ID to remove
|
|
1860
1990
|
let manifest = null;
|
|
1861
|
-
if (parsed.kind === '
|
|
1862
|
-
manifest = this.contentIndex.
|
|
1991
|
+
if (parsed.kind === 'fragment' && parsed.name) {
|
|
1992
|
+
manifest = this.contentIndex.getFragmentByName(parsed.collection, parsed.name, parsed.locale);
|
|
1863
1993
|
}
|
|
1864
1994
|
else if (parsed.kind === 'data' && parsed.name) {
|
|
1865
1995
|
manifest = this.contentIndex.getDataByName(parsed.collection, parsed.name, parsed.locale);
|
|
@@ -1867,6 +1997,12 @@ export class FileSystemContentAPI {
|
|
|
1867
1997
|
else if (parsed.kind === 'page' && parsed.site && parsed.pathname) {
|
|
1868
1998
|
manifest = this.contentIndex.getByPathname(parsed.site, parsed.pathname, parsed.locale);
|
|
1869
1999
|
}
|
|
2000
|
+
// A name finds an entry when any of its locales has it. A save that renames one locale
|
|
2001
|
+
// writes its new file and then deletes the old one, whose name the other locales still
|
|
2002
|
+
// carry: only a locale whose own name is the deleted file's has lost its file.
|
|
2003
|
+
if (parsed.kind !== 'page' && manifest?.locales[parsed.locale]?.name !== parsed.name) {
|
|
2004
|
+
manifest = null;
|
|
2005
|
+
}
|
|
1870
2006
|
if (manifest) {
|
|
1871
2007
|
// Track the deletion
|
|
1872
2008
|
deleted.push({
|
|
@@ -1879,8 +2015,8 @@ export class FileSystemContentAPI {
|
|
|
1879
2015
|
this.contentIndex.removeEntry(manifest.id);
|
|
1880
2016
|
}
|
|
1881
2017
|
else {
|
|
1882
|
-
// Remove just this locale
|
|
1883
|
-
|
|
2018
|
+
// Remove just this locale. removeLocale clears the manifest field itself and returns early
|
|
2019
|
+
// if it is already cleared, so clearing it here first left the locale's name taken.
|
|
1884
2020
|
this.contentIndex.removeLocale(manifest.id, parsed.locale);
|
|
1885
2021
|
}
|
|
1886
2022
|
filesDeleted++;
|
|
@@ -1897,9 +2033,9 @@ export class FileSystemContentAPI {
|
|
|
1897
2033
|
}
|
|
1898
2034
|
// Check if this path is already indexed
|
|
1899
2035
|
let isIndexed = false;
|
|
1900
|
-
if (parsed.kind === '
|
|
1901
|
-
// Check
|
|
1902
|
-
const manifest = this.contentIndex.
|
|
2036
|
+
if (parsed.kind === 'fragment' && parsed.name) {
|
|
2037
|
+
// Check fragment index by name
|
|
2038
|
+
const manifest = this.contentIndex.getFragmentByName(parsed.collection, parsed.name, parsed.locale);
|
|
1903
2039
|
isIndexed = !!manifest;
|
|
1904
2040
|
}
|
|
1905
2041
|
else if (parsed.kind === 'page' && parsed.site && parsed.pathname) {
|
|
@@ -1921,8 +2057,8 @@ export class FileSystemContentAPI {
|
|
|
1921
2057
|
// For specific paths, check if the file has changed
|
|
1922
2058
|
const fileStats = await readFile(filePath);
|
|
1923
2059
|
let currentManifest = null;
|
|
1924
|
-
if (parsed.kind === '
|
|
1925
|
-
currentManifest = this.contentIndex.
|
|
2060
|
+
if (parsed.kind === 'fragment' && parsed.name) {
|
|
2061
|
+
currentManifest = this.contentIndex.getFragmentByName(parsed.collection, parsed.name, parsed.locale);
|
|
1926
2062
|
}
|
|
1927
2063
|
else if (parsed.kind === 'data' && parsed.name) {
|
|
1928
2064
|
currentManifest = this.contentIndex.getDataByName(parsed.collection, parsed.name, parsed.locale);
|
|
@@ -1962,7 +2098,7 @@ export class FileSystemContentAPI {
|
|
|
1962
2098
|
// Only reindex if etag changed
|
|
1963
2099
|
const existingEtag = currentManifest.locales[parsed.locale]?.etag;
|
|
1964
2100
|
if (existingEtag === currentEtag) {
|
|
1965
|
-
const relativePath = filePath.replace(this.absoluteContentRoot
|
|
2101
|
+
const relativePath = filePath.replace(`${this.absoluteContentRoot}/`, '');
|
|
1966
2102
|
console.log(`[Content] skip ${relativePath} (unchanged)`);
|
|
1967
2103
|
filesSkipped++;
|
|
1968
2104
|
}
|
|
@@ -1983,6 +2119,16 @@ export class FileSystemContentAPI {
|
|
|
1983
2119
|
}
|
|
1984
2120
|
// Index the files
|
|
1985
2121
|
const buffers = Array.from({ length: Math.min(filesToIndex.length, 100) }, () => new Uint8Array(4096));
|
|
2122
|
+
// Drop the previous verdicts for every file this pass re-examines, so
|
|
2123
|
+
// problems do not pile up across reindexes. A full scan re-examines every
|
|
2124
|
+
// file that is not already indexed — and a file with a problem is never
|
|
2125
|
+
// indexed — so it can start from an empty slate.
|
|
2126
|
+
if (paths) {
|
|
2127
|
+
this.contentIndex.dropProblems(new Set(filesToIndex));
|
|
2128
|
+
}
|
|
2129
|
+
else {
|
|
2130
|
+
this.contentIndex.clearProblems();
|
|
2131
|
+
}
|
|
1986
2132
|
for (let i = 0; i < filesToIndex.length; i++) {
|
|
1987
2133
|
const filePath = filesToIndex[i];
|
|
1988
2134
|
const bufferIndex = i % buffers.length;
|
|
@@ -1990,6 +2136,22 @@ export class FileSystemContentAPI {
|
|
|
1990
2136
|
const result = await this.parseFileHeaderWithRepair(filePath, buffers[bufferIndex]);
|
|
1991
2137
|
if (result) {
|
|
1992
2138
|
const { manifest, localeVersion, content } = result;
|
|
2139
|
+
// A different file already claims this id+locale. Its file still
|
|
2140
|
+
// being on disk is what tells a stray copy (refuse, or it merges
|
|
2141
|
+
// over the original and a page silently stops existing) apart from
|
|
2142
|
+
// an out-of-band rename (the old path is gone — this file is the
|
|
2143
|
+
// entry's new home and must keep merging).
|
|
2144
|
+
const claimant = this.contentIndex.getManifest(manifest.id);
|
|
2145
|
+
const claimedLocale = claimant?.locales[localeVersion.locale];
|
|
2146
|
+
if (claimant && claimedLocale) {
|
|
2147
|
+
const claimantPath = this.getFilePath(claimant, claimedLocale);
|
|
2148
|
+
const claimantOnDisk = await access(claimantPath).then(() => true, () => false);
|
|
2149
|
+
if (claimantPath !== filePath && claimantOnDisk) {
|
|
2150
|
+
this.recordIndexProblem(filePath, 'duplicate-id', duplicateIdMessage(manifest.id, localeVersion.locale, claimantPath));
|
|
2151
|
+
filesSkipped++;
|
|
2152
|
+
continue;
|
|
2153
|
+
}
|
|
2154
|
+
}
|
|
1993
2155
|
// Add to index
|
|
1994
2156
|
this.contentIndex.addContent(manifest, localeVersion, content);
|
|
1995
2157
|
filesProcessed++;
|
|
@@ -2006,7 +2168,8 @@ export class FileSystemContentAPI {
|
|
|
2006
2168
|
}
|
|
2007
2169
|
}
|
|
2008
2170
|
catch (error) {
|
|
2009
|
-
console.error(`Failed to reindex ${filePath}
|
|
2171
|
+
console.error(`Failed to reindex ${filePath}: ${messageOf(error)}`);
|
|
2172
|
+
this.recordIndexProblem(filePath, 'index-error', messageOf(error));
|
|
2010
2173
|
errors++;
|
|
2011
2174
|
}
|
|
2012
2175
|
}
|
|
@@ -2033,7 +2196,7 @@ export class FileSystemContentAPI {
|
|
|
2033
2196
|
}
|
|
2034
2197
|
async getContentAsBuffer(id, locale) {
|
|
2035
2198
|
const manifest = this.contentIndex.getManifest(id);
|
|
2036
|
-
if (!manifest
|
|
2199
|
+
if (!manifest?.locales[locale]) {
|
|
2037
2200
|
return null;
|
|
2038
2201
|
}
|
|
2039
2202
|
const localeVersion = manifest.locales[locale];
|
|
@@ -2139,7 +2302,7 @@ export class FileSystemContentAPI {
|
|
|
2139
2302
|
}
|
|
2140
2303
|
// Validate metadata size if updating
|
|
2141
2304
|
if (op.data.meta) {
|
|
2142
|
-
const newMeta =
|
|
2305
|
+
const newMeta = applyMetaPatch(current.meta, op.data.meta);
|
|
2143
2306
|
const validation = validateAndPrepareMetadata(newMeta);
|
|
2144
2307
|
if (!validation.valid) {
|
|
2145
2308
|
return {
|
|
@@ -2179,3 +2342,9 @@ export class FileSystemContentAPI {
|
|
|
2179
2342
|
};
|
|
2180
2343
|
}
|
|
2181
2344
|
}
|
|
2345
|
+
/** Whether two paths name one file, as 'hero.en.mdx' and 'Hero.en.mdx' do on a case-insensitive disk. */
|
|
2346
|
+
async function isSameFile(a, b) {
|
|
2347
|
+
const [statA, statB] = await Promise.all([stat(a), stat(b)]).catch(() => [null, null]);
|
|
2348
|
+
return statA !== null && statB !== null && statA.dev === statB.dev && statA.ino === statB.ino;
|
|
2349
|
+
}
|
|
2350
|
+
//# sourceMappingURL=filesystem-content-api.js.map
|