solid-logic 1.3.12-bbf45639 → 1.3.13-438dc3ae

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