/
RepresentationMetadata.ts
430 lines (384 loc) · 14.4 KB
/
RepresentationMetadata.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import { DataFactory, Store } from 'n3';
import type { BlankNode, DefaultGraph, Literal, NamedNode, Quad, Term } from 'rdf-js';
import { getLoggerFor } from '../../logging/LogUtil';
import { InternalServerError } from '../../util/errors/InternalServerError';
import { ContentType, parseContentType } from '../../util/HeaderUtil';
import { toNamedTerm, toObjectTerm, isTerm, toLiteral } from '../../util/TermUtil';
import { CONTENT_TYPE_TERM, CONTENT_LENGTH_TERM, XSD, SOLID_META, RDFS } from '../../util/Vocabularies';
import type { ResourceIdentifier } from './ResourceIdentifier';
import { isResourceIdentifier } from './ResourceIdentifier';
export type MetadataIdentifier = ResourceIdentifier | NamedNode | BlankNode;
export type MetadataValue = NamedNode | BlankNode | Literal | string | (NamedNode | Literal | BlankNode | string)[];
export type MetadataRecord = Record<string, MetadataValue>;
export type MetadataGraph = NamedNode | BlankNode | DefaultGraph | string;
/**
* Determines whether the object is a `RepresentationMetadata`.
*/
export function isRepresentationMetadata(object: any): object is RepresentationMetadata {
return typeof object?.setMetadata === 'function';
}
// Caches named node conversions
const cachedNamedNodes: Record<string, NamedNode> = {};
/**
* Converts the incoming name (URI or shorthand) to a named node.
* The generated terms get cached to reduce the number of created nodes,
* so only use this for internal constants!
* @param name - Predicate to potentially transform.
*/
function toCachedNamedNode(name: string): NamedNode {
if (!(name in cachedNamedNodes)) {
cachedNamedNodes[name] = DataFactory.namedNode(name);
}
return cachedNamedNodes[name];
}
/**
* Stores the metadata triples and provides methods for easy access.
* Most functions return the metadata object to allow for chaining.
*/
export class RepresentationMetadata {
protected readonly logger = getLoggerFor(this);
private store: Store;
private id: NamedNode | BlankNode;
/**
* @param identifier - Identifier of the resource relevant to this metadata.
* A blank node will be generated if none is provided.
* Strings will be converted to named nodes. @ignored
* @param overrides - Key/value map of extra values that need to be added to the metadata. @ignored
*
* `@ignored` tag is necessary for Components-Generator.js
*/
public constructor(identifier?: MetadataIdentifier, overrides?: MetadataRecord);
/**
* @param metadata - Starts as a copy of the input metadata.
* @param overrides - Key/value map of extra values that need to be added to the metadata.
* Will override values that were set by the input metadata.
*/
public constructor(metadata?: RepresentationMetadata, overrides?: MetadataRecord);
/**
* @param identifier - Identifier of the resource relevant to this metadata.
* @param contentType - Override for the content type of the representation.
*/
public constructor(identifier?: MetadataIdentifier, contentType?: string);
/**
* @param metadata - Starts as a copy of the input metadata.
* @param contentType - Override for the content type of the representation.
*/
public constructor(metadata?: RepresentationMetadata, contentType?: string);
/**
* @param contentType - The content type of the representation.
*/
public constructor(contentType?: string);
/**
* @param metadata - Metadata values (defaulting to content type if a string)
*/
public constructor(metadata?: RepresentationMetadata | MetadataRecord | string);
public constructor(
input?: MetadataIdentifier | RepresentationMetadata | MetadataRecord | string,
overrides?: MetadataRecord | string,
) {
this.store = new Store();
if (isResourceIdentifier(input)) {
this.id = DataFactory.namedNode(input.path);
} else if (isTerm(input)) {
this.id = input;
} else if (isRepresentationMetadata(input)) {
this.id = input.identifier;
this.addQuads(input.quads());
} else {
overrides = input;
this.id = this.store.createBlankNode();
}
if (overrides) {
if (typeof overrides === 'string') {
this.contentType = overrides;
} else {
this.setOverrides(overrides);
}
}
}
private setOverrides(overrides: Record<string, MetadataValue>): void {
for (const predicate of Object.keys(overrides)) {
const namedPredicate = toCachedNamedNode(predicate);
this.removeAll(namedPredicate);
let objects = overrides[predicate];
if (!Array.isArray(objects)) {
objects = [ objects ];
}
for (const object of objects) {
this.store.addQuad(this.id, namedPredicate, toObjectTerm(object, true));
}
}
}
/**
* @returns All matching metadata quads.
*/
public quads(
subject: NamedNode | BlankNode | string | null = null,
predicate: NamedNode | null = null,
object: NamedNode | BlankNode | Literal | string | null = null,
graph: MetadataGraph | null = null,
): Quad[] {
return this.store.getQuads(subject, predicate, object, graph);
}
/**
* Identifier of the resource this metadata is relevant to.
* Will update all relevant triples if this value gets changed.
*/
public get identifier(): NamedNode | BlankNode {
return this.id;
}
public set identifier(id: NamedNode | BlankNode) {
if (!id.equals(this.id)) {
// Convert all instances of the old identifier to the new identifier in the stored quads
const quads = this.quads().map((quad): Quad => {
if (quad.subject.equals(this.id)) {
return DataFactory.quad(id, quad.predicate, quad.object, quad.graph);
}
if (quad.object.equals(this.id)) {
return DataFactory.quad(quad.subject, quad.predicate, id, quad.graph);
}
return quad;
});
this.store = new Store(quads);
this.id = id;
}
}
/**
* Helper function to import all entries from the given metadata.
* If the new metadata has a different identifier the internal one will be updated.
* @param metadata - Metadata to import.
*/
public setMetadata(metadata: RepresentationMetadata): this {
this.identifier = metadata.identifier;
this.addQuads(metadata.quads());
return this;
}
/**
* @param subject - Subject of quad to add.
* @param predicate - Predicate of quad to add.
* @param object - Object of quad to add.
* @param graph - Optional graph of quad to add.
*/
public addQuad(
subject: NamedNode | BlankNode | string,
predicate: NamedNode,
object: NamedNode | BlankNode | Literal | string,
graph?: MetadataGraph,
): this {
this.store.addQuad(toNamedTerm(subject),
predicate,
toObjectTerm(object, true),
graph ? toNamedTerm(graph) : undefined);
return this;
}
/**
* @param quads - Quads to add to the metadata.
*/
public addQuads(quads: Quad[]): this {
this.store.addQuads(quads);
return this;
}
/**
* @param subject - Subject of quad to remove.
* @param predicate - Predicate of quad to remove.
* @param object - Object of quad to remove.
* @param graph - Optional graph of quad to remove.
*/
public removeQuad(
subject: NamedNode | BlankNode | string,
predicate: NamedNode,
object: NamedNode | BlankNode | Literal | string,
graph?: MetadataGraph,
): this {
const quads = this.quads(toNamedTerm(subject),
predicate,
toObjectTerm(object, true),
graph ? toNamedTerm(graph) : undefined);
return this.removeQuads(quads);
}
/**
* @param quads - Quads to remove from the metadata.
*/
public removeQuads(quads: Quad[]): this {
this.store.removeQuads(quads);
return this;
}
/**
* Adds a value linked to the identifier. Strings get converted to literals.
* @param predicate - Predicate linking identifier to value.
* @param object - Value(s) to add.
* @param graph - Optional graph of where to add the values to.
*/
public add(predicate: NamedNode, object: MetadataValue, graph?: MetadataGraph): this {
return this.forQuads(predicate, object, (pred, obj): any => this.addQuad(this.id, pred, obj, graph));
}
/**
* Removes the given value from the metadata. Strings get converted to literals.
* @param predicate - Predicate linking identifier to value.
* @param object - Value(s) to remove.
* @param graph - Optional graph of where to remove the values from.
*/
public remove(predicate: NamedNode, object: MetadataValue, graph?: MetadataGraph): this {
return this.forQuads(predicate, object, (pred, obj): any => this.removeQuad(this.id, pred, obj, graph));
}
/**
* Helper function to simplify add/remove
* Runs the given function on all predicate/object pairs, but only converts the predicate to a named node once.
*/
private forQuads(predicate: NamedNode, object: MetadataValue,
forFn: (pred: NamedNode, obj: NamedNode | BlankNode | Literal) => void): this {
const objects = Array.isArray(object) ? object : [ object ];
for (const obj of objects) {
forFn(predicate, toObjectTerm(obj, true));
}
return this;
}
/**
* Removes all values linked through the given predicate.
* @param predicate - Predicate to remove.
* @param graph - Optional graph where to remove from.
*/
public removeAll(predicate: NamedNode, graph?: MetadataGraph): this {
this.removeQuads(this.store.getQuads(this.id, predicate, null, graph ?? null));
return this;
}
/**
* Verifies if a specific triple can be found in the metadata.
* Undefined parameters are interpreted as wildcards.
*/
public has(
predicate: NamedNode | string | null = null,
object: NamedNode | BlankNode | Literal | string | null = null,
graph: MetadataGraph | null = null,
): boolean {
// This works with N3.js but at the time of writing the typings have not been updated yet.
// If you see this line of code check if the typings are already correct and update this if so.
return (this.store.has as any)(this.id, predicate, object, graph);
}
/**
* Finds all object values matching the given predicate and/or graph.
* @param predicate - Optional predicate to get the values for.
* @param graph - Optional graph where to get from.
*
* @returns An array with all matches.
*/
public getAll(predicate: NamedNode, graph?: MetadataGraph): Term[] {
return this.store.getQuads(this.id, predicate, null, graph ?? null)
.map((quad): Term => quad.object);
}
/**
* @param predicate - Predicate to get the value for.
* @param graph - Optional graph where the triple should be found.
*
* @throws Error
* If there are multiple matching values.
*
* @returns The corresponding value. Undefined if there is no match
*/
public get(predicate: NamedNode, graph?: MetadataGraph): Term | undefined {
const terms = this.getAll(predicate, graph);
if (terms.length === 0) {
return;
}
if (terms.length > 1) {
this.logger.error(`Multiple results for ${predicate.value}`);
throw new InternalServerError(
`Multiple results for ${predicate.value}`,
);
}
return terms[0];
}
/**
* Sets the value for the given predicate, removing all other instances.
* In case the object is undefined this is identical to `removeAll(predicate)`.
* @param predicate - Predicate linking to the value.
* @param object - Value(s) to set.
* @param graph - Optional graph where the triple should be stored.
*/
public set(predicate: NamedNode, object?: MetadataValue, graph?: MetadataGraph): this {
this.removeAll(predicate, graph);
if (object) {
this.add(predicate, object, graph);
}
return this;
}
private setContentType(input?: ContentType | string): void {
// Make sure complete Content-Type RDF structure is gone
this.removeContentType();
if (!input) {
return;
}
if (typeof input === 'string') {
input = parseContentType(input);
}
for (const [ key, value ] of Object.entries(input.parameters)) {
const node = DataFactory.blankNode();
this.addQuad(this.id, SOLID_META.terms.contentTypeParameter, node);
this.addQuad(node, RDFS.terms.label, key);
this.addQuad(node, SOLID_META.terms.value, value);
}
// Set base content type string
this.set(CONTENT_TYPE_TERM, input.value);
}
/**
* Parse the internal RDF structure to retrieve the Record with ContentType Parameters.
* @returns A {@link ContentType} object containing the value and optional parameters if there is one.
*/
private getContentType(): ContentType | undefined {
const value = this.get(CONTENT_TYPE_TERM)?.value;
if (!value) {
return;
}
const params = this.getAll(SOLID_META.terms.contentTypeParameter);
const parameters = Object.fromEntries(params.map((param): [string, string] => {
const labels = this.store.getObjects(param, RDFS.terms.label, null);
const values = this.store.getObjects(param, SOLID_META.terms.value, null);
if (labels.length !== 1 || values.length !== 1) {
this.logger.error(`Detected invalid content-type metadata for ${this.id.value}`);
return [ 'invalid', '' ];
}
return [ labels[0].value, values[0].value ];
}));
return new ContentType(value, parameters);
}
private removeContentType(): void {
this.removeAll(CONTENT_TYPE_TERM);
const params = this.quads(this.id, SOLID_META.terms.contentTypeParameter);
for (const quad of params) {
const paramEntries = this.quads(quad.object as BlankNode);
this.store.removeQuads(paramEntries);
}
this.store.removeQuads(params);
}
// Syntactic sugar for common predicates
/**
* Shorthand for the CONTENT_TYPE predicate.
*/
public get contentType(): string | undefined {
return this.get(CONTENT_TYPE_TERM)?.value;
}
public set contentType(input) {
this.setContentType(input);
}
/**
* Shorthand for the ContentType as an object (with parameters)
*/
public get contentTypeObject(): ContentType | undefined {
return this.getContentType();
}
public set contentTypeObject(contentType) {
this.setContentType(contentType);
}
/**
* Shorthand for the CONTENT_LENGTH predicate.
*/
public get contentLength(): number | undefined {
const length = this.get(CONTENT_LENGTH_TERM);
return length?.value ? Number(length.value) : undefined;
}
public set contentLength(input) {
if (input) {
this.set(CONTENT_LENGTH_TERM, toLiteral(input, XSD.terms.integer));
}
}
}