parse-frontmatter-json.mjs
55 lines 1.5 KB
Raw
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd feat(calendar): enforce agent context tiers in retrieval AP… Human minor ⚠ breaking 1 day ago
1 /**
2 * Parse hosted canister `frontmatter` wire values (JSON object text, sometimes JSON-string-wrapped).
3 * Must tolerate BOM, quoted JSON blobs, and multi-layer string encoding from the ICP/gateway chain.
4 * @param {unknown} t
5 * @returns {Record<string, unknown>}
6 */
7 export function parseFrontmatterJsonText(t) {
8 let cur = String(t ?? '')
9 .replace(/^\uFEFF/, '')
10 .trim();
11 if (!cur) return {};
12 for (let i = 0; i < 8; i++) {
13 try {
14 const o = JSON.parse(cur);
15 if (o !== null && typeof o === 'object' && !Array.isArray(o)) {
16 return /** @type {Record<string, unknown>} */ (o);
17 }
18 if (typeof o === 'string') {
19 const next = o.trim();
20 if (next === cur) return {};
21 cur = next;
22 continue;
23 }
24 return {};
25 } catch {
26 // Value may be a JSON *string literal* whose contents are more JSON text.
27 if (cur.length >= 2 && cur.charCodeAt(0) === 34) {
28 try {
29 const inner = JSON.parse(cur);
30 if (typeof inner === 'string') {
31 cur = inner.trim();
32 continue;
33 }
34 } catch {
35 /* fall through */
36 }
37 }
38 return {};
39 }
40 }
41 return {};
42 }
43
44 /**
45 * @param {unknown} raw
46 * @returns {Record<string, unknown>}
47 */
48 export function materializeWireFrontmatter(raw) {
49 if (raw == null) return {};
50 if (typeof raw === 'object' && !Array.isArray(raw)) {
51 return /** @type {Record<string, unknown>} */ (raw);
52 }
53 if (typeof raw === 'string') return parseFrontmatterJsonText(raw);
54 return {};
55 }
File History 2 commits
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd feat(calendar): enforce agent context tiers in retrieval AP… Human minor 1 day ago
sha256:9103f98c89257ed2b01c237cea895dabb3e85ea337dccb1161c175e4422355b6 docs: accept Calendar Events v0 spec with Phase 0 security … Human 1 day ago