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.
Files changed (143) 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 +7 -1
  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 +38 -33
  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 +14 -13
  35. package/lib/inbox/InboxLogic.js.map +1 -1
  36. package/lib/index.d.ts +9 -72
  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/{index-alain.d.ts → logic/SolidLogic.d.ts} +7 -33
  49. package/lib/logic/SolidLogic.d.ts.map +1 -0
  50. package/lib/{index-alain.js → logic/SolidLogic.js} +17 -99
  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 +10 -8
  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 +10 -8
  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 +18 -16
  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 +13 -13
  87. package/src/chat/determineChatContainer.ts +0 -0
  88. package/src/inbox/InboxLogic.ts +3 -15
  89. package/src/index.ts +18 -307
  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 +4 -5
  95. package/src/typeIndex/typeIndexLogic.ts +170 -0
  96. package/src/types.ts +41 -0
  97. package/src/util/UtilityLogic.ts +4 -16
  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/index-alain.d.ts.map +0 -1
  129. package/lib/index-alain.js.map +0 -1
  130. package/lib/uri.d.ts.map +0 -1
  131. package/lib/uri.js.map +0 -1
  132. package/lib/util/UtilityLogic-alain.d.ts +0 -32
  133. package/lib/util/UtilityLogic-alain.d.ts.map +0 -1
  134. package/lib/util/UtilityLogic-alain.js +0 -248
  135. package/lib/util/UtilityLogic-alain.js.map +0 -1
  136. package/lib/util/unit.test.d.ts +0 -2
  137. package/lib/util/unit.test.d.ts.map +0 -1
  138. package/lib/util/unit.test.js +0 -200
  139. package/lib/util/unit.test.js.map +0 -1
  140. package/src/authn/NoAuthnLogic.ts +0 -16
  141. package/src/authn/index.ts +0 -5
  142. package/src/index-alain.txt +0 -316
  143. package/src/util/UtilityLogic-alain.txt +0 -181
@@ -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
- }