|
| 1 | +import { getDefaultSession } from "@inrupt/solid-client-authn-browser"; |
| 2 | +import { Parser, Store, NamedNode } from "n3"; |
| 3 | + |
| 4 | +// Cache for parsed profile documents |
| 5 | +const profileCache = new Map<string, { store: Store; baseUrl: string; mainSubject: NamedNode }>(); |
| 6 | + |
| 7 | +/** |
| 8 | + * Fetches and parses the WebID profile document, with caching to avoid duplicate fetches |
| 9 | + * @param webId - The WebID to fetch |
| 10 | + * @returns The parsed RDF store, base URL, and main subject |
| 11 | + */ |
| 12 | +export async function fetchAndParseProfile( |
| 13 | + webId: string |
| 14 | +): Promise<{ store: Store; baseUrl: string; mainSubject: NamedNode }> { |
| 15 | + // Check cache first |
| 16 | + if (profileCache.has(webId)) { |
| 17 | + return profileCache.get(webId)!; |
| 18 | + } |
| 19 | + |
| 20 | + const session = getDefaultSession(); |
| 21 | + const fetchFn = session.fetch || fetch; |
| 22 | + |
| 23 | + // Try different Accept headers to get the profile |
| 24 | + const acceptHeaders = [ |
| 25 | + 'text/turtle, application/turtle, text/n3, application/n3', |
| 26 | + 'text/turtle', |
| 27 | + 'application/ld+json', |
| 28 | + ]; |
| 29 | + |
| 30 | + let content: string | null = null; |
| 31 | + let contentType: string = ''; |
| 32 | + |
| 33 | + for (const acceptHeader of acceptHeaders) { |
| 34 | + try { |
| 35 | + const response = await fetchFn(webId, { |
| 36 | + method: 'GET', |
| 37 | + headers: { |
| 38 | + 'Accept': acceptHeader, |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + if (response.ok) { |
| 43 | + contentType = response.headers.get('content-type') || ''; |
| 44 | + content = await response.text(); |
| 45 | + break; |
| 46 | + } |
| 47 | + } catch (err) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (!content) { |
| 53 | + throw new Error("Failed to fetch profile document with any Accept header"); |
| 54 | + } |
| 55 | + |
| 56 | + // Parse the RDF content |
| 57 | + const store = new Store(); |
| 58 | + const baseUrl = webId.split('#')[0]; |
| 59 | + |
| 60 | + if (contentType.includes('text/turtle') || contentType.includes('application/turtle') || |
| 61 | + contentType.includes('text/n3') || contentType.includes('application/n3')) { |
| 62 | + const parser = new Parser({ baseIRI: baseUrl }); |
| 63 | + const quads = parser.parse(content); |
| 64 | + store.addQuads(quads); |
| 65 | + } else if (contentType.includes('application/ld+json')) { |
| 66 | + // Try parsing as Turtle anyway (most servers return Turtle even if JSON-LD is requested) |
| 67 | + try { |
| 68 | + const parser = new Parser({ baseIRI: baseUrl }); |
| 69 | + const quads = parser.parse(content); |
| 70 | + store.addQuads(quads); |
| 71 | + } catch (e) { |
| 72 | + // Silent error handling |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Find the main subject - try different variants |
| 77 | + const FOAF_NAME = "http://xmlns.com/foaf/0.1/name"; |
| 78 | + const subjectVariants = [ |
| 79 | + new NamedNode(webId), |
| 80 | + new NamedNode(baseUrl + '#me'), |
| 81 | + new NamedNode('#me'), |
| 82 | + new NamedNode(baseUrl + '#card'), |
| 83 | + ]; |
| 84 | + |
| 85 | + let mainSubject: NamedNode | null = null; |
| 86 | + |
| 87 | + for (const subject of subjectVariants) { |
| 88 | + const nameQuads = store.getQuads(subject, new NamedNode(FOAF_NAME), null, null); |
| 89 | + if (nameQuads.length > 0) { |
| 90 | + mainSubject = subject; |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // If still not found, try to find Person type |
| 96 | + if (!mainSubject) { |
| 97 | + const personType = new NamedNode('http://xmlns.com/foaf/0.1/Person'); |
| 98 | + const personQuads = store.getQuads(null, new NamedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), personType, null); |
| 99 | + if (personQuads.length > 0 && personQuads[0].subject.termType === 'NamedNode') { |
| 100 | + mainSubject = personQuads[0].subject as NamedNode; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Fallback to WebID itself |
| 105 | + if (!mainSubject) { |
| 106 | + mainSubject = new NamedNode(webId); |
| 107 | + } |
| 108 | + |
| 109 | + // Cache the result |
| 110 | + const result = { store, baseUrl, mainSubject }; |
| 111 | + profileCache.set(webId, result); |
| 112 | + |
| 113 | + return result; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Clears the profile cache (useful for testing or when profile might have changed) |
| 118 | + */ |
| 119 | +export function clearProfileCache(webId?: string): void { |
| 120 | + if (webId) { |
| 121 | + profileCache.delete(webId); |
| 122 | + } else { |
| 123 | + profileCache.clear(); |
| 124 | + } |
| 125 | +} |
| 126 | + |
0 commit comments