solid-logic 1.3.17-8a332867 → 1.3.17-a849582e

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 (115) hide show
  1. package/jest.config.js +1 -1
  2. package/lib/acl/aclLogic.d.ts +12 -30
  3. package/lib/acl/aclLogic.d.ts.map +1 -1
  4. package/lib/acl/aclLogic.js +151 -116
  5. package/lib/acl/aclLogic.js.map +1 -1
  6. package/lib/authn/SolidAuthnLogic.d.ts.map +1 -1
  7. package/lib/authn/SolidAuthnLogic.js +2 -2
  8. package/lib/authn/SolidAuthnLogic.js.map +1 -1
  9. package/lib/chat/chatLogic.d.ts +16 -0
  10. package/lib/chat/chatLogic.d.ts.map +1 -0
  11. package/lib/chat/{ChatLogic.js → chatLogic.js} +82 -87
  12. package/lib/chat/chatLogic.js.map +1 -0
  13. package/lib/discovery/discoveryLogic.d.ts +6 -9
  14. package/lib/discovery/discoveryLogic.d.ts.map +1 -1
  15. package/lib/discovery/discoveryLogic.js +95 -89
  16. package/lib/discovery/discoveryLogic.js.map +1 -1
  17. package/lib/inbox/inboxLogic.d.ts +7 -0
  18. package/lib/inbox/inboxLogic.d.ts.map +1 -0
  19. package/lib/inbox/{InboxLogic.js → inboxLogic.js} +58 -64
  20. package/lib/inbox/inboxLogic.js.map +1 -0
  21. package/lib/index.d.ts +10 -13
  22. package/lib/index.d.ts.map +1 -1
  23. package/lib/index.js +76 -44
  24. package/lib/index.js.map +1 -1
  25. package/lib/logic/CustomError.d.ts +4 -0
  26. package/lib/logic/CustomError.d.ts.map +1 -1
  27. package/lib/logic/CustomError.js +17 -1
  28. package/lib/logic/CustomError.js.map +1 -1
  29. package/lib/logic/solidLogicSingleton.d.ts +35 -3
  30. package/lib/logic/solidLogicSingleton.d.ts.map +1 -1
  31. package/lib/logic/solidLogicSingleton.js +87 -8
  32. package/lib/logic/solidLogicSingleton.js.map +1 -1
  33. package/lib/profile/profileLogic.d.ts +13 -0
  34. package/lib/profile/profileLogic.d.ts.map +1 -0
  35. package/lib/profile/profileLogic.js +268 -0
  36. package/lib/profile/profileLogic.js.map +1 -0
  37. package/lib/typeIndex/typeIndexLogic.d.ts +31 -21
  38. package/lib/typeIndex/typeIndexLogic.d.ts.map +1 -1
  39. package/lib/typeIndex/typeIndexLogic.js +650 -295
  40. package/lib/typeIndex/typeIndexLogic.js.map +1 -1
  41. package/lib/types.d.ts +17 -0
  42. package/lib/types.d.ts.map +1 -1
  43. package/lib/util/containerLogic.d.ts +11 -0
  44. package/lib/util/containerLogic.d.ts.map +1 -0
  45. package/lib/{profile/ProfileLogic.js → util/containerLogic.js} +53 -44
  46. package/lib/util/containerLogic.js.map +1 -0
  47. package/lib/util/utilityLogic.d.ts +15 -0
  48. package/lib/util/utilityLogic.d.ts.map +1 -0
  49. package/lib/util/utilityLogic.js +272 -0
  50. package/lib/util/utilityLogic.js.map +1 -0
  51. package/lib/util/utils.d.ts +8 -0
  52. package/lib/util/utils.d.ts.map +1 -0
  53. package/lib/util/utils.js +48 -0
  54. package/lib/util/utils.js.map +1 -0
  55. package/package.json +3 -1
  56. package/src/acl/aclLogic.ts +135 -119
  57. package/src/authn/SolidAuthnLogic.ts +3 -2
  58. package/src/chat/chatLogic.ts +225 -0
  59. package/src/discovery/discoveryLogic.ts +42 -85
  60. package/src/inbox/inboxLogic.ts +57 -0
  61. package/src/index.ts +71 -30
  62. package/src/logic/CustomError.ts +5 -1
  63. package/src/logic/solidLogicSingleton.ts +160 -7
  64. package/src/profile/profileLogic.ts +134 -0
  65. package/src/typeIndex/typeIndexLogic.ts +417 -153
  66. package/src/types.ts +7 -3
  67. package/src/util/containerLogic.ts +54 -0
  68. package/src/util/ns.js +5 -0
  69. package/src/util/utilityLogic.ts +155 -0
  70. package/src/util/utils.ts +52 -0
  71. package/test/aclLogic.test.ts +13 -4
  72. package/test/chatLogic.test.ts +70 -71
  73. package/test/container.test.ts +56 -0
  74. package/test/discoveryLogic.test.ts +503 -531
  75. package/test/helpers/dataSetup.ts +134 -0
  76. package/test/helpers/setup.ts +1 -0
  77. package/test/inboxLogic.test.ts +39 -38
  78. package/test/logic.test.ts +11 -9
  79. package/test/profileLogic.test.ts +246 -0
  80. package/test/typeIndexLogic.test.ts +49 -22
  81. package/test/typeIndexLogicPart2.test.ts +485 -0
  82. package/test/utilityLogic.test.ts +172 -126
  83. package/test/utils.test.ts +32 -0
  84. package/lib/chat/ChatLogic.d.ts +0 -26
  85. package/lib/chat/ChatLogic.d.ts.map +0 -1
  86. package/lib/chat/ChatLogic.js.map +0 -1
  87. package/lib/chat/determineChatContainer.d.ts +0 -3
  88. package/lib/chat/determineChatContainer.d.ts.map +0 -1
  89. package/lib/chat/determineChatContainer.js +0 -12
  90. package/lib/chat/determineChatContainer.js.map +0 -1
  91. package/lib/inbox/InboxLogic.d.ts +0 -18
  92. package/lib/inbox/InboxLogic.d.ts.map +0 -1
  93. package/lib/inbox/InboxLogic.js.map +0 -1
  94. package/lib/logic/SolidLogic.d.ts +0 -48
  95. package/lib/logic/SolidLogic.d.ts.map +0 -1
  96. package/lib/logic/SolidLogic.js +0 -321
  97. package/lib/logic/SolidLogic.js.map +0 -1
  98. package/lib/profile/ProfileLogic.d.ts +0 -13
  99. package/lib/profile/ProfileLogic.d.ts.map +0 -1
  100. package/lib/profile/ProfileLogic.js.map +0 -1
  101. package/lib/util/UtilityLogic.d.ts +0 -33
  102. package/lib/util/UtilityLogic.d.ts.map +0 -1
  103. package/lib/util/UtilityLogic.js +0 -240
  104. package/lib/util/UtilityLogic.js.map +0 -1
  105. package/lib/util/uri.d.ts +0 -3
  106. package/lib/util/uri.d.ts.map +0 -1
  107. package/lib/util/uri.js +0 -9
  108. package/lib/util/uri.js.map +0 -1
  109. package/src/chat/ChatLogic.ts +0 -244
  110. package/src/chat/determineChatContainer.ts +0 -14
  111. package/src/inbox/InboxLogic.ts +0 -66
  112. package/src/logic/SolidLogic.ts +0 -262
  113. package/src/profile/ProfileLogic.ts +0 -44
  114. package/src/util/UtilityLogic.ts +0 -161
  115. package/src/util/uri.ts +0 -5
package/lib/util/uri.js DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.newThing = void 0;
4
- var rdflib_1 = require("rdflib");
5
- function newThing(doc) {
6
- return (0, rdflib_1.sym)(doc.uri + "#" + "id" + ("" + Date.now()));
7
- }
8
- exports.newThing = newThing;
9
- //# sourceMappingURL=uri.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"uri.js","sourceRoot":"","sources":["../../src/util/uri.ts"],"names":[],"mappings":";;;AAAA,iCAAwC;AAExC,SAAgB,QAAQ,CAAC,GAAc;IACrC,OAAO,IAAA,YAAG,EAAC,GAAG,CAAC,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,CAAC,EAAE,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;AACvD,CAAC;AAFD,4BAEC"}
@@ -1,244 +0,0 @@
1
- import { NamedNode, Node, st, term, LiveStore } from "rdflib";
2
- import { ProfileLogic } from "../profile/ProfileLogic";
3
- import { SolidNamespace } from "../types";
4
- import { newThing } from "../util/uri";
5
- import { determineChatContainer } from "./determineChatContainer";
6
-
7
- const CHAT_LOCATION_IN_CONTAINER = "index.ttl#this";
8
-
9
- interface NewPaneOptions {
10
- me?: NamedNode;
11
- newInstance?: NamedNode;
12
- newBase: string;
13
- }
14
-
15
- interface CreatedPaneOptions {
16
- newInstance: NamedNode;
17
- }
18
-
19
- /**
20
- * Chat-related logic
21
- */
22
- export class ChatLogic {
23
- store: LiveStore;
24
- ns: SolidNamespace;
25
- profile: ProfileLogic;
26
-
27
- constructor(store: LiveStore, ns: SolidNamespace, profile: ProfileLogic) {
28
- this.store = store;
29
- this.ns = ns;
30
- this.profile = profile;
31
- }
32
-
33
- async setAcl(
34
- chatContainer: NamedNode,
35
- me: NamedNode,
36
- invitee: NamedNode
37
- ): Promise<void> {
38
- // Some servers don't present a Link http response header
39
- // if the container doesn't exist yet, so refetch the container
40
- // now that it has been created:
41
- await this.store.fetcher?.load(chatContainer);
42
-
43
- // FIXME: check the Why value on this quad:
44
- const chatAclDoc = this.store.any(
45
- chatContainer,
46
- new NamedNode("http://www.iana.org/assignments/link-relations/acl")
47
- );
48
- if (!chatAclDoc) {
49
- throw new Error("Chat ACL doc not found!");
50
- }
51
-
52
- const aclBody = `
53
- @prefix acl: <http://www.w3.org/ns/auth/acl#>.
54
- <#owner>
55
- a acl:Authorization;
56
- acl:agent <${me.value}>;
57
- acl:accessTo <.>;
58
- acl:default <.>;
59
- acl:mode
60
- acl:Read, acl:Write, acl:Control.
61
- <#invitee>
62
- a acl:Authorization;
63
- acl:agent <${invitee.value}>;
64
- acl:accessTo <.>;
65
- acl:default <.>;
66
- acl:mode
67
- acl:Read, acl:Append.
68
- `;
69
- await this.store.fetcher?.webOperation("PUT", chatAclDoc.value, {
70
- data: aclBody,
71
- contentType: "text/turtle",
72
- });
73
- }
74
-
75
- private async addToPrivateTypeIndex(chatThing, me) {
76
- // Add to private type index
77
- const privateTypeIndex = this.store.any(
78
- me,
79
- this.ns.solid("privateTypeIndex")
80
- ) as NamedNode | null;
81
- if (!privateTypeIndex) {
82
- throw new Error("Private type index not found!");
83
- }
84
- await this.store.fetcher?.load(privateTypeIndex);
85
- const reg = newThing(privateTypeIndex);
86
- const ins = [
87
- st(
88
- reg,
89
- this.ns.rdf("type"),
90
- this.ns.solid("TypeRegistration"),
91
- privateTypeIndex.doc()
92
- ),
93
- st(
94
- reg,
95
- this.ns.solid("forClass"),
96
- this.ns.meeting("LongChat"),
97
- privateTypeIndex.doc()
98
- ),
99
- st(reg, this.ns.solid("instance"), chatThing, privateTypeIndex.doc()),
100
- ];
101
- await new Promise((resolve, reject) => {
102
- this.store.updater?.update([], ins, function (_uri, ok, errm) {
103
- if (!ok) {
104
- reject(new Error(errm));
105
- } else {
106
- resolve(null);
107
- }
108
- });
109
- });
110
- }
111
-
112
- private async findChat(invitee: NamedNode) {
113
- const me = await this.profile.loadMe();
114
- const podRoot = await this.profile.getPodRoot(me);
115
- const chatContainer = determineChatContainer(invitee, podRoot);
116
- let exists = true;
117
- try {
118
- await this.store.fetcher?.load(
119
- new NamedNode(chatContainer.value + "index.ttl#this")
120
- );
121
- } catch (e) {
122
- exists = false;
123
- }
124
- return { me, chatContainer, exists };
125
- }
126
-
127
- private async createChatThing(
128
- chatContainer: NamedNode,
129
- me: NamedNode
130
- ): Promise<NamedNode> {
131
- const created = await this.mintNew({
132
- me,
133
- newBase: chatContainer.value,
134
- });
135
- return created.newInstance;
136
- }
137
-
138
- private mintNew(newPaneOptions: NewPaneOptions): Promise<CreatedPaneOptions> {
139
- const kb = this.store;
140
- const updater = kb.updater;
141
- if (newPaneOptions.me && !newPaneOptions.me.uri) {
142
- throw new Error("chat mintNew: Invalid userid " + newPaneOptions.me);
143
- }
144
-
145
- const newInstance = (newPaneOptions.newInstance =
146
- newPaneOptions.newInstance ||
147
- kb.sym(newPaneOptions.newBase + CHAT_LOCATION_IN_CONTAINER));
148
- const newChatDoc = newInstance.doc();
149
-
150
- kb.add(
151
- newInstance,
152
- this.ns.rdf("type"),
153
- this.ns.meeting("LongChat"),
154
- newChatDoc
155
- );
156
- kb.add(newInstance, this.ns.dc("title"), "Chat channel", newChatDoc);
157
- kb.add(
158
- newInstance,
159
- this.ns.dc("created"),
160
- term<Node>(new Date(Date.now())),
161
- newChatDoc
162
- );
163
- if (newPaneOptions.me) {
164
- kb.add(newInstance, this.ns.dc("author"), newPaneOptions.me, newChatDoc);
165
- }
166
-
167
- return new Promise(function (resolve, reject) {
168
- updater?.put(
169
- newChatDoc,
170
- kb.statementsMatching(undefined, undefined, undefined, newChatDoc),
171
- "text/turtle",
172
- function (uri2, ok, message) {
173
- if (ok) {
174
- resolve({
175
- ...newPaneOptions,
176
- newInstance,
177
- });
178
- } else {
179
- reject(
180
- new Error(
181
- "FAILED to save new chat channel at: " + uri2 + " : " + message
182
- )
183
- );
184
- }
185
- }
186
- );
187
- });
188
- }
189
-
190
- /**
191
- * Find (and optionally create) an individual chat between the current user and the given invitee
192
- * @param invitee - The person to chat with
193
- * @param createIfMissing - Whether the chat should be created, if missing
194
- * @returns null if missing, or a node referring to an already existing chat, or the newly created chat
195
- */
196
- async getChat(
197
- invitee: NamedNode,
198
- createIfMissing = true
199
- ): Promise<NamedNode | null> {
200
- const { me, chatContainer, exists } = await this.findChat(invitee);
201
- if (exists) {
202
- return new NamedNode(chatContainer.value + CHAT_LOCATION_IN_CONTAINER);
203
- }
204
-
205
- if (createIfMissing) {
206
- const chatThing = await this.createChatThing(chatContainer, me);
207
- await this.sendInvite(invitee, chatThing);
208
- await this.setAcl(chatContainer, me, invitee);
209
- await this.addToPrivateTypeIndex(chatThing, me);
210
- return chatThing;
211
- }
212
- return null;
213
- }
214
-
215
- private async sendInvite(invitee: NamedNode, chatThing: NamedNode) {
216
- await this.store.fetcher?.load(invitee.doc());
217
- const inviteeInbox = this.store.any(
218
- invitee,
219
- this.ns.ldp("inbox"),
220
- undefined,
221
- invitee.doc()
222
- );
223
- if (!inviteeInbox) {
224
- throw new Error(`Invitee inbox not found! ${invitee.value}`);
225
- }
226
- const inviteBody = `
227
- <> a <http://www.w3.org/ns/pim/meeting#LongChatInvite> ;
228
- ${this.ns.rdf("seeAlso")} <${chatThing.value}> .
229
- `;
230
-
231
- const inviteResponse = await this.store.fetcher?.webOperation(
232
- "POST",
233
- inviteeInbox.value,
234
- {
235
- data: inviteBody,
236
- contentType: "text/turtle",
237
- }
238
- );
239
- const locationStr = inviteResponse?.headers.get("location");
240
- if (!locationStr) {
241
- throw new Error(`Invite sending returned a ${inviteResponse?.status}`);
242
- }
243
- }
244
- }
@@ -1,14 +0,0 @@
1
- import { NamedNode } from "rdflib";
2
-
3
- export function determineChatContainer(
4
- invitee: NamedNode,
5
- podRoot: NamedNode
6
- ): NamedNode {
7
- // Create chat
8
- // See https://gitter.im/solid/chat-app?at=5f3c800f855be416a23ae74a
9
- const chatContainerStr = new URL(
10
- `IndividualChats/${new URL(invitee.value).host}/`,
11
- podRoot.value
12
- ).toString();
13
- return new NamedNode(chatContainerStr);
14
- }
@@ -1,66 +0,0 @@
1
- import { NamedNode, LiveStore } from "rdflib";
2
- import { ProfileLogic } from "../profile/ProfileLogic";
3
- import { SolidNamespace } from "../types";
4
- import { UtilityLogic } from "../util/UtilityLogic";
5
-
6
- /**
7
- * Inbox-related logic
8
- */
9
- export class InboxLogic {
10
- store: LiveStore;
11
- ns: SolidNamespace;
12
- profile: ProfileLogic;
13
- util: UtilityLogic;
14
-
15
- constructor(store: LiveStore, ns: SolidNamespace, profile: ProfileLogic, util: UtilityLogic) {
16
- this.store = store;
17
- this.ns = ns;
18
- this.profile = profile;
19
- this.util = util;
20
- }
21
-
22
- async getNewMessages(
23
- user?: NamedNode
24
- ): Promise<string[]> {
25
- if (!user) {
26
- user = await this.profile.loadMe();
27
- }
28
- const inbox = await this.profile.getMainInbox(user);
29
- const urls = await this.util.getContainerMembers(inbox.value);
30
- return urls.filter(url => !this.util.isContainer(url));
31
- }
32
- async createInboxFor(peerWebId: string, nick: string) {
33
- const myWebId: NamedNode = (await this.profile.loadMe());
34
- const podRoot: NamedNode = await this.profile.getPodRoot(myWebId);
35
- const ourInbox = `${podRoot.value}p2p-inboxes/${encodeURIComponent(nick)}/`;
36
- await this.util.createContainer(ourInbox);
37
- const aclDocUrl = await this.util.findAclDocUrl(ourInbox);
38
- await this.util.setSinglePeerAccess({
39
- ownerWebId: myWebId.value,
40
- peerWebId,
41
- accessToModes: 'acl:Append',
42
- target: ourInbox
43
- });
44
- return ourInbox;
45
- }
46
- async markAsRead(url: string, date: Date) {
47
- const downloaded = await this.util.underlyingFetch.fetch(url);
48
- if (downloaded.status !== 200) {
49
- throw new Error(`Not OK! ${url}`);
50
- }
51
- const archiveUrl = this.util.getArchiveUrl(url, date);
52
- const options = {
53
- method: 'PUT',
54
- body: await downloaded.text(),
55
- headers: [
56
- [ 'Content-Type', downloaded.headers.get('Content-Type') || 'application/octet-stream' ]
57
- ]
58
- };
59
- const uploaded = await this.util.underlyingFetch.fetch(archiveUrl, options);
60
- if (uploaded.status.toString()[0] === '2') {
61
- await this.store.fetcher?._fetch(url, {
62
- method: 'DELETE'
63
- });
64
- }
65
- }
66
- }
@@ -1,262 +0,0 @@
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
- ** It is important to distinquish `fetch`, a function provided by the browser
14
- ** and `Fetcher`, a helper object for the rdflib Store which turns it
15
- ** into a `ConnectedStore` or a `LiveStore`. A Fetcher object is
16
- ** available at store.fetcher, and `fetch` function at `store.fetcher._fetch`,
17
- */
18
-
19
- const ns: SolidNamespace = solidNamespace(rdf);
20
-
21
- export class SolidLogic {
22
- cache: {
23
- profileDocument: {
24
- [WebID: string]: NamedNode;
25
- };
26
- preferencesFile: {
27
- [WebID: string]: NamedNode;
28
- };
29
- };
30
-
31
- store: LiveStore;
32
- me: string | undefined;
33
- underlyingFetch: { fetch: (url: string, options?: any) => any };
34
-
35
- chat: ChatLogic;
36
- profile: ProfileLogic;
37
- authn: AuthnLogic;
38
- util: UtilityLogic;
39
-
40
- constructor(specialFetch: { fetch: (url: any, requestInit: any) => any }, session: Session) {
41
- // would xpect to be able to do it this way: but get TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation status: 999
42
- // this.store = new rdf.LiveStore({})
43
- // this.store.fetcher._fetch = fetch
44
- console.log("SolidLogic: Unique instance created. There should only be one of these.")
45
- this.store = rdf.graph() as LiveStore; // Make a Quad store
46
- rdf.fetcher(this.store, { fetch: specialFetch.fetch}); // Attach a web I/O module, store.fetcher
47
- this.store.updater = new rdf.UpdateManager(this.store); // Add real-time live updates store.updater
48
-
49
- this.store.features = [] // disable automatic node merging on store load
50
- this.cache = {
51
- profileDocument: {},
52
- preferencesFile: {},
53
- };
54
- this.underlyingFetch = { fetch: fetch }; // Note global one not the one passed
55
- this.authn = new SolidAuthnLogic(session);
56
- debug.log('SolidAuthnLogic initialized')
57
- this.profile = new ProfileLogic(this.store, ns, this.authn);
58
- this.chat = new ChatLogic(this.store, ns, this.profile);
59
- this.util = new UtilityLogic(this.store, ns, this.underlyingFetch);
60
- }
61
-
62
- findAclDocUrl(url: string) {
63
- return this.util.findAclDocUrl(url);
64
- }
65
-
66
- async loadProfile(me: NamedNode): Promise<NamedNode> {
67
- /*
68
- // console.log('loadProfile cache ', this.cache)
69
- if (this.cache.profileDocument[me.value]) {
70
- return this.cache.profileDocument[me.value];
71
- } @@ just use the cache in the store
72
- */
73
- console.log('loadProfile me ', me)
74
- const profileDocument = me.doc()
75
- try {
76
- await this.store.fetcher.load(profileDocument);
77
- return profileDocument;
78
- } catch (err) {
79
- const message = `Cannot load profile ${profileDocument} : ${err}`;
80
- throw new Error(message);
81
- }
82
- }
83
-
84
- async loadPreferences(me: NamedNode): Promise<NamedNode> {
85
- console.log('loadPreferences cache ', this.cache)
86
- if (this.cache.preferencesFile[me.value]) {
87
- return this.cache.preferencesFile[me.value];
88
- }
89
- await this.loadProfile(me) // Load pointer to pref file
90
- const preferencesFile = this.store.any(me, ns.space('preferencesFile'), null, me.doc());
91
-
92
- // console.log('this.store.any()', this.store.any())
93
- /**
94
- * Are we working cross-origin?
95
- * Returns True if we are in a webapp at an origin, and the file origin is different
96
- */
97
- function differentOrigin(): boolean {
98
- if (!preferencesFile) {
99
- return true;
100
- }
101
- return (
102
- `${window.location.origin}/` !== new URL(preferencesFile.value).origin
103
- );
104
- }
105
-
106
- if (!preferencesFile) {
107
- throw new Error(
108
- `Can't find a preference file pointer in profile ${me.doc()}`
109
- );
110
- }
111
-
112
- // //// Load preference file
113
- try {
114
- await this.store.fetcher.load(preferencesFile as NamedNode, {
115
- withCredentials: true,
116
- });
117
- } catch (err) {
118
- // Really important to look at why
119
- const status = err.status;
120
- debug.log(`HTTP status ${status} for preference file ${preferencesFile}`);
121
- if (status === 401) {
122
- throw new UnauthorizedError();
123
- }
124
- if (status === 403) {
125
- if (differentOrigin()) {
126
- throw new CrossOriginForbiddenError();
127
- }
128
- throw new SameOriginForbiddenError();
129
- }
130
- if (status === 404) {
131
- throw new NotFoundError(preferencesFile.value);
132
- }
133
- throw new FetchError(err.status, err.message);
134
- }
135
- return preferencesFile as NamedNode;
136
- }
137
-
138
- getTypeIndex(
139
- me: NamedNode | string,
140
- preferencesFile: NamedNode | string,
141
- isPublic: boolean
142
- ): NamedNode[] {
143
- // console.log('getTypeIndex', this.store.each(me, undefined, undefined, preferencesFile), isPublic, preferencesFile)
144
- return this.store.each(
145
- me as NamedNode,
146
- isPublic ? ns.solid("publicTypeIndex") : ns.solid("privateTypeIndex"),
147
- undefined,
148
- preferencesFile as NamedNode
149
- ) as NamedNode[];
150
- }
151
-
152
- getRegistrations(instance, theClass) {
153
- return this.store
154
- .each(undefined, ns.solid("instance"), instance)
155
- .filter((r) => {
156
- return this.store.holds(r, ns.solid("forClass"), theClass);
157
- });
158
- }
159
-
160
- load(doc: NamedNode | NamedNode[] | string) {
161
- return this.store.fetcher.load(doc);
162
- }
163
-
164
- async loadIndexes(
165
- me: NamedNode | string,
166
- publicProfile: NamedNode | string | null,
167
- preferencesFile: NamedNode | string | null,
168
- onWarning = async (_err: Error) => {
169
- return undefined;
170
- }
171
- ): Promise<{
172
- private: any;
173
- public: any;
174
- }> {
175
- let privateIndexes: any[] = [];
176
- let publicIndexes: any[] = [];
177
- if (publicProfile) {
178
- publicIndexes = this.getTypeIndex(me, publicProfile, true);
179
- try {
180
- await this.load(publicIndexes as NamedNode[]);
181
- } catch (err) {
182
- onWarning(new Error(`loadIndex: loading public type index(es) ${err}`));
183
- }
184
- }
185
- if (preferencesFile) {
186
- privateIndexes = this.getTypeIndex(me, preferencesFile, false);
187
- // console.log({ privateIndexes })
188
- if (privateIndexes.length === 0) {
189
- await onWarning(
190
- new Error(
191
- `Your preference file ${preferencesFile} does not point to a private type index.`
192
- )
193
- );
194
- } else {
195
- try {
196
- await this.load(privateIndexes);
197
- } catch (err) {
198
- onWarning(
199
- new Error(`loadIndex: loading private type index(es) ${err}`)
200
- );
201
- }
202
- }
203
- // } else {
204
- // debug.log(
205
- // 'We know your preference file is not available, so we are not bothering with private type indexes.'
206
- // )
207
- }
208
-
209
- return {
210
- private: privateIndexes,
211
- public: publicIndexes,
212
- };
213
- }
214
-
215
- async createEmptyRdfDoc(doc: NamedNode, comment: string) {
216
- await this.store.fetcher.webOperation("PUT", doc.uri, {
217
- data: `# ${new Date()} ${comment}
218
- `,
219
- contentType: "text/turtle",
220
- });
221
- }
222
-
223
- // @@@@ use the one in rdflib.js when it is available and delete this
224
- updatePromise(
225
- del: Array<Statement>,
226
- ins: Array<Statement> = []
227
- ): Promise<void> {
228
- return new Promise((resolve, reject) => {
229
- this.store.updater.update(del, ins, function (_uri, ok, errorBody) {
230
- if (!ok) {
231
- reject(new Error(errorBody));
232
- } else {
233
- resolve();
234
- }
235
- }); // callback
236
- }); // promise
237
- }
238
-
239
- isContainer(url: string) {
240
- return this.util.isContainer(url);
241
- }
242
-
243
- getContainerElements(containerNode: NamedNode): NamedNode[] {
244
- return this.util.getContainerElements(containerNode);
245
- }
246
-
247
- getContainerMembers(containerUrl: string): Promise<string[]> {
248
- return this.util.getContainerMembers(containerUrl);
249
- }
250
-
251
- async recursiveDelete(url: string) {
252
- return this.util.recursiveDelete(url);
253
- }
254
-
255
- clearStore() {
256
- return this.util.clearStore();
257
- }
258
-
259
- async fetch(url: string, options?: any) {
260
- return this.underlyingFetch.fetch(url, options);
261
- }
262
- }
@@ -1,44 +0,0 @@
1
- import { NamedNode, LiveStore } from "rdflib";
2
- import { AuthnLogic, SolidNamespace } from "../types";
3
-
4
- export class ProfileLogic {
5
- store: LiveStore;
6
- ns: SolidNamespace;
7
- authn: AuthnLogic;
8
-
9
- constructor(store: LiveStore, ns: SolidNamespace, authn: AuthnLogic) {
10
- this.store = store;
11
- this.ns = ns;
12
- this.authn = authn;
13
- }
14
-
15
- async loadMe(): Promise<NamedNode> {
16
- const me = this.authn.currentUser();
17
- if (me === null) {
18
- throw new Error("Current user not found! Not logged in?");
19
- }
20
- await this.store.fetcher?.load(me.doc());
21
- return me;
22
- }
23
-
24
- getPodRoot(user: NamedNode): NamedNode {
25
- const podRoot = this.findStorage(user);
26
- if (!podRoot) {
27
- throw new Error("User pod root not found!");
28
- }
29
- return podRoot as NamedNode;
30
- }
31
-
32
- async getMainInbox(user: NamedNode): Promise<NamedNode> {
33
- await this.store.fetcher?.load(user);
34
- const mainInbox = this.store.any(user, this.ns.ldp("inbox"), undefined, user.doc());
35
- if (!mainInbox) {
36
- throw new Error("User main inbox not found!");
37
- }
38
- return mainInbox as NamedNode;
39
- }
40
-
41
- private findStorage(me: NamedNode) {
42
- return this.store.any(me, this.ns.space("storage"), undefined, me.doc());
43
- }
44
- }