@conloca/content-api 0.1.16 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/asset-constraints.d.ts +16 -0
- package/dist/asset-constraints.d.ts.map +1 -0
- package/dist/asset-constraints.js +29 -0
- package/dist/asset-constraints.js.map +1 -0
- package/dist/asset-metadata-store.d.ts +60 -0
- package/dist/asset-metadata-store.d.ts.map +1 -0
- package/dist/asset-metadata-store.js +335 -0
- package/dist/asset-metadata-store.js.map +1 -0
- package/dist/asset-operations.d.ts +21 -28
- package/dist/asset-operations.d.ts.map +1 -1
- package/dist/asset-operations.js +268 -305
- package/dist/asset-operations.js.map +1 -0
- package/dist/asset-types.d.ts +23 -7
- package/dist/asset-types.d.ts.map +1 -1
- package/dist/asset-types.js +8 -2
- package/dist/asset-types.js.map +1 -0
- package/dist/asset-usage.d.ts +11 -0
- package/dist/asset-usage.d.ts.map +1 -0
- package/dist/asset-usage.js +50 -0
- package/dist/asset-usage.js.map +1 -0
- package/dist/base-content-index.d.ts +8 -2
- package/dist/base-content-index.d.ts.map +1 -1
- package/dist/base-content-index.js +24 -3
- package/dist/base-content-index.js.map +1 -0
- package/dist/cf-access.js +1 -0
- package/dist/cf-access.js.map +1 -0
- package/dist/component-registry.d.ts +1 -1
- package/dist/component-registry.d.ts.map +1 -1
- package/dist/component-registry.js +2 -1
- package/dist/component-registry.js.map +1 -0
- package/dist/component-registry.types.js +1 -0
- package/dist/component-registry.types.js.map +1 -0
- package/dist/component-vocabulary.d.ts +33 -0
- package/dist/component-vocabulary.d.ts.map +1 -0
- package/dist/component-vocabulary.js +105 -0
- package/dist/component-vocabulary.js.map +1 -0
- package/dist/content-api.interface.d.ts +5 -5
- package/dist/content-api.interface.d.ts.map +1 -1
- package/dist/content-api.interface.js +1 -0
- package/dist/content-api.interface.js.map +1 -0
- package/dist/content-filters.d.ts +2 -2
- package/dist/content-filters.d.ts.map +1 -1
- package/dist/content-filters.js +9 -8
- package/dist/content-filters.js.map +1 -0
- package/dist/content-index.d.ts +74 -16
- package/dist/content-index.d.ts.map +1 -1
- package/dist/content-index.js +183 -55
- package/dist/content-index.js.map +1 -0
- package/dist/content-operations.d.ts +71 -7
- package/dist/content-operations.d.ts.map +1 -1
- package/dist/content-operations.js +166 -82
- package/dist/content-operations.js.map +1 -0
- package/dist/content-path.d.ts +64 -0
- package/dist/content-path.d.ts.map +1 -0
- package/dist/content-path.js +95 -0
- package/dist/content-path.js.map +1 -0
- package/dist/content-reader.d.ts +2 -2
- package/dist/content-reader.d.ts.map +1 -1
- package/dist/content-reader.js +2 -1
- package/dist/content-reader.js.map +1 -0
- package/dist/content-utils.d.ts +53 -10
- package/dist/content-utils.d.ts.map +1 -1
- package/dist/content-utils.js +103 -34
- package/dist/content-utils.js.map +1 -0
- package/dist/content-watcher.d.ts +3 -3
- package/dist/content-watcher.d.ts.map +1 -1
- package/dist/content-watcher.js +13 -10
- package/dist/content-watcher.js.map +1 -0
- package/dist/data-index.d.ts +1 -1
- package/dist/data-index.d.ts.map +1 -1
- package/dist/data-index.js +2 -1
- package/dist/data-index.js.map +1 -0
- package/dist/data.d.ts +5 -13
- package/dist/data.d.ts.map +1 -1
- package/dist/data.js +7 -6
- package/dist/data.js.map +1 -0
- package/dist/editor-data-bindings.d.ts +10 -0
- package/dist/editor-data-bindings.d.ts.map +1 -0
- package/dist/editor-data-bindings.js +67 -0
- package/dist/editor-data-bindings.js.map +1 -0
- package/dist/etag-utils.d.ts.map +1 -1
- package/dist/etag-utils.js +5 -4
- package/dist/etag-utils.js.map +1 -0
- package/dist/filesystem-content-api.d.ts +45 -18
- package/dist/filesystem-content-api.d.ts.map +1 -1
- package/dist/filesystem-content-api.js +455 -286
- package/dist/filesystem-content-api.js.map +1 -0
- package/dist/filesystem-reader-backend.d.ts +46 -0
- package/dist/filesystem-reader-backend.d.ts.map +1 -0
- package/dist/filesystem-reader-backend.js +119 -0
- package/dist/filesystem-reader-backend.js.map +1 -0
- package/dist/fragment-index.d.ts +8 -0
- package/dist/fragment-index.d.ts.map +1 -0
- package/dist/fragment-index.js +8 -0
- package/dist/fragment-index.js.map +1 -0
- package/dist/fragments.d.ts +16 -0
- package/dist/fragments.d.ts.map +1 -0
- package/dist/fragments.js +104 -0
- package/dist/fragments.js.map +1 -0
- package/dist/git-operations.d.ts.map +1 -1
- package/dist/git-operations.js +5 -1
- package/dist/git-operations.js.map +1 -0
- package/dist/header-parser.d.ts +4 -4
- package/dist/header-parser.d.ts.map +1 -1
- package/dist/header-parser.js +8 -61
- package/dist/header-parser.js.map +1 -0
- package/dist/in-memory-content-api.d.ts +7 -7
- package/dist/in-memory-content-api.d.ts.map +1 -1
- package/dist/in-memory-content-api.js +74 -56
- package/dist/in-memory-content-api.js.map +1 -0
- package/dist/index.d.ts +19 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -9
- package/dist/index.js.map +1 -0
- package/dist/indexing.d.ts +4 -4
- package/dist/indexing.d.ts.map +1 -1
- package/dist/indexing.js +6 -5
- package/dist/indexing.js.map +1 -0
- package/dist/json-whitespace-view.js +1 -0
- package/dist/json-whitespace-view.js.map +1 -0
- package/dist/middleware.d.ts +8 -6
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +178 -142
- package/dist/middleware.js.map +1 -0
- package/dist/node.d.ts +24 -17
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +17 -14
- package/dist/node.js.map +1 -0
- package/dist/page-references.d.ts +21 -0
- package/dist/page-references.d.ts.map +1 -0
- package/dist/page-references.js +50 -0
- package/dist/page-references.js.map +1 -0
- package/dist/read-date.d.ts +3 -0
- package/dist/read-date.d.ts.map +1 -0
- package/dist/read-date.js +9 -0
- package/dist/read-date.js.map +1 -0
- package/dist/reader-backend.d.ts +54 -0
- package/dist/reader-backend.d.ts.map +1 -0
- package/dist/reader-backend.js +2 -0
- package/dist/reader-backend.js.map +1 -0
- package/dist/reader.d.ts +8 -8
- package/dist/reader.d.ts.map +1 -1
- package/dist/reader.js +8 -7
- package/dist/reader.js.map +1 -0
- package/dist/schemas/index.d.ts +6 -6
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +9 -8
- package/dist/schemas/index.js.map +1 -0
- package/dist/site-index.d.ts +3 -3
- package/dist/site-index.d.ts.map +1 -1
- package/dist/site-index.js +9 -5
- package/dist/site-index.js.map +1 -0
- package/dist/site.d.ts +5 -3
- package/dist/site.d.ts.map +1 -1
- package/dist/site.js +14 -2
- package/dist/site.js.map +1 -0
- package/dist/types.d.ts +203 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/url-safe-base64.js +1 -0
- package/dist/url-safe-base64.js.map +1 -0
- package/dist/url-validation.d.ts +14 -0
- package/dist/url-validation.d.ts.map +1 -1
- package/dist/url-validation.js +56 -14
- package/dist/url-validation.js.map +1 -0
- package/dist/utils/atomic-write.js +1 -0
- package/dist/utils/atomic-write.js.map +1 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +2 -1
- package/dist/utils.js.map +1 -0
- package/dist/vite-plugin.d.ts +2 -2
- package/dist/vite-plugin.js +5 -4
- package/dist/vite-plugin.js.map +1 -0
- package/dist/vxjson-serialize.d.ts +5 -6
- package/dist/vxjson-serialize.d.ts.map +1 -1
- package/dist/vxjson-serialize.js +13 -10
- package/dist/vxjson-serialize.js.map +1 -0
- package/dist/vxjson.d.ts +4 -3
- package/dist/vxjson.d.ts.map +1 -1
- package/dist/vxjson.js +10 -20
- package/dist/vxjson.js.map +1 -0
- package/dist/xxhash.d.ts.map +1 -1
- package/dist/xxhash.js +6 -2
- package/dist/xxhash.js.map +1 -0
- package/package.json +92 -56
- package/src/asset-constraints.ts +33 -0
- package/src/asset-metadata-store.ts +401 -0
- package/src/asset-operations.ts +814 -0
- package/src/asset-types.ts +74 -0
- package/src/asset-usage.ts +48 -0
- package/src/base-content-index.ts +227 -0
- package/src/cf-access.ts +85 -0
- package/src/component-registry.ts +145 -0
- package/src/component-registry.types.ts +12 -0
- package/src/component-vocabulary.ts +87 -0
- package/src/content-api.interface.ts +94 -0
- package/src/content-filters.ts +278 -0
- package/src/content-index.ts +642 -0
- package/src/content-operations.ts +497 -0
- package/src/content-path.ts +121 -0
- package/src/content-reader.ts +18 -0
- package/src/content-utils.ts +474 -0
- package/src/content-watcher.ts +122 -0
- package/src/data-index.ts +7 -0
- package/src/data.ts +134 -0
- package/src/editor-data-bindings.ts +66 -0
- package/src/etag-utils.ts +204 -0
- package/src/filesystem-content-api.ts +2740 -0
- package/src/filesystem-reader-backend.ts +135 -0
- package/src/fragment-index.ts +7 -0
- package/src/fragments.ts +113 -0
- package/src/git-operations.ts +340 -0
- package/src/header-parser.ts +272 -0
- package/src/in-memory-content-api.ts +760 -0
- package/src/index.ts +45 -0
- package/src/indexing.ts +8 -0
- package/src/json-whitespace-view.ts +155 -0
- package/src/middleware.ts +1322 -0
- package/src/node.ts +40 -0
- package/src/page-references.ts +76 -0
- package/src/read-date.ts +7 -0
- package/src/reader-backend.ts +54 -0
- package/src/reader.ts +15 -0
- package/src/schemas/index.ts +113 -0
- package/src/site-index.ts +330 -0
- package/src/site.ts +198 -0
- package/src/types.ts +759 -0
- package/src/url-safe-base64.ts +38 -0
- package/src/url-validation.ts +121 -0
- package/src/utils/atomic-write.ts +20 -0
- package/src/utils.ts +84 -0
- package/src/vite-plugin.ts +45 -0
- package/src/vxjson-serialize.ts +66 -0
- package/src/vxjson.ts +442 -0
- package/src/xxhash.ts +43 -0
- package/dist/asset-manifest.d.ts +0 -23
- package/dist/asset-manifest.d.ts.map +0 -1
- package/dist/asset-manifest.js +0 -73
- package/dist/block-index.d.ts +0 -9
- package/dist/block-index.d.ts.map +0 -1
- package/dist/block-index.js +0 -11
- package/dist/blocks.d.ts +0 -16
- package/dist/blocks.d.ts.map +0 -1
- package/dist/blocks.js +0 -103
- package/dist/component-registry.types-C0ctF9nh.d.mts +0 -71
- package/dist/component-registry.types-C0ctF9nh.d.mts.map +0 -1
- package/dist/content-index-D092_vk1.mjs +0 -629
- package/dist/content-index-D092_vk1.mjs.map +0 -1
- package/dist/content-index-D6IVMmAB.d.mts +0 -127
- package/dist/content-index-D6IVMmAB.d.mts.map +0 -1
- package/dist/content-operations-DIbsxG5A.d.mts +0 -354
- package/dist/content-operations-DIbsxG5A.d.mts.map +0 -1
- package/dist/content-utils-fWNHTrPy.mjs +0 -280
- package/dist/content-utils-fWNHTrPy.mjs.map +0 -1
- package/dist/data-DquNIEnQ.mjs +0 -62
- package/dist/data-DquNIEnQ.mjs.map +0 -1
- package/dist/filesystem-content-api-B1KIQJfk.mjs +0 -2
- package/dist/filesystem-content-api-C0HIhqZi.mjs +0 -1620
- package/dist/filesystem-content-api-C0HIhqZi.mjs.map +0 -1
- package/dist/filesystem-content-api-DCdNi-co.d.mts +0 -134
- package/dist/filesystem-content-api-DCdNi-co.d.mts.map +0 -1
- package/dist/index-DxVTFK66.d.mts +0 -105
- package/dist/index-DxVTFK66.d.mts.map +0 -1
- package/dist/index.d.mts +0 -80
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -78
- package/dist/index.mjs.map +0 -1
- package/dist/indexing.d.mts +0 -152
- package/dist/indexing.d.mts.map +0 -1
- package/dist/indexing.mjs +0 -255
- package/dist/indexing.mjs.map +0 -1
- package/dist/node.d.mts +0 -373
- package/dist/node.d.mts.map +0 -1
- package/dist/node.mjs +0 -2572
- package/dist/node.mjs.map +0 -1
- package/dist/reader.d.mts +0 -15
- package/dist/reader.d.mts.map +0 -1
- package/dist/reader.mjs +0 -22
- package/dist/reader.mjs.map +0 -1
- package/dist/schemas/index.d.mts +0 -2
- package/dist/schemas/index.mjs +0 -2
- package/dist/schemas-CFRD0FIQ.mjs +0 -84
- package/dist/schemas-CFRD0FIQ.mjs.map +0 -1
- package/dist/site-8oaj1Q60.mjs +0 -366
- package/dist/site-8oaj1Q60.mjs.map +0 -1
- package/dist/site-index-B_254bCc.d.mts +0 -136
- package/dist/site-index-B_254bCc.d.mts.map +0 -1
- package/dist/types-DOdldbfD.mjs +0 -34
- package/dist/types-DOdldbfD.mjs.map +0 -1
- package/dist/types-DjfhS272.d.mts +0 -456
- package/dist/types-DjfhS272.d.mts.map +0 -1
- package/dist/vxjson-qHrgLGip.mjs +0 -555
- package/dist/vxjson-qHrgLGip.mjs.map +0 -1
- package/dist/vxjson-serialize-geF6qvpn.mjs +0 -34
- package/dist/vxjson-serialize-geF6qvpn.mjs.map +0 -1
- package/dist/vxjson-serialize.d.mts +0 -17
- package/dist/vxjson-serialize.d.mts.map +0 -1
- package/dist/vxjson-serialize.mjs +0 -2
|
@@ -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
|
+
}
|