solid-logic 1.3.11 → 1.3.13-b3d1620b
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/.babelrc +0 -0
- package/.eslintrc.js +0 -0
- package/.github/workflows/ci.yml +40 -5
- package/.github/workflows/release.yml +0 -0
- package/LICENSE +0 -0
- package/README.md +7 -1
- package/jest.config.js +4 -1
- package/lib/acl/aclLogic.d.ts +32 -0
- package/lib/acl/aclLogic.d.ts.map +1 -0
- package/lib/acl/aclLogic.js +132 -0
- package/lib/acl/aclLogic.js.map +1 -0
- package/lib/authSession/authSession.d.ts +4 -0
- package/lib/authSession/authSession.d.ts.map +1 -0
- package/lib/authSession/authSession.js +18 -0
- package/lib/authSession/authSession.js.map +1 -0
- package/lib/authn/SolidAuthnLogic.d.ts +26 -6
- package/lib/authn/SolidAuthnLogic.d.ts.map +1 -1
- package/lib/authn/SolidAuthnLogic.js +161 -5
- package/lib/authn/SolidAuthnLogic.js.map +1 -1
- package/lib/authn/authUtil.d.ts +17 -0
- package/lib/authn/authUtil.d.ts.map +1 -0
- package/lib/authn/authUtil.js +88 -0
- package/lib/authn/authUtil.js.map +1 -0
- package/lib/chat/ChatLogic.d.ts +2 -2
- package/lib/chat/ChatLogic.d.ts.map +1 -1
- package/lib/chat/ChatLogic.js +38 -33
- package/lib/chat/ChatLogic.js.map +1 -1
- package/lib/chat/determineChatContainer.d.ts +0 -0
- package/lib/chat/determineChatContainer.d.ts.map +0 -0
- package/lib/chat/determineChatContainer.js +0 -0
- package/lib/chat/determineChatContainer.js.map +0 -0
- package/lib/inbox/InboxLogic.d.ts +2 -2
- package/lib/inbox/InboxLogic.d.ts.map +1 -1
- package/lib/inbox/InboxLogic.js +14 -13
- package/lib/inbox/InboxLogic.js.map +1 -1
- package/lib/index.d.ts +9 -72
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +31 -384
- package/lib/index.js.map +1 -1
- package/lib/issuer/issuerLogic.d.ts +8 -0
- package/lib/issuer/issuerLogic.d.ts.map +1 -0
- package/lib/issuer/issuerLogic.js +53 -0
- package/lib/issuer/issuerLogic.js.map +1 -0
- package/lib/logic/CustomError.d.ts +17 -0
- package/lib/logic/CustomError.d.ts.map +1 -0
- package/lib/logic/CustomError.js +73 -0
- package/lib/logic/CustomError.js.map +1 -0
- package/lib/{index-alain.d.ts → logic/SolidLogic.d.ts} +7 -33
- package/lib/logic/SolidLogic.d.ts.map +1 -0
- package/lib/{index-alain.js → logic/SolidLogic.js} +17 -99
- package/lib/logic/SolidLogic.js.map +1 -0
- package/lib/logic/solidLogicSingleton.d.ts +8 -0
- package/lib/logic/solidLogicSingleton.d.ts.map +1 -0
- package/lib/logic/solidLogicSingleton.js +88 -0
- package/lib/logic/solidLogicSingleton.js.map +1 -0
- package/lib/profile/ProfileLogic.d.ts +2 -3
- package/lib/profile/ProfileLogic.d.ts.map +1 -1
- package/lib/profile/ProfileLogic.js +10 -8
- package/lib/profile/ProfileLogic.js.map +1 -1
- package/lib/typeIndex/typeIndexLogic.d.ts +22 -0
- package/lib/typeIndex/typeIndexLogic.d.ts.map +1 -0
- package/lib/typeIndex/typeIndexLogic.js +302 -0
- package/lib/typeIndex/typeIndexLogic.js.map +1 -0
- package/lib/types.d.ts +29 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/{authn/index.js → types.js} +1 -1
- package/lib/types.js.map +1 -0
- package/lib/util/UtilityLogic.d.ts +2 -2
- package/lib/util/UtilityLogic.d.ts.map +1 -1
- package/lib/util/UtilityLogic.js +10 -8
- package/lib/util/UtilityLogic.js.map +1 -1
- package/lib/{debug.d.ts → util/debug.d.ts} +0 -0
- package/lib/util/debug.d.ts.map +1 -0
- package/lib/{debug.js → util/debug.js} +0 -0
- package/lib/util/debug.js.map +1 -0
- package/lib/{uri.d.ts → util/uri.d.ts} +0 -0
- package/lib/util/uri.d.ts.map +1 -0
- package/lib/{uri.js → util/uri.js} +0 -0
- package/lib/util/uri.js.map +1 -0
- package/package.json +18 -16
- package/renovate.json +0 -0
- package/src/acl/aclLogic.ts +137 -0
- package/src/authSession/authSession.ts +22 -0
- package/src/authn/SolidAuthnLogic.ts +112 -10
- package/src/authn/authUtil.ts +67 -0
- package/src/chat/ChatLogic.ts +13 -13
- package/src/chat/determineChatContainer.ts +0 -0
- package/src/inbox/InboxLogic.ts +3 -15
- package/src/index.ts +18 -307
- package/src/issuer/issuerLogic.ts +40 -0
- package/src/logic/CustomError.ts +25 -0
- package/src/logic/SolidLogic.ts +264 -0
- package/src/logic/solidLogicSingleton.ts +24 -0
- package/src/profile/ProfileLogic.ts +4 -5
- package/src/typeIndex/typeIndexLogic.ts +170 -0
- package/src/types.ts +41 -0
- package/src/util/UtilityLogic.ts +4 -16
- package/src/{debug.ts → util/debug.ts} +0 -0
- package/src/{uri.ts → util/uri.ts} +0 -0
- package/test/aclLogic.test.ts +15 -0
- package/test/authUtil.test.ts +19 -0
- package/{src/chat/integration.test.ts → test/chatLogic.test.ts} +4 -5
- package/test/helpers/setup.ts +13 -0
- package/{src/inbox/unit.test.ts → test/inboxLogic.test.ts} +5 -6
- package/test/logic.test.ts +28 -0
- package/test/solidAuthLogic.test.ts +45 -0
- package/test/typeIndexLogic.test.ts +26 -0
- package/{src/util/unit.test.ts → test/utilityLogic.test.ts} +3 -4
- package/tsconfig.json +0 -0
- package/jest.setup.ts +0 -2
- package/lib/authn/NoAuthnLogic.d.ts +0 -9
- package/lib/authn/NoAuthnLogic.d.ts.map +0 -1
- package/lib/authn/NoAuthnLogic.js +0 -17
- package/lib/authn/NoAuthnLogic.js.map +0 -1
- package/lib/authn/index.d.ts +0 -5
- package/lib/authn/index.d.ts.map +0 -1
- package/lib/authn/index.js.map +0 -1
- package/lib/chat/integration.test.d.ts +0 -2
- package/lib/chat/integration.test.d.ts.map +0 -1
- package/lib/chat/integration.test.js +0 -318
- package/lib/chat/integration.test.js.map +0 -1
- package/lib/debug.d.ts.map +0 -1
- package/lib/debug.js.map +0 -1
- package/lib/inbox/unit.test.d.ts +0 -2
- package/lib/inbox/unit.test.d.ts.map +0 -1
- package/lib/inbox/unit.test.js +0 -264
- package/lib/inbox/unit.test.js.map +0 -1
- package/lib/index-alain.d.ts.map +0 -1
- package/lib/index-alain.js.map +0 -1
- package/lib/uri.d.ts.map +0 -1
- package/lib/uri.js.map +0 -1
- package/lib/util/UtilityLogic-alain.d.ts +0 -32
- package/lib/util/UtilityLogic-alain.d.ts.map +0 -1
- package/lib/util/UtilityLogic-alain.js +0 -248
- package/lib/util/UtilityLogic-alain.js.map +0 -1
- package/lib/util/unit.test.d.ts +0 -2
- package/lib/util/unit.test.d.ts.map +0 -1
- package/lib/util/unit.test.js +0 -200
- package/lib/util/unit.test.js.map +0 -1
- package/src/authn/NoAuthnLogic.ts +0 -16
- package/src/authn/index.ts +0 -5
- package/src/index-alain.txt +0 -316
- package/src/util/UtilityLogic-alain.txt +0 -181
package/src/index-alain.txt
DELETED
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
import { Session } from "@inrupt/solid-client-authn-browser";
|
|
2
|
-
import * as rdf from "rdflib";
|
|
3
|
-
import { Fetcher, NamedNode, Statement, Store, UpdateManager } from "rdflib";
|
|
4
|
-
import solidNamespace from "solid-namespace";
|
|
5
|
-
import { AuthnLogic } from "./authn";
|
|
6
|
-
import { NoAuthnLogic } from "./authn/NoAuthnLogic";
|
|
7
|
-
import { SolidAuthnLogic } from "./authn/SolidAuthnLogic";
|
|
8
|
-
import crossFetch from 'cross-fetch';
|
|
9
|
-
|
|
10
|
-
import { ChatLogic } from "./chat/ChatLogic";
|
|
11
|
-
import * as debug from "./debug";
|
|
12
|
-
import { ProfileLogic } from "./profile/ProfileLogic";
|
|
13
|
-
import { UtilityLogic } from "./util/UtilityLogic";
|
|
14
|
-
|
|
15
|
-
export { ACL_LINK } from './util/UtilityLogic';
|
|
16
|
-
|
|
17
|
-
const ns: SolidNamespace = solidNamespace(rdf);
|
|
18
|
-
|
|
19
|
-
interface ConnectedStore extends Store {
|
|
20
|
-
fetcher: Fetcher;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface LiveStore extends ConnectedStore {
|
|
24
|
-
updater: UpdateManager;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface SolidNamespace {
|
|
28
|
-
[key: string]: (term: string) => NamedNode;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export class SolidLogic {
|
|
32
|
-
cache: {
|
|
33
|
-
profileDocument: {
|
|
34
|
-
[WebID: string]: NamedNode;
|
|
35
|
-
};
|
|
36
|
-
preferencesFile: {
|
|
37
|
-
[WebID: string]: NamedNode;
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
store: LiveStore;
|
|
42
|
-
me: string | undefined;
|
|
43
|
-
fetcher: { fetch: (url: string, options?: any) => any };
|
|
44
|
-
_fetch: Function;
|
|
45
|
-
|
|
46
|
-
chat: ChatLogic;
|
|
47
|
-
profile: ProfileLogic;
|
|
48
|
-
authn: AuthnLogic;
|
|
49
|
-
util: UtilityLogic;
|
|
50
|
-
|
|
51
|
-
constructor(fetcher: { fetch: (url: any, requestInit: any) => any }, solidAuthSession: Session) {
|
|
52
|
-
this.store = rdf.graph() as LiveStore; // Make a Quad store
|
|
53
|
-
rdf.fetcher(this.store, fetcher); // Attach a web I/O module, store.fetcher
|
|
54
|
-
this.store.updater = new rdf.UpdateManager(this.store); // Add real-time live updates store.updater
|
|
55
|
-
this.cache = {
|
|
56
|
-
profileDocument: {},
|
|
57
|
-
preferencesFile: {},
|
|
58
|
-
};
|
|
59
|
-
this.fetcher = fetcher
|
|
60
|
-
/*this._fetch = this.fetcher.fetch
|
|
61
|
-
|| typeof global !== 'undefined' && global.solidFetch
|
|
62
|
-
|| typeof window !== 'undefined' && window.solidFetch
|
|
63
|
-
|| crossFetch
|
|
64
|
-
if (!this._fetch) {
|
|
65
|
-
throw new Error('No _fetch function available for Fetcher')
|
|
66
|
-
} */
|
|
67
|
-
if (solidAuthSession) {
|
|
68
|
-
this.authn = new SolidAuthnLogic(solidAuthSession);
|
|
69
|
-
} else {
|
|
70
|
-
this.authn = new NoAuthnLogic();
|
|
71
|
-
}
|
|
72
|
-
this.profile = new ProfileLogic(this.store, ns, this.authn);
|
|
73
|
-
this.chat = new ChatLogic(this.store, ns, this.profile);
|
|
74
|
-
this.util = new UtilityLogic(this.store, ns, this.fetcher);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
findAclDocUrl(url: string) {
|
|
78
|
-
return this.util.findAclDocUrl(url);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
loadDoc(doc: NamedNode): Promise<void> {
|
|
82
|
-
return this.util.loadDoc(doc);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async loadProfile(me: NamedNode): Promise<NamedNode> {
|
|
86
|
-
// console.log('loadProfile', me)
|
|
87
|
-
if (this.cache.profileDocument[me.value]) {
|
|
88
|
-
return this.cache.profileDocument[me.value];
|
|
89
|
-
}
|
|
90
|
-
let profileDocument;
|
|
91
|
-
try {
|
|
92
|
-
profileDocument = me.doc();
|
|
93
|
-
await this.loadDoc(profileDocument);
|
|
94
|
-
return profileDocument;
|
|
95
|
-
} catch (err) {
|
|
96
|
-
const message = `Logged in but cannot load profile ${profileDocument} : ${err}`;
|
|
97
|
-
throw new Error(message);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
async loadPreferences(me: NamedNode): Promise<NamedNode> {
|
|
102
|
-
// console.log('loadPreferences', me)
|
|
103
|
-
if (this.cache.preferencesFile[me.value]) {
|
|
104
|
-
return this.cache.preferencesFile[me.value];
|
|
105
|
-
}
|
|
106
|
-
const preferencesFile = this.store.any(me, ns.space("preferencesFile"));
|
|
107
|
-
|
|
108
|
-
// console.log('this.store.any()', this.store.any())
|
|
109
|
-
/**
|
|
110
|
-
* Are we working cross-origin?
|
|
111
|
-
* Returns True if we are in a webapp at an origin, and the file origin is different
|
|
112
|
-
*/
|
|
113
|
-
function differentOrigin(): boolean {
|
|
114
|
-
if (!preferencesFile) {
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
|
-
return (
|
|
118
|
-
`${window.location.origin}/` !== new URL(preferencesFile.value).origin
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (!preferencesFile) {
|
|
123
|
-
throw new Error(
|
|
124
|
-
`Can't find a preference file pointer in profile ${me.doc()}`
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (!this.store.fetcher) {
|
|
129
|
-
throw new Error("Cannot load doc, have no fetcher");
|
|
130
|
-
}
|
|
131
|
-
// //// Load preference file
|
|
132
|
-
try {
|
|
133
|
-
await this.store.fetcher.load(preferencesFile as NamedNode, {
|
|
134
|
-
withCredentials: true,
|
|
135
|
-
});
|
|
136
|
-
} catch (err) {
|
|
137
|
-
// Really important to look at why
|
|
138
|
-
const status = err.status;
|
|
139
|
-
debug.log(`HTTP status ${status} for preference file ${preferencesFile}`);
|
|
140
|
-
if (status === 401) {
|
|
141
|
-
throw new UnauthorizedError();
|
|
142
|
-
}
|
|
143
|
-
if (status === 403) {
|
|
144
|
-
if (differentOrigin()) {
|
|
145
|
-
throw new CrossOriginForbiddenError();
|
|
146
|
-
}
|
|
147
|
-
throw new SameOriginForbiddenError();
|
|
148
|
-
}
|
|
149
|
-
if (status === 404) {
|
|
150
|
-
throw new NotFoundError(preferencesFile.value);
|
|
151
|
-
}
|
|
152
|
-
throw new FetchError(err.status, err.message);
|
|
153
|
-
}
|
|
154
|
-
return preferencesFile as NamedNode;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
getTypeIndex(
|
|
158
|
-
me: NamedNode | string,
|
|
159
|
-
preferencesFile: NamedNode | string,
|
|
160
|
-
isPublic: boolean
|
|
161
|
-
): NamedNode[] {
|
|
162
|
-
// console.log('getTypeIndex', this.store.each(me, undefined, undefined, preferencesFile), isPublic, preferencesFile)
|
|
163
|
-
return this.store.each(
|
|
164
|
-
me as NamedNode,
|
|
165
|
-
isPublic ? ns.solid("publicTypeIndex") : ns.solid("privateTypeIndex"),
|
|
166
|
-
undefined,
|
|
167
|
-
preferencesFile as NamedNode
|
|
168
|
-
) as NamedNode[];
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
getRegistrations(instance, theClass) {
|
|
172
|
-
return this.store
|
|
173
|
-
.each(undefined, ns.solid("instance"), instance)
|
|
174
|
-
.filter((r) => {
|
|
175
|
-
return this.store.holds(r, ns.solid("forClass"), theClass);
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
load(doc: NamedNode | NamedNode[] | string) {
|
|
180
|
-
if (!this.store.fetcher) {
|
|
181
|
-
throw new Error("Cannot load doc(s), have no fetcher");
|
|
182
|
-
}
|
|
183
|
-
return this.store.fetcher.load(doc);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
async loadIndexes(
|
|
187
|
-
me: NamedNode | string,
|
|
188
|
-
publicProfile: NamedNode | string | null,
|
|
189
|
-
preferencesFile: NamedNode | string | null,
|
|
190
|
-
onWarning = async (_err: Error) => {
|
|
191
|
-
return undefined;
|
|
192
|
-
}
|
|
193
|
-
): Promise<{
|
|
194
|
-
private: any;
|
|
195
|
-
public: any;
|
|
196
|
-
}> {
|
|
197
|
-
let privateIndexes: any[] = [];
|
|
198
|
-
let publicIndexes: any[] = [];
|
|
199
|
-
if (publicProfile) {
|
|
200
|
-
publicIndexes = this.getTypeIndex(me, publicProfile, true);
|
|
201
|
-
try {
|
|
202
|
-
await this.load(publicIndexes as NamedNode[]);
|
|
203
|
-
} catch (err) {
|
|
204
|
-
onWarning(new Error(`loadIndex: loading public type index(es) ${err}`));
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (preferencesFile) {
|
|
208
|
-
privateIndexes = this.getTypeIndex(me, preferencesFile, false);
|
|
209
|
-
// console.log({ privateIndexes })
|
|
210
|
-
if (privateIndexes.length === 0) {
|
|
211
|
-
await onWarning(
|
|
212
|
-
new Error(
|
|
213
|
-
`Your preference file ${preferencesFile} does not point to a private type index.`
|
|
214
|
-
)
|
|
215
|
-
);
|
|
216
|
-
} else {
|
|
217
|
-
try {
|
|
218
|
-
await this.load(privateIndexes);
|
|
219
|
-
} catch (err) {
|
|
220
|
-
onWarning(
|
|
221
|
-
new Error(`loadIndex: loading private type index(es) ${err}`)
|
|
222
|
-
);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
// } else {
|
|
226
|
-
// debug.log(
|
|
227
|
-
// 'We know your preference file is not available, so we are not bothering with private type indexes.'
|
|
228
|
-
// )
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
return {
|
|
232
|
-
private: privateIndexes,
|
|
233
|
-
public: publicIndexes,
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
async createEmptyRdfDoc(doc: NamedNode, comment: string) {
|
|
238
|
-
if (!this.store.fetcher) {
|
|
239
|
-
throw new Error("Cannot create empty rdf doc, have no fetcher");
|
|
240
|
-
}
|
|
241
|
-
await this.store.fetcher.webOperation("PUT", doc.uri, {
|
|
242
|
-
data: `# ${new Date()} ${comment}
|
|
243
|
-
`,
|
|
244
|
-
contentType: "text/turtle",
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// @@@@ use the one in rdflib.js when it is available and delete this
|
|
249
|
-
updatePromise(
|
|
250
|
-
del: Array<Statement>,
|
|
251
|
-
ins: Array<Statement> = []
|
|
252
|
-
): Promise<void> {
|
|
253
|
-
return new Promise((resolve, reject) => {
|
|
254
|
-
if (!this.store.updater) {
|
|
255
|
-
throw new Error("Cannot updatePromise, have no updater");
|
|
256
|
-
}
|
|
257
|
-
this.store.updater.update(del, ins, function (_uri, ok, errorBody) {
|
|
258
|
-
if (!ok) {
|
|
259
|
-
reject(new Error(errorBody));
|
|
260
|
-
} else {
|
|
261
|
-
resolve();
|
|
262
|
-
}
|
|
263
|
-
}); // callback
|
|
264
|
-
}); // promise
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
isContainer(url: string) {
|
|
268
|
-
return this.util.isContainer(url);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
getContainerElements(containerNode: NamedNode): NamedNode[] {
|
|
272
|
-
return this.util.getContainerElements(containerNode);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
getContainerMembers(containerUrl: string): Promise<string[]> {
|
|
276
|
-
return this.util.getContainerMembers(containerUrl);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
async recursiveDelete(url: string) {
|
|
280
|
-
return this.util.recursiveDelete(url);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
clearStore() {
|
|
284
|
-
return this.util.clearStore();
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
async fetch(url: string, options?: any) {
|
|
288
|
-
return this.fetcher.fetch(url, options);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
class CustomError extends Error {
|
|
293
|
-
constructor(message?: string) {
|
|
294
|
-
super(message);
|
|
295
|
-
// see: typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html
|
|
296
|
-
Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
|
|
297
|
-
this.name = new.target.name; // stack traces display correctly now
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
export class UnauthorizedError extends CustomError {}
|
|
302
|
-
|
|
303
|
-
export class CrossOriginForbiddenError extends CustomError {}
|
|
304
|
-
|
|
305
|
-
export class SameOriginForbiddenError extends CustomError {}
|
|
306
|
-
|
|
307
|
-
export class NotFoundError extends CustomError {}
|
|
308
|
-
|
|
309
|
-
export class FetchError extends CustomError {
|
|
310
|
-
status: number;
|
|
311
|
-
|
|
312
|
-
constructor(status: number, message?: string) {
|
|
313
|
-
super(message);
|
|
314
|
-
this.status = status;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
@@ -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
|
-
}
|