@noeldemartin/solid-utils 0.1.1-next.f279ff39536b39493ea8febae8d8052a9a3b1365 → 0.2.0

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.
@@ -10,7 +10,8 @@ const knownPrefixes: RDFContext = {
10
10
  foaf: 'http://xmlns.com/foaf/0.1/',
11
11
  pim: 'http://www.w3.org/ns/pim/space#',
12
12
  purl: 'http://purl.org/dc/terms/',
13
- rdfs: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
13
+ rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
14
+ rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
14
15
  schema: 'https://schema.org/',
15
16
  solid: 'http://www.w3.org/ns/solid/terms#',
16
17
  vcard: 'http://www.w3.org/2006/vcard/ns#',
@@ -1,5 +1,6 @@
1
1
  import { objectWithoutEmpty, requireUrlParentDirectory, urlResolve } from '@noeldemartin/utils';
2
2
 
3
+ import UnsupportedAuthorizationProtocolError from '@/errors/UnsupportedAuthorizationProtocolError';
3
4
  import { fetchSolidDocumentIfFound } from '@/helpers/io';
4
5
  import type SolidDocument from '@/models/SolidDocument';
5
6
  import type { Fetch } from '@/helpers/io';
@@ -25,8 +26,17 @@ async function fetchEffectiveACL(
25
26
  ): Promise<SolidDocument> {
26
27
  aclResourceUrl = aclResourceUrl ?? await fetchACLResourceUrl(resourceUrl, fetch);
27
28
 
28
- return await fetchSolidDocumentIfFound(aclResourceUrl ?? '', fetch)
29
- ?? await fetchEffectiveACL(requireUrlParentDirectory(resourceUrl), fetch);
29
+ const aclDocument = await fetchSolidDocumentIfFound(aclResourceUrl ?? '', fetch);
30
+
31
+ if (!aclDocument) {
32
+ return fetchEffectiveACL(requireUrlParentDirectory(resourceUrl), fetch);
33
+ }
34
+
35
+ if (aclDocument.isACPResource()) {
36
+ throw new UnsupportedAuthorizationProtocolError(resourceUrl, 'ACP');
37
+ }
38
+
39
+ return aclDocument;
30
40
  }
31
41
 
32
42
  export async function fetchSolidDocumentACL(documentUrl: string, fetch: Fetch): Promise<{
@@ -1,30 +1,38 @@
1
- import { parseDate } from '@noeldemartin/utils';
1
+ import { arrayFilter, parseDate, stringMatch } from '@noeldemartin/utils';
2
2
  import type { Quad } from 'rdf-js';
3
3
 
4
4
  import { expandIRI } from '@/helpers/vocabs';
5
5
 
6
- import SolidThing from './SolidThing';
6
+ import SolidStore from './SolidStore';
7
7
 
8
- export default class SolidDocument {
8
+ export enum SolidDocumentPermission {
9
+ Read = 'read',
10
+ Write = 'write',
11
+ Append = 'append',
12
+ Control = 'control',
13
+ }
14
+
15
+ export default class SolidDocument extends SolidStore {
9
16
 
10
17
  public readonly url: string;
11
18
  public readonly headers: Headers;
12
- private quads: Quad[];
13
19
 
14
20
  public constructor(url: string, quads: Quad[], headers: Headers) {
21
+ super(quads);
22
+
15
23
  this.url = url;
16
- this.quads = quads;
17
24
  this.headers = headers;
18
25
  }
19
26
 
20
- public isEmpty(): boolean {
21
- return this.statements.length === 0;
27
+ public isACPResource(): boolean {
28
+ return !!this.headers.get('Link')
29
+ ?.match(/<http:\/\/www\.w3\.org\/ns\/solid\/acp#AccessControlResource>;[^,]+rel="type"/);
22
30
  }
23
31
 
24
32
  public isPersonalProfile(): boolean {
25
33
  return !!this.statement(
26
34
  this.url,
27
- expandIRI('rdfs:type'),
35
+ expandIRI('rdf:type'),
28
36
  expandIRI('foaf:PersonalProfileDocument'),
29
37
  );
30
38
  }
@@ -33,41 +41,27 @@ export default class SolidDocument {
33
41
  return !!this.headers.get('Link')?.match(/<http:\/\/www\.w3\.org\/ns\/pim\/space#Storage>;[^,]+rel="type"/);
34
42
  }
35
43
 
36
- public getLastModified(): Date | null {
37
- return parseDate(this.headers.get('last-modified'))
38
- ?? parseDate(this.statement(this.url, 'purl:modified')?.object.value)
39
- ?? this.getLatestDocumentDate()
40
- ?? null;
44
+ public isUserWritable(): boolean {
45
+ return this.getUserPermissions().includes(SolidDocumentPermission.Write);
41
46
  }
42
47
 
43
- public statements(subject?: string, predicate?: string, object?: string): Quad[] {
44
- return this.quads.filter(
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
- );
48
+ public getUserPermissions(): SolidDocumentPermission[] {
49
+ return this.getPermissionsFromWAC('user');
50
50
  }
51
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;
52
+ public getPublicPermissions(): SolidDocumentPermission[] {
53
+ return this.getPermissionsFromWAC('public');
61
54
  }
62
55
 
63
- public contains(subject: string, predicate?: string, object?: string): boolean {
64
- return this.statement(subject, predicate, object) !== null;
56
+ public getLastModified(): Date | null {
57
+ return parseDate(this.headers.get('last-modified'))
58
+ ?? parseDate(this.statement(this.url, 'purl:modified')?.object.value)
59
+ ?? this.getLatestDocumentDate()
60
+ ?? null;
65
61
  }
66
62
 
67
- public getThing(subject: string): SolidThing {
68
- const statements = this.statements(subject);
69
-
70
- return new SolidThing(subject, statements);
63
+ protected expandIRI(iri: string): string {
64
+ return expandIRI(iri, { defaultPrefix: this.url });
71
65
  }
72
66
 
73
67
  private getLatestDocumentDate(): Date | null {
@@ -81,4 +75,16 @@ export default class SolidDocument {
81
75
  return dates.length > 0 ? dates.reduce((a, b) => a > b ? a : b) : null;
82
76
  }
83
77
 
78
+ private getPermissionsFromWAC(type: string): SolidDocumentPermission[] {
79
+ const wacAllow = this.headers.get('WAC-Allow') ?? '';
80
+ const publicModes = stringMatch<2>(wacAllow, new RegExp(`${type}="([^"]+)"`))?.[1] ?? '';
81
+
82
+ return arrayFilter([
83
+ publicModes.includes('read') && SolidDocumentPermission.Read,
84
+ publicModes.includes('write') && SolidDocumentPermission.Write,
85
+ publicModes.includes('append') && SolidDocumentPermission.Append,
86
+ publicModes.includes('control') && SolidDocumentPermission.Control,
87
+ ]);
88
+ }
89
+
84
90
  }
@@ -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
+ }
@@ -1,2 +1,3 @@
1
- export { default as SolidDocument } from './SolidDocument';
1
+ export { default as SolidDocument, SolidDocumentPermission } from './SolidDocument';
2
+ export { default as SolidStore } from './SolidStore';
2
3
  export { default as SolidThing } from './SolidThing';
@@ -0,0 +1,9 @@
1
+ import '@types/n3';
2
+
3
+ declare module 'n3' {
4
+
5
+ interface Parser {
6
+ _resolveRelativeIRI(iri: string): string;
7
+ }
8
+
9
+ }