@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
|
@@ -0,0 +1,760 @@
|
|
|
1
|
+
import type { ContentAPI } from './content-api.interface.js';
|
|
2
|
+
import { filterContentWithIndexes } from './content-filters.js';
|
|
3
|
+
import { ContentIndex } from './content-index.js';
|
|
4
|
+
import {
|
|
5
|
+
buildLocaleVersion,
|
|
6
|
+
buildPathnameHistory,
|
|
7
|
+
checkEntryName,
|
|
8
|
+
checkPagePathname,
|
|
9
|
+
generateContentId,
|
|
10
|
+
isContentPublished,
|
|
11
|
+
shouldUpdateModifiedTimestamp,
|
|
12
|
+
updatePublishDates,
|
|
13
|
+
validateAndPrepareMetadata,
|
|
14
|
+
validateCreateContent,
|
|
15
|
+
} from './content-operations.js';
|
|
16
|
+
import {
|
|
17
|
+
applyMetaPatch,
|
|
18
|
+
getCurrentISODate,
|
|
19
|
+
localesOf,
|
|
20
|
+
mapLocales,
|
|
21
|
+
mapSiteNames,
|
|
22
|
+
serializeMdxWithFrontmatter,
|
|
23
|
+
} from './content-utils.js';
|
|
24
|
+
import { Data } from './data.js';
|
|
25
|
+
import { calculateEtagsFromMdxBuffer, findMdxContentStartPosition } from './etag-utils.js';
|
|
26
|
+
import { Fragments } from './fragments.js';
|
|
27
|
+
import { Site } from './site.js';
|
|
28
|
+
import type {
|
|
29
|
+
BatchResult,
|
|
30
|
+
ContentData,
|
|
31
|
+
ContentEntry,
|
|
32
|
+
ContentManifest,
|
|
33
|
+
ContentMeta,
|
|
34
|
+
CreateContentInput,
|
|
35
|
+
CreateResult,
|
|
36
|
+
DeleteLocaleInput,
|
|
37
|
+
DeleteResult,
|
|
38
|
+
FindOptions,
|
|
39
|
+
GlobalFilters,
|
|
40
|
+
LocaleVersion,
|
|
41
|
+
LocalizedEntry,
|
|
42
|
+
SitesConfig,
|
|
43
|
+
UpdateLocaleInput,
|
|
44
|
+
UpdateResult,
|
|
45
|
+
VXJSONFile,
|
|
46
|
+
} from './types.js';
|
|
47
|
+
import { VXJSON } from './vxjson.js';
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* In-memory implementation of ContentAPI for testing
|
|
51
|
+
* Provides a clean, mock-free way to test the API without filesystem dependencies
|
|
52
|
+
*/
|
|
53
|
+
export class InMemoryContentAPI implements ContentAPI {
|
|
54
|
+
private contentIndex: ContentIndex;
|
|
55
|
+
readonly sitesConfig: SitesConfig;
|
|
56
|
+
readonly fragments: Fragments;
|
|
57
|
+
readonly data: Data;
|
|
58
|
+
private readonly sites: Record<string, Site>;
|
|
59
|
+
|
|
60
|
+
constructor(sitesConfig?: SitesConfig) {
|
|
61
|
+
this.sitesConfig = sitesConfig || {
|
|
62
|
+
sites: {},
|
|
63
|
+
globalLocales: ['en'],
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// Initialize content index (not cached for in-memory)
|
|
67
|
+
this.contentIndex = ContentIndex.createUncached(this.sitesConfig);
|
|
68
|
+
|
|
69
|
+
// Initialize fragments
|
|
70
|
+
this.fragments = new Fragments(this, this.contentIndex.getFragmentIndex());
|
|
71
|
+
|
|
72
|
+
// Initialize data
|
|
73
|
+
this.data = new Data(this, this.contentIndex.getDataIndex());
|
|
74
|
+
|
|
75
|
+
// Initialize all sites
|
|
76
|
+
this.sites = mapSiteNames(this.sitesConfig, (siteName) => {
|
|
77
|
+
const siteIndex = this.contentIndex.getSiteIndex(siteName);
|
|
78
|
+
if (!siteIndex) {
|
|
79
|
+
throw new Error(`Site index not found for ${siteName}`);
|
|
80
|
+
}
|
|
81
|
+
return new Site(siteName, this, siteIndex);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Core content operations
|
|
86
|
+
async getContent(id: string): Promise<ContentEntry | null> {
|
|
87
|
+
const manifest = this.contentIndex.getManifest(id);
|
|
88
|
+
if (!manifest) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Build ContentEntry from manifest and cached content
|
|
93
|
+
const contentEntry: ContentEntry = {
|
|
94
|
+
id: manifest.id,
|
|
95
|
+
kind: manifest.kind,
|
|
96
|
+
site: manifest.site,
|
|
97
|
+
collection: manifest.collection,
|
|
98
|
+
type: manifest.type,
|
|
99
|
+
locales: {},
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// Add locale data with content from cache
|
|
103
|
+
for (const localeVersion of localesOf(manifest)) {
|
|
104
|
+
const { locale } = localeVersion;
|
|
105
|
+
const cachedContent = this.contentIndex.getCachedContent(id, locale);
|
|
106
|
+
if (cachedContent) {
|
|
107
|
+
// `content.mdx` is body-only on read — frontmatter is server-
|
|
108
|
+
// owned and exposed via `meta` / `created` / `modified`. Mirrors
|
|
109
|
+
// FileSystemContentAPI.readLocaleFile (see commit a631a43 — the
|
|
110
|
+
// editor cannot round-trip frontmatter as body text without
|
|
111
|
+
// duplicating it on disk).
|
|
112
|
+
contentEntry.locales[locale] = {
|
|
113
|
+
...localeVersion,
|
|
114
|
+
content: cachedContent,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return contentEntry;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async getLocalized(id: string, locale: string): Promise<LocalizedEntry | null> {
|
|
123
|
+
const manifest = this.contentIndex.getManifest(id);
|
|
124
|
+
if (!manifest?.locales[locale]) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const localeVersion = manifest.locales[locale];
|
|
129
|
+
const cachedContent = this.contentIndex.getCachedContent(id, locale);
|
|
130
|
+
|
|
131
|
+
if (!cachedContent) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// `content.mdx` is body-only on read (see `getContent` above).
|
|
136
|
+
const contentToReturn = { ...cachedContent };
|
|
137
|
+
|
|
138
|
+
// Return a deep copy to prevent modifications to the original data
|
|
139
|
+
return {
|
|
140
|
+
id,
|
|
141
|
+
site: manifest.site,
|
|
142
|
+
collection: manifest.collection,
|
|
143
|
+
type: manifest.type,
|
|
144
|
+
kind: manifest.kind,
|
|
145
|
+
localized: {
|
|
146
|
+
...localeVersion,
|
|
147
|
+
meta: { ...localeVersion.meta },
|
|
148
|
+
content: contentToReturn,
|
|
149
|
+
previousPathnames: localeVersion.previousPathnames ? { ...localeVersion.previousPathnames } : undefined,
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async createContent(data: CreateContentInput): Promise<CreateResult> {
|
|
155
|
+
// Use shared validation
|
|
156
|
+
const validation = validateCreateContent(data, this.sites, this.fragments, this.data);
|
|
157
|
+
if (!validation.valid) {
|
|
158
|
+
return {
|
|
159
|
+
success: false,
|
|
160
|
+
reason: validation.reason,
|
|
161
|
+
error: validation.error ? new Error(validation.error) : undefined,
|
|
162
|
+
existingId: validation.existingId,
|
|
163
|
+
field: validation.field,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Generate ID
|
|
168
|
+
const id = generateContentId();
|
|
169
|
+
const now = getCurrentISODate();
|
|
170
|
+
|
|
171
|
+
const contentCache: Record<string, ContentData> = {};
|
|
172
|
+
|
|
173
|
+
const locales = mapLocales(
|
|
174
|
+
data.site ? this.sitesConfig.sites[data.site].locales : this.sitesConfig.globalLocales,
|
|
175
|
+
(locale) => {
|
|
176
|
+
const localeData = data.locales[locale];
|
|
177
|
+
if (!localeData) return undefined;
|
|
178
|
+
|
|
179
|
+
const meta = { ...data.meta, ...localeData.meta } as ContentMeta;
|
|
180
|
+
|
|
181
|
+
// Calculate dual ETag for this locale
|
|
182
|
+
const fileData: VXJSONFile = {
|
|
183
|
+
id,
|
|
184
|
+
type: data.type,
|
|
185
|
+
created: now,
|
|
186
|
+
modified: now,
|
|
187
|
+
publishAt:
|
|
188
|
+
localeData.publishAt && typeof localeData.publishAt === 'string' ? localeData.publishAt : undefined,
|
|
189
|
+
unpublishAt:
|
|
190
|
+
localeData.unpublishAt && typeof localeData.unpublishAt === 'string' ? localeData.unpublishAt : undefined,
|
|
191
|
+
meta,
|
|
192
|
+
content: localeData.content,
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// Serialize to buffer and calculate ETags based on content type
|
|
196
|
+
let buffer: Uint8Array;
|
|
197
|
+
let etag: string;
|
|
198
|
+
|
|
199
|
+
if (data.type === 'mdx') {
|
|
200
|
+
// For MDX content, serialize as frontmatter + content
|
|
201
|
+
const frontmatterData = {
|
|
202
|
+
id,
|
|
203
|
+
created: now,
|
|
204
|
+
modified: now,
|
|
205
|
+
...(localeData.publishAt && { publishAt: localeData.publishAt }),
|
|
206
|
+
...(localeData.unpublishAt && { unpublishAt: localeData.unpublishAt }),
|
|
207
|
+
...meta, // Spread metadata fields directly, not nested
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const mdxContent = serializeMdxWithFrontmatter(frontmatterData, localeData.content.mdx || '');
|
|
211
|
+
buffer = new TextEncoder().encode(mdxContent);
|
|
212
|
+
|
|
213
|
+
// Find content start position using shared utility
|
|
214
|
+
const contentPos = findMdxContentStartPosition(buffer);
|
|
215
|
+
|
|
216
|
+
// Calculate ETags using MDX-specific logic
|
|
217
|
+
const etagResult = calculateEtagsFromMdxBuffer(buffer, contentPos);
|
|
218
|
+
etag = `${etagResult.metaEtag}.${etagResult.contentEtag}`;
|
|
219
|
+
} else {
|
|
220
|
+
// For Puck content, serialize as VXJSON
|
|
221
|
+
const jsonStr = VXJSON.serialize(fileData);
|
|
222
|
+
buffer = Buffer.from(jsonStr, 'utf-8') as Uint8Array;
|
|
223
|
+
const etagResult = VXJSON.calculateETags(buffer);
|
|
224
|
+
|
|
225
|
+
if (!etagResult.success) {
|
|
226
|
+
throw new Error(`Invalid VXJSON format: ${etagResult.error}`);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
etag = etagResult.contentEtag ? `${etagResult.metaEtag}.${etagResult.contentEtag}` : etagResult.metaEtag;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Build locale manifest using shared function
|
|
233
|
+
const localeVersion = buildLocaleVersion({
|
|
234
|
+
locale,
|
|
235
|
+
etag,
|
|
236
|
+
created: now,
|
|
237
|
+
modified: now,
|
|
238
|
+
meta,
|
|
239
|
+
kind: data.kind,
|
|
240
|
+
pathname: data.kind === 'page' ? localeData.pathname : undefined,
|
|
241
|
+
name: data.kind === 'fragment' || data.kind === 'data' ? data.name : undefined,
|
|
242
|
+
publishAt:
|
|
243
|
+
localeData.publishAt && typeof localeData.publishAt === 'string' ? localeData.publishAt : undefined,
|
|
244
|
+
unpublishAt:
|
|
245
|
+
localeData.unpublishAt && typeof localeData.unpublishAt === 'string' ? localeData.unpublishAt : undefined,
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
contentCache[locale] = localeData.content;
|
|
249
|
+
return localeVersion;
|
|
250
|
+
},
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
// Build manifest and content cache
|
|
254
|
+
const manifest: ContentManifest = {
|
|
255
|
+
id,
|
|
256
|
+
type: data.type,
|
|
257
|
+
kind: data.kind,
|
|
258
|
+
site: data.site,
|
|
259
|
+
collection: data.collection,
|
|
260
|
+
locales,
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// Update index with each locale
|
|
264
|
+
for (const localeVersion of localesOf(manifest)) {
|
|
265
|
+
this.contentIndex.addContent(manifest, localeVersion, contentCache[localeVersion.locale]);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Return the first locale's etag (typically there's only one during creation)
|
|
269
|
+
const firstLocaleEtag = Object.values(manifest.locales)[0]?.etag || '';
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
success: true,
|
|
273
|
+
id,
|
|
274
|
+
etag: firstLocaleEtag,
|
|
275
|
+
created: new Date(now),
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
async updateLocalized(input: UpdateLocaleInput): Promise<UpdateResult> {
|
|
280
|
+
const { id, locale, data, etag } = input;
|
|
281
|
+
|
|
282
|
+
const manifest = this.contentIndex.getManifest(id);
|
|
283
|
+
if (!manifest) {
|
|
284
|
+
return {
|
|
285
|
+
success: false,
|
|
286
|
+
reason: 'not_found',
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Get the specific locale manifest
|
|
291
|
+
const localeVersion = manifest.locales[locale];
|
|
292
|
+
if (!localeVersion) {
|
|
293
|
+
return {
|
|
294
|
+
success: false,
|
|
295
|
+
reason: 'not_found',
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Check etag matches
|
|
300
|
+
if (localeVersion.etag !== etag) {
|
|
301
|
+
return {
|
|
302
|
+
success: false,
|
|
303
|
+
reason: 'stale_write',
|
|
304
|
+
currentEtag: localeVersion.etag,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Get current content from cache
|
|
309
|
+
const currentContent = this.contentIndex.getCachedContent(id, locale);
|
|
310
|
+
if (!currentContent) {
|
|
311
|
+
return {
|
|
312
|
+
success: false,
|
|
313
|
+
reason: 'not_found',
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
let nextMeta = localeVersion.meta;
|
|
318
|
+
// Validate the same metadata object that will be stored.
|
|
319
|
+
if (data.meta) {
|
|
320
|
+
const validation = validateAndPrepareMetadata(applyMetaPatch(localeVersion.meta, data.meta));
|
|
321
|
+
if (!validation.valid || !validation.sortedMeta) {
|
|
322
|
+
return {
|
|
323
|
+
success: false,
|
|
324
|
+
reason: 'write_error',
|
|
325
|
+
error: new Error(validation.error!),
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
nextMeta = validation.sortedMeta;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// A page's new path is refused when malformed or taken and stored tidied, as on the filesystem.
|
|
332
|
+
// An unchanged path is left alone, so a page keeps a path from before the checks.
|
|
333
|
+
let pathname = data.pathname;
|
|
334
|
+
if (pathname !== undefined && pathname !== localeVersion.pathname && manifest.kind === 'page') {
|
|
335
|
+
// Own-key only: the manifest's site name is never checked against the
|
|
336
|
+
// configured sites, so an inherited key would reach the calls below.
|
|
337
|
+
const site = manifest.site && Object.hasOwn(this.sites, manifest.site) ? this.sites[manifest.site] : undefined;
|
|
338
|
+
const checked = checkPagePathname(site, pathname, locale, id);
|
|
339
|
+
if (!checked.valid) {
|
|
340
|
+
return { success: false, reason: checked.reason, error: new Error(checked.error) };
|
|
341
|
+
}
|
|
342
|
+
pathname = checked.pathname;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// A new fragment or data name passes the create's checks, as on the filesystem. An unchanged
|
|
346
|
+
// name is left alone, so an entry keeps a name from before the checks.
|
|
347
|
+
const { kind } = manifest;
|
|
348
|
+
const renamed = data.name !== undefined && data.name !== localeVersion.name && kind !== 'page';
|
|
349
|
+
if (renamed) {
|
|
350
|
+
const entries = kind === 'fragment' ? this.fragments : this.data;
|
|
351
|
+
const refused = checkEntryName(
|
|
352
|
+
kind,
|
|
353
|
+
manifest.collection,
|
|
354
|
+
data.name,
|
|
355
|
+
(name) => (kind === 'fragment' ? this.fragments.isFragmentNameValid(name) : this.data.isDataNameValid(name)),
|
|
356
|
+
(name) => entries.getNameConflict(manifest.collection, name, id),
|
|
357
|
+
);
|
|
358
|
+
if (refused) {
|
|
359
|
+
return { success: false, reason: refused.reason, error: new Error(refused.error) };
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Update content
|
|
364
|
+
const now = new Date();
|
|
365
|
+
const nowStr = now.toISOString();
|
|
366
|
+
|
|
367
|
+
// Build updated locale manifest
|
|
368
|
+
const oldPathname = localeVersion.pathname;
|
|
369
|
+
const updatedLocaleVersion = { ...localeVersion };
|
|
370
|
+
|
|
371
|
+
// Update modified timestamp for any changes (content or metadata)
|
|
372
|
+
if (shouldUpdateModifiedTimestamp(data)) {
|
|
373
|
+
// Use provided modified timestamp or current time
|
|
374
|
+
const newModified = data.modified || nowStr;
|
|
375
|
+
updatedLocaleVersion.modified = newModified;
|
|
376
|
+
} else if (data.modified) {
|
|
377
|
+
// If modified timestamp is explicitly provided, always use it
|
|
378
|
+
updatedLocaleVersion.modified = data.modified;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (data.meta) {
|
|
382
|
+
updatedLocaleVersion.meta = nextMeta;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (pathname !== undefined && manifest.kind === 'page') {
|
|
386
|
+
// Build pathname history for published content
|
|
387
|
+
const pathnameHistory = buildPathnameHistory(oldPathname, pathname, isContentPublished(localeVersion), nowStr);
|
|
388
|
+
|
|
389
|
+
// Merge with existing pathname history
|
|
390
|
+
if (Object.keys(pathnameHistory).length > 0) {
|
|
391
|
+
updatedLocaleVersion.previousPathnames = {
|
|
392
|
+
...localeVersion.previousPathnames,
|
|
393
|
+
...pathnameHistory,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
updatedLocaleVersion.pathname = pathname;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (renamed) {
|
|
401
|
+
updatedLocaleVersion.name = data.name;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// Handle publish date updates using shared function
|
|
405
|
+
updatePublishDates(updatedLocaleVersion, data);
|
|
406
|
+
|
|
407
|
+
// Prepare updated content
|
|
408
|
+
const updatedContent = data.content ? { ...currentContent, ...data.content } : currentContent;
|
|
409
|
+
|
|
410
|
+
// Calculate new dual ETag by creating a buffer from the in-memory data
|
|
411
|
+
// Structure it like the filesystem JSON files
|
|
412
|
+
const fileData: VXJSONFile = {
|
|
413
|
+
id: manifest.id,
|
|
414
|
+
type: manifest.type,
|
|
415
|
+
created: updatedLocaleVersion.created,
|
|
416
|
+
modified: updatedLocaleVersion.modified,
|
|
417
|
+
publishAt: updatedLocaleVersion.publishAt,
|
|
418
|
+
unpublishAt: updatedLocaleVersion.unpublishAt,
|
|
419
|
+
meta: updatedLocaleVersion.meta,
|
|
420
|
+
previousPathnames: updatedLocaleVersion.previousPathnames,
|
|
421
|
+
content: updatedContent,
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
// Serialize to buffer and calculate ETags based on content type
|
|
425
|
+
let buffer: Uint8Array;
|
|
426
|
+
let newEtag: string;
|
|
427
|
+
|
|
428
|
+
if (manifest.type === 'mdx') {
|
|
429
|
+
// For MDX content, serialize as frontmatter + content
|
|
430
|
+
const frontmatterData = {
|
|
431
|
+
id: manifest.id,
|
|
432
|
+
created: updatedLocaleVersion.created,
|
|
433
|
+
modified: updatedLocaleVersion.modified,
|
|
434
|
+
...(updatedLocaleVersion.publishAt && { publishAt: updatedLocaleVersion.publishAt }),
|
|
435
|
+
...(updatedLocaleVersion.unpublishAt && { unpublishAt: updatedLocaleVersion.unpublishAt }),
|
|
436
|
+
...updatedLocaleVersion.meta, // Spread metadata fields directly, not nested
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
const mdxContent = serializeMdxWithFrontmatter(frontmatterData, updatedContent.mdx || '');
|
|
440
|
+
buffer = new TextEncoder().encode(mdxContent);
|
|
441
|
+
|
|
442
|
+
// Find content start position using shared utility
|
|
443
|
+
const contentPos = findMdxContentStartPosition(buffer);
|
|
444
|
+
|
|
445
|
+
// Calculate ETags using MDX-specific logic
|
|
446
|
+
const etagResult = calculateEtagsFromMdxBuffer(buffer, contentPos);
|
|
447
|
+
newEtag = `${etagResult.metaEtag}.${etagResult.contentEtag}`;
|
|
448
|
+
} else {
|
|
449
|
+
// For Puck content, serialize as VXJSON
|
|
450
|
+
const jsonStr = VXJSON.serialize(fileData);
|
|
451
|
+
buffer = Buffer.from(jsonStr, 'utf-8') as Uint8Array;
|
|
452
|
+
const etagResult = VXJSON.calculateETags(buffer);
|
|
453
|
+
|
|
454
|
+
if (!etagResult.success) {
|
|
455
|
+
throw new Error(`Invalid VXJSON format: ${etagResult.error}`);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
newEtag = etagResult.contentEtag ? `${etagResult.metaEtag}.${etagResult.contentEtag}` : etagResult.metaEtag;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
updatedLocaleVersion.etag = newEtag;
|
|
462
|
+
|
|
463
|
+
// Build updated manifest with only the changed locale
|
|
464
|
+
const updatedManifest: ContentManifest = {
|
|
465
|
+
...manifest,
|
|
466
|
+
locales: {
|
|
467
|
+
...manifest.locales,
|
|
468
|
+
[locale]: updatedLocaleVersion,
|
|
469
|
+
},
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
// A renamed locale is replaced, so the old name no longer finds the entry.
|
|
473
|
+
if (renamed) {
|
|
474
|
+
this.contentIndex.removeLocale(id, locale);
|
|
475
|
+
}
|
|
476
|
+
// Update content in the index (this will handle pathname index updates)
|
|
477
|
+
this.contentIndex.addContent(updatedManifest, updatedLocaleVersion, updatedContent);
|
|
478
|
+
|
|
479
|
+
return {
|
|
480
|
+
success: true,
|
|
481
|
+
etag: updatedLocaleVersion.etag,
|
|
482
|
+
modified: now,
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
async deleteContent(id: string, etag: string): Promise<DeleteResult> {
|
|
487
|
+
const manifest = this.contentIndex.getManifest(id);
|
|
488
|
+
if (!manifest) {
|
|
489
|
+
return {
|
|
490
|
+
success: false,
|
|
491
|
+
reason: 'not_found',
|
|
492
|
+
error: new Error('Content not found'),
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// Verify etag
|
|
497
|
+
let hasMatchingEtag = false;
|
|
498
|
+
for (const localeVersion of localesOf(manifest)) {
|
|
499
|
+
if (localeVersion.etag === etag) {
|
|
500
|
+
hasMatchingEtag = true;
|
|
501
|
+
break;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
if (!hasMatchingEtag && etag) {
|
|
506
|
+
return {
|
|
507
|
+
success: false,
|
|
508
|
+
reason: 'stale_write',
|
|
509
|
+
currentEtag: Array.from(localesOf(manifest))[0]?.etag || '',
|
|
510
|
+
error: new Error('ETag mismatch'),
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// Remove from index (this also removes from content cache)
|
|
515
|
+
this.contentIndex.removeEntry(id);
|
|
516
|
+
|
|
517
|
+
return {
|
|
518
|
+
success: true,
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// Site and fragments access
|
|
523
|
+
getSite(siteName: string): Site | null {
|
|
524
|
+
if (siteName === 'fragments') {
|
|
525
|
+
throw new Error('Use `fragments` property instead of getSite("fragments")');
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// Own-key only: the name is caller-supplied, and an inherited key like
|
|
529
|
+
// `constructor` would resolve to a truthy non-Site.
|
|
530
|
+
return Object.hasOwn(this.sites, siteName) ? this.sites[siteName] : null;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// Global operations
|
|
534
|
+
*listAllContent(filters?: GlobalFilters): Generator<ContentManifest> {
|
|
535
|
+
yield* filterContentWithIndexes(this.contentIndex, filters, this.sitesConfig);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
*findUntranslatedContent(targetLocale: string, options?: FindOptions): Generator<ContentManifest> {
|
|
539
|
+
// Use the index method which already handles all the filtering efficiently
|
|
540
|
+
yield* this.contentIndex.findUntranslatedContent(targetLocale, options);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
async deleteLocalized(input: DeleteLocaleInput): Promise<DeleteResult> {
|
|
544
|
+
const { id, locale, etag } = input;
|
|
545
|
+
|
|
546
|
+
// Get existing manifest
|
|
547
|
+
const manifest = this.contentIndex.getManifest(id);
|
|
548
|
+
if (!manifest) {
|
|
549
|
+
return {
|
|
550
|
+
success: false,
|
|
551
|
+
reason: 'not_found',
|
|
552
|
+
error: new Error('Content not found'),
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
const localeVersion = manifest.locales[locale];
|
|
557
|
+
if (!localeVersion) {
|
|
558
|
+
return {
|
|
559
|
+
success: false,
|
|
560
|
+
reason: 'not_found',
|
|
561
|
+
error: new Error('Locale not found'),
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// Check etag
|
|
566
|
+
if (localeVersion.etag !== etag) {
|
|
567
|
+
return {
|
|
568
|
+
success: false,
|
|
569
|
+
reason: 'stale_write',
|
|
570
|
+
currentEtag: localeVersion.etag,
|
|
571
|
+
error: new Error('Stale write - etag mismatch'),
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// Refuse the last locale — deleting it here used to silently delete the
|
|
576
|
+
// whole page. Count actual locales, not manifest keys: removeLocale
|
|
577
|
+
// clears a locale by assigning undefined and keeping the key, so a key
|
|
578
|
+
// count still includes dead slots after earlier translation deletes.
|
|
579
|
+
// Matches FileSystemContentAPI's contract.
|
|
580
|
+
if (Array.from(localesOf(manifest)).length === 1) {
|
|
581
|
+
return {
|
|
582
|
+
success: false,
|
|
583
|
+
error: new Error('Cannot delete the last locale. Use deleteContent to delete all content.'),
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
this.contentIndex.removeLocale(manifest.id, locale);
|
|
588
|
+
|
|
589
|
+
return {
|
|
590
|
+
success: true,
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
async batchUpdate(operations: UpdateLocaleInput[]): Promise<BatchResult> {
|
|
595
|
+
const results: BatchResult = {
|
|
596
|
+
operations: [],
|
|
597
|
+
success: true,
|
|
598
|
+
failed: 0,
|
|
599
|
+
updated: 0,
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
for (const op of operations) {
|
|
603
|
+
const result = await this.updateLocalized(op);
|
|
604
|
+
|
|
605
|
+
if (result.success) {
|
|
606
|
+
results.updated++;
|
|
607
|
+
results.operations.push({
|
|
608
|
+
id: op.id,
|
|
609
|
+
locale: op.locale,
|
|
610
|
+
updated: true,
|
|
611
|
+
etag: result.etag,
|
|
612
|
+
});
|
|
613
|
+
} else {
|
|
614
|
+
results.failed++;
|
|
615
|
+
results.success = false;
|
|
616
|
+
results.operations.push({
|
|
617
|
+
id: op.id,
|
|
618
|
+
locale: op.locale,
|
|
619
|
+
updated: false,
|
|
620
|
+
error: result.error || new Error(result.reason || 'Update failed'),
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
return results;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
// Test helper method to directly set content
|
|
629
|
+
setContent(content: ContentEntry): void {
|
|
630
|
+
// Build manifest from ContentEntry
|
|
631
|
+
const manifest: ContentManifest = {
|
|
632
|
+
id: content.id,
|
|
633
|
+
type: content.type,
|
|
634
|
+
kind: content.kind,
|
|
635
|
+
site: content.site,
|
|
636
|
+
collection: content.collection,
|
|
637
|
+
locales: {},
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
const contentCache: Record<string, ContentData | null> = {};
|
|
641
|
+
|
|
642
|
+
for (const [locale, localeData] of Object.entries(content.locales)) {
|
|
643
|
+
const manifestLocale: LocaleVersion = {
|
|
644
|
+
locale,
|
|
645
|
+
etag: localeData.etag,
|
|
646
|
+
created: localeData.created,
|
|
647
|
+
modified: localeData.modified,
|
|
648
|
+
meta: localeData.meta,
|
|
649
|
+
};
|
|
650
|
+
if (localeData.publishAt !== undefined) {
|
|
651
|
+
manifestLocale.publishAt = localeData.publishAt;
|
|
652
|
+
}
|
|
653
|
+
if (localeData.unpublishAt !== undefined) {
|
|
654
|
+
manifestLocale.unpublishAt = localeData.unpublishAt;
|
|
655
|
+
}
|
|
656
|
+
if (localeData.name) {
|
|
657
|
+
manifestLocale.name = localeData.name;
|
|
658
|
+
}
|
|
659
|
+
if (localeData.pathname) {
|
|
660
|
+
manifestLocale.pathname = localeData.pathname;
|
|
661
|
+
}
|
|
662
|
+
if (localeData.previousPathnames) {
|
|
663
|
+
manifestLocale.previousPathnames = localeData.previousPathnames;
|
|
664
|
+
}
|
|
665
|
+
manifest.locales[locale] = manifestLocale;
|
|
666
|
+
contentCache[locale] = localeData.content;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// Add to content index with each locale
|
|
670
|
+
for (const localeVersion of localesOf(manifest)) {
|
|
671
|
+
this.contentIndex.addContent(manifest, localeVersion, contentCache[localeVersion.locale]);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// Test helper to clear all content
|
|
676
|
+
clear(): void {
|
|
677
|
+
this.contentIndex.clear();
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
async getContentAsBuffer(
|
|
681
|
+
id: string,
|
|
682
|
+
locale: string,
|
|
683
|
+
): Promise<{
|
|
684
|
+
buffer: Uint8Array;
|
|
685
|
+
etag: string;
|
|
686
|
+
contentType: 'application/json' | 'text/mdx';
|
|
687
|
+
} | null> {
|
|
688
|
+
const manifest = this.contentIndex.getManifest(id);
|
|
689
|
+
if (!manifest?.locales[locale]) {
|
|
690
|
+
return null;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
const localeVersion = manifest.locales[locale];
|
|
694
|
+
const cachedContent = this.contentIndex.getCachedContent(id, locale);
|
|
695
|
+
if (!cachedContent) {
|
|
696
|
+
return null;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// Serialize based on content type
|
|
700
|
+
let buffer: Uint8Array;
|
|
701
|
+
let calculatedEtag: string;
|
|
702
|
+
let contentType: 'application/json' | 'text/mdx';
|
|
703
|
+
|
|
704
|
+
if (manifest.type === 'mdx') {
|
|
705
|
+
// For MDX content, serialize as frontmatter + content
|
|
706
|
+
const frontmatterData = {
|
|
707
|
+
id: manifest.id,
|
|
708
|
+
created: localeVersion.created,
|
|
709
|
+
modified: localeVersion.modified,
|
|
710
|
+
...(localeVersion.publishAt && { publishAt: localeVersion.publishAt }),
|
|
711
|
+
...(localeVersion.unpublishAt && { unpublishAt: localeVersion.unpublishAt }),
|
|
712
|
+
...localeVersion.meta, // Spread metadata fields directly, not nested
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
const mdxContent = serializeMdxWithFrontmatter(frontmatterData, cachedContent.mdx || '');
|
|
716
|
+
buffer = new TextEncoder().encode(mdxContent);
|
|
717
|
+
|
|
718
|
+
// Find content start position using shared utility
|
|
719
|
+
const contentPos = findMdxContentStartPosition(buffer);
|
|
720
|
+
|
|
721
|
+
// Calculate ETags using MDX-specific logic
|
|
722
|
+
const etagResult = calculateEtagsFromMdxBuffer(buffer, contentPos);
|
|
723
|
+
calculatedEtag = `${etagResult.metaEtag}.${etagResult.contentEtag}`;
|
|
724
|
+
contentType = 'text/mdx';
|
|
725
|
+
} else {
|
|
726
|
+
// For Puck content, serialize as VXJSON
|
|
727
|
+
const fileData: VXJSONFile = {
|
|
728
|
+
id: manifest.id,
|
|
729
|
+
type: manifest.type,
|
|
730
|
+
created: localeVersion.created,
|
|
731
|
+
modified: localeVersion.modified,
|
|
732
|
+
publishAt: localeVersion.publishAt,
|
|
733
|
+
unpublishAt: localeVersion.unpublishAt,
|
|
734
|
+
meta: localeVersion.meta,
|
|
735
|
+
previousPathnames: localeVersion.previousPathnames,
|
|
736
|
+
content: cachedContent,
|
|
737
|
+
};
|
|
738
|
+
const jsonStr = VXJSON.serialize(fileData);
|
|
739
|
+
buffer = Buffer.from(jsonStr, 'utf-8') as Uint8Array;
|
|
740
|
+
|
|
741
|
+
// Calculate ETags to ensure they match what's stored
|
|
742
|
+
const etagResult = VXJSON.calculateETags(buffer);
|
|
743
|
+
|
|
744
|
+
if (!etagResult.success) {
|
|
745
|
+
throw new Error(`Invalid VXJSON format: ${etagResult.error}`);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
calculatedEtag = etagResult.contentEtag
|
|
749
|
+
? `${etagResult.metaEtag}.${etagResult.contentEtag}`
|
|
750
|
+
: etagResult.metaEtag;
|
|
751
|
+
contentType = 'application/json';
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
return {
|
|
755
|
+
buffer,
|
|
756
|
+
etag: calculatedEtag,
|
|
757
|
+
contentType,
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
}
|