@noeldemartin/solid-utils 0.1.1-next.9e4a01d16d6b3e9e29a5546b75e8b5d0d8a452d6 → 0.1.1-next.e69e8c7806f3429b47d509ae8c4c0b85e96126e1
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/noeldemartin-solid-utils.cjs.js +1 -1
- package/dist/noeldemartin-solid-utils.cjs.js.map +1 -1
- package/dist/noeldemartin-solid-utils.d.ts +39 -9
- package/dist/noeldemartin-solid-utils.esm.js +1 -1
- package/dist/noeldemartin-solid-utils.esm.js.map +1 -1
- package/package.json +2 -2
- package/src/errors/NetworkRequestError.ts +3 -2
- package/src/helpers/auth.ts +64 -10
- package/src/helpers/identifiers.ts +56 -0
- package/src/helpers/index.ts +2 -0
- package/src/helpers/interop.ts +3 -3
- package/src/helpers/io.ts +61 -29
- package/src/helpers/testing.ts +23 -24
- package/src/helpers/vocabs.ts +4 -2
- package/src/helpers/wac.ts +45 -0
- package/src/models/SolidDocument.ts +7 -37
- package/src/models/SolidStore.ts +61 -0
- package/src/models/index.ts +1 -0
- package/src/plugins/jest/matchers.ts +5 -7
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { arr, isArray, isObject, objectDeepClone, objectWithoutEmpty, tap, urlParse, uuid } from '@noeldemartin/utils';
|
|
2
|
+
import type { UrlParts } from '@noeldemartin/utils';
|
|
3
|
+
import type { JsonLD, JsonLDResource } from '@/helpers';
|
|
4
|
+
|
|
5
|
+
export interface SubjectParts {
|
|
6
|
+
containerUrl?: string;
|
|
7
|
+
documentName?: string;
|
|
8
|
+
resourceHash?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function getContainerPath(parts: UrlParts): string | null {
|
|
12
|
+
if (!parts.path || !parts.path.startsWith('/'))
|
|
13
|
+
return null;
|
|
14
|
+
|
|
15
|
+
if (parts.path.match(/^\/[^/]*$/))
|
|
16
|
+
return '/';
|
|
17
|
+
|
|
18
|
+
return `/${arr(parts.path.split('/')).filter().slice(0, -1).join('/')}/`.replace('//', '/');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function getContainerUrl(parts: UrlParts): string | null {
|
|
22
|
+
const containerPath = getContainerPath(parts);
|
|
23
|
+
|
|
24
|
+
return parts.protocol && parts.domain
|
|
25
|
+
? `${parts.protocol}://${parts.domain}${containerPath ?? '/'}`
|
|
26
|
+
: containerPath;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function __mintJsonLDIdentifiers(jsonld: JsonLD): void {
|
|
30
|
+
if (!('@type' in jsonld) || '@value' in jsonld)
|
|
31
|
+
return;
|
|
32
|
+
|
|
33
|
+
jsonld['@id'] = jsonld['@id'] ?? uuid();
|
|
34
|
+
|
|
35
|
+
for (const propertyValue of Object.values(jsonld)) {
|
|
36
|
+
if (isObject(propertyValue))
|
|
37
|
+
__mintJsonLDIdentifiers(propertyValue);
|
|
38
|
+
|
|
39
|
+
if (isArray(propertyValue))
|
|
40
|
+
propertyValue.forEach(value => isObject(value) && __mintJsonLDIdentifiers(value));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function mintJsonLDIdentifiers(jsonld: JsonLD): JsonLDResource {
|
|
45
|
+
return tap(objectDeepClone(jsonld) as JsonLDResource, clone => __mintJsonLDIdentifiers(clone));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function parseResourceSubject(subject: string): SubjectParts {
|
|
49
|
+
const parts = urlParse(subject);
|
|
50
|
+
|
|
51
|
+
return !parts ? {} : objectWithoutEmpty({
|
|
52
|
+
containerUrl: getContainerUrl(parts),
|
|
53
|
+
documentName: parts.path ? parts.path.split('/').pop() : null,
|
|
54
|
+
resourceHash: parts.fragment,
|
|
55
|
+
});
|
|
56
|
+
}
|
package/src/helpers/index.ts
CHANGED
package/src/helpers/interop.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type { Fetch } from '@/helpers/io';
|
|
|
6
6
|
import type { SolidUserProfile } from '@/helpers/auth';
|
|
7
7
|
|
|
8
8
|
async function mintPrivateTypeIndexUrl(user: SolidUserProfile, fetch?: Fetch): Promise<string> {
|
|
9
|
-
fetch = fetch ?? window.fetch;
|
|
9
|
+
fetch = fetch ?? window.fetch.bind(fetch);
|
|
10
10
|
|
|
11
11
|
const storageUrl = user.storageUrls[0];
|
|
12
12
|
const typeIndexUrl = `${storageUrl}settings/privateTypeIndex`;
|
|
@@ -17,7 +17,7 @@ async function mintPrivateTypeIndexUrl(user: SolidUserProfile, fetch?: Fetch): P
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export async function createPrivateTypeIndex(user: SolidUserProfile, fetch?: Fetch): Promise<string> {
|
|
20
|
-
fetch = fetch ?? window.fetch;
|
|
20
|
+
fetch = fetch ?? window.fetch.bind(fetch);
|
|
21
21
|
|
|
22
22
|
const typeIndexUrl = await mintPrivateTypeIndexUrl(user, fetch);
|
|
23
23
|
const typeIndexBody = `
|
|
@@ -46,7 +46,7 @@ export async function findContainerRegistration(
|
|
|
46
46
|
): Promise<SolidThing | null> {
|
|
47
47
|
const typeIndex = await fetchSolidDocument(typeIndexUrl, fetch);
|
|
48
48
|
const containerQuad = typeIndex
|
|
49
|
-
.statements(undefined, '
|
|
49
|
+
.statements(undefined, 'rdf:type', 'solid:TypeRegistration')
|
|
50
50
|
.find(
|
|
51
51
|
statement =>
|
|
52
52
|
typeIndex.contains(statement.subject.value, 'solid:forClass', childrenType) &&
|
package/src/helpers/io.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import md5 from 'md5';
|
|
2
|
+
import { arr, arrayFilter, arrayReplace, objectWithoutEmpty, stringMatchAll, tap } from '@noeldemartin/utils';
|
|
2
3
|
import { BlankNode as N3BlankNode, Quad as N3Quad, Parser as TurtleParser, Writer as TurtleWriter } from 'n3';
|
|
4
|
+
import { fromRDF, toRDF } from 'jsonld';
|
|
3
5
|
import type { JsonLdDocument } from 'jsonld';
|
|
4
|
-
import { toRDF } from 'jsonld';
|
|
5
|
-
import md5 from 'md5';
|
|
6
6
|
import type { Quad } from 'rdf-js';
|
|
7
7
|
import type { Term as N3Term } from 'n3';
|
|
8
8
|
|
|
@@ -13,10 +13,10 @@ import NetworkRequestError from '@/errors/NetworkRequestError';
|
|
|
13
13
|
import NotFoundError from '@/errors/NotFoundError';
|
|
14
14
|
import UnauthorizedError from '@/errors/UnauthorizedError';
|
|
15
15
|
import { isJsonLDGraph } from '@/helpers/jsonld';
|
|
16
|
-
import type { JsonLD } from '@/helpers/jsonld';
|
|
16
|
+
import type { JsonLD, JsonLDGraph, JsonLDResource } from '@/helpers/jsonld';
|
|
17
17
|
|
|
18
18
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
-
export declare type AnyFetch = (input: any, options?: any) => Promise<
|
|
19
|
+
export declare type AnyFetch = (input: any, options?: any) => Promise<Response>;
|
|
20
20
|
export declare type TypedFetch = (input: RequestInfo, options?: RequestInit) => Promise<Response>;
|
|
21
21
|
export declare type Fetch = TypedFetch | AnyFetch;
|
|
22
22
|
|
|
@@ -44,7 +44,10 @@ async function fetchRawSolidDocument(url: string, fetch: Fetch): Promise<{ body:
|
|
|
44
44
|
if (error instanceof UnauthorizedError)
|
|
45
45
|
throw error;
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
if (error instanceof NotFoundError)
|
|
48
|
+
throw error;
|
|
49
|
+
|
|
50
|
+
throw new NetworkRequestError(url, { cause: error });
|
|
48
51
|
}
|
|
49
52
|
}
|
|
50
53
|
|
|
@@ -52,26 +55,23 @@ function normalizeBlankNodes(quads: Quad[]): Quad[] {
|
|
|
52
55
|
const normalizedQuads = quads.slice(0);
|
|
53
56
|
const quadsIndexes: Record<string, Set<number>> = {};
|
|
54
57
|
const blankNodeIds = arr(quads)
|
|
55
|
-
.flatMap(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return ids;
|
|
67
|
-
})
|
|
58
|
+
.flatMap(
|
|
59
|
+
(quad, index) => tap(
|
|
60
|
+
arrayFilter([
|
|
61
|
+
quad.object.termType === 'BlankNode' ? quad.object.value : null,
|
|
62
|
+
quad.subject.termType === 'BlankNode' ? quad.subject.value : null,
|
|
63
|
+
]),
|
|
64
|
+
ids => ids.forEach(id => (quadsIndexes[id] ??= new Set()).add(index)),
|
|
65
|
+
),
|
|
66
|
+
)
|
|
68
67
|
.filter()
|
|
69
68
|
.unique();
|
|
70
69
|
|
|
71
70
|
for (const originalId of blankNodeIds) {
|
|
71
|
+
const quadIndexes = quadsIndexes[originalId] as Set<number>;
|
|
72
72
|
const normalizedId = md5(
|
|
73
|
-
arr(
|
|
74
|
-
.map(index => quads[index])
|
|
73
|
+
arr(quadIndexes)
|
|
74
|
+
.map(index => quads[index] as Quad)
|
|
75
75
|
.filter(({ subject: { termType, value } }) => termType === 'BlankNode' && value === originalId)
|
|
76
76
|
.map(
|
|
77
77
|
({ predicate, object }) => object.termType === 'BlankNode'
|
|
@@ -82,9 +82,12 @@ function normalizeBlankNodes(quads: Quad[]): Quad[] {
|
|
|
82
82
|
.join(),
|
|
83
83
|
);
|
|
84
84
|
|
|
85
|
-
for (const index of
|
|
86
|
-
const quad = normalizedQuads[index];
|
|
87
|
-
const terms: Record<string, N3Term> = {
|
|
85
|
+
for (const index of quadIndexes) {
|
|
86
|
+
const quad = normalizedQuads[index] as Quad;
|
|
87
|
+
const terms: Record<string, N3Term> = {
|
|
88
|
+
subject: quad.subject as N3Term,
|
|
89
|
+
object: quad.object as N3Term,
|
|
90
|
+
};
|
|
88
91
|
|
|
89
92
|
for (const [termName, termValue] of Object.entries(terms)) {
|
|
90
93
|
if (termValue.termType !== 'BlankNode' || termValue.value !== originalId)
|
|
@@ -93,7 +96,15 @@ function normalizeBlankNodes(quads: Quad[]): Quad[] {
|
|
|
93
96
|
terms[termName] = new N3BlankNode(normalizedId);
|
|
94
97
|
}
|
|
95
98
|
|
|
96
|
-
arrayReplace(
|
|
99
|
+
arrayReplace(
|
|
100
|
+
normalizedQuads,
|
|
101
|
+
quad,
|
|
102
|
+
new N3Quad(
|
|
103
|
+
terms.subject as N3Term,
|
|
104
|
+
quad.predicate as N3Term,
|
|
105
|
+
terms.object as N3Term,
|
|
106
|
+
),
|
|
107
|
+
);
|
|
97
108
|
}
|
|
98
109
|
}
|
|
99
110
|
|
|
@@ -106,7 +117,7 @@ export interface ParsingOptions {
|
|
|
106
117
|
}
|
|
107
118
|
|
|
108
119
|
export async function createSolidDocument(url: string, body: string, fetch?: Fetch): Promise<SolidDocument> {
|
|
109
|
-
fetch = fetch ?? window.fetch;
|
|
120
|
+
fetch = fetch ?? window.fetch.bind(window);
|
|
110
121
|
|
|
111
122
|
const statements = await turtleToQuads(body);
|
|
112
123
|
|
|
@@ -126,6 +137,19 @@ export async function fetchSolidDocument(url: string, fetch?: Fetch): Promise<So
|
|
|
126
137
|
return new SolidDocument(url, statements, headers);
|
|
127
138
|
}
|
|
128
139
|
|
|
140
|
+
export async function fetchSolidDocumentIfFound(url: string, fetch?: Fetch): Promise<SolidDocument | null> {
|
|
141
|
+
try {
|
|
142
|
+
const document = await fetchSolidDocument(url, fetch);
|
|
143
|
+
|
|
144
|
+
return document;
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (!(error instanceof NotFoundError))
|
|
147
|
+
throw error;
|
|
148
|
+
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
129
153
|
export async function jsonldToQuads(jsonld: JsonLD): Promise<Quad[]> {
|
|
130
154
|
if (isJsonLDGraph(jsonld)) {
|
|
131
155
|
const graphQuads = await Promise.all(jsonld['@graph'].map(jsonldToQuads));
|
|
@@ -149,6 +173,14 @@ export function normalizeSparql(sparql: string): string {
|
|
|
149
173
|
.join(' ;\n');
|
|
150
174
|
}
|
|
151
175
|
|
|
176
|
+
export async function quadsToJsonLD(quads: Quad[]): Promise<JsonLDGraph> {
|
|
177
|
+
const graph = await fromRDF(quads);
|
|
178
|
+
|
|
179
|
+
return {
|
|
180
|
+
'@graph': graph as JsonLDResource[],
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
152
184
|
export function quadsToTurtle(quads: Quad[]): string {
|
|
153
185
|
const writer = new TurtleWriter;
|
|
154
186
|
|
|
@@ -175,7 +207,7 @@ export async function sparqlToQuads(
|
|
|
175
207
|
sparql: string,
|
|
176
208
|
options: Partial<ParsingOptions> = {},
|
|
177
209
|
): Promise<Record<string, Quad[]>> {
|
|
178
|
-
const operations = sparql
|
|
210
|
+
const operations = stringMatchAll<3>(sparql, /(\w+) DATA {([^}]+)}/g);
|
|
179
211
|
const quads: Record<string, Quad[]> = {};
|
|
180
212
|
|
|
181
213
|
await Promise.all([...operations].map(async operation => {
|
|
@@ -189,7 +221,7 @@ export async function sparqlToQuads(
|
|
|
189
221
|
}
|
|
190
222
|
|
|
191
223
|
export function sparqlToQuadsSync(sparql: string, options: Partial<ParsingOptions> = {}): Record<string, Quad[]> {
|
|
192
|
-
const operations = sparql
|
|
224
|
+
const operations = stringMatchAll<3>(sparql, /(\w+) DATA {([^}]+)}/g);
|
|
193
225
|
const quads: Record<string, Quad[]> = {};
|
|
194
226
|
|
|
195
227
|
for (const operation of operations) {
|
|
@@ -253,7 +285,7 @@ export function turtleToQuadsSync(turtle: string, options: Partial<ParsingOption
|
|
|
253
285
|
}
|
|
254
286
|
|
|
255
287
|
export async function updateSolidDocument(url: string, body: string, fetch?: Fetch): Promise<void> {
|
|
256
|
-
fetch = fetch ?? window.fetch;
|
|
288
|
+
fetch = fetch ?? window.fetch.bind(window);
|
|
257
289
|
|
|
258
290
|
await fetch(url, {
|
|
259
291
|
method: 'PATCH',
|
package/src/helpers/testing.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Error, arrayRemove, pull } from '@noeldemartin/utils';
|
|
1
|
+
import { Error, arrayRemove, pull, stringMatchAll } from '@noeldemartin/utils';
|
|
2
2
|
import type { JsonLD } from '@/helpers/jsonld';
|
|
3
3
|
import type { Quad, Quad_Object } from 'rdf-js';
|
|
4
4
|
|
|
@@ -32,37 +32,36 @@ function containsPatterns(value: string): boolean {
|
|
|
32
32
|
return /\[\[(.*\]\[)?([^\]]+)\]\]/.test(value);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
function
|
|
36
|
-
if (!containsPatterns(expected))
|
|
37
|
-
return expected === actual;
|
|
38
|
-
|
|
35
|
+
function createPatternRegexp(expected: string): RegExp {
|
|
39
36
|
const patternAliases = [];
|
|
37
|
+
const patternMatches = stringMatchAll<4, 1 | 2>(
|
|
38
|
+
expected,
|
|
39
|
+
/\[\[((.*?)\]\[)?([^\]]+)\]\]/g,
|
|
40
|
+
);
|
|
41
|
+
const patterns: string[] = [];
|
|
42
|
+
let expectedRegExp = expected;
|
|
40
43
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const patterns: string[] = [];
|
|
44
|
-
let expectedRegExp = expected;
|
|
45
|
-
|
|
46
|
-
for (const patternMatch of patternMatches) {
|
|
47
|
-
if (patternMatch[2]) {
|
|
48
|
-
patternAliases.push(patternMatch[2]);
|
|
49
|
-
}
|
|
44
|
+
for (const patternMatch of patternMatches) {
|
|
45
|
+
patternMatch[2] && patternAliases.push(patternMatch[2]);
|
|
50
46
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
expectedRegExp = expectedRegExp.replace(patternMatch[0], `%PATTERN${patterns.length - 1}%`);
|
|
54
|
-
}
|
|
47
|
+
patterns.push(patternMatch[3]);
|
|
55
48
|
|
|
56
|
-
expectedRegExp = expectedRegExp.replace(
|
|
49
|
+
expectedRegExp = expectedRegExp.replace(patternMatch[0], `%PATTERN${patterns.length - 1}%`);
|
|
50
|
+
}
|
|
57
51
|
|
|
58
|
-
|
|
59
|
-
expectedRegExp = expectedRegExp.replace(`%PATTERN${patternIndex}%`, builtInPatterns[pattern] ?? pattern);
|
|
60
|
-
}
|
|
52
|
+
expectedRegExp = expectedRegExp.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
|
|
61
53
|
|
|
62
|
-
|
|
54
|
+
for (const [patternIndex, pattern] of Object.entries(patterns)) {
|
|
55
|
+
expectedRegExp = expectedRegExp.replace(`%PATTERN${patternIndex}%`, builtInPatterns[pattern] ?? pattern);
|
|
63
56
|
}
|
|
64
57
|
|
|
65
|
-
return
|
|
58
|
+
return new RegExp(expectedRegExp);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function quadValueEquals(expected: string, actual: string): boolean {
|
|
62
|
+
return containsPatterns(expected)
|
|
63
|
+
? (patternsRegExpsIndex[expected] ??= createPatternRegexp(expected)).test(actual)
|
|
64
|
+
: expected === actual;
|
|
66
65
|
}
|
|
67
66
|
|
|
68
67
|
function quadObjectEquals(expected: Quad_Object, actual: Quad_Object): boolean {
|
package/src/helpers/vocabs.ts
CHANGED
|
@@ -6,10 +6,12 @@ export interface ExpandIRIOptions {
|
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
const knownPrefixes: RDFContext = {
|
|
9
|
+
acl: 'http://www.w3.org/ns/auth/acl#',
|
|
9
10
|
foaf: 'http://xmlns.com/foaf/0.1/',
|
|
10
11
|
pim: 'http://www.w3.org/ns/pim/space#',
|
|
11
12
|
purl: 'http://purl.org/dc/terms/',
|
|
12
|
-
|
|
13
|
+
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
|
|
14
|
+
rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
|
|
13
15
|
schema: 'https://schema.org/',
|
|
14
16
|
solid: 'http://www.w3.org/ns/solid/terms#',
|
|
15
17
|
vcard: 'http://www.w3.org/2006/vcard/ns#',
|
|
@@ -25,7 +27,7 @@ export function expandIRI(iri: string, options: Partial<ExpandIRIOptions> = {}):
|
|
|
25
27
|
|
|
26
28
|
const [prefix, name] = iri.split(':');
|
|
27
29
|
|
|
28
|
-
if (name) {
|
|
30
|
+
if (prefix && name) {
|
|
29
31
|
const expandedPrefix = knownPrefixes[prefix] ?? options.extraContext?.[prefix] ?? null;
|
|
30
32
|
|
|
31
33
|
if (!expandedPrefix)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { objectWithoutEmpty, requireUrlParentDirectory, urlResolve } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import { fetchSolidDocumentIfFound } from '@/helpers/io';
|
|
4
|
+
import type SolidDocument from '@/models/SolidDocument';
|
|
5
|
+
import type { Fetch } from '@/helpers/io';
|
|
6
|
+
|
|
7
|
+
async function fetchACLResourceUrl(resourceUrl: string, fetch: Fetch): Promise<string> {
|
|
8
|
+
fetch = fetch ?? window.fetch.bind(window);
|
|
9
|
+
|
|
10
|
+
const resourceHead = await fetch(resourceUrl, { method: 'HEAD' });
|
|
11
|
+
const linkHeader = resourceHead.headers.get('Link') ?? '';
|
|
12
|
+
const url = linkHeader.match(/<([^>]+)>;\s*rel="acl"/)?.[1] ?? null;
|
|
13
|
+
|
|
14
|
+
if (!url) {
|
|
15
|
+
throw new Error(`Could not find ACL Resource for '${resourceUrl}'`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return urlResolve(requireUrlParentDirectory(resourceUrl), url);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function fetchEffectiveACL(
|
|
22
|
+
resourceUrl: string,
|
|
23
|
+
fetch: Fetch,
|
|
24
|
+
aclResourceUrl?: string | null,
|
|
25
|
+
): Promise<SolidDocument> {
|
|
26
|
+
aclResourceUrl = aclResourceUrl ?? await fetchACLResourceUrl(resourceUrl, fetch);
|
|
27
|
+
|
|
28
|
+
return await fetchSolidDocumentIfFound(aclResourceUrl ?? '', fetch)
|
|
29
|
+
?? await fetchEffectiveACL(requireUrlParentDirectory(resourceUrl), fetch);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function fetchSolidDocumentACL(documentUrl: string, fetch: Fetch): Promise<{
|
|
33
|
+
url: string;
|
|
34
|
+
effectiveUrl: string;
|
|
35
|
+
document: SolidDocument;
|
|
36
|
+
}> {
|
|
37
|
+
const url = await fetchACLResourceUrl(documentUrl, fetch);
|
|
38
|
+
const document = await fetchEffectiveACL(documentUrl, fetch, url);
|
|
39
|
+
|
|
40
|
+
return objectWithoutEmpty({
|
|
41
|
+
url,
|
|
42
|
+
effectiveUrl: document.url,
|
|
43
|
+
document,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -3,28 +3,24 @@ import type { Quad } from 'rdf-js';
|
|
|
3
3
|
|
|
4
4
|
import { expandIRI } from '@/helpers/vocabs';
|
|
5
5
|
|
|
6
|
-
import
|
|
6
|
+
import SolidStore from './SolidStore';
|
|
7
7
|
|
|
8
|
-
export default class SolidDocument {
|
|
8
|
+
export default class SolidDocument extends SolidStore {
|
|
9
9
|
|
|
10
10
|
public readonly url: string;
|
|
11
11
|
public readonly headers: Headers;
|
|
12
|
-
private quads: Quad[];
|
|
13
12
|
|
|
14
13
|
public constructor(url: string, quads: Quad[], headers: Headers) {
|
|
14
|
+
super(quads);
|
|
15
|
+
|
|
15
16
|
this.url = url;
|
|
16
|
-
this.quads = quads;
|
|
17
17
|
this.headers = headers;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
public isEmpty(): boolean {
|
|
21
|
-
return this.statements.length === 0;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
20
|
public isPersonalProfile(): boolean {
|
|
25
21
|
return !!this.statement(
|
|
26
22
|
this.url,
|
|
27
|
-
expandIRI('
|
|
23
|
+
expandIRI('rdf:type'),
|
|
28
24
|
expandIRI('foaf:PersonalProfileDocument'),
|
|
29
25
|
);
|
|
30
26
|
}
|
|
@@ -40,34 +36,8 @@ export default class SolidDocument {
|
|
|
40
36
|
?? null;
|
|
41
37
|
}
|
|
42
38
|
|
|
43
|
-
|
|
44
|
-
return this.
|
|
45
|
-
statement =>
|
|
46
|
-
(!object || statement.object.value === expandIRI(object, { defaultPrefix: this.url })) &&
|
|
47
|
-
(!subject || statement.subject.value === expandIRI(subject, { defaultPrefix: this.url })) &&
|
|
48
|
-
(!predicate || statement.predicate.value === expandIRI(predicate, { defaultPrefix: this.url })),
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
public statement(subject?: string, predicate?: string, object?: string): Quad | null {
|
|
53
|
-
const statement = this.quads.find(
|
|
54
|
-
statement =>
|
|
55
|
-
(!object || statement.object.value === expandIRI(object, { defaultPrefix: this.url })) &&
|
|
56
|
-
(!subject || statement.subject.value === expandIRI(subject, { defaultPrefix: this.url })) &&
|
|
57
|
-
(!predicate || statement.predicate.value === expandIRI(predicate, { defaultPrefix: this.url })),
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
return statement ?? null;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public contains(subject: string, predicate?: string, object?: string): boolean {
|
|
64
|
-
return this.statement(subject, predicate, object) !== null;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
public getThing(subject: string): SolidThing {
|
|
68
|
-
const statements = this.statements(subject);
|
|
69
|
-
|
|
70
|
-
return new SolidThing(subject, statements);
|
|
39
|
+
protected expandIRI(iri: string): string {
|
|
40
|
+
return expandIRI(iri, { defaultPrefix: this.url });
|
|
71
41
|
}
|
|
72
42
|
|
|
73
43
|
private getLatestDocumentDate(): Date | null {
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Quad } from 'rdf-js';
|
|
2
|
+
|
|
3
|
+
import { expandIRI } from '@/helpers/vocabs';
|
|
4
|
+
|
|
5
|
+
import SolidThing from './SolidThing';
|
|
6
|
+
|
|
7
|
+
export default class SolidStore {
|
|
8
|
+
|
|
9
|
+
private quads: Quad[];
|
|
10
|
+
|
|
11
|
+
public constructor(quads: Quad[] = []) {
|
|
12
|
+
this.quads = quads;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public isEmpty(): boolean {
|
|
16
|
+
return this.statements.length === 0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public getQuads(): Quad[] {
|
|
20
|
+
return this.quads.slice(0);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public addQuads(quads: Quad[]): void {
|
|
24
|
+
this.quads.push(...quads);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public statements(subject?: string, predicate?: string, object?: string): Quad[] {
|
|
28
|
+
return this.quads.filter(
|
|
29
|
+
statement =>
|
|
30
|
+
(!object || statement.object.value === this.expandIRI(object)) &&
|
|
31
|
+
(!subject || statement.subject.value === this.expandIRI(subject)) &&
|
|
32
|
+
(!predicate || statement.predicate.value === this.expandIRI(predicate)),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public statement(subject?: string, predicate?: string, object?: string): Quad | null {
|
|
37
|
+
const statement = this.quads.find(
|
|
38
|
+
statement =>
|
|
39
|
+
(!object || statement.object.value === this.expandIRI(object)) &&
|
|
40
|
+
(!subject || statement.subject.value === this.expandIRI(subject)) &&
|
|
41
|
+
(!predicate || statement.predicate.value === this.expandIRI(predicate)),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
return statement ?? null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public contains(subject: string, predicate?: string, object?: string): boolean {
|
|
48
|
+
return this.statement(subject, predicate, object) !== null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public getThing(subject: string): SolidThing {
|
|
52
|
+
const statements = this.statements(subject);
|
|
53
|
+
|
|
54
|
+
return new SolidThing(subject, statements);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
protected expandIRI(iri: string): string {
|
|
58
|
+
return expandIRI(iri);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
}
|
package/src/models/index.ts
CHANGED
|
@@ -16,17 +16,15 @@ function formatResult(result: EqualityResult, options: FormatResultOptions) {
|
|
|
16
16
|
? () => [
|
|
17
17
|
result.message,
|
|
18
18
|
utils.matcherHint(options.hint),
|
|
19
|
+
].join('\n\n')
|
|
20
|
+
: () => [
|
|
21
|
+
result.message,
|
|
22
|
+
utils.matcherHint(options.hint),
|
|
19
23
|
[
|
|
20
24
|
`Expected: not ${utils.printExpected(options.expected)}`,
|
|
21
25
|
`Received: ${utils.printReceived(options.received)}`,
|
|
22
26
|
].join('\n'),
|
|
23
|
-
].join('\n\n')
|
|
24
|
-
: () => {
|
|
25
|
-
return [
|
|
26
|
-
result.message,
|
|
27
|
-
utils.matcherHint(options.hint),
|
|
28
|
-
].join('\n\n');
|
|
29
|
-
};
|
|
27
|
+
].join('\n\n');
|
|
30
28
|
|
|
31
29
|
return { pass, message };
|
|
32
30
|
}
|