solid-logic 1.3.12 → 1.3.13

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.
@@ -1,181 +0,0 @@
1
- import { NamedNode, Node, st, term, sym, Statement } from "rdflib";
2
- import { LiveStore, SolidNamespace } from "../index";
3
- import { ProfileLogic } from "../profile/ProfileLogic";
4
- import { newThing } from "../uri";
5
- import crossFetch from 'cross-fetch';
6
-
7
- export const ACL_LINK = sym(
8
- "http://www.iana.org/assignments/link-relations/acl"
9
- );
10
-
11
- interface NewPaneOptions {
12
- me?: NamedNode;
13
- newInstance?: NamedNode;
14
- newBase: string;
15
- }
16
-
17
- interface CreatedPaneOptions {
18
- newInstance: NamedNode;
19
- }
20
-
21
- /**
22
- * Utility-related logic
23
- */
24
- export class UtilityLogic {
25
- store: LiveStore;
26
- ns: SolidNamespace;
27
- fetcher: any; // { fetch: (url: string, options?: any) => any };
28
- _fetch: Function;
29
-
30
- constructor(store: LiveStore, ns: SolidNamespace, fetcher: { fetch: (url: string, options?: any) => any }) {
31
- this.store = store;
32
- this.ns = ns;
33
- this._fetch = fetcher.fetch
34
- || (typeof global !== 'undefined' && global.solidFetch)
35
- || (typeof window !== 'undefined' && window.solidFetch)
36
- || crossFetch
37
- if (!this._fetch) {
38
- throw new Error('No _fetch function available for Fetcher')
39
- }
40
- }
41
-
42
- async findAclDocUrl(url: string) {
43
- const doc = this.store.sym(url);
44
- await this.store.fetcher.load(doc);
45
- const docNode = this.store.any(doc, ACL_LINK);
46
- if (!docNode) {
47
- throw new Error(`No ACL link discovered for ${url}`);
48
- }
49
- return docNode.value;
50
- }
51
-
52
- // Copied from https://github.com/solid/web-access-control-tests/blob/v3.0.0/test/surface/delete.test.ts#L5
53
- async setSinglePeerAccess(options: {
54
- ownerWebId: string,
55
- peerWebId: string,
56
- accessToModes?: string,
57
- defaultModes?: string,
58
- target: string
59
- }) {
60
- let str = [
61
- '@prefix acl: <http://www.w3.org/ns/auth/acl#>.',
62
- '',
63
- `<#alice> a acl:Authorization;\n acl:agent <${options.ownerWebId}>;`,
64
- ` acl:accessTo <${options.target}>;`,
65
- ` acl:default <${options.target}>;`,
66
- ' acl:mode acl:Read, acl:Write, acl:Control.',
67
- ''
68
- ].join('\n')
69
- if (options.accessToModes) {
70
- str += [
71
- '<#bobAccessTo> a acl:Authorization;',
72
- ` acl:agent <${options.peerWebId}>;`,
73
- ` acl:accessTo <${options.target}>;`,
74
- ` acl:mode ${options.accessToModes}.`,
75
- ''
76
- ].join('\n')
77
- }
78
- if (options.defaultModes) {
79
- str += [
80
- '<#bobDefault> a acl:Authorization;',
81
- ` acl:agent <${options.peerWebId}>;`,
82
- ` acl:default <${options.target}>;`,
83
- ` acl:mode ${options.defaultModes}.`,
84
- ''
85
- ].join('\n')
86
- }
87
- const aclDocUrl = await this.findAclDocUrl(options.target);
88
- return this._fetch(aclDocUrl, {
89
- method: 'PUT',
90
- body: str,
91
- headers: [
92
- [ 'Content-Type', 'text/turtle' ]
93
- ]
94
- });
95
- }
96
-
97
- async loadDoc(doc: NamedNode): Promise<void> {
98
- // Load a document into the knowledge base (fetcher.store)
99
- // withCredentials: Web arch should let us just load by turning off creds helps CORS
100
- // reload: Gets around a specific old Chrome bug caching/origin/cors
101
- // console.log('loading', profileDocument)
102
- if (!this.store.fetcher) {
103
- throw new Error("Cannot load doc, have no fetcher");
104
- }
105
- await this.store.fetcher.load(doc, {
106
- withCredentials: false,
107
- cache: "reload",
108
- });
109
- // console.log('loaded', profileDocument, this.store)
110
- }
111
-
112
- isContainer(url: string) {
113
- return url.substr(-1) === "/";
114
- }
115
-
116
- async createContainer(url: string) {
117
- if (!this.isContainer(url)) {
118
- throw new Error(`Not a container URL ${url}`);
119
- }
120
- // Copied from https://github.com/solid/solid-crud-tests/blob/v3.1.0/test/surface/create-container.test.ts#L56-L64
121
- const result = await this._fetch(url, {
122
- method: "PUT",
123
- headers: {
124
- "Content-Type": "text/turtle",
125
- "If-None-Match": "*",
126
- Link: '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"', // See https://github.com/solid/node-solid-server/issues/1465
127
- },
128
- body: " ", // work around https://github.com/michielbdejong/community-server/issues/4#issuecomment-776222863
129
- });
130
- if (result.status.toString()[0] !== '2') {
131
- throw new Error(`Not OK: got ${result.status} response while creating container at ${url}`);
132
- }
133
- }
134
-
135
- getContainerElements(containerNode: NamedNode): NamedNode[] {
136
- return this.store
137
- .statementsMatching(
138
- containerNode,
139
- this.store.sym("http://www.w3.org/ns/ldp#contains"),
140
- undefined,
141
- containerNode.doc()
142
- )
143
- .map((st: Statement) => st.object as NamedNode);
144
- }
145
-
146
- async getContainerMembers(containerUrl: string): Promise<string[]> {
147
- const containerNode = this.store.sym(containerUrl);
148
- await this.store.fetcher.load(containerNode);
149
- const nodes = this.getContainerElements(containerNode);
150
- return nodes.map(node => node.value);
151
- }
152
-
153
- async recursiveDelete(url: string) {
154
- try {
155
- if (this.isContainer(url)) {
156
- const aclDocUrl = await this.findAclDocUrl(url);
157
- await this._fetch(aclDocUrl, { method: "DELETE" });
158
- const containerMembers = await this.getContainerMembers(url);
159
- await Promise.all(
160
- containerMembers.map((url) => this.recursiveDelete(url))
161
- );
162
- }
163
- return this._fetch(url, { method: "DELETE" });
164
- } catch (e) {
165
- // console.log(`Please manually remove ${url} from your system under test.`, e);
166
- }
167
- }
168
-
169
- clearStore() {
170
- this.store.statements.slice().forEach(this.store.remove.bind(this.store));
171
- }
172
-
173
- getArchiveUrl(baseUrl: string, date: Date) {
174
- const year = date.getUTCFullYear();
175
- const month = ('0' + (date.getUTCMonth()+1)).slice(-2);
176
- const day = ('0' + (date.getUTCDate())).slice(-2);
177
- const parts = baseUrl.split('/');
178
- const filename = parts[parts.length -1 ];
179
- return new URL(`./archive/${year}/${month}/${day}/${filename}`, baseUrl).toString();
180
- }
181
- }