@noeldemartin/solid-utils 0.1.1-next.2c9924cd927c0b708aef3ff4426982469f3b86fd → 0.1.1-next.43e8854d84ad29f71c5320cc51f7018a09430819
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/noeldemartin-solid-utils.cjs.js +1 -1
- package/dist/noeldemartin-solid-utils.cjs.js.map +1 -1
- package/dist/noeldemartin-solid-utils.d.ts +26 -5
- package/dist/noeldemartin-solid-utils.esm.js +1 -1
- package/dist/noeldemartin-solid-utils.esm.js.map +1 -1
- package/dist/noeldemartin-solid-utils.umd.js +90 -0
- package/dist/noeldemartin-solid-utils.umd.js.map +1 -0
- package/package.json +6 -8
- package/src/errors/UnsupportedAuthorizationProtocolError.ts +16 -0
- package/src/errors/index.ts +2 -1
- package/src/helpers/auth.ts +10 -4
- package/src/helpers/io.ts +78 -47
- package/src/helpers/jsonld.ts +21 -1
- package/src/helpers/wac.ts +12 -2
- package/src/models/SolidDocument.ts +5 -0
- package/src/types/n3.d.ts +9 -0
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noeldemartin/solid-utils",
|
|
3
|
-
"version": "0.1.1-next.
|
|
3
|
+
"version": "0.1.1-next.43e8854d84ad29f71c5320cc51f7018a09430819",
|
|
4
4
|
"description": "My JavaScript utilities for Solid",
|
|
5
5
|
"main": "dist/noeldemartin-solid-utils.cjs.js",
|
|
6
6
|
"module": "dist/noeldemartin-solid-utils.esm.js",
|
|
7
|
+
"browser": "dist/noeldemartin-solid-utils.umd.js",
|
|
7
8
|
"types": "dist/noeldemartin-solid-utils.d.ts",
|
|
8
9
|
"scripts": {
|
|
9
10
|
"build": "rm dist -rf && npm run build:js && npm run build:types",
|
|
@@ -29,13 +30,12 @@
|
|
|
29
30
|
"homepage": "https://github.com/noeldemartin/solid-utils",
|
|
30
31
|
"dependencies": {
|
|
31
32
|
"@babel/runtime": "^7.14.0",
|
|
32
|
-
"@noeldemartin/utils": "^0.
|
|
33
|
+
"@noeldemartin/solid-utils-external": "^0.1.0",
|
|
34
|
+
"@noeldemartin/utils": "0.2.1-next.6b17f2fc333f9389f0f4ebca96f962cd2172ee26",
|
|
33
35
|
"@types/rdf-js": "^4.0.1",
|
|
34
36
|
"core-js": "^3.12.1",
|
|
35
37
|
"jest-diff": "^26.6.2",
|
|
36
|
-
"
|
|
37
|
-
"md5": "^2.3.0",
|
|
38
|
-
"n3": "^1.10.0"
|
|
38
|
+
"md5": "^2.3.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@babel/core": "^7.14.3",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"@babel/preset-typescript": "^7.13.0",
|
|
46
46
|
"@microsoft/api-extractor": "^7.15.2",
|
|
47
47
|
"@noeldemartin/eslint-config-typescript": "^0.1.0-next.e83e5d37837b9845675534ab9d3745820b1326b4",
|
|
48
|
-
"@noeldemartin/scripts": "^0.1.1-next.
|
|
48
|
+
"@noeldemartin/scripts": "^0.1.1-next.7c3f1d44c5b0fe2a75160df634ca8cae4486d378",
|
|
49
49
|
"@rollup/plugin-alias": "^3.1.2",
|
|
50
50
|
"@rollup/plugin-babel": "^5.3.0",
|
|
51
51
|
"@rollup/plugin-commonjs": "^19.0.0",
|
|
@@ -54,9 +54,7 @@
|
|
|
54
54
|
"@types/chai": "^4.2.18",
|
|
55
55
|
"@types/jest": "^26.0.23",
|
|
56
56
|
"@types/jest-diff": "^24.3.0",
|
|
57
|
-
"@types/jsonld": "^1.5.6",
|
|
58
57
|
"@types/md5": "^2.3.0",
|
|
59
|
-
"@types/n3": "^1.8.0",
|
|
60
58
|
"babel-plugin-transform-remove-imports": "^1.5.4",
|
|
61
59
|
"eslint": "^7.26.0",
|
|
62
60
|
"jest": "^26.6.3",
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { JSError } from '@noeldemartin/utils';
|
|
2
|
+
import type { JSErrorOptions } from '@noeldemartin/utils';
|
|
3
|
+
|
|
4
|
+
export default class UnsupportedAuthorizationProtocolError extends JSError {
|
|
5
|
+
|
|
6
|
+
public readonly url: string;
|
|
7
|
+
public readonly protocol: string;
|
|
8
|
+
|
|
9
|
+
constructor(url: string, protocol: string, options?: JSErrorOptions) {
|
|
10
|
+
super(`The resource at ${url} is using an unsupported authorization protocol (${protocol})`, options);
|
|
11
|
+
|
|
12
|
+
this.url = url;
|
|
13
|
+
this.protocol = protocol;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
}
|
package/src/errors/index.ts
CHANGED
|
@@ -2,4 +2,5 @@ export { default as MalformedSolidDocumentError, SolidDocumentFormat } from './M
|
|
|
2
2
|
export { default as NetworkRequestError } from './NetworkRequestError';
|
|
3
3
|
export { default as NotFoundError } from './NotFoundError';
|
|
4
4
|
export { default as UnauthorizedError } from './UnauthorizedError';
|
|
5
|
-
export { default as
|
|
5
|
+
export { default as UnsuccessfulNetworkRequestError } from './UnsuccessfulNetworkRequestError';
|
|
6
|
+
export { default as UnsupportedAuthorizationProtocolError } from './UnsupportedAuthorizationProtocolError';
|
package/src/helpers/auth.ts
CHANGED
|
@@ -26,10 +26,16 @@ async function fetchExtendedUserProfile(webIdDocument: SolidDocument, fetch?: Fe
|
|
|
26
26
|
}> {
|
|
27
27
|
const store = new SolidStore(webIdDocument.getQuads());
|
|
28
28
|
const documents: Record<string, SolidDocument | false | null> = { [webIdDocument.url]: webIdDocument };
|
|
29
|
-
const addReferencedDocumentUrls = (document: SolidDocument) =>
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
const addReferencedDocumentUrls = (document: SolidDocument) => {
|
|
30
|
+
document
|
|
31
|
+
.statements(undefined, 'foaf:isPrimaryTopicOf')
|
|
32
|
+
.map(quad => quad.object.value)
|
|
33
|
+
.forEach(profileDocumentUrl => documents[profileDocumentUrl] = documents[profileDocumentUrl] ?? null);
|
|
34
|
+
document
|
|
35
|
+
.statements(undefined, 'foaf:primaryTopic')
|
|
36
|
+
.map(quad => quad.object.value)
|
|
37
|
+
.forEach(profileDocumentUrl => documents[profileDocumentUrl] = documents[profileDocumentUrl] ?? null);
|
|
38
|
+
};
|
|
33
39
|
const loadProfileDocuments = async (): Promise<void> => {
|
|
34
40
|
for (const [url, document] of Object.entries(documents)) {
|
|
35
41
|
if (document !== null) {
|
package/src/helpers/io.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import md5 from 'md5';
|
|
2
|
+
import {
|
|
3
|
+
TurtleParser,
|
|
4
|
+
TurtleWriter,
|
|
5
|
+
createBlankNode,
|
|
6
|
+
createQuad,
|
|
7
|
+
jsonLDFromRDF,
|
|
8
|
+
jsonLDToRDF,
|
|
9
|
+
} from '@noeldemartin/solid-utils-external';
|
|
2
10
|
import { arr, arrayFilter, arrayReplace, objectWithoutEmpty, stringMatchAll, tap } from '@noeldemartin/utils';
|
|
3
|
-
import { BlankNode as N3BlankNode, Quad as N3Quad, Parser as TurtleParser, Writer as TurtleWriter } from 'n3';
|
|
4
|
-
import { fromRDF, toRDF } from 'jsonld';
|
|
5
|
-
import type { JsonLdDocument } from 'jsonld';
|
|
6
11
|
import type { Quad } from 'rdf-js';
|
|
7
|
-
import type { Term
|
|
12
|
+
import type { JsonLdDocument, Term } from '@noeldemartin/solid-utils-external';
|
|
8
13
|
|
|
9
14
|
import SolidDocument from '@/models/SolidDocument';
|
|
10
15
|
|
|
@@ -84,25 +89,25 @@ function normalizeBlankNodes(quads: Quad[]): Quad[] {
|
|
|
84
89
|
|
|
85
90
|
for (const index of quadIndexes) {
|
|
86
91
|
const quad = normalizedQuads[index] as Quad;
|
|
87
|
-
const terms: Record<string,
|
|
88
|
-
subject: quad.subject as
|
|
89
|
-
object: quad.object as
|
|
92
|
+
const terms: Record<string, Term> = {
|
|
93
|
+
subject: quad.subject as Term,
|
|
94
|
+
object: quad.object as Term,
|
|
90
95
|
};
|
|
91
96
|
|
|
92
97
|
for (const [termName, termValue] of Object.entries(terms)) {
|
|
93
98
|
if (termValue.termType !== 'BlankNode' || termValue.value !== originalId)
|
|
94
99
|
continue;
|
|
95
100
|
|
|
96
|
-
terms[termName] =
|
|
101
|
+
terms[termName] = createBlankNode(normalizedId) as Term;
|
|
97
102
|
}
|
|
98
103
|
|
|
99
104
|
arrayReplace(
|
|
100
105
|
normalizedQuads,
|
|
101
106
|
quad,
|
|
102
|
-
|
|
103
|
-
terms.subject as
|
|
104
|
-
quad.predicate as
|
|
105
|
-
terms.object as
|
|
107
|
+
createQuad(
|
|
108
|
+
terms.subject as Term,
|
|
109
|
+
quad.predicate as Term,
|
|
110
|
+
terms.object as Term,
|
|
106
111
|
),
|
|
107
112
|
);
|
|
108
113
|
}
|
|
@@ -112,10 +117,15 @@ function normalizeBlankNodes(quads: Quad[]): Quad[] {
|
|
|
112
117
|
}
|
|
113
118
|
|
|
114
119
|
export interface ParsingOptions {
|
|
115
|
-
|
|
120
|
+
baseIRI: string;
|
|
116
121
|
normalizeBlankNodes: boolean;
|
|
117
122
|
}
|
|
118
123
|
|
|
124
|
+
export interface RDFGraphData {
|
|
125
|
+
quads: Quad[];
|
|
126
|
+
containsRelativeIRIs: boolean;
|
|
127
|
+
}
|
|
128
|
+
|
|
119
129
|
export async function createSolidDocument(url: string, body: string, fetch?: Fetch): Promise<SolidDocument> {
|
|
120
130
|
fetch = fetch ?? window.fetch.bind(window);
|
|
121
131
|
|
|
@@ -132,7 +142,7 @@ export async function createSolidDocument(url: string, body: string, fetch?: Fet
|
|
|
132
142
|
|
|
133
143
|
export async function fetchSolidDocument(url: string, fetch?: Fetch): Promise<SolidDocument> {
|
|
134
144
|
const { body: data, headers } = await fetchRawSolidDocument(url, fetch ?? window.fetch);
|
|
135
|
-
const statements = await turtleToQuads(data, {
|
|
145
|
+
const statements = await turtleToQuads(data, { baseIRI: url });
|
|
136
146
|
|
|
137
147
|
return new SolidDocument(url, statements, headers);
|
|
138
148
|
}
|
|
@@ -150,14 +160,14 @@ export async function fetchSolidDocumentIfFound(url: string, fetch?: Fetch): Pro
|
|
|
150
160
|
}
|
|
151
161
|
}
|
|
152
162
|
|
|
153
|
-
export async function jsonldToQuads(jsonld: JsonLD): Promise<Quad[]> {
|
|
163
|
+
export async function jsonldToQuads(jsonld: JsonLD, baseIRI?: string): Promise<Quad[]> {
|
|
154
164
|
if (isJsonLDGraph(jsonld)) {
|
|
155
|
-
const graphQuads = await Promise.all(jsonld['@graph'].map(jsonldToQuads));
|
|
165
|
+
const graphQuads = await Promise.all(jsonld['@graph'].map(resource => jsonldToQuads(resource, baseIRI)));
|
|
156
166
|
|
|
157
167
|
return graphQuads.flat();
|
|
158
168
|
}
|
|
159
169
|
|
|
160
|
-
return
|
|
170
|
+
return jsonLDToRDF(jsonld as JsonLdDocument, { base: baseIRI }) as Promise<Quad[]>;
|
|
161
171
|
}
|
|
162
172
|
|
|
163
173
|
export function normalizeSparql(sparql: string): string {
|
|
@@ -173,8 +183,54 @@ export function normalizeSparql(sparql: string): string {
|
|
|
173
183
|
.join(' ;\n');
|
|
174
184
|
}
|
|
175
185
|
|
|
186
|
+
export function parseTurtle(turtle: string, options: Partial<ParsingOptions> = {}): Promise<RDFGraphData> {
|
|
187
|
+
const parserOptions = objectWithoutEmpty({ baseIRI: options.baseIRI });
|
|
188
|
+
const parser = new TurtleParser(parserOptions);
|
|
189
|
+
const data: RDFGraphData = {
|
|
190
|
+
quads: [],
|
|
191
|
+
containsRelativeIRIs: false,
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
return new Promise((resolve, reject) => {
|
|
195
|
+
const resolveRelativeIRI = parser._resolveRelativeIRI;
|
|
196
|
+
|
|
197
|
+
parser._resolveRelativeIRI = (...args) => {
|
|
198
|
+
data.containsRelativeIRIs = true;
|
|
199
|
+
parser._resolveRelativeIRI = resolveRelativeIRI;
|
|
200
|
+
|
|
201
|
+
return parser._resolveRelativeIRI(...args);
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
parser.parse(turtle, (error, quad) => {
|
|
205
|
+
if (error) {
|
|
206
|
+
reject(
|
|
207
|
+
new MalformedSolidDocumentError(
|
|
208
|
+
options.baseIRI ?? null,
|
|
209
|
+
SolidDocumentFormat.Turtle,
|
|
210
|
+
error.message,
|
|
211
|
+
),
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (!quad) {
|
|
218
|
+
// data.quads = options.normalizeBlankNodes
|
|
219
|
+
// ? normalizeBlankNodes(data.quads)
|
|
220
|
+
// : data.quads;
|
|
221
|
+
|
|
222
|
+
resolve(data);
|
|
223
|
+
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
data.quads.push(quad);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
176
232
|
export async function quadsToJsonLD(quads: Quad[]): Promise<JsonLDGraph> {
|
|
177
|
-
const graph = await
|
|
233
|
+
const graph = await jsonLDFromRDF(quads);
|
|
178
234
|
|
|
179
235
|
return {
|
|
180
236
|
'@graph': graph as JsonLDResource[],
|
|
@@ -235,38 +291,13 @@ export function sparqlToQuadsSync(sparql: string, options: Partial<ParsingOption
|
|
|
235
291
|
}
|
|
236
292
|
|
|
237
293
|
export async function turtleToQuads(turtle: string, options: Partial<ParsingOptions> = {}): Promise<Quad[]> {
|
|
238
|
-
const
|
|
239
|
-
const parser = new TurtleParser(parserOptions);
|
|
240
|
-
const quads: Quad[] = [];
|
|
241
|
-
|
|
242
|
-
return new Promise((resolve, reject) => {
|
|
243
|
-
parser.parse(turtle, (error, quad) => {
|
|
244
|
-
if (error) {
|
|
245
|
-
reject(
|
|
246
|
-
new MalformedSolidDocumentError(
|
|
247
|
-
options.documentUrl ?? null,
|
|
248
|
-
SolidDocumentFormat.Turtle,
|
|
249
|
-
error.message,
|
|
250
|
-
),
|
|
251
|
-
);
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
if (!quad) {
|
|
256
|
-
options.normalizeBlankNodes
|
|
257
|
-
? resolve(normalizeBlankNodes(quads))
|
|
258
|
-
: resolve(quads);
|
|
294
|
+
const { quads } = await parseTurtle(turtle, options);
|
|
259
295
|
|
|
260
|
-
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
quads.push(quad);
|
|
264
|
-
});
|
|
265
|
-
});
|
|
296
|
+
return quads;
|
|
266
297
|
}
|
|
267
298
|
|
|
268
299
|
export function turtleToQuadsSync(turtle: string, options: Partial<ParsingOptions> = {}): Quad[] {
|
|
269
|
-
const parserOptions = objectWithoutEmpty({ baseIRI: options.
|
|
300
|
+
const parserOptions = objectWithoutEmpty({ baseIRI: options.baseIRI });
|
|
270
301
|
const parser = new TurtleParser(parserOptions);
|
|
271
302
|
|
|
272
303
|
try {
|
|
@@ -277,7 +308,7 @@ export function turtleToQuadsSync(turtle: string, options: Partial<ParsingOption
|
|
|
277
308
|
: quads;
|
|
278
309
|
} catch (error) {
|
|
279
310
|
throw new MalformedSolidDocumentError(
|
|
280
|
-
options.
|
|
311
|
+
options.baseIRI ?? null,
|
|
281
312
|
SolidDocumentFormat.Turtle,
|
|
282
313
|
(error as Error).message ?? '',
|
|
283
314
|
);
|
package/src/helpers/jsonld.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { compactJsonLD } from '@noeldemartin/solid-utils-external';
|
|
2
|
+
import type { JsonLdDocument } from '@noeldemartin/solid-utils-external';
|
|
3
|
+
|
|
1
4
|
export type JsonLD = Partial<{
|
|
2
5
|
'@context': Record<string, unknown>;
|
|
3
6
|
'@id': string;
|
|
@@ -5,7 +8,24 @@ export type JsonLD = Partial<{
|
|
|
5
8
|
}> & { [k: string]: unknown };
|
|
6
9
|
|
|
7
10
|
export type JsonLDResource = Omit<JsonLD, '@id'> & { '@id': string };
|
|
8
|
-
export type JsonLDGraph = {
|
|
11
|
+
export type JsonLDGraph = {
|
|
12
|
+
'@context'?: Record<string, unknown>;
|
|
13
|
+
'@graph': JsonLDResource[];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export async function compactJsonLDGraph(jsonld: JsonLDGraph): Promise<JsonLDGraph> {
|
|
17
|
+
const compactedJsonLD = await compactJsonLD(jsonld as JsonLdDocument, {});
|
|
18
|
+
|
|
19
|
+
if ('@graph' in compactedJsonLD) {
|
|
20
|
+
return compactedJsonLD as JsonLDGraph;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if ('@id' in compactedJsonLD) {
|
|
24
|
+
return { '@graph': [compactedJsonLD] } as JsonLDGraph;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return { '@graph': [] };
|
|
28
|
+
}
|
|
9
29
|
|
|
10
30
|
export function isJsonLDGraph(jsonld: JsonLD): jsonld is JsonLDGraph {
|
|
11
31
|
return '@graph' in jsonld;
|
package/src/helpers/wac.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { objectWithoutEmpty, requireUrlParentDirectory, urlResolve } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
|
+
import UnsupportedAuthorizationProtocolError from '@/errors/UnsupportedAuthorizationProtocolError';
|
|
3
4
|
import { fetchSolidDocumentIfFound } from '@/helpers/io';
|
|
4
5
|
import type SolidDocument from '@/models/SolidDocument';
|
|
5
6
|
import type { Fetch } from '@/helpers/io';
|
|
@@ -25,8 +26,17 @@ async function fetchEffectiveACL(
|
|
|
25
26
|
): Promise<SolidDocument> {
|
|
26
27
|
aclResourceUrl = aclResourceUrl ?? await fetchACLResourceUrl(resourceUrl, fetch);
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
const aclDocument = await fetchSolidDocumentIfFound(aclResourceUrl ?? '', fetch);
|
|
30
|
+
|
|
31
|
+
if (!aclDocument) {
|
|
32
|
+
return fetchEffectiveACL(requireUrlParentDirectory(resourceUrl), fetch);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (aclDocument.isACPResource()) {
|
|
36
|
+
throw new UnsupportedAuthorizationProtocolError(resourceUrl, 'ACP');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return aclDocument;
|
|
30
40
|
}
|
|
31
41
|
|
|
32
42
|
export async function fetchSolidDocumentACL(documentUrl: string, fetch: Fetch): Promise<{
|
|
@@ -24,6 +24,11 @@ export default class SolidDocument extends SolidStore {
|
|
|
24
24
|
this.headers = headers;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
public isACPResource(): boolean {
|
|
28
|
+
return !!this.headers.get('Link')
|
|
29
|
+
?.match(/<http:\/\/www\.w3\.org\/ns\/solid\/acp#AccessControlResource>;[^,]+rel="type"/);
|
|
30
|
+
}
|
|
31
|
+
|
|
27
32
|
public isPersonalProfile(): boolean {
|
|
28
33
|
return !!this.statement(
|
|
29
34
|
this.url,
|