-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathresourceLoader.ts
More file actions
59 lines (51 loc) · 2.11 KB
/
Copy pathresourceLoader.ts
File metadata and controls
59 lines (51 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { ns } from 'solid-ui'
import { log } from './debug'
import { EditorMetadata } from './types'
import { LiveStore, NamedNode } from 'rdflib'
import { happy } from './helpers'
export function getResponseMetadata (store: LiveStore, subject: NamedNode, response: Response): EditorMetadata {
let contentType: string | undefined
let eTag: string | undefined
if (response.headers && response.headers.get('content-type')) {
contentType = response.headers.get('content-type')?.split(';')[0] ?? undefined // Should work but headers may be empty
eTag = response.headers.get('etag') ?? undefined
} else {
const reqs = store.each(
null,
store.sym('http://www.w3.org/2007/ont/link#requestedURI'),
subject
)
reqs.forEach((req: any) => {
const responseNode = store.any(
req as any,
store.sym('http://www.w3.org/2007/ont/link#response')
)
if (responseNode && responseNode.termType === 'NamedNode') {
contentType = store.anyValue(responseNode as any, ns.httph('content-type')) || undefined
eTag = store.anyValue(responseNode as any, ns.httph('etag')) || undefined
if (!eTag) log('sourcePane: No eTag on GET')
}
})
}
return { contentType, eTag } as EditorMetadata
}
export async function fetchContentAndMetadata(store: LiveStore, subject: NamedNode): Promise<{ content: string, metadata: EditorMetadata }> {
const fetcher = store.fetcher
try {
const response = await fetcher.webOperation('GET', subject.uri)
if (!happy(response, 'GET')) {
throw new Error('GET request failed')
}
const content = (response as Response & { responseText?: string }).responseText
if (content === undefined) { // Defensive https://github.com/linkeddata/rdflib.js/issues/506
throw new Error('source pane: No text in response object!!')
}
const metadata = getResponseMetadata(store, subject, response)
if (!metadata.contentType) {
throw new Error('Error: No content-type available!')
}
return { content, metadata }
} catch (error: any) {
throw new Error(`Error reading file: ${error.message}`)
}
}