@conloca/content-api 0.1.15 → 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 (281) 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 +164 -0
  10. package/dist/asset-operations.d.ts.map +1 -0
  11. package/dist/asset-operations.js +784 -0
  12. package/dist/asset-operations.js.map +1 -0
  13. package/dist/asset-types.d.ts +72 -0
  14. package/dist/asset-types.d.ts.map +1 -0
  15. package/dist/asset-types.js +13 -0
  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 +63 -0
  22. package/dist/base-content-index.d.ts.map +1 -0
  23. package/dist/base-content-index.js +200 -0
  24. package/dist/base-content-index.js.map +1 -0
  25. package/dist/cf-access.d.ts +31 -0
  26. package/dist/cf-access.d.ts.map +1 -0
  27. package/dist/cf-access.js +67 -0
  28. package/dist/cf-access.js.map +1 -0
  29. package/dist/component-registry.d.ts +50 -0
  30. package/dist/component-registry.d.ts.map +1 -0
  31. package/dist/component-registry.js +121 -0
  32. package/dist/component-registry.js.map +1 -0
  33. package/dist/component-registry.types.d.ts +11 -0
  34. package/dist/component-registry.types.d.ts.map +1 -0
  35. package/dist/component-registry.types.js +2 -0
  36. package/dist/component-registry.types.js.map +1 -0
  37. package/dist/component-vocabulary.d.ts +33 -0
  38. package/dist/component-vocabulary.d.ts.map +1 -0
  39. package/dist/component-vocabulary.js +105 -0
  40. package/dist/component-vocabulary.js.map +1 -0
  41. package/dist/content-api.interface.d.ts +64 -0
  42. package/dist/content-api.interface.d.ts.map +1 -0
  43. package/dist/content-api.interface.js +2 -0
  44. package/dist/content-api.interface.js.map +1 -0
  45. package/dist/content-filters.d.ts +31 -0
  46. package/dist/content-filters.d.ts.map +1 -0
  47. package/dist/content-filters.js +247 -0
  48. package/dist/content-filters.js.map +1 -0
  49. package/dist/content-index.d.ts +184 -0
  50. package/dist/content-index.d.ts.map +1 -0
  51. package/dist/content-index.js +546 -0
  52. package/dist/content-index.js.map +1 -0
  53. package/dist/content-operations.d.ts +157 -0
  54. package/dist/content-operations.d.ts.map +1 -0
  55. package/dist/content-operations.js +359 -0
  56. package/dist/content-operations.js.map +1 -0
  57. package/dist/content-path.d.ts +59 -0
  58. package/dist/content-path.d.ts.map +1 -0
  59. package/dist/content-path.js +88 -0
  60. package/dist/content-path.js.map +1 -0
  61. package/dist/content-reader.d.ts +10 -0
  62. package/dist/content-reader.d.ts.map +1 -0
  63. package/dist/content-reader.js +14 -0
  64. package/dist/content-reader.js.map +1 -0
  65. package/dist/content-utils.d.ts +175 -0
  66. package/dist/content-utils.d.ts.map +1 -0
  67. package/dist/content-utils.js +421 -0
  68. package/dist/content-utils.js.map +1 -0
  69. package/dist/content-watcher.d.ts +43 -0
  70. package/dist/content-watcher.d.ts.map +1 -0
  71. package/dist/content-watcher.js +86 -0
  72. package/dist/content-watcher.js.map +1 -0
  73. package/dist/data-index.d.ts +8 -0
  74. package/dist/data-index.d.ts.map +1 -0
  75. package/dist/data-index.js +8 -0
  76. package/dist/data-index.js.map +1 -0
  77. package/dist/data.d.ts +35 -0
  78. package/dist/data.d.ts.map +1 -0
  79. package/dist/data.js +104 -0
  80. package/dist/data.js.map +1 -0
  81. package/dist/editor-data-bindings.d.ts +10 -0
  82. package/dist/editor-data-bindings.d.ts.map +1 -0
  83. package/dist/editor-data-bindings.js +67 -0
  84. package/dist/editor-data-bindings.js.map +1 -0
  85. package/dist/etag-utils.d.ts +77 -0
  86. package/dist/etag-utils.d.ts.map +1 -0
  87. package/dist/etag-utils.js +161 -0
  88. package/dist/etag-utils.js.map +1 -0
  89. package/dist/filesystem-content-api.d.ts +162 -0
  90. package/dist/filesystem-content-api.d.ts.map +1 -0
  91. package/dist/filesystem-content-api.js +2307 -0
  92. package/dist/filesystem-content-api.js.map +1 -0
  93. package/dist/filesystem-reader-backend.d.ts +46 -0
  94. package/dist/filesystem-reader-backend.d.ts.map +1 -0
  95. package/dist/filesystem-reader-backend.js +119 -0
  96. package/dist/filesystem-reader-backend.js.map +1 -0
  97. package/dist/fragment-index.d.ts +8 -0
  98. package/dist/fragment-index.d.ts.map +1 -0
  99. package/dist/fragment-index.js +8 -0
  100. package/dist/fragment-index.js.map +1 -0
  101. package/dist/fragments.d.ts +16 -0
  102. package/dist/fragments.d.ts.map +1 -0
  103. package/dist/fragments.js +104 -0
  104. package/dist/fragments.js.map +1 -0
  105. package/dist/git-operations.d.ts +49 -0
  106. package/dist/git-operations.d.ts.map +1 -0
  107. package/dist/git-operations.js +256 -0
  108. package/dist/git-operations.js.map +1 -0
  109. package/dist/header-parser.d.ts +37 -0
  110. package/dist/header-parser.d.ts.map +1 -0
  111. package/dist/header-parser.js +233 -0
  112. package/dist/header-parser.js.map +1 -0
  113. package/dist/in-memory-content-api.d.ts +35 -0
  114. package/dist/in-memory-content-api.d.ts.map +1 -0
  115. package/dist/in-memory-content-api.js +609 -0
  116. package/dist/in-memory-content-api.js.map +1 -0
  117. package/dist/index.d.ts +26 -0
  118. package/dist/index.d.ts.map +1 -0
  119. package/dist/index.js +28 -0
  120. package/dist/index.js.map +1 -0
  121. package/dist/indexing.d.ts +5 -0
  122. package/dist/indexing.d.ts.map +1 -0
  123. package/dist/indexing.js +8 -0
  124. package/dist/indexing.js.map +1 -0
  125. package/dist/json-whitespace-view.d.ts +32 -0
  126. package/dist/json-whitespace-view.d.ts.map +1 -0
  127. package/dist/json-whitespace-view.js +153 -0
  128. package/dist/json-whitespace-view.js.map +1 -0
  129. package/dist/middleware.d.ts +20 -0
  130. package/dist/middleware.d.ts.map +1 -0
  131. package/dist/middleware.js +1082 -0
  132. package/dist/middleware.js.map +1 -0
  133. package/dist/node.d.ts +32 -0
  134. package/dist/node.d.ts.map +1 -0
  135. package/dist/node.js +27 -0
  136. package/dist/node.js.map +1 -0
  137. package/dist/page-references.d.ts +21 -0
  138. package/dist/page-references.d.ts.map +1 -0
  139. package/dist/page-references.js +50 -0
  140. package/dist/page-references.js.map +1 -0
  141. package/dist/read-date.d.ts +3 -0
  142. package/dist/read-date.d.ts.map +1 -0
  143. package/dist/read-date.js +9 -0
  144. package/dist/read-date.js.map +1 -0
  145. package/dist/reader-backend.d.ts +54 -0
  146. package/dist/reader-backend.d.ts.map +1 -0
  147. package/dist/reader-backend.js +2 -0
  148. package/dist/reader-backend.js.map +1 -0
  149. package/dist/reader.d.ts +15 -0
  150. package/dist/reader.d.ts.map +1 -0
  151. package/dist/reader.js +14 -0
  152. package/dist/reader.js.map +1 -0
  153. package/dist/schemas/index.d.ts +102 -0
  154. package/dist/schemas/index.d.ts.map +1 -0
  155. package/dist/schemas/index.js +88 -0
  156. package/dist/schemas/index.js.map +1 -0
  157. package/dist/site-index.d.ts +60 -0
  158. package/dist/site-index.d.ts.map +1 -0
  159. package/dist/site-index.js +291 -0
  160. package/dist/site-index.js.map +1 -0
  161. package/dist/site.d.ts +23 -0
  162. package/dist/site.d.ts.map +1 -0
  163. package/dist/site.js +180 -0
  164. package/dist/site.js.map +1 -0
  165. package/dist/types.d.ts +641 -0
  166. package/dist/types.d.ts.map +1 -0
  167. package/dist/types.js +45 -0
  168. package/dist/types.js.map +1 -0
  169. package/dist/url-safe-base64.d.ts +12 -0
  170. package/dist/url-safe-base64.d.ts.map +1 -0
  171. package/dist/url-safe-base64.js +41 -0
  172. package/dist/url-safe-base64.js.map +1 -0
  173. package/dist/url-validation.d.ts +28 -0
  174. package/dist/url-validation.d.ts.map +1 -0
  175. package/dist/url-validation.js +123 -0
  176. package/dist/url-validation.js.map +1 -0
  177. package/dist/utils/atomic-write.d.ts +6 -0
  178. package/dist/utils/atomic-write.d.ts.map +1 -0
  179. package/dist/utils/atomic-write.js +21 -0
  180. package/dist/utils/atomic-write.js.map +1 -0
  181. package/dist/utils.d.ts +46 -0
  182. package/dist/utils.d.ts.map +1 -0
  183. package/dist/utils.js +71 -0
  184. package/dist/utils.js.map +1 -0
  185. package/dist/vite-plugin.d.ts +12 -0
  186. package/dist/vite-plugin.d.ts.map +1 -0
  187. package/dist/vite-plugin.js +30 -0
  188. package/dist/vite-plugin.js.map +1 -0
  189. package/dist/vxjson-serialize.d.ts +13 -0
  190. package/dist/vxjson-serialize.d.ts.map +1 -0
  191. package/dist/vxjson-serialize.js +53 -0
  192. package/dist/vxjson-serialize.js.map +1 -0
  193. package/dist/vxjson.d.ts +113 -0
  194. package/dist/vxjson.d.ts.map +1 -0
  195. package/dist/vxjson.js +381 -0
  196. package/dist/vxjson.js.map +1 -0
  197. package/dist/xxhash.d.ts +10 -0
  198. package/dist/xxhash.d.ts.map +1 -0
  199. package/dist/xxhash.js +35 -0
  200. package/dist/xxhash.js.map +1 -0
  201. package/package.json +132 -34
  202. package/src/asset-constraints.ts +33 -0
  203. package/src/asset-metadata-store.ts +401 -0
  204. package/src/asset-operations.ts +914 -0
  205. package/src/asset-types.ts +74 -0
  206. package/src/asset-usage.ts +47 -0
  207. package/src/base-content-index.ts +227 -0
  208. package/src/cf-access.ts +85 -0
  209. package/src/component-registry.ts +145 -0
  210. package/src/component-registry.types.ts +12 -0
  211. package/src/component-vocabulary.ts +87 -0
  212. package/src/content-api.interface.ts +94 -0
  213. package/src/content-filters.ts +278 -0
  214. package/src/content-index.ts +642 -0
  215. package/src/content-operations.ts +497 -0
  216. package/src/content-path.ts +113 -0
  217. package/src/content-reader.ts +18 -0
  218. package/src/content-utils.ts +474 -0
  219. package/src/content-watcher.ts +122 -0
  220. package/src/data-index.ts +7 -0
  221. package/src/data.ts +134 -0
  222. package/src/editor-data-bindings.ts +66 -0
  223. package/src/etag-utils.ts +204 -0
  224. package/src/filesystem-content-api.ts +2685 -0
  225. package/src/filesystem-reader-backend.ts +135 -0
  226. package/src/fragment-index.ts +7 -0
  227. package/src/fragments.ts +113 -0
  228. package/src/git-operations.ts +340 -0
  229. package/src/header-parser.ts +272 -0
  230. package/src/in-memory-content-api.ts +760 -0
  231. package/src/index.ts +45 -0
  232. package/src/indexing.ts +8 -0
  233. package/src/json-whitespace-view.ts +155 -0
  234. package/src/middleware.ts +1341 -0
  235. package/src/node.ts +40 -0
  236. package/src/page-references.ts +76 -0
  237. package/src/read-date.ts +7 -0
  238. package/src/reader-backend.ts +54 -0
  239. package/src/reader.ts +15 -0
  240. package/src/schemas/index.ts +113 -0
  241. package/src/site-index.ts +330 -0
  242. package/src/site.ts +198 -0
  243. package/src/types.ts +759 -0
  244. package/src/url-safe-base64.ts +38 -0
  245. package/src/url-validation.ts +121 -0
  246. package/src/utils/atomic-write.ts +20 -0
  247. package/src/utils.ts +84 -0
  248. package/src/vite-plugin.ts +45 -0
  249. package/src/vxjson-serialize.ts +66 -0
  250. package/src/vxjson.ts +442 -0
  251. package/src/xxhash.ts +43 -0
  252. package/dist/component-registry.types-CBGONnYn.d.mts +0 -71
  253. package/dist/component-registry.types-CBGONnYn.d.mts.map +0 -1
  254. package/dist/content-operations-Dv67amz3.d.mts +0 -852
  255. package/dist/content-operations-Dv67amz3.d.mts.map +0 -1
  256. package/dist/data-Db1Iv9T0.mjs +0 -60
  257. package/dist/data-Db1Iv9T0.mjs.map +0 -1
  258. package/dist/filesystem-content-api-C1TM0GLo.d.mts +0 -119
  259. package/dist/filesystem-content-api-C1TM0GLo.d.mts.map +0 -1
  260. package/dist/filesystem-content-api-kxKY5IUw.mjs +0 -2678
  261. package/dist/filesystem-content-api-kxKY5IUw.mjs.map +0 -1
  262. package/dist/index.d.mts +0 -79
  263. package/dist/index.d.mts.map +0 -1
  264. package/dist/index.mjs +0 -47
  265. package/dist/index.mjs.map +0 -1
  266. package/dist/node.d.mts +0 -378
  267. package/dist/node.d.mts.map +0 -1
  268. package/dist/node.mjs +0 -2618
  269. package/dist/node.mjs.map +0 -1
  270. package/dist/reader.d.mts +0 -18
  271. package/dist/reader.d.mts.map +0 -1
  272. package/dist/reader.mjs +0 -22
  273. package/dist/reader.mjs.map +0 -1
  274. package/dist/schemas/index.d.mts +0 -90
  275. package/dist/schemas/index.d.mts.map +0 -1
  276. package/dist/schemas/index.mjs +0 -70
  277. package/dist/schemas/index.mjs.map +0 -1
  278. package/dist/site-vNtJ5jCh.mjs +0 -538
  279. package/dist/site-vNtJ5jCh.mjs.map +0 -1
  280. package/dist/types-DWt912tu.mjs +0 -33
  281. package/dist/types-DWt912tu.mjs.map +0 -1
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Convert bytes to URL-safe base64
3
+ * Replaces + with -, / with _, and removes = padding
4
+ */
5
+ export function toUrlSafeBase64(bytes: Uint8Array): string {
6
+ const b64 = Buffer.from(bytes).toString('base64');
7
+ const buf = Buffer.from(b64, 'ascii');
8
+ let write = 0;
9
+ for (let read = 0; read < buf.length; read++) {
10
+ const c = buf[read];
11
+ if (c === 0x2b) {
12
+ // '+'
13
+ buf[write++] = 0x2d; // '-'
14
+ } else if (c === 0x2f) {
15
+ // '/'
16
+ buf[write++] = 0x5f; // '_'
17
+ } else if (c === 0x3d) {
18
+ // '='
19
+ // skip padding
20
+ } else {
21
+ buf[write++] = c;
22
+ }
23
+ }
24
+ return buf.subarray(0, write).toString('ascii');
25
+ }
26
+
27
+ /**
28
+ * Convert BigInt to URL-safe base64
29
+ * @param value - The BigInt value to convert
30
+ * @returns URL-safe base64 string representation
31
+ */
32
+ export function bigintToUrlSafeBase64(value: bigint): string {
33
+ // Convert BigInt to 8-byte buffer
34
+ const hashBuffer = new Uint8Array(8);
35
+ const dataView = new DataView(hashBuffer.buffer);
36
+ dataView.setBigUint64(0, value, false); // big-endian
37
+ return toUrlSafeBase64(hashBuffer);
38
+ }
@@ -0,0 +1,121 @@
1
+ /**
2
+ * URL validation for SSRF prevention in server-side fetch operations.
3
+ * Blocks private/reserved IP addresses and non-http(s) schemes.
4
+ */
5
+
6
+ const BLOCKED_HOSTNAMES = new Set(['localhost']);
7
+
8
+ /** Check if an IPv4 address string points to a private/reserved range */
9
+ function isPrivateIPv4(hostname: string): boolean {
10
+ const parts = hostname.split('.');
11
+ if (parts.length !== 4) return false;
12
+
13
+ const octets = parts.map((p) => Number.parseInt(p, 10));
14
+ if (octets.some((o) => Number.isNaN(o) || o < 0 || o > 255)) return false;
15
+
16
+ const [first, second] = octets;
17
+
18
+ // 0.x.x.x — unspecified
19
+ if (first === 0) return true;
20
+ // 10.x.x.x — Class A private
21
+ if (first === 10) return true;
22
+ // 127.x.x.x — loopback
23
+ if (first === 127) return true;
24
+ // 169.254.x.x — link-local (cloud metadata endpoint)
25
+ if (first === 169 && second === 254) return true;
26
+ // 172.16.0.0 – 172.31.255.255 — Class B private
27
+ if (first === 172 && second >= 16 && second <= 31) return true;
28
+ // 192.168.x.x — Class C private
29
+ if (first === 192 && second === 168) return true;
30
+
31
+ return false;
32
+ }
33
+
34
+ /** Check if an IPv6 address string points to a private/reserved range */
35
+ function isPrivateIPv6(hostname: string): boolean {
36
+ const lower = hostname.toLowerCase();
37
+
38
+ // ::1 — loopback
39
+ if (lower === '::1') return true;
40
+ // :: — unspecified
41
+ if (lower === '::') return true;
42
+ // fe80: — link-local
43
+ if (lower.startsWith('fe80:') || lower.startsWith('fe80%')) return true;
44
+ // fc00::/7 — unique local address (ULA). Anchored to the address form, as a
45
+ // bare `fc`/`fd` prefix also matches public domains such as `fdic.gov`.
46
+ if (/^f[cd][0-9a-f]{2}[:%]/.test(lower)) return true;
47
+
48
+ return false;
49
+ }
50
+
51
+ /**
52
+ * The dotted-quad address embedded in an IPv4-mapped IPv6 host, or `null`.
53
+ * Both spellings of the tail are read: dotted (`::ffff:127.0.0.1`) and hex
54
+ * (`::ffff:7f00:1`), the latter being what URL parsing canonicalizes to.
55
+ */
56
+ function ipv4MappedEmbedded(host: string): string | null {
57
+ if (!host.startsWith('::ffff:')) return null;
58
+ const tail = host.slice('::ffff:'.length);
59
+ if (/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(tail)) return tail;
60
+ const hex = tail.match(/^([0-9a-f]{1,4}):([0-9a-f]{1,4})$/);
61
+ if (!hex) return null;
62
+ const high = Number.parseInt(hex[1], 16);
63
+ const low = Number.parseInt(hex[2], 16);
64
+ return `${(high >> 8) & 0xff}.${high & 0xff}.${(low >> 8) & 0xff}.${low & 0xff}`;
65
+ }
66
+
67
+ /**
68
+ * Check if a hostname names a private/reserved destination: a blocked name,
69
+ * or an address in a loopback/link-local/private range. A trailing root-label
70
+ * dot and the IPv6 brackets `URL.hostname` keeps are stripped internally, and
71
+ * an IPv4-mapped IPv6 literal is canonicalized to the address it embeds so the
72
+ * mapped spelling cannot smuggle a private destination past the range tables.
73
+ *
74
+ * Exported so other media import guards can reuse these range tables
75
+ * instead of copying them.
76
+ *
77
+ * @param hostname The hostname to check (bare or bracketed IPv6)
78
+ * @returns true if the hostname points to a private/reserved destination
79
+ */
80
+ export function isPrivateFetchHost(hostname: string): boolean {
81
+ // A trailing root-label dot is valid syntax for the same host — `localhost.`
82
+ // resolves identically — so drop it before any table sees the name. Stripped
83
+ // ahead of the brackets, whose closing match is anchored to the end.
84
+ const rooted = hostname.replace(/\.+$/, '');
85
+ // URL.hostname keeps the brackets on an IPv6 literal; strip them here so
86
+ // consumers don't have to (no-op for bare input)
87
+ const bare = rooted.replace(/^\[|\]$/g, '').toLowerCase();
88
+
89
+ // Check blocked hostnames
90
+ if (BLOCKED_HOSTNAMES.has(bare)) return true;
91
+ // An IPv4-mapped IPv6 literal reaches the address it embeds, so range-check
92
+ // that address rather than the mapped spelling
93
+ const address = ipv4MappedEmbedded(bare) ?? bare;
94
+ // Check IPv4 private ranges
95
+ if (isPrivateIPv4(address)) return true;
96
+ // Check IPv6 private ranges
97
+ return isPrivateIPv6(address);
98
+ }
99
+
100
+ /**
101
+ * Validate that a URL is safe to fetch server-side.
102
+ * Blocks private/reserved IP addresses and non-http(s) schemes.
103
+ *
104
+ * @param url The URL string to validate
105
+ * @returns The parsed URL object if valid
106
+ * @throws Error if the URL is invalid, uses a blocked scheme, or points to a private IP
107
+ */
108
+ export function validateFetchUrl(url: string): URL {
109
+ const parsed = new URL(url); // throws on invalid URLs
110
+
111
+ // Check scheme
112
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
113
+ throw new Error('URL scheme must be http or https');
114
+ }
115
+
116
+ if (isPrivateFetchHost(parsed.hostname)) {
117
+ throw new Error('URL points to a private/reserved IP address');
118
+ }
119
+
120
+ return parsed;
121
+ }
@@ -0,0 +1,20 @@
1
+ import { rename, unlink, writeFile } from 'node:fs/promises';
2
+
3
+ /**
4
+ * Write file atomically via temp file + rename to prevent corruption on crash.
5
+ * Callers are responsible for ensuring the parent directory exists.
6
+ */
7
+ export async function atomicWriteFile(filePath: string, content: string): Promise<void> {
8
+ const hrtime = process.hrtime.bigint();
9
+ const tempFile = `${filePath}.tmp.${process.pid}.${hrtime}`;
10
+
11
+ try {
12
+ await writeFile(tempFile, content);
13
+ await rename(tempFile, filePath);
14
+ } catch (error) {
15
+ try {
16
+ await unlink(tempFile);
17
+ } catch {}
18
+ throw error;
19
+ }
20
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Utilities for working with the `<name>.<locale>.<ext>` suffix pattern.
3
+ */
4
+ export const ContentUtils = {
5
+ /**
6
+ * Extract locale from filename
7
+ * @example
8
+ * - about.en.vxjson → 'en'
9
+ * - about.nl.vxjson → 'nl'
10
+ * - hero-main.mdx → 'en' (default)
11
+ * - hero-main.nl.mdx → 'nl'
12
+ */
13
+ extractLocale(filename: string): string {
14
+ const match = filename.match(/\.([a-z]{2})\.(vxjson|mdx)$/);
15
+ return match?.[1] || 'en';
16
+ },
17
+
18
+ /**
19
+ * Get base path without locale and extension
20
+ * @example
21
+ * - about.en.vxjson → 'about'
22
+ * - about.nl.vxjson → 'about'
23
+ * - hero-main.mdx → 'hero-main'
24
+ */
25
+ extractBasePath(filename: string): string {
26
+ return filename.replace(/\.([a-z]{2})?\.(vxjson|mdx)$/, '').replace(/\.(vxjson|mdx)$/, '');
27
+ },
28
+
29
+ /**
30
+ * Build filename from base path and locale
31
+ * @example
32
+ * - ('about', 'en') → 'about.en.vxjson'
33
+ * - ('about', 'nl') → 'about.nl.vxjson'
34
+ */
35
+ buildFilename(basePath: string, locale: string, extension = 'vxjson'): string {
36
+ if (locale === 'en' && extension === 'vxjson') {
37
+ return `${basePath}.${extension}`;
38
+ }
39
+ return `${basePath}.${locale}.${extension}`;
40
+ },
41
+
42
+ /**
43
+ * Group files by base path for i18n
44
+ */
45
+ groupByContent(filenames: string[]): Map<string, Record<string, string>> {
46
+ const groups = new Map<string, Record<string, string>>();
47
+
48
+ for (const filename of filenames) {
49
+ const base = this.extractBasePath(filename);
50
+ const locale = this.extractLocale(filename);
51
+
52
+ if (!groups.has(base)) {
53
+ groups.set(base, {});
54
+ }
55
+
56
+ const localeFiles = groups.get(base)!;
57
+ localeFiles[locale] = filename;
58
+ }
59
+
60
+ return groups;
61
+ },
62
+
63
+ /**
64
+ * Check if a file is a content file (vxjson or mdx)
65
+ */
66
+ isContentFile(filename: string): boolean {
67
+ return /\.(vxjson|mdx)$/.test(filename);
68
+ },
69
+
70
+ /**
71
+ * Parse filename to get all components
72
+ */
73
+ parseFilename(filename: string): {
74
+ basePath: string;
75
+ locale: string;
76
+ extension: string;
77
+ } {
78
+ const extension = filename.match(/\.(vxjson|mdx)$/)?.[1] || '';
79
+ const locale = this.extractLocale(filename);
80
+ const basePath = this.extractBasePath(filename);
81
+
82
+ return { basePath, locale, extension };
83
+ },
84
+ };
@@ -0,0 +1,45 @@
1
+ import { getRequestListener } from '@hono/node-server';
2
+ import type { Plugin, ViteDevServer } from 'vite';
3
+ import { createContentAPI, createContentWatchHandlers } from './content-watcher.js';
4
+ import type { FileSystemContentAPI } from './filesystem-content-api.js';
5
+ import { createContentAPIRouter } from './middleware.js';
6
+
7
+ export interface ConlocaContentOptions {
8
+ contentRoot: string;
9
+ canvasDir?: string;
10
+ basePath?: string;
11
+ }
12
+
13
+ /**
14
+ * Vite plugin wrapper for content middleware, mounting the content API routes
15
+ * on the dev server so editing works without a separate backend.
16
+ */
17
+ export function conlocaContent(options: ConlocaContentOptions): Plugin {
18
+ let contentApi: FileSystemContentAPI;
19
+
20
+ const basePath = options.basePath || '/__conloca/api/content';
21
+
22
+ return {
23
+ name: 'vite-plugin-conloca-content',
24
+
25
+ async configureServer(server: ViteDevServer) {
26
+ // Initialize content API
27
+ contentApi = await createContentAPI(options);
28
+
29
+ // Set up content file watcher
30
+ server.watcher.add(options.contentRoot);
31
+
32
+ // Create Hono app with routes
33
+ const app = createContentAPIRouter(contentApi);
34
+
35
+ // Add to Vite as middleware using @hono/node-server
36
+ server.middlewares.use(basePath, getRequestListener(app.fetch));
37
+
38
+ // Set up HMR for content changes using extracted handlers
39
+ const handlers = createContentWatchHandlers(contentApi, options, server.ws);
40
+ server.watcher.on('change', handlers.onChange);
41
+ server.watcher.on('add', handlers.onAdd);
42
+ server.watcher.on('unlink', handlers.onUnlink);
43
+ },
44
+ };
45
+ }
@@ -0,0 +1,66 @@
1
+ import sortKeys from 'sort-keys';
2
+ import type { VXJSONFile } from './types.js';
3
+
4
+ /**
5
+ * The canonical VXJSON serializer: metadata alphabetical, content last so
6
+ * the indexer reads all metadata from the first 4KB, 4KB position enforced.
7
+ *
8
+ * Lives apart from vxjson.ts on purpose: that module's ETag half imports
9
+ * node crypto and WASM-backed xxhash at module level. This file imports only
10
+ * sort-keys and types, so code that must stay free of node/WASM imports can
11
+ * consume it via the dedicated `@conloca/content-api/vxjson-serialize`
12
+ * subpath.
13
+ */
14
+ export function serializeVxjson(data: VXJSONFile): string {
15
+ // Extract content to add it last
16
+ const { content, ...metadataFields } = data;
17
+
18
+ // Ensure content field exists (required for VXJSON format)
19
+ if (content === undefined || content === null) {
20
+ throw new Error('VXJSON format requires a "content" field');
21
+ }
22
+
23
+ // Sort all metadata fields alphabetically and serialize with tab indentation.
24
+ // The replacer drops empty-string FIELDS, so clearing a value removes the key
25
+ // rather than storing '' — deliberate, and covered by vxjson-empty-strings.
26
+ // Array ELEMENTS are exempt: a replacer returning undefined drops an object
27
+ // key but emits null inside an array, so ['a','','b'] used to reach disk as
28
+ // ['a',null,'b'] and crash whatever walked the list later.
29
+ const sortedMetadata = sortKeys(metadataFields, { deep: true });
30
+ const metadataJson = JSON.stringify(
31
+ sortedMetadata,
32
+ function (this: unknown, _key, value) {
33
+ if (value === '' && !Array.isArray(this)) {
34
+ return undefined;
35
+ }
36
+ return value;
37
+ },
38
+ '\t',
39
+ );
40
+
41
+ // Serialize content wrapped in an object to get proper indentation
42
+ const wrappedContent = JSON.stringify({ c: sortKeys(content, { deep: true }) }, null, '\t');
43
+ // Extract just the content part, removing the wrapper
44
+ // This transforms: '{\n "c": {\n "puckData": ...\n }\n}'
45
+ // Into: '{\n "puckData": ...\n }'
46
+ const contentJson = wrappedContent.slice(wrappedContent.indexOf(': ') + 2, -2);
47
+
48
+ // Remove the closing } and newline from metadata
49
+ const metadataWithoutClosing = metadataJson.slice(0, -2); // Remove \n}
50
+ const contentFieldPrefix = ',\n\t"content": ';
51
+ const contentKeyPosition = metadataWithoutClosing.length + 1 + '\n\t'.length; // Position of "content":
52
+
53
+ // VXJSON constraint: "content": must appear within first 4KB
54
+ const maxContentKeyPosition = 4096 - '"content":'.length; // 4086
55
+ if (contentKeyPosition > maxContentKeyPosition) {
56
+ throw new Error(
57
+ 'VXJSON format violation: metadata too large. ' +
58
+ `"content" field would start at position ${contentKeyPosition}, ` +
59
+ `but must start within ${maxContentKeyPosition} bytes. ` +
60
+ `Reduce metadata size by ${contentKeyPosition - maxContentKeyPosition} bytes.`,
61
+ );
62
+ }
63
+
64
+ // Build final JSON string efficiently
65
+ return `${metadataWithoutClosing}${contentFieldPrefix}${contentJson}\n}`;
66
+ }