solid-logic 1.3.17-8a332867 → 1.3.17-a27c1702

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 (122) 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 +152 -119
  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/inbox/inboxLogic.d.ts +7 -0
  14. package/lib/inbox/inboxLogic.d.ts.map +1 -0
  15. package/lib/inbox/{InboxLogic.js → inboxLogic.js} +58 -64
  16. package/lib/inbox/inboxLogic.js.map +1 -0
  17. package/lib/index.d.ts +18 -9
  18. package/lib/index.d.ts.map +1 -1
  19. package/lib/index.js +86 -34
  20. package/lib/index.js.map +1 -1
  21. package/lib/logic/CustomError.d.ts +4 -0
  22. package/lib/logic/CustomError.d.ts.map +1 -1
  23. package/lib/logic/CustomError.js +17 -1
  24. package/lib/logic/CustomError.js.map +1 -1
  25. package/lib/logic/SolidLogic.d.ts +8 -34
  26. package/lib/logic/SolidLogic.d.ts.map +1 -1
  27. package/lib/logic/SolidLogic.js +16 -250
  28. package/lib/logic/SolidLogic.js.map +1 -1
  29. package/lib/logic/solidLogicSingleton.js +1 -1
  30. package/lib/logic/solidLogicSingleton.js.map +1 -1
  31. package/lib/logic/solidLogicSingletonNew.d.ts +80 -0
  32. package/lib/logic/solidLogicSingletonNew.d.ts.map +1 -0
  33. package/lib/logic/solidLogicSingletonNew.js +238 -0
  34. package/lib/logic/solidLogicSingletonNew.js.map +1 -0
  35. package/lib/profile/profileLogic.d.ts +13 -0
  36. package/lib/profile/profileLogic.d.ts.map +1 -0
  37. package/lib/profile/profileLogic.js +268 -0
  38. package/lib/profile/profileLogic.js.map +1 -0
  39. package/lib/typeIndex/typeIndexLogic.d.ts +31 -21
  40. package/lib/typeIndex/typeIndexLogic.d.ts.map +1 -1
  41. package/lib/typeIndex/typeIndexLogic.js +650 -295
  42. package/lib/typeIndex/typeIndexLogic.js.map +1 -1
  43. package/lib/types.d.ts +17 -0
  44. package/lib/types.d.ts.map +1 -1
  45. package/lib/util/containerLogic.d.ts +11 -0
  46. package/lib/util/containerLogic.d.ts.map +1 -0
  47. package/lib/{profile/ProfileLogic.js → util/containerLogic.js} +53 -44
  48. package/lib/util/containerLogic.js.map +1 -0
  49. package/lib/util/ns.d.ts +2 -0
  50. package/lib/util/ns.d.ts.map +1 -0
  51. package/lib/util/ns.js +34 -0
  52. package/lib/util/ns.js.map +1 -0
  53. package/lib/util/utilityLogic.d.ts +15 -0
  54. package/lib/util/utilityLogic.d.ts.map +1 -0
  55. package/lib/util/utilityLogic.js +272 -0
  56. package/lib/util/utilityLogic.js.map +1 -0
  57. package/lib/util/utils.d.ts +8 -0
  58. package/lib/util/utils.d.ts.map +1 -0
  59. package/lib/util/utils.js +48 -0
  60. package/lib/util/utils.js.map +1 -0
  61. package/package.json +3 -1
  62. package/src/acl/aclLogic.ts +136 -118
  63. package/src/authn/SolidAuthnLogic.ts +3 -2
  64. package/src/chat/chatLogic.ts +225 -0
  65. package/src/inbox/inboxLogic.ts +57 -0
  66. package/src/index.ts +139 -27
  67. package/src/logic/CustomError.ts +5 -1
  68. package/src/logic/SolidLogic.ts +30 -211
  69. package/src/logic/solidLogicSingleton.ts +1 -1
  70. package/src/logic/solidLogicSingletonNew.ts +239 -0
  71. package/src/profile/profileLogic.ts +134 -0
  72. package/src/typeIndex/typeIndexLogic.ts +417 -153
  73. package/src/types.ts +7 -3
  74. package/src/util/containerLogic.ts +54 -0
  75. package/src/util/ns.ts +5 -0
  76. package/src/util/utilityLogic.ts +155 -0
  77. package/src/util/utils.ts +52 -0
  78. package/test/aclLogic.test.ts +13 -4
  79. package/test/chatLogic.test.ts +70 -71
  80. package/test/container.test.ts +56 -0
  81. package/test/helpers/dataSetup.ts +134 -0
  82. package/test/helpers/setup.ts +1 -0
  83. package/test/inboxLogic.test.ts +39 -38
  84. package/test/logic.test.ts +10 -9
  85. package/test/profileLogic.test.ts +246 -0
  86. package/test/typeIndexLogic.test.ts +49 -22
  87. package/test/typeIndexLogicPart2.test.ts +485 -0
  88. package/test/utilityLogic.test.ts +172 -126
  89. package/test/utils.test.ts +32 -0
  90. package/lib/chat/ChatLogic.d.ts +0 -26
  91. package/lib/chat/ChatLogic.d.ts.map +0 -1
  92. package/lib/chat/ChatLogic.js.map +0 -1
  93. package/lib/chat/determineChatContainer.d.ts +0 -3
  94. package/lib/chat/determineChatContainer.d.ts.map +0 -1
  95. package/lib/chat/determineChatContainer.js +0 -12
  96. package/lib/chat/determineChatContainer.js.map +0 -1
  97. package/lib/discovery/discoveryLogic.d.ts +0 -40
  98. package/lib/discovery/discoveryLogic.d.ts.map +0 -1
  99. package/lib/discovery/discoveryLogic.js +0 -494
  100. package/lib/discovery/discoveryLogic.js.map +0 -1
  101. package/lib/inbox/InboxLogic.d.ts +0 -18
  102. package/lib/inbox/InboxLogic.d.ts.map +0 -1
  103. package/lib/inbox/InboxLogic.js.map +0 -1
  104. package/lib/profile/ProfileLogic.d.ts +0 -13
  105. package/lib/profile/ProfileLogic.d.ts.map +0 -1
  106. package/lib/profile/ProfileLogic.js.map +0 -1
  107. package/lib/util/UtilityLogic.d.ts +0 -33
  108. package/lib/util/UtilityLogic.d.ts.map +0 -1
  109. package/lib/util/UtilityLogic.js +0 -240
  110. package/lib/util/UtilityLogic.js.map +0 -1
  111. package/lib/util/uri.d.ts +0 -3
  112. package/lib/util/uri.d.ts.map +0 -1
  113. package/lib/util/uri.js +0 -9
  114. package/lib/util/uri.js.map +0 -1
  115. package/src/chat/ChatLogic.ts +0 -244
  116. package/src/chat/determineChatContainer.ts +0 -14
  117. package/src/discovery/discoveryLogic.ts +0 -310
  118. package/src/inbox/InboxLogic.ts +0 -66
  119. package/src/profile/ProfileLogic.ts +0 -44
  120. package/src/util/UtilityLogic.ts +0 -161
  121. package/src/util/uri.ts +0 -5
  122. package/test/discoveryLogic.test.ts +0 -740
@@ -1,310 +0,0 @@
1
- import { NamedNode, Namespace, LiveStore, sym, st } from "rdflib";
2
- // import * as debug from '../util/debug'
3
- // import { getContainerMembers } from '../util/UtilityLogic'
4
- import { solidLogicSingleton } from "../logic/solidLogicSingleton"
5
- import { newThing } from "../util/uri"
6
- const { authn } = solidLogicSingleton
7
- const { currentUser } = authn
8
-
9
- type TypeIndexScope = { label: string, index: NamedNode, agent: NamedNode } ;
10
- type ScopedApp = { instance: NamedNode, scope: TypeIndexScope }
11
-
12
- const ns = {
13
- dct: Namespace('http://purl.org/dc/terms/'),
14
- ldp: Namespace('http://www.w3.org/ns/ldp#'),
15
- meeting: Namespace('http://www.w3.org/ns/pim/meeting#'),
16
- rdf: Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#'),
17
- schema: Namespace('http://schema.org/'),
18
- solid: Namespace('http://www.w3.org/ns/solid/terms#'),
19
- space: Namespace('http://www.w3.org/ns/pim/space#'),
20
- stat: Namespace('http://www.w3.org/ns/posix/stat#'),
21
- vcard: Namespace('http://www.w3.org/2006/vcard/ns#'),
22
- wf: Namespace('http://www.w3.org/2005/01/wf/flow#'),
23
- xsd: Namespace('http://www.w3.org/2001/XMLSchema#')
24
-
25
- }
26
-
27
- /** Create a resource if it really does not exist
28
- * Be absolutely sure something does not exist before creating a new empty file
29
- * as otherwise existing could be deleted.
30
- * @param doc {NamedNode} - The resource
31
- */
32
- export async function loadOrCreateIfNotExists (store: LiveStore, doc: NamedNode) {
33
- let response
34
- // console.log('@@ loadOrCreateIfNotExists doc ', doc)
35
- try {
36
- response = await store.fetcher.load(doc)
37
- } catch (err) {
38
- if (err.response.status === 404) {
39
- // console.log('createIfNotExists doc does NOT exist, will create: ' + doc)
40
- try {
41
- store.fetcher.webOperation('PUT', doc, {data: '', contentType: 'text/turtle'})
42
- } catch (err) {
43
- const msg = 'createIfNotExists: PUT FAILED: ' + doc + ': ' + err
44
- // console.log(msg)
45
- throw new Error(msg)
46
- }
47
- delete store.fetcher.requested[doc.uri] // delete cached 404 error
48
- // console.log('createIfNotExists doc created ok ' + doc)
49
- } else {
50
- const msg = 'createIfNotExists doc load error NOT 404: ' + doc + ': ' + err
51
- // console.log(msg)
52
- throw new Error(msg) // @@ add nested errors
53
- }
54
- }
55
- // console.log('createIfNotExists doc exists, all good ' + doc)
56
- return response
57
- }
58
-
59
- export function suggestPreferencesFile (me:NamedNode) {
60
- const stripped = me.uri.replace('/profile/', '/').replace('/public/', '/')
61
- // const stripped = me.uri.replace(\/[p|P]rofile/\g, '/').replace(\/[p|P]ublic/\g, '/')
62
- const folderURI = stripped.split('/').slice(0,-1).join('/') + '/Settings/'
63
- const fileURI = folderURI + 'Preferences.ttl'
64
- return sym(fileURI)
65
- }
66
-
67
- export function suggestPublicTypeIndex (me:NamedNode) {
68
- return sym(me.doc().dir()?.uri + 'publicTypeIndex.ttl')
69
- }
70
- // Note this one is based off the pref file not the profile
71
-
72
- export function suggestPrivateTypeIndex (preferencesFile:NamedNode) {
73
- return sym(preferencesFile.doc().dir()?.uri + 'privateTypeIndex.ttl')
74
- }
75
- /* Follow link from this doc to another thing, or else make a new link
76
- **
77
- ** return: null no ld one and failed to make a new one
78
- */
79
- export async function followOrCreateLink (store: LiveStore, subject: NamedNode, predicate: NamedNode,
80
- object: NamedNode, doc:NamedNode):Promise<NamedNode | null> {
81
- await store.fetcher.load(doc)
82
- const result = store.any(subject, predicate, null, doc)
83
- // console.log('@@ followOrCreateLink result ', result)
84
-
85
- if (result) return result as NamedNode
86
- if (!store.updater.editable(doc)) {
87
- // console.log(`followOrCreateLink: Can't modify ${doc} so can't make new link to ${object}.`)
88
- // console.log('followOrCreateLink @@ connectedStatements', store.connectedStatements(subject))
89
- return null
90
- }
91
- try {
92
- await store.updater.update([], [ st(subject, predicate, object, doc)])
93
- } catch (err) {
94
- console.warn(`followOrCreateLink: Error making link in ${doc} to ${object}: ${err}`)
95
- return null
96
- }
97
-
98
- // console.log(`Success making link in ${doc} to ${object}` )
99
-
100
- try {
101
- await loadOrCreateIfNotExists(store, object)
102
- // store.fetcher.webOperation('PUT', object, { data: '', contentType: 'text/turtle'})
103
- } catch (err) {
104
- console.warn(`followOrCreateLink: Error loading or saving new linked document: ${object}: ${err}`)
105
- }
106
- // console.log(`followOrCreateLink: Success loading or saving new linked document: ${object}.`)
107
- return object
108
- }
109
-
110
- export async function loadProfile (store: LiveStore, user: NamedNode) {
111
- // console.log(' @@ loadProfile: user', user)
112
- if (!user) {
113
- throw new Error(`loadProfile: no user given.`)
114
- }
115
- // try {
116
- await store.fetcher.load(user.doc())
117
- // } catch (err) {
118
- // throw new Error(`Unable to load profile of user ${user}: ${err}`)
119
- //}
120
- return user.doc()
121
- }
122
-
123
- export async function loadPreferences (store: LiveStore, user: NamedNode): Promise <NamedNode | undefined > {
124
- // console.log('loadPreferences @@ user', user)
125
- await loadProfile(store as LiveStore, user)
126
-
127
- const possiblePreferencesFile = suggestPreferencesFile(user)
128
-
129
- const preferencesFile = await followOrCreateLink(store, user, ns.space('preferencesFile') as NamedNode, possiblePreferencesFile, user.doc())
130
- // const preferencesFile = store.any(user, ns.space('preferencesFile'), undefined, profile)
131
-
132
- // console.log('loadPreferences @@ pref file', preferencesFile)
133
- if (!preferencesFile) {
134
- const message = `User ${user} has no pointer in profile to preferences file.`
135
- console.warn(message)
136
- // throw new Error()
137
- return undefined
138
- }
139
- try {
140
- await store.fetcher.load(preferencesFile as NamedNode)
141
- } catch (err) { // Mabeb a permission propblem or origin problem
142
- return undefined
143
- // throw new Error(`Unable to load preferences file ${preferencesFile} of user <${user}>: ${err}`)
144
- }
145
- return preferencesFile as NamedNode
146
- }
147
-
148
- export async function loadTypeIndexesFor (store: LiveStore, user:NamedNode): Promise<Array<TypeIndexScope>> {
149
- // console.log('@@ loadTypeIndexesFor user', user)
150
- if (!user) throw new Error(`loadTypeIndexesFor: No user given`)
151
- const profile = await loadProfile(store, user)
152
-
153
- const suggestion = suggestPublicTypeIndex(user)
154
-
155
- const publicTypeIndex = await followOrCreateLink(store, user, ns.solid('publicTypeIndex') as NamedNode, suggestion, profile)
156
-
157
- // const publicTypeIndex = store.any(user, ns.solid('publicTypeIndex'), undefined, profile)
158
- // console.log('@@ loadTypeIndexesFor publicTypeIndex', publicTypeIndex)
159
-
160
- const publicScopes = publicTypeIndex ? [ { label: 'public', index: publicTypeIndex as NamedNode, agent: user } ] : []
161
-
162
- let preferencesFile
163
- try {
164
- preferencesFile = await loadPreferences(store, user)
165
- } catch (err) {
166
- preferencesFile = null
167
- }
168
-
169
- let privateScopes
170
- if (preferencesFile) { // watch out - can be in either as spec was not clear. Legacy is profile.
171
- // If there is a legacy one linked from the profile, use that.
172
- // Otherwiae use or make one linked from Preferences
173
- const suggestedPrivateTypeIndex = suggestPrivateTypeIndex(preferencesFile)
174
-
175
- const privateTypeIndex = store.any(user, ns.solid('privateTypeIndex'), undefined, profile) ||
176
-
177
- await followOrCreateLink(store, user, ns.solid('privateTypeIndex') as NamedNode, suggestedPrivateTypeIndex, preferencesFile);
178
-
179
- privateScopes = privateTypeIndex ? [ { label: 'private', index: privateTypeIndex as NamedNode, agent: user } ] : []
180
- } else {
181
- privateScopes = []
182
- }
183
- const scopes = publicScopes.concat(privateScopes)
184
- if (scopes.length === 0) return scopes
185
- const files = scopes.map(scope => scope.index)
186
- // console.log('@@ loadTypeIndexesFor files ', files)
187
- try {
188
- await store.fetcher.load(files)
189
- } catch (err) {
190
- console.warn('Problems loading type index: ', err)
191
- }
192
- return scopes
193
- }
194
-
195
- export async function loadCommunityTypeIndexes (store:LiveStore, user:NamedNode): Promise<TypeIndexScope[][]> {
196
- const preferencesFile = await loadPreferences(store, user)
197
- if (preferencesFile) { // For now, pick up communities as simple links from the preferences file.
198
- const communities = store.each(user, ns.solid('community'), undefined, preferencesFile as NamedNode)
199
- // console.log('loadCommunityTypeIndexes communities: ',communities)
200
- let result = []
201
- for (const org of communities) {
202
- result = result.concat(await loadTypeIndexesFor(store, org as NamedNode) as any)
203
- }
204
- // const communityTypeIndexesPromises = communities.map(async community => await loadTypeIndexesFor(store, community as NamedNode))
205
- // const result1 = Promise.all(communityTypeIndexesPromises)
206
- return result
207
- }
208
- return [] // No communities
209
- }
210
-
211
- export async function loadAllTypeIndexes (store:LiveStore, user:NamedNode) {
212
- return (await loadTypeIndexesFor(store, user)).concat((await loadCommunityTypeIndexes(store, user)).flat())
213
- }
214
-
215
- // Utility: remove duplicates from Array of NamedNodes
216
-
217
- export function uniqueNodes (arr: NamedNode[]): NamedNode[] {
218
- const uris = arr.map(x => x.uri)
219
- const set = new Set(uris)
220
- const uris2 = Array.from(set)
221
- const arr2 = uris2.map(u => new NamedNode(u))
222
- return arr2 // Array.from(new Set(arr.map(x => x.uri))).map(u => sym(u))
223
- }
224
-
225
- export async function getScopedAppsFromIndex (store, scope, theClass: NamedNode) {
226
- // console.log(`getScopedAppsFromIndex agent ${scope.agent} index: ${scope.index}` )
227
- const index = scope.index
228
- const registrations = store.each(undefined, ns.solid('forClass'), theClass, index)
229
- // console.log(' registrations', registrations )
230
-
231
- const directInstances = registrations.map(reg => store.each(reg as NamedNode, ns.solid('instance'), null, index)).flat()
232
- // console.log(' directInstances', directInstances )
233
- let instances = uniqueNodes(directInstances)
234
-
235
- /*
236
- let instanceContainers = []
237
- for (const reg of registrations) {
238
- const cont = store.any(reg as NamedNode, ns.solid('instanceContainer'), null, index)
239
- if (cont) {
240
- // console.log(' @@ getScopedAppsFromIndex got one: ', cont)
241
- instanceContainers.push(cont)
242
- }
243
- }
244
- */
245
- const instanceContainers = registrations.map(
246
- reg => store.each(reg as NamedNode, ns.solid('instanceContainer'), null, index)).flat()
247
-
248
- // instanceContainers may be deprocatable if no one has used them
249
-
250
- const containers = uniqueNodes(instanceContainers)
251
- for (let i = 0; i < containers.length; i++) {
252
- const cont = containers[i]
253
- await store.fetcher.load(cont)
254
- const contents = store.each(cont, ns.ldp('contains'), null, cont)
255
- // if (contents.length) console.log('getScopedAppsFromIndex @@ instanceContainer contents:', contents)
256
- instances = instances.concat(contents)
257
- }
258
- return instances.map(instance => { return {instance, scope}})
259
- }
260
-
261
-
262
- export async function getScopedAppInstances (store:LiveStore, klass: NamedNode, user: NamedNode):Promise<ScopedApp[]> {
263
- // console.log('getScopedAppInstances @@ ' + user)
264
- const scopes = await loadAllTypeIndexes(store, user)
265
- let scopedApps = []
266
- for (const scope of scopes) {
267
- const scopedApps0 = await getScopedAppsFromIndex(store, scope, klass) as any
268
- scopedApps = scopedApps.concat(scopedApps0)
269
- }
270
- return scopedApps
271
- }
272
- // This is the function signature which used to be in solid-ui/logic
273
- // Recommended to use getScopedAppInstances instead as it provides more information.
274
- //
275
- export async function getAppInstances (store:LiveStore, klass: NamedNode): Promise<NamedNode[]> {
276
- const user = currentUser()
277
- if (!user) throw new Error('getAppInstances: Must be logged in to find apps.')
278
- const scopedAppInstances = await getScopedAppInstances(store, klass, user)
279
- return scopedAppInstances.map(scoped => scoped.instance)
280
- }
281
- /**
282
- * Register a new app in a type index
283
- * used in chat in bookmark.js (solid-ui)
284
- * Returns the registration object if successful else null
285
- */
286
- export async function registerInstanceInTypeIndex (
287
- store:LiveStore,
288
- instance: NamedNode,
289
- index: NamedNode,
290
- theClass: NamedNode,
291
- // agent: NamedNode
292
- ): Promise<NamedNode | null> {
293
- const registration = newThing(index)
294
- const ins = [
295
- // See https://github.com/solid/solid/blob/main/proposals/data-discovery.md
296
- st(registration, ns.rdf('type'), ns.solid('TypeRegistration'), index),
297
- st(registration, ns.solid('forClass'), theClass, index),
298
- st(registration, ns.solid('instance'), instance, index)
299
- ]
300
- try {
301
- console.log('patching index', ins)
302
- await store.updater.update([], ins)
303
- } catch (err) {
304
- const msg = `Unable to register ${instance} in index ${index}: ${err}`
305
- console.warn(msg)
306
- return null
307
- }
308
- return registration
309
- }
310
- // ENDS
@@ -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,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
- }
@@ -1,161 +0,0 @@
1
- import { NamedNode, Statement, sym, LiveStore } from "rdflib";
2
- import { SolidNamespace } from "../types";
3
-
4
- export const ACL_LINK = sym(
5
- "http://www.iana.org/assignments/link-relations/acl"
6
- );
7
-
8
- /**
9
- * Utility-related logic
10
- */
11
- export class UtilityLogic {
12
- store: LiveStore;
13
- ns: SolidNamespace;
14
- underlyingFetch: { fetch: (url: string, options?: any) => any };
15
-
16
- constructor(store: LiveStore, ns: SolidNamespace, underlyingFetch: { fetch: (url: string, options?: any) => any }) {
17
- this.store = store;
18
- this.ns = ns;
19
- this.underlyingFetch = underlyingFetch;
20
- }
21
-
22
- async findAclDocUrl(url: string) {
23
- const doc = this.store.sym(url);
24
- await this.store.fetcher?.load(doc);
25
- const docNode = this.store.any(doc, ACL_LINK);
26
- if (!docNode) {
27
- throw new Error(`No ACL link discovered for ${url}`);
28
- }
29
- return docNode.value;
30
- }
31
-
32
- // Copied from https://github.com/solidos/web-access-control-tests/blob/v3.0.0/test/surface/delete.test.ts#L5
33
- async setSinglePeerAccess(options: {
34
- ownerWebId: string,
35
- peerWebId: string,
36
- accessToModes?: string,
37
- defaultModes?: string,
38
- target: string
39
- }) {
40
- let str = [
41
- '@prefix acl: <http://www.w3.org/ns/auth/acl#>.',
42
- '',
43
- `<#alice> a acl:Authorization;\n acl:agent <${options.ownerWebId}>;`,
44
- ` acl:accessTo <${options.target}>;`,
45
- ` acl:default <${options.target}>;`,
46
- ' acl:mode acl:Read, acl:Write, acl:Control.',
47
- ''
48
- ].join('\n')
49
- if (options.accessToModes) {
50
- str += [
51
- '<#bobAccessTo> a acl:Authorization;',
52
- ` acl:agent <${options.peerWebId}>;`,
53
- ` acl:accessTo <${options.target}>;`,
54
- ` acl:mode ${options.accessToModes}.`,
55
- ''
56
- ].join('\n')
57
- }
58
- if (options.defaultModes) {
59
- str += [
60
- '<#bobDefault> a acl:Authorization;',
61
- ` acl:agent <${options.peerWebId}>;`,
62
- ` acl:default <${options.target}>;`,
63
- ` acl:mode ${options.defaultModes}.`,
64
- ''
65
- ].join('\n')
66
- }
67
- const aclDocUrl = await this.findAclDocUrl(options.target);
68
- return this.underlyingFetch.fetch(aclDocUrl, {
69
- method: 'PUT',
70
- body: str,
71
- headers: [
72
- [ 'Content-Type', 'text/turtle' ]
73
- ]
74
- });
75
- }
76
-
77
- async loadDoc(doc: NamedNode): Promise<void> {
78
- // Load a document into the knowledge base (fetcher.store)
79
- // withCredentials: Web arch should let us just load by turning off creds helps CORS
80
- // reload: Gets around a specific old Chrome bug caching/origin/cors
81
- // console.log('loading', profileDocument)
82
- if (!this.store.fetcher) {
83
- throw new Error("Cannot load doc, have no fetcher");
84
- }
85
- await this.store.fetcher.load(doc, {
86
- withCredentials: false, // @@ BUT this won't work when logged in an accessing private stuff!
87
- cache: "reload",
88
- });
89
- // console.log('loaded', profileDocument, this.store)
90
- }
91
-
92
- isContainer(url: string) {
93
- return url.substr(-1) === "/";
94
- }
95
-
96
- async createContainer(url: string) {
97
- if (!this.isContainer(url)) {
98
- throw new Error(`Not a container URL ${url}`);
99
- }
100
- // Copied from https://github.com/solidos/solid-crud-tests/blob/v3.1.0/test/surface/create-container.test.ts#L56-L64
101
- const result = await this.underlyingFetch.fetch(url, {
102
- method: "PUT",
103
- headers: {
104
- "Content-Type": "text/turtle",
105
- "If-None-Match": "*",
106
- Link: '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"', // See https://github.com/solidos/node-solid-server/issues/1465
107
- },
108
- body: " ", // work around https://github.com/michielbdejong/community-server/issues/4#issuecomment-776222863
109
- });
110
- if (result.status.toString()[0] !== '2') {
111
- throw new Error(`Not OK: got ${result.status} response while creating container at ${url}`);
112
- }
113
- }
114
-
115
- getContainerElements(containerNode: NamedNode): NamedNode[] {
116
- return this.store
117
- .statementsMatching(
118
- containerNode,
119
- this.store.sym("http://www.w3.org/ns/ldp#contains"),
120
- undefined,
121
- containerNode.doc()
122
- )
123
- .map((st: Statement) => st.object as NamedNode);
124
- }
125
-
126
- async getContainerMembers(containerUrl: string): Promise<string[]> {
127
- const containerNode = this.store.sym(containerUrl);
128
- await this.store.fetcher?.load(containerNode);
129
- const nodes = this.getContainerElements(containerNode);
130
- return nodes.map(node => node.value);
131
- }
132
-
133
- async recursiveDelete(url: string) {
134
- try {
135
- if (this.isContainer(url)) {
136
- const aclDocUrl = await this.findAclDocUrl(url);
137
- await this.underlyingFetch.fetch(aclDocUrl, { method: "DELETE" });
138
- const containerMembers = await this.getContainerMembers(url);
139
- await Promise.all(
140
- containerMembers.map((url) => this.recursiveDelete(url))
141
- );
142
- }
143
- return this.underlyingFetch.fetch(url, { method: "DELETE" });
144
- } catch (e) {
145
- // console.log(`Please manually remove ${url} from your system under test.`, e);
146
- }
147
- }
148
-
149
- clearStore() {
150
- this.store.statements.slice().forEach(this.store.remove.bind(this.store));
151
- }
152
-
153
- getArchiveUrl(baseUrl: string, date: Date) {
154
- const year = date.getUTCFullYear();
155
- const month = ('0' + (date.getUTCMonth()+1)).slice(-2);
156
- const day = ('0' + (date.getUTCDate())).slice(-2);
157
- const parts = baseUrl.split('/');
158
- const filename = parts[parts.length -1 ];
159
- return new URL(`./archive/${year}/${month}/${day}/${filename}`, baseUrl).toString();
160
- }
161
- }
package/src/util/uri.ts DELETED
@@ -1,5 +0,0 @@
1
- import { NamedNode, sym } from "rdflib";
2
-
3
- export function newThing(doc: NamedNode): NamedNode {
4
- return sym(doc.uri + "#" + "id" + ("" + Date.now()));
5
- }