@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.
Files changed (299) hide show
  1. package/dist/asset-constraints.d.ts +16 -0
  2. package/dist/asset-constraints.d.ts.map +1 -0
  3. package/dist/asset-constraints.js +29 -0
  4. package/dist/asset-constraints.js.map +1 -0
  5. package/dist/asset-metadata-store.d.ts +60 -0
  6. package/dist/asset-metadata-store.d.ts.map +1 -0
  7. package/dist/asset-metadata-store.js +335 -0
  8. package/dist/asset-metadata-store.js.map +1 -0
  9. package/dist/asset-operations.d.ts +18 -13
  10. package/dist/asset-operations.d.ts.map +1 -1
  11. package/dist/asset-operations.js +302 -254
  12. package/dist/asset-operations.js.map +1 -0
  13. package/dist/asset-types.d.ts +23 -7
  14. package/dist/asset-types.d.ts.map +1 -1
  15. package/dist/asset-types.js +8 -2
  16. package/dist/asset-types.js.map +1 -0
  17. package/dist/asset-usage.d.ts +10 -0
  18. package/dist/asset-usage.d.ts.map +1 -0
  19. package/dist/asset-usage.js +49 -0
  20. package/dist/asset-usage.js.map +1 -0
  21. package/dist/base-content-index.d.ts +8 -2
  22. package/dist/base-content-index.d.ts.map +1 -1
  23. package/dist/base-content-index.js +24 -3
  24. package/dist/base-content-index.js.map +1 -0
  25. package/dist/cf-access.js +1 -0
  26. package/dist/cf-access.js.map +1 -0
  27. package/dist/component-registry.d.ts +1 -1
  28. package/dist/component-registry.d.ts.map +1 -1
  29. package/dist/component-registry.js +2 -1
  30. package/dist/component-registry.js.map +1 -0
  31. package/dist/component-registry.types.js +1 -0
  32. package/dist/component-registry.types.js.map +1 -0
  33. package/dist/component-vocabulary.d.ts +33 -0
  34. package/dist/component-vocabulary.d.ts.map +1 -0
  35. package/dist/component-vocabulary.js +105 -0
  36. package/dist/component-vocabulary.js.map +1 -0
  37. package/dist/content-api.interface.d.ts +5 -5
  38. package/dist/content-api.interface.d.ts.map +1 -1
  39. package/dist/content-api.interface.js +1 -0
  40. package/dist/content-api.interface.js.map +1 -0
  41. package/dist/content-filters.d.ts +2 -2
  42. package/dist/content-filters.d.ts.map +1 -1
  43. package/dist/content-filters.js +9 -8
  44. package/dist/content-filters.js.map +1 -0
  45. package/dist/content-index.d.ts +74 -16
  46. package/dist/content-index.d.ts.map +1 -1
  47. package/dist/content-index.js +183 -55
  48. package/dist/content-index.js.map +1 -0
  49. package/dist/content-operations.d.ts +71 -7
  50. package/dist/content-operations.d.ts.map +1 -1
  51. package/dist/content-operations.js +166 -82
  52. package/dist/content-operations.js.map +1 -0
  53. package/dist/content-path.d.ts +59 -0
  54. package/dist/content-path.d.ts.map +1 -0
  55. package/dist/content-path.js +88 -0
  56. package/dist/content-path.js.map +1 -0
  57. package/dist/content-reader.d.ts +2 -2
  58. package/dist/content-reader.d.ts.map +1 -1
  59. package/dist/content-reader.js +2 -1
  60. package/dist/content-reader.js.map +1 -0
  61. package/dist/content-utils.d.ts +53 -10
  62. package/dist/content-utils.d.ts.map +1 -1
  63. package/dist/content-utils.js +103 -34
  64. package/dist/content-utils.js.map +1 -0
  65. package/dist/content-watcher.d.ts +3 -3
  66. package/dist/content-watcher.d.ts.map +1 -1
  67. package/dist/content-watcher.js +13 -10
  68. package/dist/content-watcher.js.map +1 -0
  69. package/dist/data-index.d.ts +1 -1
  70. package/dist/data-index.d.ts.map +1 -1
  71. package/dist/data-index.js +2 -1
  72. package/dist/data-index.js.map +1 -0
  73. package/dist/data.d.ts +5 -13
  74. package/dist/data.d.ts.map +1 -1
  75. package/dist/data.js +7 -6
  76. package/dist/data.js.map +1 -0
  77. package/dist/editor-data-bindings.d.ts +10 -0
  78. package/dist/editor-data-bindings.d.ts.map +1 -0
  79. package/dist/editor-data-bindings.js +67 -0
  80. package/dist/editor-data-bindings.js.map +1 -0
  81. package/dist/etag-utils.d.ts.map +1 -1
  82. package/dist/etag-utils.js +5 -4
  83. package/dist/etag-utils.js.map +1 -0
  84. package/dist/filesystem-content-api.d.ts +45 -18
  85. package/dist/filesystem-content-api.d.ts.map +1 -1
  86. package/dist/filesystem-content-api.js +407 -281
  87. package/dist/filesystem-content-api.js.map +1 -0
  88. package/dist/filesystem-reader-backend.d.ts +46 -0
  89. package/dist/filesystem-reader-backend.d.ts.map +1 -0
  90. package/dist/filesystem-reader-backend.js +119 -0
  91. package/dist/filesystem-reader-backend.js.map +1 -0
  92. package/dist/fragment-index.d.ts +8 -0
  93. package/dist/fragment-index.d.ts.map +1 -0
  94. package/dist/fragment-index.js +8 -0
  95. package/dist/fragment-index.js.map +1 -0
  96. package/dist/fragments.d.ts +16 -0
  97. package/dist/fragments.d.ts.map +1 -0
  98. package/dist/fragments.js +104 -0
  99. package/dist/fragments.js.map +1 -0
  100. package/dist/git-operations.d.ts.map +1 -1
  101. package/dist/git-operations.js +5 -1
  102. package/dist/git-operations.js.map +1 -0
  103. package/dist/header-parser.d.ts +4 -4
  104. package/dist/header-parser.d.ts.map +1 -1
  105. package/dist/header-parser.js +8 -61
  106. package/dist/header-parser.js.map +1 -0
  107. package/dist/in-memory-content-api.d.ts +7 -7
  108. package/dist/in-memory-content-api.d.ts.map +1 -1
  109. package/dist/in-memory-content-api.js +74 -56
  110. package/dist/in-memory-content-api.js.map +1 -0
  111. package/dist/index.d.ts +19 -12
  112. package/dist/index.d.ts.map +1 -1
  113. package/dist/index.js +21 -9
  114. package/dist/index.js.map +1 -0
  115. package/dist/indexing.d.ts +4 -4
  116. package/dist/indexing.d.ts.map +1 -1
  117. package/dist/indexing.js +6 -5
  118. package/dist/indexing.js.map +1 -0
  119. package/dist/json-whitespace-view.js +1 -0
  120. package/dist/json-whitespace-view.js.map +1 -0
  121. package/dist/middleware.d.ts +6 -6
  122. package/dist/middleware.d.ts.map +1 -1
  123. package/dist/middleware.js +170 -119
  124. package/dist/middleware.js.map +1 -0
  125. package/dist/node.d.ts +24 -17
  126. package/dist/node.d.ts.map +1 -1
  127. package/dist/node.js +17 -14
  128. package/dist/node.js.map +1 -0
  129. package/dist/page-references.d.ts +21 -0
  130. package/dist/page-references.d.ts.map +1 -0
  131. package/dist/page-references.js +50 -0
  132. package/dist/page-references.js.map +1 -0
  133. package/dist/read-date.d.ts +3 -0
  134. package/dist/read-date.d.ts.map +1 -0
  135. package/dist/read-date.js +9 -0
  136. package/dist/read-date.js.map +1 -0
  137. package/dist/reader-backend.d.ts +54 -0
  138. package/dist/reader-backend.d.ts.map +1 -0
  139. package/dist/reader-backend.js +2 -0
  140. package/dist/reader-backend.js.map +1 -0
  141. package/dist/reader.d.ts +8 -8
  142. package/dist/reader.d.ts.map +1 -1
  143. package/dist/reader.js +8 -7
  144. package/dist/reader.js.map +1 -0
  145. package/dist/schemas/index.d.ts +6 -6
  146. package/dist/schemas/index.d.ts.map +1 -1
  147. package/dist/schemas/index.js +9 -8
  148. package/dist/schemas/index.js.map +1 -0
  149. package/dist/site-index.d.ts +3 -3
  150. package/dist/site-index.d.ts.map +1 -1
  151. package/dist/site-index.js +9 -5
  152. package/dist/site-index.js.map +1 -0
  153. package/dist/site.d.ts +5 -3
  154. package/dist/site.d.ts.map +1 -1
  155. package/dist/site.js +14 -2
  156. package/dist/site.js.map +1 -0
  157. package/dist/types.d.ts +203 -15
  158. package/dist/types.d.ts.map +1 -1
  159. package/dist/types.js +9 -0
  160. package/dist/types.js.map +1 -0
  161. package/dist/url-safe-base64.js +1 -0
  162. package/dist/url-safe-base64.js.map +1 -0
  163. package/dist/url-validation.d.ts +14 -0
  164. package/dist/url-validation.d.ts.map +1 -1
  165. package/dist/url-validation.js +56 -14
  166. package/dist/url-validation.js.map +1 -0
  167. package/dist/utils/atomic-write.js +1 -0
  168. package/dist/utils/atomic-write.js.map +1 -0
  169. package/dist/utils.d.ts +1 -1
  170. package/dist/utils.js +2 -1
  171. package/dist/utils.js.map +1 -0
  172. package/dist/vite-plugin.d.ts +2 -2
  173. package/dist/vite-plugin.js +5 -4
  174. package/dist/vite-plugin.js.map +1 -0
  175. package/dist/vxjson-serialize.d.ts +5 -6
  176. package/dist/vxjson-serialize.d.ts.map +1 -1
  177. package/dist/vxjson-serialize.js +13 -10
  178. package/dist/vxjson-serialize.js.map +1 -0
  179. package/dist/vxjson.d.ts +4 -3
  180. package/dist/vxjson.d.ts.map +1 -1
  181. package/dist/vxjson.js +10 -20
  182. package/dist/vxjson.js.map +1 -0
  183. package/dist/xxhash.d.ts.map +1 -1
  184. package/dist/xxhash.js +6 -2
  185. package/dist/xxhash.js.map +1 -0
  186. package/package.json +92 -56
  187. package/src/asset-constraints.ts +33 -0
  188. package/src/asset-metadata-store.ts +401 -0
  189. package/src/asset-operations.ts +914 -0
  190. package/src/asset-types.ts +74 -0
  191. package/src/asset-usage.ts +47 -0
  192. package/src/base-content-index.ts +227 -0
  193. package/src/cf-access.ts +85 -0
  194. package/src/component-registry.ts +145 -0
  195. package/src/component-registry.types.ts +12 -0
  196. package/src/component-vocabulary.ts +87 -0
  197. package/src/content-api.interface.ts +94 -0
  198. package/src/content-filters.ts +278 -0
  199. package/src/content-index.ts +642 -0
  200. package/src/content-operations.ts +497 -0
  201. package/src/content-path.ts +113 -0
  202. package/src/content-reader.ts +18 -0
  203. package/src/content-utils.ts +474 -0
  204. package/src/content-watcher.ts +122 -0
  205. package/src/data-index.ts +7 -0
  206. package/src/data.ts +134 -0
  207. package/src/editor-data-bindings.ts +66 -0
  208. package/src/etag-utils.ts +204 -0
  209. package/src/filesystem-content-api.ts +2685 -0
  210. package/src/filesystem-reader-backend.ts +135 -0
  211. package/src/fragment-index.ts +7 -0
  212. package/src/fragments.ts +113 -0
  213. package/src/git-operations.ts +340 -0
  214. package/src/header-parser.ts +272 -0
  215. package/src/in-memory-content-api.ts +760 -0
  216. package/src/index.ts +45 -0
  217. package/src/indexing.ts +8 -0
  218. package/src/json-whitespace-view.ts +155 -0
  219. package/src/middleware.ts +1341 -0
  220. package/src/node.ts +40 -0
  221. package/src/page-references.ts +76 -0
  222. package/src/read-date.ts +7 -0
  223. package/src/reader-backend.ts +54 -0
  224. package/src/reader.ts +15 -0
  225. package/src/schemas/index.ts +113 -0
  226. package/src/site-index.ts +330 -0
  227. package/src/site.ts +198 -0
  228. package/src/types.ts +759 -0
  229. package/src/url-safe-base64.ts +38 -0
  230. package/src/url-validation.ts +121 -0
  231. package/src/utils/atomic-write.ts +20 -0
  232. package/src/utils.ts +84 -0
  233. package/src/vite-plugin.ts +45 -0
  234. package/src/vxjson-serialize.ts +66 -0
  235. package/src/vxjson.ts +442 -0
  236. package/src/xxhash.ts +43 -0
  237. package/dist/asset-manifest.d.ts +0 -23
  238. package/dist/asset-manifest.d.ts.map +0 -1
  239. package/dist/asset-manifest.js +0 -73
  240. package/dist/block-index.d.ts +0 -9
  241. package/dist/block-index.d.ts.map +0 -1
  242. package/dist/block-index.js +0 -11
  243. package/dist/blocks.d.ts +0 -16
  244. package/dist/blocks.d.ts.map +0 -1
  245. package/dist/blocks.js +0 -103
  246. package/dist/component-registry.types-C0ctF9nh.d.mts +0 -71
  247. package/dist/component-registry.types-C0ctF9nh.d.mts.map +0 -1
  248. package/dist/content-index-D092_vk1.mjs +0 -629
  249. package/dist/content-index-D092_vk1.mjs.map +0 -1
  250. package/dist/content-index-D6IVMmAB.d.mts +0 -127
  251. package/dist/content-index-D6IVMmAB.d.mts.map +0 -1
  252. package/dist/content-operations-DIbsxG5A.d.mts +0 -354
  253. package/dist/content-operations-DIbsxG5A.d.mts.map +0 -1
  254. package/dist/content-utils-fWNHTrPy.mjs +0 -280
  255. package/dist/content-utils-fWNHTrPy.mjs.map +0 -1
  256. package/dist/data-DquNIEnQ.mjs +0 -62
  257. package/dist/data-DquNIEnQ.mjs.map +0 -1
  258. package/dist/filesystem-content-api-B1KIQJfk.mjs +0 -2
  259. package/dist/filesystem-content-api-C0HIhqZi.mjs +0 -1620
  260. package/dist/filesystem-content-api-C0HIhqZi.mjs.map +0 -1
  261. package/dist/filesystem-content-api-DCdNi-co.d.mts +0 -134
  262. package/dist/filesystem-content-api-DCdNi-co.d.mts.map +0 -1
  263. package/dist/index-DxVTFK66.d.mts +0 -105
  264. package/dist/index-DxVTFK66.d.mts.map +0 -1
  265. package/dist/index.d.mts +0 -80
  266. package/dist/index.d.mts.map +0 -1
  267. package/dist/index.mjs +0 -78
  268. package/dist/index.mjs.map +0 -1
  269. package/dist/indexing.d.mts +0 -152
  270. package/dist/indexing.d.mts.map +0 -1
  271. package/dist/indexing.mjs +0 -255
  272. package/dist/indexing.mjs.map +0 -1
  273. package/dist/node.d.mts +0 -373
  274. package/dist/node.d.mts.map +0 -1
  275. package/dist/node.mjs +0 -2572
  276. package/dist/node.mjs.map +0 -1
  277. package/dist/reader.d.mts +0 -15
  278. package/dist/reader.d.mts.map +0 -1
  279. package/dist/reader.mjs +0 -22
  280. package/dist/reader.mjs.map +0 -1
  281. package/dist/schemas/index.d.mts +0 -2
  282. package/dist/schemas/index.mjs +0 -2
  283. package/dist/schemas-CFRD0FIQ.mjs +0 -84
  284. package/dist/schemas-CFRD0FIQ.mjs.map +0 -1
  285. package/dist/site-8oaj1Q60.mjs +0 -366
  286. package/dist/site-8oaj1Q60.mjs.map +0 -1
  287. package/dist/site-index-B_254bCc.d.mts +0 -136
  288. package/dist/site-index-B_254bCc.d.mts.map +0 -1
  289. package/dist/types-DOdldbfD.mjs +0 -34
  290. package/dist/types-DOdldbfD.mjs.map +0 -1
  291. package/dist/types-DjfhS272.d.mts +0 -456
  292. package/dist/types-DjfhS272.d.mts.map +0 -1
  293. package/dist/vxjson-qHrgLGip.mjs +0 -555
  294. package/dist/vxjson-qHrgLGip.mjs.map +0 -1
  295. package/dist/vxjson-serialize-geF6qvpn.mjs +0 -34
  296. package/dist/vxjson-serialize-geF6qvpn.mjs.map +0 -1
  297. package/dist/vxjson-serialize.d.mts +0 -17
  298. package/dist/vxjson-serialize.d.mts.map +0 -1
  299. package/dist/vxjson-serialize.mjs +0 -2
package/src/node.ts ADDED
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Node.js-specific exports for @conloca/content-api
3
+ *
4
+ * These exports require Node.js runtime APIs and should not be imported in browser environments.
5
+ * Use the main package exports for browser-compatible APIs.
6
+ */
7
+
8
+ export { type AssetConfig, AssetOperations } from './asset-operations.js';
9
+ export type { AssetEntry, AssetManifestData, AssetMetadata, AssetMetadataPatch } from './asset-types.js';
10
+ // CF Access authentication
11
+ export {
12
+ type CFAccessResult,
13
+ type CFAccessUser,
14
+ extractCFAccessToken,
15
+ validateCFAccessRequest,
16
+ } from './cf-access.js';
17
+ export { ComponentRegistry } from './component-registry.js';
18
+ export * from './component-registry.types.js';
19
+ export type { ContentAPI } from './content-api.interface.js';
20
+ export { ContentIndex } from './content-index.js';
21
+ export * from './content-operations.js';
22
+ export type { ContentFileExtension, ContentPathLayout, LocaleFilename } from './content-path.js';
23
+ export * from './content-utils.js';
24
+ export type { ContentWatcherOptions, WebSocketSender } from './content-watcher.js';
25
+ export { createContentAPI, createContentWatchHandlers } from './content-watcher.js';
26
+ // Node.js specific implementations (require fs, xxhash, etc.)
27
+ export { FileSystemContentAPI, readSitesConfig } from './filesystem-content-api.js';
28
+ export type { FileSystemReaderBackendOptions } from './filesystem-reader-backend.js';
29
+ export { FileSystemReaderBackend } from './filesystem-reader-backend.js';
30
+ export { Fragments } from './fragments.js';
31
+ export type { ParseHeaderResult } from './header-parser.js';
32
+ export { parseContentHeader } from './header-parser.js';
33
+ export { InMemoryContentAPI } from './in-memory-content-api.js';
34
+ export { createContentAPIRouter, createContentMiddleware } from './middleware.js';
35
+ export type { BackendReadResult, ContentReaderBackend } from './reader-backend.js';
36
+ export { Site } from './site.js';
37
+ // Re-export types and utilities that are safe for Node.js environments
38
+ export * from './types.js';
39
+ export type { ConlocaContentOptions } from './vite-plugin.js';
40
+ export { conlocaContent } from './vite-plugin.js';
@@ -0,0 +1,76 @@
1
+ import { readDate } from './read-date.js';
2
+ import type { ContentIdentity, DataBindingConfig, LocaleVersion, PageReference } from './types.js';
3
+
4
+ /**
5
+ * The timestamps a page reference carries. `published` is the date page lists
6
+ * order by: the page's publish time, or its creation time when it has none.
7
+ * Empty or unreadable dates count as not set.
8
+ */
9
+ export function pageTimestampsOf(dates: {
10
+ created?: string | Date | null | undefined;
11
+ modified?: string | Date | null | undefined;
12
+ publishAt?: string | Date | null | undefined;
13
+ }): NonNullable<PageReference['timestamps']> {
14
+ const created = readDate(dates.created);
15
+ const modified = readDate(dates.modified);
16
+ const published = readDate(dates.publishAt) ?? created;
17
+ // Spread rather than `key: undefined`, for hosts compiling with exactOptionalPropertyTypes.
18
+ return { ...(created ? { created } : {}), ...(modified ? { modified } : {}), ...(published ? { published } : {}) };
19
+ }
20
+
21
+ /** The reference a data-bound block reads for one locale of a page. */
22
+ export function pageReferenceOf(
23
+ entry: Pick<ContentIdentity, 'id' | 'collection'>,
24
+ version: LocaleVersion,
25
+ ): PageReference {
26
+ return {
27
+ id: entry.id,
28
+ pathname: version.pathname || '/',
29
+ title: version.meta.title,
30
+ collection: entry.collection,
31
+ meta: version.meta,
32
+ timestamps: pageTimestampsOf(version),
33
+ };
34
+ }
35
+
36
+ type Comparator = (a: PageReference, b: PageReference) => number;
37
+
38
+ /** A page's publish date in ms; a page with none sorts as the oldest. */
39
+ const publishedTime = (page: PageReference) => page.timestamps?.published?.getTime() ?? 0;
40
+
41
+ /** Code-unit order: the same in Bun and every browser, unlike a locale-aware compare. */
42
+ const compareText = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0);
43
+
44
+ /** Ties break by path, then id (a path is unique within one language, which is all one list holds). */
45
+ const byPath: Comparator = (a, b) => compareText(a.pathname, b.pathname) || compareText(a.id, b.id);
46
+
47
+ type PageSort = NonNullable<NonNullable<DataBindingConfig['pages']>['sort']>;
48
+ const ORDER = {
49
+ 'date-desc': (a, b) => publishedTime(b) - publishedTime(a) || byPath(a, b),
50
+ 'date-asc': (a, b) => publishedTime(a) - publishedTime(b) || byPath(a, b),
51
+ title: (a, b) => (a.title || '').localeCompare(b.title || '') || byPath(a, b),
52
+ } satisfies Record<PageSort, Comparator>;
53
+
54
+ /**
55
+ * The page feed a route's `dataBindings.pages` declares: the pages under
56
+ * `prefix` without the listing page itself, sorted (newest publish date first
57
+ * by default; ties by path) and capped. Shared, so the published page and the
58
+ * editor list the same pages in the same order, whatever order they read them in.
59
+ */
60
+ export function selectPagesByPrefix(
61
+ pages: PageReference[],
62
+ prefix: string,
63
+ options?: Omit<NonNullable<DataBindingConfig['pages']>, 'prefix'>,
64
+ ): PageReference[] {
65
+ let filtered = pages.filter(
66
+ (page) => page.pathname.startsWith(prefix) && page.pathname !== prefix && page.pathname !== prefix.slice(0, -1),
67
+ );
68
+
69
+ filtered.sort(ORDER[options?.sort ?? 'date-desc']);
70
+
71
+ if (options?.limit && options.limit > 0) {
72
+ filtered = filtered.slice(0, options.limit);
73
+ }
74
+
75
+ return filtered;
76
+ }
@@ -0,0 +1,7 @@
1
+ /** A stored date as a Date, or undefined when it is empty or cannot be read: such a date counts as not set. */
2
+ export function readDate(value: string | Date | null | undefined): Date | undefined {
3
+ if (!value) return undefined;
4
+ // Copies a Date, so a caller never shares the one it was handed.
5
+ const date = new Date(value);
6
+ return Number.isNaN(date.getTime()) ? undefined : date;
7
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Backend-agnostic source of content blob bytes.
3
+ *
4
+ * The implementation shipped here is `FileSystemReaderBackend` — it wraps
5
+ * fast-glob + node:fs open/read. Another backend can supply the bytes by
6
+ * implementing the same interface.
7
+ *
8
+ * The index hydrator (`ContentIndex.indexAll`) consumes this interface and
9
+ * never knows which backend produced the bytes. The interface is deliberately
10
+ * backend-agnostic: no filesystem types, no platform storage types, no Node `Buffer`.
11
+ * Only `Uint8Array` and the discriminated `BackendReadResult` flow through it,
12
+ * so the same hydrator code runs on any JavaScript runtime.
13
+ */
14
+ export interface ContentReaderBackend {
15
+ /**
16
+ * Stream the set of content paths matching the optional glob, attaching up
17
+ * to `headerBytes` (default 4096) of each blob's bytes plus a `complete`
18
+ * flag set when the buffer holds the entire blob.
19
+ *
20
+ * The header is enough to parse VXJSON manifest metadata and MDX
21
+ * frontmatter without a second read, so the hydrator never has to call
22
+ * `readPath` for index construction.
23
+ *
24
+ * Errors per item are reported as a `BackendReadResult` with `ok: false` —
25
+ * never thrown — so a single corrupt file doesn't abort the whole stream.
26
+ */
27
+ listContentPaths(opts?: { glob?: string; headerBytes?: number }): AsyncIterable<BackendReadResult>;
28
+
29
+ /**
30
+ * Read the full bytes of a content path. Returns null if the path no
31
+ * longer exists (race against deletion is acceptable).
32
+ */
33
+ readPath(path: string): Promise<Uint8Array | null>;
34
+ }
35
+
36
+ /**
37
+ * Discriminated union for per-item results from `listContentPaths`.
38
+ * `ok: true` carries the header bytes plus size/complete metadata; `ok: false`
39
+ * carries an errno-derived reason and a human-readable message.
40
+ */
41
+ export type BackendReadResult =
42
+ | {
43
+ ok: true;
44
+ path: string;
45
+ size: number;
46
+ header: Uint8Array; // length <= size; complete === true ⇒ length === size
47
+ complete: boolean;
48
+ }
49
+ | {
50
+ ok: false;
51
+ path: string;
52
+ reason: 'not-found' | 'permission-denied' | 'corrupt-header' | 'io-error';
53
+ message: string;
54
+ };
package/src/reader.ts ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Read-only Node.js exports for @conloca/content-api.
3
+ *
4
+ * Intended for static/build-time consumers that need content discovery and reads
5
+ * without importing the full server barrel.
6
+ */
7
+
8
+ export type { ContentAPI } from './content-api.interface.js';
9
+ export * from './content-operations.js';
10
+ export { createContentAPI } from './content-reader.js';
11
+ export * from './content-utils.js';
12
+ export { Data } from './data.js';
13
+ export { Fragments } from './fragments.js';
14
+ export { Site } from './site.js';
15
+ export * from './types.js';
@@ -0,0 +1,113 @@
1
+ import { z } from 'zod';
2
+
3
+ // ===== Page Schemas =====
4
+
5
+ /**
6
+ * Base schema for page metadata - editable fields only.
7
+ * Used by CMS forms for dynamic form generation.
8
+ * Add .describe() for form field labels/hints.
9
+ */
10
+ export const pageEditableSchema = z.object({
11
+ title: z.string().describe('Page title for SEO and browser tab'),
12
+ description: z.string().optional().describe('Meta description for search engines'),
13
+ pathname: z.string().describe('URL path (e.g., /about)'),
14
+ publishAt: z.coerce.date().nullable().optional().describe('Schedule publish date/time'),
15
+ unpublishAt: z.coerce.date().nullable().optional().describe('Schedule unpublish date/time'),
16
+ robots: z.string().optional().describe('Robots meta tag (e.g., index, follow)'),
17
+ canonical: z.string().url().optional().describe('Canonical URL for duplicate content'),
18
+ });
19
+
20
+ export type PageEditable = z.infer<typeof pageEditableSchema>;
21
+
22
+ /**
23
+ * Full page schema with system fields.
24
+ * Used by Astro Content Collections for validation.
25
+ */
26
+ export const pageMetaSchema = pageEditableSchema.extend({
27
+ id: z.string(),
28
+ locale: z.string(),
29
+ });
30
+
31
+ export type PageMeta = z.infer<typeof pageMetaSchema>;
32
+
33
+ // ===== Fragment Schemas =====
34
+
35
+ /**
36
+ * Base schema for fragment metadata - editable fields only.
37
+ * Used by CMS forms for dynamic form generation.
38
+ * Add .describe() for form field labels/hints.
39
+ */
40
+ export const fragmentEditableSchema = z.object({
41
+ title: z.string().describe('Display name for the fragment'),
42
+ description: z.string().optional().describe('Brief description of this fragment'),
43
+ category: z.string().optional().describe('Category for organizing fragments (e.g., headers, cta, content)'),
44
+ tags: z.array(z.string()).optional().describe('Tags for filtering and organization'),
45
+ });
46
+
47
+ export type FragmentEditable = z.infer<typeof fragmentEditableSchema>;
48
+
49
+ /**
50
+ * Full fragment schema with system fields.
51
+ * Used by Astro Content Collections for validation.
52
+ */
53
+ export const fragmentMetaSchema = fragmentEditableSchema.extend({
54
+ id: z.string(),
55
+ locale: z.string(),
56
+ name: z.string(),
57
+ });
58
+
59
+ export type FragmentMeta = z.infer<typeof fragmentMetaSchema>;
60
+
61
+ // ===== MDX Page Schemas =====
62
+
63
+ /**
64
+ * Base schema for mdx-type page frontmatter — renderer-neutral.
65
+ *
66
+ * Default for the CMS form when editing an mdx page; projects override via
67
+ * `mdxPages.schema` when they want a renderer-specific shape (e.g. an
68
+ * Astro-collection schema with extra fields).
69
+ *
70
+ * Permissive on purpose: `.passthrough()` lets unknown frontmatter keys flow
71
+ * through unchanged so the file's existing fields are never lost on save.
72
+ */
73
+ export const mdxPageFrontmatterSchema = z
74
+ .object({
75
+ title: z.string().describe('Page title for SEO and browser tab'),
76
+ description: z.string().optional().describe('Meta description for search engines'),
77
+ draft: z.boolean().optional().describe('Hide the page from production builds'),
78
+ })
79
+ .passthrough();
80
+
81
+ export type MdxPageFrontmatter = z.infer<typeof mdxPageFrontmatterSchema>;
82
+
83
+ // ===== Data Schemas =====
84
+
85
+ /**
86
+ * Base schema for data entry metadata - editable fields only.
87
+ * Used by CMS forms for dynamic form generation.
88
+ * Add .describe() for form field labels/hints.
89
+ */
90
+ export const dataEditableSchema = z.object({
91
+ title: z.string().describe('Display name for the entry'),
92
+ description: z.string().optional().describe('Brief description of this entry'),
93
+ category: z.string().optional().describe('Category for organizing entries'),
94
+ tags: z.array(z.string()).optional().describe('Tags for filtering and organization'),
95
+ });
96
+
97
+ export type DataEditable = z.infer<typeof dataEditableSchema>;
98
+
99
+ /**
100
+ * Zod schema for data entry metadata.
101
+ * Covers common organizational fields for structured data collections.
102
+ */
103
+ export const dataMetaSchema = dataEditableSchema.extend({
104
+ // System fields (added by loader)
105
+ id: z.string(),
106
+ locale: z.string(),
107
+ name: z.string(),
108
+
109
+ // Actual data content (arbitrary JSON structure)
110
+ data: z.record(z.string(), z.unknown()).optional(),
111
+ });
112
+
113
+ export type DataMeta = z.infer<typeof dataMetaSchema>;
@@ -0,0 +1,330 @@
1
+ import { isContentPublished, localesOf, mapLocales } from './content-utils.js';
2
+ import type { ContentData, ContentIdentity, ContentManifest, LocaleVersion } from './types.js';
3
+
4
+ /**
5
+ * V8-optimized index for a single site
6
+ * Maintains pathname lookups and collections for one site only
7
+ */
8
+ export class SiteIndex {
9
+ // Primary index: id -> ContentManifest
10
+ private index: Map<string, ContentManifest> = new Map();
11
+
12
+ // Content cache: id -> locale -> content (for small files)
13
+ private contentCache: Map<string, Record<string, ContentData | null>> = new Map();
14
+
15
+ // Pathname index: locale -> (pathname -> ContentManifest)
16
+ // Locale level uses V8-optimized record, pathname level uses Map for dynamic keys
17
+ private pathnameIndex: Record<string, Map<string, ContentManifest>>;
18
+
19
+ // Previous pathname index for redirects: locale -> (oldPathname -> ContentManifest)
20
+ // Same structure - record for locales, Map for pathnames
21
+ private previousPathnameIndex: Record<string, Map<string, ContentManifest>>;
22
+
23
+ // Collections used by this site
24
+ readonly collections: Set<string> = new Set();
25
+
26
+ constructor(readonly locales: string[]) {
27
+ // Initialize pathname indexes - V8-optimized record for locales, Maps for pathnames
28
+ this.pathnameIndex = mapLocales(locales, () => new Map());
29
+ this.previousPathnameIndex = mapLocales(locales, () => new Map());
30
+ }
31
+
32
+ /**
33
+ * Add or update content in the index
34
+ * Merges manifest locales and updates content cache if provided
35
+ */
36
+ addContent(identity: ContentIdentity, localeVersion: LocaleVersion, content?: ContentData | null): void {
37
+ // Get existing manifest
38
+ const existing = this.index.get(identity.id);
39
+ const locale = localeVersion.locale;
40
+
41
+ if (existing) {
42
+ const existingLocale = existing.locales[locale];
43
+
44
+ // Track what we need to rollback
45
+ // First check what pathname currently maps to this manifest in our index
46
+ // This handles in-place updates where the manifest object has already been modified
47
+ let oldPathname: string | undefined;
48
+
49
+ // Find all pathnames that currently point to this manifest
50
+ for (const [pathname, indexedManifest] of this.pathnameIndex[locale].entries()) {
51
+ if (indexedManifest === existing && pathname !== localeVersion.pathname) {
52
+ oldPathname = pathname;
53
+ break;
54
+ }
55
+ }
56
+
57
+ // If we didn't find it in the index, fall back to what's stored in the existing locale
58
+ if (!oldPathname && existingLocale?.pathname && existingLocale.pathname !== localeVersion.pathname) {
59
+ oldPathname = existingLocale.pathname;
60
+ }
61
+
62
+ const hadExistingLocale = !!existingLocale;
63
+ let deletedOldPathname = false;
64
+ let addedNewPathname = false;
65
+ const addedPreviousPathnames: string[] = [];
66
+
67
+ // Update locale manifest if changed
68
+ // We need to always check for pathname changes, even if other fields haven't changed
69
+ // This handles in-place updates where the manifest is modified before calling addContent
70
+ const needsUpdate =
71
+ !existingLocale ||
72
+ existingLocale.etag !== localeVersion.etag ||
73
+ existingLocale.modified !== localeVersion.modified ||
74
+ (oldPathname && oldPathname !== localeVersion.pathname);
75
+
76
+ if (needsUpdate) {
77
+ try {
78
+ // Update the manifest locale
79
+ existing.locales[locale] = localeVersion;
80
+
81
+ // Update pathname indexes after manifest is updated
82
+ if (localeVersion.pathname) {
83
+ // If pathname changed, remove the old one
84
+ if (oldPathname && oldPathname !== localeVersion.pathname) {
85
+ this.pathnameIndex[locale].delete(oldPathname);
86
+ deletedOldPathname = true;
87
+
88
+ // Track the old pathname for redirects only if content is published
89
+ if (isContentPublished(localeVersion)) {
90
+ this.previousPathnameIndex[locale].set(oldPathname, existing);
91
+ addedPreviousPathnames.push(oldPathname);
92
+ }
93
+ }
94
+
95
+ // Add/update the new pathname
96
+ this.pathnameIndex[locale].set(localeVersion.pathname, existing);
97
+ addedNewPathname = true;
98
+ }
99
+
100
+ // Update previous pathname indexes from the manifest
101
+ if (localeVersion.previousPathnames) {
102
+ for (const prevPath of Object.keys(localeVersion.previousPathnames)) {
103
+ // Only add if not already there
104
+ if (!this.previousPathnameIndex[locale].has(prevPath)) {
105
+ this.previousPathnameIndex[locale].set(prevPath, existing);
106
+ addedPreviousPathnames.push(prevPath);
107
+ }
108
+ }
109
+ }
110
+ } catch (error) {
111
+ // Rollback only what we changed
112
+ if (hadExistingLocale) {
113
+ existing.locales[locale] = existingLocale;
114
+ } else {
115
+ existing.locales[locale] = undefined;
116
+ }
117
+
118
+ // Rollback pathname index changes
119
+ if (deletedOldPathname && oldPathname) {
120
+ this.pathnameIndex[locale].set(oldPathname, existing);
121
+ }
122
+ if (addedNewPathname && localeVersion.pathname) {
123
+ this.pathnameIndex[locale].delete(localeVersion.pathname);
124
+ }
125
+
126
+ // Rollback previous pathname additions
127
+ for (const prevPath of addedPreviousPathnames) {
128
+ this.previousPathnameIndex[locale].delete(prevPath);
129
+ }
130
+
131
+ throw error;
132
+ }
133
+ }
134
+ } else {
135
+ // New content - create ContentManifest with single locale
136
+ const newManifest: ContentManifest = {
137
+ id: identity.id,
138
+ type: identity.type,
139
+ kind: identity.kind,
140
+ site: identity.site,
141
+ collection: identity.collection,
142
+ locales: {
143
+ [locale]: localeVersion,
144
+ },
145
+ };
146
+
147
+ this.index.set(identity.id, newManifest);
148
+ this.collections.add(identity.collection);
149
+
150
+ // Add pathname indexes
151
+ if (localeVersion.pathname) {
152
+ this.pathnameIndex[locale].set(localeVersion.pathname, newManifest);
153
+ }
154
+ if (localeVersion.previousPathnames) {
155
+ for (const prevPath of Object.keys(localeVersion.previousPathnames)) {
156
+ this.previousPathnameIndex[locale].set(prevPath, newManifest);
157
+ }
158
+ }
159
+ }
160
+
161
+ // Update content cache if provided
162
+ if (content !== undefined) {
163
+ const existingCache = this.contentCache.get(identity.id);
164
+ if (existingCache) {
165
+ // Update single locale content
166
+ existingCache[locale] = content;
167
+ } else {
168
+ this.contentCache.set(identity.id, { [locale]: content });
169
+ }
170
+ }
171
+ }
172
+
173
+ /**
174
+ * Remove a specific locale from an entry
175
+ */
176
+ removeLocale(id: string, locale: string): void {
177
+ const manifest = this.index.get(id);
178
+ if (!manifest?.locales[locale]) return;
179
+
180
+ const localeVersion = manifest.locales[locale];
181
+
182
+ // Remove from pathname indexes
183
+ if (localeVersion.pathname) {
184
+ this.pathnameIndex[locale].delete(localeVersion.pathname);
185
+ }
186
+ if (localeVersion.previousPathnames) {
187
+ for (const prevPath of Object.keys(localeVersion.previousPathnames)) {
188
+ this.previousPathnameIndex[locale].delete(prevPath);
189
+ }
190
+ }
191
+
192
+ // Remove locale from manifest
193
+ manifest.locales[locale] = undefined;
194
+
195
+ // Remove from content cache
196
+ const contentCache = this.contentCache.get(id);
197
+ if (contentCache) {
198
+ delete contentCache[locale];
199
+ }
200
+
201
+ // If no DEFINED locales left, remove the entire entry. Count values, not
202
+ // keys — the removal above assigns `undefined` (keeping the key), so the
203
+ // key count never reaches zero (BaseContentIndex.removeLocale already
204
+ // counts defined values; this mirrored it incorrectly).
205
+ if (Object.values(manifest.locales).every((lv) => lv === undefined)) {
206
+ this.removeEntry(id);
207
+ }
208
+ }
209
+
210
+ /**
211
+ * Remove an entry from the index
212
+ */
213
+ removeEntry(id: string): void {
214
+ const manifest = this.index.get(id);
215
+ if (!manifest) return;
216
+
217
+ // Remove from pathname indexes
218
+ for (const localeVersion of localesOf(manifest)) {
219
+ const { locale } = localeVersion;
220
+ if (localeVersion.pathname) {
221
+ this.pathnameIndex[locale].delete(localeVersion.pathname);
222
+ }
223
+ if (localeVersion.previousPathnames) {
224
+ for (const prevPath of Object.keys(localeVersion.previousPathnames)) {
225
+ this.previousPathnameIndex[locale].delete(prevPath);
226
+ }
227
+ }
228
+ }
229
+
230
+ // Remove from primary index
231
+ this.index.delete(id);
232
+
233
+ // Remove from content cache
234
+ this.contentCache.delete(id);
235
+ }
236
+
237
+ /**
238
+ * Get cached content for a specific locale
239
+ */
240
+ getCachedContent(id: string, locale: string): ContentData | null {
241
+ const localeContent = this.contentCache.get(id);
242
+ return localeContent?.[locale] || null;
243
+ }
244
+
245
+ /**
246
+ * Get content manifest
247
+ */
248
+ getManifest(id: string): ContentManifest | null {
249
+ return this.index.get(id) || null;
250
+ }
251
+
252
+ /**
253
+ * Find content by pathname
254
+ */
255
+ getByPathname(pathname: string, locale?: string): ContentManifest | null {
256
+ if (locale) {
257
+ return this.pathnameIndex[locale]?.get(pathname) || null;
258
+ }
259
+
260
+ // Check all locales
261
+ for (const locale of this.locales) {
262
+ const manifest = this.pathnameIndex[locale].get(pathname);
263
+ if (manifest) {
264
+ return manifest;
265
+ }
266
+ }
267
+
268
+ return null;
269
+ }
270
+
271
+ /**
272
+ * Find content by previous pathname (for redirects)
273
+ */
274
+ getByPreviousPathname(pathname: string, locale?: string): ContentManifest | null {
275
+ if (locale) {
276
+ return this.previousPathnameIndex[locale]?.get(pathname) || null;
277
+ }
278
+
279
+ // Check all locales
280
+ for (const locale of this.locales) {
281
+ const manifest = this.previousPathnameIndex[locale].get(pathname);
282
+ if (manifest) {
283
+ return manifest;
284
+ }
285
+ }
286
+
287
+ return null;
288
+ }
289
+
290
+ /**
291
+ * Generator for all manifests
292
+ */
293
+ *getAllManifests(): Generator<ContentManifest> {
294
+ for (const manifest of this.index.values()) {
295
+ yield manifest;
296
+ }
297
+ }
298
+
299
+ /**
300
+ * Generator for manifests by locale
301
+ */
302
+ *getManifestsByLocale(locale: string): Generator<ContentManifest> {
303
+ for (const manifest of this.index.values()) {
304
+ if (manifest.locales[locale]) {
305
+ yield manifest;
306
+ }
307
+ }
308
+ }
309
+
310
+ /**
311
+ * Get the count of indexed content entries (unique IDs)
312
+ */
313
+ get entryCount(): number {
314
+ return this.index.size;
315
+ }
316
+
317
+ /**
318
+ * Clear all indexes
319
+ */
320
+ clear(): void {
321
+ this.index.clear();
322
+ this.contentCache.clear();
323
+ // Clear all Maps in the pathname indexes
324
+ for (const locale of this.locales) {
325
+ this.pathnameIndex[locale].clear();
326
+ this.previousPathnameIndex[locale].clear();
327
+ }
328
+ this.collections.clear();
329
+ }
330
+ }