@conloca/content-api 0.1.16 → 0.2.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/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 +18 -13
- package/dist/asset-operations.d.ts.map +1 -1
- package/dist/asset-operations.js +302 -254
- 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 +10 -0
- package/dist/asset-usage.d.ts.map +1 -0
- package/dist/asset-usage.js +49 -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 +59 -0
- package/dist/content-path.d.ts.map +1 -0
- package/dist/content-path.js +88 -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 +407 -281
- 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 +6 -6
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +170 -119
- 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 +914 -0
- package/src/asset-types.ts +74 -0
- package/src/asset-usage.ts +47 -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 +113 -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 +2685 -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 +1341 -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 { 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,23 @@ function ensureError(error) {
|
|
|
22
23
|
}
|
|
23
24
|
// Reusable TextDecoder instance - safe to reuse as it's stateless
|
|
24
25
|
const textDecoder = new TextDecoder();
|
|
26
|
+
async function readFilePrefix(filePath, buffer) {
|
|
27
|
+
const handle = await open(filePath, 'r');
|
|
28
|
+
try {
|
|
29
|
+
return (await handle.read(buffer, 0, buffer.byteLength, 0)).bytesRead;
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
await handle.close();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
25
35
|
// Parse 4KB MDX content - extracts frontmatter from buffer
|
|
26
36
|
function parse4KBMDX(buffer, bytesRead) {
|
|
27
37
|
// Only convert to string what we need
|
|
28
38
|
const partialContent = textDecoder.decode(buffer.subarray(0, bytesRead));
|
|
39
|
+
// Empty options opt out of gray-matter's module-level memo cache — a plain
|
|
40
|
+
// object keyed by the raw input, so inputs naming an Object.prototype member
|
|
41
|
+
// ('valueOf', 'toString', …) return the inherited member instead of a parse.
|
|
42
|
+
// Same at every matter() call below.
|
|
29
43
|
const { data: frontmatter } = matter(partialContent, {});
|
|
30
44
|
// Find where content starts (after second ---)
|
|
31
45
|
const delimiter = new TextEncoder().encode('---');
|
|
@@ -68,7 +82,7 @@ function parse4KBMDX(buffer, bytesRead) {
|
|
|
68
82
|
*
|
|
69
83
|
* Stores content as files on disk in a git-friendly format:
|
|
70
84
|
* - Pages: /{site}/pages/{filename}.{locale}.vxjson
|
|
71
|
-
* -
|
|
85
|
+
* - Fragments: /fragments/{collection}/{filename}.{locale}.{mdx|vxjson}
|
|
72
86
|
*
|
|
73
87
|
* ## Indexing
|
|
74
88
|
*
|
|
@@ -102,35 +116,53 @@ function parse4KBMDX(buffer, bytesRead) {
|
|
|
102
116
|
export async function readSitesConfig(contentRoot) {
|
|
103
117
|
const absoluteContentRoot = resolve(contentRoot);
|
|
104
118
|
const sitesPath = join(absoluteContentRoot, 'sites.json');
|
|
119
|
+
let content;
|
|
105
120
|
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;
|
|
121
|
+
content = await readFile(sitesPath, 'utf-8');
|
|
121
122
|
}
|
|
122
123
|
catch {
|
|
123
|
-
//
|
|
124
|
+
// A genuinely absent sites.json is a normal starting state.
|
|
124
125
|
console.warn(`Warning: sites.json is missing at ${sitesPath}. Using empty configuration.`);
|
|
125
126
|
return { sites: {}, globalLocales: ['en'] };
|
|
126
127
|
}
|
|
128
|
+
// Tolerating a malformed sites.json is deliberate ("Handle invalid sites.json gracefully"). What
|
|
129
|
+
// was not deliberate is the diagnosis: this used to share the catch above
|
|
130
|
+
// and report the file as MISSING, naming a path where it plainly sits,
|
|
131
|
+
// while every page silently dropped out of every site. Say what is
|
|
132
|
+
// actually wrong; the tolerance itself is unchanged.
|
|
133
|
+
let parsed;
|
|
134
|
+
try {
|
|
135
|
+
parsed = JSON.parse(content);
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
console.warn(`Warning: sites.json at ${sitesPath} is not valid JSON (${messageOf(error)}). ` +
|
|
139
|
+
'Using empty configuration — every site will be empty until this is fixed.');
|
|
140
|
+
return { sites: {}, globalLocales: ['en'] };
|
|
141
|
+
}
|
|
142
|
+
// Validate the structure. `null` is valid JSON, so `parsed` itself needs
|
|
143
|
+
// the null check before anything reads a property off it.
|
|
144
|
+
if (!parsed?.sites || typeof parsed.sites !== 'object') {
|
|
145
|
+
console.warn(`Warning: sites.json has invalid structure at ${sitesPath}. Expected 'sites' property. Using default configuration.`);
|
|
146
|
+
return { sites: {}, globalLocales: ['en'] };
|
|
147
|
+
}
|
|
148
|
+
if (!parsed.globalLocales || !Array.isArray(parsed.globalLocales)) {
|
|
149
|
+
console.warn(`Warning: sites.json is missing 'globalLocales' array at ${sitesPath}. Using default global locales.`);
|
|
150
|
+
return {
|
|
151
|
+
sites: parsed.sites,
|
|
152
|
+
globalLocales: ['en'],
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return parsed;
|
|
156
|
+
}
|
|
157
|
+
function messageOf(error) {
|
|
158
|
+
return error instanceof Error ? error.message : String(error);
|
|
127
159
|
}
|
|
128
160
|
export class FileSystemContentAPI {
|
|
129
161
|
contentRoot;
|
|
130
162
|
absoluteContentRoot;
|
|
131
163
|
contentIndex;
|
|
132
164
|
sitesConfig;
|
|
133
|
-
|
|
165
|
+
fragments;
|
|
134
166
|
data;
|
|
135
167
|
sites;
|
|
136
168
|
// mdx-page support: optional second filesystem root for kind:'page'+type:'mdx'.
|
|
@@ -150,7 +182,7 @@ export class FileSystemContentAPI {
|
|
|
150
182
|
get normalizedContentRoot() {
|
|
151
183
|
return this.contentRoot;
|
|
152
184
|
}
|
|
153
|
-
constructor(contentRoot,
|
|
185
|
+
constructor(contentRoot, sitesConfig, contentIndex, mdxPagesOptions, localeOptions) {
|
|
154
186
|
// Normalize contentRoot: remove ./ prefix for relative paths, keep absolute paths as-is
|
|
155
187
|
if (contentRoot.startsWith('./')) {
|
|
156
188
|
this.contentRoot = contentRoot.slice(2);
|
|
@@ -165,7 +197,6 @@ export class FileSystemContentAPI {
|
|
|
165
197
|
}
|
|
166
198
|
// Store absolute path for file operations
|
|
167
199
|
this.absoluteContentRoot = resolve(this.contentRoot);
|
|
168
|
-
// TODO: Use canvasDir for canvas/unlinked components
|
|
169
200
|
this.sitesConfig = sitesConfig;
|
|
170
201
|
this.contentIndex = contentIndex;
|
|
171
202
|
// Resolve which site owns mdx-type pages. Resolution order:
|
|
@@ -217,8 +248,8 @@ export class FileSystemContentAPI {
|
|
|
217
248
|
}
|
|
218
249
|
merged.add(this.mdxPagesDefaultLocale);
|
|
219
250
|
this.availableLocales = merged;
|
|
220
|
-
// Initialize
|
|
221
|
-
this.
|
|
251
|
+
// Initialize fragments
|
|
252
|
+
this.fragments = new Fragments(this, this.contentIndex.getFragmentIndex());
|
|
222
253
|
// Initialize data
|
|
223
254
|
this.data = new Data(this, this.contentIndex.getDataIndex());
|
|
224
255
|
// Initialize all sites
|
|
@@ -231,13 +262,12 @@ export class FileSystemContentAPI {
|
|
|
231
262
|
});
|
|
232
263
|
}
|
|
233
264
|
static async create(options) {
|
|
234
|
-
const { canvasDir } = options;
|
|
235
265
|
// Keep contentRoot as provided (will be normalized in constructor)
|
|
236
266
|
const contentRoot = options.contentRoot;
|
|
237
267
|
// Load sites config (canonical home for per-site fields including mdxPages).
|
|
238
268
|
let sitesConfig = await readSitesConfig(contentRoot);
|
|
239
269
|
// Apply the top-level locales option as the authoritative source.
|
|
240
|
-
// We expand sitesConfig so that the
|
|
270
|
+
// We expand sitesConfig so that the FragmentIndex (which keys on
|
|
241
271
|
// globalLocales) and the resolved SiteIndex (which keys on the
|
|
242
272
|
// site's locales array) both have slots for every option-declared
|
|
243
273
|
// locale. Without this, the parser would correctly identify a file's
|
|
@@ -267,7 +297,7 @@ export class FileSystemContentAPI {
|
|
|
267
297
|
const normalizedContentRoot = contentRoot.startsWith('./') ? contentRoot.slice(2) : contentRoot;
|
|
268
298
|
const contentIndex = await ContentIndex.getCachedOrCreate(normalizedContentRoot, sitesConfig);
|
|
269
299
|
// Create instance (not cached)
|
|
270
|
-
const api = new FileSystemContentAPI(contentRoot,
|
|
300
|
+
const api = new FileSystemContentAPI(contentRoot, sitesConfig, contentIndex, {
|
|
271
301
|
root: options.mdxPagesRoot,
|
|
272
302
|
defaultLocale: options.mdxPagesDefaultLocale,
|
|
273
303
|
site: options.mdxPagesSite,
|
|
@@ -275,68 +305,119 @@ export class FileSystemContentAPI {
|
|
|
275
305
|
availableLocales: options.availableLocales,
|
|
276
306
|
defaultLocale: options.defaultLocale,
|
|
277
307
|
});
|
|
278
|
-
// Index all content files (only if index is empty)
|
|
308
|
+
// Index all content files (only if index is empty).
|
|
309
|
+
//
|
|
310
|
+
// FileSystemContentAPI continues to hydrate via parseFileHeaderWithRepair
|
|
311
|
+
// (legacy path), which performs read-repair side effects on disk (renaming
|
|
312
|
+
// missing-locale files, rewriting non-canonical VXJSON, adding missing
|
|
313
|
+
// id/created/modified). ContentIndex.indexAll(backend) is the newer,
|
|
314
|
+
// backend-agnostic hydration path for content that is not on disk;
|
|
315
|
+
// FileSystemContentAPI's behavior must stay identical, so this call site
|
|
316
|
+
// keeps the legacy path until the repair side effects move behind a backend.
|
|
279
317
|
if (contentIndex.entryCount === 0) {
|
|
280
318
|
await api.indexAllContent();
|
|
281
319
|
}
|
|
282
320
|
return api;
|
|
283
321
|
}
|
|
284
322
|
/**
|
|
285
|
-
*
|
|
323
|
+
* Hydrate the content index for this FileSystemContentAPI by scanning the
|
|
324
|
+
* content root and running parseFileHeaderWithRepair on each file. This is
|
|
325
|
+
* the legacy hydration path that preserves the filesystem read-repair
|
|
326
|
+
* behavior (file renames, canonical-form rewrites, id/timestamp backfill).
|
|
327
|
+
*
|
|
328
|
+
* ContentIndex.indexAll(backend) is the backend-agnostic replacement for
|
|
329
|
+
* content that is not on disk; this private method exists so FileSystemContentAPI
|
|
330
|
+
* can keep its repair-on-read side effects until they are lifted onto a
|
|
331
|
+
* dedicated FileSystemReaderBackend field.
|
|
286
332
|
*/
|
|
287
|
-
static clearCaches() {
|
|
288
|
-
ContentIndex.clearCache();
|
|
289
|
-
// TODO: CanvasIndex.clearCache() when implemented
|
|
290
|
-
}
|
|
291
333
|
async indexAllContent() {
|
|
292
|
-
//
|
|
334
|
+
// Also drops the previous problems: they describe an index that no longer exists.
|
|
293
335
|
this.contentIndex.clear();
|
|
294
|
-
// Scan content directory
|
|
295
336
|
const files = await this.scanContentFiles();
|
|
296
|
-
// Use optimized batch size based on benchmarks
|
|
297
337
|
const OPTIMAL_BATCH_SIZE = 1000;
|
|
298
338
|
console.debug(`Indexing ${files.length} content files...`);
|
|
299
|
-
//
|
|
300
|
-
//
|
|
301
|
-
|
|
302
|
-
|
|
339
|
+
// Which file claimed each id+locale — same guard ContentIndex.indexAll
|
|
340
|
+
// carries. Without it a duplicated file merges into the first one's
|
|
341
|
+
// locale slot and a page silently stops existing, and this legacy path
|
|
342
|
+
// is the one every local dev server and Astro build hydrates through.
|
|
343
|
+
const claimedBy = new Map();
|
|
344
|
+
// Reuse a small pool of 4KB buffers across the batch; matches the
|
|
345
|
+
// existing reindex() loop. parseFileHeaderWithRepair owns the buffer
|
|
346
|
+
// contract (reads up to 4096 bytes from offset 0).
|
|
347
|
+
const buffers = Array.from({ length: Math.min(files.length, 100) }, () => new Uint8Array(4096));
|
|
303
348
|
for (let i = 0; i < files.length; i += OPTIMAL_BATCH_SIZE) {
|
|
304
349
|
const batch = files.slice(i, i + OPTIMAL_BATCH_SIZE);
|
|
305
|
-
|
|
306
|
-
|
|
350
|
+
await Promise.all(batch.map(async (filePath, batchIndex) => {
|
|
351
|
+
const buffer = buffers[(i + batchIndex) % buffers.length];
|
|
307
352
|
try {
|
|
308
|
-
const result = await this.parseFileHeaderWithRepair(filePath,
|
|
353
|
+
const result = await this.parseFileHeaderWithRepair(filePath, buffer);
|
|
309
354
|
if (result) {
|
|
310
|
-
|
|
311
|
-
//
|
|
312
|
-
|
|
355
|
+
// Same reason as the shared indexAll loop: a page under a site
|
|
356
|
+
// sites.json does not declare would be filed nowhere and
|
|
357
|
+
// silently vanish from every lookup and build.
|
|
358
|
+
if (result.manifest.kind === 'page' &&
|
|
359
|
+
result.manifest.site &&
|
|
360
|
+
!this.contentIndex.hasSite(result.manifest.site)) {
|
|
361
|
+
this.recordIndexProblem(filePath, 'unknown-site', unknownSiteMessage(result.manifest.site));
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
const claim = `${result.manifest.id}::${result.localeVersion.locale}`;
|
|
365
|
+
const claimant = claimedBy.get(claim);
|
|
366
|
+
if (claimant !== undefined && claimant !== filePath) {
|
|
367
|
+
this.recordIndexProblem(filePath, 'duplicate-id', duplicateIdMessage(result.manifest.id, result.localeVersion.locale, claimant));
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
claimedBy.set(claim, filePath);
|
|
371
|
+
this.contentIndex.addContent(result.manifest, result.localeVersion, result.content);
|
|
313
372
|
}
|
|
314
373
|
}
|
|
315
374
|
catch (error) {
|
|
316
|
-
console.error(`Failed to index ${filePath}
|
|
375
|
+
console.error(`Failed to index ${filePath}: ${messageOf(error)}`);
|
|
376
|
+
this.recordIndexProblem(filePath, 'index-error', messageOf(error));
|
|
317
377
|
}
|
|
318
378
|
}));
|
|
319
379
|
}
|
|
320
380
|
}
|
|
381
|
+
/**
|
|
382
|
+
* Record a file the indexer could not read. Signature matches the `onError`
|
|
383
|
+
* callback of ContentIndex.indexAll so it can be handed over unchanged when
|
|
384
|
+
* this class migrates off the legacy repair-on-read hydration path. The
|
|
385
|
+
* problems live on the index itself, not on this instance: the index is
|
|
386
|
+
* cached per content root, and a later instance over the same root reuses it
|
|
387
|
+
* without re-indexing, so instance-local problems would vanish.
|
|
388
|
+
*/
|
|
389
|
+
recordIndexProblem(path, reason, message) {
|
|
390
|
+
this.contentIndex.recordProblem(path, reason, message);
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Files the indexer could not read. They are absent from listAllContent(),
|
|
394
|
+
* so any caller that treats that list as complete — `conloca doctor`, most
|
|
395
|
+
* of all — must consult this to avoid reporting success over broken content.
|
|
396
|
+
*/
|
|
397
|
+
getIndexProblems() {
|
|
398
|
+
return this.contentIndex.getProblems();
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Clear all caches - useful for testing
|
|
402
|
+
*/
|
|
403
|
+
static clearCaches() {
|
|
404
|
+
ContentIndex.clearCache();
|
|
405
|
+
// TODO: CanvasIndex.clearCache() when implemented
|
|
406
|
+
}
|
|
321
407
|
async parseFileHeaderWithRepair(originalFilePath, buffer) {
|
|
322
|
-
let handle;
|
|
323
408
|
let bytesRead = 0;
|
|
324
409
|
let filePath = originalFilePath; // Use a local variable that can be updated if file is renamed
|
|
325
410
|
try {
|
|
326
|
-
|
|
327
|
-
bytesRead = (await handle.read(buffer, 0, 4096, 0)).bytesRead;
|
|
411
|
+
bytesRead = await readFilePrefix(filePath, buffer);
|
|
328
412
|
}
|
|
329
413
|
catch (error) {
|
|
330
414
|
console.error(`Error reading ${filePath}:`, error);
|
|
331
415
|
throw error;
|
|
332
416
|
}
|
|
333
|
-
finally {
|
|
334
|
-
await handle?.close();
|
|
335
|
-
}
|
|
336
417
|
try {
|
|
337
418
|
// Now parse the buffer content
|
|
338
419
|
// Parse file to extract metadata
|
|
339
|
-
let relativePath = filePath.replace(this.absoluteContentRoot
|
|
420
|
+
let relativePath = filePath.replace(`${this.absoluteContentRoot}/`, '');
|
|
340
421
|
let parts = relativePath.split('/');
|
|
341
422
|
// Determine site/collection structure
|
|
342
423
|
let site;
|
|
@@ -351,11 +432,11 @@ export class FileSystemContentAPI {
|
|
|
351
432
|
// leading prefix is stripped here; locale lives as a directory segment
|
|
352
433
|
// ({locale}/{slug}.mdx for non-default locales, {slug}.mdx at the root
|
|
353
434
|
// for the default locale).
|
|
354
|
-
if (this.absoluteMdxPagesRoot && filePath.startsWith(this.absoluteMdxPagesRoot
|
|
435
|
+
if (this.absoluteMdxPagesRoot && filePath.startsWith(`${this.absoluteMdxPagesRoot}/`)) {
|
|
355
436
|
kind = 'page';
|
|
356
437
|
site = this.mdxPagesSite;
|
|
357
438
|
collection = this.mdxPagesCollection;
|
|
358
|
-
const mdxRelativePath = filePath.replace(this.absoluteMdxPagesRoot
|
|
439
|
+
const mdxRelativePath = filePath.replace(`${this.absoluteMdxPagesRoot}/`, '');
|
|
359
440
|
const mdxParts = mdxRelativePath.split('/');
|
|
360
441
|
const mdxFilename = mdxParts[mdxParts.length - 1];
|
|
361
442
|
const noLocaleMatch = mdxFilename.match(/^(.+)\.mdx$/);
|
|
@@ -370,77 +451,75 @@ export class FileSystemContentAPI {
|
|
|
370
451
|
const slugParts = mdxParts.slice(1);
|
|
371
452
|
pathname =
|
|
372
453
|
slugParts[slugParts.length - 1] === 'index'
|
|
373
|
-
?
|
|
374
|
-
:
|
|
454
|
+
? `/${slugParts.slice(0, -1).join('/')}`
|
|
455
|
+
: `/${slugParts.join('/')}`;
|
|
375
456
|
}
|
|
376
457
|
else {
|
|
377
458
|
locale = this.mdxPagesDefaultLocale;
|
|
378
459
|
pathname =
|
|
379
460
|
mdxParts[mdxParts.length - 1] === 'index'
|
|
380
|
-
?
|
|
381
|
-
:
|
|
461
|
+
? `/${mdxParts.slice(0, -1).join('/')}`
|
|
462
|
+
: `/${mdxParts.join('/')}`;
|
|
382
463
|
}
|
|
383
464
|
parsedData = parse4KBMDX(buffer, bytesRead);
|
|
384
465
|
}
|
|
385
|
-
else if (parts[0] === '
|
|
386
|
-
kind = '
|
|
387
|
-
// Check if
|
|
466
|
+
else if (parts[0] === 'fragments') {
|
|
467
|
+
kind = 'fragment';
|
|
468
|
+
// Check if fragment is directly in fragments/ directory (no collection subdirectory)
|
|
388
469
|
if (parts.length === 2) {
|
|
389
|
-
//
|
|
390
|
-
collection = '
|
|
470
|
+
// Keep the direct layout unless the repair move succeeds.
|
|
471
|
+
collection = '';
|
|
391
472
|
const filename = parts[1];
|
|
392
473
|
// Create the general directory if it doesn't exist
|
|
393
|
-
const generalDir = join(this.absoluteContentRoot, '
|
|
474
|
+
const generalDir = join(this.absoluteContentRoot, 'fragments', 'general');
|
|
394
475
|
try {
|
|
395
476
|
await mkdir(generalDir, { recursive: true });
|
|
396
477
|
}
|
|
397
|
-
catch
|
|
478
|
+
catch {
|
|
398
479
|
// Directory might already exist, that's fine
|
|
399
480
|
}
|
|
400
481
|
// Move the file to the general collection
|
|
401
482
|
const newFilePath = join(generalDir, filename);
|
|
402
483
|
try {
|
|
403
|
-
await handle?.close(); // Close the current file handle before moving
|
|
404
|
-
handle = undefined;
|
|
405
484
|
// Check if target file already exists
|
|
406
485
|
try {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
console.warn(`Cannot move ${filename} to blocks/general/: target file already exists`);
|
|
486
|
+
await access(newFilePath);
|
|
487
|
+
console.warn(`Cannot move ${filename} to fragments/general/: target file already exists`);
|
|
410
488
|
}
|
|
411
|
-
catch
|
|
489
|
+
catch {
|
|
412
490
|
// Target doesn't exist, safe to move
|
|
413
491
|
await rename(filePath, newFilePath);
|
|
414
|
-
console.log(`Repaired: Moved ${filename} to
|
|
415
|
-
//
|
|
492
|
+
console.log(`Repaired: Moved ${filename} to fragments/general/ collection`);
|
|
493
|
+
// The file now uses the repaired collection layout.
|
|
494
|
+
collection = 'general';
|
|
416
495
|
filePath = newFilePath;
|
|
417
496
|
// Update relativePath and parts for the new file path
|
|
418
|
-
relativePath = filePath.replace(this.absoluteContentRoot
|
|
497
|
+
relativePath = filePath.replace(`${this.absoluteContentRoot}/`, '');
|
|
419
498
|
parts = relativePath.split('/');
|
|
420
|
-
|
|
421
|
-
handle = await open(filePath, 'r');
|
|
422
|
-
bytesRead = (await handle.read(buffer, 0, 4096, 0)).bytesRead;
|
|
499
|
+
bytesRead = await readFilePrefix(filePath, buffer);
|
|
423
500
|
}
|
|
424
501
|
}
|
|
425
502
|
catch (moveError) {
|
|
426
503
|
console.error(`Failed to move ${filename} to general collection:`, moveError);
|
|
427
504
|
// Continue processing with original file
|
|
428
|
-
|
|
429
|
-
bytesRead = (await handle.read(buffer, 0, 4096, 0)).bytesRead;
|
|
505
|
+
bytesRead = await readFilePrefix(filePath, buffer);
|
|
430
506
|
}
|
|
431
507
|
}
|
|
432
508
|
else {
|
|
433
|
-
//
|
|
509
|
+
// Fragment is in a collection subdirectory
|
|
434
510
|
collection = parts[1];
|
|
435
511
|
}
|
|
436
512
|
const filename = parts[parts.length - 1];
|
|
437
513
|
// 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
|
-
|
|
514
|
+
// Also handle files without locale: hero.mdx -> name: hero, locale: default from sites.json.
|
|
515
|
+
// The locale grammar is the shared one (content-path.ts): a suffix that is not
|
|
516
|
+
// a locale ('hero.v2.mdx') takes the rename repair below instead of being
|
|
517
|
+
// indexed under a made-up locale.
|
|
518
|
+
const localeSuffix = parseLocaleFilename(filename);
|
|
440
519
|
const matchWithoutLocale = filename.match(/^(.+)\.(mdx|vxjson)$/);
|
|
441
|
-
if (
|
|
442
|
-
name =
|
|
443
|
-
locale =
|
|
520
|
+
if (localeSuffix && localeSuffix.extension !== 'json') {
|
|
521
|
+
name = localeSuffix.base;
|
|
522
|
+
locale = localeSuffix.locale;
|
|
444
523
|
}
|
|
445
524
|
else if (matchWithoutLocale) {
|
|
446
525
|
name = matchWithoutLocale[1];
|
|
@@ -454,15 +533,11 @@ export class FileSystemContentAPI {
|
|
|
454
533
|
console.log(`Will rename to: ${newFilename} at ${newFilePath}`);
|
|
455
534
|
// Check if target file already exists
|
|
456
535
|
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();
|
|
536
|
+
await access(newFilePath);
|
|
462
537
|
// Target file exists, skip renaming to avoid conflict
|
|
463
538
|
console.warn(`Cannot rename ${filename} to ${newFilename}: target file already exists`);
|
|
464
539
|
}
|
|
465
|
-
catch
|
|
540
|
+
catch {
|
|
466
541
|
// Target file doesn't exist, safe to rename
|
|
467
542
|
console.log(`Target file doesn't exist, proceeding with rename`);
|
|
468
543
|
try {
|
|
@@ -471,17 +546,14 @@ export class FileSystemContentAPI {
|
|
|
471
546
|
// Update filePath to the new path for further processing
|
|
472
547
|
filePath = newFilePath;
|
|
473
548
|
// Update relativePath and parts for the new file path
|
|
474
|
-
relativePath = filePath.replace(this.absoluteContentRoot
|
|
549
|
+
relativePath = filePath.replace(`${this.absoluteContentRoot}/`, '');
|
|
475
550
|
parts = relativePath.split('/');
|
|
476
|
-
|
|
477
|
-
handle = await open(filePath, 'r');
|
|
478
|
-
bytesRead = (await handle.read(buffer, 0, 4096, 0)).bytesRead;
|
|
551
|
+
bytesRead = await readFilePrefix(filePath, buffer);
|
|
479
552
|
}
|
|
480
553
|
catch (renameError) {
|
|
481
554
|
console.error(`Failed to rename ${filename}:`, renameError);
|
|
482
555
|
// Continue processing with original file
|
|
483
|
-
|
|
484
|
-
bytesRead = (await handle.read(buffer, 0, 4096, 0)).bytesRead;
|
|
556
|
+
bytesRead = await readFilePrefix(filePath, buffer);
|
|
485
557
|
}
|
|
486
558
|
}
|
|
487
559
|
}
|
|
@@ -498,12 +570,12 @@ export class FileSystemContentAPI {
|
|
|
498
570
|
kind = 'data';
|
|
499
571
|
collection = parts[1];
|
|
500
572
|
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 =
|
|
573
|
+
// Extract name and locale from filename: testimonial.en.json -> name: testimonial, locale: en.
|
|
574
|
+
// The locale grammar is the shared one (content-path.ts).
|
|
575
|
+
const localeSuffix = parseLocaleFilename(filename);
|
|
576
|
+
if (localeSuffix?.extension === 'json') {
|
|
577
|
+
name = localeSuffix.base;
|
|
578
|
+
locale = localeSuffix.locale;
|
|
507
579
|
}
|
|
508
580
|
// Parse JSON to get ID, metadata, and data
|
|
509
581
|
const content = textDecoder.decode(buffer.subarray(0, bytesRead));
|
|
@@ -522,7 +594,8 @@ export class FileSystemContentAPI {
|
|
|
522
594
|
};
|
|
523
595
|
}
|
|
524
596
|
catch (error) {
|
|
525
|
-
console.error(`Failed to parse JSON data file ${filePath}
|
|
597
|
+
console.error(`Failed to parse JSON data file ${filePath}: ${messageOf(error)}`);
|
|
598
|
+
this.recordIndexProblem(filePath, 'malformed-json', messageOf(error));
|
|
526
599
|
return null;
|
|
527
600
|
}
|
|
528
601
|
}
|
|
@@ -539,17 +612,24 @@ export class FileSystemContentAPI {
|
|
|
539
612
|
// above) and won't reach this branch.
|
|
540
613
|
const pathParts = parts.slice(2); // Remove site/collection
|
|
541
614
|
const filename = pathParts[pathParts.length - 1];
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
: '/' + pathParts.join('/');
|
|
615
|
+
// One grammar with the header parser and every other reader (content-path.ts).
|
|
616
|
+
// A page file with no parseable locale suffix is NOT content: indexing it
|
|
617
|
+
// under the fallback locale filed it with no pathname at all, invisible to
|
|
618
|
+
// every pathname lookup while still occupying that locale slot. parseFilePath
|
|
619
|
+
// (the reindex path) has always refused these — now both halves agree.
|
|
620
|
+
const localeSuffix = filename ? parseLocaleFilename(filename) : null;
|
|
621
|
+
if (!localeSuffix || localeSuffix.extension === 'json') {
|
|
622
|
+
console.warn(`Skipping page file without a locale suffix: ${filePath}`);
|
|
623
|
+
return null;
|
|
552
624
|
}
|
|
625
|
+
locale = localeSuffix.locale;
|
|
626
|
+
// Replace filename with basename
|
|
627
|
+
pathParts[pathParts.length - 1] = localeSuffix.base;
|
|
628
|
+
// index files map to parent: /index -> /, /foo/index -> /foo
|
|
629
|
+
pathname =
|
|
630
|
+
pathParts[pathParts.length - 1] === 'index'
|
|
631
|
+
? `/${pathParts.slice(0, -1).join('/')}`
|
|
632
|
+
: `/${pathParts.join('/')}`;
|
|
553
633
|
// Parse format-appropriately. parse4KBMDX reads frontmatter from the
|
|
554
634
|
// MDX header; VXJSON.parse4KB reads the leading JSON metadata.
|
|
555
635
|
if (filePath.endsWith('.mdx')) {
|
|
@@ -575,6 +655,7 @@ export class FileSystemContentAPI {
|
|
|
575
655
|
...(parsedData.previousPathnames && { previousPathnames: parsedData.previousPathnames }),
|
|
576
656
|
...(name && { name }),
|
|
577
657
|
meta: parsedData.meta || { title: name || 'Untitled' },
|
|
658
|
+
sourcePath: filePath,
|
|
578
659
|
};
|
|
579
660
|
// Build ContentManifest (without content)
|
|
580
661
|
const manifest = {
|
|
@@ -592,7 +673,7 @@ export class FileSystemContentAPI {
|
|
|
592
673
|
// Check if we need to repair the file first (missing required fields)
|
|
593
674
|
if (needsRepair) {
|
|
594
675
|
// Perform repair and get the repaired content
|
|
595
|
-
const repairedData = await this.performReadRepair(filePath, manifest
|
|
676
|
+
const repairedData = await this.performReadRepair(filePath, manifest);
|
|
596
677
|
if (repairedData) {
|
|
597
678
|
content = repairedData.content;
|
|
598
679
|
localeVersion.etag = repairedData.etag;
|
|
@@ -601,6 +682,7 @@ export class FileSystemContentAPI {
|
|
|
601
682
|
}
|
|
602
683
|
// Repair failed, skip this file
|
|
603
684
|
console.error(`Failed to repair ${filePath}, skipping`);
|
|
685
|
+
this.recordIndexProblem(filePath, 'repair-failed', 'Read-repair failed');
|
|
604
686
|
return null;
|
|
605
687
|
}
|
|
606
688
|
if (bytesRead < 4096 ||
|
|
@@ -639,7 +721,7 @@ export class FileSystemContentAPI {
|
|
|
639
721
|
if (!etagResult.success) {
|
|
640
722
|
// Content field is not last or invalid JSON - repair immediately
|
|
641
723
|
// Perform repair and get the repaired content
|
|
642
|
-
const repairedData = await this.performReadRepair(filePath, manifest
|
|
724
|
+
const repairedData = await this.performReadRepair(filePath, manifest);
|
|
643
725
|
if (repairedData) {
|
|
644
726
|
content = repairedData.content;
|
|
645
727
|
localeVersion.etag = repairedData.etag;
|
|
@@ -648,6 +730,7 @@ export class FileSystemContentAPI {
|
|
|
648
730
|
}
|
|
649
731
|
// Repair failed, skip this file
|
|
650
732
|
console.error(`Failed to repair ${filePath}, skipping`);
|
|
733
|
+
this.recordIndexProblem(filePath, 'repair-failed', 'Read-repair failed');
|
|
651
734
|
return null;
|
|
652
735
|
}
|
|
653
736
|
localeVersion.etag = etagResult.contentEtag
|
|
@@ -659,7 +742,8 @@ export class FileSystemContentAPI {
|
|
|
659
742
|
return { manifest, locale, localeVersion, bytesRead, content };
|
|
660
743
|
}
|
|
661
744
|
catch (error) {
|
|
662
|
-
console.error(`Error indexing ${originalFilePath}
|
|
745
|
+
console.error(`Error indexing ${originalFilePath}: ${messageOf(error)}`);
|
|
746
|
+
this.recordIndexProblem(originalFilePath, 'parse-failed', messageOf(error));
|
|
663
747
|
return null;
|
|
664
748
|
}
|
|
665
749
|
}
|
|
@@ -667,7 +751,7 @@ export class FileSystemContentAPI {
|
|
|
667
751
|
* Perform read-repair by writing back the file with missing fields
|
|
668
752
|
* This ensures stable IDs and timestamps across restarts
|
|
669
753
|
*/
|
|
670
|
-
async performReadRepair(filePath, manifest
|
|
754
|
+
async performReadRepair(filePath, manifest) {
|
|
671
755
|
try {
|
|
672
756
|
const now = new Date().toISOString();
|
|
673
757
|
let needsWrite = false;
|
|
@@ -799,7 +883,7 @@ export class FileSystemContentAPI {
|
|
|
799
883
|
}
|
|
800
884
|
}
|
|
801
885
|
}
|
|
802
|
-
catch
|
|
886
|
+
catch {
|
|
803
887
|
// Directory doesn't exist yet
|
|
804
888
|
}
|
|
805
889
|
}
|
|
@@ -816,7 +900,7 @@ export class FileSystemContentAPI {
|
|
|
816
900
|
// practice.
|
|
817
901
|
if (this.absoluteMdxPagesRoot &&
|
|
818
902
|
this.absoluteMdxPagesRoot !== this.absoluteContentRoot &&
|
|
819
|
-
!this.absoluteMdxPagesRoot.startsWith(this.absoluteContentRoot
|
|
903
|
+
!this.absoluteMdxPagesRoot.startsWith(`${this.absoluteContentRoot}/`)) {
|
|
820
904
|
async function scanMdxPages(dir) {
|
|
821
905
|
try {
|
|
822
906
|
const entries = await readdir(dir, { withFileTypes: true });
|
|
@@ -830,7 +914,7 @@ export class FileSystemContentAPI {
|
|
|
830
914
|
}
|
|
831
915
|
}
|
|
832
916
|
}
|
|
833
|
-
catch
|
|
917
|
+
catch {
|
|
834
918
|
// Directory doesn't exist yet
|
|
835
919
|
}
|
|
836
920
|
}
|
|
@@ -838,10 +922,23 @@ export class FileSystemContentAPI {
|
|
|
838
922
|
}
|
|
839
923
|
return files;
|
|
840
924
|
}
|
|
925
|
+
/**
|
|
926
|
+
* Why a page of `type` cannot have `pathname` in `locale` where `getFilePath` puts it: an mdx
|
|
927
|
+
* page lives in the MDX pages folder when one is configured, where a default-language path may
|
|
928
|
+
* not start with a language folder (see `checkMdxPagesPathname`).
|
|
929
|
+
*/
|
|
930
|
+
mdxPagesPathnameRefusal(type, pathname, locale) {
|
|
931
|
+
if (type !== 'mdx' || !this.absoluteMdxPagesRoot)
|
|
932
|
+
return null;
|
|
933
|
+
return checkMdxPagesPathname(pathname, locale, {
|
|
934
|
+
defaultLocale: this.mdxPagesDefaultLocale,
|
|
935
|
+
locales: this.availableLocales,
|
|
936
|
+
});
|
|
937
|
+
}
|
|
841
938
|
getFilePath(manifest, { locale, name, pathname }) {
|
|
842
|
-
if (manifest.kind === '
|
|
939
|
+
if (manifest.kind === 'fragment') {
|
|
843
940
|
const ext = manifest.type === 'mdx' ? 'mdx' : 'vxjson';
|
|
844
|
-
return join(this.absoluteContentRoot, '
|
|
941
|
+
return join(this.absoluteContentRoot, 'fragments', manifest.collection, `${name}.${locale}.${ext}`);
|
|
845
942
|
}
|
|
846
943
|
if (manifest.kind === 'data') {
|
|
847
944
|
return join(this.absoluteContentRoot, 'data', manifest.collection, `${name}.${locale}.json`);
|
|
@@ -877,8 +974,8 @@ export class FileSystemContentAPI {
|
|
|
877
974
|
// their position relative to absoluteContentRoot. Mirror the parsing
|
|
878
975
|
// performed in parseFileHeaderWithRepair so both code paths agree on
|
|
879
976
|
// the derived (pathname, locale) tuple.
|
|
880
|
-
if (this.absoluteMdxPagesRoot && filePath.startsWith(this.absoluteMdxPagesRoot
|
|
881
|
-
const mdxRelativePath = filePath.replace(this.absoluteMdxPagesRoot
|
|
977
|
+
if (this.absoluteMdxPagesRoot && filePath.startsWith(`${this.absoluteMdxPagesRoot}/`)) {
|
|
978
|
+
const mdxRelativePath = filePath.replace(`${this.absoluteMdxPagesRoot}/`, '');
|
|
882
979
|
const mdxParts = mdxRelativePath.split('/');
|
|
883
980
|
const mdxFilename = mdxParts[mdxParts.length - 1];
|
|
884
981
|
let locale;
|
|
@@ -894,15 +991,13 @@ export class FileSystemContentAPI {
|
|
|
894
991
|
const slugParts = mdxParts.slice(1);
|
|
895
992
|
pathname =
|
|
896
993
|
slugParts[slugParts.length - 1] === 'index'
|
|
897
|
-
?
|
|
898
|
-
:
|
|
994
|
+
? `/${slugParts.slice(0, -1).join('/')}`
|
|
995
|
+
: `/${slugParts.join('/')}`;
|
|
899
996
|
}
|
|
900
997
|
else {
|
|
901
998
|
locale = this.mdxPagesDefaultLocale;
|
|
902
999
|
pathname =
|
|
903
|
-
mdxParts[mdxParts.length - 1] === 'index'
|
|
904
|
-
? '/' + mdxParts.slice(0, -1).join('/') || '/'
|
|
905
|
-
: '/' + mdxParts.join('/');
|
|
1000
|
+
mdxParts[mdxParts.length - 1] === 'index' ? `/${mdxParts.slice(0, -1).join('/')}` : `/${mdxParts.join('/')}`;
|
|
906
1001
|
}
|
|
907
1002
|
return {
|
|
908
1003
|
kind: 'page',
|
|
@@ -912,55 +1007,12 @@ export class FileSystemContentAPI {
|
|
|
912
1007
|
locale,
|
|
913
1008
|
};
|
|
914
1009
|
}
|
|
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;
|
|
1010
|
+
// Everything under the primary content root shares ONE grammar with the
|
|
1011
|
+
// header parser and every other reader (content-path.ts) — including the
|
|
1012
|
+
// `index` -> parent-pathname rule this method used to skip. Missing it made
|
|
1013
|
+
// reindex look up `/index`, never find the entry the indexer had filed under
|
|
1014
|
+
// `/`, and leave a deleted index page in the index forever.
|
|
1015
|
+
return parseContentPathLayout(filePath.replace(`${this.absoluteContentRoot}/`, ''));
|
|
964
1016
|
}
|
|
965
1017
|
async getContent(id) {
|
|
966
1018
|
// Get manifest for this ID
|
|
@@ -1003,7 +1055,7 @@ export class FileSystemContentAPI {
|
|
|
1003
1055
|
await this.reindex();
|
|
1004
1056
|
// Try again with updated index
|
|
1005
1057
|
const updatedManifest = this.contentIndex.getManifest(id);
|
|
1006
|
-
if (!updatedManifest
|
|
1058
|
+
if (!updatedManifest?.locales[locale]) {
|
|
1007
1059
|
return null;
|
|
1008
1060
|
}
|
|
1009
1061
|
return this.getLocalized(id, locale);
|
|
@@ -1162,21 +1214,30 @@ export class FileSystemContentAPI {
|
|
|
1162
1214
|
}
|
|
1163
1215
|
return result;
|
|
1164
1216
|
}
|
|
1165
|
-
catch
|
|
1217
|
+
catch {
|
|
1166
1218
|
return null;
|
|
1167
1219
|
}
|
|
1168
1220
|
}
|
|
1169
1221
|
async createContent(data) {
|
|
1170
1222
|
// Use shared validation
|
|
1171
|
-
const validation = validateCreateContent(data, this.sites, this.
|
|
1223
|
+
const validation = validateCreateContent(data, this.sites, this.fragments, this.data);
|
|
1172
1224
|
if (!validation.valid) {
|
|
1173
1225
|
return {
|
|
1174
1226
|
success: false,
|
|
1175
1227
|
reason: validation.reason,
|
|
1176
1228
|
error: validation.error ? new Error(validation.error) : undefined,
|
|
1177
1229
|
existingId: validation.existingId,
|
|
1230
|
+
field: validation.field,
|
|
1178
1231
|
};
|
|
1179
1232
|
}
|
|
1233
|
+
if (data.kind === 'page') {
|
|
1234
|
+
for (const [locale, localeData] of Object.entries(data.locales)) {
|
|
1235
|
+
const refused = this.mdxPagesPathnameRefusal(data.type, localeData.pathname ?? '', locale);
|
|
1236
|
+
if (refused) {
|
|
1237
|
+
return { success: false, reason: refused.reason, error: new Error(refused.error), field: 'pathname' };
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1180
1241
|
const id = generateContentId();
|
|
1181
1242
|
const now = data.created || new Date().toISOString();
|
|
1182
1243
|
try {
|
|
@@ -1299,7 +1360,7 @@ export class FileSystemContentAPI {
|
|
|
1299
1360
|
meta,
|
|
1300
1361
|
kind: data.kind,
|
|
1301
1362
|
pathname: data.kind === 'page' ? localeData.pathname : undefined,
|
|
1302
|
-
name: data.kind === '
|
|
1363
|
+
name: data.kind === 'fragment' || data.kind === 'data' ? data.name : undefined,
|
|
1303
1364
|
publishAt: localeData.publishAt && typeof localeData.publishAt === 'string' ? localeData.publishAt : undefined,
|
|
1304
1365
|
unpublishAt: localeData.unpublishAt && typeof localeData.unpublishAt === 'string' ? localeData.unpublishAt : undefined,
|
|
1305
1366
|
});
|
|
@@ -1408,7 +1469,7 @@ export class FileSystemContentAPI {
|
|
|
1408
1469
|
// No changes and no explicit timestamp, keep current
|
|
1409
1470
|
modified = current.modified;
|
|
1410
1471
|
}
|
|
1411
|
-
const { sortedMeta: newMeta, valid: metaValid, error: metaError, } = validateAndPrepareMetadata(
|
|
1472
|
+
const { sortedMeta: newMeta, valid: metaValid, error: metaError, } = validateAndPrepareMetadata(applyMetaPatch(current.meta, data.meta ?? {}));
|
|
1412
1473
|
// Validate new metadata
|
|
1413
1474
|
if (!newMeta || !metaValid) {
|
|
1414
1475
|
return {
|
|
@@ -1419,58 +1480,59 @@ export class FileSystemContentAPI {
|
|
|
1419
1480
|
}
|
|
1420
1481
|
// Merge updates
|
|
1421
1482
|
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
|
-
};
|
|
1483
|
+
// A page's new path is tidied, and refused when it is malformed or another page has it, as a
|
|
1484
|
+
// create's is; the tidied form names the file and keys the page in the site index. An unchanged
|
|
1485
|
+
// path is left alone, so a page keeps a path from before the checks.
|
|
1486
|
+
let pathname = data.pathname;
|
|
1487
|
+
if (pathname !== undefined && pathname !== localeVersion.pathname && manifest.kind === 'page') {
|
|
1488
|
+
// Own-key only: the manifest's site name comes from the on-disk layout
|
|
1489
|
+
// and is never checked against the configured sites, so an inherited key
|
|
1490
|
+
// would reach the calls below.
|
|
1491
|
+
const site = manifest.site && Object.hasOwn(this.sites, manifest.site) ? this.sites[manifest.site] : undefined;
|
|
1492
|
+
const checked = checkPagePathname(site, pathname, locale, id);
|
|
1493
|
+
if (!checked.valid) {
|
|
1494
|
+
return { success: false, reason: checked.reason, error: new Error(checked.error) };
|
|
1495
|
+
}
|
|
1496
|
+
const refused = this.mdxPagesPathnameRefusal(manifest.type, checked.pathname, locale);
|
|
1497
|
+
if (refused) {
|
|
1498
|
+
return { success: false, reason: refused.reason, error: new Error(refused.error) };
|
|
1499
|
+
}
|
|
1500
|
+
pathname = checked.pathname;
|
|
1501
|
+
}
|
|
1502
|
+
// A new fragment or data name passes the create's checks. An unchanged name is left alone, so
|
|
1503
|
+
// an entry keeps a name from before the checks.
|
|
1504
|
+
if (data.name !== undefined &&
|
|
1505
|
+
data.name !== localeVersion.name &&
|
|
1506
|
+
(manifest.kind === 'fragment' || manifest.kind === 'data')) {
|
|
1507
|
+
const entries = manifest.kind === 'fragment' ? this.fragments : this.data;
|
|
1508
|
+
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));
|
|
1509
|
+
if (refused) {
|
|
1510
|
+
return { success: false, reason: refused.reason, error: new Error(refused.error) };
|
|
1452
1511
|
}
|
|
1453
1512
|
}
|
|
1454
1513
|
// Handle pathname changes - track history only for published content
|
|
1455
|
-
const pathnameHistory = buildPathnameHistory(localeVersion.pathname,
|
|
1514
|
+
const pathnameHistory = buildPathnameHistory(localeVersion.pathname, pathname, isContentPublished({
|
|
1456
1515
|
publishAt: resolvePublishDate(data.publishAt, current.publishAt),
|
|
1457
1516
|
unpublishAt: resolvePublishDate(data.unpublishAt, current.unpublishAt),
|
|
1458
1517
|
}), new Date().toISOString());
|
|
1459
1518
|
try {
|
|
1460
1519
|
// Determine the target file path (may be different if pathname or name changed)
|
|
1461
1520
|
let targetFilePath = filePath;
|
|
1462
|
-
if (
|
|
1521
|
+
if (pathname && localeVersion.pathname && pathname !== localeVersion.pathname && manifest.site) {
|
|
1463
1522
|
// For pages, calculate new file path based on new pathname
|
|
1464
1523
|
const newLocaleVersion = {
|
|
1465
1524
|
...localeVersion,
|
|
1466
|
-
pathname
|
|
1525
|
+
pathname,
|
|
1467
1526
|
};
|
|
1468
1527
|
targetFilePath = this.getFilePath(manifest, newLocaleVersion);
|
|
1469
1528
|
// Ensure target directory exists
|
|
1470
1529
|
await mkdir(dirname(targetFilePath), { recursive: true });
|
|
1471
1530
|
}
|
|
1472
|
-
else if (data.name &&
|
|
1473
|
-
|
|
1531
|
+
else if (data.name &&
|
|
1532
|
+
localeVersion.name &&
|
|
1533
|
+
data.name !== localeVersion.name &&
|
|
1534
|
+
(manifest.kind === 'fragment' || manifest.kind === 'data')) {
|
|
1535
|
+
// A fragment's or data entry's name is part of its file path, so a rename moves the file
|
|
1474
1536
|
const newLocaleVersion = {
|
|
1475
1537
|
...localeVersion,
|
|
1476
1538
|
name: data.name,
|
|
@@ -1564,7 +1626,7 @@ export class FileSystemContentAPI {
|
|
|
1564
1626
|
content: sortKeys(newContent, { deep: true }),
|
|
1565
1627
|
};
|
|
1566
1628
|
// Handle pathname changes and track history
|
|
1567
|
-
if (
|
|
1629
|
+
if (pathname && localeVersion.pathname && pathname !== localeVersion.pathname) {
|
|
1568
1630
|
// Only add to history if content is published
|
|
1569
1631
|
if (isContentPublished(current)) {
|
|
1570
1632
|
if (!updatedFile.previousPathnames) {
|
|
@@ -1636,7 +1698,7 @@ export class FileSystemContentAPI {
|
|
|
1636
1698
|
etag: newEtag,
|
|
1637
1699
|
modified,
|
|
1638
1700
|
meta: newMeta,
|
|
1639
|
-
...(
|
|
1701
|
+
...(pathname && { pathname }),
|
|
1640
1702
|
...(data.name && { name: data.name }),
|
|
1641
1703
|
};
|
|
1642
1704
|
// Handle publish dates using shared function
|
|
@@ -1668,16 +1730,21 @@ export class FileSystemContentAPI {
|
|
|
1668
1730
|
}
|
|
1669
1731
|
}
|
|
1670
1732
|
// Store the old locale manifest for rollback if needed
|
|
1671
|
-
const oldLocaleVersion =
|
|
1733
|
+
const oldLocaleVersion = localeVersion;
|
|
1672
1734
|
try {
|
|
1673
|
-
//
|
|
1674
|
-
|
|
1675
|
-
// Update index with the updated locale
|
|
1735
|
+
// Replace the locale so its old path/name mappings are removed first.
|
|
1736
|
+
this.contentIndex.removeLocale(id, locale);
|
|
1676
1737
|
this.contentIndex.addContent(manifest, updatedLocaleVersion, newContent);
|
|
1677
1738
|
// If pathname changed, delete the old file
|
|
1678
1739
|
if (targetFilePath !== filePath) {
|
|
1679
1740
|
try {
|
|
1680
|
-
|
|
1741
|
+
// A name changed only in letter case is the same file on a case-insensitive disk: the
|
|
1742
|
+
// write went into it, so deleting the old path would delete the entry. Renaming it
|
|
1743
|
+
// gives the file its new name's case.
|
|
1744
|
+
if (await isSameFile(filePath, targetFilePath))
|
|
1745
|
+
await rename(filePath, targetFilePath);
|
|
1746
|
+
else
|
|
1747
|
+
await unlink(filePath);
|
|
1681
1748
|
}
|
|
1682
1749
|
catch (error) {
|
|
1683
1750
|
// Log but don't fail if old file deletion fails
|
|
@@ -1686,10 +1753,12 @@ export class FileSystemContentAPI {
|
|
|
1686
1753
|
}
|
|
1687
1754
|
}
|
|
1688
1755
|
catch (error) {
|
|
1689
|
-
// Rollback on error to maintain
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1756
|
+
// Rollback on error to maintain the original locale and name mapping.
|
|
1757
|
+
this.contentIndex.removeLocale(id, locale);
|
|
1758
|
+
this.contentIndex.addContent(manifest, oldLocaleVersion, current.content);
|
|
1759
|
+
// If we wrote to a new file, try to clean it up. A name changed only in letter case wrote
|
|
1760
|
+
// into the entry's own file on a case-insensitive disk, and that file is kept.
|
|
1761
|
+
if (targetFilePath !== filePath && !(await isSameFile(filePath, targetFilePath))) {
|
|
1693
1762
|
try {
|
|
1694
1763
|
await unlink(targetFilePath);
|
|
1695
1764
|
}
|
|
@@ -1740,23 +1809,32 @@ export class FileSystemContentAPI {
|
|
|
1740
1809
|
error: new Error('Invalid etag - must match at least one locale'),
|
|
1741
1810
|
};
|
|
1742
1811
|
}
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1812
|
+
// Settled, not all: Promise.all let one rejection abandon the others
|
|
1813
|
+
// AFTER they had already unlinked, so the caller was told the delete
|
|
1814
|
+
// failed while the content was gone and the index still listed it. A file
|
|
1815
|
+
// already absent (git checkout, a manual rm) is the common cause, and for
|
|
1816
|
+
// a delete that is the desired end state, not an error.
|
|
1817
|
+
const localeVersions = Array.from(localesOf(manifest));
|
|
1818
|
+
const outcomes = await Promise.allSettled(localeVersions.map(async (localeVersion) => {
|
|
1819
|
+
await rm(this.getFilePath(manifest, localeVersion), { force: true });
|
|
1820
|
+
}));
|
|
1821
|
+
const failures = outcomes.filter((outcome) => outcome.status === 'rejected');
|
|
1822
|
+
if (failures.length === 0) {
|
|
1751
1823
|
this.contentIndex.removeEntry(id);
|
|
1752
1824
|
return { success: true };
|
|
1753
1825
|
}
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1826
|
+
// Some files are gone and some are not. Drop exactly what was removed so
|
|
1827
|
+
// the index still describes what is on disk; the survivors stay indexed
|
|
1828
|
+
// rather than becoming unreachable orphans.
|
|
1829
|
+
for (const [position, outcome] of outcomes.entries()) {
|
|
1830
|
+
if (outcome.status === 'fulfilled') {
|
|
1831
|
+
this.contentIndex.removeLocale(id, localeVersions[position].locale);
|
|
1832
|
+
}
|
|
1759
1833
|
}
|
|
1834
|
+
return {
|
|
1835
|
+
success: false,
|
|
1836
|
+
error: ensureError(failures[0].reason),
|
|
1837
|
+
};
|
|
1760
1838
|
}
|
|
1761
1839
|
async deleteLocalized(input) {
|
|
1762
1840
|
const { id, locale, etag } = input;
|
|
@@ -1787,19 +1865,27 @@ export class FileSystemContentAPI {
|
|
|
1787
1865
|
error: new Error('Invalid etag for locale'),
|
|
1788
1866
|
};
|
|
1789
1867
|
}
|
|
1790
|
-
// Don't allow deleting the last locale
|
|
1791
|
-
|
|
1868
|
+
// Don't allow deleting the last locale. Count actual locales, not
|
|
1869
|
+
// manifest keys — removeLocale clears a locale by assigning undefined
|
|
1870
|
+
// and keeping the key, so after an earlier translation delete the key
|
|
1871
|
+
// count still includes the dead slot and the guard would wave the
|
|
1872
|
+
// genuinely last locale through.
|
|
1873
|
+
if (Array.from(localesOf(manifest)).length === 1) {
|
|
1792
1874
|
return {
|
|
1793
1875
|
success: false,
|
|
1794
1876
|
error: new Error('Cannot delete the last locale. Use deleteContent to delete all content.'),
|
|
1795
1877
|
};
|
|
1796
1878
|
}
|
|
1797
1879
|
try {
|
|
1798
|
-
// Delete the locale file
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
//
|
|
1802
|
-
manifest
|
|
1880
|
+
// Delete the locale file. A file already absent (git checkout, a
|
|
1881
|
+
// manual rm) is the desired end state of a delete, not an error —
|
|
1882
|
+
// same stance deleteContent takes. Failing on it left the deleted
|
|
1883
|
+
// translation routable and cached while reporting failure.
|
|
1884
|
+
await rm(this.getFilePath(manifest, localeVersion), { force: true });
|
|
1885
|
+
// Remove locale from manifest and index. removeLocale clears the
|
|
1886
|
+
// manifest field itself, and returns early if it is already cleared —
|
|
1887
|
+
// clearing it here first skipped the pathname-index and content-cache
|
|
1888
|
+
// cleanup, leaving the deleted translation routable and cached.
|
|
1803
1889
|
this.contentIndex.removeLocale(manifest.id, locale);
|
|
1804
1890
|
return { success: true };
|
|
1805
1891
|
}
|
|
@@ -1811,11 +1897,12 @@ export class FileSystemContentAPI {
|
|
|
1811
1897
|
}
|
|
1812
1898
|
}
|
|
1813
1899
|
getSite(siteName) {
|
|
1814
|
-
if (siteName === '
|
|
1815
|
-
throw new Error('Use `
|
|
1900
|
+
if (siteName === 'fragments') {
|
|
1901
|
+
throw new Error('Use `fragments` property instead of getSite("fragments")');
|
|
1816
1902
|
}
|
|
1817
|
-
|
|
1818
|
-
|
|
1903
|
+
// Own-key only: the name is caller-supplied, and an inherited key like
|
|
1904
|
+
// `constructor` would resolve to a truthy non-Site.
|
|
1905
|
+
return Object.hasOwn(this.sites, siteName) ? this.sites[siteName] : null;
|
|
1819
1906
|
}
|
|
1820
1907
|
*listAllContent(filters) {
|
|
1821
1908
|
yield* filterContentWithIndexes(this.contentIndex, filters, this.sitesConfig);
|
|
@@ -1852,14 +1939,14 @@ export class FileSystemContentAPI {
|
|
|
1852
1939
|
try {
|
|
1853
1940
|
await readFile(filePath);
|
|
1854
1941
|
}
|
|
1855
|
-
catch
|
|
1942
|
+
catch {
|
|
1856
1943
|
// File doesn't exist - remove from index
|
|
1857
1944
|
const parsed = this.parseFilePath(filePath);
|
|
1858
1945
|
if (parsed) {
|
|
1859
1946
|
// Find the content ID to remove
|
|
1860
1947
|
let manifest = null;
|
|
1861
|
-
if (parsed.kind === '
|
|
1862
|
-
manifest = this.contentIndex.
|
|
1948
|
+
if (parsed.kind === 'fragment' && parsed.name) {
|
|
1949
|
+
manifest = this.contentIndex.getFragmentByName(parsed.collection, parsed.name, parsed.locale);
|
|
1863
1950
|
}
|
|
1864
1951
|
else if (parsed.kind === 'data' && parsed.name) {
|
|
1865
1952
|
manifest = this.contentIndex.getDataByName(parsed.collection, parsed.name, parsed.locale);
|
|
@@ -1867,6 +1954,12 @@ export class FileSystemContentAPI {
|
|
|
1867
1954
|
else if (parsed.kind === 'page' && parsed.site && parsed.pathname) {
|
|
1868
1955
|
manifest = this.contentIndex.getByPathname(parsed.site, parsed.pathname, parsed.locale);
|
|
1869
1956
|
}
|
|
1957
|
+
// A name finds an entry when any of its locales has it. A save that renames one locale
|
|
1958
|
+
// writes its new file and then deletes the old one, whose name the other locales still
|
|
1959
|
+
// carry: only a locale whose own name is the deleted file's has lost its file.
|
|
1960
|
+
if (parsed.kind !== 'page' && manifest?.locales[parsed.locale]?.name !== parsed.name) {
|
|
1961
|
+
manifest = null;
|
|
1962
|
+
}
|
|
1870
1963
|
if (manifest) {
|
|
1871
1964
|
// Track the deletion
|
|
1872
1965
|
deleted.push({
|
|
@@ -1879,8 +1972,8 @@ export class FileSystemContentAPI {
|
|
|
1879
1972
|
this.contentIndex.removeEntry(manifest.id);
|
|
1880
1973
|
}
|
|
1881
1974
|
else {
|
|
1882
|
-
// Remove just this locale
|
|
1883
|
-
|
|
1975
|
+
// Remove just this locale. removeLocale clears the manifest field itself and returns early
|
|
1976
|
+
// if it is already cleared, so clearing it here first left the locale's name taken.
|
|
1884
1977
|
this.contentIndex.removeLocale(manifest.id, parsed.locale);
|
|
1885
1978
|
}
|
|
1886
1979
|
filesDeleted++;
|
|
@@ -1897,9 +1990,9 @@ export class FileSystemContentAPI {
|
|
|
1897
1990
|
}
|
|
1898
1991
|
// Check if this path is already indexed
|
|
1899
1992
|
let isIndexed = false;
|
|
1900
|
-
if (parsed.kind === '
|
|
1901
|
-
// Check
|
|
1902
|
-
const manifest = this.contentIndex.
|
|
1993
|
+
if (parsed.kind === 'fragment' && parsed.name) {
|
|
1994
|
+
// Check fragment index by name
|
|
1995
|
+
const manifest = this.contentIndex.getFragmentByName(parsed.collection, parsed.name, parsed.locale);
|
|
1903
1996
|
isIndexed = !!manifest;
|
|
1904
1997
|
}
|
|
1905
1998
|
else if (parsed.kind === 'page' && parsed.site && parsed.pathname) {
|
|
@@ -1921,8 +2014,8 @@ export class FileSystemContentAPI {
|
|
|
1921
2014
|
// For specific paths, check if the file has changed
|
|
1922
2015
|
const fileStats = await readFile(filePath);
|
|
1923
2016
|
let currentManifest = null;
|
|
1924
|
-
if (parsed.kind === '
|
|
1925
|
-
currentManifest = this.contentIndex.
|
|
2017
|
+
if (parsed.kind === 'fragment' && parsed.name) {
|
|
2018
|
+
currentManifest = this.contentIndex.getFragmentByName(parsed.collection, parsed.name, parsed.locale);
|
|
1926
2019
|
}
|
|
1927
2020
|
else if (parsed.kind === 'data' && parsed.name) {
|
|
1928
2021
|
currentManifest = this.contentIndex.getDataByName(parsed.collection, parsed.name, parsed.locale);
|
|
@@ -1962,7 +2055,7 @@ export class FileSystemContentAPI {
|
|
|
1962
2055
|
// Only reindex if etag changed
|
|
1963
2056
|
const existingEtag = currentManifest.locales[parsed.locale]?.etag;
|
|
1964
2057
|
if (existingEtag === currentEtag) {
|
|
1965
|
-
const relativePath = filePath.replace(this.absoluteContentRoot
|
|
2058
|
+
const relativePath = filePath.replace(`${this.absoluteContentRoot}/`, '');
|
|
1966
2059
|
console.log(`[Content] skip ${relativePath} (unchanged)`);
|
|
1967
2060
|
filesSkipped++;
|
|
1968
2061
|
}
|
|
@@ -1983,6 +2076,16 @@ export class FileSystemContentAPI {
|
|
|
1983
2076
|
}
|
|
1984
2077
|
// Index the files
|
|
1985
2078
|
const buffers = Array.from({ length: Math.min(filesToIndex.length, 100) }, () => new Uint8Array(4096));
|
|
2079
|
+
// Drop the previous verdicts for every file this pass re-examines, so
|
|
2080
|
+
// problems do not pile up across reindexes. A full scan re-examines every
|
|
2081
|
+
// file that is not already indexed — and a file with a problem is never
|
|
2082
|
+
// indexed — so it can start from an empty slate.
|
|
2083
|
+
if (paths) {
|
|
2084
|
+
this.contentIndex.dropProblems(new Set(filesToIndex));
|
|
2085
|
+
}
|
|
2086
|
+
else {
|
|
2087
|
+
this.contentIndex.clearProblems();
|
|
2088
|
+
}
|
|
1986
2089
|
for (let i = 0; i < filesToIndex.length; i++) {
|
|
1987
2090
|
const filePath = filesToIndex[i];
|
|
1988
2091
|
const bufferIndex = i % buffers.length;
|
|
@@ -1990,6 +2093,22 @@ export class FileSystemContentAPI {
|
|
|
1990
2093
|
const result = await this.parseFileHeaderWithRepair(filePath, buffers[bufferIndex]);
|
|
1991
2094
|
if (result) {
|
|
1992
2095
|
const { manifest, localeVersion, content } = result;
|
|
2096
|
+
// A different file already claims this id+locale. Its file still
|
|
2097
|
+
// being on disk is what tells a stray copy (refuse, or it merges
|
|
2098
|
+
// over the original and a page silently stops existing) apart from
|
|
2099
|
+
// an out-of-band rename (the old path is gone — this file is the
|
|
2100
|
+
// entry's new home and must keep merging).
|
|
2101
|
+
const claimant = this.contentIndex.getManifest(manifest.id);
|
|
2102
|
+
const claimedLocale = claimant?.locales[localeVersion.locale];
|
|
2103
|
+
if (claimant && claimedLocale) {
|
|
2104
|
+
const claimantPath = this.getFilePath(claimant, claimedLocale);
|
|
2105
|
+
const claimantOnDisk = await access(claimantPath).then(() => true, () => false);
|
|
2106
|
+
if (claimantPath !== filePath && claimantOnDisk) {
|
|
2107
|
+
this.recordIndexProblem(filePath, 'duplicate-id', duplicateIdMessage(manifest.id, localeVersion.locale, claimantPath));
|
|
2108
|
+
filesSkipped++;
|
|
2109
|
+
continue;
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
1993
2112
|
// Add to index
|
|
1994
2113
|
this.contentIndex.addContent(manifest, localeVersion, content);
|
|
1995
2114
|
filesProcessed++;
|
|
@@ -2006,7 +2125,8 @@ export class FileSystemContentAPI {
|
|
|
2006
2125
|
}
|
|
2007
2126
|
}
|
|
2008
2127
|
catch (error) {
|
|
2009
|
-
console.error(`Failed to reindex ${filePath}
|
|
2128
|
+
console.error(`Failed to reindex ${filePath}: ${messageOf(error)}`);
|
|
2129
|
+
this.recordIndexProblem(filePath, 'index-error', messageOf(error));
|
|
2010
2130
|
errors++;
|
|
2011
2131
|
}
|
|
2012
2132
|
}
|
|
@@ -2033,7 +2153,7 @@ export class FileSystemContentAPI {
|
|
|
2033
2153
|
}
|
|
2034
2154
|
async getContentAsBuffer(id, locale) {
|
|
2035
2155
|
const manifest = this.contentIndex.getManifest(id);
|
|
2036
|
-
if (!manifest
|
|
2156
|
+
if (!manifest?.locales[locale]) {
|
|
2037
2157
|
return null;
|
|
2038
2158
|
}
|
|
2039
2159
|
const localeVersion = manifest.locales[locale];
|
|
@@ -2139,7 +2259,7 @@ export class FileSystemContentAPI {
|
|
|
2139
2259
|
}
|
|
2140
2260
|
// Validate metadata size if updating
|
|
2141
2261
|
if (op.data.meta) {
|
|
2142
|
-
const newMeta =
|
|
2262
|
+
const newMeta = applyMetaPatch(current.meta, op.data.meta);
|
|
2143
2263
|
const validation = validateAndPrepareMetadata(newMeta);
|
|
2144
2264
|
if (!validation.valid) {
|
|
2145
2265
|
return {
|
|
@@ -2179,3 +2299,9 @@ export class FileSystemContentAPI {
|
|
|
2179
2299
|
};
|
|
2180
2300
|
}
|
|
2181
2301
|
}
|
|
2302
|
+
/** Whether two paths name one file, as 'hero.en.mdx' and 'Hero.en.mdx' do on a case-insensitive disk. */
|
|
2303
|
+
async function isSameFile(a, b) {
|
|
2304
|
+
const [statA, statB] = await Promise.all([stat(a), stat(b)]).catch(() => [null, null]);
|
|
2305
|
+
return statA !== null && statB !== null && statA.dev === statB.dev && statA.ino === statB.ino;
|
|
2306
|
+
}
|
|
2307
|
+
//# sourceMappingURL=filesystem-content-api.js.map
|